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