35 #define DEBUG_TYPE "wasm-cfg-sort" 39 StringRef getPassName()
const override {
return "WebAssembly CFG Sort"; }
60 "Reorders blocks in topological order",
false,
false)
63 return new WebAssemblyCFGSort();
68 bool AnyBarrier =
false;
70 bool AllAnalyzable =
true;
73 AnyBarrier |= Term.isBarrier();
75 AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
77 assert((AnyBarrier || AllAnalyzable) &&
78 "AnalyzeBranch needs to analyze any block with a fallthrough");
85 struct CompareBlockNumbers {
92 struct CompareBlockNumbersBackwards {
102 unsigned NumBlocksLeft;
106 std::vector<MachineBasicBlock *> Deferred;
124 unsigned N = MBB.pred_size();
126 if (L->getHeader() == &MBB)
128 if (L->contains(Pred))
130 NumPredsLeft[MBB.getNumber()] =
N;
144 CompareBlockNumbersBackwards>
157 for (Entry &
E : Loops)
158 if (
E.Loop->contains(MBB) && --
E.NumBlocksLeft == 0)
159 for (
auto DeferredBlock :
E.Deferred)
160 Ready.push(DeferredBlock);
161 while (!Loops.empty() && Loops.back().NumBlocksLeft == 0)
168 if (SuccL->getHeader() == Succ && SuccL->contains(MBB))
171 if (--NumPredsLeft[Succ->getNumber()] == 0)
172 Preferred.push(Succ);
177 while (!Preferred.empty()) {
178 Next = Preferred.top();
182 if (!Loops.
empty() &&
184 Loops.
back().Deferred.push_back(Next);
190 if (Next->
getNumber() < MBB->getNumber() &&
212 if (!Loops.
empty() &&
214 Loops.
back().Deferred.push_back(Next);
225 assert(Loops.
empty() &&
"Active loop list not finished");
236 for (
auto &MBB : MF) {
237 assert(MBB.getNumber() >= 0 &&
"Renumbered blocks should be non-negative.");
240 if (Loop && &MBB == Loop->getHeader()) {
243 for (
auto Pred : MBB.predecessors())
245 (Pred->getNumber() < MBB.getNumber() || Loop->contains(Pred)) &&
246 "Loop header predecessors must be loop predecessors or backedges");
247 assert(OnStack.
insert(Loop) &&
"Loops should be declared at most once.");
250 for (
auto Pred : MBB.predecessors())
251 assert(Pred->getNumber() < MBB.getNumber() &&
252 "Non-loop-header predecessors should be topologically sorted");
254 "Blocks must be nested in their loops");
260 "The function entry block shouldn't actually be a loop header");
262 "Control flow stack pushes and pops should be balanced.");
268 "********** Function: " 271 const auto &MLI = getAnalysis<MachineLoopInfo>();
272 auto &MDT = getAnalysis<MachineDominatorTree>();
void push_back(const T &Elt)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_NODISCARD T pop_back_val()
Compute iterated dominance frontiers using a linear time algorithm.
FunctionPass * createWebAssemblyCFGSort()
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
size_type size() const
Determine the number of elements in the SetVector.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
INITIALIZE_PASS(WebAssemblyCFGSort, DEBUG_TYPE, "Reorders blocks in topological order", false, false) FunctionPass *llvm
void moveAfter(MachineBasicBlock *NewBefore)
const T & back() const
Return the last element of the SetVector.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
iterator_range< succ_iterator > successors()
AnalysisUsage & addRequired()
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.
BlockT * getHeader() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
This file contains the declaration of the WebAssembly-specific utility functions. ...
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...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
A SetVector that performs no allocations if smaller than a certain size.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
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.
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
Represents a single loop in the control flow graph.
bool empty() const
Determine if the SetVector is empty or not.
static void MaybeUpdateTerminator(MachineBasicBlock *MBB)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineBasicBlock * getBottom(const T *Unit)
Return the "bottom" block of an entity, which can be either a MachineLoop or WebAssemblyException.
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
Return the innermost loop that BB lives in.
StringRef - Represent a constant reference to a string, i.e.
PriorityQueue - This class behaves like std::priority_queue and provides a few additional convenience...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...