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