43#define DEBUG_TYPE "wasm-cfg-stackify"
45STATISTIC(NumCallUnwindMismatches,
"Number of call unwind mismatches found");
46STATISTIC(NumCatchUnwindMismatches,
"Number of catch unwind mismatches found");
69 int EndNo =
End->getNumber();
70 if (!ScopeTops[EndNo] || ScopeTops[EndNo]->getNumber() > BeginNo)
71 ScopeTops[EndNo] = Begin;
97 std::pair<const MachineBasicBlock *, const MachineInstr *>;
120 UnwindDestToTrampoline;
132 if (CallerTrampolineBB)
133 MF.
insert(CallerTrampolineBB->getIterator(), AppendixBB);
144 if (!CallerTrampolineBB) {
148 return CallerTrampolineBB;
179char WebAssemblyCFGStackify::ID = 0;
182 "Insert BLOCK/LOOP/TRY/TRY_TABLE markers for WebAssembly scopes",
false,
186 return new WebAssemblyCFGStackify();
198 if (MO.isMBB() && MO.getMBB() ==
MBB)
208template <
typename Container>
211 const Container &AfterSet) {
212 auto InsertPos =
MBB->
end();
214 if (BeforeSet.count(&*std::prev(InsertPos))) {
217 for (
auto Pos = InsertPos, E =
MBB->
begin(); Pos != E; --Pos)
218 assert(!AfterSet.count(&*std::prev(Pos)));
232template <
typename Container>
235 const Container &AfterSet) {
237 while (InsertPos !=
MBB->
end()) {
238 if (AfterSet.count(&*InsertPos)) {
241 for (
auto Pos = InsertPos, E =
MBB->
end(); Pos != E; ++Pos)
242 assert(!BeforeSet.count(&*Pos));
251void WebAssemblyCFGStackify::registerScope(
MachineInstr *Begin,
253 BeginToEnd[Begin] =
End;
254 EndToBegin[
End] = Begin;
258void WebAssemblyCFGStackify::registerTryScope(
MachineInstr *Begin,
261 registerScope(Begin,
End);
262 TryToEHPad[Begin] = EHPad;
263 EHPadToTry[EHPad] = Begin;
266void WebAssemblyCFGStackify::unregisterScope(
MachineInstr *Begin) {
270 BeginToEnd.
erase(Begin);
275 TryToEHPad.
erase(Begin);
276 EHPadToTry.
erase(EHPad);
293 bool IsBranchedTo =
false;
296 if (Pred->getNumber() < MBBNumber) {
297 Header = Header ? MDT->findNearestCommonDominator(Header, Pred) : Pred;
307 assert(&
MBB != &MF.
front() &&
"Header blocks shouldn't have predecessors");
314 if (ScopeTop->getNumber() > Header->getNumber()) {
316 I = std::next(ScopeTop->getIterator());
331 for (
const auto &
MI : *Header) {
335 if (
MI.getOpcode() == WebAssembly::LOOP) {
336 auto *LoopBottom = BeginToEnd[&
MI]->getParent()->getPrevNode();
349 if (
MI.getOpcode() == WebAssembly::BLOCK ||
350 MI.getOpcode() == WebAssembly::TRY ||
351 MI.getOpcode() == WebAssembly::TRY_TABLE) {
362 if (
MI.getOpcode() == WebAssembly::END_BLOCK ||
363 MI.getOpcode() == WebAssembly::END_LOOP ||
364 MI.getOpcode() == WebAssembly::END_TRY ||
365 MI.getOpcode() == WebAssembly::END_TRY_TABLE)
370 if (
MI.isTerminator())
375 for (
auto I = Header->getFirstTerminator(), E = Header->begin();
I != E;
377 if (std::prev(
I)->isDebugInstr() || std::prev(
I)->isPosition())
380 AfterSet.
insert(&*std::prev(
I));
389 BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
390 TII.get(WebAssembly::BLOCK))
391 .
addImm(int64_t(ReturnType));
396 for (
auto &
MI :
MBB) {
399 if (
MI.getOpcode() == WebAssembly::LOOP)
412 if (
MI.getOpcode() == WebAssembly::END_LOOP ||
413 MI.getOpcode() == WebAssembly::END_TRY) {
414 if (EndToBegin[&
MI]->
getParent()->getNumber() >= Header->getNumber())
426 TII.get(WebAssembly::END_BLOCK));
427 registerScope(Begin,
End);
430 updateScopeTops(Header, &
MBB);
436 const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
437 const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
449 if (Iter == MF.
end()) {
450 getAppendixBlock(MF);
458 for (
const auto &
MI :
MBB) {
461 if (
MI.getOpcode() == WebAssembly::END_LOOP)
472 TII.get(WebAssembly::LOOP))
473 .
addImm(int64_t(WebAssembly::BlockType::Void));
479 for (
const auto &
MI :
MBB)
481 if (
MI.getOpcode() == WebAssembly::END_LOOP)
490 : (*AfterLoop->pred_rbegin())->findBranchDebugLoc();
492 BuildMI(*AfterLoop, InsertPos, EndDL,
TII.get(WebAssembly::END_LOOP));
493 registerScope(Begin,
End);
497 "With block sorting the outermost loop for a block should be first.");
498 updateScopeTops(&
MBB, AfterLoop);
504 auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
506 const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
507 const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
515 if (Pred->getNumber() < MBBNumber) {
516 Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
518 "Explicit branch to an EH pad!");
530 if (Iter == MF.
end()) {
531 getAppendixBlock(MF);
540 if (ScopeTop->getNumber() > Header->getNumber()) {
542 I = std::next(ScopeTop->getIterator());
557 for (
const auto &
MI : *Header) {
561 if (
MI.getOpcode() == WebAssembly::LOOP) {
562 auto *LoopBottom = BeginToEnd[&
MI]->getParent()->getPrevNode();
573 if (
MI.getOpcode() == WebAssembly::BLOCK ||
574 MI.getOpcode() == WebAssembly::TRY)
579 if (
MI.getOpcode() == WebAssembly::END_BLOCK ||
580 MI.getOpcode() == WebAssembly::END_LOOP ||
581 MI.getOpcode() == WebAssembly::END_TRY)
586 if (
MI.isTerminator())
597 auto TermPos = Header->getFirstTerminator();
598 if (TermPos == Header->end() ||
599 TermPos->getOpcode() != WebAssembly::RETHROW) {
606 if (
MI.getIterator() != Header->begin() &&
607 std::prev(
MI.getIterator())->isEHLabel()) {
608 AfterSet.
insert(&*std::prev(
MI.getIterator()));
609 ThrowingCall = &*std::prev(
MI.getIterator());
624 : Header->getFirstTerminator();
625 for (
auto I = SearchStartPt, E = Header->begin();
I != E; --
I) {
626 if (std::prev(
I)->isDebugInstr() || std::prev(
I)->isPosition())
629 AfterSet.
insert(&*std::prev(
I));
637 BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
638 TII.get(WebAssembly::TRY))
639 .
addImm(int64_t(WebAssembly::BlockType::Void));
644 for (
const auto &
MI : *Cont) {
647 if (
MI.getOpcode() == WebAssembly::LOOP)
652 if (
MI.getOpcode() == WebAssembly::END_TRY)
660 if (
MI.getOpcode() == WebAssembly::END_LOOP) {
663 if (EndToBegin[&
MI]->
getParent()->getNumber() > Header->getNumber())
677 TII.get(WebAssembly::END_TRY));
678 registerTryScope(Begin,
End, &
MBB);
691 for (
auto *
End : {&
MBB, Cont})
692 updateScopeTops(Header,
End);
698 auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
700 const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
701 const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
709 if (Pred->getNumber() < MBBNumber) {
710 Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
712 "Explicit branch to an EH pad!");
725 if (Iter == MF.
end())
733 if (ScopeTop->getNumber() > Header->getNumber()) {
735 I = std::next(ScopeTop->getIterator());
750 for (
const auto &
MI : *Header) {
754 if (
MI.getOpcode() == WebAssembly::LOOP) {
755 auto *LoopBottom = BeginToEnd[&
MI]->getParent()->getPrevNode();
766 if (
MI.getOpcode() == WebAssembly::BLOCK ||
767 MI.getOpcode() == WebAssembly::TRY_TABLE)
772 if (
MI.getOpcode() == WebAssembly::END_BLOCK ||
773 MI.getOpcode() == WebAssembly::END_LOOP ||
774 MI.getOpcode() == WebAssembly::END_TRY_TABLE)
779 if (
MI.isTerminator())
790 auto TermPos = Header->getFirstTerminator();
791 if (TermPos == Header->end() ||
792 TermPos->getOpcode() != WebAssembly::RETHROW) {
799 if (
MI.getIterator() != Header->begin() &&
800 std::prev(
MI.getIterator())->isEHLabel()) {
801 AfterSet.
insert(&*std::prev(
MI.getIterator()));
802 ThrowingCall = &*std::prev(
MI.getIterator());
817 : Header->getFirstTerminator();
818 for (
auto I = SearchStartPt, E = Header->begin();
I != E; --
I) {
819 if (std::prev(
I)->isDebugInstr() || std::prev(
I)->isPosition())
822 AfterSet.
insert(&*std::prev(
I));
842 BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
843 TII.get(WebAssembly::BLOCK));
846 BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
847 TII.get(WebAssembly::TRY_TABLE))
848 .
addImm(int64_t(WebAssembly::BlockType::Void))
850 auto *TryTable = TryTableMIB.
getInstr();
856 switch (
Catch->getOpcode()) {
857 case WebAssembly::CATCH:
860 BlockMIB.
addImm(int64_t(WebAssembly::BlockType::I32));
862 for (
const auto &
Use :
Catch->uses()) {
869 case WebAssembly::CATCH_REF:
874 BlockMIB.
addImm(int64_t(WebAssembly::BlockType::Multivalue));
877 for (
const auto &
Use :
Catch->uses()) {
883 case WebAssembly::CATCH_ALL:
885 BlockMIB.
addImm(int64_t(WebAssembly::BlockType::Void));
889 case WebAssembly::CATCH_ALL_REF:
891 BlockMIB.
addImm(int64_t(WebAssembly::BlockType::Exnref));
901 for (
const auto &
MI :
MBB) {
904 if (
MI.getOpcode() == WebAssembly::LOOP)
912 if (
MI.getOpcode() == WebAssembly::END_LOOP) {
913 if (EndToBegin[&
MI]->
getParent()->getNumber() >= Header->getNumber())
934 TII.get(WebAssembly::END_TRY_TABLE));
935 registerTryScope(TryTable, EndTryTable, &
MBB);
938 TII.get(WebAssembly::END_BLOCK));
939 registerScope(
Block, EndBlock);
981 for (
auto *
End : {&
MBB, Cont})
982 updateScopeTops(Header,
End);
985void WebAssemblyCFGStackify::removeUnnecessaryInstrs(
MachineFunction &MF) {
1021 for (
auto &
MBB : MF) {
1045 if (Analyzable && ((
Cond.empty() &&
TBB &&
TBB == Cont) ||
1046 (!
Cond.empty() && FBB && FBB == Cont))) {
1047 bool ErasedUncondBr =
false;
1048 (void)ErasedUncondBr;
1049 for (
auto I = EHPadLayoutPred->
end(), E = EHPadLayoutPred->
begin();
1051 auto PrevI = std::prev(
I);
1052 if (PrevI->isTerminator()) {
1053 assert(PrevI->getOpcode() == WebAssembly::BR);
1054 PrevI->eraseFromParent();
1055 ErasedUncondBr =
true;
1059 assert(ErasedUncondBr &&
"Unconditional branch not erased!");
1075 for (
auto &
MBB : MF) {
1076 for (
auto &
MI :
MBB) {
1077 if (
MI.getOpcode() != WebAssembly::TRY)
1080 if (EndTry->getOpcode() == WebAssembly::DELEGATE)
1086 for (
auto B = Try->
getIterator(), E = std::next(EndTry->getIterator());
1087 B != TryBB->
begin() && E != Cont->
end() &&
1088 std::prev(
B)->getOpcode() == WebAssembly::BLOCK &&
1089 E->getOpcode() == WebAssembly::END_BLOCK &&
1090 std::prev(
B)->getOperand(0).getImm() == RetType;
1097 for (
auto *
MI : ToDelete) {
1098 if (
MI->getOpcode() == WebAssembly::BLOCK)
1099 unregisterScope(
MI);
1100 MI->eraseFromParent();
1113 for (
auto &
MI : Split) {
1114 for (
auto &MO :
MI.explicit_uses()) {
1115 if (!MO.isReg() || MO.getReg().isPhysical())
1118 if (Def->getParent() == &
MBB)
1119 MFI.unstackifyVReg(MO.getReg());
1152 if (!MFI.isVRegStackified(TeeReg)) {
1154 MFI.unstackifyVReg(DefReg);
1160 MI.eraseFromParent();
1167void WebAssemblyCFGStackify::addNestedTryDelegate(
1170 auto *BeginBB = RangeBegin->
getParent();
1179 AfterSet.
insert(RangeBegin);
1182 if (std::prev(
I)->isDebugInstr() || std::prev(
I)->isPosition())
1185 AfterSet.
insert(&*std::prev(
I));
1194 TII.get(WebAssembly::TRY))
1195 .
addImm(int64_t(WebAssembly::BlockType::Void));
1201 if (UnwindDest != FakeCallerBB)
1204 auto SplitPos = std::next(RangeEnd->
getIterator());
1205 if (SplitPos == EndBB->end()) {
1208 MF.
insert(std::next(EndBB->getIterator()), DelegateBB);
1209 EndBB->addSuccessor(DelegateBB);
1219 bool CatchAfterSplit =
false;
1220 if (EndBB->isEHPad()) {
1224 CatchAfterSplit =
true;
1231 if (!CatchAfterSplit) {
1250 PostBB->splice(PostBB->end(), PreBB, SplitPos, PreBB->
end());
1251 PostBB->transferSuccessors(PreBB);
1267 assert(EndBB->isEHPad());
1270 MF.
insert(PostBB->getIterator(), PreBB);
1271 MF.
insert(PostBB->getIterator(), DelegateBB);
1272 PreBB->
splice(PreBB->
end(), PostBB, PostBB->begin(), SplitPos);
1284 TII.get(WebAssembly::DELEGATE))
1286 registerTryScope(Try, Delegate,
nullptr);
1308 auto It = UnwindDestToTrampoline.
find(UnwindDest);
1309 if (It != UnwindDestToTrampoline.
end())
1320 if (UnwindDest == getFakeCallerBlock(MF)) {
1324 auto BeginPos = MF.
begin()->begin();
1328 TII.get(WebAssembly::BLOCK))
1329 .
addImm(int64_t(WebAssembly::BlockType::Exnref));
1330 TrampolineBB = getCallerTrampolineBlock(MF);
1337 auto *TargetBeginTry = EHPadToTry[UnwindDest];
1338 auto *TargetEndTry = BeginToEnd[TargetBeginTry];
1339 auto *TargetBeginBB = TargetBeginTry->getParent();
1340 auto *TargetEndBB = TargetEndTry->getParent();
1342 Block =
BuildMI(*TargetBeginBB, std::next(TargetBeginTry->getIterator()),
1343 TargetBeginTry->getDebugLoc(),
TII.get(WebAssembly::BLOCK))
1344 .
addImm(int64_t(WebAssembly::BlockType::Exnref));
1346 EndDebugLoc = TargetEndTry->getDebugLoc();
1347 MF.
insert(TargetEndBB->getIterator(), TrampolineBB);
1354 BuildMI(TrampolineBB, EndDebugLoc,
TII.get(WebAssembly::END_BLOCK));
1355 auto ExnReg =
MRI.createVirtualRegister(&WebAssembly::EXNREFRegClass);
1356 BuildMI(TrampolineBB, EndDebugLoc,
TII.get(WebAssembly::CATCH_ALL_REF))
1358 BuildMI(TrampolineBB, EndDebugLoc,
TII.get(WebAssembly::THROW_REF))
1361 registerScope(
Block, EndBlock);
1362 UnwindDestToTrampoline[UnwindDest] = TrampolineBB;
1363 return TrampolineBB;
1368void WebAssemblyCFGStackify::addNestedTryTable(
MachineInstr *RangeBegin,
1371 auto *BeginBB = RangeBegin->
getParent();
1379 auto *TrampolineBB = getTrampolineBlock(UnwindDest);
1384 AfterSet.
insert(RangeBegin);
1387 if (std::prev(
I)->isDebugInstr() || std::prev(
I)->isPosition())
1390 AfterSet.
insert(&*std::prev(
I));
1400 TII.get(WebAssembly::TRY_TABLE))
1401 .
addImm(int64_t(WebAssembly::BlockType::Void))
1410 auto SplitPos = std::next(RangeEnd->
getIterator());
1411 if (SplitPos == EndBB->end()) {
1414 MF.
insert(std::next(EndBB->getIterator()), EndTryTableBB);
1415 EndBB->addSuccessor(EndTryTableBB);
1425 bool CatchAfterSplit =
false;
1426 if (EndBB->isEHPad()) {
1430 CatchAfterSplit =
true;
1437 if (!CatchAfterSplit) {
1456 PostBB->splice(PostBB->end(), PreBB, SplitPos, PreBB->
end());
1457 PostBB->transferSuccessors(PreBB);
1473 assert(EndBB->isEHPad());
1476 MF.
insert(PostBB->getIterator(), PreBB);
1477 MF.
insert(PostBB->getIterator(), EndTryTableBB);
1478 PreBB->
splice(PreBB->
end(), PostBB, PostBB->begin(), SplitPos);
1490 TII.get(WebAssembly::END_TRY_TABLE));
1491 registerTryScope(TryTable, EndTryTable,
nullptr);
1543 MachineInstr *EndTryTable =
nullptr, *EndLoop =
nullptr;
1545 if (
MI.getOpcode() == WebAssembly::END_TRY_TABLE) {
1549 if (EndTryTable &&
MI.getOpcode() == WebAssembly::END_LOOP) {
1559 auto SplitPos = std::next(EndLoop->getIterator());
1560 EndLoopBB->splice(EndLoopBB->end(), UnwindDest, UnwindDest->
begin(),
1562 EndLoopBB->addSuccessor(UnwindDest);
1565bool WebAssemblyCFGStackify::fixCallUnwindMismatches(
MachineFunction &MF) {
1812 using TryRange = std::pair<MachineInstr *, MachineInstr *>;
1820 bool SeenThrowableInstInBB =
false;
1834 SeenThrowableInstInBB =
true;
1845 if (Succ->isEHPad()) {
1850 if (EHPadStack.
back() == UnwindDest)
1856 std::prev(RangeBegin->
getIterator())->isEHLabel())
1857 RangeBegin = &*std::prev(RangeBegin->
getIterator());
1858 if (std::next(RangeEnd->getIterator()) !=
MBB.
end() &&
1859 std::next(RangeEnd->getIterator())->isEHLabel())
1860 RangeEnd = &*std::next(RangeEnd->getIterator());
1863 UnwindDestToTryRanges[UnwindDest].push_back(
1864 TryRange(RangeBegin, RangeEnd));
1866 <<
"\nCall = " <<
MI
1867 <<
"\nOriginal dest = " << UnwindDest->
getName()
1868 <<
" Current dest = " << EHPadStack.
back()->getName()
1880 MachineInstr *RangeBegin =
nullptr, *RangeEnd =
nullptr;
1884 UnwindDestToTryRanges[getFakeCallerBlock(MF)].push_back(
1885 TryRange(RangeBegin, RangeEnd));
1888 <<
"\nRange begin = " << *RangeBegin
1889 <<
"Range end = " << *RangeEnd
1890 <<
"\nOriginal dest = caller Current dest = "
1891 << CurrentDest->getName() <<
"\n\n");
1892 RangeBegin = RangeEnd =
nullptr;
1896 bool SeenThrowableInstInBB =
false;
1904 SeenThrowableInstInBB =
true;
1909 RecordCallerMismatchRange(EHPadStack.
back());
1913 else if (EHPadStack.
empty() || !MayThrow) {
1921 RangeBegin = RangeEnd = &
MI;
1934 RecordCallerMismatchRange(EHPadStack.
back());
1940 if (UnwindDestToTryRanges.
empty())
1946 for (
auto &[UnwindDest,
_] : UnwindDestToTryRanges)
1950 for (
auto &
P : UnwindDestToTryRanges) {
1951 NumCallUnwindMismatches +=
P.second.size();
1953 auto &TryRanges =
P.second;
1955 for (
auto Range : TryRanges) {
1956 MachineInstr *RangeBegin =
nullptr, *RangeEnd =
nullptr;
1957 std::tie(RangeBegin, RangeEnd) =
Range;
1966 if (UnwindDest != getFakeCallerBlock(MF)) {
1969 if (Succ->isEHPad()) {
1979 addNestedTryTable(RangeBegin, RangeEnd, UnwindDest);
1981 addNestedTryDelegate(RangeBegin, RangeEnd, UnwindDest);
2006bool WebAssemblyCFGStackify::fixCatchUnwindMismatches(
MachineFunction &MF) {
2116 if (
MI.getOpcode() == WebAssembly::TRY)
2118 else if (
MI.getOpcode() == WebAssembly::TRY_TABLE) {
2124 }
else if (
MI.getOpcode() == WebAssembly::DELEGATE)
2142 else if (EHPadStack.
empty() && EHInfo->hasUnwindDest(EHPad)) {
2144 <<
"'s unwind destination does not exist anymore"
2150 else if (!EHPadStack.
empty() && !EHInfo->hasUnwindDest(EHPad)) {
2151 EHPadToUnwindDest[EHPad] = getFakeCallerBlock(MF);
2153 <<
"- Catch unwind mismatch:\nEHPad = " << EHPad->
getName()
2154 <<
" Original dest = caller Current dest = "
2155 << EHPadStack.
back()->getName() <<
"\n\n");
2160 else if (!EHPadStack.
empty() && EHInfo->hasUnwindDest(EHPad)) {
2161 auto *UnwindDest = EHInfo->getUnwindDest(EHPad);
2162 if (EHPadStack.
back() != UnwindDest) {
2163 EHPadToUnwindDest[EHPad] = UnwindDest;
2165 << EHPad->
getName() <<
" Original dest = "
2166 << UnwindDest->
getName() <<
" Current dest = "
2167 << EHPadStack.
back()->getName() <<
"\n\n");
2177 if (EHPadToUnwindDest.
empty())
2182 for (
auto &[
_, UnwindDest] : EHPadToUnwindDest)
2185 NumCatchUnwindMismatches += EHPadToUnwindDest.size();
2188 for (
auto &[EHPad, UnwindDest] : EHPadToUnwindDest) {
2192 addNestedTryTable(Try, EndTry, UnwindDest);
2194 addNestedTryDelegate(Try, EndTry, UnwindDest);
2246 for (
auto &
MBB : MF) {
2247 for (
auto &
MI :
MBB) {
2248 if (
MI.isTerminator()) {
2249 for (
auto &MO :
MI.operands()) {
2250 if (MO.isMBB() && NewEndTryBBs.
count(MO.getMBB())) {
2251 auto *BrDest = MO.getMBB();
2252 bool FoundEndBlock =
false;
2253 for (; std::next(BrDest->getIterator()) != MF.end();
2254 BrDest = BrDest->getNextNode()) {
2255 for (
const auto &
MI : *BrDest) {
2256 if (
MI.getOpcode() == WebAssembly::END_BLOCK) {
2257 FoundEndBlock =
true;
2275void WebAssemblyCFGStackify::recalculateScopeTops(
MachineFunction &MF) {
2279 MDT->updateBlockNumbers();
2286 switch (
MI.getOpcode()) {
2287 case WebAssembly::END_BLOCK:
2288 case WebAssembly::END_LOOP:
2289 case WebAssembly::END_TRY:
2290 case WebAssembly::END_TRY_TABLE:
2291 case WebAssembly::DELEGATE:
2294 case WebAssembly::CATCH_LEGACY:
2295 case WebAssembly::CATCH_ALL_LEGACY:
2310void WebAssemblyCFGStackify::fixEndsAtEndOfFunction(
MachineFunction &MF) {
2313 if (MFI.getResults().empty())
2320 ? WebAssembly::BlockType::Multivalue
2331 if (
MI.isPosition() ||
MI.isDebugInstr())
2333 switch (
MI.getOpcode()) {
2334 case WebAssembly::END_TRY: {
2338 auto *EHPad = TryToEHPad.
lookup(EndToBegin[&
MI]);
2342 if (NextIt != EHPad->
rend())
2346 case WebAssembly::END_BLOCK:
2347 case WebAssembly::END_LOOP:
2348 case WebAssembly::END_TRY_TABLE:
2349 case WebAssembly::DELEGATE:
2350 EndToBegin[&
MI]->getOperand(0).setImm(int32_t(RetType));
2362 while (!Worklist.
empty())
2372 TII.get(WebAssembly::END_FUNCTION));
2381 for (
auto &
MBB : MF)
2382 placeLoopMarker(
MBB);
2384 const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
2385 for (
auto &
MBB : MF) {
2389 MF.getFunction().hasPersonalityFn()) {
2391 placeTryTableMarker(
MBB);
2393 placeTryMarker(
MBB);
2397 placeBlockMarker(
MBB);
2403 MF.getFunction().hasPersonalityFn()) {
2404 bool MismatchFixed = fixCallUnwindMismatches(MF);
2405 MismatchFixed |= fixCatchUnwindMismatches(MF);
2407 recalculateScopeTops(MF);
2411unsigned WebAssemblyCFGStackify::getBranchDepth(
2423unsigned WebAssemblyCFGStackify::getDelegateDepth(
2425 if (
MBB == FakeCallerBB)
2426 return Stack.size();
2432 return getBranchDepth(Stack,
MBB);
2450 if (
X.first == EndTry->
getParent() &&
X.second == EndTry)
2458unsigned WebAssemblyCFGStackify::getRethrowDepth(
2464 if (
End->getOpcode() == WebAssembly::END_TRY) {
2465 auto *EHPad = TryToEHPad[EndToBegin[
End]];
2466 if (EHPadToRethrow == EHPad)
2475void WebAssemblyCFGStackify::rewriteDepthImmediates(
MachineFunction &MF) {
2482 while (
MI.getNumOperands() > 0)
2483 MI.removeOperand(
MI.getNumOperands() - 1);
2484 for (
auto MO : Ops) {
2486 if (
MI.getOpcode() == WebAssembly::DELEGATE)
2488 else if (
MI.getOpcode() == WebAssembly::RETHROW)
2493 MI.addOperand(MF, MO);
2499 switch (
MI.getOpcode()) {
2500 case WebAssembly::BLOCK:
2501 case WebAssembly::TRY:
2502 assert(ScopeTops[
Stack.back().first->getNumber()]->getNumber() <=
2504 "Block/try/try_table marker should be balanced");
2508 case WebAssembly::TRY_TABLE:
2509 assert(ScopeTops[
Stack.back().first->getNumber()]->getNumber() <=
2511 "Block/try/try_table marker should be balanced");
2513 RewriteOperands(
MI);
2516 case WebAssembly::LOOP:
2517 assert(
Stack.back().first == &
MBB &&
"Loop top should be balanced");
2521 case WebAssembly::END_BLOCK:
2522 case WebAssembly::END_TRY:
2523 case WebAssembly::END_TRY_TABLE:
2527 case WebAssembly::END_LOOP:
2531 case WebAssembly::DELEGATE:
2532 RewriteOperands(
MI);
2537 if (
MI.isTerminator())
2538 RewriteOperands(
MI);
2543 assert(
Stack.empty() &&
"Control flow should be balanced");
2546void WebAssemblyCFGStackify::cleanupFunctionData(
MachineFunction &MF) {
2549 AppendixBB = FakeCallerBB = CallerTrampolineBB =
nullptr;
2552void WebAssemblyCFGStackify::releaseMemory() {
2558 UnwindDestToTrampoline.
clear();
2561bool WebAssemblyCFGStackify::runOnMachineFunction(
MachineFunction &MF) {
2563 "********** Function: "
2566 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
2580 removeUnnecessaryInstrs(MF);
2583 rewriteDepthImmediates(MF);
2587 fixEndsAtEndOfFunction(MF);
2593 cleanupFunctionData(MF);
unsigned const MachineRegisterInfo * MRI
static const Function * getParent(const Value *V)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const HexagonInstrInfo * TII
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static bool explicitlyBranchesTo(MachineBasicBlock *Pred, MachineBasicBlock *MBB)
Test whether Pred has any terminators explicitly branching to MBB, as opposed to falling through.
static MachineBasicBlock::iterator getLatestInsertPos(MachineBasicBlock *MBB, const Container &BeforeSet, const Container &AfterSet)
static void appendEndToFunction(MachineFunction &MF, const WebAssemblyInstrInfo &TII)
static void splitEndLoopBB(MachineBasicBlock *UnwindDest)
static void unstackifyVRegsUsedInSplitBB(MachineBasicBlock &MBB, MachineBasicBlock &Split)
static MachineBasicBlock * getSingleUnwindDest(const MachineInstr *TryTable)
static MachineBasicBlock::iterator getEarliestInsertPos(MachineBasicBlock *MBB, const Container &BeforeSet, const Container &AfterSet)
This file implements WebAssemblyException information analysis.
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
This file implements regions used in CFGSort and CFGStackify.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific type parsing utility functions.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
FunctionPass class - This class is used to implement most global optimizations.
bool hasPersonalityFn() const
Check whether this function has a personality function.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
BlockT * getHeader() const
Represents a single loop in the control flow graph.
This class is intended to be used as a base class for asm properties and features specific to the tar...
ExceptionHandling getExceptionHandlingType() const
bool hasEHPadSuccessor() const
bool isEHPad() const
Returns true if the block is a landing pad.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
DebugLoc findPrevDebugLoc(instr_iterator MBBI)
Find the previous valid DebugLoc preceding MBBI, skipping any debug instructions.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< iterator > terminators()
DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
reverse_iterator rbegin()
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void push_back(MachineBasicBlock *MBB)
reverse_iterator rbegin()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void deleteMachineBasicBlock(MachineBasicBlock *MBB)
DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineBasicBlock & back() const
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const WasmEHFuncInfo * getWasmEHFuncInfo() const
getWasmEHFuncInfo - Return information about how the current function uses Wasm exception handling.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
MachineBasicBlock * getMBB() const
static MachineOperand CreateImm(int64_t Val)
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
virtual void releaseMemory()
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Wrapper class representing virtual and physical registers.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
A Use represents the edge between a Value definition and its users.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
const std::vector< MVT > & getResults() const
self_iterator getIterator()
A collection of legacy interfaces for querying information about the current executing process.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI)
Test whether MI is a child of some other node in an expression tree.
bool isArgument(unsigned Opc)
bool isCatchAll(unsigned Opc)
bool isMarker(unsigned Opc)
unsigned getCopyOpcodeForRegClass(const TargetRegisterClass *RC)
Returns the appropriate copy opcode for the given register class.
wasm::ValType toValType(MVT Type)
MachineInstr * findCatch(MachineBasicBlock *EHPad)
Find a catch instruction from an EH pad.
bool isCatch(unsigned Opc)
cl::opt< bool > WasmEnableExnref
BlockType
Used as immediate MachineOperands for block signatures.
bool mayThrow(const MachineInstr &MI)
@ WASM_OPCODE_CATCH_ALL_REF
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
auto reverse(ContainerTy &&C)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createWebAssemblyCFGStackify()