hotspot/src/share/vm/opto/postaloc.cpp
changeset 28648 102bdbb42723
parent 26913 9ad70cd32368
child 28723 0a36120cb225
equal deleted inserted replaced
28647:f44908f03772 28648:102bdbb42723
   261   // Skip through all copies to the _value_ being used.  Do not change from
   261   // Skip through all copies to the _value_ being used.  Do not change from
   262   // int to pointer.  This attempts to jump through a chain of copies, where
   262   // int to pointer.  This attempts to jump through a chain of copies, where
   263   // intermediate copies might be illegal, i.e., value is stored down to stack
   263   // intermediate copies might be illegal, i.e., value is stored down to stack
   264   // then reloaded BUT survives in a register the whole way.
   264   // then reloaded BUT survives in a register the whole way.
   265   Node *val = skip_copies(n->in(k));
   265   Node *val = skip_copies(n->in(k));
   266 
       
   267   if (val == x && nk_idx != 0 &&
       
   268       regnd[nk_reg] != NULL && regnd[nk_reg] != x &&
       
   269       _lrg_map.live_range_id(x) == _lrg_map.live_range_id(regnd[nk_reg])) {
       
   270     // When rematerialzing nodes and stretching lifetimes, the
       
   271     // allocator will reuse the original def for multidef LRG instead
       
   272     // of the current reaching def because it can't know it's safe to
       
   273     // do so.  After allocation completes if they are in the same LRG
       
   274     // then it should use the current reaching def instead.
       
   275     n->set_req(k, regnd[nk_reg]);
       
   276     blk_adjust += yank_if_dead(val, current_block, &value, &regnd);
       
   277     val = skip_copies(n->in(k));
       
   278   }
       
   279 
       
   280   if (val == x) return blk_adjust; // No progress?
   266   if (val == x) return blk_adjust; // No progress?
   281 
   267 
   282   int n_regs = RegMask::num_registers(val->ideal_reg());
   268   int n_regs = RegMask::num_registers(val->ideal_reg());
   283   uint val_idx = _lrg_map.live_range_id(val);
   269   uint val_idx = _lrg_map.live_range_id(val);
   284   OptoReg::Name val_reg = lrgs(val_idx).reg();
   270   OptoReg::Name val_reg = lrgs(val_idx).reg();
   380     return true;
   366     return true;
   381   }
   367   }
   382   return false;
   368   return false;
   383 }
   369 }
   384 
   370 
       
   371 // The algorithms works as follows:
       
   372 // We traverse the block top to bottom. possibly_merge_multidef() is invoked for every input edge k
       
   373 // of the instruction n. We check to see if the input is a multidef lrg. If it is, we record the fact that we've
       
   374 // seen a definition (coming as an input) and add that fact to the reg2defuse array. The array maps registers to their
       
   375 // current reaching definitions (we track only multidefs though). With each definition we also associate the first
       
   376 // instruction we saw use it. If we encounter the situation when we observe an def (an input) that is a part of the
       
   377 // same lrg but is different from the previous seen def we merge the two with a MachMerge node and substitute
       
   378 // all the uses that we've seen so far to use the merge. After that we keep replacing the new defs in the same lrg
       
   379 // as they get encountered with the merge node and keep adding these defs to the merge inputs.
       
   380 void PhaseChaitin::merge_multidefs() {
       
   381   Compile::TracePhase tp("mergeMultidefs", &timers[_t_mergeMultidefs]);
       
   382   ResourceMark rm;
       
   383   // Keep track of the defs seen in registers and collect their uses in the block.
       
   384   RegToDefUseMap reg2defuse(_max_reg, _max_reg, RegDefUse());
       
   385   for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
       
   386     Block* block = _cfg.get_block(i);
       
   387     for (uint j = 1; j < block->number_of_nodes(); j++) {
       
   388       Node* n = block->get_node(j);
       
   389       if (n->is_Phi()) continue;
       
   390       for (uint k = 1; k < n->req(); k++) {
       
   391         j += possibly_merge_multidef(n, k, block, reg2defuse);
       
   392       }
       
   393       // Null out the value produced by the instruction itself, since we're only interested in defs
       
   394       // implicitly defined by the uses. We are actually interested in tracking only redefinitions
       
   395       // of the multidef lrgs in the same register. For that matter it's enough to track changes in
       
   396       // the base register only and ignore other effects of multi-register lrgs and fat projections.
       
   397       // It is also ok to ignore defs coming from singledefs. After an implicit overwrite by one of
       
   398       // those our register is guaranteed to be used by another lrg and we won't attempt to merge it.
       
   399       uint lrg = _lrg_map.live_range_id(n);
       
   400       if (lrg > 0 && lrgs(lrg).is_multidef()) {
       
   401         OptoReg::Name reg = lrgs(lrg).reg();
       
   402         reg2defuse.at(reg).clear();
       
   403       }
       
   404     }
       
   405     // Clear reg->def->use tracking for the next block
       
   406     for (int j = 0; j < reg2defuse.length(); j++) {
       
   407       reg2defuse.at(j).clear();
       
   408     }
       
   409   }
       
   410 }
       
   411 
       
   412 int PhaseChaitin::possibly_merge_multidef(Node *n, uint k, Block *block, RegToDefUseMap& reg2defuse) {
       
   413   int blk_adjust = 0;
       
   414 
       
   415   uint lrg = _lrg_map.live_range_id(n->in(k));
       
   416   if (lrg > 0 && lrgs(lrg).is_multidef()) {
       
   417     OptoReg::Name reg = lrgs(lrg).reg();
       
   418 
       
   419     Node* def = reg2defuse.at(reg).def();
       
   420     if (def != NULL && lrg == _lrg_map.live_range_id(def) && def != n->in(k)) {
       
   421       // Same lrg but different node, we have to merge.
       
   422       MachMergeNode* merge;
       
   423       if (def->is_MachMerge()) { // is it already a merge?
       
   424         merge = def->as_MachMerge();
       
   425       } else {
       
   426         merge = new MachMergeNode(def);
       
   427 
       
   428         // Insert the merge node into the block before the first use.
       
   429         uint use_index = block->find_node(reg2defuse.at(reg).first_use());
       
   430         block->insert_node(merge, use_index++);
       
   431 
       
   432         // Let the allocator know about the new node, use the same lrg
       
   433         _lrg_map.extend(merge->_idx, lrg);
       
   434         blk_adjust++;
       
   435 
       
   436         // Fixup all the uses (there is at least one) that happened between the first
       
   437         // use and before the current one.
       
   438         for (; use_index < block->number_of_nodes(); use_index++) {
       
   439           Node* use = block->get_node(use_index);
       
   440           if (use == n) {
       
   441             break;
       
   442           }
       
   443           use->replace_edge(def, merge);
       
   444         }
       
   445       }
       
   446       if (merge->find_edge(n->in(k)) == -1) {
       
   447         merge->add_req(n->in(k));
       
   448       }
       
   449       n->set_req(k, merge);
       
   450     }
       
   451 
       
   452     // update the uses
       
   453     reg2defuse.at(reg).update(n->in(k), n);
       
   454   }
       
   455 
       
   456   return blk_adjust;
       
   457 }
       
   458 
   385 
   459 
   386 //------------------------------post_allocate_copy_removal---------------------
   460 //------------------------------post_allocate_copy_removal---------------------
   387 // Post-Allocation peephole copy removal.  We do this in 1 pass over the
   461 // Post-Allocation peephole copy removal.  We do this in 1 pass over the
   388 // basic blocks.  We maintain a mapping of registers to Nodes (an  array of
   462 // basic blocks.  We maintain a mapping of registers to Nodes (an  array of
   389 // Nodes indexed by machine register or stack slot number).  NULL means that a
   463 // Nodes indexed by machine register or stack slot number).  NULL means that a