llvm/llvm/test/Transforms/ConstraintElimination/add-nuw.ll

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s

define void @test.not.uge.ult(i8 %start, i8 %low, i8 %high) {
; CHECK-LABEL: @test.not.uge.ult(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[ADD_PTR_I:%.*]] = add nuw i8 [[START:%.*]], 3
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_PTR_I]], [[HIGH:%.*]]
; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    ret void
; CHECK:       if.end:
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_1:%.*]] = add nuw i8 [[START]], 1
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_2:%.*]] = add nuw i8 [[START]], 2
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_3:%.*]] = add nuw i8 [[START]], 3
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_4:%.*]] = add nuw i8 [[START]], 4
; CHECK-NEXT:    [[C_4:%.*]] = icmp ult i8 [[START_4]], [[HIGH]]
; CHECK-NEXT:    call void @use(i1 [[C_4]])
; CHECK-NEXT:    ret void
;
entry:
  %add.ptr.i = add nuw i8 %start, 3
  %c.1 = icmp uge i8 %add.ptr.i, %high
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  ret void

if.end:                                           ; preds = %entry
  %t.0 = icmp ult i8 %start, %high
  call void @use(i1 %t.0)
  %start.1 = add nuw i8 %start, 1
  %t.1 = icmp ult i8 %start.1, %high
  call void @use(i1 %t.1)
  %start.2 = add nuw i8 %start, 2
  %t.2 = icmp ult i8 %start.2, %high
  call void @use(i1 %t.2)
  %start.3 = add nuw i8 %start, 3
  %t.3 = icmp ult i8 %start.3, %high
  call void @use(i1 %t.3)
  %start.4 = add nuw i8 %start, 4
  %c.4 = icmp ult i8 %start.4, %high
  call void @use(i1 %c.4)
  ret void
}

define void @test.not.uge.ule(i8 %start, i8 %low, i8 %high) {
; CHECK-LABEL: @test.not.uge.ule(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[ADD_PTR_I:%.*]] = add nuw i8 [[START:%.*]], 3
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_PTR_I]], [[HIGH:%.*]]
; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    ret void
; CHECK:       if.end:
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_1:%.*]] = add nuw i8 [[START]], 1
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_2:%.*]] = add nuw i8 [[START]], 2
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_3:%.*]] = add nuw i8 [[START]], 3
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_4:%.*]] = add nuw i8 [[START]], 4
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[START_5:%.*]] = add nuw i8 [[START]], 5
; CHECK-NEXT:    [[C_5:%.*]] = icmp ule i8 [[START_5]], [[HIGH]]
; CHECK-NEXT:    call void @use(i1 [[C_5]])
; CHECK-NEXT:    ret void
;
entry:
  %add.ptr.i = add nuw i8 %start, 3
  %c.1 = icmp uge i8 %add.ptr.i, %high
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  ret void

if.end:                                           ; preds = %entry
  %t.0 = icmp ule i8 %start, %high
  call void @use(i1 %t.0)
  %start.1 = add nuw i8 %start, 1
  %t.1 = icmp ule i8 %start.1, %high
  call void @use(i1 %t.1)
  %start.2 = add nuw i8 %start, 2
  %t.2 = icmp ule i8 %start.2, %high
  call void @use(i1 %t.2)
  %start.3 = add nuw i8 %start, 3
  %t.3 = icmp ule i8 %start.3, %high
  call void @use(i1 %t.3)
  %start.4 = add nuw i8 %start, 4
  %t.4 = icmp ule i8 %start.4, %high
  call void @use(i1 %t.4)

  %start.5 = add nuw i8 %start, 5
  %c.5 = icmp ule i8 %start.5, %high
  call void @use(i1 %c.5)

  ret void
}

define void @test.not.uge.ugt(i8 %start, i8 %low, i8 %high) {
; CHECK-LABEL: @test.not.uge.ugt(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[ADD_PTR_I:%.*]] = add nuw i8 [[START:%.*]], 3
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_PTR_I]], [[HIGH:%.*]]
; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    ret void
; CHECK:       if.end:
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_1:%.*]] = add nuw i8 [[START]], 1
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_2:%.*]] = add nuw i8 [[START]], 2
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_3:%.*]] = add nuw i8 [[START]], 3
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_4:%.*]] = add nuw i8 [[START]], 4
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_5:%.*]] = add nuw i8 [[START]], 5
; CHECK-NEXT:    [[C_5:%.*]] = icmp ugt i8 [[START_5]], [[HIGH]]
; CHECK-NEXT:    call void @use(i1 [[C_5]])
; CHECK-NEXT:    ret void
;
entry:
  %add.ptr.i = add nuw i8 %start, 3
  %c.1 = icmp uge i8 %add.ptr.i, %high
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  ret void

