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