src/share/vm/c1/c1_ValueMap.cpp

Print this page
rev 4136 : 7153771: array bound check elimination for c1
Summary: when possible optimize out array bound checks, inserting predicates when needed.
Reviewed-by:


   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_Canonicalizer.hpp"
  27 #include "c1/c1_IR.hpp"
  28 #include "c1/c1_ValueMap.hpp"

  29 #include "utilities/bitMap.inline.hpp"
  30 
  31 
  32 #ifndef PRODUCT
  33 
  34   int ValueMap::_number_of_finds = 0;
  35   int ValueMap::_number_of_hits = 0;
  36   int ValueMap::_number_of_kills = 0;
  37 
  38   #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
  39 
  40 #else
  41 
  42   #define TRACE_VALUE_NUMBERING(code)
  43 
  44 #endif
  45 
  46 
  47 ValueMap::ValueMap()
  48   : _nesting(0)
  49   , _entries(ValueMapInitialSize, NULL)
  50   , _killed_values()
  51   , _entry_count(0)


 175       } else {                                                                           \
 176         prev_entry = entry;                                                              \
 177       }                                                                                  \
 178     }                                                                                    \
 179   }                                                                                      \
 180 
 181 #define MUST_KILL_MEMORY(must_kill, entry, value)                                        \
 182   bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
 183 
 184 #define MUST_KILL_ARRAY(must_kill, entry, value)                                         \
 185   bool must_kill = value->as_LoadIndexed() != NULL                                       \
 186                    && value->type()->tag() == type->tag();
 187 
 188 #define MUST_KILL_FIELD(must_kill, entry, value)                                         \
 189   /* ciField's are not unique; must compare their contents */                            \
 190   LoadField* lf = value->as_LoadField();                                                 \
 191   bool must_kill = lf != NULL                                                            \
 192                    && lf->field()->holder() == field->holder()                           \
 193                    && (all_offsets || lf->field()->offset() == field->offset());
 194 
 195 #define MUST_KILL_EXCEPTION(must_kill, entry, value)                                     \
 196   assert(entry->nesting() < nesting(), "must not find bigger nesting than current");     \
 197   bool must_kill = (entry->nesting() == nesting() - 1);
 198 
 199 
 200 void ValueMap::kill_memory() {
 201   GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
 202 }
 203 
 204 void ValueMap::kill_array(ValueType* type) {
 205   GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
 206 }
 207 
 208 void ValueMap::kill_field(ciField* field, bool all_offsets) {
 209   GENERIC_KILL_VALUE(MUST_KILL_FIELD);
 210 }
 211 
 212 void ValueMap::kill_exception() {
 213   GENERIC_KILL_VALUE(MUST_KILL_EXCEPTION);
 214 }
 215 
 216 
 217 void ValueMap::kill_map(ValueMap* map) {
 218   assert(is_global_value_numbering(), "only for global value numbering");
 219   _killed_values.set_union(&map->_killed_values);
 220 }
 221 
 222 void ValueMap::kill_all() {
 223   assert(is_local_value_numbering(), "only for local value numbering");
 224   for (int i = size() - 1; i >= 0; i--) {
 225     _entries.at_put(i, NULL);
 226   }
 227   _entry_count = 0;
 228 }
 229 
 230 
 231 #ifndef PRODUCT
 232 
 233 void ValueMap::print() {
 234   tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
 235 
 236   int entries = 0;


 257 }
 258 
 259 void ValueMap::print_statistics() {
 260   float hit_rate = 0;
 261   if (_number_of_finds != 0) {
 262     hit_rate = (float)_number_of_hits / _number_of_finds;
 263   }
 264 
 265   tty->print_cr("finds:%3d  hits:%3d   kills:%3d  hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
 266 }
 267 
 268 #endif
 269 
 270 
 271 
 272 class ShortLoopOptimizer : public ValueNumberingVisitor {
 273  private:
 274   GlobalValueNumbering* _gvn;
 275   BlockList             _loop_blocks;
 276   bool                  _too_complicated_loop;


 277 
 278   // simplified access to methods of GlobalValueNumbering
 279   ValueMap* current_map()                        { return _gvn->current_map(); }
 280   ValueMap* value_map_of(BlockBegin* block)      { return _gvn->value_map_of(block); }
 281 
 282   // implementation for abstract methods of ValueNumberingVisitor
 283   void      kill_memory()                                 { _too_complicated_loop = true; }
 284   void      kill_field(ciField* field, bool all_offsets)  { current_map()->kill_field(field, all_offsets); };
 285   void      kill_array(ValueType* type)                   { current_map()->kill_array(type); };








 286 
 287  public:
 288   ShortLoopOptimizer(GlobalValueNumbering* gvn)
 289     : _gvn(gvn)
 290     , _loop_blocks(ValueMapMaxLoopSize)
 291     , _too_complicated_loop(false)
 292   {














 293   }
 294 
 295   bool process(BlockBegin* loop_header);
 296 };
 297 
































































































































 298 
 299 bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
 300   TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
 301 
 302   _too_complicated_loop = false;
 303   _loop_blocks.clear();
 304   _loop_blocks.append(loop_header);
 305 
 306   for (int i = 0; i < _loop_blocks.length(); i++) {
 307     BlockBegin* block = _loop_blocks.at(i);
 308     TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
 309 
 310     if (block->is_set(BlockBegin::exception_entry_flag)) {
 311       // this would be too complicated
 312       return false;
 313     }
 314 
 315     // add predecessors to worklist
 316     for (int j = block->number_of_preds() - 1; j >= 0; j--) {
 317       BlockBegin* pred = block->pred_at(j);
 318 




 319       ValueMap* pred_map = value_map_of(pred);
 320       if (pred_map != NULL) {
 321         current_map()->kill_map(pred_map);
 322       } else if (!_loop_blocks.contains(pred)) {
 323         if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
 324           return false;
 325         }
 326         _loop_blocks.append(pred);
 327       }
 328     }
 329 
 330     // use the instruction visitor for killing values
 331     for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
 332       instr->visit(this);
 333       if (_too_complicated_loop) {
 334         return false;
 335       }
 336     }
 337   }
 338 






 339   TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
 340   return true;
 341 }
 342 
 343 
 344 GlobalValueNumbering::GlobalValueNumbering(IR* ir)
 345   : _current_map(NULL)
 346   , _value_maps(ir->linear_scan_order()->length(), NULL)

 347 {
 348   TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
 349 
 350   ShortLoopOptimizer short_loop_optimizer(this);
 351   int subst_count = 0;
 352 
 353   BlockList* blocks = ir->linear_scan_order();
 354   int num_blocks = blocks->length();
 355 
 356   BlockBegin* start_block = blocks->at(0);
 357   assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
 358   assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
 359 






 360   // initial, empty value map with nesting 0
 361   set_value_map_of(start_block, new ValueMap());
 362 
 363   for (int i = 1; i < num_blocks; i++) {
 364     BlockBegin* block = blocks->at(i);
 365     TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
 366 
 367     int num_preds = block->number_of_preds();
 368     assert(num_preds > 0, "block must have predecessors");
 369 
 370     BlockBegin* dominator = block->dominator();
 371     assert(dominator != NULL, "dominator must exist");
 372     assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
 373 
 374     // create new value map with increased nesting
 375     _current_map = new ValueMap(value_map_of(dominator));
 376 
 377     if (num_preds == 1) {
 378       assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
 379       // nothing to do here
 380 
 381     } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
 382       // block has incoming backward branches -> try to optimize short loops
 383       if (!short_loop_optimizer.process(block)) {
 384         // loop is too complicated, so kill all memory loads because there might be
 385         // stores to them in the loop
 386         current_map()->kill_memory();
 387       }
 388 
 389     } else {
 390       // only incoming forward branches that are already processed
 391       for (int j = 0; j < num_preds; j++) {
 392         BlockBegin* pred = block->pred_at(j);
 393         ValueMap* pred_map = value_map_of(pred);
 394 
 395         if (pred_map != NULL) {
 396           // propagate killed values of the predecessor to this block
 397           current_map()->kill_map(value_map_of(pred));
 398         } else {
 399           // kill all memory loads because predecessor not yet processed
 400           // (this can happen with non-natural loops and OSR-compiles)
 401           current_map()->kill_memory();
 402         }
 403       }
 404     }
 405 
 406     if (block->is_set(BlockBegin::exception_entry_flag)) {
 407       current_map()->kill_exception();
 408     }

 409 
 410     TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
 411 
 412     // visit all instructions of this block
 413     for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
 414       assert(!instr->has_subst(), "substitution already set");
 415 
 416       // check if instruction kills any values
 417       instr->visit(this);
 418 
 419       if (instr->hash() != 0) {
 420         Value f = current_map()->find_insert(instr);
 421         if (f != instr) {
 422           assert(!f->has_subst(), "can't have a substitution");
 423           instr->set_subst(f);
 424           subst_count++;
 425         }
 426       }
 427     }
 428 
 429     // remember value map for successors
 430     set_value_map_of(block, current_map());
 431   }
 432 
 433   if (subst_count != 0) {
 434     SubstitutionResolver resolver(ir);
 435   }
 436 
 437   TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());













 438 }


   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_Canonicalizer.hpp"
  27 #include "c1/c1_IR.hpp"
  28 #include "c1/c1_ValueMap.hpp"
  29 #include "c1/c1_ValueStack.hpp"
  30 #include "utilities/bitMap.inline.hpp"
  31 

  32 #ifndef PRODUCT
  33 
  34   int ValueMap::_number_of_finds = 0;
  35   int ValueMap::_number_of_hits = 0;
  36   int ValueMap::_number_of_kills = 0;
  37 
  38   #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
  39 
  40 #else
  41 
  42   #define TRACE_VALUE_NUMBERING(code)
  43 
  44 #endif
  45 
  46 
  47 ValueMap::ValueMap()
  48   : _nesting(0)
  49   , _entries(ValueMapInitialSize, NULL)
  50   , _killed_values()
  51   , _entry_count(0)


 175       } else {                                                                           \
 176         prev_entry = entry;                                                              \
 177       }                                                                                  \
 178     }                                                                                    \
 179   }                                                                                      \
 180 
 181 #define MUST_KILL_MEMORY(must_kill, entry, value)                                        \
 182   bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
 183 
 184 #define MUST_KILL_ARRAY(must_kill, entry, value)                                         \
 185   bool must_kill = value->as_LoadIndexed() != NULL                                       \
 186                    && value->type()->tag() == type->tag();
 187 
 188 #define MUST_KILL_FIELD(must_kill, entry, value)                                         \
 189   /* ciField's are not unique; must compare their contents */                            \
 190   LoadField* lf = value->as_LoadField();                                                 \
 191   bool must_kill = lf != NULL                                                            \
 192                    && lf->field()->holder() == field->holder()                           \
 193                    && (all_offsets || lf->field()->offset() == field->offset());
 194 




 195 
 196 void ValueMap::kill_memory() {
 197   GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
 198 }
 199 
 200 void ValueMap::kill_array(ValueType* type) {
 201   GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
 202 }
 203 
 204 void ValueMap::kill_field(ciField* field, bool all_offsets) {
 205   GENERIC_KILL_VALUE(MUST_KILL_FIELD);
 206 }
 207 





 208 void ValueMap::kill_map(ValueMap* map) {
 209   assert(is_global_value_numbering(), "only for global value numbering");
 210   _killed_values.set_union(&map->_killed_values);
 211 }
 212 
 213 void ValueMap::kill_all() {
 214   assert(is_local_value_numbering(), "only for local value numbering");
 215   for (int i = size() - 1; i >= 0; i--) {
 216     _entries.at_put(i, NULL);
 217   }
 218   _entry_count = 0;
 219 }
 220 
 221 
 222 #ifndef PRODUCT
 223 
 224 void ValueMap::print() {
 225   tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
 226 
 227   int entries = 0;


 248 }
 249 
 250 void ValueMap::print_statistics() {
 251   float hit_rate = 0;
 252   if (_number_of_finds != 0) {
 253     hit_rate = (float)_number_of_hits / _number_of_finds;
 254   }
 255 
 256   tty->print_cr("finds:%3d  hits:%3d   kills:%3d  hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
 257 }
 258 
 259 #endif
 260 
 261 
 262 
 263 class ShortLoopOptimizer : public ValueNumberingVisitor {
 264  private:
 265   GlobalValueNumbering* _gvn;
 266   BlockList             _loop_blocks;
 267   bool                  _too_complicated_loop;
 268   bool                  _has_field_store[T_ARRAY + 1];
 269   bool                  _has_indexed_store[T_ARRAY + 1];
 270 
 271   // simplified access to methods of GlobalValueNumbering
 272   ValueMap* current_map()                        { return _gvn->current_map(); }
 273   ValueMap* value_map_of(BlockBegin* block)      { return _gvn->value_map_of(block); }
 274 
 275   // implementation for abstract methods of ValueNumberingVisitor
 276   void      kill_memory()                                 { _too_complicated_loop = true; }
 277   void      kill_field(ciField* field, bool all_offsets)  {
 278     current_map()->kill_field(field, all_offsets);
 279     assert(field->type()->basic_type() >= 0 && field->type()->basic_type() <= T_ARRAY, "Invalid type");
 280     _has_field_store[field->type()->basic_type()] = true; 
 281   }
 282   void      kill_array(ValueType* type)                   {
 283     current_map()->kill_array(type); 
 284     BasicType basic_type = as_BasicType(type); assert(basic_type >= 0 && basic_type <= T_ARRAY, "Invalid type");
 285     _has_indexed_store[basic_type] = true;
 286   }
 287 
 288  public:
 289   ShortLoopOptimizer(GlobalValueNumbering* gvn)
 290     : _gvn(gvn)
 291     , _loop_blocks(ValueMapMaxLoopSize)
 292     , _too_complicated_loop(false)
 293   {
 294     for (int i=0; i<= T_ARRAY; i++){
 295       _has_field_store[i] = false;
 296       _has_indexed_store[i] = false;
 297     }
 298   }
 299 
 300   bool has_field_store(BasicType type) {
 301     assert(type >= 0 && type <= T_ARRAY, "Invalid type");
 302     return _has_field_store[type];
 303   }
 304 
 305   bool has_indexed_store(BasicType type) {
 306     assert(type >= 0 && type <= T_ARRAY, "Invalid type");
 307     return _has_indexed_store[type];
 308   }
 309 
 310   bool process(BlockBegin* loop_header);
 311 };
 312 
 313 class LoopInvariantCodeMotion : public StackObj  {
 314  private:
 315   GlobalValueNumbering* _gvn;
 316   ShortLoopOptimizer*   _short_loop_optimizer;
 317   Instruction*          _insertion_point;
 318   ValueStack *          _state;
 319 
 320   void set_invariant(Value v) const    { _gvn->set_processed(v); }
 321   bool is_invariant(Value v) const     { return _gvn->is_processed(v); }
 322 
 323   void process_block(BlockBegin* block);
 324 
 325  public:
 326   LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks);
 327 };
 328 
 329 LoopInvariantCodeMotion::LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks) 
 330   : _gvn(gvn), _short_loop_optimizer(slo)
 331 {
 332 
 333   TRACE_VALUE_NUMBERING(tty->print_cr("using loop invariant code motion loop_header = %d", loop_header->block_id()));
 334   TRACE_VALUE_NUMBERING(tty->print_cr("** loop invariant code motion for short loop B%d", loop_header->block_id()));
 335 
 336   BlockBegin* insertion_block = loop_header->dominator();
 337   if (insertion_block->number_of_preds() == 0) {
 338     return;  // only the entry block does not have a predecessor
 339   }
 340 
 341   assert(insertion_block->end()->as_Base() == NULL, "cannot insert into entry block");
 342   _insertion_point = insertion_block->end()->prev();
 343 
 344   BlockEnd *block_end = insertion_block->end();
 345   _state = block_end->state_before();
 346 
 347   if (!_state) {
 348     // If, TableSwitch and LookupSwitch always have state_before when
 349     // loop invariant code motion happens..
 350     assert(block_end->as_Goto(), "Block has to be goto");
 351     _state = block_end->state();
 352   }
 353 
 354   // the loop_blocks are filled by going backward from the loop header, so this processing order is best
 355   assert(loop_blocks->at(0) == loop_header, "loop header must be first loop block");
 356   process_block(loop_header);
 357   for (int i = loop_blocks->length() - 1; i >= 1; i--) {
 358     process_block(loop_blocks->at(i));
 359   }
 360 }
 361 
 362 void LoopInvariantCodeMotion::process_block(BlockBegin* block) {
 363 
 364   TRACE_VALUE_NUMBERING(tty->print_cr("processing block B%d", block->block_id()));
 365 
 366   Instruction* prev = block;
 367   Instruction* cur = block->next();
 368 
 369   while (cur != NULL) {
 370 
 371     // determine if cur instruction is loop invariant
 372     // only selected instruction types are processed here
 373     bool cur_invariant = false;
 374 
 375     if (cur->as_Constant() != NULL) {
 376 
 377       cur_invariant = !cur->can_trap();
 378 
 379     } else if (cur->as_ArithmeticOp() != NULL || cur->as_LogicOp() != NULL || cur->as_ShiftOp() != NULL) {
 380 
 381       assert(cur->as_Op2() != NULL, "must be Op2");
 382       Op2* op2 = (Op2*)cur;
 383       cur_invariant = !op2->can_trap() && is_invariant(op2->x()) && is_invariant(op2->y());
 384 
 385     } else if (cur->as_LoadField() != NULL) {
 386       
 387       LoadField* lf = (LoadField*)cur;
 388 
 389       // deoptimizes on NullPointerException
 390       cur_invariant = !lf->needs_patching() && !lf->field()->is_volatile() && !_short_loop_optimizer->has_field_store(lf->field()->type()->basic_type()) && is_invariant(lf->obj());
 391     } else if (cur->as_ArrayLength() != NULL) {
 392     
 393       ArrayLength *length = cur->as_ArrayLength();
 394       cur_invariant = is_invariant(length->array());
 395     } else if (cur->as_LoadIndexed() != NULL) {
 396        
 397       LoadIndexed *li = (LoadIndexed *)cur->as_LoadIndexed();
 398       cur_invariant = (!_short_loop_optimizer->has_indexed_store(li->elt_type())) && (is_invariant(li->array()) && is_invariant(li->index()));
 399     }
 400 
 401     if (cur_invariant) {
 402 
 403       // perform value numbering and mark instruction as loop-invariant
 404       _gvn->substitute(cur);
 405 
 406       if (cur->as_Constant() == NULL) {
 407         // ensure that code for non-constant instructions is always generated
 408         cur->pin();
 409       }
 410 
 411       // remove cur instruction from loop block and append it to block before loop
 412       Instruction* next = cur->next();
 413       
 414       Instruction* in = _insertion_point->next();
 415       _insertion_point = _insertion_point->set_next(cur);
 416       cur->set_next(in);
 417 
 418       //  Deoptimize on exception
 419       cur->set_flag(Instruction::DeoptimizeOnException, true);
 420 
 421       //  Clear exception handlers
 422       cur->set_exception_handlers(NULL);
 423 
 424       TRACE_VALUE_NUMBERING(tty->print_cr("Instruction %c%d is loop invariant", cur->type()->tchar(), cur->id()));
 425 
 426       if (cur->state_before() != NULL) {
 427         cur->set_state_before(_state->copy());
 428       }
 429       if (cur->exception_state() != NULL) {
 430         cur->set_exception_state(_state->copy());
 431       }
 432 
 433       cur = prev->set_next(next);
 434 
 435     } else {
 436       prev = cur;
 437       cur = cur->next();
 438     }
 439   }
 440 }
 441 
 442 bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
 443   TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
 444 
 445   _too_complicated_loop = false;
 446   _loop_blocks.clear();
 447   _loop_blocks.append(loop_header);
 448 
 449   for (int i = 0; i < _loop_blocks.length(); i++) {
 450     BlockBegin* block = _loop_blocks.at(i);
 451     TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
 452 
 453     if (block->is_set(BlockBegin::exception_entry_flag)) {
 454       // this would be too complicated
 455       return false;
 456     }
 457 
 458     // add predecessors to worklist
 459     for (int j = block->number_of_preds() - 1; j >= 0; j--) {
 460       BlockBegin* pred = block->pred_at(j);
 461 
 462       if (pred->is_set(BlockBegin::osr_entry_flag)) {
 463         return false;
 464       }
 465 
 466       ValueMap* pred_map = value_map_of(pred);
 467       if (pred_map != NULL) {
 468         current_map()->kill_map(pred_map);
 469       } else if (!_loop_blocks.contains(pred)) {
 470         if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
 471           return false;
 472         }
 473         _loop_blocks.append(pred);
 474       }
 475     }
 476 
 477     // use the instruction visitor for killing values
 478     for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
 479       instr->visit(this);
 480       if (_too_complicated_loop) {
 481         return false;
 482       }
 483     }
 484   }
 485 
 486   bool optimistic = this->_gvn->compilation()->is_optimistic();
 487 
 488   if (UseLoopInvariantCodeMotion && optimistic) {
 489     LoopInvariantCodeMotion code_motion(this, _gvn, loop_header, &_loop_blocks);
 490   }
 491 
 492   TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
 493   return true;
 494 }
 495 
 496 
 497 GlobalValueNumbering::GlobalValueNumbering(IR* ir)
 498   : _current_map(NULL)
 499   , _value_maps(ir->linear_scan_order()->length(), NULL)
 500   , _compilation(ir->compilation())
 501 {
 502   TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
 503 
 504   ShortLoopOptimizer short_loop_optimizer(this);

 505 
 506   BlockList* blocks = ir->linear_scan_order();
 507   int num_blocks = blocks->length();
 508 
 509   BlockBegin* start_block = blocks->at(0);
 510   assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
 511   assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
 512 
 513   // method parameters are not linked in instructions list, so process them separateley
 514   for_each_state_value(start_block->state(), value, 
 515      assert(value->as_Local() != NULL, "only method parameters allowed");
 516      set_processed(value);
 517   );
 518 
 519   // initial, empty value map with nesting 0
 520   set_value_map_of(start_block, new ValueMap());
 521 
 522   for (int i = 1; i < num_blocks; i++) {
 523     BlockBegin* block = blocks->at(i);
 524     TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
 525 
 526     int num_preds = block->number_of_preds();
 527     assert(num_preds > 0, "block must have predecessors");
 528 
 529     BlockBegin* dominator = block->dominator();
 530     assert(dominator != NULL, "dominator must exist");
 531     assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
 532 
 533     // create new value map with increased nesting
 534     _current_map = new ValueMap(value_map_of(dominator));
 535 
 536     if (num_preds == 1 && !block->is_set(BlockBegin::exception_entry_flag)) {
 537       assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
 538       // nothing to do here
 539 
 540     } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
 541       // block has incoming backward branches -> try to optimize short loops
 542       if (!short_loop_optimizer.process(block)) {
 543         // loop is too complicated, so kill all memory loads because there might be
 544         // stores to them in the loop
 545         current_map()->kill_memory();
 546       }
 547 
 548     } else {
 549       // only incoming forward branches that are already processed
 550       for (int j = 0; j < num_preds; j++) {
 551         BlockBegin* pred = block->pred_at(j);
 552         ValueMap* pred_map = value_map_of(pred);
 553 
 554         if (pred_map != NULL) {
 555           // propagate killed values of the predecessor to this block
 556           current_map()->kill_map(value_map_of(pred));
 557         } else {
 558           // kill all memory loads because predecessor not yet processed
 559           // (this can happen with non-natural loops and OSR-compiles)
 560           current_map()->kill_memory();
 561         }
 562       }
 563     }
 564 
 565     // phi functions are not linked in instructions list, so process them separateley
 566     for_each_phi_fun(block, phi, 
 567       set_processed(phi);
 568     );
 569 
 570     TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
 571 
 572     // visit all instructions of this block
 573     for (Value instr = block->next(); instr != NULL; instr = instr->next()) {


 574       // check if instruction kills any values
 575       instr->visit(this);
 576       // perform actual value numbering
 577       substitute(instr);







 578     }
 579 
 580     // remember value map for successors
 581     set_value_map_of(block, current_map());
 582   }
 583 
 584   if (_has_substitutions) {
 585     SubstitutionResolver resolver(ir);
 586   }
 587 
 588   TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
 589 }
 590 
 591 void GlobalValueNumbering::substitute(Instruction* instr) {
 592   assert(!instr->has_subst(), "substitution already set");
 593   Value subst = current_map()->find_insert(instr);
 594   if (subst != instr) {
 595     assert(!subst->has_subst(), "can't have a substitution");
 596 
 597     TRACE_VALUE_NUMBERING(tty->print_cr("substitution for %d set to %d", instr->id(), subst->id()));
 598     instr->set_subst(subst);
 599     _has_substitutions = true;
 600   }
 601   set_processed(instr);
 602 }