if.end:                                           ; preds = %entry
  %f.0 = icmp ugt i8 %start, %high
  call void @use(i1 %f.0)

  %start.1 = add nuw i8 %start, 1
  %f.1 = icmp ugt i8 %start.1, %high
  call void @use(i1 %f.1)

  %start.2 = add nuw i8 %start, 2
  %f.2 = icmp ugt i8 %start.2, %high
  call void @use(i1 %f.2)

  %start.3 = add nuw i8 %start, 3
  %f.3 = icmp ugt i8 %start.3, %high
  call void @use(i1 %f.3)

  %start.4 = add nuw i8 %start, 4
  %f.4 = icmp ugt i8 %start.4, %high
  call void @use(i1 %f.4)

  %start.5 = add nuw i8 %start, 5
  %c.5 = icmp ugt i8 %start.5, %high
  call void @use(i1 %c.5)

  ret void
}

define void @test.not.uge.uge(i8 %start, i8 %low, i8 %high) {
; CHECK-LABEL: @test.not.uge.uge(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[ADD_PTR_I:%.*]] = add nuw i8 [[START:%.*]], 3
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_PTR_I]], [[HIGH:%.*]]
; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    ret void
; CHECK:       if.end:
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_1:%.*]] = add nuw i8 [[START]], 1
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_2:%.*]] = add nuw i8 [[START]], 2
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_3:%.*]] = add nuw i8 [[START]], 3
; CHECK-NEXT:    call void @use(i1 false)
; CHECK-NEXT:    [[START_4:%.*]] = add nuw i8 [[START]], 4
; CHECK-NEXT:    [[C_4:%.*]] = icmp uge i8 [[START_4]], [[HIGH]]
; CHECK-NEXT:    call void @use(i1 [[C_4]])
; CHECK-NEXT:    [[START_5:%.*]] = add nuw i8 [[START]], 5
; CHECK-NEXT:    [[C_5:%.*]] = icmp uge i8 [[START_5]], [[HIGH]]
; CHECK-NEXT:    call void @use(i1 [[C_5]])
; CHECK-NEXT:    ret void
;
entry:
  %add.ptr.i = add nuw i8 %start, 3
  %c.1 = icmp uge i8 %add.ptr.i, %high
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  ret void

if.end:                                           ; preds = %entry
  %f.0 = icmp ugt i8 %start, %high
  call void @use(i1 %f.0)

  %start.1 = add nuw i8 %start, 1
  %f.1 = icmp uge i8 %start.1, %high
  call void @use(i1 %f.1)

  %start.2 = add nuw i8 %start, 2
  %f.2 = icmp uge i8 %start.2, %high
  call void @use(i1 %f.2)

  %start.3 = add nuw i8 %start, 3
  %f.3 = icmp uge i8 %start.3, %high
  call void @use(i1 %f.3)

  %start.4 = add nuw i8 %start, 4
  %c.4 = icmp uge i8 %start.4, %high
  call void @use(i1 %c.4)

  %start.5 = add nuw i8 %start, 5
  %c.5 = icmp uge i8 %start.5, %high
  call void @use(i1 %c.5)

  ret void
}

