From 24fbb5875c7f45bfab43a656c049d73146dcdfff Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Fri, 2 Sep 2011 01:30:08 +0000 Subject: [PATCH] Perform the upgrading of the old EH to the new EH in a more sane manner. Perform the upgrading in steps. * First, create a map of the invokes to the EH intrinsics. * Next, take that mapping and determine if the invoke's unwind destination has a single predecessor. If not, then create a new empty block to hold the new landingpad instruction. * Create a landingpad instruction into the uwnind destination. Fill it with the values from the old selector. Map the old intrinsic calls to the new landingpad values (there may be multiple landingpad instructions per instrinic call pairs). * Go through the old intrinsic calls, create a PHI node when necessary, and then replace their values with the new values from the landingpad instructions. * Delete all dead instructions. * ??? * Profit! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@138990 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/AutoUpgrade.cpp | 155 ++++++++++++++++++++++++++++--------- 1 file changed, 117 insertions(+), 38 deletions(-) diff --git a/lib/VMCore/AutoUpgrade.cpp b/lib/VMCore/AutoUpgrade.cpp index 7b0b34ed89f..2c327660d47 100644 --- a/lib/VMCore/AutoUpgrade.cpp +++ b/lib/VMCore/AutoUpgrade.cpp @@ -387,10 +387,9 @@ void llvm::UpgradeExceptionHandling(Module *M) { Type *SelTy = Type::getInt32Ty(Context); Type *LPadSlotTy = StructType::get(ExnTy, SelTy, NULL); - // This map stores the slots where the exception object and selector value are - // stored within a function. - SmallPtrSet DeadInsts; - DenseMap > FnToLPadSlotMap; + // This map links the invoke instruction with the eh.exception and eh.selector + // calls associated with it. + DenseMap > InvokeToIntrinsicsMap; for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { Function &F = *I; @@ -401,49 +400,129 @@ void llvm::UpgradeExceptionHandling(Module *M) { InvokeInst *Inst = dyn_cast(BB->getTerminator()); if (!Inst) continue; BasicBlock *UnwindDest = Inst->getUnwindDest(); - if (UnwindDest->isLandingPad()) continue; // All ready converted. - - // Store the exception object and selector value in the entry block. - Value *ExnSlot = 0; - Value *SelSlot = 0; - if (!FnToLPadSlotMap[&F].first) { - BasicBlock *Entry = &F.front(); - ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator()); - SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator()); - FnToLPadSlotMap[&F] = std::make_pair(ExnSlot, SelSlot); - } else { - ExnSlot = FnToLPadSlotMap[&F].first; - SelSlot = FnToLPadSlotMap[&F].second; - } - - // We're in an unwind block. Try to find the eh.exception and eh.selector - // calls. - IRBuilder<> Builder(Context); - Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstNonPHI()); + if (UnwindDest->isLandingPad()) continue; // Already converted. SmallPtrSet Visited; CallInst *Exn = 0; CallInst *Sel = 0; FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited); assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!"); - - Value *PersFn = Sel->getArgOperand(1); - LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0); - Value *LPExn = Builder.CreateExtractValue(LPI, 0); - Value *LPSel = Builder.CreateExtractValue(LPI, 1); - Builder.CreateStore(LPExn, ExnSlot); - Builder.CreateStore(LPSel, SelSlot); - - TransferClausesToLandingPadInst(LPI, Sel); - - Exn->replaceAllUsesWith(LPExn); - Sel->replaceAllUsesWith(LPSel); - - DeadInsts.insert(Exn); - DeadInsts.insert(Sel); + InvokeToIntrinsicsMap[Inst] = std::make_pair(Exn, Sel); } } + // This map stores the slots where the exception object and selector value are + // stored within a function. + DenseMap > FnToLPadSlotMap; + + // This maps the old intrinsic calls (eh.exception & eh.selector) to the new + // landingpad instruction(s). + DenseMap, + SmallVector, 8> > OldExnToNewExnMap; + + SmallPtrSet DeadInsts; + for (DenseMap >::iterator + I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end(); + I != E; ++I) { + InvokeInst *Invoke = I->first; + BasicBlock *UnwindDest = Invoke->getUnwindDest(); + Function *F = UnwindDest->getParent(); + std::pair EHIntrinsics = I->second; + CallInst *Exn = cast(EHIntrinsics.first); + CallInst *Sel = cast(EHIntrinsics.second); + + // Store the exception object and selector value in the entry block. + Value *ExnSlot = 0; + Value *SelSlot = 0; + if (!FnToLPadSlotMap[F].first) { + BasicBlock *Entry = &F->front(); + ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator()); + SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator()); + FnToLPadSlotMap[F] = std::make_pair(ExnSlot, SelSlot); + } else { + ExnSlot = FnToLPadSlotMap[F].first; + SelSlot = FnToLPadSlotMap[F].second; + } + + if (!UnwindDest->getSinglePredecessor()) { + // The unwind destination doesn't have a single predecessor. Create an + // unwind destination which has only one predecessor. + BasicBlock *NewBB = BasicBlock::Create(Context, "new.lpad", + UnwindDest->getParent()); + BranchInst::Create(UnwindDest, NewBB); + Invoke->setUnwindDest(NewBB); + + // Fix up any PHIs in the original unwind destination block. + for (BasicBlock::iterator + II = UnwindDest->begin(); isa(II); ++II) { + PHINode *PN = cast(II); + int Idx = PN->getBasicBlockIndex(Invoke->getParent()); + if (Idx == -1) continue; + PN->setIncomingBlock(Idx, NewBB); + } + + UnwindDest = NewBB; + } + + IRBuilder<> Builder(Context); + Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstInsertionPt()); + + Value *PersFn = Sel->getArgOperand(1); + LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0); + Value *LPExn = Builder.CreateExtractValue(LPI, 0); + Value *LPSel = Builder.CreateExtractValue(LPI, 1); + Builder.CreateStore(LPExn, ExnSlot); + Builder.CreateStore(LPSel, SelSlot); + + TransferClausesToLandingPadInst(LPI, Sel); + + OldExnToNewExnMap[std::make_pair(Exn, Sel)]. + push_back(std::make_pair(LPExn, LPSel)); + + DeadInsts.insert(Exn); + DeadInsts.insert(Sel); + } + + // Replace the old intrinsic calls with the new (possibly PHI'ed) values. + for (DenseMap, + SmallVector, 8> >::iterator + I = OldExnToNewExnMap.begin(), E = OldExnToNewExnMap.end(); + I != E; ++I) { + std::pair OldExnSel = I->first; + CallInst *Exn = OldExnSel.first; + CallInst *Sel = OldExnSel.second; + SmallVector, 8> &LPExnSel = I->second; + unsigned Size = LPExnSel.size(); + Value *LPExn = LPExnSel[0].first; + Value *LPSel = LPExnSel[0].second; + + if (Size != 1) { + BasicBlock *Parent = Exn->getParent(); + IRBuilder<> Builder(Context); + Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt()); + + PHINode *PN = Builder.CreatePHI(Exn->getType(), Size, "exn.phi"); + for (SmallVector, 8>::iterator + II = LPExnSel.begin(), IE = LPExnSel.end(); II != IE; ++II) + PN->addIncoming(II->first, cast(II->first)->getParent()); + + LPExn = PN; + + Parent = Sel->getParent(); + Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt()); + + PN = Builder.CreatePHI(Sel->getType(), Size, "sel.phi"); + for (SmallVector, 8>::iterator + II = LPExnSel.begin(), IE = LPExnSel.end(); II != IE; ++II) + PN->addIncoming(II->second, cast(II->second)->getParent()); + + LPSel = PN; + } + + Exn->replaceAllUsesWith(LPExn); + Sel->replaceAllUsesWith(LPSel); + } + // Remove the dead instructions. for (SmallPtrSet::iterator I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) {