blob: a39349c562fdd8f32721d3a493e1bc65d73f1b74 [file] [log] [blame]
Dan Gohman950a13c2015-09-16 16:51:30 +00001//===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements a CFG stacking pass.
12///
13/// This pass reorders the blocks in a function to put them into a reverse
14/// post-order [0], with special care to keep the order as similar as possible
15/// to the original order, and to keep loops contiguous even in the case of
16/// split backedges.
17///
18/// Then, it inserts BLOCK and LOOP markers to mark the start of scopes, since
19/// scope boundaries serve as the labels for WebAssembly's control transfers.
20///
21/// This is sufficient to convert arbitrary CFGs into a form that works on
22/// WebAssembly, provided that all loops are single-entry.
23///
24/// [0] https://en.wikipedia.org/wiki/Depth-first_search#Vertex_orderings
25///
26//===----------------------------------------------------------------------===//
27
28#include "WebAssembly.h"
29#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
30#include "WebAssemblySubtarget.h"
31#include "llvm/ADT/SCCIterator.h"
Dan Gohman8fe7e862015-12-14 22:51:54 +000032#include "llvm/ADT/SetVector.h"
Dan Gohman32807932015-11-23 16:19:56 +000033#include "llvm/CodeGen/MachineDominators.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000034#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
36#include "llvm/CodeGen/MachineLoopInfo.h"
Derek Schuff9c3bf312016-01-13 17:10:28 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000038#include "llvm/CodeGen/Passes.h"
39#include "llvm/Support/Debug.h"
40#include "llvm/Support/raw_ostream.h"
41using namespace llvm;
42
43#define DEBUG_TYPE "wasm-cfg-stackify"
44
45namespace {
46class WebAssemblyCFGStackify final : public MachineFunctionPass {
47 const char *getPassName() const override {
48 return "WebAssembly CFG Stackify";
49 }
50
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.setPreservesCFG();
Dan Gohman32807932015-11-23 16:19:56 +000053 AU.addRequired<MachineDominatorTree>();
54 AU.addPreserved<MachineDominatorTree>();
Dan Gohman950a13c2015-09-16 16:51:30 +000055 AU.addRequired<MachineLoopInfo>();
56 AU.addPreserved<MachineLoopInfo>();
57 MachineFunctionPass::getAnalysisUsage(AU);
58 }
59
60 bool runOnMachineFunction(MachineFunction &MF) override;
61
62public:
63 static char ID; // Pass identification, replacement for typeid
64 WebAssemblyCFGStackify() : MachineFunctionPass(ID) {}
65};
66} // end anonymous namespace
67
68char WebAssemblyCFGStackify::ID = 0;
69FunctionPass *llvm::createWebAssemblyCFGStackify() {
70 return new WebAssemblyCFGStackify();
71}
72
73static void EliminateMultipleEntryLoops(MachineFunction &MF,
74 const MachineLoopInfo &MLI) {
75 SmallPtrSet<MachineBasicBlock *, 8> InSet;
76 for (scc_iterator<MachineFunction *> I = scc_begin(&MF), E = scc_end(&MF);
77 I != E; ++I) {
78 const std::vector<MachineBasicBlock *> &CurrentSCC = *I;
79
80 // Skip trivial SCCs.
81 if (CurrentSCC.size() == 1)
82 continue;
83
84 InSet.insert(CurrentSCC.begin(), CurrentSCC.end());
85 MachineBasicBlock *Header = nullptr;
86 for (MachineBasicBlock *MBB : CurrentSCC) {
87 for (MachineBasicBlock *Pred : MBB->predecessors()) {
88 if (InSet.count(Pred))
89 continue;
90 if (!Header) {
91 Header = MBB;
92 break;
93 }
94 // TODO: Implement multiple-entry loops.
95 report_fatal_error("multiple-entry loops are not supported yet");
96 }
97 }
98 assert(MLI.isLoopHeader(Header));
99
100 InSet.clear();
101 }
102}
103
104namespace {
105/// Post-order traversal stack entry.
106struct POStackEntry {
107 MachineBasicBlock *MBB;
108 SmallVector<MachineBasicBlock *, 0> Succs;
109
110 POStackEntry(MachineBasicBlock *MBB, MachineFunction &MF,
111 const MachineLoopInfo &MLI);
112};
113} // end anonymous namespace
114
Dan Gohman32807932015-11-23 16:19:56 +0000115static bool LoopContains(const MachineLoop *Loop,
116 const MachineBasicBlock *MBB) {
117 return Loop ? Loop->contains(MBB) : true;
118}
119
Dan Gohman950a13c2015-09-16 16:51:30 +0000120POStackEntry::POStackEntry(MachineBasicBlock *MBB, MachineFunction &MF,
121 const MachineLoopInfo &MLI)
122 : MBB(MBB), Succs(MBB->successors()) {
123 // RPO is not a unique form, since at every basic block with multiple
124 // successors, the DFS has to pick which order to visit the successors in.
125 // Sort them strategically (see below).
126 MachineLoop *Loop = MLI.getLoopFor(MBB);
127 MachineFunction::iterator Next = next(MachineFunction::iterator(MBB));
128 MachineBasicBlock *LayoutSucc = Next == MF.end() ? nullptr : &*Next;
129 std::stable_sort(
130 Succs.begin(), Succs.end(),
131 [=, &MLI](const MachineBasicBlock *A, const MachineBasicBlock *B) {
132 if (A == B)
133 return false;
134
135 // Keep loops contiguous by preferring the block that's in the same
136 // loop.
Dan Gohman32807932015-11-23 16:19:56 +0000137 bool LoopContainsA = LoopContains(Loop, A);
138 bool LoopContainsB = LoopContains(Loop, B);
139 if (LoopContainsA && !LoopContainsB)
Dan Gohman950a13c2015-09-16 16:51:30 +0000140 return true;
Dan Gohman32807932015-11-23 16:19:56 +0000141 if (!LoopContainsA && LoopContainsB)
Dan Gohman950a13c2015-09-16 16:51:30 +0000142 return false;
143
144 // Minimize perturbation by preferring the block which is the immediate
145 // layout successor.
146 if (A == LayoutSucc)
147 return true;
148 if (B == LayoutSucc)
149 return false;
150
151 // TODO: More sophisticated orderings may be profitable here.
152
153 return false;
154 });
155}
156
Dan Gohman8fe7e862015-12-14 22:51:54 +0000157/// Return the "bottom" block of a loop. This differs from
158/// MachineLoop::getBottomBlock in that it works even if the loop is
159/// discontiguous.
160static MachineBasicBlock *LoopBottom(const MachineLoop *Loop) {
161 MachineBasicBlock *Bottom = Loop->getHeader();
162 for (MachineBasicBlock *MBB : Loop->blocks())
163 if (MBB->getNumber() > Bottom->getNumber())
164 Bottom = MBB;
165 return Bottom;
166}
167
Dan Gohman950a13c2015-09-16 16:51:30 +0000168/// Sort the blocks in RPO, taking special care to make sure that loops are
169/// contiguous even in the case of split backedges.
Dan Gohman8fe7e862015-12-14 22:51:54 +0000170///
171/// TODO: Determine whether RPO is actually worthwhile, or whether we should
172/// move to just a stable-topological-sort-based approach that would preserve
173/// more of the original order.
Dan Gohman950a13c2015-09-16 16:51:30 +0000174static void SortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI) {
175 // Note that we do our own RPO rather than using
176 // "llvm/ADT/PostOrderIterator.h" because we want control over the order that
177 // successors are visited in (see above). Also, we can sort the blocks in the
178 // MachineFunction as we go.
179 SmallPtrSet<MachineBasicBlock *, 16> Visited;
180 SmallVector<POStackEntry, 16> Stack;
181
Dan Gohman96029f72015-11-30 18:42:08 +0000182 MachineBasicBlock *EntryBlock = &*MF.begin();
183 Visited.insert(EntryBlock);
184 Stack.push_back(POStackEntry(EntryBlock, MF, MLI));
Dan Gohman950a13c2015-09-16 16:51:30 +0000185
186 for (;;) {
187 POStackEntry &Entry = Stack.back();
188 SmallVectorImpl<MachineBasicBlock *> &Succs = Entry.Succs;
189 if (!Succs.empty()) {
190 MachineBasicBlock *Succ = Succs.pop_back_val();
191 if (Visited.insert(Succ).second)
192 Stack.push_back(POStackEntry(Succ, MF, MLI));
193 continue;
194 }
195
196 // Put the block in its position in the MachineFunction.
197 MachineBasicBlock &MBB = *Entry.MBB;
Nico Weber00406472015-11-07 02:47:31 +0000198 MBB.moveBefore(&*MF.begin());
Dan Gohman950a13c2015-09-16 16:51:30 +0000199
200 // Branch instructions may utilize a fallthrough, so update them if a
201 // fallthrough has been added or removed.
202 if (!MBB.empty() && MBB.back().isTerminator() && !MBB.back().isBranch() &&
203 !MBB.back().isBarrier())
204 report_fatal_error(
205 "Non-branch terminator with fallthrough cannot yet be rewritten");
206 if (MBB.empty() || !MBB.back().isTerminator() || MBB.back().isBranch())
207 MBB.updateTerminator();
208
209 Stack.pop_back();
210 if (Stack.empty())
211 break;
212 }
213
214 // Now that we've sorted the blocks in RPO, renumber them.
215 MF.RenumberBlocks();
216
217#ifndef NDEBUG
Dan Gohman8fe7e862015-12-14 22:51:54 +0000218 SmallSetVector<MachineLoop *, 8> OnStack;
219
220 // Insert a sentinel representing the degenerate loop that starts at the
221 // function entry block and includes the entire function as a "loop" that
222 // executes once.
223 OnStack.insert(nullptr);
224
225 for (auto &MBB : MF) {
226 assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative.");
227
228 MachineLoop *Loop = MLI.getLoopFor(&MBB);
229 if (Loop && &MBB == Loop->getHeader()) {
230 // Loop header. The loop predecessor should be sorted above, and the other
231 // predecessors should be backedges below.
232 for (auto Pred : MBB.predecessors())
233 assert(
234 (Pred->getNumber() < MBB.getNumber() || Loop->contains(Pred)) &&
235 "Loop header predecessors must be loop predecessors or backedges");
236 assert(OnStack.insert(Loop) && "Loops should be declared at most once.");
Dan Gohman950a13c2015-09-16 16:51:30 +0000237 } else {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000238 // Not a loop header. All predecessors should be sorted above.
Dan Gohman950a13c2015-09-16 16:51:30 +0000239 for (auto Pred : MBB.predecessors())
240 assert(Pred->getNumber() < MBB.getNumber() &&
Dan Gohman8fe7e862015-12-14 22:51:54 +0000241 "Non-loop-header predecessors should be topologically sorted");
242 assert(OnStack.count(MLI.getLoopFor(&MBB)) &&
243 "Blocks must be nested in their loops");
Dan Gohman950a13c2015-09-16 16:51:30 +0000244 }
Dan Gohman8fe7e862015-12-14 22:51:54 +0000245 while (OnStack.size() > 1 && &MBB == LoopBottom(OnStack.back()))
246 OnStack.pop_back();
247 }
248 assert(OnStack.pop_back_val() == nullptr &&
249 "The function entry block shouldn't actually be a loop header");
250 assert(OnStack.empty() &&
251 "Control flow stack pushes and pops should be balanced.");
Dan Gohman950a13c2015-09-16 16:51:30 +0000252#endif
253}
254
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000255/// Test whether Pred has any terminators explicitly branching to MBB, as
256/// opposed to falling through. Note that it's possible (eg. in unoptimized
257/// code) for a branch instruction to both branch to a block and fallthrough
258/// to it, so we check the actual branch operands to see if there are any
259/// explicit mentions.
Dan Gohman35e4a282016-01-08 01:06:00 +0000260static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred,
261 MachineBasicBlock *MBB) {
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000262 for (MachineInstr &MI : Pred->terminators())
263 for (MachineOperand &MO : MI.explicit_operands())
264 if (MO.isMBB() && MO.getMBB() == MBB)
265 return true;
266 return false;
267}
268
Dan Gohman32807932015-11-23 16:19:56 +0000269/// Insert a BLOCK marker for branches to MBB (if needed).
Dan Gohman8fe7e862015-12-14 22:51:54 +0000270static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
271 SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
272 const WebAssemblyInstrInfo &TII,
273 const MachineLoopInfo &MLI,
274 MachineDominatorTree &MDT) {
275 // First compute the nearest common dominator of all forward non-fallthrough
276 // predecessors so that we minimize the time that the BLOCK is on the stack,
277 // which reduces overall stack height.
Dan Gohman32807932015-11-23 16:19:56 +0000278 MachineBasicBlock *Header = nullptr;
279 bool IsBranchedTo = false;
280 int MBBNumber = MBB.getNumber();
281 for (MachineBasicBlock *Pred : MBB.predecessors())
282 if (Pred->getNumber() < MBBNumber) {
283 Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000284 if (ExplicitlyBranchesTo(Pred, &MBB))
Dan Gohman32807932015-11-23 16:19:56 +0000285 IsBranchedTo = true;
286 }
287 if (!Header)
288 return;
289 if (!IsBranchedTo)
Dan Gohman950a13c2015-09-16 16:51:30 +0000290 return;
291
Dan Gohman8fe7e862015-12-14 22:51:54 +0000292 assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors");
293 MachineBasicBlock *LayoutPred = &*prev(MachineFunction::iterator(&MBB));
294
295 // If the nearest common dominator is inside a more deeply nested context,
296 // walk out to the nearest scope which isn't more deeply nested.
297 for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
298 if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
299 if (ScopeTop->getNumber() > Header->getNumber()) {
300 // Skip over an intervening scope.
301 I = next(MachineFunction::iterator(ScopeTop));
302 } else {
303 // We found a scope level at an appropriate depth.
304 Header = ScopeTop;
305 break;
306 }
307 }
308 }
309
310 // If there's a loop which ends just before MBB which contains Header, we can
311 // reuse its label instead of inserting a new BLOCK.
312 for (MachineLoop *Loop = MLI.getLoopFor(LayoutPred);
313 Loop && Loop->contains(LayoutPred); Loop = Loop->getParentLoop())
314 if (Loop && LoopBottom(Loop) == LayoutPred && Loop->contains(Header))
315 return;
316
317 // Decide where in Header to put the BLOCK.
Dan Gohman32807932015-11-23 16:19:56 +0000318 MachineBasicBlock::iterator InsertPos;
319 MachineLoop *HeaderLoop = MLI.getLoopFor(Header);
Dan Gohman8fe7e862015-12-14 22:51:54 +0000320 if (HeaderLoop && MBB.getNumber() > LoopBottom(HeaderLoop)->getNumber()) {
321 // Header is the header of a loop that does not lexically contain MBB, so
322 // the BLOCK needs to be above the LOOP.
Dan Gohman32807932015-11-23 16:19:56 +0000323 InsertPos = Header->begin();
Dan Gohman32807932015-11-23 16:19:56 +0000324 } else {
Dan Gohman8887d1f2015-12-25 00:31:02 +0000325 // Otherwise, insert the BLOCK as late in Header as we can, but before the
326 // beginning of the local expression tree and any nested BLOCKs.
Dan Gohman32807932015-11-23 16:19:56 +0000327 InsertPos = Header->getFirstTerminator();
328 while (InsertPos != Header->begin() &&
Dan Gohman8887d1f2015-12-25 00:31:02 +0000329 prev(InsertPos)->definesRegister(WebAssembly::EXPR_STACK) &&
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000330 prev(InsertPos)->getOpcode() != WebAssembly::LOOP &&
331 prev(InsertPos)->getOpcode() != WebAssembly::END_BLOCK &&
332 prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP)
Dan Gohman32807932015-11-23 16:19:56 +0000333 --InsertPos;
Dan Gohman950a13c2015-09-16 16:51:30 +0000334 }
335
Dan Gohman8fe7e862015-12-14 22:51:54 +0000336 // Add the BLOCK.
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000337 BuildMI(*Header, InsertPos, DebugLoc(), TII.get(WebAssembly::BLOCK));
338
339 // Mark the end of the block.
340 InsertPos = MBB.begin();
341 while (InsertPos != MBB.end() &&
342 InsertPos->getOpcode() == WebAssembly::END_LOOP)
343 ++InsertPos;
344 BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::END_BLOCK));
Dan Gohman8fe7e862015-12-14 22:51:54 +0000345
346 // Track the farthest-spanning scope that ends at this point.
347 int Number = MBB.getNumber();
348 if (!ScopeTops[Number] ||
349 ScopeTops[Number]->getNumber() > Header->getNumber())
350 ScopeTops[Number] = Header;
351}
352
353/// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000354static void PlaceLoopMarker(
355 MachineBasicBlock &MBB, MachineFunction &MF,
356 SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
357 DenseMap<const MachineInstr *, const MachineBasicBlock *> &LoopTops,
358 const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI) {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000359 MachineLoop *Loop = MLI.getLoopFor(&MBB);
360 if (!Loop || Loop->getHeader() != &MBB)
361 return;
362
363 // The operand of a LOOP is the first block after the loop. If the loop is the
364 // bottom of the function, insert a dummy block at the end.
365 MachineBasicBlock *Bottom = LoopBottom(Loop);
366 auto Iter = next(MachineFunction::iterator(Bottom));
367 if (Iter == MF.end()) {
368 MachineBasicBlock *Label = MF.CreateMachineBasicBlock();
369 // Give it a fake predecessor so that AsmPrinter prints its label.
370 Label->addSuccessor(Label);
371 MF.push_back(Label);
372 Iter = next(MachineFunction::iterator(Bottom));
373 }
374 MachineBasicBlock *AfterLoop = &*Iter;
Dan Gohman8fe7e862015-12-14 22:51:54 +0000375
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000376 // Mark the beginning of the loop (after the end of any existing loop that
377 // ends here).
378 auto InsertPos = MBB.begin();
379 while (InsertPos != MBB.end() &&
380 InsertPos->getOpcode() == WebAssembly::END_LOOP)
381 ++InsertPos;
382 BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::LOOP));
383
384 // Mark the end of the loop.
385 MachineInstr *End = BuildMI(*AfterLoop, AfterLoop->begin(), DebugLoc(),
386 TII.get(WebAssembly::END_LOOP));
387 LoopTops[End] = &MBB;
Dan Gohman8fe7e862015-12-14 22:51:54 +0000388
389 assert((!ScopeTops[AfterLoop->getNumber()] ||
390 ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) &&
391 "With RPO we should visit the outer-most loop for a block first.");
392 if (!ScopeTops[AfterLoop->getNumber()])
393 ScopeTops[AfterLoop->getNumber()] = &MBB;
Dan Gohman950a13c2015-09-16 16:51:30 +0000394}
395
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000396static unsigned
397GetDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack,
398 const MachineBasicBlock *MBB) {
399 unsigned Depth = 0;
400 for (auto X : reverse(Stack)) {
401 if (X == MBB)
402 break;
403 ++Depth;
404 }
405 assert(Depth < Stack.size() && "Branch destination should be in scope");
406 return Depth;
407}
408
Dan Gohman950a13c2015-09-16 16:51:30 +0000409/// Insert LOOP and BLOCK markers at appropriate places.
410static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI,
Dan Gohman32807932015-11-23 16:19:56 +0000411 const WebAssemblyInstrInfo &TII,
412 MachineDominatorTree &MDT) {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000413 // For each block whose label represents the end of a scope, record the block
414 // which holds the beginning of the scope. This will allow us to quickly skip
415 // over scoped regions when walking blocks. We allocate one more than the
416 // number of blocks in the function to accommodate for the possible fake block
417 // we may insert at the end.
418 SmallVector<MachineBasicBlock *, 8> ScopeTops(MF.getNumBlockIDs() + 1);
419
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000420 // For eacn LOOP_END, the corresponding LOOP.
421 DenseMap<const MachineInstr *, const MachineBasicBlock *> LoopTops;
422
Dan Gohman950a13c2015-09-16 16:51:30 +0000423 for (auto &MBB : MF) {
Dan Gohman32807932015-11-23 16:19:56 +0000424 // Place the LOOP for MBB if MBB is the header of a loop.
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000425 PlaceLoopMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI);
Dan Gohman950a13c2015-09-16 16:51:30 +0000426
Dan Gohman32807932015-11-23 16:19:56 +0000427 // Place the BLOCK for MBB if MBB is branched to from above.
Dan Gohman8fe7e862015-12-14 22:51:54 +0000428 PlaceBlockMarker(MBB, MF, ScopeTops, TII, MLI, MDT);
Dan Gohman950a13c2015-09-16 16:51:30 +0000429 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000430
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000431 // Now rewrite references to basic blocks to be depth immediates.
432 SmallVector<const MachineBasicBlock *, 8> Stack;
433 for (auto &MBB : reverse(MF)) {
434 for (auto &MI : reverse(MBB)) {
435 switch (MI.getOpcode()) {
436 case WebAssembly::BLOCK:
437 assert(ScopeTops[Stack.back()->getNumber()] == &MBB &&
438 "Block should be balanced");
439 Stack.pop_back();
440 break;
441 case WebAssembly::LOOP:
442 assert(Stack.back() == &MBB && "Loop top should be balanced");
443 Stack.pop_back();
444 Stack.pop_back();
445 break;
446 case WebAssembly::END_BLOCK:
447 Stack.push_back(&MBB);
448 break;
449 case WebAssembly::END_LOOP:
450 Stack.push_back(&MBB);
451 Stack.push_back(LoopTops[&MI]);
452 break;
453 default:
454 if (MI.isTerminator()) {
455 // Rewrite MBB operands to be depth immediates.
456 SmallVector<MachineOperand, 4> Ops(MI.operands());
457 while (MI.getNumOperands() > 0)
458 MI.RemoveOperand(MI.getNumOperands() - 1);
459 for (auto MO : Ops) {
460 if (MO.isMBB())
461 MO = MachineOperand::CreateImm(GetDepth(Stack, MO.getMBB()));
462 MI.addOperand(MF, MO);
463 }
464 }
465 break;
466 }
467 }
468 }
469 assert(Stack.empty() && "Control flow should be balanced");
Dan Gohman32807932015-11-23 16:19:56 +0000470}
Dan Gohman32807932015-11-23 16:19:56 +0000471
Dan Gohman950a13c2015-09-16 16:51:30 +0000472bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
473 DEBUG(dbgs() << "********** CFG Stackifying **********\n"
474 "********** Function: "
475 << MF.getName() << '\n');
476
477 const auto &MLI = getAnalysis<MachineLoopInfo>();
Dan Gohman32807932015-11-23 16:19:56 +0000478 auto &MDT = getAnalysis<MachineDominatorTree>();
Derek Schuff9c3bf312016-01-13 17:10:28 +0000479 // Liveness is not tracked for EXPR_STACK physreg.
Dan Gohman950a13c2015-09-16 16:51:30 +0000480 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff9c3bf312016-01-13 17:10:28 +0000481 MF.getRegInfo().invalidateLiveness();
Dan Gohman950a13c2015-09-16 16:51:30 +0000482
483 // RPO sorting needs all loops to be single-entry.
484 EliminateMultipleEntryLoops(MF, MLI);
485
486 // Sort the blocks in RPO, with contiguous loops.
487 SortBlocks(MF, MLI);
488
489 // Place the BLOCK and LOOP markers to indicate the beginnings of scopes.
Dan Gohman32807932015-11-23 16:19:56 +0000490 PlaceMarkers(MF, MLI, TII, MDT);
491
Dan Gohman950a13c2015-09-16 16:51:30 +0000492 return true;
493}