#ifndef LLVM_CODEGEN_REGALLOCPBQP_H
#define LLVM_CODEGEN_REGALLOCPBQP_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/PBQP/Graph.h"
#include "llvm/CodeGen/PBQP/Solution.h"
#include <map>
#include <set>
namespace llvm {
class LiveIntervals;
class MachineFunction;
class MachineLoopInfo;
class PBQPRAProblem {
public:
typedef SmallVector<unsigned, 16> AllowedSet;
PBQP::Graph& getGraph() { return graph; }
const PBQP::Graph& getGraph() const { return graph; }
template <typename AllowedRegsItr>
void recordVReg(unsigned vreg, PBQP::Graph::NodeItr node,
AllowedRegsItr arBegin, AllowedRegsItr arEnd) {
assert(node2VReg.find(node) == node2VReg.end() && "Re-mapping node.");
assert(vreg2Node.find(vreg) == vreg2Node.end() && "Re-mapping vreg.");
assert(allowedSets[vreg].empty() && "vreg already has pregs.");
node2VReg[node] = vreg;
vreg2Node[vreg] = node;
std::copy(arBegin, arEnd, std::back_inserter(allowedSets[vreg]));
}
unsigned getVRegForNode(PBQP::Graph::ConstNodeItr node) const;
PBQP::Graph::NodeItr getNodeForVReg(unsigned vreg) const;
bool isPRegOption(unsigned vreg, unsigned option) const {
return !isSpillOption(vreg, option);
}
bool isSpillOption(unsigned vreg, unsigned option) const {
return option == 0;
}
const AllowedSet& getAllowedSet(unsigned vreg) const;
unsigned getPRegForOption(unsigned vreg, unsigned option) const;
private:
typedef std::map<PBQP::Graph::ConstNodeItr, unsigned,
PBQP::NodeItrComparator> Node2VReg;
typedef DenseMap<unsigned, PBQP::Graph::NodeItr> VReg2Node;
typedef std::map<unsigned, AllowedSet> AllowedSetMap;
PBQP::Graph graph;
Node2VReg node2VReg;
VReg2Node vreg2Node;
AllowedSetMap allowedSets;
};
class PBQPBuilder {
private:
PBQPBuilder(const PBQPBuilder&) {}
void operator=(const PBQPBuilder&) {}
public:
typedef std::set<unsigned> RegSet;
PBQPBuilder() {}
virtual ~PBQPBuilder() {}
virtual std::auto_ptr<PBQPRAProblem> build(
MachineFunction *mf,
const LiveIntervals *lis,
const MachineLoopInfo *loopInfo,
const RegSet &vregs);
private:
void addSpillCosts(PBQP::Vector &costVec, PBQP::PBQPNum spillCost);
void addInterferenceCosts(PBQP::Matrix &costMat,
const PBQPRAProblem::AllowedSet &vr1Allowed,
const PBQPRAProblem::AllowedSet &vr2Allowed,
const TargetRegisterInfo *tri);
};
class PBQPBuilderWithCoalescing : public PBQPBuilder {
public:
virtual std::auto_ptr<PBQPRAProblem> build(
MachineFunction *mf,
const LiveIntervals *lis,
const MachineLoopInfo *loopInfo,
const RegSet &vregs);
private:
void addPhysRegCoalesce(PBQP::Vector &costVec, unsigned pregOption,
PBQP::PBQPNum benefit);
void addVirtRegCoalesce(PBQP::Matrix &costMat,
const PBQPRAProblem::AllowedSet &vr1Allowed,
const PBQPRAProblem::AllowedSet &vr2Allowed,
PBQP::PBQPNum benefit);
};
FunctionPass* createPBQPRegisterAllocator(std::auto_ptr<PBQPBuilder> builder);
}
#endif