101095a5dSDimitry Andric //=- WebAssemblyFixIrreducibleControlFlow.cpp - Fix irreducible control flow -//
201095a5dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601095a5dSDimitry Andric //
701095a5dSDimitry Andric //===----------------------------------------------------------------------===//
801095a5dSDimitry Andric ///
901095a5dSDimitry Andric /// \file
10e6d15924SDimitry Andric /// This file implements a pass that removes irreducible control flow.
11e6d15924SDimitry Andric /// Irreducible control flow means multiple-entry loops, which this pass
12e6d15924SDimitry Andric /// transforms to have a single entry.
1301095a5dSDimitry Andric ///
1401095a5dSDimitry Andric /// Note that LLVM has a generic pass that lowers irreducible control flow, but
1501095a5dSDimitry Andric /// it linearizes control flow, turning diamonds into two triangles, which is
1601095a5dSDimitry Andric /// both unnecessary and undesirable for WebAssembly.
1701095a5dSDimitry Andric ///
18e6d15924SDimitry Andric /// The big picture: We recursively process each "region", defined as a group
19e6d15924SDimitry Andric /// of blocks with a single entry and no branches back to that entry. A region
20e6d15924SDimitry Andric /// may be the entire function body, or the inner part of a loop, i.e., the
21e6d15924SDimitry Andric /// loop's body without branches back to the loop entry. In each region we fix
22e6d15924SDimitry Andric /// up multi-entry loops by adding a new block that can dispatch to each of the
23e6d15924SDimitry Andric /// loop entries, based on the value of a label "helper" variable, and we
24e6d15924SDimitry Andric /// replace direct branches to the entries with assignments to the label
25e6d15924SDimitry Andric /// variable and a branch to the dispatch block. Then the dispatch block is the
26e6d15924SDimitry Andric /// single entry in the loop containing the previous multiple entries. After
27e6d15924SDimitry Andric /// ensuring all the loops in a region are reducible, we recurse into them. The
28e6d15924SDimitry Andric /// total time complexity of this pass is:
29d8e91e46SDimitry Andric ///
30e6d15924SDimitry Andric /// O(NumBlocks * NumNestedLoops * NumIrreducibleLoops +
31e6d15924SDimitry Andric /// NumLoops * NumLoops)
32e6d15924SDimitry Andric ///
33e6d15924SDimitry Andric /// This pass is similar to what the Relooper [1] does. Both identify looping
34e6d15924SDimitry Andric /// code that requires multiple entries, and resolve it in a similar way (in
35e6d15924SDimitry Andric /// Relooper terminology, we implement a Multiple shape in a Loop shape). Note
36d8e91e46SDimitry Andric /// also that like the Relooper, we implement a "minimal" intervention: we only
37d8e91e46SDimitry Andric /// use the "label" helper for the blocks we absolutely must and no others. We
38e6d15924SDimitry Andric /// also prioritize code size and do not duplicate code in order to resolve
39e6d15924SDimitry Andric /// irreducibility. The graph algorithms for finding loops and entries and so
40e6d15924SDimitry Andric /// forth are also similar to the Relooper. The main differences between this
41e6d15924SDimitry Andric /// pass and the Relooper are:
42d8e91e46SDimitry Andric ///
43e6d15924SDimitry Andric /// * We just care about irreducibility, so we just look at loops.
44e6d15924SDimitry Andric /// * The Relooper emits structured control flow (with ifs etc.), while we
45e6d15924SDimitry Andric /// emit a CFG.
46d8e91e46SDimitry Andric ///
47d8e91e46SDimitry Andric /// [1] Alon Zakai. 2011. Emscripten: an LLVM-to-JavaScript compiler. In
48d8e91e46SDimitry Andric /// Proceedings of the ACM international conference companion on Object oriented
49d8e91e46SDimitry Andric /// programming systems languages and applications companion (SPLASH '11). ACM,
50d8e91e46SDimitry Andric /// New York, NY, USA, 301-312. DOI=10.1145/2048147.2048224
51d8e91e46SDimitry Andric /// http://doi.acm.org/10.1145/2048147.2048224
5201095a5dSDimitry Andric ///
5301095a5dSDimitry Andric //===----------------------------------------------------------------------===//
5401095a5dSDimitry Andric
5501095a5dSDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
567ab83427SDimitry Andric #include "WebAssembly.h"
5701095a5dSDimitry Andric #include "WebAssemblySubtarget.h"
58145449b1SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
5901095a5dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
601d5ae102SDimitry Andric #include "llvm/Support/Debug.h"
6101095a5dSDimitry Andric using namespace llvm;
6201095a5dSDimitry Andric
6301095a5dSDimitry Andric #define DEBUG_TYPE "wasm-fix-irreducible-control-flow"
6401095a5dSDimitry Andric
6501095a5dSDimitry Andric namespace {
6601095a5dSDimitry Andric
67e6d15924SDimitry Andric using BlockVector = SmallVector<MachineBasicBlock *, 4>;
68e6d15924SDimitry Andric using BlockSet = SmallPtrSet<MachineBasicBlock *, 4>;
6901095a5dSDimitry Andric
getSortedEntries(const BlockSet & Entries)70cfca06d7SDimitry Andric static BlockVector getSortedEntries(const BlockSet &Entries) {
71cfca06d7SDimitry Andric BlockVector SortedEntries(Entries.begin(), Entries.end());
72cfca06d7SDimitry Andric llvm::sort(SortedEntries,
73cfca06d7SDimitry Andric [](const MachineBasicBlock *A, const MachineBasicBlock *B) {
74cfca06d7SDimitry Andric auto ANum = A->getNumber();
75cfca06d7SDimitry Andric auto BNum = B->getNumber();
76cfca06d7SDimitry Andric return ANum < BNum;
77cfca06d7SDimitry Andric });
78cfca06d7SDimitry Andric return SortedEntries;
79cfca06d7SDimitry Andric }
80cfca06d7SDimitry Andric
81e6d15924SDimitry Andric // Calculates reachability in a region. Ignores branches to blocks outside of
82e6d15924SDimitry Andric // the region, and ignores branches to the region entry (for the case where
83e6d15924SDimitry Andric // the region is the inner part of a loop).
84e6d15924SDimitry Andric class ReachabilityGraph {
85e6d15924SDimitry Andric public:
ReachabilityGraph(MachineBasicBlock * Entry,const BlockSet & Blocks)86e6d15924SDimitry Andric ReachabilityGraph(MachineBasicBlock *Entry, const BlockSet &Blocks)
87e6d15924SDimitry Andric : Entry(Entry), Blocks(Blocks) {
88e6d15924SDimitry Andric #ifndef NDEBUG
89e6d15924SDimitry Andric // The region must have a single entry.
90e6d15924SDimitry Andric for (auto *MBB : Blocks) {
91e6d15924SDimitry Andric if (MBB != Entry) {
92e6d15924SDimitry Andric for (auto *Pred : MBB->predecessors()) {
93e6d15924SDimitry Andric assert(inRegion(Pred));
94e6d15924SDimitry Andric }
95e6d15924SDimitry Andric }
96e6d15924SDimitry Andric }
97e6d15924SDimitry Andric #endif
98e6d15924SDimitry Andric calculate();
99e6d15924SDimitry Andric }
100e6d15924SDimitry Andric
canReach(MachineBasicBlock * From,MachineBasicBlock * To) const101e6d15924SDimitry Andric bool canReach(MachineBasicBlock *From, MachineBasicBlock *To) const {
102e6d15924SDimitry Andric assert(inRegion(From) && inRegion(To));
103e6d15924SDimitry Andric auto I = Reachable.find(From);
104e6d15924SDimitry Andric if (I == Reachable.end())
105e6d15924SDimitry Andric return false;
106e6d15924SDimitry Andric return I->second.count(To);
107e6d15924SDimitry Andric }
108e6d15924SDimitry Andric
109e6d15924SDimitry Andric // "Loopers" are blocks that are in a loop. We detect these by finding blocks
110e6d15924SDimitry Andric // that can reach themselves.
getLoopers() const111e6d15924SDimitry Andric const BlockSet &getLoopers() const { return Loopers; }
112e6d15924SDimitry Andric
113e6d15924SDimitry Andric // Get all blocks that are loop entries.
getLoopEntries() const114e6d15924SDimitry Andric const BlockSet &getLoopEntries() const { return LoopEntries; }
115e6d15924SDimitry Andric
116e6d15924SDimitry Andric // Get all blocks that enter a particular loop from outside.
getLoopEnterers(MachineBasicBlock * LoopEntry) const117e6d15924SDimitry Andric const BlockSet &getLoopEnterers(MachineBasicBlock *LoopEntry) const {
118e6d15924SDimitry Andric assert(inRegion(LoopEntry));
119e6d15924SDimitry Andric auto I = LoopEnterers.find(LoopEntry);
120e6d15924SDimitry Andric assert(I != LoopEnterers.end());
121e6d15924SDimitry Andric return I->second;
122e6d15924SDimitry Andric }
123eb11fae6SDimitry Andric
124d8e91e46SDimitry Andric private:
125e6d15924SDimitry Andric MachineBasicBlock *Entry;
126e6d15924SDimitry Andric const BlockSet &Blocks;
12701095a5dSDimitry Andric
128e6d15924SDimitry Andric BlockSet Loopers, LoopEntries;
129e6d15924SDimitry Andric DenseMap<MachineBasicBlock *, BlockSet> LoopEnterers;
13001095a5dSDimitry Andric
inRegion(MachineBasicBlock * MBB) const131e6d15924SDimitry Andric bool inRegion(MachineBasicBlock *MBB) const { return Blocks.count(MBB); }
132e6d15924SDimitry Andric
133e6d15924SDimitry Andric // Maps a block to all the other blocks it can reach.
134d8e91e46SDimitry Andric DenseMap<MachineBasicBlock *, BlockSet> Reachable;
13501095a5dSDimitry Andric
calculate()136e6d15924SDimitry Andric void calculate() {
137e6d15924SDimitry Andric // Reachability computation work list. Contains pairs of recent additions
138e6d15924SDimitry Andric // (A, B) where we just added a link A => B.
139d8e91e46SDimitry Andric using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
140d8e91e46SDimitry Andric SmallVector<BlockPair, 4> WorkList;
14101095a5dSDimitry Andric
142e6d15924SDimitry Andric // Add all relevant direct branches.
143e6d15924SDimitry Andric for (auto *MBB : Blocks) {
144e6d15924SDimitry Andric for (auto *Succ : MBB->successors()) {
145e6d15924SDimitry Andric if (Succ != Entry && inRegion(Succ)) {
146e6d15924SDimitry Andric Reachable[MBB].insert(Succ);
147d8e91e46SDimitry Andric WorkList.emplace_back(MBB, Succ);
148d8e91e46SDimitry Andric }
149d8e91e46SDimitry Andric }
150d8e91e46SDimitry Andric }
151e6d15924SDimitry Andric
152e6d15924SDimitry Andric while (!WorkList.empty()) {
153e6d15924SDimitry Andric MachineBasicBlock *MBB, *Succ;
154e6d15924SDimitry Andric std::tie(MBB, Succ) = WorkList.pop_back_val();
155e6d15924SDimitry Andric assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
156e6d15924SDimitry Andric if (MBB != Entry) {
157e6d15924SDimitry Andric // We recently added MBB => Succ, and that means we may have enabled
158e6d15924SDimitry Andric // Pred => MBB => Succ.
159e6d15924SDimitry Andric for (auto *Pred : MBB->predecessors()) {
160e6d15924SDimitry Andric if (Reachable[Pred].insert(Succ).second) {
161e6d15924SDimitry Andric WorkList.emplace_back(Pred, Succ);
162e6d15924SDimitry Andric }
163e6d15924SDimitry Andric }
164e6d15924SDimitry Andric }
165e6d15924SDimitry Andric }
166e6d15924SDimitry Andric
167e6d15924SDimitry Andric // Blocks that can return to themselves are in a loop.
168e6d15924SDimitry Andric for (auto *MBB : Blocks) {
169e6d15924SDimitry Andric if (canReach(MBB, MBB)) {
170e6d15924SDimitry Andric Loopers.insert(MBB);
171e6d15924SDimitry Andric }
172e6d15924SDimitry Andric }
173e6d15924SDimitry Andric assert(!Loopers.count(Entry));
174e6d15924SDimitry Andric
175e6d15924SDimitry Andric // Find the loop entries - loopers reachable from blocks not in that loop -
176e6d15924SDimitry Andric // and those outside blocks that reach them, the "loop enterers".
177e6d15924SDimitry Andric for (auto *Looper : Loopers) {
178e6d15924SDimitry Andric for (auto *Pred : Looper->predecessors()) {
179e6d15924SDimitry Andric // Pred can reach Looper. If Looper can reach Pred, it is in the loop;
180e6d15924SDimitry Andric // otherwise, it is a block that enters into the loop.
181e6d15924SDimitry Andric if (!canReach(Looper, Pred)) {
182e6d15924SDimitry Andric LoopEntries.insert(Looper);
183e6d15924SDimitry Andric LoopEnterers[Looper].insert(Pred);
184e6d15924SDimitry Andric }
185e6d15924SDimitry Andric }
186e6d15924SDimitry Andric }
187e6d15924SDimitry Andric }
188d8e91e46SDimitry Andric };
189d8e91e46SDimitry Andric
190e6d15924SDimitry Andric // Finds the blocks in a single-entry loop, given the loop entry and the
191e6d15924SDimitry Andric // list of blocks that enter the loop.
192e6d15924SDimitry Andric class LoopBlocks {
193e6d15924SDimitry Andric public:
LoopBlocks(MachineBasicBlock * Entry,const BlockSet & Enterers)194e6d15924SDimitry Andric LoopBlocks(MachineBasicBlock *Entry, const BlockSet &Enterers)
195e6d15924SDimitry Andric : Entry(Entry), Enterers(Enterers) {
196e6d15924SDimitry Andric calculate();
197d8e91e46SDimitry Andric }
198e6d15924SDimitry Andric
getBlocks()199e6d15924SDimitry Andric BlockSet &getBlocks() { return Blocks; }
200e6d15924SDimitry Andric
201e6d15924SDimitry Andric private:
202e6d15924SDimitry Andric MachineBasicBlock *Entry;
203e6d15924SDimitry Andric const BlockSet &Enterers;
204e6d15924SDimitry Andric
205e6d15924SDimitry Andric BlockSet Blocks;
206e6d15924SDimitry Andric
calculate()207e6d15924SDimitry Andric void calculate() {
208e6d15924SDimitry Andric // Going backwards from the loop entry, if we ignore the blocks entering
209e6d15924SDimitry Andric // from outside, we will traverse all the blocks in the loop.
210e6d15924SDimitry Andric BlockVector WorkList;
211e6d15924SDimitry Andric BlockSet AddedToWorkList;
212e6d15924SDimitry Andric Blocks.insert(Entry);
213e6d15924SDimitry Andric for (auto *Pred : Entry->predecessors()) {
214e6d15924SDimitry Andric if (!Enterers.count(Pred)) {
215e6d15924SDimitry Andric WorkList.push_back(Pred);
216e6d15924SDimitry Andric AddedToWorkList.insert(Pred);
217d8e91e46SDimitry Andric }
218d8e91e46SDimitry Andric }
219d8e91e46SDimitry Andric
220d8e91e46SDimitry Andric while (!WorkList.empty()) {
221e6d15924SDimitry Andric auto *MBB = WorkList.pop_back_val();
222e6d15924SDimitry Andric assert(!Enterers.count(MBB));
223e6d15924SDimitry Andric if (Blocks.insert(MBB).second) {
224d8e91e46SDimitry Andric for (auto *Pred : MBB->predecessors()) {
225145449b1SDimitry Andric if (AddedToWorkList.insert(Pred).second)
226e6d15924SDimitry Andric WorkList.push_back(Pred);
227d8e91e46SDimitry Andric }
228d8e91e46SDimitry Andric }
229e6d15924SDimitry Andric }
230e6d15924SDimitry Andric }
231e6d15924SDimitry Andric };
232e6d15924SDimitry Andric
233e6d15924SDimitry Andric class WebAssemblyFixIrreducibleControlFlow final : public MachineFunctionPass {
getPassName() const234e6d15924SDimitry Andric StringRef getPassName() const override {
235e6d15924SDimitry Andric return "WebAssembly Fix Irreducible Control Flow";
236e6d15924SDimitry Andric }
237e6d15924SDimitry Andric
238e6d15924SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
239e6d15924SDimitry Andric
240e6d15924SDimitry Andric bool processRegion(MachineBasicBlock *Entry, BlockSet &Blocks,
241e6d15924SDimitry Andric MachineFunction &MF);
242e6d15924SDimitry Andric
243e6d15924SDimitry Andric void makeSingleEntryLoop(BlockSet &Entries, BlockSet &Blocks,
244e6d15924SDimitry Andric MachineFunction &MF, const ReachabilityGraph &Graph);
245e6d15924SDimitry Andric
246e6d15924SDimitry Andric public:
247e6d15924SDimitry Andric static char ID; // Pass identification, replacement for typeid
WebAssemblyFixIrreducibleControlFlow()248e6d15924SDimitry Andric WebAssemblyFixIrreducibleControlFlow() : MachineFunctionPass(ID) {}
249e6d15924SDimitry Andric };
250e6d15924SDimitry Andric
processRegion(MachineBasicBlock * Entry,BlockSet & Blocks,MachineFunction & MF)251e6d15924SDimitry Andric bool WebAssemblyFixIrreducibleControlFlow::processRegion(
252e6d15924SDimitry Andric MachineBasicBlock *Entry, BlockSet &Blocks, MachineFunction &MF) {
253e6d15924SDimitry Andric bool Changed = false;
254e6d15924SDimitry Andric // Remove irreducibility before processing child loops, which may take
255e6d15924SDimitry Andric // multiple iterations.
256e6d15924SDimitry Andric while (true) {
257e6d15924SDimitry Andric ReachabilityGraph Graph(Entry, Blocks);
258e6d15924SDimitry Andric
259e6d15924SDimitry Andric bool FoundIrreducibility = false;
260e6d15924SDimitry Andric
261cfca06d7SDimitry Andric for (auto *LoopEntry : getSortedEntries(Graph.getLoopEntries())) {
262e6d15924SDimitry Andric // Find mutual entries - all entries which can reach this one, and
263e6d15924SDimitry Andric // are reached by it (that always includes LoopEntry itself). All mutual
264e6d15924SDimitry Andric // entries must be in the same loop, so if we have more than one, then we
265e6d15924SDimitry Andric // have irreducible control flow.
266e6d15924SDimitry Andric //
267cfca06d7SDimitry Andric // (Note that we need to sort the entries here, as otherwise the order can
268cfca06d7SDimitry Andric // matter: being mutual is a symmetric relationship, and each set of
269cfca06d7SDimitry Andric // mutuals will be handled properly no matter which we see first. However,
270cfca06d7SDimitry Andric // there can be multiple disjoint sets of mutuals, and which we process
271cfca06d7SDimitry Andric // first changes the output.)
272cfca06d7SDimitry Andric //
273e6d15924SDimitry Andric // Note that irreducibility may involve inner loops, e.g. imagine A
274e6d15924SDimitry Andric // starts one loop, and it has B inside it which starts an inner loop.
275e6d15924SDimitry Andric // If we add a branch from all the way on the outside to B, then in a
276e6d15924SDimitry Andric // sense B is no longer an "inner" loop, semantically speaking. We will
277e6d15924SDimitry Andric // fix that irreducibility by adding a block that dispatches to either
278e6d15924SDimitry Andric // either A or B, so B will no longer be an inner loop in our output.
279e6d15924SDimitry Andric // (A fancier approach might try to keep it as such.)
280e6d15924SDimitry Andric //
281e6d15924SDimitry Andric // Note that we still need to recurse into inner loops later, to handle
282e6d15924SDimitry Andric // the case where the irreducibility is entirely nested - we would not
283e6d15924SDimitry Andric // be able to identify that at this point, since the enclosing loop is
284e6d15924SDimitry Andric // a group of blocks all of whom can reach each other. (We'll see the
285e6d15924SDimitry Andric // irreducibility after removing branches to the top of that enclosing
286e6d15924SDimitry Andric // loop.)
287e6d15924SDimitry Andric BlockSet MutualLoopEntries;
288e6d15924SDimitry Andric MutualLoopEntries.insert(LoopEntry);
289e6d15924SDimitry Andric for (auto *OtherLoopEntry : Graph.getLoopEntries()) {
290e6d15924SDimitry Andric if (OtherLoopEntry != LoopEntry &&
291e6d15924SDimitry Andric Graph.canReach(LoopEntry, OtherLoopEntry) &&
292e6d15924SDimitry Andric Graph.canReach(OtherLoopEntry, LoopEntry)) {
293e6d15924SDimitry Andric MutualLoopEntries.insert(OtherLoopEntry);
294e6d15924SDimitry Andric }
295e6d15924SDimitry Andric }
296d8e91e46SDimitry Andric
297e6d15924SDimitry Andric if (MutualLoopEntries.size() > 1) {
298e6d15924SDimitry Andric makeSingleEntryLoop(MutualLoopEntries, Blocks, MF, Graph);
299e6d15924SDimitry Andric FoundIrreducibility = true;
300e6d15924SDimitry Andric Changed = true;
301d8e91e46SDimitry Andric break;
302d8e91e46SDimitry Andric }
303d8e91e46SDimitry Andric }
304e6d15924SDimitry Andric // Only go on to actually process the inner loops when we are done
305e6d15924SDimitry Andric // removing irreducible control flow and changing the graph. Modifying
306e6d15924SDimitry Andric // the graph as we go is possible, and that might let us avoid looking at
307e6d15924SDimitry Andric // the already-fixed loops again if we are careful, but all that is
308e6d15924SDimitry Andric // complex and bug-prone. Since irreducible loops are rare, just starting
309e6d15924SDimitry Andric // another iteration is best.
310e6d15924SDimitry Andric if (FoundIrreducibility) {
311e6d15924SDimitry Andric continue;
312d8e91e46SDimitry Andric }
313d8e91e46SDimitry Andric
314e6d15924SDimitry Andric for (auto *LoopEntry : Graph.getLoopEntries()) {
315e6d15924SDimitry Andric LoopBlocks InnerBlocks(LoopEntry, Graph.getLoopEnterers(LoopEntry));
316e6d15924SDimitry Andric // Each of these calls to processRegion may change the graph, but are
317e6d15924SDimitry Andric // guaranteed not to interfere with each other. The only changes we make
318e6d15924SDimitry Andric // to the graph are to add blocks on the way to a loop entry. As the
319e6d15924SDimitry Andric // loops are disjoint, that means we may only alter branches that exit
320e6d15924SDimitry Andric // another loop, which are ignored when recursing into that other loop
321e6d15924SDimitry Andric // anyhow.
322e6d15924SDimitry Andric if (processRegion(LoopEntry, InnerBlocks.getBlocks(), MF)) {
323e6d15924SDimitry Andric Changed = true;
324e6d15924SDimitry Andric }
325e6d15924SDimitry Andric }
326e6d15924SDimitry Andric
327e6d15924SDimitry Andric return Changed;
328e6d15924SDimitry Andric }
329e6d15924SDimitry Andric }
330e6d15924SDimitry Andric
331e6d15924SDimitry Andric // Given a set of entries to a single loop, create a single entry for that
332e6d15924SDimitry Andric // loop by creating a dispatch block for them, routing control flow using
333e6d15924SDimitry Andric // a helper variable. Also updates Blocks with any new blocks created, so
334e6d15924SDimitry Andric // that we properly track all the blocks in the region. But this does not update
335e6d15924SDimitry Andric // ReachabilityGraph; this will be updated in the caller of this function as
336e6d15924SDimitry Andric // needed.
makeSingleEntryLoop(BlockSet & Entries,BlockSet & Blocks,MachineFunction & MF,const ReachabilityGraph & Graph)337e6d15924SDimitry Andric void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
338e6d15924SDimitry Andric BlockSet &Entries, BlockSet &Blocks, MachineFunction &MF,
339e6d15924SDimitry Andric const ReachabilityGraph &Graph) {
340e6d15924SDimitry Andric assert(Entries.size() >= 2);
34101095a5dSDimitry Andric
342d8e91e46SDimitry Andric // Sort the entries to ensure a deterministic build.
343cfca06d7SDimitry Andric BlockVector SortedEntries = getSortedEntries(Entries);
34401095a5dSDimitry Andric
345d8e91e46SDimitry Andric #ifndef NDEBUG
346e3b55780SDimitry Andric for (auto *Block : SortedEntries)
347d8e91e46SDimitry Andric assert(Block->getNumber() != -1);
348d8e91e46SDimitry Andric if (SortedEntries.size() > 1) {
349e6d15924SDimitry Andric for (auto I = SortedEntries.begin(), E = SortedEntries.end() - 1; I != E;
350e6d15924SDimitry Andric ++I) {
351d8e91e46SDimitry Andric auto ANum = (*I)->getNumber();
352d8e91e46SDimitry Andric auto BNum = (*(std::next(I)))->getNumber();
353d8e91e46SDimitry Andric assert(ANum != BNum);
354d8e91e46SDimitry Andric }
355d8e91e46SDimitry Andric }
356d8e91e46SDimitry Andric #endif
357d8e91e46SDimitry Andric
358d8e91e46SDimitry Andric // Create a dispatch block which will contain a jump table to the entries.
35901095a5dSDimitry Andric MachineBasicBlock *Dispatch = MF.CreateMachineBasicBlock();
36001095a5dSDimitry Andric MF.insert(MF.end(), Dispatch);
361e6d15924SDimitry Andric Blocks.insert(Dispatch);
36201095a5dSDimitry Andric
36301095a5dSDimitry Andric // Add the jump table.
36401095a5dSDimitry Andric const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
365e6d15924SDimitry Andric MachineInstrBuilder MIB =
366e6d15924SDimitry Andric BuildMI(Dispatch, DebugLoc(), TII.get(WebAssembly::BR_TABLE_I32));
36701095a5dSDimitry Andric
36801095a5dSDimitry Andric // Add the register which will be used to tell the jump table which block to
36901095a5dSDimitry Andric // jump to.
37001095a5dSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
3711d5ae102SDimitry Andric Register Reg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
37201095a5dSDimitry Andric MIB.addReg(Reg);
37301095a5dSDimitry Andric
374d8e91e46SDimitry Andric // Compute the indices in the superheader, one for each bad block, and
375d8e91e46SDimitry Andric // add them as successors.
37601095a5dSDimitry Andric DenseMap<MachineBasicBlock *, unsigned> Indices;
377e6d15924SDimitry Andric for (auto *Entry : SortedEntries) {
378e6d15924SDimitry Andric auto Pair = Indices.insert(std::make_pair(Entry, 0));
379e6d15924SDimitry Andric assert(Pair.second);
38001095a5dSDimitry Andric
38101095a5dSDimitry Andric unsigned Index = MIB.getInstr()->getNumExplicitOperands() - 1;
38201095a5dSDimitry Andric Pair.first->second = Index;
38301095a5dSDimitry Andric
384e6d15924SDimitry Andric MIB.addMBB(Entry);
385e6d15924SDimitry Andric Dispatch->addSuccessor(Entry);
38601095a5dSDimitry Andric }
38701095a5dSDimitry Andric
388e6d15924SDimitry Andric // Rewrite the problematic successors for every block that wants to reach
389e6d15924SDimitry Andric // the bad blocks. For simplicity, we just introduce a new block for every
390e6d15924SDimitry Andric // edge we need to rewrite. (Fancier things are possible.)
391d8e91e46SDimitry Andric
392e6d15924SDimitry Andric BlockVector AllPreds;
393e6d15924SDimitry Andric for (auto *Entry : SortedEntries) {
394e6d15924SDimitry Andric for (auto *Pred : Entry->predecessors()) {
395d8e91e46SDimitry Andric if (Pred != Dispatch) {
396d8e91e46SDimitry Andric AllPreds.push_back(Pred);
397d8e91e46SDimitry Andric }
398d8e91e46SDimitry Andric }
399d8e91e46SDimitry Andric }
400d8e91e46SDimitry Andric
401e6d15924SDimitry Andric // This set stores predecessors within this loop.
402e6d15924SDimitry Andric DenseSet<MachineBasicBlock *> InLoop;
403e6d15924SDimitry Andric for (auto *Pred : AllPreds) {
404e6d15924SDimitry Andric for (auto *Entry : Pred->successors()) {
405e6d15924SDimitry Andric if (!Entries.count(Entry))
40601095a5dSDimitry Andric continue;
407e6d15924SDimitry Andric if (Graph.canReach(Entry, Pred)) {
408e6d15924SDimitry Andric InLoop.insert(Pred);
409e6d15924SDimitry Andric break;
410e6d15924SDimitry Andric }
411e6d15924SDimitry Andric }
412d8e91e46SDimitry Andric }
41301095a5dSDimitry Andric
414e6d15924SDimitry Andric // Record if each entry has a layout predecessor. This map stores
415cfca06d7SDimitry Andric // <<loop entry, Predecessor is within the loop?>, layout predecessor>
416cfca06d7SDimitry Andric DenseMap<PointerIntPair<MachineBasicBlock *, 1, bool>, MachineBasicBlock *>
417e6d15924SDimitry Andric EntryToLayoutPred;
418cfca06d7SDimitry Andric for (auto *Pred : AllPreds) {
419cfca06d7SDimitry Andric bool PredInLoop = InLoop.count(Pred);
420e6d15924SDimitry Andric for (auto *Entry : Pred->successors())
421e6d15924SDimitry Andric if (Entries.count(Entry) && Pred->isLayoutSuccessor(Entry))
422cfca06d7SDimitry Andric EntryToLayoutPred[{Entry, PredInLoop}] = Pred;
423cfca06d7SDimitry Andric }
424e6d15924SDimitry Andric
425e6d15924SDimitry Andric // We need to create at most two routing blocks per entry: one for
426e6d15924SDimitry Andric // predecessors outside the loop and one for predecessors inside the loop.
427e6d15924SDimitry Andric // This map stores
428cfca06d7SDimitry Andric // <<loop entry, Predecessor is within the loop?>, routing block>
429cfca06d7SDimitry Andric DenseMap<PointerIntPair<MachineBasicBlock *, 1, bool>, MachineBasicBlock *>
430cfca06d7SDimitry Andric Map;
431e6d15924SDimitry Andric for (auto *Pred : AllPreds) {
432e6d15924SDimitry Andric bool PredInLoop = InLoop.count(Pred);
433e6d15924SDimitry Andric for (auto *Entry : Pred->successors()) {
434cfca06d7SDimitry Andric if (!Entries.count(Entry) || Map.count({Entry, PredInLoop}))
435e6d15924SDimitry Andric continue;
436e6d15924SDimitry Andric // If there exists a layout predecessor of this entry and this predecessor
437e6d15924SDimitry Andric // is not that, we rather create a routing block after that layout
438e6d15924SDimitry Andric // predecessor to save a branch.
439cfca06d7SDimitry Andric if (auto *OtherPred = EntryToLayoutPred.lookup({Entry, PredInLoop}))
440cfca06d7SDimitry Andric if (OtherPred != Pred)
441e6d15924SDimitry Andric continue;
442e6d15924SDimitry Andric
443d8e91e46SDimitry Andric // This is a successor we need to rewrite.
444e6d15924SDimitry Andric MachineBasicBlock *Routing = MF.CreateMachineBasicBlock();
445e6d15924SDimitry Andric MF.insert(Pred->isLayoutSuccessor(Entry)
446e6d15924SDimitry Andric ? MachineFunction::iterator(Entry)
44701095a5dSDimitry Andric : MF.end(),
448e6d15924SDimitry Andric Routing);
449e6d15924SDimitry Andric Blocks.insert(Routing);
45001095a5dSDimitry Andric
45101095a5dSDimitry Andric // Set the jump table's register of the index of the block we wish to
45201095a5dSDimitry Andric // jump to, and jump to the jump table.
453e6d15924SDimitry Andric BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::CONST_I32), Reg)
454e6d15924SDimitry Andric .addImm(Indices[Entry]);
455e6d15924SDimitry Andric BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::BR)).addMBB(Dispatch);
456e6d15924SDimitry Andric Routing->addSuccessor(Dispatch);
457cfca06d7SDimitry Andric Map[{Entry, PredInLoop}] = Routing;
45801095a5dSDimitry Andric }
459e6d15924SDimitry Andric }
460e6d15924SDimitry Andric
461e6d15924SDimitry Andric for (auto *Pred : AllPreds) {
462e6d15924SDimitry Andric bool PredInLoop = InLoop.count(Pred);
46301095a5dSDimitry Andric // Remap the terminator operands and the successor list.
464e6d15924SDimitry Andric for (MachineInstr &Term : Pred->terminators())
46501095a5dSDimitry Andric for (auto &Op : Term.explicit_uses())
46601095a5dSDimitry Andric if (Op.isMBB() && Indices.count(Op.getMBB()))
467cfca06d7SDimitry Andric Op.setMBB(Map[{Op.getMBB(), PredInLoop}]);
468e6d15924SDimitry Andric
469e6d15924SDimitry Andric for (auto *Succ : Pred->successors()) {
470e6d15924SDimitry Andric if (!Entries.count(Succ))
471e6d15924SDimitry Andric continue;
472cfca06d7SDimitry Andric auto *Routing = Map[{Succ, PredInLoop}];
473e6d15924SDimitry Andric Pred->replaceSuccessor(Succ, Routing);
474e6d15924SDimitry Andric }
47501095a5dSDimitry Andric }
47601095a5dSDimitry Andric
47701095a5dSDimitry Andric // Create a fake default label, because br_table requires one.
47801095a5dSDimitry Andric MIB.addMBB(MIB.getInstr()
47901095a5dSDimitry Andric ->getOperand(MIB.getInstr()->getNumExplicitOperands() - 1)
48001095a5dSDimitry Andric .getMBB());
48101095a5dSDimitry Andric }
48201095a5dSDimitry Andric
483d8e91e46SDimitry Andric } // end anonymous namespace
484d8e91e46SDimitry Andric
485d8e91e46SDimitry Andric char WebAssemblyFixIrreducibleControlFlow::ID = 0;
486d8e91e46SDimitry Andric INITIALIZE_PASS(WebAssemblyFixIrreducibleControlFlow, DEBUG_TYPE,
487d8e91e46SDimitry Andric "Removes irreducible control flow", false, false)
488d8e91e46SDimitry Andric
createWebAssemblyFixIrreducibleControlFlow()489d8e91e46SDimitry Andric FunctionPass *llvm::createWebAssemblyFixIrreducibleControlFlow() {
490d8e91e46SDimitry Andric return new WebAssemblyFixIrreducibleControlFlow();
491d8e91e46SDimitry Andric }
492d8e91e46SDimitry Andric
493145449b1SDimitry Andric // Test whether the given register has an ARGUMENT def.
hasArgumentDef(unsigned Reg,const MachineRegisterInfo & MRI)494145449b1SDimitry Andric static bool hasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
495145449b1SDimitry Andric for (const auto &Def : MRI.def_instructions(Reg))
496145449b1SDimitry Andric if (WebAssembly::isArgument(Def.getOpcode()))
497145449b1SDimitry Andric return true;
498145449b1SDimitry Andric return false;
499145449b1SDimitry Andric }
500145449b1SDimitry Andric
501145449b1SDimitry Andric // Add a register definition with IMPLICIT_DEFs for every register to cover for
502145449b1SDimitry Andric // register uses that don't have defs in every possible path.
503145449b1SDimitry Andric // TODO: This is fairly heavy-handed; find a better approach.
addImplicitDefs(MachineFunction & MF)504145449b1SDimitry Andric static void addImplicitDefs(MachineFunction &MF) {
505145449b1SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
506145449b1SDimitry Andric const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
507145449b1SDimitry Andric MachineBasicBlock &Entry = *MF.begin();
508145449b1SDimitry Andric for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
509145449b1SDimitry Andric Register Reg = Register::index2VirtReg(I);
510145449b1SDimitry Andric
511145449b1SDimitry Andric // Skip unused registers.
512145449b1SDimitry Andric if (MRI.use_nodbg_empty(Reg))
513145449b1SDimitry Andric continue;
514145449b1SDimitry Andric
515145449b1SDimitry Andric // Skip registers that have an ARGUMENT definition.
516145449b1SDimitry Andric if (hasArgumentDef(Reg, MRI))
517145449b1SDimitry Andric continue;
518145449b1SDimitry Andric
519145449b1SDimitry Andric BuildMI(Entry, Entry.begin(), DebugLoc(),
520145449b1SDimitry Andric TII.get(WebAssembly::IMPLICIT_DEF), Reg);
521145449b1SDimitry Andric }
522145449b1SDimitry Andric
523145449b1SDimitry Andric // Move ARGUMENT_* instructions to the top of the entry block, so that their
524145449b1SDimitry Andric // liveness reflects the fact that these really are live-in values.
525145449b1SDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(Entry)) {
526145449b1SDimitry Andric if (WebAssembly::isArgument(MI.getOpcode())) {
527145449b1SDimitry Andric MI.removeFromParent();
528145449b1SDimitry Andric Entry.insert(Entry.begin(), &MI);
529145449b1SDimitry Andric }
530145449b1SDimitry Andric }
531145449b1SDimitry Andric }
532145449b1SDimitry Andric
runOnMachineFunction(MachineFunction & MF)53301095a5dSDimitry Andric bool WebAssemblyFixIrreducibleControlFlow::runOnMachineFunction(
53401095a5dSDimitry Andric MachineFunction &MF) {
535eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "********** Fixing Irreducible Control Flow **********\n"
53601095a5dSDimitry Andric "********** Function: "
53701095a5dSDimitry Andric << MF.getName() << '\n');
53801095a5dSDimitry Andric
539e6d15924SDimitry Andric // Start the recursive process on the entire function body.
540e6d15924SDimitry Andric BlockSet AllBlocks;
541e6d15924SDimitry Andric for (auto &MBB : MF) {
542e6d15924SDimitry Andric AllBlocks.insert(&MBB);
54301095a5dSDimitry Andric }
54401095a5dSDimitry Andric
545e6d15924SDimitry Andric if (LLVM_UNLIKELY(processRegion(&*MF.begin(), AllBlocks, MF))) {
546e6d15924SDimitry Andric // We rewrote part of the function; recompute relevant things.
547e6d15924SDimitry Andric MF.RenumberBlocks();
548145449b1SDimitry Andric // Now we've inserted dispatch blocks, some register uses can have incoming
549145449b1SDimitry Andric // paths without a def. For example, before this pass register %a was
550145449b1SDimitry Andric // defined in BB1 and used in BB2, and there was only one path from BB1 and
551145449b1SDimitry Andric // BB2. But if this pass inserts a dispatch block having multiple
552145449b1SDimitry Andric // predecessors between the two BBs, now there are paths to BB2 without
553145449b1SDimitry Andric // visiting BB1, and %a's use in BB2 is not dominated by its def. Adding
554145449b1SDimitry Andric // IMPLICIT_DEFs to all regs is one simple way to fix it.
555145449b1SDimitry Andric addImplicitDefs(MF);
556e6d15924SDimitry Andric return true;
557e6d15924SDimitry Andric }
558e6d15924SDimitry Andric
559e6d15924SDimitry Andric return false;
56001095a5dSDimitry Andric }
561