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 | 54cc2ef | 2010-07-19 15:22:28 +0000 | [diff] [blame] | 34 | #include "RenderMachineFunction.h" |
Lang Hames | 12f35c5 | 2010-07-18 00:57:59 +0000 | [diff] [blame] | 35 | #include "Splitter.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 36 | #include "VirtRegMap.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 37 | #include "VirtRegRewriter.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/RegAllocPBQP.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" |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/PBQP/HeuristicSolver.h" |
| 46 | #include "llvm/CodeGen/PBQP/Graph.h" |
| 47 | #include "llvm/CodeGen/PBQP/Heuristics/Briggs.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 49 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetInstrInfo.h" |
| 53 | #include "llvm/Target/TargetMachine.h" |
| 54 | #include <limits> |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 55 | #include <memory> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 56 | #include <set> |
| 57 | #include <vector> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 58 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 59 | using namespace llvm; |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 60 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 61 | static RegisterRegAlloc |
Duncan Sands | 1aecd15 | 2010-02-18 14:10:41 +0000 | [diff] [blame] | 62 | registerPBQPRepAlloc("pbqp", "PBQP register allocator", |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 63 | createDefaultPBQPRegisterAllocator); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 64 | |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
| 66 | pbqpCoalescing("pbqp-coalescing", |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 67 | cl::desc("Attempt coalescing during PBQP register allocation."), |
| 68 | cl::init(false), cl::Hidden); |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 69 | |
Lang Hames | 12f35c5 | 2010-07-18 00:57:59 +0000 | [diff] [blame] | 70 | static cl::opt<bool> |
| 71 | pbqpPreSplitting("pbqp-pre-splitting", |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 72 | cl::desc("Pre-split before PBQP register allocation."), |
Lang Hames | 12f35c5 | 2010-07-18 00:57:59 +0000 | [diff] [blame] | 73 | cl::init(false), cl::Hidden); |
| 74 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | |
| 77 | /// |
| 78 | /// PBQP based allocators solve the register allocation problem by mapping |
| 79 | /// register allocation problems to Partitioned Boolean Quadratic |
| 80 | /// Programming problems. |
| 81 | class RegAllocPBQP : public MachineFunctionPass { |
| 82 | public: |
| 83 | |
| 84 | static char ID; |
| 85 | |
| 86 | /// Construct a PBQP register allocator. |
| 87 | RegAllocPBQP(std::auto_ptr<PBQPBuilder> b) : MachineFunctionPass(ID), builder(b) {} |
| 88 | |
| 89 | /// Return the pass name. |
| 90 | virtual const char* getPassName() const { |
| 91 | return "PBQP Register Allocator"; |
| 92 | } |
| 93 | |
| 94 | /// PBQP analysis usage. |
| 95 | virtual void getAnalysisUsage(AnalysisUsage &au) const; |
| 96 | |
| 97 | /// Perform register allocation |
| 98 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 99 | |
| 100 | private: |
| 101 | |
| 102 | typedef std::map<const LiveInterval*, unsigned> LI2NodeMap; |
| 103 | typedef std::vector<const LiveInterval*> Node2LIMap; |
| 104 | typedef std::vector<unsigned> AllowedSet; |
| 105 | typedef std::vector<AllowedSet> AllowedSetMap; |
| 106 | typedef std::pair<unsigned, unsigned> RegPair; |
| 107 | typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap; |
| 108 | typedef std::vector<PBQP::Graph::NodeItr> NodeVector; |
| 109 | typedef std::set<unsigned> RegSet; |
| 110 | |
| 111 | |
| 112 | std::auto_ptr<PBQPBuilder> builder; |
| 113 | |
| 114 | MachineFunction *mf; |
| 115 | const TargetMachine *tm; |
| 116 | const TargetRegisterInfo *tri; |
| 117 | const TargetInstrInfo *tii; |
| 118 | const MachineLoopInfo *loopInfo; |
| 119 | MachineRegisterInfo *mri; |
| 120 | RenderMachineFunction *rmf; |
| 121 | |
| 122 | LiveIntervals *lis; |
| 123 | LiveStacks *lss; |
| 124 | VirtRegMap *vrm; |
| 125 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 126 | RegSet vregsToAlloc, emptyIntervalVRegs; |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 127 | |
| 128 | /// \brief Finds the initial set of vreg intervals to allocate. |
| 129 | void findVRegIntervalsToAlloc(); |
| 130 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 131 | /// \brief Adds a stack interval if the given live interval has been |
| 132 | /// spilled. Used to support stack slot coloring. |
| 133 | void addStackInterval(const LiveInterval *spilled,MachineRegisterInfo* mri); |
| 134 | |
| 135 | /// \brief Given a solved PBQP problem maps this solution back to a register |
| 136 | /// assignment. |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 137 | bool mapPBQPToRegAlloc(const PBQPRAProblem &problem, |
| 138 | const PBQP::Solution &solution); |
| 139 | |
| 140 | /// \brief Postprocessing before final spilling. Sets basic block "live in" |
| 141 | /// variables. |
| 142 | void finalizeAlloc() const; |
| 143 | |
| 144 | }; |
| 145 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 146 | char RegAllocPBQP::ID = 0; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 147 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 148 | } // End anonymous namespace. |
| 149 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 150 | unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const { |
| 151 | Node2VReg::const_iterator vregItr = node2VReg.find(node); |
| 152 | assert(vregItr != node2VReg.end() && "No vreg for node."); |
| 153 | return vregItr->second; |
| 154 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 155 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 156 | PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const { |
| 157 | VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg); |
| 158 | assert(nodeItr != vreg2Node.end() && "No node for vreg."); |
| 159 | return nodeItr->second; |
| 160 | |
| 161 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 162 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 163 | const PBQPRAProblem::AllowedSet& |
| 164 | PBQPRAProblem::getAllowedSet(unsigned vreg) const { |
| 165 | AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg); |
| 166 | assert(allowedSetItr != allowedSets.end() && "No pregs for vreg."); |
| 167 | const AllowedSet &allowedSet = allowedSetItr->second; |
| 168 | return allowedSet; |
| 169 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 170 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 171 | unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const { |
| 172 | assert(isPRegOption(vreg, option) && "Not a preg option."); |
| 173 | |
| 174 | const AllowedSet& allowedSet = getAllowedSet(vreg); |
| 175 | assert(option <= allowedSet.size() && "Option outside allowed set."); |
| 176 | return allowedSet[option - 1]; |
| 177 | } |
| 178 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 179 | std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf, |
| 180 | const LiveIntervals *lis, |
| 181 | const MachineLoopInfo *loopInfo, |
| 182 | const RegSet &vregs) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 183 | |
| 184 | typedef std::vector<const LiveInterval*> LIVector; |
| 185 | |
| 186 | MachineRegisterInfo *mri = &mf->getRegInfo(); |
| 187 | const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo(); |
| 188 | |
| 189 | std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem()); |
| 190 | PBQP::Graph &g = p->getGraph(); |
| 191 | RegSet pregs; |
| 192 | |
| 193 | // Collect the set of preg intervals, record that they're used in the MF. |
| 194 | for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end(); |
| 195 | itr != end; ++itr) { |
| 196 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) { |
| 197 | pregs.insert(itr->first); |
| 198 | mri->setPhysRegUsed(itr->first); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 199 | } |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 200 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 201 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 202 | BitVector reservedRegs = tri->getReservedRegs(*mf); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 203 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 204 | // Iterate over vregs. |
| 205 | for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end(); |
| 206 | vregItr != vregEnd; ++vregItr) { |
| 207 | unsigned vreg = *vregItr; |
| 208 | const TargetRegisterClass *trc = mri->getRegClass(vreg); |
| 209 | const LiveInterval *vregLI = &lis->getInterval(vreg); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 210 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 211 | // Compute an initial allowed set for the current vreg. |
| 212 | typedef std::vector<unsigned> VRAllowed; |
| 213 | VRAllowed vrAllowed; |
| 214 | for (TargetRegisterClass::iterator aoItr = trc->allocation_order_begin(*mf), |
| 215 | aoEnd = trc->allocation_order_end(*mf); |
| 216 | aoItr != aoEnd; ++aoItr) { |
| 217 | unsigned preg = *aoItr; |
| 218 | if (!reservedRegs.test(preg)) { |
| 219 | vrAllowed.push_back(preg); |
Lang Hames | d0f6f01 | 2010-07-17 06:31:41 +0000 | [diff] [blame] | 220 | } |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 221 | } |
Lang Hames | d0f6f01 | 2010-07-17 06:31:41 +0000 | [diff] [blame] | 222 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 223 | // Remove any physical registers which overlap. |
| 224 | for (RegSet::const_iterator pregItr = pregs.begin(), |
| 225 | pregEnd = pregs.end(); |
| 226 | pregItr != pregEnd; ++pregItr) { |
| 227 | unsigned preg = *pregItr; |
| 228 | const LiveInterval *pregLI = &lis->getInterval(preg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 229 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 230 | if (pregLI->empty()) |
| 231 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 232 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 233 | if (!vregLI->overlaps(*pregLI)) |
| 234 | continue; |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 235 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 236 | // Remove the register from the allowed set. |
| 237 | VRAllowed::iterator eraseItr = |
| 238 | std::find(vrAllowed.begin(), vrAllowed.end(), preg); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 239 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 240 | if (eraseItr != vrAllowed.end()) { |
| 241 | vrAllowed.erase(eraseItr); |
| 242 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 243 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 244 | // Also remove any aliases. |
| 245 | const unsigned *aliasItr = tri->getAliasSet(preg); |
| 246 | if (aliasItr != 0) { |
| 247 | for (; *aliasItr != 0; ++aliasItr) { |
| 248 | VRAllowed::iterator eraseItr = |
| 249 | std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 250 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 251 | if (eraseItr != vrAllowed.end()) { |
| 252 | vrAllowed.erase(eraseItr); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 257 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 258 | // Construct the node. |
| 259 | PBQP::Graph::NodeItr node = |
| 260 | g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 261 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 262 | // Record the mapping and allowed set in the problem. |
| 263 | p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 264 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 265 | PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ? |
| 266 | vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 267 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 268 | addSpillCosts(g.getNodeCosts(node), spillCost); |
| 269 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 270 | |
Lang Hames | 481630d | 2010-09-18 09:49:08 +0000 | [diff] [blame] | 271 | for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 272 | vr1Itr != vrEnd; ++vr1Itr) { |
| 273 | unsigned vr1 = *vr1Itr; |
| 274 | const LiveInterval &l1 = lis->getInterval(vr1); |
| 275 | const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 276 | |
Benjamin Kramer | 9e8d1f9 | 2010-09-18 14:41:26 +0000 | [diff] [blame] | 277 | for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 278 | vr2Itr != vrEnd; ++vr2Itr) { |
| 279 | unsigned vr2 = *vr2Itr; |
| 280 | const LiveInterval &l2 = lis->getInterval(vr2); |
| 281 | const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 282 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 283 | assert(!l2.empty() && "Empty interval in vreg set?"); |
| 284 | if (l1.overlaps(l2)) { |
| 285 | PBQP::Graph::EdgeItr edge = |
| 286 | g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2), |
| 287 | PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0)); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 288 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 289 | addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri); |
| 290 | } |
| 291 | } |
| 292 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 293 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 294 | return p; |
| 295 | } |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 296 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 297 | void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec, |
| 298 | PBQP::PBQPNum spillCost) { |
| 299 | costVec[0] = spillCost; |
| 300 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 301 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 302 | void PBQPBuilder::addInterferenceCosts( |
| 303 | PBQP::Matrix &costMat, |
| 304 | const PBQPRAProblem::AllowedSet &vr1Allowed, |
| 305 | const PBQPRAProblem::AllowedSet &vr2Allowed, |
| 306 | const TargetRegisterInfo *tri) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 307 | assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch."); |
| 308 | assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch."); |
| 309 | |
| 310 | for (unsigned i = 0; i < vr1Allowed.size(); ++i) { |
| 311 | unsigned preg1 = vr1Allowed[i]; |
| 312 | |
| 313 | for (unsigned j = 0; j < vr2Allowed.size(); ++j) { |
| 314 | unsigned preg2 = vr2Allowed[j]; |
| 315 | |
| 316 | if (tri->regsOverlap(preg1, preg2)) { |
| 317 | costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity(); |
| 318 | } |
| 319 | } |
| 320 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 323 | std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build( |
| 324 | MachineFunction *mf, |
| 325 | const LiveIntervals *lis, |
| 326 | const MachineLoopInfo *loopInfo, |
| 327 | const RegSet &vregs) { |
| 328 | |
| 329 | std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs); |
| 330 | PBQP::Graph &g = p->getGraph(); |
| 331 | |
| 332 | const TargetMachine &tm = mf->getTarget(); |
| 333 | CoalescerPair cp(*tm.getInstrInfo(), *tm.getRegisterInfo()); |
| 334 | |
| 335 | // Scan the machine function and add a coalescing cost whenever CoalescerPair |
| 336 | // gives the Ok. |
| 337 | for (MachineFunction::const_iterator mbbItr = mf->begin(), |
| 338 | mbbEnd = mf->end(); |
| 339 | mbbItr != mbbEnd; ++mbbItr) { |
| 340 | const MachineBasicBlock *mbb = &*mbbItr; |
| 341 | |
| 342 | for (MachineBasicBlock::const_iterator miItr = mbb->begin(), |
| 343 | miEnd = mbb->end(); |
| 344 | miItr != miEnd; ++miItr) { |
| 345 | const MachineInstr *mi = &*miItr; |
| 346 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 347 | if (!cp.setRegisters(mi)) |
| 348 | continue; // Not coalescable. |
| 349 | |
| 350 | if (cp.getSrcReg() == cp.getDstReg()) |
| 351 | continue; // Already coalesced. |
| 352 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 353 | unsigned dst = cp.getDstReg(), |
| 354 | src = cp.getSrcReg(); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 355 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 356 | const float copyFactor = 0.5; // Cost of copy relative to load. Current |
| 357 | // value plucked randomly out of the air. |
| 358 | |
| 359 | PBQP::PBQPNum cBenefit = |
| 360 | copyFactor * LiveIntervals::getSpillWeight(false, true, |
| 361 | loopInfo->getLoopDepth(mbb)); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 362 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 363 | if (cp.isPhys()) { |
| 364 | if (!lis->isAllocatable(dst)) |
| 365 | continue; |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 366 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 367 | const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src); |
| 368 | unsigned pregOpt = 0; |
| 369 | while (pregOpt < allowed.size() && allowed[pregOpt] != dst) |
| 370 | ++pregOpt; |
| 371 | if (pregOpt < allowed.size()) { |
| 372 | ++pregOpt; // +1 to account for spill option. |
| 373 | PBQP::Graph::NodeItr node = p->getNodeForVReg(src); |
| 374 | addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 375 | } |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 376 | } else { |
| 377 | const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst); |
| 378 | const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src); |
| 379 | PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst); |
| 380 | PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src); |
| 381 | PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2); |
| 382 | if (edge == g.edgesEnd()) { |
| 383 | edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1, |
| 384 | allowed2->size() + 1, |
| 385 | 0)); |
| 386 | } else { |
| 387 | if (g.getEdgeNode1(edge) == node2) { |
| 388 | std::swap(node1, node2); |
| 389 | std::swap(allowed1, allowed2); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2, |
| 394 | cBenefit); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return p; |
| 400 | } |
| 401 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 402 | void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec, |
| 403 | unsigned pregOption, |
| 404 | PBQP::PBQPNum benefit) { |
| 405 | costVec[pregOption] += -benefit; |
| 406 | } |
| 407 | |
| 408 | void PBQPBuilderWithCoalescing::addVirtRegCoalesce( |
| 409 | PBQP::Matrix &costMat, |
| 410 | const PBQPRAProblem::AllowedSet &vr1Allowed, |
| 411 | const PBQPRAProblem::AllowedSet &vr2Allowed, |
| 412 | PBQP::PBQPNum benefit) { |
| 413 | |
| 414 | assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch."); |
| 415 | assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch."); |
| 416 | |
| 417 | for (unsigned i = 0; i < vr1Allowed.size(); ++i) { |
| 418 | unsigned preg1 = vr1Allowed[i]; |
| 419 | for (unsigned j = 0; j < vr2Allowed.size(); ++j) { |
| 420 | unsigned preg2 = vr2Allowed[j]; |
| 421 | |
| 422 | if (preg1 == preg2) { |
| 423 | costMat[i + 1][j + 1] += -benefit; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 428 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 429 | |
| 430 | void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const { |
| 431 | au.addRequired<SlotIndexes>(); |
| 432 | au.addPreserved<SlotIndexes>(); |
| 433 | au.addRequired<LiveIntervals>(); |
| 434 | //au.addRequiredID(SplitCriticalEdgesID); |
| 435 | au.addRequired<RegisterCoalescer>(); |
| 436 | au.addRequired<CalculateSpillWeights>(); |
| 437 | au.addRequired<LiveStacks>(); |
| 438 | au.addPreserved<LiveStacks>(); |
| 439 | au.addRequired<MachineLoopInfo>(); |
| 440 | au.addPreserved<MachineLoopInfo>(); |
| 441 | if (pbqpPreSplitting) |
| 442 | au.addRequired<LoopSplitter>(); |
| 443 | au.addRequired<VirtRegMap>(); |
| 444 | au.addRequired<RenderMachineFunction>(); |
| 445 | MachineFunctionPass::getAnalysisUsage(au); |
| 446 | } |
| 447 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 448 | void RegAllocPBQP::findVRegIntervalsToAlloc() { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 449 | |
| 450 | // Iterate over all live ranges. |
| 451 | for (LiveIntervals::iterator itr = lis->begin(), end = lis->end(); |
| 452 | itr != end; ++itr) { |
| 453 | |
| 454 | // Ignore physical ones. |
| 455 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) |
| 456 | continue; |
| 457 | |
| 458 | LiveInterval *li = itr->second; |
| 459 | |
| 460 | // If this live interval is non-empty we will use pbqp to allocate it. |
| 461 | // Empty intervals we allocate in a simple post-processing stage in |
| 462 | // finalizeAlloc. |
| 463 | if (!li->empty()) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 464 | vregsToAlloc.insert(li->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 465 | } |
| 466 | else { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 467 | emptyIntervalVRegs.insert(li->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 472 | void RegAllocPBQP::addStackInterval(const LiveInterval *spilled, |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 473 | MachineRegisterInfo* mri) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 474 | int stackSlot = vrm->getStackSlot(spilled->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 475 | |
| 476 | if (stackSlot == VirtRegMap::NO_STACK_SLOT) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 477 | return; |
| 478 | |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 479 | const TargetRegisterClass *RC = mri->getRegClass(spilled->reg); |
| 480 | LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 481 | |
| 482 | VNInfo *vni; |
| 483 | if (stackInterval.getNumValNums() != 0) |
| 484 | vni = stackInterval.getValNumInfo(0); |
| 485 | else |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 486 | vni = stackInterval.getNextValue( |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 487 | SlotIndex(), 0, lss->getVNInfoAllocator()); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 488 | |
| 489 | LiveInterval &rhsInterval = lis->getInterval(spilled->reg); |
| 490 | stackInterval.MergeRangesInAsValue(rhsInterval, vni); |
| 491 | } |
| 492 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 493 | bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem, |
| 494 | const PBQP::Solution &solution) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 495 | // Set to true if we have any spills |
| 496 | bool anotherRoundNeeded = false; |
| 497 | |
| 498 | // Clear the existing allocation. |
| 499 | vrm->clearAllVirt(); |
| 500 | |
| 501 | const PBQP::Graph &g = problem.getGraph(); |
| 502 | // Iterate over the nodes mapping the PBQP solution to a register |
| 503 | // assignment. |
| 504 | for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(), |
| 505 | nodeEnd = g.nodesEnd(); |
| 506 | node != nodeEnd; ++node) { |
| 507 | unsigned vreg = problem.getVRegForNode(node); |
| 508 | unsigned alloc = solution.getSelection(node); |
| 509 | |
| 510 | if (problem.isPRegOption(vreg, alloc)) { |
| 511 | unsigned preg = problem.getPRegForOption(vreg, alloc); |
| 512 | DEBUG(dbgs() << "VREG " << vreg << " -> " << tri->getName(preg) << "\n"); |
| 513 | assert(preg != 0 && "Invalid preg selected."); |
| 514 | vrm->assignVirt2Phys(vreg, preg); |
| 515 | } else if (problem.isSpillOption(vreg, alloc)) { |
| 516 | vregsToAlloc.erase(vreg); |
| 517 | const LiveInterval* spillInterval = &lis->getInterval(vreg); |
| 518 | double oldWeight = spillInterval->weight; |
| 519 | SmallVector<LiveInterval*, 8> spillIs; |
| 520 | rmf->rememberUseDefs(spillInterval); |
| 521 | std::vector<LiveInterval*> newSpills = |
| 522 | lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm); |
| 523 | addStackInterval(spillInterval, mri); |
| 524 | rmf->rememberSpills(spillInterval, newSpills); |
| 525 | |
| 526 | (void) oldWeight; |
| 527 | DEBUG(dbgs() << "VREG " << vreg << " -> SPILLED (Cost: " |
| 528 | << oldWeight << ", New vregs: "); |
| 529 | |
| 530 | // Copy any newly inserted live intervals into the list of regs to |
| 531 | // allocate. |
| 532 | for (std::vector<LiveInterval*>::const_iterator |
| 533 | itr = newSpills.begin(), end = newSpills.end(); |
| 534 | itr != end; ++itr) { |
| 535 | assert(!(*itr)->empty() && "Empty spill range."); |
| 536 | DEBUG(dbgs() << (*itr)->reg << " "); |
| 537 | vregsToAlloc.insert((*itr)->reg); |
| 538 | } |
| 539 | |
| 540 | DEBUG(dbgs() << ")\n"); |
| 541 | |
| 542 | // We need another round if spill intervals were added. |
| 543 | anotherRoundNeeded |= !newSpills.empty(); |
| 544 | } else { |
| 545 | assert(false && "Unknown allocation option."); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | return !anotherRoundNeeded; |
| 550 | } |
| 551 | |
| 552 | |
| 553 | void RegAllocPBQP::finalizeAlloc() const { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 554 | typedef LiveIntervals::iterator LIIterator; |
| 555 | typedef LiveInterval::Ranges::const_iterator LRIterator; |
| 556 | |
| 557 | // First allocate registers for the empty intervals. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 558 | for (RegSet::const_iterator |
| 559 | itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 560 | itr != end; ++itr) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 561 | LiveInterval *li = &lis->getInterval(*itr); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 562 | |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 563 | unsigned physReg = vrm->getRegAllocPref(li->reg); |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 564 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 565 | if (physReg == 0) { |
| 566 | const TargetRegisterClass *liRC = mri->getRegClass(li->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 567 | physReg = *liRC->allocation_order_begin(*mf); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 568 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 569 | |
| 570 | vrm->assignVirt2Phys(li->reg, physReg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 571 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 572 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 573 | // Finally iterate over the basic blocks to compute and set the live-in sets. |
| 574 | SmallVector<MachineBasicBlock*, 8> liveInMBBs; |
| 575 | MachineBasicBlock *entryMBB = &*mf->begin(); |
| 576 | |
| 577 | for (LIIterator liItr = lis->begin(), liEnd = lis->end(); |
| 578 | liItr != liEnd; ++liItr) { |
| 579 | |
| 580 | const LiveInterval *li = liItr->second; |
| 581 | unsigned reg = 0; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 582 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 583 | // Get the physical register for this interval |
| 584 | if (TargetRegisterInfo::isPhysicalRegister(li->reg)) { |
| 585 | reg = li->reg; |
| 586 | } |
| 587 | else if (vrm->isAssignedReg(li->reg)) { |
| 588 | reg = vrm->getPhys(li->reg); |
| 589 | } |
| 590 | else { |
| 591 | // Ranges which are assigned a stack slot only are ignored. |
| 592 | continue; |
| 593 | } |
| 594 | |
Lang Hames | b0e519f | 2009-05-17 23:50:36 +0000 | [diff] [blame] | 595 | if (reg == 0) { |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 596 | // Filter out zero regs - they're for intervals that were spilled. |
Lang Hames | b0e519f | 2009-05-17 23:50:36 +0000 | [diff] [blame] | 597 | continue; |
| 598 | } |
| 599 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 600 | // Iterate over the ranges of the current interval... |
| 601 | for (LRIterator lrItr = li->begin(), lrEnd = li->end(); |
| 602 | lrItr != lrEnd; ++lrItr) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 603 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 604 | // Find the set of basic blocks which this range is live into... |
| 605 | if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) { |
| 606 | // And add the physreg for this interval to their live-in sets. |
| 607 | for (unsigned i = 0; i < liveInMBBs.size(); ++i) { |
| 608 | if (liveInMBBs[i] != entryMBB) { |
| 609 | if (!liveInMBBs[i]->isLiveIn(reg)) { |
| 610 | liveInMBBs[i]->addLiveIn(reg); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | liveInMBBs.clear(); |
| 615 | } |
| 616 | } |
| 617 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 618 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 621 | bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 622 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 623 | mf = &MF; |
| 624 | tm = &mf->getTarget(); |
| 625 | tri = tm->getRegisterInfo(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 626 | tii = tm->getInstrInfo(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 627 | mri = &mf->getRegInfo(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 628 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 629 | lis = &getAnalysis<LiveIntervals>(); |
| 630 | lss = &getAnalysis<LiveStacks>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 631 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
Lang Hames | 3319839 | 2010-09-02 08:27:00 +0000 | [diff] [blame] | 632 | rmf = &getAnalysis<RenderMachineFunction>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 633 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 634 | vrm = &getAnalysis<VirtRegMap>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 635 | |
Lang Hames | 54cc2ef | 2010-07-19 15:22:28 +0000 | [diff] [blame] | 636 | |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 637 | DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 638 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 639 | // Allocator main loop: |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 640 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 641 | // * Map current regalloc problem to a PBQP problem |
| 642 | // * Solve the PBQP problem |
| 643 | // * Map the solution back to a register allocation |
| 644 | // * Spill if necessary |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 645 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 646 | // This process is continued till no more spills are generated. |
| 647 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 648 | // Find the vreg intervals in need of allocation. |
| 649 | findVRegIntervalsToAlloc(); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 650 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 651 | // If there are non-empty intervals allocate them using pbqp. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 652 | if (!vregsToAlloc.empty()) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 653 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 654 | bool pbqpAllocComplete = false; |
| 655 | unsigned round = 0; |
| 656 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 657 | while (!pbqpAllocComplete) { |
| 658 | DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 659 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 660 | std::auto_ptr<PBQPRAProblem> problem = |
| 661 | builder->build(mf, lis, loopInfo, vregsToAlloc); |
| 662 | PBQP::Solution solution = |
| 663 | PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve( |
| 664 | problem->getGraph()); |
Lang Hames | 233fd9c | 2009-08-18 23:34:50 +0000 | [diff] [blame] | 665 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 666 | pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 667 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 668 | ++round; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 669 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 672 | // Finalise allocation, allocate empty ranges. |
| 673 | finalizeAlloc(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 674 | |
Lang Hames | c4bcc77 | 2010-07-20 07:41:44 +0000 | [diff] [blame] | 675 | rmf->renderMachineFunction("After PBQP register allocation.", vrm); |
| 676 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 677 | vregsToAlloc.clear(); |
| 678 | emptyIntervalVRegs.clear(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 679 | |
David Greene | 3093154 | 2010-01-05 01:25:43 +0000 | [diff] [blame] | 680 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 681 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 682 | // Run rewriter |
| 683 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 684 | |
| 685 | rewriter->runOnMachineFunction(*mf, *vrm, lis); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 686 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 687 | return true; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 690 | FunctionPass* llvm::createPBQPRegisterAllocator( |
| 691 | std::auto_ptr<PBQPBuilder> builder) { |
| 692 | return new RegAllocPBQP(builder); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 695 | FunctionPass* llvm::createDefaultPBQPRegisterAllocator() { |
| 696 | if (pbqpCoalescing) { |
| 697 | return createPBQPRegisterAllocator( |
| 698 | std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing())); |
| 699 | } // else |
| 700 | return createPBQPRegisterAllocator( |
| 701 | std::auto_ptr<PBQPBuilder>(new PBQPBuilder())); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 702 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 703 | |
| 704 | #undef DEBUG_TYPE |