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