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