Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1 | //===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a linear scan register allocator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 13 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "regalloc" |
Jakob Stoklund Olesen | 42acf06 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 15 | #include "LiveDebugVariables.h" |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 16 | #include "VirtRegMap.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 17 | #include "VirtRegRewriter.h" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 18 | #include "Spiller.h" |
Jakob Stoklund Olesen | e93198a | 2010-11-10 23:55:56 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 24 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/Passes.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/RegAllocRegistry.h" |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Owen Anderson | 95dad83 | 2008-10-07 20:22:28 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/EquivalenceClasses.h" |
Dan Gohman | d68a076 | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
| 37 | #include "llvm/ADT/STLExtras.h" |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Alkis Evlogimenos | 26f5a69 | 2004-05-30 07:24:39 +0000 | [diff] [blame] | 42 | #include <set> |
Alkis Evlogimenos | 53eb373 | 2004-07-22 08:14:44 +0000 | [diff] [blame] | 43 | #include <queue> |
Duraid Madina | 3005961 | 2005-12-28 04:55:42 +0000 | [diff] [blame] | 44 | #include <memory> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 45 | #include <cmath> |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 46 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 49 | STATISTIC(NumIters , "Number of iterations performed"); |
| 50 | STATISTIC(NumBacktracks, "Number of times we had to backtrack"); |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 51 | STATISTIC(NumCoalesce, "Number of copies coalesced"); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 52 | STATISTIC(NumDowngrade, "Number of registers downgraded"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 53 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 54 | static cl::opt<bool> |
| 55 | NewHeuristic("new-spilling-heuristic", |
| 56 | cl::desc("Use new spilling heuristic"), |
| 57 | cl::init(false), cl::Hidden); |
| 58 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 59 | static cl::opt<bool> |
| 60 | PreSplitIntervals("pre-alloc-split", |
| 61 | cl::desc("Pre-register allocation live interval splitting"), |
| 62 | cl::init(false), cl::Hidden); |
| 63 | |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
| 65 | TrivCoalesceEnds("trivial-coalesce-ends", |
| 66 | cl::desc("Attempt trivial coalescing of interval ends"), |
| 67 | cl::init(false), cl::Hidden); |
| 68 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 69 | static RegisterRegAlloc |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 70 | linearscanRegAlloc("linearscan", "linear scan register allocator", |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 71 | createLinearScanRegisterAllocator); |
| 72 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 73 | namespace { |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 74 | // When we allocate a register, add it to a fixed-size queue of |
| 75 | // registers to skip in subsequent allocations. This trades a small |
| 76 | // amount of register pressure and increased spills for flexibility in |
| 77 | // the post-pass scheduler. |
| 78 | // |
| 79 | // Note that in a the number of registers used for reloading spills |
| 80 | // will be one greater than the value of this option. |
| 81 | // |
| 82 | // One big limitation of this is that it doesn't differentiate between |
| 83 | // different register classes. So on x86-64, if there is xmm register |
| 84 | // pressure, it can caused fewer GPRs to be held in the queue. |
| 85 | static cl::opt<unsigned> |
| 86 | NumRecentlyUsedRegs("linearscan-skip-count", |
Eric Christopher | cd075a4 | 2010-07-02 23:17:38 +0000 | [diff] [blame] | 87 | cl::desc("Number of registers for linearscan to remember" |
| 88 | "to skip."), |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 89 | cl::init(0), |
| 90 | cl::Hidden); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 91 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 92 | struct RALinScan : public MachineFunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 93 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 94 | RALinScan() : MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | 42acf06 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 95 | initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 96 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 97 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 98 | initializeRegisterCoalescerAnalysisGroup( |
| 99 | *PassRegistry::getPassRegistry()); |
| 100 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 101 | initializePreAllocSplittingPass(*PassRegistry::getPassRegistry()); |
| 102 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 103 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 104 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 105 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 106 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 107 | |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 108 | // Initialize the queue to record recently-used registers. |
| 109 | if (NumRecentlyUsedRegs > 0) |
| 110 | RecentRegs.resize(NumRecentlyUsedRegs, 0); |
David Greene | a96fc2f | 2009-11-20 21:13:27 +0000 | [diff] [blame] | 111 | RecentNext = RecentRegs.begin(); |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 112 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 113 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 114 | typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr; |
Owen Anderson | cd1dcbd | 2008-08-15 18:49:41 +0000 | [diff] [blame] | 115 | typedef SmallVector<IntervalPtr, 32> IntervalPtrs; |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 116 | private: |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 117 | /// RelatedRegClasses - This structure is built the first time a function is |
| 118 | /// compiled, and keeps track of which register classes have registers that |
| 119 | /// belong to multiple classes or have aliases that are in other classes. |
| 120 | EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses; |
Owen Anderson | 9738216 | 2008-08-13 23:36:23 +0000 | [diff] [blame] | 121 | DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg; |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 122 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 123 | // NextReloadMap - For each register in the map, it maps to the another |
| 124 | // register which is defined by a reload from the same stack slot and |
| 125 | // both reloads are in the same basic block. |
| 126 | DenseMap<unsigned, unsigned> NextReloadMap; |
| 127 | |
| 128 | // DowngradedRegs - A set of registers which are being "downgraded", i.e. |
| 129 | // un-favored for allocation. |
| 130 | SmallSet<unsigned, 8> DowngradedRegs; |
| 131 | |
| 132 | // DowngradeMap - A map from virtual registers to physical registers being |
| 133 | // downgraded for the virtual registers. |
| 134 | DenseMap<unsigned, unsigned> DowngradeMap; |
| 135 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 136 | MachineFunction* mf_; |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 137 | MachineRegisterInfo* mri_; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 138 | const TargetMachine* tm_; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 139 | const TargetRegisterInfo* tri_; |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 140 | const TargetInstrInfo* tii_; |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 141 | BitVector allocatableRegs_; |
Jim Grosbach | 067a648 | 2010-09-01 21:04:27 +0000 | [diff] [blame] | 142 | BitVector reservedRegs_; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 143 | LiveIntervals* li_; |
Jakob Stoklund Olesen | 9529a1c | 2010-07-19 18:41:20 +0000 | [diff] [blame] | 144 | MachineLoopInfo *loopInfo; |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 145 | |
| 146 | /// handled_ - Intervals are added to the handled_ set in the order of their |
| 147 | /// start value. This is uses for backtracking. |
| 148 | std::vector<LiveInterval*> handled_; |
| 149 | |
| 150 | /// fixed_ - Intervals that correspond to machine registers. |
| 151 | /// |
| 152 | IntervalPtrs fixed_; |
| 153 | |
| 154 | /// active_ - Intervals that are currently being processed, and which have a |
| 155 | /// live range active for the current point. |
| 156 | IntervalPtrs active_; |
| 157 | |
| 158 | /// inactive_ - Intervals that are currently being processed, but which have |
| 159 | /// a hold at the current point. |
| 160 | IntervalPtrs inactive_; |
| 161 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 162 | typedef std::priority_queue<LiveInterval*, |
Owen Anderson | cd1dcbd | 2008-08-15 18:49:41 +0000 | [diff] [blame] | 163 | SmallVector<LiveInterval*, 64>, |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 164 | greater_ptr<LiveInterval> > IntervalHeap; |
| 165 | IntervalHeap unhandled_; |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 166 | |
| 167 | /// regUse_ - Tracks register usage. |
| 168 | SmallVector<unsigned, 32> regUse_; |
| 169 | SmallVector<unsigned, 32> regUseBackUp_; |
| 170 | |
| 171 | /// vrm_ - Tracks register assignments. |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 172 | VirtRegMap* vrm_; |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 173 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 174 | std::auto_ptr<VirtRegRewriter> rewriter_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 175 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 176 | std::auto_ptr<Spiller> spiller_; |
| 177 | |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 178 | // The queue of recently-used registers. |
David Greene | a96fc2f | 2009-11-20 21:13:27 +0000 | [diff] [blame] | 179 | SmallVector<unsigned, 4> RecentRegs; |
| 180 | SmallVector<unsigned, 4>::iterator RecentNext; |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 181 | |
| 182 | // Record that we just picked this register. |
| 183 | void recordRecentlyUsed(unsigned reg) { |
| 184 | assert(reg != 0 && "Recently used register is NOREG!"); |
| 185 | if (!RecentRegs.empty()) { |
David Greene | a96fc2f | 2009-11-20 21:13:27 +0000 | [diff] [blame] | 186 | *RecentNext++ = reg; |
| 187 | if (RecentNext == RecentRegs.end()) |
| 188 | RecentNext = RecentRegs.begin(); |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 192 | public: |
| 193 | virtual const char* getPassName() const { |
| 194 | return "Linear Scan Register Allocator"; |
| 195 | } |
| 196 | |
| 197 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 198 | AU.setPreservesCFG(); |
Jakob Stoklund Olesen | e93198a | 2010-11-10 23:55:56 +0000 | [diff] [blame] | 199 | AU.addRequired<AliasAnalysis>(); |
| 200 | AU.addPreserved<AliasAnalysis>(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 201 | AU.addRequired<LiveIntervals>(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 202 | AU.addPreserved<SlotIndexes>(); |
Owen Anderson | 95dad83 | 2008-10-07 20:22:28 +0000 | [diff] [blame] | 203 | if (StrongPHIElim) |
| 204 | AU.addRequiredID(StrongPHIEliminationID); |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 205 | // Make sure PassManager knows which analyses to make available |
| 206 | // to coalescing and which analyses coalescing invalidates. |
| 207 | AU.addRequiredTransitive<RegisterCoalescer>(); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 208 | AU.addRequired<CalculateSpillWeights>(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 209 | if (PreSplitIntervals) |
| 210 | AU.addRequiredID(PreAllocSplittingID); |
Jakob Stoklund Olesen | 2d17293 | 2010-10-26 00:11:33 +0000 | [diff] [blame] | 211 | AU.addRequiredID(LiveStacksID); |
| 212 | AU.addPreservedID(LiveStacksID); |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 213 | AU.addRequired<MachineLoopInfo>(); |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 214 | AU.addPreserved<MachineLoopInfo>(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 215 | AU.addRequired<VirtRegMap>(); |
| 216 | AU.addPreserved<VirtRegMap>(); |
Jakob Stoklund Olesen | 42acf06 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 217 | AU.addRequired<LiveDebugVariables>(); |
| 218 | AU.addPreserved<LiveDebugVariables>(); |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 219 | AU.addRequiredID(MachineDominatorsID); |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 220 | AU.addPreservedID(MachineDominatorsID); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 221 | MachineFunctionPass::getAnalysisUsage(AU); |
| 222 | } |
| 223 | |
| 224 | /// runOnMachineFunction - register allocate the whole function |
| 225 | bool runOnMachineFunction(MachineFunction&); |
| 226 | |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 227 | // Determine if we skip this register due to its being recently used. |
| 228 | bool isRecentlyUsed(unsigned reg) const { |
| 229 | return std::find(RecentRegs.begin(), RecentRegs.end(), reg) != |
| 230 | RecentRegs.end(); |
| 231 | } |
| 232 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 233 | private: |
| 234 | /// linearScan - the linear scan algorithm |
| 235 | void linearScan(); |
| 236 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 237 | /// initIntervalSets - initialize the interval sets. |
| 238 | /// |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 239 | void initIntervalSets(); |
| 240 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 241 | /// processActiveIntervals - expire old intervals and move non-overlapping |
| 242 | /// ones to the inactive list. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 243 | void processActiveIntervals(SlotIndex CurPoint); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 244 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 245 | /// processInactiveIntervals - expire old intervals and move overlapping |
| 246 | /// ones to the active list. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 247 | void processInactiveIntervals(SlotIndex CurPoint); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 248 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 249 | /// hasNextReloadInterval - Return the next liveinterval that's being |
| 250 | /// defined by a reload from the same SS as the specified one. |
| 251 | LiveInterval *hasNextReloadInterval(LiveInterval *cur); |
| 252 | |
| 253 | /// DowngradeRegister - Downgrade a register for allocation. |
| 254 | void DowngradeRegister(LiveInterval *li, unsigned Reg); |
| 255 | |
| 256 | /// UpgradeRegister - Upgrade a register for allocation. |
| 257 | void UpgradeRegister(unsigned Reg); |
| 258 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 259 | /// assignRegOrStackSlotAtInterval - assign a register if one |
| 260 | /// is available, or spill. |
| 261 | void assignRegOrStackSlotAtInterval(LiveInterval* cur); |
| 262 | |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 263 | void updateSpillWeights(std::vector<float> &Weights, |
| 264 | unsigned reg, float weight, |
| 265 | const TargetRegisterClass *RC); |
| 266 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 267 | /// findIntervalsToSpill - Determine the intervals to spill for the |
| 268 | /// specified interval. It's passed the physical registers whose spill |
| 269 | /// weight is the lowest among all the registers whose live intervals |
| 270 | /// conflict with the interval. |
| 271 | void findIntervalsToSpill(LiveInterval *cur, |
| 272 | std::vector<std::pair<unsigned,float> > &Candidates, |
| 273 | unsigned NumCands, |
| 274 | SmallVector<LiveInterval*, 8> &SpillIntervals); |
| 275 | |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 276 | /// attemptTrivialCoalescing - If a simple interval is defined by a copy, |
Jim Grosbach | 977fa34 | 2010-07-27 18:36:27 +0000 | [diff] [blame] | 277 | /// try to allocate the definition to the same register as the source, |
| 278 | /// if the register is not defined during the life time of the interval. |
| 279 | /// This eliminates a copy, and is used to coalesce copies which were not |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 280 | /// coalesced away before allocation either due to dest and src being in |
| 281 | /// different register classes or because the coalescer was overly |
| 282 | /// conservative. |
| 283 | unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg); |
| 284 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 285 | /// |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 286 | /// Register usage / availability tracking helpers. |
| 287 | /// |
| 288 | |
| 289 | void initRegUses() { |
| 290 | regUse_.resize(tri_->getNumRegs(), 0); |
| 291 | regUseBackUp_.resize(tri_->getNumRegs(), 0); |
| 292 | } |
| 293 | |
| 294 | void finalizeRegUses() { |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 295 | #ifndef NDEBUG |
| 296 | // Verify all the registers are "freed". |
| 297 | bool Error = false; |
| 298 | for (unsigned i = 0, e = tri_->getNumRegs(); i != e; ++i) { |
| 299 | if (regUse_[i] != 0) { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 300 | dbgs() << tri_->getName(i) << " is still in use!\n"; |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 301 | Error = true; |
| 302 | } |
| 303 | } |
| 304 | if (Error) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 305 | llvm_unreachable(0); |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 306 | #endif |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 307 | regUse_.clear(); |
| 308 | regUseBackUp_.clear(); |
| 309 | } |
| 310 | |
| 311 | void addRegUse(unsigned physReg) { |
| 312 | assert(TargetRegisterInfo::isPhysicalRegister(physReg) && |
| 313 | "should be physical register!"); |
| 314 | ++regUse_[physReg]; |
| 315 | for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) |
| 316 | ++regUse_[*as]; |
| 317 | } |
| 318 | |
| 319 | void delRegUse(unsigned physReg) { |
| 320 | assert(TargetRegisterInfo::isPhysicalRegister(physReg) && |
| 321 | "should be physical register!"); |
| 322 | assert(regUse_[physReg] != 0); |
| 323 | --regUse_[physReg]; |
| 324 | for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) { |
| 325 | assert(regUse_[*as] != 0); |
| 326 | --regUse_[*as]; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | bool isRegAvail(unsigned physReg) const { |
| 331 | assert(TargetRegisterInfo::isPhysicalRegister(physReg) && |
| 332 | "should be physical register!"); |
| 333 | return regUse_[physReg] == 0; |
| 334 | } |
| 335 | |
| 336 | void backUpRegUses() { |
| 337 | regUseBackUp_ = regUse_; |
| 338 | } |
| 339 | |
| 340 | void restoreRegUses() { |
| 341 | regUse_ = regUseBackUp_; |
| 342 | } |
| 343 | |
| 344 | /// |
| 345 | /// Register handling helpers. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 346 | /// |
| 347 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 348 | /// getFreePhysReg - return a free physical register for this virtual |
| 349 | /// register interval if we have one, otherwise return 0. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 350 | unsigned getFreePhysReg(LiveInterval* cur); |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 351 | unsigned getFreePhysReg(LiveInterval* cur, |
| 352 | const TargetRegisterClass *RC, |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 353 | unsigned MaxInactiveCount, |
| 354 | SmallVector<unsigned, 256> &inactiveCounts, |
| 355 | bool SkipDGRegs); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 356 | |
Jim Grosbach | 5a4cbea | 2010-09-01 21:34:41 +0000 | [diff] [blame] | 357 | /// getFirstNonReservedPhysReg - return the first non-reserved physical |
| 358 | /// register in the register class. |
| 359 | unsigned getFirstNonReservedPhysReg(const TargetRegisterClass *RC) { |
| 360 | TargetRegisterClass::iterator aoe = RC->allocation_order_end(*mf_); |
| 361 | TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_); |
| 362 | while (i != aoe && reservedRegs_.test(*i)) |
| 363 | ++i; |
| 364 | assert(i != aoe && "All registers reserved?!"); |
| 365 | return *i; |
| 366 | } |
| 367 | |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 368 | void ComputeRelatedRegClasses(); |
| 369 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 370 | template <typename ItTy> |
| 371 | void printIntervals(const char* const str, ItTy i, ItTy e) const { |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 372 | DEBUG({ |
| 373 | if (str) |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 374 | dbgs() << str << " intervals:\n"; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 375 | |
| 376 | for (; i != e; ++i) { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 377 | dbgs() << "\t" << *i->first << " -> "; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 378 | |
| 379 | unsigned reg = i->first->reg; |
| 380 | if (TargetRegisterInfo::isVirtualRegister(reg)) |
| 381 | reg = vrm_->getPhys(reg); |
| 382 | |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 383 | dbgs() << tri_->getName(reg) << '\n'; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 384 | } |
| 385 | }); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 386 | } |
| 387 | }; |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 388 | char RALinScan::ID = 0; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 391 | INITIALIZE_PASS_BEGIN(RALinScan, "linearscan-regalloc", |
| 392 | "Linear Scan Register Allocator", false, false) |
| 393 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 394 | INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination) |
| 395 | INITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights) |
| 396 | INITIALIZE_PASS_DEPENDENCY(PreAllocSplitting) |
| 397 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) |
| 398 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 399 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 400 | INITIALIZE_AG_DEPENDENCY(RegisterCoalescer) |
Jakob Stoklund Olesen | e93198a | 2010-11-10 23:55:56 +0000 | [diff] [blame] | 401 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 402 | INITIALIZE_PASS_END(RALinScan, "linearscan-regalloc", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 403 | "Linear Scan Register Allocator", false, false) |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 404 | |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 405 | void RALinScan::ComputeRelatedRegClasses() { |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 406 | // First pass, add all reg classes to the union, and determine at least one |
| 407 | // reg class that each register is in. |
| 408 | bool HasAliases = false; |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 409 | for (TargetRegisterInfo::regclass_iterator RCI = tri_->regclass_begin(), |
| 410 | E = tri_->regclass_end(); RCI != E; ++RCI) { |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 411 | RelatedRegClasses.insert(*RCI); |
| 412 | for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end(); |
| 413 | I != E; ++I) { |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 414 | HasAliases = HasAliases || *tri_->getAliasSet(*I) != 0; |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 415 | |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 416 | const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I]; |
| 417 | if (PRC) { |
| 418 | // Already processed this register. Just make sure we know that |
| 419 | // multiple register classes share a register. |
| 420 | RelatedRegClasses.unionSets(PRC, *RCI); |
| 421 | } else { |
| 422 | PRC = *RCI; |
| 423 | } |
| 424 | } |
| 425 | } |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 426 | |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 427 | // Second pass, now that we know conservatively what register classes each reg |
| 428 | // belongs to, add info about aliases. We don't need to do this for targets |
| 429 | // without register aliases. |
| 430 | if (HasAliases) |
Owen Anderson | 9738216 | 2008-08-13 23:36:23 +0000 | [diff] [blame] | 431 | for (DenseMap<unsigned, const TargetRegisterClass*>::iterator |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 432 | I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end(); |
| 433 | I != E; ++I) |
Bob Wilson | adf9c8b | 2011-01-27 07:26:15 +0000 | [diff] [blame] | 434 | for (const unsigned *AS = tri_->getAliasSet(I->first); *AS; ++AS) { |
| 435 | const TargetRegisterClass *AliasClass = |
| 436 | OneClassForEachPhysReg.lookup(*AS); |
| 437 | if (AliasClass) |
| 438 | RelatedRegClasses.unionSets(I->second, AliasClass); |
| 439 | } |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 442 | /// attemptTrivialCoalescing - If a simple interval is defined by a copy, try |
| 443 | /// allocate the definition the same register as the source register if the |
| 444 | /// register is not defined during live time of the interval. If the interval is |
| 445 | /// killed by a copy, try to use the destination register. This eliminates a |
| 446 | /// copy. This is used to coalesce copies which were not coalesced away before |
| 447 | /// allocation either due to dest and src being in different register classes or |
| 448 | /// because the coalescer was overly conservative. |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 449 | unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) { |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 450 | unsigned Preference = vrm_->getRegAllocPref(cur.reg); |
| 451 | if ((Preference && Preference == Reg) || !cur.containsOneValue()) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 452 | return Reg; |
| 453 | |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 454 | // We cannot handle complicated live ranges. Simple linear stuff only. |
| 455 | if (cur.ranges.size() != 1) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 456 | return Reg; |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 457 | |
| 458 | const LiveRange &range = cur.ranges.front(); |
| 459 | |
| 460 | VNInfo *vni = range.valno; |
| 461 | if (vni->isUnused()) |
Bill Wendling | dc492e0 | 2009-12-05 07:30:23 +0000 | [diff] [blame] | 462 | return Reg; |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 463 | |
| 464 | unsigned CandReg; |
| 465 | { |
| 466 | MachineInstr *CopyMI; |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 467 | if ((CopyMI = li_->getInstructionFromIndex(vni->def)) && CopyMI->isCopy()) |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 468 | // Defined by a copy, try to extend SrcReg forward |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 469 | CandReg = CopyMI->getOperand(1).getReg(); |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 470 | else if (TrivCoalesceEnds && |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 471 | (CopyMI = li_->getInstructionFromIndex(range.end.getBaseIndex())) && |
| 472 | CopyMI->isCopy() && cur.reg == CopyMI->getOperand(1).getReg()) |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 473 | // Only used by a copy, try to extend DstReg backwards |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 474 | CandReg = CopyMI->getOperand(0).getReg(); |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 475 | else |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 476 | return Reg; |
Jakob Stoklund Olesen | e7fbdcd | 2010-11-19 05:45:24 +0000 | [diff] [blame] | 477 | |
| 478 | // If the target of the copy is a sub-register then don't coalesce. |
| 479 | if(CopyMI->getOperand(0).getSubReg()) |
| 480 | return Reg; |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 481 | } |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 482 | |
| 483 | if (TargetRegisterInfo::isVirtualRegister(CandReg)) { |
| 484 | if (!vrm_->isAssignedReg(CandReg)) |
| 485 | return Reg; |
| 486 | CandReg = vrm_->getPhys(CandReg); |
| 487 | } |
| 488 | if (Reg == CandReg) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 489 | return Reg; |
| 490 | |
Evan Cheng | 841ee1a | 2008-09-18 22:38:47 +0000 | [diff] [blame] | 491 | const TargetRegisterClass *RC = mri_->getRegClass(cur.reg); |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 492 | if (!RC->contains(CandReg)) |
| 493 | return Reg; |
| 494 | |
| 495 | if (li_->conflictsWithPhysReg(cur, *vrm_, CandReg)) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 496 | return Reg; |
| 497 | |
Bill Wendling | dc492e0 | 2009-12-05 07:30:23 +0000 | [diff] [blame] | 498 | // Try to coalesce. |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 499 | DEBUG(dbgs() << "Coalescing: " << cur << " -> " << tri_->getName(CandReg) |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 500 | << '\n'); |
| 501 | vrm_->clearVirt(cur.reg); |
| 502 | vrm_->assignVirt2Phys(cur.reg, CandReg); |
Bill Wendling | dc492e0 | 2009-12-05 07:30:23 +0000 | [diff] [blame] | 503 | |
Jakob Stoklund Olesen | cf97036 | 2009-12-10 17:48:32 +0000 | [diff] [blame] | 504 | ++NumCoalesce; |
| 505 | return CandReg; |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 508 | bool RALinScan::runOnMachineFunction(MachineFunction &fn) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 509 | mf_ = &fn; |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 510 | mri_ = &fn.getRegInfo(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 511 | tm_ = &fn.getTarget(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 512 | tri_ = tm_->getRegisterInfo(); |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 513 | tii_ = tm_->getInstrInfo(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 514 | allocatableRegs_ = tri_->getAllocatableSet(fn); |
Jim Grosbach | 067a648 | 2010-09-01 21:04:27 +0000 | [diff] [blame] | 515 | reservedRegs_ = tri_->getReservedRegs(fn); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 516 | li_ = &getAnalysis<LiveIntervals>(); |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 517 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
Chris Lattner | f348e3a | 2004-11-18 04:33:31 +0000 | [diff] [blame] | 518 | |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 519 | // We don't run the coalescer here because we have no reason to |
| 520 | // interact with it. If the coalescer requires interaction, it |
| 521 | // won't do anything. If it doesn't require interaction, we assume |
| 522 | // it was run as a separate pass. |
| 523 | |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 524 | // If this is the first function compiled, compute the related reg classes. |
| 525 | if (RelatedRegClasses.empty()) |
| 526 | ComputeRelatedRegClasses(); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 527 | |
| 528 | // Also resize register usage trackers. |
| 529 | initRegUses(); |
| 530 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 531 | vrm_ = &getAnalysis<VirtRegMap>(); |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 532 | if (!rewriter_.get()) rewriter_.reset(createVirtRegRewriter()); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 533 | |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 534 | spiller_.reset(createSpiller(*this, *mf_, *vrm_)); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 535 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 536 | initIntervalSets(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 537 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 538 | linearScan(); |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 539 | |
Chris Lattner | b0f31bf | 2005-01-23 22:45:13 +0000 | [diff] [blame] | 540 | // Rewrite spill code and update the PhysRegsUsed set. |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 541 | rewriter_->runOnMachineFunction(*mf_, *vrm_, li_); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 542 | |
Jakob Stoklund Olesen | 42acf06 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 543 | // Write out new DBG_VALUE instructions. |
| 544 | getAnalysis<LiveDebugVariables>().emitDebugValues(vrm_); |
| 545 | |
Dan Gohman | 51cd9d6 | 2008-06-23 23:51:16 +0000 | [diff] [blame] | 546 | assert(unhandled_.empty() && "Unhandled live intervals remain!"); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 547 | |
| 548 | finalizeRegUses(); |
| 549 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 550 | fixed_.clear(); |
| 551 | active_.clear(); |
| 552 | inactive_.clear(); |
| 553 | handled_.clear(); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 554 | NextReloadMap.clear(); |
| 555 | DowngradedRegs.clear(); |
| 556 | DowngradeMap.clear(); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 557 | spiller_.reset(0); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 558 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 559 | return true; |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 562 | /// initIntervalSets - initialize the interval sets. |
| 563 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 564 | void RALinScan::initIntervalSets() |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 565 | { |
| 566 | assert(unhandled_.empty() && fixed_.empty() && |
| 567 | active_.empty() && inactive_.empty() && |
| 568 | "interval sets should be empty on initialization"); |
| 569 | |
Owen Anderson | cd1dcbd | 2008-08-15 18:49:41 +0000 | [diff] [blame] | 570 | handled_.reserve(li_->getNumIntervals()); |
| 571 | |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 572 | for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 573 | if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 574 | if (!i->second->empty()) { |
| 575 | mri_->setPhysRegUsed(i->second->reg); |
| 576 | fixed_.push_back(std::make_pair(i->second, i->second->begin())); |
| 577 | } |
| 578 | } else { |
| 579 | if (i->second->empty()) { |
| 580 | assignRegOrStackSlotAtInterval(i->second); |
| 581 | } |
| 582 | else |
| 583 | unhandled_.push(i->second); |
| 584 | } |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 588 | void RALinScan::linearScan() { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 589 | // linear scan algorithm |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 590 | DEBUG({ |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 591 | dbgs() << "********** LINEAR SCAN **********\n" |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 592 | << "********** Function: " |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 593 | << mf_->getFunction()->getName() << '\n'; |
| 594 | printIntervals("fixed", fixed_.begin(), fixed_.end()); |
| 595 | }); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 596 | |
| 597 | while (!unhandled_.empty()) { |
| 598 | // pick the interval with the earliest start point |
| 599 | LiveInterval* cur = unhandled_.top(); |
| 600 | unhandled_.pop(); |
Evan Cheng | 11923cc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 601 | ++NumIters; |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 602 | DEBUG(dbgs() << "\n*** CURRENT ***: " << *cur << '\n'); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 603 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 604 | assert(!cur->empty() && "Empty interval in unhandled set."); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 605 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 606 | processActiveIntervals(cur->beginIndex()); |
| 607 | processInactiveIntervals(cur->beginIndex()); |
| 608 | |
| 609 | assert(TargetRegisterInfo::isVirtualRegister(cur->reg) && |
| 610 | "Can only allocate virtual registers!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 611 | |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 612 | // Allocating a virtual register. try to find a free |
| 613 | // physical register or spill an interval (possibly this one) in order to |
| 614 | // assign it one. |
| 615 | assignRegOrStackSlotAtInterval(cur); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 616 | |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 617 | DEBUG({ |
| 618 | printIntervals("active", active_.begin(), active_.end()); |
| 619 | printIntervals("inactive", inactive_.begin(), inactive_.end()); |
| 620 | }); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 621 | } |
Alkis Evlogimenos | 7d629b5 | 2004-01-07 09:20:58 +0000 | [diff] [blame] | 622 | |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 623 | // Expire any remaining active intervals |
Evan Cheng | 11923cc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 624 | while (!active_.empty()) { |
| 625 | IntervalPtr &IP = active_.back(); |
| 626 | unsigned reg = IP.first->reg; |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 627 | DEBUG(dbgs() << "\tinterval " << *IP.first << " expired\n"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 628 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 629 | "Can only allocate virtual registers!"); |
| 630 | reg = vrm_->getPhys(reg); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 631 | delRegUse(reg); |
Evan Cheng | 11923cc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 632 | active_.pop_back(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 633 | } |
Alkis Evlogimenos | 7d629b5 | 2004-01-07 09:20:58 +0000 | [diff] [blame] | 634 | |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 635 | // Expire any remaining inactive intervals |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 636 | DEBUG({ |
| 637 | for (IntervalPtrs::reverse_iterator |
| 638 | i = inactive_.rbegin(); i != inactive_.rend(); ++i) |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 639 | dbgs() << "\tinterval " << *i->first << " expired\n"; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 640 | }); |
Evan Cheng | 11923cc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 641 | inactive_.clear(); |
Alkis Evlogimenos | b7be115 | 2004-01-13 20:42:08 +0000 | [diff] [blame] | 642 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 643 | // Add live-ins to every BB except for entry. Also perform trivial coalescing. |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 644 | MachineFunction::iterator EntryMBB = mf_->begin(); |
Evan Cheng | a5bfc97 | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 645 | SmallVector<MachineBasicBlock*, 8> LiveInMBBs; |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 646 | for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 647 | LiveInterval &cur = *i->second; |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 648 | unsigned Reg = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 649 | bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 650 | if (isPhys) |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 651 | Reg = cur.reg; |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 652 | else if (vrm_->isAssignedReg(cur.reg)) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 653 | Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg)); |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 654 | if (!Reg) |
| 655 | continue; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 656 | // Ignore splited live intervals. |
| 657 | if (!isPhys && vrm_->getPreSplitReg(cur.reg)) |
| 658 | continue; |
Evan Cheng | 550aacb | 2009-06-04 20:28:22 +0000 | [diff] [blame] | 659 | |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 660 | for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end(); |
| 661 | I != E; ++I) { |
| 662 | const LiveRange &LR = *I; |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 663 | if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) { |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i) |
Evan Cheng | 073e7e5 | 2009-06-04 20:53:36 +0000 | [diff] [blame] | 665 | if (LiveInMBBs[i] != EntryMBB) { |
| 666 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 667 | "Adding a virtual register to livein set?"); |
Evan Cheng | 3f4b80e | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 668 | LiveInMBBs[i]->addLiveIn(Reg); |
Evan Cheng | 073e7e5 | 2009-06-04 20:53:36 +0000 | [diff] [blame] | 669 | } |
Evan Cheng | a5bfc97 | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 670 | LiveInMBBs.clear(); |
Evan Cheng | 9fc508f | 2007-02-16 09:05:02 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 675 | DEBUG(dbgs() << *vrm_); |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 676 | |
| 677 | // Look for physical registers that end up not being allocated even though |
| 678 | // register allocator had to spill other registers in its register class. |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 679 | if (!vrm_->FindUnusedRegisters(li_)) |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 680 | return; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 683 | /// processActiveIntervals - expire old intervals and move non-overlapping ones |
| 684 | /// to the inactive list. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 685 | void RALinScan::processActiveIntervals(SlotIndex CurPoint) |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 686 | { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 687 | DEBUG(dbgs() << "\tprocessing active intervals:\n"); |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 688 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 689 | for (unsigned i = 0, e = active_.size(); i != e; ++i) { |
| 690 | LiveInterval *Interval = active_[i].first; |
| 691 | LiveInterval::iterator IntervalPos = active_[i].second; |
| 692 | unsigned reg = Interval->reg; |
Alkis Evlogimenos | ed54373 | 2004-09-01 22:52:29 +0000 | [diff] [blame] | 693 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 694 | IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); |
| 695 | |
| 696 | if (IntervalPos == Interval->end()) { // Remove expired intervals. |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 697 | DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 698 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 699 | "Can only allocate virtual registers!"); |
| 700 | reg = vrm_->getPhys(reg); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 701 | delRegUse(reg); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 702 | |
| 703 | // Pop off the end of the list. |
| 704 | active_[i] = active_.back(); |
| 705 | active_.pop_back(); |
| 706 | --i; --e; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 707 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 708 | } else if (IntervalPos->start > CurPoint) { |
| 709 | // Move inactive intervals to inactive list. |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 710 | DEBUG(dbgs() << "\t\tinterval " << *Interval << " inactive\n"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 711 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 712 | "Can only allocate virtual registers!"); |
| 713 | reg = vrm_->getPhys(reg); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 714 | delRegUse(reg); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 715 | // add to inactive. |
| 716 | inactive_.push_back(std::make_pair(Interval, IntervalPos)); |
| 717 | |
| 718 | // Pop off the end of the list. |
| 719 | active_[i] = active_.back(); |
| 720 | active_.pop_back(); |
| 721 | --i; --e; |
| 722 | } else { |
| 723 | // Otherwise, just update the iterator position. |
| 724 | active_[i].second = IntervalPos; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 729 | /// processInactiveIntervals - expire old intervals and move overlapping |
| 730 | /// ones to the active list. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 731 | void RALinScan::processInactiveIntervals(SlotIndex CurPoint) |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 732 | { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 733 | DEBUG(dbgs() << "\tprocessing inactive intervals:\n"); |
Chris Lattner | 365b95f | 2004-11-18 04:13:02 +0000 | [diff] [blame] | 734 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 735 | for (unsigned i = 0, e = inactive_.size(); i != e; ++i) { |
| 736 | LiveInterval *Interval = inactive_[i].first; |
| 737 | LiveInterval::iterator IntervalPos = inactive_[i].second; |
| 738 | unsigned reg = Interval->reg; |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 739 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 740 | IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 741 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 742 | if (IntervalPos == Interval->end()) { // remove expired intervals. |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 743 | DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n"); |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 744 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 745 | // Pop off the end of the list. |
| 746 | inactive_[i] = inactive_.back(); |
| 747 | inactive_.pop_back(); |
| 748 | --i; --e; |
| 749 | } else if (IntervalPos->start <= CurPoint) { |
| 750 | // move re-activated intervals in active list |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 751 | DEBUG(dbgs() << "\t\tinterval " << *Interval << " active\n"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 752 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 753 | "Can only allocate virtual registers!"); |
| 754 | reg = vrm_->getPhys(reg); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 755 | addRegUse(reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 756 | // add to active |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 757 | active_.push_back(std::make_pair(Interval, IntervalPos)); |
| 758 | |
| 759 | // Pop off the end of the list. |
| 760 | inactive_[i] = inactive_.back(); |
| 761 | inactive_.pop_back(); |
| 762 | --i; --e; |
| 763 | } else { |
| 764 | // Otherwise, just update the iterator position. |
| 765 | inactive_[i].second = IntervalPos; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 770 | /// updateSpillWeights - updates the spill weights of the specifed physical |
| 771 | /// register and its weight. |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 772 | void RALinScan::updateSpillWeights(std::vector<float> &Weights, |
| 773 | unsigned reg, float weight, |
| 774 | const TargetRegisterClass *RC) { |
| 775 | SmallSet<unsigned, 4> Processed; |
| 776 | SmallSet<unsigned, 4> SuperAdded; |
| 777 | SmallVector<unsigned, 4> Supers; |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 778 | Weights[reg] += weight; |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 779 | Processed.insert(reg); |
| 780 | for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) { |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 781 | Weights[*as] += weight; |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 782 | Processed.insert(*as); |
| 783 | if (tri_->isSubRegister(*as, reg) && |
| 784 | SuperAdded.insert(*as) && |
| 785 | RC->contains(*as)) { |
| 786 | Supers.push_back(*as); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // If the alias is a super-register, and the super-register is in the |
| 791 | // register class we are trying to allocate. Then add the weight to all |
| 792 | // sub-registers of the super-register even if they are not aliases. |
| 793 | // e.g. allocating for GR32, bh is not used, updating bl spill weight. |
| 794 | // bl should get the same spill weight otherwise it will be choosen |
| 795 | // as a spill candidate since spilling bh doesn't make ebx available. |
| 796 | for (unsigned i = 0, e = Supers.size(); i != e; ++i) { |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 797 | for (const unsigned *sr = tri_->getSubRegisters(Supers[i]); *sr; ++sr) |
| 798 | if (!Processed.count(*sr)) |
| 799 | Weights[*sr] += weight; |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 800 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 803 | static |
| 804 | RALinScan::IntervalPtrs::iterator |
| 805 | FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) { |
| 806 | for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); |
| 807 | I != E; ++I) |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 808 | if (I->first == LI) return I; |
| 809 | return IP.end(); |
| 810 | } |
| 811 | |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 812 | static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, |
| 813 | SlotIndex Point){ |
Chris Lattner | 19828d4 | 2004-11-18 03:49:30 +0000 | [diff] [blame] | 814 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 815 | RALinScan::IntervalPtr &IP = V[i]; |
Chris Lattner | 19828d4 | 2004-11-18 03:49:30 +0000 | [diff] [blame] | 816 | LiveInterval::iterator I = std::upper_bound(IP.first->begin(), |
| 817 | IP.second, Point); |
| 818 | if (I != IP.first->begin()) --I; |
| 819 | IP.second = I; |
| 820 | } |
| 821 | } |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 822 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 823 | /// getConflictWeight - Return the number of conflicts between cur |
| 824 | /// live interval and defs and uses of Reg weighted by loop depthes. |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 825 | static |
| 826 | float getConflictWeight(LiveInterval *cur, unsigned Reg, LiveIntervals *li_, |
| 827 | MachineRegisterInfo *mri_, |
Jakob Stoklund Olesen | 9529a1c | 2010-07-19 18:41:20 +0000 | [diff] [blame] | 828 | MachineLoopInfo *loopInfo) { |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 829 | float Conflicts = 0; |
| 830 | for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg), |
| 831 | E = mri_->reg_end(); I != E; ++I) { |
| 832 | MachineInstr *MI = &*I; |
| 833 | if (cur->liveAt(li_->getInstructionIndex(MI))) { |
| 834 | unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent()); |
Chris Lattner | 87565c1 | 2010-05-15 17:10:24 +0000 | [diff] [blame] | 835 | Conflicts += std::pow(10.0f, (float)loopDepth); |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | return Conflicts; |
| 839 | } |
| 840 | |
| 841 | /// findIntervalsToSpill - Determine the intervals to spill for the |
| 842 | /// specified interval. It's passed the physical registers whose spill |
| 843 | /// weight is the lowest among all the registers whose live intervals |
| 844 | /// conflict with the interval. |
| 845 | void RALinScan::findIntervalsToSpill(LiveInterval *cur, |
| 846 | std::vector<std::pair<unsigned,float> > &Candidates, |
| 847 | unsigned NumCands, |
| 848 | SmallVector<LiveInterval*, 8> &SpillIntervals) { |
| 849 | // We have figured out the *best* register to spill. But there are other |
| 850 | // registers that are pretty good as well (spill weight within 3%). Spill |
| 851 | // the one that has fewest defs and uses that conflict with cur. |
| 852 | float Conflicts[3] = { 0.0f, 0.0f, 0.0f }; |
| 853 | SmallVector<LiveInterval*, 8> SLIs[3]; |
| 854 | |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 855 | DEBUG({ |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 856 | dbgs() << "\tConsidering " << NumCands << " candidates: "; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 857 | for (unsigned i = 0; i != NumCands; ++i) |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 858 | dbgs() << tri_->getName(Candidates[i].first) << " "; |
| 859 | dbgs() << "\n"; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 860 | }); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 861 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 862 | // Calculate the number of conflicts of each candidate. |
| 863 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { |
| 864 | unsigned Reg = i->first->reg; |
| 865 | unsigned PhysReg = vrm_->getPhys(Reg); |
| 866 | if (!cur->overlapsFrom(*i->first, i->second)) |
| 867 | continue; |
| 868 | for (unsigned j = 0; j < NumCands; ++j) { |
| 869 | unsigned Candidate = Candidates[j].first; |
| 870 | if (tri_->regsOverlap(PhysReg, Candidate)) { |
| 871 | if (NumCands > 1) |
| 872 | Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo); |
| 873 | SLIs[j].push_back(i->first); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){ |
| 879 | unsigned Reg = i->first->reg; |
| 880 | unsigned PhysReg = vrm_->getPhys(Reg); |
| 881 | if (!cur->overlapsFrom(*i->first, i->second-1)) |
| 882 | continue; |
| 883 | for (unsigned j = 0; j < NumCands; ++j) { |
| 884 | unsigned Candidate = Candidates[j].first; |
| 885 | if (tri_->regsOverlap(PhysReg, Candidate)) { |
| 886 | if (NumCands > 1) |
| 887 | Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo); |
| 888 | SLIs[j].push_back(i->first); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | // Which is the best candidate? |
| 894 | unsigned BestCandidate = 0; |
| 895 | float MinConflicts = Conflicts[0]; |
| 896 | for (unsigned i = 1; i != NumCands; ++i) { |
| 897 | if (Conflicts[i] < MinConflicts) { |
| 898 | BestCandidate = i; |
| 899 | MinConflicts = Conflicts[i]; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(), |
| 904 | std::back_inserter(SpillIntervals)); |
| 905 | } |
| 906 | |
| 907 | namespace { |
| 908 | struct WeightCompare { |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 909 | private: |
| 910 | const RALinScan &Allocator; |
| 911 | |
| 912 | public: |
Douglas Gregor | cabdd74 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 913 | WeightCompare(const RALinScan &Alloc) : Allocator(Alloc) {} |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 914 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 915 | typedef std::pair<unsigned, float> RegWeightPair; |
| 916 | bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const { |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 917 | return LHS.second < RHS.second && !Allocator.isRecentlyUsed(LHS.first); |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 918 | } |
| 919 | }; |
| 920 | } |
| 921 | |
| 922 | static bool weightsAreClose(float w1, float w2) { |
| 923 | if (!NewHeuristic) |
| 924 | return false; |
| 925 | |
| 926 | float diff = w1 - w2; |
| 927 | if (diff <= 0.02f) // Within 0.02f |
| 928 | return true; |
| 929 | return (diff / w2) <= 0.05f; // Within 5%. |
| 930 | } |
| 931 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 932 | LiveInterval *RALinScan::hasNextReloadInterval(LiveInterval *cur) { |
| 933 | DenseMap<unsigned, unsigned>::iterator I = NextReloadMap.find(cur->reg); |
| 934 | if (I == NextReloadMap.end()) |
| 935 | return 0; |
| 936 | return &li_->getInterval(I->second); |
| 937 | } |
| 938 | |
| 939 | void RALinScan::DowngradeRegister(LiveInterval *li, unsigned Reg) { |
Jakob Stoklund Olesen | 19bb35d | 2011-01-06 01:33:22 +0000 | [diff] [blame] | 940 | for (const unsigned *AS = tri_->getOverlaps(Reg); *AS; ++AS) { |
| 941 | bool isNew = DowngradedRegs.insert(*AS); |
| 942 | (void)isNew; // Silence compiler warning. |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 943 | assert(isNew && "Multiple reloads holding the same register?"); |
| 944 | DowngradeMap.insert(std::make_pair(li->reg, *AS)); |
| 945 | } |
| 946 | ++NumDowngrade; |
| 947 | } |
| 948 | |
| 949 | void RALinScan::UpgradeRegister(unsigned Reg) { |
| 950 | if (Reg) { |
| 951 | DowngradedRegs.erase(Reg); |
| 952 | for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS) |
| 953 | DowngradedRegs.erase(*AS); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | namespace { |
| 958 | struct LISorter { |
| 959 | bool operator()(LiveInterval* A, LiveInterval* B) { |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 960 | return A->beginIndex() < B->beginIndex(); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 961 | } |
| 962 | }; |
| 963 | } |
| 964 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 965 | /// assignRegOrStackSlotAtInterval - assign a register if one is available, or |
| 966 | /// spill. |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 967 | void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) { |
Jakob Stoklund Olesen | fd900a2 | 2010-11-16 19:55:12 +0000 | [diff] [blame] | 968 | const TargetRegisterClass *RC = mri_->getRegClass(cur->reg); |
| 969 | DEBUG(dbgs() << "\tallocating current interval from " |
| 970 | << RC->getName() << ": "); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 971 | |
Evan Cheng | f30a49d | 2008-04-03 16:40:27 +0000 | [diff] [blame] | 972 | // This is an implicitly defined live interval, just assign any register. |
Evan Cheng | f30a49d | 2008-04-03 16:40:27 +0000 | [diff] [blame] | 973 | if (cur->empty()) { |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 974 | unsigned physReg = vrm_->getRegAllocPref(cur->reg); |
Jim Grosbach | 5a4cbea | 2010-09-01 21:34:41 +0000 | [diff] [blame] | 975 | if (!physReg) |
| 976 | physReg = getFirstNonReservedPhysReg(RC); |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 977 | DEBUG(dbgs() << tri_->getName(physReg) << '\n'); |
Evan Cheng | f30a49d | 2008-04-03 16:40:27 +0000 | [diff] [blame] | 978 | // Note the register is not really in use. |
| 979 | vrm_->assignVirt2Phys(cur->reg, physReg); |
Evan Cheng | f30a49d | 2008-04-03 16:40:27 +0000 | [diff] [blame] | 980 | return; |
| 981 | } |
| 982 | |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 983 | backUpRegUses(); |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 984 | |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 985 | std::vector<std::pair<unsigned, float> > SpillWeightsToAdd; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 986 | SlotIndex StartPosition = cur->beginIndex(); |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 987 | const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 988 | |
Evan Cheng | d0deec2 | 2009-01-20 00:16:18 +0000 | [diff] [blame] | 989 | // If start of this live interval is defined by a move instruction and its |
| 990 | // source is assigned a physical register that is compatible with the target |
| 991 | // register class, then we should try to assign it the same register. |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 992 | // This can happen when the move is from a larger register class to a smaller |
| 993 | // one, e.g. X86::mov32to32_. These move instructions are not coalescable. |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 994 | if (!vrm_->getRegAllocPref(cur->reg) && cur->hasAtLeastOneValue()) { |
Evan Cheng | d0deec2 | 2009-01-20 00:16:18 +0000 | [diff] [blame] | 995 | VNInfo *vni = cur->begin()->valno; |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 996 | if (!vni->isUnused()) { |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 997 | MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 998 | if (CopyMI && CopyMI->isCopy()) { |
| 999 | unsigned DstSubReg = CopyMI->getOperand(0).getSubReg(); |
| 1000 | unsigned SrcReg = CopyMI->getOperand(1).getReg(); |
| 1001 | unsigned SrcSubReg = CopyMI->getOperand(1).getSubReg(); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1002 | unsigned Reg = 0; |
| 1003 | if (TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
| 1004 | Reg = SrcReg; |
| 1005 | else if (vrm_->isAssignedReg(SrcReg)) |
| 1006 | Reg = vrm_->getPhys(SrcReg); |
| 1007 | if (Reg) { |
| 1008 | if (SrcSubReg) |
| 1009 | Reg = tri_->getSubReg(Reg, SrcSubReg); |
| 1010 | if (DstSubReg) |
| 1011 | Reg = tri_->getMatchingSuperReg(Reg, DstSubReg, RC); |
| 1012 | if (Reg && allocatableRegs_[Reg] && RC->contains(Reg)) |
| 1013 | mri_->setRegAllocationHint(cur->reg, 0, Reg); |
| 1014 | } |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1019 | // For every interval in inactive we overlap with, mark the |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1020 | // register as not free and update spill weights. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1021 | for (IntervalPtrs::const_iterator i = inactive_.begin(), |
| 1022 | e = inactive_.end(); i != e; ++i) { |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1023 | unsigned Reg = i->first->reg; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1024 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1025 | "Can only allocate virtual registers!"); |
Evan Cheng | 841ee1a | 2008-09-18 22:38:47 +0000 | [diff] [blame] | 1026 | const TargetRegisterClass *RegRC = mri_->getRegClass(Reg); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1027 | // If this is not in a related reg class to the register we're allocating, |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1028 | // don't check it. |
| 1029 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && |
| 1030 | cur->overlapsFrom(*i->first, i->second-1)) { |
| 1031 | Reg = vrm_->getPhys(Reg); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1032 | addRegUse(Reg); |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1033 | SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight)); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1034 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1035 | } |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1037 | // Speculatively check to see if we can get a register right now. If not, |
| 1038 | // we know we won't be able to by adding more constraints. If so, we can |
| 1039 | // check to see if it is valid. Doing an exhaustive search of the fixed_ list |
| 1040 | // is very bad (it contains all callee clobbered registers for any functions |
| 1041 | // with a call), so we want to avoid doing that if possible. |
| 1042 | unsigned physReg = getFreePhysReg(cur); |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 1043 | unsigned BestPhysReg = physReg; |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1044 | if (physReg) { |
| 1045 | // We got a register. However, if it's in the fixed_ list, we might |
Chris Lattner | e836ad6 | 2005-08-30 21:03:36 +0000 | [diff] [blame] | 1046 | // conflict with it. Check to see if we conflict with it or any of its |
| 1047 | // aliases. |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 1048 | SmallSet<unsigned, 8> RegAliases; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1049 | for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS) |
Chris Lattner | e836ad6 | 2005-08-30 21:03:36 +0000 | [diff] [blame] | 1050 | RegAliases.insert(*AS); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1052 | bool ConflictsWithFixed = false; |
| 1053 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
Jim Laskey | e719d9f | 2006-10-24 14:35:25 +0000 | [diff] [blame] | 1054 | IntervalPtr &IP = fixed_[i]; |
| 1055 | if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) { |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1056 | // Okay, this reg is on the fixed list. Check to see if we actually |
| 1057 | // conflict. |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1058 | LiveInterval *I = IP.first; |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 1059 | if (I->endIndex() > StartPosition) { |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1060 | LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); |
| 1061 | IP.second = II; |
| 1062 | if (II != I->begin() && II->start > StartPosition) |
| 1063 | --II; |
Chris Lattner | e836ad6 | 2005-08-30 21:03:36 +0000 | [diff] [blame] | 1064 | if (cur->overlapsFrom(*I, II)) { |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1065 | ConflictsWithFixed = true; |
Chris Lattner | e836ad6 | 2005-08-30 21:03:36 +0000 | [diff] [blame] | 1066 | break; |
| 1067 | } |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | f348e3a | 2004-11-18 04:33:31 +0000 | [diff] [blame] | 1069 | } |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 1070 | } |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1072 | // Okay, the register picked by our speculative getFreePhysReg call turned |
| 1073 | // out to be in use. Actually add all of the conflicting fixed registers to |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1074 | // regUse_ so we can do an accurate query. |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1075 | if (ConflictsWithFixed) { |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1076 | // For every interval in fixed we overlap with, mark the register as not |
| 1077 | // free and update spill weights. |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1078 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
| 1079 | IntervalPtr &IP = fixed_[i]; |
| 1080 | LiveInterval *I = IP.first; |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1081 | |
| 1082 | const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg]; |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1083 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 1084 | I->endIndex() > StartPosition) { |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1085 | LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); |
| 1086 | IP.second = II; |
| 1087 | if (II != I->begin() && II->start > StartPosition) |
| 1088 | --II; |
| 1089 | if (cur->overlapsFrom(*I, II)) { |
| 1090 | unsigned reg = I->reg; |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1091 | addRegUse(reg); |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1092 | SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight)); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 1096 | |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1097 | // Using the newly updated regUse_ object, which includes conflicts in the |
Chris Lattner | a411cbc | 2005-08-22 20:59:30 +0000 | [diff] [blame] | 1098 | // future, see if there are any registers available. |
| 1099 | physReg = getFreePhysReg(cur); |
| 1100 | } |
| 1101 | } |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1102 | |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1103 | // Restore the physical register tracker, removing information about the |
| 1104 | // future. |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1105 | restoreRegUses(); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1106 | |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1107 | // If we find a free register, we are done: assign this virtual to |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1108 | // the free physical register and add this interval to the active |
| 1109 | // list. |
| 1110 | if (physReg) { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1111 | DEBUG(dbgs() << tri_->getName(physReg) << '\n'); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1112 | vrm_->assignVirt2Phys(cur->reg, physReg); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1113 | addRegUse(physReg); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1114 | active_.push_back(std::make_pair(cur, cur->begin())); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1115 | handled_.push_back(cur); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1116 | |
| 1117 | // "Upgrade" the physical register since it has been allocated. |
| 1118 | UpgradeRegister(physReg); |
| 1119 | if (LiveInterval *NextReloadLI = hasNextReloadInterval(cur)) { |
| 1120 | // "Downgrade" physReg to try to keep physReg from being allocated until |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1121 | // the next reload from the same SS is allocated. |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1122 | mri_->setRegAllocationHint(NextReloadLI->reg, 0, physReg); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1123 | DowngradeRegister(cur, physReg); |
| 1124 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1127 | DEBUG(dbgs() << "no free registers\n"); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1129 | // Compile the spill weights into an array that is better for scanning. |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1130 | std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f); |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1131 | for (std::vector<std::pair<unsigned, float> >::iterator |
| 1132 | I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I) |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 1133 | updateSpillWeights(SpillWeights, I->first, I->second, RC); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1135 | // for each interval in active, update spill weights. |
| 1136 | for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end(); |
| 1137 | i != e; ++i) { |
| 1138 | unsigned reg = i->first->reg; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1139 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1140 | "Can only allocate virtual registers!"); |
| 1141 | reg = vrm_->getPhys(reg); |
Evan Cheng | 5d088fe | 2009-03-23 22:57:19 +0000 | [diff] [blame] | 1142 | updateSpillWeights(SpillWeights, reg, i->first->weight, RC); |
Chris Lattner | a6c1750 | 2005-08-22 20:20:42 +0000 | [diff] [blame] | 1143 | } |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1144 | |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1145 | DEBUG(dbgs() << "\tassigning stack slot at interval "<< *cur << ":\n"); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | c8e2c55 | 2006-03-25 23:00:56 +0000 | [diff] [blame] | 1147 | // Find a register to spill. |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 1148 | float minWeight = HUGE_VALF; |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 1149 | unsigned minReg = 0; |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1150 | |
| 1151 | bool Found = false; |
| 1152 | std::vector<std::pair<unsigned,float> > RegsWeights; |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 1153 | if (!minReg || SpillWeights[minReg] == HUGE_VALF) |
| 1154 | for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), |
| 1155 | e = RC->allocation_order_end(*mf_); i != e; ++i) { |
| 1156 | unsigned reg = *i; |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1157 | float regWeight = SpillWeights[reg]; |
Jim Grosbach | 188da25 | 2010-09-01 22:48:34 +0000 | [diff] [blame] | 1158 | // Don't even consider reserved regs. |
| 1159 | if (reservedRegs_.test(reg)) |
| 1160 | continue; |
Jim Grosbach | 067a648 | 2010-09-01 21:04:27 +0000 | [diff] [blame] | 1161 | // Skip recently allocated registers and reserved registers. |
Jim Grosbach | 188da25 | 2010-09-01 22:48:34 +0000 | [diff] [blame] | 1162 | if (minWeight > regWeight && !isRecentlyUsed(reg)) |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1163 | Found = true; |
| 1164 | RegsWeights.push_back(std::make_pair(reg, regWeight)); |
Alkis Evlogimenos | 3bf564a | 2003-12-23 18:00:33 +0000 | [diff] [blame] | 1165 | } |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | c8e2c55 | 2006-03-25 23:00:56 +0000 | [diff] [blame] | 1167 | // If we didn't find a register that is spillable, try aliases? |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1168 | if (!Found) { |
Evan Cheng | 3b6d56c | 2006-05-12 19:07:46 +0000 | [diff] [blame] | 1169 | for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), |
| 1170 | e = RC->allocation_order_end(*mf_); i != e; ++i) { |
| 1171 | unsigned reg = *i; |
Jim Grosbach | 067a648 | 2010-09-01 21:04:27 +0000 | [diff] [blame] | 1172 | if (reservedRegs_.test(reg)) |
| 1173 | continue; |
Evan Cheng | 3b6d56c | 2006-05-12 19:07:46 +0000 | [diff] [blame] | 1174 | // No need to worry about if the alias register size < regsize of RC. |
| 1175 | // We are going to spill all registers that alias it anyway. |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1176 | for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) |
| 1177 | RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as])); |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 1178 | } |
Evan Cheng | 3b6d56c | 2006-05-12 19:07:46 +0000 | [diff] [blame] | 1179 | } |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Sort all potential spill candidates by weight. |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 1182 | std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare(*this)); |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1183 | minReg = RegsWeights[0].first; |
| 1184 | minWeight = RegsWeights[0].second; |
| 1185 | if (minWeight == HUGE_VALF) { |
| 1186 | // All registers must have inf weight. Just grab one! |
Jim Grosbach | 5a4cbea | 2010-09-01 21:34:41 +0000 | [diff] [blame] | 1187 | minReg = BestPhysReg ? BestPhysReg : getFirstNonReservedPhysReg(RC); |
Owen Anderson | a1566f2 | 2008-07-22 22:46:49 +0000 | [diff] [blame] | 1188 | if (cur->weight == HUGE_VALF || |
Evan Cheng | 5e8d9de | 2008-09-20 01:28:05 +0000 | [diff] [blame] | 1189 | li_->getApproximateInstructionCount(*cur) == 0) { |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1190 | // Spill a physical register around defs and uses. |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1191 | if (li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_)) { |
Evan Cheng | 96f3fd9 | 2009-04-29 07:16:34 +0000 | [diff] [blame] | 1192 | // spillPhysRegAroundRegDefsUses may have invalidated iterator stored |
| 1193 | // in fixed_. Reset them. |
| 1194 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
| 1195 | IntervalPtr &IP = fixed_[i]; |
| 1196 | LiveInterval *I = IP.first; |
| 1197 | if (I->reg == minReg || tri_->isSubRegister(minReg, I->reg)) |
| 1198 | IP.second = I->advanceTo(I->begin(), StartPosition); |
| 1199 | } |
| 1200 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1201 | DowngradedRegs.clear(); |
Evan Cheng | 2824a65 | 2009-03-23 18:24:37 +0000 | [diff] [blame] | 1202 | assignRegOrStackSlotAtInterval(cur); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1203 | } else { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 1204 | assert(false && "Ran out of registers during register allocation!"); |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 1205 | report_fatal_error("Ran out of registers during register allocation!"); |
Evan Cheng | 2824a65 | 2009-03-23 18:24:37 +0000 | [diff] [blame] | 1206 | } |
Evan Cheng | 5e8d9de | 2008-09-20 01:28:05 +0000 | [diff] [blame] | 1207 | return; |
| 1208 | } |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // Find up to 3 registers to consider as spill candidates. |
| 1212 | unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1; |
| 1213 | while (LastCandidate > 1) { |
| 1214 | if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight)) |
| 1215 | break; |
| 1216 | --LastCandidate; |
| 1217 | } |
| 1218 | |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 1219 | DEBUG({ |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1220 | dbgs() << "\t\tregister(s) with min weight(s): "; |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 1221 | |
| 1222 | for (unsigned i = 0; i != LastCandidate; ++i) |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1223 | dbgs() << tri_->getName(RegsWeights[i].first) |
Bill Wendling | c3115a0 | 2009-08-22 20:30:53 +0000 | [diff] [blame] | 1224 | << " (" << RegsWeights[i].second << ")\n"; |
| 1225 | }); |
Alkis Evlogimenos | 3bf564a | 2003-12-23 18:00:33 +0000 | [diff] [blame] | 1226 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1227 | // If the current has the minimum weight, we need to spill it and |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1228 | // add any added intervals back to unhandled, and restart |
| 1229 | // linearscan. |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 1230 | if (cur->weight != HUGE_VALF && cur->weight <= minWeight) { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1231 | DEBUG(dbgs() << "\t\t\tspilling(c): " << *cur << '\n'); |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 1232 | SmallVector<LiveInterval*, 8> spillIs, added; |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 1233 | spiller_->spill(cur, added, spillIs); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 1234 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1235 | std::sort(added.begin(), added.end(), LISorter()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1236 | if (added.empty()) |
| 1237 | return; // Early exit if all spills were folded. |
Alkis Evlogimenos | f5eaf16 | 2004-02-06 18:08:18 +0000 | [diff] [blame] | 1238 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1239 | // Merge added with unhandled. Note that we have already sorted |
| 1240 | // intervals returned by addIntervalsForSpills by their starting |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1241 | // point. |
Evan Cheng | c4f718a | 2009-04-20 17:23:48 +0000 | [diff] [blame] | 1242 | // This also update the NextReloadMap. That is, it adds mapping from a |
| 1243 | // register defined by a reload from SS to the next reload from SS in the |
| 1244 | // same basic block. |
| 1245 | MachineBasicBlock *LastReloadMBB = 0; |
| 1246 | LiveInterval *LastReload = 0; |
| 1247 | int LastReloadSS = VirtRegMap::NO_STACK_SLOT; |
| 1248 | for (unsigned i = 0, e = added.size(); i != e; ++i) { |
| 1249 | LiveInterval *ReloadLi = added[i]; |
| 1250 | if (ReloadLi->weight == HUGE_VALF && |
| 1251 | li_->getApproximateInstructionCount(*ReloadLi) == 0) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 1252 | SlotIndex ReloadIdx = ReloadLi->beginIndex(); |
Evan Cheng | c4f718a | 2009-04-20 17:23:48 +0000 | [diff] [blame] | 1253 | MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx); |
| 1254 | int ReloadSS = vrm_->getStackSlot(ReloadLi->reg); |
| 1255 | if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) { |
| 1256 | // Last reload of same SS is in the same MBB. We want to try to |
| 1257 | // allocate both reloads the same register and make sure the reg |
| 1258 | // isn't clobbered in between if at all possible. |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 1259 | assert(LastReload->beginIndex() < ReloadIdx); |
Evan Cheng | c4f718a | 2009-04-20 17:23:48 +0000 | [diff] [blame] | 1260 | NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg)); |
| 1261 | } |
| 1262 | LastReloadMBB = ReloadMBB; |
| 1263 | LastReload = ReloadLi; |
| 1264 | LastReloadSS = ReloadSS; |
| 1265 | } |
| 1266 | unhandled_.push(ReloadLi); |
| 1267 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | |
Chris Lattner | 19828d4 | 2004-11-18 03:49:30 +0000 | [diff] [blame] | 1271 | ++NumBacktracks; |
| 1272 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1273 | // Push the current interval back to unhandled since we are going |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1274 | // to re-run at least this iteration. Since we didn't modify it it |
| 1275 | // should go back right in the front of the list |
| 1276 | unhandled_.push(cur); |
| 1277 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1278 | assert(TargetRegisterInfo::isPhysicalRegister(minReg) && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1279 | "did not choose a register to spill?"); |
Chris Lattner | 19828d4 | 2004-11-18 03:49:30 +0000 | [diff] [blame] | 1280 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1281 | // We spill all intervals aliasing the register with |
| 1282 | // minimum weight, rollback to the interval with the earliest |
| 1283 | // start point and let the linear scan algorithm run again |
| 1284 | SmallVector<LiveInterval*, 8> spillIs; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1285 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1286 | // Determine which intervals have to be spilled. |
| 1287 | findIntervalsToSpill(cur, RegsWeights, LastCandidate, spillIs); |
| 1288 | |
| 1289 | // Set of spilled vregs (used later to rollback properly) |
| 1290 | SmallSet<unsigned, 8> spilled; |
| 1291 | |
| 1292 | // The earliest start of a Spilled interval indicates up to where |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1293 | // in handled we need to roll back |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1294 | assert(!spillIs.empty() && "No spill intervals?"); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 1295 | SlotIndex earliestStart = spillIs[0]->beginIndex(); |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 1296 | |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1297 | // Spill live intervals of virtual regs mapped to the physical register we |
Chris Lattner | 19828d4 | 2004-11-18 03:49:30 +0000 | [diff] [blame] | 1298 | // want to clear (and its aliases). We only spill those that overlap with the |
| 1299 | // current interval as the rest do not affect its allocation. we also keep |
| 1300 | // track of the earliest start of all spilled live intervals since this will |
| 1301 | // mark our rollback point. |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 1302 | SmallVector<LiveInterval*, 8> added; |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1303 | while (!spillIs.empty()) { |
| 1304 | LiveInterval *sli = spillIs.back(); |
| 1305 | spillIs.pop_back(); |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1306 | DEBUG(dbgs() << "\t\t\tspilling(a): " << *sli << '\n'); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 1307 | if (sli->beginIndex() < earliestStart) |
| 1308 | earliestStart = sli->beginIndex(); |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 1309 | spiller_->spill(sli, added, spillIs); |
Evan Cheng | 3e17225 | 2008-06-20 21:45:16 +0000 | [diff] [blame] | 1310 | spilled.insert(sli->reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 1313 | // Include any added intervals in earliestStart. |
| 1314 | for (unsigned i = 0, e = added.size(); i != e; ++i) { |
| 1315 | SlotIndex SI = added[i]->beginIndex(); |
| 1316 | if (SI < earliestStart) |
| 1317 | earliestStart = SI; |
| 1318 | } |
| 1319 | |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1320 | DEBUG(dbgs() << "\t\trolling back to: " << earliestStart << '\n'); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Scan handled in reverse order up to the earliest start of a |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1323 | // spilled live interval and undo each one, restoring the state of |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1324 | // unhandled. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1325 | while (!handled_.empty()) { |
| 1326 | LiveInterval* i = handled_.back(); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1327 | // If this interval starts before t we are done. |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 1328 | if (!i->empty() && i->beginIndex() < earliestStart) |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1329 | break; |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1330 | DEBUG(dbgs() << "\t\t\tundo changes for: " << *i << '\n'); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1331 | handled_.pop_back(); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1332 | |
| 1333 | // When undoing a live interval allocation we must know if it is active or |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1334 | // inactive to properly update regUse_ and the VirtRegMap. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1335 | IntervalPtrs::iterator it; |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1336 | if ((it = FindIntervalInVector(active_, i)) != active_.end()) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1337 | active_.erase(it); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1338 | assert(!TargetRegisterInfo::isPhysicalRegister(i->reg)); |
Chris Lattner | ffab422 | 2006-02-23 06:44:17 +0000 | [diff] [blame] | 1339 | if (!spilled.count(i->reg)) |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1340 | unhandled_.push(i); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1341 | delRegUse(vrm_->getPhys(i->reg)); |
Chris Lattner | ffab422 | 2006-02-23 06:44:17 +0000 | [diff] [blame] | 1342 | vrm_->clearVirt(i->reg); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1343 | } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1344 | inactive_.erase(it); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1345 | assert(!TargetRegisterInfo::isPhysicalRegister(i->reg)); |
Chris Lattner | ffab422 | 2006-02-23 06:44:17 +0000 | [diff] [blame] | 1346 | if (!spilled.count(i->reg)) |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1347 | unhandled_.push(i); |
Chris Lattner | ffab422 | 2006-02-23 06:44:17 +0000 | [diff] [blame] | 1348 | vrm_->clearVirt(i->reg); |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 1349 | } else { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1350 | assert(TargetRegisterInfo::isVirtualRegister(i->reg) && |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 1351 | "Can only allocate virtual registers!"); |
| 1352 | vrm_->clearVirt(i->reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1353 | unhandled_.push(i); |
| 1354 | } |
Evan Cheng | 9aeaf75 | 2007-11-04 08:32:21 +0000 | [diff] [blame] | 1355 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1356 | DenseMap<unsigned, unsigned>::iterator ii = DowngradeMap.find(i->reg); |
| 1357 | if (ii == DowngradeMap.end()) |
| 1358 | // It interval has a preference, it must be defined by a copy. Clear the |
| 1359 | // preference now since the source interval allocation may have been |
| 1360 | // undone as well. |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1361 | mri_->setRegAllocationHint(i->reg, 0, 0); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1362 | else { |
| 1363 | UpgradeRegister(ii->second); |
| 1364 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Chris Lattner | 19828d4 | 2004-11-18 03:49:30 +0000 | [diff] [blame] | 1367 | // Rewind the iterators in the active, inactive, and fixed lists back to the |
| 1368 | // point we reverted to. |
| 1369 | RevertVectorIteratorsTo(active_, earliestStart); |
| 1370 | RevertVectorIteratorsTo(inactive_, earliestStart); |
| 1371 | RevertVectorIteratorsTo(fixed_, earliestStart); |
| 1372 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1373 | // Scan the rest and undo each interval that expired after t and |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1374 | // insert it in active (the next iteration of the algorithm will |
| 1375 | // put it in inactive if required) |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1376 | for (unsigned i = 0, e = handled_.size(); i != e; ++i) { |
| 1377 | LiveInterval *HI = handled_[i]; |
| 1378 | if (!HI->expiredAt(earliestStart) && |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 1379 | HI->expiredAt(cur->beginIndex())) { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1380 | DEBUG(dbgs() << "\t\t\tundo changes for: " << *HI << '\n'); |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1381 | active_.push_back(std::make_pair(HI, HI->begin())); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1382 | assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg)); |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1383 | addRegUse(vrm_->getPhys(HI->reg)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1387 | // Merge added with unhandled. |
| 1388 | // This also update the NextReloadMap. That is, it adds mapping from a |
| 1389 | // register defined by a reload from SS to the next reload from SS in the |
| 1390 | // same basic block. |
| 1391 | MachineBasicBlock *LastReloadMBB = 0; |
| 1392 | LiveInterval *LastReload = 0; |
| 1393 | int LastReloadSS = VirtRegMap::NO_STACK_SLOT; |
| 1394 | std::sort(added.begin(), added.end(), LISorter()); |
| 1395 | for (unsigned i = 0, e = added.size(); i != e; ++i) { |
| 1396 | LiveInterval *ReloadLi = added[i]; |
| 1397 | if (ReloadLi->weight == HUGE_VALF && |
| 1398 | li_->getApproximateInstructionCount(*ReloadLi) == 0) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 1399 | SlotIndex ReloadIdx = ReloadLi->beginIndex(); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1400 | MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx); |
| 1401 | int ReloadSS = vrm_->getStackSlot(ReloadLi->reg); |
| 1402 | if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) { |
| 1403 | // Last reload of same SS is in the same MBB. We want to try to |
| 1404 | // allocate both reloads the same register and make sure the reg |
| 1405 | // isn't clobbered in between if at all possible. |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 1406 | assert(LastReload->beginIndex() < ReloadIdx); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1407 | NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg)); |
| 1408 | } |
| 1409 | LastReloadMBB = ReloadMBB; |
| 1410 | LastReload = ReloadLi; |
| 1411 | LastReloadSS = ReloadSS; |
| 1412 | } |
| 1413 | unhandled_.push(ReloadLi); |
| 1414 | } |
| 1415 | } |
| 1416 | |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1417 | unsigned RALinScan::getFreePhysReg(LiveInterval* cur, |
| 1418 | const TargetRegisterClass *RC, |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1419 | unsigned MaxInactiveCount, |
| 1420 | SmallVector<unsigned, 256> &inactiveCounts, |
| 1421 | bool SkipDGRegs) { |
| 1422 | unsigned FreeReg = 0; |
| 1423 | unsigned FreeRegInactiveCount = 0; |
| 1424 | |
Evan Cheng | f9f1da1 | 2009-06-18 02:04:01 +0000 | [diff] [blame] | 1425 | std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(cur->reg); |
| 1426 | // Resolve second part of the hint (if possible) given the current allocation. |
| 1427 | unsigned physReg = Hint.second; |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1428 | if (TargetRegisterInfo::isVirtualRegister(physReg) && vrm_->hasPhys(physReg)) |
Evan Cheng | f9f1da1 | 2009-06-18 02:04:01 +0000 | [diff] [blame] | 1429 | physReg = vrm_->getPhys(physReg); |
| 1430 | |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1431 | TargetRegisterClass::iterator I, E; |
Evan Cheng | f9f1da1 | 2009-06-18 02:04:01 +0000 | [diff] [blame] | 1432 | tie(I, E) = tri_->getAllocationOrder(RC, Hint.first, physReg, *mf_); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1433 | assert(I != E && "No allocatable register in this register class!"); |
| 1434 | |
| 1435 | // Scan for the first available register. |
| 1436 | for (; I != E; ++I) { |
| 1437 | unsigned Reg = *I; |
| 1438 | // Ignore "downgraded" registers. |
| 1439 | if (SkipDGRegs && DowngradedRegs.count(Reg)) |
| 1440 | continue; |
Jim Grosbach | 067a648 | 2010-09-01 21:04:27 +0000 | [diff] [blame] | 1441 | // Skip reserved registers. |
| 1442 | if (reservedRegs_.test(Reg)) |
| 1443 | continue; |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 1444 | // Skip recently allocated registers. |
| 1445 | if (isRegAvail(Reg) && !isRecentlyUsed(Reg)) { |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1446 | FreeReg = Reg; |
| 1447 | if (FreeReg < inactiveCounts.size()) |
| 1448 | FreeRegInactiveCount = inactiveCounts[FreeReg]; |
| 1449 | else |
| 1450 | FreeRegInactiveCount = 0; |
| 1451 | break; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | // If there are no free regs, or if this reg has the max inactive count, |
| 1456 | // return this register. |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 1457 | if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) { |
| 1458 | // Remember what register we picked so we can skip it next time. |
| 1459 | if (FreeReg != 0) recordRecentlyUsed(FreeReg); |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1460 | return FreeReg; |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1463 | // Continue scanning the registers, looking for the one with the highest |
| 1464 | // inactive count. Alkis found that this reduced register pressure very |
| 1465 | // slightly on X86 (in rev 1.94 of this file), though this should probably be |
| 1466 | // reevaluated now. |
| 1467 | for (; I != E; ++I) { |
| 1468 | unsigned Reg = *I; |
| 1469 | // Ignore "downgraded" registers. |
| 1470 | if (SkipDGRegs && DowngradedRegs.count(Reg)) |
| 1471 | continue; |
Jim Grosbach | 067a648 | 2010-09-01 21:04:27 +0000 | [diff] [blame] | 1472 | // Skip reserved registers. |
| 1473 | if (reservedRegs_.test(Reg)) |
| 1474 | continue; |
Evan Cheng | 5b16cd2 | 2009-05-01 01:03:49 +0000 | [diff] [blame] | 1475 | if (isRegAvail(Reg) && Reg < inactiveCounts.size() && |
David Greene | feb5bfb | 2009-11-19 19:09:39 +0000 | [diff] [blame] | 1476 | FreeRegInactiveCount < inactiveCounts[Reg] && !isRecentlyUsed(Reg)) { |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1477 | FreeReg = Reg; |
| 1478 | FreeRegInactiveCount = inactiveCounts[Reg]; |
| 1479 | if (FreeRegInactiveCount == MaxInactiveCount) |
| 1480 | break; // We found the one with the max inactive count. |
| 1481 | } |
| 1482 | } |
| 1483 | |
David Greene | 7cfd336 | 2009-11-19 15:55:49 +0000 | [diff] [blame] | 1484 | // Remember what register we picked so we can skip it next time. |
| 1485 | recordRecentlyUsed(FreeReg); |
| 1486 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1487 | return FreeReg; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 1488 | } |
Alkis Evlogimenos | f5eaf16 | 2004-02-06 18:08:18 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1490 | /// getFreePhysReg - return a free physical register for this virtual register |
| 1491 | /// interval if we have one, otherwise return 0. |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 1492 | unsigned RALinScan::getFreePhysReg(LiveInterval *cur) { |
Chris Lattner | fe42462 | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 1493 | SmallVector<unsigned, 256> inactiveCounts; |
Chris Lattner | f8355d9 | 2005-08-22 16:55:22 +0000 | [diff] [blame] | 1494 | unsigned MaxInactiveCount = 0; |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1495 | |
Evan Cheng | 841ee1a | 2008-09-18 22:38:47 +0000 | [diff] [blame] | 1496 | const TargetRegisterClass *RC = mri_->getRegClass(cur->reg); |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1497 | const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1498 | |
Alkis Evlogimenos | 84f5bcb | 2004-09-02 21:23:32 +0000 | [diff] [blame] | 1499 | for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end(); |
| 1500 | i != e; ++i) { |
Chris Lattner | cbb5625 | 2004-11-18 02:42:27 +0000 | [diff] [blame] | 1501 | unsigned reg = i->first->reg; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1502 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Chris Lattner | c8b9f33 | 2004-11-18 06:01:45 +0000 | [diff] [blame] | 1503 | "Can only allocate virtual registers!"); |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1504 | |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1505 | // If this is not in a related reg class to the register we're allocating, |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1506 | // don't check it. |
Evan Cheng | 841ee1a | 2008-09-18 22:38:47 +0000 | [diff] [blame] | 1507 | const TargetRegisterClass *RegRC = mri_->getRegClass(reg); |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1508 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) { |
| 1509 | reg = vrm_->getPhys(reg); |
Chris Lattner | fe42462 | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 1510 | if (inactiveCounts.size() <= reg) |
| 1511 | inactiveCounts.resize(reg+1); |
Chris Lattner | b980578 | 2005-08-23 22:27:31 +0000 | [diff] [blame] | 1512 | ++inactiveCounts[reg]; |
| 1513 | MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]); |
| 1514 | } |
Alkis Evlogimenos | 84f5bcb | 2004-09-02 21:23:32 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 1517 | // If copy coalescer has assigned a "preferred" register, check if it's |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 1518 | // available first. |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 1519 | unsigned Preference = vrm_->getRegAllocPref(cur->reg); |
| 1520 | if (Preference) { |
David Greene | 3727776 | 2010-01-05 01:25:20 +0000 | [diff] [blame] | 1521 | DEBUG(dbgs() << "(preferred: " << tri_->getName(Preference) << ") "); |
Jim Grosbach | 662fb77 | 2010-09-01 21:48:06 +0000 | [diff] [blame] | 1522 | if (isRegAvail(Preference) && |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 1523 | RC->contains(Preference)) |
| 1524 | return Preference; |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 1525 | } |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 1526 | |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1527 | if (!DowngradedRegs.empty()) { |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1528 | unsigned FreeReg = getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, |
Evan Cheng | 206d185 | 2009-04-20 08:01:12 +0000 | [diff] [blame] | 1529 | true); |
| 1530 | if (FreeReg) |
| 1531 | return FreeReg; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1532 | } |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1533 | return getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, false); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1536 | FunctionPass* llvm::createLinearScanRegisterAllocator() { |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 1537 | return new RALinScan(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1538 | } |