diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -6529,17 +6529,22 @@ // Only look at the first use, avoid hurting compile time with long uselists User *Use = *I->user_begin(); + auto *UseI = dyn_cast(Use); + // Bail out if Use is not in the same BB as I or UseI == I or UseI comes + // before I in the block. The latter two can be the case if UseI is a PHI + // node. + if (!UseI || UseI->getParent() != I->getParent() || UseI == I || + UseI->comesBefore(I)) + return false; + // Now make sure that there are no instructions in between that can alter // control flow (eg. calls) - for (BasicBlock::iterator - i = ++BasicBlock::iterator(I), - UI = BasicBlock::iterator(dyn_cast(Use)); - i != UI; ++i) { - if (i == I->getParent()->end()) - return false; - if (!isGuaranteedToTransferExecutionToSuccessor(&*i)) - return false; - } + auto InstrRange = + make_range(std::next(I->getIterator()), UseI->getIterator()); + if (any_of(InstrRange, [](Instruction &I) { + return !isGuaranteedToTransferExecutionToSuccessor(&I); + })) + return false; // Look through GEPs. A load from a GEP derived from NULL is still undefined if (GetElementPtrInst *GEP = dyn_cast(Use))