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