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. |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 129 | void initializeGraph(PBQPRAGraph &G, VirtRegMap &VRM, Spiller &VRegSpiller); |
| 130 | |
| 131 | /// \brief Spill the given VReg. |
| 132 | void spillVReg(unsigned VReg, SmallVectorImpl<unsigned> &NewIntervals, |
| 133 | MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM, |
| 134 | Spiller &VRegSpiller); |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 135 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 136 | /// \brief Given a solved PBQP problem maps this solution back to a register |
| 137 | /// assignment. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 138 | bool mapPBQPToRegAlloc(const PBQPRAGraph &G, |
| 139 | const PBQP::Solution &Solution, |
| 140 | VirtRegMap &VRM, |
| 141 | Spiller &VRegSpiller); |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 142 | |
| 143 | /// \brief Postprocessing before final spilling. Sets basic block "live in" |
| 144 | /// variables. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 145 | void finalizeAlloc(MachineFunction &MF, LiveIntervals &LIS, |
| 146 | VirtRegMap &VRM) const; |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 147 | |
| 148 | }; |
| 149 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 150 | char RegAllocPBQP::ID = 0; |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 151 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 152 | /// @brief Set spill costs for each node in the PBQP reg-alloc graph. |
| 153 | class SpillCosts : public PBQPRAConstraint { |
| 154 | public: |
| 155 | void apply(PBQPRAGraph &G) override { |
| 156 | LiveIntervals &LIS = G.getMetadata().LIS; |
| 157 | |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 158 | // A minimum spill costs, so that register constraints can can be set |
| 159 | // without normalization in the [0.0:MinSpillCost( interval. |
| 160 | const PBQP::PBQPNum MinSpillCost = 10.0; |
| 161 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 162 | for (auto NId : G.nodeIds()) { |
| 163 | PBQP::PBQPNum SpillCost = |
| 164 | LIS.getInterval(G.getNodeMetadata(NId).getVReg()).weight; |
| 165 | if (SpillCost == 0.0) |
| 166 | SpillCost = std::numeric_limits<PBQP::PBQPNum>::min(); |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 167 | else |
| 168 | SpillCost += MinSpillCost; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 169 | PBQPRAGraph::RawVector NodeCosts(G.getNodeCosts(NId)); |
| 170 | NodeCosts[PBQP::RegAlloc::getSpillOptionIdx()] = SpillCost; |
| 171 | G.setNodeCosts(NId, std::move(NodeCosts)); |
| 172 | } |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | /// @brief Add interference edges between overlapping vregs. |
| 177 | class Interference : public PBQPRAConstraint { |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 178 | private: |
| 179 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 180 | typedef const PBQP::RegAlloc::AllowedRegVector* AllowedRegVecPtr; |
| 181 | typedef std::pair<AllowedRegVecPtr, AllowedRegVecPtr> IMatrixKey; |
| 182 | typedef DenseMap<IMatrixKey, PBQPRAGraph::MatrixPtr> IMatrixCache; |
| 183 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 184 | // Holds (Interval, CurrentSegmentID, and NodeId). The first two are required |
| 185 | // for the fast interference graph construction algorithm. The last is there |
| 186 | // to save us from looking up node ids via the VRegToNode map in the graph |
| 187 | // metadata. |
| 188 | typedef std::tuple<LiveInterval*, size_t, PBQP::GraphBase::NodeId> |
| 189 | IntervalInfo; |
| 190 | |
| 191 | static SlotIndex getStartPoint(const IntervalInfo &I) { |
| 192 | return std::get<0>(I)->segments[std::get<1>(I)].start; |
| 193 | } |
| 194 | |
| 195 | static SlotIndex getEndPoint(const IntervalInfo &I) { |
| 196 | return std::get<0>(I)->segments[std::get<1>(I)].end; |
| 197 | } |
| 198 | |
| 199 | static PBQP::GraphBase::NodeId getNodeId(const IntervalInfo &I) { |
| 200 | return std::get<2>(I); |
| 201 | } |
| 202 | |
| 203 | static bool lowestStartPoint(const IntervalInfo &I1, |
| 204 | const IntervalInfo &I2) { |
| 205 | // Condition reversed because priority queue has the *highest* element at |
| 206 | // the front, rather than the lowest. |
| 207 | return getStartPoint(I1) > getStartPoint(I2); |
| 208 | } |
| 209 | |
| 210 | static bool lowestEndPoint(const IntervalInfo &I1, |
| 211 | const IntervalInfo &I2) { |
| 212 | SlotIndex E1 = getEndPoint(I1); |
| 213 | SlotIndex E2 = getEndPoint(I2); |
| 214 | |
| 215 | if (E1 < E2) |
| 216 | return true; |
| 217 | |
| 218 | if (E1 > E2) |
| 219 | return false; |
| 220 | |
| 221 | // If two intervals end at the same point, we need a way to break the tie or |
| 222 | // the set will assume they're actually equal and refuse to insert a |
| 223 | // "duplicate". Just compare the vregs - fast and guaranteed unique. |
| 224 | return std::get<0>(I1)->reg < std::get<0>(I2)->reg; |
| 225 | } |
| 226 | |
| 227 | static bool isAtLastSegment(const IntervalInfo &I) { |
| 228 | return std::get<1>(I) == std::get<0>(I)->size() - 1; |
| 229 | } |
| 230 | |
| 231 | static IntervalInfo nextSegment(const IntervalInfo &I) { |
| 232 | return std::make_tuple(std::get<0>(I), std::get<1>(I) + 1, std::get<2>(I)); |
| 233 | } |
| 234 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 235 | public: |
| 236 | |
| 237 | void apply(PBQPRAGraph &G) override { |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 238 | // The following is loosely based on the linear scan algorithm introduced in |
| 239 | // "Linear Scan Register Allocation" by Poletto and Sarkar. This version |
| 240 | // isn't linear, because the size of the active set isn't bound by the |
| 241 | // number of registers, but rather the size of the largest clique in the |
| 242 | // graph. Still, we expect this to be better than N^2. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 243 | LiveIntervals &LIS = G.getMetadata().LIS; |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 244 | |
| 245 | // Interferenc matrices are incredibly regular - they're only a function of |
| 246 | // the allowed sets, so we cache them to avoid the overhead of constructing |
| 247 | // and uniquing them. |
| 248 | IMatrixCache C; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 249 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 250 | typedef std::set<IntervalInfo, decltype(&lowestEndPoint)> IntervalSet; |
| 251 | typedef std::priority_queue<IntervalInfo, std::vector<IntervalInfo>, |
| 252 | decltype(&lowestStartPoint)> IntervalQueue; |
| 253 | IntervalSet Active(lowestEndPoint); |
| 254 | IntervalQueue Inactive(lowestStartPoint); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 255 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 256 | // Start by building the inactive set. |
| 257 | for (auto NId : G.nodeIds()) { |
| 258 | unsigned VReg = G.getNodeMetadata(NId).getVReg(); |
| 259 | LiveInterval &LI = LIS.getInterval(VReg); |
| 260 | assert(!LI.empty() && "PBQP graph contains node for empty interval"); |
| 261 | Inactive.push(std::make_tuple(&LI, 0, NId)); |
| 262 | } |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 263 | |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 264 | while (!Inactive.empty()) { |
| 265 | // Tentatively grab the "next" interval - this choice may be overriden |
| 266 | // below. |
| 267 | IntervalInfo Cur = Inactive.top(); |
| 268 | |
| 269 | // Retire any active intervals that end before Cur starts. |
| 270 | IntervalSet::iterator RetireItr = Active.begin(); |
| 271 | while (RetireItr != Active.end() && |
| 272 | (getEndPoint(*RetireItr) <= getStartPoint(Cur))) { |
| 273 | // If this interval has subsequent segments, add the next one to the |
| 274 | // inactive list. |
| 275 | if (!isAtLastSegment(*RetireItr)) |
| 276 | Inactive.push(nextSegment(*RetireItr)); |
| 277 | |
| 278 | ++RetireItr; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 279 | } |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 280 | Active.erase(Active.begin(), RetireItr); |
| 281 | |
| 282 | // One of the newly retired segments may actually start before the |
| 283 | // Cur segment, so re-grab the front of the inactive list. |
| 284 | Cur = Inactive.top(); |
| 285 | Inactive.pop(); |
| 286 | |
| 287 | // At this point we know that Cur overlaps all active intervals. Add the |
| 288 | // interference edges. |
| 289 | PBQP::GraphBase::NodeId NId = getNodeId(Cur); |
| 290 | for (const auto &A : Active) { |
| 291 | PBQP::GraphBase::NodeId MId = getNodeId(A); |
| 292 | |
| 293 | // Check that we haven't already added this edge |
| 294 | // FIXME: findEdge is expensive in the worst case (O(max_clique(G))). |
| 295 | // It might be better to replace this with a local bit-matrix. |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 296 | if (G.findEdge(NId, MId) != PBQPRAGraph::invalidEdgeId()) |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 297 | continue; |
| 298 | |
| 299 | // This is a new edge - add it to the graph. |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 300 | createInterferenceEdge(G, NId, MId, C); |
Lang Hames | ad0962a | 2014-10-18 17:26:07 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // Finally, add Cur to the Active set. |
| 304 | Active.insert(Cur); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | private: |
| 309 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 310 | void createInterferenceEdge(PBQPRAGraph &G, PBQPRAGraph::NodeId NId, |
| 311 | PBQPRAGraph::NodeId MId, IMatrixCache &C) { |
| 312 | |
| 313 | const TargetRegisterInfo &TRI = |
Eric Christopher | 7592b0c | 2015-01-27 08:27:06 +0000 | [diff] [blame] | 314 | *G.getMetadata().MF.getSubtarget().getRegisterInfo(); |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 315 | |
| 316 | const auto &NRegs = G.getNodeMetadata(NId).getAllowedRegs(); |
| 317 | const auto &MRegs = G.getNodeMetadata(MId).getAllowedRegs(); |
| 318 | |
| 319 | // Try looking the edge costs up in the IMatrixCache first. |
| 320 | IMatrixKey K(&NRegs, &MRegs); |
| 321 | IMatrixCache::iterator I = C.find(K); |
| 322 | if (I != C.end()) { |
| 323 | G.addEdgeBypassingCostAllocator(NId, MId, I->second); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | PBQPRAGraph::RawMatrix M(NRegs.size() + 1, MRegs.size() + 1, 0); |
| 328 | for (unsigned I = 0; I != NRegs.size(); ++I) { |
| 329 | unsigned PRegN = NRegs[I]; |
| 330 | for (unsigned J = 0; J != MRegs.size(); ++J) { |
| 331 | unsigned PRegM = MRegs[J]; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 332 | if (TRI.regsOverlap(PRegN, PRegM)) |
| 333 | M[I + 1][J + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity(); |
| 334 | } |
| 335 | } |
| 336 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 337 | PBQPRAGraph::EdgeId EId = G.addEdge(NId, MId, std::move(M)); |
| 338 | C[K] = G.getEdgeCostsPtr(EId); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 339 | } |
| 340 | }; |
| 341 | |
| 342 | |
| 343 | class Coalescing : public PBQPRAConstraint { |
| 344 | public: |
| 345 | void apply(PBQPRAGraph &G) override { |
| 346 | MachineFunction &MF = G.getMetadata().MF; |
| 347 | MachineBlockFrequencyInfo &MBFI = G.getMetadata().MBFI; |
Eric Christopher | 7592b0c | 2015-01-27 08:27:06 +0000 | [diff] [blame] | 348 | CoalescerPair CP(*MF.getSubtarget().getRegisterInfo()); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 349 | |
| 350 | // Scan the machine function and add a coalescing cost whenever CoalescerPair |
| 351 | // gives the Ok. |
| 352 | for (const auto &MBB : MF) { |
| 353 | for (const auto &MI : MBB) { |
| 354 | |
| 355 | // Skip not-coalescable or already coalesced copies. |
| 356 | if (!CP.setRegisters(&MI) || CP.getSrcReg() == CP.getDstReg()) |
| 357 | continue; |
| 358 | |
| 359 | unsigned DstReg = CP.getDstReg(); |
| 360 | unsigned SrcReg = CP.getSrcReg(); |
| 361 | |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 362 | const float Scale = 1.0f / MBFI.getEntryFreq(); |
| 363 | PBQP::PBQPNum CBenefit = MBFI.getBlockFreq(&MBB).getFrequency() * Scale; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 364 | |
| 365 | if (CP.isPhys()) { |
| 366 | if (!MF.getRegInfo().isAllocatable(DstReg)) |
| 367 | continue; |
| 368 | |
| 369 | PBQPRAGraph::NodeId NId = G.getMetadata().getNodeIdForVReg(SrcReg); |
| 370 | |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 371 | const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed = |
| 372 | G.getNodeMetadata(NId).getAllowedRegs(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 373 | |
| 374 | unsigned PRegOpt = 0; |
| 375 | while (PRegOpt < Allowed.size() && Allowed[PRegOpt] != DstReg) |
| 376 | ++PRegOpt; |
| 377 | |
| 378 | if (PRegOpt < Allowed.size()) { |
| 379 | PBQPRAGraph::RawVector NewCosts(G.getNodeCosts(NId)); |
Arnaud A. de Grandmaison | d3648d0 | 2014-10-21 16:24:15 +0000 | [diff] [blame] | 380 | NewCosts[PRegOpt + 1] -= CBenefit; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 381 | G.setNodeCosts(NId, std::move(NewCosts)); |
| 382 | } |
| 383 | } else { |
| 384 | PBQPRAGraph::NodeId N1Id = G.getMetadata().getNodeIdForVReg(DstReg); |
| 385 | PBQPRAGraph::NodeId N2Id = G.getMetadata().getNodeIdForVReg(SrcReg); |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 386 | const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed1 = |
| 387 | &G.getNodeMetadata(N1Id).getAllowedRegs(); |
| 388 | const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed2 = |
| 389 | &G.getNodeMetadata(N2Id).getAllowedRegs(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 390 | |
| 391 | PBQPRAGraph::EdgeId EId = G.findEdge(N1Id, N2Id); |
| 392 | if (EId == G.invalidEdgeId()) { |
| 393 | PBQPRAGraph::RawMatrix Costs(Allowed1->size() + 1, |
| 394 | Allowed2->size() + 1, 0); |
| 395 | addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit); |
| 396 | G.addEdge(N1Id, N2Id, std::move(Costs)); |
| 397 | } else { |
| 398 | if (G.getEdgeNode1Id(EId) == N2Id) { |
| 399 | std::swap(N1Id, N2Id); |
| 400 | std::swap(Allowed1, Allowed2); |
| 401 | } |
| 402 | PBQPRAGraph::RawMatrix Costs(G.getEdgeCosts(EId)); |
| 403 | addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit); |
| 404 | G.setEdgeCosts(EId, std::move(Costs)); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private: |
| 412 | |
| 413 | void addVirtRegCoalesce( |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 414 | PBQPRAGraph::RawMatrix &CostMat, |
| 415 | const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed1, |
| 416 | const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed2, |
| 417 | PBQP::PBQPNum Benefit) { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 418 | assert(CostMat.getRows() == Allowed1.size() + 1 && "Size mismatch."); |
| 419 | assert(CostMat.getCols() == Allowed2.size() + 1 && "Size mismatch."); |
| 420 | for (unsigned I = 0; I != Allowed1.size(); ++I) { |
| 421 | unsigned PReg1 = Allowed1[I]; |
| 422 | for (unsigned J = 0; J != Allowed2.size(); ++J) { |
| 423 | unsigned PReg2 = Allowed2[J]; |
| 424 | if (PReg1 == PReg2) |
Arnaud A. de Grandmaison | d3648d0 | 2014-10-21 16:24:15 +0000 | [diff] [blame] | 425 | CostMat[I + 1][J + 1] -= Benefit; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | }; |
| 431 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 432 | } // End anonymous namespace. |
| 433 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 434 | // Out-of-line destructor/anchor for PBQPRAConstraint. |
| 435 | PBQPRAConstraint::~PBQPRAConstraint() {} |
| 436 | void PBQPRAConstraint::anchor() {} |
| 437 | void PBQPRAConstraintList::anchor() {} |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 438 | |
| 439 | void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const { |
Lang Hames | b13b6a0 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 440 | au.setPreservesCFG(); |
| 441 | au.addRequired<AliasAnalysis>(); |
| 442 | au.addPreserved<AliasAnalysis>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 443 | au.addRequired<SlotIndexes>(); |
| 444 | au.addPreserved<SlotIndexes>(); |
| 445 | au.addRequired<LiveIntervals>(); |
Lang Hames | 8ce99f2 | 2012-10-04 04:50:53 +0000 | [diff] [blame] | 446 | au.addPreserved<LiveIntervals>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 447 | //au.addRequiredID(SplitCriticalEdgesID); |
Lang Hames | 934625e | 2011-06-17 07:09:01 +0000 | [diff] [blame] | 448 | if (customPassID) |
| 449 | au.addRequiredID(*customPassID); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 450 | au.addRequired<LiveStacks>(); |
| 451 | au.addPreserved<LiveStacks>(); |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 452 | au.addRequired<MachineBlockFrequencyInfo>(); |
| 453 | au.addPreserved<MachineBlockFrequencyInfo>(); |
Lang Hames | 7d99d79 | 2013-07-01 20:47:47 +0000 | [diff] [blame] | 454 | au.addRequired<MachineLoopInfo>(); |
| 455 | au.addPreserved<MachineLoopInfo>(); |
Lang Hames | b13b6a0 | 2011-12-06 01:45:57 +0000 | [diff] [blame] | 456 | au.addRequired<MachineDominatorTree>(); |
| 457 | au.addPreserved<MachineDominatorTree>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 458 | au.addRequired<VirtRegMap>(); |
Lang Hames | 8ce99f2 | 2012-10-04 04:50:53 +0000 | [diff] [blame] | 459 | au.addPreserved<VirtRegMap>(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 460 | MachineFunctionPass::getAnalysisUsage(au); |
| 461 | } |
| 462 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 463 | void RegAllocPBQP::findVRegIntervalsToAlloc(const MachineFunction &MF, |
| 464 | LiveIntervals &LIS) { |
| 465 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 466 | |
| 467 | // Iterate over all live ranges. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 468 | for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) { |
| 469 | unsigned Reg = TargetRegisterInfo::index2VirtReg(I); |
| 470 | if (MRI.reg_nodbg_empty(Reg)) |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 471 | continue; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 472 | LiveInterval &LI = LIS.getInterval(Reg); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 473 | |
| 474 | // If this live interval is non-empty we will use pbqp to allocate it. |
| 475 | // Empty intervals we allocate in a simple post-processing stage in |
| 476 | // finalizeAlloc. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 477 | if (!LI.empty()) { |
| 478 | VRegsToAlloc.insert(LI.reg); |
Lang Hames | c702ba6 | 2010-11-12 05:47:21 +0000 | [diff] [blame] | 479 | } else { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 480 | EmptyIntervalVRegs.insert(LI.reg); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Arnaud A. de Grandmaison | a11cab3 | 2014-11-04 20:51:29 +0000 | [diff] [blame] | 485 | static bool isACalleeSavedRegister(unsigned reg, const TargetRegisterInfo &TRI, |
| 486 | const MachineFunction &MF) { |
| 487 | const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF); |
| 488 | for (unsigned i = 0; CSR[i] != 0; ++i) |
| 489 | if (TRI.regsOverlap(reg, CSR[i])) |
| 490 | return true; |
| 491 | return false; |
| 492 | } |
| 493 | |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 494 | void RegAllocPBQP::initializeGraph(PBQPRAGraph &G, VirtRegMap &VRM, |
| 495 | Spiller &VRegSpiller) { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 496 | MachineFunction &MF = G.getMetadata().MF; |
| 497 | |
| 498 | LiveIntervals &LIS = G.getMetadata().LIS; |
| 499 | const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo(); |
| 500 | const TargetRegisterInfo &TRI = |
Eric Christopher | 7592b0c | 2015-01-27 08:27:06 +0000 | [diff] [blame] | 501 | *G.getMetadata().MF.getSubtarget().getRegisterInfo(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 502 | |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 503 | std::vector<unsigned> Worklist(VRegsToAlloc.begin(), VRegsToAlloc.end()); |
| 504 | |
| 505 | while (!Worklist.empty()) { |
| 506 | unsigned VReg = Worklist.back(); |
| 507 | Worklist.pop_back(); |
| 508 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 509 | const TargetRegisterClass *TRC = MRI.getRegClass(VReg); |
| 510 | LiveInterval &VRegLI = LIS.getInterval(VReg); |
| 511 | |
| 512 | // Record any overlaps with regmask operands. |
| 513 | BitVector RegMaskOverlaps; |
| 514 | LIS.checkRegMaskInterference(VRegLI, RegMaskOverlaps); |
| 515 | |
| 516 | // Compute an initial allowed set for the current vreg. |
| 517 | std::vector<unsigned> VRegAllowed; |
| 518 | ArrayRef<MCPhysReg> RawPRegOrder = TRC->getRawAllocationOrder(MF); |
| 519 | for (unsigned I = 0; I != RawPRegOrder.size(); ++I) { |
| 520 | unsigned PReg = RawPRegOrder[I]; |
| 521 | if (MRI.isReserved(PReg)) |
| 522 | continue; |
| 523 | |
| 524 | // vregLI crosses a regmask operand that clobbers preg. |
| 525 | if (!RegMaskOverlaps.empty() && !RegMaskOverlaps.test(PReg)) |
| 526 | continue; |
| 527 | |
| 528 | // vregLI overlaps fixed regunit interference. |
| 529 | bool Interference = false; |
| 530 | for (MCRegUnitIterator Units(PReg, &TRI); Units.isValid(); ++Units) { |
| 531 | if (VRegLI.overlaps(LIS.getRegUnit(*Units))) { |
| 532 | Interference = true; |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | if (Interference) |
| 537 | continue; |
| 538 | |
| 539 | // preg is usable for this virtual register. |
| 540 | VRegAllowed.push_back(PReg); |
| 541 | } |
| 542 | |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 543 | // Check for vregs that have no allowed registers. These should be |
| 544 | // pre-spilled and the new vregs added to the worklist. |
| 545 | if (VRegAllowed.empty()) { |
| 546 | SmallVector<unsigned, 8> NewVRegs; |
| 547 | spillVReg(VReg, NewVRegs, MF, LIS, VRM, VRegSpiller); |
| 548 | for (auto NewVReg : NewVRegs) |
| 549 | Worklist.push_back(NewVReg); |
| 550 | continue; |
| 551 | } |
| 552 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 553 | PBQPRAGraph::RawVector NodeCosts(VRegAllowed.size() + 1, 0); |
Arnaud A. de Grandmaison | a11cab3 | 2014-11-04 20:51:29 +0000 | [diff] [blame] | 554 | |
| 555 | // Tweak cost of callee saved registers, as using then force spilling and |
| 556 | // restoring them. This would only happen in the prologue / epilogue though. |
| 557 | for (unsigned i = 0; i != VRegAllowed.size(); ++i) |
| 558 | if (isACalleeSavedRegister(VRegAllowed[i], TRI, MF)) |
| 559 | NodeCosts[1 + i] += 1.0; |
| 560 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 561 | PBQPRAGraph::NodeId NId = G.addNode(std::move(NodeCosts)); |
| 562 | G.getNodeMetadata(NId).setVReg(VReg); |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 563 | G.getNodeMetadata(NId).setAllowedRegs( |
| 564 | G.getMetadata().getAllowedRegs(std::move(VRegAllowed))); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 565 | G.getMetadata().setNodeIdForVReg(VReg, NId); |
| 566 | } |
| 567 | } |
| 568 | |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 569 | void RegAllocPBQP::spillVReg(unsigned VReg, |
| 570 | SmallVectorImpl<unsigned> &NewIntervals, |
| 571 | MachineFunction &MF, LiveIntervals &LIS, |
| 572 | VirtRegMap &VRM, Spiller &VRegSpiller) { |
| 573 | |
| 574 | VRegsToAlloc.erase(VReg); |
| 575 | LiveRangeEdit LRE(&LIS.getInterval(VReg), NewIntervals, MF, LIS, &VRM); |
| 576 | VRegSpiller.spill(LRE); |
| 577 | |
| 578 | const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); |
| 579 | (void)TRI; |
| 580 | DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> SPILLED (Cost: " |
| 581 | << LRE.getParent().weight << ", New vregs: "); |
| 582 | |
| 583 | // Copy any newly inserted live intervals into the list of regs to |
| 584 | // allocate. |
| 585 | for (LiveRangeEdit::iterator I = LRE.begin(), E = LRE.end(); |
| 586 | I != E; ++I) { |
| 587 | const LiveInterval &LI = LIS.getInterval(*I); |
| 588 | assert(!LI.empty() && "Empty spill range."); |
| 589 | DEBUG(dbgs() << PrintReg(LI.reg, &TRI) << " "); |
| 590 | VRegsToAlloc.insert(LI.reg); |
| 591 | } |
| 592 | |
| 593 | DEBUG(dbgs() << ")\n"); |
| 594 | } |
| 595 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 596 | bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAGraph &G, |
| 597 | const PBQP::Solution &Solution, |
| 598 | VirtRegMap &VRM, |
| 599 | Spiller &VRegSpiller) { |
| 600 | MachineFunction &MF = G.getMetadata().MF; |
| 601 | LiveIntervals &LIS = G.getMetadata().LIS; |
Eric Christopher | 7592b0c | 2015-01-27 08:27:06 +0000 | [diff] [blame] | 602 | const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 603 | (void)TRI; |
| 604 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 605 | // Set to true if we have any spills |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 606 | bool AnotherRoundNeeded = false; |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 607 | |
| 608 | // Clear the existing allocation. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 609 | VRM.clearAllVirt(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 610 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 611 | // Iterate over the nodes mapping the PBQP solution to a register |
| 612 | // assignment. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 613 | for (auto NId : G.nodeIds()) { |
| 614 | unsigned VReg = G.getNodeMetadata(NId).getVReg(); |
| 615 | unsigned AllocOption = Solution.getSelection(NId); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 616 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 617 | if (AllocOption != PBQP::RegAlloc::getSpillOptionIdx()) { |
Lang Hames | 5fe30ca | 2014-10-27 17:44:25 +0000 | [diff] [blame] | 618 | unsigned PReg = G.getNodeMetadata(NId).getAllowedRegs()[AllocOption - 1]; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 619 | DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> " |
| 620 | << TRI.getName(PReg) << "\n"); |
| 621 | assert(PReg != 0 && "Invalid preg selected."); |
| 622 | VRM.assignVirt2Phys(VReg, PReg); |
| 623 | } else { |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 624 | // Spill VReg. If this introduces new intervals we'll need another round |
| 625 | // of allocation. |
| 626 | SmallVector<unsigned, 8> NewVRegs; |
| 627 | spillVReg(VReg, NewVRegs, MF, LIS, VRM, VRegSpiller); |
| 628 | AnotherRoundNeeded |= !NewVRegs.empty(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 632 | return !AnotherRoundNeeded; |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 635 | void RegAllocPBQP::finalizeAlloc(MachineFunction &MF, |
| 636 | LiveIntervals &LIS, |
| 637 | VirtRegMap &VRM) const { |
| 638 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 639 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 640 | // First allocate registers for the empty intervals. |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 641 | for (RegSet::const_iterator |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 642 | I = EmptyIntervalVRegs.begin(), E = EmptyIntervalVRegs.end(); |
| 643 | I != E; ++I) { |
| 644 | LiveInterval &LI = LIS.getInterval(*I); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 645 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 646 | unsigned PReg = MRI.getSimpleHint(LI.reg); |
Lang Hames | 88fae6f | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 647 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 648 | if (PReg == 0) { |
| 649 | const TargetRegisterClass &RC = *MRI.getRegClass(LI.reg); |
| 650 | PReg = RC.getRawAllocationOrder(MF).front(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 651 | } |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 652 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 653 | VRM.assignVirt2Phys(LI.reg, PReg); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 654 | } |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 657 | static inline float normalizePBQPSpillWeight(float UseDefFreq, unsigned Size, |
| 658 | unsigned NumInstr) { |
| 659 | // All intervals have a spill weight that is mostly proportional to the number |
| 660 | // of uses, with uses in loops having a bigger weight. |
| 661 | return NumInstr * normalizeSpillWeight(UseDefFreq, Size, 1); |
| 662 | } |
| 663 | |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 664 | bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 665 | LiveIntervals &LIS = getAnalysis<LiveIntervals>(); |
| 666 | MachineBlockFrequencyInfo &MBFI = |
| 667 | getAnalysis<MachineBlockFrequencyInfo>(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 668 | |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 669 | calculateSpillWeightsAndHints(LIS, MF, getAnalysis<MachineLoopInfo>(), MBFI, |
| 670 | normalizePBQPSpillWeight); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 671 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 672 | VirtRegMap &VRM = getAnalysis<VirtRegMap>(); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 673 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 674 | std::unique_ptr<Spiller> VRegSpiller(createInlineSpiller(*this, MF, VRM)); |
Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 675 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 676 | MF.getRegInfo().freezeReservedRegs(MF); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 677 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 678 | DEBUG(dbgs() << "PBQP Register Allocating for " << MF.getName() << "\n"); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 679 | |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 680 | // Allocator main loop: |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 681 | // |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 682 | // * Map current regalloc problem to a PBQP problem |
| 683 | // * Solve the PBQP problem |
| 684 | // * Map the solution back to a register allocation |
| 685 | // * Spill if necessary |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 686 | // |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 687 | // This process is continued till no more spills are generated. |
| 688 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 689 | // Find the vreg intervals in need of allocation. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 690 | findVRegIntervalsToAlloc(MF, LIS); |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 691 | |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 692 | #ifndef NDEBUG |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 693 | const Function &F = *MF.getFunction(); |
| 694 | std::string FullyQualifiedName = |
| 695 | F.getParent()->getModuleIdentifier() + "." + F.getName().str(); |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 696 | #endif |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 697 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 698 | // If there are non-empty intervals allocate them using pbqp. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 699 | if (!VRegsToAlloc.empty()) { |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 700 | |
Eric Christopher | 7592b0c | 2015-01-27 08:27:06 +0000 | [diff] [blame] | 701 | const TargetSubtargetInfo &Subtarget = MF.getSubtarget(); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 702 | std::unique_ptr<PBQPRAConstraintList> ConstraintsRoot = |
| 703 | llvm::make_unique<PBQPRAConstraintList>(); |
| 704 | ConstraintsRoot->addConstraint(llvm::make_unique<SpillCosts>()); |
| 705 | ConstraintsRoot->addConstraint(llvm::make_unique<Interference>()); |
| 706 | if (PBQPCoalescing) |
| 707 | ConstraintsRoot->addConstraint(llvm::make_unique<Coalescing>()); |
| 708 | ConstraintsRoot->addConstraint(Subtarget.getCustomPBQPConstraints()); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 709 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 710 | bool PBQPAllocComplete = false; |
| 711 | unsigned Round = 0; |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 712 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 713 | while (!PBQPAllocComplete) { |
| 714 | DEBUG(dbgs() << " PBQP Regalloc round " << Round << ":\n"); |
| 715 | |
| 716 | PBQPRAGraph G(PBQPRAGraph::GraphMetadata(MF, LIS, MBFI)); |
Lang Hames | d48bf3f | 2015-02-03 06:14:06 +0000 | [diff] [blame] | 717 | initializeGraph(G, VRM, *VRegSpiller); |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 718 | ConstraintsRoot->apply(G); |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 719 | |
| 720 | #ifndef NDEBUG |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 721 | if (PBQPDumpGraphs) { |
| 722 | std::ostringstream RS; |
| 723 | RS << Round; |
| 724 | std::string GraphFileName = FullyQualifiedName + "." + RS.str() + |
| 725 | ".pbqpgraph"; |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 726 | std::error_code EC; |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 727 | raw_fd_ostream OS(GraphFileName, EC, sys::fs::F_Text); |
| 728 | DEBUG(dbgs() << "Dumping graph for round " << Round << " to \"" |
| 729 | << GraphFileName << "\"\n"); |
Arnaud A. de Grandmaison | 10797c5 | 2015-02-03 23:40:24 +0000 | [diff] [blame^] | 730 | G.dump(OS); |
Lang Hames | 95e021f | 2012-03-26 23:07:23 +0000 | [diff] [blame] | 731 | } |
| 732 | #endif |
| 733 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 734 | PBQP::Solution Solution = PBQP::RegAlloc::solve(G); |
| 735 | PBQPAllocComplete = mapPBQPToRegAlloc(G, Solution, VRM, *VRegSpiller); |
| 736 | ++Round; |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 737 | } |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 740 | // Finalise allocation, allocate empty ranges. |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 741 | finalizeAlloc(MF, LIS, VRM); |
| 742 | VRegsToAlloc.clear(); |
| 743 | EmptyIntervalVRegs.clear(); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 744 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 745 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << VRM << "\n"); |
Lang Hames | 49ab8bc | 2008-11-16 12:12:54 +0000 | [diff] [blame] | 746 | |
Misha Brukman | da46748 | 2009-01-08 15:50:22 +0000 | [diff] [blame] | 747 | return true; |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Arnaud A. de Grandmaison | 10797c5 | 2015-02-03 23:40:24 +0000 | [diff] [blame^] | 750 | namespace { |
| 751 | // A Helper class for print node and register info in a consistent way |
| 752 | class PrintNodeInfo { |
| 753 | public: |
| 754 | typedef PBQP::RegAlloc::PBQPRAGraph Graph; |
| 755 | typedef PBQP::RegAlloc::PBQPRAGraph::NodeId NodeId; |
| 756 | |
| 757 | PrintNodeInfo(NodeId NId, const Graph &G) : G(G), NId(NId) {} |
| 758 | |
| 759 | void print(raw_ostream &OS) const { |
| 760 | const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo(); |
| 761 | const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo(); |
| 762 | unsigned VReg = G.getNodeMetadata(NId).getVReg(); |
| 763 | const char *RegClassName = TRI->getRegClassName(MRI.getRegClass(VReg)); |
| 764 | OS << NId << " (" << RegClassName << ':' << PrintReg(VReg, TRI) << ')'; |
| 765 | } |
| 766 | |
| 767 | private: |
| 768 | const Graph &G; |
| 769 | NodeId NId; |
| 770 | }; |
| 771 | |
| 772 | inline raw_ostream &operator<<(raw_ostream &OS, const PrintNodeInfo &PR) { |
| 773 | PR.print(OS); |
| 774 | return OS; |
| 775 | } |
| 776 | } // anonymous namespace |
| 777 | |
| 778 | void PBQP::RegAlloc::PBQPRAGraph::dump(raw_ostream &OS) const { |
| 779 | for (auto NId : nodeIds()) { |
| 780 | const Vector &Costs = getNodeCosts(NId); |
| 781 | assert(Costs.getLength() != 0 && "Empty vector in graph."); |
| 782 | OS << PrintNodeInfo(NId, *this) << ": " << Costs << '\n'; |
| 783 | } |
| 784 | OS << '\n'; |
| 785 | |
| 786 | for (auto EId : edgeIds()) { |
| 787 | NodeId N1Id = getEdgeNode1Id(EId); |
| 788 | NodeId N2Id = getEdgeNode2Id(EId); |
| 789 | assert(N1Id != N2Id && "PBQP graphs should not have self-edges."); |
| 790 | const Matrix &M = getEdgeCosts(EId); |
| 791 | assert(M.getRows() != 0 && "No rows in matrix."); |
| 792 | assert(M.getCols() != 0 && "No cols in matrix."); |
| 793 | OS << PrintNodeInfo(N1Id, *this) << ' ' << M.getRows() << " rows / "; |
| 794 | OS << PrintNodeInfo(N2Id, *this) << ' ' << M.getCols() << " cols:\n"; |
| 795 | OS << M << '\n'; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | void PBQP::RegAlloc::PBQPRAGraph::dump() const { dump(dbgs()); } |
| 800 | |
| 801 | void PBQP::RegAlloc::PBQPRAGraph::printDot(raw_ostream &OS) const { |
| 802 | OS << "graph {\n"; |
| 803 | for (auto NId : nodeIds()) { |
| 804 | OS << " node" << NId << " [ label=\"" |
| 805 | << PrintNodeInfo(NId, *this) << "\\n" |
| 806 | << getNodeCosts(NId) << "\" ]\n"; |
| 807 | } |
| 808 | |
| 809 | OS << " edge [ len=" << nodeIds().size() << " ]\n"; |
| 810 | for (auto EId : edgeIds()) { |
| 811 | OS << " node" << getEdgeNode1Id(EId) |
| 812 | << " -- node" << getEdgeNode2Id(EId) |
| 813 | << " [ label=\""; |
| 814 | const Matrix &EdgeCosts = getEdgeCosts(EId); |
| 815 | for (unsigned i = 0; i < EdgeCosts.getRows(); ++i) { |
| 816 | OS << EdgeCosts.getRowAsVector(i) << "\\n"; |
| 817 | } |
| 818 | OS << "\" ]\n"; |
| 819 | } |
| 820 | OS << "}\n"; |
| 821 | } |
| 822 | |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 823 | FunctionPass *llvm::createPBQPRegisterAllocator(char *customPassID) { |
| 824 | return new RegAllocPBQP(customPassID); |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Lang Hames | fd1bc42 | 2010-09-23 04:28:54 +0000 | [diff] [blame] | 827 | FunctionPass* llvm::createDefaultPBQPRegisterAllocator() { |
Lang Hames | 8f31f44 | 2014-10-09 18:20:51 +0000 | [diff] [blame] | 828 | return createPBQPRegisterAllocator(); |
Lang Hames | cb1e101 | 2010-09-18 09:07:10 +0000 | [diff] [blame] | 829 | } |
Evan Cheng | b25f463 | 2008-10-02 18:29:27 +0000 | [diff] [blame] | 830 | |
| 831 | #undef DEBUG_TYPE |