36 #define DEBUG_TYPE "wasm-cfg-stackify" 40 StringRef getPassName()
const override {
return "WebAssembly CFG Stackify"; }
61 "Insert BLOCK and LOOP markers for WebAssembly scopes",
65 return new WebAssemblyCFGStackify();
77 if (MO.isMBB() && MO.getMBB() == MBB)
96 bool IsBranchedTo =
false;
99 if (Pred->getNumber() < MBBNumber) {
109 assert(&MBB != &MF.
front() &&
"Header blocks shouldn't have predecessors");
116 if (ScopeTop->getNumber() > Header->
getNumber()) {
134 InsertPos = Header->
begin();
136 InsertPos->getOpcode() == WebAssembly::END_LOOP)
142 while (InsertPos != Header->
begin() &&
146 std::prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP)
153 TII.get(WebAssembly::BLOCK))
157 InsertPos = MBB.
begin();
158 while (InsertPos != MBB.
end() &&
159 InsertPos->getOpcode() == WebAssembly::END_LOOP &&
160 LoopTops[&*InsertPos]->getParent()->getNumber() >= Header->
getNumber())
164 BlockTops[End] = Begin;
168 if (!ScopeTops[Number] ||
169 ScopeTops[Number]->getNumber() > Header->
getNumber())
170 ScopeTops[Number] = Header;
187 if (Iter == MF.
end()) {
198 auto InsertPos = MBB.
begin();
199 while (InsertPos != MBB.
end() &&
200 InsertPos->getOpcode() == WebAssembly::END_LOOP)
210 TII.get(WebAssembly::END_LOOP));
211 LoopTops[End] = Begin;
215 "With block sorting the outermost loop for a block should be first.");
217 ScopeTops[AfterLoop->
getNumber()] = &MBB;
229 assert(Depth < Stack.
size() &&
"Branch destination should be in scope");
266 if (
MI.isPosition() ||
MI.isDebugInstr())
269 BlockTops[&
MI]->getOperand(0).setImm(int32_t(retType));
272 if (
MI.getOpcode() == WebAssembly::END_LOOP) {
273 LoopTops[&
MI]->getOperand(0).setImm(int32_t(retType));
289 TII.get(WebAssembly::END_FUNCTION));
310 for (
auto &MBB : MF) {
315 PlaceBlockMarker(MBB, MF, ScopeTops, BlockTops, LoopTops, TII, MLI, MDT, MFI);
320 for (
auto &MBB :
reverse(MF)) {
322 switch (
MI.getOpcode()) {
323 case WebAssembly::BLOCK:
325 "Block should be balanced");
329 assert(Stack.
back() == &MBB &&
"Loop top should be balanced");
335 case WebAssembly::END_LOOP:
339 if (
MI.isTerminator()) {
342 while (
MI.getNumOperands() > 0)
343 MI.RemoveOperand(
MI.getNumOperands() - 1);
344 for (
auto MO : Ops) {
347 MI.addOperand(MF, MO);
354 assert(Stack.
empty() &&
"Control flow should be balanced");
364 bool WebAssemblyCFGStackify::runOnMachineFunction(
MachineFunction &MF) {
366 "********** Function: " 369 const auto &MLI = getAnalysis<MachineLoopInfo>();
370 auto &MDT = getAnalysis<MachineDominatorTree>();
pred_reverse_iterator pred_rbegin()
void push_back(const T &Elt)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Compute iterated dominance frontiers using a linear time algorithm.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI, const WebAssemblyInstrInfo &TII, MachineDominatorTree &MDT, WebAssemblyFunctionInfo &MFI)
Insert LOOP and BLOCK markers at appropriate places.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred, MachineBasicBlock *MBB)
Test whether Pred has any terminators explicitly branching to MBB, as opposed to falling through...
AnalysisUsage & addRequired()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
INITIALIZE_PASS(WebAssemblyCFGStackify, DEBUG_TYPE, "Insert BLOCK and LOOP markers for WebAssembly scopes", false, false) FunctionPass *llvm
static unsigned GetDepth(const SmallVectorImpl< const MachineBasicBlock *> &Stack, const MachineBasicBlock *MBB)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator_range< iterator > terminators()
static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF, SmallVectorImpl< MachineBasicBlock *> &ScopeTops, DenseMap< const MachineInstr *, MachineInstr *> &BlockTops, DenseMap< const MachineInstr *, MachineInstr *> &LoopTops, const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI, MachineDominatorTree &MDT, WebAssemblyFunctionInfo &MFI)
Insert a BLOCK marker for branches to MBB (if needed).
BlockT * getHeader() const
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
DebugLoc findPrevDebugLoc(instr_iterator MBBI)
Find the previous valid DebugLoc preceding MBBI, skipping and DBG_VALUE instructions.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
static void AppendEndToFunction(MachineFunction &MF, const WebAssemblyInstrInfo &TII)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
This file contains the declaration of the WebAssembly-specific utility functions. ...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE and DBG_LABEL instructions...
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
const MachineBasicBlock & front() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
Iterator for intrusive lists based on ilist_node.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
ExprType
This is used to indicate block signatures.
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.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
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.
static MachineOperand CreateImm(int64_t Val)
This file declares WebAssembly-specific per-machine-function information.
const MachineBasicBlock & back() const
const std::vector< MVT > & getResults() const
FunctionPass * createWebAssemblyCFGStackify()
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.
void push_back(MachineBasicBlock *MBB)
static const Function * getParent(const Value *V)
static void PlaceLoopMarker(MachineBasicBlock &MBB, MachineFunction &MF, SmallVectorImpl< MachineBasicBlock *> &ScopeTops, DenseMap< const MachineInstr *, MachineInstr *> &LoopTops, const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI)
Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
StringRef - Represent a constant reference to a string, i.e.
static void FixEndsAtEndOfFunction(MachineFunction &MF, const WebAssemblyFunctionInfo &MFI, DenseMap< const MachineInstr *, MachineInstr *> &BlockTops, DenseMap< const MachineInstr *, MachineInstr *> &LoopTops)
In normal assembly languages, when the end of a function is unreachable, because the function ends in...
bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI)
Test whether MI is a child of some other node in an expression tree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...