1d7a2eea6SDan Gohman //=- WebAssemblyFixIrreducibleControlFlow.cpp - Fix irreducible control flow -//
2d7a2eea6SDan Gohman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d7a2eea6SDan Gohman //
7d7a2eea6SDan Gohman //===----------------------------------------------------------------------===//
8d7a2eea6SDan Gohman ///
9d7a2eea6SDan Gohman /// \file
10a41250c7SHeejin Ahn /// This file implements a pass that removes irreducible control flow.
11a41250c7SHeejin Ahn /// Irreducible control flow means multiple-entry loops, which this pass
12a41250c7SHeejin Ahn /// transforms to have a single entry.
13d7a2eea6SDan Gohman ///
14ddfa1a6cSDan Gohman /// Note that LLVM has a generic pass that lowers irreducible control flow, but
15ddfa1a6cSDan Gohman /// it linearizes control flow, turning diamonds into two triangles, which is
16ddfa1a6cSDan Gohman /// both unnecessary and undesirable for WebAssembly.
17ddfa1a6cSDan Gohman ///
18a41250c7SHeejin Ahn /// The big picture: We recursively process each "region", defined as a group
19a41250c7SHeejin Ahn /// of blocks with a single entry and no branches back to that entry. A region
20a41250c7SHeejin Ahn /// may be the entire function body, or the inner part of a loop, i.e., the
21a41250c7SHeejin Ahn /// loop's body without branches back to the loop entry. In each region we fix
22a41250c7SHeejin Ahn /// up multi-entry loops by adding a new block that can dispatch to each of the
23a41250c7SHeejin Ahn /// loop entries, based on the value of a label "helper" variable, and we
24a41250c7SHeejin Ahn /// replace direct branches to the entries with assignments to the label
25a41250c7SHeejin Ahn /// variable and a branch to the dispatch block. Then the dispatch block is the
26a41250c7SHeejin Ahn /// single entry in the loop containing the previous multiple entries. After
27a41250c7SHeejin Ahn /// ensuring all the loops in a region are reducible, we recurse into them. The
28a41250c7SHeejin Ahn /// total time complexity of this pass is:
29e9e01cc7SAlon Zakai ///
30a41250c7SHeejin Ahn ///   O(NumBlocks * NumNestedLoops * NumIrreducibleLoops +
31a41250c7SHeejin Ahn ///     NumLoops * NumLoops)
32777d01c7SHeejin Ahn ///
33a41250c7SHeejin Ahn /// This pass is similar to what the Relooper [1] does. Both identify looping
34a41250c7SHeejin Ahn /// code that requires multiple entries, and resolve it in a similar way (in
35a41250c7SHeejin Ahn /// Relooper terminology, we implement a Multiple shape in a Loop shape). Note
36777d01c7SHeejin Ahn /// also that like the Relooper, we implement a "minimal" intervention: we only
37777d01c7SHeejin Ahn /// use the "label" helper for the blocks we absolutely must and no others. We
38a41250c7SHeejin Ahn /// also prioritize code size and do not duplicate code in order to resolve
39a41250c7SHeejin Ahn /// irreducibility. The graph algorithms for finding loops and entries and so
40a41250c7SHeejin Ahn /// forth are also similar to the Relooper. The main differences between this
41a41250c7SHeejin Ahn /// pass and the Relooper are:
42e9e01cc7SAlon Zakai ///
43a41250c7SHeejin Ahn ///  * We just care about irreducibility, so we just look at loops.
44a41250c7SHeejin Ahn ///  * The Relooper emits structured control flow (with ifs etc.), while we
45a41250c7SHeejin Ahn ///    emit a CFG.
46777d01c7SHeejin Ahn ///
47777d01c7SHeejin Ahn /// [1] Alon Zakai. 2011. Emscripten: an LLVM-to-JavaScript compiler. In
48777d01c7SHeejin Ahn /// Proceedings of the ACM international conference companion on Object oriented
49777d01c7SHeejin Ahn /// programming systems languages and applications companion (SPLASH '11). ACM,
50777d01c7SHeejin Ahn /// New York, NY, USA, 301-312. DOI=10.1145/2048147.2048224
51777d01c7SHeejin Ahn /// http://doi.acm.org/10.1145/2048147.2048224
52d7a2eea6SDan Gohman ///
53d7a2eea6SDan Gohman //===----------------------------------------------------------------------===//
54d7a2eea6SDan Gohman 
55d7a2eea6SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
566bda14b3SChandler Carruth #include "WebAssembly.h"
57d7a2eea6SDan Gohman #include "WebAssemblySubtarget.h"
58989f1c72Sserge-sans-paille #include "llvm/CodeGen/MachineFunctionPass.h"
59d7a2eea6SDan Gohman #include "llvm/CodeGen/MachineInstrBuilder.h"
60904cd3e0SReid Kleckner #include "llvm/Support/Debug.h"
61d7a2eea6SDan Gohman using namespace llvm;
62d7a2eea6SDan Gohman 
63d7a2eea6SDan Gohman #define DEBUG_TYPE "wasm-fix-irreducible-control-flow"
64d7a2eea6SDan Gohman 
65d7a2eea6SDan Gohman namespace {
66d7a2eea6SDan Gohman 
67a41250c7SHeejin Ahn using BlockVector = SmallVector<MachineBasicBlock *, 4>;
68a41250c7SHeejin Ahn using BlockSet = SmallPtrSet<MachineBasicBlock *, 4>;
69d7a2eea6SDan Gohman 
getSortedEntries(const BlockSet & Entries)703648370aSHeejin Ahn static BlockVector getSortedEntries(const BlockSet &Entries) {
713648370aSHeejin Ahn   BlockVector SortedEntries(Entries.begin(), Entries.end());
723648370aSHeejin Ahn   llvm::sort(SortedEntries,
733648370aSHeejin Ahn              [](const MachineBasicBlock *A, const MachineBasicBlock *B) {
743648370aSHeejin Ahn                auto ANum = A->getNumber();
753648370aSHeejin Ahn                auto BNum = B->getNumber();
763648370aSHeejin Ahn                return ANum < BNum;
773648370aSHeejin Ahn              });
783648370aSHeejin Ahn   return SortedEntries;
793648370aSHeejin Ahn }
803648370aSHeejin Ahn 
81a41250c7SHeejin Ahn // Calculates reachability in a region. Ignores branches to blocks outside of
82a41250c7SHeejin Ahn // the region, and ignores branches to the region entry (for the case where
83a41250c7SHeejin Ahn // the region is the inner part of a loop).
84a41250c7SHeejin Ahn class ReachabilityGraph {
85a41250c7SHeejin Ahn public:
ReachabilityGraph(MachineBasicBlock * Entry,const BlockSet & Blocks)86a41250c7SHeejin Ahn   ReachabilityGraph(MachineBasicBlock *Entry, const BlockSet &Blocks)
87a41250c7SHeejin Ahn       : Entry(Entry), Blocks(Blocks) {
88a41250c7SHeejin Ahn #ifndef NDEBUG
89a41250c7SHeejin Ahn     // The region must have a single entry.
90a41250c7SHeejin Ahn     for (auto *MBB : Blocks) {
91a41250c7SHeejin Ahn       if (MBB != Entry) {
92a41250c7SHeejin Ahn         for (auto *Pred : MBB->predecessors()) {
93a41250c7SHeejin Ahn           assert(inRegion(Pred));
94a41250c7SHeejin Ahn         }
95a41250c7SHeejin Ahn       }
96a41250c7SHeejin Ahn     }
97a41250c7SHeejin Ahn #endif
98a41250c7SHeejin Ahn     calculate();
99a41250c7SHeejin Ahn   }
100a41250c7SHeejin Ahn 
canReach(MachineBasicBlock * From,MachineBasicBlock * To) const101c60bc94aSHeejin Ahn   bool canReach(MachineBasicBlock *From, MachineBasicBlock *To) const {
102a41250c7SHeejin Ahn     assert(inRegion(From) && inRegion(To));
103c60bc94aSHeejin Ahn     auto I = Reachable.find(From);
104c60bc94aSHeejin Ahn     if (I == Reachable.end())
105c60bc94aSHeejin Ahn       return false;
106c60bc94aSHeejin Ahn     return I->second.count(To);
107a41250c7SHeejin Ahn   }
108a41250c7SHeejin Ahn 
109a41250c7SHeejin Ahn   // "Loopers" are blocks that are in a loop. We detect these by finding blocks
110a41250c7SHeejin Ahn   // that can reach themselves.
getLoopers() const111c60bc94aSHeejin Ahn   const BlockSet &getLoopers() const { return Loopers; }
112a41250c7SHeejin Ahn 
113a41250c7SHeejin Ahn   // Get all blocks that are loop entries.
getLoopEntries() const114c60bc94aSHeejin Ahn   const BlockSet &getLoopEntries() const { return LoopEntries; }
115a41250c7SHeejin Ahn 
116a41250c7SHeejin Ahn   // Get all blocks that enter a particular loop from outside.
getLoopEnterers(MachineBasicBlock * LoopEntry) const117c60bc94aSHeejin Ahn   const BlockSet &getLoopEnterers(MachineBasicBlock *LoopEntry) const {
118a41250c7SHeejin Ahn     assert(inRegion(LoopEntry));
119c60bc94aSHeejin Ahn     auto I = LoopEnterers.find(LoopEntry);
120c60bc94aSHeejin Ahn     assert(I != LoopEnterers.end());
121c60bc94aSHeejin Ahn     return I->second;
122a41250c7SHeejin Ahn   }
12340926451SJacob Gravelle 
124777d01c7SHeejin Ahn private:
125a41250c7SHeejin Ahn   MachineBasicBlock *Entry;
126a41250c7SHeejin Ahn   const BlockSet &Blocks;
127d7a2eea6SDan Gohman 
128a41250c7SHeejin Ahn   BlockSet Loopers, LoopEntries;
129a41250c7SHeejin Ahn   DenseMap<MachineBasicBlock *, BlockSet> LoopEnterers;
130d7a2eea6SDan Gohman 
inRegion(MachineBasicBlock * MBB) const131c60bc94aSHeejin Ahn   bool inRegion(MachineBasicBlock *MBB) const { return Blocks.count(MBB); }
132a41250c7SHeejin Ahn 
133a41250c7SHeejin Ahn   // Maps a block to all the other blocks it can reach.
134777d01c7SHeejin Ahn   DenseMap<MachineBasicBlock *, BlockSet> Reachable;
135d7a2eea6SDan Gohman 
calculate()136a41250c7SHeejin Ahn   void calculate() {
137a41250c7SHeejin Ahn     // Reachability computation work list. Contains pairs of recent additions
138a41250c7SHeejin Ahn     // (A, B) where we just added a link A => B.
139777d01c7SHeejin Ahn     using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
140777d01c7SHeejin Ahn     SmallVector<BlockPair, 4> WorkList;
141d7a2eea6SDan Gohman 
142a41250c7SHeejin Ahn     // Add all relevant direct branches.
143a41250c7SHeejin Ahn     for (auto *MBB : Blocks) {
144a41250c7SHeejin Ahn       for (auto *Succ : MBB->successors()) {
145a41250c7SHeejin Ahn         if (Succ != Entry && inRegion(Succ)) {
146a41250c7SHeejin Ahn           Reachable[MBB].insert(Succ);
147777d01c7SHeejin Ahn           WorkList.emplace_back(MBB, Succ);
148777d01c7SHeejin Ahn         }
149777d01c7SHeejin Ahn       }
150777d01c7SHeejin Ahn     }
151a41250c7SHeejin Ahn 
152a41250c7SHeejin Ahn     while (!WorkList.empty()) {
153a41250c7SHeejin Ahn       MachineBasicBlock *MBB, *Succ;
154a41250c7SHeejin Ahn       std::tie(MBB, Succ) = WorkList.pop_back_val();
155a41250c7SHeejin Ahn       assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
156a41250c7SHeejin Ahn       if (MBB != Entry) {
157a41250c7SHeejin Ahn         // We recently added MBB => Succ, and that means we may have enabled
158a41250c7SHeejin Ahn         // Pred => MBB => Succ.
159a41250c7SHeejin Ahn         for (auto *Pred : MBB->predecessors()) {
160a41250c7SHeejin Ahn           if (Reachable[Pred].insert(Succ).second) {
161a41250c7SHeejin Ahn             WorkList.emplace_back(Pred, Succ);
162a41250c7SHeejin Ahn           }
163a41250c7SHeejin Ahn         }
164a41250c7SHeejin Ahn       }
165a41250c7SHeejin Ahn     }
166a41250c7SHeejin Ahn 
167a41250c7SHeejin Ahn     // Blocks that can return to themselves are in a loop.
168a41250c7SHeejin Ahn     for (auto *MBB : Blocks) {
169a41250c7SHeejin Ahn       if (canReach(MBB, MBB)) {
170a41250c7SHeejin Ahn         Loopers.insert(MBB);
171a41250c7SHeejin Ahn       }
172a41250c7SHeejin Ahn     }
173a41250c7SHeejin Ahn     assert(!Loopers.count(Entry));
174a41250c7SHeejin Ahn 
175a41250c7SHeejin Ahn     // Find the loop entries - loopers reachable from blocks not in that loop -
176a41250c7SHeejin Ahn     // and those outside blocks that reach them, the "loop enterers".
177a41250c7SHeejin Ahn     for (auto *Looper : Loopers) {
178a41250c7SHeejin Ahn       for (auto *Pred : Looper->predecessors()) {
179a41250c7SHeejin Ahn         // Pred can reach Looper. If Looper can reach Pred, it is in the loop;
180a41250c7SHeejin Ahn         // otherwise, it is a block that enters into the loop.
181a41250c7SHeejin Ahn         if (!canReach(Looper, Pred)) {
182a41250c7SHeejin Ahn           LoopEntries.insert(Looper);
183a41250c7SHeejin Ahn           LoopEnterers[Looper].insert(Pred);
184a41250c7SHeejin Ahn         }
185a41250c7SHeejin Ahn       }
186a41250c7SHeejin Ahn     }
187a41250c7SHeejin Ahn   }
188777d01c7SHeejin Ahn };
189777d01c7SHeejin Ahn 
190a41250c7SHeejin Ahn // Finds the blocks in a single-entry loop, given the loop entry and the
191a41250c7SHeejin Ahn // list of blocks that enter the loop.
192a41250c7SHeejin Ahn class LoopBlocks {
193a41250c7SHeejin Ahn public:
LoopBlocks(MachineBasicBlock * Entry,const BlockSet & Enterers)194a41250c7SHeejin Ahn   LoopBlocks(MachineBasicBlock *Entry, const BlockSet &Enterers)
195a41250c7SHeejin Ahn       : Entry(Entry), Enterers(Enterers) {
196a41250c7SHeejin Ahn     calculate();
197777d01c7SHeejin Ahn   }
198a41250c7SHeejin Ahn 
getBlocks()199a41250c7SHeejin Ahn   BlockSet &getBlocks() { return Blocks; }
200a41250c7SHeejin Ahn 
201a41250c7SHeejin Ahn private:
202a41250c7SHeejin Ahn   MachineBasicBlock *Entry;
203a41250c7SHeejin Ahn   const BlockSet &Enterers;
204a41250c7SHeejin Ahn 
205a41250c7SHeejin Ahn   BlockSet Blocks;
206a41250c7SHeejin Ahn 
calculate()207a41250c7SHeejin Ahn   void calculate() {
208a41250c7SHeejin Ahn     // Going backwards from the loop entry, if we ignore the blocks entering
209a41250c7SHeejin Ahn     // from outside, we will traverse all the blocks in the loop.
210a41250c7SHeejin Ahn     BlockVector WorkList;
211a41250c7SHeejin Ahn     BlockSet AddedToWorkList;
212a41250c7SHeejin Ahn     Blocks.insert(Entry);
213a41250c7SHeejin Ahn     for (auto *Pred : Entry->predecessors()) {
214a41250c7SHeejin Ahn       if (!Enterers.count(Pred)) {
215a41250c7SHeejin Ahn         WorkList.push_back(Pred);
216a41250c7SHeejin Ahn         AddedToWorkList.insert(Pred);
217777d01c7SHeejin Ahn       }
218777d01c7SHeejin Ahn     }
219777d01c7SHeejin Ahn 
220777d01c7SHeejin Ahn     while (!WorkList.empty()) {
221a41250c7SHeejin Ahn       auto *MBB = WorkList.pop_back_val();
222a41250c7SHeejin Ahn       assert(!Enterers.count(MBB));
223a41250c7SHeejin Ahn       if (Blocks.insert(MBB).second) {
224777d01c7SHeejin Ahn         for (auto *Pred : MBB->predecessors()) {
225*4271a1ffSKazu Hirata           if (AddedToWorkList.insert(Pred).second)
226a41250c7SHeejin Ahn             WorkList.push_back(Pred);
227777d01c7SHeejin Ahn         }
228777d01c7SHeejin Ahn       }
229a41250c7SHeejin Ahn     }
230a41250c7SHeejin Ahn   }
231a41250c7SHeejin Ahn };
232a41250c7SHeejin Ahn 
233a41250c7SHeejin Ahn class WebAssemblyFixIrreducibleControlFlow final : public MachineFunctionPass {
getPassName() const234a41250c7SHeejin Ahn   StringRef getPassName() const override {
235a41250c7SHeejin Ahn     return "WebAssembly Fix Irreducible Control Flow";
236a41250c7SHeejin Ahn   }
237a41250c7SHeejin Ahn 
238a41250c7SHeejin Ahn   bool runOnMachineFunction(MachineFunction &MF) override;
239a41250c7SHeejin Ahn 
240a41250c7SHeejin Ahn   bool processRegion(MachineBasicBlock *Entry, BlockSet &Blocks,
241a41250c7SHeejin Ahn                      MachineFunction &MF);
242a41250c7SHeejin Ahn 
243a41250c7SHeejin Ahn   void makeSingleEntryLoop(BlockSet &Entries, BlockSet &Blocks,
2447e7aad15SHeejin Ahn                            MachineFunction &MF, const ReachabilityGraph &Graph);
245a41250c7SHeejin Ahn 
246a41250c7SHeejin Ahn public:
247a41250c7SHeejin Ahn   static char ID; // Pass identification, replacement for typeid
WebAssemblyFixIrreducibleControlFlow()248a41250c7SHeejin Ahn   WebAssemblyFixIrreducibleControlFlow() : MachineFunctionPass(ID) {}
249a41250c7SHeejin Ahn };
250a41250c7SHeejin Ahn 
processRegion(MachineBasicBlock * Entry,BlockSet & Blocks,MachineFunction & MF)251a41250c7SHeejin Ahn bool WebAssemblyFixIrreducibleControlFlow::processRegion(
252a41250c7SHeejin Ahn     MachineBasicBlock *Entry, BlockSet &Blocks, MachineFunction &MF) {
253a41250c7SHeejin Ahn   bool Changed = false;
254a41250c7SHeejin Ahn   // Remove irreducibility before processing child loops, which may take
255a41250c7SHeejin Ahn   // multiple iterations.
256a41250c7SHeejin Ahn   while (true) {
257a41250c7SHeejin Ahn     ReachabilityGraph Graph(Entry, Blocks);
258a41250c7SHeejin Ahn 
259a41250c7SHeejin Ahn     bool FoundIrreducibility = false;
260a41250c7SHeejin Ahn 
2613648370aSHeejin Ahn     for (auto *LoopEntry : getSortedEntries(Graph.getLoopEntries())) {
262a41250c7SHeejin Ahn       // Find mutual entries - all entries which can reach this one, and
263a41250c7SHeejin Ahn       // are reached by it (that always includes LoopEntry itself). All mutual
264a41250c7SHeejin Ahn       // entries must be in the same loop, so if we have more than one, then we
265a41250c7SHeejin Ahn       // have irreducible control flow.
266a41250c7SHeejin Ahn       //
2673648370aSHeejin Ahn       // (Note that we need to sort the entries here, as otherwise the order can
2683648370aSHeejin Ahn       // matter: being mutual is a symmetric relationship, and each set of
2693648370aSHeejin Ahn       // mutuals will be handled properly no matter which we see first. However,
2703648370aSHeejin Ahn       // there can be multiple disjoint sets of mutuals, and which we process
2713648370aSHeejin Ahn       // first changes the output.)
2723648370aSHeejin Ahn       //
273a41250c7SHeejin Ahn       // Note that irreducibility may involve inner loops, e.g. imagine A
274a41250c7SHeejin Ahn       // starts one loop, and it has B inside it which starts an inner loop.
275a41250c7SHeejin Ahn       // If we add a branch from all the way on the outside to B, then in a
276a41250c7SHeejin Ahn       // sense B is no longer an "inner" loop, semantically speaking. We will
277a41250c7SHeejin Ahn       // fix that irreducibility by adding a block that dispatches to either
278a41250c7SHeejin Ahn       // either A or B, so B will no longer be an inner loop in our output.
279a41250c7SHeejin Ahn       // (A fancier approach might try to keep it as such.)
280a41250c7SHeejin Ahn       //
281a41250c7SHeejin Ahn       // Note that we still need to recurse into inner loops later, to handle
282a41250c7SHeejin Ahn       // the case where the irreducibility is entirely nested - we would not
283a41250c7SHeejin Ahn       // be able to identify that at this point, since the enclosing loop is
284a41250c7SHeejin Ahn       // a group of blocks all of whom can reach each other. (We'll see the
285a41250c7SHeejin Ahn       // irreducibility after removing branches to the top of that enclosing
286a41250c7SHeejin Ahn       // loop.)
287a41250c7SHeejin Ahn       BlockSet MutualLoopEntries;
288a41250c7SHeejin Ahn       MutualLoopEntries.insert(LoopEntry);
289a41250c7SHeejin Ahn       for (auto *OtherLoopEntry : Graph.getLoopEntries()) {
290a41250c7SHeejin Ahn         if (OtherLoopEntry != LoopEntry &&
291a41250c7SHeejin Ahn             Graph.canReach(LoopEntry, OtherLoopEntry) &&
292a41250c7SHeejin Ahn             Graph.canReach(OtherLoopEntry, LoopEntry)) {
293a41250c7SHeejin Ahn           MutualLoopEntries.insert(OtherLoopEntry);
294a41250c7SHeejin Ahn         }
295a41250c7SHeejin Ahn       }
296777d01c7SHeejin Ahn 
297a41250c7SHeejin Ahn       if (MutualLoopEntries.size() > 1) {
2987e7aad15SHeejin Ahn         makeSingleEntryLoop(MutualLoopEntries, Blocks, MF, Graph);
299a41250c7SHeejin Ahn         FoundIrreducibility = true;
300a41250c7SHeejin Ahn         Changed = true;
301777d01c7SHeejin Ahn         break;
302777d01c7SHeejin Ahn       }
303777d01c7SHeejin Ahn     }
304a41250c7SHeejin Ahn     // Only go on to actually process the inner loops when we are done
305a41250c7SHeejin Ahn     // removing irreducible control flow and changing the graph. Modifying
306a41250c7SHeejin Ahn     // the graph as we go is possible, and that might let us avoid looking at
307a41250c7SHeejin Ahn     // the already-fixed loops again if we are careful, but all that is
308a41250c7SHeejin Ahn     // complex and bug-prone. Since irreducible loops are rare, just starting
309a41250c7SHeejin Ahn     // another iteration is best.
310a41250c7SHeejin Ahn     if (FoundIrreducibility) {
311a41250c7SHeejin Ahn       continue;
312777d01c7SHeejin Ahn     }
313777d01c7SHeejin Ahn 
314a41250c7SHeejin Ahn     for (auto *LoopEntry : Graph.getLoopEntries()) {
315a41250c7SHeejin Ahn       LoopBlocks InnerBlocks(LoopEntry, Graph.getLoopEnterers(LoopEntry));
316a41250c7SHeejin Ahn       // Each of these calls to processRegion may change the graph, but are
317a41250c7SHeejin Ahn       // guaranteed not to interfere with each other. The only changes we make
318a41250c7SHeejin Ahn       // to the graph are to add blocks on the way to a loop entry. As the
319a41250c7SHeejin Ahn       // loops are disjoint, that means we may only alter branches that exit
320a41250c7SHeejin Ahn       // another loop, which are ignored when recursing into that other loop
321a41250c7SHeejin Ahn       // anyhow.
322a41250c7SHeejin Ahn       if (processRegion(LoopEntry, InnerBlocks.getBlocks(), MF)) {
323a41250c7SHeejin Ahn         Changed = true;
324a41250c7SHeejin Ahn       }
325a41250c7SHeejin Ahn     }
326a41250c7SHeejin Ahn 
327a41250c7SHeejin Ahn     return Changed;
328a41250c7SHeejin Ahn   }
329a41250c7SHeejin Ahn }
330a41250c7SHeejin Ahn 
331a41250c7SHeejin Ahn // Given a set of entries to a single loop, create a single entry for that
332a41250c7SHeejin Ahn // loop by creating a dispatch block for them, routing control flow using
333a41250c7SHeejin Ahn // a helper variable. Also updates Blocks with any new blocks created, so
3347e7aad15SHeejin Ahn // that we properly track all the blocks in the region. But this does not update
3357e7aad15SHeejin Ahn // ReachabilityGraph; this will be updated in the caller of this function as
3367e7aad15SHeejin Ahn // needed.
makeSingleEntryLoop(BlockSet & Entries,BlockSet & Blocks,MachineFunction & MF,const ReachabilityGraph & Graph)337a41250c7SHeejin Ahn void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
3387e7aad15SHeejin Ahn     BlockSet &Entries, BlockSet &Blocks, MachineFunction &MF,
3397e7aad15SHeejin Ahn     const ReachabilityGraph &Graph) {
340a41250c7SHeejin Ahn   assert(Entries.size() >= 2);
341d7a2eea6SDan Gohman 
342777d01c7SHeejin Ahn   // Sort the entries to ensure a deterministic build.
3433648370aSHeejin Ahn   BlockVector SortedEntries = getSortedEntries(Entries);
344d7a2eea6SDan Gohman 
345e1fef949SRichard Trieu #ifndef NDEBUG
346e1fef949SRichard Trieu   for (auto Block : SortedEntries)
347e1fef949SRichard Trieu     assert(Block->getNumber() != -1);
348e1fef949SRichard Trieu   if (SortedEntries.size() > 1) {
349a41250c7SHeejin Ahn     for (auto I = SortedEntries.begin(), E = SortedEntries.end() - 1; I != E;
350a41250c7SHeejin Ahn          ++I) {
351e1fef949SRichard Trieu       auto ANum = (*I)->getNumber();
352e1fef949SRichard Trieu       auto BNum = (*(std::next(I)))->getNumber();
353e1fef949SRichard Trieu       assert(ANum != BNum);
354e1fef949SRichard Trieu     }
355e1fef949SRichard Trieu   }
356e1fef949SRichard Trieu #endif
357e1fef949SRichard Trieu 
358777d01c7SHeejin Ahn   // Create a dispatch block which will contain a jump table to the entries.
359d7a2eea6SDan Gohman   MachineBasicBlock *Dispatch = MF.CreateMachineBasicBlock();
360d7a2eea6SDan Gohman   MF.insert(MF.end(), Dispatch);
361a41250c7SHeejin Ahn   Blocks.insert(Dispatch);
362d7a2eea6SDan Gohman 
363d7a2eea6SDan Gohman   // Add the jump table.
364d7a2eea6SDan Gohman   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
365c60bc94aSHeejin Ahn   MachineInstrBuilder MIB =
366c60bc94aSHeejin Ahn       BuildMI(Dispatch, DebugLoc(), TII.get(WebAssembly::BR_TABLE_I32));
367d7a2eea6SDan Gohman 
368d7a2eea6SDan Gohman   // Add the register which will be used to tell the jump table which block to
369d7a2eea6SDan Gohman   // jump to.
370d7a2eea6SDan Gohman   MachineRegisterInfo &MRI = MF.getRegInfo();
37105c145d6SDaniel Sanders   Register Reg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
372d7a2eea6SDan Gohman   MIB.addReg(Reg);
373d7a2eea6SDan Gohman 
374777d01c7SHeejin Ahn   // Compute the indices in the superheader, one for each bad block, and
375777d01c7SHeejin Ahn   // add them as successors.
376d7a2eea6SDan Gohman   DenseMap<MachineBasicBlock *, unsigned> Indices;
377a41250c7SHeejin Ahn   for (auto *Entry : SortedEntries) {
378a41250c7SHeejin Ahn     auto Pair = Indices.insert(std::make_pair(Entry, 0));
379a41250c7SHeejin Ahn     assert(Pair.second);
380d7a2eea6SDan Gohman 
381f456290fSDan Gohman     unsigned Index = MIB.getInstr()->getNumExplicitOperands() - 1;
382d7a2eea6SDan Gohman     Pair.first->second = Index;
383d7a2eea6SDan Gohman 
384a41250c7SHeejin Ahn     MIB.addMBB(Entry);
385a41250c7SHeejin Ahn     Dispatch->addSuccessor(Entry);
386d7a2eea6SDan Gohman   }
387d7a2eea6SDan Gohman 
388a41250c7SHeejin Ahn   // Rewrite the problematic successors for every block that wants to reach
389a41250c7SHeejin Ahn   // the bad blocks. For simplicity, we just introduce a new block for every
390a41250c7SHeejin Ahn   // edge we need to rewrite. (Fancier things are possible.)
391777d01c7SHeejin Ahn 
392a41250c7SHeejin Ahn   BlockVector AllPreds;
393a41250c7SHeejin Ahn   for (auto *Entry : SortedEntries) {
394a41250c7SHeejin Ahn     for (auto *Pred : Entry->predecessors()) {
395777d01c7SHeejin Ahn       if (Pred != Dispatch) {
396777d01c7SHeejin Ahn         AllPreds.push_back(Pred);
397777d01c7SHeejin Ahn       }
398777d01c7SHeejin Ahn     }
399777d01c7SHeejin Ahn   }
400777d01c7SHeejin Ahn 
4017e7aad15SHeejin Ahn   // This set stores predecessors within this loop.
4027e7aad15SHeejin Ahn   DenseSet<MachineBasicBlock *> InLoop;
4037e7aad15SHeejin Ahn   for (auto *Pred : AllPreds) {
404a41250c7SHeejin Ahn     for (auto *Entry : Pred->successors()) {
4057e7aad15SHeejin Ahn       if (!Entries.count(Entry))
406d7a2eea6SDan Gohman         continue;
4077e7aad15SHeejin Ahn       if (Graph.canReach(Entry, Pred)) {
4087e7aad15SHeejin Ahn         InLoop.insert(Pred);
4097e7aad15SHeejin Ahn         break;
4107e7aad15SHeejin Ahn       }
4117e7aad15SHeejin Ahn     }
412777d01c7SHeejin Ahn   }
413d7a2eea6SDan Gohman 
4147e7aad15SHeejin Ahn   // Record if each entry has a layout predecessor. This map stores
4159a08c307SBenjamin Kramer   // <<loop entry, Predecessor is within the loop?>, layout predecessor>
4169a08c307SBenjamin Kramer   DenseMap<PointerIntPair<MachineBasicBlock *, 1, bool>, MachineBasicBlock *>
4177e7aad15SHeejin Ahn       EntryToLayoutPred;
4189a08c307SBenjamin Kramer   for (auto *Pred : AllPreds) {
4199a08c307SBenjamin Kramer     bool PredInLoop = InLoop.count(Pred);
4207e7aad15SHeejin Ahn     for (auto *Entry : Pred->successors())
4217e7aad15SHeejin Ahn       if (Entries.count(Entry) && Pred->isLayoutSuccessor(Entry))
4229a08c307SBenjamin Kramer         EntryToLayoutPred[{Entry, PredInLoop}] = Pred;
4239a08c307SBenjamin Kramer   }
4247e7aad15SHeejin Ahn 
4257e7aad15SHeejin Ahn   // We need to create at most two routing blocks per entry: one for
4267e7aad15SHeejin Ahn   // predecessors outside the loop and one for predecessors inside the loop.
4277e7aad15SHeejin Ahn   // This map stores
4289a08c307SBenjamin Kramer   // <<loop entry, Predecessor is within the loop?>, routing block>
4299a08c307SBenjamin Kramer   DenseMap<PointerIntPair<MachineBasicBlock *, 1, bool>, MachineBasicBlock *>
4309a08c307SBenjamin Kramer       Map;
4317e7aad15SHeejin Ahn   for (auto *Pred : AllPreds) {
4327e7aad15SHeejin Ahn     bool PredInLoop = InLoop.count(Pred);
4337e7aad15SHeejin Ahn     for (auto *Entry : Pred->successors()) {
4349a08c307SBenjamin Kramer       if (!Entries.count(Entry) || Map.count({Entry, PredInLoop}))
4357e7aad15SHeejin Ahn         continue;
4367e7aad15SHeejin Ahn       // If there exists a layout predecessor of this entry and this predecessor
4377e7aad15SHeejin Ahn       // is not that, we rather create a routing block after that layout
4387e7aad15SHeejin Ahn       // predecessor to save a branch.
4399a08c307SBenjamin Kramer       if (auto *OtherPred = EntryToLayoutPred.lookup({Entry, PredInLoop}))
4409a08c307SBenjamin Kramer         if (OtherPred != Pred)
4417e7aad15SHeejin Ahn           continue;
4427e7aad15SHeejin Ahn 
443777d01c7SHeejin Ahn       // This is a successor we need to rewrite.
4447e7aad15SHeejin Ahn       MachineBasicBlock *Routing = MF.CreateMachineBasicBlock();
445a41250c7SHeejin Ahn       MF.insert(Pred->isLayoutSuccessor(Entry)
446a41250c7SHeejin Ahn                     ? MachineFunction::iterator(Entry)
447a41250c7SHeejin Ahn                     : MF.end(),
4487e7aad15SHeejin Ahn                 Routing);
4497e7aad15SHeejin Ahn       Blocks.insert(Routing);
450d7a2eea6SDan Gohman 
451d7a2eea6SDan Gohman       // Set the jump table's register of the index of the block we wish to
452d7a2eea6SDan Gohman       // jump to, and jump to the jump table.
4537e7aad15SHeejin Ahn       BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::CONST_I32), Reg)
454a41250c7SHeejin Ahn           .addImm(Indices[Entry]);
4557e7aad15SHeejin Ahn       BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::BR)).addMBB(Dispatch);
4567e7aad15SHeejin Ahn       Routing->addSuccessor(Dispatch);
4579a08c307SBenjamin Kramer       Map[{Entry, PredInLoop}] = Routing;
458d7a2eea6SDan Gohman     }
4597e7aad15SHeejin Ahn   }
4607e7aad15SHeejin Ahn 
4617e7aad15SHeejin Ahn   for (auto *Pred : AllPreds) {
4627e7aad15SHeejin Ahn     bool PredInLoop = InLoop.count(Pred);
463d7a2eea6SDan Gohman     // Remap the terminator operands and the successor list.
464a41250c7SHeejin Ahn     for (MachineInstr &Term : Pred->terminators())
465d7a2eea6SDan Gohman       for (auto &Op : Term.explicit_uses())
466d7a2eea6SDan Gohman         if (Op.isMBB() && Indices.count(Op.getMBB()))
4679a08c307SBenjamin Kramer           Op.setMBB(Map[{Op.getMBB(), PredInLoop}]);
4687e7aad15SHeejin Ahn 
4697e7aad15SHeejin Ahn     for (auto *Succ : Pred->successors()) {
4707e7aad15SHeejin Ahn       if (!Entries.count(Succ))
4717e7aad15SHeejin Ahn         continue;
4729a08c307SBenjamin Kramer       auto *Routing = Map[{Succ, PredInLoop}];
4737e7aad15SHeejin Ahn       Pred->replaceSuccessor(Succ, Routing);
4747e7aad15SHeejin Ahn     }
475d7a2eea6SDan Gohman   }
476d7a2eea6SDan Gohman 
477d7a2eea6SDan Gohman   // Create a fake default label, because br_table requires one.
478d7a2eea6SDan Gohman   MIB.addMBB(MIB.getInstr()
479d7a2eea6SDan Gohman                  ->getOperand(MIB.getInstr()->getNumExplicitOperands() - 1)
480d7a2eea6SDan Gohman                  .getMBB());
481d7a2eea6SDan Gohman }
482d7a2eea6SDan Gohman 
483777d01c7SHeejin Ahn } // end anonymous namespace
484777d01c7SHeejin Ahn 
485777d01c7SHeejin Ahn char WebAssemblyFixIrreducibleControlFlow::ID = 0;
486777d01c7SHeejin Ahn INITIALIZE_PASS(WebAssemblyFixIrreducibleControlFlow, DEBUG_TYPE,
487777d01c7SHeejin Ahn                 "Removes irreducible control flow", false, false)
488777d01c7SHeejin Ahn 
createWebAssemblyFixIrreducibleControlFlow()489777d01c7SHeejin Ahn FunctionPass *llvm::createWebAssemblyFixIrreducibleControlFlow() {
490777d01c7SHeejin Ahn   return new WebAssemblyFixIrreducibleControlFlow();
491777d01c7SHeejin Ahn }
492777d01c7SHeejin Ahn 
493cde083e0SHeejin Ahn // Test whether the given register has an ARGUMENT def.
hasArgumentDef(unsigned Reg,const MachineRegisterInfo & MRI)494cde083e0SHeejin Ahn static bool hasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
495cde083e0SHeejin Ahn   for (const auto &Def : MRI.def_instructions(Reg))
496cde083e0SHeejin Ahn     if (WebAssembly::isArgument(Def.getOpcode()))
497cde083e0SHeejin Ahn       return true;
498cde083e0SHeejin Ahn   return false;
499cde083e0SHeejin Ahn }
500cde083e0SHeejin Ahn 
501cde083e0SHeejin Ahn // Add a register definition with IMPLICIT_DEFs for every register to cover for
502cde083e0SHeejin Ahn // register uses that don't have defs in every possible path.
503cde083e0SHeejin Ahn // TODO: This is fairly heavy-handed; find a better approach.
addImplicitDefs(MachineFunction & MF)504cde083e0SHeejin Ahn static void addImplicitDefs(MachineFunction &MF) {
505cde083e0SHeejin Ahn   const MachineRegisterInfo &MRI = MF.getRegInfo();
506cde083e0SHeejin Ahn   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
507cde083e0SHeejin Ahn   MachineBasicBlock &Entry = *MF.begin();
508cde083e0SHeejin Ahn   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
509cde083e0SHeejin Ahn     Register Reg = Register::index2VirtReg(I);
510cde083e0SHeejin Ahn 
511cde083e0SHeejin Ahn     // Skip unused registers.
512cde083e0SHeejin Ahn     if (MRI.use_nodbg_empty(Reg))
513cde083e0SHeejin Ahn       continue;
514cde083e0SHeejin Ahn 
515cde083e0SHeejin Ahn     // Skip registers that have an ARGUMENT definition.
516cde083e0SHeejin Ahn     if (hasArgumentDef(Reg, MRI))
517cde083e0SHeejin Ahn       continue;
518cde083e0SHeejin Ahn 
519cde083e0SHeejin Ahn     BuildMI(Entry, Entry.begin(), DebugLoc(),
520cde083e0SHeejin Ahn             TII.get(WebAssembly::IMPLICIT_DEF), Reg);
521cde083e0SHeejin Ahn   }
522cde083e0SHeejin Ahn 
523cde083e0SHeejin Ahn   // Move ARGUMENT_* instructions to the top of the entry block, so that their
524cde083e0SHeejin Ahn   // liveness reflects the fact that these really are live-in values.
525cde083e0SHeejin Ahn   for (MachineInstr &MI : llvm::make_early_inc_range(Entry)) {
526cde083e0SHeejin Ahn     if (WebAssembly::isArgument(MI.getOpcode())) {
527cde083e0SHeejin Ahn       MI.removeFromParent();
528cde083e0SHeejin Ahn       Entry.insert(Entry.begin(), &MI);
529cde083e0SHeejin Ahn     }
530cde083e0SHeejin Ahn   }
531cde083e0SHeejin Ahn }
532cde083e0SHeejin Ahn 
runOnMachineFunction(MachineFunction & MF)533d7a2eea6SDan Gohman bool WebAssemblyFixIrreducibleControlFlow::runOnMachineFunction(
534d7a2eea6SDan Gohman     MachineFunction &MF) {
535d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "********** Fixing Irreducible Control Flow **********\n"
536d7a2eea6SDan Gohman                        "********** Function: "
537d7a2eea6SDan Gohman                     << MF.getName() << '\n');
538d7a2eea6SDan Gohman 
539a41250c7SHeejin Ahn   // Start the recursive process on the entire function body.
540a41250c7SHeejin Ahn   BlockSet AllBlocks;
541a41250c7SHeejin Ahn   for (auto &MBB : MF) {
542a41250c7SHeejin Ahn     AllBlocks.insert(&MBB);
543d7a2eea6SDan Gohman   }
544d7a2eea6SDan Gohman 
545a41250c7SHeejin Ahn   if (LLVM_UNLIKELY(processRegion(&*MF.begin(), AllBlocks, MF))) {
546a41250c7SHeejin Ahn     // We rewrote part of the function; recompute relevant things.
547a41250c7SHeejin Ahn     MF.RenumberBlocks();
548cde083e0SHeejin Ahn     // Now we've inserted dispatch blocks, some register uses can have incoming
549cde083e0SHeejin Ahn     // paths without a def. For example, before this pass register %a was
550cde083e0SHeejin Ahn     // defined in BB1 and used in BB2, and there was only one path from BB1 and
551cde083e0SHeejin Ahn     // BB2. But if this pass inserts a dispatch block having multiple
552cde083e0SHeejin Ahn     // predecessors between the two BBs, now there are paths to BB2 without
553cde083e0SHeejin Ahn     // visiting BB1, and %a's use in BB2 is not dominated by its def. Adding
554cde083e0SHeejin Ahn     // IMPLICIT_DEFs to all regs is one simple way to fix it.
555cde083e0SHeejin Ahn     addImplicitDefs(MF);
556a41250c7SHeejin Ahn     return true;
557a41250c7SHeejin Ahn   }
558a41250c7SHeejin Ahn 
559a41250c7SHeejin Ahn   return false;
560d7a2eea6SDan Gohman }
561