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