Index: lib/Transforms/Utils/CloneFunction.cpp =================================================================== --- lib/Transforms/Utils/CloneFunction.cpp +++ lib/Transforms/Utils/CloneFunction.cpp @@ -310,10 +310,11 @@ // the basic block. if (Value *V = SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) { - // On the off-chance that this simplifies to an instruction in the old - // function, map it back into the new function. - if (Value *MappedV = VMap.lookup(V)) - V = MappedV; + assert((dyn_cast(V) == nullptr || + dyn_cast(V)->getParent() == nullptr || + dyn_cast(V)->getFunction() != OldFunc || + OldFunc == NewFunc) && + "Simplified Instruction should not be in the old function."); if (!NewInst->mayHaveSideEffects()) { VMap[&*II] = V; @@ -453,7 +454,7 @@ // // Defer PHI resolution until rest of function is resolved. SmallVector PHIToResolve; - for (const BasicBlock &BI : *OldFunc) { + for (const BasicBlock &BI : make_range(OldFunc->begin(), OldFunc->end())) { Value *V = VMap.lookup(&BI); BasicBlock *NewBB = cast_or_null(V); if (!NewBB) continue; // Dead block.