src/share/vm/c1/c1_ValueMap.cpp

Wed, 31 Aug 2011 16:46:11 -0700

author
never
date
Wed, 31 Aug 2011 16:46:11 -0700
changeset 3099
c124e2e7463e
parent 2314
f95d63e2154a
child 3498
f067b4e0e04b
permissions
-rw-r--r--

7083786: dead various dead chunks of code
Reviewed-by: iveresov, kvn

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "c1/c1_Canonicalizer.hpp"
stefank@2314 27 #include "c1/c1_IR.hpp"
stefank@2314 28 #include "c1/c1_ValueMap.hpp"
stefank@2314 29 #include "utilities/bitMap.inline.hpp"
duke@435 30
duke@435 31
duke@435 32 #ifndef PRODUCT
duke@435 33
duke@435 34 int ValueMap::_number_of_finds = 0;
duke@435 35 int ValueMap::_number_of_hits = 0;
duke@435 36 int ValueMap::_number_of_kills = 0;
duke@435 37
duke@435 38 #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
duke@435 39
duke@435 40 #else
duke@435 41
duke@435 42 #define TRACE_VALUE_NUMBERING(code)
duke@435 43
duke@435 44 #endif
duke@435 45
duke@435 46
duke@435 47 ValueMap::ValueMap()
duke@435 48 : _nesting(0)
duke@435 49 , _entries(ValueMapInitialSize, NULL)
duke@435 50 , _killed_values()
duke@435 51 , _entry_count(0)
duke@435 52 {
duke@435 53 NOT_PRODUCT(reset_statistics());
duke@435 54 }
duke@435 55
duke@435 56
duke@435 57 ValueMap::ValueMap(ValueMap* old)
duke@435 58 : _nesting(old->_nesting + 1)
duke@435 59 , _entries(old->_entries.length())
duke@435 60 , _killed_values()
duke@435 61 , _entry_count(old->_entry_count)
duke@435 62 {
duke@435 63 for (int i = size() - 1; i >= 0; i--) {
duke@435 64 _entries.at_put(i, old->entry_at(i));
duke@435 65 }
duke@435 66 _killed_values.set_from(&old->_killed_values);
duke@435 67 }
duke@435 68
duke@435 69
duke@435 70 void ValueMap::increase_table_size() {
duke@435 71 int old_size = size();
duke@435 72 int new_size = old_size * 2 + 1;
duke@435 73
duke@435 74 ValueMapEntryList worklist(8);
duke@435 75 ValueMapEntryArray new_entries(new_size, NULL);
duke@435 76 int new_entry_count = 0;
duke@435 77
duke@435 78 TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));
duke@435 79
duke@435 80 for (int i = old_size - 1; i >= 0; i--) {
duke@435 81 ValueMapEntry* entry;
duke@435 82 for (entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 83 if (!is_killed(entry->value())) {
duke@435 84 worklist.push(entry);
duke@435 85 }
duke@435 86 }
duke@435 87
duke@435 88 while (!worklist.is_empty()) {
duke@435 89 entry = worklist.pop();
duke@435 90 int new_index = entry_index(entry->hash(), new_size);
duke@435 91
duke@435 92 if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {
duke@435 93 // changing entries with a lower nesting than the current nesting of the table
duke@435 94 // is not allowed because then the same entry is contained in multiple value maps.
duke@435 95 // clone entry when next-pointer must be changed
duke@435 96 entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);
duke@435 97 }
duke@435 98 entry->set_next(new_entries.at(new_index));
duke@435 99 new_entries.at_put(new_index, entry);
duke@435 100 new_entry_count++;
duke@435 101 }
duke@435 102 }
duke@435 103
duke@435 104 _entries = new_entries;
duke@435 105 _entry_count = new_entry_count;
duke@435 106 }
duke@435 107
duke@435 108
duke@435 109 Value ValueMap::find_insert(Value x) {
duke@435 110 const intx hash = x->hash();
duke@435 111 if (hash != 0) {
duke@435 112 // 0 hash means: exclude from value numbering
duke@435 113 NOT_PRODUCT(_number_of_finds++);
duke@435 114
duke@435 115 for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {
duke@435 116 if (entry->hash() == hash) {
duke@435 117 Value f = entry->value();
duke@435 118
duke@435 119 if (!is_killed(f) && f->is_equal(x)) {
duke@435 120 NOT_PRODUCT(_number_of_hits++);
duke@435 121 TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: %s %c%d equal to %c%d (size %d, entries %d, nesting-diff %d)", x->name(), x->type()->tchar(), x->id(), f->type()->tchar(), f->id(), size(), entry_count(), nesting() - entry->nesting()));
duke@435 122
duke@435 123 if (entry->nesting() != nesting() && f->as_Constant() == NULL) {
duke@435 124 // non-constant values of of another block must be pinned,
duke@435 125 // otherwise it is possible that they are not evaluated
duke@435 126 f->pin(Instruction::PinGlobalValueNumbering);
duke@435 127 }
duke@435 128
duke@435 129 return f;
duke@435 130
duke@435 131 }
duke@435 132 }
duke@435 133 }
duke@435 134
duke@435 135 // x not found, so insert it
duke@435 136 if (entry_count() >= size_threshold()) {
duke@435 137 increase_table_size();
duke@435 138 }
duke@435 139 int idx = entry_index(hash, size());
duke@435 140 _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));
duke@435 141 _entry_count++;
duke@435 142
duke@435 143 TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: insert %s %c%d (size %d, entries %d, nesting %d)", x->name(), x->type()->tchar(), x->id(), size(), entry_count(), nesting()));
duke@435 144 }
duke@435 145
duke@435 146 return x;
duke@435 147 }
duke@435 148
duke@435 149
duke@435 150 #define GENERIC_KILL_VALUE(must_kill_implementation) \
duke@435 151 NOT_PRODUCT(_number_of_kills++); \
duke@435 152 \
duke@435 153 for (int i = size() - 1; i >= 0; i--) { \
duke@435 154 ValueMapEntry* prev_entry = NULL; \
duke@435 155 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { \
duke@435 156 Value value = entry->value(); \
duke@435 157 \
duke@435 158 must_kill_implementation(must_kill, entry, value) \
duke@435 159 \
duke@435 160 if (must_kill) { \
duke@435 161 kill_value(value); \
duke@435 162 \
duke@435 163 if (prev_entry == NULL) { \
duke@435 164 _entries.at_put(i, entry->next()); \
duke@435 165 _entry_count--; \
duke@435 166 } else if (prev_entry->nesting() == nesting()) { \
duke@435 167 prev_entry->set_next(entry->next()); \
duke@435 168 _entry_count--; \
duke@435 169 } else { \
duke@435 170 prev_entry = entry; \
duke@435 171 } \
duke@435 172 \
duke@435 173 TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: killed %s %c%d (size %d, entries %d, nesting-diff %d)", value->name(), value->type()->tchar(), value->id(), size(), entry_count(), nesting() - entry->nesting())); \
duke@435 174 } else { \
duke@435 175 prev_entry = entry; \
duke@435 176 } \
duke@435 177 } \
duke@435 178 } \
duke@435 179
duke@435 180 #define MUST_KILL_MEMORY(must_kill, entry, value) \
duke@435 181 bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
duke@435 182
duke@435 183 #define MUST_KILL_ARRAY(must_kill, entry, value) \
duke@435 184 bool must_kill = value->as_LoadIndexed() != NULL \
duke@435 185 && value->type()->tag() == type->tag();
duke@435 186
duke@435 187 #define MUST_KILL_FIELD(must_kill, entry, value) \
duke@435 188 /* ciField's are not unique; must compare their contents */ \
duke@435 189 LoadField* lf = value->as_LoadField(); \
duke@435 190 bool must_kill = lf != NULL \
duke@435 191 && lf->field()->holder() == field->holder() \
duke@435 192 && lf->field()->offset() == field->offset();
duke@435 193
duke@435 194 #define MUST_KILL_EXCEPTION(must_kill, entry, value) \
duke@435 195 assert(entry->nesting() < nesting(), "must not find bigger nesting than current"); \
duke@435 196 bool must_kill = (entry->nesting() == nesting() - 1);
duke@435 197
duke@435 198
duke@435 199 void ValueMap::kill_memory() {
duke@435 200 GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
duke@435 201 }
duke@435 202
duke@435 203 void ValueMap::kill_array(ValueType* type) {
duke@435 204 GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
duke@435 205 }
duke@435 206
duke@435 207 void ValueMap::kill_field(ciField* field) {
duke@435 208 GENERIC_KILL_VALUE(MUST_KILL_FIELD);
duke@435 209 }
duke@435 210
duke@435 211 void ValueMap::kill_exception() {
duke@435 212 GENERIC_KILL_VALUE(MUST_KILL_EXCEPTION);
duke@435 213 }
duke@435 214
duke@435 215
duke@435 216 void ValueMap::kill_map(ValueMap* map) {
duke@435 217 assert(is_global_value_numbering(), "only for global value numbering");
duke@435 218 _killed_values.set_union(&map->_killed_values);
duke@435 219 }
duke@435 220
duke@435 221 void ValueMap::kill_all() {
duke@435 222 assert(is_local_value_numbering(), "only for local value numbering");
duke@435 223 for (int i = size() - 1; i >= 0; i--) {
duke@435 224 _entries.at_put(i, NULL);
duke@435 225 }
duke@435 226 _entry_count = 0;
duke@435 227 }
duke@435 228
duke@435 229
duke@435 230 #ifndef PRODUCT
duke@435 231
duke@435 232 void ValueMap::print() {
duke@435 233 tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
duke@435 234
duke@435 235 int entries = 0;
duke@435 236 for (int i = 0; i < size(); i++) {
duke@435 237 if (entry_at(i) != NULL) {
duke@435 238 tty->print(" %2d: ", i);
duke@435 239 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 240 Value value = entry->value();
duke@435 241 tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());
duke@435 242 entries++;
duke@435 243 }
duke@435 244 tty->print_cr("NULL");
duke@435 245 }
duke@435 246 }
duke@435 247
duke@435 248 _killed_values.print();
duke@435 249 assert(entry_count() == entries, "entry_count incorrect");
duke@435 250 }
duke@435 251
duke@435 252 void ValueMap::reset_statistics() {
duke@435 253 _number_of_finds = 0;
duke@435 254 _number_of_hits = 0;
duke@435 255 _number_of_kills = 0;
duke@435 256 }
duke@435 257
duke@435 258 void ValueMap::print_statistics() {
duke@435 259 float hit_rate = 0;
duke@435 260 if (_number_of_finds != 0) {
duke@435 261 hit_rate = (float)_number_of_hits / _number_of_finds;
duke@435 262 }
duke@435 263
duke@435 264 tty->print_cr("finds:%3d hits:%3d kills:%3d hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
duke@435 265 }
duke@435 266
duke@435 267 #endif
duke@435 268
duke@435 269
duke@435 270
duke@435 271 class ShortLoopOptimizer : public ValueNumberingVisitor {
duke@435 272 private:
duke@435 273 GlobalValueNumbering* _gvn;
duke@435 274 BlockList _loop_blocks;
duke@435 275 bool _too_complicated_loop;
duke@435 276
duke@435 277 // simplified access to methods of GlobalValueNumbering
duke@435 278 ValueMap* current_map() { return _gvn->current_map(); }
duke@435 279 ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); }
duke@435 280
duke@435 281 // implementation for abstract methods of ValueNumberingVisitor
duke@435 282 void kill_memory() { _too_complicated_loop = true; }
duke@435 283 void kill_field(ciField* field) { current_map()->kill_field(field); };
duke@435 284 void kill_array(ValueType* type) { current_map()->kill_array(type); };
duke@435 285
duke@435 286 public:
duke@435 287 ShortLoopOptimizer(GlobalValueNumbering* gvn)
duke@435 288 : _gvn(gvn)
duke@435 289 , _loop_blocks(ValueMapMaxLoopSize)
duke@435 290 , _too_complicated_loop(false)
duke@435 291 {
duke@435 292 }
duke@435 293
duke@435 294 bool process(BlockBegin* loop_header);
duke@435 295 };
duke@435 296
duke@435 297
duke@435 298 bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
duke@435 299 TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
duke@435 300
duke@435 301 _too_complicated_loop = false;
duke@435 302 _loop_blocks.clear();
duke@435 303 _loop_blocks.append(loop_header);
duke@435 304
duke@435 305 for (int i = 0; i < _loop_blocks.length(); i++) {
duke@435 306 BlockBegin* block = _loop_blocks.at(i);
duke@435 307 TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
duke@435 308
duke@435 309 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 310 // this would be too complicated
duke@435 311 return false;
duke@435 312 }
duke@435 313
duke@435 314 // add predecessors to worklist
duke@435 315 for (int j = block->number_of_preds() - 1; j >= 0; j--) {
duke@435 316 BlockBegin* pred = block->pred_at(j);
duke@435 317
duke@435 318 ValueMap* pred_map = value_map_of(pred);
duke@435 319 if (pred_map != NULL) {
duke@435 320 current_map()->kill_map(pred_map);
duke@435 321 } else if (!_loop_blocks.contains(pred)) {
duke@435 322 if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
duke@435 323 return false;
duke@435 324 }
duke@435 325 _loop_blocks.append(pred);
duke@435 326 }
duke@435 327 }
duke@435 328
duke@435 329 // use the instruction visitor for killing values
duke@435 330 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 331 instr->visit(this);
duke@435 332 if (_too_complicated_loop) {
duke@435 333 return false;
duke@435 334 }
duke@435 335 }
duke@435 336 }
duke@435 337
duke@435 338 TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
duke@435 339 return true;
duke@435 340 }
duke@435 341
duke@435 342
duke@435 343 GlobalValueNumbering::GlobalValueNumbering(IR* ir)
duke@435 344 : _current_map(NULL)
duke@435 345 , _value_maps(ir->linear_scan_order()->length(), NULL)
duke@435 346 {
duke@435 347 TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
duke@435 348
duke@435 349 ShortLoopOptimizer short_loop_optimizer(this);
duke@435 350 int subst_count = 0;
duke@435 351
duke@435 352 BlockList* blocks = ir->linear_scan_order();
duke@435 353 int num_blocks = blocks->length();
duke@435 354
duke@435 355 BlockBegin* start_block = blocks->at(0);
duke@435 356 assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
duke@435 357 assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
duke@435 358
duke@435 359 // initial, empty value map with nesting 0
duke@435 360 set_value_map_of(start_block, new ValueMap());
duke@435 361
duke@435 362 for (int i = 1; i < num_blocks; i++) {
duke@435 363 BlockBegin* block = blocks->at(i);
duke@435 364 TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
duke@435 365
duke@435 366 int num_preds = block->number_of_preds();
duke@435 367 assert(num_preds > 0, "block must have predecessors");
duke@435 368
duke@435 369 BlockBegin* dominator = block->dominator();
duke@435 370 assert(dominator != NULL, "dominator must exist");
duke@435 371 assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
duke@435 372
duke@435 373 // create new value map with increased nesting
duke@435 374 _current_map = new ValueMap(value_map_of(dominator));
duke@435 375
duke@435 376 if (num_preds == 1) {
duke@435 377 assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
duke@435 378 // nothing to do here
duke@435 379
duke@435 380 } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
duke@435 381 // block has incoming backward branches -> try to optimize short loops
duke@435 382 if (!short_loop_optimizer.process(block)) {
duke@435 383 // loop is too complicated, so kill all memory loads because there might be
duke@435 384 // stores to them in the loop
duke@435 385 current_map()->kill_memory();
duke@435 386 }
duke@435 387
duke@435 388 } else {
duke@435 389 // only incoming forward branches that are already processed
duke@435 390 for (int j = 0; j < num_preds; j++) {
duke@435 391 BlockBegin* pred = block->pred_at(j);
duke@435 392 ValueMap* pred_map = value_map_of(pred);
duke@435 393
duke@435 394 if (pred_map != NULL) {
duke@435 395 // propagate killed values of the predecessor to this block
duke@435 396 current_map()->kill_map(value_map_of(pred));
duke@435 397 } else {
duke@435 398 // kill all memory loads because predecessor not yet processed
duke@435 399 // (this can happen with non-natural loops and OSR-compiles)
duke@435 400 current_map()->kill_memory();
duke@435 401 }
duke@435 402 }
duke@435 403 }
duke@435 404
duke@435 405 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 406 current_map()->kill_exception();
duke@435 407 }
duke@435 408
duke@435 409 TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
duke@435 410
duke@435 411 // visit all instructions of this block
duke@435 412 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 413 assert(!instr->has_subst(), "substitution already set");
duke@435 414
duke@435 415 // check if instruction kills any values
duke@435 416 instr->visit(this);
duke@435 417
duke@435 418 if (instr->hash() != 0) {
duke@435 419 Value f = current_map()->find_insert(instr);
duke@435 420 if (f != instr) {
duke@435 421 assert(!f->has_subst(), "can't have a substitution");
duke@435 422 instr->set_subst(f);
duke@435 423 subst_count++;
duke@435 424 }
duke@435 425 }
duke@435 426 }
duke@435 427
duke@435 428 // remember value map for successors
duke@435 429 set_value_map_of(block, current_map());
duke@435 430 }
duke@435 431
duke@435 432 if (subst_count != 0) {
duke@435 433 SubstitutionResolver resolver(ir);
duke@435 434 }
duke@435 435
duke@435 436 TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
duke@435 437 }

mercurial