+/// If we inlined an invoke site, we need to convert calls
+/// in the body of the inlined function into invokes.
+///
+/// II is the invoke instruction being inlined. FirstNewBlock is the first
+/// block of the inlined code (the last block is the end of the function),
+/// and InlineCodeInfo is information about the code that got inlined.
+static void HandleInlinedEHPad(InvokeInst *II, BasicBlock *FirstNewBlock,
+ ClonedCodeInfo &InlinedCodeInfo) {
+ BasicBlock *UnwindDest = II->getUnwindDest();
+ Function *Caller = FirstNewBlock->getParent();
+
+ assert(UnwindDest->getFirstNonPHI()->isEHPad() && "unexpected BasicBlock!");
+
+ // If there are PHI nodes in the unwind destination block, we need to keep
+ // track of which values came into them from the invoke before removing the
+ // edge from this block.
+ SmallVector<Value *, 8> UnwindDestPHIValues;
+ llvm::BasicBlock *InvokeBB = II->getParent();
+ for (Instruction &I : *UnwindDest) {
+ // Save the value to use for this edge.
+ PHINode *PHI = dyn_cast<PHINode>(&I);
+ if (!PHI)
+ break;
+ UnwindDestPHIValues.push_back(PHI->getIncomingValueForBlock(InvokeBB));
+ }
+
+ // Add incoming-PHI values to the unwind destination block for the given basic
+ // block, using the values for the original invoke's source block.
+ auto UpdatePHINodes = [&](BasicBlock *Src) {
+ BasicBlock::iterator I = UnwindDest->begin();
+ for (Value *V : UnwindDestPHIValues) {
+ PHINode *PHI = cast<PHINode>(I);
+ PHI->addIncoming(V, Src);
+ ++I;
+ }
+ };
+
+ // Forward EH terminator instructions to the caller's invoke destination.
+ // This is as simple as connect all the instructions which 'unwind to caller'
+ // to the invoke destination.
+ for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E;
+ ++BB) {
+ Instruction *I = BB->getFirstNonPHI();
+ if (I->isEHPad()) {
+ if (auto *CEPI = dyn_cast<CatchEndPadInst>(I)) {
+ if (CEPI->unwindsToCaller()) {
+ CatchEndPadInst::Create(CEPI->getContext(), UnwindDest, CEPI);
+ CEPI->eraseFromParent();
+ UpdatePHINodes(BB);
+ }
+ } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(I)) {
+ if (CEPI->unwindsToCaller()) {
+ CleanupEndPadInst::Create(CEPI->getCleanupPad(), UnwindDest, CEPI);
+ CEPI->eraseFromParent();
+ UpdatePHINodes(BB);
+ }
+ } else if (auto *TPI = dyn_cast<TerminatePadInst>(I)) {
+ if (TPI->unwindsToCaller()) {
+ SmallVector<Value *, 3> TerminatePadArgs;
+ for (Value *Operand : TPI->operands())
+ TerminatePadArgs.push_back(Operand);
+ TerminatePadInst::Create(TPI->getContext(), UnwindDest, TPI);
+ TPI->eraseFromParent();
+ UpdatePHINodes(BB);
+ }
+ } else {
+ assert(isa<CatchPadInst>(I) || isa<CleanupPadInst>(I));
+ }
+ }
+
+ if (auto *CRI = dyn_cast<CleanupReturnInst>(BB->getTerminator())) {
+ if (CRI->unwindsToCaller()) {
+ CleanupReturnInst::Create(CRI->getCleanupPad(), UnwindDest, CRI);
+ CRI->eraseFromParent();
+ UpdatePHINodes(BB);
+ }
+ }
+ }
+
+ if (InlinedCodeInfo.ContainsCalls)
+ for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E;
+ ++BB)
+ if (BasicBlock *NewBB =
+ HandleCallsInBlockInlinedThroughInvoke(BB, UnwindDest))
+ // Update any PHI nodes in the exceptional block to indicate that there
+ // is now a new entry in them.
+ UpdatePHINodes(NewBB);
+
+ // Now that everything is happy, we have one final detail. The PHI nodes in
+ // the exception destination block still have entries due to the original
+ // invoke instruction. Eliminate these entries (which might even delete the
+ // PHI node) now.
+ UnwindDest->removePredecessor(InvokeBB);
+}
+