define void @test.decompose.nonconst(i8 %a, i8 %b, i8 %c, i8 %d) {
; CHECK-LABEL: @test.decompose.nonconst(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_0:%.*]] = icmp uge i8 [[A:%.*]], [[C:%.*]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[B:%.*]], [[C]]
; CHECK-NEXT:    [[AND_0:%.*]] = and i1 [[C_0]], [[C_1]]
; CHECK-NEXT:    br i1 [[AND_0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    [[AND_1:%.*]] = and i1 true, true
; CHECK-NEXT:    br i1 [[AND_1]], label [[IF_THEN_2:%.*]], label [[IF_END]]
; CHECK:       if.then.2:
; CHECK-NEXT:    [[ADD_0:%.*]] = add nuw i8 [[A]], [[B]]
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], [[A]]
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[A]], [[D:%.*]]
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    ret void
; CHECK:       if.end:
; CHECK-NEXT:    ret void
;
entry:
  %c.0 = icmp uge i8 %a, %c
  %c.1 = icmp uge i8 %b, %c
  %and.0 = and i1 %c.0, %c.1
  br i1 %and.0, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %c.2 = icmp uge i8 %a, 0
  %c.3 = icmp uge i8 %b, 0
  %and.1 = and i1 %c.2, %c.3
  br i1 %and.1, label %if.then.2, label %if.end

if.then.2:
  %add.0 = add nuw i8 %a, %b
  %t.0 = icmp uge i8 %add.0, %c
  call void @use(i1 %t.0)
  %add.1 = add nuw i8 %a, %a
  %t.1 = icmp uge i8 %add.0, %c
  call void @use(i1 %t.1)
  %add.2 = add nuw i8 %a, %d
  %c.4 = icmp uge i8 %add.2, %c
  call void @use(i1 %c.4)
  ret void

if.end:                                           ; preds = %entry
  ret void
}

define void @test.decompose.nonconst.no.null.check(i8 %a, i8 %b, i8 %c, i8 %d) {
; CHECK-LABEL: @test.decompose.nonconst.no.null.check(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_0:%.*]] = icmp uge i8 [[A:%.*]], [[C:%.*]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[B:%.*]], [[C]]
; CHECK-NEXT:    [[AND_0:%.*]] = and i1 [[C_0]], [[C_1]]
; CHECK-NEXT:    br i1 [[AND_0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    [[ADD_0:%.*]] = add nuw i8 [[A]], [[B]]
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], [[A]]
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[A]], [[D:%.*]]
; CHECK-NEXT:    call void @use(i1 true)
; CHECK-NEXT:    ret void
; CHECK:       if.end:
; CHECK-NEXT:    ret void
;
entry:
  %c.0 = icmp uge i8 %a, %c
  %c.1 = icmp uge i8 %b, %c
  %and.0 = and i1 %c.0, %c.1
  br i1 %and.0, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %add.0 = add nuw i8 %a, %b
  %t.0 = icmp uge i8 %add.0, %c
  call void @use(i1 %t.0)
  %add.1 = add nuw i8 %a, %a
  %t.1 = icmp uge i8 %add.0, %c
  call void @use(i1 %t.1)
  %add.2 = add nuw i8 %a, %d
  %c.4 = icmp uge i8 %add.2, %c
  call void @use(i1 %c.4)
  ret void

if.end:                                           ; preds = %entry
  ret void
}


define i1 @test_n_must_ule_1_due_to_nuw(i8 %n, i8 %i) {
; CHECK-LABEL: @test_n_must_ule_1_due_to_nuw(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[SUB_N_1:%.*]] = add nuw i8 [[N:%.*]], -1
; CHECK-NEXT:    [[ADD:%.*]] = add nuw i8 [[I:%.*]], [[SUB_N_1]]
; CHECK-NEXT:    br i1 false, label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    ret i1 true
; CHECK:       if.end:
; CHECK-NEXT:    [[F:%.*]] = icmp ule i8 [[N]], 1
; CHECK-NEXT:    ret i1 [[F]]
;
entry:
  %sub.n.1 = add nuw i8 %n, -1
  %add = add nuw i8 %i, %sub.n.1
  %c.1 = icmp uge i8 %i, %add
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %t = icmp ule i8 %n, 1
  ret i1 %t

if.end:                                           ; preds = %entry
  %f = icmp ule i8 %n, 1
  ret i1 %f
}


define i1 @test_n_unknown_missing_nuw(i8 %n, i8 %i) {
; CHECK-LABEL: @test_n_unknown_missing_nuw(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[SUB_N_1:%.*]] = add i8 [[N:%.*]], -1
; CHECK-NEXT:    [[ADD:%.*]] = add i8 [[I:%.*]], [[SUB_N_1]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[I]], [[ADD]]
; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    [[T:%.*]] = icmp ule i8 [[N]], 1
; CHECK-NEXT:    ret i1 [[T]]
; CHECK:       if.end:
; CHECK-NEXT:    [[F:%.*]] = icmp ule i8 [[N]], 1
; CHECK-NEXT:    ret i1 [[F]]
;
entry:
  %sub.n.1 = add i8 %n, -1
  %add = add i8 %i, %sub.n.1
  %c.1 = icmp uge i8 %i, %add
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %t = icmp ule i8 %n, 1
  ret i1 %t

if.end:                                           ; preds = %entry
  %f = icmp ule i8 %n, 1
  ret i1 %f
}

