16 #ifndef LLVM_CODEGEN_REGALLOCPBQP_H
17 #define LLVM_CODEGEN_REGALLOCPBQP_H
39 class MachineBlockFrequencyInfo;
40 class MachineFunction;
55 : UnsafeRows(new
bool[M.getRows() - 1]()),
56 UnsafeCols(new
bool[M.getCols() - 1]()) {
57 unsigned* ColCounts =
new unsigned[M.
getCols() - 1]();
59 for (
unsigned i = 1; i < M.
getRows(); ++i) {
60 unsigned RowCount = 0;
61 for (
unsigned j = 1; j < M.
getCols(); ++j) {
62 if (M[i][j] == std::numeric_limits<PBQPNum>::infinity()) {
65 UnsafeRows[i - 1] =
true;
66 UnsafeCols[j - 1] =
true;
69 WorstRow =
std::max(WorstRow, RowCount);
71 unsigned WorstColCountForCurRow =
72 *std::max_element(ColCounts, ColCounts + M.
getCols() - 1);
73 WorstCol =
std::max(WorstCol, WorstColCountForCurRow);
86 unsigned WorstRow = 0;
87 unsigned WorstCol = 0;
88 std::unique_ptr<bool[]> UnsafeRows;
89 std::unique_ptr<bool[]> UnsafeCols;
102 std::copy(OptVec.begin(), OptVec.end(), Opts.get());
105 unsigned size()
const {
return NumOpts; }
109 if (NumOpts != Other.NumOpts)
111 return std::equal(Opts.get(), Opts.get() + NumOpts, Other.Opts.get());
115 return !(*
this == Other);
119 unsigned NumOpts = 0;
120 std::unique_ptr<unsigned[]> Opts;
124 unsigned *OStart = OptRegs.Opts.get();
125 unsigned *OEnd = OptRegs.Opts.get() + OptRegs.NumOpts;
141 : MF(MF), LIS(LIS), MBFI(MBFI) {}
148 VRegToNodeId[VReg] = NId;
152 auto VRegItr = VRegToNodeId.find(VReg);
153 if (VRegItr == VRegToNodeId.end())
155 return VRegItr->second;
159 return AllowedRegVecs.
getValue(std::move(Allowed));
164 AllowedRegVecPool AllowedRegVecs;
177 NotProvablyAllocatable,
178 ConservativelyAllocatable,
185 : RS(Other.RS), NumOpts(Other.NumOpts), DeniedOpts(Other.DeniedOpts),
186 OptUnsafeEdges(new
unsigned[NumOpts]), VReg(Other.VReg),
187 AllowedRegs(Other.AllowedRegs)
189 , everConservativelyAllocatable(Other.everConservativelyAllocatable)
193 std::copy(&Other.OptUnsafeEdges[0], &Other.OptUnsafeEdges[NumOpts],
201 void setVReg(
unsigned VReg) { this->VReg = VReg; }
205 this->AllowedRegs = std::move(AllowedRegs);
211 OptUnsafeEdges = std::unique_ptr<unsigned[]>(
new unsigned[NumOpts]());
216 assert(RS >= this->RS &&
"A node's reduction state can not be downgraded");
222 if (RS == ConservativelyAllocatable)
223 everConservativelyAllocatable =
true;
229 const bool* UnsafeOpts =
231 for (
unsigned i = 0; i < NumOpts; ++i)
232 OptUnsafeEdges[i] += UnsafeOpts[i];
237 const bool* UnsafeOpts =
239 for (
unsigned i = 0; i < NumOpts; ++i)
240 OptUnsafeEdges[i] -= UnsafeOpts[i];
244 return (DeniedOpts < NumOpts) ||
245 (
std::find(&OptUnsafeEdges[0], &OptUnsafeEdges[NumOpts], 0) !=
246 &OptUnsafeEdges[NumOpts]);
251 return everConservativelyAllocatable;
257 unsigned NumOpts = 0;
258 unsigned DeniedOpts = 0;
259 std::unique_ptr<unsigned[]> OptUnsafeEdges;
264 bool everConservativelyAllocatable =
false;
301 "PBQP Graph should not contain single or zero-option nodes");
354 moveToOptimallyReducibleNodes(NId);
356 NodeMetadata::NotProvablyAllocatable &&
359 moveToConservativelyAllocatableNodes(NId);
363 void removeFromCurrentSet(
NodeId NId) {
365 case NodeMetadata::Unprocessed:
break;
366 case NodeMetadata::OptimallyReducible:
367 assert(OptimallyReducibleNodes.find(NId) !=
368 OptimallyReducibleNodes.end() &&
369 "Node not in optimally reducible set.");
370 OptimallyReducibleNodes.erase(NId);
372 case NodeMetadata::ConservativelyAllocatable:
373 assert(ConservativelyAllocatableNodes.find(NId) !=
374 ConservativelyAllocatableNodes.end() &&
375 "Node not in conservatively allocatable set.");
376 ConservativelyAllocatableNodes.erase(NId);
378 case NodeMetadata::NotProvablyAllocatable:
379 assert(NotProvablyAllocatableNodes.find(NId) !=
380 NotProvablyAllocatableNodes.end() &&
381 "Node not in not-provably-allocatable set.");
382 NotProvablyAllocatableNodes.erase(NId);
387 void moveToOptimallyReducibleNodes(
NodeId NId) {
388 removeFromCurrentSet(NId);
389 OptimallyReducibleNodes.insert(NId);
391 NodeMetadata::OptimallyReducible);
394 void moveToConservativelyAllocatableNodes(
NodeId NId) {
395 removeFromCurrentSet(NId);
396 ConservativelyAllocatableNodes.insert(NId);
398 NodeMetadata::ConservativelyAllocatable);
401 void moveToNotProvablyAllocatableNodes(
NodeId NId) {
402 removeFromCurrentSet(NId);
403 NotProvablyAllocatableNodes.insert(NId);
405 NodeMetadata::NotProvablyAllocatable);
412 moveToOptimallyReducibleNodes(NId);
414 moveToConservativelyAllocatableNodes(NId);
416 moveToNotProvablyAllocatableNodes(NId);
426 std::vector<GraphBase::NodeId> reduce() {
427 assert(!G.
empty() &&
"Cannot reduce empty graph.");
430 std::vector<NodeId> NodeStack;
434 if (!OptimallyReducibleNodes.empty()) {
435 NodeSet::iterator NItr = OptimallyReducibleNodes.begin();
437 OptimallyReducibleNodes.erase(NItr);
438 NodeStack.push_back(NId);
450 }
else if (!ConservativelyAllocatableNodes.empty()) {
457 NodeSet::iterator NItr = ConservativelyAllocatableNodes.begin();
459 ConservativelyAllocatableNodes.erase(NItr);
460 NodeStack.push_back(NId);
462 }
else if (!NotProvablyAllocatableNodes.empty()) {
463 NodeSet::iterator NItr =
464 std::min_element(NotProvablyAllocatableNodes.begin(),
465 NotProvablyAllocatableNodes.end(),
466 SpillCostComparator(G));
468 NotProvablyAllocatableNodes.erase(NItr);
469 NodeStack.push_back(NId);
478 class SpillCostComparator {
480 SpillCostComparator(
const Graph& G) : G(G) {}
482 bool operator()(NodeId N1Id, NodeId N2Id) {
483 PBQPNum N1SC =
G.getNodeCosts(N1Id)[0];
484 PBQPNum N2SC =
G.getNodeCosts(N2Id)[0];
486 return G.getNodeDegree(N1Id) <
G.getNodeDegree(N2Id);
495 using NodeSet = std::set<NodeId>;
496 NodeSet OptimallyReducibleNodes;
497 NodeSet ConservativelyAllocatableNodes;
498 NodeSet NotProvablyAllocatableNodes;
524 return RegAllocSolver.
solve();
536 #endif // LLVM_CODEGEN_REGALLOCPBQP_H
unsigned getCols() const
Return the number of cols in this matrix.
Represents a solution to a PBQP problem.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
NodeId getEdgeNode1Id(EdgeId EId) const
Get the first node connected to this edge.
Holds a vector of the allowed physical regs for a vreg.
void handleSetNodeCosts(NodeId NId, const Vector &newCosts)
bool operator!=(const AllowedRegVector &Other) const
static NodeId invalidNodeId()
Returns a value representing an invalid (non-existent) node.
NodeId getEdgeNode2Id(EdgeId EId) const
Get the second node connected to this edge.
PoolRef getValue(ValueKeyT ValueKey)
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
AllowedRegVector()=default
const Vector & getNodeCosts(NodeId NId) const
Get a node's cost vector.
RegAllocSolverImpl(Graph &G)
FunctionPass * createPBQPRegisterAllocator(char *customPassID=nullptr)
Create a PBQP register allocator instance.
void dump() const
Dump this graph to dbgs().
NodeMetadata & getNodeMetadata(NodeId NId)
typename RegAllocSolverImpl::GraphMetadata GraphMetadata
friend hash_code hash_value(const AllowedRegVector &)
bool operator==(const AllowedRegVector &Other) const
void printDot(raw_ostream &OS) const
Print a representation of this graph in DOT format.
void applyR1(GraphT &G, typename GraphT::NodeId NId)
Reduce a node of degree one.
void handleDisconnectEdge(EdgeId EId, NodeId NId)
NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const
NodeIdSet nodeIds() const
PBQPRAGraph(GraphMetadata Metadata)
AllowedRegVector(const std::vector< unsigned > &OptVec)
void setSolver(SolverT &S)
Lock this graph to the given solver instance in preparation for running the solver.
unsigned getLength() const
Return the length of the vector.
const Metadata & getMetadata() const
void unsetSolver()
Release from solver instance.
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
unsigned operator[](size_t I) const
Solution backpropagate(GraphT &G, StackT stack)
const Matrix & getEdgeCosts(EdgeId EId) const
Get an edge's cost matrix.
bool empty() const
Returns true if the graph is empty.
void handleUpdateCosts(EdgeId EId, const Matrix &NewCosts)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
An opaque object representing a hash code.
PBQP::Graph< RegAllocSolverImpl > Graph
llvm_unreachable("Unknown RegBank!")
static cl::opt< RegisterRegAlloc::FunctionPassCtor, false, RegisterPassParser< RegisterRegAlloc > > RegAlloc("regalloc", cl::init(&useDefaultRegisterAllocator), cl::desc("Register allocator to use"))
void disconnectAllNeighborsFromNode(NodeId NId)
Convenience method to disconnect all neighbours from the given node.
unsigned getRows() const
Return the number of rows in this matrix.
std::shared_ptr< const AllowedRegVector > PoolRef
void handleReconnectEdge(EdgeId EId, NodeId NId)
hash_code hash_value(const AllowedRegVector &OptRegs)
Solution solve(PBQPRAGraph &G)
unsigned getSpillOptionIdx()
Spill option index.
void handleAddEdge(EdgeId EId)
void applyR2(GraphT &G, typename GraphT::NodeId NId)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
void handleAddNode(NodeId NId)
void handleRemoveNode(NodeId NId)