Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp +++ lib/Transforms/Scalar/GVN.cpp @@ -2195,7 +2195,9 @@ // It is not safe to do PRE when P->CurrentBlock is a loop backedge, and // when CurInst has operand defined in CurrentBlock (so it may be defined // by phi in the loop header). - if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock] && + const auto PNum = BlockRPONumber[P]; + const auto CNum = BlockRPONumber[CurrentBlock]; + if (PNum >= CNum && llvm::any_of(CurInst->operands(), [&](const Use &U) { if (auto *Inst = dyn_cast(U.get())) return Inst->getParent() == CurrentBlock;