llvm.org GIT mirror llvm / 6212b4d
[WinEH] Update CATCHRET's operand to match its successor The CATCHRET operand did not match the MachineFunction's CFG. This mismatch happened because FrameLowering created a new MachineBasicBlock and updated the CFG but forgot to update the CATCHRET operand. Let's make sure this doesn't happen again by strengthing the funclet membership analysis: it can now reason about the membership of all basic blocks, not just those inside of funclets. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249344 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 4 years ago
3 changed file(s) with 28 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
687687 if (!MF.getMMI().hasEHFunclets())
688688 return FuncletMembership;
689689
690 int EntryBBNumber = MF.front().getNumber();
690691 bool IsSEH = isAsynchronousEHPersonality(
691692 classifyEHPersonality(MF.getFunction()->getPersonalityFn()));
692693
693694 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
694695 SmallVector FuncletBlocks;
696 SmallVector UnreachableBlocks;
697 SmallVector SEHCatchPads;
695698 SmallVector, 16> CatchRetSuccessors;
696699 for (const MachineBasicBlock &MBB : MF) {
697 if (MBB.isEHFuncletEntry())
700 if (MBB.isEHFuncletEntry()) {
698701 FuncletBlocks.push_back(&MBB);
702 } else if (IsSEH && MBB.isEHPad()) {
703 SEHCatchPads.push_back(&MBB);
704 } else if (MBB.pred_empty()) {
705 UnreachableBlocks.push_back(&MBB);
706 }
699707
700708 MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
701709 // CatchPads are not funclets for SEH so do not consider CatchRet to
702710 // transfer control to another funclet.
703 if (IsSEH || MBBI->getOpcode() != TII->getCatchReturnOpcode())
711 if (MBBI->getOpcode() != TII->getCatchReturnOpcode())
704712 continue;
705713
714 // FIXME: SEH CatchPads are not necessarily in the parent function:
715 // they could be inside a finally block.
706716 const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();
707717 const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();
708 CatchRetSuccessors.push_back({Successor, SuccessorColor->getNumber()});
718 CatchRetSuccessors.push_back(
719 {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});
709720 }
710721
711722 // We don't have anything to do if there aren't any EH pads.
713724 return FuncletMembership;
714725
715726 // Identify all the basic blocks reachable from the function entry.
716 collectFuncletMembers(FuncletMembership, MF.front().getNumber(), MF.begin());
727 collectFuncletMembers(FuncletMembership, EntryBBNumber, MF.begin());
728 // All blocks not part of a funclet are in the parent function.
729 for (const MachineBasicBlock *MBB : UnreachableBlocks)
730 collectFuncletMembers(FuncletMembership, EntryBBNumber, MBB);
717731 // Next, identify all the blocks inside the funclets.
718732 for (const MachineBasicBlock *MBB : FuncletBlocks)
719733 collectFuncletMembers(FuncletMembership, MBB->getNumber(), MBB);
734 // SEH CatchPads aren't really funclets, handle them separately.
735 for (const MachineBasicBlock *MBB : SEHCatchPads)
736 collectFuncletMembers(FuncletMembership, EntryBBNumber, MBB);
720737 // Finally, identify all the targets of a catchret.
721738 for (std::pair CatchRetPair :
722739 CatchRetSuccessors)
723740 collectFuncletMembers(FuncletMembership, CatchRetPair.second,
724741 CatchRetPair.first);
725 // All blocks not part of a funclet are in the parent function.
726 for (const MachineBasicBlock &MBB : MF)
727 FuncletMembership.insert({&MBB, MF.front().getNumber()});
728742 return FuncletMembership;
729743 }
4141 if (FuncletMembership.empty())
4242 return false;
4343
44 F.sort([&](MachineBasicBlock &x, MachineBasicBlock &y) {
45 return FuncletMembership[&x] < FuncletMembership[&y];
44 F.sort([&](MachineBasicBlock &X, MachineBasicBlock &Y) {
45 auto FuncletX = FuncletMembership.find(&X);
46 auto FuncletY = FuncletMembership.find(&Y);
47 assert(FuncletX != FuncletMembership.end());
48 assert(FuncletY != FuncletMembership.end());
49 return FuncletX->second < FuncletY->second;
4650 });
4751
4852 // Conservatively assume we changed something.
10871087 MBB.removeSuccessor(TargetMBB);
10881088 MBB.addSuccessor(RestoreMBB);
10891089 RestoreMBB->addSuccessor(TargetMBB);
1090 MBBI->getOperand(0).setMBB(RestoreMBB);
10901091 }
10911092
10921093 // Fill EAX/RAX with the address of the target block.