blob: 65bb738fc43b3fb9ae62d76e194278472c006f27 [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///
Dan Gohman442bfce2016-02-16 16:22:41 +000013/// This pass reorders the blocks in a function to put them into topological
14/// order, ignoring loop backedges, and without any loop being interrupted
15/// by a block not dominated by the loop header, with special care to keep the
16/// order as similar as possible to the original order.
Dan Gohman950a13c2015-09-16 16:51:30 +000017///
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///
Dan Gohman950a13c2015-09-16 16:51:30 +000024//===----------------------------------------------------------------------===//
25
26#include "WebAssembly.h"
27#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmaned0f1132016-01-30 05:01:06 +000028#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000029#include "WebAssemblySubtarget.h"
Dan Gohman442bfce2016-02-16 16:22:41 +000030#include "llvm/ADT/PriorityQueue.h"
Dan Gohman8fe7e862015-12-14 22:51:54 +000031#include "llvm/ADT/SetVector.h"
Dan Gohman32807932015-11-23 16:19:56 +000032#include "llvm/CodeGen/MachineDominators.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineLoopInfo.h"
Derek Schuff9c3bf312016-01-13 17:10:28 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000037#include "llvm/CodeGen/Passes.h"
38#include "llvm/Support/Debug.h"
39#include "llvm/Support/raw_ostream.h"
40using namespace llvm;
41
42#define DEBUG_TYPE "wasm-cfg-stackify"
43
44namespace {
45class WebAssemblyCFGStackify final : public MachineFunctionPass {
46 const char *getPassName() const override {
47 return "WebAssembly CFG Stackify";
48 }
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesCFG();
Dan Gohman32807932015-11-23 16:19:56 +000052 AU.addRequired<MachineDominatorTree>();
53 AU.addPreserved<MachineDominatorTree>();
Dan Gohman950a13c2015-09-16 16:51:30 +000054 AU.addRequired<MachineLoopInfo>();
55 AU.addPreserved<MachineLoopInfo>();
56 MachineFunctionPass::getAnalysisUsage(AU);
57 }
58
59 bool runOnMachineFunction(MachineFunction &MF) override;
60
61public:
62 static char ID; // Pass identification, replacement for typeid
63 WebAssemblyCFGStackify() : MachineFunctionPass(ID) {}
64};
65} // end anonymous namespace
66
67char WebAssemblyCFGStackify::ID = 0;
68FunctionPass *llvm::createWebAssemblyCFGStackify() {
69 return new WebAssemblyCFGStackify();
70}
71
Dan Gohman8fe7e862015-12-14 22:51:54 +000072/// Return the "bottom" block of a loop. This differs from
73/// MachineLoop::getBottomBlock in that it works even if the loop is
74/// discontiguous.
75static MachineBasicBlock *LoopBottom(const MachineLoop *Loop) {
76 MachineBasicBlock *Bottom = Loop->getHeader();
77 for (MachineBasicBlock *MBB : Loop->blocks())
78 if (MBB->getNumber() > Bottom->getNumber())
79 Bottom = MBB;
80 return Bottom;
81}
82
Dan Gohman442bfce2016-02-16 16:22:41 +000083static void MaybeUpdateTerminator(MachineBasicBlock *MBB) {
84#ifndef NDEBUG
85 bool AnyBarrier = false;
86#endif
87 bool AllAnalyzable = true;
88 for (const MachineInstr &Term : MBB->terminators()) {
89#ifndef NDEBUG
90 AnyBarrier |= Term.isBarrier();
91#endif
92 AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
93 }
94 assert((AnyBarrier || AllAnalyzable) &&
95 "AnalyzeBranch needs to analyze any block with a fallthrough");
96 if (AllAnalyzable)
97 MBB->updateTerminator();
98}
Dan Gohman950a13c2015-09-16 16:51:30 +000099
Dan Gohman442bfce2016-02-16 16:22:41 +0000100namespace {
101/// Sort blocks by their number.
102struct CompareBlockNumbers {
103 bool operator()(const MachineBasicBlock *A,
104 const MachineBasicBlock *B) const {
105 return A->getNumber() > B->getNumber();
106 }
107};
108/// Sort blocks by their number in the opposite order..
109struct CompareBlockNumbersBackwards {
110 bool operator()(const MachineBasicBlock *A,
111 const MachineBasicBlock *B) const {
112 return A->getNumber() < B->getNumber();
113 }
114};
115/// Bookkeeping for a loop to help ensure that we don't mix blocks not dominated
116/// by the loop header among the loop's blocks.
117struct Entry {
118 const MachineLoop *Loop;
119 unsigned NumBlocksLeft;
Dan Gohman950a13c2015-09-16 16:51:30 +0000120
Dan Gohman442bfce2016-02-16 16:22:41 +0000121 /// List of blocks not dominated by Loop's header that are deferred until
122 /// after all of Loop's blocks have been seen.
123 std::vector<MachineBasicBlock *> Deferred;
Dan Gohman950a13c2015-09-16 16:51:30 +0000124
Dan Gohman442bfce2016-02-16 16:22:41 +0000125 explicit Entry(const MachineLoop *L)
126 : Loop(L), NumBlocksLeft(L->getNumBlocks()) {}
127};
128}
Dan Gohman950a13c2015-09-16 16:51:30 +0000129
Dan Gohman442bfce2016-02-16 16:22:41 +0000130/// Sort the blocks, taking special care to make sure that loops are not
131/// interrupted by blocks not dominated by their header.
132/// TODO: There are many opportunities for improving the heuristics here.
133/// Explore them.
134static void SortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
135 const MachineDominatorTree &MDT) {
136 // Prepare for a topological sort: Record the number of predecessors each
137 // block has, ignoring loop backedges.
138 MF.RenumberBlocks();
139 SmallVector<unsigned, 16> NumPredsLeft(MF.getNumBlockIDs(), 0);
140 for (MachineBasicBlock &MBB : MF) {
141 unsigned N = MBB.pred_size();
142 if (MachineLoop *L = MLI.getLoopFor(&MBB))
143 if (L->getHeader() == &MBB)
144 for (const MachineBasicBlock *Pred : MBB.predecessors())
145 if (L->contains(Pred))
146 --N;
147 NumPredsLeft[MBB.getNumber()] = N;
Dan Gohman950a13c2015-09-16 16:51:30 +0000148 }
149
Dan Gohman442bfce2016-02-16 16:22:41 +0000150 // Topological sort the CFG, with additional constraints:
151 // - Between a loop header and the last block in the loop, there can be
152 // no blocks not dominated by the loop header.
153 // - It's desirable to preserve the original block order when possible.
154 // We use two ready lists; Preferred and Ready. Preferred has recently
155 // processed sucessors, to help preserve block sequences from the original
156 // order. Ready has the remaining ready blocks.
157 PriorityQueue<MachineBasicBlock *, std::vector<MachineBasicBlock *>,
158 CompareBlockNumbers>
159 Preferred;
160 PriorityQueue<MachineBasicBlock *, std::vector<MachineBasicBlock *>,
161 CompareBlockNumbersBackwards>
162 Ready;
163 SmallVector<Entry, 4> Loops;
164 for (MachineBasicBlock *MBB = &MF.front();;) {
165 const MachineLoop *L = MLI.getLoopFor(MBB);
166 if (L) {
167 // If MBB is a loop header, add it to the active loop list. We can't put
168 // any blocks that it doesn't dominate until we see the end of the loop.
169 if (L->getHeader() == MBB)
170 Loops.push_back(Entry(L));
171 // For each active loop the block is in, decrement the count. If MBB is
172 // the last block in an active loop, take it off the list and pick up any
173 // blocks deferred because the header didn't dominate them.
174 for (Entry &E : Loops)
175 if (E.Loop->contains(MBB) && --E.NumBlocksLeft == 0)
176 for (auto DeferredBlock : E.Deferred)
177 Ready.push(DeferredBlock);
178 while (!Loops.empty() && Loops.back().NumBlocksLeft == 0)
179 Loops.pop_back();
180 }
181 // The main topological sort logic.
182 for (MachineBasicBlock *Succ : MBB->successors()) {
183 // Ignore backedges.
184 if (MachineLoop *SuccL = MLI.getLoopFor(Succ))
185 if (SuccL->getHeader() == Succ && SuccL->contains(MBB))
186 continue;
187 // Decrement the predecessor count. If it's now zero, it's ready.
188 if (--NumPredsLeft[Succ->getNumber()] == 0)
189 Preferred.push(Succ);
190 }
191 // Determine the block to follow MBB. First try to find a preferred block,
192 // to preserve the original block order when possible.
193 MachineBasicBlock *Next = nullptr;
194 while (!Preferred.empty()) {
195 Next = Preferred.top();
196 Preferred.pop();
197 // If X isn't dominated by the top active loop header, defer it until that
198 // loop is done.
199 if (!Loops.empty() &&
200 !MDT.dominates(Loops.back().Loop->getHeader(), Next)) {
201 Loops.back().Deferred.push_back(Next);
202 Next = nullptr;
203 continue;
204 }
205 // If Next was originally ordered before MBB, and it isn't because it was
206 // loop-rotated above the header, it's not preferred.
207 if (Next->getNumber() < MBB->getNumber() &&
208 (!L || !L->contains(Next) ||
209 L->getHeader()->getNumber() < Next->getNumber())) {
210 Ready.push(Next);
211 Next = nullptr;
212 continue;
213 }
214 break;
215 }
216 // If we didn't find a suitable block in the Preferred list, check the
217 // general Ready list.
218 if (!Next) {
219 // If there are no more blocks to process, we're done.
220 if (Ready.empty()) {
221 MaybeUpdateTerminator(MBB);
222 break;
223 }
224 for (;;) {
225 Next = Ready.top();
226 Ready.pop();
227 // If Next isn't dominated by the top active loop header, defer it until
228 // that loop is done.
229 if (!Loops.empty() &&
230 !MDT.dominates(Loops.back().Loop->getHeader(), Next)) {
231 Loops.back().Deferred.push_back(Next);
232 continue;
233 }
234 break;
235 }
236 }
237 // Move the next block into place and iterate.
238 Next->moveAfter(MBB);
239 MaybeUpdateTerminator(MBB);
240 MBB = Next;
241 }
242 assert(Loops.empty() && "Active loop list not finished");
Dan Gohman950a13c2015-09-16 16:51:30 +0000243 MF.RenumberBlocks();
244
245#ifndef NDEBUG
Dan Gohman8fe7e862015-12-14 22:51:54 +0000246 SmallSetVector<MachineLoop *, 8> OnStack;
247
248 // Insert a sentinel representing the degenerate loop that starts at the
249 // function entry block and includes the entire function as a "loop" that
250 // executes once.
251 OnStack.insert(nullptr);
252
253 for (auto &MBB : MF) {
254 assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative.");
255
256 MachineLoop *Loop = MLI.getLoopFor(&MBB);
257 if (Loop && &MBB == Loop->getHeader()) {
258 // Loop header. The loop predecessor should be sorted above, and the other
259 // predecessors should be backedges below.
260 for (auto Pred : MBB.predecessors())
261 assert(
262 (Pred->getNumber() < MBB.getNumber() || Loop->contains(Pred)) &&
263 "Loop header predecessors must be loop predecessors or backedges");
264 assert(OnStack.insert(Loop) && "Loops should be declared at most once.");
Dan Gohman950a13c2015-09-16 16:51:30 +0000265 } else {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000266 // Not a loop header. All predecessors should be sorted above.
Dan Gohman950a13c2015-09-16 16:51:30 +0000267 for (auto Pred : MBB.predecessors())
268 assert(Pred->getNumber() < MBB.getNumber() &&
Dan Gohman8fe7e862015-12-14 22:51:54 +0000269 "Non-loop-header predecessors should be topologically sorted");
270 assert(OnStack.count(MLI.getLoopFor(&MBB)) &&
271 "Blocks must be nested in their loops");
Dan Gohman950a13c2015-09-16 16:51:30 +0000272 }
Dan Gohman8fe7e862015-12-14 22:51:54 +0000273 while (OnStack.size() > 1 && &MBB == LoopBottom(OnStack.back()))
274 OnStack.pop_back();
275 }
276 assert(OnStack.pop_back_val() == nullptr &&
277 "The function entry block shouldn't actually be a loop header");
278 assert(OnStack.empty() &&
279 "Control flow stack pushes and pops should be balanced.");
Dan Gohman950a13c2015-09-16 16:51:30 +0000280#endif
281}
282
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000283/// Test whether Pred has any terminators explicitly branching to MBB, as
284/// opposed to falling through. Note that it's possible (eg. in unoptimized
285/// code) for a branch instruction to both branch to a block and fallthrough
286/// to it, so we check the actual branch operands to see if there are any
287/// explicit mentions.
Dan Gohman35e4a282016-01-08 01:06:00 +0000288static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred,
289 MachineBasicBlock *MBB) {
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000290 for (MachineInstr &MI : Pred->terminators())
291 for (MachineOperand &MO : MI.explicit_operands())
292 if (MO.isMBB() && MO.getMBB() == MBB)
293 return true;
294 return false;
295}
296
Dan Gohmaned0f1132016-01-30 05:01:06 +0000297/// Test whether MI is a child of some other node in an expression tree.
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +0000298static bool IsChild(const MachineInstr &MI,
Dan Gohmaned0f1132016-01-30 05:01:06 +0000299 const WebAssemblyFunctionInfo &MFI) {
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +0000300 if (MI.getNumOperands() == 0)
Dan Gohmaned0f1132016-01-30 05:01:06 +0000301 return false;
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +0000302 const MachineOperand &MO = MI.getOperand(0);
Dan Gohmaned0f1132016-01-30 05:01:06 +0000303 if (!MO.isReg() || MO.isImplicit() || !MO.isDef())
304 return false;
305 unsigned Reg = MO.getReg();
306 return TargetRegisterInfo::isVirtualRegister(Reg) &&
307 MFI.isVRegStackified(Reg);
308}
309
Dan Gohman32807932015-11-23 16:19:56 +0000310/// Insert a BLOCK marker for branches to MBB (if needed).
Dan Gohman8fe7e862015-12-14 22:51:54 +0000311static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
312 SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
313 const WebAssemblyInstrInfo &TII,
314 const MachineLoopInfo &MLI,
Dan Gohmaned0f1132016-01-30 05:01:06 +0000315 MachineDominatorTree &MDT,
316 WebAssemblyFunctionInfo &MFI) {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000317 // First compute the nearest common dominator of all forward non-fallthrough
318 // predecessors so that we minimize the time that the BLOCK is on the stack,
319 // which reduces overall stack height.
Dan Gohman32807932015-11-23 16:19:56 +0000320 MachineBasicBlock *Header = nullptr;
321 bool IsBranchedTo = false;
322 int MBBNumber = MBB.getNumber();
323 for (MachineBasicBlock *Pred : MBB.predecessors())
324 if (Pred->getNumber() < MBBNumber) {
325 Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000326 if (ExplicitlyBranchesTo(Pred, &MBB))
Dan Gohman32807932015-11-23 16:19:56 +0000327 IsBranchedTo = true;
328 }
329 if (!Header)
330 return;
331 if (!IsBranchedTo)
Dan Gohman950a13c2015-09-16 16:51:30 +0000332 return;
333
Dan Gohman8fe7e862015-12-14 22:51:54 +0000334 assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors");
Benjamin Krameref0a45a2016-09-05 12:06:47 +0000335 MachineBasicBlock *LayoutPred = &*std::prev(MachineFunction::iterator(&MBB));
Dan Gohman8fe7e862015-12-14 22:51:54 +0000336
337 // If the nearest common dominator is inside a more deeply nested context,
338 // walk out to the nearest scope which isn't more deeply nested.
339 for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
340 if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
341 if (ScopeTop->getNumber() > Header->getNumber()) {
342 // Skip over an intervening scope.
Benjamin Krameref0a45a2016-09-05 12:06:47 +0000343 I = std::next(MachineFunction::iterator(ScopeTop));
Dan Gohman8fe7e862015-12-14 22:51:54 +0000344 } else {
345 // We found a scope level at an appropriate depth.
346 Header = ScopeTop;
347 break;
348 }
349 }
350 }
351
352 // If there's a loop which ends just before MBB which contains Header, we can
353 // reuse its label instead of inserting a new BLOCK.
354 for (MachineLoop *Loop = MLI.getLoopFor(LayoutPred);
355 Loop && Loop->contains(LayoutPred); Loop = Loop->getParentLoop())
356 if (Loop && LoopBottom(Loop) == LayoutPred && Loop->contains(Header))
357 return;
358
359 // Decide where in Header to put the BLOCK.
Dan Gohman32807932015-11-23 16:19:56 +0000360 MachineBasicBlock::iterator InsertPos;
361 MachineLoop *HeaderLoop = MLI.getLoopFor(Header);
Dan Gohman8fe7e862015-12-14 22:51:54 +0000362 if (HeaderLoop && MBB.getNumber() > LoopBottom(HeaderLoop)->getNumber()) {
363 // Header is the header of a loop that does not lexically contain MBB, so
Dan Gohmana187ab22016-02-12 21:19:25 +0000364 // the BLOCK needs to be above the LOOP, after any END constructs.
Dan Gohman32807932015-11-23 16:19:56 +0000365 InsertPos = Header->begin();
Dan Gohmana187ab22016-02-12 21:19:25 +0000366 while (InsertPos->getOpcode() != WebAssembly::LOOP)
367 ++InsertPos;
Dan Gohman32807932015-11-23 16:19:56 +0000368 } else {
Dan Gohman8887d1f2015-12-25 00:31:02 +0000369 // Otherwise, insert the BLOCK as late in Header as we can, but before the
370 // beginning of the local expression tree and any nested BLOCKs.
Dan Gohman32807932015-11-23 16:19:56 +0000371 InsertPos = Header->getFirstTerminator();
Benjamin Krameref0a45a2016-09-05 12:06:47 +0000372 while (InsertPos != Header->begin() &&
373 IsChild(*std::prev(InsertPos), MFI) &&
374 std::prev(InsertPos)->getOpcode() != WebAssembly::LOOP &&
375 std::prev(InsertPos)->getOpcode() != WebAssembly::END_BLOCK &&
376 std::prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP)
Dan Gohman32807932015-11-23 16:19:56 +0000377 --InsertPos;
Dan Gohman950a13c2015-09-16 16:51:30 +0000378 }
379
Dan Gohman8fe7e862015-12-14 22:51:54 +0000380 // Add the BLOCK.
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000381 BuildMI(*Header, InsertPos, DebugLoc(), TII.get(WebAssembly::BLOCK));
382
383 // Mark the end of the block.
384 InsertPos = MBB.begin();
385 while (InsertPos != MBB.end() &&
386 InsertPos->getOpcode() == WebAssembly::END_LOOP)
387 ++InsertPos;
388 BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::END_BLOCK));
Dan Gohman8fe7e862015-12-14 22:51:54 +0000389
390 // Track the farthest-spanning scope that ends at this point.
391 int Number = MBB.getNumber();
392 if (!ScopeTops[Number] ||
393 ScopeTops[Number]->getNumber() > Header->getNumber())
394 ScopeTops[Number] = Header;
395}
396
397/// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000398static void PlaceLoopMarker(
399 MachineBasicBlock &MBB, MachineFunction &MF,
400 SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
401 DenseMap<const MachineInstr *, const MachineBasicBlock *> &LoopTops,
402 const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI) {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000403 MachineLoop *Loop = MLI.getLoopFor(&MBB);
404 if (!Loop || Loop->getHeader() != &MBB)
405 return;
406
407 // The operand of a LOOP is the first block after the loop. If the loop is the
408 // bottom of the function, insert a dummy block at the end.
409 MachineBasicBlock *Bottom = LoopBottom(Loop);
Benjamin Krameref0a45a2016-09-05 12:06:47 +0000410 auto Iter = std::next(MachineFunction::iterator(Bottom));
Dan Gohman8fe7e862015-12-14 22:51:54 +0000411 if (Iter == MF.end()) {
412 MachineBasicBlock *Label = MF.CreateMachineBasicBlock();
413 // Give it a fake predecessor so that AsmPrinter prints its label.
414 Label->addSuccessor(Label);
415 MF.push_back(Label);
Benjamin Krameref0a45a2016-09-05 12:06:47 +0000416 Iter = std::next(MachineFunction::iterator(Bottom));
Dan Gohman8fe7e862015-12-14 22:51:54 +0000417 }
418 MachineBasicBlock *AfterLoop = &*Iter;
Dan Gohman8fe7e862015-12-14 22:51:54 +0000419
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000420 // Mark the beginning of the loop (after the end of any existing loop that
421 // ends here).
422 auto InsertPos = MBB.begin();
423 while (InsertPos != MBB.end() &&
424 InsertPos->getOpcode() == WebAssembly::END_LOOP)
425 ++InsertPos;
426 BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::LOOP));
427
428 // Mark the end of the loop.
429 MachineInstr *End = BuildMI(*AfterLoop, AfterLoop->begin(), DebugLoc(),
430 TII.get(WebAssembly::END_LOOP));
431 LoopTops[End] = &MBB;
Dan Gohman8fe7e862015-12-14 22:51:54 +0000432
433 assert((!ScopeTops[AfterLoop->getNumber()] ||
434 ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) &&
Dan Gohman442bfce2016-02-16 16:22:41 +0000435 "With block sorting the outermost loop for a block should be first.");
Dan Gohman8fe7e862015-12-14 22:51:54 +0000436 if (!ScopeTops[AfterLoop->getNumber()])
437 ScopeTops[AfterLoop->getNumber()] = &MBB;
Dan Gohman950a13c2015-09-16 16:51:30 +0000438}
439
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000440static unsigned
441GetDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack,
442 const MachineBasicBlock *MBB) {
443 unsigned Depth = 0;
444 for (auto X : reverse(Stack)) {
445 if (X == MBB)
446 break;
447 ++Depth;
448 }
449 assert(Depth < Stack.size() && "Branch destination should be in scope");
450 return Depth;
451}
452
Dan Gohman950a13c2015-09-16 16:51:30 +0000453/// Insert LOOP and BLOCK markers at appropriate places.
454static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI,
Dan Gohman32807932015-11-23 16:19:56 +0000455 const WebAssemblyInstrInfo &TII,
Dan Gohmaned0f1132016-01-30 05:01:06 +0000456 MachineDominatorTree &MDT,
457 WebAssemblyFunctionInfo &MFI) {
Dan Gohman8fe7e862015-12-14 22:51:54 +0000458 // For each block whose label represents the end of a scope, record the block
459 // which holds the beginning of the scope. This will allow us to quickly skip
460 // over scoped regions when walking blocks. We allocate one more than the
461 // number of blocks in the function to accommodate for the possible fake block
462 // we may insert at the end.
463 SmallVector<MachineBasicBlock *, 8> ScopeTops(MF.getNumBlockIDs() + 1);
464
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000465 // For eacn LOOP_END, the corresponding LOOP.
466 DenseMap<const MachineInstr *, const MachineBasicBlock *> LoopTops;
467
Dan Gohman950a13c2015-09-16 16:51:30 +0000468 for (auto &MBB : MF) {
Dan Gohman32807932015-11-23 16:19:56 +0000469 // Place the LOOP for MBB if MBB is the header of a loop.
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000470 PlaceLoopMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI);
Dan Gohman950a13c2015-09-16 16:51:30 +0000471
Dan Gohman32807932015-11-23 16:19:56 +0000472 // Place the BLOCK for MBB if MBB is branched to from above.
Dan Gohmaned0f1132016-01-30 05:01:06 +0000473 PlaceBlockMarker(MBB, MF, ScopeTops, TII, MLI, MDT, MFI);
Dan Gohman950a13c2015-09-16 16:51:30 +0000474 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000475
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000476 // Now rewrite references to basic blocks to be depth immediates.
477 SmallVector<const MachineBasicBlock *, 8> Stack;
478 for (auto &MBB : reverse(MF)) {
479 for (auto &MI : reverse(MBB)) {
480 switch (MI.getOpcode()) {
481 case WebAssembly::BLOCK:
482 assert(ScopeTops[Stack.back()->getNumber()] == &MBB &&
483 "Block should be balanced");
484 Stack.pop_back();
485 break;
486 case WebAssembly::LOOP:
487 assert(Stack.back() == &MBB && "Loop top should be balanced");
488 Stack.pop_back();
489 Stack.pop_back();
490 break;
491 case WebAssembly::END_BLOCK:
492 Stack.push_back(&MBB);
493 break;
494 case WebAssembly::END_LOOP:
495 Stack.push_back(&MBB);
496 Stack.push_back(LoopTops[&MI]);
497 break;
498 default:
499 if (MI.isTerminator()) {
500 // Rewrite MBB operands to be depth immediates.
501 SmallVector<MachineOperand, 4> Ops(MI.operands());
502 while (MI.getNumOperands() > 0)
503 MI.RemoveOperand(MI.getNumOperands() - 1);
504 for (auto MO : Ops) {
505 if (MO.isMBB())
506 MO = MachineOperand::CreateImm(GetDepth(Stack, MO.getMBB()));
507 MI.addOperand(MF, MO);
508 }
509 }
510 break;
511 }
512 }
513 }
514 assert(Stack.empty() && "Control flow should be balanced");
Dan Gohman32807932015-11-23 16:19:56 +0000515}
Dan Gohman32807932015-11-23 16:19:56 +0000516
Dan Gohman950a13c2015-09-16 16:51:30 +0000517bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
518 DEBUG(dbgs() << "********** CFG Stackifying **********\n"
519 "********** Function: "
520 << MF.getName() << '\n');
521
522 const auto &MLI = getAnalysis<MachineLoopInfo>();
Dan Gohman32807932015-11-23 16:19:56 +0000523 auto &MDT = getAnalysis<MachineDominatorTree>();
Derek Schuff9c3bf312016-01-13 17:10:28 +0000524 // Liveness is not tracked for EXPR_STACK physreg.
Dan Gohman950a13c2015-09-16 16:51:30 +0000525 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Dan Gohmaned0f1132016-01-30 05:01:06 +0000526 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Derek Schuff9c3bf312016-01-13 17:10:28 +0000527 MF.getRegInfo().invalidateLiveness();
Dan Gohman950a13c2015-09-16 16:51:30 +0000528
Dan Gohman442bfce2016-02-16 16:22:41 +0000529 // Sort the blocks, with contiguous loops.
530 SortBlocks(MF, MLI, MDT);
Dan Gohman950a13c2015-09-16 16:51:30 +0000531
532 // Place the BLOCK and LOOP markers to indicate the beginnings of scopes.
Dan Gohmaned0f1132016-01-30 05:01:06 +0000533 PlaceMarkers(MF, MLI, TII, MDT, MFI);
Dan Gohman32807932015-11-23 16:19:56 +0000534
Dan Gohman950a13c2015-09-16 16:51:30 +0000535 return true;
536}