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