blob: a429bee466400f73393887f3c4e8bebb04a0c78f [file] [log] [blame]
Dan Gohman950a13c2015-09-16 16:51:30 +00001//===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman950a13c2015-09-16 16:51:30 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file implements a CFG stacking pass.
Dan Gohman950a13c2015-09-16 16:51:30 +000011///
Heejin Ahne76fa9e2018-08-16 23:50:59 +000012/// This pass inserts BLOCK, LOOP, and TRY markers to mark the start of scopes,
13/// since scope boundaries serve as the labels for WebAssembly's control
14/// transfers.
Dan Gohman950a13c2015-09-16 16:51:30 +000015///
16/// This is sufficient to convert arbitrary CFGs into a form that works on
17/// WebAssembly, provided that all loops are single-entry.
18///
Heejin Ahne76fa9e2018-08-16 23:50:59 +000019/// In case we use exceptions, this pass also fixes mismatches in unwind
20/// destinations created during transforming CFG into wasm structured format.
21///
Dan Gohman950a13c2015-09-16 16:51:30 +000022//===----------------------------------------------------------------------===//
23
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "WebAssembly.h"
Heejin Ahne76fa9e2018-08-16 23:50:59 +000025#include "WebAssemblyExceptionInfo.h"
Dan Gohmaned0f1132016-01-30 05:01:06 +000026#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000027#include "WebAssemblySubtarget.h"
Dan Gohman4fc4e422016-10-24 19:49:43 +000028#include "WebAssemblyUtilities.h"
Heejin Ahnc4ac74f2019-03-30 11:04:48 +000029#include "llvm/ADT/Statistic.h"
Dan Gohman32807932015-11-23 16:19:56 +000030#include "llvm/CodeGen/MachineDominators.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Heejin Ahne76fa9e2018-08-16 23:50:59 +000032#include "llvm/MC/MCAsmInfo.h"
Dan Gohman950a13c2015-09-16 16:51:30 +000033using namespace llvm;
34
35#define DEBUG_TYPE "wasm-cfg-stackify"
36
Heejin Ahnc4ac74f2019-03-30 11:04:48 +000037STATISTIC(NumUnwindMismatches, "Number of EH pad unwind mismatches found");
38
Dan Gohman950a13c2015-09-16 16:51:30 +000039namespace {
40class WebAssemblyCFGStackify final : public MachineFunctionPass {
Mehdi Amini117296c2016-10-01 02:56:57 +000041 StringRef getPassName() const override { return "WebAssembly CFG Stackify"; }
Dan Gohman950a13c2015-09-16 16:51:30 +000042
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman32807932015-11-23 16:19:56 +000044 AU.addRequired<MachineDominatorTree>();
Dan Gohman950a13c2015-09-16 16:51:30 +000045 AU.addRequired<MachineLoopInfo>();
Heejin Ahne76fa9e2018-08-16 23:50:59 +000046 AU.addRequired<WebAssemblyExceptionInfo>();
Dan Gohman950a13c2015-09-16 16:51:30 +000047 MachineFunctionPass::getAnalysisUsage(AU);
48 }
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51
Heejin Ahne76fa9e2018-08-16 23:50:59 +000052 // For each block whose label represents the end of a scope, record the block
53 // which holds the beginning of the scope. This will allow us to quickly skip
54 // over scoped regions when walking blocks.
55 SmallVector<MachineBasicBlock *, 8> ScopeTops;
56
Heejin Ahnc4ac74f2019-03-30 11:04:48 +000057 // Placing markers.
Heejin Ahne76fa9e2018-08-16 23:50:59 +000058 void placeMarkers(MachineFunction &MF);
59 void placeBlockMarker(MachineBasicBlock &MBB);
60 void placeLoopMarker(MachineBasicBlock &MBB);
61 void placeTryMarker(MachineBasicBlock &MBB);
Heejin Ahncf699b42019-02-27 00:50:53 +000062 void removeUnnecessaryInstrs(MachineFunction &MF);
Heejin Ahnc4ac74f2019-03-30 11:04:48 +000063 bool fixUnwindMismatches(MachineFunction &MF);
Heejin Ahne76fa9e2018-08-16 23:50:59 +000064 void rewriteDepthImmediates(MachineFunction &MF);
65 void fixEndsAtEndOfFunction(MachineFunction &MF);
66
67 // For each BLOCK|LOOP|TRY, the corresponding END_(BLOCK|LOOP|TRY).
68 DenseMap<const MachineInstr *, MachineInstr *> BeginToEnd;
69 // For each END_(BLOCK|LOOP|TRY), the corresponding BLOCK|LOOP|TRY.
70 DenseMap<const MachineInstr *, MachineInstr *> EndToBegin;
71 // <TRY marker, EH pad> map
72 DenseMap<const MachineInstr *, MachineBasicBlock *> TryToEHPad;
73 // <EH pad, TRY marker> map
74 DenseMap<const MachineBasicBlock *, MachineInstr *> EHPadToTry;
Heejin Ahne76fa9e2018-08-16 23:50:59 +000075
Heejin Ahnc4ac74f2019-03-30 11:04:48 +000076 // There can be an appendix block at the end of each function, shared for:
77 // - creating a correct signature for fallthrough returns
78 // - target for rethrows that need to unwind to the caller, but are trapped
79 // inside another try/catch
80 MachineBasicBlock *AppendixBB = nullptr;
81 MachineBasicBlock *getAppendixBlock(MachineFunction &MF) {
82 if (!AppendixBB) {
83 AppendixBB = MF.CreateMachineBasicBlock();
84 // Give it a fake predecessor so that AsmPrinter prints its label.
85 AppendixBB->addSuccessor(AppendixBB);
86 MF.push_back(AppendixBB);
87 }
88 return AppendixBB;
89 }
90
Heejin Ahncf699b42019-02-27 00:50:53 +000091 // Helper functions to register / unregister scope information created by
92 // marker instructions.
Heejin Ahne76fa9e2018-08-16 23:50:59 +000093 void registerScope(MachineInstr *Begin, MachineInstr *End);
94 void registerTryScope(MachineInstr *Begin, MachineInstr *End,
95 MachineBasicBlock *EHPad);
Heejin Ahncf699b42019-02-27 00:50:53 +000096 void unregisterScope(MachineInstr *Begin);
Heejin Ahne76fa9e2018-08-16 23:50:59 +000097
Dan Gohman950a13c2015-09-16 16:51:30 +000098public:
99 static char ID; // Pass identification, replacement for typeid
100 WebAssemblyCFGStackify() : MachineFunctionPass(ID) {}
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000101 ~WebAssemblyCFGStackify() override { releaseMemory(); }
102 void releaseMemory() override;
Dan Gohman950a13c2015-09-16 16:51:30 +0000103};
104} // end anonymous namespace
105
106char WebAssemblyCFGStackify::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +0000107INITIALIZE_PASS(WebAssemblyCFGStackify, DEBUG_TYPE,
Heejin Ahnc4ac74f2019-03-30 11:04:48 +0000108 "Insert BLOCK/LOOP/TRY markers for WebAssembly scopes", false,
Heejin Ahnf208f632018-09-05 01:27:38 +0000109 false)
Jacob Gravelle40926452018-03-30 20:36:58 +0000110
Dan Gohman950a13c2015-09-16 16:51:30 +0000111FunctionPass *llvm::createWebAssemblyCFGStackify() {
112 return new WebAssemblyCFGStackify();
113}
114
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000115/// Test whether Pred has any terminators explicitly branching to MBB, as
116/// opposed to falling through. Note that it's possible (eg. in unoptimized
117/// code) for a branch instruction to both branch to a block and fallthrough
118/// to it, so we check the actual branch operands to see if there are any
119/// explicit mentions.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000120static bool explicitlyBranchesTo(MachineBasicBlock *Pred,
Dan Gohman35e4a282016-01-08 01:06:00 +0000121 MachineBasicBlock *MBB) {
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000122 for (MachineInstr &MI : Pred->terminators())
Heejin Ahnd6f48782019-01-30 03:21:57 +0000123 for (MachineOperand &MO : MI.explicit_operands())
124 if (MO.isMBB() && MO.getMBB() == MBB)
125 return true;
Dan Gohmanb3aa1ec2015-12-16 19:06:41 +0000126 return false;
127}
128
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000129// Returns an iterator to the earliest position possible within the MBB,
130// satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
131// contains instructions that should go before the marker, and AfterSet contains
132// ones that should go after the marker. In this function, AfterSet is only
133// used for sanity checking.
134static MachineBasicBlock::iterator
Heejin Ahn18c56a02019-02-04 19:13:39 +0000135getEarliestInsertPos(MachineBasicBlock *MBB,
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000136 const SmallPtrSet<const MachineInstr *, 4> &BeforeSet,
137 const SmallPtrSet<const MachineInstr *, 4> &AfterSet) {
138 auto InsertPos = MBB->end();
139 while (InsertPos != MBB->begin()) {
140 if (BeforeSet.count(&*std::prev(InsertPos))) {
141#ifndef NDEBUG
142 // Sanity check
143 for (auto Pos = InsertPos, E = MBB->begin(); Pos != E; --Pos)
144 assert(!AfterSet.count(&*std::prev(Pos)));
145#endif
146 break;
147 }
148 --InsertPos;
149 }
150 return InsertPos;
151}
152
153// Returns an iterator to the latest position possible within the MBB,
154// satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
155// contains instructions that should go before the marker, and AfterSet contains
156// ones that should go after the marker. In this function, BeforeSet is only
157// used for sanity checking.
158static MachineBasicBlock::iterator
Heejin Ahn18c56a02019-02-04 19:13:39 +0000159getLatestInsertPos(MachineBasicBlock *MBB,
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000160 const SmallPtrSet<const MachineInstr *, 4> &BeforeSet,
161 const SmallPtrSet<const MachineInstr *, 4> &AfterSet) {
162 auto InsertPos = MBB->begin();
163 while (InsertPos != MBB->end()) {
164 if (AfterSet.count(&*InsertPos)) {
165#ifndef NDEBUG
166 // Sanity check
167 for (auto Pos = InsertPos, E = MBB->end(); Pos != E; ++Pos)
168 assert(!BeforeSet.count(&*Pos));
169#endif
170 break;
171 }
172 ++InsertPos;
173 }
174 return InsertPos;
175}
176
177void WebAssemblyCFGStackify::registerScope(MachineInstr *Begin,
178 MachineInstr *End) {
179 BeginToEnd[Begin] = End;
180 EndToBegin[End] = Begin;
181}
182
183void WebAssemblyCFGStackify::registerTryScope(MachineInstr *Begin,
184 MachineInstr *End,
185 MachineBasicBlock *EHPad) {
186 registerScope(Begin, End);
187 TryToEHPad[Begin] = EHPad;
188 EHPadToTry[EHPad] = Begin;
189}
190
Heejin Ahncf699b42019-02-27 00:50:53 +0000191void WebAssemblyCFGStackify::unregisterScope(MachineInstr *Begin) {
192 assert(BeginToEnd.count(Begin));
193 MachineInstr *End = BeginToEnd[Begin];
194 assert(EndToBegin.count(End));
195 BeginToEnd.erase(Begin);
196 EndToBegin.erase(End);
197 MachineBasicBlock *EHPad = TryToEHPad.lookup(Begin);
198 if (EHPad) {
199 assert(EHPadToTry.count(EHPad));
200 TryToEHPad.erase(Begin);
201 EHPadToTry.erase(EHPad);
202 }
203}
204
Dan Gohman32807932015-11-23 16:19:56 +0000205/// Insert a BLOCK marker for branches to MBB (if needed).
Heejin Ahnc4ac74f2019-03-30 11:04:48 +0000206// TODO Consider a more generalized way of handling block (and also loop and
207// try) signatures when we implement the multi-value proposal later.
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000208void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) {
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000209 assert(!MBB.isEHPad());
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000210 MachineFunction &MF = *MBB.getParent();
211 auto &MDT = getAnalysis<MachineDominatorTree>();
212 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
213 const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
214
Dan Gohman8fe7e862015-12-14 22:51:54 +0000215 // First compute the nearest common dominator of all forward non-fallthrough
216 // predecessors so that we minimize the time that the BLOCK is on the stack,
217 // which reduces overall stack height.
Dan Gohman32807932015-11-23 16:19:56 +0000218 MachineBasicBlock *Header = nullptr;
219 bool IsBranchedTo = false;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000220 bool IsBrOnExn = false;
221 MachineInstr *BrOnExn = nullptr;
Dan Gohman32807932015-11-23 16:19:56 +0000222 int MBBNumber = MBB.getNumber();
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000223 for (MachineBasicBlock *Pred : MBB.predecessors()) {
Dan Gohman32807932015-11-23 16:19:56 +0000224 if (Pred->getNumber() < MBBNumber) {
225 Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000226 if (explicitlyBranchesTo(Pred, &MBB)) {
Dan Gohman32807932015-11-23 16:19:56 +0000227 IsBranchedTo = true;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000228 if (Pred->getFirstTerminator()->getOpcode() == WebAssembly::BR_ON_EXN) {
229 IsBrOnExn = true;
230 assert(!BrOnExn && "There should be only one br_on_exn per block");
231 BrOnExn = &*Pred->getFirstTerminator();
232 }
233 }
Dan Gohman32807932015-11-23 16:19:56 +0000234 }
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000235 }
Dan Gohman32807932015-11-23 16:19:56 +0000236 if (!Header)
237 return;
238 if (!IsBranchedTo)
Dan Gohman950a13c2015-09-16 16:51:30 +0000239 return;
240
Dan Gohman8fe7e862015-12-14 22:51:54 +0000241 assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors");
Heejin Ahn5c644c92019-03-05 21:05:09 +0000242 MachineBasicBlock *LayoutPred = MBB.getPrevNode();
Dan Gohman8fe7e862015-12-14 22:51:54 +0000243
244 // If the nearest common dominator is inside a more deeply nested context,
245 // walk out to the nearest scope which isn't more deeply nested.
246 for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
247 if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
248 if (ScopeTop->getNumber() > Header->getNumber()) {
249 // Skip over an intervening scope.
Heejin Ahn5c644c92019-03-05 21:05:09 +0000250 I = std::next(ScopeTop->getIterator());
Dan Gohman8fe7e862015-12-14 22:51:54 +0000251 } else {
252 // We found a scope level at an appropriate depth.
253 Header = ScopeTop;
254 break;
255 }
256 }
257 }
258
Dan Gohman8fe7e862015-12-14 22:51:54 +0000259 // Decide where in Header to put the BLOCK.
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000260
261 // Instructions that should go before the BLOCK.
262 SmallPtrSet<const MachineInstr *, 4> BeforeSet;
263 // Instructions that should go after the BLOCK.
264 SmallPtrSet<const MachineInstr *, 4> AfterSet;
265 for (const auto &MI : *Header) {
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000266 // If there is a previously placed LOOP marker and the bottom block of the
267 // loop is above MBB, it should be after the BLOCK, because the loop is
268 // nested in this BLOCK. Otherwise it should be before the BLOCK.
269 if (MI.getOpcode() == WebAssembly::LOOP) {
270 auto *LoopBottom = BeginToEnd[&MI]->getParent()->getPrevNode();
271 if (MBB.getNumber() > LoopBottom->getNumber())
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000272 AfterSet.insert(&MI);
273#ifndef NDEBUG
274 else
275 BeforeSet.insert(&MI);
276#endif
277 }
278
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000279 // All previously inserted BLOCK/TRY markers should be after the BLOCK
280 // because they are all nested blocks.
281 if (MI.getOpcode() == WebAssembly::BLOCK ||
282 MI.getOpcode() == WebAssembly::TRY)
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000283 AfterSet.insert(&MI);
284
285#ifndef NDEBUG
286 // All END_(BLOCK|LOOP|TRY) markers should be before the BLOCK.
287 if (MI.getOpcode() == WebAssembly::END_BLOCK ||
288 MI.getOpcode() == WebAssembly::END_LOOP ||
289 MI.getOpcode() == WebAssembly::END_TRY)
290 BeforeSet.insert(&MI);
291#endif
292
293 // Terminators should go after the BLOCK.
294 if (MI.isTerminator())
295 AfterSet.insert(&MI);
296 }
297
298 // Local expression tree should go after the BLOCK.
299 for (auto I = Header->getFirstTerminator(), E = Header->begin(); I != E;
300 --I) {
Yury Delendik409b4392018-10-04 23:31:00 +0000301 if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
302 continue;
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000303 if (WebAssembly::isChild(*std::prev(I), MFI))
304 AfterSet.insert(&*std::prev(I));
305 else
306 break;
Dan Gohman950a13c2015-09-16 16:51:30 +0000307 }
308
Dan Gohman8fe7e862015-12-14 22:51:54 +0000309 // Add the BLOCK.
Heejin Ahnd6f48782019-01-30 03:21:57 +0000310
311 // 'br_on_exn' extracts except_ref object and pushes variable number of values
312 // depending on its tag. For C++ exception, its a single i32 value, and the
313 // generated code will be in the form of:
314 // block i32
315 // br_on_exn 0, $__cpp_exception
316 // rethrow
317 // end_block
318 WebAssembly::ExprType ReturnType = WebAssembly::ExprType::Void;
319 if (IsBrOnExn) {
320 const char *TagName = BrOnExn->getOperand(1).getSymbolName();
321 if (std::strcmp(TagName, "__cpp_exception") != 0)
322 llvm_unreachable("Only C++ exception is supported");
323 ReturnType = WebAssembly::ExprType::I32;
324 }
325
Heejin Ahn18c56a02019-02-04 19:13:39 +0000326 auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet);
Heejin Ahn92401cc2018-04-14 00:12:12 +0000327 MachineInstr *Begin =
328 BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
329 TII.get(WebAssembly::BLOCK))
Heejin Ahnd6f48782019-01-30 03:21:57 +0000330 .addImm(int64_t(ReturnType));
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000331
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000332 // Decide where in Header to put the END_BLOCK.
333 BeforeSet.clear();
334 AfterSet.clear();
335 for (auto &MI : MBB) {
336#ifndef NDEBUG
337 // END_BLOCK should precede existing LOOP and TRY markers.
338 if (MI.getOpcode() == WebAssembly::LOOP ||
339 MI.getOpcode() == WebAssembly::TRY)
340 AfterSet.insert(&MI);
341#endif
342
343 // If there is a previously placed END_LOOP marker and the header of the
344 // loop is above this block's header, the END_LOOP should be placed after
345 // the BLOCK, because the loop contains this block. Otherwise the END_LOOP
346 // should be placed before the BLOCK. The same for END_TRY.
347 if (MI.getOpcode() == WebAssembly::END_LOOP ||
348 MI.getOpcode() == WebAssembly::END_TRY) {
349 if (EndToBegin[&MI]->getParent()->getNumber() >= Header->getNumber())
350 BeforeSet.insert(&MI);
351#ifndef NDEBUG
352 else
353 AfterSet.insert(&MI);
354#endif
355 }
356 }
357
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000358 // Mark the end of the block.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000359 InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet);
Derek Schuff10b31352018-03-15 22:06:51 +0000360 MachineInstr *End = BuildMI(MBB, InsertPos, MBB.findPrevDebugLoc(InsertPos),
Dan Gohman2726b882016-10-06 22:29:32 +0000361 TII.get(WebAssembly::END_BLOCK));
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000362 registerScope(Begin, End);
Dan Gohman8fe7e862015-12-14 22:51:54 +0000363
364 // Track the farthest-spanning scope that ends at this point.
365 int Number = MBB.getNumber();
366 if (!ScopeTops[Number] ||
367 ScopeTops[Number]->getNumber() > Header->getNumber())
368 ScopeTops[Number] = Header;
369}
370
371/// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000372void WebAssemblyCFGStackify::placeLoopMarker(MachineBasicBlock &MBB) {
373 MachineFunction &MF = *MBB.getParent();
374 const auto &MLI = getAnalysis<MachineLoopInfo>();
375 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
376
Dan Gohman8fe7e862015-12-14 22:51:54 +0000377 MachineLoop *Loop = MLI.getLoopFor(&MBB);
378 if (!Loop || Loop->getHeader() != &MBB)
379 return;
380
381 // The operand of a LOOP is the first block after the loop. If the loop is the
382 // bottom of the function, insert a dummy block at the end.
Heejin Ahn817811ca2018-06-19 00:32:03 +0000383 MachineBasicBlock *Bottom = WebAssembly::getBottom(Loop);
Heejin Ahn5c644c92019-03-05 21:05:09 +0000384 auto Iter = std::next(Bottom->getIterator());
Dan Gohman8fe7e862015-12-14 22:51:54 +0000385 if (Iter == MF.end()) {
Heejin Ahnc4ac74f2019-03-30 11:04:48 +0000386 getAppendixBlock(MF);
Heejin Ahn5c644c92019-03-05 21:05:09 +0000387 Iter = std::next(Bottom->getIterator());
Dan Gohman8fe7e862015-12-14 22:51:54 +0000388 }
389 MachineBasicBlock *AfterLoop = &*Iter;
Dan Gohman8fe7e862015-12-14 22:51:54 +0000390
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000391 // Decide where in Header to put the LOOP.
392 SmallPtrSet<const MachineInstr *, 4> BeforeSet;
393 SmallPtrSet<const MachineInstr *, 4> AfterSet;
394 for (const auto &MI : MBB) {
395 // LOOP marker should be after any existing loop that ends here. Otherwise
396 // we assume the instruction belongs to the loop.
397 if (MI.getOpcode() == WebAssembly::END_LOOP)
398 BeforeSet.insert(&MI);
399#ifndef NDEBUG
400 else
401 AfterSet.insert(&MI);
402#endif
403 }
404
405 // Mark the beginning of the loop.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000406 auto InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet);
Derek Schuff10b31352018-03-15 22:06:51 +0000407 MachineInstr *Begin = BuildMI(MBB, InsertPos, MBB.findDebugLoc(InsertPos),
Dan Gohman2726b882016-10-06 22:29:32 +0000408 TII.get(WebAssembly::LOOP))
Derek Schuff10b31352018-03-15 22:06:51 +0000409 .addImm(int64_t(WebAssembly::ExprType::Void));
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000410
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000411 // Decide where in Header to put the END_LOOP.
412 BeforeSet.clear();
413 AfterSet.clear();
414#ifndef NDEBUG
415 for (const auto &MI : MBB)
416 // Existing END_LOOP markers belong to parent loops of this loop
417 if (MI.getOpcode() == WebAssembly::END_LOOP)
418 AfterSet.insert(&MI);
419#endif
420
421 // Mark the end of the loop (using arbitrary debug location that branched to
422 // the loop end as its location).
Heejin Ahn18c56a02019-02-04 19:13:39 +0000423 InsertPos = getEarliestInsertPos(AfterLoop, BeforeSet, AfterSet);
Heejin Ahn67f74ac2019-03-29 19:36:51 +0000424 DebugLoc EndDL = AfterLoop->pred_empty()
425 ? DebugLoc()
426 : (*AfterLoop->pred_rbegin())->findBranchDebugLoc();
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000427 MachineInstr *End =
428 BuildMI(*AfterLoop, InsertPos, EndDL, TII.get(WebAssembly::END_LOOP));
429 registerScope(Begin, End);
Dan Gohman8fe7e862015-12-14 22:51:54 +0000430
431 assert((!ScopeTops[AfterLoop->getNumber()] ||
432 ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) &&
Dan Gohman442bfce2016-02-16 16:22:41 +0000433 "With block sorting the outermost loop for a block should be first.");
Dan Gohman8fe7e862015-12-14 22:51:54 +0000434 if (!ScopeTops[AfterLoop->getNumber()])
435 ScopeTops[AfterLoop->getNumber()] = &MBB;
Dan Gohman950a13c2015-09-16 16:51:30 +0000436}
437
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000438void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000439 assert(MBB.isEHPad());
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000440 MachineFunction &MF = *MBB.getParent();
441 auto &MDT = getAnalysis<MachineDominatorTree>();
442 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
443 const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
444 const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
445
446 // Compute the nearest common dominator of all unwind predecessors
447 MachineBasicBlock *Header = nullptr;
448 int MBBNumber = MBB.getNumber();
449 for (auto *Pred : MBB.predecessors()) {
450 if (Pred->getNumber() < MBBNumber) {
451 Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000452 assert(!explicitlyBranchesTo(Pred, &MBB) &&
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000453 "Explicit branch to an EH pad!");
454 }
455 }
456 if (!Header)
457 return;
458
459 // If this try is at the bottom of the function, insert a dummy block at the
460 // end.
461 WebAssemblyException *WE = WEI.getExceptionFor(&MBB);
462 assert(WE);
463 MachineBasicBlock *Bottom = WebAssembly::getBottom(WE);
464
Heejin Ahn5c644c92019-03-05 21:05:09 +0000465 auto Iter = std::next(Bottom->getIterator());
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000466 if (Iter == MF.end()) {
Heejin Ahnc4ac74f2019-03-30 11:04:48 +0000467 getAppendixBlock(MF);
Heejin Ahn5c644c92019-03-05 21:05:09 +0000468 Iter = std::next(Bottom->getIterator());
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000469 }
Heejin Ahn20cf0742019-02-24 08:30:06 +0000470 MachineBasicBlock *Cont = &*Iter;
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000471
Heejin Ahn20cf0742019-02-24 08:30:06 +0000472 assert(Cont != &MF.front());
Heejin Ahn5c644c92019-03-05 21:05:09 +0000473 MachineBasicBlock *LayoutPred = Cont->getPrevNode();
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000474
475 // If the nearest common dominator is inside a more deeply nested context,
476 // walk out to the nearest scope which isn't more deeply nested.
477 for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
478 if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
479 if (ScopeTop->getNumber() > Header->getNumber()) {
480 // Skip over an intervening scope.
Heejin Ahn5c644c92019-03-05 21:05:09 +0000481 I = std::next(ScopeTop->getIterator());
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000482 } else {
483 // We found a scope level at an appropriate depth.
484 Header = ScopeTop;
485 break;
486 }
487 }
488 }
489
490 // Decide where in Header to put the TRY.
491
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000492 // Instructions that should go before the TRY.
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000493 SmallPtrSet<const MachineInstr *, 4> BeforeSet;
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000494 // Instructions that should go after the TRY.
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000495 SmallPtrSet<const MachineInstr *, 4> AfterSet;
496 for (const auto &MI : *Header) {
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000497 // If there is a previously placed LOOP marker and the bottom block of the
498 // loop is above MBB, it should be after the TRY, because the loop is nested
499 // in this TRY. Otherwise it should be before the TRY.
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000500 if (MI.getOpcode() == WebAssembly::LOOP) {
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000501 auto *LoopBottom = BeginToEnd[&MI]->getParent()->getPrevNode();
502 if (MBB.getNumber() > LoopBottom->getNumber())
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000503 AfterSet.insert(&MI);
504#ifndef NDEBUG
505 else
506 BeforeSet.insert(&MI);
507#endif
508 }
509
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000510 // All previously inserted BLOCK/TRY markers should be after the TRY because
511 // they are all nested trys.
512 if (MI.getOpcode() == WebAssembly::BLOCK ||
513 MI.getOpcode() == WebAssembly::TRY)
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000514 AfterSet.insert(&MI);
515
516#ifndef NDEBUG
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000517 // All END_(BLOCK/LOOP/TRY) markers should be before the TRY.
518 if (MI.getOpcode() == WebAssembly::END_BLOCK ||
519 MI.getOpcode() == WebAssembly::END_LOOP ||
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000520 MI.getOpcode() == WebAssembly::END_TRY)
521 BeforeSet.insert(&MI);
522#endif
523
524 // Terminators should go after the TRY.
525 if (MI.isTerminator())
526 AfterSet.insert(&MI);
527 }
528
529 // Local expression tree should go after the TRY.
530 for (auto I = Header->getFirstTerminator(), E = Header->begin(); I != E;
531 --I) {
Yury Delendik409b4392018-10-04 23:31:00 +0000532 if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
533 continue;
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000534 if (WebAssembly::isChild(*std::prev(I), MFI))
535 AfterSet.insert(&*std::prev(I));
536 else
537 break;
538 }
539
540 // If Header unwinds to MBB (= Header contains 'invoke'), the try block should
541 // contain the call within it. So the call should go after the TRY. The
542 // exception is when the header's terminator is a rethrow instruction, in
543 // which case that instruction, not a call instruction before it, is gonna
544 // throw.
545 if (MBB.isPredecessor(Header)) {
546 auto TermPos = Header->getFirstTerminator();
Heejin Ahnd6f48782019-01-30 03:21:57 +0000547 if (TermPos == Header->end() ||
548 TermPos->getOpcode() != WebAssembly::RETHROW) {
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000549 for (const auto &MI : reverse(*Header)) {
550 if (MI.isCall()) {
551 AfterSet.insert(&MI);
Heejin Ahn8b49b6b2019-03-13 00:37:31 +0000552 // Possibly throwing calls are usually wrapped by EH_LABEL
553 // instructions. We don't want to split them and the call.
554 if (MI.getIterator() != Header->begin() &&
555 std::prev(MI.getIterator())->isEHLabel())
556 AfterSet.insert(&*std::prev(MI.getIterator()));
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000557 break;
558 }
559 }
560 }
561 }
562
563 // Add the TRY.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000564 auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet);
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000565 MachineInstr *Begin =
566 BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
567 TII.get(WebAssembly::TRY))
568 .addImm(int64_t(WebAssembly::ExprType::Void));
569
570 // Decide where in Header to put the END_TRY.
571 BeforeSet.clear();
572 AfterSet.clear();
Heejin Ahn20cf0742019-02-24 08:30:06 +0000573 for (const auto &MI : *Cont) {
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000574#ifndef NDEBUG
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000575 // END_TRY should precede existing LOOP and BLOCK markers.
576 if (MI.getOpcode() == WebAssembly::LOOP ||
577 MI.getOpcode() == WebAssembly::BLOCK)
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000578 AfterSet.insert(&MI);
579
580 // All END_TRY markers placed earlier belong to exceptions that contains
581 // this one.
582 if (MI.getOpcode() == WebAssembly::END_TRY)
583 AfterSet.insert(&MI);
584#endif
585
586 // If there is a previously placed END_LOOP marker and its header is after
587 // where TRY marker is, this loop is contained within the 'catch' part, so
588 // the END_TRY marker should go after that. Otherwise, the whole try-catch
589 // is contained within this loop, so the END_TRY should go before that.
590 if (MI.getOpcode() == WebAssembly::END_LOOP) {
Heejin Ahn222718f2019-03-26 17:29:55 +0000591 // For a LOOP to be after TRY, LOOP's BB should be after TRY's BB; if they
592 // are in the same BB, LOOP is always before TRY.
593 if (EndToBegin[&MI]->getParent()->getNumber() > Header->getNumber())
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000594 BeforeSet.insert(&MI);
595#ifndef NDEBUG
596 else
597 AfterSet.insert(&MI);
598#endif
599 }
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000600
601 // It is not possible for an END_BLOCK to be already in this block.
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000602 }
603
604 // Mark the end of the TRY.
Heejin Ahn20cf0742019-02-24 08:30:06 +0000605 InsertPos = getEarliestInsertPos(Cont, BeforeSet, AfterSet);
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000606 MachineInstr *End =
Heejin Ahn20cf0742019-02-24 08:30:06 +0000607 BuildMI(*Cont, InsertPos, Bottom->findBranchDebugLoc(),
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000608 TII.get(WebAssembly::END_TRY));
609 registerTryScope(Begin, End, &MBB);
610
Heejin Ahn82da1ff2019-02-27 01:35:14 +0000611 // Track the farthest-spanning scope that ends at this point. We create two
612 // mappings: (BB with 'end_try' -> BB with 'try') and (BB with 'catch' -> BB
613 // with 'try'). We need to create 'catch' -> 'try' mapping here too because
614 // markers should not span across 'catch'. For example, this should not
615 // happen:
616 //
617 // try
618 // block --| (X)
619 // catch |
620 // end_block --|
621 // end_try
622 for (int Number : {Cont->getNumber(), MBB.getNumber()}) {
623 if (!ScopeTops[Number] ||
624 ScopeTops[Number]->getNumber() > Header->getNumber())
625 ScopeTops[Number] = Header;
626 }
Heejin Ahne76fa9e2018-08-16 23:50:59 +0000627}
628
Heejin Ahncf699b42019-02-27 00:50:53 +0000629void WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) {
630 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
631
632 // When there is an unconditional branch right before a catch instruction and
633 // it branches to the end of end_try marker, we don't need the branch, because
634 // it there is no exception, the control flow transfers to that point anyway.
635 // bb0:
636 // try
637 // ...
638 // br bb2 <- Not necessary
639 // bb1:
640 // catch
641 // ...
642 // bb2:
643 // end
644 for (auto &MBB : MF) {
645 if (!MBB.isEHPad())
646 continue;
647
648 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
649 SmallVector<MachineOperand, 4> Cond;
Heejin Ahn5c644c92019-03-05 21:05:09 +0000650 MachineBasicBlock *EHPadLayoutPred = MBB.getPrevNode();
Heejin Ahncf699b42019-02-27 00:50:53 +0000651 MachineBasicBlock *Cont = BeginToEnd[EHPadToTry[&MBB]]->getParent();
652 bool Analyzable = !TII.analyzeBranch(*EHPadLayoutPred, TBB, FBB, Cond);
653 if (Analyzable && ((Cond.empty() && TBB && TBB == Cont) ||
654 (!Cond.empty() && FBB && FBB == Cont)))
655 TII.removeBranch(*EHPadLayoutPred);
656 }
657
658 // When there are block / end_block markers that overlap with try / end_try
659 // markers, and the block and try markers' return types are the same, the
660 // block /end_block markers are not necessary, because try / end_try markers
661 // also can serve as boundaries for branches.
662 // block <- Not necessary
663 // try
664 // ...
665 // catch
666 // ...
667 // end
668 // end <- Not necessary
669 SmallVector<MachineInstr *, 32> ToDelete;
670 for (auto &MBB : MF) {
671 for (auto &MI : MBB) {
672 if (MI.getOpcode() != WebAssembly::TRY)
673 continue;
674
675 MachineInstr *Try = &MI, *EndTry = BeginToEnd[Try];
676 MachineBasicBlock *TryBB = Try->getParent();
677 MachineBasicBlock *Cont = EndTry->getParent();
678 int64_t RetType = Try->getOperand(0).getImm();
Heejin Ahn5c644c92019-03-05 21:05:09 +0000679 for (auto B = Try->getIterator(), E = std::next(EndTry->getIterator());
Heejin Ahncf699b42019-02-27 00:50:53 +0000680 B != TryBB->begin() && E != Cont->end() &&
681 std::prev(B)->getOpcode() == WebAssembly::BLOCK &&
682 E->getOpcode() == WebAssembly::END_BLOCK &&
683 std::prev(B)->getOperand(0).getImm() == RetType;
684 --B, ++E) {
685 ToDelete.push_back(&*std::prev(B));
686 ToDelete.push_back(&*E);
687 }
688 }
689 }
690 for (auto *MI : ToDelete) {
691 if (MI->getOpcode() == WebAssembly::BLOCK)
692 unregisterScope(MI);
693 MI->eraseFromParent();
694 }
695}
696
Heejin Ahnc4ac74f2019-03-30 11:04:48 +0000697bool WebAssemblyCFGStackify::fixUnwindMismatches(MachineFunction &MF) {
698 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
699 MachineRegisterInfo &MRI = MF.getRegInfo();
700
701 // Linearizing the control flow by placing TRY / END_TRY markers can create
702 // mismatches in unwind destinations. There are two kinds of mismatches we
703 // try to solve here.
704
705 // 1. When an instruction may throw, but the EH pad it will unwind to can be
706 // different from the original CFG.
707 //
708 // Example: we have the following CFG:
709 // bb0:
710 // call @foo (if it throws, unwind to bb2)
711 // bb1:
712 // call @bar (if it throws, unwind to bb3)
713 // bb2 (ehpad):
714 // catch
715 // ...
716 // bb3 (ehpad)
717 // catch
718 // handler body
719 //
720 // And the CFG is sorted in this order. Then after placing TRY markers, it
721 // will look like: (BB markers are omitted)
722 // try $label1
723 // try
724 // call @foo
725 // call @bar (if it throws, unwind to bb3)
726 // catch <- ehpad (bb2)
727 // ...
728 // end_try
729 // catch <- ehpad (bb3)
730 // handler body
731 // end_try
732 //
733 // Now if bar() throws, it is going to end up ip in bb2, not bb3, where it
734 // is supposed to end up. We solve this problem by
735 // a. Split the target unwind EH pad (here bb3) so that the handler body is
736 // right after 'end_try', which means we extract the handler body out of
737 // the catch block. We do this because this handler body should be
738 // somewhere branch-eable from the inner scope.
739 // b. Wrap the call that has an incorrect unwind destination ('call @bar'
740 // here) with a nested try/catch/end_try scope, and within the new catch
741 // block, branches to the handler body.
742 // c. Place a branch after the newly inserted nested end_try so it can bypass
743 // the handler body, which is now outside of a catch block.
744 //
745 // The result will like as follows. (new: a) means this instruction is newly
746 // created in the process of doing 'a' above.
747 //
748 // block $label0 (new: placeBlockMarker)
749 // try $label1
750 // try
751 // call @foo
752 // try (new: b)
753 // call @bar
754 // catch (new: b)
755 // local.set n / drop (new: b)
756 // br $label1 (new: b)
757 // end_try (new: b)
758 // catch <- ehpad (bb2)
759 // end_try
760 // br $label0 (new: c)
761 // catch <- ehpad (bb3)
762 // end_try (hoisted: a)
763 // handler body
764 // end_block (new: placeBlockMarker)
765 //
766 // Note that the new wrapping block/end_block will be generated later in
767 // placeBlockMarker.
768 //
769 // TODO Currently local.set and local.gets are generated to move except_ref
770 // value created by catches. That's because we don't support yielding values
771 // from a block in LLVM machine IR yet, even though it is supported by wasm.
772 // Delete unnecessary local.get/local.sets once yielding values from a block
773 // is supported. The full EH spec requires multi-value support to do this, but
774 // for C++ we don't yet need it because we only throw a single i32.
775 //
776 // ---
777 // 2. The same as 1, but in this case an instruction unwinds to a caller
778 // function and not another EH pad.
779 //
780 // Example: we have the following CFG:
781 // bb0:
782 // call @foo (if it throws, unwind to bb2)
783 // bb1:
784 // call @bar (if it throws, unwind to caller)
785 // bb2 (ehpad):
786 // catch
787 // ...
788 //
789 // And the CFG is sorted in this order. Then after placing TRY markers, it
790 // will look like:
791 // try
792 // call @foo
793 // call @bar (if it throws, unwind to caller)
794 // catch <- ehpad (bb2)
795 // ...
796 // end_try
797 //
798 // Now if bar() throws, it is going to end up ip in bb2, when it is supposed
799 // throw up to the caller.
800 // We solve this problem by
801 // a. Create a new 'appendix' BB at the end of the function and put a single
802 // 'rethrow' instruction (+ local.get) in there.
803 // b. Wrap the call that has an incorrect unwind destination ('call @bar'
804 // here) with a nested try/catch/end_try scope, and within the new catch
805 // block, branches to the new appendix block.
806 //
807 // block $label0 (new: placeBlockMarker)
808 // try
809 // call @foo
810 // try (new: b)
811 // call @bar
812 // catch (new: b)
813 // local.set n (new: b)
814 // br $label0 (new: b)
815 // end_try (new: b)
816 // catch <- ehpad (bb2)
817 // ...
818 // end_try
819 // ...
820 // end_block (new: placeBlockMarker)
821 // local.get n (new: a) <- appendix block
822 // rethrow (new: a)
823 //
824 // In case there are multiple calls in a BB that may throw to the caller, they
825 // can be wrapped together in one nested try scope. (In 1, this couldn't
826 // happen, because may-throwing instruction there had an unwind destination,
827 // i.e., it was an invoke before, and there could be only one invoke within a
828 // BB.)
829
830 SmallVector<const MachineBasicBlock *, 8> EHPadStack;
831 // Range of intructions to be wrapped in a new nested try/catch
832 using TryRange = std::pair<MachineInstr *, MachineInstr *>;
833 // In original CFG, <unwind destionation BB, a vector of try ranges>
834 DenseMap<MachineBasicBlock *, SmallVector<TryRange, 4>> UnwindDestToTryRanges;
835 // In new CFG, <destination to branch to, a vector of try ranges>
836 DenseMap<MachineBasicBlock *, SmallVector<TryRange, 4>> BrDestToTryRanges;
837 // In new CFG, <destination to branch to, register containing except_ref>
838 DenseMap<MachineBasicBlock *, unsigned> BrDestToExnReg;
839
840 // Gather possibly throwing calls (i.e., previously invokes) whose current
841 // unwind destination is not the same as the original CFG.
842 for (auto &MBB : reverse(MF)) {
843 bool SeenThrowableInstInBB = false;
844 for (auto &MI : reverse(MBB)) {
845 if (MI.getOpcode() == WebAssembly::TRY)
846 EHPadStack.pop_back();
847 else if (MI.getOpcode() == WebAssembly::CATCH)
848 EHPadStack.push_back(MI.getParent());
849
850 // In this loop we only gather calls that have an EH pad to unwind. So
851 // there will be at most 1 such call (= invoke) in a BB, so after we've
852 // seen one, we can skip the rest of BB. Also if MBB has no EH pad
853 // successor or MI does not throw, this is not an invoke.
854 if (SeenThrowableInstInBB || !MBB.hasEHPadSuccessor() ||
855 !WebAssembly::mayThrow(MI))
856 continue;
857 SeenThrowableInstInBB = true;
858
859 // If the EH pad on the stack top is where this instruction should unwind
860 // next, we're good.
861 MachineBasicBlock *UnwindDest = nullptr;
862 for (auto *Succ : MBB.successors()) {
863 if (Succ->isEHPad()) {
864 UnwindDest = Succ;
865 break;
866 }
867 }
868 if (EHPadStack.back() == UnwindDest)
869 continue;
870
871 // If not, record the range.
872 UnwindDestToTryRanges[UnwindDest].push_back(TryRange(&MI, &MI));
873 }
874 }
875
876 assert(EHPadStack.empty());
877
878 // Gather possibly throwing calls that are supposed to unwind up to the caller
879 // if they throw, but currently unwind to an incorrect destination. Unlike the
880 // loop above, there can be multiple calls within a BB that unwind to the
881 // caller, which we should group together in a range.
882 bool NeedAppendixBlock = false;
883 for (auto &MBB : reverse(MF)) {
884 MachineInstr *RangeBegin = nullptr, *RangeEnd = nullptr; // inclusive
885 for (auto &MI : reverse(MBB)) {
886 if (MI.getOpcode() == WebAssembly::TRY)
887 EHPadStack.pop_back();
888 else if (MI.getOpcode() == WebAssembly::CATCH)
889 EHPadStack.push_back(MI.getParent());
890
891 // If MBB has an EH pad successor, this inst does not unwind to caller.
892 if (MBB.hasEHPadSuccessor())
893 continue;
894
895 // We wrap up the current range when we see a marker even if we haven't
896 // finished a BB.
897 if (RangeEnd && WebAssembly::isMarker(MI)) {
898 NeedAppendixBlock = true;
899 // Record the range. nullptr here means the unwind destination is the
900 // caller.
901 UnwindDestToTryRanges[nullptr].push_back(
902 TryRange(RangeBegin, RangeEnd));
903 RangeBegin = RangeEnd = nullptr; // Reset range pointers
904 }
905
906 // If EHPadStack is empty, that means it is correctly unwind to caller if
907 // it throws, so we're good. If MI does not throw, we're good too.
908 if (EHPadStack.empty() || !WebAssembly::mayThrow(MI))
909 continue;
910
911 // We found an instruction that unwinds to the caller but currently has an
912 // incorrect unwind destination. Create a new range or increment the
913 // currently existing range.
914 if (!RangeEnd)
915 RangeBegin = RangeEnd = &MI;
916 else
917 RangeBegin = &MI;
918 }
919
920 if (RangeEnd) {
921 NeedAppendixBlock = true;
922 // Record the range. nullptr here means the unwind destination is the
923 // caller.
924 UnwindDestToTryRanges[nullptr].push_back(TryRange(RangeBegin, RangeEnd));
925 RangeBegin = RangeEnd = nullptr; // Reset range pointers
926 }
927 }
928
929 assert(EHPadStack.empty());
930 // We don't have any unwind destination mismatches to resolve.
931 if (UnwindDestToTryRanges.empty())
932 return false;
933
934 // If we found instructions that should unwind to the caller but currently
935 // have incorrect unwind destination, we create an appendix block at the end
936 // of the function with a local.get and a rethrow instruction.
937 if (NeedAppendixBlock) {
938 auto *AppendixBB = getAppendixBlock(MF);
939 unsigned ExnReg =
940 MRI.createVirtualRegister(&WebAssembly::EXCEPT_REFRegClass);
941 BuildMI(AppendixBB, DebugLoc(), TII.get(WebAssembly::RETHROW))
942 .addReg(ExnReg);
943 // These instruction ranges should branch to this appendix BB.
944 for (auto Range : UnwindDestToTryRanges[nullptr])
945 BrDestToTryRanges[AppendixBB].push_back(Range);
946 BrDestToExnReg[AppendixBB] = ExnReg;
947 }
948
949 // We loop through unwind destination EH pads that are targeted from some
950 // inner scopes. Because these EH pads are destination of more than one scope
951 // now, we split them so that the handler body is after 'end_try'.
952 // - Before
953 // ehpad:
954 // catch
955 // local.set n / drop
956 // handler body
957 // ...
958 // cont:
959 // end_try
960 //
961 // - After
962 // ehpad:
963 // catch
964 // local.set n / drop
965 // brdest: (new)
966 // end_try (hoisted from 'cont' BB)
967 // handler body (taken from 'ehpad')
968 // ...
969 // cont:
970 for (auto &P : UnwindDestToTryRanges) {
971 NumUnwindMismatches++;
972
973 // This means the destination is the appendix BB, which was separately
974 // handled above.
975 if (!P.first)
976 continue;
977
978 MachineBasicBlock *EHPad = P.first;
979
980 // Find 'catch' and 'local.set' or 'drop' instruction that follows the
981 // 'catch'. If -wasm-disable-explicit-locals is not set, 'catch' should be
982 // always followed by either 'local.set' or a 'drop', because 'br_on_exn' is
983 // generated after 'catch' in LateEHPrepare and we don't support blocks
984 // taking values yet.
985 MachineInstr *Catch = nullptr;
986 unsigned ExnReg = 0;
987 for (auto &MI : *EHPad) {
988 switch (MI.getOpcode()) {
989 case WebAssembly::CATCH:
990 Catch = &MI;
991 ExnReg = Catch->getOperand(0).getReg();
992 break;
993 }
994 }
995 assert(Catch && "EH pad does not have a catch");
996 assert(ExnReg != 0 && "Invalid register");
997
998 auto SplitPos = std::next(Catch->getIterator());
999
1000 // Create a new BB that's gonna be the destination for branches from the
1001 // inner mismatched scope.
1002 MachineInstr *BeginTry = EHPadToTry[EHPad];
1003 MachineInstr *EndTry = BeginToEnd[BeginTry];
1004 MachineBasicBlock *Cont = EndTry->getParent();
1005 auto *BrDest = MF.CreateMachineBasicBlock();
1006 MF.insert(std::next(EHPad->getIterator()), BrDest);
1007 // Hoist up the existing 'end_try'.
1008 BrDest->insert(BrDest->end(), EndTry->removeFromParent());
1009 // Take out the handler body from EH pad to the new branch destination BB.
1010 BrDest->splice(BrDest->end(), EHPad, SplitPos, EHPad->end());
1011 // Fix predecessor-successor relationship.
1012 BrDest->transferSuccessors(EHPad);
1013 EHPad->addSuccessor(BrDest);
1014
1015 // All try ranges that were supposed to unwind to this EH pad now have to
1016 // branch to this new branch dest BB.
1017 for (auto Range : UnwindDestToTryRanges[EHPad])
1018 BrDestToTryRanges[BrDest].push_back(Range);
1019 BrDestToExnReg[BrDest] = ExnReg;
1020
1021 // In case we fall through to the continuation BB after the catch block, we
1022 // now have to add a branch to it.
1023 // - Before
1024 // try
1025 // ...
1026 // (falls through to 'cont')
1027 // catch
1028 // handler body
1029 // end
1030 // <-- cont
1031 //
1032 // - After
1033 // try
1034 // ...
1035 // br %cont (new)
1036 // catch
1037 // end
1038 // handler body
1039 // <-- cont
1040 MachineBasicBlock *EHPadLayoutPred = &*std::prev(EHPad->getIterator());
1041 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1042 SmallVector<MachineOperand, 4> Cond;
1043 bool Analyzable = !TII.analyzeBranch(*EHPadLayoutPred, TBB, FBB, Cond);
1044 if (Analyzable && !TBB && !FBB) {
1045 DebugLoc DL = EHPadLayoutPred->empty()
1046 ? DebugLoc()
1047 : EHPadLayoutPred->rbegin()->getDebugLoc();
1048 BuildMI(EHPadLayoutPred, DL, TII.get(WebAssembly::BR)).addMBB(Cont);
1049 }
1050 }
1051
1052 // For possibly throwing calls whose unwind destinations are currently
1053 // incorrect because of CFG linearization, we wrap them with a nested
1054 // try/catch/end_try, and within the new catch block, we branch to the correct
1055 // handler.
1056 // - Before
1057 // mbb:
1058 // call @foo <- Unwind destination mismatch!
1059 // ehpad:
1060 // ...
1061 //
1062 // - After
1063 // mbb:
1064 // try (new)
1065 // call @foo
1066 // nested-ehpad: (new)
1067 // catch (new)
1068 // local.set n / drop (new)
1069 // br %brdest (new)
1070 // nested-end: (new)
1071 // end_try (new)
1072 // ehpad:
1073 // ...
1074 for (auto &P : BrDestToTryRanges) {
1075 MachineBasicBlock *BrDest = P.first;
1076 auto &TryRanges = P.second;
1077 unsigned ExnReg = BrDestToExnReg[BrDest];
1078
1079 for (auto Range : TryRanges) {
1080 MachineInstr *RangeBegin = nullptr, *RangeEnd = nullptr;
1081 std::tie(RangeBegin, RangeEnd) = Range;
1082 auto *MBB = RangeBegin->getParent();
1083
1084 // Include possible EH_LABELs in the range
1085 if (RangeBegin->getIterator() != MBB->begin() &&
1086 std::prev(RangeBegin->getIterator())->isEHLabel())
1087 RangeBegin = &*std::prev(RangeBegin->getIterator());
1088 if (std::next(RangeEnd->getIterator()) != MBB->end() &&
1089 std::next(RangeEnd->getIterator())->isEHLabel())
1090 RangeEnd = &*std::next(RangeEnd->getIterator());
1091
1092 MachineBasicBlock *EHPad = nullptr;
1093 for (auto *Succ : MBB->successors()) {
1094 if (Succ->isEHPad()) {
1095 EHPad = Succ;
1096 break;
1097 }
1098 }
1099
1100 // Create the nested try instruction.
1101 MachineInstr *NestedTry =
1102 BuildMI(*MBB, *RangeBegin, RangeBegin->getDebugLoc(),
1103 TII.get(WebAssembly::TRY))
1104 .addImm(int64_t(WebAssembly::ExprType::Void));
1105
1106 // Create the nested EH pad and fill instructions in.
1107 MachineBasicBlock *NestedEHPad = MF.CreateMachineBasicBlock();
1108 MF.insert(std::next(MBB->getIterator()), NestedEHPad);
1109 NestedEHPad->setIsEHPad();
1110 NestedEHPad->setIsEHScopeEntry();
1111 BuildMI(NestedEHPad, RangeEnd->getDebugLoc(), TII.get(WebAssembly::CATCH),
1112 ExnReg);
1113 BuildMI(NestedEHPad, RangeEnd->getDebugLoc(), TII.get(WebAssembly::BR))
1114 .addMBB(BrDest);
1115
1116 // Create the nested continuation BB and end_try instruction.
1117 MachineBasicBlock *NestedCont = MF.CreateMachineBasicBlock();
1118 MF.insert(std::next(NestedEHPad->getIterator()), NestedCont);
1119 MachineInstr *NestedEndTry =
1120 BuildMI(*NestedCont, NestedCont->begin(), RangeEnd->getDebugLoc(),
1121 TII.get(WebAssembly::END_TRY));
1122 // In case MBB has more instructions after the try range, move them to the
1123 // new nested continuation BB.
1124 NestedCont->splice(NestedCont->end(), MBB,
1125 std::next(RangeEnd->getIterator()), MBB->end());
1126 registerTryScope(NestedTry, NestedEndTry, NestedEHPad);
1127
1128 // Fix predecessor-successor relationship.
1129 NestedCont->transferSuccessors(MBB);
1130 if (EHPad)
1131 NestedCont->removeSuccessor(EHPad);
1132 MBB->addSuccessor(NestedEHPad);
1133 MBB->addSuccessor(NestedCont);
1134 NestedEHPad->addSuccessor(BrDest);
1135 }
1136 }
1137
1138 // Renumber BBs and recalculate ScopeTop info because new BBs might have been
1139 // created and inserted above.
1140 MF.RenumberBlocks();
1141 ScopeTops.clear();
1142 ScopeTops.resize(MF.getNumBlockIDs());
1143 for (auto &MBB : reverse(MF)) {
1144 for (auto &MI : reverse(MBB)) {
1145 if (ScopeTops[MBB.getNumber()])
1146 break;
1147 switch (MI.getOpcode()) {
1148 case WebAssembly::END_BLOCK:
1149 case WebAssembly::END_LOOP:
1150 case WebAssembly::END_TRY:
1151 ScopeTops[MBB.getNumber()] = EndToBegin[&MI]->getParent();
1152 break;
1153 case WebAssembly::CATCH:
1154 ScopeTops[MBB.getNumber()] = EHPadToTry[&MBB]->getParent();
1155 break;
1156 }
1157 }
1158 }
1159
1160 // Recompute the dominator tree.
1161 getAnalysis<MachineDominatorTree>().runOnMachineFunction(MF);
1162
1163 // Place block markers for newly added branches.
1164 SmallVector <MachineBasicBlock *, 8> BrDests;
1165 for (auto &P : BrDestToTryRanges)
1166 BrDests.push_back(P.first);
1167 llvm::sort(BrDests,
1168 [&](const MachineBasicBlock *A, const MachineBasicBlock *B) {
1169 auto ANum = A->getNumber();
1170 auto BNum = B->getNumber();
1171 return ANum < BNum;
1172 });
1173 for (auto *Dest : BrDests)
1174 placeBlockMarker(*Dest);
1175
1176 return true;
1177}
1178
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001179static unsigned
Heejin Ahn18c56a02019-02-04 19:13:39 +00001180getDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack,
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001181 const MachineBasicBlock *MBB) {
1182 unsigned Depth = 0;
1183 for (auto X : reverse(Stack)) {
1184 if (X == MBB)
1185 break;
1186 ++Depth;
1187 }
1188 assert(Depth < Stack.size() && "Branch destination should be in scope");
1189 return Depth;
1190}
1191
Dan Gohman2726b882016-10-06 22:29:32 +00001192/// In normal assembly languages, when the end of a function is unreachable,
1193/// because the function ends in an infinite loop or a noreturn call or similar,
1194/// it isn't necessary to worry about the function return type at the end of
1195/// the function, because it's never reached. However, in WebAssembly, blocks
1196/// that end at the function end need to have a return type signature that
1197/// matches the function signature, even though it's unreachable. This function
1198/// checks for such cases and fixes up the signatures.
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001199void WebAssemblyCFGStackify::fixEndsAtEndOfFunction(MachineFunction &MF) {
1200 const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Dan Gohman2726b882016-10-06 22:29:32 +00001201 assert(MFI.getResults().size() <= 1);
1202
1203 if (MFI.getResults().empty())
1204 return;
1205
Heejin Ahn18c56a02019-02-04 19:13:39 +00001206 WebAssembly::ExprType RetType;
Dan Gohman2726b882016-10-06 22:29:32 +00001207 switch (MFI.getResults().front().SimpleTy) {
Heejin Ahnf208f632018-09-05 01:27:38 +00001208 case MVT::i32:
Heejin Ahn18c56a02019-02-04 19:13:39 +00001209 RetType = WebAssembly::ExprType::I32;
Heejin Ahnf208f632018-09-05 01:27:38 +00001210 break;
1211 case MVT::i64:
Heejin Ahn18c56a02019-02-04 19:13:39 +00001212 RetType = WebAssembly::ExprType::I64;
Heejin Ahnf208f632018-09-05 01:27:38 +00001213 break;
1214 case MVT::f32:
Heejin Ahn18c56a02019-02-04 19:13:39 +00001215 RetType = WebAssembly::ExprType::F32;
Heejin Ahnf208f632018-09-05 01:27:38 +00001216 break;
1217 case MVT::f64:
Heejin Ahn18c56a02019-02-04 19:13:39 +00001218 RetType = WebAssembly::ExprType::F64;
Heejin Ahnf208f632018-09-05 01:27:38 +00001219 break;
Derek Schuff2c783852018-08-06 23:16:50 +00001220 case MVT::v16i8:
1221 case MVT::v8i16:
1222 case MVT::v4i32:
Derek Schuff51ed1312018-08-07 21:24:01 +00001223 case MVT::v2i64:
Derek Schuff2c783852018-08-06 23:16:50 +00001224 case MVT::v4f32:
Derek Schuff51ed1312018-08-07 21:24:01 +00001225 case MVT::v2f64:
Heejin Ahn18c56a02019-02-04 19:13:39 +00001226 RetType = WebAssembly::ExprType::V128;
Derek Schuff2c783852018-08-06 23:16:50 +00001227 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001228 case MVT::ExceptRef:
Heejin Ahn18c56a02019-02-04 19:13:39 +00001229 RetType = WebAssembly::ExprType::ExceptRef;
Heejin Ahnf208f632018-09-05 01:27:38 +00001230 break;
1231 default:
1232 llvm_unreachable("unexpected return type");
Dan Gohman2726b882016-10-06 22:29:32 +00001233 }
1234
1235 for (MachineBasicBlock &MBB : reverse(MF)) {
1236 for (MachineInstr &MI : reverse(MBB)) {
Shiva Chen801bf7e2018-05-09 02:42:00 +00001237 if (MI.isPosition() || MI.isDebugInstr())
Dan Gohman2726b882016-10-06 22:29:32 +00001238 continue;
1239 if (MI.getOpcode() == WebAssembly::END_BLOCK) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001240 EndToBegin[&MI]->getOperand(0).setImm(int32_t(RetType));
Dan Gohman2726b882016-10-06 22:29:32 +00001241 continue;
1242 }
1243 if (MI.getOpcode() == WebAssembly::END_LOOP) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001244 EndToBegin[&MI]->getOperand(0).setImm(int32_t(RetType));
Dan Gohman2726b882016-10-06 22:29:32 +00001245 continue;
1246 }
1247 // Something other than an `end`. We're done.
1248 return;
1249 }
1250 }
1251}
1252
Dan Gohmand934cb82017-02-24 23:18:00 +00001253// WebAssembly functions end with an end instruction, as if the function body
1254// were a block.
Heejin Ahn18c56a02019-02-04 19:13:39 +00001255static void appendEndToFunction(MachineFunction &MF,
Heejin Ahnf208f632018-09-05 01:27:38 +00001256 const WebAssemblyInstrInfo &TII) {
Derek Schuff10b31352018-03-15 22:06:51 +00001257 BuildMI(MF.back(), MF.back().end(),
1258 MF.back().findPrevDebugLoc(MF.back().end()),
Dan Gohmand934cb82017-02-24 23:18:00 +00001259 TII.get(WebAssembly::END_FUNCTION));
1260}
1261
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001262/// Insert LOOP/TRY/BLOCK markers at appropriate places.
1263void WebAssemblyCFGStackify::placeMarkers(MachineFunction &MF) {
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001264 // We allocate one more than the number of blocks in the function to
1265 // accommodate for the possible fake block we may insert at the end.
1266 ScopeTops.resize(MF.getNumBlockIDs() + 1);
1267 // Place the LOOP for MBB if MBB is the header of a loop.
1268 for (auto &MBB : MF)
1269 placeLoopMarker(MBB);
Heejin Ahn44a5a4b2019-03-26 17:15:55 +00001270
Heejin Ahnd6f48782019-01-30 03:21:57 +00001271 const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
Heejin Ahn44a5a4b2019-03-26 17:15:55 +00001272 for (auto &MBB : MF) {
1273 if (MBB.isEHPad()) {
1274 // Place the TRY for MBB if MBB is the EH pad of an exception.
1275 if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
1276 MF.getFunction().hasPersonalityFn())
1277 placeTryMarker(MBB);
1278 } else {
1279 // Place the BLOCK for MBB if MBB is branched to from above.
1280 placeBlockMarker(MBB);
1281 }
1282 }
Heejin Ahnc4ac74f2019-03-30 11:04:48 +00001283 // Fix mismatches in unwind destinations induced by linearizing the code.
1284 fixUnwindMismatches(MF);
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001285}
Dan Gohman8fe7e862015-12-14 22:51:54 +00001286
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001287void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001288 // Now rewrite references to basic blocks to be depth immediates.
1289 SmallVector<const MachineBasicBlock *, 8> Stack;
1290 for (auto &MBB : reverse(MF)) {
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001291 for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
1292 MachineInstr &MI = *I;
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001293 switch (MI.getOpcode()) {
1294 case WebAssembly::BLOCK:
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001295 case WebAssembly::TRY:
1296 assert(ScopeTops[Stack.back()->getNumber()]->getNumber() <=
1297 MBB.getNumber() &&
1298 "Block/try marker should be balanced");
1299 Stack.pop_back();
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001300 break;
1301
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001302 case WebAssembly::LOOP:
1303 assert(Stack.back() == &MBB && "Loop top should be balanced");
1304 Stack.pop_back();
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001305 break;
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001306
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001307 case WebAssembly::END_BLOCK:
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001308 case WebAssembly::END_TRY:
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001309 Stack.push_back(&MBB);
1310 break;
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001311
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001312 case WebAssembly::END_LOOP:
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001313 Stack.push_back(EndToBegin[&MI]->getParent());
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001314 break;
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001315
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001316 default:
1317 if (MI.isTerminator()) {
1318 // Rewrite MBB operands to be depth immediates.
1319 SmallVector<MachineOperand, 4> Ops(MI.operands());
1320 while (MI.getNumOperands() > 0)
1321 MI.RemoveOperand(MI.getNumOperands() - 1);
1322 for (auto MO : Ops) {
1323 if (MO.isMBB())
Heejin Ahn18c56a02019-02-04 19:13:39 +00001324 MO = MachineOperand::CreateImm(getDepth(Stack, MO.getMBB()));
Dan Gohman1d68e80f2016-01-12 19:14:46 +00001325 MI.addOperand(MF, MO);
1326 }
1327 }
1328 break;
1329 }
1330 }
1331 }
1332 assert(Stack.empty() && "Control flow should be balanced");
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001333}
Dan Gohman2726b882016-10-06 22:29:32 +00001334
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001335void WebAssemblyCFGStackify::releaseMemory() {
1336 ScopeTops.clear();
1337 BeginToEnd.clear();
1338 EndToBegin.clear();
1339 TryToEHPad.clear();
1340 EHPadToTry.clear();
Heejin Ahnc4ac74f2019-03-30 11:04:48 +00001341 AppendixBB = nullptr;
Dan Gohman32807932015-11-23 16:19:56 +00001342}
Dan Gohman32807932015-11-23 16:19:56 +00001343
Dan Gohman950a13c2015-09-16 16:51:30 +00001344bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001345 LLVM_DEBUG(dbgs() << "********** CFG Stackifying **********\n"
1346 "********** Function: "
1347 << MF.getName() << '\n');
Heejin Ahncf699b42019-02-27 00:50:53 +00001348 const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
Dan Gohman950a13c2015-09-16 16:51:30 +00001349
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001350 releaseMemory();
1351
Dan Gohmane0405332016-10-03 22:43:53 +00001352 // Liveness is not tracked for VALUE_STACK physreg.
Derek Schuff9c3bf312016-01-13 17:10:28 +00001353 MF.getRegInfo().invalidateLiveness();
Dan Gohman950a13c2015-09-16 16:51:30 +00001354
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001355 // Place the BLOCK/LOOP/TRY markers to indicate the beginnings of scopes.
1356 placeMarkers(MF);
1357
Heejin Ahnc4ac74f2019-03-30 11:04:48 +00001358 // Remove unnecessary instructions possibly introduced by try/end_trys.
Heejin Ahncf699b42019-02-27 00:50:53 +00001359 if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
1360 MF.getFunction().hasPersonalityFn())
Heejin Ahncf699b42019-02-27 00:50:53 +00001361 removeUnnecessaryInstrs(MF);
1362
Heejin Ahne76fa9e2018-08-16 23:50:59 +00001363 // Convert MBB operands in terminators to relative depth immediates.
1364 rewriteDepthImmediates(MF);
1365
1366 // Fix up block/loop/try signatures at the end of the function to conform to
1367 // WebAssembly's rules.
1368 fixEndsAtEndOfFunction(MF);
1369
1370 // Add an end instruction at the end of the function body.
1371 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1372 if (!MF.getSubtarget<WebAssemblySubtarget>()
1373 .getTargetTriple()
1374 .isOSBinFormatELF())
Heejin Ahn18c56a02019-02-04 19:13:39 +00001375 appendEndToFunction(MF, TII);
Dan Gohman32807932015-11-23 16:19:56 +00001376
Heejin Ahn1aaa4812019-03-26 17:46:14 +00001377 MF.getInfo<WebAssemblyFunctionInfo>()->setCFGStackified();
Dan Gohman950a13c2015-09-16 16:51:30 +00001378 return true;
1379}