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