39#define DEBUG_TYPE "wasm-cfg-sort"
44 StringRef getPassName()
const override {
return "WebAssembly CFG Sort"; }
62char WebAssemblyCFGSortLegacy::ID = 0;
64 "Reorders blocks in topological order",
false,
false)
67 return new WebAssemblyCFGSortLegacy();
72 bool AnyBarrier =
false;
74 bool AllAnalyzable =
true;
77 AnyBarrier |= Term.isBarrier();
79 AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
81 assert((AnyBarrier || AllAnalyzable) &&
82 "analyzeBranch needs to analyze any block with a fallthrough");
92 MBB->updateTerminator(OriginalSuccessor);
130struct CompareBlockNumbers {
132 bool operator()(
const MachineBasicBlock *
A,
133 const MachineBasicBlock *
B)
const {
135 if (
A->isEHPad() && !
B->isEHPad())
137 if (!
A->isEHPad() &&
B->isEHPad())
141 return A->getNumber() >
B->getNumber();
145struct CompareBlockNumbersBackwards {
147 bool operator()(
const MachineBasicBlock *
A,
148 const MachineBasicBlock *
B)
const {
150 if (
A->isEHPad() && !
B->isEHPad())
152 if (!
A->isEHPad() &&
B->isEHPad())
156 return A->getNumber() <
B->getNumber();
162 const SortRegion *TheRegion;
163 unsigned NumBlocksLeft;
167 std::vector<MachineBasicBlock *> Deferred;
169 explicit Entry(
const SortRegion *R)
170 : TheRegion(
R), NumBlocksLeft(
R->getNumBlocks()) {}
189 unsigned N =
MBB.pred_size();
191 if (L->getHeader() == &
MBB)
193 if (L->contains(
Pred))
195 NumPredsLeft[
MBB.getNumber()] =
N;
198 const bool EHPadSort =
210 Preferred(CompareBlockNumbers{EHPadSort});
212 CompareBlockNumbersBackwards>
213 Ready(CompareBlockNumbersBackwards{EHPadSort});
215 SortRegionInfo SRI(MLI, WEI);
218 const SortRegion *R = SRI.getRegionFor(
MBB);
223 if (R->getHeader() ==
MBB)
228 for (Entry &
E : Entries)
229 if (
E.TheRegion->contains(
MBB) && --
E.NumBlocksLeft == 0)
230 for (
auto *DeferredBlock :
E.Deferred)
231 Ready.push(DeferredBlock);
232 while (!Entries.empty() && Entries.back().NumBlocksLeft == 0)
239 if (SuccL->getHeader() == Succ && SuccL->contains(
MBB))
242 if (--NumPredsLeft[Succ->getNumber()] == 0)
243 Preferred.push(Succ);
248 while (!Preferred.empty()) {
249 Next = Preferred.top();
253 if (!Entries.
empty() &&
255 Entries.
back().Deferred.push_back(
Next);
261 if (
Next->getNumber() <
MBB->getNumber() &&
262 (!EHPadSort || !
Next->isEHPad()) &&
263 (!R || !R->contains(
Next) ||
264 R->getHeader()->getNumber() <
Next->getNumber())) {
284 if (!Entries.
empty() &&
286 Entries.
back().Deferred.push_back(
Next);
297 assert(Entries.
empty() &&
"Active sort region list not finished");
301 for (
auto &
MBB : MF) {
302 assert(
MBB.getNumber() >= 0 &&
"Renumbered blocks should be non-negative.");
303 const SortRegion *
Region = SRI.getRegionFor(&
MBB);
310 for (
auto *
Pred :
MBB.predecessors())
313 "Loop header predecessors must be loop predecessors or "
317 for (
auto *
Pred :
MBB.predecessors())
319 "Non-loop-header predecessors should be topologically sorted");
323 for (
auto *
Pred :
MBB.predecessors())
325 "Non-loop-header predecessors should be topologically sorted");
330 for (
auto &
MBB : MF) {
331 const SortRegion *
Region = SRI.getRegionFor(&
MBB);
338 unsigned RegionIdx = 0;
339 for (
auto *
Region : Regions) {
341 "The function entry block shouldn't actually be a region header");
343 auto *Header =
Region->getHeader();
344 auto *Bottom = SRI.getBottom(
Region);
346 assert(Header &&
"Regions must have a header");
347 assert(Bottom &&
"Regions must have a bottom");
349 std::pair<int, int>
Interval = {Header->getNumber(), Bottom->getNumber()};
351 "Region bottoms must be sorted after region headers");
353 RegionIntervals[RegionIdx++] =
Interval;
358 "All blocks within a region must have numbers within the region's "
363 for (
const auto &IntervalA : RegionIntervals) {
364 for (
const auto &IntervalB : RegionIntervals) {
365 auto AContainsB = IntervalA.first <= IntervalB.first &&
366 IntervalA.second >= IntervalB.second;
367 auto BContainsA = IntervalB.first <= IntervalA.first &&
368 IntervalB.second >= IntervalA.second;
369 auto Disjoint = IntervalA.second < IntervalB.first ||
370 IntervalA.first > IntervalB.second;
371 assert((AContainsB || BContainsA || Disjoint) &&
372 "Regions must be fully contained within their parents and not "
373 "overlap their siblings");
382 "********** Function: "
394bool WebAssemblyCFGSortLegacy::runOnMachineFunction(
MachineFunction &MF) {
395 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
396 WebAssemblyExceptionInfo &WEI =
397 getAnalysis<WebAssemblyExceptionInfoWrapperPass>().getWEI();
398 MachineDominatorTree &MDT =
399 getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
400 return sortCFG(MF, MLI, WEI, MDT);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
std::pair< uint64_t, uint64_t > Interval
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the PriorityQueue class.
static bool sortCFG(MachineFunction &MF, MachineLoopInfo &MLI, WebAssemblyExceptionInfo &WEI, MachineDominatorTree &MDT)
static void maybeUpdateTerminator(MachineBasicBlock *MBB)
This file implements WebAssemblyException information analysis.
This file implements regions used in CFGSort and CFGStackify.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineBasicBlock & front() const
Representation of each machine instruction.
Analysis pass that exposes the MachineLoopInfo for a machine function.
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PriorityQueue - This class behaves like std::priority_queue and provides a few additional convenience...
block_range blocks()
Returns a range view of the basic blocks in the region.
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
bool sortBlocks(Function &F)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
FunctionPass * createWebAssemblyCFGSortLegacyPass()