46 #define DEBUG_TYPE "wasm-fix-irreducible-control-flow" 51 return "WebAssembly Fix Irreducible Control Flow";
75 "Removes irreducible control flow",
false,
false)
78 return new WebAssemblyFixIrreducibleControlFlow();
111 bool operator==(
const MetaBlock &MBB) {
return Block == MBB.Block; }
112 bool operator!=(
const MetaBlock &MBB) {
return Block != MBB.Block; }
115 class SuccessorList final :
public MetaBlock {
126 bool HasNext()
const {
return Index != Num; }
136 bool WebAssemblyFixIrreducibleControlFlow::VisitLoop(
MachineFunction &MF,
148 LoopWorklist.
push_back(SuccessorList(Header));
151 while (!LoopWorklist.
empty()) {
152 SuccessorList &Top = LoopWorklist.
back();
155 if (Next == Header || (Loop && !Loop->
contains(Next)))
158 if (!Visited.
insert(Next).second) {
163 if (InnerLoop != Loop)
164 LoopWorklist.
push_back(SuccessorList(InnerLoop));
166 LoopWorklist.
push_back(SuccessorList(Next));
168 RewriteSuccs.
insert(Top.getBlock());
172 OnStack.
erase(Top.getBlock());
191 TII.get(WebAssembly::BR_TABLE_I32));
196 unsigned Reg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
204 while (!SuccWorklist.empty()) {
206 auto Pair = Indices.
insert(std::make_pair(MBB, 0));
214 Pair.first->second =
Index;
216 RewriteSuccs.
insert(Pred);
222 for (
auto *Succ : Meta.successors())
223 if (Succ != Header && (!Loop || Loop->
contains(Succ)))
224 SuccWorklist.push_back(Succ);
232 for (
auto *Succ : MBB->successors()) {
233 if (!Indices.
count(Succ))
246 .addImm(Indices[Succ]);
254 for (
auto &
Op : Term.explicit_uses())
255 if (
Op.isMBB() && Indices.
count(
Op.getMBB()))
256 Op.setMBB(Map[
Op.getMBB()]);
257 for (
auto Rewrite : Map)
258 MBB->replaceSuccessor(Rewrite.first, Rewrite.second);
269 bool WebAssemblyFixIrreducibleControlFlow::runOnMachineFunction(
271 LLVM_DEBUG(
dbgs() <<
"********** Fixing Irreducible Control Flow **********\n" 272 "********** Function: " 275 bool Changed =
false;
276 auto &MLI = getAnalysis<MachineLoopInfo>();
279 Changed |= VisitLoop(MF, MLI,
nullptr);
283 while (!Worklist.empty()) {
285 Worklist.append(CurLoop->
begin(), CurLoop->
end());
286 Changed |= VisitLoop(MF, MLI, CurLoop);
294 getAnalysis<MachineDominatorTree>().runOnMachineFunction(MF);
void push_back(const T &Elt)
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
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.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
iterator end()
Get an iterator to the end of the SetVector.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
AnalysisUsage & addRequired()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
BlockT * getHeader() const
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
bool insert(const value_type &X)
Insert a new element into the SetVector.
iterator begin()
Get an iterator to the beginning of the SetVector.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
BasicBlockListType::iterator iterator
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Control flow instructions. These all have token chains.
unsigned const MachineRegisterInfo * MRI
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.
This file provides WebAssembly-specific target descriptions.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
#define LLVM_LIKELY(EXPR)
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
#define LLVM_UNLIKELY(EXPR)
succ_iterator succ_begin()
pred_iterator pred_begin()
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
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.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
pred_range predecessors(BasicBlock *BB)
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly. ...
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.
FunctionPass * createWebAssemblyFixIrreducibleControlFlow()
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
bool operator!=(uint64_t V1, const APInt &V2)
Representation of each machine instruction.
bool runOnMachineFunction(MachineFunction &F) override
Calculate the natural loop 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.
INITIALIZE_PASS(WebAssemblyFixIrreducibleControlFlow, DEBUG_TYPE, "Removes irreducible control flow", false, false) FunctionPass *llvm
bool empty() const
Determine if the SetVector is empty or not.
This file declares WebAssembly-specific per-machine-function information.
void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L)
Change the top-level loop that contains BB to the specified loop.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
succ_range successors(BasicBlock *BB)
A vector that has set insertion semantics.
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
Return the innermost loop that BB lives in.
StringRef - Represent a constant reference to a string, i.e.
bool operator==(uint64_t V1, const APInt &V2)
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
static void Split(std::vector< std::string > &V, StringRef S)
Splits a string of comma separated items in to a vector of strings.
const MachineOperand & getOperand(unsigned i) const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...