Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements a CFG stacking pass. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 11 | /// |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 12 | /// 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 Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 15 | /// |
| 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 Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 19 | /// In case we use exceptions, this pass also fixes mismatches in unwind |
| 20 | /// destinations created during transforming CFG into wasm structured format. |
| 21 | /// |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 24 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 25 | #include "WebAssembly.h" |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 26 | #include "WebAssemblyExceptionInfo.h" |
Dan Gohman | ed0f113 | 2016-01-30 05:01:06 +0000 | [diff] [blame] | 27 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 28 | #include "WebAssemblySubtarget.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 29 | #include "WebAssemblyUtilities.h" |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineDominators.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 33 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Derek Schuff | 9c3bf31 | 2016-01-13 17:10:28 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/Passes.h" |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/WasmEHFuncInfo.h" |
| 37 | #include "llvm/MC/MCAsmInfo.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 40 | #include <cstring> |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| 43 | #define DEBUG_TYPE "wasm-cfg-stackify" |
| 44 | |
| 45 | namespace { |
| 46 | class WebAssemblyCFGStackify final : public MachineFunctionPass { |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 47 | StringRef getPassName() const override { return "WebAssembly CFG Stackify"; } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 48 | |
| 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 50 | AU.addRequired<MachineDominatorTree>(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 51 | AU.addRequired<MachineLoopInfo>(); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 52 | AU.addRequired<WebAssemblyExceptionInfo>(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 53 | MachineFunctionPass::getAnalysisUsage(AU); |
| 54 | } |
| 55 | |
| 56 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 57 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 58 | // For each block whose label represents the end of a scope, record the block |
| 59 | // which holds the beginning of the scope. This will allow us to quickly skip |
| 60 | // over scoped regions when walking blocks. |
| 61 | SmallVector<MachineBasicBlock *, 8> ScopeTops; |
| 62 | |
| 63 | void placeMarkers(MachineFunction &MF); |
| 64 | void placeBlockMarker(MachineBasicBlock &MBB); |
| 65 | void placeLoopMarker(MachineBasicBlock &MBB); |
| 66 | void placeTryMarker(MachineBasicBlock &MBB); |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 67 | void removeUnnecessaryInstrs(MachineFunction &MF); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 68 | void rewriteDepthImmediates(MachineFunction &MF); |
| 69 | void fixEndsAtEndOfFunction(MachineFunction &MF); |
| 70 | |
| 71 | // For each BLOCK|LOOP|TRY, the corresponding END_(BLOCK|LOOP|TRY). |
| 72 | DenseMap<const MachineInstr *, MachineInstr *> BeginToEnd; |
| 73 | // For each END_(BLOCK|LOOP|TRY), the corresponding BLOCK|LOOP|TRY. |
| 74 | DenseMap<const MachineInstr *, MachineInstr *> EndToBegin; |
| 75 | // <TRY marker, EH pad> map |
| 76 | DenseMap<const MachineInstr *, MachineBasicBlock *> TryToEHPad; |
| 77 | // <EH pad, TRY marker> map |
| 78 | DenseMap<const MachineBasicBlock *, MachineInstr *> EHPadToTry; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 79 | |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 80 | // Helper functions to register / unregister scope information created by |
| 81 | // marker instructions. |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 82 | void registerScope(MachineInstr *Begin, MachineInstr *End); |
| 83 | void registerTryScope(MachineInstr *Begin, MachineInstr *End, |
| 84 | MachineBasicBlock *EHPad); |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 85 | void unregisterScope(MachineInstr *Begin); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 86 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 87 | public: |
| 88 | static char ID; // Pass identification, replacement for typeid |
| 89 | WebAssemblyCFGStackify() : MachineFunctionPass(ID) {} |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 90 | ~WebAssemblyCFGStackify() override { releaseMemory(); } |
| 91 | void releaseMemory() override; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 92 | }; |
| 93 | } // end anonymous namespace |
| 94 | |
| 95 | char WebAssemblyCFGStackify::ID = 0; |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 96 | INITIALIZE_PASS(WebAssemblyCFGStackify, DEBUG_TYPE, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 97 | "Insert BLOCK and LOOP markers for WebAssembly scopes", false, |
| 98 | false) |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 99 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 100 | FunctionPass *llvm::createWebAssemblyCFGStackify() { |
| 101 | return new WebAssemblyCFGStackify(); |
| 102 | } |
| 103 | |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 104 | /// Test whether Pred has any terminators explicitly branching to MBB, as |
| 105 | /// opposed to falling through. Note that it's possible (eg. in unoptimized |
| 106 | /// code) for a branch instruction to both branch to a block and fallthrough |
| 107 | /// to it, so we check the actual branch operands to see if there are any |
| 108 | /// explicit mentions. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 109 | static bool explicitlyBranchesTo(MachineBasicBlock *Pred, |
Dan Gohman | 35e4a28 | 2016-01-08 01:06:00 +0000 | [diff] [blame] | 110 | MachineBasicBlock *MBB) { |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 111 | for (MachineInstr &MI : Pred->terminators()) |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 112 | for (MachineOperand &MO : MI.explicit_operands()) |
| 113 | if (MO.isMBB() && MO.getMBB() == MBB) |
| 114 | return true; |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 118 | // Returns an iterator to the earliest position possible within the MBB, |
| 119 | // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet |
| 120 | // contains instructions that should go before the marker, and AfterSet contains |
| 121 | // ones that should go after the marker. In this function, AfterSet is only |
| 122 | // used for sanity checking. |
| 123 | static MachineBasicBlock::iterator |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 124 | getEarliestInsertPos(MachineBasicBlock *MBB, |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 125 | const SmallPtrSet<const MachineInstr *, 4> &BeforeSet, |
| 126 | const SmallPtrSet<const MachineInstr *, 4> &AfterSet) { |
| 127 | auto InsertPos = MBB->end(); |
| 128 | while (InsertPos != MBB->begin()) { |
| 129 | if (BeforeSet.count(&*std::prev(InsertPos))) { |
| 130 | #ifndef NDEBUG |
| 131 | // Sanity check |
| 132 | for (auto Pos = InsertPos, E = MBB->begin(); Pos != E; --Pos) |
| 133 | assert(!AfterSet.count(&*std::prev(Pos))); |
| 134 | #endif |
| 135 | break; |
| 136 | } |
| 137 | --InsertPos; |
| 138 | } |
| 139 | return InsertPos; |
| 140 | } |
| 141 | |
| 142 | // Returns an iterator to the latest position possible within the MBB, |
| 143 | // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet |
| 144 | // contains instructions that should go before the marker, and AfterSet contains |
| 145 | // ones that should go after the marker. In this function, BeforeSet is only |
| 146 | // used for sanity checking. |
| 147 | static MachineBasicBlock::iterator |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 148 | getLatestInsertPos(MachineBasicBlock *MBB, |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 149 | const SmallPtrSet<const MachineInstr *, 4> &BeforeSet, |
| 150 | const SmallPtrSet<const MachineInstr *, 4> &AfterSet) { |
| 151 | auto InsertPos = MBB->begin(); |
| 152 | while (InsertPos != MBB->end()) { |
| 153 | if (AfterSet.count(&*InsertPos)) { |
| 154 | #ifndef NDEBUG |
| 155 | // Sanity check |
| 156 | for (auto Pos = InsertPos, E = MBB->end(); Pos != E; ++Pos) |
| 157 | assert(!BeforeSet.count(&*Pos)); |
| 158 | #endif |
| 159 | break; |
| 160 | } |
| 161 | ++InsertPos; |
| 162 | } |
| 163 | return InsertPos; |
| 164 | } |
| 165 | |
| 166 | void WebAssemblyCFGStackify::registerScope(MachineInstr *Begin, |
| 167 | MachineInstr *End) { |
| 168 | BeginToEnd[Begin] = End; |
| 169 | EndToBegin[End] = Begin; |
| 170 | } |
| 171 | |
| 172 | void WebAssemblyCFGStackify::registerTryScope(MachineInstr *Begin, |
| 173 | MachineInstr *End, |
| 174 | MachineBasicBlock *EHPad) { |
| 175 | registerScope(Begin, End); |
| 176 | TryToEHPad[Begin] = EHPad; |
| 177 | EHPadToTry[EHPad] = Begin; |
| 178 | } |
| 179 | |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 180 | void WebAssemblyCFGStackify::unregisterScope(MachineInstr *Begin) { |
| 181 | assert(BeginToEnd.count(Begin)); |
| 182 | MachineInstr *End = BeginToEnd[Begin]; |
| 183 | assert(EndToBegin.count(End)); |
| 184 | BeginToEnd.erase(Begin); |
| 185 | EndToBegin.erase(End); |
| 186 | MachineBasicBlock *EHPad = TryToEHPad.lookup(Begin); |
| 187 | if (EHPad) { |
| 188 | assert(EHPadToTry.count(EHPad)); |
| 189 | TryToEHPad.erase(Begin); |
| 190 | EHPadToTry.erase(EHPad); |
| 191 | } |
| 192 | } |
| 193 | |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 194 | /// Insert a BLOCK marker for branches to MBB (if needed). |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 195 | void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) { |
| 196 | // This should have been handled in placeTryMarker. |
| 197 | if (MBB.isEHPad()) |
| 198 | return; |
| 199 | |
| 200 | MachineFunction &MF = *MBB.getParent(); |
| 201 | auto &MDT = getAnalysis<MachineDominatorTree>(); |
| 202 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 203 | const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 204 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 205 | // First compute the nearest common dominator of all forward non-fallthrough |
| 206 | // predecessors so that we minimize the time that the BLOCK is on the stack, |
| 207 | // which reduces overall stack height. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 208 | MachineBasicBlock *Header = nullptr; |
| 209 | bool IsBranchedTo = false; |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 210 | bool IsBrOnExn = false; |
| 211 | MachineInstr *BrOnExn = nullptr; |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 212 | int MBBNumber = MBB.getNumber(); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 213 | for (MachineBasicBlock *Pred : MBB.predecessors()) { |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 214 | if (Pred->getNumber() < MBBNumber) { |
| 215 | Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 216 | if (explicitlyBranchesTo(Pred, &MBB)) { |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 217 | IsBranchedTo = true; |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 218 | if (Pred->getFirstTerminator()->getOpcode() == WebAssembly::BR_ON_EXN) { |
| 219 | IsBrOnExn = true; |
| 220 | assert(!BrOnExn && "There should be only one br_on_exn per block"); |
| 221 | BrOnExn = &*Pred->getFirstTerminator(); |
| 222 | } |
| 223 | } |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 224 | } |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 225 | } |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 226 | if (!Header) |
| 227 | return; |
| 228 | if (!IsBranchedTo) |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 229 | return; |
| 230 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 231 | assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors"); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 232 | MachineBasicBlock *LayoutPred = &*std::prev(MachineFunction::iterator(&MBB)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 233 | |
| 234 | // If the nearest common dominator is inside a more deeply nested context, |
| 235 | // walk out to the nearest scope which isn't more deeply nested. |
| 236 | for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) { |
| 237 | if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) { |
| 238 | if (ScopeTop->getNumber() > Header->getNumber()) { |
| 239 | // Skip over an intervening scope. |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 240 | I = std::next(MachineFunction::iterator(ScopeTop)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 241 | } else { |
| 242 | // We found a scope level at an appropriate depth. |
| 243 | Header = ScopeTop; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 249 | // Decide where in Header to put the BLOCK. |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 250 | |
| 251 | // Instructions that should go before the BLOCK. |
| 252 | SmallPtrSet<const MachineInstr *, 4> BeforeSet; |
| 253 | // Instructions that should go after the BLOCK. |
| 254 | SmallPtrSet<const MachineInstr *, 4> AfterSet; |
| 255 | for (const auto &MI : *Header) { |
| 256 | // If there is a previously placed LOOP/TRY marker and the bottom block of |
| 257 | // the loop/exception is above MBB, it should be after the BLOCK, because |
| 258 | // the loop/exception is nested in this block. Otherwise it should be before |
| 259 | // the BLOCK. |
| 260 | if (MI.getOpcode() == WebAssembly::LOOP || |
| 261 | MI.getOpcode() == WebAssembly::TRY) { |
Heejin Ahn | 85631d8 | 2019-02-22 07:19:30 +0000 | [diff] [blame] | 262 | auto *BottomBB = |
| 263 | &*std::prev(MachineFunction::iterator(BeginToEnd[&MI]->getParent())); |
| 264 | if (MBB.getNumber() > BottomBB->getNumber()) |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 265 | AfterSet.insert(&MI); |
| 266 | #ifndef NDEBUG |
| 267 | else |
| 268 | BeforeSet.insert(&MI); |
| 269 | #endif |
| 270 | } |
| 271 | |
| 272 | // All previously inserted BLOCK markers should be after the BLOCK because |
| 273 | // they are all nested blocks. |
| 274 | if (MI.getOpcode() == WebAssembly::BLOCK) |
| 275 | AfterSet.insert(&MI); |
| 276 | |
| 277 | #ifndef NDEBUG |
| 278 | // All END_(BLOCK|LOOP|TRY) markers should be before the BLOCK. |
| 279 | if (MI.getOpcode() == WebAssembly::END_BLOCK || |
| 280 | MI.getOpcode() == WebAssembly::END_LOOP || |
| 281 | MI.getOpcode() == WebAssembly::END_TRY) |
| 282 | BeforeSet.insert(&MI); |
| 283 | #endif |
| 284 | |
| 285 | // Terminators should go after the BLOCK. |
| 286 | if (MI.isTerminator()) |
| 287 | AfterSet.insert(&MI); |
| 288 | } |
| 289 | |
| 290 | // Local expression tree should go after the BLOCK. |
| 291 | for (auto I = Header->getFirstTerminator(), E = Header->begin(); I != E; |
| 292 | --I) { |
Yury Delendik | 409b439 | 2018-10-04 23:31:00 +0000 | [diff] [blame] | 293 | if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition()) |
| 294 | continue; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 295 | if (WebAssembly::isChild(*std::prev(I), MFI)) |
| 296 | AfterSet.insert(&*std::prev(I)); |
| 297 | else |
| 298 | break; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 301 | // Add the BLOCK. |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 302 | |
| 303 | // 'br_on_exn' extracts except_ref object and pushes variable number of values |
| 304 | // depending on its tag. For C++ exception, its a single i32 value, and the |
| 305 | // generated code will be in the form of: |
| 306 | // block i32 |
| 307 | // br_on_exn 0, $__cpp_exception |
| 308 | // rethrow |
| 309 | // end_block |
| 310 | WebAssembly::ExprType ReturnType = WebAssembly::ExprType::Void; |
| 311 | if (IsBrOnExn) { |
| 312 | const char *TagName = BrOnExn->getOperand(1).getSymbolName(); |
| 313 | if (std::strcmp(TagName, "__cpp_exception") != 0) |
| 314 | llvm_unreachable("Only C++ exception is supported"); |
| 315 | ReturnType = WebAssembly::ExprType::I32; |
| 316 | } |
| 317 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 318 | auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet); |
Heejin Ahn | 92401cc | 2018-04-14 00:12:12 +0000 | [diff] [blame] | 319 | MachineInstr *Begin = |
| 320 | BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos), |
| 321 | TII.get(WebAssembly::BLOCK)) |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 322 | .addImm(int64_t(ReturnType)); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 323 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 324 | // Decide where in Header to put the END_BLOCK. |
| 325 | BeforeSet.clear(); |
| 326 | AfterSet.clear(); |
| 327 | for (auto &MI : MBB) { |
| 328 | #ifndef NDEBUG |
| 329 | // END_BLOCK should precede existing LOOP and TRY markers. |
| 330 | if (MI.getOpcode() == WebAssembly::LOOP || |
| 331 | MI.getOpcode() == WebAssembly::TRY) |
| 332 | AfterSet.insert(&MI); |
| 333 | #endif |
| 334 | |
| 335 | // If there is a previously placed END_LOOP marker and the header of the |
| 336 | // loop is above this block's header, the END_LOOP should be placed after |
| 337 | // the BLOCK, because the loop contains this block. Otherwise the END_LOOP |
| 338 | // should be placed before the BLOCK. The same for END_TRY. |
| 339 | if (MI.getOpcode() == WebAssembly::END_LOOP || |
| 340 | MI.getOpcode() == WebAssembly::END_TRY) { |
| 341 | if (EndToBegin[&MI]->getParent()->getNumber() >= Header->getNumber()) |
| 342 | BeforeSet.insert(&MI); |
| 343 | #ifndef NDEBUG |
| 344 | else |
| 345 | AfterSet.insert(&MI); |
| 346 | #endif |
| 347 | } |
| 348 | } |
| 349 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 350 | // Mark the end of the block. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 351 | InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet); |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 352 | MachineInstr *End = BuildMI(MBB, InsertPos, MBB.findPrevDebugLoc(InsertPos), |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 353 | TII.get(WebAssembly::END_BLOCK)); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 354 | registerScope(Begin, End); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 355 | |
| 356 | // Track the farthest-spanning scope that ends at this point. |
| 357 | int Number = MBB.getNumber(); |
| 358 | if (!ScopeTops[Number] || |
| 359 | ScopeTops[Number]->getNumber() > Header->getNumber()) |
| 360 | ScopeTops[Number] = Header; |
| 361 | } |
| 362 | |
| 363 | /// Insert a LOOP marker for a loop starting at MBB (if it's a loop header). |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 364 | void WebAssemblyCFGStackify::placeLoopMarker(MachineBasicBlock &MBB) { |
| 365 | MachineFunction &MF = *MBB.getParent(); |
| 366 | const auto &MLI = getAnalysis<MachineLoopInfo>(); |
| 367 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 368 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 369 | MachineLoop *Loop = MLI.getLoopFor(&MBB); |
| 370 | if (!Loop || Loop->getHeader() != &MBB) |
| 371 | return; |
| 372 | |
| 373 | // The operand of a LOOP is the first block after the loop. If the loop is the |
| 374 | // bottom of the function, insert a dummy block at the end. |
Heejin Ahn | 817811ca | 2018-06-19 00:32:03 +0000 | [diff] [blame] | 375 | MachineBasicBlock *Bottom = WebAssembly::getBottom(Loop); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 376 | auto Iter = std::next(MachineFunction::iterator(Bottom)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 377 | if (Iter == MF.end()) { |
| 378 | MachineBasicBlock *Label = MF.CreateMachineBasicBlock(); |
| 379 | // Give it a fake predecessor so that AsmPrinter prints its label. |
| 380 | Label->addSuccessor(Label); |
| 381 | MF.push_back(Label); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 382 | Iter = std::next(MachineFunction::iterator(Bottom)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 383 | } |
| 384 | MachineBasicBlock *AfterLoop = &*Iter; |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 385 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 386 | // Decide where in Header to put the LOOP. |
| 387 | SmallPtrSet<const MachineInstr *, 4> BeforeSet; |
| 388 | SmallPtrSet<const MachineInstr *, 4> AfterSet; |
| 389 | for (const auto &MI : MBB) { |
| 390 | // LOOP marker should be after any existing loop that ends here. Otherwise |
| 391 | // we assume the instruction belongs to the loop. |
| 392 | if (MI.getOpcode() == WebAssembly::END_LOOP) |
| 393 | BeforeSet.insert(&MI); |
| 394 | #ifndef NDEBUG |
| 395 | else |
| 396 | AfterSet.insert(&MI); |
| 397 | #endif |
| 398 | } |
| 399 | |
| 400 | // Mark the beginning of the loop. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 401 | auto InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet); |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 402 | MachineInstr *Begin = BuildMI(MBB, InsertPos, MBB.findDebugLoc(InsertPos), |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 403 | TII.get(WebAssembly::LOOP)) |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 404 | .addImm(int64_t(WebAssembly::ExprType::Void)); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 405 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 406 | // Decide where in Header to put the END_LOOP. |
| 407 | BeforeSet.clear(); |
| 408 | AfterSet.clear(); |
| 409 | #ifndef NDEBUG |
| 410 | for (const auto &MI : MBB) |
| 411 | // Existing END_LOOP markers belong to parent loops of this loop |
| 412 | if (MI.getOpcode() == WebAssembly::END_LOOP) |
| 413 | AfterSet.insert(&MI); |
| 414 | #endif |
| 415 | |
| 416 | // Mark the end of the loop (using arbitrary debug location that branched to |
| 417 | // the loop end as its location). |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 418 | InsertPos = getEarliestInsertPos(AfterLoop, BeforeSet, AfterSet); |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 419 | DebugLoc EndDL = (*AfterLoop->pred_rbegin())->findBranchDebugLoc(); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 420 | MachineInstr *End = |
| 421 | BuildMI(*AfterLoop, InsertPos, EndDL, TII.get(WebAssembly::END_LOOP)); |
| 422 | registerScope(Begin, End); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 423 | |
| 424 | assert((!ScopeTops[AfterLoop->getNumber()] || |
| 425 | ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) && |
Dan Gohman | 442bfce | 2016-02-16 16:22:41 +0000 | [diff] [blame] | 426 | "With block sorting the outermost loop for a block should be first."); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 427 | if (!ScopeTops[AfterLoop->getNumber()]) |
| 428 | ScopeTops[AfterLoop->getNumber()] = &MBB; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 431 | void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) { |
| 432 | if (!MBB.isEHPad()) |
| 433 | return; |
| 434 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 435 | MachineFunction &MF = *MBB.getParent(); |
| 436 | auto &MDT = getAnalysis<MachineDominatorTree>(); |
| 437 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 438 | const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>(); |
| 439 | const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 440 | |
| 441 | // Compute the nearest common dominator of all unwind predecessors |
| 442 | MachineBasicBlock *Header = nullptr; |
| 443 | int MBBNumber = MBB.getNumber(); |
| 444 | for (auto *Pred : MBB.predecessors()) { |
| 445 | if (Pred->getNumber() < MBBNumber) { |
| 446 | Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 447 | assert(!explicitlyBranchesTo(Pred, &MBB) && |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 448 | "Explicit branch to an EH pad!"); |
| 449 | } |
| 450 | } |
| 451 | if (!Header) |
| 452 | return; |
| 453 | |
| 454 | // If this try is at the bottom of the function, insert a dummy block at the |
| 455 | // end. |
| 456 | WebAssemblyException *WE = WEI.getExceptionFor(&MBB); |
| 457 | assert(WE); |
| 458 | MachineBasicBlock *Bottom = WebAssembly::getBottom(WE); |
| 459 | |
| 460 | auto Iter = std::next(MachineFunction::iterator(Bottom)); |
| 461 | if (Iter == MF.end()) { |
| 462 | MachineBasicBlock *Label = MF.CreateMachineBasicBlock(); |
| 463 | // Give it a fake predecessor so that AsmPrinter prints its label. |
| 464 | Label->addSuccessor(Label); |
| 465 | MF.push_back(Label); |
| 466 | Iter = std::next(MachineFunction::iterator(Bottom)); |
| 467 | } |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 468 | MachineBasicBlock *Cont = &*Iter; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 469 | |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 470 | assert(Cont != &MF.front()); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 471 | MachineBasicBlock *LayoutPred = |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 472 | &*std::prev(MachineFunction::iterator(Cont)); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 473 | |
| 474 | // If the nearest common dominator is inside a more deeply nested context, |
| 475 | // walk out to the nearest scope which isn't more deeply nested. |
| 476 | for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) { |
| 477 | if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) { |
| 478 | if (ScopeTop->getNumber() > Header->getNumber()) { |
| 479 | // Skip over an intervening scope. |
| 480 | I = std::next(MachineFunction::iterator(ScopeTop)); |
| 481 | } else { |
| 482 | // We found a scope level at an appropriate depth. |
| 483 | Header = ScopeTop; |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | // Decide where in Header to put the TRY. |
| 490 | |
| 491 | // Instructions that should go before the BLOCK. |
| 492 | SmallPtrSet<const MachineInstr *, 4> BeforeSet; |
| 493 | // Instructions that should go after the BLOCK. |
| 494 | SmallPtrSet<const MachineInstr *, 4> AfterSet; |
| 495 | for (const auto &MI : *Header) { |
| 496 | // If there is a previously placed LOOP marker and the bottom block of |
| 497 | // the loop is above MBB, the LOOP should be after the TRY, because the |
| 498 | // loop is nested in this try. Otherwise it should be before the TRY. |
| 499 | if (MI.getOpcode() == WebAssembly::LOOP) { |
| 500 | if (MBB.getNumber() > Bottom->getNumber()) |
| 501 | AfterSet.insert(&MI); |
| 502 | #ifndef NDEBUG |
| 503 | else |
| 504 | BeforeSet.insert(&MI); |
| 505 | #endif |
| 506 | } |
| 507 | |
| 508 | // All previously inserted TRY markers should be after the TRY because they |
| 509 | // are all nested trys. |
| 510 | if (MI.getOpcode() == WebAssembly::TRY) |
| 511 | AfterSet.insert(&MI); |
| 512 | |
| 513 | #ifndef NDEBUG |
| 514 | // All END_(LOOP/TRY) markers should be before the TRY. |
| 515 | if (MI.getOpcode() == WebAssembly::END_LOOP || |
| 516 | MI.getOpcode() == WebAssembly::END_TRY) |
| 517 | BeforeSet.insert(&MI); |
| 518 | #endif |
| 519 | |
| 520 | // Terminators should go after the TRY. |
| 521 | if (MI.isTerminator()) |
| 522 | AfterSet.insert(&MI); |
| 523 | } |
| 524 | |
| 525 | // Local expression tree should go after the TRY. |
| 526 | for (auto I = Header->getFirstTerminator(), E = Header->begin(); I != E; |
| 527 | --I) { |
Yury Delendik | 409b439 | 2018-10-04 23:31:00 +0000 | [diff] [blame] | 528 | if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition()) |
| 529 | continue; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 530 | if (WebAssembly::isChild(*std::prev(I), MFI)) |
| 531 | AfterSet.insert(&*std::prev(I)); |
| 532 | else |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | // If Header unwinds to MBB (= Header contains 'invoke'), the try block should |
| 537 | // contain the call within it. So the call should go after the TRY. The |
| 538 | // exception is when the header's terminator is a rethrow instruction, in |
| 539 | // which case that instruction, not a call instruction before it, is gonna |
| 540 | // throw. |
| 541 | if (MBB.isPredecessor(Header)) { |
| 542 | auto TermPos = Header->getFirstTerminator(); |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 543 | if (TermPos == Header->end() || |
| 544 | TermPos->getOpcode() != WebAssembly::RETHROW) { |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 545 | for (const auto &MI : reverse(*Header)) { |
| 546 | if (MI.isCall()) { |
| 547 | AfterSet.insert(&MI); |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Add the TRY. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 555 | auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 556 | MachineInstr *Begin = |
| 557 | BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos), |
| 558 | TII.get(WebAssembly::TRY)) |
| 559 | .addImm(int64_t(WebAssembly::ExprType::Void)); |
| 560 | |
| 561 | // Decide where in Header to put the END_TRY. |
| 562 | BeforeSet.clear(); |
| 563 | AfterSet.clear(); |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 564 | for (const auto &MI : *Cont) { |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 565 | #ifndef NDEBUG |
| 566 | // END_TRY should precede existing LOOP markers. |
| 567 | if (MI.getOpcode() == WebAssembly::LOOP) |
| 568 | AfterSet.insert(&MI); |
| 569 | |
| 570 | // All END_TRY markers placed earlier belong to exceptions that contains |
| 571 | // this one. |
| 572 | if (MI.getOpcode() == WebAssembly::END_TRY) |
| 573 | AfterSet.insert(&MI); |
| 574 | #endif |
| 575 | |
| 576 | // If there is a previously placed END_LOOP marker and its header is after |
| 577 | // where TRY marker is, this loop is contained within the 'catch' part, so |
| 578 | // the END_TRY marker should go after that. Otherwise, the whole try-catch |
| 579 | // is contained within this loop, so the END_TRY should go before that. |
| 580 | if (MI.getOpcode() == WebAssembly::END_LOOP) { |
| 581 | if (EndToBegin[&MI]->getParent()->getNumber() >= Header->getNumber()) |
| 582 | BeforeSet.insert(&MI); |
| 583 | #ifndef NDEBUG |
| 584 | else |
| 585 | AfterSet.insert(&MI); |
| 586 | #endif |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Mark the end of the TRY. |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 591 | InsertPos = getEarliestInsertPos(Cont, BeforeSet, AfterSet); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 592 | MachineInstr *End = |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 593 | BuildMI(*Cont, InsertPos, Bottom->findBranchDebugLoc(), |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 594 | TII.get(WebAssembly::END_TRY)); |
| 595 | registerTryScope(Begin, End, &MBB); |
| 596 | |
| 597 | // Track the farthest-spanning scope that ends at this point. |
Heejin Ahn | 20cf074 | 2019-02-24 08:30:06 +0000 | [diff] [blame] | 598 | int Number = Cont->getNumber(); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 599 | if (!ScopeTops[Number] || |
| 600 | ScopeTops[Number]->getNumber() > Header->getNumber()) |
| 601 | ScopeTops[Number] = Header; |
| 602 | } |
| 603 | |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 604 | void WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) { |
| 605 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 606 | |
| 607 | // When there is an unconditional branch right before a catch instruction and |
| 608 | // it branches to the end of end_try marker, we don't need the branch, because |
| 609 | // it there is no exception, the control flow transfers to that point anyway. |
| 610 | // bb0: |
| 611 | // try |
| 612 | // ... |
| 613 | // br bb2 <- Not necessary |
| 614 | // bb1: |
| 615 | // catch |
| 616 | // ... |
| 617 | // bb2: |
| 618 | // end |
| 619 | for (auto &MBB : MF) { |
| 620 | if (!MBB.isEHPad()) |
| 621 | continue; |
| 622 | |
| 623 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
| 624 | SmallVector<MachineOperand, 4> Cond; |
| 625 | MachineBasicBlock *EHPadLayoutPred = |
| 626 | &*std::prev(MachineFunction::iterator(&MBB)); |
| 627 | MachineBasicBlock *Cont = BeginToEnd[EHPadToTry[&MBB]]->getParent(); |
| 628 | bool Analyzable = !TII.analyzeBranch(*EHPadLayoutPred, TBB, FBB, Cond); |
| 629 | if (Analyzable && ((Cond.empty() && TBB && TBB == Cont) || |
| 630 | (!Cond.empty() && FBB && FBB == Cont))) |
| 631 | TII.removeBranch(*EHPadLayoutPred); |
| 632 | } |
| 633 | |
| 634 | // When there are block / end_block markers that overlap with try / end_try |
| 635 | // markers, and the block and try markers' return types are the same, the |
| 636 | // block /end_block markers are not necessary, because try / end_try markers |
| 637 | // also can serve as boundaries for branches. |
| 638 | // block <- Not necessary |
| 639 | // try |
| 640 | // ... |
| 641 | // catch |
| 642 | // ... |
| 643 | // end |
| 644 | // end <- Not necessary |
| 645 | SmallVector<MachineInstr *, 32> ToDelete; |
| 646 | for (auto &MBB : MF) { |
| 647 | for (auto &MI : MBB) { |
| 648 | if (MI.getOpcode() != WebAssembly::TRY) |
| 649 | continue; |
| 650 | |
| 651 | MachineInstr *Try = &MI, *EndTry = BeginToEnd[Try]; |
| 652 | MachineBasicBlock *TryBB = Try->getParent(); |
| 653 | MachineBasicBlock *Cont = EndTry->getParent(); |
| 654 | int64_t RetType = Try->getOperand(0).getImm(); |
| 655 | for (auto B = MachineBasicBlock::iterator(Try), |
| 656 | E = std::next(MachineBasicBlock::iterator(EndTry)); |
| 657 | B != TryBB->begin() && E != Cont->end() && |
| 658 | std::prev(B)->getOpcode() == WebAssembly::BLOCK && |
| 659 | E->getOpcode() == WebAssembly::END_BLOCK && |
| 660 | std::prev(B)->getOperand(0).getImm() == RetType; |
| 661 | --B, ++E) { |
| 662 | ToDelete.push_back(&*std::prev(B)); |
| 663 | ToDelete.push_back(&*E); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | for (auto *MI : ToDelete) { |
| 668 | if (MI->getOpcode() == WebAssembly::BLOCK) |
| 669 | unregisterScope(MI); |
| 670 | MI->eraseFromParent(); |
| 671 | } |
| 672 | } |
| 673 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 674 | static unsigned |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 675 | getDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack, |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 676 | const MachineBasicBlock *MBB) { |
| 677 | unsigned Depth = 0; |
| 678 | for (auto X : reverse(Stack)) { |
| 679 | if (X == MBB) |
| 680 | break; |
| 681 | ++Depth; |
| 682 | } |
| 683 | assert(Depth < Stack.size() && "Branch destination should be in scope"); |
| 684 | return Depth; |
| 685 | } |
| 686 | |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 687 | /// In normal assembly languages, when the end of a function is unreachable, |
| 688 | /// because the function ends in an infinite loop or a noreturn call or similar, |
| 689 | /// it isn't necessary to worry about the function return type at the end of |
| 690 | /// the function, because it's never reached. However, in WebAssembly, blocks |
| 691 | /// that end at the function end need to have a return type signature that |
| 692 | /// matches the function signature, even though it's unreachable. This function |
| 693 | /// checks for such cases and fixes up the signatures. |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 694 | void WebAssemblyCFGStackify::fixEndsAtEndOfFunction(MachineFunction &MF) { |
| 695 | const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 696 | assert(MFI.getResults().size() <= 1); |
| 697 | |
| 698 | if (MFI.getResults().empty()) |
| 699 | return; |
| 700 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 701 | WebAssembly::ExprType RetType; |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 702 | switch (MFI.getResults().front().SimpleTy) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 703 | case MVT::i32: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 704 | RetType = WebAssembly::ExprType::I32; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 705 | break; |
| 706 | case MVT::i64: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 707 | RetType = WebAssembly::ExprType::I64; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 708 | break; |
| 709 | case MVT::f32: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 710 | RetType = WebAssembly::ExprType::F32; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 711 | break; |
| 712 | case MVT::f64: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 713 | RetType = WebAssembly::ExprType::F64; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 714 | break; |
Derek Schuff | 2c78385 | 2018-08-06 23:16:50 +0000 | [diff] [blame] | 715 | case MVT::v16i8: |
| 716 | case MVT::v8i16: |
| 717 | case MVT::v4i32: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 718 | case MVT::v2i64: |
Derek Schuff | 2c78385 | 2018-08-06 23:16:50 +0000 | [diff] [blame] | 719 | case MVT::v4f32: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 720 | case MVT::v2f64: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 721 | RetType = WebAssembly::ExprType::V128; |
Derek Schuff | 2c78385 | 2018-08-06 23:16:50 +0000 | [diff] [blame] | 722 | break; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 723 | case MVT::ExceptRef: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 724 | RetType = WebAssembly::ExprType::ExceptRef; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 725 | break; |
| 726 | default: |
| 727 | llvm_unreachable("unexpected return type"); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | for (MachineBasicBlock &MBB : reverse(MF)) { |
| 731 | for (MachineInstr &MI : reverse(MBB)) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 732 | if (MI.isPosition() || MI.isDebugInstr()) |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 733 | continue; |
| 734 | if (MI.getOpcode() == WebAssembly::END_BLOCK) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 735 | EndToBegin[&MI]->getOperand(0).setImm(int32_t(RetType)); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 736 | continue; |
| 737 | } |
| 738 | if (MI.getOpcode() == WebAssembly::END_LOOP) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 739 | EndToBegin[&MI]->getOperand(0).setImm(int32_t(RetType)); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 740 | continue; |
| 741 | } |
| 742 | // Something other than an `end`. We're done. |
| 743 | return; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 748 | // WebAssembly functions end with an end instruction, as if the function body |
| 749 | // were a block. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 750 | static void appendEndToFunction(MachineFunction &MF, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 751 | const WebAssemblyInstrInfo &TII) { |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 752 | BuildMI(MF.back(), MF.back().end(), |
| 753 | MF.back().findPrevDebugLoc(MF.back().end()), |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 754 | TII.get(WebAssembly::END_FUNCTION)); |
| 755 | } |
| 756 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 757 | /// Insert LOOP/TRY/BLOCK markers at appropriate places. |
| 758 | void WebAssemblyCFGStackify::placeMarkers(MachineFunction &MF) { |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 759 | // We allocate one more than the number of blocks in the function to |
| 760 | // accommodate for the possible fake block we may insert at the end. |
| 761 | ScopeTops.resize(MF.getNumBlockIDs() + 1); |
| 762 | // Place the LOOP for MBB if MBB is the header of a loop. |
| 763 | for (auto &MBB : MF) |
| 764 | placeLoopMarker(MBB); |
| 765 | // Place the TRY for MBB if MBB is the EH pad of an exception. |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 766 | const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo(); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 767 | if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm && |
| 768 | MF.getFunction().hasPersonalityFn()) |
| 769 | for (auto &MBB : MF) |
| 770 | placeTryMarker(MBB); |
| 771 | // Place the BLOCK for MBB if MBB is branched to from above. |
| 772 | for (auto &MBB : MF) |
| 773 | placeBlockMarker(MBB); |
| 774 | } |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 775 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 776 | void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) { |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 777 | // Now rewrite references to basic blocks to be depth immediates. |
| 778 | SmallVector<const MachineBasicBlock *, 8> Stack; |
| 779 | for (auto &MBB : reverse(MF)) { |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 780 | for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) { |
| 781 | MachineInstr &MI = *I; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 782 | switch (MI.getOpcode()) { |
| 783 | case WebAssembly::BLOCK: |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 784 | case WebAssembly::TRY: |
| 785 | assert(ScopeTops[Stack.back()->getNumber()]->getNumber() <= |
| 786 | MBB.getNumber() && |
| 787 | "Block/try marker should be balanced"); |
| 788 | Stack.pop_back(); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 789 | break; |
| 790 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 791 | case WebAssembly::LOOP: |
| 792 | assert(Stack.back() == &MBB && "Loop top should be balanced"); |
| 793 | Stack.pop_back(); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 794 | break; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 795 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 796 | case WebAssembly::END_BLOCK: |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 797 | case WebAssembly::END_TRY: |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 798 | Stack.push_back(&MBB); |
| 799 | break; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 800 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 801 | case WebAssembly::END_LOOP: |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 802 | Stack.push_back(EndToBegin[&MI]->getParent()); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 803 | break; |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 804 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 805 | default: |
| 806 | if (MI.isTerminator()) { |
| 807 | // Rewrite MBB operands to be depth immediates. |
| 808 | SmallVector<MachineOperand, 4> Ops(MI.operands()); |
| 809 | while (MI.getNumOperands() > 0) |
| 810 | MI.RemoveOperand(MI.getNumOperands() - 1); |
| 811 | for (auto MO : Ops) { |
| 812 | if (MO.isMBB()) |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 813 | MO = MachineOperand::CreateImm(getDepth(Stack, MO.getMBB())); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 814 | MI.addOperand(MF, MO); |
| 815 | } |
| 816 | } |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | assert(Stack.empty() && "Control flow should be balanced"); |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 822 | } |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 823 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 824 | void WebAssemblyCFGStackify::releaseMemory() { |
| 825 | ScopeTops.clear(); |
| 826 | BeginToEnd.clear(); |
| 827 | EndToBegin.clear(); |
| 828 | TryToEHPad.clear(); |
| 829 | EHPadToTry.clear(); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 830 | } |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 831 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 832 | bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 833 | LLVM_DEBUG(dbgs() << "********** CFG Stackifying **********\n" |
| 834 | "********** Function: " |
| 835 | << MF.getName() << '\n'); |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 836 | const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 837 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 838 | releaseMemory(); |
| 839 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 840 | // Liveness is not tracked for VALUE_STACK physreg. |
Derek Schuff | 9c3bf31 | 2016-01-13 17:10:28 +0000 | [diff] [blame] | 841 | MF.getRegInfo().invalidateLiveness(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 842 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 843 | // Place the BLOCK/LOOP/TRY markers to indicate the beginnings of scopes. |
| 844 | placeMarkers(MF); |
| 845 | |
Heejin Ahn | cf699b4 | 2019-02-27 00:50:53 +0000 | [diff] [blame] | 846 | if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm && |
| 847 | MF.getFunction().hasPersonalityFn()) |
| 848 | // Remove unnecessary instructions. |
| 849 | removeUnnecessaryInstrs(MF); |
| 850 | |
Heejin Ahn | e76fa9e | 2018-08-16 23:50:59 +0000 | [diff] [blame] | 851 | // Convert MBB operands in terminators to relative depth immediates. |
| 852 | rewriteDepthImmediates(MF); |
| 853 | |
| 854 | // Fix up block/loop/try signatures at the end of the function to conform to |
| 855 | // WebAssembly's rules. |
| 856 | fixEndsAtEndOfFunction(MF); |
| 857 | |
| 858 | // Add an end instruction at the end of the function body. |
| 859 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 860 | if (!MF.getSubtarget<WebAssemblySubtarget>() |
| 861 | .getTargetTriple() |
| 862 | .isOSBinFormatELF()) |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 863 | appendEndToFunction(MF, TII); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 864 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 865 | return true; |
| 866 | } |