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