WebAssemblyCFGStackify.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. //===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements a CFG stacking pass.
  11. ///
  12. /// This pass inserts BLOCK, LOOP, and TRY markers to mark the start of scopes,
  13. /// since scope boundaries serve as the labels for WebAssembly's control
  14. /// 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. /// In case we use exceptions, this pass also fixes mismatches in unwind
  20. /// destinations created during transforming CFG into wasm structured format.
  21. ///
  22. //===----------------------------------------------------------------------===//
  23. #include "Utils/WebAssemblyTypeUtilities.h"
  24. #include "Utils/WebAssemblyUtilities.h"
  25. #include "WebAssembly.h"
  26. #include "WebAssemblyExceptionInfo.h"
  27. #include "WebAssemblyMachineFunctionInfo.h"
  28. #include "WebAssemblySortRegion.h"
  29. #include "WebAssemblySubtarget.h"
  30. #include "llvm/ADT/Statistic.h"
  31. #include "llvm/CodeGen/MachineDominators.h"
  32. #include "llvm/CodeGen/MachineInstrBuilder.h"
  33. #include "llvm/CodeGen/MachineLoopInfo.h"
  34. #include "llvm/CodeGen/WasmEHFuncInfo.h"
  35. #include "llvm/MC/MCAsmInfo.h"
  36. #include "llvm/Target/TargetMachine.h"
  37. using namespace llvm;
  38. using WebAssembly::SortRegionInfo;
  39. #define DEBUG_TYPE "wasm-cfg-stackify"
  40. STATISTIC(NumCallUnwindMismatches, "Number of call unwind mismatches found");
  41. STATISTIC(NumCatchUnwindMismatches, "Number of catch unwind mismatches found");
  42. namespace {
  43. class WebAssemblyCFGStackify final : public MachineFunctionPass {
  44. StringRef getPassName() const override { return "WebAssembly CFG Stackify"; }
  45. void getAnalysisUsage(AnalysisUsage &AU) const override {
  46. AU.addRequired<MachineDominatorTree>();
  47. AU.addRequired<MachineLoopInfo>();
  48. AU.addRequired<WebAssemblyExceptionInfo>();
  49. MachineFunctionPass::getAnalysisUsage(AU);
  50. }
  51. bool runOnMachineFunction(MachineFunction &MF) override;
  52. // For each block whose label represents the end of a scope, record the block
  53. // which holds the beginning of the scope. This will allow us to quickly skip
  54. // over scoped regions when walking blocks.
  55. SmallVector<MachineBasicBlock *, 8> ScopeTops;
  56. void updateScopeTops(MachineBasicBlock *Begin, MachineBasicBlock *End) {
  57. int EndNo = End->getNumber();
  58. if (!ScopeTops[EndNo] || ScopeTops[EndNo]->getNumber() > Begin->getNumber())
  59. ScopeTops[EndNo] = Begin;
  60. }
  61. // Placing markers.
  62. void placeMarkers(MachineFunction &MF);
  63. void placeBlockMarker(MachineBasicBlock &MBB);
  64. void placeLoopMarker(MachineBasicBlock &MBB);
  65. void placeTryMarker(MachineBasicBlock &MBB);
  66. // Exception handling related functions
  67. bool fixCallUnwindMismatches(MachineFunction &MF);
  68. bool fixCatchUnwindMismatches(MachineFunction &MF);
  69. void addTryDelegate(MachineInstr *RangeBegin, MachineInstr *RangeEnd,
  70. MachineBasicBlock *DelegateDest);
  71. void recalculateScopeTops(MachineFunction &MF);
  72. void removeUnnecessaryInstrs(MachineFunction &MF);
  73. // Wrap-up
  74. using EndMarkerInfo =
  75. std::pair<const MachineBasicBlock *, const MachineInstr *>;
  76. unsigned getBranchDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
  77. const MachineBasicBlock *MBB);
  78. unsigned getDelegateDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
  79. const MachineBasicBlock *MBB);
  80. unsigned
  81. getRethrowDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
  82. const SmallVectorImpl<const MachineBasicBlock *> &EHPadStack);
  83. void rewriteDepthImmediates(MachineFunction &MF);
  84. void fixEndsAtEndOfFunction(MachineFunction &MF);
  85. void cleanupFunctionData(MachineFunction &MF);
  86. // For each BLOCK|LOOP|TRY, the corresponding END_(BLOCK|LOOP|TRY) or DELEGATE
  87. // (in case of TRY).
  88. DenseMap<const MachineInstr *, MachineInstr *> BeginToEnd;
  89. // For each END_(BLOCK|LOOP|TRY) or DELEGATE, the corresponding
  90. // BLOCK|LOOP|TRY.
  91. DenseMap<const MachineInstr *, MachineInstr *> EndToBegin;
  92. // <TRY marker, EH pad> map
  93. DenseMap<const MachineInstr *, MachineBasicBlock *> TryToEHPad;
  94. // <EH pad, TRY marker> map
  95. DenseMap<const MachineBasicBlock *, MachineInstr *> EHPadToTry;
  96. // We need an appendix block to place 'end_loop' or 'end_try' marker when the
  97. // loop / exception bottom block is the last block in a function
  98. MachineBasicBlock *AppendixBB = nullptr;
  99. MachineBasicBlock *getAppendixBlock(MachineFunction &MF) {
  100. if (!AppendixBB) {
  101. AppendixBB = MF.CreateMachineBasicBlock();
  102. // Give it a fake predecessor so that AsmPrinter prints its label.
  103. AppendixBB->addSuccessor(AppendixBB);
  104. MF.push_back(AppendixBB);
  105. }
  106. return AppendixBB;
  107. }
  108. // Before running rewriteDepthImmediates function, 'delegate' has a BB as its
  109. // destination operand. getFakeCallerBlock() returns a fake BB that will be
  110. // used for the operand when 'delegate' needs to rethrow to the caller. This
  111. // will be rewritten as an immediate value that is the number of block depths
  112. // + 1 in rewriteDepthImmediates, and this fake BB will be removed at the end
  113. // of the pass.
  114. MachineBasicBlock *FakeCallerBB = nullptr;
  115. MachineBasicBlock *getFakeCallerBlock(MachineFunction &MF) {
  116. if (!FakeCallerBB)
  117. FakeCallerBB = MF.CreateMachineBasicBlock();
  118. return FakeCallerBB;
  119. }
  120. // Helper functions to register / unregister scope information created by
  121. // marker instructions.
  122. void registerScope(MachineInstr *Begin, MachineInstr *End);
  123. void registerTryScope(MachineInstr *Begin, MachineInstr *End,
  124. MachineBasicBlock *EHPad);
  125. void unregisterScope(MachineInstr *Begin);
  126. public:
  127. static char ID; // Pass identification, replacement for typeid
  128. WebAssemblyCFGStackify() : MachineFunctionPass(ID) {}
  129. ~WebAssemblyCFGStackify() override { releaseMemory(); }
  130. void releaseMemory() override;
  131. };
  132. } // end anonymous namespace
  133. char WebAssemblyCFGStackify::ID = 0;
  134. INITIALIZE_PASS(WebAssemblyCFGStackify, DEBUG_TYPE,
  135. "Insert BLOCK/LOOP/TRY markers for WebAssembly scopes", false,
  136. false)
  137. FunctionPass *llvm::createWebAssemblyCFGStackify() {
  138. return new WebAssemblyCFGStackify();
  139. }
  140. /// Test whether Pred has any terminators explicitly branching to MBB, as
  141. /// opposed to falling through. Note that it's possible (eg. in unoptimized
  142. /// code) for a branch instruction to both branch to a block and fallthrough
  143. /// to it, so we check the actual branch operands to see if there are any
  144. /// explicit mentions.
  145. static bool explicitlyBranchesTo(MachineBasicBlock *Pred,
  146. MachineBasicBlock *MBB) {
  147. for (MachineInstr &MI : Pred->terminators())
  148. for (MachineOperand &MO : MI.explicit_operands())
  149. if (MO.isMBB() && MO.getMBB() == MBB)
  150. return true;
  151. return false;
  152. }
  153. // Returns an iterator to the earliest position possible within the MBB,
  154. // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
  155. // contains instructions that should go before the marker, and AfterSet contains
  156. // ones that should go after the marker. In this function, AfterSet is only
  157. // used for validation checking.
  158. template <typename Container>
  159. static MachineBasicBlock::iterator
  160. getEarliestInsertPos(MachineBasicBlock *MBB, const Container &BeforeSet,
  161. const Container &AfterSet) {
  162. auto InsertPos = MBB->end();
  163. while (InsertPos != MBB->begin()) {
  164. if (BeforeSet.count(&*std::prev(InsertPos))) {
  165. #ifndef NDEBUG
  166. // Validation check
  167. for (auto Pos = InsertPos, E = MBB->begin(); Pos != E; --Pos)
  168. assert(!AfterSet.count(&*std::prev(Pos)));
  169. #endif
  170. break;
  171. }
  172. --InsertPos;
  173. }
  174. return InsertPos;
  175. }
  176. // Returns an iterator to the latest position possible within the MBB,
  177. // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
  178. // contains instructions that should go before the marker, and AfterSet contains
  179. // ones that should go after the marker. In this function, BeforeSet is only
  180. // used for validation checking.
  181. template <typename Container>
  182. static MachineBasicBlock::iterator
  183. getLatestInsertPos(MachineBasicBlock *MBB, const Container &BeforeSet,
  184. const Container &AfterSet) {
  185. auto InsertPos = MBB->begin();
  186. while (InsertPos != MBB->end()) {
  187. if (AfterSet.count(&*InsertPos)) {
  188. #ifndef NDEBUG
  189. // Validation check
  190. for (auto Pos = InsertPos, E = MBB->end(); Pos != E; ++Pos)
  191. assert(!BeforeSet.count(&*Pos));
  192. #endif
  193. break;
  194. }
  195. ++InsertPos;
  196. }
  197. return InsertPos;
  198. }
  199. void WebAssemblyCFGStackify::registerScope(MachineInstr *Begin,
  200. MachineInstr *End) {
  201. BeginToEnd[Begin] = End;
  202. EndToBegin[End] = Begin;
  203. }
  204. // When 'End' is not an 'end_try' but 'delegate, EHPad is nullptr.
  205. void WebAssemblyCFGStackify::registerTryScope(MachineInstr *Begin,
  206. MachineInstr *End,
  207. MachineBasicBlock *EHPad) {
  208. registerScope(Begin, End);
  209. TryToEHPad[Begin] = EHPad;
  210. EHPadToTry[EHPad] = Begin;
  211. }
  212. void WebAssemblyCFGStackify::unregisterScope(MachineInstr *Begin) {
  213. assert(BeginToEnd.count(Begin));
  214. MachineInstr *End = BeginToEnd[Begin];
  215. assert(EndToBegin.count(End));
  216. BeginToEnd.erase(Begin);
  217. EndToBegin.erase(End);
  218. MachineBasicBlock *EHPad = TryToEHPad.lookup(Begin);
  219. if (EHPad) {
  220. assert(EHPadToTry.count(EHPad));
  221. TryToEHPad.erase(Begin);
  222. EHPadToTry.erase(EHPad);
  223. }
  224. }
  225. /// Insert a BLOCK marker for branches to MBB (if needed).
  226. // TODO Consider a more generalized way of handling block (and also loop and
  227. // try) signatures when we implement the multi-value proposal later.
  228. void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) {
  229. assert(!MBB.isEHPad());
  230. MachineFunction &MF = *MBB.getParent();
  231. auto &MDT = getAnalysis<MachineDominatorTree>();
  232. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  233. const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  234. // First compute the nearest common dominator of all forward non-fallthrough
  235. // predecessors so that we minimize the time that the BLOCK is on the stack,
  236. // which reduces overall stack height.
  237. MachineBasicBlock *Header = nullptr;
  238. bool IsBranchedTo = false;
  239. int MBBNumber = MBB.getNumber();
  240. for (MachineBasicBlock *Pred : MBB.predecessors()) {
  241. if (Pred->getNumber() < MBBNumber) {
  242. Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
  243. if (explicitlyBranchesTo(Pred, &MBB))
  244. IsBranchedTo = true;
  245. }
  246. }
  247. if (!Header)
  248. return;
  249. if (!IsBranchedTo)
  250. return;
  251. assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors");
  252. MachineBasicBlock *LayoutPred = MBB.getPrevNode();
  253. // If the nearest common dominator is inside a more deeply nested context,
  254. // walk out to the nearest scope which isn't more deeply nested.
  255. for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
  256. if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
  257. if (ScopeTop->getNumber() > Header->getNumber()) {
  258. // Skip over an intervening scope.
  259. I = std::next(ScopeTop->getIterator());
  260. } else {
  261. // We found a scope level at an appropriate depth.
  262. Header = ScopeTop;
  263. break;
  264. }
  265. }
  266. }
  267. // Decide where in Header to put the BLOCK.
  268. // Instructions that should go before the BLOCK.
  269. SmallPtrSet<const MachineInstr *, 4> BeforeSet;
  270. // Instructions that should go after the BLOCK.
  271. SmallPtrSet<const MachineInstr *, 4> AfterSet;
  272. for (const auto &MI : *Header) {
  273. // If there is a previously placed LOOP marker and the bottom block of the
  274. // loop is above MBB, it should be after the BLOCK, because the loop is
  275. // nested in this BLOCK. Otherwise it should be before the BLOCK.
  276. if (MI.getOpcode() == WebAssembly::LOOP) {
  277. auto *LoopBottom = BeginToEnd[&MI]->getParent()->getPrevNode();
  278. if (MBB.getNumber() > LoopBottom->getNumber())
  279. AfterSet.insert(&MI);
  280. #ifndef NDEBUG
  281. else
  282. BeforeSet.insert(&MI);
  283. #endif
  284. }
  285. // If there is a previously placed BLOCK/TRY marker and its corresponding
  286. // END marker is before the current BLOCK's END marker, that should be
  287. // placed after this BLOCK. Otherwise it should be placed before this BLOCK
  288. // marker.
  289. if (MI.getOpcode() == WebAssembly::BLOCK ||
  290. MI.getOpcode() == WebAssembly::TRY) {
  291. if (BeginToEnd[&MI]->getParent()->getNumber() <= MBB.getNumber())
  292. AfterSet.insert(&MI);
  293. #ifndef NDEBUG
  294. else
  295. BeforeSet.insert(&MI);
  296. #endif
  297. }
  298. #ifndef NDEBUG
  299. // All END_(BLOCK|LOOP|TRY) markers should be before the BLOCK.
  300. if (MI.getOpcode() == WebAssembly::END_BLOCK ||
  301. MI.getOpcode() == WebAssembly::END_LOOP ||
  302. MI.getOpcode() == WebAssembly::END_TRY)
  303. BeforeSet.insert(&MI);
  304. #endif
  305. // Terminators should go after the BLOCK.
  306. if (MI.isTerminator())
  307. AfterSet.insert(&MI);
  308. }
  309. // Local expression tree should go after the BLOCK.
  310. for (auto I = Header->getFirstTerminator(), E = Header->begin(); I != E;
  311. --I) {
  312. if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
  313. continue;
  314. if (WebAssembly::isChild(*std::prev(I), MFI))
  315. AfterSet.insert(&*std::prev(I));
  316. else
  317. break;
  318. }
  319. // Add the BLOCK.
  320. WebAssembly::BlockType ReturnType = WebAssembly::BlockType::Void;
  321. auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet);
  322. MachineInstr *Begin =
  323. BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
  324. TII.get(WebAssembly::BLOCK))
  325. .addImm(int64_t(ReturnType));
  326. // Decide where in Header to put the END_BLOCK.
  327. BeforeSet.clear();
  328. AfterSet.clear();
  329. for (auto &MI : MBB) {
  330. #ifndef NDEBUG
  331. // END_BLOCK should precede existing LOOP and TRY markers.
  332. if (MI.getOpcode() == WebAssembly::LOOP ||
  333. MI.getOpcode() == WebAssembly::TRY)
  334. AfterSet.insert(&MI);
  335. #endif
  336. // If there is a previously placed END_LOOP marker and the header of the
  337. // loop is above this block's header, the END_LOOP should be placed after
  338. // the BLOCK, because the loop contains this block. Otherwise the END_LOOP
  339. // should be placed before the BLOCK. The same for END_TRY.
  340. if (MI.getOpcode() == WebAssembly::END_LOOP ||
  341. MI.getOpcode() == WebAssembly::END_TRY) {
  342. if (EndToBegin[&MI]->getParent()->getNumber() >= Header->getNumber())
  343. BeforeSet.insert(&MI);
  344. #ifndef NDEBUG
  345. else
  346. AfterSet.insert(&MI);
  347. #endif
  348. }
  349. }
  350. // Mark the end of the block.
  351. InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet);
  352. MachineInstr *End = BuildMI(MBB, InsertPos, MBB.findPrevDebugLoc(InsertPos),
  353. TII.get(WebAssembly::END_BLOCK));
  354. registerScope(Begin, End);
  355. // Track the farthest-spanning scope that ends at this point.
  356. updateScopeTops(Header, &MBB);
  357. }
  358. /// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
  359. void WebAssemblyCFGStackify::placeLoopMarker(MachineBasicBlock &MBB) {
  360. MachineFunction &MF = *MBB.getParent();
  361. const auto &MLI = getAnalysis<MachineLoopInfo>();
  362. const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
  363. SortRegionInfo SRI(MLI, WEI);
  364. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  365. MachineLoop *Loop = MLI.getLoopFor(&MBB);
  366. if (!Loop || Loop->getHeader() != &MBB)
  367. return;
  368. // The operand of a LOOP is the first block after the loop. If the loop is the
  369. // bottom of the function, insert a dummy block at the end.
  370. MachineBasicBlock *Bottom = SRI.getBottom(Loop);
  371. auto Iter = std::next(Bottom->getIterator());
  372. if (Iter == MF.end()) {
  373. getAppendixBlock(MF);
  374. Iter = std::next(Bottom->getIterator());
  375. }
  376. MachineBasicBlock *AfterLoop = &*Iter;
  377. // Decide where in Header to put the LOOP.
  378. SmallPtrSet<const MachineInstr *, 4> BeforeSet;
  379. SmallPtrSet<const MachineInstr *, 4> AfterSet;
  380. for (const auto &MI : MBB) {
  381. // LOOP marker should be after any existing loop that ends here. Otherwise
  382. // we assume the instruction belongs to the loop.
  383. if (MI.getOpcode() == WebAssembly::END_LOOP)
  384. BeforeSet.insert(&MI);
  385. #ifndef NDEBUG
  386. else
  387. AfterSet.insert(&MI);
  388. #endif
  389. }
  390. // Mark the beginning of the loop.
  391. auto InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet);
  392. MachineInstr *Begin = BuildMI(MBB, InsertPos, MBB.findDebugLoc(InsertPos),
  393. TII.get(WebAssembly::LOOP))
  394. .addImm(int64_t(WebAssembly::BlockType::Void));
  395. // Decide where in Header to put the END_LOOP.
  396. BeforeSet.clear();
  397. AfterSet.clear();
  398. #ifndef NDEBUG
  399. for (const auto &MI : MBB)
  400. // Existing END_LOOP markers belong to parent loops of this loop
  401. if (MI.getOpcode() == WebAssembly::END_LOOP)
  402. AfterSet.insert(&MI);
  403. #endif
  404. // Mark the end of the loop (using arbitrary debug location that branched to
  405. // the loop end as its location).
  406. InsertPos = getEarliestInsertPos(AfterLoop, BeforeSet, AfterSet);
  407. DebugLoc EndDL = AfterLoop->pred_empty()
  408. ? DebugLoc()
  409. : (*AfterLoop->pred_rbegin())->findBranchDebugLoc();
  410. MachineInstr *End =
  411. BuildMI(*AfterLoop, InsertPos, EndDL, TII.get(WebAssembly::END_LOOP));
  412. registerScope(Begin, End);
  413. assert((!ScopeTops[AfterLoop->getNumber()] ||
  414. ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) &&
  415. "With block sorting the outermost loop for a block should be first.");
  416. updateScopeTops(&MBB, AfterLoop);
  417. }
  418. void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
  419. assert(MBB.isEHPad());
  420. MachineFunction &MF = *MBB.getParent();
  421. auto &MDT = getAnalysis<MachineDominatorTree>();
  422. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  423. const auto &MLI = getAnalysis<MachineLoopInfo>();
  424. const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
  425. SortRegionInfo SRI(MLI, WEI);
  426. const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  427. // Compute the nearest common dominator of all unwind predecessors
  428. MachineBasicBlock *Header = nullptr;
  429. int MBBNumber = MBB.getNumber();
  430. for (auto *Pred : MBB.predecessors()) {
  431. if (Pred->getNumber() < MBBNumber) {
  432. Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
  433. assert(!explicitlyBranchesTo(Pred, &MBB) &&
  434. "Explicit branch to an EH pad!");
  435. }
  436. }
  437. if (!Header)
  438. return;
  439. // If this try is at the bottom of the function, insert a dummy block at the
  440. // end.
  441. WebAssemblyException *WE = WEI.getExceptionFor(&MBB);
  442. assert(WE);
  443. MachineBasicBlock *Bottom = SRI.getBottom(WE);
  444. auto Iter = std::next(Bottom->getIterator());
  445. if (Iter == MF.end()) {
  446. getAppendixBlock(MF);
  447. Iter = std::next(Bottom->getIterator());
  448. }
  449. MachineBasicBlock *Cont = &*Iter;
  450. assert(Cont != &MF.front());
  451. MachineBasicBlock *LayoutPred = Cont->getPrevNode();
  452. // If the nearest common dominator is inside a more deeply nested context,
  453. // walk out to the nearest scope which isn't more deeply nested.
  454. for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
  455. if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
  456. if (ScopeTop->getNumber() > Header->getNumber()) {
  457. // Skip over an intervening scope.
  458. I = std::next(ScopeTop->getIterator());
  459. } else {
  460. // We found a scope level at an appropriate depth.
  461. Header = ScopeTop;
  462. break;
  463. }
  464. }
  465. }
  466. // Decide where in Header to put the TRY.
  467. // Instructions that should go before the TRY.
  468. SmallPtrSet<const MachineInstr *, 4> BeforeSet;
  469. // Instructions that should go after the TRY.
  470. SmallPtrSet<const MachineInstr *, 4> AfterSet;
  471. for (const auto &MI : *Header) {
  472. // If there is a previously placed LOOP marker and the bottom block of the
  473. // loop is above MBB, it should be after the TRY, because the loop is nested
  474. // in this TRY. Otherwise it should be before the TRY.
  475. if (MI.getOpcode() == WebAssembly::LOOP) {
  476. auto *LoopBottom = BeginToEnd[&MI]->getParent()->getPrevNode();
  477. if (MBB.getNumber() > LoopBottom->getNumber())
  478. AfterSet.insert(&MI);
  479. #ifndef NDEBUG
  480. else
  481. BeforeSet.insert(&MI);
  482. #endif
  483. }
  484. // All previously inserted BLOCK/TRY markers should be after the TRY because
  485. // they are all nested trys.
  486. if (MI.getOpcode() == WebAssembly::BLOCK ||
  487. MI.getOpcode() == WebAssembly::TRY)
  488. AfterSet.insert(&MI);
  489. #ifndef NDEBUG
  490. // All END_(BLOCK/LOOP/TRY) markers should be before the TRY.
  491. if (MI.getOpcode() == WebAssembly::END_BLOCK ||
  492. MI.getOpcode() == WebAssembly::END_LOOP ||
  493. MI.getOpcode() == WebAssembly::END_TRY)
  494. BeforeSet.insert(&MI);
  495. #endif
  496. // Terminators should go after the TRY.
  497. if (MI.isTerminator())
  498. AfterSet.insert(&MI);
  499. }
  500. // If Header unwinds to MBB (= Header contains 'invoke'), the try block should
  501. // contain the call within it. So the call should go after the TRY. The
  502. // exception is when the header's terminator is a rethrow instruction, in
  503. // which case that instruction, not a call instruction before it, is gonna
  504. // throw.
  505. MachineInstr *ThrowingCall = nullptr;
  506. if (MBB.isPredecessor(Header)) {
  507. auto TermPos = Header->getFirstTerminator();
  508. if (TermPos == Header->end() ||
  509. TermPos->getOpcode() != WebAssembly::RETHROW) {
  510. for (auto &MI : reverse(*Header)) {
  511. if (MI.isCall()) {
  512. AfterSet.insert(&MI);
  513. ThrowingCall = &MI;
  514. // Possibly throwing calls are usually wrapped by EH_LABEL
  515. // instructions. We don't want to split them and the call.
  516. if (MI.getIterator() != Header->begin() &&
  517. std::prev(MI.getIterator())->isEHLabel()) {
  518. AfterSet.insert(&*std::prev(MI.getIterator()));
  519. ThrowingCall = &*std::prev(MI.getIterator());
  520. }
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. // Local expression tree should go after the TRY.
  527. // For BLOCK placement, we start the search from the previous instruction of a
  528. // BB's terminator, but in TRY's case, we should start from the previous
  529. // instruction of a call that can throw, or a EH_LABEL that precedes the call,
  530. // because the return values of the call's previous instructions can be
  531. // stackified and consumed by the throwing call.
  532. auto SearchStartPt = ThrowingCall ? MachineBasicBlock::iterator(ThrowingCall)
  533. : Header->getFirstTerminator();
  534. for (auto I = SearchStartPt, E = Header->begin(); I != E; --I) {
  535. if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
  536. continue;
  537. if (WebAssembly::isChild(*std::prev(I), MFI))
  538. AfterSet.insert(&*std::prev(I));
  539. else
  540. break;
  541. }
  542. // Add the TRY.
  543. auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet);
  544. MachineInstr *Begin =
  545. BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
  546. TII.get(WebAssembly::TRY))
  547. .addImm(int64_t(WebAssembly::BlockType::Void));
  548. // Decide where in Header to put the END_TRY.
  549. BeforeSet.clear();
  550. AfterSet.clear();
  551. for (const auto &MI : *Cont) {
  552. #ifndef NDEBUG
  553. // END_TRY should precede existing LOOP and BLOCK markers.
  554. if (MI.getOpcode() == WebAssembly::LOOP ||
  555. MI.getOpcode() == WebAssembly::BLOCK)
  556. AfterSet.insert(&MI);
  557. // All END_TRY markers placed earlier belong to exceptions that contains
  558. // this one.
  559. if (MI.getOpcode() == WebAssembly::END_TRY)
  560. AfterSet.insert(&MI);
  561. #endif
  562. // If there is a previously placed END_LOOP marker and its header is after
  563. // where TRY marker is, this loop is contained within the 'catch' part, so
  564. // the END_TRY marker should go after that. Otherwise, the whole try-catch
  565. // is contained within this loop, so the END_TRY should go before that.
  566. if (MI.getOpcode() == WebAssembly::END_LOOP) {
  567. // For a LOOP to be after TRY, LOOP's BB should be after TRY's BB; if they
  568. // are in the same BB, LOOP is always before TRY.
  569. if (EndToBegin[&MI]->getParent()->getNumber() > Header->getNumber())
  570. BeforeSet.insert(&MI);
  571. #ifndef NDEBUG
  572. else
  573. AfterSet.insert(&MI);
  574. #endif
  575. }
  576. // It is not possible for an END_BLOCK to be already in this block.
  577. }
  578. // Mark the end of the TRY.
  579. InsertPos = getEarliestInsertPos(Cont, BeforeSet, AfterSet);
  580. MachineInstr *End =
  581. BuildMI(*Cont, InsertPos, Bottom->findBranchDebugLoc(),
  582. TII.get(WebAssembly::END_TRY));
  583. registerTryScope(Begin, End, &MBB);
  584. // Track the farthest-spanning scope that ends at this point. We create two
  585. // mappings: (BB with 'end_try' -> BB with 'try') and (BB with 'catch' -> BB
  586. // with 'try'). We need to create 'catch' -> 'try' mapping here too because
  587. // markers should not span across 'catch'. For example, this should not
  588. // happen:
  589. //
  590. // try
  591. // block --| (X)
  592. // catch |
  593. // end_block --|
  594. // end_try
  595. for (auto *End : {&MBB, Cont})
  596. updateScopeTops(Header, End);
  597. }
  598. void WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) {
  599. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  600. // When there is an unconditional branch right before a catch instruction and
  601. // it branches to the end of end_try marker, we don't need the branch, because
  602. // it there is no exception, the control flow transfers to that point anyway.
  603. // bb0:
  604. // try
  605. // ...
  606. // br bb2 <- Not necessary
  607. // bb1 (ehpad):
  608. // catch
  609. // ...
  610. // bb2: <- Continuation BB
  611. // end
  612. //
  613. // A more involved case: When the BB where 'end' is located is an another EH
  614. // pad, the Cont (= continuation) BB is that EH pad's 'end' BB. For example,
  615. // bb0:
  616. // try
  617. // try
  618. // ...
  619. // br bb3 <- Not necessary
  620. // bb1 (ehpad):
  621. // catch
  622. // bb2 (ehpad):
  623. // end
  624. // catch
  625. // ...
  626. // bb3: <- Continuation BB
  627. // end
  628. //
  629. // When the EH pad at hand is bb1, its matching end_try is in bb2. But it is
  630. // another EH pad, so bb0's continuation BB becomes bb3. So 'br bb3' in the
  631. // code can be deleted. This is why we run 'while' until 'Cont' is not an EH
  632. // pad.
  633. for (auto &MBB : MF) {
  634. if (!MBB.isEHPad())
  635. continue;
  636. MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
  637. SmallVector<MachineOperand, 4> Cond;
  638. MachineBasicBlock *EHPadLayoutPred = MBB.getPrevNode();
  639. MachineBasicBlock *Cont = &MBB;
  640. while (Cont->isEHPad()) {
  641. MachineInstr *Try = EHPadToTry[Cont];
  642. MachineInstr *EndTry = BeginToEnd[Try];
  643. // We started from an EH pad, so the end marker cannot be a delegate
  644. assert(EndTry->getOpcode() != WebAssembly::DELEGATE);
  645. Cont = EndTry->getParent();
  646. }
  647. bool Analyzable = !TII.analyzeBranch(*EHPadLayoutPred, TBB, FBB, Cond);
  648. // This condition means either
  649. // 1. This BB ends with a single unconditional branch whose destinaion is
  650. // Cont.
  651. // 2. This BB ends with a conditional branch followed by an unconditional
  652. // branch, and the unconditional branch's destination is Cont.
  653. // In both cases, we want to remove the last (= unconditional) branch.
  654. if (Analyzable && ((Cond.empty() && TBB && TBB == Cont) ||
  655. (!Cond.empty() && FBB && FBB == Cont))) {
  656. bool ErasedUncondBr = false;
  657. (void)ErasedUncondBr;
  658. for (auto I = EHPadLayoutPred->end(), E = EHPadLayoutPred->begin();
  659. I != E; --I) {
  660. auto PrevI = std::prev(I);
  661. if (PrevI->isTerminator()) {
  662. assert(PrevI->getOpcode() == WebAssembly::BR);
  663. PrevI->eraseFromParent();
  664. ErasedUncondBr = true;
  665. break;
  666. }
  667. }
  668. assert(ErasedUncondBr && "Unconditional branch not erased!");
  669. }
  670. }
  671. // When there are block / end_block markers that overlap with try / end_try
  672. // markers, and the block and try markers' return types are the same, the
  673. // block /end_block markers are not necessary, because try / end_try markers
  674. // also can serve as boundaries for branches.
  675. // block <- Not necessary
  676. // try
  677. // ...
  678. // catch
  679. // ...
  680. // end
  681. // end <- Not necessary
  682. SmallVector<MachineInstr *, 32> ToDelete;
  683. for (auto &MBB : MF) {
  684. for (auto &MI : MBB) {
  685. if (MI.getOpcode() != WebAssembly::TRY)
  686. continue;
  687. MachineInstr *Try = &MI, *EndTry = BeginToEnd[Try];
  688. if (EndTry->getOpcode() == WebAssembly::DELEGATE)
  689. continue;
  690. MachineBasicBlock *TryBB = Try->getParent();
  691. MachineBasicBlock *Cont = EndTry->getParent();
  692. int64_t RetType = Try->getOperand(0).getImm();
  693. for (auto B = Try->getIterator(), E = std::next(EndTry->getIterator());
  694. B != TryBB->begin() && E != Cont->end() &&
  695. std::prev(B)->getOpcode() == WebAssembly::BLOCK &&
  696. E->getOpcode() == WebAssembly::END_BLOCK &&
  697. std::prev(B)->getOperand(0).getImm() == RetType;
  698. --B, ++E) {
  699. ToDelete.push_back(&*std::prev(B));
  700. ToDelete.push_back(&*E);
  701. }
  702. }
  703. }
  704. for (auto *MI : ToDelete) {
  705. if (MI->getOpcode() == WebAssembly::BLOCK)
  706. unregisterScope(MI);
  707. MI->eraseFromParent();
  708. }
  709. }
  710. // When MBB is split into MBB and Split, we should unstackify defs in MBB that
  711. // have their uses in Split.
  712. static void unstackifyVRegsUsedInSplitBB(MachineBasicBlock &MBB,
  713. MachineBasicBlock &Split) {
  714. MachineFunction &MF = *MBB.getParent();
  715. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  716. auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  717. auto &MRI = MF.getRegInfo();
  718. for (auto &MI : Split) {
  719. for (auto &MO : MI.explicit_uses()) {
  720. if (!MO.isReg() || MO.getReg().isPhysical())
  721. continue;
  722. if (MachineInstr *Def = MRI.getUniqueVRegDef(MO.getReg()))
  723. if (Def->getParent() == &MBB)
  724. MFI.unstackifyVReg(MO.getReg());
  725. }
  726. }
  727. // In RegStackify, when a register definition is used multiple times,
  728. // Reg = INST ...
  729. // INST ..., Reg, ...
  730. // INST ..., Reg, ...
  731. // INST ..., Reg, ...
  732. //
  733. // we introduce a TEE, which has the following form:
  734. // DefReg = INST ...
  735. // TeeReg, Reg = TEE_... DefReg
  736. // INST ..., TeeReg, ...
  737. // INST ..., Reg, ...
  738. // INST ..., Reg, ...
  739. // with DefReg and TeeReg stackified but Reg not stackified.
  740. //
  741. // But the invariant that TeeReg should be stackified can be violated while we
  742. // unstackify registers in the split BB above. In this case, we convert TEEs
  743. // into two COPYs. This COPY will be eventually eliminated in ExplicitLocals.
  744. // DefReg = INST ...
  745. // TeeReg = COPY DefReg
  746. // Reg = COPY DefReg
  747. // INST ..., TeeReg, ...
  748. // INST ..., Reg, ...
  749. // INST ..., Reg, ...
  750. for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
  751. if (!WebAssembly::isTee(MI.getOpcode()))
  752. continue;
  753. Register TeeReg = MI.getOperand(0).getReg();
  754. Register Reg = MI.getOperand(1).getReg();
  755. Register DefReg = MI.getOperand(2).getReg();
  756. if (!MFI.isVRegStackified(TeeReg)) {
  757. // Now we are not using TEE anymore, so unstackify DefReg too
  758. MFI.unstackifyVReg(DefReg);
  759. unsigned CopyOpc =
  760. WebAssembly::getCopyOpcodeForRegClass(MRI.getRegClass(DefReg));
  761. BuildMI(MBB, &MI, MI.getDebugLoc(), TII.get(CopyOpc), TeeReg)
  762. .addReg(DefReg);
  763. BuildMI(MBB, &MI, MI.getDebugLoc(), TII.get(CopyOpc), Reg).addReg(DefReg);
  764. MI.eraseFromParent();
  765. }
  766. }
  767. }
  768. // Wrap the given range of instruction with try-delegate. RangeBegin and
  769. // RangeEnd are inclusive.
  770. void WebAssemblyCFGStackify::addTryDelegate(MachineInstr *RangeBegin,
  771. MachineInstr *RangeEnd,
  772. MachineBasicBlock *DelegateDest) {
  773. auto *BeginBB = RangeBegin->getParent();
  774. auto *EndBB = RangeEnd->getParent();
  775. MachineFunction &MF = *BeginBB->getParent();
  776. const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  777. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  778. // Local expression tree before the first call of this range should go
  779. // after the nested TRY.
  780. SmallPtrSet<const MachineInstr *, 4> AfterSet;
  781. AfterSet.insert(RangeBegin);
  782. for (auto I = MachineBasicBlock::iterator(RangeBegin), E = BeginBB->begin();
  783. I != E; --I) {
  784. if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
  785. continue;
  786. if (WebAssembly::isChild(*std::prev(I), MFI))
  787. AfterSet.insert(&*std::prev(I));
  788. else
  789. break;
  790. }
  791. // Create the nested try instruction.
  792. auto TryPos = getLatestInsertPos(
  793. BeginBB, SmallPtrSet<const MachineInstr *, 4>(), AfterSet);
  794. MachineInstr *Try = BuildMI(*BeginBB, TryPos, RangeBegin->getDebugLoc(),
  795. TII.get(WebAssembly::TRY))
  796. .addImm(int64_t(WebAssembly::BlockType::Void));
  797. // Create a BB to insert the 'delegate' instruction.
  798. MachineBasicBlock *DelegateBB = MF.CreateMachineBasicBlock();
  799. // If the destination of 'delegate' is not the caller, adds the destination to
  800. // the BB's successors.
  801. if (DelegateDest != FakeCallerBB)
  802. DelegateBB->addSuccessor(DelegateDest);
  803. auto SplitPos = std::next(RangeEnd->getIterator());
  804. if (SplitPos == EndBB->end()) {
  805. // If the range's end instruction is at the end of the BB, insert the new
  806. // delegate BB after the current BB.
  807. MF.insert(std::next(EndBB->getIterator()), DelegateBB);
  808. EndBB->addSuccessor(DelegateBB);
  809. } else {
  810. // When the split pos is in the middle of a BB, we split the BB into two and
  811. // put the 'delegate' BB in between. We normally create a split BB and make
  812. // it a successor of the original BB (PostSplit == true), but in case the BB
  813. // is an EH pad and the split pos is before 'catch', we should preserve the
  814. // BB's property, including that it is an EH pad, in the later part of the
  815. // BB, where 'catch' is. In this case we set PostSplit to false.
  816. bool PostSplit = true;
  817. if (EndBB->isEHPad()) {
  818. for (auto I = MachineBasicBlock::iterator(SplitPos), E = EndBB->end();
  819. I != E; ++I) {
  820. if (WebAssembly::isCatch(I->getOpcode())) {
  821. PostSplit = false;
  822. break;
  823. }
  824. }
  825. }
  826. MachineBasicBlock *PreBB = nullptr, *PostBB = nullptr;
  827. if (PostSplit) {
  828. // If the range's end instruction is in the middle of the BB, we split the
  829. // BB into two and insert the delegate BB in between.
  830. // - Before:
  831. // bb:
  832. // range_end
  833. // other_insts
  834. //
  835. // - After:
  836. // pre_bb: (previous 'bb')
  837. // range_end
  838. // delegate_bb: (new)
  839. // delegate
  840. // post_bb: (new)
  841. // other_insts
  842. PreBB = EndBB;
  843. PostBB = MF.CreateMachineBasicBlock();
  844. MF.insert(std::next(PreBB->getIterator()), PostBB);
  845. MF.insert(std::next(PreBB->getIterator()), DelegateBB);
  846. PostBB->splice(PostBB->end(), PreBB, SplitPos, PreBB->end());
  847. PostBB->transferSuccessors(PreBB);
  848. } else {
  849. // - Before:
  850. // ehpad:
  851. // range_end
  852. // catch
  853. // ...
  854. //
  855. // - After:
  856. // pre_bb: (new)
  857. // range_end
  858. // delegate_bb: (new)
  859. // delegate
  860. // post_bb: (previous 'ehpad')
  861. // catch
  862. // ...
  863. assert(EndBB->isEHPad());
  864. PreBB = MF.CreateMachineBasicBlock();
  865. PostBB = EndBB;
  866. MF.insert(PostBB->getIterator(), PreBB);
  867. MF.insert(PostBB->getIterator(), DelegateBB);
  868. PreBB->splice(PreBB->end(), PostBB, PostBB->begin(), SplitPos);
  869. // We don't need to transfer predecessors of the EH pad to 'PreBB',
  870. // because an EH pad's predecessors are all through unwind edges and they
  871. // should still unwind to the EH pad, not PreBB.
  872. }
  873. unstackifyVRegsUsedInSplitBB(*PreBB, *PostBB);
  874. PreBB->addSuccessor(DelegateBB);
  875. PreBB->addSuccessor(PostBB);
  876. }
  877. // Add 'delegate' instruction in the delegate BB created above.
  878. MachineInstr *Delegate = BuildMI(DelegateBB, RangeEnd->getDebugLoc(),
  879. TII.get(WebAssembly::DELEGATE))
  880. .addMBB(DelegateDest);
  881. registerTryScope(Try, Delegate, nullptr);
  882. }
  883. bool WebAssemblyCFGStackify::fixCallUnwindMismatches(MachineFunction &MF) {
  884. // Linearizing the control flow by placing TRY / END_TRY markers can create
  885. // mismatches in unwind destinations for throwing instructions, such as calls.
  886. //
  887. // We use the 'delegate' instruction to fix the unwind mismatches. 'delegate'
  888. // instruction delegates an exception to an outer 'catch'. It can target not
  889. // only 'catch' but all block-like structures including another 'delegate',
  890. // but with slightly different semantics than branches. When it targets a
  891. // 'catch', it will delegate the exception to that catch. It is being
  892. // discussed how to define the semantics when 'delegate''s target is a non-try
  893. // block: it will either be a validation failure or it will target the next
  894. // outer try-catch. But anyway our LLVM backend currently does not generate
  895. // such code. The example below illustrates where the 'delegate' instruction
  896. // in the middle will delegate the exception to, depending on the value of N.
  897. // try
  898. // try
  899. // block
  900. // try
  901. // try
  902. // call @foo
  903. // delegate N ;; Where will this delegate to?
  904. // catch ;; N == 0
  905. // end
  906. // end ;; N == 1 (invalid; will not be generated)
  907. // delegate ;; N == 2
  908. // catch ;; N == 3
  909. // end
  910. // ;; N == 4 (to caller)
  911. // 1. When an instruction may throw, but the EH pad it will unwind to can be
  912. // different from the original CFG.
  913. //
  914. // Example: we have the following CFG:
  915. // bb0:
  916. // call @foo ; if it throws, unwind to bb2
  917. // bb1:
  918. // call @bar ; if it throws, unwind to bb3
  919. // bb2 (ehpad):
  920. // catch
  921. // ...
  922. // bb3 (ehpad)
  923. // catch
  924. // ...
  925. //
  926. // And the CFG is sorted in this order. Then after placing TRY markers, it
  927. // will look like: (BB markers are omitted)
  928. // try
  929. // try
  930. // call @foo
  931. // call @bar ;; if it throws, unwind to bb3
  932. // catch ;; ehpad (bb2)
  933. // ...
  934. // end_try
  935. // catch ;; ehpad (bb3)
  936. // ...
  937. // end_try
  938. //
  939. // Now if bar() throws, it is going to end up ip in bb2, not bb3, where it
  940. // is supposed to end up. We solve this problem by wrapping the mismatching
  941. // call with an inner try-delegate that rethrows the exception to the right
  942. // 'catch'.
  943. //
  944. // try
  945. // try
  946. // call @foo
  947. // try ;; (new)
  948. // call @bar
  949. // delegate 1 (bb3) ;; (new)
  950. // catch ;; ehpad (bb2)
  951. // ...
  952. // end_try
  953. // catch ;; ehpad (bb3)
  954. // ...
  955. // end_try
  956. //
  957. // ---
  958. // 2. The same as 1, but in this case an instruction unwinds to a caller
  959. // function and not another EH pad.
  960. //
  961. // Example: we have the following CFG:
  962. // bb0:
  963. // call @foo ; if it throws, unwind to bb2
  964. // bb1:
  965. // call @bar ; if it throws, unwind to caller
  966. // bb2 (ehpad):
  967. // catch
  968. // ...
  969. //
  970. // And the CFG is sorted in this order. Then after placing TRY markers, it
  971. // will look like:
  972. // try
  973. // call @foo
  974. // call @bar ;; if it throws, unwind to caller
  975. // catch ;; ehpad (bb2)
  976. // ...
  977. // end_try
  978. //
  979. // Now if bar() throws, it is going to end up ip in bb2, when it is supposed
  980. // throw up to the caller. We solve this problem in the same way, but in this
  981. // case 'delegate's immediate argument is the number of block depths + 1,
  982. // which means it rethrows to the caller.
  983. // try
  984. // call @foo
  985. // try ;; (new)
  986. // call @bar
  987. // delegate 1 (caller) ;; (new)
  988. // catch ;; ehpad (bb2)
  989. // ...
  990. // end_try
  991. //
  992. // Before rewriteDepthImmediates, delegate's argument is a BB. In case of the
  993. // caller, it will take a fake BB generated by getFakeCallerBlock(), which
  994. // will be converted to a correct immediate argument later.
  995. //
  996. // In case there are multiple calls in a BB that may throw to the caller, they
  997. // can be wrapped together in one nested try-delegate scope. (In 1, this
  998. // couldn't happen, because may-throwing instruction there had an unwind
  999. // destination, i.e., it was an invoke before, and there could be only one
  1000. // invoke within a BB.)
  1001. SmallVector<const MachineBasicBlock *, 8> EHPadStack;
  1002. // Range of intructions to be wrapped in a new nested try/catch. A range
  1003. // exists in a single BB and does not span multiple BBs.
  1004. using TryRange = std::pair<MachineInstr *, MachineInstr *>;
  1005. // In original CFG, <unwind destination BB, a vector of try ranges>
  1006. DenseMap<MachineBasicBlock *, SmallVector<TryRange, 4>> UnwindDestToTryRanges;
  1007. // Gather possibly throwing calls (i.e., previously invokes) whose current
  1008. // unwind destination is not the same as the original CFG. (Case 1)
  1009. for (auto &MBB : reverse(MF)) {
  1010. bool SeenThrowableInstInBB = false;
  1011. for (auto &MI : reverse(MBB)) {
  1012. if (MI.getOpcode() == WebAssembly::TRY)
  1013. EHPadStack.pop_back();
  1014. else if (WebAssembly::isCatch(MI.getOpcode()))
  1015. EHPadStack.push_back(MI.getParent());
  1016. // In this loop we only gather calls that have an EH pad to unwind. So
  1017. // there will be at most 1 such call (= invoke) in a BB, so after we've
  1018. // seen one, we can skip the rest of BB. Also if MBB has no EH pad
  1019. // successor or MI does not throw, this is not an invoke.
  1020. if (SeenThrowableInstInBB || !MBB.hasEHPadSuccessor() ||
  1021. !WebAssembly::mayThrow(MI))
  1022. continue;
  1023. SeenThrowableInstInBB = true;
  1024. // If the EH pad on the stack top is where this instruction should unwind
  1025. // next, we're good.
  1026. MachineBasicBlock *UnwindDest = getFakeCallerBlock(MF);
  1027. for (auto *Succ : MBB.successors()) {
  1028. // Even though semantically a BB can have multiple successors in case an
  1029. // exception is not caught by a catchpad, in our backend implementation
  1030. // it is guaranteed that a BB can have at most one EH pad successor. For
  1031. // details, refer to comments in findWasmUnwindDestinations function in
  1032. // SelectionDAGBuilder.cpp.
  1033. if (Succ->isEHPad()) {
  1034. UnwindDest = Succ;
  1035. break;
  1036. }
  1037. }
  1038. if (EHPadStack.back() == UnwindDest)
  1039. continue;
  1040. // Include EH_LABELs in the range before and afer the invoke
  1041. MachineInstr *RangeBegin = &MI, *RangeEnd = &MI;
  1042. if (RangeBegin->getIterator() != MBB.begin() &&
  1043. std::prev(RangeBegin->getIterator())->isEHLabel())
  1044. RangeBegin = &*std::prev(RangeBegin->getIterator());
  1045. if (std::next(RangeEnd->getIterator()) != MBB.end() &&
  1046. std::next(RangeEnd->getIterator())->isEHLabel())
  1047. RangeEnd = &*std::next(RangeEnd->getIterator());
  1048. // If not, record the range.
  1049. UnwindDestToTryRanges[UnwindDest].push_back(
  1050. TryRange(RangeBegin, RangeEnd));
  1051. LLVM_DEBUG(dbgs() << "- Call unwind mismatch: MBB = " << MBB.getName()
  1052. << "\nCall = " << MI
  1053. << "\nOriginal dest = " << UnwindDest->getName()
  1054. << " Current dest = " << EHPadStack.back()->getName()
  1055. << "\n\n");
  1056. }
  1057. }
  1058. assert(EHPadStack.empty());
  1059. // Gather possibly throwing calls that are supposed to unwind up to the caller
  1060. // if they throw, but currently unwind to an incorrect destination. Unlike the
  1061. // loop above, there can be multiple calls within a BB that unwind to the
  1062. // caller, which we should group together in a range. (Case 2)
  1063. MachineInstr *RangeBegin = nullptr, *RangeEnd = nullptr; // inclusive
  1064. // Record the range.
  1065. auto RecordCallerMismatchRange = [&](const MachineBasicBlock *CurrentDest) {
  1066. UnwindDestToTryRanges[getFakeCallerBlock(MF)].push_back(
  1067. TryRange(RangeBegin, RangeEnd));
  1068. LLVM_DEBUG(dbgs() << "- Call unwind mismatch: MBB = "
  1069. << RangeBegin->getParent()->getName()
  1070. << "\nRange begin = " << *RangeBegin
  1071. << "Range end = " << *RangeEnd
  1072. << "\nOriginal dest = caller Current dest = "
  1073. << CurrentDest->getName() << "\n\n");
  1074. RangeBegin = RangeEnd = nullptr; // Reset range pointers
  1075. };
  1076. for (auto &MBB : reverse(MF)) {
  1077. bool SeenThrowableInstInBB = false;
  1078. for (auto &MI : reverse(MBB)) {
  1079. bool MayThrow = WebAssembly::mayThrow(MI);
  1080. // If MBB has an EH pad successor and this is the last instruction that
  1081. // may throw, this instruction unwinds to the EH pad and not to the
  1082. // caller.
  1083. if (MBB.hasEHPadSuccessor() && MayThrow && !SeenThrowableInstInBB)
  1084. SeenThrowableInstInBB = true;
  1085. // We wrap up the current range when we see a marker even if we haven't
  1086. // finished a BB.
  1087. else if (RangeEnd && WebAssembly::isMarker(MI.getOpcode()))
  1088. RecordCallerMismatchRange(EHPadStack.back());
  1089. // If EHPadStack is empty, that means it correctly unwinds to the caller
  1090. // if it throws, so we're good. If MI does not throw, we're good too.
  1091. else if (EHPadStack.empty() || !MayThrow) {
  1092. }
  1093. // We found an instruction that unwinds to the caller but currently has an
  1094. // incorrect unwind destination. Create a new range or increment the
  1095. // currently existing range.
  1096. else {
  1097. if (!RangeEnd)
  1098. RangeBegin = RangeEnd = &MI;
  1099. else
  1100. RangeBegin = &MI;
  1101. }
  1102. // Update EHPadStack.
  1103. if (MI.getOpcode() == WebAssembly::TRY)
  1104. EHPadStack.pop_back();
  1105. else if (WebAssembly::isCatch(MI.getOpcode()))
  1106. EHPadStack.push_back(MI.getParent());
  1107. }
  1108. if (RangeEnd)
  1109. RecordCallerMismatchRange(EHPadStack.back());
  1110. }
  1111. assert(EHPadStack.empty());
  1112. // We don't have any unwind destination mismatches to resolve.
  1113. if (UnwindDestToTryRanges.empty())
  1114. return false;
  1115. // Now we fix the mismatches by wrapping calls with inner try-delegates.
  1116. for (auto &P : UnwindDestToTryRanges) {
  1117. NumCallUnwindMismatches += P.second.size();
  1118. MachineBasicBlock *UnwindDest = P.first;
  1119. auto &TryRanges = P.second;
  1120. for (auto Range : TryRanges) {
  1121. MachineInstr *RangeBegin = nullptr, *RangeEnd = nullptr;
  1122. std::tie(RangeBegin, RangeEnd) = Range;
  1123. auto *MBB = RangeBegin->getParent();
  1124. // If this BB has an EH pad successor, i.e., ends with an 'invoke', now we
  1125. // are going to wrap the invoke with try-delegate, making the 'delegate'
  1126. // BB the new successor instead, so remove the EH pad succesor here. The
  1127. // BB may not have an EH pad successor if calls in this BB throw to the
  1128. // caller.
  1129. MachineBasicBlock *EHPad = nullptr;
  1130. for (auto *Succ : MBB->successors()) {
  1131. if (Succ->isEHPad()) {
  1132. EHPad = Succ;
  1133. break;
  1134. }
  1135. }
  1136. if (EHPad)
  1137. MBB->removeSuccessor(EHPad);
  1138. addTryDelegate(RangeBegin, RangeEnd, UnwindDest);
  1139. }
  1140. }
  1141. return true;
  1142. }
  1143. bool WebAssemblyCFGStackify::fixCatchUnwindMismatches(MachineFunction &MF) {
  1144. // There is another kind of unwind destination mismatches besides call unwind
  1145. // mismatches, which we will call "catch unwind mismatches". See this example
  1146. // after the marker placement:
  1147. // try
  1148. // try
  1149. // call @foo
  1150. // catch __cpp_exception ;; ehpad A (next unwind dest: caller)
  1151. // ...
  1152. // end_try
  1153. // catch_all ;; ehpad B
  1154. // ...
  1155. // end_try
  1156. //
  1157. // 'call @foo's unwind destination is the ehpad A. But suppose 'call @foo'
  1158. // throws a foreign exception that is not caught by ehpad A, and its next
  1159. // destination should be the caller. But after control flow linearization,
  1160. // another EH pad can be placed in between (e.g. ehpad B here), making the
  1161. // next unwind destination incorrect. In this case, the foreign exception
  1162. // will instead go to ehpad B and will be caught there instead. In this
  1163. // example the correct next unwind destination is the caller, but it can be
  1164. // another outer catch in other cases.
  1165. //
  1166. // There is no specific 'call' or 'throw' instruction to wrap with a
  1167. // try-delegate, so we wrap the whole try-catch-end with a try-delegate and
  1168. // make it rethrow to the right destination, as in the example below:
  1169. // try
  1170. // try ;; (new)
  1171. // try
  1172. // call @foo
  1173. // catch __cpp_exception ;; ehpad A (next unwind dest: caller)
  1174. // ...
  1175. // end_try
  1176. // delegate 1 (caller) ;; (new)
  1177. // catch_all ;; ehpad B
  1178. // ...
  1179. // end_try
  1180. const auto *EHInfo = MF.getWasmEHFuncInfo();
  1181. SmallVector<const MachineBasicBlock *, 8> EHPadStack;
  1182. // For EH pads that have catch unwind mismatches, a map of <EH pad, its
  1183. // correct unwind destination>.
  1184. DenseMap<MachineBasicBlock *, MachineBasicBlock *> EHPadToUnwindDest;
  1185. for (auto &MBB : reverse(MF)) {
  1186. for (auto &MI : reverse(MBB)) {
  1187. if (MI.getOpcode() == WebAssembly::TRY)
  1188. EHPadStack.pop_back();
  1189. else if (MI.getOpcode() == WebAssembly::DELEGATE)
  1190. EHPadStack.push_back(&MBB);
  1191. else if (WebAssembly::isCatch(MI.getOpcode())) {
  1192. auto *EHPad = &MBB;
  1193. // catch_all always catches an exception, so we don't need to do
  1194. // anything
  1195. if (MI.getOpcode() == WebAssembly::CATCH_ALL) {
  1196. }
  1197. // This can happen when the unwind dest was removed during the
  1198. // optimization, e.g. because it was unreachable.
  1199. else if (EHPadStack.empty() && EHInfo->hasUnwindDest(EHPad)) {
  1200. LLVM_DEBUG(dbgs() << "EHPad (" << EHPad->getName()
  1201. << "'s unwind destination does not exist anymore"
  1202. << "\n\n");
  1203. }
  1204. // The EHPad's next unwind destination is the caller, but we incorrectly
  1205. // unwind to another EH pad.
  1206. else if (!EHPadStack.empty() && !EHInfo->hasUnwindDest(EHPad)) {
  1207. EHPadToUnwindDest[EHPad] = getFakeCallerBlock(MF);
  1208. LLVM_DEBUG(dbgs()
  1209. << "- Catch unwind mismatch:\nEHPad = " << EHPad->getName()
  1210. << " Original dest = caller Current dest = "
  1211. << EHPadStack.back()->getName() << "\n\n");
  1212. }
  1213. // The EHPad's next unwind destination is an EH pad, whereas we
  1214. // incorrectly unwind to another EH pad.
  1215. else if (!EHPadStack.empty() && EHInfo->hasUnwindDest(EHPad)) {
  1216. auto *UnwindDest = EHInfo->getUnwindDest(EHPad);
  1217. if (EHPadStack.back() != UnwindDest) {
  1218. EHPadToUnwindDest[EHPad] = UnwindDest;
  1219. LLVM_DEBUG(dbgs() << "- Catch unwind mismatch:\nEHPad = "
  1220. << EHPad->getName() << " Original dest = "
  1221. << UnwindDest->getName() << " Current dest = "
  1222. << EHPadStack.back()->getName() << "\n\n");
  1223. }
  1224. }
  1225. EHPadStack.push_back(EHPad);
  1226. }
  1227. }
  1228. }
  1229. assert(EHPadStack.empty());
  1230. if (EHPadToUnwindDest.empty())
  1231. return false;
  1232. NumCatchUnwindMismatches += EHPadToUnwindDest.size();
  1233. SmallPtrSet<MachineBasicBlock *, 4> NewEndTryBBs;
  1234. for (auto &P : EHPadToUnwindDest) {
  1235. MachineBasicBlock *EHPad = P.first;
  1236. MachineBasicBlock *UnwindDest = P.second;
  1237. MachineInstr *Try = EHPadToTry[EHPad];
  1238. MachineInstr *EndTry = BeginToEnd[Try];
  1239. addTryDelegate(Try, EndTry, UnwindDest);
  1240. NewEndTryBBs.insert(EndTry->getParent());
  1241. }
  1242. // Adding a try-delegate wrapping an existing try-catch-end can make existing
  1243. // branch destination BBs invalid. For example,
  1244. //
  1245. // - Before:
  1246. // bb0:
  1247. // block
  1248. // br bb3
  1249. // bb1:
  1250. // try
  1251. // ...
  1252. // bb2: (ehpad)
  1253. // catch
  1254. // bb3:
  1255. // end_try
  1256. // end_block ;; 'br bb3' targets here
  1257. //
  1258. // Suppose this try-catch-end has a catch unwind mismatch, so we need to wrap
  1259. // this with a try-delegate. Then this becomes:
  1260. //
  1261. // - After:
  1262. // bb0:
  1263. // block
  1264. // br bb3 ;; invalid destination!
  1265. // bb1:
  1266. // try ;; (new instruction)
  1267. // try
  1268. // ...
  1269. // bb2: (ehpad)
  1270. // catch
  1271. // bb3:
  1272. // end_try ;; 'br bb3' still incorrectly targets here!
  1273. // delegate_bb: ;; (new BB)
  1274. // delegate ;; (new instruction)
  1275. // split_bb: ;; (new BB)
  1276. // end_block
  1277. //
  1278. // Now 'br bb3' incorrectly branches to an inner scope.
  1279. //
  1280. // As we can see in this case, when branches target a BB that has both
  1281. // 'end_try' and 'end_block' and the BB is split to insert a 'delegate', we
  1282. // have to remap existing branch destinations so that they target not the
  1283. // 'end_try' BB but the new 'end_block' BB. There can be multiple 'delegate's
  1284. // in between, so we try to find the next BB with 'end_block' instruction. In
  1285. // this example, the 'br bb3' instruction should be remapped to 'br split_bb'.
  1286. for (auto &MBB : MF) {
  1287. for (auto &MI : MBB) {
  1288. if (MI.isTerminator()) {
  1289. for (auto &MO : MI.operands()) {
  1290. if (MO.isMBB() && NewEndTryBBs.count(MO.getMBB())) {
  1291. auto *BrDest = MO.getMBB();
  1292. bool FoundEndBlock = false;
  1293. for (; std::next(BrDest->getIterator()) != MF.end();
  1294. BrDest = BrDest->getNextNode()) {
  1295. for (const auto &MI : *BrDest) {
  1296. if (MI.getOpcode() == WebAssembly::END_BLOCK) {
  1297. FoundEndBlock = true;
  1298. break;
  1299. }
  1300. }
  1301. if (FoundEndBlock)
  1302. break;
  1303. }
  1304. assert(FoundEndBlock);
  1305. MO.setMBB(BrDest);
  1306. }
  1307. }
  1308. }
  1309. }
  1310. }
  1311. return true;
  1312. }
  1313. void WebAssemblyCFGStackify::recalculateScopeTops(MachineFunction &MF) {
  1314. // Renumber BBs and recalculate ScopeTop info because new BBs might have been
  1315. // created and inserted during fixing unwind mismatches.
  1316. MF.RenumberBlocks();
  1317. ScopeTops.clear();
  1318. ScopeTops.resize(MF.getNumBlockIDs());
  1319. for (auto &MBB : reverse(MF)) {
  1320. for (auto &MI : reverse(MBB)) {
  1321. if (ScopeTops[MBB.getNumber()])
  1322. break;
  1323. switch (MI.getOpcode()) {
  1324. case WebAssembly::END_BLOCK:
  1325. case WebAssembly::END_LOOP:
  1326. case WebAssembly::END_TRY:
  1327. case WebAssembly::DELEGATE:
  1328. updateScopeTops(EndToBegin[&MI]->getParent(), &MBB);
  1329. break;
  1330. case WebAssembly::CATCH:
  1331. case WebAssembly::CATCH_ALL:
  1332. updateScopeTops(EHPadToTry[&MBB]->getParent(), &MBB);
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. }
  1338. /// In normal assembly languages, when the end of a function is unreachable,
  1339. /// because the function ends in an infinite loop or a noreturn call or similar,
  1340. /// it isn't necessary to worry about the function return type at the end of
  1341. /// the function, because it's never reached. However, in WebAssembly, blocks
  1342. /// that end at the function end need to have a return type signature that
  1343. /// matches the function signature, even though it's unreachable. This function
  1344. /// checks for such cases and fixes up the signatures.
  1345. void WebAssemblyCFGStackify::fixEndsAtEndOfFunction(MachineFunction &MF) {
  1346. const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  1347. if (MFI.getResults().empty())
  1348. return;
  1349. // MCInstLower will add the proper types to multivalue signatures based on the
  1350. // function return type
  1351. WebAssembly::BlockType RetType =
  1352. MFI.getResults().size() > 1
  1353. ? WebAssembly::BlockType::Multivalue
  1354. : WebAssembly::BlockType(
  1355. WebAssembly::toValType(MFI.getResults().front()));
  1356. SmallVector<MachineBasicBlock::reverse_iterator, 4> Worklist;
  1357. Worklist.push_back(MF.rbegin()->rbegin());
  1358. auto Process = [&](MachineBasicBlock::reverse_iterator It) {
  1359. auto *MBB = It->getParent();
  1360. while (It != MBB->rend()) {
  1361. MachineInstr &MI = *It++;
  1362. if (MI.isPosition() || MI.isDebugInstr())
  1363. continue;
  1364. switch (MI.getOpcode()) {
  1365. case WebAssembly::END_TRY: {
  1366. // If a 'try''s return type is fixed, both its try body and catch body
  1367. // should satisfy the return type, so we need to search 'end'
  1368. // instructions before its corresponding 'catch' too.
  1369. auto *EHPad = TryToEHPad.lookup(EndToBegin[&MI]);
  1370. assert(EHPad);
  1371. auto NextIt =
  1372. std::next(WebAssembly::findCatch(EHPad)->getReverseIterator());
  1373. if (NextIt != EHPad->rend())
  1374. Worklist.push_back(NextIt);
  1375. [[fallthrough]];
  1376. }
  1377. case WebAssembly::END_BLOCK:
  1378. case WebAssembly::END_LOOP:
  1379. case WebAssembly::DELEGATE:
  1380. EndToBegin[&MI]->getOperand(0).setImm(int32_t(RetType));
  1381. continue;
  1382. default:
  1383. // Something other than an `end`. We're done for this BB.
  1384. return;
  1385. }
  1386. }
  1387. // We've reached the beginning of a BB. Continue the search in the previous
  1388. // BB.
  1389. Worklist.push_back(MBB->getPrevNode()->rbegin());
  1390. };
  1391. while (!Worklist.empty())
  1392. Process(Worklist.pop_back_val());
  1393. }
  1394. // WebAssembly functions end with an end instruction, as if the function body
  1395. // were a block.
  1396. static void appendEndToFunction(MachineFunction &MF,
  1397. const WebAssemblyInstrInfo &TII) {
  1398. BuildMI(MF.back(), MF.back().end(),
  1399. MF.back().findPrevDebugLoc(MF.back().end()),
  1400. TII.get(WebAssembly::END_FUNCTION));
  1401. }
  1402. /// Insert LOOP/TRY/BLOCK markers at appropriate places.
  1403. void WebAssemblyCFGStackify::placeMarkers(MachineFunction &MF) {
  1404. // We allocate one more than the number of blocks in the function to
  1405. // accommodate for the possible fake block we may insert at the end.
  1406. ScopeTops.resize(MF.getNumBlockIDs() + 1);
  1407. // Place the LOOP for MBB if MBB is the header of a loop.
  1408. for (auto &MBB : MF)
  1409. placeLoopMarker(MBB);
  1410. const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
  1411. for (auto &MBB : MF) {
  1412. if (MBB.isEHPad()) {
  1413. // Place the TRY for MBB if MBB is the EH pad of an exception.
  1414. if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
  1415. MF.getFunction().hasPersonalityFn())
  1416. placeTryMarker(MBB);
  1417. } else {
  1418. // Place the BLOCK for MBB if MBB is branched to from above.
  1419. placeBlockMarker(MBB);
  1420. }
  1421. }
  1422. // Fix mismatches in unwind destinations induced by linearizing the code.
  1423. if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
  1424. MF.getFunction().hasPersonalityFn()) {
  1425. bool Changed = fixCallUnwindMismatches(MF);
  1426. Changed |= fixCatchUnwindMismatches(MF);
  1427. if (Changed)
  1428. recalculateScopeTops(MF);
  1429. }
  1430. }
  1431. unsigned WebAssemblyCFGStackify::getBranchDepth(
  1432. const SmallVectorImpl<EndMarkerInfo> &Stack, const MachineBasicBlock *MBB) {
  1433. unsigned Depth = 0;
  1434. for (auto X : reverse(Stack)) {
  1435. if (X.first == MBB)
  1436. break;
  1437. ++Depth;
  1438. }
  1439. assert(Depth < Stack.size() && "Branch destination should be in scope");
  1440. return Depth;
  1441. }
  1442. unsigned WebAssemblyCFGStackify::getDelegateDepth(
  1443. const SmallVectorImpl<EndMarkerInfo> &Stack, const MachineBasicBlock *MBB) {
  1444. if (MBB == FakeCallerBB)
  1445. return Stack.size();
  1446. // Delegate's destination is either a catch or a another delegate BB. When the
  1447. // destination is another delegate, we can compute the argument in the same
  1448. // way as branches, because the target delegate BB only contains the single
  1449. // delegate instruction.
  1450. if (!MBB->isEHPad()) // Target is a delegate BB
  1451. return getBranchDepth(Stack, MBB);
  1452. // When the delegate's destination is a catch BB, we need to use its
  1453. // corresponding try's end_try BB because Stack contains each marker's end BB.
  1454. // Also we need to check if the end marker instruction matches, because a
  1455. // single BB can contain multiple end markers, like this:
  1456. // bb:
  1457. // END_BLOCK
  1458. // END_TRY
  1459. // END_BLOCK
  1460. // END_TRY
  1461. // ...
  1462. //
  1463. // In case of branches getting the immediate that targets any of these is
  1464. // fine, but delegate has to exactly target the correct try.
  1465. unsigned Depth = 0;
  1466. const MachineInstr *EndTry = BeginToEnd[EHPadToTry[MBB]];
  1467. for (auto X : reverse(Stack)) {
  1468. if (X.first == EndTry->getParent() && X.second == EndTry)
  1469. break;
  1470. ++Depth;
  1471. }
  1472. assert(Depth < Stack.size() && "Delegate destination should be in scope");
  1473. return Depth;
  1474. }
  1475. unsigned WebAssemblyCFGStackify::getRethrowDepth(
  1476. const SmallVectorImpl<EndMarkerInfo> &Stack,
  1477. const SmallVectorImpl<const MachineBasicBlock *> &EHPadStack) {
  1478. unsigned Depth = 0;
  1479. // In our current implementation, rethrows always rethrow the exception caught
  1480. // by the innermost enclosing catch. This means while traversing Stack in the
  1481. // reverse direction, when we encounter END_TRY, we should check if the
  1482. // END_TRY corresponds to the current innermost EH pad. For example:
  1483. // try
  1484. // ...
  1485. // catch ;; (a)
  1486. // try
  1487. // rethrow 1 ;; (b)
  1488. // catch ;; (c)
  1489. // rethrow 0 ;; (d)
  1490. // end ;; (e)
  1491. // end ;; (f)
  1492. //
  1493. // When we are at 'rethrow' (d), while reversely traversing Stack the first
  1494. // 'end' we encounter is the 'end' (e), which corresponds to the 'catch' (c).
  1495. // And 'rethrow' (d) rethrows the exception caught by 'catch' (c), so we stop
  1496. // there and the depth should be 0. But when we are at 'rethrow' (b), it
  1497. // rethrows the exception caught by 'catch' (a), so when traversing Stack
  1498. // reversely, we should skip the 'end' (e) and choose 'end' (f), which
  1499. // corresponds to 'catch' (a).
  1500. for (auto X : reverse(Stack)) {
  1501. const MachineInstr *End = X.second;
  1502. if (End->getOpcode() == WebAssembly::END_TRY) {
  1503. auto *EHPad = TryToEHPad[EndToBegin[End]];
  1504. if (EHPadStack.back() == EHPad)
  1505. break;
  1506. }
  1507. ++Depth;
  1508. }
  1509. assert(Depth < Stack.size() && "Rethrow destination should be in scope");
  1510. return Depth;
  1511. }
  1512. void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
  1513. // Now rewrite references to basic blocks to be depth immediates.
  1514. SmallVector<EndMarkerInfo, 8> Stack;
  1515. SmallVector<const MachineBasicBlock *, 8> EHPadStack;
  1516. for (auto &MBB : reverse(MF)) {
  1517. for (MachineInstr &MI : llvm::reverse(MBB)) {
  1518. switch (MI.getOpcode()) {
  1519. case WebAssembly::BLOCK:
  1520. case WebAssembly::TRY:
  1521. assert(ScopeTops[Stack.back().first->getNumber()]->getNumber() <=
  1522. MBB.getNumber() &&
  1523. "Block/try marker should be balanced");
  1524. Stack.pop_back();
  1525. break;
  1526. case WebAssembly::LOOP:
  1527. assert(Stack.back().first == &MBB && "Loop top should be balanced");
  1528. Stack.pop_back();
  1529. break;
  1530. case WebAssembly::END_BLOCK:
  1531. Stack.push_back(std::make_pair(&MBB, &MI));
  1532. break;
  1533. case WebAssembly::END_TRY: {
  1534. // We handle DELEGATE in the default level, because DELEGATE has
  1535. // immediate operands to rewrite.
  1536. Stack.push_back(std::make_pair(&MBB, &MI));
  1537. auto *EHPad = TryToEHPad[EndToBegin[&MI]];
  1538. EHPadStack.push_back(EHPad);
  1539. break;
  1540. }
  1541. case WebAssembly::END_LOOP:
  1542. Stack.push_back(std::make_pair(EndToBegin[&MI]->getParent(), &MI));
  1543. break;
  1544. case WebAssembly::CATCH:
  1545. case WebAssembly::CATCH_ALL:
  1546. EHPadStack.pop_back();
  1547. break;
  1548. case WebAssembly::RETHROW:
  1549. MI.getOperand(0).setImm(getRethrowDepth(Stack, EHPadStack));
  1550. break;
  1551. default:
  1552. if (MI.isTerminator()) {
  1553. // Rewrite MBB operands to be depth immediates.
  1554. SmallVector<MachineOperand, 4> Ops(MI.operands());
  1555. while (MI.getNumOperands() > 0)
  1556. MI.removeOperand(MI.getNumOperands() - 1);
  1557. for (auto MO : Ops) {
  1558. if (MO.isMBB()) {
  1559. if (MI.getOpcode() == WebAssembly::DELEGATE)
  1560. MO = MachineOperand::CreateImm(
  1561. getDelegateDepth(Stack, MO.getMBB()));
  1562. else
  1563. MO = MachineOperand::CreateImm(
  1564. getBranchDepth(Stack, MO.getMBB()));
  1565. }
  1566. MI.addOperand(MF, MO);
  1567. }
  1568. }
  1569. if (MI.getOpcode() == WebAssembly::DELEGATE)
  1570. Stack.push_back(std::make_pair(&MBB, &MI));
  1571. break;
  1572. }
  1573. }
  1574. }
  1575. assert(Stack.empty() && "Control flow should be balanced");
  1576. }
  1577. void WebAssemblyCFGStackify::cleanupFunctionData(MachineFunction &MF) {
  1578. if (FakeCallerBB)
  1579. MF.deleteMachineBasicBlock(FakeCallerBB);
  1580. AppendixBB = FakeCallerBB = nullptr;
  1581. }
  1582. void WebAssemblyCFGStackify::releaseMemory() {
  1583. ScopeTops.clear();
  1584. BeginToEnd.clear();
  1585. EndToBegin.clear();
  1586. TryToEHPad.clear();
  1587. EHPadToTry.clear();
  1588. }
  1589. bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
  1590. LLVM_DEBUG(dbgs() << "********** CFG Stackifying **********\n"
  1591. "********** Function: "
  1592. << MF.getName() << '\n');
  1593. const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
  1594. releaseMemory();
  1595. // Liveness is not tracked for VALUE_STACK physreg.
  1596. MF.getRegInfo().invalidateLiveness();
  1597. // Place the BLOCK/LOOP/TRY markers to indicate the beginnings of scopes.
  1598. placeMarkers(MF);
  1599. // Remove unnecessary instructions possibly introduced by try/end_trys.
  1600. if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
  1601. MF.getFunction().hasPersonalityFn())
  1602. removeUnnecessaryInstrs(MF);
  1603. // Convert MBB operands in terminators to relative depth immediates.
  1604. rewriteDepthImmediates(MF);
  1605. // Fix up block/loop/try signatures at the end of the function to conform to
  1606. // WebAssembly's rules.
  1607. fixEndsAtEndOfFunction(MF);
  1608. // Add an end instruction at the end of the function body.
  1609. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  1610. if (!MF.getSubtarget<WebAssemblySubtarget>()
  1611. .getTargetTriple()
  1612. .isOSBinFormatELF())
  1613. appendEndToFunction(MF, TII);
  1614. cleanupFunctionData(MF);
  1615. MF.getInfo<WebAssemblyFunctionInfo>()->setCFGStackified();
  1616. return true;
  1617. }