WebAssemblyCFGStackify.cpp 15 KB

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