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" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetSubtargetInfo.h" |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 53 | #include <limits> |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 54 | #include <memory> |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 55 | #include <queue> |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 56 | #include <set> |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 57 | #include <sstream> |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 58 | #include <vector> |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 59 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 62 | #define DEBUG_TYPE "regalloc" |
| 63 | |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 64 | static RegisterRegAlloc |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 65 | RegisterPBQPRepAlloc("pbqp", "PBQP register allocator", |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 66 | createDefaultPBQPRegisterAllocator); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 67 | |
Lang Hames | 11732ad | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 69 | PBQPCoalescing("pbqp-coalescing", |
Lang Hames | 090c7e8 | 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 | 11732ad | 2009-08-19 01:36:14 +0000 | [diff] [blame] | 72 | |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 73 | #ifndef NDEBUG |
| 74 | static cl::opt<bool> |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 75 | PBQPDumpGraphs("pbqp-dump-graphs", |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 76 | cl::desc("Dump graphs for each function/round in the compilation unit."), |
| 77 | cl::init(false), cl::Hidden); |
| 78 | #endif |
| 79 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | |
| 82 | /// |
| 83 | /// PBQP based allocators solve the register allocation problem by mapping |
| 84 | /// register allocation problems to Partitioned Boolean Quadratic |
| 85 | /// Programming problems. |
| 86 | class RegAllocPBQP : public MachineFunctionPass { |
| 87 | public: |
| 88 | |
| 89 | static char ID; |
| 90 | |
| 91 | /// Construct a PBQP register allocator. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 92 | RegAllocPBQP(char *cPassID = nullptr) |
| 93 | : MachineFunctionPass(ID), customPassID(cPassID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 94 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 95 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 96 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 97 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 98 | } |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 99 | |
| 100 | /// Return the pass name. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 101 | const char* getPassName() const override { |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 102 | return "PBQP Register Allocator"; |
| 103 | } |
| 104 | |
| 105 | /// PBQP analysis usage. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 106 | void getAnalysisUsage(AnalysisUsage &au) const override; |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 107 | |
| 108 | /// Perform register allocation |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 109 | bool runOnMachineFunction(MachineFunction &MF) override; |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | |
| 113 | typedef std::map<const LiveInterval*, unsigned> LI2NodeMap; |
| 114 | typedef std::vector<const LiveInterval*> Node2LIMap; |
| 115 | typedef std::vector<unsigned> AllowedSet; |
| 116 | typedef std::vector<AllowedSet> AllowedSetMap; |
| 117 | typedef std::pair<unsigned, unsigned> RegPair; |
| 118 | typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap; |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 119 | typedef std::set<unsigned> RegSet; |
| 120 | |
Lang Hames | 934625e | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 121 | char *customPassID; |
| 122 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 123 | RegSet VRegsToAlloc, EmptyIntervalVRegs; |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 124 | |
| 125 | /// \brief Finds the initial set of vreg intervals to allocate. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 126 | void findVRegIntervalsToAlloc(const MachineFunction &MF, LiveIntervals &LIS); |
| 127 | |
| 128 | /// \brief Constructs an initial graph. |
| 129 | void initializeGraph(PBQPRAGraph &G); |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 130 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 131 | /// \brief Given a solved PBQP problem maps this solution back to a register |
| 132 | /// assignment. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 133 | bool mapPBQPToRegAlloc(const PBQPRAGraph &G, |
| 134 | const PBQP::Solution &Solution, |
| 135 | VirtRegMap &VRM, |
| 136 | Spiller &VRegSpiller); |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 137 | |
| 138 | /// \brief Postprocessing before final spilling. Sets basic block "live in" |
| 139 | /// variables. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 140 | void finalizeAlloc(MachineFunction &MF, LiveIntervals &LIS, |
| 141 | VirtRegMap &VRM) const; |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 142 | |
| 143 | }; |
| 144 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 145 | char RegAllocPBQP::ID = 0; |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 146 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 147 | /// @brief Set spill costs for each node in the PBQP reg-alloc graph. |
| 148 | class SpillCosts : public PBQPRAConstraint { |
| 149 | public: |
| 150 | void apply(PBQPRAGraph &G) override { |
| 151 | LiveIntervals &LIS = G.getMetadata().LIS; |
| 152 | |
| 153 | for (auto NId : G.nodeIds()) { |
| 154 | PBQP::PBQPNum SpillCost = |
| 155 | LIS.getInterval(G.getNodeMetadata(NId).getVReg()).weight; |
| 156 | if (SpillCost == 0.0) |
| 157 | SpillCost = std::numeric_limits<PBQP::PBQPNum>::min(); |
| 158 | PBQPRAGraph::RawVector NodeCosts(G.getNodeCosts(NId)); |
| 159 | NodeCosts[PBQP::RegAlloc::getSpillOptionIdx()] = SpillCost; |
| 160 | G.setNodeCosts(NId, std::move(NodeCosts)); |
| 161 | } |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | /// @brief Add interference edges between overlapping vregs. |
| 166 | class Interference : public PBQPRAConstraint { |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 167 | private: |
| 168 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 169 | private: |
| 170 | |
| 171 | typedef const PBQP::RegAlloc::AllowedRegVector* AllowedRegVecPtr; |
| 172 | typedef std::pair<AllowedRegVecPtr, AllowedRegVecPtr> IMatrixKey; |
| 173 | typedef DenseMap<IMatrixKey, PBQPRAGraph::MatrixPtr> IMatrixCache; |
| 174 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 175 | // Holds (Interval, CurrentSegmentID, and NodeId). The first two are required |
| 176 | // for the fast interference graph construction algorithm. The last is there |
| 177 | // to save us from looking up node ids via the VRegToNode map in the graph |
| 178 | // metadata. |
| 179 | typedef std::tuple<LiveInterval*, size_t, PBQP::GraphBase::NodeId> |
| 180 | IntervalInfo; |
| 181 | |
| 182 | static SlotIndex getStartPoint(const IntervalInfo &I) { |
| 183 | return std::get<0>(I)->segments[std::get<1>(I)].start; |
| 184 | } |
| 185 | |
| 186 | static SlotIndex getEndPoint(const IntervalInfo &I) { |
| 187 | return std::get<0>(I)->segments[std::get<1>(I)].end; |
| 188 | } |
| 189 | |
| 190 | static PBQP::GraphBase::NodeId getNodeId(const IntervalInfo &I) { |
| 191 | return std::get<2>(I); |
| 192 | } |
| 193 | |
| 194 | static bool lowestStartPoint(const IntervalInfo &I1, |
| 195 | const IntervalInfo &I2) { |
| 196 | // Condition reversed because priority queue has the *highest* element at |
| 197 | // the front, rather than the lowest. |
| 198 | return getStartPoint(I1) > getStartPoint(I2); |
| 199 | } |
| 200 | |
| 201 | static bool lowestEndPoint(const IntervalInfo &I1, |
| 202 | const IntervalInfo &I2) { |
| 203 | SlotIndex E1 = getEndPoint(I1); |
| 204 | SlotIndex E2 = getEndPoint(I2); |
| 205 | |
| 206 | if (E1 < E2) |
| 207 | return true; |
| 208 | |
| 209 | if (E1 > E2) |
| 210 | return false; |
| 211 | |
| 212 | // If two intervals end at the same point, we need a way to break the tie or |
| 213 | // the set will assume they're actually equal and refuse to insert a |
| 214 | // "duplicate". Just compare the vregs - fast and guaranteed unique. |
| 215 | return std::get<0>(I1)->reg < std::get<0>(I2)->reg; |
| 216 | } |
| 217 | |
| 218 | static bool isAtLastSegment(const IntervalInfo &I) { |
| 219 | return std::get<1>(I) == std::get<0>(I)->size() - 1; |
| 220 | } |
| 221 | |
| 222 | static IntervalInfo nextSegment(const IntervalInfo &I) { |
| 223 | return std::make_tuple(std::get<0>(I), std::get<1>(I) + 1, std::get<2>(I)); |
| 224 | } |
| 225 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 226 | public: |
| 227 | |
| 228 | void apply(PBQPRAGraph &G) override { |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 229 | // The following is loosely based on the linear scan algorithm introduced in |
| 230 | // "Linear Scan Register Allocation" by Poletto and Sarkar. This version |
| 231 | // isn't linear, because the size of the active set isn't bound by the |
| 232 | // number of registers, but rather the size of the largest clique in the |
| 233 | // graph. Still, we expect this to be better than N^2. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 234 | LiveIntervals &LIS = G.getMetadata().LIS; |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 235 | |
| 236 | // Interferenc matrices are incredibly regular - they're only a function of |
| 237 | // the allowed sets, so we cache them to avoid the overhead of constructing |
| 238 | // and uniquing them. |
| 239 | IMatrixCache C; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 240 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 241 | typedef std::set<IntervalInfo, decltype(&lowestEndPoint)> IntervalSet; |
| 242 | typedef std::priority_queue<IntervalInfo, std::vector<IntervalInfo>, |
| 243 | decltype(&lowestStartPoint)> IntervalQueue; |
| 244 | IntervalSet Active(lowestEndPoint); |
| 245 | IntervalQueue Inactive(lowestStartPoint); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 246 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 247 | // Start by building the inactive set. |
| 248 | for (auto NId : G.nodeIds()) { |
| 249 | unsigned VReg = G.getNodeMetadata(NId).getVReg(); |
| 250 | LiveInterval &LI = LIS.getInterval(VReg); |
| 251 | assert(!LI.empty() && "PBQP graph contains node for empty interval"); |
| 252 | Inactive.push(std::make_tuple(&LI, 0, NId)); |
| 253 | } |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 254 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 255 | while (!Inactive.empty()) { |
| 256 | // Tentatively grab the "next" interval - this choice may be overriden |
| 257 | // below. |
| 258 | IntervalInfo Cur = Inactive.top(); |
| 259 | |
| 260 | // Retire any active intervals that end before Cur starts. |
| 261 | IntervalSet::iterator RetireItr = Active.begin(); |
| 262 | while (RetireItr != Active.end() && |
| 263 | (getEndPoint(*RetireItr) <= getStartPoint(Cur))) { |
| 264 | // If this interval has subsequent segments, add the next one to the |
| 265 | // inactive list. |
| 266 | if (!isAtLastSegment(*RetireItr)) |
| 267 | Inactive.push(nextSegment(*RetireItr)); |
| 268 | |
| 269 | ++RetireItr; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 270 | } |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 271 | Active.erase(Active.begin(), RetireItr); |
| 272 | |
| 273 | // One of the newly retired segments may actually start before the |
| 274 | // Cur segment, so re-grab the front of the inactive list. |
| 275 | Cur = Inactive.top(); |
| 276 | Inactive.pop(); |
| 277 | |
| 278 | // At this point we know that Cur overlaps all active intervals. Add the |
| 279 | // interference edges. |
| 280 | PBQP::GraphBase::NodeId NId = getNodeId(Cur); |
| 281 | for (const auto &A : Active) { |
| 282 | PBQP::GraphBase::NodeId MId = getNodeId(A); |
| 283 | |
| 284 | // Check that we haven't already added this edge |
| 285 | // FIXME: findEdge is expensive in the worst case (O(max_clique(G))). |
| 286 | // It might be better to replace this with a local bit-matrix. |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 287 | if (G.findEdge(NId, MId) != PBQPRAGraph::invalidEdgeId()) |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 288 | continue; |
| 289 | |
| 290 | // This is a new edge - add it to the graph. |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 291 | createInterferenceEdge(G, NId, MId, C); |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Finally, add Cur to the Active set. |
| 295 | Active.insert(Cur); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | private: |
| 300 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 301 | void createInterferenceEdge(PBQPRAGraph &G, PBQPRAGraph::NodeId NId, |
| 302 | PBQPRAGraph::NodeId MId, IMatrixCache &C) { |
| 303 | |
| 304 | const TargetRegisterInfo &TRI = |
| 305 | *G.getMetadata().MF.getTarget().getSubtargetImpl()->getRegisterInfo(); |
| 306 | |
| 307 | const auto &NRegs = G.getNodeMetadata(NId).getAllowedRegs(); |
| 308 | const auto &MRegs = G.getNodeMetadata(MId).getAllowedRegs(); |
| 309 | |
| 310 | // Try looking the edge costs up in the IMatrixCache first. |
| 311 | IMatrixKey K(&NRegs, &MRegs); |
| 312 | IMatrixCache::iterator I = C.find(K); |
| 313 | if (I != C.end()) { |
| 314 | G.addEdgeBypassingCostAllocator(NId, MId, I->second); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | PBQPRAGraph::RawMatrix M(NRegs.size() + 1, MRegs.size() + 1, 0); |
| 319 | for (unsigned I = 0; I != NRegs.size(); ++I) { |
| 320 | unsigned PRegN = NRegs[I]; |
| 321 | for (unsigned J = 0; J != MRegs.size(); ++J) { |
| 322 | unsigned PRegM = MRegs[J]; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 323 | if (TRI.regsOverlap(PRegN, PRegM)) |
| 324 | M[I + 1][J + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity(); |
| 325 | } |
| 326 | } |
| 327 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 328 | PBQPRAGraph::EdgeId EId = G.addEdge(NId, MId, std::move(M)); |
| 329 | C[K] = G.getEdgeCostsPtr(EId); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 330 | } |
| 331 | }; |
| 332 | |
| 333 | |
| 334 | class Coalescing : public PBQPRAConstraint { |
| 335 | public: |
| 336 | void apply(PBQPRAGraph &G) override { |
| 337 | MachineFunction &MF = G.getMetadata().MF; |
| 338 | MachineBlockFrequencyInfo &MBFI = G.getMetadata().MBFI; |
| 339 | CoalescerPair CP(*MF.getTarget().getSubtargetImpl()->getRegisterInfo()); |
| 340 | |
| 341 | // Scan the machine function and add a coalescing cost whenever CoalescerPair |
| 342 | // gives the Ok. |
| 343 | for (const auto &MBB : MF) { |
| 344 | for (const auto &MI : MBB) { |
| 345 | |
| 346 | // Skip not-coalescable or already coalesced copies. |
| 347 | if (!CP.setRegisters(&MI) || CP.getSrcReg() == CP.getDstReg()) |
| 348 | continue; |
| 349 | |
| 350 | unsigned DstReg = CP.getDstReg(); |
| 351 | unsigned SrcReg = CP.getSrcReg(); |
| 352 | |
| 353 | const float CopyFactor = 0.5; // Cost of copy relative to load. Current |
| 354 | // value plucked randomly out of the air. |
| 355 | |
| 356 | PBQP::PBQPNum CBenefit = |
| 357 | CopyFactor * LiveIntervals::getSpillWeight(false, true, &MBFI, &MI); |
| 358 | |
| 359 | if (CP.isPhys()) { |
| 360 | if (!MF.getRegInfo().isAllocatable(DstReg)) |
| 361 | continue; |
| 362 | |
| 363 | PBQPRAGraph::NodeId NId = G.getMetadata().getNodeIdForVReg(SrcReg); |
| 364 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 365 | const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed = |
| 366 | G.getNodeMetadata(NId).getAllowedRegs(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 367 | |
| 368 | unsigned PRegOpt = 0; |
| 369 | while (PRegOpt < Allowed.size() && Allowed[PRegOpt] != DstReg) |
| 370 | ++PRegOpt; |
| 371 | |
| 372 | if (PRegOpt < Allowed.size()) { |
| 373 | PBQPRAGraph::RawVector NewCosts(G.getNodeCosts(NId)); |
Arnaud A. de Grandmaison | d3648d0 | 2014-10-21 16:24:15 +0000 | [diff] [blame] | 374 | NewCosts[PRegOpt + 1] -= CBenefit; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 375 | G.setNodeCosts(NId, std::move(NewCosts)); |
| 376 | } |
| 377 | } else { |
| 378 | PBQPRAGraph::NodeId N1Id = G.getMetadata().getNodeIdForVReg(DstReg); |
| 379 | PBQPRAGraph::NodeId N2Id = G.getMetadata().getNodeIdForVReg(SrcReg); |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 380 | const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed1 = |
| 381 | &G.getNodeMetadata(N1Id).getAllowedRegs(); |
| 382 | const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed2 = |
| 383 | &G.getNodeMetadata(N2Id).getAllowedRegs(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 384 | |
| 385 | PBQPRAGraph::EdgeId EId = G.findEdge(N1Id, N2Id); |
| 386 | if (EId == G.invalidEdgeId()) { |
| 387 | PBQPRAGraph::RawMatrix Costs(Allowed1->size() + 1, |
| 388 | Allowed2->size() + 1, 0); |
| 389 | addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit); |
| 390 | G.addEdge(N1Id, N2Id, std::move(Costs)); |
| 391 | } else { |
| 392 | if (G.getEdgeNode1Id(EId) == N2Id) { |
| 393 | std::swap(N1Id, N2Id); |
| 394 | std::swap(Allowed1, Allowed2); |
| 395 | } |
| 396 | PBQPRAGraph::RawMatrix Costs(G.getEdgeCosts(EId)); |
| 397 | addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit); |
| 398 | G.setEdgeCosts(EId, std::move(Costs)); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private: |
| 406 | |
| 407 | void addVirtRegCoalesce( |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 408 | PBQPRAGraph::RawMatrix &CostMat, |
| 409 | const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed1, |
| 410 | const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed2, |
| 411 | PBQP::PBQPNum Benefit) { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 412 | assert(CostMat.getRows() == Allowed1.size() + 1 && "Size mismatch."); |
| 413 | assert(CostMat.getCols() == Allowed2.size() + 1 && "Size mismatch."); |
| 414 | for (unsigned I = 0; I != Allowed1.size(); ++I) { |
| 415 | unsigned PReg1 = Allowed1[I]; |
| 416 | for (unsigned J = 0; J != Allowed2.size(); ++J) { |
| 417 | unsigned PReg2 = Allowed2[J]; |
| 418 | if (PReg1 == PReg2) |
Arnaud A. de Grandmaison | d3648d0 | 2014-10-21 16:24:15 +0000 | [diff] [blame] | 419 | CostMat[I + 1][J + 1] -= Benefit; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | }; |
| 425 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 426 | } // End anonymous namespace. |
| 427 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 428 | // Out-of-line destructor/anchor for PBQPRAConstraint. |
| 429 | PBQPRAConstraint::~PBQPRAConstraint() {} |
| 430 | void PBQPRAConstraint::anchor() {} |
| 431 | void PBQPRAConstraintList::anchor() {} |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 432 | |
| 433 | void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const { |
Lang Hames | b13b6a0 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 434 | au.setPreservesCFG(); |
| 435 | au.addRequired<AliasAnalysis>(); |
| 436 | au.addPreserved<AliasAnalysis>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 437 | au.addRequired<SlotIndexes>(); |
| 438 | au.addPreserved<SlotIndexes>(); |
| 439 | au.addRequired<LiveIntervals>(); |
Lang Hames | 8ce99f2 | 2012-10-04 04:50:53 +0000 | [diff] [blame] | 440 | au.addPreserved<LiveIntervals>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 441 | //au.addRequiredID(SplitCriticalEdgesID); |
Lang Hames | 934625e | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 442 | if (customPassID) |
| 443 | au.addRequiredID(*customPassID); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 444 | au.addRequired<LiveStacks>(); |
| 445 | au.addPreserved<LiveStacks>(); |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 446 | au.addRequired<MachineBlockFrequencyInfo>(); |
| 447 | au.addPreserved<MachineBlockFrequencyInfo>(); |
Lang Hames | 7d99d79 | 2013-07-01 20:47:47 +0000 | [diff] [blame] | 448 | au.addRequired<MachineLoopInfo>(); |
| 449 | au.addPreserved<MachineLoopInfo>(); |
Lang Hames | b13b6a0 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 450 | au.addRequired<MachineDominatorTree>(); |
| 451 | au.addPreserved<MachineDominatorTree>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 452 | au.addRequired<VirtRegMap>(); |
Lang Hames | 8ce99f2 | 2012-10-04 04:50:53 +0000 | [diff] [blame] | 453 | au.addPreserved<VirtRegMap>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 454 | MachineFunctionPass::getAnalysisUsage(au); |
| 455 | } |
| 456 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 457 | void RegAllocPBQP::findVRegIntervalsToAlloc(const MachineFunction &MF, |
| 458 | LiveIntervals &LIS) { |
| 459 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 460 | |
| 461 | // Iterate over all live ranges. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 462 | for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) { |
| 463 | unsigned Reg = TargetRegisterInfo::index2VirtReg(I); |
| 464 | if (MRI.reg_nodbg_empty(Reg)) |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 465 | continue; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 466 | LiveInterval &LI = LIS.getInterval(Reg); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 467 | |
| 468 | // If this live interval is non-empty we will use pbqp to allocate it. |
| 469 | // Empty intervals we allocate in a simple post-processing stage in |
| 470 | // finalizeAlloc. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 471 | if (!LI.empty()) { |
| 472 | VRegsToAlloc.insert(LI.reg); |
Lang Hames | c702ba6 | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 473 | } else { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 474 | EmptyIntervalVRegs.insert(LI.reg); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 479 | void RegAllocPBQP::initializeGraph(PBQPRAGraph &G) { |
| 480 | MachineFunction &MF = G.getMetadata().MF; |
| 481 | |
| 482 | LiveIntervals &LIS = G.getMetadata().LIS; |
| 483 | const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo(); |
| 484 | const TargetRegisterInfo &TRI = |
| 485 | *G.getMetadata().MF.getTarget().getSubtargetImpl()->getRegisterInfo(); |
| 486 | |
| 487 | for (auto VReg : VRegsToAlloc) { |
| 488 | const TargetRegisterClass *TRC = MRI.getRegClass(VReg); |
| 489 | LiveInterval &VRegLI = LIS.getInterval(VReg); |
| 490 | |
| 491 | // Record any overlaps with regmask operands. |
| 492 | BitVector RegMaskOverlaps; |
| 493 | LIS.checkRegMaskInterference(VRegLI, RegMaskOverlaps); |
| 494 | |
| 495 | // Compute an initial allowed set for the current vreg. |
| 496 | std::vector<unsigned> VRegAllowed; |
| 497 | ArrayRef<MCPhysReg> RawPRegOrder = TRC->getRawAllocationOrder(MF); |
| 498 | for (unsigned I = 0; I != RawPRegOrder.size(); ++I) { |
| 499 | unsigned PReg = RawPRegOrder[I]; |
| 500 | if (MRI.isReserved(PReg)) |
| 501 | continue; |
| 502 | |
| 503 | // vregLI crosses a regmask operand that clobbers preg. |
| 504 | if (!RegMaskOverlaps.empty() && !RegMaskOverlaps.test(PReg)) |
| 505 | continue; |
| 506 | |
| 507 | // vregLI overlaps fixed regunit interference. |
| 508 | bool Interference = false; |
| 509 | for (MCRegUnitIterator Units(PReg, &TRI); Units.isValid(); ++Units) { |
| 510 | if (VRegLI.overlaps(LIS.getRegUnit(*Units))) { |
| 511 | Interference = true; |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | if (Interference) |
| 516 | continue; |
| 517 | |
| 518 | // preg is usable for this virtual register. |
| 519 | VRegAllowed.push_back(PReg); |
| 520 | } |
| 521 | |
| 522 | PBQPRAGraph::RawVector NodeCosts(VRegAllowed.size() + 1, 0); |
| 523 | PBQPRAGraph::NodeId NId = G.addNode(std::move(NodeCosts)); |
| 524 | G.getNodeMetadata(NId).setVReg(VReg); |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 525 | G.getNodeMetadata(NId).setAllowedRegs( |
| 526 | G.getMetadata().getAllowedRegs(std::move(VRegAllowed))); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 527 | G.getMetadata().setNodeIdForVReg(VReg, NId); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAGraph &G, |
| 532 | const PBQP::Solution &Solution, |
| 533 | VirtRegMap &VRM, |
| 534 | Spiller &VRegSpiller) { |
| 535 | MachineFunction &MF = G.getMetadata().MF; |
| 536 | LiveIntervals &LIS = G.getMetadata().LIS; |
| 537 | const TargetRegisterInfo &TRI = |
| 538 | *MF.getTarget().getSubtargetImpl()->getRegisterInfo(); |
| 539 | (void)TRI; |
| 540 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 541 | // Set to true if we have any spills |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 542 | bool AnotherRoundNeeded = false; |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 543 | |
| 544 | // Clear the existing allocation. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 545 | VRM.clearAllVirt(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 546 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 547 | // Iterate over the nodes mapping the PBQP solution to a register |
| 548 | // assignment. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 549 | for (auto NId : G.nodeIds()) { |
| 550 | unsigned VReg = G.getNodeMetadata(NId).getVReg(); |
| 551 | unsigned AllocOption = Solution.getSelection(NId); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 552 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 553 | if (AllocOption != PBQP::RegAlloc::getSpillOptionIdx()) { |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 554 | unsigned PReg = G.getNodeMetadata(NId).getAllowedRegs()[AllocOption - 1]; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 555 | DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> " |
| 556 | << TRI.getName(PReg) << "\n"); |
| 557 | assert(PReg != 0 && "Invalid preg selected."); |
| 558 | VRM.assignVirt2Phys(VReg, PReg); |
| 559 | } else { |
| 560 | VRegsToAlloc.erase(VReg); |
| 561 | SmallVector<unsigned, 8> NewSpills; |
| 562 | LiveRangeEdit LRE(&LIS.getInterval(VReg), NewSpills, MF, LIS, &VRM); |
| 563 | VRegSpiller.spill(LRE); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 564 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 565 | DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> SPILLED (Cost: " |
Jakob Stoklund Olesen | 11bb63a | 2011-11-12 23:17:52 +0000 | [diff] [blame] | 566 | << LRE.getParent().weight << ", New vregs: "); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 567 | |
| 568 | // Copy any newly inserted live intervals into the list of regs to |
| 569 | // allocate. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 570 | for (LiveRangeEdit::iterator I = LRE.begin(), E = LRE.end(); |
| 571 | I != E; ++I) { |
| 572 | LiveInterval &LI = LIS.getInterval(*I); |
| 573 | assert(!LI.empty() && "Empty spill range."); |
| 574 | DEBUG(dbgs() << PrintReg(LI.reg, &TRI) << " "); |
| 575 | VRegsToAlloc.insert(LI.reg); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | DEBUG(dbgs() << ")\n"); |
| 579 | |
| 580 | // We need another round if spill intervals were added. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 581 | AnotherRoundNeeded |= !LRE.empty(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 585 | return !AnotherRoundNeeded; |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 588 | void RegAllocPBQP::finalizeAlloc(MachineFunction &MF, |
| 589 | LiveIntervals &LIS, |
| 590 | VirtRegMap &VRM) const { |
| 591 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 592 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 593 | // First allocate registers for the empty intervals. |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 594 | for (RegSet::const_iterator |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 595 | I = EmptyIntervalVRegs.begin(), E = EmptyIntervalVRegs.end(); |
| 596 | I != E; ++I) { |
| 597 | LiveInterval &LI = LIS.getInterval(*I); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 598 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 599 | unsigned PReg = MRI.getSimpleHint(LI.reg); |
Lang Hames | 88fae6f | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 600 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 601 | if (PReg == 0) { |
| 602 | const TargetRegisterClass &RC = *MRI.getRegClass(LI.reg); |
| 603 | PReg = RC.getRawAllocationOrder(MF).front(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 604 | } |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 605 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 606 | VRM.assignVirt2Phys(LI.reg, PReg); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 607 | } |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 610 | bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 611 | LiveIntervals &LIS = getAnalysis<LiveIntervals>(); |
| 612 | MachineBlockFrequencyInfo &MBFI = |
| 613 | getAnalysis<MachineBlockFrequencyInfo>(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 614 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 615 | calculateSpillWeightsAndHints(LIS, MF, getAnalysis<MachineLoopInfo>(), MBFI); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 616 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 617 | VirtRegMap &VRM = getAnalysis<VirtRegMap>(); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 618 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 619 | std::unique_ptr<Spiller> VRegSpiller(createInlineSpiller(*this, MF, VRM)); |
Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 620 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 621 | MF.getRegInfo().freezeReservedRegs(MF); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 622 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 623 | DEBUG(dbgs() << "PBQP Register Allocating for " << MF.getName() << "\n"); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 624 | |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 625 | // Allocator main loop: |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 626 | // |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 627 | // * Map current regalloc problem to a PBQP problem |
| 628 | // * Solve the PBQP problem |
| 629 | // * Map the solution back to a register allocation |
| 630 | // * Spill if necessary |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 631 | // |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 632 | // This process is continued till no more spills are generated. |
| 633 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 634 | // Find the vreg intervals in need of allocation. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 635 | findVRegIntervalsToAlloc(MF, LIS); |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 636 | |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 637 | #ifndef NDEBUG |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 638 | const Function &F = *MF.getFunction(); |
| 639 | std::string FullyQualifiedName = |
| 640 | F.getParent()->getModuleIdentifier() + "." + F.getName().str(); |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 641 | #endif |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 642 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 643 | // If there are non-empty intervals allocate them using pbqp. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 644 | if (!VRegsToAlloc.empty()) { |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 645 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 646 | const TargetSubtargetInfo &Subtarget = *MF.getTarget().getSubtargetImpl(); |
| 647 | std::unique_ptr<PBQPRAConstraintList> ConstraintsRoot = |
| 648 | llvm::make_unique<PBQPRAConstraintList>(); |
| 649 | ConstraintsRoot->addConstraint(llvm::make_unique<SpillCosts>()); |
| 650 | ConstraintsRoot->addConstraint(llvm::make_unique<Interference>()); |
| 651 | if (PBQPCoalescing) |
| 652 | ConstraintsRoot->addConstraint(llvm::make_unique<Coalescing>()); |
| 653 | ConstraintsRoot->addConstraint(Subtarget.getCustomPBQPConstraints()); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 654 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 655 | bool PBQPAllocComplete = false; |
| 656 | unsigned Round = 0; |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 657 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 658 | while (!PBQPAllocComplete) { |
| 659 | DEBUG(dbgs() << " PBQP Regalloc round " << Round << ":\n"); |
| 660 | |
| 661 | PBQPRAGraph G(PBQPRAGraph::GraphMetadata(MF, LIS, MBFI)); |
| 662 | initializeGraph(G); |
| 663 | ConstraintsRoot->apply(G); |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 664 | |
| 665 | #ifndef NDEBUG |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 666 | if (PBQPDumpGraphs) { |
| 667 | std::ostringstream RS; |
| 668 | RS << Round; |
| 669 | std::string GraphFileName = FullyQualifiedName + "." + RS.str() + |
| 670 | ".pbqpgraph"; |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 671 | std::error_code EC; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 672 | raw_fd_ostream OS(GraphFileName, EC, sys::fs::F_Text); |
| 673 | DEBUG(dbgs() << "Dumping graph for round " << Round << " to \"" |
| 674 | << GraphFileName << "\"\n"); |
| 675 | G.dumpToStream(OS); |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 676 | } |
| 677 | #endif |
| 678 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 679 | PBQP::Solution Solution = PBQP::RegAlloc::solve(G); |
| 680 | PBQPAllocComplete = mapPBQPToRegAlloc(G, Solution, VRM, *VRegSpiller); |
| 681 | ++Round; |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 682 | } |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 685 | // Finalise allocation, allocate empty ranges. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 686 | finalizeAlloc(MF, LIS, VRM); |
| 687 | VRegsToAlloc.clear(); |
| 688 | EmptyIntervalVRegs.clear(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 689 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 690 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << VRM << "\n"); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 691 | |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 692 | return true; |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 695 | FunctionPass *llvm::createPBQPRegisterAllocator(char *customPassID) { |
| 696 | return new RegAllocPBQP(customPassID); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 699 | FunctionPass* llvm::createDefaultPBQPRegisterAllocator() { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 700 | return createPBQPRegisterAllocator(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 701 | } |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 702 | |
| 703 | #undef DEBUG_TYPE |