define i1 @test_n_must_ule_2_due_to_nuw(i8 %n, i8 %i) {
; CHECK-LABEL: @test_n_must_ule_2_due_to_nuw(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[SUB_N_1:%.*]] = add nuw i8 [[N:%.*]], -2
; CHECK-NEXT:    [[ADD:%.*]] = add nuw i8 [[I:%.*]], [[SUB_N_1]]
; CHECK-NEXT:    br i1 false, label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    ret i1 true
; CHECK:       if.end:
; CHECK-NEXT:    [[F:%.*]] = icmp ule i8 [[N]], 2
; CHECK-NEXT:    ret i1 [[F]]
;
entry:
  %sub.n.1 = add nuw i8 %n, -2
  %add = add nuw i8 %i, %sub.n.1
  %c.1 = icmp uge i8 %i, %add
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %t = icmp ule i8 %n, 2
  ret i1 %t

if.end:                                           ; preds = %entry
  %f = icmp ule i8 %n, 2
  ret i1 %f
}


define i1 @test_n_unknown_missing_nuw2(i8 %n, i8 %i) {
; CHECK-LABEL: @test_n_unknown_missing_nuw2(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[SUB_N_1:%.*]] = add i8 [[N:%.*]], -2
; CHECK-NEXT:    [[ADD:%.*]] = add i8 [[I:%.*]], [[SUB_N_1]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[I]], [[ADD]]
; CHECK-NEXT:    br i1 [[C_1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    [[T:%.*]] = icmp ule i8 [[N]], 1
; CHECK-NEXT:    ret i1 [[T]]
; CHECK:       if.end:
; CHECK-NEXT:    [[F:%.*]] = icmp ule i8 [[N]], 1
; CHECK-NEXT:    ret i1 [[F]]
;
entry:
  %sub.n.1 = add i8 %n, -2
  %add = add i8 %i, %sub.n.1
  %c.1 = icmp uge i8 %i, %add
  br i1 %c.1, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %t = icmp ule i8 %n, 1
  ret i1 %t

if.end:                                           ; preds = %entry
  %f = icmp ule i8 %n, 1
  ret i1 %f
}

declare void @use(i1)

define i1 @add_nuw_neg_pr54224_i16(i16 %a) {
; CHECK-LABEL: @add_nuw_neg_pr54224_i16(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[NEG2:%.*]] = add nuw i16 [[A:%.*]], -305
; CHECK-NEXT:    br i1 false, label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK:       exit.1:
; CHECK-NEXT:    ret i1 true
; CHECK:       exit.2:
; CHECK-NEXT:    [[C_3:%.*]] = icmp ugt i16 [[A]], 0
; CHECK-NEXT:    ret i1 [[C_3]]
;
entry:
  %neg2 = add nuw i16 %a, -305
  %c.1 = icmp ugt i16 0, %neg2
  br i1 %c.1, label %exit.1, label %exit.2

exit.1:
  %c.2 = icmp ugt i16 %a, 0
  ret i1 %c.2

exit.2:
  %c.3 = icmp ugt i16 %a, 0
  ret i1 %c.3
}

define i1 @add_nuw_neg_pr54224_i64(i64 %a) {
; CHECK-LABEL: @add_nuw_neg_pr54224_i64(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[NEG2:%.*]] = add nuw i64 [[A:%.*]], -305
; CHECK-NEXT:    [[C_1:%.*]] = icmp ugt i64 0, [[NEG2]]
; CHECK-NEXT:    br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK:       exit.1:
; CHECK-NEXT:    ret i1 true
; CHECK:       exit.2:
; CHECK-NEXT:    [[C_3:%.*]] = icmp ugt i64 [[A]], 0
; CHECK-NEXT:    ret i1 [[C_3]]
;
entry:
  %neg2 = add nuw i64 %a, -305
  %c.1 = icmp ugt i64 0, %neg2
  br i1 %c.1, label %exit.1, label %exit.2

exit.1:
  %c.2 = icmp ugt i64 %a, 0
  ret i1 %c.2

exit.2:
  %c.3 = icmp ugt i64 %a, 0
  ret i1 %c.3
}

define i1 @add_nuw_neg2_i8(i8 %a) {
; CHECK-LABEL: @add_nuw_neg2_i8(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[NEG2:%.*]] = add nuw i8 [[A:%.*]], -4
; CHECK-NEXT:    [[C_1:%.*]] = icmp ult i8 [[NEG2]], -2
; CHECK-NEXT:    br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK:       exit.1:
; CHECK-NEXT:    [[C_2:%.*]] = icmp ult i8 [[A]], 1
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_2]]
; CHECK-NEXT:    ret i1 [[RES_1]]
; CHECK:       exit.2:
; CHECK-NEXT:    [[C_3:%.*]] = icmp ult i8 [[A]], 3
; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[C_3]], false
; CHECK-NEXT:    ret i1 [[RES_2]]
;
entry:
  %neg2 = add nuw i8 %a, -4
  %c.1 = icmp ult i8 %neg2, -2
  br i1 %c.1, label %exit.1, label %exit.2

