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