1*9880d681SAndroid Build Coastguard Worker //===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains a Partitioned Boolean Quadratic Programming (PBQP) based
11*9880d681SAndroid Build Coastguard Worker // register allocator for LLVM. This allocator works by constructing a PBQP
12*9880d681SAndroid Build Coastguard Worker // problem representing the register allocation problem under consideration,
13*9880d681SAndroid Build Coastguard Worker // solving this using a PBQP solver, and mapping the solution back to a
14*9880d681SAndroid Build Coastguard Worker // register assignment. If any variables are selected for spilling then spill
15*9880d681SAndroid Build Coastguard Worker // code is inserted and the process repeated.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned
18*9880d681SAndroid Build Coastguard Worker // for register allocation. For more information on PBQP for register
19*9880d681SAndroid Build Coastguard Worker // allocation, see the following papers:
20*9880d681SAndroid Build Coastguard Worker //
21*9880d681SAndroid Build Coastguard Worker // (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with
22*9880d681SAndroid Build Coastguard Worker // PBQP. In Proceedings of the 7th Joint Modular Languages Conference
23*9880d681SAndroid Build Coastguard Worker // (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361.
24*9880d681SAndroid Build Coastguard Worker //
25*9880d681SAndroid Build Coastguard Worker // (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular
26*9880d681SAndroid Build Coastguard Worker // architectures. In Proceedings of the Joint Conference on Languages,
27*9880d681SAndroid Build Coastguard Worker // Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York,
28*9880d681SAndroid Build Coastguard Worker // NY, USA, 139-148.
29*9880d681SAndroid Build Coastguard Worker //
30*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegAllocPBQP.h"
33*9880d681SAndroid Build Coastguard Worker #include "RegisterCoalescer.h"
34*9880d681SAndroid Build Coastguard Worker #include "Spiller.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/CalcSpillWeights.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveIntervalAnalysis.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveRangeEdit.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveStackAnalysis.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegAllocRegistry.h"
46*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/VirtRegMap.h"
47*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
48*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
49*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
50*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Printable.h"
51*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
52*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
53*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
54*9880d681SAndroid Build Coastguard Worker #include <limits>
55*9880d681SAndroid Build Coastguard Worker #include <memory>
56*9880d681SAndroid Build Coastguard Worker #include <queue>
57*9880d681SAndroid Build Coastguard Worker #include <set>
58*9880d681SAndroid Build Coastguard Worker #include <sstream>
59*9880d681SAndroid Build Coastguard Worker #include <vector>
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker using namespace llvm;
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "regalloc"
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker static RegisterRegAlloc
66*9880d681SAndroid Build Coastguard Worker RegisterPBQPRepAlloc("pbqp", "PBQP register allocator",
67*9880d681SAndroid Build Coastguard Worker createDefaultPBQPRegisterAllocator);
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
70*9880d681SAndroid Build Coastguard Worker PBQPCoalescing("pbqp-coalescing",
71*9880d681SAndroid Build Coastguard Worker cl::desc("Attempt coalescing during PBQP register allocation."),
72*9880d681SAndroid Build Coastguard Worker cl::init(false), cl::Hidden);
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
75*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
76*9880d681SAndroid Build Coastguard Worker PBQPDumpGraphs("pbqp-dump-graphs",
77*9880d681SAndroid Build Coastguard Worker cl::desc("Dump graphs for each function/round in the compilation unit."),
78*9880d681SAndroid Build Coastguard Worker cl::init(false), cl::Hidden);
79*9880d681SAndroid Build Coastguard Worker #endif
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker namespace {
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker ///
84*9880d681SAndroid Build Coastguard Worker /// PBQP based allocators solve the register allocation problem by mapping
85*9880d681SAndroid Build Coastguard Worker /// register allocation problems to Partitioned Boolean Quadratic
86*9880d681SAndroid Build Coastguard Worker /// Programming problems.
87*9880d681SAndroid Build Coastguard Worker class RegAllocPBQP : public MachineFunctionPass {
88*9880d681SAndroid Build Coastguard Worker public:
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker static char ID;
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker /// Construct a PBQP register allocator.
RegAllocPBQP(char * cPassID=nullptr)93*9880d681SAndroid Build Coastguard Worker RegAllocPBQP(char *cPassID = nullptr)
94*9880d681SAndroid Build Coastguard Worker : MachineFunctionPass(ID), customPassID(cPassID) {
95*9880d681SAndroid Build Coastguard Worker initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
96*9880d681SAndroid Build Coastguard Worker initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
97*9880d681SAndroid Build Coastguard Worker initializeLiveStacksPass(*PassRegistry::getPassRegistry());
98*9880d681SAndroid Build Coastguard Worker initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker /// Return the pass name.
getPassName() const102*9880d681SAndroid Build Coastguard Worker const char* getPassName() const override {
103*9880d681SAndroid Build Coastguard Worker return "PBQP Register Allocator";
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker /// PBQP analysis usage.
107*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &au) const override;
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker /// Perform register allocation
110*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker private:
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
115*9880d681SAndroid Build Coastguard Worker typedef std::vector<const LiveInterval*> Node2LIMap;
116*9880d681SAndroid Build Coastguard Worker typedef std::vector<unsigned> AllowedSet;
117*9880d681SAndroid Build Coastguard Worker typedef std::vector<AllowedSet> AllowedSetMap;
118*9880d681SAndroid Build Coastguard Worker typedef std::pair<unsigned, unsigned> RegPair;
119*9880d681SAndroid Build Coastguard Worker typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
120*9880d681SAndroid Build Coastguard Worker typedef std::set<unsigned> RegSet;
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker char *customPassID;
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker RegSet VRegsToAlloc, EmptyIntervalVRegs;
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker /// Inst which is a def of an original reg and whose defs are already all
127*9880d681SAndroid Build Coastguard Worker /// dead after remat is saved in DeadRemats. The deletion of such inst is
128*9880d681SAndroid Build Coastguard Worker /// postponed till all the allocations are done, so its remat expr is
129*9880d681SAndroid Build Coastguard Worker /// always available for the remat of all the siblings of the original reg.
130*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr *, 32> DeadRemats;
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker /// \brief Finds the initial set of vreg intervals to allocate.
133*9880d681SAndroid Build Coastguard Worker void findVRegIntervalsToAlloc(const MachineFunction &MF, LiveIntervals &LIS);
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker /// \brief Constructs an initial graph.
136*9880d681SAndroid Build Coastguard Worker void initializeGraph(PBQPRAGraph &G, VirtRegMap &VRM, Spiller &VRegSpiller);
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker /// \brief Spill the given VReg.
139*9880d681SAndroid Build Coastguard Worker void spillVReg(unsigned VReg, SmallVectorImpl<unsigned> &NewIntervals,
140*9880d681SAndroid Build Coastguard Worker MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM,
141*9880d681SAndroid Build Coastguard Worker Spiller &VRegSpiller);
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker /// \brief Given a solved PBQP problem maps this solution back to a register
144*9880d681SAndroid Build Coastguard Worker /// assignment.
145*9880d681SAndroid Build Coastguard Worker bool mapPBQPToRegAlloc(const PBQPRAGraph &G,
146*9880d681SAndroid Build Coastguard Worker const PBQP::Solution &Solution,
147*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM,
148*9880d681SAndroid Build Coastguard Worker Spiller &VRegSpiller);
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker /// \brief Postprocessing before final spilling. Sets basic block "live in"
151*9880d681SAndroid Build Coastguard Worker /// variables.
152*9880d681SAndroid Build Coastguard Worker void finalizeAlloc(MachineFunction &MF, LiveIntervals &LIS,
153*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM) const;
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker void postOptimization(Spiller &VRegSpiller, LiveIntervals &LIS);
156*9880d681SAndroid Build Coastguard Worker };
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker char RegAllocPBQP::ID = 0;
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker /// @brief Set spill costs for each node in the PBQP reg-alloc graph.
161*9880d681SAndroid Build Coastguard Worker class SpillCosts : public PBQPRAConstraint {
162*9880d681SAndroid Build Coastguard Worker public:
apply(PBQPRAGraph & G)163*9880d681SAndroid Build Coastguard Worker void apply(PBQPRAGraph &G) override {
164*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS = G.getMetadata().LIS;
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker // A minimum spill costs, so that register constraints can can be set
167*9880d681SAndroid Build Coastguard Worker // without normalization in the [0.0:MinSpillCost( interval.
168*9880d681SAndroid Build Coastguard Worker const PBQP::PBQPNum MinSpillCost = 10.0;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker for (auto NId : G.nodeIds()) {
171*9880d681SAndroid Build Coastguard Worker PBQP::PBQPNum SpillCost =
172*9880d681SAndroid Build Coastguard Worker LIS.getInterval(G.getNodeMetadata(NId).getVReg()).weight;
173*9880d681SAndroid Build Coastguard Worker if (SpillCost == 0.0)
174*9880d681SAndroid Build Coastguard Worker SpillCost = std::numeric_limits<PBQP::PBQPNum>::min();
175*9880d681SAndroid Build Coastguard Worker else
176*9880d681SAndroid Build Coastguard Worker SpillCost += MinSpillCost;
177*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawVector NodeCosts(G.getNodeCosts(NId));
178*9880d681SAndroid Build Coastguard Worker NodeCosts[PBQP::RegAlloc::getSpillOptionIdx()] = SpillCost;
179*9880d681SAndroid Build Coastguard Worker G.setNodeCosts(NId, std::move(NodeCosts));
180*9880d681SAndroid Build Coastguard Worker }
181*9880d681SAndroid Build Coastguard Worker }
182*9880d681SAndroid Build Coastguard Worker };
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker /// @brief Add interference edges between overlapping vregs.
185*9880d681SAndroid Build Coastguard Worker class Interference : public PBQPRAConstraint {
186*9880d681SAndroid Build Coastguard Worker private:
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker typedef const PBQP::RegAlloc::AllowedRegVector* AllowedRegVecPtr;
189*9880d681SAndroid Build Coastguard Worker typedef std::pair<AllowedRegVecPtr, AllowedRegVecPtr> IKey;
190*9880d681SAndroid Build Coastguard Worker typedef DenseMap<IKey, PBQPRAGraph::MatrixPtr> IMatrixCache;
191*9880d681SAndroid Build Coastguard Worker typedef DenseSet<IKey> DisjointAllowedRegsCache;
192*9880d681SAndroid Build Coastguard Worker typedef std::pair<PBQP::GraphBase::NodeId, PBQP::GraphBase::NodeId> IEdgeKey;
193*9880d681SAndroid Build Coastguard Worker typedef DenseSet<IEdgeKey> IEdgeCache;
194*9880d681SAndroid Build Coastguard Worker
haveDisjointAllowedRegs(const PBQPRAGraph & G,PBQPRAGraph::NodeId NId,PBQPRAGraph::NodeId MId,const DisjointAllowedRegsCache & D) const195*9880d681SAndroid Build Coastguard Worker bool haveDisjointAllowedRegs(const PBQPRAGraph &G, PBQPRAGraph::NodeId NId,
196*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId MId,
197*9880d681SAndroid Build Coastguard Worker const DisjointAllowedRegsCache &D) const {
198*9880d681SAndroid Build Coastguard Worker const auto *NRegs = &G.getNodeMetadata(NId).getAllowedRegs();
199*9880d681SAndroid Build Coastguard Worker const auto *MRegs = &G.getNodeMetadata(MId).getAllowedRegs();
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker if (NRegs == MRegs)
202*9880d681SAndroid Build Coastguard Worker return false;
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker if (NRegs < MRegs)
205*9880d681SAndroid Build Coastguard Worker return D.count(IKey(NRegs, MRegs)) > 0;
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker return D.count(IKey(MRegs, NRegs)) > 0;
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker
setDisjointAllowedRegs(const PBQPRAGraph & G,PBQPRAGraph::NodeId NId,PBQPRAGraph::NodeId MId,DisjointAllowedRegsCache & D)210*9880d681SAndroid Build Coastguard Worker void setDisjointAllowedRegs(const PBQPRAGraph &G, PBQPRAGraph::NodeId NId,
211*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId MId,
212*9880d681SAndroid Build Coastguard Worker DisjointAllowedRegsCache &D) {
213*9880d681SAndroid Build Coastguard Worker const auto *NRegs = &G.getNodeMetadata(NId).getAllowedRegs();
214*9880d681SAndroid Build Coastguard Worker const auto *MRegs = &G.getNodeMetadata(MId).getAllowedRegs();
215*9880d681SAndroid Build Coastguard Worker
216*9880d681SAndroid Build Coastguard Worker assert(NRegs != MRegs && "AllowedRegs can not be disjoint with itself");
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker if (NRegs < MRegs)
219*9880d681SAndroid Build Coastguard Worker D.insert(IKey(NRegs, MRegs));
220*9880d681SAndroid Build Coastguard Worker else
221*9880d681SAndroid Build Coastguard Worker D.insert(IKey(MRegs, NRegs));
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker // Holds (Interval, CurrentSegmentID, and NodeId). The first two are required
225*9880d681SAndroid Build Coastguard Worker // for the fast interference graph construction algorithm. The last is there
226*9880d681SAndroid Build Coastguard Worker // to save us from looking up node ids via the VRegToNode map in the graph
227*9880d681SAndroid Build Coastguard Worker // metadata.
228*9880d681SAndroid Build Coastguard Worker typedef std::tuple<LiveInterval*, size_t, PBQP::GraphBase::NodeId>
229*9880d681SAndroid Build Coastguard Worker IntervalInfo;
230*9880d681SAndroid Build Coastguard Worker
getStartPoint(const IntervalInfo & I)231*9880d681SAndroid Build Coastguard Worker static SlotIndex getStartPoint(const IntervalInfo &I) {
232*9880d681SAndroid Build Coastguard Worker return std::get<0>(I)->segments[std::get<1>(I)].start;
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker
getEndPoint(const IntervalInfo & I)235*9880d681SAndroid Build Coastguard Worker static SlotIndex getEndPoint(const IntervalInfo &I) {
236*9880d681SAndroid Build Coastguard Worker return std::get<0>(I)->segments[std::get<1>(I)].end;
237*9880d681SAndroid Build Coastguard Worker }
238*9880d681SAndroid Build Coastguard Worker
getNodeId(const IntervalInfo & I)239*9880d681SAndroid Build Coastguard Worker static PBQP::GraphBase::NodeId getNodeId(const IntervalInfo &I) {
240*9880d681SAndroid Build Coastguard Worker return std::get<2>(I);
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker
lowestStartPoint(const IntervalInfo & I1,const IntervalInfo & I2)243*9880d681SAndroid Build Coastguard Worker static bool lowestStartPoint(const IntervalInfo &I1,
244*9880d681SAndroid Build Coastguard Worker const IntervalInfo &I2) {
245*9880d681SAndroid Build Coastguard Worker // Condition reversed because priority queue has the *highest* element at
246*9880d681SAndroid Build Coastguard Worker // the front, rather than the lowest.
247*9880d681SAndroid Build Coastguard Worker return getStartPoint(I1) > getStartPoint(I2);
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker
lowestEndPoint(const IntervalInfo & I1,const IntervalInfo & I2)250*9880d681SAndroid Build Coastguard Worker static bool lowestEndPoint(const IntervalInfo &I1,
251*9880d681SAndroid Build Coastguard Worker const IntervalInfo &I2) {
252*9880d681SAndroid Build Coastguard Worker SlotIndex E1 = getEndPoint(I1);
253*9880d681SAndroid Build Coastguard Worker SlotIndex E2 = getEndPoint(I2);
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker if (E1 < E2)
256*9880d681SAndroid Build Coastguard Worker return true;
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker if (E1 > E2)
259*9880d681SAndroid Build Coastguard Worker return false;
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard Worker // If two intervals end at the same point, we need a way to break the tie or
262*9880d681SAndroid Build Coastguard Worker // the set will assume they're actually equal and refuse to insert a
263*9880d681SAndroid Build Coastguard Worker // "duplicate". Just compare the vregs - fast and guaranteed unique.
264*9880d681SAndroid Build Coastguard Worker return std::get<0>(I1)->reg < std::get<0>(I2)->reg;
265*9880d681SAndroid Build Coastguard Worker }
266*9880d681SAndroid Build Coastguard Worker
isAtLastSegment(const IntervalInfo & I)267*9880d681SAndroid Build Coastguard Worker static bool isAtLastSegment(const IntervalInfo &I) {
268*9880d681SAndroid Build Coastguard Worker return std::get<1>(I) == std::get<0>(I)->size() - 1;
269*9880d681SAndroid Build Coastguard Worker }
270*9880d681SAndroid Build Coastguard Worker
nextSegment(const IntervalInfo & I)271*9880d681SAndroid Build Coastguard Worker static IntervalInfo nextSegment(const IntervalInfo &I) {
272*9880d681SAndroid Build Coastguard Worker return std::make_tuple(std::get<0>(I), std::get<1>(I) + 1, std::get<2>(I));
273*9880d681SAndroid Build Coastguard Worker }
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker public:
276*9880d681SAndroid Build Coastguard Worker
apply(PBQPRAGraph & G)277*9880d681SAndroid Build Coastguard Worker void apply(PBQPRAGraph &G) override {
278*9880d681SAndroid Build Coastguard Worker // The following is loosely based on the linear scan algorithm introduced in
279*9880d681SAndroid Build Coastguard Worker // "Linear Scan Register Allocation" by Poletto and Sarkar. This version
280*9880d681SAndroid Build Coastguard Worker // isn't linear, because the size of the active set isn't bound by the
281*9880d681SAndroid Build Coastguard Worker // number of registers, but rather the size of the largest clique in the
282*9880d681SAndroid Build Coastguard Worker // graph. Still, we expect this to be better than N^2.
283*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS = G.getMetadata().LIS;
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker // Interferenc matrices are incredibly regular - they're only a function of
286*9880d681SAndroid Build Coastguard Worker // the allowed sets, so we cache them to avoid the overhead of constructing
287*9880d681SAndroid Build Coastguard Worker // and uniquing them.
288*9880d681SAndroid Build Coastguard Worker IMatrixCache C;
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker // Finding an edge is expensive in the worst case (O(max_clique(G))). So
291*9880d681SAndroid Build Coastguard Worker // cache locally edges we have already seen.
292*9880d681SAndroid Build Coastguard Worker IEdgeCache EC;
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker // Cache known disjoint allowed registers pairs
295*9880d681SAndroid Build Coastguard Worker DisjointAllowedRegsCache D;
296*9880d681SAndroid Build Coastguard Worker
297*9880d681SAndroid Build Coastguard Worker typedef std::set<IntervalInfo, decltype(&lowestEndPoint)> IntervalSet;
298*9880d681SAndroid Build Coastguard Worker typedef std::priority_queue<IntervalInfo, std::vector<IntervalInfo>,
299*9880d681SAndroid Build Coastguard Worker decltype(&lowestStartPoint)> IntervalQueue;
300*9880d681SAndroid Build Coastguard Worker IntervalSet Active(lowestEndPoint);
301*9880d681SAndroid Build Coastguard Worker IntervalQueue Inactive(lowestStartPoint);
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker // Start by building the inactive set.
304*9880d681SAndroid Build Coastguard Worker for (auto NId : G.nodeIds()) {
305*9880d681SAndroid Build Coastguard Worker unsigned VReg = G.getNodeMetadata(NId).getVReg();
306*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(VReg);
307*9880d681SAndroid Build Coastguard Worker assert(!LI.empty() && "PBQP graph contains node for empty interval");
308*9880d681SAndroid Build Coastguard Worker Inactive.push(std::make_tuple(&LI, 0, NId));
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard Worker while (!Inactive.empty()) {
312*9880d681SAndroid Build Coastguard Worker // Tentatively grab the "next" interval - this choice may be overriden
313*9880d681SAndroid Build Coastguard Worker // below.
314*9880d681SAndroid Build Coastguard Worker IntervalInfo Cur = Inactive.top();
315*9880d681SAndroid Build Coastguard Worker
316*9880d681SAndroid Build Coastguard Worker // Retire any active intervals that end before Cur starts.
317*9880d681SAndroid Build Coastguard Worker IntervalSet::iterator RetireItr = Active.begin();
318*9880d681SAndroid Build Coastguard Worker while (RetireItr != Active.end() &&
319*9880d681SAndroid Build Coastguard Worker (getEndPoint(*RetireItr) <= getStartPoint(Cur))) {
320*9880d681SAndroid Build Coastguard Worker // If this interval has subsequent segments, add the next one to the
321*9880d681SAndroid Build Coastguard Worker // inactive list.
322*9880d681SAndroid Build Coastguard Worker if (!isAtLastSegment(*RetireItr))
323*9880d681SAndroid Build Coastguard Worker Inactive.push(nextSegment(*RetireItr));
324*9880d681SAndroid Build Coastguard Worker
325*9880d681SAndroid Build Coastguard Worker ++RetireItr;
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker Active.erase(Active.begin(), RetireItr);
328*9880d681SAndroid Build Coastguard Worker
329*9880d681SAndroid Build Coastguard Worker // One of the newly retired segments may actually start before the
330*9880d681SAndroid Build Coastguard Worker // Cur segment, so re-grab the front of the inactive list.
331*9880d681SAndroid Build Coastguard Worker Cur = Inactive.top();
332*9880d681SAndroid Build Coastguard Worker Inactive.pop();
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard Worker // At this point we know that Cur overlaps all active intervals. Add the
335*9880d681SAndroid Build Coastguard Worker // interference edges.
336*9880d681SAndroid Build Coastguard Worker PBQP::GraphBase::NodeId NId = getNodeId(Cur);
337*9880d681SAndroid Build Coastguard Worker for (const auto &A : Active) {
338*9880d681SAndroid Build Coastguard Worker PBQP::GraphBase::NodeId MId = getNodeId(A);
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker // Do not add an edge when the nodes' allowed registers do not
341*9880d681SAndroid Build Coastguard Worker // intersect: there is obviously no interference.
342*9880d681SAndroid Build Coastguard Worker if (haveDisjointAllowedRegs(G, NId, MId, D))
343*9880d681SAndroid Build Coastguard Worker continue;
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker // Check that we haven't already added this edge
346*9880d681SAndroid Build Coastguard Worker IEdgeKey EK(std::min(NId, MId), std::max(NId, MId));
347*9880d681SAndroid Build Coastguard Worker if (EC.count(EK))
348*9880d681SAndroid Build Coastguard Worker continue;
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker // This is a new edge - add it to the graph.
351*9880d681SAndroid Build Coastguard Worker if (!createInterferenceEdge(G, NId, MId, C))
352*9880d681SAndroid Build Coastguard Worker setDisjointAllowedRegs(G, NId, MId, D);
353*9880d681SAndroid Build Coastguard Worker else
354*9880d681SAndroid Build Coastguard Worker EC.insert(EK);
355*9880d681SAndroid Build Coastguard Worker }
356*9880d681SAndroid Build Coastguard Worker
357*9880d681SAndroid Build Coastguard Worker // Finally, add Cur to the Active set.
358*9880d681SAndroid Build Coastguard Worker Active.insert(Cur);
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker private:
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker // Create an Interference edge and add it to the graph, unless it is
365*9880d681SAndroid Build Coastguard Worker // a null matrix, meaning the nodes' allowed registers do not have any
366*9880d681SAndroid Build Coastguard Worker // interference. This case occurs frequently between integer and floating
367*9880d681SAndroid Build Coastguard Worker // point registers for example.
368*9880d681SAndroid Build Coastguard Worker // return true iff both nodes interferes.
createInterferenceEdge(PBQPRAGraph & G,PBQPRAGraph::NodeId NId,PBQPRAGraph::NodeId MId,IMatrixCache & C)369*9880d681SAndroid Build Coastguard Worker bool createInterferenceEdge(PBQPRAGraph &G,
370*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId NId, PBQPRAGraph::NodeId MId,
371*9880d681SAndroid Build Coastguard Worker IMatrixCache &C) {
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI =
374*9880d681SAndroid Build Coastguard Worker *G.getMetadata().MF.getSubtarget().getRegisterInfo();
375*9880d681SAndroid Build Coastguard Worker const auto &NRegs = G.getNodeMetadata(NId).getAllowedRegs();
376*9880d681SAndroid Build Coastguard Worker const auto &MRegs = G.getNodeMetadata(MId).getAllowedRegs();
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker // Try looking the edge costs up in the IMatrixCache first.
379*9880d681SAndroid Build Coastguard Worker IKey K(&NRegs, &MRegs);
380*9880d681SAndroid Build Coastguard Worker IMatrixCache::iterator I = C.find(K);
381*9880d681SAndroid Build Coastguard Worker if (I != C.end()) {
382*9880d681SAndroid Build Coastguard Worker G.addEdgeBypassingCostAllocator(NId, MId, I->second);
383*9880d681SAndroid Build Coastguard Worker return true;
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawMatrix M(NRegs.size() + 1, MRegs.size() + 1, 0);
387*9880d681SAndroid Build Coastguard Worker bool NodesInterfere = false;
388*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I != NRegs.size(); ++I) {
389*9880d681SAndroid Build Coastguard Worker unsigned PRegN = NRegs[I];
390*9880d681SAndroid Build Coastguard Worker for (unsigned J = 0; J != MRegs.size(); ++J) {
391*9880d681SAndroid Build Coastguard Worker unsigned PRegM = MRegs[J];
392*9880d681SAndroid Build Coastguard Worker if (TRI.regsOverlap(PRegN, PRegM)) {
393*9880d681SAndroid Build Coastguard Worker M[I + 1][J + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
394*9880d681SAndroid Build Coastguard Worker NodesInterfere = true;
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker }
397*9880d681SAndroid Build Coastguard Worker }
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker if (!NodesInterfere)
400*9880d681SAndroid Build Coastguard Worker return false;
401*9880d681SAndroid Build Coastguard Worker
402*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::EdgeId EId = G.addEdge(NId, MId, std::move(M));
403*9880d681SAndroid Build Coastguard Worker C[K] = G.getEdgeCostsPtr(EId);
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker return true;
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker };
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker
410*9880d681SAndroid Build Coastguard Worker class Coalescing : public PBQPRAConstraint {
411*9880d681SAndroid Build Coastguard Worker public:
apply(PBQPRAGraph & G)412*9880d681SAndroid Build Coastguard Worker void apply(PBQPRAGraph &G) override {
413*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = G.getMetadata().MF;
414*9880d681SAndroid Build Coastguard Worker MachineBlockFrequencyInfo &MBFI = G.getMetadata().MBFI;
415*9880d681SAndroid Build Coastguard Worker CoalescerPair CP(*MF.getSubtarget().getRegisterInfo());
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker // Scan the machine function and add a coalescing cost whenever CoalescerPair
418*9880d681SAndroid Build Coastguard Worker // gives the Ok.
419*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : MF) {
420*9880d681SAndroid Build Coastguard Worker for (const auto &MI : MBB) {
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker // Skip not-coalescable or already coalesced copies.
423*9880d681SAndroid Build Coastguard Worker if (!CP.setRegisters(&MI) || CP.getSrcReg() == CP.getDstReg())
424*9880d681SAndroid Build Coastguard Worker continue;
425*9880d681SAndroid Build Coastguard Worker
426*9880d681SAndroid Build Coastguard Worker unsigned DstReg = CP.getDstReg();
427*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = CP.getSrcReg();
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard Worker const float Scale = 1.0f / MBFI.getEntryFreq();
430*9880d681SAndroid Build Coastguard Worker PBQP::PBQPNum CBenefit = MBFI.getBlockFreq(&MBB).getFrequency() * Scale;
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker if (CP.isPhys()) {
433*9880d681SAndroid Build Coastguard Worker if (!MF.getRegInfo().isAllocatable(DstReg))
434*9880d681SAndroid Build Coastguard Worker continue;
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId NId = G.getMetadata().getNodeIdForVReg(SrcReg);
437*9880d681SAndroid Build Coastguard Worker
438*9880d681SAndroid Build Coastguard Worker const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed =
439*9880d681SAndroid Build Coastguard Worker G.getNodeMetadata(NId).getAllowedRegs();
440*9880d681SAndroid Build Coastguard Worker
441*9880d681SAndroid Build Coastguard Worker unsigned PRegOpt = 0;
442*9880d681SAndroid Build Coastguard Worker while (PRegOpt < Allowed.size() && Allowed[PRegOpt] != DstReg)
443*9880d681SAndroid Build Coastguard Worker ++PRegOpt;
444*9880d681SAndroid Build Coastguard Worker
445*9880d681SAndroid Build Coastguard Worker if (PRegOpt < Allowed.size()) {
446*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawVector NewCosts(G.getNodeCosts(NId));
447*9880d681SAndroid Build Coastguard Worker NewCosts[PRegOpt + 1] -= CBenefit;
448*9880d681SAndroid Build Coastguard Worker G.setNodeCosts(NId, std::move(NewCosts));
449*9880d681SAndroid Build Coastguard Worker }
450*9880d681SAndroid Build Coastguard Worker } else {
451*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId N1Id = G.getMetadata().getNodeIdForVReg(DstReg);
452*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId N2Id = G.getMetadata().getNodeIdForVReg(SrcReg);
453*9880d681SAndroid Build Coastguard Worker const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed1 =
454*9880d681SAndroid Build Coastguard Worker &G.getNodeMetadata(N1Id).getAllowedRegs();
455*9880d681SAndroid Build Coastguard Worker const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed2 =
456*9880d681SAndroid Build Coastguard Worker &G.getNodeMetadata(N2Id).getAllowedRegs();
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::EdgeId EId = G.findEdge(N1Id, N2Id);
459*9880d681SAndroid Build Coastguard Worker if (EId == G.invalidEdgeId()) {
460*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawMatrix Costs(Allowed1->size() + 1,
461*9880d681SAndroid Build Coastguard Worker Allowed2->size() + 1, 0);
462*9880d681SAndroid Build Coastguard Worker addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit);
463*9880d681SAndroid Build Coastguard Worker G.addEdge(N1Id, N2Id, std::move(Costs));
464*9880d681SAndroid Build Coastguard Worker } else {
465*9880d681SAndroid Build Coastguard Worker if (G.getEdgeNode1Id(EId) == N2Id) {
466*9880d681SAndroid Build Coastguard Worker std::swap(N1Id, N2Id);
467*9880d681SAndroid Build Coastguard Worker std::swap(Allowed1, Allowed2);
468*9880d681SAndroid Build Coastguard Worker }
469*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawMatrix Costs(G.getEdgeCosts(EId));
470*9880d681SAndroid Build Coastguard Worker addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit);
471*9880d681SAndroid Build Coastguard Worker G.updateEdgeCosts(EId, std::move(Costs));
472*9880d681SAndroid Build Coastguard Worker }
473*9880d681SAndroid Build Coastguard Worker }
474*9880d681SAndroid Build Coastguard Worker }
475*9880d681SAndroid Build Coastguard Worker }
476*9880d681SAndroid Build Coastguard Worker }
477*9880d681SAndroid Build Coastguard Worker
478*9880d681SAndroid Build Coastguard Worker private:
479*9880d681SAndroid Build Coastguard Worker
addVirtRegCoalesce(PBQPRAGraph::RawMatrix & CostMat,const PBQPRAGraph::NodeMetadata::AllowedRegVector & Allowed1,const PBQPRAGraph::NodeMetadata::AllowedRegVector & Allowed2,PBQP::PBQPNum Benefit)480*9880d681SAndroid Build Coastguard Worker void addVirtRegCoalesce(
481*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawMatrix &CostMat,
482*9880d681SAndroid Build Coastguard Worker const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed1,
483*9880d681SAndroid Build Coastguard Worker const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed2,
484*9880d681SAndroid Build Coastguard Worker PBQP::PBQPNum Benefit) {
485*9880d681SAndroid Build Coastguard Worker assert(CostMat.getRows() == Allowed1.size() + 1 && "Size mismatch.");
486*9880d681SAndroid Build Coastguard Worker assert(CostMat.getCols() == Allowed2.size() + 1 && "Size mismatch.");
487*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I != Allowed1.size(); ++I) {
488*9880d681SAndroid Build Coastguard Worker unsigned PReg1 = Allowed1[I];
489*9880d681SAndroid Build Coastguard Worker for (unsigned J = 0; J != Allowed2.size(); ++J) {
490*9880d681SAndroid Build Coastguard Worker unsigned PReg2 = Allowed2[J];
491*9880d681SAndroid Build Coastguard Worker if (PReg1 == PReg2)
492*9880d681SAndroid Build Coastguard Worker CostMat[I + 1][J + 1] -= Benefit;
493*9880d681SAndroid Build Coastguard Worker }
494*9880d681SAndroid Build Coastguard Worker }
495*9880d681SAndroid Build Coastguard Worker }
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Worker };
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker } // End anonymous namespace.
500*9880d681SAndroid Build Coastguard Worker
501*9880d681SAndroid Build Coastguard Worker // Out-of-line destructor/anchor for PBQPRAConstraint.
~PBQPRAConstraint()502*9880d681SAndroid Build Coastguard Worker PBQPRAConstraint::~PBQPRAConstraint() {}
anchor()503*9880d681SAndroid Build Coastguard Worker void PBQPRAConstraint::anchor() {}
anchor()504*9880d681SAndroid Build Coastguard Worker void PBQPRAConstraintList::anchor() {}
505*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & au) const506*9880d681SAndroid Build Coastguard Worker void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
507*9880d681SAndroid Build Coastguard Worker au.setPreservesCFG();
508*9880d681SAndroid Build Coastguard Worker au.addRequired<AAResultsWrapperPass>();
509*9880d681SAndroid Build Coastguard Worker au.addPreserved<AAResultsWrapperPass>();
510*9880d681SAndroid Build Coastguard Worker au.addRequired<SlotIndexes>();
511*9880d681SAndroid Build Coastguard Worker au.addPreserved<SlotIndexes>();
512*9880d681SAndroid Build Coastguard Worker au.addRequired<LiveIntervals>();
513*9880d681SAndroid Build Coastguard Worker au.addPreserved<LiveIntervals>();
514*9880d681SAndroid Build Coastguard Worker //au.addRequiredID(SplitCriticalEdgesID);
515*9880d681SAndroid Build Coastguard Worker if (customPassID)
516*9880d681SAndroid Build Coastguard Worker au.addRequiredID(*customPassID);
517*9880d681SAndroid Build Coastguard Worker au.addRequired<LiveStacks>();
518*9880d681SAndroid Build Coastguard Worker au.addPreserved<LiveStacks>();
519*9880d681SAndroid Build Coastguard Worker au.addRequired<MachineBlockFrequencyInfo>();
520*9880d681SAndroid Build Coastguard Worker au.addPreserved<MachineBlockFrequencyInfo>();
521*9880d681SAndroid Build Coastguard Worker au.addRequired<MachineLoopInfo>();
522*9880d681SAndroid Build Coastguard Worker au.addPreserved<MachineLoopInfo>();
523*9880d681SAndroid Build Coastguard Worker au.addRequired<MachineDominatorTree>();
524*9880d681SAndroid Build Coastguard Worker au.addPreserved<MachineDominatorTree>();
525*9880d681SAndroid Build Coastguard Worker au.addRequired<VirtRegMap>();
526*9880d681SAndroid Build Coastguard Worker au.addPreserved<VirtRegMap>();
527*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(au);
528*9880d681SAndroid Build Coastguard Worker }
529*9880d681SAndroid Build Coastguard Worker
findVRegIntervalsToAlloc(const MachineFunction & MF,LiveIntervals & LIS)530*9880d681SAndroid Build Coastguard Worker void RegAllocPBQP::findVRegIntervalsToAlloc(const MachineFunction &MF,
531*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS) {
532*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo &MRI = MF.getRegInfo();
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker // Iterate over all live ranges.
535*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
536*9880d681SAndroid Build Coastguard Worker unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
537*9880d681SAndroid Build Coastguard Worker if (MRI.reg_nodbg_empty(Reg))
538*9880d681SAndroid Build Coastguard Worker continue;
539*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(Reg);
540*9880d681SAndroid Build Coastguard Worker
541*9880d681SAndroid Build Coastguard Worker // If this live interval is non-empty we will use pbqp to allocate it.
542*9880d681SAndroid Build Coastguard Worker // Empty intervals we allocate in a simple post-processing stage in
543*9880d681SAndroid Build Coastguard Worker // finalizeAlloc.
544*9880d681SAndroid Build Coastguard Worker if (!LI.empty()) {
545*9880d681SAndroid Build Coastguard Worker VRegsToAlloc.insert(LI.reg);
546*9880d681SAndroid Build Coastguard Worker } else {
547*9880d681SAndroid Build Coastguard Worker EmptyIntervalVRegs.insert(LI.reg);
548*9880d681SAndroid Build Coastguard Worker }
549*9880d681SAndroid Build Coastguard Worker }
550*9880d681SAndroid Build Coastguard Worker }
551*9880d681SAndroid Build Coastguard Worker
isACalleeSavedRegister(unsigned reg,const TargetRegisterInfo & TRI,const MachineFunction & MF)552*9880d681SAndroid Build Coastguard Worker static bool isACalleeSavedRegister(unsigned reg, const TargetRegisterInfo &TRI,
553*9880d681SAndroid Build Coastguard Worker const MachineFunction &MF) {
554*9880d681SAndroid Build Coastguard Worker const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF);
555*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; CSR[i] != 0; ++i)
556*9880d681SAndroid Build Coastguard Worker if (TRI.regsOverlap(reg, CSR[i]))
557*9880d681SAndroid Build Coastguard Worker return true;
558*9880d681SAndroid Build Coastguard Worker return false;
559*9880d681SAndroid Build Coastguard Worker }
560*9880d681SAndroid Build Coastguard Worker
initializeGraph(PBQPRAGraph & G,VirtRegMap & VRM,Spiller & VRegSpiller)561*9880d681SAndroid Build Coastguard Worker void RegAllocPBQP::initializeGraph(PBQPRAGraph &G, VirtRegMap &VRM,
562*9880d681SAndroid Build Coastguard Worker Spiller &VRegSpiller) {
563*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = G.getMetadata().MF;
564*9880d681SAndroid Build Coastguard Worker
565*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS = G.getMetadata().LIS;
566*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo();
567*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI =
568*9880d681SAndroid Build Coastguard Worker *G.getMetadata().MF.getSubtarget().getRegisterInfo();
569*9880d681SAndroid Build Coastguard Worker
570*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> Worklist(VRegsToAlloc.begin(), VRegsToAlloc.end());
571*9880d681SAndroid Build Coastguard Worker
572*9880d681SAndroid Build Coastguard Worker while (!Worklist.empty()) {
573*9880d681SAndroid Build Coastguard Worker unsigned VReg = Worklist.back();
574*9880d681SAndroid Build Coastguard Worker Worklist.pop_back();
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
577*9880d681SAndroid Build Coastguard Worker LiveInterval &VRegLI = LIS.getInterval(VReg);
578*9880d681SAndroid Build Coastguard Worker
579*9880d681SAndroid Build Coastguard Worker // Record any overlaps with regmask operands.
580*9880d681SAndroid Build Coastguard Worker BitVector RegMaskOverlaps;
581*9880d681SAndroid Build Coastguard Worker LIS.checkRegMaskInterference(VRegLI, RegMaskOverlaps);
582*9880d681SAndroid Build Coastguard Worker
583*9880d681SAndroid Build Coastguard Worker // Compute an initial allowed set for the current vreg.
584*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> VRegAllowed;
585*9880d681SAndroid Build Coastguard Worker ArrayRef<MCPhysReg> RawPRegOrder = TRC->getRawAllocationOrder(MF);
586*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I != RawPRegOrder.size(); ++I) {
587*9880d681SAndroid Build Coastguard Worker unsigned PReg = RawPRegOrder[I];
588*9880d681SAndroid Build Coastguard Worker if (MRI.isReserved(PReg))
589*9880d681SAndroid Build Coastguard Worker continue;
590*9880d681SAndroid Build Coastguard Worker
591*9880d681SAndroid Build Coastguard Worker // vregLI crosses a regmask operand that clobbers preg.
592*9880d681SAndroid Build Coastguard Worker if (!RegMaskOverlaps.empty() && !RegMaskOverlaps.test(PReg))
593*9880d681SAndroid Build Coastguard Worker continue;
594*9880d681SAndroid Build Coastguard Worker
595*9880d681SAndroid Build Coastguard Worker // vregLI overlaps fixed regunit interference.
596*9880d681SAndroid Build Coastguard Worker bool Interference = false;
597*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(PReg, &TRI); Units.isValid(); ++Units) {
598*9880d681SAndroid Build Coastguard Worker if (VRegLI.overlaps(LIS.getRegUnit(*Units))) {
599*9880d681SAndroid Build Coastguard Worker Interference = true;
600*9880d681SAndroid Build Coastguard Worker break;
601*9880d681SAndroid Build Coastguard Worker }
602*9880d681SAndroid Build Coastguard Worker }
603*9880d681SAndroid Build Coastguard Worker if (Interference)
604*9880d681SAndroid Build Coastguard Worker continue;
605*9880d681SAndroid Build Coastguard Worker
606*9880d681SAndroid Build Coastguard Worker // preg is usable for this virtual register.
607*9880d681SAndroid Build Coastguard Worker VRegAllowed.push_back(PReg);
608*9880d681SAndroid Build Coastguard Worker }
609*9880d681SAndroid Build Coastguard Worker
610*9880d681SAndroid Build Coastguard Worker // Check for vregs that have no allowed registers. These should be
611*9880d681SAndroid Build Coastguard Worker // pre-spilled and the new vregs added to the worklist.
612*9880d681SAndroid Build Coastguard Worker if (VRegAllowed.empty()) {
613*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> NewVRegs;
614*9880d681SAndroid Build Coastguard Worker spillVReg(VReg, NewVRegs, MF, LIS, VRM, VRegSpiller);
615*9880d681SAndroid Build Coastguard Worker Worklist.insert(Worklist.end(), NewVRegs.begin(), NewVRegs.end());
616*9880d681SAndroid Build Coastguard Worker continue;
617*9880d681SAndroid Build Coastguard Worker }
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::RawVector NodeCosts(VRegAllowed.size() + 1, 0);
620*9880d681SAndroid Build Coastguard Worker
621*9880d681SAndroid Build Coastguard Worker // Tweak cost of callee saved registers, as using then force spilling and
622*9880d681SAndroid Build Coastguard Worker // restoring them. This would only happen in the prologue / epilogue though.
623*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != VRegAllowed.size(); ++i)
624*9880d681SAndroid Build Coastguard Worker if (isACalleeSavedRegister(VRegAllowed[i], TRI, MF))
625*9880d681SAndroid Build Coastguard Worker NodeCosts[1 + i] += 1.0;
626*9880d681SAndroid Build Coastguard Worker
627*9880d681SAndroid Build Coastguard Worker PBQPRAGraph::NodeId NId = G.addNode(std::move(NodeCosts));
628*9880d681SAndroid Build Coastguard Worker G.getNodeMetadata(NId).setVReg(VReg);
629*9880d681SAndroid Build Coastguard Worker G.getNodeMetadata(NId).setAllowedRegs(
630*9880d681SAndroid Build Coastguard Worker G.getMetadata().getAllowedRegs(std::move(VRegAllowed)));
631*9880d681SAndroid Build Coastguard Worker G.getMetadata().setNodeIdForVReg(VReg, NId);
632*9880d681SAndroid Build Coastguard Worker }
633*9880d681SAndroid Build Coastguard Worker }
634*9880d681SAndroid Build Coastguard Worker
spillVReg(unsigned VReg,SmallVectorImpl<unsigned> & NewIntervals,MachineFunction & MF,LiveIntervals & LIS,VirtRegMap & VRM,Spiller & VRegSpiller)635*9880d681SAndroid Build Coastguard Worker void RegAllocPBQP::spillVReg(unsigned VReg,
636*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> &NewIntervals,
637*9880d681SAndroid Build Coastguard Worker MachineFunction &MF, LiveIntervals &LIS,
638*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM, Spiller &VRegSpiller) {
639*9880d681SAndroid Build Coastguard Worker
640*9880d681SAndroid Build Coastguard Worker VRegsToAlloc.erase(VReg);
641*9880d681SAndroid Build Coastguard Worker LiveRangeEdit LRE(&LIS.getInterval(VReg), NewIntervals, MF, LIS, &VRM,
642*9880d681SAndroid Build Coastguard Worker nullptr, &DeadRemats);
643*9880d681SAndroid Build Coastguard Worker VRegSpiller.spill(LRE);
644*9880d681SAndroid Build Coastguard Worker
645*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
646*9880d681SAndroid Build Coastguard Worker (void)TRI;
647*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> SPILLED (Cost: "
648*9880d681SAndroid Build Coastguard Worker << LRE.getParent().weight << ", New vregs: ");
649*9880d681SAndroid Build Coastguard Worker
650*9880d681SAndroid Build Coastguard Worker // Copy any newly inserted live intervals into the list of regs to
651*9880d681SAndroid Build Coastguard Worker // allocate.
652*9880d681SAndroid Build Coastguard Worker for (LiveRangeEdit::iterator I = LRE.begin(), E = LRE.end();
653*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
654*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LIS.getInterval(*I);
655*9880d681SAndroid Build Coastguard Worker assert(!LI.empty() && "Empty spill range.");
656*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << PrintReg(LI.reg, &TRI) << " ");
657*9880d681SAndroid Build Coastguard Worker VRegsToAlloc.insert(LI.reg);
658*9880d681SAndroid Build Coastguard Worker }
659*9880d681SAndroid Build Coastguard Worker
660*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << ")\n");
661*9880d681SAndroid Build Coastguard Worker }
662*9880d681SAndroid Build Coastguard Worker
mapPBQPToRegAlloc(const PBQPRAGraph & G,const PBQP::Solution & Solution,VirtRegMap & VRM,Spiller & VRegSpiller)663*9880d681SAndroid Build Coastguard Worker bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAGraph &G,
664*9880d681SAndroid Build Coastguard Worker const PBQP::Solution &Solution,
665*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM,
666*9880d681SAndroid Build Coastguard Worker Spiller &VRegSpiller) {
667*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = G.getMetadata().MF;
668*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS = G.getMetadata().LIS;
669*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
670*9880d681SAndroid Build Coastguard Worker (void)TRI;
671*9880d681SAndroid Build Coastguard Worker
672*9880d681SAndroid Build Coastguard Worker // Set to true if we have any spills
673*9880d681SAndroid Build Coastguard Worker bool AnotherRoundNeeded = false;
674*9880d681SAndroid Build Coastguard Worker
675*9880d681SAndroid Build Coastguard Worker // Clear the existing allocation.
676*9880d681SAndroid Build Coastguard Worker VRM.clearAllVirt();
677*9880d681SAndroid Build Coastguard Worker
678*9880d681SAndroid Build Coastguard Worker // Iterate over the nodes mapping the PBQP solution to a register
679*9880d681SAndroid Build Coastguard Worker // assignment.
680*9880d681SAndroid Build Coastguard Worker for (auto NId : G.nodeIds()) {
681*9880d681SAndroid Build Coastguard Worker unsigned VReg = G.getNodeMetadata(NId).getVReg();
682*9880d681SAndroid Build Coastguard Worker unsigned AllocOption = Solution.getSelection(NId);
683*9880d681SAndroid Build Coastguard Worker
684*9880d681SAndroid Build Coastguard Worker if (AllocOption != PBQP::RegAlloc::getSpillOptionIdx()) {
685*9880d681SAndroid Build Coastguard Worker unsigned PReg = G.getNodeMetadata(NId).getAllowedRegs()[AllocOption - 1];
686*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> "
687*9880d681SAndroid Build Coastguard Worker << TRI.getName(PReg) << "\n");
688*9880d681SAndroid Build Coastguard Worker assert(PReg != 0 && "Invalid preg selected.");
689*9880d681SAndroid Build Coastguard Worker VRM.assignVirt2Phys(VReg, PReg);
690*9880d681SAndroid Build Coastguard Worker } else {
691*9880d681SAndroid Build Coastguard Worker // Spill VReg. If this introduces new intervals we'll need another round
692*9880d681SAndroid Build Coastguard Worker // of allocation.
693*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> NewVRegs;
694*9880d681SAndroid Build Coastguard Worker spillVReg(VReg, NewVRegs, MF, LIS, VRM, VRegSpiller);
695*9880d681SAndroid Build Coastguard Worker AnotherRoundNeeded |= !NewVRegs.empty();
696*9880d681SAndroid Build Coastguard Worker }
697*9880d681SAndroid Build Coastguard Worker }
698*9880d681SAndroid Build Coastguard Worker
699*9880d681SAndroid Build Coastguard Worker return !AnotherRoundNeeded;
700*9880d681SAndroid Build Coastguard Worker }
701*9880d681SAndroid Build Coastguard Worker
finalizeAlloc(MachineFunction & MF,LiveIntervals & LIS,VirtRegMap & VRM) const702*9880d681SAndroid Build Coastguard Worker void RegAllocPBQP::finalizeAlloc(MachineFunction &MF,
703*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS,
704*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM) const {
705*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF.getRegInfo();
706*9880d681SAndroid Build Coastguard Worker
707*9880d681SAndroid Build Coastguard Worker // First allocate registers for the empty intervals.
708*9880d681SAndroid Build Coastguard Worker for (RegSet::const_iterator
709*9880d681SAndroid Build Coastguard Worker I = EmptyIntervalVRegs.begin(), E = EmptyIntervalVRegs.end();
710*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
711*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(*I);
712*9880d681SAndroid Build Coastguard Worker
713*9880d681SAndroid Build Coastguard Worker unsigned PReg = MRI.getSimpleHint(LI.reg);
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Worker if (PReg == 0) {
716*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass &RC = *MRI.getRegClass(LI.reg);
717*9880d681SAndroid Build Coastguard Worker PReg = RC.getRawAllocationOrder(MF).front();
718*9880d681SAndroid Build Coastguard Worker }
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard Worker VRM.assignVirt2Phys(LI.reg, PReg);
721*9880d681SAndroid Build Coastguard Worker }
722*9880d681SAndroid Build Coastguard Worker }
723*9880d681SAndroid Build Coastguard Worker
postOptimization(Spiller & VRegSpiller,LiveIntervals & LIS)724*9880d681SAndroid Build Coastguard Worker void RegAllocPBQP::postOptimization(Spiller &VRegSpiller, LiveIntervals &LIS) {
725*9880d681SAndroid Build Coastguard Worker VRegSpiller.postOptimization();
726*9880d681SAndroid Build Coastguard Worker /// Remove dead defs because of rematerialization.
727*9880d681SAndroid Build Coastguard Worker for (auto DeadInst : DeadRemats) {
728*9880d681SAndroid Build Coastguard Worker LIS.RemoveMachineInstrFromMaps(*DeadInst);
729*9880d681SAndroid Build Coastguard Worker DeadInst->eraseFromParent();
730*9880d681SAndroid Build Coastguard Worker }
731*9880d681SAndroid Build Coastguard Worker DeadRemats.clear();
732*9880d681SAndroid Build Coastguard Worker }
733*9880d681SAndroid Build Coastguard Worker
normalizePBQPSpillWeight(float UseDefFreq,unsigned Size,unsigned NumInstr)734*9880d681SAndroid Build Coastguard Worker static inline float normalizePBQPSpillWeight(float UseDefFreq, unsigned Size,
735*9880d681SAndroid Build Coastguard Worker unsigned NumInstr) {
736*9880d681SAndroid Build Coastguard Worker // All intervals have a spill weight that is mostly proportional to the number
737*9880d681SAndroid Build Coastguard Worker // of uses, with uses in loops having a bigger weight.
738*9880d681SAndroid Build Coastguard Worker return NumInstr * normalizeSpillWeight(UseDefFreq, Size, 1);
739*9880d681SAndroid Build Coastguard Worker }
740*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)741*9880d681SAndroid Build Coastguard Worker bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
742*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS = getAnalysis<LiveIntervals>();
743*9880d681SAndroid Build Coastguard Worker MachineBlockFrequencyInfo &MBFI =
744*9880d681SAndroid Build Coastguard Worker getAnalysis<MachineBlockFrequencyInfo>();
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM = getAnalysis<VirtRegMap>();
747*9880d681SAndroid Build Coastguard Worker
748*9880d681SAndroid Build Coastguard Worker calculateSpillWeightsAndHints(LIS, MF, &VRM, getAnalysis<MachineLoopInfo>(),
749*9880d681SAndroid Build Coastguard Worker MBFI, normalizePBQPSpillWeight);
750*9880d681SAndroid Build Coastguard Worker
751*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Spiller> VRegSpiller(createInlineSpiller(*this, MF, VRM));
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker MF.getRegInfo().freezeReservedRegs(MF);
754*9880d681SAndroid Build Coastguard Worker
755*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "PBQP Register Allocating for " << MF.getName() << "\n");
756*9880d681SAndroid Build Coastguard Worker
757*9880d681SAndroid Build Coastguard Worker // Allocator main loop:
758*9880d681SAndroid Build Coastguard Worker //
759*9880d681SAndroid Build Coastguard Worker // * Map current regalloc problem to a PBQP problem
760*9880d681SAndroid Build Coastguard Worker // * Solve the PBQP problem
761*9880d681SAndroid Build Coastguard Worker // * Map the solution back to a register allocation
762*9880d681SAndroid Build Coastguard Worker // * Spill if necessary
763*9880d681SAndroid Build Coastguard Worker //
764*9880d681SAndroid Build Coastguard Worker // This process is continued till no more spills are generated.
765*9880d681SAndroid Build Coastguard Worker
766*9880d681SAndroid Build Coastguard Worker // Find the vreg intervals in need of allocation.
767*9880d681SAndroid Build Coastguard Worker findVRegIntervalsToAlloc(MF, LIS);
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
770*9880d681SAndroid Build Coastguard Worker const Function &F = *MF.getFunction();
771*9880d681SAndroid Build Coastguard Worker std::string FullyQualifiedName =
772*9880d681SAndroid Build Coastguard Worker F.getParent()->getModuleIdentifier() + "." + F.getName().str();
773*9880d681SAndroid Build Coastguard Worker #endif
774*9880d681SAndroid Build Coastguard Worker
775*9880d681SAndroid Build Coastguard Worker // If there are non-empty intervals allocate them using pbqp.
776*9880d681SAndroid Build Coastguard Worker if (!VRegsToAlloc.empty()) {
777*9880d681SAndroid Build Coastguard Worker
778*9880d681SAndroid Build Coastguard Worker const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
779*9880d681SAndroid Build Coastguard Worker std::unique_ptr<PBQPRAConstraintList> ConstraintsRoot =
780*9880d681SAndroid Build Coastguard Worker llvm::make_unique<PBQPRAConstraintList>();
781*9880d681SAndroid Build Coastguard Worker ConstraintsRoot->addConstraint(llvm::make_unique<SpillCosts>());
782*9880d681SAndroid Build Coastguard Worker ConstraintsRoot->addConstraint(llvm::make_unique<Interference>());
783*9880d681SAndroid Build Coastguard Worker if (PBQPCoalescing)
784*9880d681SAndroid Build Coastguard Worker ConstraintsRoot->addConstraint(llvm::make_unique<Coalescing>());
785*9880d681SAndroid Build Coastguard Worker ConstraintsRoot->addConstraint(Subtarget.getCustomPBQPConstraints());
786*9880d681SAndroid Build Coastguard Worker
787*9880d681SAndroid Build Coastguard Worker bool PBQPAllocComplete = false;
788*9880d681SAndroid Build Coastguard Worker unsigned Round = 0;
789*9880d681SAndroid Build Coastguard Worker
790*9880d681SAndroid Build Coastguard Worker while (!PBQPAllocComplete) {
791*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " PBQP Regalloc round " << Round << ":\n");
792*9880d681SAndroid Build Coastguard Worker
793*9880d681SAndroid Build Coastguard Worker PBQPRAGraph G(PBQPRAGraph::GraphMetadata(MF, LIS, MBFI));
794*9880d681SAndroid Build Coastguard Worker initializeGraph(G, VRM, *VRegSpiller);
795*9880d681SAndroid Build Coastguard Worker ConstraintsRoot->apply(G);
796*9880d681SAndroid Build Coastguard Worker
797*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
798*9880d681SAndroid Build Coastguard Worker if (PBQPDumpGraphs) {
799*9880d681SAndroid Build Coastguard Worker std::ostringstream RS;
800*9880d681SAndroid Build Coastguard Worker RS << Round;
801*9880d681SAndroid Build Coastguard Worker std::string GraphFileName = FullyQualifiedName + "." + RS.str() +
802*9880d681SAndroid Build Coastguard Worker ".pbqpgraph";
803*9880d681SAndroid Build Coastguard Worker std::error_code EC;
804*9880d681SAndroid Build Coastguard Worker raw_fd_ostream OS(GraphFileName, EC, sys::fs::F_Text);
805*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Dumping graph for round " << Round << " to \""
806*9880d681SAndroid Build Coastguard Worker << GraphFileName << "\"\n");
807*9880d681SAndroid Build Coastguard Worker G.dump(OS);
808*9880d681SAndroid Build Coastguard Worker }
809*9880d681SAndroid Build Coastguard Worker #endif
810*9880d681SAndroid Build Coastguard Worker
811*9880d681SAndroid Build Coastguard Worker PBQP::Solution Solution = PBQP::RegAlloc::solve(G);
812*9880d681SAndroid Build Coastguard Worker PBQPAllocComplete = mapPBQPToRegAlloc(G, Solution, VRM, *VRegSpiller);
813*9880d681SAndroid Build Coastguard Worker ++Round;
814*9880d681SAndroid Build Coastguard Worker }
815*9880d681SAndroid Build Coastguard Worker }
816*9880d681SAndroid Build Coastguard Worker
817*9880d681SAndroid Build Coastguard Worker // Finalise allocation, allocate empty ranges.
818*9880d681SAndroid Build Coastguard Worker finalizeAlloc(MF, LIS, VRM);
819*9880d681SAndroid Build Coastguard Worker postOptimization(*VRegSpiller, LIS);
820*9880d681SAndroid Build Coastguard Worker VRegsToAlloc.clear();
821*9880d681SAndroid Build Coastguard Worker EmptyIntervalVRegs.clear();
822*9880d681SAndroid Build Coastguard Worker
823*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << VRM << "\n");
824*9880d681SAndroid Build Coastguard Worker
825*9880d681SAndroid Build Coastguard Worker return true;
826*9880d681SAndroid Build Coastguard Worker }
827*9880d681SAndroid Build Coastguard Worker
828*9880d681SAndroid Build Coastguard Worker /// Create Printable object for node and register info.
PrintNodeInfo(PBQP::RegAlloc::PBQPRAGraph::NodeId NId,const PBQP::RegAlloc::PBQPRAGraph & G)829*9880d681SAndroid Build Coastguard Worker static Printable PrintNodeInfo(PBQP::RegAlloc::PBQPRAGraph::NodeId NId,
830*9880d681SAndroid Build Coastguard Worker const PBQP::RegAlloc::PBQPRAGraph &G) {
831*9880d681SAndroid Build Coastguard Worker return Printable([NId, &G](raw_ostream &OS) {
832*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo();
833*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
834*9880d681SAndroid Build Coastguard Worker unsigned VReg = G.getNodeMetadata(NId).getVReg();
835*9880d681SAndroid Build Coastguard Worker const char *RegClassName = TRI->getRegClassName(MRI.getRegClass(VReg));
836*9880d681SAndroid Build Coastguard Worker OS << NId << " (" << RegClassName << ':' << PrintReg(VReg, TRI) << ')';
837*9880d681SAndroid Build Coastguard Worker });
838*9880d681SAndroid Build Coastguard Worker }
839*9880d681SAndroid Build Coastguard Worker
dump(raw_ostream & OS) const840*9880d681SAndroid Build Coastguard Worker void PBQP::RegAlloc::PBQPRAGraph::dump(raw_ostream &OS) const {
841*9880d681SAndroid Build Coastguard Worker for (auto NId : nodeIds()) {
842*9880d681SAndroid Build Coastguard Worker const Vector &Costs = getNodeCosts(NId);
843*9880d681SAndroid Build Coastguard Worker assert(Costs.getLength() != 0 && "Empty vector in graph.");
844*9880d681SAndroid Build Coastguard Worker OS << PrintNodeInfo(NId, *this) << ": " << Costs << '\n';
845*9880d681SAndroid Build Coastguard Worker }
846*9880d681SAndroid Build Coastguard Worker OS << '\n';
847*9880d681SAndroid Build Coastguard Worker
848*9880d681SAndroid Build Coastguard Worker for (auto EId : edgeIds()) {
849*9880d681SAndroid Build Coastguard Worker NodeId N1Id = getEdgeNode1Id(EId);
850*9880d681SAndroid Build Coastguard Worker NodeId N2Id = getEdgeNode2Id(EId);
851*9880d681SAndroid Build Coastguard Worker assert(N1Id != N2Id && "PBQP graphs should not have self-edges.");
852*9880d681SAndroid Build Coastguard Worker const Matrix &M = getEdgeCosts(EId);
853*9880d681SAndroid Build Coastguard Worker assert(M.getRows() != 0 && "No rows in matrix.");
854*9880d681SAndroid Build Coastguard Worker assert(M.getCols() != 0 && "No cols in matrix.");
855*9880d681SAndroid Build Coastguard Worker OS << PrintNodeInfo(N1Id, *this) << ' ' << M.getRows() << " rows / ";
856*9880d681SAndroid Build Coastguard Worker OS << PrintNodeInfo(N2Id, *this) << ' ' << M.getCols() << " cols:\n";
857*9880d681SAndroid Build Coastguard Worker OS << M << '\n';
858*9880d681SAndroid Build Coastguard Worker }
859*9880d681SAndroid Build Coastguard Worker }
860*9880d681SAndroid Build Coastguard Worker
dump() const861*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void PBQP::RegAlloc::PBQPRAGraph::dump() const { dump(dbgs()); }
862*9880d681SAndroid Build Coastguard Worker
printDot(raw_ostream & OS) const863*9880d681SAndroid Build Coastguard Worker void PBQP::RegAlloc::PBQPRAGraph::printDot(raw_ostream &OS) const {
864*9880d681SAndroid Build Coastguard Worker OS << "graph {\n";
865*9880d681SAndroid Build Coastguard Worker for (auto NId : nodeIds()) {
866*9880d681SAndroid Build Coastguard Worker OS << " node" << NId << " [ label=\""
867*9880d681SAndroid Build Coastguard Worker << PrintNodeInfo(NId, *this) << "\\n"
868*9880d681SAndroid Build Coastguard Worker << getNodeCosts(NId) << "\" ]\n";
869*9880d681SAndroid Build Coastguard Worker }
870*9880d681SAndroid Build Coastguard Worker
871*9880d681SAndroid Build Coastguard Worker OS << " edge [ len=" << nodeIds().size() << " ]\n";
872*9880d681SAndroid Build Coastguard Worker for (auto EId : edgeIds()) {
873*9880d681SAndroid Build Coastguard Worker OS << " node" << getEdgeNode1Id(EId)
874*9880d681SAndroid Build Coastguard Worker << " -- node" << getEdgeNode2Id(EId)
875*9880d681SAndroid Build Coastguard Worker << " [ label=\"";
876*9880d681SAndroid Build Coastguard Worker const Matrix &EdgeCosts = getEdgeCosts(EId);
877*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < EdgeCosts.getRows(); ++i) {
878*9880d681SAndroid Build Coastguard Worker OS << EdgeCosts.getRowAsVector(i) << "\\n";
879*9880d681SAndroid Build Coastguard Worker }
880*9880d681SAndroid Build Coastguard Worker OS << "\" ]\n";
881*9880d681SAndroid Build Coastguard Worker }
882*9880d681SAndroid Build Coastguard Worker OS << "}\n";
883*9880d681SAndroid Build Coastguard Worker }
884*9880d681SAndroid Build Coastguard Worker
createPBQPRegisterAllocator(char * customPassID)885*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createPBQPRegisterAllocator(char *customPassID) {
886*9880d681SAndroid Build Coastguard Worker return new RegAllocPBQP(customPassID);
887*9880d681SAndroid Build Coastguard Worker }
888*9880d681SAndroid Build Coastguard Worker
createDefaultPBQPRegisterAllocator()889*9880d681SAndroid Build Coastguard Worker FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
890*9880d681SAndroid Build Coastguard Worker return createPBQPRegisterAllocator();
891*9880d681SAndroid Build Coastguard Worker }
892*9880d681SAndroid Build Coastguard Worker
893*9880d681SAndroid Build Coastguard Worker #undef DEBUG_TYPE
894