exit.1:
  %t.1 = icmp ult i8 %a, 2
  %c.2 = icmp ult i8 %a, 1
  %res.1 = xor i1 %t.1, %c.2
  ret i1 %res.1

exit.2:
  %c.3 = icmp ult i8 %a, 3
  %f.1 = icmp ult i8 %a, 2
  %res.2 = xor i1 %c.3, %f.1
  ret i1 %res.2
}

define i1 @add_nuw_neg2_i64(i64 %a) {
; CHECK-LABEL: @add_nuw_neg2_i64(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[NEG2:%.*]] = add nuw i64 [[A:%.*]], -4
; CHECK-NEXT:    [[C_1:%.*]] = icmp ult i64 [[NEG2]], -2
; CHECK-NEXT:    br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
; CHECK:       exit.1:
; CHECK-NEXT:    [[T_1:%.*]] = icmp ult i64 [[A]], 2
; CHECK-NEXT:    [[C_2:%.*]] = icmp ult i64 [[A]], 1
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[T_1]], [[C_2]]
; CHECK-NEXT:    ret i1 [[RES_1]]
; CHECK:       exit.2:
; CHECK-NEXT:    [[C_3:%.*]] = icmp ult i64 [[A]], 3
; CHECK-NEXT:    [[F_1:%.*]] = icmp ult i64 [[A]], 2
; CHECK-NEXT:    [[RES_2:%.*]] = xor i1 [[C_3]], [[F_1]]
; CHECK-NEXT:    ret i1 [[RES_2]]
;
entry:
  %neg2 = add nuw i64 %a, -4
  %c.1 = icmp ult i64 %neg2, -2
  br i1 %c.1, label %exit.1, label %exit.2

exit.1:
  %t.1 = icmp ult i64 %a, 2
  %c.2 = icmp ult i64 %a, 1
  %res.1 = xor i1 %t.1, %c.2
  ret i1 %res.1

exit.2:
  %c.3 = icmp ult i64 %a, 3
  %f.1 = icmp ult i64 %a, 2
  %res.2 = xor i1 %c.3, %f.1
  ret i1 %res.2
}

define i1 @test_chained_adds_nuw_1(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_nuw_1(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], [[B]]
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], 2
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_2]], 14
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add nuw i8 %a, %b
  %add.2 = add nuw i8 %add.1, 2
  %t.1 = icmp uge i8 %add.2, 13
  %c.1 = icmp uge i8 %add.2, 14
  %res.1 = xor i1 %t.1, %c.1
  ret i1 %res.1
}

define i1 @test_chained_adds_nuw_2(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_nuw_2(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], [[B]]
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], 2
; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], [[A]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 19
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add nuw i8 %a, %b
  %add.2 = add nuw i8 %add.1, 2
  %add.3 = add nuw i8 %add.2, %a
  %t.1 = icmp uge i8 %add.3, 18
  %c.1 = icmp uge i8 %add.3, 19
  %res.1 = xor i1 %t.1, %c.1
  ret i1 %res.1
}

