15 #ifndef LLVM_CODEGEN_REGALLOCPBQP_H 16 #define LLVM_CODEGEN_REGALLOCPBQP_H 38 class MachineBlockFrequencyInfo;
39 class MachineFunction;
54 : UnsafeRows(new
bool[M.getRows() - 1]()),
55 UnsafeCols(new
bool[M.getCols() - 1]()) {
56 unsigned* ColCounts =
new unsigned[M.
getCols() - 1]();
58 for (
unsigned i = 1; i < M.
getRows(); ++i) {
59 unsigned RowCount = 0;
60 for (
unsigned j = 1; j < M.
getCols(); ++j) {
61 if (M[i][j] == std::numeric_limits<PBQPNum>::infinity()) {
64 UnsafeRows[i - 1] =
true;
65 UnsafeCols[j - 1] =
true;
68 WorstRow =
std::max(WorstRow, RowCount);
70 unsigned WorstColCountForCurRow =
71 *std::max_element(ColCounts, ColCounts + M.
getCols() - 1);
72 WorstCol =
std::max(WorstCol, WorstColCountForCurRow);
85 unsigned WorstRow = 0;
86 unsigned WorstCol = 0;
87 std::unique_ptr<bool[]> UnsafeRows;
88 std::unique_ptr<bool[]> UnsafeCols;
101 std::copy(OptVec.begin(), OptVec.end(), Opts.get());
104 unsigned size()
const {
return NumOpts; }
108 if (NumOpts != Other.NumOpts)
110 return std::equal(Opts.get(), Opts.get() + NumOpts, Other.Opts.get());
114 return !(*
this == Other);
118 unsigned NumOpts = 0;
119 std::unique_ptr<unsigned[]> Opts;
123 unsigned *OStart = OptRegs.Opts.get();
124 unsigned *OEnd = OptRegs.Opts.get() + OptRegs.NumOpts;
140 : MF(MF), LIS(LIS), MBFI(MBFI) {}
147 VRegToNodeId[VReg] = NId;
151 auto VRegItr = VRegToNodeId.find(VReg);
152 if (VRegItr == VRegToNodeId.end())
154 return VRegItr->second;
158 return AllowedRegVecs.getValue(std::move(Allowed));
176 NotProvablyAllocatable,
177 ConservativelyAllocatable,
184 : RS(Other.RS), NumOpts(Other.NumOpts), DeniedOpts(Other.DeniedOpts),
185 OptUnsafeEdges(new
unsigned[NumOpts]), VReg(Other.VReg),
186 AllowedRegs(Other.AllowedRegs)
188 , everConservativelyAllocatable(Other.everConservativelyAllocatable)
192 std::copy(&Other.OptUnsafeEdges[0], &Other.OptUnsafeEdges[NumOpts],
200 void setVReg(
unsigned VReg) { this->VReg = VReg; }
204 this->AllowedRegs = std::move(AllowedRegs);
210 OptUnsafeEdges = std::unique_ptr<unsigned[]>(
new unsigned[NumOpts]());
215 assert(RS >= this->RS &&
"A node's reduction state can not be downgraded");
221 if (RS == ConservativelyAllocatable)
222 everConservativelyAllocatable =
true;
228 const bool* UnsafeOpts =
230 for (
unsigned i = 0; i < NumOpts; ++i)
231 OptUnsafeEdges[i] += UnsafeOpts[i];
236 const bool* UnsafeOpts =
238 for (
unsigned i = 0; i < NumOpts; ++i)
239 OptUnsafeEdges[i] -= UnsafeOpts[i];
243 return (DeniedOpts < NumOpts) ||
244 (
std::find(&OptUnsafeEdges[0], &OptUnsafeEdges[NumOpts], 0) !=
245 &OptUnsafeEdges[NumOpts]);
250 return everConservativelyAllocatable;
256 unsigned NumOpts = 0;
257 unsigned DeniedOpts = 0;
258 std::unique_ptr<unsigned[]> OptUnsafeEdges;
263 bool everConservativelyAllocatable =
false;
299 assert(
G.getNodeCosts(NId).getLength() > 1 &&
300 "PBQP Graph should not contain single or zero-option nodes");
301 G.getNodeMetadata(NId).setup(
G.getNodeCosts(NId));
308 handleReconnectEdge(EId,
G.getEdgeNode1Id(EId));
309 handleReconnectEdge(EId,
G.getEdgeNode2Id(EId));
326 NodeId N1Id =
G.getEdgeNode1Id(EId);
327 NodeId N2Id =
G.getEdgeNode2Id(EId);
330 bool Transpose = N1Id !=
G.getEdgeNode1Id(EId);
351 if (
G.getNodeDegree(NId) == 3) {
353 moveToOptimallyReducibleNodes(NId);
355 NodeMetadata::NotProvablyAllocatable &&
358 moveToConservativelyAllocatableNodes(NId);
362 void removeFromCurrentSet(
NodeId NId) {
363 switch (
G.getNodeMetadata(NId).getReductionState()) {
364 case NodeMetadata::Unprocessed:
break;
365 case NodeMetadata::OptimallyReducible:
366 assert(OptimallyReducibleNodes.find(NId) !=
367 OptimallyReducibleNodes.end() &&
368 "Node not in optimally reducible set.");
369 OptimallyReducibleNodes.erase(NId);
371 case NodeMetadata::ConservativelyAllocatable:
372 assert(ConservativelyAllocatableNodes.find(NId) !=
373 ConservativelyAllocatableNodes.end() &&
374 "Node not in conservatively allocatable set.");
375 ConservativelyAllocatableNodes.erase(NId);
377 case NodeMetadata::NotProvablyAllocatable:
378 assert(NotProvablyAllocatableNodes.find(NId) !=
379 NotProvablyAllocatableNodes.end() &&
380 "Node not in not-provably-allocatable set.");
381 NotProvablyAllocatableNodes.erase(NId);
386 void moveToOptimallyReducibleNodes(
NodeId NId) {
387 removeFromCurrentSet(NId);
388 OptimallyReducibleNodes.insert(NId);
389 G.getNodeMetadata(NId).setReductionState(
390 NodeMetadata::OptimallyReducible);
393 void moveToConservativelyAllocatableNodes(
NodeId NId) {
394 removeFromCurrentSet(NId);
395 ConservativelyAllocatableNodes.insert(NId);
396 G.getNodeMetadata(NId).setReductionState(
397 NodeMetadata::ConservativelyAllocatable);
400 void moveToNotProvablyAllocatableNodes(
NodeId NId) {
401 removeFromCurrentSet(NId);
402 NotProvablyAllocatableNodes.insert(NId);
403 G.getNodeMetadata(NId).setReductionState(
404 NodeMetadata::NotProvablyAllocatable);
409 for (
auto NId :
G.nodeIds()) {
410 if (
G.getNodeDegree(NId) < 3)
411 moveToOptimallyReducibleNodes(NId);
412 else if (
G.getNodeMetadata(NId).isConservativelyAllocatable())
413 moveToConservativelyAllocatableNodes(NId);
415 moveToNotProvablyAllocatableNodes(NId);
425 std::vector<GraphBase::NodeId>
reduce() {
426 assert(!
G.empty() &&
"Cannot reduce empty graph.");
429 std::vector<NodeId> NodeStack;
433 if (!OptimallyReducibleNodes.empty()) {
436 OptimallyReducibleNodes.erase(NItr);
437 NodeStack.push_back(NId);
438 switch (
G.getNodeDegree(NId)) {
449 }
else if (!ConservativelyAllocatableNodes.empty()) {
458 ConservativelyAllocatableNodes.erase(NItr);
459 NodeStack.push_back(NId);
460 G.disconnectAllNeighborsFromNode(NId);
461 }
else if (!NotProvablyAllocatableNodes.empty()) {
463 std::min_element(NotProvablyAllocatableNodes.begin(),
464 NotProvablyAllocatableNodes.end(),
465 SpillCostComparator(
G));
467 NotProvablyAllocatableNodes.erase(NItr);
468 NodeStack.push_back(NId);
469 G.disconnectAllNeighborsFromNode(NId);
477 class SpillCostComparator {
479 SpillCostComparator(
const Graph&
G) :
G(G) {}
494 using NodeSet = std::set<NodeId>;
495 NodeSet OptimallyReducibleNodes;
496 NodeSet ConservativelyAllocatableNodes;
497 NodeSet NotProvablyAllocatableNodes;
523 return RegAllocSolver.
solve();
535 #endif // LLVM_CODEGEN_REGALLOCPBQP_H Represents a solution to a PBQP problem.
This class represents lattice values for constants.
Holds a vector of the allowed physical regs for a vreg.
bool empty() const
Returns true if the graph is empty.
void handleSetNodeCosts(NodeId NId, const Vector &newCosts)
unsigned getLength() const
Return the length of the vector.
static NodeId invalidNodeId()
Returns a value representing an invalid (non-existent) node.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
RegAllocSolverImpl(Graph &G)
FunctionPass * createPBQPRegisterAllocator(char *customPassID=nullptr)
Create a PBQP register allocator instance.
typename RegAllocSolverImpl ::GraphMetadata GraphMetadata
unsigned getRows() const
Return the number of rows in this matrix.
void applyR1(GraphT &G, typename GraphT::NodeId NId)
Reduce a node of degree one.
void handleDisconnectEdge(EdgeId EId, NodeId NId)
unsigned getCols() const
Return the number of cols in this matrix.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
const Vector & getNodeCosts(NodeId NId) const
Get a node's cost vector.
PBQPRAGraph(GraphMetadata Metadata)
AllowedRegVector(const std::vector< unsigned > &OptVec)
bool operator==(const AllowedRegVector &Other) const
FunctionPass class - This class is used to implement most global optimizations.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SetVector< SUnit * >::const_iterator iterator
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Align max(MaybeAlign Lhs, Align Rhs)
static cl::opt< RegisterRegAlloc::FunctionPassCtor, false, RegisterPassParser< RegisterRegAlloc > > RegAlloc("regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator), cl::desc("Register allocator to use"))
Solution backpropagate(GraphT &G, StackT stack)
NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const
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.
unsigned operator[](size_t I) const
bool operator!=(const AllowedRegVector &Other) const
const Metadata & getMetadata() const
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)
OutputIt copy(R &&Range, OutputIt Out)
void handleRemoveNode(NodeId NId)