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