define i1 @test_chained_adds_nuw_3(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_nuw_3(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], 2
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], [[B]]
; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], [[A]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 19
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add nuw i8 %a, 2
  %add.2 = add nuw i8 %add.1, %b
  %add.3 = add nuw i8 %add.2, %a
  %t.1 = icmp uge i8 %add.3, 18
  %c.1 = icmp uge i8 %add.3, 19
  %res.1 = xor i1 %t.1, %c.1
  ret i1 %res.1
}

define i1 @test_chained_adds_nuw_4(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_nuw_4(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], 2
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], [[B]]
; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], 10
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 24
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 true, [[C_1]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add nuw i8 %a, 2
  %add.2 = add nuw i8 %add.1, %b
  %add.3 = add nuw i8 %add.2, 10
  %t.1 = icmp uge i8 %add.3, 23
  %c.1 = icmp uge i8 %add.3, 24
  %res.1 = xor i1 %t.1, %c.1
  ret i1 %res.1
}

define i1 @test_chained_adds_missing_nuw_1(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_missing_nuw_1(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add i8 [[A]], 2
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], [[B]]
; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], [[A]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 18
; CHECK-NEXT:    [[C_2:%.*]] = icmp uge i8 [[ADD_3]], 19
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[C_1]], [[C_2]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add i8 %a, 2
  %add.2 = add nuw i8 %add.1, %b
  %add.3 = add nuw i8 %add.2, %a
  %c.1 = icmp uge i8 %add.3, 18
  %c.2 = icmp uge i8 %add.3, 19
  %res.1 = xor i1 %c.1, %c.2
  ret i1 %res.1
}

define i1 @test_chained_adds_missing_nuw_2(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_missing_nuw_2(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], 2
; CHECK-NEXT:    [[ADD_2:%.*]] = add i8 [[ADD_1]], [[B]]
; CHECK-NEXT:    [[ADD_3:%.*]] = add nuw i8 [[ADD_2]], [[A]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 18
; CHECK-NEXT:    [[C_2:%.*]] = icmp uge i8 [[ADD_3]], 19
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[C_1]], [[C_2]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add nuw i8 %a, 2
  %add.2 = add i8 %add.1, %b
  %add.3 = add nuw i8 %add.2, %a
  %c.1 = icmp uge i8 %add.3, 18
  %c.2 = icmp uge i8 %add.3, 19
  %res.1 = xor i1 %c.1, %c.2
  ret i1 %res.1
}

define i1 @test_chained_adds_missing_nuw_3(i8 %a, i8 %b) {
; CHECK-LABEL: @test_chained_adds_missing_nuw_3(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[C_A:%.*]] = icmp uge i8 [[A:%.*]], 5
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_A]])
; CHECK-NEXT:    [[C_B:%.*]] = icmp uge i8 [[B:%.*]], 6
; CHECK-NEXT:    call void @llvm.assume(i1 [[C_B]])
; CHECK-NEXT:    [[ADD_1:%.*]] = add nuw i8 [[A]], 2
; CHECK-NEXT:    [[ADD_2:%.*]] = add nuw i8 [[ADD_1]], [[B]]
; CHECK-NEXT:    [[ADD_3:%.*]] = add i8 [[ADD_2]], [[A]]
; CHECK-NEXT:    [[C_1:%.*]] = icmp uge i8 [[ADD_3]], 18
; CHECK-NEXT:    [[C_2:%.*]] = icmp uge i8 [[ADD_3]], 19
; CHECK-NEXT:    [[RES_1:%.*]] = xor i1 [[C_1]], [[C_2]]
; CHECK-NEXT:    ret i1 [[RES_1]]
;
entry:
  %c.a = icmp uge i8 %a, 5
  call void @llvm.assume(i1 %c.a)
  %c.b = icmp uge i8 %b, 6
  call void @llvm.assume(i1 %c.b)
  %add.1 = add nuw i8 %a, 2
  %add.2 = add nuw i8 %add.1, %b
  %add.3 = add i8 %add.2, %a
  %c.1 = icmp uge i8 %add.3, 18
  %c.2 = icmp uge i8 %add.3, 19
  %res.1 = xor i1 %c.1, %c.2
  ret i1 %res.1
}

declare void @llvm.assume(i1)