Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 1 | // $Id$ |
| 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // PhyRegAlloc.cpp |
| 5 | // |
| 6 | // Purpose: |
| 7 | // Register allocation for LLVM. |
| 8 | // |
| 9 | // History: |
| 10 | // 9/10/01 - Ruchira Sasanka - created. |
| 11 | //**************************************************************************/ |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 12 | |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/PhyRegAlloc.h" |
| 14 | #include "llvm/CodeGen/MachineInstr.h" |
| 15 | #include "llvm/Target/TargetMachine.h" |
| 16 | #include "llvm/Target/MachineFrameInfo.h" |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 17 | #include <math.h> |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | // ***TODO: There are several places we add instructions. Validate the order |
| 21 | // of adding these instructions. |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 25 | cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags, |
| 26 | "enable register allocation debugging information", |
| 27 | clEnumValN(RA_DEBUG_None , "n", "disable debug output"), |
| 28 | clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"), |
| 29 | clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | //---------------------------------------------------------------------------- |
| 33 | // Constructor: Init local composite objects and create register classes. |
| 34 | //---------------------------------------------------------------------------- |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 35 | PhyRegAlloc::PhyRegAlloc(Method *M, |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 36 | const TargetMachine& tm, |
| 37 | MethodLiveVarInfo *const Lvi) |
| 38 | : RegClassList(), |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 39 | TM(tm), |
| 40 | Meth(M), |
| 41 | mcInfo(MachineCodeForMethod::get(M)), |
| 42 | LVI(Lvi), LRI(M, tm, RegClassList), |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 43 | MRI( tm.getRegInfo() ), |
| 44 | NumOfRegClasses(MRI.getNumOfRegClasses()), |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 45 | AddedInstrMap(), LoopDepthCalc(M), ResColList() { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 46 | |
| 47 | // create each RegisterClass and put in RegClassList |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 48 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 49 | for( unsigned int rc=0; rc < NumOfRegClasses; rc++) |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 50 | RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), |
| 51 | &ResColList) ); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 54 | |
| 55 | //---------------------------------------------------------------------------- |
| 56 | // Destructor: Deletes register classes |
| 57 | //---------------------------------------------------------------------------- |
| 58 | PhyRegAlloc::~PhyRegAlloc() { |
| 59 | |
| 60 | for( unsigned int rc=0; rc < NumOfRegClasses; rc++) { |
| 61 | RegClass *RC = RegClassList[rc]; |
| 62 | delete RC; |
| 63 | } |
| 64 | } |
| 65 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 66 | //---------------------------------------------------------------------------- |
| 67 | // This method initally creates interference graphs (one in each reg class) |
| 68 | // and IGNodeList (one in each IG). The actual nodes will be pushed later. |
| 69 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 70 | void PhyRegAlloc::createIGNodeListsAndIGs() |
| 71 | { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 72 | if(DEBUG_RA ) cout << "Creating LR lists ..." << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 73 | |
| 74 | // hash map iterator |
| 75 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 76 | |
| 77 | // hash map end |
| 78 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 79 | |
| 80 | for( ; HMI != HMIEnd ; ++HMI ) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 81 | |
| 82 | if( (*HMI).first ) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 83 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 84 | LiveRange *L = (*HMI).second; // get the LiveRange |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 85 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 86 | if( !L) { |
| 87 | if( DEBUG_RA) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 88 | cout << "\n*?!?Warning: Null liver range found for: "; |
| 89 | printValue( (*HMI).first) ; cout << endl; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 90 | } |
| 91 | continue; |
| 92 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 93 | // if the Value * is not null, and LR |
| 94 | // is not yet written to the IGNodeList |
| 95 | if( !(L->getUserIGNode()) ) { |
| 96 | |
| 97 | RegClass *const RC = // RegClass of first value in the LR |
| 98 | //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))]; |
| 99 | RegClassList[ L->getRegClass()->getID() ]; |
| 100 | |
| 101 | RC-> addLRToIG( L ); // add this LR to an IG |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // init RegClassList |
| 107 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 108 | RegClassList[ rc ]->createInterferenceGraph(); |
| 109 | |
| 110 | if( DEBUG_RA) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 111 | cout << "LRLists Created!" << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 116 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 117 | //---------------------------------------------------------------------------- |
| 118 | // This method will add all interferences at for a given instruction. |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 119 | // Interence occurs only if the LR of Def (Inst or Arg) is of the same reg |
| 120 | // class as that of live var. The live var passed to this function is the |
| 121 | // LVset AFTER the instruction |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 122 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 123 | void PhyRegAlloc::addInterference(const Value *const Def, |
| 124 | const LiveVarSet *const LVSet, |
| 125 | const bool isCallInst) { |
| 126 | |
| 127 | LiveVarSet::const_iterator LIt = LVSet->begin(); |
| 128 | |
| 129 | // get the live range of instruction |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 130 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 131 | const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def ); |
| 132 | |
| 133 | IGNode *const IGNodeOfDef = LROfDef->getUserIGNode(); |
| 134 | assert( IGNodeOfDef ); |
| 135 | |
| 136 | RegClass *const RCOfDef = LROfDef->getRegClass(); |
| 137 | |
| 138 | // for each live var in live variable set |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 139 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 140 | for( ; LIt != LVSet->end(); ++LIt) { |
| 141 | |
| 142 | if( DEBUG_RA > 1) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 143 | cout << "< Def="; printValue(Def); |
| 144 | cout << ", Lvar="; printValue( *LIt); cout << "> "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // get the live range corresponding to live var |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 148 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 149 | LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt ); |
| 150 | |
| 151 | // LROfVar can be null if it is a const since a const |
| 152 | // doesn't have a dominating def - see Assumptions above |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 153 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 154 | if( LROfVar) { |
| 155 | |
| 156 | if(LROfDef == LROfVar) // do not set interf for same LR |
| 157 | continue; |
| 158 | |
| 159 | // if 2 reg classes are the same set interference |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 160 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 161 | if( RCOfDef == LROfVar->getRegClass() ){ |
| 162 | RCOfDef->setInterference( LROfDef, LROfVar); |
| 163 | |
| 164 | } |
| 165 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 166 | else if(DEBUG_RA > 1) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 167 | // we will not have LRs for values not explicitly allocated in the |
| 168 | // instruction stream (e.g., constants) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 169 | cout << " warning: no live range for " ; |
| 170 | printValue( *LIt); cout << endl; } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 171 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 172 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 173 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 178 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 179 | |
| 180 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 181 | //---------------------------------------------------------------------------- |
| 182 | // For a call instruction, this method sets the CallInterference flag in |
| 183 | // the LR of each variable live int the Live Variable Set live after the |
| 184 | // call instruction (except the return value of the call instruction - since |
| 185 | // the return value does not interfere with that call itself). |
| 186 | //---------------------------------------------------------------------------- |
| 187 | |
| 188 | void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst, |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 189 | const LiveVarSet *const LVSetAft ) { |
| 190 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 191 | // Now find the LR of the return value of the call |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 192 | // We do this because, we look at the LV set *after* the instruction |
| 193 | // to determine, which LRs must be saved across calls. The return value |
| 194 | // of the call is live in this set - but it does not interfere with call |
| 195 | // (i.e., we can allocate a volatile register to the return value) |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 196 | // |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 197 | LiveRange *RetValLR = NULL; |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 198 | const Value *RetVal = MRI.getCallInstRetVal( MInst ); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 199 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 200 | if( RetVal ) { |
| 201 | RetValLR = LRI.getLiveRangeForValue( RetVal ); |
| 202 | assert( RetValLR && "No LR for RetValue of call"); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 205 | if( DEBUG_RA) |
| 206 | cout << "\n For call inst: " << *MInst; |
| 207 | |
| 208 | LiveVarSet::const_iterator LIt = LVSetAft->begin(); |
| 209 | |
| 210 | // for each live var in live variable set after machine inst |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 211 | // |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 212 | for( ; LIt != LVSetAft->end(); ++LIt) { |
| 213 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 214 | // get the live range corresponding to live var |
| 215 | // |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 216 | LiveRange *const LR = LRI.getLiveRangeForValue(*LIt ); |
| 217 | |
| 218 | if( LR && DEBUG_RA) { |
| 219 | cout << "\n\tLR Aft Call: "; |
| 220 | LR->printSet(); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | // LR can be null if it is a const since a const |
| 225 | // doesn't have a dominating def - see Assumptions above |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 226 | // |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 227 | if( LR && (LR != RetValLR) ) { |
| 228 | LR->setCallInterference(); |
| 229 | if( DEBUG_RA) { |
| 230 | cout << "\n ++Added call interf for LR: " ; |
| 231 | LR->printSet(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 240 | |
| 241 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 242 | //---------------------------------------------------------------------------- |
| 243 | // This method will walk thru code and create interferences in the IG of |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 244 | // each RegClass. Also, this method calculates the spill cost of each |
| 245 | // Live Range (it is done in this method to save another pass over the code). |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 246 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 247 | void PhyRegAlloc::buildInterferenceGraphs() |
| 248 | { |
| 249 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 250 | if(DEBUG_RA) cout << "Creating interference graphs ..." << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 251 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 252 | unsigned BBLoopDepthCost; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 253 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 254 | |
| 255 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 256 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 257 | // find the 10^(loop_depth) of this BB |
| 258 | // |
| 259 | BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI)); |
| 260 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 261 | // get the iterator for machine instructions |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 262 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 263 | const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 264 | MachineCodeForBasicBlock::const_iterator |
| 265 | MInstIterator = MIVec.begin(); |
| 266 | |
| 267 | // iterate over all the machine instructions in BB |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 268 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 269 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 270 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 271 | const MachineInstr * MInst = *MInstIterator; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 272 | |
| 273 | // get the LV set after the instruction |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 274 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 275 | const LiveVarSet *const LVSetAI = |
| 276 | LVI->getLiveVarSetAfterMInst(MInst, *BBI); |
| 277 | |
| 278 | const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode()); |
| 279 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 280 | if( isCallInst ) { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 281 | // set the isCallInterference flag of each live range wich extends |
| 282 | // accross this call instruction. This information is used by graph |
| 283 | // coloring algo to avoid allocating volatile colors to live ranges |
| 284 | // that span across calls (since they have to be saved/restored) |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 285 | // |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 286 | setCallInterferences( MInst, LVSetAI); |
| 287 | } |
| 288 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 289 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 290 | // iterate over all MI operands to find defs |
| 291 | // |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 292 | for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) { |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 293 | |
| 294 | if( OpI.isDef() ) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 295 | // create a new LR iff this operand is a def |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 296 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 297 | addInterference(*OpI, LVSetAI, isCallInst ); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Calculate the spill cost of each live range |
| 301 | // |
| 302 | LiveRange *LR = LRI.getLiveRangeForValue( *OpI ); |
| 303 | if( LR ) |
| 304 | LR->addSpillCost(BBLoopDepthCost); |
| 305 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 306 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 307 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 308 | // if there are multiple defs in this instruction e.g. in SETX |
| 309 | // |
| 310 | if( (TM.getInstrInfo()).isPseudoInstr( MInst->getOpCode()) ) |
| 311 | addInterf4PseudoInstr(MInst); |
| 312 | |
| 313 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 314 | // Also add interference for any implicit definitions in a machine |
| 315 | // instr (currently, only calls have this). |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 316 | // |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 317 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 318 | if( NumOfImpRefs > 0 ) { |
| 319 | for(unsigned z=0; z < NumOfImpRefs; z++) |
| 320 | if( MInst->implicitRefIsDefined(z) ) |
| 321 | addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst ); |
| 322 | } |
| 323 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 324 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 325 | } // for all machine instructions in BB |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 326 | |
| 327 | } // for all BBs in method |
| 328 | |
| 329 | |
| 330 | // add interferences for method arguments. Since there are no explict |
| 331 | // defs in method for args, we have to add them manually |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 332 | // |
| 333 | addInterferencesForArgs(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 334 | |
| 335 | if( DEBUG_RA) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 336 | cout << "Interference graphs calculted!" << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 337 | |
| 338 | } |
| 339 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 340 | |
| 341 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 342 | //-------------------------------------------------------------------------- |
| 343 | // Pseudo instructions will be exapnded to multiple instructions by the |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 344 | // assembler. Consequently, all the opernds must get distinct registers. |
| 345 | // Therefore, we mark all operands of a pseudo instruction as they interfere |
| 346 | // with one another. |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 347 | //-------------------------------------------------------------------------- |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 348 | void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) { |
| 349 | |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 350 | bool setInterf = false; |
| 351 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 352 | // iterate over MI operands to find defs |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 353 | // |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 354 | for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) { |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 355 | |
| 356 | const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 ); |
| 357 | |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 358 | if( !LROfOp1 && It1.isDef() ) |
| 359 | assert( 0 && "No LR for Def in PSEUDO insruction"); |
| 360 | |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 361 | MachineInstr::val_const_op_iterator It2 = It1; |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 362 | ++It2; |
| 363 | |
| 364 | for( ; !It2.done(); ++It2) { |
| 365 | |
| 366 | const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 ); |
| 367 | |
| 368 | if( LROfOp2) { |
| 369 | |
| 370 | RegClass *const RCOfOp1 = LROfOp1->getRegClass(); |
| 371 | RegClass *const RCOfOp2 = LROfOp2->getRegClass(); |
| 372 | |
| 373 | if( RCOfOp1 == RCOfOp2 ){ |
| 374 | RCOfOp1->setInterference( LROfOp1, LROfOp2 ); |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 375 | setInterf = true; |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | } // if Op2 has a LR |
| 379 | |
| 380 | } // for all other defs in machine instr |
| 381 | |
| 382 | } // for all operands in an instruction |
| 383 | |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 384 | if( !setInterf && (MInst->getNumOperands() > 2) ) { |
| 385 | cerr << "\nInterf not set for any operand in pseudo instr:\n"; |
| 386 | cerr << *MInst; |
| 387 | assert(0 && "Interf not set for pseudo instr with > 2 operands" ); |
| 388 | |
| 389 | } |
| 390 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 394 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 395 | //---------------------------------------------------------------------------- |
| 396 | // This method will add interferences for incoming arguments to a method. |
| 397 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 398 | void PhyRegAlloc::addInterferencesForArgs() |
| 399 | { |
| 400 | // get the InSet of root BB |
| 401 | const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() ); |
| 402 | |
| 403 | // get the argument list |
| 404 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 405 | |
| 406 | // get an iterator to arg list |
| 407 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
| 408 | |
| 409 | |
| 410 | for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument |
| 411 | addInterference( *ArgIt, InSet, false ); // add interferences between |
| 412 | // args and LVars at start |
| 413 | if( DEBUG_RA > 1) { |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 414 | cout << " - %% adding interference for argument "; |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 415 | printValue( (const Value *) *ArgIt); cout << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 421 | |
| 422 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 423 | //---------------------------------------------------------------------------- |
| 424 | // This method is called after register allocation is complete to set the |
| 425 | // allocated reisters in the machine code. This code will add register numbers |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 426 | // to MachineOperands that contain a Value. Also it calls target specific |
| 427 | // methods to produce caller saving instructions. At the end, it adds all |
| 428 | // additional instructions produced by the register allocator to the |
| 429 | // instruction stream. |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 430 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 431 | void PhyRegAlloc::updateMachineCode() |
| 432 | { |
| 433 | |
| 434 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 435 | |
| 436 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 437 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 438 | // get the iterator for machine instructions |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 439 | // |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 440 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 441 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 442 | |
| 443 | // iterate over all the machine instructions in BB |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 444 | // |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 445 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 446 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 447 | MachineInstr *MInst = *MInstIterator; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 448 | |
| 449 | unsigned Opcode = MInst->getOpCode(); |
| 450 | |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 451 | // do not process Phis |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 452 | if( (TM.getInstrInfo()).isPhi( Opcode ) ) |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 453 | continue; |
| 454 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 455 | // Now insert speical instructions (if necessary) for call/return |
| 456 | // instructions. |
| 457 | // |
| 458 | if( (TM.getInstrInfo()).isCall( Opcode) || |
| 459 | (TM.getInstrInfo()).isReturn( Opcode) ) { |
| 460 | |
| 461 | AddedInstrns *AI = AddedInstrMap[ MInst]; |
| 462 | if ( !AI ) { |
| 463 | AI = new AddedInstrns(); |
| 464 | AddedInstrMap[ MInst ] = AI; |
| 465 | } |
| 466 | |
| 467 | // Tmp stack poistions are needed by some calls that have spilled args |
| 468 | // So reset it before we call each such method |
Ruchira Sasanka | 6a3db8c | 2002-01-07 21:09:06 +0000 | [diff] [blame] | 469 | // |
| 470 | mcInfo.popAllTempValues(TM); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 471 | |
| 472 | if( (TM.getInstrInfo()).isCall( Opcode ) ) |
| 473 | MRI.colorCallArgs( MInst, LRI, AI, *this, *BBI ); |
| 474 | |
| 475 | else if ( (TM.getInstrInfo()).isReturn(Opcode) ) |
| 476 | MRI.colorRetValue( MInst, LRI, AI ); |
| 477 | |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /* -- Using above code instead of this |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 482 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 483 | // if this machine instr is call, insert caller saving code |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 484 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 485 | if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) ) |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 486 | MRI.insertCallerSavingCode(MInst, *BBI, *this ); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 487 | |
| 488 | */ |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 489 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 490 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 491 | // reset the stack offset for temporary variables since we may |
| 492 | // need that to spill |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 493 | // mcInfo.popAllTempValues(TM); |
Ruchira Sasanka | f90870f | 2001-11-15 22:02:06 +0000 | [diff] [blame] | 494 | // TODO ** : do later |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 496 | //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 497 | |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 498 | |
| 499 | // Now replace set the registers for operands in the machine instruction |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 500 | // |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 501 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 502 | |
| 503 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 504 | |
| 505 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
| 506 | Op.getOperandType() == MachineOperand::MO_CCRegister) { |
| 507 | |
| 508 | const Value *const Val = Op.getVRegValue(); |
| 509 | |
| 510 | // delete this condition checking later (must assert if Val is null) |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 511 | if( !Val) { |
| 512 | if (DEBUG_RA) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 513 | cout << "Warning: NULL Value found for operand" << endl; |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 514 | continue; |
| 515 | } |
| 516 | assert( Val && "Value is NULL"); |
| 517 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 518 | LiveRange *const LR = LRI.getLiveRangeForValue(Val); |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 519 | |
| 520 | if ( !LR ) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 521 | |
| 522 | // nothing to worry if it's a const or a label |
| 523 | |
Chris Lattner | 4c3aaa4 | 2001-09-19 16:09:04 +0000 | [diff] [blame] | 524 | if (DEBUG_RA) { |
Ruchira Sasanka | 1b732fd | 2001-10-16 16:34:44 +0000 | [diff] [blame] | 525 | cout << "*NO LR for operand : " << Op ; |
| 526 | cout << " [reg:" << Op.getAllocatedRegNum() << "]"; |
| 527 | cout << " in inst:\t" << *MInst << endl; |
Chris Lattner | 4c3aaa4 | 2001-09-19 16:09:04 +0000 | [diff] [blame] | 528 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 529 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 530 | // if register is not allocated, mark register as invalid |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 531 | if( Op.getAllocatedRegNum() == -1) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 532 | Op.setRegForValue( MRI.getInvalidRegNum()); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 533 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 534 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 535 | continue; |
| 536 | } |
| 537 | |
| 538 | unsigned RCID = (LR->getRegClass())->getID(); |
| 539 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 540 | if( LR->hasColor() ) { |
| 541 | Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) ); |
| 542 | } |
| 543 | else { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 544 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 545 | // LR did NOT receive a color (register). Now, insert spill code |
| 546 | // for spilled opeands in this machine instruction |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 547 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 548 | //assert(0 && "LR must be spilled"); |
| 549 | insertCode4SpilledLR(LR, MInst, *BBI, OpNum ); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 550 | |
| 551 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 554 | } // for each operand |
| 555 | |
| 556 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 557 | // Now add instructions that the register allocator inserts before/after |
| 558 | // this machine instructions (done only for calls/rets/incoming args) |
| 559 | // We do this here, to ensure that spill for an instruction is inserted |
| 560 | // closest as possible to an instruction (see above insertCode4Spill...) |
| 561 | // |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 562 | // If there are instructions to be added, *before* this machine |
| 563 | // instruction, add them now. |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 564 | // |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 565 | if( AddedInstrMap[ MInst ] ) { |
| 566 | |
| 567 | deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore; |
| 568 | |
| 569 | if( ! IBef.empty() ) { |
| 570 | |
| 571 | deque<MachineInstr *>::iterator AdIt; |
| 572 | |
| 573 | for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) { |
| 574 | |
| 575 | if( DEBUG_RA) { |
| 576 | cerr << "For inst " << *MInst; |
| 577 | cerr << " PREPENDed instr: " << **AdIt << endl; |
| 578 | } |
| 579 | |
| 580 | MInstIterator = MIVec.insert( MInstIterator, *AdIt ); |
| 581 | ++MInstIterator; |
| 582 | } |
| 583 | |
| 584 | } |
| 585 | |
| 586 | } |
| 587 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 588 | // If there are instructions to be added *after* this machine |
| 589 | // instruction, add them now |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 590 | // |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 591 | if( AddedInstrMap[ MInst ] && |
| 592 | ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 593 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 594 | // if there are delay slots for this instruction, the instructions |
| 595 | // added after it must really go after the delayed instruction(s) |
| 596 | // So, we move the InstrAfter of the current instruction to the |
| 597 | // corresponding delayed instruction |
| 598 | |
| 599 | unsigned delay; |
| 600 | if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){ |
| 601 | move2DelayedInstr(MInst, *(MInstIterator+delay) ); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 602 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 603 | if(DEBUG_RA) cout<< "\nMoved an added instr after the delay slot"; |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 604 | } |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 605 | |
| 606 | else { |
| 607 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 608 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 609 | // Here we can add the "instructions after" to the current |
| 610 | // instruction since there are no delay slots for this instruction |
| 611 | |
| 612 | deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter; |
| 613 | |
| 614 | if( ! IAft.empty() ) { |
| 615 | |
| 616 | deque<MachineInstr *>::iterator AdIt; |
| 617 | |
| 618 | ++MInstIterator; // advance to the next instruction |
| 619 | |
| 620 | for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) { |
| 621 | |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 622 | if(DEBUG_RA) { |
| 623 | cerr << "For inst " << *MInst; |
Ruchira Sasanka | ad14009 | 2001-11-09 23:49:42 +0000 | [diff] [blame] | 624 | cerr << " APPENDed instr: " << **AdIt << endl; |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 627 | MInstIterator = MIVec.insert( MInstIterator, *AdIt ); |
| 628 | ++MInstIterator; |
| 629 | } |
| 630 | |
| 631 | // MInsterator already points to the next instr. Since the |
| 632 | // for loop also increments it, decrement it to point to the |
| 633 | // instruction added last |
| 634 | --MInstIterator; |
| 635 | |
| 636 | } |
| 637 | |
| 638 | } // if not delay |
| 639 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 640 | } |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 641 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 642 | } // for each machine instruction |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 647 | |
| 648 | //---------------------------------------------------------------------------- |
| 649 | // This method inserts spill code for AN operand whose LR was spilled. |
| 650 | // This method may be called several times for a single machine instruction |
| 651 | // if it contains many spilled operands. Each time it is called, it finds |
| 652 | // a register which is not live at that instruction and also which is not |
| 653 | // used by other spilled operands of the same instruction. Then it uses |
| 654 | // this register temporarily to accomodate the spilled value. |
| 655 | //---------------------------------------------------------------------------- |
| 656 | void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR, |
| 657 | MachineInstr *MInst, |
| 658 | const BasicBlock *BB, |
| 659 | const unsigned OpNum) { |
| 660 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 661 | assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) && |
| 662 | (! TM.getInstrInfo().isReturn(MInst->getOpCode())) && |
| 663 | "Arg of a call/ret must be handled elsewhere"); |
| 664 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 665 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 666 | bool isDef = MInst->operandIsDefined(OpNum); |
| 667 | unsigned RegType = MRI.getRegType( LR ); |
| 668 | int SpillOff = LR->getSpillOffFromFP(); |
| 669 | RegClass *RC = LR->getRegClass(); |
| 670 | const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 671 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 672 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 673 | int TmpOff = |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 674 | mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) ); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 675 | |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 676 | MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL; |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 677 | |
| 678 | int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft); |
| 679 | |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 680 | // get the added instructions for this instruciton |
| 681 | AddedInstrns *AI = AddedInstrMap[ MInst ]; |
| 682 | if ( !AI ) { |
| 683 | AI = new AddedInstrns(); |
| 684 | AddedInstrMap[ MInst ] = AI; |
| 685 | } |
| 686 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 687 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 688 | if( !isDef ) { |
| 689 | |
| 690 | // for a USE, we have to load the value of LR from stack to a TmpReg |
| 691 | // and use the TmpReg as one operand of instruction |
| 692 | |
| 693 | // actual loading instruction |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 694 | AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 695 | |
| 696 | if( MIBef ) |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 697 | (AI->InstrnsBefore).push_back(MIBef); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 698 | |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 699 | (AI->InstrnsBefore).push_back(AdIMid); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 700 | |
| 701 | if( MIAft) |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 702 | (AI->InstrnsAfter).push_front(MIAft); |
| 703 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 704 | |
| 705 | } |
| 706 | else { // if this is a Def |
| 707 | |
| 708 | // for a DEF, we have to store the value produced by this instruction |
| 709 | // on the stack position allocated for this LR |
| 710 | |
| 711 | // actual storing instruction |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 712 | AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 713 | |
| 714 | if( MIBef ) |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 715 | (AI->InstrnsBefore).push_back(MIBef); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 716 | |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 717 | (AI->InstrnsAfter).push_front(AdIMid); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 718 | |
| 719 | if( MIAft) |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 720 | (AI->InstrnsAfter).push_front(MIAft); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 721 | |
| 722 | } // if !DEF |
| 723 | |
| 724 | cerr << "\nFor Inst " << *MInst; |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 725 | cerr << " - SPILLED LR: "; LR->printSet(); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 726 | cerr << "\n - Added Instructions:"; |
| 727 | if( MIBef ) cerr << *MIBef; |
| 728 | cerr << *AdIMid; |
| 729 | if( MIAft ) cerr << *MIAft; |
| 730 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 731 | Op.setRegForValue( TmpRegU ); // set the opearnd |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 732 | |
| 733 | |
| 734 | } |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 741 | //---------------------------------------------------------------------------- |
| 742 | // We can use the following method to get a temporary register to be used |
| 743 | // BEFORE any given machine instruction. If there is a register available, |
| 744 | // this method will simply return that register and set MIBef = MIAft = NULL. |
| 745 | // Otherwise, it will return a register and MIAft and MIBef will contain |
| 746 | // two instructions used to free up this returned register. |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 747 | // Returned register number is the UNIFIED register number |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 748 | //---------------------------------------------------------------------------- |
| 749 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 750 | int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC, |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 751 | const int RegType, |
| 752 | const MachineInstr *MInst, |
| 753 | const LiveVarSet *LVSetBef, |
| 754 | MachineInstr *MIBef, |
| 755 | MachineInstr *MIAft) { |
| 756 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 757 | int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 758 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 759 | |
| 760 | if( RegU != -1) { |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 761 | // we found an unused register, so we can simply use it |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 762 | MIBef = MIAft = NULL; |
| 763 | } |
| 764 | else { |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 765 | // we couldn't find an unused register. Generate code to free up a reg by |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 766 | // saving it on stack and restoring after the instruction |
| 767 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 768 | int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) ); |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 769 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 770 | RegU = getUniRegNotUsedByThisInst(RC, MInst); |
| 771 | MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType ); |
| 772 | MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType ); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 775 | return RegU; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | //---------------------------------------------------------------------------- |
| 779 | // This method is called to get a new unused register that can be used to |
| 780 | // accomodate a spilled value. |
| 781 | // This method may be called several times for a single machine instruction |
| 782 | // if it contains many spilled operands. Each time it is called, it finds |
| 783 | // a register which is not live at that instruction and also which is not |
| 784 | // used by other spilled operands of the same instruction. |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 785 | // Return register number is relative to the register class. NOT |
| 786 | // unified number |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 787 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 788 | int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 789 | const MachineInstr *MInst, |
| 790 | const LiveVarSet *LVSetBef) { |
| 791 | |
| 792 | unsigned NumAvailRegs = RC->getNumOfAvailRegs(); |
| 793 | |
| 794 | bool *IsColorUsedArr = RC->getIsColorUsedArr(); |
| 795 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 796 | for(unsigned i=0; i < NumAvailRegs; i++) // Reset array |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 797 | IsColorUsedArr[i] = false; |
| 798 | |
| 799 | LiveVarSet::const_iterator LIt = LVSetBef->begin(); |
| 800 | |
| 801 | // for each live var in live variable set after machine inst |
| 802 | for( ; LIt != LVSetBef->end(); ++LIt) { |
| 803 | |
| 804 | // get the live range corresponding to live var |
| 805 | LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt ); |
| 806 | |
| 807 | // LR can be null if it is a const since a const |
| 808 | // doesn't have a dominating def - see Assumptions above |
| 809 | if( LRofLV ) |
| 810 | if( LRofLV->hasColor() ) |
| 811 | IsColorUsedArr[ LRofLV->getColor() ] = true; |
| 812 | } |
| 813 | |
| 814 | // It is possible that one operand of this MInst was already spilled |
| 815 | // and it received some register temporarily. If that's the case, |
| 816 | // it is recorded in machine operand. We must skip such registers. |
| 817 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 818 | setRelRegsUsedByThisInst(RC, MInst); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 819 | |
| 820 | unsigned c; // find first unused color |
| 821 | for( c=0; c < NumAvailRegs; c++) |
| 822 | if( ! IsColorUsedArr[ c ] ) break; |
| 823 | |
| 824 | if(c < NumAvailRegs) |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 825 | return MRI.getUnifiedRegNum(RC->getID(), c); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 826 | else |
| 827 | return -1; |
| 828 | |
| 829 | |
| 830 | } |
| 831 | |
| 832 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 833 | //---------------------------------------------------------------------------- |
| 834 | // Get any other register in a register class, other than what is used |
| 835 | // by operands of a machine instruction. Returns the unified reg number. |
| 836 | //---------------------------------------------------------------------------- |
| 837 | int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, |
| 838 | const MachineInstr *MInst) { |
| 839 | |
| 840 | bool *IsColorUsedArr = RC->getIsColorUsedArr(); |
| 841 | unsigned NumAvailRegs = RC->getNumOfAvailRegs(); |
| 842 | |
| 843 | |
| 844 | for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array |
| 845 | IsColorUsedArr[i] = false; |
| 846 | |
| 847 | setRelRegsUsedByThisInst(RC, MInst); |
| 848 | |
| 849 | unsigned c; // find first unused color |
| 850 | for( c=0; c < RC->getNumOfAvailRegs(); c++) |
| 851 | if( ! IsColorUsedArr[ c ] ) break; |
| 852 | |
| 853 | if(c < NumAvailRegs) |
| 854 | return MRI.getUnifiedRegNum(RC->getID(), c); |
| 855 | else |
| 856 | assert( 0 && "FATAL: No free register could be found in reg class!!"); |
| 857 | |
| 858 | } |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 863 | |
| 864 | //---------------------------------------------------------------------------- |
| 865 | // This method modifies the IsColorUsedArr of the register class passed to it. |
| 866 | // It sets the bits corresponding to the registers used by this machine |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 867 | // instructions. Both explicit and implicit operands are set. |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 868 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 869 | void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 870 | const MachineInstr *MInst ) { |
| 871 | |
| 872 | bool *IsColorUsedArr = RC->getIsColorUsedArr(); |
| 873 | |
| 874 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 875 | |
| 876 | const MachineOperand& Op = MInst->getOperand(OpNum); |
| 877 | |
| 878 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 879 | Op.getOperandType() == MachineOperand::MO_CCRegister ) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 880 | |
| 881 | const Value *const Val = Op.getVRegValue(); |
| 882 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 883 | if( Val ) |
| 884 | if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 885 | int Reg; |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 886 | if( (Reg=Op.getAllocatedRegNum()) != -1) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 887 | IsColorUsedArr[ Reg ] = true; |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 888 | } |
| 889 | else { |
| 890 | // it is possilbe that this operand still is not marked with |
| 891 | // a register but it has a LR and that received a color |
| 892 | |
| 893 | LiveRange *LROfVal = LRI.getLiveRangeForValue(Val); |
| 894 | if( LROfVal) |
| 895 | if( LROfVal->hasColor() ) |
| 896 | IsColorUsedArr[ LROfVal->getColor() ] = true; |
| 897 | } |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 898 | |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 899 | } // if reg classes are the same |
| 900 | } |
| 901 | else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 902 | IsColorUsedArr[ Op.getMachineRegNum() ] = true; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | // If there are implicit references, mark them as well |
| 907 | |
| 908 | for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) { |
| 909 | |
| 910 | LiveRange *const LRofImpRef = |
| 911 | LRI.getLiveRangeForValue( MInst->getImplicitRef(z) ); |
| 912 | |
| 913 | if( LRofImpRef ) |
| 914 | if( LRofImpRef->hasColor() ) |
| 915 | IsColorUsedArr[ LRofImpRef->getColor() ] = true; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | |
| 920 | } |
| 921 | |
| 922 | |
| 923 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 930 | // If there are delay slots for an instruction, the instructions |
| 931 | // added after it must really go after the delayed instruction(s). |
| 932 | // So, we move the InstrAfter of that instruction to the |
| 933 | // corresponding delayed instruction using the following method. |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 934 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 935 | //---------------------------------------------------------------------------- |
| 936 | void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI, |
| 937 | const MachineInstr *DelayedMI) { |
| 938 | |
| 939 | |
| 940 | // "added after" instructions of the original instr |
| 941 | deque<MachineInstr *> &OrigAft = (AddedInstrMap[OrigMI])->InstrnsAfter; |
| 942 | |
| 943 | // "added instructions" of the delayed instr |
| 944 | AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI]; |
| 945 | |
| 946 | if(! DelayAdI ) { // create a new "added after" if necessary |
| 947 | DelayAdI = new AddedInstrns(); |
| 948 | AddedInstrMap[DelayedMI] = DelayAdI; |
| 949 | } |
| 950 | |
| 951 | // "added after" instructions of the delayed instr |
| 952 | deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter; |
| 953 | |
| 954 | // go thru all the "added after instructions" of the original instruction |
| 955 | // and append them to the "addded after instructions" of the delayed |
| 956 | // instructions |
| 957 | |
| 958 | deque<MachineInstr *>::iterator OrigAdIt; |
| 959 | |
| 960 | for( OrigAdIt = OrigAft.begin(); OrigAdIt != OrigAft.end() ; ++OrigAdIt ) { |
| 961 | DelayedAft.push_back( *OrigAdIt ); |
| 962 | } |
| 963 | |
| 964 | // empty the "added after instructions" of the original instruction |
| 965 | OrigAft.clear(); |
| 966 | |
| 967 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 968 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 969 | //---------------------------------------------------------------------------- |
| 970 | // This method prints the code with registers after register allocation is |
| 971 | // complete. |
| 972 | //---------------------------------------------------------------------------- |
| 973 | void PhyRegAlloc::printMachineCode() |
| 974 | { |
| 975 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 976 | cout << endl << ";************** Method "; |
| 977 | cout << Meth->getName() << " *****************" << endl; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 978 | |
| 979 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 980 | |
| 981 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 982 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 983 | cout << endl ; printLabel( *BBI); cout << ": "; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 984 | |
| 985 | // get the iterator for machine instructions |
| 986 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 987 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 988 | |
| 989 | // iterate over all the machine instructions in BB |
| 990 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 991 | |
| 992 | MachineInstr *const MInst = *MInstIterator; |
| 993 | |
| 994 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 995 | cout << endl << "\t"; |
| 996 | cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 997 | |
| 998 | |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 999 | //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1000 | |
| 1001 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 1002 | |
| 1003 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 1004 | |
| 1005 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 1006 | Op.getOperandType() == MachineOperand::MO_CCRegister /*|| |
| 1007 | Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1008 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1009 | const Value *const Val = Op.getVRegValue () ; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1010 | // ****this code is temporary till NULL Values are fixed |
| 1011 | if( ! Val ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1012 | cout << "\t<*NULL*>"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1013 | continue; |
| 1014 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1015 | |
| 1016 | // if a label or a constant |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1017 | if( (Val->getValueType() == Value::BasicBlockVal) ) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1018 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1019 | cout << "\t"; printLabel( Op.getVRegValue () ); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1020 | } |
| 1021 | else { |
| 1022 | // else it must be a register value |
| 1023 | const int RegNum = Op.getAllocatedRegNum(); |
| 1024 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1025 | cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum ); |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 1026 | if (Val->hasName() ) |
| 1027 | cout << "(" << Val->getName() << ")"; |
| 1028 | else |
| 1029 | cout << "(" << Val << ")"; |
| 1030 | |
| 1031 | if( Op.opIsDef() ) |
| 1032 | cout << "*"; |
| 1033 | |
| 1034 | const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val); |
| 1035 | if( LROfVal ) |
| 1036 | if( LROfVal->hasSpillOffset() ) |
| 1037 | cout << "$"; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | } |
| 1041 | else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1042 | cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum()); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | else |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1046 | cout << "\t" << Op; // use dump field |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1049 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1050 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1051 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 1052 | if( NumOfImpRefs > 0 ) { |
| 1053 | |
| 1054 | cout << "\tImplicit:"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1055 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1056 | for(unsigned z=0; z < NumOfImpRefs; z++) { |
| 1057 | printValue( MInst->getImplicitRef(z) ); |
| 1058 | cout << "\t"; |
| 1059 | } |
| 1060 | |
| 1061 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1062 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1063 | } // for all machine instructions |
| 1064 | |
| 1065 | |
| 1066 | cout << endl; |
| 1067 | |
| 1068 | } // for all BBs |
| 1069 | |
| 1070 | cout << endl; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1073 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1074 | #if 0 |
| 1075 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1076 | //---------------------------------------------------------------------------- |
| 1077 | // |
| 1078 | //---------------------------------------------------------------------------- |
| 1079 | |
| 1080 | void PhyRegAlloc::colorCallRetArgs() |
| 1081 | { |
| 1082 | |
| 1083 | CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList(); |
| 1084 | CallRetInstrListType::const_iterator It = CallRetInstList.begin(); |
| 1085 | |
| 1086 | for( ; It != CallRetInstList.end(); ++It ) { |
| 1087 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 1088 | const MachineInstr *const CRMI = *It; |
| 1089 | unsigned OpCode = CRMI->getOpCode(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1090 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1091 | // get the added instructions for this Call/Ret instruciton |
| 1092 | AddedInstrns *AI = AddedInstrMap[ CRMI ]; |
| 1093 | if ( !AI ) { |
| 1094 | AI = new AddedInstrns(); |
| 1095 | AddedInstrMap[ CRMI ] = AI; |
| 1096 | } |
| 1097 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1098 | // Tmp stack poistions are needed by some calls that have spilled args |
| 1099 | // So reset it before we call each such method |
Ruchira Sasanka | f90870f | 2001-11-15 22:02:06 +0000 | [diff] [blame] | 1100 | //mcInfo.popAllTempValues(TM); |
| 1101 | |
| 1102 | |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 1103 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 1104 | if( (TM.getInstrInfo()).isCall( OpCode ) ) |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1105 | MRI.colorCallArgs( CRMI, LRI, AI, *this ); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1106 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 1107 | else if ( (TM.getInstrInfo()).isReturn(OpCode) ) |
| 1108 | MRI.colorRetValue( CRMI, LRI, AI ); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1109 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1110 | else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" ); |
| 1111 | |
| 1112 | } |
| 1113 | |
| 1114 | } |
| 1115 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1116 | #endif |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1117 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1118 | //---------------------------------------------------------------------------- |
| 1119 | |
| 1120 | //---------------------------------------------------------------------------- |
| 1121 | void PhyRegAlloc::colorIncomingArgs() |
| 1122 | { |
| 1123 | const BasicBlock *const FirstBB = Meth->front(); |
| 1124 | const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin()); |
| 1125 | assert( FirstMI && "No machine instruction in entry BB"); |
| 1126 | |
| 1127 | AddedInstrns *AI = AddedInstrMap[ FirstMI ]; |
| 1128 | if ( !AI ) { |
| 1129 | AI = new AddedInstrns(); |
| 1130 | AddedInstrMap[ FirstMI ] = AI; |
| 1131 | } |
| 1132 | |
| 1133 | MRI.colorMethodArgs(Meth, LRI, AI ); |
| 1134 | } |
| 1135 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1136 | |
| 1137 | //---------------------------------------------------------------------------- |
| 1138 | // Used to generate a label for a basic block |
| 1139 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1140 | void PhyRegAlloc::printLabel(const Value *const Val) |
| 1141 | { |
| 1142 | if( Val->hasName() ) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1143 | cout << Val->getName(); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1144 | else |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1145 | cout << "Label" << Val; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1149 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1150 | // This method calls setSugColorUsable method of each live range. This |
| 1151 | // will determine whether the suggested color of LR is really usable. |
| 1152 | // A suggested color is not usable when the suggested color is volatile |
| 1153 | // AND when there are call interferences |
| 1154 | //---------------------------------------------------------------------------- |
| 1155 | |
| 1156 | void PhyRegAlloc::markUnusableSugColors() |
| 1157 | { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1158 | if(DEBUG_RA ) cout << "\nmarking unusable suggested colors ..." << endl; |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1159 | |
| 1160 | // hash map iterator |
| 1161 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 1162 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 1163 | |
| 1164 | for( ; HMI != HMIEnd ; ++HMI ) { |
| 1165 | |
| 1166 | if( (*HMI).first ) { |
| 1167 | |
| 1168 | LiveRange *L = (*HMI).second; // get the LiveRange |
| 1169 | |
| 1170 | if(L) { |
| 1171 | if( L->hasSuggestedColor() ) { |
| 1172 | |
| 1173 | int RCID = (L->getRegClass())->getID(); |
| 1174 | if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) && |
| 1175 | L->isCallInterference() ) |
| 1176 | L->setSuggestedColorUsable( false ); |
| 1177 | else |
| 1178 | L->setSuggestedColorUsable( true ); |
| 1179 | } |
| 1180 | } // if L->hasSuggestedColor() |
| 1181 | } |
| 1182 | } // for all LR's in hash map |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1187 | //---------------------------------------------------------------------------- |
| 1188 | // The following method will set the stack offsets of the live ranges that |
| 1189 | // are decided to be spillled. This must be called just after coloring the |
| 1190 | // LRs using the graph coloring algo. For each live range that is spilled, |
| 1191 | // this method allocate a new spill position on the stack. |
| 1192 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1193 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1194 | void PhyRegAlloc::allocateStackSpace4SpilledLRs() |
| 1195 | { |
| 1196 | if(DEBUG_RA ) cout << "\nsetting LR stack offsets ..." << endl; |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1197 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1198 | // hash map iterator |
| 1199 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 1200 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 1201 | |
| 1202 | for( ; HMI != HMIEnd ; ++HMI ) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1203 | if( (*HMI).first ) { |
| 1204 | LiveRange *L = (*HMI).second; // get the LiveRange |
| 1205 | if(L) |
| 1206 | if( ! L->hasColor() ) |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1207 | |
| 1208 | // NOTE: ** allocating the size of long Type ** |
| 1209 | L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, |
| 1210 | Type::LongTy)); |
| 1211 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } // for all LR's in hash map |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1214 | } |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1215 | |
| 1216 | |
| 1217 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1218 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1219 | // The entry pont to Register Allocation |
| 1220 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1221 | |
| 1222 | void PhyRegAlloc::allocateRegisters() |
| 1223 | { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1224 | |
| 1225 | // make sure that we put all register classes into the RegClassList |
| 1226 | // before we call constructLiveRanges (now done in the constructor of |
| 1227 | // PhyRegAlloc class). |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1228 | // |
| 1229 | LRI.constructLiveRanges(); // create LR info |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1230 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1231 | if( DEBUG_RA ) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1232 | LRI.printLiveRanges(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1233 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1234 | createIGNodeListsAndIGs(); // create IGNode list and IGs |
| 1235 | |
| 1236 | buildInterferenceGraphs(); // build IGs in all reg classes |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1237 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1238 | |
| 1239 | if( DEBUG_RA ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1240 | // print all LRs in all reg classes |
| 1241 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 1242 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1243 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1244 | // print IGs in all register classes |
| 1245 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 1246 | RegClassList[ rc ]->printIG(); |
| 1247 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1248 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1249 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1250 | LRI.coalesceLRs(); // coalesce all live ranges |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1251 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1252 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1253 | if( DEBUG_RA) { |
| 1254 | // print all LRs in all reg classes |
| 1255 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 1256 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1257 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1258 | // print IGs in all register classes |
| 1259 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 1260 | RegClassList[ rc ]->printIG(); |
| 1261 | } |
| 1262 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1263 | |
| 1264 | // mark un-usable suggested color before graph coloring algorithm. |
| 1265 | // When this is done, the graph coloring algo will not reserve |
| 1266 | // suggested color unnecessarily - they can be used by another LR |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1267 | // |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1268 | markUnusableSugColors(); |
| 1269 | |
| 1270 | // color all register classes using the graph coloring algo |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1271 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 1272 | RegClassList[ rc ]->colorAllRegs(); |
| 1273 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1274 | // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled) |
| 1275 | // a poistion for such spilled LRs |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1276 | // |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1277 | allocateStackSpace4SpilledLRs(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1278 | |
Ruchira Sasanka | f90870f | 2001-11-15 22:02:06 +0000 | [diff] [blame] | 1279 | mcInfo.popAllTempValues(TM); // TODO **Check |
| 1280 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1281 | // color incoming args - if the correct color was not received |
| 1282 | // insert code to copy to the correct register |
| 1283 | // |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1284 | colorIncomingArgs(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1285 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Now update the machine code with register names and add any |
| 1288 | // additional code inserted by the register allocator to the instruction |
| 1289 | // stream |
| 1290 | // |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1291 | updateMachineCode(); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1292 | |
| 1293 | |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 1294 | if (DEBUG_RA) { |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 1295 | MachineCodeForMethod::get(Meth).dump(); |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 1296 | printMachineCode(); // only for DEBUGGING |
| 1297 | } |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 1298 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1301 | |
| 1302 | |