2008-10-02 18:29:27 +00:00
|
|
|
//===------ RegAllocPBQP.cpp ---- PBQP Register Allocator -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-01-08 15:50:22 +00:00
|
|
|
//
|
2008-10-02 18:29:27 +00:00
|
|
|
// This file contains a Partitioned Boolean Quadratic Programming (PBQP) based
|
|
|
|
// register allocator for LLVM. This allocator works by constructing a PBQP
|
|
|
|
// problem representing the register allocation problem under consideration,
|
|
|
|
// solving this using a PBQP solver, and mapping the solution back to a
|
|
|
|
// register assignment. If any variables are selected for spilling then spill
|
2009-01-08 15:50:22 +00:00
|
|
|
// code is inserted and the process repeated.
|
2008-10-02 18:29:27 +00:00
|
|
|
//
|
|
|
|
// The PBQP solver (pbqp.c) provided for this allocator uses a heuristic tuned
|
|
|
|
// for register allocation. For more information on PBQP for register
|
2009-01-08 16:40:25 +00:00
|
|
|
// allocation, see the following papers:
|
2008-10-02 18:29:27 +00:00
|
|
|
//
|
|
|
|
// (1) Hames, L. and Scholz, B. 2006. Nearly optimal register allocation with
|
|
|
|
// PBQP. In Proceedings of the 7th Joint Modular Languages Conference
|
|
|
|
// (JMLC'06). LNCS, vol. 4228. Springer, New York, NY, USA. 346-361.
|
|
|
|
//
|
|
|
|
// (2) Scholz, B., Eckstein, E. 2002. Register allocation for irregular
|
|
|
|
// architectures. In Proceedings of the Joint Conference on Languages,
|
|
|
|
// Compilers and Tools for Embedded Systems (LCTES'02), ACM Press, New York,
|
|
|
|
// NY, USA, 139-148.
|
2009-01-08 15:50:22 +00:00
|
|
|
//
|
2008-10-02 18:29:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/RegAllocPBQP.h"
|
2011-06-26 21:41:06 +00:00
|
|
|
#include "RegisterCoalescer.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "Spiller.h"
|
2011-12-06 01:45:57 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2009-12-14 06:49:42 +00:00
|
|
|
#include "llvm/CodeGen/CalcSpillWeights.h"
|
2008-10-02 18:29:27 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2012-04-02 22:44:18 +00:00
|
|
|
#include "llvm/CodeGen/LiveRangeEdit.h"
|
2008-11-16 12:12:54 +00:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2013-06-17 19:00:36 +00:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2011-12-06 01:45:57 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2009-01-08 15:50:22 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2013-07-01 20:47:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2009-01-08 15:50:22 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2012-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2008-10-02 18:29:27 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-01-08 15:50:22 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2009-01-08 15:50:22 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
2014-10-18 17:26:07 +00:00
|
|
|
#include <queue>
|
2008-10-02 18:29:27 +00:00
|
|
|
#include <set>
|
2012-03-26 23:07:23 +00:00
|
|
|
#include <sstream>
|
2008-10-02 18:29:27 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2010-09-23 04:28:54 +00:00
|
|
|
using namespace llvm;
|
2010-09-18 09:07:10 +00:00
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2008-10-02 18:29:27 +00:00
|
|
|
static RegisterRegAlloc
|
2014-10-09 18:20:51 +00:00
|
|
|
RegisterPBQPRepAlloc("pbqp", "PBQP register allocator",
|
2010-09-23 04:28:54 +00:00
|
|
|
createDefaultPBQPRegisterAllocator);
|
2008-10-02 18:29:27 +00:00
|
|
|
|
2009-08-19 01:36:14 +00:00
|
|
|
static cl::opt<bool>
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQPCoalescing("pbqp-coalescing",
|
2010-01-26 04:49:58 +00:00
|
|
|
cl::desc("Attempt coalescing during PBQP register allocation."),
|
|
|
|
cl::init(false), cl::Hidden);
|
2009-08-19 01:36:14 +00:00
|
|
|
|
2012-03-26 23:07:23 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static cl::opt<bool>
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQPDumpGraphs("pbqp-dump-graphs",
|
2012-03-26 23:07:23 +00:00
|
|
|
cl::desc("Dump graphs for each function/round in the compilation unit."),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
#endif
|
|
|
|
|
2010-09-23 04:28:54 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
///
|
|
|
|
/// PBQP based allocators solve the register allocation problem by mapping
|
|
|
|
/// register allocation problems to Partitioned Boolean Quadratic
|
|
|
|
/// Programming problems.
|
|
|
|
class RegAllocPBQP : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
/// Construct a PBQP register allocator.
|
2014-10-09 18:20:51 +00:00
|
|
|
RegAllocPBQP(char *cPassID = nullptr)
|
|
|
|
: MachineFunctionPass(ID), customPassID(cPassID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeLiveStacksPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
/// Return the pass name.
|
2014-03-07 09:26:03 +00:00
|
|
|
const char* getPassName() const override {
|
2010-09-23 04:28:54 +00:00
|
|
|
return "PBQP Register Allocator";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PBQP analysis usage.
|
2014-03-07 09:26:03 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &au) const override;
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
/// Perform register allocation
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
typedef std::map<const LiveInterval*, unsigned> LI2NodeMap;
|
|
|
|
typedef std::vector<const LiveInterval*> Node2LIMap;
|
|
|
|
typedef std::vector<unsigned> AllowedSet;
|
|
|
|
typedef std::vector<AllowedSet> AllowedSetMap;
|
|
|
|
typedef std::pair<unsigned, unsigned> RegPair;
|
|
|
|
typedef std::map<RegPair, PBQP::PBQPNum> CoalesceMap;
|
|
|
|
typedef std::set<unsigned> RegSet;
|
|
|
|
|
2011-06-17 07:09:01 +00:00
|
|
|
char *customPassID;
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
RegSet VRegsToAlloc, EmptyIntervalVRegs;
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
/// \brief Finds the initial set of vreg intervals to allocate.
|
2014-10-09 18:20:51 +00:00
|
|
|
void findVRegIntervalsToAlloc(const MachineFunction &MF, LiveIntervals &LIS);
|
|
|
|
|
|
|
|
/// \brief Constructs an initial graph.
|
|
|
|
void initializeGraph(PBQPRAGraph &G);
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
/// \brief Given a solved PBQP problem maps this solution back to a register
|
|
|
|
/// assignment.
|
2014-10-09 18:20:51 +00:00
|
|
|
bool mapPBQPToRegAlloc(const PBQPRAGraph &G,
|
|
|
|
const PBQP::Solution &Solution,
|
|
|
|
VirtRegMap &VRM,
|
|
|
|
Spiller &VRegSpiller);
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
/// \brief Postprocessing before final spilling. Sets basic block "live in"
|
|
|
|
/// variables.
|
2014-10-09 18:20:51 +00:00
|
|
|
void finalizeAlloc(MachineFunction &MF, LiveIntervals &LIS,
|
|
|
|
VirtRegMap &VRM) const;
|
2010-09-23 04:28:54 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-09-18 09:07:10 +00:00
|
|
|
char RegAllocPBQP::ID = 0;
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
/// @brief Set spill costs for each node in the PBQP reg-alloc graph.
|
|
|
|
class SpillCosts : public PBQPRAConstraint {
|
|
|
|
public:
|
|
|
|
void apply(PBQPRAGraph &G) override {
|
|
|
|
LiveIntervals &LIS = G.getMetadata().LIS;
|
|
|
|
|
|
|
|
for (auto NId : G.nodeIds()) {
|
|
|
|
PBQP::PBQPNum SpillCost =
|
|
|
|
LIS.getInterval(G.getNodeMetadata(NId).getVReg()).weight;
|
|
|
|
if (SpillCost == 0.0)
|
|
|
|
SpillCost = std::numeric_limits<PBQP::PBQPNum>::min();
|
|
|
|
PBQPRAGraph::RawVector NodeCosts(G.getNodeCosts(NId));
|
|
|
|
NodeCosts[PBQP::RegAlloc::getSpillOptionIdx()] = SpillCost;
|
|
|
|
G.setNodeCosts(NId, std::move(NodeCosts));
|
|
|
|
}
|
2010-09-18 09:07:10 +00:00
|
|
|
}
|
2014-10-09 18:20:51 +00:00
|
|
|
};
|
2010-09-18 09:07:10 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
/// @brief Add interference edges between overlapping vregs.
|
|
|
|
class Interference : public PBQPRAConstraint {
|
2014-10-18 17:26:07 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
// Holds (Interval, CurrentSegmentID, and NodeId). The first two are required
|
|
|
|
// for the fast interference graph construction algorithm. The last is there
|
|
|
|
// to save us from looking up node ids via the VRegToNode map in the graph
|
|
|
|
// metadata.
|
|
|
|
typedef std::tuple<LiveInterval*, size_t, PBQP::GraphBase::NodeId>
|
|
|
|
IntervalInfo;
|
|
|
|
|
|
|
|
static SlotIndex getStartPoint(const IntervalInfo &I) {
|
|
|
|
return std::get<0>(I)->segments[std::get<1>(I)].start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SlotIndex getEndPoint(const IntervalInfo &I) {
|
|
|
|
return std::get<0>(I)->segments[std::get<1>(I)].end;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PBQP::GraphBase::NodeId getNodeId(const IntervalInfo &I) {
|
|
|
|
return std::get<2>(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lowestStartPoint(const IntervalInfo &I1,
|
|
|
|
const IntervalInfo &I2) {
|
|
|
|
// Condition reversed because priority queue has the *highest* element at
|
|
|
|
// the front, rather than the lowest.
|
|
|
|
return getStartPoint(I1) > getStartPoint(I2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lowestEndPoint(const IntervalInfo &I1,
|
|
|
|
const IntervalInfo &I2) {
|
|
|
|
SlotIndex E1 = getEndPoint(I1);
|
|
|
|
SlotIndex E2 = getEndPoint(I2);
|
|
|
|
|
|
|
|
if (E1 < E2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (E1 > E2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If two intervals end at the same point, we need a way to break the tie or
|
|
|
|
// the set will assume they're actually equal and refuse to insert a
|
|
|
|
// "duplicate". Just compare the vregs - fast and guaranteed unique.
|
|
|
|
return std::get<0>(I1)->reg < std::get<0>(I2)->reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAtLastSegment(const IntervalInfo &I) {
|
|
|
|
return std::get<1>(I) == std::get<0>(I)->size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntervalInfo nextSegment(const IntervalInfo &I) {
|
|
|
|
return std::make_tuple(std::get<0>(I), std::get<1>(I) + 1, std::get<2>(I));
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
public:
|
2012-06-20 22:32:05 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
void apply(PBQPRAGraph &G) override {
|
2014-10-18 17:26:07 +00:00
|
|
|
// The following is loosely based on the linear scan algorithm introduced in
|
|
|
|
// "Linear Scan Register Allocation" by Poletto and Sarkar. This version
|
|
|
|
// isn't linear, because the size of the active set isn't bound by the
|
|
|
|
// number of registers, but rather the size of the largest clique in the
|
|
|
|
// graph. Still, we expect this to be better than N^2.
|
2014-10-09 18:20:51 +00:00
|
|
|
LiveIntervals &LIS = G.getMetadata().LIS;
|
|
|
|
const TargetRegisterInfo &TRI =
|
|
|
|
*G.getMetadata().MF.getTarget().getSubtargetImpl()->getRegisterInfo();
|
|
|
|
|
2014-10-18 17:26:07 +00:00
|
|
|
typedef std::set<IntervalInfo, decltype(&lowestEndPoint)> IntervalSet;
|
|
|
|
typedef std::priority_queue<IntervalInfo, std::vector<IntervalInfo>,
|
|
|
|
decltype(&lowestStartPoint)> IntervalQueue;
|
|
|
|
IntervalSet Active(lowestEndPoint);
|
|
|
|
IntervalQueue Inactive(lowestStartPoint);
|
|
|
|
|
|
|
|
// Start by building the inactive set.
|
|
|
|
for (auto NId : G.nodeIds()) {
|
|
|
|
unsigned VReg = G.getNodeMetadata(NId).getVReg();
|
|
|
|
LiveInterval &LI = LIS.getInterval(VReg);
|
|
|
|
assert(!LI.empty() && "PBQP graph contains node for empty interval");
|
|
|
|
Inactive.push(std::make_tuple(&LI, 0, NId));
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!Inactive.empty()) {
|
|
|
|
// Tentatively grab the "next" interval - this choice may be overriden
|
|
|
|
// below.
|
|
|
|
IntervalInfo Cur = Inactive.top();
|
|
|
|
|
|
|
|
// Retire any active intervals that end before Cur starts.
|
|
|
|
IntervalSet::iterator RetireItr = Active.begin();
|
|
|
|
while (RetireItr != Active.end() &&
|
|
|
|
(getEndPoint(*RetireItr) <= getStartPoint(Cur))) {
|
|
|
|
// If this interval has subsequent segments, add the next one to the
|
|
|
|
// inactive list.
|
|
|
|
if (!isAtLastSegment(*RetireItr))
|
|
|
|
Inactive.push(nextSegment(*RetireItr));
|
|
|
|
|
|
|
|
++RetireItr;
|
|
|
|
}
|
|
|
|
Active.erase(Active.begin(), RetireItr);
|
|
|
|
|
|
|
|
// One of the newly retired segments may actually start before the
|
|
|
|
// Cur segment, so re-grab the front of the inactive list.
|
|
|
|
Cur = Inactive.top();
|
|
|
|
Inactive.pop();
|
|
|
|
|
|
|
|
// At this point we know that Cur overlaps all active intervals. Add the
|
|
|
|
// interference edges.
|
|
|
|
PBQP::GraphBase::NodeId NId = getNodeId(Cur);
|
|
|
|
for (const auto &A : Active) {
|
|
|
|
PBQP::GraphBase::NodeId MId = getNodeId(A);
|
|
|
|
|
|
|
|
// Check that we haven't already added this edge
|
|
|
|
// FIXME: findEdge is expensive in the worst case (O(max_clique(G))).
|
|
|
|
// It might be better to replace this with a local bit-matrix.
|
|
|
|
if (G.findEdge(NId, MId) != PBQP::GraphBase::invalidEdgeId())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// This is a new edge - add it to the graph.
|
|
|
|
const auto &NOpts = G.getNodeMetadata(NId).getOptionRegs();
|
|
|
|
const auto &MOpts = G.getNodeMetadata(MId).getOptionRegs();
|
|
|
|
G.addEdge(NId, MId, createInterferenceMatrix(TRI, NOpts, MOpts));
|
2010-09-18 09:07:10 +00:00
|
|
|
}
|
2014-10-18 17:26:07 +00:00
|
|
|
|
|
|
|
// Finally, add Cur to the Active set.
|
|
|
|
Active.insert(Cur);
|
2010-09-18 09:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
private:
|
2010-07-17 06:31:41 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQPRAGraph::RawMatrix createInterferenceMatrix(
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap &NOpts,
|
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap &MOpts) {
|
|
|
|
PBQPRAGraph::RawMatrix M(NOpts.size() + 1, MOpts.size() + 1, 0);
|
|
|
|
for (unsigned I = 0; I != NOpts.size(); ++I) {
|
|
|
|
unsigned PRegN = NOpts[I];
|
|
|
|
for (unsigned J = 0; J != MOpts.size(); ++J) {
|
|
|
|
unsigned PRegM = MOpts[J];
|
|
|
|
if (TRI.regsOverlap(PRegN, PRegM))
|
|
|
|
M[I + 1][J + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
|
2010-07-17 06:31:41 +00:00
|
|
|
}
|
2010-09-18 09:07:10 +00:00
|
|
|
}
|
2014-10-09 18:20:51 +00:00
|
|
|
|
|
|
|
return M;
|
2010-09-18 09:07:10 +00:00
|
|
|
}
|
2014-10-09 18:20:51 +00:00
|
|
|
};
|
2008-10-02 18:29:27 +00:00
|
|
|
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
class Coalescing : public PBQPRAConstraint {
|
|
|
|
public:
|
|
|
|
void apply(PBQPRAGraph &G) override {
|
|
|
|
MachineFunction &MF = G.getMetadata().MF;
|
|
|
|
MachineBlockFrequencyInfo &MBFI = G.getMetadata().MBFI;
|
|
|
|
CoalescerPair CP(*MF.getTarget().getSubtargetImpl()->getRegisterInfo());
|
|
|
|
|
|
|
|
// Scan the machine function and add a coalescing cost whenever CoalescerPair
|
|
|
|
// gives the Ok.
|
|
|
|
for (const auto &MBB : MF) {
|
|
|
|
for (const auto &MI : MBB) {
|
|
|
|
|
|
|
|
// Skip not-coalescable or already coalesced copies.
|
|
|
|
if (!CP.setRegisters(&MI) || CP.getSrcReg() == CP.getDstReg())
|
|
|
|
continue;
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
unsigned DstReg = CP.getDstReg();
|
|
|
|
unsigned SrcReg = CP.getSrcReg();
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
const float CopyFactor = 0.5; // Cost of copy relative to load. Current
|
|
|
|
// value plucked randomly out of the air.
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQP::PBQPNum CBenefit =
|
|
|
|
CopyFactor * LiveIntervals::getSpillWeight(false, true, &MBFI, &MI);
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
if (CP.isPhys()) {
|
|
|
|
if (!MF.getRegInfo().isAllocatable(DstReg))
|
|
|
|
continue;
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQPRAGraph::NodeId NId = G.getMetadata().getNodeIdForVReg(SrcReg);
|
2012-02-10 04:10:26 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap &Allowed =
|
|
|
|
G.getNodeMetadata(NId).getOptionRegs();
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
unsigned PRegOpt = 0;
|
|
|
|
while (PRegOpt < Allowed.size() && Allowed[PRegOpt] != DstReg)
|
|
|
|
++PRegOpt;
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
if (PRegOpt < Allowed.size()) {
|
|
|
|
PBQPRAGraph::RawVector NewCosts(G.getNodeCosts(NId));
|
|
|
|
NewCosts[PRegOpt + 1] += CBenefit;
|
|
|
|
G.setNodeCosts(NId, std::move(NewCosts));
|
|
|
|
}
|
2010-09-21 13:19:36 +00:00
|
|
|
} else {
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQPRAGraph::NodeId N1Id = G.getMetadata().getNodeIdForVReg(DstReg);
|
|
|
|
PBQPRAGraph::NodeId N2Id = G.getMetadata().getNodeIdForVReg(SrcReg);
|
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap *Allowed1 =
|
|
|
|
&G.getNodeMetadata(N1Id).getOptionRegs();
|
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap *Allowed2 =
|
|
|
|
&G.getNodeMetadata(N2Id).getOptionRegs();
|
|
|
|
|
|
|
|
PBQPRAGraph::EdgeId EId = G.findEdge(N1Id, N2Id);
|
|
|
|
if (EId == G.invalidEdgeId()) {
|
|
|
|
PBQPRAGraph::RawMatrix Costs(Allowed1->size() + 1,
|
|
|
|
Allowed2->size() + 1, 0);
|
|
|
|
addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit);
|
|
|
|
G.addEdge(N1Id, N2Id, std::move(Costs));
|
|
|
|
} else {
|
|
|
|
if (G.getEdgeNode1Id(EId) == N2Id) {
|
|
|
|
std::swap(N1Id, N2Id);
|
|
|
|
std::swap(Allowed1, Allowed2);
|
|
|
|
}
|
|
|
|
PBQPRAGraph::RawMatrix Costs(G.getEdgeCosts(EId));
|
|
|
|
addVirtRegCoalesce(Costs, *Allowed1, *Allowed2, CBenefit);
|
|
|
|
G.setEdgeCosts(EId, std::move(Costs));
|
2010-09-21 13:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
private:
|
2010-09-21 13:19:36 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
void addVirtRegCoalesce(
|
|
|
|
PBQPRAGraph::RawMatrix &CostMat,
|
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap &Allowed1,
|
|
|
|
const PBQPRAGraph::NodeMetadata::OptionToRegMap &Allowed2,
|
|
|
|
PBQP::PBQPNum Benefit) {
|
|
|
|
assert(CostMat.getRows() == Allowed1.size() + 1 && "Size mismatch.");
|
|
|
|
assert(CostMat.getCols() == Allowed2.size() + 1 && "Size mismatch.");
|
|
|
|
for (unsigned I = 0; I != Allowed1.size(); ++I) {
|
|
|
|
unsigned PReg1 = Allowed1[I];
|
|
|
|
for (unsigned J = 0; J != Allowed2.size(); ++J) {
|
|
|
|
unsigned PReg2 = Allowed2[J];
|
|
|
|
if (PReg1 == PReg2)
|
|
|
|
CostMat[I + 1][J + 1] += -Benefit;
|
2012-02-10 04:10:26 +00:00
|
|
|
}
|
2010-09-21 13:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-02 18:29:27 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace.
|
|
|
|
|
|
|
|
// Out-of-line destructor/anchor for PBQPRAConstraint.
|
|
|
|
PBQPRAConstraint::~PBQPRAConstraint() {}
|
|
|
|
void PBQPRAConstraint::anchor() {}
|
|
|
|
void PBQPRAConstraintList::anchor() {}
|
2010-09-18 09:07:10 +00:00
|
|
|
|
|
|
|
void RegAllocPBQP::getAnalysisUsage(AnalysisUsage &au) const {
|
2011-12-06 01:45:57 +00:00
|
|
|
au.setPreservesCFG();
|
|
|
|
au.addRequired<AliasAnalysis>();
|
|
|
|
au.addPreserved<AliasAnalysis>();
|
2010-09-18 09:07:10 +00:00
|
|
|
au.addRequired<SlotIndexes>();
|
|
|
|
au.addPreserved<SlotIndexes>();
|
|
|
|
au.addRequired<LiveIntervals>();
|
2012-10-04 04:50:53 +00:00
|
|
|
au.addPreserved<LiveIntervals>();
|
2010-09-18 09:07:10 +00:00
|
|
|
//au.addRequiredID(SplitCriticalEdgesID);
|
2011-06-17 07:09:01 +00:00
|
|
|
if (customPassID)
|
|
|
|
au.addRequiredID(*customPassID);
|
2010-09-18 09:07:10 +00:00
|
|
|
au.addRequired<LiveStacks>();
|
|
|
|
au.addPreserved<LiveStacks>();
|
2013-06-17 19:00:36 +00:00
|
|
|
au.addRequired<MachineBlockFrequencyInfo>();
|
|
|
|
au.addPreserved<MachineBlockFrequencyInfo>();
|
2013-07-01 20:47:47 +00:00
|
|
|
au.addRequired<MachineLoopInfo>();
|
|
|
|
au.addPreserved<MachineLoopInfo>();
|
2011-12-06 01:45:57 +00:00
|
|
|
au.addRequired<MachineDominatorTree>();
|
|
|
|
au.addPreserved<MachineDominatorTree>();
|
2010-09-18 09:07:10 +00:00
|
|
|
au.addRequired<VirtRegMap>();
|
2012-10-04 04:50:53 +00:00
|
|
|
au.addPreserved<VirtRegMap>();
|
2010-09-18 09:07:10 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(au);
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
void RegAllocPBQP::findVRegIntervalsToAlloc(const MachineFunction &MF,
|
|
|
|
LiveIntervals &LIS) {
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
2008-11-16 12:12:54 +00:00
|
|
|
|
|
|
|
// Iterate over all live ranges.
|
2014-10-09 18:20:51 +00:00
|
|
|
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
|
|
|
|
if (MRI.reg_nodbg_empty(Reg))
|
2008-11-16 12:12:54 +00:00
|
|
|
continue;
|
2014-10-09 18:20:51 +00:00
|
|
|
LiveInterval &LI = LIS.getInterval(Reg);
|
2008-11-16 12:12:54 +00:00
|
|
|
|
|
|
|
// If this live interval is non-empty we will use pbqp to allocate it.
|
|
|
|
// Empty intervals we allocate in a simple post-processing stage in
|
|
|
|
// finalizeAlloc.
|
2014-10-09 18:20:51 +00:00
|
|
|
if (!LI.empty()) {
|
|
|
|
VRegsToAlloc.insert(LI.reg);
|
2010-11-12 05:47:21 +00:00
|
|
|
} else {
|
2014-10-09 18:20:51 +00:00
|
|
|
EmptyIntervalVRegs.insert(LI.reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAllocPBQP::initializeGraph(PBQPRAGraph &G) {
|
|
|
|
MachineFunction &MF = G.getMetadata().MF;
|
|
|
|
|
|
|
|
LiveIntervals &LIS = G.getMetadata().LIS;
|
|
|
|
const MachineRegisterInfo &MRI = G.getMetadata().MF.getRegInfo();
|
|
|
|
const TargetRegisterInfo &TRI =
|
|
|
|
*G.getMetadata().MF.getTarget().getSubtargetImpl()->getRegisterInfo();
|
|
|
|
|
|
|
|
for (auto VReg : VRegsToAlloc) {
|
|
|
|
const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
|
|
|
|
LiveInterval &VRegLI = LIS.getInterval(VReg);
|
|
|
|
|
|
|
|
// Record any overlaps with regmask operands.
|
|
|
|
BitVector RegMaskOverlaps;
|
|
|
|
LIS.checkRegMaskInterference(VRegLI, RegMaskOverlaps);
|
|
|
|
|
|
|
|
// Compute an initial allowed set for the current vreg.
|
|
|
|
std::vector<unsigned> VRegAllowed;
|
|
|
|
ArrayRef<MCPhysReg> RawPRegOrder = TRC->getRawAllocationOrder(MF);
|
|
|
|
for (unsigned I = 0; I != RawPRegOrder.size(); ++I) {
|
|
|
|
unsigned PReg = RawPRegOrder[I];
|
|
|
|
if (MRI.isReserved(PReg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// vregLI crosses a regmask operand that clobbers preg.
|
|
|
|
if (!RegMaskOverlaps.empty() && !RegMaskOverlaps.test(PReg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// vregLI overlaps fixed regunit interference.
|
|
|
|
bool Interference = false;
|
|
|
|
for (MCRegUnitIterator Units(PReg, &TRI); Units.isValid(); ++Units) {
|
|
|
|
if (VRegLI.overlaps(LIS.getRegUnit(*Units))) {
|
|
|
|
Interference = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Interference)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// preg is usable for this virtual register.
|
|
|
|
VRegAllowed.push_back(PReg);
|
2008-11-16 12:12:54 +00:00
|
|
|
}
|
2014-10-09 18:20:51 +00:00
|
|
|
|
|
|
|
PBQPRAGraph::RawVector NodeCosts(VRegAllowed.size() + 1, 0);
|
|
|
|
PBQPRAGraph::NodeId NId = G.addNode(std::move(NodeCosts));
|
|
|
|
G.getNodeMetadata(NId).setVReg(VReg);
|
|
|
|
G.getNodeMetadata(NId).setOptionRegs(std::move(VRegAllowed));
|
|
|
|
G.getMetadata().setNodeIdForVReg(VReg, NId);
|
2008-11-16 12:12:54 +00:00
|
|
|
}
|
2008-10-02 18:29:27 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
bool RegAllocPBQP::mapPBQPToRegAlloc(const PBQPRAGraph &G,
|
|
|
|
const PBQP::Solution &Solution,
|
|
|
|
VirtRegMap &VRM,
|
|
|
|
Spiller &VRegSpiller) {
|
|
|
|
MachineFunction &MF = G.getMetadata().MF;
|
|
|
|
LiveIntervals &LIS = G.getMetadata().LIS;
|
|
|
|
const TargetRegisterInfo &TRI =
|
|
|
|
*MF.getTarget().getSubtargetImpl()->getRegisterInfo();
|
|
|
|
(void)TRI;
|
|
|
|
|
2010-09-18 09:07:10 +00:00
|
|
|
// Set to true if we have any spills
|
2014-10-09 18:20:51 +00:00
|
|
|
bool AnotherRoundNeeded = false;
|
2010-09-18 09:07:10 +00:00
|
|
|
|
|
|
|
// Clear the existing allocation.
|
2014-10-09 18:20:51 +00:00
|
|
|
VRM.clearAllVirt();
|
2010-09-18 09:07:10 +00:00
|
|
|
|
|
|
|
// Iterate over the nodes mapping the PBQP solution to a register
|
|
|
|
// assignment.
|
2014-10-09 18:20:51 +00:00
|
|
|
for (auto NId : G.nodeIds()) {
|
|
|
|
unsigned VReg = G.getNodeMetadata(NId).getVReg();
|
|
|
|
unsigned AllocOption = Solution.getSelection(NId);
|
|
|
|
|
|
|
|
if (AllocOption != PBQP::RegAlloc::getSpillOptionIdx()) {
|
|
|
|
unsigned PReg = G.getNodeMetadata(NId).getOptionRegs()[AllocOption - 1];
|
|
|
|
DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> "
|
|
|
|
<< TRI.getName(PReg) << "\n");
|
|
|
|
assert(PReg != 0 && "Invalid preg selected.");
|
|
|
|
VRM.assignVirt2Phys(VReg, PReg);
|
|
|
|
} else {
|
|
|
|
VRegsToAlloc.erase(VReg);
|
|
|
|
SmallVector<unsigned, 8> NewSpills;
|
|
|
|
LiveRangeEdit LRE(&LIS.getInterval(VReg), NewSpills, MF, LIS, &VRM);
|
|
|
|
VRegSpiller.spill(LRE);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "VREG " << PrintReg(VReg, &TRI) << " -> SPILLED (Cost: "
|
2011-11-12 23:17:52 +00:00
|
|
|
<< LRE.getParent().weight << ", New vregs: ");
|
2010-09-18 09:07:10 +00:00
|
|
|
|
|
|
|
// Copy any newly inserted live intervals into the list of regs to
|
|
|
|
// allocate.
|
2014-10-09 18:20:51 +00:00
|
|
|
for (LiveRangeEdit::iterator I = LRE.begin(), E = LRE.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
LiveInterval &LI = LIS.getInterval(*I);
|
|
|
|
assert(!LI.empty() && "Empty spill range.");
|
|
|
|
DEBUG(dbgs() << PrintReg(LI.reg, &TRI) << " ");
|
|
|
|
VRegsToAlloc.insert(LI.reg);
|
2008-11-16 12:12:54 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 01:25:43 +00:00
|
|
|
DEBUG(dbgs() << ")\n");
|
2008-10-02 18:29:27 +00:00
|
|
|
|
|
|
|
// We need another round if spill intervals were added.
|
2014-10-09 18:20:51 +00:00
|
|
|
AnotherRoundNeeded |= !LRE.empty();
|
2008-10-02 18:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
return !AnotherRoundNeeded;
|
2008-10-02 18:29:27 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 09:07:10 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
void RegAllocPBQP::finalizeAlloc(MachineFunction &MF,
|
|
|
|
LiveIntervals &LIS,
|
|
|
|
VirtRegMap &VRM) const {
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
2008-11-16 12:12:54 +00:00
|
|
|
// First allocate registers for the empty intervals.
|
2010-09-18 09:07:10 +00:00
|
|
|
for (RegSet::const_iterator
|
2014-10-09 18:20:51 +00:00
|
|
|
I = EmptyIntervalVRegs.begin(), E = EmptyIntervalVRegs.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
LiveInterval &LI = LIS.getInterval(*I);
|
2008-11-16 12:12:54 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
unsigned PReg = MRI.getSimpleHint(LI.reg);
|
2009-08-06 23:32:48 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
if (PReg == 0) {
|
|
|
|
const TargetRegisterClass &RC = *MRI.getRegClass(LI.reg);
|
|
|
|
PReg = RC.getRawAllocationOrder(MF).front();
|
2008-11-16 12:12:54 +00:00
|
|
|
}
|
2009-01-08 15:50:22 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
VRM.assignVirt2Phys(LI.reg, PReg);
|
2008-11-16 12:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-18 09:07:10 +00:00
|
|
|
bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
|
2014-10-09 18:20:51 +00:00
|
|
|
LiveIntervals &LIS = getAnalysis<LiveIntervals>();
|
|
|
|
MachineBlockFrequencyInfo &MBFI =
|
|
|
|
getAnalysis<MachineBlockFrequencyInfo>();
|
2008-11-16 12:12:54 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
calculateSpillWeightsAndHints(LIS, MF, getAnalysis<MachineLoopInfo>(), MBFI);
|
2008-10-02 18:29:27 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
VirtRegMap &VRM = getAnalysis<VirtRegMap>();
|
2013-11-10 17:46:31 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
std::unique_ptr<Spiller> VRegSpiller(createInlineSpiller(*this, MF, VRM));
|
2008-10-02 18:29:27 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
MF.getRegInfo().freezeReservedRegs(MF);
|
2012-11-28 00:21:29 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
DEBUG(dbgs() << "PBQP Register Allocating for " << MF.getName() << "\n");
|
2008-11-16 12:12:54 +00:00
|
|
|
|
2008-10-02 18:29:27 +00:00
|
|
|
// Allocator main loop:
|
2009-01-08 15:50:22 +00:00
|
|
|
//
|
2008-10-02 18:29:27 +00:00
|
|
|
// * Map current regalloc problem to a PBQP problem
|
|
|
|
// * Solve the PBQP problem
|
|
|
|
// * Map the solution back to a register allocation
|
|
|
|
// * Spill if necessary
|
2009-01-08 15:50:22 +00:00
|
|
|
//
|
2008-10-02 18:29:27 +00:00
|
|
|
// This process is continued till no more spills are generated.
|
|
|
|
|
2008-11-16 12:12:54 +00:00
|
|
|
// Find the vreg intervals in need of allocation.
|
2014-10-09 18:20:51 +00:00
|
|
|
findVRegIntervalsToAlloc(MF, LIS);
|
2009-01-08 15:50:22 +00:00
|
|
|
|
2012-08-22 06:07:19 +00:00
|
|
|
#ifndef NDEBUG
|
2014-10-09 18:20:51 +00:00
|
|
|
const Function &F = *MF.getFunction();
|
|
|
|
std::string FullyQualifiedName =
|
|
|
|
F.getParent()->getModuleIdentifier() + "." + F.getName().str();
|
2012-08-22 06:07:19 +00:00
|
|
|
#endif
|
2012-03-26 23:07:23 +00:00
|
|
|
|
2008-11-16 12:12:54 +00:00
|
|
|
// If there are non-empty intervals allocate them using pbqp.
|
2014-10-09 18:20:51 +00:00
|
|
|
if (!VRegsToAlloc.empty()) {
|
2008-11-16 12:12:54 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
const TargetSubtargetInfo &Subtarget = *MF.getTarget().getSubtargetImpl();
|
|
|
|
std::unique_ptr<PBQPRAConstraintList> ConstraintsRoot =
|
|
|
|
llvm::make_unique<PBQPRAConstraintList>();
|
|
|
|
ConstraintsRoot->addConstraint(llvm::make_unique<SpillCosts>());
|
|
|
|
ConstraintsRoot->addConstraint(llvm::make_unique<Interference>());
|
|
|
|
if (PBQPCoalescing)
|
|
|
|
ConstraintsRoot->addConstraint(llvm::make_unique<Coalescing>());
|
|
|
|
ConstraintsRoot->addConstraint(Subtarget.getCustomPBQPConstraints());
|
2008-11-16 12:12:54 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
bool PBQPAllocComplete = false;
|
|
|
|
unsigned Round = 0;
|
2009-08-06 23:32:48 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
while (!PBQPAllocComplete) {
|
|
|
|
DEBUG(dbgs() << " PBQP Regalloc round " << Round << ":\n");
|
|
|
|
|
|
|
|
PBQPRAGraph G(PBQPRAGraph::GraphMetadata(MF, LIS, MBFI));
|
|
|
|
initializeGraph(G);
|
|
|
|
ConstraintsRoot->apply(G);
|
2012-03-26 23:07:23 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2014-10-09 18:20:51 +00:00
|
|
|
if (PBQPDumpGraphs) {
|
|
|
|
std::ostringstream RS;
|
|
|
|
RS << Round;
|
|
|
|
std::string GraphFileName = FullyQualifiedName + "." + RS.str() +
|
|
|
|
".pbqpgraph";
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
2014-10-09 18:20:51 +00:00
|
|
|
raw_fd_ostream OS(GraphFileName, EC, sys::fs::F_Text);
|
|
|
|
DEBUG(dbgs() << "Dumping graph for round " << Round << " to \""
|
|
|
|
<< GraphFileName << "\"\n");
|
|
|
|
G.dumpToStream(OS);
|
2012-03-26 23:07:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
PBQP::Solution Solution = PBQP::RegAlloc::solve(G);
|
|
|
|
PBQPAllocComplete = mapPBQPToRegAlloc(G, Solution, VRM, *VRegSpiller);
|
|
|
|
++Round;
|
2008-11-16 12:12:54 +00:00
|
|
|
}
|
2008-10-02 18:29:27 +00:00
|
|
|
}
|
|
|
|
|
2008-11-16 12:12:54 +00:00
|
|
|
// Finalise allocation, allocate empty ranges.
|
2014-10-09 18:20:51 +00:00
|
|
|
finalizeAlloc(MF, LIS, VRM);
|
|
|
|
VRegsToAlloc.clear();
|
|
|
|
EmptyIntervalVRegs.clear();
|
2008-10-02 18:29:27 +00:00
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << VRM << "\n");
|
2008-11-16 12:12:54 +00:00
|
|
|
|
2009-01-08 15:50:22 +00:00
|
|
|
return true;
|
2008-10-02 18:29:27 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
FunctionPass *llvm::createPBQPRegisterAllocator(char *customPassID) {
|
|
|
|
return new RegAllocPBQP(customPassID);
|
2008-10-02 18:29:27 +00:00
|
|
|
}
|
|
|
|
|
2010-09-23 04:28:54 +00:00
|
|
|
FunctionPass* llvm::createDefaultPBQPRegisterAllocator() {
|
2014-10-09 18:20:51 +00:00
|
|
|
return createPBQPRegisterAllocator();
|
2010-09-18 09:07:10 +00:00
|
|
|
}
|
2008-10-02 18:29:27 +00:00
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|