Index: llvm/lib/Transforms/Scalar/GuardWidening.cpp =================================================================== --- llvm/lib/Transforms/Scalar/GuardWidening.cpp +++ llvm/lib/Transforms/Scalar/GuardWidening.cpp @@ -495,6 +495,9 @@ makeAvailableAt(Op, Loc); Inst->moveBefore(Loc); + // If we moved instruction before guard we must clean nuw, nsw flags. + Inst->setHasNoUnsignedWrap(false); + Inst->setHasNoSignedWrap(false); } bool GuardWideningImpl::widenCondCommon(Value *Cond0, Value *Cond1, Index: llvm/test/Transforms/GuardWidening/range-check-merging.ll =================================================================== --- llvm/test/Transforms/GuardWidening/range-check-merging.ll +++ llvm/test/Transforms/GuardWidening/range-check-merging.ll @@ -231,5 +231,23 @@ ret void } +define void @f_8(i32 %x, i32* %length_buf) { +; Check that we clean nuw nsw flags +; CHECK-LABEL: @f_8( +; CHECK: %x.inc1 = add i32 %x, 1 +; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %wide.chk) [ "deopt"() ] +; CHECK: ret void +entry: + %length = load i32, i32* %length_buf, !range !0 + %chk0 = icmp ult i32 %x, %length + call void(i1, ...) @llvm.experimental.guard(i1 %chk0) [ "deopt"() ] + + %x.inc1 = add nuw nsw i32 %x, 1 + %chk1 = icmp ult i32 %x.inc1, %length + call void(i1, ...) @llvm.experimental.guard(i1 %chk1) [ "deopt"() ] + ret void +} + + !0 = !{i32 0, i32 2147483648}