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