35 #define DEBUG_TYPE "wasm-cfg-sort"
39 StringRef getPassName()
const override {
return "WebAssembly CFG Sort"; }
60 return new WebAssemblyCFGSort();
65 bool AnyBarrier =
false;
67 bool AllAnalyzable =
true;
70 AnyBarrier |= Term.isBarrier();
72 AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
74 assert((AnyBarrier || AllAnalyzable) &&
75 "AnalyzeBranch needs to analyze any block with a fallthrough");
82 struct CompareBlockNumbers {
89 struct CompareBlockNumbersBackwards {
99 unsigned NumBlocksLeft;
103 std::vector<MachineBasicBlock *> Deferred;
106 :
Loop(L), NumBlocksLeft(L->getNumBlocks()) {}
121 unsigned N = MBB.pred_size();
127 NumPredsLeft[MBB.getNumber()] =
N;
141 CompareBlockNumbersBackwards>
154 for (Entry &
E : Loops)
155 if (
E.Loop->contains(MBB) && --
E.NumBlocksLeft == 0)
156 for (
auto DeferredBlock :
E.Deferred)
157 Ready.push(DeferredBlock);
158 while (!Loops.empty() && Loops.back().NumBlocksLeft == 0)
165 if (SuccL->getHeader() == Succ && SuccL->contains(MBB))
168 if (--NumPredsLeft[Succ->getNumber()] == 0)
169 Preferred.push(Succ);
174 while (!Preferred.empty()) {
175 Next = Preferred.top();
179 if (!Loops.
empty() &&
181 Loops.
back().Deferred.push_back(Next);
187 if (Next->
getNumber() < MBB->getNumber() &&
209 if (!Loops.
empty() &&
211 Loops.
back().Deferred.push_back(Next);
222 assert(Loops.
empty() &&
"Active loop list not finished");
233 for (
auto &MBB : MF) {
234 assert(MBB.getNumber() >= 0 &&
"Renumbered blocks should be non-negative.");
237 if (Loop && &MBB == Loop->getHeader()) {
240 for (
auto Pred : MBB.predecessors())
242 (Pred->getNumber() < MBB.getNumber() || Loop->contains(Pred)) &&
243 "Loop header predecessors must be loop predecessors or backedges");
244 assert(OnStack.
insert(Loop) &&
"Loops should be declared at most once.");
247 for (
auto Pred : MBB.predecessors())
248 assert(Pred->getNumber() < MBB.getNumber() &&
249 "Non-loop-header predecessors should be topologically sorted");
251 "Blocks must be nested in their loops");
257 "The function entry block shouldn't actually be a loop header");
259 "Control flow stack pushes and pops should be balanced.");
264 DEBUG(
dbgs() <<
"********** CFG Sorting **********\n"
265 "********** Function: "
268 const auto &MLI = getAnalysis<MachineLoopInfo>();
269 auto &MDT = getAnalysis<MachineDominatorTree>();
void push_back(const T &Elt)
MachineBasicBlock * LoopBottom(const MachineLoop *Loop)
Return the "bottom" block of a loop.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_NODISCARD T pop_back_val()
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
void moveAfter(MachineBasicBlock *NewBefore)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
size_type size() const
Determine the number of elements in the SetVector.
BlockT * getHeader() const
iterator_range< succ_iterator > successors()
AnalysisUsage & addRequired()
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
iterator_range< iterator > terminators()
void pop_back()
Remove the last element of the SetVector.
LLVM_NODISCARD bool empty() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
This file contains the declaration of the WebAssembly-specific utility functions. ...
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
Return the innermost loop that BB lives in.
static void SortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI, const MachineDominatorTree &MDT)
Sort the blocks, taking special care to make sure that loops are not interrupted by blocks not domina...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
This file declares the WebAssembly-specific subclass of TargetSubtarget.
A SetVector that performs no allocations if smaller than a certain size.
void updateTerminator()
Update the terminator instructions in block to account for changes to the layout. ...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Representation of each machine instruction.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Represents a single loop in the control flow graph.
const T & back() const
Return the last element of the SetVector.
static void MaybeUpdateTerminator(MachineBasicBlock *MBB)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
PriorityQueue - This class behaves like std::priority_queue and provides a few additional convenience...
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
FunctionPass * createWebAssemblyCFGSort()