1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4; PR30281 - https://llvm.org/bugs/show_bug.cgi?id=30281
5
6; All of these tests contain foldable division-by-constant instructions, but we
7; can't assert that those folds have occurred before we process the later icmp.
8
9define i32 @icmp_div(i16 %a, i16 %c) {
10; CHECK-LABEL: @icmp_div(
11; CHECK-NEXT:  entry:
12; CHECK-NEXT:    [[TOBOOL:%.*]] = icmp eq i16 %a, 0
13; CHECK-NEXT:    br i1 [[TOBOOL]], label %then, label %exit
14; CHECK:       then:
15; CHECK-NEXT:    [[NOT_CMP:%.*]] = icmp eq i16 %c, 0
16; CHECK-NEXT:    [[PHITMP1:%.*]] = sext i1 [[NOT_CMP]] to i32
17; CHECK-NEXT:    br label %exit
18; CHECK:       exit:
19; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ -1, %entry ], [ [[PHITMP1]], %then ]
20; CHECK-NEXT:    ret i32 [[PHI]]
21;
22entry:
23  %tobool = icmp eq i16 %a, 0
24  br i1 %tobool, label %then, label %exit
25
26then:
27  %div = sdiv i16 %c, -1
28  %cmp = icmp ne i16 %div, 0
29  br label %exit
30
31exit:
32  %phi = phi i1 [ false, %entry ], [ %cmp, %then ]
33  %zext = zext i1 %phi to i32
34  %add = add nsw i32 %zext, -1
35  ret i32 %add
36}
37
38define i32 @icmp_div2(i16 %a, i16 %c) {
39; CHECK-LABEL: @icmp_div2(
40; CHECK-NEXT:  entry:
41; CHECK-NEXT:    [[TOBOOL:%.*]] = icmp eq i16 %a, 0
42; CHECK-NEXT:    br i1 [[TOBOOL]], label %then, label %exit
43; CHECK:       then:
44; CHECK-NEXT:    br label %exit
45; CHECK:       exit:
46; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ -1, %entry ], [ 0, %then ]
47; CHECK-NEXT:    ret i32 [[PHI]]
48;
49entry:
50  %tobool = icmp eq i16 %a, 0
51  br i1 %tobool, label %then, label %exit
52
53then:
54  %div = sdiv i16 %c, 0
55  %cmp = icmp ne i16 %div, 0
56  br label %exit
57
58exit:
59  %phi = phi i1 [ false, %entry ], [ %cmp, %then ]
60  %zext = zext i1 %phi to i32
61  %add = add nsw i32 %zext, -1
62  ret i32 %add
63}
64
65define i32 @icmp_div3(i16 %a, i16 %c) {
66; CHECK-LABEL: @icmp_div3(
67; CHECK-NEXT:  entry:
68; CHECK-NEXT:    [[TOBOOL:%.*]] = icmp eq i16 %a, 0
69; CHECK-NEXT:    br i1 [[TOBOOL]], label %then, label %exit
70; CHECK:       then:
71; CHECK-NEXT:    [[NOT_CMP:%.*]] = icmp eq i16 %c, 0
72; CHECK-NEXT:    [[PHITMP1:%.*]] = sext i1 [[NOT_CMP]] to i32
73; CHECK-NEXT:    br label %exit
74; CHECK:       exit:
75; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ -1, %entry ], [ [[PHITMP1]], %then ]
76; CHECK-NEXT:    ret i32 [[PHI]]
77;
78entry:
79  %tobool = icmp eq i16 %a, 0
80  br i1 %tobool, label %then, label %exit
81
82then:
83  %div = sdiv i16 %c, 1
84  %cmp = icmp ne i16 %div, 0
85  br label %exit
86
87exit:
88  %phi = phi i1 [ false, %entry ], [ %cmp, %then ]
89  %zext = zext i1 %phi to i32
90  %add = add nsw i32 %zext, -1
91  ret i32 %add
92}
93
94