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