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