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