|
LLVM
4.0.0
|
This file implements a CFG stacking pass. More...
#include "WebAssembly.h"#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"#include "WebAssemblyMachineFunctionInfo.h"#include "WebAssemblySubtarget.h"#include "WebAssemblyUtilities.h"#include "llvm/ADT/PriorityQueue.h"#include "llvm/ADT/SetVector.h"#include "llvm/CodeGen/MachineDominators.h"#include "llvm/CodeGen/MachineFunction.h"#include "llvm/CodeGen/MachineInstrBuilder.h"#include "llvm/CodeGen/MachineLoopInfo.h"#include "llvm/CodeGen/MachineRegisterInfo.h"#include "llvm/CodeGen/Passes.h"#include "llvm/Support/Debug.h"#include "llvm/Support/raw_ostream.h"Go to the source code of this file.
Macros | |
| #define | DEBUG_TYPE "wasm-cfg-stackify" |
Functions | |
| static MachineBasicBlock * | LoopBottom (const MachineLoop *Loop) |
| Return the "bottom" block of a loop. More... | |
| static void | MaybeUpdateTerminator (MachineBasicBlock *MBB) |
| 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 dominated by their header. More... | |
| static bool | ExplicitlyBranchesTo (MachineBasicBlock *Pred, MachineBasicBlock *MBB) |
| Test whether Pred has any terminators explicitly branching to MBB, as opposed to falling through. More... | |
| 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). More... | |
| 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). More... | |
| static unsigned | GetDepth (const SmallVectorImpl< const MachineBasicBlock * > &Stack, const MachineBasicBlock *MBB) |
| 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 an infinite loop or a noreturn call or similar, it isn't necessary to worry about the function return type at the end of the function, because it's never reached. More... | |
| static void | PlaceMarkers (MachineFunction &MF, const MachineLoopInfo &MLI, const WebAssemblyInstrInfo &TII, MachineDominatorTree &MDT, WebAssemblyFunctionInfo &MFI) |
| Insert LOOP and BLOCK markers at appropriate places. More... | |
This file implements a CFG stacking pass.
This pass reorders the blocks in a function to put them into topological order, ignoring loop backedges, and without any loop being interrupted by a block not dominated by the loop header, with special care to keep the order as similar as possible to the original order.
Then, it inserts BLOCK and LOOP markers to mark the start of scopes, since scope boundaries serve as the labels for WebAssembly's control transfers.
This is sufficient to convert arbitrary CFGs into a form that works on WebAssembly, provided that all loops are single-entry.
Definition in file WebAssemblyCFGStackify.cpp.
| #define DEBUG_TYPE "wasm-cfg-stackify" |
Definition at line 43 of file WebAssemblyCFGStackify.cpp.
|
static |
Test whether Pred has any terminators explicitly branching to MBB, as opposed to falling through.
Note that it's possible (eg. in unoptimized code) for a branch instruction to both branch to a block and fallthrough to it, so we check the actual branch operands to see if there are any explicit mentions.
Definition at line 287 of file WebAssemblyCFGStackify.cpp.
References MBB, MI, and llvm::MachineBasicBlock::terminators().
Referenced by PlaceBlockMarker().
|
static |
In normal assembly languages, when the end of a function is unreachable, because the function ends in an infinite loop or a noreturn call or similar, it isn't necessary to worry about the function return type at the end of the function, because it's never reached.
However, in WebAssembly, blocks that end at the function end need to have a return type signature that matches the function signature, even though it's unreachable. This function checks for such cases and fixes up the signatures.
Definition at line 450 of file WebAssemblyCFGStackify.cpp.
References assert(), llvm::bitc::END_BLOCK, llvm::MVT::f32, llvm::WebAssembly::F32, llvm::WebAssembly::F32x4, llvm::MVT::f64, llvm::WebAssembly::F64, llvm::WebAssemblyFunctionInfo::getResults(), llvm::WebAssembly::I16x8, llvm::MVT::i32, llvm::WebAssembly::I32, llvm::WebAssembly::I32x4, llvm::MVT::i64, llvm::WebAssembly::I64, llvm::WebAssembly::I8x16, llvm_unreachable, MBB, MI, llvm::reverse(), llvm::MVT::v16i8, llvm::MVT::v4f32, llvm::MVT::v4i32, and llvm::MVT::v8i16.
Referenced by PlaceMarkers().
|
static |
Definition at line 431 of file WebAssemblyCFGStackify.cpp.
References assert(), llvm::Depth, llvm::reverse(), llvm::SmallVectorTemplateCommon< T >::size(), and X.
Referenced by PlaceMarkers().
|
static |
Return the "bottom" block of a loop.
This differs from MachineLoop::getBottomBlock in that it works even if the loop is discontiguous.
Definition at line 74 of file WebAssemblyCFGStackify.cpp.
References llvm::LoopBase< N, M >::blocks(), llvm::LoopBase< N, M >::getHeader(), llvm::MachineBasicBlock::getNumber(), and MBB.
Referenced by PlaceBlockMarker(), PlaceLoopMarker(), and SortBlocks().
|
static |
Definition at line 82 of file WebAssemblyCFGStackify.cpp.
References assert(), llvm::MachineBasicBlock::terminators(), and llvm::MachineBasicBlock::updateTerminator().
Referenced by SortBlocks().
|
static |
Insert a BLOCK marker for branches to MBB (if needed).
Definition at line 297 of file WebAssemblyCFGStackify.cpp.
References assert(), llvm::MachineBasicBlock::begin(), llvm::BuildMI(), E, llvm::WebAssembly::End, llvm::MachineBasicBlock::end(), llvm::bitc::END_BLOCK, ExplicitlyBranchesTo(), llvm::MachineDominatorTree::findNearestCommonDominator(), llvm::MachineFunction::front(), llvm::MachineBasicBlock::getFirstTerminator(), llvm::MachineLoopInfo::getLoopFor(), llvm::MachineBasicBlock::getNumber(), I, llvm::WebAssembly::isChild(), LoopBottom(), llvm::Number, llvm::MachineBasicBlock::predecessors(), and llvm::WebAssembly::Void.
Referenced by PlaceMarkers().
|
static |
Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
Definition at line 386 of file WebAssemblyCFGStackify.cpp.
References llvm::MachineBasicBlock::addSuccessor(), assert(), llvm::MachineBasicBlock::begin(), llvm::BuildMI(), llvm::MachineFunction::CreateMachineBasicBlock(), llvm::WebAssembly::End, llvm::MachineBasicBlock::end(), llvm::MachineFunction::end(), llvm::LoopBase< N, M >::getHeader(), llvm::MachineLoopInfo::getLoopFor(), llvm::MachineBasicBlock::getNumber(), LoopBottom(), MBB, llvm::MachineFunction::push_back(), and llvm::WebAssembly::Void.
Referenced by PlaceMarkers().
|
static |
Insert LOOP and BLOCK markers at appropriate places.
Definition at line 492 of file WebAssemblyCFGStackify.cpp.
References assert(), llvm::SmallVectorTemplateCommon< T >::back(), llvm::MachineOperand::CreateImm(), llvm::SmallVectorBase::empty(), llvm::bitc::END_BLOCK, FixEndsAtEndOfFunction(), GetDepth(), llvm::MachineBasicBlock::getNumber(), llvm::MachineFunction::getNumBlockIDs(), getParent(), MBB, MI, PlaceBlockMarker(), PlaceLoopMarker(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::pop_back(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), and llvm::reverse().
|
static |
Sort the blocks, taking special care to make sure that loops are not interrupted by blocks not dominated by their header.
TODO: There are many opportunities for improving the heuristics here. Explore them.
Definition at line 133 of file WebAssemblyCFGStackify.cpp.
References assert(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::back(), llvm::SmallVectorTemplateCommon< T, typename >::back(), llvm::LoopBase< N, M >::contains(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::count(), llvm::MachineDominatorTree::dominates(), E, llvm::SmallVectorBase::empty(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::empty(), llvm::MachineBasicBlock::front(), llvm::LoopBase< N, M >::getHeader(), llvm::MachineLoopInfo::getLoopFor(), llvm::MachineBasicBlock::getNumber(), llvm::MachineFunction::getNumBlockIDs(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), L, LoopBottom(), Loops, MaybeUpdateTerminator(), MBB, llvm::MachineBasicBlock::moveAfter(), N, llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::pop_back(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::pop_back_val(), llvm::MachineBasicBlock::pred_size(), llvm::MachineBasicBlock::predecessors(), llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), llvm::MachineFunction::RenumberBlocks(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size(), and llvm::MachineBasicBlock::successors().
1.8.6