62 #define DEBUG_TYPE "wasm-fix-irreducible-control-flow" 72 class ReachabilityGraph {
75 : Entry(Entry), Blocks(Blocks) {
78 for (
auto *MBB : Blocks) {
80 for (
auto *Pred : MBB->predecessors()) {
90 assert(inRegion(From) && inRegion(To));
91 auto I = Reachable.find(From);
92 if (
I == Reachable.end())
94 return I->second.count(To);
99 const BlockSet &getLoopers()
const {
return Loopers; }
102 const BlockSet &getLoopEntries()
const {
return LoopEntries; }
106 assert(inRegion(LoopEntry));
107 auto I = LoopEnterers.find(LoopEntry);
108 assert(
I != LoopEnterers.end());
114 const BlockSet &Blocks;
116 BlockSet Loopers, LoopEntries;
127 using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
131 for (
auto *MBB : Blocks) {
133 if (Succ != Entry && inRegion(Succ)) {
134 Reachable[MBB].
insert(Succ);
140 while (!WorkList.
empty()) {
143 assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
148 if (Reachable[Pred].insert(Succ).second) {
156 for (
auto *MBB : Blocks) {
157 if (canReach(MBB, MBB)) {
161 assert(!Loopers.count(Entry));
165 for (
auto *Looper : Loopers) {
166 for (
auto *Pred : Looper->predecessors()) {
169 if (!canReach(Looper, Pred)) {
170 LoopEntries.insert(Looper);
171 LoopEnterers[Looper].
insert(Pred);
183 : Entry(Entry), Enterers(Enterers) {
187 BlockSet &getBlocks() {
return Blocks; }
191 const BlockSet &Enterers;
198 BlockVector WorkList;
199 BlockSet AddedToWorkList;
202 if (!Enterers.count(Pred)) {
203 WorkList.push_back(Pred);
204 AddedToWorkList.insert(Pred);
208 while (!WorkList.empty()) {
209 auto *MBB = WorkList.pop_back_val();
210 assert(!Enterers.count(MBB));
211 if (Blocks.insert(MBB).second) {
212 for (
auto *Pred : MBB->predecessors()) {
213 if (!AddedToWorkList.count(Pred)) {
214 WorkList.push_back(Pred);
215 AddedToWorkList.insert(Pred);
225 return "WebAssembly Fix Irreducible Control Flow";
233 void makeSingleEntryLoop(BlockSet &Entries, BlockSet &Blocks,
241 bool WebAssemblyFixIrreducibleControlFlow::processRegion(
243 bool Changed =
false;
248 ReachabilityGraph Graph(Entry, Blocks);
250 bool FoundIrreducibility =
false;
252 for (
auto *LoopEntry : Graph.getLoopEntries()) {
272 BlockSet MutualLoopEntries;
273 MutualLoopEntries.insert(LoopEntry);
274 for (
auto *OtherLoopEntry : Graph.getLoopEntries()) {
275 if (OtherLoopEntry != LoopEntry &&
276 Graph.canReach(LoopEntry, OtherLoopEntry) &&
277 Graph.canReach(OtherLoopEntry, LoopEntry)) {
278 MutualLoopEntries.insert(OtherLoopEntry);
282 if (MutualLoopEntries.size() > 1) {
283 makeSingleEntryLoop(MutualLoopEntries, Blocks, MF, Graph);
284 FoundIrreducibility =
true;
295 if (FoundIrreducibility) {
299 for (
auto *LoopEntry : Graph.getLoopEntries()) {
300 LoopBlocks InnerBlocks(LoopEntry, Graph.getLoopEnterers(LoopEntry));
307 if (processRegion(LoopEntry, InnerBlocks.getBlocks(), MF)) {
322 void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
324 const ReachabilityGraph &Graph) {
325 assert(Entries.size() >= 2);
328 BlockVector SortedEntries(Entries.begin(), Entries.end());
337 for (
auto Block : SortedEntries)
338 assert(Block->getNumber() != -1);
339 if (SortedEntries.size() > 1) {
340 for (
auto I = SortedEntries.begin(),
E = SortedEntries.end() - 1;
I !=
E;
342 auto ANum = (*I)->getNumber();
343 auto BNum = (*(std::next(
I)))->getNumber();
352 Blocks.insert(Dispatch);
362 Register Reg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
368 for (
auto *Entry : SortedEntries) {
369 auto Pair = Indices.
insert(std::make_pair(Entry, 0));
373 Pair.first->second =
Index;
383 BlockVector AllPreds;
384 for (
auto *Entry : SortedEntries) {
386 if (Pred != Dispatch) {
387 AllPreds.push_back(Pred);
394 for (
auto *Pred : AllPreds) {
396 if (!Entries.count(Entry))
398 if (Graph.canReach(Entry, Pred)) {
409 for (
auto *Pred : AllPreds)
411 if (Entries.count(Entry) && Pred->isLayoutSuccessor(Entry))
412 EntryToLayoutPred[std::make_pair(InLoop.
count(Pred), Entry)] = Pred;
419 for (
auto *Pred : AllPreds) {
420 bool PredInLoop = InLoop.
count(Pred);
422 if (!Entries.count(Entry) ||
423 Map.count(std::make_pair(InLoop.
count(Pred), Entry)))
428 if (EntryToLayoutPred.count(std::make_pair(PredInLoop, Entry)) &&
429 EntryToLayoutPred[std::make_pair(PredInLoop, Entry)] != Pred)
434 MF.
insert(Pred->isLayoutSuccessor(Entry)
438 Blocks.insert(Routing);
443 .addImm(Indices[Entry]);
446 Map[std::make_pair(PredInLoop, Entry)] = Routing;
450 for (
auto *Pred : AllPreds) {
451 bool PredInLoop = InLoop.
count(Pred);
454 for (
auto &
Op : Term.explicit_uses())
455 if (
Op.isMBB() && Indices.
count(
Op.getMBB()))
456 Op.setMBB(Map[std::make_pair(PredInLoop,
Op.getMBB())]);
458 for (
auto *Succ : Pred->successors()) {
459 if (!Entries.count(Succ))
461 auto *Routing = Map[std::make_pair(PredInLoop, Succ)];
462 Pred->replaceSuccessor(Succ, Routing);
476 "Removes irreducible control flow",
false,
false)
479 return new WebAssemblyFixIrreducibleControlFlow();
482 bool WebAssemblyFixIrreducibleControlFlow::runOnMachineFunction(
484 LLVM_DEBUG(
dbgs() <<
"********** Fixing Irreducible Control Flow **********\n" 485 "********** Function: " 490 for (
auto &MBB : MF) {
491 AllBlocks.insert(&MBB);
494 if (
LLVM_UNLIKELY(processRegion(&*MF.begin(), AllBlocks, MF))) {
496 MF.getRegInfo().invalidateLiveness();
reference emplace_back(ArgTypes &&... Args)
This class represents lattice values for constants.
#define LLVM_UNLIKELY(EXPR)
Implements a dense probed hash-table based set.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
iterator_range< succ_iterator > successors()
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
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.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(const ValueT &V)
This file provides WebAssembly-specific target descriptions.
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
void sort(IteratorTy Start, IteratorTy End)
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.
BlockVerifier::State From
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly. ...
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.
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
INITIALIZE_PASS(WebAssemblyFixIrreducibleControlFlow, DEBUG_TYPE, "Removes irreducible control flow", false, false) FunctionPass *llvm
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
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)
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.