postaloc.cpp revision 3851
846N/A * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. 846N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 846N/A * This code is free software; you can redistribute it and/or modify it 846N/A * under the terms of the GNU General Public License version 2 only, as 846N/A * published by the Free Software Foundation. 846N/A * This code is distributed in the hope that it will be useful, but WITHOUT 846N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 846N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 846N/A * version 2 for more details (a copy is included in the LICENSE file that 846N/A * accompanied this code). 846N/A * You should have received a copy of the GNU General Public License version 846N/A * 2 along with this work; if not, write to the Free Software Foundation, 846N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 846N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 846N/A// See if this register (or pairs, or vector) already contains the value. 846N/A//---------------------------may_be_copy_of_callee----------------------------- 846N/A// Check to see if we can possibly be a copy of a callee-save value. 846N/A // Short circuit if there are no callee save registers 846N/A // Expect only a spill-down and reload on exit for callee-save spills. 846N/A // Chains of copies cannot be deep. 846N/A // 5008997 - This is wishful thinking. Register allocator seems to 846N/A // be splitting live ranges for callee save registers to such 846N/A // an extent that in large methods the chains can be very long 846N/A // (50+). The conservative answer is to return true if we don't 846N/A // know as this prevents optimizations from occurring. 846N/A return true;
// Direct use of callee-save proj 846N/A // If we reached the end and didn't find a callee save proj 846N/A // then this may be a callee save proj so we return true 846N/A // as the conservative answer. If we didn't reach then end 846N/A // we must have discovered that it was not a callee save 846N/A // else we would have returned. 846N/A//------------------------------yank----------------------------------- 846N/A// Helper function for yank_if_dead 846N/A // Count 1 if deleting an instruction from the current block 846N/A // This code is expected only next original nodes: 846N/A // - load from constant table node which may have next data input nodes: 846N/A // MachConstantBase, Phi, MachTemp, MachSpillCopy 846N/A // - load constant node which may have next data input nodes: 846N/A // MachTemp, MachSpillCopy 846N/A // - MachProj and Copy dead nodes 846N/A//------------------------------yank_if_dead----------------------------------- 846N/A// Removed edges from 'old'. Yank if dead. Return adjustment counts to 846N/A// iterators in the current block. 846N/A // Disconnect control and remove precedence edges if any exist 846N/A//------------------------------use_prior_register----------------------------- 846N/A// Use the prior value instead of the current value, in an effort to make 846N/A// the current value go dead. Return block iterator adjustment, in case 846N/A// we yank some instructions from this block. 846N/A // Def is currently dead and can be removed? Do not resurrect 846N/A // Not every pair of physical registers are assignment compatible, 846N/A // e.g. on sparc floating point registers are not assignable to integer 846N/A // Check for a copy to or from a misaligned pair. 846N/A // It is workaround for a sparc with misaligned pairs. 846N/A // Capture the old def in case it goes dead... 846N/A // Save-on-call copies can only be elided if the entire copy chain can go 846N/A // away, lest we get the same callee-save value alive in 2 locations at 846N/A // once. We check for the obvious trivial case here. Although it can 846N/A // sometimes be elided with cooperation outside our scope, here we will just 846N/A // miss the opportunity. :-( 846N/A if( !
idx2 )
return 0;
// Not a chain of 2 copies 846N/A // Is old def now dead? We successfully yanked a copy? 846N/A//------------------------------skip_copies------------------------------------ 846N/A// Skip through any number of copies (that don't mod oop-i-ness) 846N/A break;
// casting copy, not the same value 846N/A//------------------------------elide_copy------------------------------------- 846N/A// Remove (bypass) copies along Node n, edge k. // Remove obvious same-register copies if( n->
in(k) !=
copy )
break;
// Failed for some cutout? x =
copy;
// Progress, try again // Phis and 2-address instructions cannot change registers so easily - their // outputs must match their input. // Loop backedges won't have a value-mapping yet // Skip through all copies to the _value_ being used. Do not change from // int to pointer. This attempts to jump through a chain of copies, where // intermediate copies might be illegal, i.e., value is stored down to stack // then reloaded BUT survives in a register the whole way. // When rematerialzing nodes and stretching lifetimes, the // allocator will reuse the original def for multidef LRG instead // of the current reaching def because it can't know it's safe to // do so. After allocation completes if they are in the same LRG // then it should use the current reaching def instead. // See if it happens to already be in the correct register! // (either Phi's direct register, or the common case of the name // never-clobbered original-def register) // See if we can skip the copy by changing registers. Don't change from // using a register to using the stack unless we know we can remove a // copy-load. Otherwise we might end up making a pile of Intel cisc-spill // ops reading from memory instead of just loading once and using the // Also handle duplicate copies here. // Scan all registers to see if this value is around already // Found ourselves so check if there is only one user of this // copy and keep on searching for a better copy if so. if (
n_regs >
1) {
// Doubles and vectors check for aligned-adjacent set if ((
reg&
last) !=
last)
continue;
// Wrong part of a set if(
vv ==
val ||
// Got a direct hit? assert( !n->
is_Phi(),
"cannot change registers at a Phi so easily" );
regnd[
reg]->
outcnt()==
1 ) {
// last use of a spill-load turns into a CISC use if( n->
in(k) ==
regnd[
reg] )
// Success! Quit trying }
// End of if not degrading to a stack }
// End of if found value in another register }
// End of scan all machine registers// Check if nreg already contains the constant value val. Normal copy // elimination doesn't doesn't work on constants because multiple // nodes can represent the same constant so the type and rule of the // MachNode must be checked to ensure equivalence. // This code assumes that two MachNodes representing constants // which have the same rule and the same bottom type will produce // identical effects into a register. This seems like it must be // objectively true unless there are hidden inputs to the nodes // but if that were to change this code would need to updated. // Since they are equivalent the second one if redundant and can // n will be replaced with the old value but n might have // kills projections associated with it so remove them now so that // yank_if_dead will be able to eliminate the copy once the uses // have been transferred to the old[value]. // Kill projections have no users and one input //------------------------------post_allocate_copy_removal--------------------- // Post-Allocation peephole copy removal. We do this in 1 pass over the // basic blocks. We maintain a mapping of registers to Nodes (an array of // Nodes indexed by machine register or stack slot number). NULL means that a // register is not mapped to any Node. We can (want to have!) have several // registers map to the same Node. We walk forward over the instructions // updating the mapping as we go. At merge points we force a NULL if we have // to merge 2 different Nodes into the same register. Phi functions will give // us a new Node if there is a proper value merging. Since the blocks are // arranged in some RPO, we will visit all parent blocks before visiting any // successor blocks (except at loops). // If we find a Copy we look to see if the Copy's source register is a stack // slot and that value has already been loaded into some machine register; if // so we use machine register directly. This turns a Load into a reg-reg // Move. We also look for reloads of identical constants. // When we see a use from a reg-reg Copy, we will attempt to use the copy's // source directly and make the copy go dead. // Need a mapping from basic block Node_Lists. We need a Node_List to // map from register number to value-producing Node. // Need a mapping from basic block Node_Lists. We need a Node_List to // map from register number to register-defining Node. // We keep unused Node_Lists on a free_list to avoid wasting // Count of Phis in block // If any predecessor has not been visited, we do not know the state // of registers at the start. Check for this, while updating copies // Remove copies along phi edges // See if this predecessor's mappings have been used by everybody // who wants them. If so, free 'em. break;
// Found a future user freed =
pb;
// Record last block freed }
else {
// This block has unvisited (loopback) inputs // Extract Node_List mappings. If 'freed' is non-zero, we just popped // 'freed's blocks off the list // Set mappings as OUR mappings // Initialize value & regnd for this block // Some predecessor has not yet been visited; zap map to empty if( !
freed ) {
// Didn't get a freebie prior block // Merge all inputs together, setting to NULL any conflicts. if(
pb ==
freed )
continue;
// Did self already via freelist if(
regnd[k] !=
p_regnd[k] ) {
// Conflict on reaching defs? // Remove copies remaining on edges. Check for junk phi. for( k=
1; k<
phi->
req(); k++ ) {
if(
phi != x && u != x )
// Found a different input u = u ?
NodeSentinel : x;
// Capture unique input, or NodeSentinel for 2nd input // Note that if value[pidx] exists, then we merged no new values here // and the phi is useless. This can happen even with the above phi // removal for complex flows. I cannot keep the better known value here // because locally the phi appears to define a new merged value. If I // keep the better value then a copy of the phi, being unable to use the // global flow analysis, can't "peek through" the phi to the original // reaching value and so will act like it's defining a new value. This // can lead to situations where some uses are from the old and some from // the new values. Not illegal by itself but throws the over-strong for (
int l =
1; l <
n_regs; l++) {
// For all remaining instructions if( n->
outcnt() == 0 &&
// Dead? n != C->
top() &&
// (ignore TOP, it has no du info) !n->
is_Proj() ) {
// fat-proj kills // Improve reaching-def info. Occasionally post-alloc's liveness gives // up (at loop backedges, because we aren't doing a full flow pass). // The presence of a live use essentially asserts that the use's def is // alive and well at the use (or else the allocator fubar'd). Take // advantage of this info to set a reaching def for the use-reg. for( k =
1; k < n->
req(); k++ ) {
Node *
def = n->
in(k);
// n->in(k) is a USE; def is the DEF for this USE int idx;
// Skip occasional useless copy // Record other half of doubles for (
int l =
1; l <
n_regs; l++) {
// Remove copies along input edges for( k =
1; k < n->
req(); k++ )
// Unallocated Nodes define no registers // Update the register defined by this instruction // Skip through all copies to the _value_ being defined. // Do not change from int to pointer // Clear out a dead definition before starting so that the // elimination code doesn't have to guard against it. The // definition could in fact be a kill projection with a count of // 0 which is safe but since those are uninteresting for copy // elimination just delete them as well. // If Node 'n' does not change the value mapped by the register, // then 'n' is a useless copy. Do not update the register->node // mapping so 'n' will go dead. // Update the mapping: record new Node defined by the register // Update mapping for defined *value*, which is the defined // Node after skipping all copies. // If Node 'n' does not change the value mapped by the register, // then 'n' is a useless copy. Do not update the register->node // mapping so 'n' will go dead. // Update the mapping: record new Node defined by the register // Update mapping for defined *value*, which is the defined // Node after skipping all copies. for (
int l =
1; l <
n_regs; l++) {
// Note: vector can't be constant and can't be copy of calee. // If the value occupies a register pair, record same info // Sparc occasionally has non-adjacent pairs. // Find the actual other value // Fat projections kill many registers // wow, what an expensive iterator... }
// End of for all instructions in the block