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