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