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 | |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 34 | #include "Spiller.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 35 | #include "VirtRegMap.h" |
Rafael Espindola | fdf16ca | 2011-06-26 21:41:06 +0000 | [diff] [blame] | 36 | #include "RegisterCoalescer.h" |
Lang Hames | 20df03c | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 37 | #include "llvm/Module.h" |
Lang Hames | 9ad7e07 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/AliasAnalysis.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Pete Cooper | 789d5d8 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/RegAllocPBQP.h" |
Lang Hames | 9ad7e07 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineDominators.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 46 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/PBQP/HeuristicSolver.h" |
| 49 | #include "llvm/CodeGen/PBQP/Graph.h" |
| 50 | #include "llvm/CodeGen/PBQP/Heuristics/Briggs.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 53 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetInstrInfo.h" |
| 55 | #include "llvm/Target/TargetMachine.h" |
| 56 | #include <limits> |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 57 | #include <memory> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 58 | #include <set> |
Lang Hames | 20df03c | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 59 | #include <sstream> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 60 | #include <vector> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 61 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 62 | using namespace llvm; |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 63 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 64 | static RegisterRegAlloc |
Duncan Sands | 1aecd15 | 2010-02-18 14:10:41 +0000 | [diff] [blame] | 65 | registerPBQPRepAlloc("pbqp", "PBQP register allocator", |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 66 | createDefaultPBQPRegisterAllocator); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 67 | |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
| 69 | pbqpCoalescing("pbqp-coalescing", |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 70 | cl::desc("Attempt coalescing during PBQP register allocation."), |
| 71 | cl::init(false), cl::Hidden); |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 72 | |
Lang Hames | 20df03c | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 73 | #ifndef NDEBUG |
| 74 | static cl::opt<bool> |
| 75 | pbqpDumpGraphs("pbqp-dump-graphs", |
| 76 | cl::desc("Dump graphs for each function/round in the compilation unit."), |
| 77 | cl::init(false), cl::Hidden); |
| 78 | #endif |
| 79 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | |
| 82 | /// |
| 83 | /// PBQP based allocators solve the register allocation problem by mapping |
| 84 | /// register allocation problems to Partitioned Boolean Quadratic |
| 85 | /// Programming problems. |
| 86 | class RegAllocPBQP : public MachineFunctionPass { |
| 87 | public: |
| 88 | |
| 89 | static char ID; |
| 90 | |
| 91 | /// Construct a PBQP register allocator. |
Lang Hames | 8d85766 | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 92 | RegAllocPBQP(std::auto_ptr<PBQPBuilder> b, char *cPassID=0) |
| 93 | : MachineFunctionPass(ID), builder(b), customPassID(cPassID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 94 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 95 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 96 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 97 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
| 98 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 99 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 100 | } |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 101 | |
| 102 | /// Return the pass name. |
| 103 | virtual const char* getPassName() const { |
| 104 | return "PBQP Register Allocator"; |
| 105 | } |
| 106 | |
| 107 | /// PBQP analysis usage. |
| 108 | virtual void getAnalysisUsage(AnalysisUsage &au) const; |
| 109 | |
| 110 | /// Perform register allocation |
| 111 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 112 | |
| 113 | private: |
| 114 | |
| 115 | typedef std::map<const LiveInterval*, unsigned> LI2NodeMap; |
| 116 | typedef std::vector<const LiveInterval*> Node2LIMap; |
| 117 | typedef std::vector<unsigned> AllowedSet; |
| 118 | typedef std::vector<AllowedSet> AllowedSetMap; |
| 119 | typedef std::pair<unsigned, unsigned> RegPair; |
| 120 | typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap; |
| 121 | typedef std::vector<PBQP::Graph::NodeItr> NodeVector; |
| 122 | typedef std::set<unsigned> RegSet; |
| 123 | |
| 124 | |
| 125 | std::auto_ptr<PBQPBuilder> builder; |
| 126 | |
Lang Hames | 8d85766 | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 127 | char *customPassID; |
| 128 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 129 | MachineFunction *mf; |
| 130 | const TargetMachine *tm; |
| 131 | const TargetRegisterInfo *tri; |
| 132 | const TargetInstrInfo *tii; |
| 133 | const MachineLoopInfo *loopInfo; |
| 134 | MachineRegisterInfo *mri; |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 135 | |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 136 | std::auto_ptr<Spiller> spiller; |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 137 | LiveIntervals *lis; |
| 138 | LiveStacks *lss; |
| 139 | VirtRegMap *vrm; |
| 140 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 141 | RegSet vregsToAlloc, emptyIntervalVRegs; |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 142 | |
| 143 | /// \brief Finds the initial set of vreg intervals to allocate. |
| 144 | void findVRegIntervalsToAlloc(); |
| 145 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 146 | /// \brief Given a solved PBQP problem maps this solution back to a register |
| 147 | /// assignment. |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 148 | bool mapPBQPToRegAlloc(const PBQPRAProblem &problem, |
| 149 | const PBQP::Solution &solution); |
| 150 | |
| 151 | /// \brief Postprocessing before final spilling. Sets basic block "live in" |
| 152 | /// variables. |
| 153 | void finalizeAlloc() const; |
| 154 | |
| 155 | }; |
| 156 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 157 | char RegAllocPBQP::ID = 0; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 158 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 159 | } // End anonymous namespace. |
| 160 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 161 | unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const { |
| 162 | Node2VReg::const_iterator vregItr = node2VReg.find(node); |
| 163 | assert(vregItr != node2VReg.end() && "No vreg for node."); |
| 164 | return vregItr->second; |
| 165 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 166 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 167 | PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const { |
| 168 | VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg); |
| 169 | assert(nodeItr != vreg2Node.end() && "No node for vreg."); |
| 170 | return nodeItr->second; |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 171 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 172 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 173 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 174 | const PBQPRAProblem::AllowedSet& |
| 175 | PBQPRAProblem::getAllowedSet(unsigned vreg) const { |
| 176 | AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg); |
| 177 | assert(allowedSetItr != allowedSets.end() && "No pregs for vreg."); |
| 178 | const AllowedSet &allowedSet = allowedSetItr->second; |
| 179 | return allowedSet; |
| 180 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 181 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 182 | unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const { |
| 183 | assert(isPRegOption(vreg, option) && "Not a preg option."); |
| 184 | |
| 185 | const AllowedSet& allowedSet = getAllowedSet(vreg); |
| 186 | assert(option <= allowedSet.size() && "Option outside allowed set."); |
| 187 | return allowedSet[option - 1]; |
| 188 | } |
| 189 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 190 | std::auto_ptr<PBQPRAProblem> PBQPBuilder::build(MachineFunction *mf, |
| 191 | const LiveIntervals *lis, |
| 192 | const MachineLoopInfo *loopInfo, |
| 193 | const RegSet &vregs) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 194 | |
| 195 | typedef std::vector<const LiveInterval*> LIVector; |
Jakob Stoklund Olesen | 3b30bca | 2012-06-20 22:32:05 +0000 | [diff] [blame] | 196 | LiveIntervals *LIS = const_cast<LiveIntervals*>(lis); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 197 | MachineRegisterInfo *mri = &mf->getRegInfo(); |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 198 | const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 199 | |
| 200 | std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem()); |
| 201 | PBQP::Graph &g = p->getGraph(); |
| 202 | RegSet pregs; |
| 203 | |
| 204 | // Collect the set of preg intervals, record that they're used in the MF. |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 205 | for (unsigned Reg = 1, e = tri->getNumRegs(); Reg != e; ++Reg) { |
Jakob Stoklund Olesen | 3b30bca | 2012-06-20 22:32:05 +0000 | [diff] [blame] | 206 | if (mri->def_empty(Reg)) |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 207 | continue; |
| 208 | pregs.insert(Reg); |
| 209 | mri->setPhysRegUsed(Reg); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 210 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 211 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 212 | BitVector reservedRegs = tri->getReservedRegs(*mf); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 213 | |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 214 | // Iterate over vregs. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 215 | for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end(); |
| 216 | vregItr != vregEnd; ++vregItr) { |
| 217 | unsigned vreg = *vregItr; |
| 218 | const TargetRegisterClass *trc = mri->getRegClass(vreg); |
Jakob Stoklund Olesen | 3b30bca | 2012-06-20 22:32:05 +0000 | [diff] [blame] | 219 | LiveInterval *vregLI = &LIS->getInterval(vreg); |
| 220 | |
| 221 | // Record any overlaps with regmask operands. |
| 222 | BitVector regMaskOverlaps(tri->getNumRegs()); |
| 223 | LIS->checkRegMaskInterference(*vregLI, regMaskOverlaps); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 224 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 225 | // Compute an initial allowed set for the current vreg. |
| 226 | typedef std::vector<unsigned> VRAllowed; |
| 227 | VRAllowed vrAllowed; |
Craig Topper | b6632ba | 2012-03-04 10:16:38 +0000 | [diff] [blame] | 228 | ArrayRef<uint16_t> rawOrder = trc->getRawAllocationOrder(*mf); |
Jakob Stoklund Olesen | 714c0eb | 2011-06-16 20:37:45 +0000 | [diff] [blame] | 229 | for (unsigned i = 0; i != rawOrder.size(); ++i) { |
| 230 | unsigned preg = rawOrder[i]; |
Jakob Stoklund Olesen | 3b30bca | 2012-06-20 22:32:05 +0000 | [diff] [blame] | 231 | if (reservedRegs.test(preg)) |
| 232 | continue; |
| 233 | |
| 234 | // vregLI crosses a regmask operand that clobbers preg. |
| 235 | if (!regMaskOverlaps.empty() && !regMaskOverlaps.test(preg)) |
| 236 | continue; |
| 237 | |
| 238 | // vregLI overlaps fixed regunit interference. |
Jakob Stoklund Olesen | 241d020 | 2012-06-22 16:46:44 +0000 | [diff] [blame] | 239 | bool Interference = false; |
| 240 | for (MCRegUnitIterator Units(preg, tri); Units.isValid(); ++Units) { |
| 241 | if (vregLI->overlaps(LIS->getRegUnit(*Units))) { |
| 242 | Interference = true; |
| 243 | break; |
Jakob Stoklund Olesen | 3b30bca | 2012-06-20 22:32:05 +0000 | [diff] [blame] | 244 | } |
Lang Hames | d0f6f01 | 2010-07-17 06:31:41 +0000 | [diff] [blame] | 245 | } |
Jakob Stoklund Olesen | 241d020 | 2012-06-22 16:46:44 +0000 | [diff] [blame] | 246 | if (Interference) |
| 247 | continue; |
Jakob Stoklund Olesen | 3b30bca | 2012-06-20 22:32:05 +0000 | [diff] [blame] | 248 | |
| 249 | // preg is usable for this virtual register. |
| 250 | vrAllowed.push_back(preg); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 251 | } |
Lang Hames | d0f6f01 | 2010-07-17 06:31:41 +0000 | [diff] [blame] | 252 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 253 | // Construct the node. |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 254 | PBQP::Graph::NodeItr node = |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 255 | g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 256 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 257 | // Record the mapping and allowed set in the problem. |
| 258 | p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 259 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 260 | PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ? |
| 261 | vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 262 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 263 | addSpillCosts(g.getNodeCosts(node), spillCost); |
| 264 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 265 | |
Lang Hames | 481630d | 2010-09-18 09:49:08 +0000 | [diff] [blame] | 266 | for (RegSet::const_iterator vr1Itr = vregs.begin(), vrEnd = vregs.end(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 267 | vr1Itr != vrEnd; ++vr1Itr) { |
| 268 | unsigned vr1 = *vr1Itr; |
| 269 | const LiveInterval &l1 = lis->getInterval(vr1); |
| 270 | const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 271 | |
Benjamin Kramer | 9e8d1f9 | 2010-09-18 14:41:26 +0000 | [diff] [blame] | 272 | for (RegSet::const_iterator vr2Itr = llvm::next(vr1Itr); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 273 | vr2Itr != vrEnd; ++vr2Itr) { |
| 274 | unsigned vr2 = *vr2Itr; |
| 275 | const LiveInterval &l2 = lis->getInterval(vr2); |
| 276 | const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 277 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 278 | assert(!l2.empty() && "Empty interval in vreg set?"); |
| 279 | if (l1.overlaps(l2)) { |
| 280 | PBQP::Graph::EdgeItr edge = |
| 281 | g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2), |
| 282 | PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0)); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 283 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 284 | addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri); |
| 285 | } |
| 286 | } |
| 287 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 288 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 289 | return p; |
| 290 | } |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 291 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 292 | void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec, |
| 293 | PBQP::PBQPNum spillCost) { |
| 294 | costVec[0] = spillCost; |
| 295 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 296 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 297 | void PBQPBuilder::addInterferenceCosts( |
| 298 | PBQP::Matrix &costMat, |
| 299 | const PBQPRAProblem::AllowedSet &vr1Allowed, |
| 300 | const PBQPRAProblem::AllowedSet &vr2Allowed, |
| 301 | const TargetRegisterInfo *tri) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 302 | assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch."); |
| 303 | assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch."); |
| 304 | |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 305 | for (unsigned i = 0; i != vr1Allowed.size(); ++i) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 306 | unsigned preg1 = vr1Allowed[i]; |
| 307 | |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 308 | for (unsigned j = 0; j != vr2Allowed.size(); ++j) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 309 | unsigned preg2 = vr2Allowed[j]; |
| 310 | |
| 311 | if (tri->regsOverlap(preg1, preg2)) { |
| 312 | costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity(); |
| 313 | } |
| 314 | } |
| 315 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 318 | std::auto_ptr<PBQPRAProblem> PBQPBuilderWithCoalescing::build( |
| 319 | MachineFunction *mf, |
| 320 | const LiveIntervals *lis, |
| 321 | const MachineLoopInfo *loopInfo, |
| 322 | const RegSet &vregs) { |
| 323 | |
| 324 | std::auto_ptr<PBQPRAProblem> p = PBQPBuilder::build(mf, lis, loopInfo, vregs); |
| 325 | PBQP::Graph &g = p->getGraph(); |
| 326 | |
| 327 | const TargetMachine &tm = mf->getTarget(); |
Benjamin Kramer | a7542d5 | 2012-06-06 18:25:08 +0000 | [diff] [blame] | 328 | CoalescerPair cp(*tm.getRegisterInfo()); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 329 | |
| 330 | // Scan the machine function and add a coalescing cost whenever CoalescerPair |
| 331 | // gives the Ok. |
| 332 | for (MachineFunction::const_iterator mbbItr = mf->begin(), |
| 333 | mbbEnd = mf->end(); |
| 334 | mbbItr != mbbEnd; ++mbbItr) { |
| 335 | const MachineBasicBlock *mbb = &*mbbItr; |
| 336 | |
| 337 | for (MachineBasicBlock::const_iterator miItr = mbb->begin(), |
| 338 | miEnd = mbb->end(); |
| 339 | miItr != miEnd; ++miItr) { |
| 340 | const MachineInstr *mi = &*miItr; |
| 341 | |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 342 | if (!cp.setRegisters(mi)) { |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 343 | continue; // Not coalescable. |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 344 | } |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 345 | |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 346 | if (cp.getSrcReg() == cp.getDstReg()) { |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 347 | continue; // Already coalesced. |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 348 | } |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 349 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 350 | unsigned dst = cp.getDstReg(), |
| 351 | src = cp.getSrcReg(); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 352 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 353 | const float copyFactor = 0.5; // Cost of copy relative to load. Current |
| 354 | // value plucked randomly out of the air. |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 355 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 356 | PBQP::PBQPNum cBenefit = |
| 357 | copyFactor * LiveIntervals::getSpillWeight(false, true, |
| 358 | loopInfo->getLoopDepth(mbb)); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 359 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 360 | if (cp.isPhys()) { |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 361 | if (!lis->isAllocatable(dst)) { |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 362 | continue; |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 363 | } |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 364 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 365 | const PBQPRAProblem::AllowedSet &allowed = p->getAllowedSet(src); |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 366 | unsigned pregOpt = 0; |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 367 | while (pregOpt < allowed.size() && allowed[pregOpt] != dst) { |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 368 | ++pregOpt; |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 369 | } |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 370 | if (pregOpt < allowed.size()) { |
| 371 | ++pregOpt; // +1 to account for spill option. |
| 372 | PBQP::Graph::NodeItr node = p->getNodeForVReg(src); |
| 373 | addPhysRegCoalesce(g.getNodeCosts(node), pregOpt, cBenefit); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 374 | } |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 375 | } else { |
| 376 | const PBQPRAProblem::AllowedSet *allowed1 = &p->getAllowedSet(dst); |
| 377 | const PBQPRAProblem::AllowedSet *allowed2 = &p->getAllowedSet(src); |
| 378 | PBQP::Graph::NodeItr node1 = p->getNodeForVReg(dst); |
| 379 | PBQP::Graph::NodeItr node2 = p->getNodeForVReg(src); |
| 380 | PBQP::Graph::EdgeItr edge = g.findEdge(node1, node2); |
| 381 | if (edge == g.edgesEnd()) { |
| 382 | edge = g.addEdge(node1, node2, PBQP::Matrix(allowed1->size() + 1, |
| 383 | allowed2->size() + 1, |
| 384 | 0)); |
| 385 | } else { |
| 386 | if (g.getEdgeNode1(edge) == node2) { |
| 387 | std::swap(node1, node2); |
| 388 | std::swap(allowed1, allowed2); |
| 389 | } |
| 390 | } |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 391 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 392 | addVirtRegCoalesce(g.getEdgeCosts(edge), *allowed1, *allowed2, |
| 393 | cBenefit); |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return p; |
| 399 | } |
| 400 | |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 401 | void PBQPBuilderWithCoalescing::addPhysRegCoalesce(PBQP::Vector &costVec, |
| 402 | unsigned pregOption, |
| 403 | PBQP::PBQPNum benefit) { |
| 404 | costVec[pregOption] += -benefit; |
| 405 | } |
| 406 | |
| 407 | void PBQPBuilderWithCoalescing::addVirtRegCoalesce( |
| 408 | PBQP::Matrix &costMat, |
| 409 | const PBQPRAProblem::AllowedSet &vr1Allowed, |
| 410 | const PBQPRAProblem::AllowedSet &vr2Allowed, |
| 411 | PBQP::PBQPNum benefit) { |
| 412 | |
| 413 | assert(costMat.getRows() == vr1Allowed.size() + 1 && "Size mismatch."); |
| 414 | assert(costMat.getCols() == vr2Allowed.size() + 1 && "Size mismatch."); |
| 415 | |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 416 | for (unsigned i = 0; i != vr1Allowed.size(); ++i) { |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 417 | unsigned preg1 = vr1Allowed[i]; |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 418 | for (unsigned j = 0; j != vr2Allowed.size(); ++j) { |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 419 | unsigned preg2 = vr2Allowed[j]; |
| 420 | |
| 421 | if (preg1 == preg2) { |
| 422 | costMat[i + 1][j + 1] += -benefit; |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 423 | } |
Lang Hames | e9c9356 | 2010-09-21 13:19:36 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 427 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 428 | |
| 429 | void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const { |
Lang Hames | 9ad7e07 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 430 | au.setPreservesCFG(); |
| 431 | au.addRequired<AliasAnalysis>(); |
| 432 | au.addPreserved<AliasAnalysis>(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 433 | au.addRequired<SlotIndexes>(); |
| 434 | au.addPreserved<SlotIndexes>(); |
| 435 | au.addRequired<LiveIntervals>(); |
| 436 | //au.addRequiredID(SplitCriticalEdgesID); |
Lang Hames | 8d85766 | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 437 | if (customPassID) |
| 438 | au.addRequiredID(*customPassID); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 439 | au.addRequired<CalculateSpillWeights>(); |
| 440 | au.addRequired<LiveStacks>(); |
| 441 | au.addPreserved<LiveStacks>(); |
Lang Hames | 9ad7e07 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 442 | au.addRequired<MachineDominatorTree>(); |
| 443 | au.addPreserved<MachineDominatorTree>(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 444 | au.addRequired<MachineLoopInfo>(); |
| 445 | au.addPreserved<MachineLoopInfo>(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 446 | au.addRequired<VirtRegMap>(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 447 | MachineFunctionPass::getAnalysisUsage(au); |
| 448 | } |
| 449 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 450 | void RegAllocPBQP::findVRegIntervalsToAlloc() { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 451 | |
| 452 | // Iterate over all live ranges. |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 453 | for (unsigned i = 0, e = mri->getNumVirtRegs(); i != e; ++i) { |
| 454 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 455 | if (mri->reg_nodbg_empty(Reg)) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 456 | continue; |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 457 | LiveInterval *li = &lis->getInterval(Reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 458 | |
| 459 | // If this live interval is non-empty we will use pbqp to allocate it. |
| 460 | // Empty intervals we allocate in a simple post-processing stage in |
| 461 | // finalizeAlloc. |
| 462 | if (!li->empty()) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 463 | vregsToAlloc.insert(li->reg); |
Lang Hames | 5e77f4b | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 464 | } else { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 465 | emptyIntervalVRegs.insert(li->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 470 | bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAProblem &problem, |
| 471 | const PBQP::Solution &solution) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 472 | // Set to true if we have any spills |
| 473 | bool anotherRoundNeeded = false; |
| 474 | |
| 475 | // Clear the existing allocation. |
| 476 | vrm->clearAllVirt(); |
| 477 | |
| 478 | const PBQP::Graph &g = problem.getGraph(); |
| 479 | // Iterate over the nodes mapping the PBQP solution to a register |
| 480 | // assignment. |
| 481 | for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(), |
| 482 | nodeEnd = g.nodesEnd(); |
| 483 | node != nodeEnd; ++node) { |
| 484 | unsigned vreg = problem.getVRegForNode(node); |
| 485 | unsigned alloc = solution.getSelection(node); |
| 486 | |
| 487 | if (problem.isPRegOption(vreg, alloc)) { |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 488 | unsigned preg = problem.getPRegForOption(vreg, alloc); |
Patrik Hägglund | d769387 | 2012-05-23 12:12:58 +0000 | [diff] [blame] | 489 | DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> " |
| 490 | << tri->getName(preg) << "\n"); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 491 | assert(preg != 0 && "Invalid preg selected."); |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 492 | vrm->assignVirt2Phys(vreg, preg); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 493 | } else if (problem.isSpillOption(vreg, alloc)) { |
| 494 | vregsToAlloc.erase(vreg); |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 495 | SmallVector<LiveInterval*, 8> newSpills; |
Jakob Stoklund Olesen | 20942dc | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 496 | LiveRangeEdit LRE(&lis->getInterval(vreg), newSpills, *mf, *lis, vrm); |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 497 | spiller->spill(LRE); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 498 | |
Patrik Hägglund | d769387 | 2012-05-23 12:12:58 +0000 | [diff] [blame] | 499 | DEBUG(dbgs() << "VREG " << PrintReg(vreg, tri) << " -> SPILLED (Cost: " |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 500 | << LRE.getParent().weight << ", New vregs: "); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 501 | |
| 502 | // Copy any newly inserted live intervals into the list of regs to |
| 503 | // allocate. |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 504 | for (LiveRangeEdit::iterator itr = LRE.begin(), end = LRE.end(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 505 | itr != end; ++itr) { |
| 506 | assert(!(*itr)->empty() && "Empty spill range."); |
Patrik Hägglund | d769387 | 2012-05-23 12:12:58 +0000 | [diff] [blame] | 507 | DEBUG(dbgs() << PrintReg((*itr)->reg, tri) << " "); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 508 | vregsToAlloc.insert((*itr)->reg); |
| 509 | } |
| 510 | |
| 511 | DEBUG(dbgs() << ")\n"); |
| 512 | |
| 513 | // We need another round if spill intervals were added. |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 514 | anotherRoundNeeded |= !LRE.empty(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 515 | } else { |
Craig Topper | 5e25ee8 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 516 | llvm_unreachable("Unknown allocation option."); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | return !anotherRoundNeeded; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | void RegAllocPBQP::finalizeAlloc() const { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 525 | // First allocate registers for the empty intervals. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 526 | for (RegSet::const_iterator |
| 527 | itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 528 | itr != end; ++itr) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 529 | LiveInterval *li = &lis->getInterval(*itr); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 530 | |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 531 | unsigned physReg = vrm->getRegAllocPref(li->reg); |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 532 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 533 | if (physReg == 0) { |
| 534 | const TargetRegisterClass *liRC = mri->getRegClass(li->reg); |
Jakob Stoklund Olesen | 714c0eb | 2011-06-16 20:37:45 +0000 | [diff] [blame] | 535 | physReg = liRC->getRawAllocationOrder(*mf).front(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 536 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 537 | |
| 538 | vrm->assignVirt2Phys(li->reg, physReg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 539 | } |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 542 | bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 543 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 544 | mf = &MF; |
| 545 | tm = &mf->getTarget(); |
| 546 | tri = tm->getRegisterInfo(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 547 | tii = tm->getInstrInfo(); |
Andrew Trick | 16f72dd | 2012-02-10 04:10:26 +0000 | [diff] [blame] | 548 | mri = &mf->getRegInfo(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 549 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 550 | lis = &getAnalysis<LiveIntervals>(); |
| 551 | lss = &getAnalysis<LiveStacks>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 552 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
| 553 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 554 | vrm = &getAnalysis<VirtRegMap>(); |
Jakob Stoklund Olesen | cfa8101 | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 555 | spiller.reset(createInlineSpiller(*this, MF, *vrm)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 556 | |
Jakob Stoklund Olesen | d9e5c76 | 2012-01-05 00:26:49 +0000 | [diff] [blame] | 557 | mri->freezeReservedRegs(MF); |
Lang Hames | 54cc2ef | 2010-07-19 15:22:28 +0000 | [diff] [blame] | 558 | |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 559 | DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 560 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 561 | // Allocator main loop: |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 562 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 563 | // * Map current regalloc problem to a PBQP problem |
| 564 | // * Solve the PBQP problem |
| 565 | // * Map the solution back to a register allocation |
| 566 | // * Spill if necessary |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 567 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 568 | // This process is continued till no more spills are generated. |
| 569 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 570 | // Find the vreg intervals in need of allocation. |
| 571 | findVRegIntervalsToAlloc(); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 572 | |
Lang Hames | 20df03c | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 573 | const Function* func = mf->getFunction(); |
| 574 | std::string fqn = |
| 575 | func->getParent()->getModuleIdentifier() + "." + |
| 576 | func->getName().str(); |
| 577 | (void)fqn; |
| 578 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 579 | // If there are non-empty intervals allocate them using pbqp. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 580 | if (!vregsToAlloc.empty()) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 581 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 582 | bool pbqpAllocComplete = false; |
| 583 | unsigned round = 0; |
| 584 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 585 | while (!pbqpAllocComplete) { |
| 586 | DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 587 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 588 | std::auto_ptr<PBQPRAProblem> problem = |
| 589 | builder->build(mf, lis, loopInfo, vregsToAlloc); |
Lang Hames | 20df03c | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 590 | |
| 591 | #ifndef NDEBUG |
| 592 | if (pbqpDumpGraphs) { |
| 593 | std::ostringstream rs; |
| 594 | rs << round; |
| 595 | std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph"); |
| 596 | std::string tmp; |
| 597 | raw_fd_ostream os(graphFileName.c_str(), tmp); |
| 598 | DEBUG(dbgs() << "Dumping graph for round " << round << " to \"" |
| 599 | << graphFileName << "\"\n"); |
| 600 | problem->getGraph().dump(os); |
| 601 | } |
| 602 | #endif |
| 603 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 604 | PBQP::Solution solution = |
| 605 | PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve( |
| 606 | problem->getGraph()); |
Lang Hames | 233fd9c | 2009-08-18 23:34:50 +0000 | [diff] [blame] | 607 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 608 | pbqpAllocComplete = mapPBQPToRegAlloc(*problem, solution); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 609 | |
Lang Hames | ab62b7e | 2010-10-04 12:13:07 +0000 | [diff] [blame] | 610 | ++round; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 611 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 614 | // Finalise allocation, allocate empty ranges. |
| 615 | finalizeAlloc(); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 616 | vregsToAlloc.clear(); |
| 617 | emptyIntervalVRegs.clear(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 618 | |
David Greene | 3093154 | 2010-01-05 01:25:43 +0000 | [diff] [blame] | 619 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 620 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 621 | return true; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 624 | FunctionPass* llvm::createPBQPRegisterAllocator( |
Lang Hames | 8d85766 | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 625 | std::auto_ptr<PBQPBuilder> builder, |
| 626 | char *customPassID) { |
| 627 | return new RegAllocPBQP(builder, customPassID); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Lang Hames | f70e7cc | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 630 | FunctionPass* llvm::createDefaultPBQPRegisterAllocator() { |
| 631 | if (pbqpCoalescing) { |
| 632 | return createPBQPRegisterAllocator( |
| 633 | std::auto_ptr<PBQPBuilder>(new PBQPBuilderWithCoalescing())); |
| 634 | } // else |
| 635 | return createPBQPRegisterAllocator( |
| 636 | std::auto_ptr<PBQPBuilder>(new PBQPBuilder())); |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 637 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 638 | |
| 639 | #undef DEBUG_TYPE |