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 | /// |
Dan Gohman | f52ee17 | 2017-02-27 22:38:58 +0000 | [diff] [blame] | 13 | /// This pass inserts BLOCK and LOOP markers to mark the start of scopes, since |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 14 | /// scope boundaries serve as the labels for WebAssembly's control transfers. |
| 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 | /// |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 21 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "WebAssembly.h" |
Dan Gohman | ed0f113 | 2016-01-30 05:01:06 +0000 | [diff] [blame] | 23 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 24 | #include "WebAssemblySubtarget.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 25 | #include "WebAssemblyUtilities.h" |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineDominators.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Derek Schuff | 9c3bf31 | 2016-01-13 17:10:28 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | using namespace llvm; |
| 35 | |
| 36 | #define DEBUG_TYPE "wasm-cfg-stackify" |
| 37 | |
| 38 | namespace { |
| 39 | class WebAssemblyCFGStackify final : public MachineFunctionPass { |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 40 | StringRef getPassName() const override { return "WebAssembly CFG Stackify"; } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 41 | |
| 42 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 43 | AU.setPreservesCFG(); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 44 | AU.addRequired<MachineDominatorTree>(); |
| 45 | AU.addPreserved<MachineDominatorTree>(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 46 | AU.addRequired<MachineLoopInfo>(); |
| 47 | AU.addPreserved<MachineLoopInfo>(); |
| 48 | MachineFunctionPass::getAnalysisUsage(AU); |
| 49 | } |
| 50 | |
| 51 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 52 | |
| 53 | public: |
| 54 | static char ID; // Pass identification, replacement for typeid |
| 55 | WebAssemblyCFGStackify() : MachineFunctionPass(ID) {} |
| 56 | }; |
| 57 | } // end anonymous namespace |
| 58 | |
| 59 | char WebAssemblyCFGStackify::ID = 0; |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 60 | INITIALIZE_PASS(WebAssemblyCFGStackify, DEBUG_TYPE, |
| 61 | "Insert BLOCK and LOOP markers for WebAssembly scopes", |
| 62 | false, false) |
| 63 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 64 | FunctionPass *llvm::createWebAssemblyCFGStackify() { |
| 65 | return new WebAssemblyCFGStackify(); |
| 66 | } |
| 67 | |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 68 | /// Test whether Pred has any terminators explicitly branching to MBB, as |
| 69 | /// opposed to falling through. Note that it's possible (eg. in unoptimized |
| 70 | /// code) for a branch instruction to both branch to a block and fallthrough |
| 71 | /// to it, so we check the actual branch operands to see if there are any |
| 72 | /// explicit mentions. |
Dan Gohman | 35e4a28 | 2016-01-08 01:06:00 +0000 | [diff] [blame] | 73 | static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred, |
| 74 | MachineBasicBlock *MBB) { |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 75 | for (MachineInstr &MI : Pred->terminators()) |
| 76 | for (MachineOperand &MO : MI.explicit_operands()) |
| 77 | if (MO.isMBB() && MO.getMBB() == MBB) |
| 78 | return true; |
| 79 | return false; |
| 80 | } |
| 81 | |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 82 | /// Insert a BLOCK marker for branches to MBB (if needed). |
Dan Gohman | 3a643e8 | 2016-10-06 22:10:23 +0000 | [diff] [blame] | 83 | static void PlaceBlockMarker( |
| 84 | MachineBasicBlock &MBB, MachineFunction &MF, |
| 85 | SmallVectorImpl<MachineBasicBlock *> &ScopeTops, |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 86 | DenseMap<const MachineInstr *, MachineInstr *> &BlockTops, |
| 87 | DenseMap<const MachineInstr *, MachineInstr *> &LoopTops, |
Dan Gohman | 3a643e8 | 2016-10-06 22:10:23 +0000 | [diff] [blame] | 88 | const WebAssemblyInstrInfo &TII, |
| 89 | const MachineLoopInfo &MLI, |
| 90 | MachineDominatorTree &MDT, |
| 91 | WebAssemblyFunctionInfo &MFI) { |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 92 | // First compute the nearest common dominator of all forward non-fallthrough |
| 93 | // predecessors so that we minimize the time that the BLOCK is on the stack, |
| 94 | // which reduces overall stack height. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 95 | MachineBasicBlock *Header = nullptr; |
| 96 | bool IsBranchedTo = false; |
| 97 | int MBBNumber = MBB.getNumber(); |
| 98 | for (MachineBasicBlock *Pred : MBB.predecessors()) |
| 99 | if (Pred->getNumber() < MBBNumber) { |
| 100 | Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred; |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 101 | if (ExplicitlyBranchesTo(Pred, &MBB)) |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 102 | IsBranchedTo = true; |
| 103 | } |
| 104 | if (!Header) |
| 105 | return; |
| 106 | if (!IsBranchedTo) |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 107 | return; |
| 108 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 109 | assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors"); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 110 | MachineBasicBlock *LayoutPred = &*std::prev(MachineFunction::iterator(&MBB)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 111 | |
| 112 | // If the nearest common dominator is inside a more deeply nested context, |
| 113 | // walk out to the nearest scope which isn't more deeply nested. |
| 114 | for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) { |
| 115 | if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) { |
| 116 | if (ScopeTop->getNumber() > Header->getNumber()) { |
| 117 | // Skip over an intervening scope. |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 118 | I = std::next(MachineFunction::iterator(ScopeTop)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 119 | } else { |
| 120 | // We found a scope level at an appropriate depth. |
| 121 | Header = ScopeTop; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 127 | // Decide where in Header to put the BLOCK. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 128 | MachineBasicBlock::iterator InsertPos; |
| 129 | MachineLoop *HeaderLoop = MLI.getLoopFor(Header); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 130 | if (HeaderLoop && MBB.getNumber() > LoopBottom(HeaderLoop)->getNumber()) { |
| 131 | // Header is the header of a loop that does not lexically contain MBB, so |
Dan Gohman | a187ab2 | 2016-02-12 21:19:25 +0000 | [diff] [blame] | 132 | // the BLOCK needs to be above the LOOP, after any END constructs. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 133 | InsertPos = Header->begin(); |
Dan Gohman | 3a643e8 | 2016-10-06 22:10:23 +0000 | [diff] [blame] | 134 | while (InsertPos->getOpcode() == WebAssembly::END_BLOCK || |
| 135 | InsertPos->getOpcode() == WebAssembly::END_LOOP) |
Dan Gohman | a187ab2 | 2016-02-12 21:19:25 +0000 | [diff] [blame] | 136 | ++InsertPos; |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 137 | } else { |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 138 | // Otherwise, insert the BLOCK as late in Header as we can, but before the |
| 139 | // beginning of the local expression tree and any nested BLOCKs. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 140 | InsertPos = Header->getFirstTerminator(); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 141 | while (InsertPos != Header->begin() && |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 142 | WebAssembly::isChild(*std::prev(InsertPos), MFI) && |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 143 | std::prev(InsertPos)->getOpcode() != WebAssembly::LOOP && |
| 144 | std::prev(InsertPos)->getOpcode() != WebAssembly::END_BLOCK && |
| 145 | std::prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP) |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 146 | --InsertPos; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 147 | } |
Heejin Ahn | 2b8158f | 2018-04-17 21:19:21 +0000 | [diff] [blame] | 148 | // The header block in which a 'block' mark will be inserted should have a |
| 149 | // terminator because it is branching to a non-layout successor. |
| 150 | assert(InsertPos != Header->end()); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 151 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 152 | // Add the BLOCK. |
Heejin Ahn | 92401cc | 2018-04-14 00:12:12 +0000 | [diff] [blame] | 153 | MachineInstr *Begin = |
| 154 | BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos), |
| 155 | TII.get(WebAssembly::BLOCK)) |
| 156 | .addImm(int64_t(WebAssembly::ExprType::Void)); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 157 | |
| 158 | // Mark the end of the block. |
| 159 | InsertPos = MBB.begin(); |
| 160 | while (InsertPos != MBB.end() && |
Dan Gohman | 3a643e8 | 2016-10-06 22:10:23 +0000 | [diff] [blame] | 161 | InsertPos->getOpcode() == WebAssembly::END_LOOP && |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 162 | LoopTops[&*InsertPos]->getParent()->getNumber() >= Header->getNumber()) |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 163 | ++InsertPos; |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 164 | MachineInstr *End = BuildMI(MBB, InsertPos, MBB.findPrevDebugLoc(InsertPos), |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 165 | TII.get(WebAssembly::END_BLOCK)); |
| 166 | BlockTops[End] = Begin; |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 167 | |
| 168 | // Track the farthest-spanning scope that ends at this point. |
| 169 | int Number = MBB.getNumber(); |
| 170 | if (!ScopeTops[Number] || |
| 171 | ScopeTops[Number]->getNumber() > Header->getNumber()) |
| 172 | ScopeTops[Number] = Header; |
| 173 | } |
| 174 | |
| 175 | /// Insert a LOOP marker for a loop starting at MBB (if it's a loop header). |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 176 | static void PlaceLoopMarker( |
| 177 | MachineBasicBlock &MBB, MachineFunction &MF, |
| 178 | SmallVectorImpl<MachineBasicBlock *> &ScopeTops, |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 179 | DenseMap<const MachineInstr *, MachineInstr *> &LoopTops, |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 180 | const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI) { |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 181 | MachineLoop *Loop = MLI.getLoopFor(&MBB); |
| 182 | if (!Loop || Loop->getHeader() != &MBB) |
| 183 | return; |
| 184 | |
| 185 | // The operand of a LOOP is the first block after the loop. If the loop is the |
| 186 | // bottom of the function, insert a dummy block at the end. |
| 187 | MachineBasicBlock *Bottom = LoopBottom(Loop); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 188 | auto Iter = std::next(MachineFunction::iterator(Bottom)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 189 | if (Iter == MF.end()) { |
| 190 | MachineBasicBlock *Label = MF.CreateMachineBasicBlock(); |
| 191 | // Give it a fake predecessor so that AsmPrinter prints its label. |
| 192 | Label->addSuccessor(Label); |
| 193 | MF.push_back(Label); |
Benjamin Kramer | ef0a45a | 2016-09-05 12:06:47 +0000 | [diff] [blame] | 194 | Iter = std::next(MachineFunction::iterator(Bottom)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 195 | } |
| 196 | MachineBasicBlock *AfterLoop = &*Iter; |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 197 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 198 | // Mark the beginning of the loop (after the end of any existing loop that |
| 199 | // ends here). |
| 200 | auto InsertPos = MBB.begin(); |
| 201 | while (InsertPos != MBB.end() && |
| 202 | InsertPos->getOpcode() == WebAssembly::END_LOOP) |
| 203 | ++InsertPos; |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 204 | MachineInstr *Begin = BuildMI(MBB, InsertPos, MBB.findDebugLoc(InsertPos), |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 205 | TII.get(WebAssembly::LOOP)) |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 206 | .addImm(int64_t(WebAssembly::ExprType::Void)); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 207 | |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 208 | // Mark the end of the loop (using arbitrary debug location that branched |
| 209 | // to the loop end as its location). |
| 210 | DebugLoc EndDL = (*AfterLoop->pred_rbegin())->findBranchDebugLoc(); |
| 211 | MachineInstr *End = BuildMI(*AfterLoop, AfterLoop->begin(), EndDL, |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 212 | TII.get(WebAssembly::END_LOOP)); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 213 | LoopTops[End] = Begin; |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 214 | |
| 215 | assert((!ScopeTops[AfterLoop->getNumber()] || |
| 216 | ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) && |
Dan Gohman | 442bfce | 2016-02-16 16:22:41 +0000 | [diff] [blame] | 217 | "With block sorting the outermost loop for a block should be first."); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 218 | if (!ScopeTops[AfterLoop->getNumber()]) |
| 219 | ScopeTops[AfterLoop->getNumber()] = &MBB; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 222 | static unsigned |
| 223 | GetDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack, |
| 224 | const MachineBasicBlock *MBB) { |
| 225 | unsigned Depth = 0; |
| 226 | for (auto X : reverse(Stack)) { |
| 227 | if (X == MBB) |
| 228 | break; |
| 229 | ++Depth; |
| 230 | } |
| 231 | assert(Depth < Stack.size() && "Branch destination should be in scope"); |
| 232 | return Depth; |
| 233 | } |
| 234 | |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 235 | /// In normal assembly languages, when the end of a function is unreachable, |
| 236 | /// because the function ends in an infinite loop or a noreturn call or similar, |
| 237 | /// it isn't necessary to worry about the function return type at the end of |
| 238 | /// the function, because it's never reached. However, in WebAssembly, blocks |
| 239 | /// that end at the function end need to have a return type signature that |
| 240 | /// matches the function signature, even though it's unreachable. This function |
| 241 | /// checks for such cases and fixes up the signatures. |
| 242 | static void FixEndsAtEndOfFunction( |
| 243 | MachineFunction &MF, |
| 244 | const WebAssemblyFunctionInfo &MFI, |
| 245 | DenseMap<const MachineInstr *, MachineInstr *> &BlockTops, |
| 246 | DenseMap<const MachineInstr *, MachineInstr *> &LoopTops) { |
| 247 | assert(MFI.getResults().size() <= 1); |
| 248 | |
| 249 | if (MFI.getResults().empty()) |
| 250 | return; |
| 251 | |
| 252 | WebAssembly::ExprType retType; |
| 253 | switch (MFI.getResults().front().SimpleTy) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 254 | case MVT::i32: retType = WebAssembly::ExprType::I32; break; |
| 255 | case MVT::i64: retType = WebAssembly::ExprType::I64; break; |
| 256 | case MVT::f32: retType = WebAssembly::ExprType::F32; break; |
| 257 | case MVT::f64: retType = WebAssembly::ExprType::F64; break; |
| 258 | case MVT::v16i8: retType = WebAssembly::ExprType::I8x16; break; |
| 259 | case MVT::v8i16: retType = WebAssembly::ExprType::I16x8; break; |
| 260 | case MVT::v4i32: retType = WebAssembly::ExprType::I32x4; break; |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 261 | case MVT::v4f32: retType = WebAssembly::ExprType::F32x4; break; |
Heejin Ahn | 0de5872 | 2018-03-08 04:05:37 +0000 | [diff] [blame] | 262 | case MVT::ExceptRef: retType = WebAssembly::ExprType::ExceptRef; break; |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 263 | default: llvm_unreachable("unexpected return type"); |
| 264 | } |
| 265 | |
| 266 | for (MachineBasicBlock &MBB : reverse(MF)) { |
| 267 | for (MachineInstr &MI : reverse(MBB)) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 268 | if (MI.isPosition() || MI.isDebugInstr()) |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 269 | continue; |
| 270 | if (MI.getOpcode() == WebAssembly::END_BLOCK) { |
| 271 | BlockTops[&MI]->getOperand(0).setImm(int32_t(retType)); |
| 272 | continue; |
| 273 | } |
| 274 | if (MI.getOpcode() == WebAssembly::END_LOOP) { |
| 275 | LoopTops[&MI]->getOperand(0).setImm(int32_t(retType)); |
| 276 | continue; |
| 277 | } |
| 278 | // Something other than an `end`. We're done. |
| 279 | return; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 284 | // WebAssembly functions end with an end instruction, as if the function body |
| 285 | // were a block. |
| 286 | static void AppendEndToFunction( |
| 287 | MachineFunction &MF, |
| 288 | const WebAssemblyInstrInfo &TII) { |
Derek Schuff | 10b3135 | 2018-03-15 22:06:51 +0000 | [diff] [blame] | 289 | BuildMI(MF.back(), MF.back().end(), |
| 290 | MF.back().findPrevDebugLoc(MF.back().end()), |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 291 | TII.get(WebAssembly::END_FUNCTION)); |
| 292 | } |
| 293 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 294 | /// Insert LOOP and BLOCK markers at appropriate places. |
| 295 | static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI, |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 296 | const WebAssemblyInstrInfo &TII, |
Dan Gohman | ed0f113 | 2016-01-30 05:01:06 +0000 | [diff] [blame] | 297 | MachineDominatorTree &MDT, |
| 298 | WebAssemblyFunctionInfo &MFI) { |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 299 | // For each block whose label represents the end of a scope, record the block |
| 300 | // which holds the beginning of the scope. This will allow us to quickly skip |
| 301 | // over scoped regions when walking blocks. We allocate one more than the |
| 302 | // number of blocks in the function to accommodate for the possible fake block |
| 303 | // we may insert at the end. |
| 304 | SmallVector<MachineBasicBlock *, 8> ScopeTops(MF.getNumBlockIDs() + 1); |
| 305 | |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 306 | // For each LOOP_END, the corresponding LOOP. |
| 307 | DenseMap<const MachineInstr *, MachineInstr *> LoopTops; |
| 308 | |
| 309 | // For each END_BLOCK, the corresponding BLOCK. |
| 310 | DenseMap<const MachineInstr *, MachineInstr *> BlockTops; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 311 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 312 | for (auto &MBB : MF) { |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 313 | // Place the LOOP for MBB if MBB is the header of a loop. |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 314 | PlaceLoopMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 315 | |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 316 | // Place the BLOCK for MBB if MBB is branched to from above. |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 317 | PlaceBlockMarker(MBB, MF, ScopeTops, BlockTops, LoopTops, TII, MLI, MDT, MFI); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 318 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 319 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 320 | // Now rewrite references to basic blocks to be depth immediates. |
| 321 | SmallVector<const MachineBasicBlock *, 8> Stack; |
| 322 | for (auto &MBB : reverse(MF)) { |
| 323 | for (auto &MI : reverse(MBB)) { |
| 324 | switch (MI.getOpcode()) { |
| 325 | case WebAssembly::BLOCK: |
Dan Gohman | 3a643e8 | 2016-10-06 22:10:23 +0000 | [diff] [blame] | 326 | assert(ScopeTops[Stack.back()->getNumber()]->getNumber() <= MBB.getNumber() && |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 327 | "Block should be balanced"); |
| 328 | Stack.pop_back(); |
| 329 | break; |
| 330 | case WebAssembly::LOOP: |
| 331 | assert(Stack.back() == &MBB && "Loop top should be balanced"); |
| 332 | Stack.pop_back(); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 333 | break; |
| 334 | case WebAssembly::END_BLOCK: |
| 335 | Stack.push_back(&MBB); |
| 336 | break; |
| 337 | case WebAssembly::END_LOOP: |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 338 | Stack.push_back(LoopTops[&MI]->getParent()); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 339 | break; |
| 340 | default: |
| 341 | if (MI.isTerminator()) { |
| 342 | // Rewrite MBB operands to be depth immediates. |
| 343 | SmallVector<MachineOperand, 4> Ops(MI.operands()); |
| 344 | while (MI.getNumOperands() > 0) |
| 345 | MI.RemoveOperand(MI.getNumOperands() - 1); |
| 346 | for (auto MO : Ops) { |
| 347 | if (MO.isMBB()) |
| 348 | MO = MachineOperand::CreateImm(GetDepth(Stack, MO.getMBB())); |
| 349 | MI.addOperand(MF, MO); |
| 350 | } |
| 351 | } |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | assert(Stack.empty() && "Control flow should be balanced"); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 357 | |
| 358 | // Fix up block/loop signatures at the end of the function to conform to |
| 359 | // WebAssembly's rules. |
| 360 | FixEndsAtEndOfFunction(MF, MFI, BlockTops, LoopTops); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 361 | |
| 362 | // Add an end instruction at the end of the function body. |
| 363 | if (!MF.getSubtarget<WebAssemblySubtarget>() |
| 364 | .getTargetTriple().isOSBinFormatELF()) |
| 365 | AppendEndToFunction(MF, TII); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 366 | } |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 367 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 368 | bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 369 | LLVM_DEBUG(dbgs() << "********** CFG Stackifying **********\n" |
| 370 | "********** Function: " |
| 371 | << MF.getName() << '\n'); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 372 | |
| 373 | const auto &MLI = getAnalysis<MachineLoopInfo>(); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 374 | auto &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 375 | // Liveness is not tracked for VALUE_STACK physreg. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 376 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Dan Gohman | ed0f113 | 2016-01-30 05:01:06 +0000 | [diff] [blame] | 377 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
Derek Schuff | 9c3bf31 | 2016-01-13 17:10:28 +0000 | [diff] [blame] | 378 | MF.getRegInfo().invalidateLiveness(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 379 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 380 | // Place the BLOCK and LOOP markers to indicate the beginnings of scopes. |
Dan Gohman | ed0f113 | 2016-01-30 05:01:06 +0000 | [diff] [blame] | 381 | PlaceMarkers(MF, MLI, TII, MDT, MFI); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 382 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 383 | return true; |
| 384 | } |