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