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