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