10b57cec5SDimitry Andric //===- RegAllocPBQP.cpp ---- PBQP Register Allocator ----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a Partitioned Boolean Quadratic Programming (PBQP) based
100b57cec5SDimitry Andric // register allocator for LLVM. This allocator works by constructing a PBQP
110b57cec5SDimitry Andric // problem representing the register allocation problem under consideration,
120b57cec5SDimitry Andric // solving this using a PBQP solver, and mapping the solution back to a
130b57cec5SDimitry Andric // register assignment. If any variables are selected for spilling then spill
140b57cec5SDimitry Andric // code is inserted and the process repeated.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned
170b57cec5SDimitry Andric // for register allocation. For more information on PBQP for register
180b57cec5SDimitry Andric // allocation, see the following papers:
190b57cec5SDimitry Andric //
200b57cec5SDimitry Andric // (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with
210b57cec5SDimitry Andric // PBQP. In Proceedings of the 7th Joint Modular Languages Conference
220b57cec5SDimitry Andric // (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361.
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric // (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular
250b57cec5SDimitry Andric // architectures. In Proceedings of the Joint Conference on Languages,
260b57cec5SDimitry Andric // Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York,
270b57cec5SDimitry Andric // NY, USA, 139-148.
280b57cec5SDimitry Andric //
290b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric #include "llvm/CodeGen/RegAllocPBQP.h"
320b57cec5SDimitry Andric #include "RegisterCoalescer.h"
330b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
340b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
350b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
360b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
370b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
380b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
390b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
400b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
410b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
440b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
460b57cec5SDimitry Andric #include "llvm/CodeGen/LiveStacks.h"
470b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
480b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
490b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
500b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
510b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
520b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
530b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
540b57cec5SDimitry Andric #include "llvm/CodeGen/PBQP/Graph.h"
550b57cec5SDimitry Andric #include "llvm/CodeGen/PBQP/Math.h"
560b57cec5SDimitry Andric #include "llvm/CodeGen/PBQP/Solution.h"
570b57cec5SDimitry Andric #include "llvm/CodeGen/PBQPRAConstraint.h"
580b57cec5SDimitry Andric #include "llvm/CodeGen/RegAllocRegistry.h"
590b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
605ffd83dbSDimitry Andric #include "llvm/CodeGen/Spiller.h"
610b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
620b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
630b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
640b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
650b57cec5SDimitry Andric #include "llvm/IR/Function.h"
660b57cec5SDimitry Andric #include "llvm/IR/Module.h"
670b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
680b57cec5SDimitry Andric #include "llvm/Pass.h"
690b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
700b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
710b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
720b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
730b57cec5SDimitry Andric #include "llvm/Support/Printable.h"
740b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
750b57cec5SDimitry Andric #include <algorithm>
760b57cec5SDimitry Andric #include <cassert>
770b57cec5SDimitry Andric #include <cstddef>
780b57cec5SDimitry Andric #include <limits>
790b57cec5SDimitry Andric #include <map>
800b57cec5SDimitry Andric #include <memory>
810b57cec5SDimitry Andric #include <queue>
820b57cec5SDimitry Andric #include <set>
830b57cec5SDimitry Andric #include <sstream>
840b57cec5SDimitry Andric #include <string>
850b57cec5SDimitry Andric #include <system_error>
860b57cec5SDimitry Andric #include <tuple>
870b57cec5SDimitry Andric #include <utility>
880b57cec5SDimitry Andric #include <vector>
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric using namespace llvm;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric static RegisterRegAlloc
950b57cec5SDimitry Andric RegisterPBQPRepAlloc("pbqp", "PBQP register allocator",
960b57cec5SDimitry Andric createDefaultPBQPRegisterAllocator);
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric static cl::opt<bool>
990b57cec5SDimitry Andric PBQPCoalescing("pbqp-coalescing",
1000b57cec5SDimitry Andric cl::desc("Attempt coalescing during PBQP register allocation."),
1010b57cec5SDimitry Andric cl::init(false), cl::Hidden);
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric #ifndef NDEBUG
1040b57cec5SDimitry Andric static cl::opt<bool>
1050b57cec5SDimitry Andric PBQPDumpGraphs("pbqp-dump-graphs",
1060b57cec5SDimitry Andric cl::desc("Dump graphs for each function/round in the compilation unit."),
1070b57cec5SDimitry Andric cl::init(false), cl::Hidden);
1080b57cec5SDimitry Andric #endif
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric namespace {
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric ///
1130b57cec5SDimitry Andric /// PBQP based allocators solve the register allocation problem by mapping
1140b57cec5SDimitry Andric /// register allocation problems to Partitioned Boolean Quadratic
1150b57cec5SDimitry Andric /// Programming problems.
1160b57cec5SDimitry Andric class RegAllocPBQP : public MachineFunctionPass {
1170b57cec5SDimitry Andric public:
1180b57cec5SDimitry Andric static char ID;
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric /// Construct a PBQP register allocator.
RegAllocPBQP(char * cPassID=nullptr)1210b57cec5SDimitry Andric RegAllocPBQP(char *cPassID = nullptr)
1220b57cec5SDimitry Andric : MachineFunctionPass(ID), customPassID(cPassID) {
123*0fca6ea1SDimitry Andric initializeSlotIndexesWrapperPassPass(*PassRegistry::getPassRegistry());
124*0fca6ea1SDimitry Andric initializeLiveIntervalsWrapperPassPass(*PassRegistry::getPassRegistry());
1250b57cec5SDimitry Andric initializeLiveStacksPass(*PassRegistry::getPassRegistry());
1260b57cec5SDimitry Andric initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric /// Return the pass name.
getPassName() const1300b57cec5SDimitry Andric StringRef getPassName() const override { return "PBQP Register Allocator"; }
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric /// PBQP analysis usage.
1330b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &au) const override;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric /// Perform register allocation
1360b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
1370b57cec5SDimitry Andric
getRequiredProperties() const1380b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
1390b57cec5SDimitry Andric return MachineFunctionProperties().set(
1400b57cec5SDimitry Andric MachineFunctionProperties::Property::NoPHIs);
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric
getClearedProperties() const143e8d8bef9SDimitry Andric MachineFunctionProperties getClearedProperties() const override {
144e8d8bef9SDimitry Andric return MachineFunctionProperties().set(
145e8d8bef9SDimitry Andric MachineFunctionProperties::Property::IsSSA);
146e8d8bef9SDimitry Andric }
147e8d8bef9SDimitry Andric
1480b57cec5SDimitry Andric private:
149e8d8bef9SDimitry Andric using RegSet = std::set<Register>;
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric char *customPassID;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric RegSet VRegsToAlloc, EmptyIntervalVRegs;
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric /// Inst which is a def of an original reg and whose defs are already all
1560b57cec5SDimitry Andric /// dead after remat is saved in DeadRemats. The deletion of such inst is
1570b57cec5SDimitry Andric /// postponed till all the allocations are done, so its remat expr is
1580b57cec5SDimitry Andric /// always available for the remat of all the siblings of the original reg.
1590b57cec5SDimitry Andric SmallPtrSet<MachineInstr *, 32> DeadRemats;
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric /// Finds the initial set of vreg intervals to allocate.
1620b57cec5SDimitry Andric void findVRegIntervalsToAlloc(const MachineFunction &MF, LiveIntervals &LIS);
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric /// Constructs an initial graph.
1650b57cec5SDimitry Andric void initializeGraph(PBQPRAGraph &G, VirtRegMap &VRM, Spiller &VRegSpiller);
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric /// Spill the given VReg.
1685ffd83dbSDimitry Andric void spillVReg(Register VReg, SmallVectorImpl<Register> &NewIntervals,
1690b57cec5SDimitry Andric MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM,
1700b57cec5SDimitry Andric Spiller &VRegSpiller);
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric /// Given a solved PBQP problem maps this solution back to a register
1730b57cec5SDimitry Andric /// assignment.
1740b57cec5SDimitry Andric bool mapPBQPToRegAlloc(const PBQPRAGraph &G,
1750b57cec5SDimitry Andric const PBQP::Solution &Solution,
1760b57cec5SDimitry Andric VirtRegMap &VRM,
1770b57cec5SDimitry Andric Spiller &VRegSpiller);
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric /// Postprocessing before final spilling. Sets basic block "live in"
1800b57cec5SDimitry Andric /// variables.
1810b57cec5SDimitry Andric void finalizeAlloc(MachineFunction &MF, LiveIntervals &LIS,
1820b57cec5SDimitry Andric VirtRegMap &VRM) const;
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric void postOptimization(Spiller &VRegSpiller, LiveIntervals &LIS);
1850b57cec5SDimitry Andric };
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andric char RegAllocPBQP::ID = 0;
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric /// Set spill costs for each node in the PBQP reg-alloc graph.
1900b57cec5SDimitry Andric class SpillCosts : public PBQPRAConstraint {
1910b57cec5SDimitry Andric public:
apply(PBQPRAGraph & G)1920b57cec5SDimitry Andric void apply(PBQPRAGraph &G) override {
1930b57cec5SDimitry Andric LiveIntervals &LIS = G.getMetadata().LIS;
1940b57cec5SDimitry Andric
1955f757f3fSDimitry Andric // A minimum spill costs, so that register constraints can be set
1960b57cec5SDimitry Andric // without normalization in the [0.0:MinSpillCost( interval.
1970b57cec5SDimitry Andric const PBQP::PBQPNum MinSpillCost = 10.0;
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric for (auto NId : G.nodeIds()) {
2000b57cec5SDimitry Andric PBQP::PBQPNum SpillCost =
201e8d8bef9SDimitry Andric LIS.getInterval(G.getNodeMetadata(NId).getVReg()).weight();
2020b57cec5SDimitry Andric if (SpillCost == 0.0)
2030b57cec5SDimitry Andric SpillCost = std::numeric_limits<PBQP::PBQPNum>::min();
2040b57cec5SDimitry Andric else
2050b57cec5SDimitry Andric SpillCost += MinSpillCost;
2060b57cec5SDimitry Andric PBQPRAGraph::RawVector NodeCosts(G.getNodeCosts(NId));
2070b57cec5SDimitry Andric NodeCosts[PBQP::RegAlloc::getSpillOptionIdx()] = SpillCost;
2080b57cec5SDimitry Andric G.setNodeCosts(NId, std::move(NodeCosts));
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric };
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric /// Add interference edges between overlapping vregs.
2140b57cec5SDimitry Andric class Interference : public PBQPRAConstraint {
2150b57cec5SDimitry Andric private:
2160b57cec5SDimitry Andric using AllowedRegVecPtr = const PBQP::RegAlloc::AllowedRegVector *;
2170b57cec5SDimitry Andric using IKey = std::pair<AllowedRegVecPtr, AllowedRegVecPtr>;
2180b57cec5SDimitry Andric using IMatrixCache = DenseMap<IKey, PBQPRAGraph::MatrixPtr>;
2190b57cec5SDimitry Andric using DisjointAllowedRegsCache = DenseSet<IKey>;
2200b57cec5SDimitry Andric using IEdgeKey = std::pair<PBQP::GraphBase::NodeId, PBQP::GraphBase::NodeId>;
2210b57cec5SDimitry Andric using IEdgeCache = DenseSet<IEdgeKey>;
2220b57cec5SDimitry Andric
haveDisjointAllowedRegs(const PBQPRAGraph & G,PBQPRAGraph::NodeId NId,PBQPRAGraph::NodeId MId,const DisjointAllowedRegsCache & D) const2230b57cec5SDimitry Andric bool haveDisjointAllowedRegs(const PBQPRAGraph &G, PBQPRAGraph::NodeId NId,
2240b57cec5SDimitry Andric PBQPRAGraph::NodeId MId,
2250b57cec5SDimitry Andric const DisjointAllowedRegsCache &D) const {
2260b57cec5SDimitry Andric const auto *NRegs = &G.getNodeMetadata(NId).getAllowedRegs();
2270b57cec5SDimitry Andric const auto *MRegs = &G.getNodeMetadata(MId).getAllowedRegs();
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric if (NRegs == MRegs)
2300b57cec5SDimitry Andric return false;
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric if (NRegs < MRegs)
233e8d8bef9SDimitry Andric return D.contains(IKey(NRegs, MRegs));
2340b57cec5SDimitry Andric
235e8d8bef9SDimitry Andric return D.contains(IKey(MRegs, NRegs));
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric
setDisjointAllowedRegs(const PBQPRAGraph & G,PBQPRAGraph::NodeId NId,PBQPRAGraph::NodeId MId,DisjointAllowedRegsCache & D)2380b57cec5SDimitry Andric void setDisjointAllowedRegs(const PBQPRAGraph &G, PBQPRAGraph::NodeId NId,
2390b57cec5SDimitry Andric PBQPRAGraph::NodeId MId,
2400b57cec5SDimitry Andric DisjointAllowedRegsCache &D) {
2410b57cec5SDimitry Andric const auto *NRegs = &G.getNodeMetadata(NId).getAllowedRegs();
2420b57cec5SDimitry Andric const auto *MRegs = &G.getNodeMetadata(MId).getAllowedRegs();
2430b57cec5SDimitry Andric
2440b57cec5SDimitry Andric assert(NRegs != MRegs && "AllowedRegs can not be disjoint with itself");
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andric if (NRegs < MRegs)
2470b57cec5SDimitry Andric D.insert(IKey(NRegs, MRegs));
2480b57cec5SDimitry Andric else
2490b57cec5SDimitry Andric D.insert(IKey(MRegs, NRegs));
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric // Holds (Interval, CurrentSegmentID, and NodeId). The first two are required
2530b57cec5SDimitry Andric // for the fast interference graph construction algorithm. The last is there
2540b57cec5SDimitry Andric // to save us from looking up node ids via the VRegToNode map in the graph
2550b57cec5SDimitry Andric // metadata.
2560b57cec5SDimitry Andric using IntervalInfo =
2570b57cec5SDimitry Andric std::tuple<LiveInterval*, size_t, PBQP::GraphBase::NodeId>;
2580b57cec5SDimitry Andric
getStartPoint(const IntervalInfo & I)2590b57cec5SDimitry Andric static SlotIndex getStartPoint(const IntervalInfo &I) {
2600b57cec5SDimitry Andric return std::get<0>(I)->segments[std::get<1>(I)].start;
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric
getEndPoint(const IntervalInfo & I)2630b57cec5SDimitry Andric static SlotIndex getEndPoint(const IntervalInfo &I) {
2640b57cec5SDimitry Andric return std::get<0>(I)->segments[std::get<1>(I)].end;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric
getNodeId(const IntervalInfo & I)2670b57cec5SDimitry Andric static PBQP::GraphBase::NodeId getNodeId(const IntervalInfo &I) {
2680b57cec5SDimitry Andric return std::get<2>(I);
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric
lowestStartPoint(const IntervalInfo & I1,const IntervalInfo & I2)2710b57cec5SDimitry Andric static bool lowestStartPoint(const IntervalInfo &I1,
2720b57cec5SDimitry Andric const IntervalInfo &I2) {
2730b57cec5SDimitry Andric // Condition reversed because priority queue has the *highest* element at
2740b57cec5SDimitry Andric // the front, rather than the lowest.
2750b57cec5SDimitry Andric return getStartPoint(I1) > getStartPoint(I2);
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric
lowestEndPoint(const IntervalInfo & I1,const IntervalInfo & I2)2780b57cec5SDimitry Andric static bool lowestEndPoint(const IntervalInfo &I1,
2790b57cec5SDimitry Andric const IntervalInfo &I2) {
2800b57cec5SDimitry Andric SlotIndex E1 = getEndPoint(I1);
2810b57cec5SDimitry Andric SlotIndex E2 = getEndPoint(I2);
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric if (E1 < E2)
2840b57cec5SDimitry Andric return true;
2850b57cec5SDimitry Andric
2860b57cec5SDimitry Andric if (E1 > E2)
2870b57cec5SDimitry Andric return false;
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric // If two intervals end at the same point, we need a way to break the tie or
2900b57cec5SDimitry Andric // the set will assume they're actually equal and refuse to insert a
2910b57cec5SDimitry Andric // "duplicate". Just compare the vregs - fast and guaranteed unique.
292e8d8bef9SDimitry Andric return std::get<0>(I1)->reg() < std::get<0>(I2)->reg();
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric
isAtLastSegment(const IntervalInfo & I)2950b57cec5SDimitry Andric static bool isAtLastSegment(const IntervalInfo &I) {
2960b57cec5SDimitry Andric return std::get<1>(I) == std::get<0>(I)->size() - 1;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric
nextSegment(const IntervalInfo & I)2990b57cec5SDimitry Andric static IntervalInfo nextSegment(const IntervalInfo &I) {
3000b57cec5SDimitry Andric return std::make_tuple(std::get<0>(I), std::get<1>(I) + 1, std::get<2>(I));
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric public:
apply(PBQPRAGraph & G)3040b57cec5SDimitry Andric void apply(PBQPRAGraph &G) override {
3050b57cec5SDimitry Andric // The following is loosely based on the linear scan algorithm introduced in
3060b57cec5SDimitry Andric // "Linear Scan Register Allocation" by Poletto and Sarkar. This version
3070b57cec5SDimitry Andric // isn't linear, because the size of the active set isn't bound by the
3080b57cec5SDimitry Andric // number of registers, but rather the size of the largest clique in the
3090b57cec5SDimitry Andric // graph. Still, we expect this to be better than N^2.
3100b57cec5SDimitry Andric LiveIntervals &LIS = G.getMetadata().LIS;
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric // Interferenc matrices are incredibly regular - they're only a function of
3130b57cec5SDimitry Andric // the allowed sets, so we cache them to avoid the overhead of constructing
3140b57cec5SDimitry Andric // and uniquing them.
3150b57cec5SDimitry Andric IMatrixCache C;
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric // Finding an edge is expensive in the worst case (O(max_clique(G))). So
3180b57cec5SDimitry Andric // cache locally edges we have already seen.
3190b57cec5SDimitry Andric IEdgeCache EC;
3200b57cec5SDimitry Andric
3210b57cec5SDimitry Andric // Cache known disjoint allowed registers pairs
3220b57cec5SDimitry Andric DisjointAllowedRegsCache D;
3230b57cec5SDimitry Andric
3240b57cec5SDimitry Andric using IntervalSet = std::set<IntervalInfo, decltype(&lowestEndPoint)>;
3250b57cec5SDimitry Andric using IntervalQueue =
3260b57cec5SDimitry Andric std::priority_queue<IntervalInfo, std::vector<IntervalInfo>,
3270b57cec5SDimitry Andric decltype(&lowestStartPoint)>;
3280b57cec5SDimitry Andric IntervalSet Active(lowestEndPoint);
3290b57cec5SDimitry Andric IntervalQueue Inactive(lowestStartPoint);
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andric // Start by building the inactive set.
3320b57cec5SDimitry Andric for (auto NId : G.nodeIds()) {
333e8d8bef9SDimitry Andric Register VReg = G.getNodeMetadata(NId).getVReg();
3340b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(VReg);
3350b57cec5SDimitry Andric assert(!LI.empty() && "PBQP graph contains node for empty interval");
3360b57cec5SDimitry Andric Inactive.push(std::make_tuple(&LI, 0, NId));
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric while (!Inactive.empty()) {
3400b57cec5SDimitry Andric // Tentatively grab the "next" interval - this choice may be overriden
3410b57cec5SDimitry Andric // below.
3420b57cec5SDimitry Andric IntervalInfo Cur = Inactive.top();
3430b57cec5SDimitry Andric
3440b57cec5SDimitry Andric // Retire any active intervals that end before Cur starts.
3450b57cec5SDimitry Andric IntervalSet::iterator RetireItr = Active.begin();
3460b57cec5SDimitry Andric while (RetireItr != Active.end() &&
3470b57cec5SDimitry Andric (getEndPoint(*RetireItr) <= getStartPoint(Cur))) {
3480b57cec5SDimitry Andric // If this interval has subsequent segments, add the next one to the
3490b57cec5SDimitry Andric // inactive list.
3500b57cec5SDimitry Andric if (!isAtLastSegment(*RetireItr))
3510b57cec5SDimitry Andric Inactive.push(nextSegment(*RetireItr));
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric ++RetireItr;
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric Active.erase(Active.begin(), RetireItr);
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric // One of the newly retired segments may actually start before the
3580b57cec5SDimitry Andric // Cur segment, so re-grab the front of the inactive list.
3590b57cec5SDimitry Andric Cur = Inactive.top();
3600b57cec5SDimitry Andric Inactive.pop();
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric // At this point we know that Cur overlaps all active intervals. Add the
3630b57cec5SDimitry Andric // interference edges.
3640b57cec5SDimitry Andric PBQP::GraphBase::NodeId NId = getNodeId(Cur);
3650b57cec5SDimitry Andric for (const auto &A : Active) {
3660b57cec5SDimitry Andric PBQP::GraphBase::NodeId MId = getNodeId(A);
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric // Do not add an edge when the nodes' allowed registers do not
3690b57cec5SDimitry Andric // intersect: there is obviously no interference.
3700b57cec5SDimitry Andric if (haveDisjointAllowedRegs(G, NId, MId, D))
3710b57cec5SDimitry Andric continue;
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric // Check that we haven't already added this edge
3740b57cec5SDimitry Andric IEdgeKey EK(std::min(NId, MId), std::max(NId, MId));
3750b57cec5SDimitry Andric if (EC.count(EK))
3760b57cec5SDimitry Andric continue;
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andric // This is a new edge - add it to the graph.
3790b57cec5SDimitry Andric if (!createInterferenceEdge(G, NId, MId, C))
3800b57cec5SDimitry Andric setDisjointAllowedRegs(G, NId, MId, D);
3810b57cec5SDimitry Andric else
3820b57cec5SDimitry Andric EC.insert(EK);
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric // Finally, add Cur to the Active set.
3860b57cec5SDimitry Andric Active.insert(Cur);
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric private:
3910b57cec5SDimitry Andric // Create an Interference edge and add it to the graph, unless it is
3920b57cec5SDimitry Andric // a null matrix, meaning the nodes' allowed registers do not have any
3930b57cec5SDimitry Andric // interference. This case occurs frequently between integer and floating
3940b57cec5SDimitry Andric // point registers for example.
3950b57cec5SDimitry Andric // return true iff both nodes interferes.
createInterferenceEdge(PBQPRAGraph & G,PBQPRAGraph::NodeId NId,PBQPRAGraph::NodeId MId,IMatrixCache & C)3960b57cec5SDimitry Andric bool createInterferenceEdge(PBQPRAGraph &G,
3970b57cec5SDimitry Andric PBQPRAGraph::NodeId NId, PBQPRAGraph::NodeId MId,
3980b57cec5SDimitry Andric IMatrixCache &C) {
3990b57cec5SDimitry Andric const TargetRegisterInfo &TRI =
4000b57cec5SDimitry Andric *G.getMetadata().MF.getSubtarget().getRegisterInfo();
4010b57cec5SDimitry Andric const auto &NRegs = G.getNodeMetadata(NId).getAllowedRegs();
4020b57cec5SDimitry Andric const auto &MRegs = G.getNodeMetadata(MId).getAllowedRegs();
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric // Try looking the edge costs up in the IMatrixCache first.
4050b57cec5SDimitry Andric IKey K(&NRegs, &MRegs);
4060b57cec5SDimitry Andric IMatrixCache::iterator I = C.find(K);
4070b57cec5SDimitry Andric if (I != C.end()) {
4080b57cec5SDimitry Andric G.addEdgeBypassingCostAllocator(NId, MId, I->second);
4090b57cec5SDimitry Andric return true;
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andric PBQPRAGraph::RawMatrix M(NRegs.size() + 1, MRegs.size() + 1, 0);
4130b57cec5SDimitry Andric bool NodesInterfere = false;
4140b57cec5SDimitry Andric for (unsigned I = 0; I != NRegs.size(); ++I) {
415e8d8bef9SDimitry Andric MCRegister PRegN = NRegs[I];
4160b57cec5SDimitry Andric for (unsigned J = 0; J != MRegs.size(); ++J) {
417e8d8bef9SDimitry Andric MCRegister PRegM = MRegs[J];
4180b57cec5SDimitry Andric if (TRI.regsOverlap(PRegN, PRegM)) {
4190b57cec5SDimitry Andric M[I + 1][J + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
4200b57cec5SDimitry Andric NodesInterfere = true;
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andric if (!NodesInterfere)
4260b57cec5SDimitry Andric return false;
4270b57cec5SDimitry Andric
4280b57cec5SDimitry Andric PBQPRAGraph::EdgeId EId = G.addEdge(NId, MId, std::move(M));
4290b57cec5SDimitry Andric C[K] = G.getEdgeCostsPtr(EId);
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andric return true;
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric };
4340b57cec5SDimitry Andric
4350b57cec5SDimitry Andric class Coalescing : public PBQPRAConstraint {
4360b57cec5SDimitry Andric public:
apply(PBQPRAGraph & G)4370b57cec5SDimitry Andric void apply(PBQPRAGraph &G) override {
4380b57cec5SDimitry Andric MachineFunction &MF = G.getMetadata().MF;
4390b57cec5SDimitry Andric MachineBlockFrequencyInfo &MBFI = G.getMetadata().MBFI;
4400b57cec5SDimitry Andric CoalescerPair CP(*MF.getSubtarget().getRegisterInfo());
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric // Scan the machine function and add a coalescing cost whenever CoalescerPair
4430b57cec5SDimitry Andric // gives the Ok.
4440b57cec5SDimitry Andric for (const auto &MBB : MF) {
4450b57cec5SDimitry Andric for (const auto &MI : MBB) {
4460b57cec5SDimitry Andric // Skip not-coalescable or already coalesced copies.
4470b57cec5SDimitry Andric if (!CP.setRegisters(&MI) || CP.getSrcReg() == CP.getDstReg())
4480b57cec5SDimitry Andric continue;
4490b57cec5SDimitry Andric
450e8d8bef9SDimitry Andric Register DstReg = CP.getDstReg();
451e8d8bef9SDimitry Andric Register SrcReg = CP.getSrcReg();
4520b57cec5SDimitry Andric
453e8d8bef9SDimitry Andric PBQP::PBQPNum CBenefit = MBFI.getBlockFreqRelativeToEntryBlock(&MBB);
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric if (CP.isPhys()) {
4560b57cec5SDimitry Andric if (!MF.getRegInfo().isAllocatable(DstReg))
4570b57cec5SDimitry Andric continue;
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric PBQPRAGraph::NodeId NId = G.getMetadata().getNodeIdForVReg(SrcReg);
4600b57cec5SDimitry Andric
4610b57cec5SDimitry Andric const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed =
4620b57cec5SDimitry Andric G.getNodeMetadata(NId).getAllowedRegs();
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric unsigned PRegOpt = 0;
465e8d8bef9SDimitry Andric while (PRegOpt < Allowed.size() && Allowed[PRegOpt].id() != DstReg)
4660b57cec5SDimitry Andric ++PRegOpt;
4670b57cec5SDimitry Andric
4680b57cec5SDimitry Andric if (PRegOpt < Allowed.size()) {
4690b57cec5SDimitry Andric PBQPRAGraph::RawVector NewCosts(G.getNodeCosts(NId));
4700b57cec5SDimitry Andric NewCosts[PRegOpt + 1] -= CBenefit;
4710b57cec5SDimitry Andric G.setNodeCosts(NId, std::move(NewCosts));
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric } else {
4740b57cec5SDimitry Andric PBQPRAGraph::NodeId N1Id = G.getMetadata().getNodeIdForVReg(DstReg);
4750b57cec5SDimitry Andric PBQPRAGraph::NodeId N2Id = G.getMetadata().getNodeIdForVReg(SrcReg);
4760b57cec5SDimitry Andric const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed1 =
4770b57cec5SDimitry Andric &G.getNodeMetadata(N1Id).getAllowedRegs();
4780b57cec5SDimitry Andric const PBQPRAGraph::NodeMetadata::AllowedRegVector *Allowed2 =
4790b57cec5SDimitry Andric &G.getNodeMetadata(N2Id).getAllowedRegs();
4800b57cec5SDimitry Andric
4810b57cec5SDimitry Andric PBQPRAGraph::EdgeId EId = G.findEdge(N1Id, N2Id);
4820b57cec5SDimitry Andric if (EId == G.invalidEdgeId()) {
4830b57cec5SDimitry Andric PBQPRAGraph::RawMatrix Costs(Allowed1->size() + 1,
4840b57cec5SDimitry Andric Allowed2->size() + 1, 0);
4850b57cec5SDimitry Andric addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit);
4860b57cec5SDimitry Andric G.addEdge(N1Id, N2Id, std::move(Costs));
4870b57cec5SDimitry Andric } else {
4880b57cec5SDimitry Andric if (G.getEdgeNode1Id(EId) == N2Id) {
4890b57cec5SDimitry Andric std::swap(N1Id, N2Id);
4900b57cec5SDimitry Andric std::swap(Allowed1, Allowed2);
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric PBQPRAGraph::RawMatrix Costs(G.getEdgeCosts(EId));
4930b57cec5SDimitry Andric addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit);
4940b57cec5SDimitry Andric G.updateEdgeCosts(EId, std::move(Costs));
4950b57cec5SDimitry Andric }
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric }
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andric private:
addVirtRegCoalesce(PBQPRAGraph::RawMatrix & CostMat,const PBQPRAGraph::NodeMetadata::AllowedRegVector & Allowed1,const PBQPRAGraph::NodeMetadata::AllowedRegVector & Allowed2,PBQP::PBQPNum Benefit)5020b57cec5SDimitry Andric void addVirtRegCoalesce(
5030b57cec5SDimitry Andric PBQPRAGraph::RawMatrix &CostMat,
5040b57cec5SDimitry Andric const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed1,
5050b57cec5SDimitry Andric const PBQPRAGraph::NodeMetadata::AllowedRegVector &Allowed2,
5060b57cec5SDimitry Andric PBQP::PBQPNum Benefit) {
5070b57cec5SDimitry Andric assert(CostMat.getRows() == Allowed1.size() + 1 && "Size mismatch.");
5080b57cec5SDimitry Andric assert(CostMat.getCols() == Allowed2.size() + 1 && "Size mismatch.");
5090b57cec5SDimitry Andric for (unsigned I = 0; I != Allowed1.size(); ++I) {
510e8d8bef9SDimitry Andric MCRegister PReg1 = Allowed1[I];
5110b57cec5SDimitry Andric for (unsigned J = 0; J != Allowed2.size(); ++J) {
512e8d8bef9SDimitry Andric MCRegister PReg2 = Allowed2[J];
5130b57cec5SDimitry Andric if (PReg1 == PReg2)
5140b57cec5SDimitry Andric CostMat[I + 1][J + 1] -= Benefit;
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric };
5190b57cec5SDimitry Andric
520e8d8bef9SDimitry Andric /// PBQP-specific implementation of weight normalization.
521e8d8bef9SDimitry Andric class PBQPVirtRegAuxInfo final : public VirtRegAuxInfo {
normalize(float UseDefFreq,unsigned Size,unsigned NumInstr)522e8d8bef9SDimitry Andric float normalize(float UseDefFreq, unsigned Size, unsigned NumInstr) override {
523e8d8bef9SDimitry Andric // All intervals have a spill weight that is mostly proportional to the
524e8d8bef9SDimitry Andric // number of uses, with uses in loops having a bigger weight.
525e8d8bef9SDimitry Andric return NumInstr * VirtRegAuxInfo::normalize(UseDefFreq, Size, 1);
526e8d8bef9SDimitry Andric }
527e8d8bef9SDimitry Andric
528e8d8bef9SDimitry Andric public:
PBQPVirtRegAuxInfo(MachineFunction & MF,LiveIntervals & LIS,VirtRegMap & VRM,const MachineLoopInfo & Loops,const MachineBlockFrequencyInfo & MBFI)529e8d8bef9SDimitry Andric PBQPVirtRegAuxInfo(MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM,
530e8d8bef9SDimitry Andric const MachineLoopInfo &Loops,
531e8d8bef9SDimitry Andric const MachineBlockFrequencyInfo &MBFI)
532e8d8bef9SDimitry Andric : VirtRegAuxInfo(MF, LIS, VRM, Loops, MBFI) {}
533e8d8bef9SDimitry Andric };
5340b57cec5SDimitry Andric } // end anonymous namespace
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andric // Out-of-line destructor/anchor for PBQPRAConstraint.
5370b57cec5SDimitry Andric PBQPRAConstraint::~PBQPRAConstraint() = default;
5380b57cec5SDimitry Andric
anchor()5390b57cec5SDimitry Andric void PBQPRAConstraint::anchor() {}
5400b57cec5SDimitry Andric
anchor()5410b57cec5SDimitry Andric void PBQPRAConstraintList::anchor() {}
5420b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & au) const5430b57cec5SDimitry Andric void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
5440b57cec5SDimitry Andric au.setPreservesCFG();
5450b57cec5SDimitry Andric au.addRequired<AAResultsWrapperPass>();
5460b57cec5SDimitry Andric au.addPreserved<AAResultsWrapperPass>();
547*0fca6ea1SDimitry Andric au.addRequired<SlotIndexesWrapperPass>();
548*0fca6ea1SDimitry Andric au.addPreserved<SlotIndexesWrapperPass>();
549*0fca6ea1SDimitry Andric au.addRequired<LiveIntervalsWrapperPass>();
550*0fca6ea1SDimitry Andric au.addPreserved<LiveIntervalsWrapperPass>();
5510b57cec5SDimitry Andric //au.addRequiredID(SplitCriticalEdgesID);
5520b57cec5SDimitry Andric if (customPassID)
5530b57cec5SDimitry Andric au.addRequiredID(*customPassID);
5540b57cec5SDimitry Andric au.addRequired<LiveStacks>();
5550b57cec5SDimitry Andric au.addPreserved<LiveStacks>();
556*0fca6ea1SDimitry Andric au.addRequired<MachineBlockFrequencyInfoWrapperPass>();
557*0fca6ea1SDimitry Andric au.addPreserved<MachineBlockFrequencyInfoWrapperPass>();
558*0fca6ea1SDimitry Andric au.addRequired<MachineLoopInfoWrapperPass>();
559*0fca6ea1SDimitry Andric au.addPreserved<MachineLoopInfoWrapperPass>();
560*0fca6ea1SDimitry Andric au.addRequired<MachineDominatorTreeWrapperPass>();
561*0fca6ea1SDimitry Andric au.addPreserved<MachineDominatorTreeWrapperPass>();
5620b57cec5SDimitry Andric au.addRequired<VirtRegMap>();
5630b57cec5SDimitry Andric au.addPreserved<VirtRegMap>();
5640b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(au);
5650b57cec5SDimitry Andric }
5660b57cec5SDimitry Andric
findVRegIntervalsToAlloc(const MachineFunction & MF,LiveIntervals & LIS)5670b57cec5SDimitry Andric void RegAllocPBQP::findVRegIntervalsToAlloc(const MachineFunction &MF,
5680b57cec5SDimitry Andric LiveIntervals &LIS) {
5690b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric // Iterate over all live ranges.
5720b57cec5SDimitry Andric for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
573e8d8bef9SDimitry Andric Register Reg = Register::index2VirtReg(I);
5740b57cec5SDimitry Andric if (MRI.reg_nodbg_empty(Reg))
5750b57cec5SDimitry Andric continue;
5760b57cec5SDimitry Andric VRegsToAlloc.insert(Reg);
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric }
5790b57cec5SDimitry Andric
isACalleeSavedRegister(MCRegister Reg,const TargetRegisterInfo & TRI,const MachineFunction & MF)580e8d8bef9SDimitry Andric static bool isACalleeSavedRegister(MCRegister Reg,
581e8d8bef9SDimitry Andric const TargetRegisterInfo &TRI,
5820b57cec5SDimitry Andric const MachineFunction &MF) {
5830b57cec5SDimitry Andric const MCPhysReg *CSR = MF.getRegInfo().getCalleeSavedRegs();
5840b57cec5SDimitry Andric for (unsigned i = 0; CSR[i] != 0; ++i)
585e8d8bef9SDimitry Andric if (TRI.regsOverlap(Reg, CSR[i]))
5860b57cec5SDimitry Andric return true;
5870b57cec5SDimitry Andric return false;
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric
initializeGraph(PBQPRAGraph & G,VirtRegMap & VRM,Spiller & VRegSpiller)5900b57cec5SDimitry Andric void RegAllocPBQP::initializeGraph(PBQPRAGraph &G, VirtRegMap &VRM,
5910b57cec5SDimitry Andric Spiller &VRegSpiller) {
5920b57cec5SDimitry Andric MachineFunction &MF = G.getMetadata().MF;
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric LiveIntervals &LIS = G.getMetadata().LIS;
5950b57cec5SDimitry Andric const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo();
5960b57cec5SDimitry Andric const TargetRegisterInfo &TRI =
5970b57cec5SDimitry Andric *G.getMetadata().MF.getSubtarget().getRegisterInfo();
5980b57cec5SDimitry Andric
599e8d8bef9SDimitry Andric std::vector<Register> Worklist(VRegsToAlloc.begin(), VRegsToAlloc.end());
6000b57cec5SDimitry Andric
601e8d8bef9SDimitry Andric std::map<Register, std::vector<MCRegister>> VRegAllowedMap;
6020b57cec5SDimitry Andric
6030b57cec5SDimitry Andric while (!Worklist.empty()) {
604e8d8bef9SDimitry Andric Register VReg = Worklist.back();
6050b57cec5SDimitry Andric Worklist.pop_back();
6060b57cec5SDimitry Andric
6070b57cec5SDimitry Andric LiveInterval &VRegLI = LIS.getInterval(VReg);
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric // If this is an empty interval move it to the EmptyIntervalVRegs set then
6100b57cec5SDimitry Andric // continue.
6110b57cec5SDimitry Andric if (VRegLI.empty()) {
612e8d8bef9SDimitry Andric EmptyIntervalVRegs.insert(VRegLI.reg());
613e8d8bef9SDimitry Andric VRegsToAlloc.erase(VRegLI.reg());
6140b57cec5SDimitry Andric continue;
6150b57cec5SDimitry Andric }
6160b57cec5SDimitry Andric
6170b57cec5SDimitry Andric const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
6180b57cec5SDimitry Andric
6190b57cec5SDimitry Andric // Record any overlaps with regmask operands.
6200b57cec5SDimitry Andric BitVector RegMaskOverlaps;
6210b57cec5SDimitry Andric LIS.checkRegMaskInterference(VRegLI, RegMaskOverlaps);
6220b57cec5SDimitry Andric
6230b57cec5SDimitry Andric // Compute an initial allowed set for the current vreg.
624e8d8bef9SDimitry Andric std::vector<MCRegister> VRegAllowed;
6250b57cec5SDimitry Andric ArrayRef<MCPhysReg> RawPRegOrder = TRC->getRawAllocationOrder(MF);
6260eae32dcSDimitry Andric for (MCPhysReg R : RawPRegOrder) {
6270eae32dcSDimitry Andric MCRegister PReg(R);
6280b57cec5SDimitry Andric if (MRI.isReserved(PReg))
6290b57cec5SDimitry Andric continue;
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andric // vregLI crosses a regmask operand that clobbers preg.
6320b57cec5SDimitry Andric if (!RegMaskOverlaps.empty() && !RegMaskOverlaps.test(PReg))
6330b57cec5SDimitry Andric continue;
6340b57cec5SDimitry Andric
6350b57cec5SDimitry Andric // vregLI overlaps fixed regunit interference.
6360b57cec5SDimitry Andric bool Interference = false;
63706c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(PReg)) {
63806c3fb27SDimitry Andric if (VRegLI.overlaps(LIS.getRegUnit(Unit))) {
6390b57cec5SDimitry Andric Interference = true;
6400b57cec5SDimitry Andric break;
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric if (Interference)
6440b57cec5SDimitry Andric continue;
6450b57cec5SDimitry Andric
6460b57cec5SDimitry Andric // preg is usable for this virtual register.
6470b57cec5SDimitry Andric VRegAllowed.push_back(PReg);
6480b57cec5SDimitry Andric }
6490b57cec5SDimitry Andric
6500b57cec5SDimitry Andric // Check for vregs that have no allowed registers. These should be
6510b57cec5SDimitry Andric // pre-spilled and the new vregs added to the worklist.
6520b57cec5SDimitry Andric if (VRegAllowed.empty()) {
6535ffd83dbSDimitry Andric SmallVector<Register, 8> NewVRegs;
6540b57cec5SDimitry Andric spillVReg(VReg, NewVRegs, MF, LIS, VRM, VRegSpiller);
655e8d8bef9SDimitry Andric llvm::append_range(Worklist, NewVRegs);
6560b57cec5SDimitry Andric continue;
657e8d8bef9SDimitry Andric }
658e8d8bef9SDimitry Andric
659e8d8bef9SDimitry Andric VRegAllowedMap[VReg.id()] = std::move(VRegAllowed);
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric for (auto &KV : VRegAllowedMap) {
6630b57cec5SDimitry Andric auto VReg = KV.first;
6640b57cec5SDimitry Andric
6650b57cec5SDimitry Andric // Move empty intervals to the EmptyIntervalVReg set.
6660b57cec5SDimitry Andric if (LIS.getInterval(VReg).empty()) {
6670b57cec5SDimitry Andric EmptyIntervalVRegs.insert(VReg);
6680b57cec5SDimitry Andric VRegsToAlloc.erase(VReg);
6690b57cec5SDimitry Andric continue;
6700b57cec5SDimitry Andric }
6710b57cec5SDimitry Andric
6720b57cec5SDimitry Andric auto &VRegAllowed = KV.second;
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andric PBQPRAGraph::RawVector NodeCosts(VRegAllowed.size() + 1, 0);
6750b57cec5SDimitry Andric
6760b57cec5SDimitry Andric // Tweak cost of callee saved registers, as using then force spilling and
6770b57cec5SDimitry Andric // restoring them. This would only happen in the prologue / epilogue though.
6780b57cec5SDimitry Andric for (unsigned i = 0; i != VRegAllowed.size(); ++i)
6790b57cec5SDimitry Andric if (isACalleeSavedRegister(VRegAllowed[i], TRI, MF))
6800b57cec5SDimitry Andric NodeCosts[1 + i] += 1.0;
6810b57cec5SDimitry Andric
6820b57cec5SDimitry Andric PBQPRAGraph::NodeId NId = G.addNode(std::move(NodeCosts));
6830b57cec5SDimitry Andric G.getNodeMetadata(NId).setVReg(VReg);
6840b57cec5SDimitry Andric G.getNodeMetadata(NId).setAllowedRegs(
6850b57cec5SDimitry Andric G.getMetadata().getAllowedRegs(std::move(VRegAllowed)));
6860b57cec5SDimitry Andric G.getMetadata().setNodeIdForVReg(VReg, NId);
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric
spillVReg(Register VReg,SmallVectorImpl<Register> & NewIntervals,MachineFunction & MF,LiveIntervals & LIS,VirtRegMap & VRM,Spiller & VRegSpiller)6905ffd83dbSDimitry Andric void RegAllocPBQP::spillVReg(Register VReg,
6915ffd83dbSDimitry Andric SmallVectorImpl<Register> &NewIntervals,
6920b57cec5SDimitry Andric MachineFunction &MF, LiveIntervals &LIS,
6930b57cec5SDimitry Andric VirtRegMap &VRM, Spiller &VRegSpiller) {
6940b57cec5SDimitry Andric VRegsToAlloc.erase(VReg);
6950b57cec5SDimitry Andric LiveRangeEdit LRE(&LIS.getInterval(VReg), NewIntervals, MF, LIS, &VRM,
6960b57cec5SDimitry Andric nullptr, &DeadRemats);
6970b57cec5SDimitry Andric VRegSpiller.spill(LRE);
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7000b57cec5SDimitry Andric (void)TRI;
7010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "VREG " << printReg(VReg, &TRI) << " -> SPILLED (Cost: "
702e8d8bef9SDimitry Andric << LRE.getParent().weight() << ", New vregs: ");
7030b57cec5SDimitry Andric
7040b57cec5SDimitry Andric // Copy any newly inserted live intervals into the list of regs to
7050b57cec5SDimitry Andric // allocate.
706fe6060f1SDimitry Andric for (const Register &R : LRE) {
707fe6060f1SDimitry Andric const LiveInterval &LI = LIS.getInterval(R);
7080b57cec5SDimitry Andric assert(!LI.empty() && "Empty spill range.");
709e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << printReg(LI.reg(), &TRI) << " ");
710e8d8bef9SDimitry Andric VRegsToAlloc.insert(LI.reg());
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric
7130b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ")\n");
7140b57cec5SDimitry Andric }
7150b57cec5SDimitry Andric
mapPBQPToRegAlloc(const PBQPRAGraph & G,const PBQP::Solution & Solution,VirtRegMap & VRM,Spiller & VRegSpiller)7160b57cec5SDimitry Andric bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAGraph &G,
7170b57cec5SDimitry Andric const PBQP::Solution &Solution,
7180b57cec5SDimitry Andric VirtRegMap &VRM,
7190b57cec5SDimitry Andric Spiller &VRegSpiller) {
7200b57cec5SDimitry Andric MachineFunction &MF = G.getMetadata().MF;
7210b57cec5SDimitry Andric LiveIntervals &LIS = G.getMetadata().LIS;
7220b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7230b57cec5SDimitry Andric (void)TRI;
7240b57cec5SDimitry Andric
7250b57cec5SDimitry Andric // Set to true if we have any spills
7260b57cec5SDimitry Andric bool AnotherRoundNeeded = false;
7270b57cec5SDimitry Andric
7280b57cec5SDimitry Andric // Clear the existing allocation.
7290b57cec5SDimitry Andric VRM.clearAllVirt();
7300b57cec5SDimitry Andric
7310b57cec5SDimitry Andric // Iterate over the nodes mapping the PBQP solution to a register
7320b57cec5SDimitry Andric // assignment.
7330b57cec5SDimitry Andric for (auto NId : G.nodeIds()) {
734e8d8bef9SDimitry Andric Register VReg = G.getNodeMetadata(NId).getVReg();
735e8d8bef9SDimitry Andric unsigned AllocOpt = Solution.getSelection(NId);
7360b57cec5SDimitry Andric
737e8d8bef9SDimitry Andric if (AllocOpt != PBQP::RegAlloc::getSpillOptionIdx()) {
738e8d8bef9SDimitry Andric MCRegister PReg = G.getNodeMetadata(NId).getAllowedRegs()[AllocOpt - 1];
7390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "VREG " << printReg(VReg, &TRI) << " -> "
7400b57cec5SDimitry Andric << TRI.getName(PReg) << "\n");
7410b57cec5SDimitry Andric assert(PReg != 0 && "Invalid preg selected.");
7420b57cec5SDimitry Andric VRM.assignVirt2Phys(VReg, PReg);
7430b57cec5SDimitry Andric } else {
7440b57cec5SDimitry Andric // Spill VReg. If this introduces new intervals we'll need another round
7450b57cec5SDimitry Andric // of allocation.
7465ffd83dbSDimitry Andric SmallVector<Register, 8> NewVRegs;
7470b57cec5SDimitry Andric spillVReg(VReg, NewVRegs, MF, LIS, VRM, VRegSpiller);
7480b57cec5SDimitry Andric AnotherRoundNeeded |= !NewVRegs.empty();
7490b57cec5SDimitry Andric }
7500b57cec5SDimitry Andric }
7510b57cec5SDimitry Andric
7520b57cec5SDimitry Andric return !AnotherRoundNeeded;
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric
finalizeAlloc(MachineFunction & MF,LiveIntervals & LIS,VirtRegMap & VRM) const7550b57cec5SDimitry Andric void RegAllocPBQP::finalizeAlloc(MachineFunction &MF,
7560b57cec5SDimitry Andric LiveIntervals &LIS,
7570b57cec5SDimitry Andric VirtRegMap &VRM) const {
7580b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
7590b57cec5SDimitry Andric
7600b57cec5SDimitry Andric // First allocate registers for the empty intervals.
761fe6060f1SDimitry Andric for (const Register &R : EmptyIntervalVRegs) {
762fe6060f1SDimitry Andric LiveInterval &LI = LIS.getInterval(R);
7630b57cec5SDimitry Andric
764e8d8bef9SDimitry Andric Register PReg = MRI.getSimpleHint(LI.reg());
7650b57cec5SDimitry Andric
7660b57cec5SDimitry Andric if (PReg == 0) {
767e8d8bef9SDimitry Andric const TargetRegisterClass &RC = *MRI.getRegClass(LI.reg());
7680b57cec5SDimitry Andric const ArrayRef<MCPhysReg> RawPRegOrder = RC.getRawAllocationOrder(MF);
769e8d8bef9SDimitry Andric for (MCRegister CandidateReg : RawPRegOrder) {
7700b57cec5SDimitry Andric if (!VRM.getRegInfo().isReserved(CandidateReg)) {
7710b57cec5SDimitry Andric PReg = CandidateReg;
7720b57cec5SDimitry Andric break;
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric }
7750b57cec5SDimitry Andric assert(PReg &&
7760b57cec5SDimitry Andric "No un-reserved physical registers in this register class");
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric
779e8d8bef9SDimitry Andric VRM.assignVirt2Phys(LI.reg(), PReg);
7800b57cec5SDimitry Andric }
7810b57cec5SDimitry Andric }
7820b57cec5SDimitry Andric
postOptimization(Spiller & VRegSpiller,LiveIntervals & LIS)7830b57cec5SDimitry Andric void RegAllocPBQP::postOptimization(Spiller &VRegSpiller, LiveIntervals &LIS) {
7840b57cec5SDimitry Andric VRegSpiller.postOptimization();
7850b57cec5SDimitry Andric /// Remove dead defs because of rematerialization.
786fcaf7f86SDimitry Andric for (auto *DeadInst : DeadRemats) {
7870b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(*DeadInst);
7880b57cec5SDimitry Andric DeadInst->eraseFromParent();
7890b57cec5SDimitry Andric }
7900b57cec5SDimitry Andric DeadRemats.clear();
7910b57cec5SDimitry Andric }
7920b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)7930b57cec5SDimitry Andric bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
794*0fca6ea1SDimitry Andric LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
7950b57cec5SDimitry Andric MachineBlockFrequencyInfo &MBFI =
796*0fca6ea1SDimitry Andric getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andric VirtRegMap &VRM = getAnalysis<VirtRegMap>();
7990b57cec5SDimitry Andric
800*0fca6ea1SDimitry Andric PBQPVirtRegAuxInfo VRAI(
801*0fca6ea1SDimitry Andric MF, LIS, VRM, getAnalysis<MachineLoopInfoWrapperPass>().getLI(), MBFI);
802e8d8bef9SDimitry Andric VRAI.calculateSpillWeightsAndHints();
8030b57cec5SDimitry Andric
804fe6060f1SDimitry Andric // FIXME: we create DefaultVRAI here to match existing behavior pre-passing
805fe6060f1SDimitry Andric // the VRAI through the spiller to the live range editor. However, it probably
806fe6060f1SDimitry Andric // makes more sense to pass the PBQP VRAI. The existing behavior had
807fe6060f1SDimitry Andric // LiveRangeEdit make its own VirtRegAuxInfo object.
808*0fca6ea1SDimitry Andric VirtRegAuxInfo DefaultVRAI(
809*0fca6ea1SDimitry Andric MF, LIS, VRM, getAnalysis<MachineLoopInfoWrapperPass>().getLI(), MBFI);
810fe6060f1SDimitry Andric std::unique_ptr<Spiller> VRegSpiller(
811fe6060f1SDimitry Andric createInlineSpiller(*this, MF, VRM, DefaultVRAI));
8120b57cec5SDimitry Andric
813*0fca6ea1SDimitry Andric MF.getRegInfo().freezeReservedRegs();
8140b57cec5SDimitry Andric
8150b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "PBQP Register Allocating for " << MF.getName() << "\n");
8160b57cec5SDimitry Andric
8170b57cec5SDimitry Andric // Allocator main loop:
8180b57cec5SDimitry Andric //
8190b57cec5SDimitry Andric // * Map current regalloc problem to a PBQP problem
8200b57cec5SDimitry Andric // * Solve the PBQP problem
8210b57cec5SDimitry Andric // * Map the solution back to a register allocation
8220b57cec5SDimitry Andric // * Spill if necessary
8230b57cec5SDimitry Andric //
8240b57cec5SDimitry Andric // This process is continued till no more spills are generated.
8250b57cec5SDimitry Andric
8260b57cec5SDimitry Andric // Find the vreg intervals in need of allocation.
8270b57cec5SDimitry Andric findVRegIntervalsToAlloc(MF, LIS);
8280b57cec5SDimitry Andric
8290b57cec5SDimitry Andric #ifndef NDEBUG
8300b57cec5SDimitry Andric const Function &F = MF.getFunction();
8310b57cec5SDimitry Andric std::string FullyQualifiedName =
8320b57cec5SDimitry Andric F.getParent()->getModuleIdentifier() + "." + F.getName().str();
8330b57cec5SDimitry Andric #endif
8340b57cec5SDimitry Andric
8350b57cec5SDimitry Andric // If there are non-empty intervals allocate them using pbqp.
8360b57cec5SDimitry Andric if (!VRegsToAlloc.empty()) {
8370b57cec5SDimitry Andric const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
8380b57cec5SDimitry Andric std::unique_ptr<PBQPRAConstraintList> ConstraintsRoot =
8398bcb0991SDimitry Andric std::make_unique<PBQPRAConstraintList>();
8408bcb0991SDimitry Andric ConstraintsRoot->addConstraint(std::make_unique<SpillCosts>());
8418bcb0991SDimitry Andric ConstraintsRoot->addConstraint(std::make_unique<Interference>());
8420b57cec5SDimitry Andric if (PBQPCoalescing)
8438bcb0991SDimitry Andric ConstraintsRoot->addConstraint(std::make_unique<Coalescing>());
8440b57cec5SDimitry Andric ConstraintsRoot->addConstraint(Subtarget.getCustomPBQPConstraints());
8450b57cec5SDimitry Andric
8460b57cec5SDimitry Andric bool PBQPAllocComplete = false;
8470b57cec5SDimitry Andric unsigned Round = 0;
8480b57cec5SDimitry Andric
8490b57cec5SDimitry Andric while (!PBQPAllocComplete) {
8500b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " PBQP Regalloc round " << Round << ":\n");
85181ad6265SDimitry Andric (void) Round;
8520b57cec5SDimitry Andric
8530b57cec5SDimitry Andric PBQPRAGraph G(PBQPRAGraph::GraphMetadata(MF, LIS, MBFI));
8540b57cec5SDimitry Andric initializeGraph(G, VRM, *VRegSpiller);
8550b57cec5SDimitry Andric ConstraintsRoot->apply(G);
8560b57cec5SDimitry Andric
8570b57cec5SDimitry Andric #ifndef NDEBUG
8580b57cec5SDimitry Andric if (PBQPDumpGraphs) {
8590b57cec5SDimitry Andric std::ostringstream RS;
8600b57cec5SDimitry Andric RS << Round;
8610b57cec5SDimitry Andric std::string GraphFileName = FullyQualifiedName + "." + RS.str() +
8620b57cec5SDimitry Andric ".pbqpgraph";
8630b57cec5SDimitry Andric std::error_code EC;
864fe6060f1SDimitry Andric raw_fd_ostream OS(GraphFileName, EC, sys::fs::OF_TextWithCRLF);
8650b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Dumping graph for round " << Round << " to \""
8660b57cec5SDimitry Andric << GraphFileName << "\"\n");
8670b57cec5SDimitry Andric G.dump(OS);
8680b57cec5SDimitry Andric }
8690b57cec5SDimitry Andric #endif
8700b57cec5SDimitry Andric
8710b57cec5SDimitry Andric PBQP::Solution Solution = PBQP::RegAlloc::solve(G);
8720b57cec5SDimitry Andric PBQPAllocComplete = mapPBQPToRegAlloc(G, Solution, VRM, *VRegSpiller);
8730b57cec5SDimitry Andric ++Round;
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric }
8760b57cec5SDimitry Andric
8770b57cec5SDimitry Andric // Finalise allocation, allocate empty ranges.
8780b57cec5SDimitry Andric finalizeAlloc(MF, LIS, VRM);
8790b57cec5SDimitry Andric postOptimization(*VRegSpiller, LIS);
8800b57cec5SDimitry Andric VRegsToAlloc.clear();
8810b57cec5SDimitry Andric EmptyIntervalVRegs.clear();
8820b57cec5SDimitry Andric
8830b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << VRM << "\n");
8840b57cec5SDimitry Andric
8850b57cec5SDimitry Andric return true;
8860b57cec5SDimitry Andric }
8870b57cec5SDimitry Andric
8880b57cec5SDimitry Andric /// Create Printable object for node and register info.
PrintNodeInfo(PBQP::RegAlloc::PBQPRAGraph::NodeId NId,const PBQP::RegAlloc::PBQPRAGraph & G)8890b57cec5SDimitry Andric static Printable PrintNodeInfo(PBQP::RegAlloc::PBQPRAGraph::NodeId NId,
8900b57cec5SDimitry Andric const PBQP::RegAlloc::PBQPRAGraph &G) {
8910b57cec5SDimitry Andric return Printable([NId, &G](raw_ostream &OS) {
8920b57cec5SDimitry Andric const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo();
8930b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
894e8d8bef9SDimitry Andric Register VReg = G.getNodeMetadata(NId).getVReg();
8950b57cec5SDimitry Andric const char *RegClassName = TRI->getRegClassName(MRI.getRegClass(VReg));
8960b57cec5SDimitry Andric OS << NId << " (" << RegClassName << ':' << printReg(VReg, TRI) << ')';
8970b57cec5SDimitry Andric });
8980b57cec5SDimitry Andric }
8990b57cec5SDimitry Andric
9000b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(raw_ostream & OS) const9010b57cec5SDimitry Andric LLVM_DUMP_METHOD void PBQP::RegAlloc::PBQPRAGraph::dump(raw_ostream &OS) const {
9020b57cec5SDimitry Andric for (auto NId : nodeIds()) {
9030b57cec5SDimitry Andric const Vector &Costs = getNodeCosts(NId);
9040b57cec5SDimitry Andric assert(Costs.getLength() != 0 && "Empty vector in graph.");
9050b57cec5SDimitry Andric OS << PrintNodeInfo(NId, *this) << ": " << Costs << '\n';
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric OS << '\n';
9080b57cec5SDimitry Andric
9090b57cec5SDimitry Andric for (auto EId : edgeIds()) {
9100b57cec5SDimitry Andric NodeId N1Id = getEdgeNode1Id(EId);
9110b57cec5SDimitry Andric NodeId N2Id = getEdgeNode2Id(EId);
9120b57cec5SDimitry Andric assert(N1Id != N2Id && "PBQP graphs should not have self-edges.");
9130b57cec5SDimitry Andric const Matrix &M = getEdgeCosts(EId);
9140b57cec5SDimitry Andric assert(M.getRows() != 0 && "No rows in matrix.");
9150b57cec5SDimitry Andric assert(M.getCols() != 0 && "No cols in matrix.");
9160b57cec5SDimitry Andric OS << PrintNodeInfo(N1Id, *this) << ' ' << M.getRows() << " rows / ";
9170b57cec5SDimitry Andric OS << PrintNodeInfo(N2Id, *this) << ' ' << M.getCols() << " cols:\n";
9180b57cec5SDimitry Andric OS << M << '\n';
9190b57cec5SDimitry Andric }
9200b57cec5SDimitry Andric }
9210b57cec5SDimitry Andric
dump() const9220b57cec5SDimitry Andric LLVM_DUMP_METHOD void PBQP::RegAlloc::PBQPRAGraph::dump() const {
9230b57cec5SDimitry Andric dump(dbgs());
9240b57cec5SDimitry Andric }
9250b57cec5SDimitry Andric #endif
9260b57cec5SDimitry Andric
printDot(raw_ostream & OS) const9270b57cec5SDimitry Andric void PBQP::RegAlloc::PBQPRAGraph::printDot(raw_ostream &OS) const {
9280b57cec5SDimitry Andric OS << "graph {\n";
9290b57cec5SDimitry Andric for (auto NId : nodeIds()) {
9300b57cec5SDimitry Andric OS << " node" << NId << " [ label=\""
9310b57cec5SDimitry Andric << PrintNodeInfo(NId, *this) << "\\n"
9320b57cec5SDimitry Andric << getNodeCosts(NId) << "\" ]\n";
9330b57cec5SDimitry Andric }
9340b57cec5SDimitry Andric
9350b57cec5SDimitry Andric OS << " edge [ len=" << nodeIds().size() << " ]\n";
9360b57cec5SDimitry Andric for (auto EId : edgeIds()) {
9370b57cec5SDimitry Andric OS << " node" << getEdgeNode1Id(EId)
9380b57cec5SDimitry Andric << " -- node" << getEdgeNode2Id(EId)
9390b57cec5SDimitry Andric << " [ label=\"";
9400b57cec5SDimitry Andric const Matrix &EdgeCosts = getEdgeCosts(EId);
9410b57cec5SDimitry Andric for (unsigned i = 0; i < EdgeCosts.getRows(); ++i) {
9420b57cec5SDimitry Andric OS << EdgeCosts.getRowAsVector(i) << "\\n";
9430b57cec5SDimitry Andric }
9440b57cec5SDimitry Andric OS << "\" ]\n";
9450b57cec5SDimitry Andric }
9460b57cec5SDimitry Andric OS << "}\n";
9470b57cec5SDimitry Andric }
9480b57cec5SDimitry Andric
createPBQPRegisterAllocator(char * customPassID)9490b57cec5SDimitry Andric FunctionPass *llvm::createPBQPRegisterAllocator(char *customPassID) {
9500b57cec5SDimitry Andric return new RegAllocPBQP(customPassID);
9510b57cec5SDimitry Andric }
9520b57cec5SDimitry Andric
createDefaultPBQPRegisterAllocator()9530b57cec5SDimitry Andric FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
9540b57cec5SDimitry Andric return createPBQPRegisterAllocator();
9550b57cec5SDimitry Andric }
956