+ if (isa<AllocaInst>(LastBlock->begin())) {
+ BasicBlock::iterator InsertPoint = Caller->begin()->begin();
+ while (isa<AllocaInst>(InsertPoint)) ++InsertPoint;
+
+ for (BasicBlock::iterator I = LastBlock->begin(), E = LastBlock->end();
+ I != E; )
+ if (AllocaInst *AI = dyn_cast<AllocaInst>(I++))
+ if (isa<Constant>(AI->getArraySize())) {
+ LastBlock->getInstList().remove(AI);
+ Caller->front().getInstList().insert(InsertPoint, AI);
+ }
+ }
+
+ // If we just inlined a call due to an invoke instruction, scan the inlined
+ // function checking for function calls that should now be made into invoke
+ // instructions, and for unwind's which should be turned into branches.
+ if (InvokeDest) {
+ for (Function::iterator BB = LastBlock, E = Caller->end(); BB != E; ++BB) {
+ for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
+ // We only need to check for function calls: inlined invoke instructions
+ // require no special handling...
+ if (CallInst *CI = dyn_cast<CallInst>(I)) {
+ // Convert this function call into an invoke instruction...
+
+ // First, split the basic block...
+ BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
+
+ // Next, create the new invoke instruction, inserting it at the end
+ // of the old basic block.
+ InvokeInst *II =
+ new InvokeInst(CI->getCalledValue(), Split, InvokeDest,
+ std::vector<Value*>(CI->op_begin()+1, CI->op_end()),
+ CI->getName(), BB->getTerminator());
+
+ // Make sure that anything using the call now uses the invoke!
+ CI->replaceAllUsesWith(II);
+
+ // Delete the unconditional branch inserted by splitBasicBlock
+ BB->getInstList().pop_back();
+ Split->getInstList().pop_front(); // Delete the original call
+
+ // Update any PHI nodes in the exceptional block to indicate that
+ // there is now a new entry in them.
+ unsigned i = 0;
+ for (BasicBlock::iterator I = InvokeDest->begin();
+ PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i)
+ PN->addIncoming(InvokeDestPHIValues[i], BB);
+
+ // This basic block is now complete, start scanning the next one.
+ break;
+ } else {
+ ++I;
+ }
+ }
+
+ if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
+ // An UnwindInst requires special handling when it gets inlined into an
+ // invoke site. Once this happens, we know that the unwind would cause
+ // a control transfer to the invoke exception destination, so we can
+ // transform it into a direct branch to the exception destination.
+ new BranchInst(InvokeDest, UI);
+
+ // Delete the unwind instruction!
+ UI->getParent()->getInstList().pop_back();
+
+ // Update any PHI nodes in the exceptional block to indicate that
+ // there is now a new entry in them.
+ unsigned i = 0;
+ for (BasicBlock::iterator I = InvokeDest->begin();
+ PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i)
+ PN->addIncoming(InvokeDestPHIValues[i], BB);
+ }