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