Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1 | //===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 9 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 10 | // This file contains a Partitioned Boolean Quadratic Programming (PBQP) based |
| 11 | // register allocator for LLVM. This allocator works by constructing a PBQP |
| 12 | // problem representing the register allocation problem under consideration, |
| 13 | // solving this using a PBQP solver, and mapping the solution back to a |
| 14 | // register assignment. If any variables are selected for spilling then spill |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 15 | // code is inserted and the process repeated. |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 16 | // |
| 17 | // The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned |
| 18 | // for register allocation. For more information on PBQP for register |
Misha Brukman | ce07e99 | 2009-01-08 16:40:25 +0000 | [diff] [blame] | 19 | // allocation, see the following papers: |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 20 | // |
| 21 | // (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with |
| 22 | // PBQP. In Proceedings of the 7th Joint Modular Languages Conference |
| 23 | // (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361. |
| 24 | // |
| 25 | // (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular |
| 26 | // architectures. In Proceedings of the Joint Conference on Languages, |
| 27 | // Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York, |
| 28 | // NY, USA, 139-148. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 29 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "regalloc" |
| 33 | |
| 34 | #include "PBQP.h" |
| 35 | #include "VirtRegMap.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 36 | #include "VirtRegRewriter.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 42 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 43 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetInstrInfo.h" |
| 47 | #include "llvm/Target/TargetMachine.h" |
| 48 | #include <limits> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 49 | #include <map> |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 50 | #include <memory> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 51 | #include <set> |
| 52 | #include <vector> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace llvm; |
| 55 | |
| 56 | static RegisterRegAlloc |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 57 | registerPBQPRepAlloc("pbqp", "PBQP register allocator", |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 58 | createPBQPRegisterAllocator); |
| 59 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | |
| 62 | //! |
| 63 | //! PBQP based allocators solve the register allocation problem by mapping |
| 64 | //! register allocation problems to Partitioned Boolean Quadratic |
| 65 | //! Programming problems. |
| 66 | class VISIBILITY_HIDDEN PBQPRegAlloc : public MachineFunctionPass { |
| 67 | public: |
| 68 | |
| 69 | static char ID; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 70 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 71 | //! Construct a PBQP register allocator. |
| 72 | PBQPRegAlloc() : MachineFunctionPass((intptr_t)&ID) {} |
| 73 | |
| 74 | //! Return the pass name. |
| 75 | virtual const char* getPassName() const throw() { |
| 76 | return "PBQP Register Allocator"; |
| 77 | } |
| 78 | |
| 79 | //! PBQP analysis usage. |
| 80 | virtual void getAnalysisUsage(AnalysisUsage &au) const { |
| 81 | au.addRequired<LiveIntervals>(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 82 | au.addRequiredTransitive<RegisterCoalescer>(); |
| 83 | au.addRequired<LiveStacks>(); |
| 84 | au.addPreserved<LiveStacks>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 85 | au.addRequired<MachineLoopInfo>(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 86 | au.addPreserved<MachineLoopInfo>(); |
Sanjiv Gupta | 12a9dc8 | 2009-03-17 15:46:15 +0000 | [diff] [blame] | 87 | au.addRequired<VirtRegMap>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 88 | MachineFunctionPass::getAnalysisUsage(au); |
| 89 | } |
| 90 | |
| 91 | //! Perform register allocation |
| 92 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 93 | |
| 94 | private: |
| 95 | typedef std::map<const LiveInterval*, unsigned> LI2NodeMap; |
| 96 | typedef std::vector<const LiveInterval*> Node2LIMap; |
| 97 | typedef std::vector<unsigned> AllowedSet; |
| 98 | typedef std::vector<AllowedSet> AllowedSetMap; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 99 | typedef std::set<unsigned> RegSet; |
| 100 | typedef std::pair<unsigned, unsigned> RegPair; |
| 101 | typedef std::map<RegPair, PBQPNum> CoalesceMap; |
| 102 | |
| 103 | typedef std::set<LiveInterval*> LiveIntervalSet; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 104 | |
| 105 | MachineFunction *mf; |
| 106 | const TargetMachine *tm; |
| 107 | const TargetRegisterInfo *tri; |
| 108 | const TargetInstrInfo *tii; |
| 109 | const MachineLoopInfo *loopInfo; |
| 110 | MachineRegisterInfo *mri; |
| 111 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 112 | LiveIntervals *lis; |
| 113 | LiveStacks *lss; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 114 | VirtRegMap *vrm; |
| 115 | |
| 116 | LI2NodeMap li2Node; |
| 117 | Node2LIMap node2LI; |
| 118 | AllowedSetMap allowedSets; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 119 | LiveIntervalSet vregIntervalsToAlloc, |
| 120 | emptyVRegIntervals; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 121 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 122 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 123 | //! Builds a PBQP cost vector. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 124 | template <typename RegContainer> |
| 125 | PBQPVector* buildCostVector(unsigned vReg, |
| 126 | const RegContainer &allowed, |
| 127 | const CoalesceMap &cealesces, |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 128 | PBQPNum spillCost) const; |
| 129 | |
Evan Cheng | 17a82ea | 2008-10-03 17:11:58 +0000 | [diff] [blame] | 130 | //! \brief Builds a PBQP interference matrix. |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 131 | //! |
| 132 | //! @return Either a pointer to a non-zero PBQP matrix representing the |
| 133 | //! allocation option costs, or a null pointer for a zero matrix. |
| 134 | //! |
| 135 | //! Expects allowed sets for two interfering LiveIntervals. These allowed |
| 136 | //! sets should contain only allocable registers from the LiveInterval's |
| 137 | //! register class, with any interfering pre-colored registers removed. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 138 | template <typename RegContainer> |
| 139 | PBQPMatrix* buildInterferenceMatrix(const RegContainer &allowed1, |
| 140 | const RegContainer &allowed2) const; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 141 | |
| 142 | //! |
| 143 | //! Expects allowed sets for two potentially coalescable LiveIntervals, |
| 144 | //! and an estimated benefit due to coalescing. The allowed sets should |
| 145 | //! contain only allocable registers from the LiveInterval's register |
| 146 | //! classes, with any interfering pre-colored registers removed. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 147 | template <typename RegContainer> |
| 148 | PBQPMatrix* buildCoalescingMatrix(const RegContainer &allowed1, |
| 149 | const RegContainer &allowed2, |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 150 | PBQPNum cBenefit) const; |
| 151 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 152 | //! \brief Finds coalescing opportunities and returns them as a map. |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 153 | //! |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 154 | //! Any entries in the map are guaranteed coalescable, even if their |
| 155 | //! corresponding live intervals overlap. |
| 156 | CoalesceMap findCoalesces(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 157 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 158 | //! \brief Finds the initial set of vreg intervals to allocate. |
| 159 | void findVRegIntervalsToAlloc(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 160 | |
| 161 | //! \brief Constructs a PBQP problem representation of the register |
| 162 | //! allocation problem for this function. |
| 163 | //! |
| 164 | //! @return a PBQP solver object for the register allocation problem. |
| 165 | pbqp* constructPBQPProblem(); |
| 166 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 167 | //! \brief Adds a stack interval if the given live interval has been |
| 168 | //! spilled. Used to support stack slot coloring. |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 169 | void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 170 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 171 | //! \brief Given a solved PBQP problem maps this solution back to a register |
| 172 | //! assignment. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 173 | bool mapPBQPToRegAlloc(pbqp *problem); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 174 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 175 | //! \brief Postprocessing before final spilling. Sets basic block "live in" |
| 176 | //! variables. |
| 177 | void finalizeAlloc() const; |
| 178 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | char PBQPRegAlloc::ID = 0; |
| 182 | } |
| 183 | |
| 184 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 185 | template <typename RegContainer> |
| 186 | PBQPVector* PBQPRegAlloc::buildCostVector(unsigned vReg, |
| 187 | const RegContainer &allowed, |
| 188 | const CoalesceMap &coalesces, |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 189 | PBQPNum spillCost) const { |
| 190 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 191 | typedef typename RegContainer::const_iterator AllowedItr; |
| 192 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 193 | // Allocate vector. Additional element (0th) used for spill option |
| 194 | PBQPVector *v = new PBQPVector(allowed.size() + 1); |
| 195 | |
| 196 | (*v)[0] = spillCost; |
| 197 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 198 | // Iterate over the allowed registers inserting coalesce benefits if there |
| 199 | // are any. |
| 200 | unsigned ai = 0; |
| 201 | for (AllowedItr itr = allowed.begin(), end = allowed.end(); |
| 202 | itr != end; ++itr, ++ai) { |
| 203 | |
| 204 | unsigned pReg = *itr; |
| 205 | |
| 206 | CoalesceMap::const_iterator cmItr = |
| 207 | coalesces.find(RegPair(vReg, pReg)); |
| 208 | |
| 209 | // No coalesce - on to the next preg. |
| 210 | if (cmItr == coalesces.end()) |
| 211 | continue; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 212 | |
| 213 | // We have a coalesce - insert the benefit. |
| 214 | (*v)[ai + 1] = -cmItr->second; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 217 | return v; |
| 218 | } |
| 219 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 220 | template <typename RegContainer> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 221 | PBQPMatrix* PBQPRegAlloc::buildInterferenceMatrix( |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 222 | const RegContainer &allowed1, const RegContainer &allowed2) const { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 223 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 224 | typedef typename RegContainer::const_iterator RegContainerIterator; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 225 | |
| 226 | // Construct a PBQP matrix representing the cost of allocation options. The |
| 227 | // rows and columns correspond to the allocation options for the two live |
| 228 | // intervals. Elements will be infinite where corresponding registers alias, |
| 229 | // since we cannot allocate aliasing registers to interfering live intervals. |
| 230 | // All other elements (non-aliasing combinations) will have zero cost. Note |
| 231 | // that the spill option (element 0,0) has zero cost, since we can allocate |
| 232 | // both intervals to memory safely (the cost for each individual allocation |
| 233 | // to memory is accounted for by the cost vectors for each live interval). |
| 234 | PBQPMatrix *m = new PBQPMatrix(allowed1.size() + 1, allowed2.size() + 1); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 235 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 236 | // Assume this is a zero matrix until proven otherwise. Zero matrices occur |
| 237 | // between interfering live ranges with non-overlapping register sets (e.g. |
| 238 | // non-overlapping reg classes, or disjoint sets of allowed regs within the |
| 239 | // same class). The term "overlapping" is used advisedly: sets which do not |
| 240 | // intersect, but contain registers which alias, will have non-zero matrices. |
| 241 | // We optimize zero matrices away to improve solver speed. |
| 242 | bool isZeroMatrix = true; |
| 243 | |
| 244 | |
| 245 | // Row index. Starts at 1, since the 0th row is for the spill option, which |
| 246 | // is always zero. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 247 | unsigned ri = 1; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 248 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 249 | // Iterate over allowed sets, insert infinities where required. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 250 | for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 251 | a1Itr != a1End; ++a1Itr) { |
| 252 | |
| 253 | // Column index, starts at 1 as for row index. |
| 254 | unsigned ci = 1; |
| 255 | unsigned reg1 = *a1Itr; |
| 256 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 257 | for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 258 | a2Itr != a2End; ++a2Itr) { |
| 259 | |
| 260 | unsigned reg2 = *a2Itr; |
| 261 | |
| 262 | // If the row/column regs are identical or alias insert an infinity. |
| 263 | if ((reg1 == reg2) || tri->areAliases(reg1, reg2)) { |
| 264 | (*m)[ri][ci] = std::numeric_limits<PBQPNum>::infinity(); |
| 265 | isZeroMatrix = false; |
| 266 | } |
| 267 | |
| 268 | ++ci; |
| 269 | } |
| 270 | |
| 271 | ++ri; |
| 272 | } |
| 273 | |
| 274 | // If this turns out to be a zero matrix... |
| 275 | if (isZeroMatrix) { |
| 276 | // free it and return null. |
| 277 | delete m; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | // ...otherwise return the cost matrix. |
| 282 | return m; |
| 283 | } |
| 284 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 285 | template <typename RegContainer> |
| 286 | PBQPMatrix* PBQPRegAlloc::buildCoalescingMatrix( |
| 287 | const RegContainer &allowed1, const RegContainer &allowed2, |
| 288 | PBQPNum cBenefit) const { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 289 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 290 | typedef typename RegContainer::const_iterator RegContainerIterator; |
| 291 | |
| 292 | // Construct a PBQP Matrix representing the benefits of coalescing. As with |
| 293 | // interference matrices the rows and columns represent allowed registers |
| 294 | // for the LiveIntervals which are (potentially) to be coalesced. The amount |
| 295 | // -cBenefit will be placed in any element representing the same register |
| 296 | // for both intervals. |
| 297 | PBQPMatrix *m = new PBQPMatrix(allowed1.size() + 1, allowed2.size() + 1); |
| 298 | |
| 299 | // Reset costs to zero. |
| 300 | m->reset(0); |
| 301 | |
| 302 | // Assume the matrix is zero till proven otherwise. Zero matrices will be |
| 303 | // optimized away as in the interference case. |
| 304 | bool isZeroMatrix = true; |
| 305 | |
| 306 | // Row index. Starts at 1, since the 0th row is for the spill option, which |
| 307 | // is always zero. |
| 308 | unsigned ri = 1; |
| 309 | |
| 310 | // Iterate over the allowed sets, insert coalescing benefits where |
| 311 | // appropriate. |
| 312 | for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end(); |
| 313 | a1Itr != a1End; ++a1Itr) { |
| 314 | |
| 315 | // Column index, starts at 1 as for row index. |
| 316 | unsigned ci = 1; |
| 317 | unsigned reg1 = *a1Itr; |
| 318 | |
| 319 | for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end(); |
| 320 | a2Itr != a2End; ++a2Itr) { |
| 321 | |
| 322 | // If the row and column represent the same register insert a beneficial |
| 323 | // cost to preference this allocation - it would allow us to eliminate a |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 324 | // move instruction. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 325 | if (reg1 == *a2Itr) { |
| 326 | (*m)[ri][ci] = -cBenefit; |
| 327 | isZeroMatrix = false; |
| 328 | } |
| 329 | |
| 330 | ++ci; |
| 331 | } |
| 332 | |
| 333 | ++ri; |
| 334 | } |
| 335 | |
| 336 | // If this turns out to be a zero matrix... |
| 337 | if (isZeroMatrix) { |
| 338 | // ...free it and return null. |
| 339 | delete m; |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | return m; |
| 344 | } |
| 345 | |
| 346 | PBQPRegAlloc::CoalesceMap PBQPRegAlloc::findCoalesces() { |
| 347 | |
| 348 | typedef MachineFunction::const_iterator MFIterator; |
| 349 | typedef MachineBasicBlock::const_iterator MBBIterator; |
| 350 | typedef LiveInterval::const_vni_iterator VNIIterator; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 351 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 352 | CoalesceMap coalescesFound; |
| 353 | |
| 354 | // To find coalesces we need to iterate over the function looking for |
| 355 | // copy instructions. |
| 356 | for (MFIterator bbItr = mf->begin(), bbEnd = mf->end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 357 | bbItr != bbEnd; ++bbItr) { |
| 358 | |
| 359 | const MachineBasicBlock *mbb = &*bbItr; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 360 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 361 | for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end(); |
| 362 | iItr != iEnd; ++iItr) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 363 | |
| 364 | const MachineInstr *instr = &*iItr; |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 365 | unsigned srcReg, dstReg, srcSubReg, dstSubReg; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 366 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 367 | // If this isn't a copy then continue to the next instruction. |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 368 | if (!tii->isMoveInstr(*instr, srcReg, dstReg, srcSubReg, dstSubReg)) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 369 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 370 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 371 | // If the registers are already the same our job is nice and easy. |
| 372 | if (dstReg == srcReg) |
| 373 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 374 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 375 | bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg), |
| 376 | dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg); |
| 377 | |
| 378 | // If both registers are physical then we can't coalesce. |
| 379 | if (srcRegIsPhysical && dstRegIsPhysical) |
| 380 | continue; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 381 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 382 | // If it's a copy that includes a virtual register but the source and |
| 383 | // destination classes differ then we can't coalesce, so continue with |
| 384 | // the next instruction. |
| 385 | const TargetRegisterClass *srcRegClass = srcRegIsPhysical ? |
| 386 | tri->getPhysicalRegisterRegClass(srcReg) : mri->getRegClass(srcReg); |
| 387 | |
| 388 | const TargetRegisterClass *dstRegClass = dstRegIsPhysical ? |
| 389 | tri->getPhysicalRegisterRegClass(dstReg) : mri->getRegClass(dstReg); |
| 390 | |
| 391 | if (srcRegClass != dstRegClass) |
| 392 | continue; |
| 393 | |
| 394 | // We also need any physical regs to be allocable, coalescing with |
| 395 | // a non-allocable register is invalid. |
| 396 | if (srcRegIsPhysical) { |
| 397 | if (std::find(srcRegClass->allocation_order_begin(*mf), |
| 398 | srcRegClass->allocation_order_end(*mf), srcReg) == |
| 399 | srcRegClass->allocation_order_end(*mf)) |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 400 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 403 | if (dstRegIsPhysical) { |
| 404 | if (std::find(dstRegClass->allocation_order_begin(*mf), |
| 405 | dstRegClass->allocation_order_end(*mf), dstReg) == |
| 406 | dstRegClass->allocation_order_end(*mf)) |
| 407 | continue; |
| 408 | } |
| 409 | |
| 410 | // If we've made it here we have a copy with compatible register classes. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 411 | // We can probably coalesce, but we need to consider overlap. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 412 | const LiveInterval *srcLI = &lis->getInterval(srcReg), |
| 413 | *dstLI = &lis->getInterval(dstReg); |
| 414 | |
| 415 | if (srcLI->overlaps(*dstLI)) { |
| 416 | // Even in the case of an overlap we might still be able to coalesce, |
| 417 | // but we need to make sure that no definition of either range occurs |
| 418 | // while the other range is live. |
| 419 | |
| 420 | // Otherwise start by assuming we're ok. |
| 421 | bool badDef = false; |
| 422 | |
| 423 | // Test all defs of the source range. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 424 | for (VNIIterator |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 425 | vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end(); |
| 426 | vniItr != vniEnd; ++vniItr) { |
| 427 | |
| 428 | // If we find a def that kills the coalescing opportunity then |
| 429 | // record it and break from the loop. |
| 430 | if (dstLI->liveAt((*vniItr)->def)) { |
| 431 | badDef = true; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // If we have a bad def give up, continue to the next instruction. |
| 437 | if (badDef) |
| 438 | continue; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 439 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 440 | // Otherwise test definitions of the destination range. |
| 441 | for (VNIIterator |
| 442 | vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end(); |
| 443 | vniItr != vniEnd; ++vniItr) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 444 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 445 | // We want to make sure we skip the copy instruction itself. |
| 446 | if ((*vniItr)->copy == instr) |
| 447 | continue; |
| 448 | |
| 449 | if (srcLI->liveAt((*vniItr)->def)) { |
| 450 | badDef = true; |
| 451 | break; |
| 452 | } |
| 453 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 454 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 455 | // As before a bad def we give up and continue to the next instr. |
| 456 | if (badDef) |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | // If we make it to here then either the ranges didn't overlap, or they |
| 461 | // did, but none of their definitions would prevent us from coalescing. |
| 462 | // We're good to go with the coalesce. |
| 463 | |
| 464 | float cBenefit = powf(10.0f, loopInfo->getLoopDepth(mbb)) / 5.0; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 465 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 466 | coalescesFound[RegPair(srcReg, dstReg)] = cBenefit; |
| 467 | coalescesFound[RegPair(dstReg, srcReg)] = cBenefit; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | } |
| 471 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 472 | return coalescesFound; |
| 473 | } |
| 474 | |
| 475 | void PBQPRegAlloc::findVRegIntervalsToAlloc() { |
| 476 | |
| 477 | // Iterate over all live ranges. |
| 478 | for (LiveIntervals::iterator itr = lis->begin(), end = lis->end(); |
| 479 | itr != end; ++itr) { |
| 480 | |
| 481 | // Ignore physical ones. |
| 482 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) |
| 483 | continue; |
| 484 | |
| 485 | LiveInterval *li = itr->second; |
| 486 | |
| 487 | // If this live interval is non-empty we will use pbqp to allocate it. |
| 488 | // Empty intervals we allocate in a simple post-processing stage in |
| 489 | // finalizeAlloc. |
| 490 | if (!li->empty()) { |
| 491 | vregIntervalsToAlloc.insert(li); |
| 492 | } |
| 493 | else { |
| 494 | emptyVRegIntervals.insert(li); |
| 495 | } |
| 496 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | pbqp* PBQPRegAlloc::constructPBQPProblem() { |
| 500 | |
| 501 | typedef std::vector<const LiveInterval*> LIVector; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 502 | typedef std::vector<unsigned> RegVector; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 503 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 504 | // This will store the physical intervals for easy reference. |
| 505 | LIVector physIntervals; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 506 | |
| 507 | // Start by clearing the old node <-> live interval mappings & allowed sets |
| 508 | li2Node.clear(); |
| 509 | node2LI.clear(); |
| 510 | allowedSets.clear(); |
| 511 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 512 | // Populate physIntervals, update preg use: |
| 513 | for (LiveIntervals::iterator itr = lis->begin(), end = lis->end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 514 | itr != end; ++itr) { |
| 515 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 516 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) { |
| 517 | physIntervals.push_back(itr->second); |
| 518 | mri->setPhysRegUsed(itr->second->reg); |
| 519 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 522 | // Iterate over vreg intervals, construct live interval <-> node number |
| 523 | // mappings. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 524 | for (LiveIntervalSet::const_iterator |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 525 | itr = vregIntervalsToAlloc.begin(), end = vregIntervalsToAlloc.end(); |
| 526 | itr != end; ++itr) { |
| 527 | const LiveInterval *li = *itr; |
| 528 | |
| 529 | li2Node[li] = node2LI.size(); |
| 530 | node2LI.push_back(li); |
| 531 | } |
| 532 | |
| 533 | // Get the set of potential coalesces. |
| 534 | CoalesceMap coalesces(findCoalesces()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 535 | |
| 536 | // Construct a PBQP solver for this problem |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 537 | pbqp *solver = alloc_pbqp(vregIntervalsToAlloc.size()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 538 | |
| 539 | // Resize allowedSets container appropriately. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 540 | allowedSets.resize(vregIntervalsToAlloc.size()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 541 | |
| 542 | // Iterate over virtual register intervals to compute allowed sets... |
| 543 | for (unsigned node = 0; node < node2LI.size(); ++node) { |
| 544 | |
| 545 | // Grab pointers to the interval and its register class. |
| 546 | const LiveInterval *li = node2LI[node]; |
| 547 | const TargetRegisterClass *liRC = mri->getRegClass(li->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 548 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 549 | // Start by assuming all allocable registers in the class are allowed... |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 550 | RegVector liAllowed(liRC->allocation_order_begin(*mf), |
| 551 | liRC->allocation_order_end(*mf)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 552 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 553 | // Eliminate the physical registers which overlap with this range, along |
| 554 | // with all their aliases. |
| 555 | for (LIVector::iterator pItr = physIntervals.begin(), |
| 556 | pEnd = physIntervals.end(); pItr != pEnd; ++pItr) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 557 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 558 | if (!li->overlaps(**pItr)) |
| 559 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 560 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 561 | unsigned pReg = (*pItr)->reg; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 562 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 563 | // If we get here then the live intervals overlap, but we're still ok |
| 564 | // if they're coalescable. |
| 565 | if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end()) |
| 566 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 567 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 568 | // If we get here then we have a genuine exclusion. |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 569 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 570 | // Remove the overlapping reg... |
| 571 | RegVector::iterator eraseItr = |
| 572 | std::find(liAllowed.begin(), liAllowed.end(), pReg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 573 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 574 | if (eraseItr != liAllowed.end()) |
| 575 | liAllowed.erase(eraseItr); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 576 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 577 | const unsigned *aliasItr = tri->getAliasSet(pReg); |
| 578 | |
| 579 | if (aliasItr != 0) { |
| 580 | // ...and its aliases. |
| 581 | for (; *aliasItr != 0; ++aliasItr) { |
| 582 | RegVector::iterator eraseItr = |
| 583 | std::find(liAllowed.begin(), liAllowed.end(), *aliasItr); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 584 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 585 | if (eraseItr != liAllowed.end()) { |
| 586 | liAllowed.erase(eraseItr); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 587 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 588 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 589 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | // Copy the allowed set into a member vector for use when constructing cost |
| 593 | // vectors & matrices, and mapping PBQP solutions back to assignments. |
| 594 | allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end()); |
| 595 | |
| 596 | // Set the spill cost to the interval weight, or epsilon if the |
| 597 | // interval weight is zero |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 598 | PBQPNum spillCost = (li->weight != 0.0) ? |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 599 | li->weight : std::numeric_limits<PBQPNum>::min(); |
| 600 | |
| 601 | // Build a cost vector for this interval. |
| 602 | add_pbqp_nodecosts(solver, node, |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 603 | buildCostVector(li->reg, allowedSets[node], coalesces, |
| 604 | spillCost)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 605 | |
| 606 | } |
| 607 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 608 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 609 | // Now add the cost matrices... |
| 610 | for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 611 | const LiveInterval *li = node2LI[node1]; |
| 612 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 613 | // Test for live range overlaps and insert interference matrices. |
| 614 | for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) { |
| 615 | const LiveInterval *li2 = node2LI[node2]; |
| 616 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 617 | CoalesceMap::const_iterator cmItr = |
| 618 | coalesces.find(RegPair(li->reg, li2->reg)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 619 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 620 | PBQPMatrix *m = 0; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 621 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 622 | if (cmItr != coalesces.end()) { |
| 623 | m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2], |
| 624 | cmItr->second); |
| 625 | } |
| 626 | else if (li->overlaps(*li2)) { |
| 627 | m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]); |
| 628 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 629 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 630 | if (m != 0) { |
| 631 | add_pbqp_edgecosts(solver, node1, node2, m); |
| 632 | delete m; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // We're done, PBQP problem constructed - return it. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 638 | return solver; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 641 | void PBQPRegAlloc::addStackInterval(const LiveInterval *spilled, |
| 642 | MachineRegisterInfo* mri) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 643 | int stackSlot = vrm->getStackSlot(spilled->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 644 | |
| 645 | if (stackSlot == VirtRegMap::NO_STACK_SLOT) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 646 | return; |
| 647 | |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 648 | const TargetRegisterClass *RC = mri->getRegClass(spilled->reg); |
| 649 | LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 650 | |
| 651 | VNInfo *vni; |
| 652 | if (stackInterval.getNumValNums() != 0) |
| 653 | vni = stackInterval.getValNumInfo(0); |
| 654 | else |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 655 | vni = stackInterval.getNextValue(0, 0, false, lss->getVNInfoAllocator()); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 656 | |
| 657 | LiveInterval &rhsInterval = lis->getInterval(spilled->reg); |
| 658 | stackInterval.MergeRangesInAsValue(rhsInterval, vni); |
| 659 | } |
| 660 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 661 | bool PBQPRegAlloc::mapPBQPToRegAlloc(pbqp *problem) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 662 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 663 | // Set to true if we have any spills |
| 664 | bool anotherRoundNeeded = false; |
| 665 | |
| 666 | // Clear the existing allocation. |
| 667 | vrm->clearAllVirt(); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 668 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 669 | // Iterate over the nodes mapping the PBQP solution to a register assignment. |
| 670 | for (unsigned node = 0; node < node2LI.size(); ++node) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 671 | unsigned virtReg = node2LI[node]->reg, |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 672 | allocSelection = get_pbqp_solution(problem, node); |
| 673 | |
| 674 | // If the PBQP solution is non-zero it's a physical register... |
| 675 | if (allocSelection != 0) { |
| 676 | // Get the physical reg, subtracting 1 to account for the spill option. |
| 677 | unsigned physReg = allowedSets[node][allocSelection - 1]; |
| 678 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 679 | DOUT << "VREG " << virtReg << " -> " << tri->getName(physReg) << "\n"; |
| 680 | |
| 681 | assert(physReg != 0); |
| 682 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 683 | // Add to the virt reg map and update the used phys regs. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 684 | vrm->assignVirt2Phys(virtReg, physReg); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 685 | } |
| 686 | // ...Otherwise it's a spill. |
| 687 | else { |
| 688 | |
| 689 | // Make sure we ignore this virtual reg on the next round |
| 690 | // of allocation |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 691 | vregIntervalsToAlloc.erase(&lis->getInterval(virtReg)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 692 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 693 | // Insert spill ranges for this live range |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 694 | const LiveInterval *spillInterval = node2LI[node]; |
| 695 | double oldSpillWeight = spillInterval->weight; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 696 | SmallVector<LiveInterval*, 8> spillIs; |
| 697 | std::vector<LiveInterval*> newSpills = |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 698 | lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm); |
| 699 | addStackInterval(spillInterval, mri); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 700 | |
| 701 | DOUT << "VREG " << virtReg << " -> SPILLED (Cost: " |
| 702 | << oldSpillWeight << ", New vregs: "; |
| 703 | |
| 704 | // Copy any newly inserted live intervals into the list of regs to |
| 705 | // allocate. |
| 706 | for (std::vector<LiveInterval*>::const_iterator |
| 707 | itr = newSpills.begin(), end = newSpills.end(); |
| 708 | itr != end; ++itr) { |
| 709 | |
| 710 | assert(!(*itr)->empty() && "Empty spill range."); |
| 711 | |
| 712 | DOUT << (*itr)->reg << " "; |
| 713 | |
| 714 | vregIntervalsToAlloc.insert(*itr); |
| 715 | } |
| 716 | |
| 717 | DOUT << ")\n"; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 718 | |
| 719 | // We need another round if spill intervals were added. |
| 720 | anotherRoundNeeded |= !newSpills.empty(); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return !anotherRoundNeeded; |
| 725 | } |
| 726 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 727 | void PBQPRegAlloc::finalizeAlloc() const { |
| 728 | typedef LiveIntervals::iterator LIIterator; |
| 729 | typedef LiveInterval::Ranges::const_iterator LRIterator; |
| 730 | |
| 731 | // First allocate registers for the empty intervals. |
Argyrios Kyrtzidis | 3713c0b | 2008-11-19 12:56:21 +0000 | [diff] [blame] | 732 | for (LiveIntervalSet::const_iterator |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 733 | itr = emptyVRegIntervals.begin(), end = emptyVRegIntervals.end(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 734 | itr != end; ++itr) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 735 | LiveInterval *li = *itr; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 736 | |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 737 | unsigned physReg = vrm->getRegAllocPref(li->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 738 | if (physReg == 0) { |
| 739 | const TargetRegisterClass *liRC = mri->getRegClass(li->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 740 | physReg = *liRC->allocation_order_begin(*mf); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 741 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 742 | |
| 743 | vrm->assignVirt2Phys(li->reg, physReg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 744 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 745 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 746 | // Finally iterate over the basic blocks to compute and set the live-in sets. |
| 747 | SmallVector<MachineBasicBlock*, 8> liveInMBBs; |
| 748 | MachineBasicBlock *entryMBB = &*mf->begin(); |
| 749 | |
| 750 | for (LIIterator liItr = lis->begin(), liEnd = lis->end(); |
| 751 | liItr != liEnd; ++liItr) { |
| 752 | |
| 753 | const LiveInterval *li = liItr->second; |
| 754 | unsigned reg = 0; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 755 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 756 | // Get the physical register for this interval |
| 757 | if (TargetRegisterInfo::isPhysicalRegister(li->reg)) { |
| 758 | reg = li->reg; |
| 759 | } |
| 760 | else if (vrm->isAssignedReg(li->reg)) { |
| 761 | reg = vrm->getPhys(li->reg); |
| 762 | } |
| 763 | else { |
| 764 | // Ranges which are assigned a stack slot only are ignored. |
| 765 | continue; |
| 766 | } |
| 767 | |
Lang Hames | b0e519f | 2009-05-17 23:50:36 +0000 | [diff] [blame] | 768 | // Ignore unallocated vregs: |
| 769 | if (reg == 0) { |
| 770 | continue; |
| 771 | } |
| 772 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 773 | // Iterate over the ranges of the current interval... |
| 774 | for (LRIterator lrItr = li->begin(), lrEnd = li->end(); |
| 775 | lrItr != lrEnd; ++lrItr) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 776 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 777 | // Find the set of basic blocks which this range is live into... |
| 778 | if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) { |
| 779 | // And add the physreg for this interval to their live-in sets. |
| 780 | for (unsigned i = 0; i < liveInMBBs.size(); ++i) { |
| 781 | if (liveInMBBs[i] != entryMBB) { |
| 782 | if (!liveInMBBs[i]->isLiveIn(reg)) { |
| 783 | liveInMBBs[i]->addLiveIn(reg); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | liveInMBBs.clear(); |
| 788 | } |
| 789 | } |
| 790 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 791 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 794 | bool PBQPRegAlloc::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 795 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 796 | mf = &MF; |
| 797 | tm = &mf->getTarget(); |
| 798 | tri = tm->getRegisterInfo(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 799 | tii = tm->getInstrInfo(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 800 | mri = &mf->getRegInfo(); |
| 801 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 802 | lis = &getAnalysis<LiveIntervals>(); |
| 803 | lss = &getAnalysis<LiveStacks>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 804 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
| 805 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 806 | vrm = &getAnalysis<VirtRegMap>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 807 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 808 | DEBUG(errs() << "PBQP Register Allocating for " |
| 809 | << mf->getFunction()->getName() << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 810 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 811 | // Allocator main loop: |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 812 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 813 | // * Map current regalloc problem to a PBQP problem |
| 814 | // * Solve the PBQP problem |
| 815 | // * Map the solution back to a register allocation |
| 816 | // * Spill if necessary |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 817 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 818 | // This process is continued till no more spills are generated. |
| 819 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 820 | // Find the vreg intervals in need of allocation. |
| 821 | findVRegIntervalsToAlloc(); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 822 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 823 | // If there aren't any then we're done here. |
| 824 | if (vregIntervalsToAlloc.empty() && emptyVRegIntervals.empty()) |
| 825 | return true; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 826 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 827 | // If there are non-empty intervals allocate them using pbqp. |
| 828 | if (!vregIntervalsToAlloc.empty()) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 829 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 830 | bool pbqpAllocComplete = false; |
| 831 | unsigned round = 0; |
| 832 | |
| 833 | while (!pbqpAllocComplete) { |
| 834 | DOUT << " PBQP Regalloc round " << round << ":\n"; |
| 835 | |
| 836 | pbqp *problem = constructPBQPProblem(); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 837 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 838 | solve_pbqp(problem); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 839 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 840 | pbqpAllocComplete = mapPBQPToRegAlloc(problem); |
| 841 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 842 | free_pbqp(problem); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 843 | |
| 844 | ++round; |
| 845 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 848 | // Finalise allocation, allocate empty ranges. |
| 849 | finalizeAlloc(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 850 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 851 | vregIntervalsToAlloc.clear(); |
| 852 | emptyVRegIntervals.clear(); |
| 853 | li2Node.clear(); |
| 854 | node2LI.clear(); |
| 855 | allowedSets.clear(); |
| 856 | |
| 857 | DOUT << "Post alloc VirtRegMap:\n" << *vrm << "\n"; |
| 858 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 859 | // Run rewriter |
| 860 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 861 | |
| 862 | rewriter->runOnMachineFunction(*mf, *vrm, lis); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 863 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 864 | return true; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | FunctionPass* llvm::createPBQPRegisterAllocator() { |
| 868 | return new PBQPRegAlloc(); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | #undef DEBUG_TYPE |