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