36 #define DEBUG_TYPE "wasm-cfg-stackify"
40 StringRef getPassName()
const override {
return "WebAssembly CFG Stackify"; }
61 return new WebAssemblyCFGStackify();
73 if (MO.isMBB() && MO.getMBB() == MBB)
92 bool IsBranchedTo =
false;
95 if (Pred->getNumber() < MBBNumber) {
105 assert(&MBB != &MF.
front() &&
"Header blocks shouldn't have predecessors");
112 if (ScopeTop->getNumber() > Header->
getNumber()) {
129 InsertPos = Header->
begin();
131 InsertPos->getOpcode() == WebAssembly::END_LOOP)
137 while (InsertPos != Header->
begin() &&
141 std::prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP)
147 TII.get(WebAssembly::BLOCK))
151 InsertPos = MBB.
begin();
152 while (InsertPos != MBB.
end() &&
153 InsertPos->getOpcode() == WebAssembly::END_LOOP &&
154 LoopTops[&*InsertPos]->getParent()->getNumber() >= Header->
getNumber())
158 BlockTops[
End] = Begin;
162 if (!ScopeTops[Number] ||
163 ScopeTops[Number]->getNumber() > Header->
getNumber())
164 ScopeTops[Number] = Header;
181 if (Iter == MF.
end()) {
192 auto InsertPos = MBB.
begin();
193 while (InsertPos != MBB.
end() &&
194 InsertPos->getOpcode() == WebAssembly::END_LOOP)
202 TII.get(WebAssembly::END_LOOP));
203 LoopTops[
End] = Begin;
207 "With block sorting the outermost loop for a block should be first.");
209 ScopeTops[AfterLoop->
getNumber()] = &MBB;
221 assert(Depth < Stack.
size() &&
"Branch destination should be in scope");
257 if (
MI.isPosition() ||
MI.isDebugValue())
260 BlockTops[&
MI]->getOperand(0).setImm(int32_t(retType));
263 if (
MI.getOpcode() == WebAssembly::END_LOOP) {
264 LoopTops[&
MI]->getOperand(0).setImm(int32_t(retType));
279 TII.get(WebAssembly::END_FUNCTION));
300 for (
auto &MBB : MF) {
305 PlaceBlockMarker(MBB, MF, ScopeTops, BlockTops, LoopTops, TII, MLI, MDT, MFI);
310 for (
auto &MBB :
reverse(MF)) {
312 switch (
MI.getOpcode()) {
313 case WebAssembly::BLOCK:
315 "Block should be balanced");
319 assert(Stack.
back() == &MBB &&
"Loop top should be balanced");
325 case WebAssembly::END_LOOP:
329 if (
MI.isTerminator()) {
332 while (
MI.getNumOperands() > 0)
333 MI.RemoveOperand(
MI.getNumOperands() - 1);
334 for (
auto MO : Ops) {
337 MI.addOperand(MF, MO);
344 assert(Stack.
empty() &&
"Control flow should be balanced");
356 bool WebAssemblyCFGStackify::runOnMachineFunction(
MachineFunction &MF) {
357 DEBUG(
dbgs() <<
"********** CFG Stackifying **********\n"
358 "********** Function: "
361 const auto &MLI = getAnalysis<MachineLoopInfo>();
362 auto &MDT = getAnalysis<MachineDominatorTree>();
void push_back(const T &Elt)
const Triple & getTargetTriple() const
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.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
static unsigned GetDepth(const SmallVectorImpl< const MachineBasicBlock * > &Stack, const MachineBasicBlock *MBB)
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...
BlockT * getHeader() const
AnalysisUsage & addRequired()
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator_range< iterator > terminators()
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...
ExprType
This is used to indicate block signatures.
LLVM_NODISCARD bool empty() const
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).
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
const MachineBasicBlock & front() const
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)
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.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static const unsigned End
const std::vector< MVT > & getResults() const
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()
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).
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...
FunctionPass * createWebAssemblyCFGStackify()
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.
llvm_unreachable("Unknown RegBank!")
Representation of each machine instruction.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Represents a single loop in the control flow graph.
static MachineOperand CreateImm(int64_t Val)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
This file declares WebAssembly-specific per-machine-function information.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void push_back(MachineBasicBlock *MBB)
static const Function * getParent(const Value *V)
const MachineBasicBlock & back() const
StringRef - Represent a constant reference to a string, i.e.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
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...