Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1 | //===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 9 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 10 | // This file contains a Partitioned Boolean Quadratic Programming (PBQP) based |
| 11 | // register allocator for LLVM. This allocator works by constructing a PBQP |
| 12 | // problem representing the register allocation problem under consideration, |
| 13 | // solving this using a PBQP solver, and mapping the solution back to a |
| 14 | // register assignment. If any variables are selected for spilling then spill |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 15 | // code is inserted and the process repeated. |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 16 | // |
| 17 | // The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned |
| 18 | // for register allocation. For more information on PBQP for register |
Misha Brukman | ce07e99 | 2009-01-08 16:40:25 +0000 | [diff] [blame] | 19 | // allocation, see the following papers: |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 20 | // |
| 21 | // (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with |
| 22 | // PBQP. In Proceedings of the 7th Joint Modular Languages Conference |
| 23 | // (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361. |
| 24 | // |
| 25 | // (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular |
| 26 | // architectures. In Proceedings of the Joint Conference on Languages, |
| 27 | // Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York, |
| 28 | // NY, USA, 139-148. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 29 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "regalloc" |
| 33 | |
Lang Hames | 54cc2ef | 2010-07-19 15:22:28 +0000 | [diff] [blame] | 34 | #include "RenderMachineFunction.h" |
Lang Hames | 12f35c5 | 2010-07-18 00:57:59 +0000 | [diff] [blame] | 35 | #include "Splitter.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 36 | #include "VirtRegMap.h" |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 37 | #include "VirtRegRewriter.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 41 | #include "llvm/CodeGen/RegAllocPBQP.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 45 | #include "llvm/CodeGen/PBQP/HeuristicSolver.h" |
| 46 | #include "llvm/CodeGen/PBQP/Graph.h" |
| 47 | #include "llvm/CodeGen/PBQP/Heuristics/Briggs.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 49 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetInstrInfo.h" |
| 53 | #include "llvm/Target/TargetMachine.h" |
| 54 | #include <limits> |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 55 | #include <memory> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 56 | #include <set> |
| 57 | #include <vector> |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 58 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 59 | namespace llvm { |
| 60 | |
| 61 | using namespace PBQP; |
| 62 | using namespace PBQP::Heuristics; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 63 | |
| 64 | static RegisterRegAlloc |
Duncan Sands | 1aecd15 | 2010-02-18 14:10:41 +0000 | [diff] [blame] | 65 | registerPBQPRepAlloc("pbqp", "PBQP register allocator", |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 66 | llvm::createPBQPRegisterAllocator); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 67 | |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
| 69 | pbqpCoalescing("pbqp-coalescing", |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 70 | cl::desc("Attempt coalescing during PBQP register allocation."), |
| 71 | cl::init(false), cl::Hidden); |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 72 | |
Lang Hames | 12f35c5 | 2010-07-18 00:57:59 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 74 | pbqpBuilder("pbqp-builder", |
| 75 | cl::desc("Use new builder system."), |
| 76 | cl::init(false), cl::Hidden); |
| 77 | |
| 78 | |
| 79 | static cl::opt<bool> |
Lang Hames | 12f35c5 | 2010-07-18 00:57:59 +0000 | [diff] [blame] | 80 | pbqpPreSplitting("pbqp-pre-splitting", |
| 81 | cl::desc("Pre-splite before PBQP register allocation."), |
| 82 | cl::init(false), cl::Hidden); |
| 83 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 84 | char RegAllocPBQP::ID = 0; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 85 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 86 | unsigned PBQPRAProblem::getVRegForNode(PBQP::Graph::ConstNodeItr node) const { |
| 87 | Node2VReg::const_iterator vregItr = node2VReg.find(node); |
| 88 | assert(vregItr != node2VReg.end() && "No vreg for node."); |
| 89 | return vregItr->second; |
| 90 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 91 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 92 | PBQP::Graph::NodeItr PBQPRAProblem::getNodeForVReg(unsigned vreg) const { |
| 93 | VReg2Node::const_iterator nodeItr = vreg2Node.find(vreg); |
| 94 | assert(nodeItr != vreg2Node.end() && "No node for vreg."); |
| 95 | return nodeItr->second; |
| 96 | |
| 97 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 98 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 99 | const PBQPRAProblem::AllowedSet& |
| 100 | PBQPRAProblem::getAllowedSet(unsigned vreg) const { |
| 101 | AllowedSetMap::const_iterator allowedSetItr = allowedSets.find(vreg); |
| 102 | assert(allowedSetItr != allowedSets.end() && "No pregs for vreg."); |
| 103 | const AllowedSet &allowedSet = allowedSetItr->second; |
| 104 | return allowedSet; |
| 105 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 106 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 107 | unsigned PBQPRAProblem::getPRegForOption(unsigned vreg, unsigned option) const { |
| 108 | assert(isPRegOption(vreg, option) && "Not a preg option."); |
| 109 | |
| 110 | const AllowedSet& allowedSet = getAllowedSet(vreg); |
| 111 | assert(option <= allowedSet.size() && "Option outside allowed set."); |
| 112 | return allowedSet[option - 1]; |
| 113 | } |
| 114 | |
| 115 | std::auto_ptr<PBQPRAProblem> PBQPBuilder::build( |
| 116 | MachineFunction *mf, |
| 117 | const LiveIntervals *lis, |
| 118 | const RegSet &vregs) { |
| 119 | |
| 120 | typedef std::vector<const LiveInterval*> LIVector; |
| 121 | |
| 122 | MachineRegisterInfo *mri = &mf->getRegInfo(); |
| 123 | const TargetRegisterInfo *tri = mf->getTarget().getRegisterInfo(); |
| 124 | |
| 125 | std::auto_ptr<PBQPRAProblem> p(new PBQPRAProblem()); |
| 126 | PBQP::Graph &g = p->getGraph(); |
| 127 | RegSet pregs; |
| 128 | |
| 129 | // Collect the set of preg intervals, record that they're used in the MF. |
| 130 | for (LiveIntervals::const_iterator itr = lis->begin(), end = lis->end(); |
| 131 | itr != end; ++itr) { |
| 132 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) { |
| 133 | pregs.insert(itr->first); |
| 134 | mri->setPhysRegUsed(itr->first); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 135 | } |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 136 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 137 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 138 | BitVector reservedRegs = tri->getReservedRegs(*mf); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 139 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 140 | // Iterate over vregs. |
| 141 | for (RegSet::const_iterator vregItr = vregs.begin(), vregEnd = vregs.end(); |
| 142 | vregItr != vregEnd; ++vregItr) { |
| 143 | unsigned vreg = *vregItr; |
| 144 | const TargetRegisterClass *trc = mri->getRegClass(vreg); |
| 145 | const LiveInterval *vregLI = &lis->getInterval(vreg); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 146 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 147 | // Compute an initial allowed set for the current vreg. |
| 148 | typedef std::vector<unsigned> VRAllowed; |
| 149 | VRAllowed vrAllowed; |
| 150 | for (TargetRegisterClass::iterator aoItr = trc->allocation_order_begin(*mf), |
| 151 | aoEnd = trc->allocation_order_end(*mf); |
| 152 | aoItr != aoEnd; ++aoItr) { |
| 153 | unsigned preg = *aoItr; |
| 154 | if (!reservedRegs.test(preg)) { |
| 155 | vrAllowed.push_back(preg); |
Lang Hames | d0f6f01 | 2010-07-17 06:31:41 +0000 | [diff] [blame] | 156 | } |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 157 | } |
Lang Hames | d0f6f01 | 2010-07-17 06:31:41 +0000 | [diff] [blame] | 158 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 159 | // Remove any physical registers which overlap. |
| 160 | for (RegSet::const_iterator pregItr = pregs.begin(), |
| 161 | pregEnd = pregs.end(); |
| 162 | pregItr != pregEnd; ++pregItr) { |
| 163 | unsigned preg = *pregItr; |
| 164 | const LiveInterval *pregLI = &lis->getInterval(preg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 165 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 166 | if (pregLI->empty()) |
| 167 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 168 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 169 | if (!vregLI->overlaps(*pregLI)) |
| 170 | continue; |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 171 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 172 | // Remove the register from the allowed set. |
| 173 | VRAllowed::iterator eraseItr = |
| 174 | std::find(vrAllowed.begin(), vrAllowed.end(), preg); |
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 | if (eraseItr != vrAllowed.end()) { |
| 177 | vrAllowed.erase(eraseItr); |
| 178 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 179 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 180 | // Also remove any aliases. |
| 181 | const unsigned *aliasItr = tri->getAliasSet(preg); |
| 182 | if (aliasItr != 0) { |
| 183 | for (; *aliasItr != 0; ++aliasItr) { |
| 184 | VRAllowed::iterator eraseItr = |
| 185 | std::find(vrAllowed.begin(), vrAllowed.end(), *aliasItr); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 186 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 187 | if (eraseItr != vrAllowed.end()) { |
| 188 | vrAllowed.erase(eraseItr); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 193 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 194 | // Construct the node. |
| 195 | PBQP::Graph::NodeItr node = |
| 196 | g.addNode(PBQP::Vector(vrAllowed.size() + 1, 0)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 197 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 198 | // Record the mapping and allowed set in the problem. |
| 199 | p->recordVReg(vreg, node, vrAllowed.begin(), vrAllowed.end()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 200 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 201 | PBQP::PBQPNum spillCost = (vregLI->weight != 0.0) ? |
| 202 | vregLI->weight : std::numeric_limits<PBQP::PBQPNum>::min(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 203 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 204 | addSpillCosts(g.getNodeCosts(node), spillCost); |
| 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 | for (RegSet::iterator vr1Itr = vregs.begin(), vrEnd = vregs.end(); |
| 208 | vr1Itr != vrEnd; ++vr1Itr) { |
| 209 | unsigned vr1 = *vr1Itr; |
| 210 | const LiveInterval &l1 = lis->getInterval(vr1); |
| 211 | const PBQPRAProblem::AllowedSet &vr1Allowed = p->getAllowedSet(vr1); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 212 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 213 | for (RegSet::iterator vr2Itr = llvm::next(vr1Itr); |
| 214 | vr2Itr != vrEnd; ++vr2Itr) { |
| 215 | unsigned vr2 = *vr2Itr; |
| 216 | const LiveInterval &l2 = lis->getInterval(vr2); |
| 217 | const PBQPRAProblem::AllowedSet &vr2Allowed = p->getAllowedSet(vr2); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 218 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 219 | assert(!l2.empty() && "Empty interval in vreg set?"); |
| 220 | if (l1.overlaps(l2)) { |
| 221 | PBQP::Graph::EdgeItr edge = |
| 222 | g.addEdge(p->getNodeForVReg(vr1), p->getNodeForVReg(vr2), |
| 223 | PBQP::Matrix(vr1Allowed.size()+1, vr2Allowed.size()+1, 0)); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 224 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 225 | addInterferenceCosts(g.getEdgeCosts(edge), vr1Allowed, vr2Allowed, tri); |
| 226 | } |
| 227 | } |
| 228 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 229 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 230 | return p; |
| 231 | } |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 232 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 233 | void PBQPBuilder::addSpillCosts(PBQP::Vector &costVec, |
| 234 | PBQP::PBQPNum spillCost) { |
| 235 | costVec[0] = spillCost; |
| 236 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 237 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 238 | void PBQPBuilder::addInterferenceCosts(PBQP::Matrix &costMat, |
| 239 | const PBQPRAProblem::AllowedSet &vr1Allowed, |
| 240 | const PBQPRAProblem::AllowedSet &vr2Allowed, |
| 241 | const TargetRegisterInfo *tri) { |
| 242 | assert(costMat.getRows() == vr1Allowed.size() + 1 && "Matrix height mismatch."); |
| 243 | assert(costMat.getCols() == vr2Allowed.size() + 1 && "Matrix width mismatch."); |
| 244 | |
| 245 | for (unsigned i = 0; i < vr1Allowed.size(); ++i) { |
| 246 | unsigned preg1 = vr1Allowed[i]; |
| 247 | |
| 248 | for (unsigned j = 0; j < vr2Allowed.size(); ++j) { |
| 249 | unsigned preg2 = vr2Allowed[j]; |
| 250 | |
| 251 | if (tri->regsOverlap(preg1, preg2)) { |
| 252 | costMat[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity(); |
| 253 | } |
| 254 | } |
| 255 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 259 | |
| 260 | void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const { |
| 261 | au.addRequired<SlotIndexes>(); |
| 262 | au.addPreserved<SlotIndexes>(); |
| 263 | au.addRequired<LiveIntervals>(); |
| 264 | //au.addRequiredID(SplitCriticalEdgesID); |
| 265 | au.addRequired<RegisterCoalescer>(); |
| 266 | au.addRequired<CalculateSpillWeights>(); |
| 267 | au.addRequired<LiveStacks>(); |
| 268 | au.addPreserved<LiveStacks>(); |
| 269 | au.addRequired<MachineLoopInfo>(); |
| 270 | au.addPreserved<MachineLoopInfo>(); |
| 271 | if (pbqpPreSplitting) |
| 272 | au.addRequired<LoopSplitter>(); |
| 273 | au.addRequired<VirtRegMap>(); |
| 274 | au.addRequired<RenderMachineFunction>(); |
| 275 | MachineFunctionPass::getAnalysisUsage(au); |
| 276 | } |
| 277 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 278 | template <typename RegContainer> |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 279 | PBQP::Vector RegAllocPBQP::buildCostVector(unsigned vReg, |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 280 | const RegContainer &allowed, |
| 281 | const CoalesceMap &coalesces, |
| 282 | PBQP::PBQPNum spillCost) const { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 283 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 284 | typedef typename RegContainer::const_iterator AllowedItr; |
| 285 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 286 | // Allocate vector. Additional element (0th) used for spill option |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 287 | PBQP::Vector v(allowed.size() + 1, 0); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 288 | |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 289 | v[0] = spillCost; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 290 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 291 | // Iterate over the allowed registers inserting coalesce benefits if there |
| 292 | // are any. |
| 293 | unsigned ai = 0; |
| 294 | for (AllowedItr itr = allowed.begin(), end = allowed.end(); |
| 295 | itr != end; ++itr, ++ai) { |
| 296 | |
| 297 | unsigned pReg = *itr; |
| 298 | |
| 299 | CoalesceMap::const_iterator cmItr = |
| 300 | coalesces.find(RegPair(vReg, pReg)); |
| 301 | |
| 302 | // No coalesce - on to the next preg. |
| 303 | if (cmItr == coalesces.end()) |
| 304 | continue; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 305 | |
| 306 | // We have a coalesce - insert the benefit. |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 307 | v[ai + 1] = -cmItr->second; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 310 | return v; |
| 311 | } |
| 312 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 313 | template <typename RegContainer> |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 314 | PBQP::Matrix* RegAllocPBQP::buildInterferenceMatrix( |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 315 | const RegContainer &allowed1, const RegContainer &allowed2) const { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 316 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 317 | typedef typename RegContainer::const_iterator RegContainerIterator; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 318 | |
| 319 | // Construct a PBQP matrix representing the cost of allocation options. The |
| 320 | // rows and columns correspond to the allocation options for the two live |
| 321 | // intervals. Elements will be infinite where corresponding registers alias, |
| 322 | // since we cannot allocate aliasing registers to interfering live intervals. |
| 323 | // All other elements (non-aliasing combinations) will have zero cost. Note |
| 324 | // that the spill option (element 0,0) has zero cost, since we can allocate |
| 325 | // both intervals to memory safely (the cost for each individual allocation |
| 326 | // to memory is accounted for by the cost vectors for each live interval). |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 327 | PBQP::Matrix *m = |
| 328 | new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 329 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 330 | // Assume this is a zero matrix until proven otherwise. Zero matrices occur |
| 331 | // between interfering live ranges with non-overlapping register sets (e.g. |
| 332 | // non-overlapping reg classes, or disjoint sets of allowed regs within the |
| 333 | // same class). The term "overlapping" is used advisedly: sets which do not |
| 334 | // intersect, but contain registers which alias, will have non-zero matrices. |
| 335 | // We optimize zero matrices away to improve solver speed. |
| 336 | bool isZeroMatrix = true; |
| 337 | |
| 338 | |
| 339 | // Row index. Starts at 1, since the 0th row is for the spill option, which |
| 340 | // is always zero. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 341 | unsigned ri = 1; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 342 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 343 | // Iterate over allowed sets, insert infinities where required. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 344 | for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 345 | a1Itr != a1End; ++a1Itr) { |
| 346 | |
| 347 | // Column index, starts at 1 as for row index. |
| 348 | unsigned ci = 1; |
| 349 | unsigned reg1 = *a1Itr; |
| 350 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 351 | for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 352 | a2Itr != a2End; ++a2Itr) { |
| 353 | |
| 354 | unsigned reg2 = *a2Itr; |
| 355 | |
| 356 | // If the row/column regs are identical or alias insert an infinity. |
Lang Hames | 3f2f3f5 | 2009-09-03 02:52:02 +0000 | [diff] [blame] | 357 | if (tri->regsOverlap(reg1, reg2)) { |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 358 | (*m)[ri][ci] = std::numeric_limits<PBQP::PBQPNum>::infinity(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 359 | isZeroMatrix = false; |
| 360 | } |
| 361 | |
| 362 | ++ci; |
| 363 | } |
| 364 | |
| 365 | ++ri; |
| 366 | } |
| 367 | |
| 368 | // If this turns out to be a zero matrix... |
| 369 | if (isZeroMatrix) { |
| 370 | // free it and return null. |
| 371 | delete m; |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | // ...otherwise return the cost matrix. |
| 376 | return m; |
| 377 | } |
| 378 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 379 | template <typename RegContainer> |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 380 | PBQP::Matrix* RegAllocPBQP::buildCoalescingMatrix( |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 381 | const RegContainer &allowed1, const RegContainer &allowed2, |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 382 | PBQP::PBQPNum cBenefit) const { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 383 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 384 | typedef typename RegContainer::const_iterator RegContainerIterator; |
| 385 | |
| 386 | // Construct a PBQP Matrix representing the benefits of coalescing. As with |
| 387 | // interference matrices the rows and columns represent allowed registers |
| 388 | // for the LiveIntervals which are (potentially) to be coalesced. The amount |
| 389 | // -cBenefit will be placed in any element representing the same register |
| 390 | // for both intervals. |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 391 | PBQP::Matrix *m = |
| 392 | new PBQP::Matrix(allowed1.size() + 1, allowed2.size() + 1, 0); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 393 | |
| 394 | // Reset costs to zero. |
| 395 | m->reset(0); |
| 396 | |
| 397 | // Assume the matrix is zero till proven otherwise. Zero matrices will be |
| 398 | // optimized away as in the interference case. |
| 399 | bool isZeroMatrix = true; |
| 400 | |
| 401 | // Row index. Starts at 1, since the 0th row is for the spill option, which |
| 402 | // is always zero. |
| 403 | unsigned ri = 1; |
| 404 | |
| 405 | // Iterate over the allowed sets, insert coalescing benefits where |
| 406 | // appropriate. |
| 407 | for (RegContainerIterator a1Itr = allowed1.begin(), a1End = allowed1.end(); |
| 408 | a1Itr != a1End; ++a1Itr) { |
| 409 | |
| 410 | // Column index, starts at 1 as for row index. |
| 411 | unsigned ci = 1; |
| 412 | unsigned reg1 = *a1Itr; |
| 413 | |
| 414 | for (RegContainerIterator a2Itr = allowed2.begin(), a2End = allowed2.end(); |
| 415 | a2Itr != a2End; ++a2Itr) { |
| 416 | |
| 417 | // If the row and column represent the same register insert a beneficial |
| 418 | // cost to preference this allocation - it would allow us to eliminate a |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 419 | // move instruction. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 420 | if (reg1 == *a2Itr) { |
| 421 | (*m)[ri][ci] = -cBenefit; |
| 422 | isZeroMatrix = false; |
| 423 | } |
| 424 | |
| 425 | ++ci; |
| 426 | } |
| 427 | |
| 428 | ++ri; |
| 429 | } |
| 430 | |
| 431 | // If this turns out to be a zero matrix... |
| 432 | if (isZeroMatrix) { |
| 433 | // ...free it and return null. |
| 434 | delete m; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | return m; |
| 439 | } |
| 440 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 441 | RegAllocPBQP::CoalesceMap RegAllocPBQP::findCoalesces() { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 442 | |
| 443 | typedef MachineFunction::const_iterator MFIterator; |
| 444 | typedef MachineBasicBlock::const_iterator MBBIterator; |
| 445 | typedef LiveInterval::const_vni_iterator VNIIterator; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 446 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 447 | CoalesceMap coalescesFound; |
| 448 | |
| 449 | // To find coalesces we need to iterate over the function looking for |
| 450 | // copy instructions. |
| 451 | for (MFIterator bbItr = mf->begin(), bbEnd = mf->end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 452 | bbItr != bbEnd; ++bbItr) { |
| 453 | |
| 454 | const MachineBasicBlock *mbb = &*bbItr; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 455 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 456 | for (MBBIterator iItr = mbb->begin(), iEnd = mbb->end(); |
| 457 | iItr != iEnd; ++iItr) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 458 | |
| 459 | const MachineInstr *instr = &*iItr; |
| 460 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 461 | // If this isn't a copy then continue to the next instruction. |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 462 | if (!instr->isCopy()) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 463 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 464 | |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 465 | unsigned srcReg = instr->getOperand(1).getReg(); |
| 466 | unsigned dstReg = instr->getOperand(0).getReg(); |
| 467 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 468 | // If the registers are already the same our job is nice and easy. |
| 469 | if (dstReg == srcReg) |
| 470 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 471 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 472 | bool srcRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(srcReg), |
| 473 | dstRegIsPhysical = TargetRegisterInfo::isPhysicalRegister(dstReg); |
| 474 | |
| 475 | // If both registers are physical then we can't coalesce. |
| 476 | if (srcRegIsPhysical && dstRegIsPhysical) |
| 477 | continue; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 478 | |
Rafael Espindola | cbeb3db | 2010-07-12 01:45:38 +0000 | [diff] [blame] | 479 | // If it's a copy that includes two virtual register but the source and |
| 480 | // destination classes differ then we can't coalesce. |
| 481 | if (!srcRegIsPhysical && !dstRegIsPhysical && |
| 482 | mri->getRegClass(srcReg) != mri->getRegClass(dstReg)) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 483 | continue; |
| 484 | |
Rafael Espindola | cbeb3db | 2010-07-12 01:45:38 +0000 | [diff] [blame] | 485 | // If one is physical and one is virtual, check that the physical is |
| 486 | // allocatable in the class of the virtual. |
| 487 | if (srcRegIsPhysical && !dstRegIsPhysical) { |
| 488 | const TargetRegisterClass *dstRegClass = mri->getRegClass(dstReg); |
Lang Hames | 0b23dc0 | 2010-02-09 00:50:27 +0000 | [diff] [blame] | 489 | if (std::find(dstRegClass->allocation_order_begin(*mf), |
| 490 | dstRegClass->allocation_order_end(*mf), srcReg) == |
| 491 | dstRegClass->allocation_order_end(*mf)) |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 492 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 493 | } |
Rafael Espindola | cbeb3db | 2010-07-12 01:45:38 +0000 | [diff] [blame] | 494 | if (!srcRegIsPhysical && dstRegIsPhysical) { |
| 495 | const TargetRegisterClass *srcRegClass = mri->getRegClass(srcReg); |
Lang Hames | 0b23dc0 | 2010-02-09 00:50:27 +0000 | [diff] [blame] | 496 | if (std::find(srcRegClass->allocation_order_begin(*mf), |
| 497 | srcRegClass->allocation_order_end(*mf), dstReg) == |
| 498 | srcRegClass->allocation_order_end(*mf)) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 499 | continue; |
| 500 | } |
| 501 | |
| 502 | // If we've made it here we have a copy with compatible register classes. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 503 | // We can probably coalesce, but we need to consider overlap. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 504 | const LiveInterval *srcLI = &lis->getInterval(srcReg), |
| 505 | *dstLI = &lis->getInterval(dstReg); |
| 506 | |
| 507 | if (srcLI->overlaps(*dstLI)) { |
| 508 | // Even in the case of an overlap we might still be able to coalesce, |
| 509 | // but we need to make sure that no definition of either range occurs |
| 510 | // while the other range is live. |
| 511 | |
| 512 | // Otherwise start by assuming we're ok. |
| 513 | bool badDef = false; |
| 514 | |
| 515 | // Test all defs of the source range. |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 516 | for (VNIIterator |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 517 | vniItr = srcLI->vni_begin(), vniEnd = srcLI->vni_end(); |
| 518 | vniItr != vniEnd; ++vniItr) { |
| 519 | |
Lang Hames | 0b23dc0 | 2010-02-09 00:50:27 +0000 | [diff] [blame] | 520 | // If we find a poorly defined def we err on the side of caution. |
| 521 | if (!(*vniItr)->def.isValid()) { |
| 522 | badDef = true; |
| 523 | break; |
| 524 | } |
| 525 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 526 | // If we find a def that kills the coalescing opportunity then |
| 527 | // record it and break from the loop. |
| 528 | if (dstLI->liveAt((*vniItr)->def)) { |
| 529 | badDef = true; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // If we have a bad def give up, continue to the next instruction. |
| 535 | if (badDef) |
| 536 | continue; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 537 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 538 | // Otherwise test definitions of the destination range. |
| 539 | for (VNIIterator |
| 540 | vniItr = dstLI->vni_begin(), vniEnd = dstLI->vni_end(); |
| 541 | vniItr != vniEnd; ++vniItr) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 542 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 543 | // We want to make sure we skip the copy instruction itself. |
Lang Hames | 52c1afc | 2009-08-10 23:43:28 +0000 | [diff] [blame] | 544 | if ((*vniItr)->getCopy() == instr) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 545 | continue; |
| 546 | |
Lang Hames | 0b23dc0 | 2010-02-09 00:50:27 +0000 | [diff] [blame] | 547 | if (!(*vniItr)->def.isValid()) { |
| 548 | badDef = true; |
| 549 | break; |
| 550 | } |
| 551 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 552 | if (srcLI->liveAt((*vniItr)->def)) { |
| 553 | badDef = true; |
| 554 | break; |
| 555 | } |
| 556 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 557 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 558 | // As before a bad def we give up and continue to the next instr. |
| 559 | if (badDef) |
| 560 | continue; |
| 561 | } |
| 562 | |
| 563 | // If we make it to here then either the ranges didn't overlap, or they |
| 564 | // did, but none of their definitions would prevent us from coalescing. |
| 565 | // We're good to go with the coalesce. |
| 566 | |
Chris Lattner | 87565c1 | 2010-05-15 17:10:24 +0000 | [diff] [blame] | 567 | float cBenefit = std::pow(10.0f, (float)loopInfo->getLoopDepth(mbb)) / 5.0; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 568 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 569 | coalescesFound[RegPair(srcReg, dstReg)] = cBenefit; |
| 570 | coalescesFound[RegPair(dstReg, srcReg)] = cBenefit; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | } |
| 574 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 575 | return coalescesFound; |
| 576 | } |
| 577 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 578 | void RegAllocPBQP::findVRegIntervalsToAlloc() { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 579 | |
| 580 | // Iterate over all live ranges. |
| 581 | for (LiveIntervals::iterator itr = lis->begin(), end = lis->end(); |
| 582 | itr != end; ++itr) { |
| 583 | |
| 584 | // Ignore physical ones. |
| 585 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) |
| 586 | continue; |
| 587 | |
| 588 | LiveInterval *li = itr->second; |
| 589 | |
| 590 | // If this live interval is non-empty we will use pbqp to allocate it. |
| 591 | // Empty intervals we allocate in a simple post-processing stage in |
| 592 | // finalizeAlloc. |
| 593 | if (!li->empty()) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 594 | vregsToAlloc.insert(li->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 595 | } |
| 596 | else { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 597 | emptyIntervalVRegs.insert(li->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 602 | PBQP::Graph RegAllocPBQP::constructPBQPProblem() { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 603 | |
| 604 | typedef std::vector<const LiveInterval*> LIVector; |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 605 | typedef std::vector<unsigned> RegVector; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 606 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 607 | // This will store the physical intervals for easy reference. |
| 608 | LIVector physIntervals; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 609 | |
| 610 | // Start by clearing the old node <-> live interval mappings & allowed sets |
| 611 | li2Node.clear(); |
| 612 | node2LI.clear(); |
| 613 | allowedSets.clear(); |
| 614 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 615 | // Populate physIntervals, update preg use: |
| 616 | for (LiveIntervals::iterator itr = lis->begin(), end = lis->end(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 617 | itr != end; ++itr) { |
| 618 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 619 | if (TargetRegisterInfo::isPhysicalRegister(itr->first)) { |
| 620 | physIntervals.push_back(itr->second); |
| 621 | mri->setPhysRegUsed(itr->second->reg); |
| 622 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 625 | // Iterate over vreg intervals, construct live interval <-> node number |
| 626 | // mappings. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 627 | for (RegSet::const_iterator itr = vregsToAlloc.begin(), |
| 628 | end = vregsToAlloc.end(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 629 | itr != end; ++itr) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 630 | const LiveInterval *li = &lis->getInterval(*itr); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 631 | |
| 632 | li2Node[li] = node2LI.size(); |
| 633 | node2LI.push_back(li); |
| 634 | } |
| 635 | |
| 636 | // Get the set of potential coalesces. |
Lang Hames | 8481e3b | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 637 | CoalesceMap coalesces; |
| 638 | |
| 639 | if (pbqpCoalescing) { |
| 640 | coalesces = findCoalesces(); |
| 641 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 642 | |
| 643 | // Construct a PBQP solver for this problem |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 644 | PBQP::Graph problem; |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 645 | problemNodes.resize(vregsToAlloc.size()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 646 | |
| 647 | // Resize allowedSets container appropriately. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 648 | allowedSets.resize(vregsToAlloc.size()); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 649 | |
Jim Grosbach | 269354e | 2010-09-01 21:23:03 +0000 | [diff] [blame] | 650 | BitVector ReservedRegs = tri->getReservedRegs(*mf); |
| 651 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 652 | // Iterate over virtual register intervals to compute allowed sets... |
| 653 | for (unsigned node = 0; node < node2LI.size(); ++node) { |
| 654 | |
| 655 | // Grab pointers to the interval and its register class. |
| 656 | const LiveInterval *li = node2LI[node]; |
| 657 | const TargetRegisterClass *liRC = mri->getRegClass(li->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 658 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 659 | // Start by assuming all allocable registers in the class are allowed... |
Jim Grosbach | 269354e | 2010-09-01 21:23:03 +0000 | [diff] [blame] | 660 | RegVector liAllowed; |
| 661 | TargetRegisterClass::iterator aob = liRC->allocation_order_begin(*mf); |
| 662 | TargetRegisterClass::iterator aoe = liRC->allocation_order_end(*mf); |
| 663 | for (TargetRegisterClass::iterator it = aob; it != aoe; ++it) |
| 664 | if (!ReservedRegs.test(*it)) |
| 665 | liAllowed.push_back(*it); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 666 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 667 | // Eliminate the physical registers which overlap with this range, along |
| 668 | // with all their aliases. |
| 669 | for (LIVector::iterator pItr = physIntervals.begin(), |
| 670 | pEnd = physIntervals.end(); pItr != pEnd; ++pItr) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 671 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 672 | if (!li->overlaps(**pItr)) |
| 673 | continue; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 674 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 675 | unsigned pReg = (*pItr)->reg; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 676 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 677 | // If we get here then the live intervals overlap, but we're still ok |
| 678 | // if they're coalescable. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 679 | if (coalesces.find(RegPair(li->reg, pReg)) != coalesces.end()) { |
| 680 | DEBUG(dbgs() << "CoalescingOverride: (" << li->reg << ", " << pReg << ")\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 681 | continue; |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 682 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 683 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 684 | // If we get here then we have a genuine exclusion. |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 685 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 686 | // Remove the overlapping reg... |
| 687 | RegVector::iterator eraseItr = |
| 688 | std::find(liAllowed.begin(), liAllowed.end(), pReg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 689 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 690 | if (eraseItr != liAllowed.end()) |
| 691 | liAllowed.erase(eraseItr); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 692 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 693 | const unsigned *aliasItr = tri->getAliasSet(pReg); |
| 694 | |
| 695 | if (aliasItr != 0) { |
| 696 | // ...and its aliases. |
| 697 | for (; *aliasItr != 0; ++aliasItr) { |
| 698 | RegVector::iterator eraseItr = |
| 699 | std::find(liAllowed.begin(), liAllowed.end(), *aliasItr); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 700 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 701 | if (eraseItr != liAllowed.end()) { |
| 702 | liAllowed.erase(eraseItr); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 703 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 704 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 705 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Copy the allowed set into a member vector for use when constructing cost |
| 709 | // vectors & matrices, and mapping PBQP solutions back to assignments. |
| 710 | allowedSets[node] = AllowedSet(liAllowed.begin(), liAllowed.end()); |
| 711 | |
| 712 | // Set the spill cost to the interval weight, or epsilon if the |
| 713 | // interval weight is zero |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 714 | PBQP::PBQPNum spillCost = (li->weight != 0.0) ? |
| 715 | li->weight : std::numeric_limits<PBQP::PBQPNum>::min(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 716 | |
| 717 | // Build a cost vector for this interval. |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 718 | problemNodes[node] = |
| 719 | problem.addNode( |
| 720 | buildCostVector(li->reg, allowedSets[node], coalesces, spillCost)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 721 | |
| 722 | } |
| 723 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 724 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 725 | // Now add the cost matrices... |
| 726 | for (unsigned node1 = 0; node1 < node2LI.size(); ++node1) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 727 | const LiveInterval *li = node2LI[node1]; |
| 728 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 729 | // Test for live range overlaps and insert interference matrices. |
| 730 | for (unsigned node2 = node1 + 1; node2 < node2LI.size(); ++node2) { |
| 731 | const LiveInterval *li2 = node2LI[node2]; |
| 732 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 733 | CoalesceMap::const_iterator cmItr = |
| 734 | coalesces.find(RegPair(li->reg, li2->reg)); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 735 | |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 736 | PBQP::Matrix *m = 0; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 737 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 738 | if (cmItr != coalesces.end()) { |
| 739 | m = buildCoalescingMatrix(allowedSets[node1], allowedSets[node2], |
| 740 | cmItr->second); |
| 741 | } |
| 742 | else if (li->overlaps(*li2)) { |
| 743 | m = buildInterferenceMatrix(allowedSets[node1], allowedSets[node2]); |
| 744 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 745 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 746 | if (m != 0) { |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 747 | problem.addEdge(problemNodes[node1], |
| 748 | problemNodes[node2], |
| 749 | *m); |
| 750 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 751 | delete m; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 756 | assert(problem.getNumNodes() == allowedSets.size()); |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 757 | /* |
| 758 | std::cerr << "Allocating for " << problem.getNumNodes() << " nodes, " |
| 759 | << problem.getNumEdges() << " edges.\n"; |
| 760 | |
| 761 | problem.printDot(std::cerr); |
| 762 | */ |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 763 | // We're done, PBQP problem constructed - return it. |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 764 | return problem; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 767 | void RegAllocPBQP::addStackInterval(const LiveInterval *spilled, |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 768 | MachineRegisterInfo* mri) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 769 | int stackSlot = vrm->getStackSlot(spilled->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 770 | |
| 771 | if (stackSlot == VirtRegMap::NO_STACK_SLOT) |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 772 | return; |
| 773 | |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 774 | const TargetRegisterClass *RC = mri->getRegClass(spilled->reg); |
| 775 | LiveInterval &stackInterval = lss->getOrCreateInterval(stackSlot, RC); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 776 | |
| 777 | VNInfo *vni; |
| 778 | if (stackInterval.getNumValNums() != 0) |
| 779 | vni = stackInterval.getValNumInfo(0); |
| 780 | else |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 781 | vni = stackInterval.getNextValue( |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 782 | SlotIndex(), 0, false, lss->getVNInfoAllocator()); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 783 | |
| 784 | LiveInterval &rhsInterval = lis->getInterval(spilled->reg); |
| 785 | stackInterval.MergeRangesInAsValue(rhsInterval, vni); |
| 786 | } |
| 787 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 788 | bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQP::Solution &solution) { |
Lang Hames | e98b4b0 | 2009-11-15 04:39:51 +0000 | [diff] [blame] | 789 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 790 | // Set to true if we have any spills |
| 791 | bool anotherRoundNeeded = false; |
| 792 | |
| 793 | // Clear the existing allocation. |
| 794 | vrm->clearAllVirt(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 795 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 796 | // Iterate over the nodes mapping the PBQP solution to a register assignment. |
| 797 | for (unsigned node = 0; node < node2LI.size(); ++node) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 798 | unsigned virtReg = node2LI[node]->reg, |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 799 | allocSelection = solution.getSelection(problemNodes[node]); |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 800 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 801 | |
| 802 | // If the PBQP solution is non-zero it's a physical register... |
| 803 | if (allocSelection != 0) { |
| 804 | // Get the physical reg, subtracting 1 to account for the spill option. |
| 805 | unsigned physReg = allowedSets[node][allocSelection - 1]; |
| 806 | |
David Greene | 3093154 | 2010-01-05 01:25:43 +0000 | [diff] [blame] | 807 | DEBUG(dbgs() << "VREG " << virtReg << " -> " |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 808 | << tri->getName(physReg) << " (Option: " << allocSelection << ")\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 809 | |
| 810 | assert(physReg != 0); |
| 811 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 812 | // Add to the virt reg map and update the used phys regs. |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 813 | vrm->assignVirt2Phys(virtReg, physReg); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 814 | } |
| 815 | // ...Otherwise it's a spill. |
| 816 | else { |
| 817 | |
| 818 | // Make sure we ignore this virtual reg on the next round |
| 819 | // of allocation |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 820 | vregsToAlloc.erase(virtReg); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 821 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 822 | // Insert spill ranges for this live range |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 823 | const LiveInterval *spillInterval = node2LI[node]; |
| 824 | double oldSpillWeight = spillInterval->weight; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 825 | SmallVector<LiveInterval*, 8> spillIs; |
Lang Hames | 3319839 | 2010-09-02 08:27:00 +0000 | [diff] [blame] | 826 | rmf->rememberUseDefs(spillInterval); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 827 | std::vector<LiveInterval*> newSpills = |
Evan Cheng | c781a24 | 2009-05-03 18:32:42 +0000 | [diff] [blame] | 828 | lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm); |
| 829 | addStackInterval(spillInterval, mri); |
Lang Hames | 3319839 | 2010-09-02 08:27:00 +0000 | [diff] [blame] | 830 | rmf->rememberSpills(spillInterval, newSpills); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | bc84ad9 | 2009-08-20 20:01:34 +0000 | [diff] [blame] | 832 | (void) oldSpillWeight; |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 833 | DEBUG(dbgs() << "VREG " << virtReg << " -> SPILLED (Option: 0, Cost: " |
Lang Hames | 233fd9c | 2009-08-18 23:34:50 +0000 | [diff] [blame] | 834 | << oldSpillWeight << ", New vregs: "); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 835 | |
| 836 | // Copy any newly inserted live intervals into the list of regs to |
| 837 | // allocate. |
| 838 | for (std::vector<LiveInterval*>::const_iterator |
| 839 | itr = newSpills.begin(), end = newSpills.end(); |
| 840 | itr != end; ++itr) { |
| 841 | |
| 842 | assert(!(*itr)->empty() && "Empty spill range."); |
| 843 | |
David Greene | 3093154 | 2010-01-05 01:25:43 +0000 | [diff] [blame] | 844 | DEBUG(dbgs() << (*itr)->reg << " "); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 845 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 846 | vregsToAlloc.insert((*itr)->reg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 847 | } |
| 848 | |
David Greene | 3093154 | 2010-01-05 01:25:43 +0000 | [diff] [blame] | 849 | DEBUG(dbgs() << ")\n"); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 850 | |
| 851 | // We need another round if spill intervals were added. |
| 852 | anotherRoundNeeded |= !newSpills.empty(); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | return !anotherRoundNeeded; |
| 857 | } |
| 858 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 859 | bool RegAllocPBQP::mapPBQPToRegAlloc2(const PBQPRAProblem &problem, |
| 860 | const PBQP::Solution &solution) { |
| 861 | // Set to true if we have any spills |
| 862 | bool anotherRoundNeeded = false; |
| 863 | |
| 864 | // Clear the existing allocation. |
| 865 | vrm->clearAllVirt(); |
| 866 | |
| 867 | const PBQP::Graph &g = problem.getGraph(); |
| 868 | // Iterate over the nodes mapping the PBQP solution to a register |
| 869 | // assignment. |
| 870 | for (PBQP::Graph::ConstNodeItr node = g.nodesBegin(), |
| 871 | nodeEnd = g.nodesEnd(); |
| 872 | node != nodeEnd; ++node) { |
| 873 | unsigned vreg = problem.getVRegForNode(node); |
| 874 | unsigned alloc = solution.getSelection(node); |
| 875 | |
| 876 | if (problem.isPRegOption(vreg, alloc)) { |
| 877 | unsigned preg = problem.getPRegForOption(vreg, alloc); |
| 878 | DEBUG(dbgs() << "VREG " << vreg << " -> " << tri->getName(preg) << "\n"); |
| 879 | assert(preg != 0 && "Invalid preg selected."); |
| 880 | vrm->assignVirt2Phys(vreg, preg); |
| 881 | } else if (problem.isSpillOption(vreg, alloc)) { |
| 882 | vregsToAlloc.erase(vreg); |
| 883 | const LiveInterval* spillInterval = &lis->getInterval(vreg); |
| 884 | double oldWeight = spillInterval->weight; |
| 885 | SmallVector<LiveInterval*, 8> spillIs; |
| 886 | rmf->rememberUseDefs(spillInterval); |
| 887 | std::vector<LiveInterval*> newSpills = |
| 888 | lis->addIntervalsForSpills(*spillInterval, spillIs, loopInfo, *vrm); |
| 889 | addStackInterval(spillInterval, mri); |
| 890 | rmf->rememberSpills(spillInterval, newSpills); |
| 891 | |
| 892 | (void) oldWeight; |
| 893 | DEBUG(dbgs() << "VREG " << vreg << " -> SPILLED (Cost: " |
| 894 | << oldWeight << ", New vregs: "); |
| 895 | |
| 896 | // Copy any newly inserted live intervals into the list of regs to |
| 897 | // allocate. |
| 898 | for (std::vector<LiveInterval*>::const_iterator |
| 899 | itr = newSpills.begin(), end = newSpills.end(); |
| 900 | itr != end; ++itr) { |
| 901 | assert(!(*itr)->empty() && "Empty spill range."); |
| 902 | DEBUG(dbgs() << (*itr)->reg << " "); |
| 903 | vregsToAlloc.insert((*itr)->reg); |
| 904 | } |
| 905 | |
| 906 | DEBUG(dbgs() << ")\n"); |
| 907 | |
| 908 | // We need another round if spill intervals were added. |
| 909 | anotherRoundNeeded |= !newSpills.empty(); |
| 910 | } else { |
| 911 | assert(false && "Unknown allocation option."); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | return !anotherRoundNeeded; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | void RegAllocPBQP::finalizeAlloc() const { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 920 | typedef LiveIntervals::iterator LIIterator; |
| 921 | typedef LiveInterval::Ranges::const_iterator LRIterator; |
| 922 | |
| 923 | // First allocate registers for the empty intervals. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 924 | for (RegSet::const_iterator |
| 925 | itr = emptyIntervalVRegs.begin(), end = emptyIntervalVRegs.end(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 926 | itr != end; ++itr) { |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 927 | LiveInterval *li = &lis->getInterval(*itr); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 928 | |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 929 | unsigned physReg = vrm->getRegAllocPref(li->reg); |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 930 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 931 | if (physReg == 0) { |
| 932 | const TargetRegisterClass *liRC = mri->getRegClass(li->reg); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 933 | physReg = *liRC->allocation_order_begin(*mf); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 934 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 935 | |
| 936 | vrm->assignVirt2Phys(li->reg, physReg); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 937 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 938 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 939 | // Finally iterate over the basic blocks to compute and set the live-in sets. |
| 940 | SmallVector<MachineBasicBlock*, 8> liveInMBBs; |
| 941 | MachineBasicBlock *entryMBB = &*mf->begin(); |
| 942 | |
| 943 | for (LIIterator liItr = lis->begin(), liEnd = lis->end(); |
| 944 | liItr != liEnd; ++liItr) { |
| 945 | |
| 946 | const LiveInterval *li = liItr->second; |
| 947 | unsigned reg = 0; |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 948 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 949 | // Get the physical register for this interval |
| 950 | if (TargetRegisterInfo::isPhysicalRegister(li->reg)) { |
| 951 | reg = li->reg; |
| 952 | } |
| 953 | else if (vrm->isAssignedReg(li->reg)) { |
| 954 | reg = vrm->getPhys(li->reg); |
| 955 | } |
| 956 | else { |
| 957 | // Ranges which are assigned a stack slot only are ignored. |
| 958 | continue; |
| 959 | } |
| 960 | |
Lang Hames | b0e519f | 2009-05-17 23:50:36 +0000 | [diff] [blame] | 961 | if (reg == 0) { |
Lang Hames | 6699fb2 | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 962 | // Filter out zero regs - they're for intervals that were spilled. |
Lang Hames | b0e519f | 2009-05-17 23:50:36 +0000 | [diff] [blame] | 963 | continue; |
| 964 | } |
| 965 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 966 | // Iterate over the ranges of the current interval... |
| 967 | for (LRIterator lrItr = li->begin(), lrEnd = li->end(); |
| 968 | lrItr != lrEnd; ++lrItr) { |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 969 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 970 | // Find the set of basic blocks which this range is live into... |
| 971 | if (lis->findLiveInMBBs(lrItr->start, lrItr->end, liveInMBBs)) { |
| 972 | // And add the physreg for this interval to their live-in sets. |
| 973 | for (unsigned i = 0; i < liveInMBBs.size(); ++i) { |
| 974 | if (liveInMBBs[i] != entryMBB) { |
| 975 | if (!liveInMBBs[i]->isLiveIn(reg)) { |
| 976 | liveInMBBs[i]->addLiveIn(reg); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | liveInMBBs.clear(); |
| 981 | } |
| 982 | } |
| 983 | } |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 984 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 987 | bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 988 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 989 | mf = &MF; |
| 990 | tm = &mf->getTarget(); |
| 991 | tri = tm->getRegisterInfo(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 992 | tii = tm->getInstrInfo(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 993 | mri = &mf->getRegInfo(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 994 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 995 | lis = &getAnalysis<LiveIntervals>(); |
| 996 | lss = &getAnalysis<LiveStacks>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 997 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
Lang Hames | 3319839 | 2010-09-02 08:27:00 +0000 | [diff] [blame] | 998 | rmf = &getAnalysis<RenderMachineFunction>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 999 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 1000 | vrm = &getAnalysis<VirtRegMap>(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1001 | |
Lang Hames | 54cc2ef | 2010-07-19 15:22:28 +0000 | [diff] [blame] | 1002 | |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 1003 | DEBUG(dbgs() << "PBQP Register Allocating for " << mf->getFunction()->getName() << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1004 | |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1005 | // Allocator main loop: |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 1006 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1007 | // * Map current regalloc problem to a PBQP problem |
| 1008 | // * Solve the PBQP problem |
| 1009 | // * Map the solution back to a register allocation |
| 1010 | // * Spill if necessary |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 1011 | // |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1012 | // This process is continued till no more spills are generated. |
| 1013 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1014 | // Find the vreg intervals in need of allocation. |
| 1015 | findVRegIntervalsToAlloc(); |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 1016 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1017 | // If there are non-empty intervals allocate them using pbqp. |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1018 | if (!vregsToAlloc.empty()) { |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1019 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1020 | bool pbqpAllocComplete = false; |
| 1021 | unsigned round = 0; |
| 1022 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1023 | if (!pbqpBuilder) { |
| 1024 | while (!pbqpAllocComplete) { |
| 1025 | DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1026 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1027 | PBQP::Graph problem = constructPBQPProblem(); |
| 1028 | PBQP::Solution solution = |
| 1029 | PBQP::HeuristicSolver<PBQP::Heuristics::Briggs>::solve(problem); |
Lang Hames | 233fd9c | 2009-08-18 23:34:50 +0000 | [diff] [blame] | 1030 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1031 | pbqpAllocComplete = mapPBQPToRegAlloc(solution); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1032 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1033 | ++round; |
| 1034 | } |
| 1035 | } else { |
| 1036 | while (!pbqpAllocComplete) { |
| 1037 | DEBUG(dbgs() << " PBQP Regalloc round " << round << ":\n"); |
| 1038 | |
| 1039 | std::auto_ptr<PBQPRAProblem> problem = |
| 1040 | builder->build(mf, lis, vregsToAlloc); |
| 1041 | PBQP::Solution solution = |
| 1042 | HeuristicSolver<Briggs>::solve(problem->getGraph()); |
| 1043 | |
| 1044 | pbqpAllocComplete = mapPBQPToRegAlloc2(*problem, solution); |
| 1045 | |
| 1046 | ++round; |
| 1047 | } |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1048 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1051 | // Finalise allocation, allocate empty ranges. |
| 1052 | finalizeAlloc(); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1053 | |
Lang Hames | c4bcc77 | 2010-07-20 07:41:44 +0000 | [diff] [blame] | 1054 | rmf->renderMachineFunction("After PBQP register allocation.", vrm); |
| 1055 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1056 | vregsToAlloc.clear(); |
| 1057 | emptyIntervalVRegs.clear(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1058 | li2Node.clear(); |
| 1059 | node2LI.clear(); |
| 1060 | allowedSets.clear(); |
Lang Hames | 030c4bf | 2010-01-26 04:49:58 +0000 | [diff] [blame] | 1061 | problemNodes.clear(); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1062 | |
David Greene | 3093154 | 2010-01-05 01:25:43 +0000 | [diff] [blame] | 1063 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm << "\n"); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1064 | |
Lang Hames | 87e3bca | 2009-05-06 02:36:21 +0000 | [diff] [blame] | 1065 | // Run rewriter |
| 1066 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 1067 | |
| 1068 | rewriter->runOnMachineFunction(*mf, *vrm, lis); |
Lang Hames | 27601ef | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 1069 | |
Misha Brukman | 2a835f9 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 1070 | return true; |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1073 | FunctionPass* createPBQPRegisterAllocator() { |
| 1074 | return new RegAllocPBQP(std::auto_ptr<PBQPBuilder>(new PBQPBuilder())); |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Lang Hames | eb6c8f5 | 2010-09-18 09:07:10 +0000 | [diff] [blame^] | 1077 | } |
Evan Cheng | b1290a6 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 1078 | |
| 1079 | #undef DEBUG_TYPE |