1 //===-- RegAllocPBQP.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PBQPBuilder interface, for classes which build PBQP
11 // instances to represent register allocation problems, and the RegAllocPBQP
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_REGALLOCPBQP_H
17 #define LLVM_CODEGEN_REGALLOCPBQP_H
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/PBQP/Graph.h"
22 #include "llvm/CodeGen/PBQP/Solution.h"
29 class MachineFunction;
30 class MachineLoopInfo;
32 /// This class wraps up a PBQP instance representing a register allocation
33 /// problem, plus the structures necessary to map back from the PBQP solution
34 /// to a register allocation solution. (i.e. The PBQP-node <--> vreg map,
35 /// and the PBQP option <--> storage location map).
40 typedef SmallVector<unsigned, 16> AllowedSet;
42 PBQP::Graph& getGraph() { return graph; }
44 const PBQP::Graph& getGraph() const { return graph; }
46 /// Record the mapping between the given virtual register and PBQP node,
47 /// and the set of allowed pregs for the vreg.
49 /// If you are extending
50 /// PBQPBuilder you are unlikely to need this: Nodes and options for all
51 /// vregs will already have been set up for you by the base class.
52 template <typename AllowedRegsItr>
53 void recordVReg(unsigned vreg, PBQP::Graph::NodeItr node,
54 AllowedRegsItr arBegin, AllowedRegsItr arEnd) {
55 assert(node2VReg.find(node) == node2VReg.end() && "Re-mapping node.");
56 assert(vreg2Node.find(vreg) == vreg2Node.end() && "Re-mapping vreg.");
57 assert(allowedSets[vreg].empty() && "vreg already has pregs.");
59 node2VReg[node] = vreg;
60 vreg2Node[vreg] = node;
61 std::copy(arBegin, arEnd, std::back_inserter(allowedSets[vreg]));
64 /// Get the virtual register corresponding to the given PBQP node.
65 unsigned getVRegForNode(PBQP::Graph::ConstNodeItr node) const;
67 /// Get the PBQP node corresponding to the given virtual register.
68 PBQP::Graph::NodeItr getNodeForVReg(unsigned vreg) const;
70 /// Returns true if the given PBQP option represents a physical register,
72 bool isPRegOption(unsigned vreg, unsigned option) const {
73 // At present we only have spills or pregs, so anything that's not a
74 // spill is a preg. (This might be extended one day to support remat).
75 return !isSpillOption(vreg, option);
78 /// Returns true if the given PBQP option represents spilling, false
80 bool isSpillOption(unsigned vreg, unsigned option) const {
81 // We hardcode option zero as the spill option.
85 /// Returns the allowed set for the given virtual register.
86 const AllowedSet& getAllowedSet(unsigned vreg) const;
88 /// Get PReg for option.
89 unsigned getPRegForOption(unsigned vreg, unsigned option) const;
93 typedef std::map<PBQP::Graph::ConstNodeItr, unsigned,
94 PBQP::NodeItrComparator> Node2VReg;
95 typedef DenseMap<unsigned, PBQP::Graph::NodeItr> VReg2Node;
96 typedef std::map<unsigned, AllowedSet> AllowedSetMap;
102 AllowedSetMap allowedSets;
106 /// Builds PBQP instances to represent register allocation problems. Includes
107 /// spill, interference and coalescing costs by default. You can extend this
108 /// class to support additional constraints for your architecture.
111 PBQPBuilder(const PBQPBuilder&) {}
112 void operator=(const PBQPBuilder&) {}
115 typedef std::set<unsigned> RegSet;
117 /// Default constructor.
120 /// Clean up a PBQPBuilder.
121 virtual ~PBQPBuilder() {}
123 /// Build a PBQP instance to represent the register allocation problem for
124 /// the given MachineFunction.
125 virtual std::auto_ptr<PBQPRAProblem> build(
127 const LiveIntervals *lis,
128 const MachineLoopInfo *loopInfo,
129 const RegSet &vregs);
132 void addSpillCosts(PBQP::Vector &costVec, PBQP::PBQPNum spillCost);
134 void addInterferenceCosts(PBQP::Matrix &costMat,
135 const PBQPRAProblem::AllowedSet &vr1Allowed,
136 const PBQPRAProblem::AllowedSet &vr2Allowed,
137 const TargetRegisterInfo *tri);
140 /// Extended builder which adds coalescing constraints to a problem.
141 class PBQPBuilderWithCoalescing : public PBQPBuilder {
144 /// Build a PBQP instance to represent the register allocation problem for
145 /// the given MachineFunction.
146 virtual std::auto_ptr<PBQPRAProblem> build(
148 const LiveIntervals *lis,
149 const MachineLoopInfo *loopInfo,
150 const RegSet &vregs);
154 void addPhysRegCoalesce(PBQP::Vector &costVec, unsigned pregOption,
155 PBQP::PBQPNum benefit);
157 void addVirtRegCoalesce(PBQP::Matrix &costMat,
158 const PBQPRAProblem::AllowedSet &vr1Allowed,
159 const PBQPRAProblem::AllowedSet &vr2Allowed,
160 PBQP::PBQPNum benefit);
163 FunctionPass* createPBQPRegisterAllocator(std::auto_ptr<PBQPBuilder> builder);
166 #endif /* LLVM_CODEGEN_REGALLOCPBQP_H */