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