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 |
| 11 | /// \brief This file implements a CFG stacking pass. |
| 12 | /// |
| 13 | /// This pass reorders the blocks in a function to put them into a reverse |
| 14 | /// post-order [0], with special care to keep the order as similar as possible |
| 15 | /// to the original order, and to keep loops contiguous even in the case of |
| 16 | /// split backedges. |
| 17 | /// |
| 18 | /// Then, it inserts BLOCK and LOOP markers to mark the start of scopes, since |
| 19 | /// scope boundaries serve as the labels for WebAssembly's control transfers. |
| 20 | /// |
| 21 | /// This is sufficient to convert arbitrary CFGs into a form that works on |
| 22 | /// WebAssembly, provided that all loops are single-entry. |
| 23 | /// |
| 24 | /// [0] https://en.wikipedia.org/wiki/Depth-first_search#Vertex_orderings |
| 25 | /// |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "WebAssembly.h" |
| 29 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 30 | #include "WebAssemblySubtarget.h" |
| 31 | #include "llvm/ADT/SCCIterator.h" |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SetVector.h" |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineDominators.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineFunction.h" |
| 35 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 36 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 37 | #include "llvm/CodeGen/Passes.h" |
| 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | using namespace llvm; |
| 41 | |
| 42 | #define DEBUG_TYPE "wasm-cfg-stackify" |
| 43 | |
| 44 | namespace { |
| 45 | class WebAssemblyCFGStackify final : public MachineFunctionPass { |
| 46 | const char *getPassName() const override { |
| 47 | return "WebAssembly CFG Stackify"; |
| 48 | } |
| 49 | |
| 50 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 51 | AU.setPreservesCFG(); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 52 | AU.addRequired<MachineDominatorTree>(); |
| 53 | AU.addPreserved<MachineDominatorTree>(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 54 | AU.addRequired<MachineLoopInfo>(); |
| 55 | AU.addPreserved<MachineLoopInfo>(); |
| 56 | MachineFunctionPass::getAnalysisUsage(AU); |
| 57 | } |
| 58 | |
| 59 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 60 | |
| 61 | public: |
| 62 | static char ID; // Pass identification, replacement for typeid |
| 63 | WebAssemblyCFGStackify() : MachineFunctionPass(ID) {} |
| 64 | }; |
| 65 | } // end anonymous namespace |
| 66 | |
| 67 | char WebAssemblyCFGStackify::ID = 0; |
| 68 | FunctionPass *llvm::createWebAssemblyCFGStackify() { |
| 69 | return new WebAssemblyCFGStackify(); |
| 70 | } |
| 71 | |
| 72 | static void EliminateMultipleEntryLoops(MachineFunction &MF, |
| 73 | const MachineLoopInfo &MLI) { |
| 74 | SmallPtrSet<MachineBasicBlock *, 8> InSet; |
| 75 | for (scc_iterator<MachineFunction *> I = scc_begin(&MF), E = scc_end(&MF); |
| 76 | I != E; ++I) { |
| 77 | const std::vector<MachineBasicBlock *> &CurrentSCC = *I; |
| 78 | |
| 79 | // Skip trivial SCCs. |
| 80 | if (CurrentSCC.size() == 1) |
| 81 | continue; |
| 82 | |
| 83 | InSet.insert(CurrentSCC.begin(), CurrentSCC.end()); |
| 84 | MachineBasicBlock *Header = nullptr; |
| 85 | for (MachineBasicBlock *MBB : CurrentSCC) { |
| 86 | for (MachineBasicBlock *Pred : MBB->predecessors()) { |
| 87 | if (InSet.count(Pred)) |
| 88 | continue; |
| 89 | if (!Header) { |
| 90 | Header = MBB; |
| 91 | break; |
| 92 | } |
| 93 | // TODO: Implement multiple-entry loops. |
| 94 | report_fatal_error("multiple-entry loops are not supported yet"); |
| 95 | } |
| 96 | } |
| 97 | assert(MLI.isLoopHeader(Header)); |
| 98 | |
| 99 | InSet.clear(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | namespace { |
| 104 | /// Post-order traversal stack entry. |
| 105 | struct POStackEntry { |
| 106 | MachineBasicBlock *MBB; |
| 107 | SmallVector<MachineBasicBlock *, 0> Succs; |
| 108 | |
| 109 | POStackEntry(MachineBasicBlock *MBB, MachineFunction &MF, |
| 110 | const MachineLoopInfo &MLI); |
| 111 | }; |
| 112 | } // end anonymous namespace |
| 113 | |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 114 | static bool LoopContains(const MachineLoop *Loop, |
| 115 | const MachineBasicBlock *MBB) { |
| 116 | return Loop ? Loop->contains(MBB) : true; |
| 117 | } |
| 118 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 119 | POStackEntry::POStackEntry(MachineBasicBlock *MBB, MachineFunction &MF, |
| 120 | const MachineLoopInfo &MLI) |
| 121 | : MBB(MBB), Succs(MBB->successors()) { |
| 122 | // RPO is not a unique form, since at every basic block with multiple |
| 123 | // successors, the DFS has to pick which order to visit the successors in. |
| 124 | // Sort them strategically (see below). |
| 125 | MachineLoop *Loop = MLI.getLoopFor(MBB); |
| 126 | MachineFunction::iterator Next = next(MachineFunction::iterator(MBB)); |
| 127 | MachineBasicBlock *LayoutSucc = Next == MF.end() ? nullptr : &*Next; |
| 128 | std::stable_sort( |
| 129 | Succs.begin(), Succs.end(), |
| 130 | [=, &MLI](const MachineBasicBlock *A, const MachineBasicBlock *B) { |
| 131 | if (A == B) |
| 132 | return false; |
| 133 | |
| 134 | // Keep loops contiguous by preferring the block that's in the same |
| 135 | // loop. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 136 | bool LoopContainsA = LoopContains(Loop, A); |
| 137 | bool LoopContainsB = LoopContains(Loop, B); |
| 138 | if (LoopContainsA && !LoopContainsB) |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 139 | return true; |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 140 | if (!LoopContainsA && LoopContainsB) |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 141 | return false; |
| 142 | |
| 143 | // Minimize perturbation by preferring the block which is the immediate |
| 144 | // layout successor. |
| 145 | if (A == LayoutSucc) |
| 146 | return true; |
| 147 | if (B == LayoutSucc) |
| 148 | return false; |
| 149 | |
| 150 | // TODO: More sophisticated orderings may be profitable here. |
| 151 | |
| 152 | return false; |
| 153 | }); |
| 154 | } |
| 155 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 156 | /// Return the "bottom" block of a loop. This differs from |
| 157 | /// MachineLoop::getBottomBlock in that it works even if the loop is |
| 158 | /// discontiguous. |
| 159 | static MachineBasicBlock *LoopBottom(const MachineLoop *Loop) { |
| 160 | MachineBasicBlock *Bottom = Loop->getHeader(); |
| 161 | for (MachineBasicBlock *MBB : Loop->blocks()) |
| 162 | if (MBB->getNumber() > Bottom->getNumber()) |
| 163 | Bottom = MBB; |
| 164 | return Bottom; |
| 165 | } |
| 166 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 167 | /// Sort the blocks in RPO, taking special care to make sure that loops are |
| 168 | /// contiguous even in the case of split backedges. |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 169 | /// |
| 170 | /// TODO: Determine whether RPO is actually worthwhile, or whether we should |
| 171 | /// move to just a stable-topological-sort-based approach that would preserve |
| 172 | /// more of the original order. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 173 | static void SortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI) { |
| 174 | // Note that we do our own RPO rather than using |
| 175 | // "llvm/ADT/PostOrderIterator.h" because we want control over the order that |
| 176 | // successors are visited in (see above). Also, we can sort the blocks in the |
| 177 | // MachineFunction as we go. |
| 178 | SmallPtrSet<MachineBasicBlock *, 16> Visited; |
| 179 | SmallVector<POStackEntry, 16> Stack; |
| 180 | |
Dan Gohman | 96029f7 | 2015-11-30 18:42:08 +0000 | [diff] [blame] | 181 | MachineBasicBlock *EntryBlock = &*MF.begin(); |
| 182 | Visited.insert(EntryBlock); |
| 183 | Stack.push_back(POStackEntry(EntryBlock, MF, MLI)); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 184 | |
| 185 | for (;;) { |
| 186 | POStackEntry &Entry = Stack.back(); |
| 187 | SmallVectorImpl<MachineBasicBlock *> &Succs = Entry.Succs; |
| 188 | if (!Succs.empty()) { |
| 189 | MachineBasicBlock *Succ = Succs.pop_back_val(); |
| 190 | if (Visited.insert(Succ).second) |
| 191 | Stack.push_back(POStackEntry(Succ, MF, MLI)); |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | // Put the block in its position in the MachineFunction. |
| 196 | MachineBasicBlock &MBB = *Entry.MBB; |
Nico Weber | 0040647 | 2015-11-07 02:47:31 +0000 | [diff] [blame] | 197 | MBB.moveBefore(&*MF.begin()); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 198 | |
| 199 | // Branch instructions may utilize a fallthrough, so update them if a |
| 200 | // fallthrough has been added or removed. |
| 201 | if (!MBB.empty() && MBB.back().isTerminator() && !MBB.back().isBranch() && |
| 202 | !MBB.back().isBarrier()) |
| 203 | report_fatal_error( |
| 204 | "Non-branch terminator with fallthrough cannot yet be rewritten"); |
| 205 | if (MBB.empty() || !MBB.back().isTerminator() || MBB.back().isBranch()) |
| 206 | MBB.updateTerminator(); |
| 207 | |
| 208 | Stack.pop_back(); |
| 209 | if (Stack.empty()) |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | // Now that we've sorted the blocks in RPO, renumber them. |
| 214 | MF.RenumberBlocks(); |
| 215 | |
| 216 | #ifndef NDEBUG |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 217 | SmallSetVector<MachineLoop *, 8> OnStack; |
| 218 | |
| 219 | // Insert a sentinel representing the degenerate loop that starts at the |
| 220 | // function entry block and includes the entire function as a "loop" that |
| 221 | // executes once. |
| 222 | OnStack.insert(nullptr); |
| 223 | |
| 224 | for (auto &MBB : MF) { |
| 225 | assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative."); |
| 226 | |
| 227 | MachineLoop *Loop = MLI.getLoopFor(&MBB); |
| 228 | if (Loop && &MBB == Loop->getHeader()) { |
| 229 | // Loop header. The loop predecessor should be sorted above, and the other |
| 230 | // predecessors should be backedges below. |
| 231 | for (auto Pred : MBB.predecessors()) |
| 232 | assert( |
| 233 | (Pred->getNumber() < MBB.getNumber() || Loop->contains(Pred)) && |
| 234 | "Loop header predecessors must be loop predecessors or backedges"); |
| 235 | assert(OnStack.insert(Loop) && "Loops should be declared at most once."); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 236 | } else { |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 237 | // Not a loop header. All predecessors should be sorted above. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 238 | for (auto Pred : MBB.predecessors()) |
| 239 | assert(Pred->getNumber() < MBB.getNumber() && |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 240 | "Non-loop-header predecessors should be topologically sorted"); |
| 241 | assert(OnStack.count(MLI.getLoopFor(&MBB)) && |
| 242 | "Blocks must be nested in their loops"); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 243 | } |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 244 | while (OnStack.size() > 1 && &MBB == LoopBottom(OnStack.back())) |
| 245 | OnStack.pop_back(); |
| 246 | } |
| 247 | assert(OnStack.pop_back_val() == nullptr && |
| 248 | "The function entry block shouldn't actually be a loop header"); |
| 249 | assert(OnStack.empty() && |
| 250 | "Control flow stack pushes and pops should be balanced."); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 251 | #endif |
| 252 | } |
| 253 | |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 254 | /// Test whether Pred has any terminators explicitly branching to MBB, as |
| 255 | /// opposed to falling through. Note that it's possible (eg. in unoptimized |
| 256 | /// code) for a branch instruction to both branch to a block and fallthrough |
| 257 | /// to it, so we check the actual branch operands to see if there are any |
| 258 | /// explicit mentions. |
Dan Gohman | 35e4a28 | 2016-01-08 01:06:00 +0000 | [diff] [blame] | 259 | static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred, |
| 260 | MachineBasicBlock *MBB) { |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 261 | for (MachineInstr &MI : Pred->terminators()) |
| 262 | for (MachineOperand &MO : MI.explicit_operands()) |
| 263 | if (MO.isMBB() && MO.getMBB() == MBB) |
| 264 | return true; |
| 265 | return false; |
| 266 | } |
| 267 | |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 268 | /// Insert a BLOCK marker for branches to MBB (if needed). |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 269 | static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF, |
| 270 | SmallVectorImpl<MachineBasicBlock *> &ScopeTops, |
| 271 | const WebAssemblyInstrInfo &TII, |
| 272 | const MachineLoopInfo &MLI, |
| 273 | MachineDominatorTree &MDT) { |
| 274 | // First compute the nearest common dominator of all forward non-fallthrough |
| 275 | // predecessors so that we minimize the time that the BLOCK is on the stack, |
| 276 | // which reduces overall stack height. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 277 | MachineBasicBlock *Header = nullptr; |
| 278 | bool IsBranchedTo = false; |
| 279 | int MBBNumber = MBB.getNumber(); |
| 280 | for (MachineBasicBlock *Pred : MBB.predecessors()) |
| 281 | if (Pred->getNumber() < MBBNumber) { |
| 282 | Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred; |
Dan Gohman | b3aa1ec | 2015-12-16 19:06:41 +0000 | [diff] [blame] | 283 | if (ExplicitlyBranchesTo(Pred, &MBB)) |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 284 | IsBranchedTo = true; |
| 285 | } |
| 286 | if (!Header) |
| 287 | return; |
| 288 | if (!IsBranchedTo) |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 289 | return; |
| 290 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 291 | assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors"); |
| 292 | MachineBasicBlock *LayoutPred = &*prev(MachineFunction::iterator(&MBB)); |
| 293 | |
| 294 | // If the nearest common dominator is inside a more deeply nested context, |
| 295 | // walk out to the nearest scope which isn't more deeply nested. |
| 296 | for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) { |
| 297 | if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) { |
| 298 | if (ScopeTop->getNumber() > Header->getNumber()) { |
| 299 | // Skip over an intervening scope. |
| 300 | I = next(MachineFunction::iterator(ScopeTop)); |
| 301 | } else { |
| 302 | // We found a scope level at an appropriate depth. |
| 303 | Header = ScopeTop; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // If there's a loop which ends just before MBB which contains Header, we can |
| 310 | // reuse its label instead of inserting a new BLOCK. |
| 311 | for (MachineLoop *Loop = MLI.getLoopFor(LayoutPred); |
| 312 | Loop && Loop->contains(LayoutPred); Loop = Loop->getParentLoop()) |
| 313 | if (Loop && LoopBottom(Loop) == LayoutPred && Loop->contains(Header)) |
| 314 | return; |
| 315 | |
| 316 | // Decide where in Header to put the BLOCK. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 317 | MachineBasicBlock::iterator InsertPos; |
| 318 | MachineLoop *HeaderLoop = MLI.getLoopFor(Header); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 319 | if (HeaderLoop && MBB.getNumber() > LoopBottom(HeaderLoop)->getNumber()) { |
| 320 | // Header is the header of a loop that does not lexically contain MBB, so |
| 321 | // the BLOCK needs to be above the LOOP. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 322 | InsertPos = Header->begin(); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 323 | } else { |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 324 | // Otherwise, insert the BLOCK as late in Header as we can, but before the |
| 325 | // beginning of the local expression tree and any nested BLOCKs. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 326 | InsertPos = Header->getFirstTerminator(); |
| 327 | while (InsertPos != Header->begin() && |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 328 | prev(InsertPos)->definesRegister(WebAssembly::EXPR_STACK) && |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 329 | prev(InsertPos)->getOpcode() != WebAssembly::LOOP && |
| 330 | prev(InsertPos)->getOpcode() != WebAssembly::END_BLOCK && |
| 331 | prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP) |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 332 | --InsertPos; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 335 | // Add the BLOCK. |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 336 | BuildMI(*Header, InsertPos, DebugLoc(), TII.get(WebAssembly::BLOCK)); |
| 337 | |
| 338 | // Mark the end of the block. |
| 339 | InsertPos = MBB.begin(); |
| 340 | while (InsertPos != MBB.end() && |
| 341 | InsertPos->getOpcode() == WebAssembly::END_LOOP) |
| 342 | ++InsertPos; |
| 343 | BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::END_BLOCK)); |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 344 | |
| 345 | // Track the farthest-spanning scope that ends at this point. |
| 346 | int Number = MBB.getNumber(); |
| 347 | if (!ScopeTops[Number] || |
| 348 | ScopeTops[Number]->getNumber() > Header->getNumber()) |
| 349 | ScopeTops[Number] = Header; |
| 350 | } |
| 351 | |
| 352 | /// 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^] | 353 | static void PlaceLoopMarker( |
| 354 | MachineBasicBlock &MBB, MachineFunction &MF, |
| 355 | SmallVectorImpl<MachineBasicBlock *> &ScopeTops, |
| 356 | DenseMap<const MachineInstr *, const MachineBasicBlock *> &LoopTops, |
| 357 | const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI) { |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 358 | MachineLoop *Loop = MLI.getLoopFor(&MBB); |
| 359 | if (!Loop || Loop->getHeader() != &MBB) |
| 360 | return; |
| 361 | |
| 362 | // The operand of a LOOP is the first block after the loop. If the loop is the |
| 363 | // bottom of the function, insert a dummy block at the end. |
| 364 | MachineBasicBlock *Bottom = LoopBottom(Loop); |
| 365 | auto Iter = next(MachineFunction::iterator(Bottom)); |
| 366 | if (Iter == MF.end()) { |
| 367 | MachineBasicBlock *Label = MF.CreateMachineBasicBlock(); |
| 368 | // Give it a fake predecessor so that AsmPrinter prints its label. |
| 369 | Label->addSuccessor(Label); |
| 370 | MF.push_back(Label); |
| 371 | Iter = next(MachineFunction::iterator(Bottom)); |
| 372 | } |
| 373 | MachineBasicBlock *AfterLoop = &*Iter; |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 374 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 375 | // Mark the beginning of the loop (after the end of any existing loop that |
| 376 | // ends here). |
| 377 | auto InsertPos = MBB.begin(); |
| 378 | while (InsertPos != MBB.end() && |
| 379 | InsertPos->getOpcode() == WebAssembly::END_LOOP) |
| 380 | ++InsertPos; |
| 381 | BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::LOOP)); |
| 382 | |
| 383 | // Mark the end of the loop. |
| 384 | MachineInstr *End = BuildMI(*AfterLoop, AfterLoop->begin(), DebugLoc(), |
| 385 | TII.get(WebAssembly::END_LOOP)); |
| 386 | LoopTops[End] = &MBB; |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 387 | |
| 388 | assert((!ScopeTops[AfterLoop->getNumber()] || |
| 389 | ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) && |
| 390 | "With RPO we should visit the outer-most loop for a block first."); |
| 391 | if (!ScopeTops[AfterLoop->getNumber()]) |
| 392 | ScopeTops[AfterLoop->getNumber()] = &MBB; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 395 | static unsigned |
| 396 | GetDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack, |
| 397 | const MachineBasicBlock *MBB) { |
| 398 | unsigned Depth = 0; |
| 399 | for (auto X : reverse(Stack)) { |
| 400 | if (X == MBB) |
| 401 | break; |
| 402 | ++Depth; |
| 403 | } |
| 404 | assert(Depth < Stack.size() && "Branch destination should be in scope"); |
| 405 | return Depth; |
| 406 | } |
| 407 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 408 | /// Insert LOOP and BLOCK markers at appropriate places. |
| 409 | static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI, |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 410 | const WebAssemblyInstrInfo &TII, |
| 411 | MachineDominatorTree &MDT) { |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 412 | // For each block whose label represents the end of a scope, record the block |
| 413 | // which holds the beginning of the scope. This will allow us to quickly skip |
| 414 | // over scoped regions when walking blocks. We allocate one more than the |
| 415 | // number of blocks in the function to accommodate for the possible fake block |
| 416 | // we may insert at the end. |
| 417 | SmallVector<MachineBasicBlock *, 8> ScopeTops(MF.getNumBlockIDs() + 1); |
| 418 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 419 | // For eacn LOOP_END, the corresponding LOOP. |
| 420 | DenseMap<const MachineInstr *, const MachineBasicBlock *> LoopTops; |
| 421 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 422 | for (auto &MBB : MF) { |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 423 | // 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^] | 424 | PlaceLoopMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 425 | |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 426 | // Place the BLOCK for MBB if MBB is branched to from above. |
Dan Gohman | 8fe7e86 | 2015-12-14 22:51:54 +0000 | [diff] [blame] | 427 | PlaceBlockMarker(MBB, MF, ScopeTops, TII, MLI, MDT); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 428 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 429 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 430 | // Now rewrite references to basic blocks to be depth immediates. |
| 431 | SmallVector<const MachineBasicBlock *, 8> Stack; |
| 432 | for (auto &MBB : reverse(MF)) { |
| 433 | for (auto &MI : reverse(MBB)) { |
| 434 | switch (MI.getOpcode()) { |
| 435 | case WebAssembly::BLOCK: |
| 436 | assert(ScopeTops[Stack.back()->getNumber()] == &MBB && |
| 437 | "Block should be balanced"); |
| 438 | Stack.pop_back(); |
| 439 | break; |
| 440 | case WebAssembly::LOOP: |
| 441 | assert(Stack.back() == &MBB && "Loop top should be balanced"); |
| 442 | Stack.pop_back(); |
| 443 | Stack.pop_back(); |
| 444 | break; |
| 445 | case WebAssembly::END_BLOCK: |
| 446 | Stack.push_back(&MBB); |
| 447 | break; |
| 448 | case WebAssembly::END_LOOP: |
| 449 | Stack.push_back(&MBB); |
| 450 | Stack.push_back(LoopTops[&MI]); |
| 451 | break; |
| 452 | default: |
| 453 | if (MI.isTerminator()) { |
| 454 | // Rewrite MBB operands to be depth immediates. |
| 455 | SmallVector<MachineOperand, 4> Ops(MI.operands()); |
| 456 | while (MI.getNumOperands() > 0) |
| 457 | MI.RemoveOperand(MI.getNumOperands() - 1); |
| 458 | for (auto MO : Ops) { |
| 459 | if (MO.isMBB()) |
| 460 | MO = MachineOperand::CreateImm(GetDepth(Stack, MO.getMBB())); |
| 461 | MI.addOperand(MF, MO); |
| 462 | } |
| 463 | } |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | assert(Stack.empty() && "Control flow should be balanced"); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 469 | } |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 470 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 471 | bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) { |
| 472 | DEBUG(dbgs() << "********** CFG Stackifying **********\n" |
| 473 | "********** Function: " |
| 474 | << MF.getName() << '\n'); |
| 475 | |
| 476 | const auto &MLI = getAnalysis<MachineLoopInfo>(); |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 477 | auto &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 478 | const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 479 | |
| 480 | // RPO sorting needs all loops to be single-entry. |
| 481 | EliminateMultipleEntryLoops(MF, MLI); |
| 482 | |
| 483 | // Sort the blocks in RPO, with contiguous loops. |
| 484 | SortBlocks(MF, MLI); |
| 485 | |
| 486 | // Place the BLOCK and LOOP markers to indicate the beginnings of scopes. |
Dan Gohman | 3280793 | 2015-11-23 16:19:56 +0000 | [diff] [blame] | 487 | PlaceMarkers(MF, MLI, TII, MDT); |
| 488 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 489 | return true; |
| 490 | } |