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