WebAssemblyCFGStackify.cpp 14 KB

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