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