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