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