xref: /llvm-project/llvm/test/Transforms/InstCombine/icmp-mul-zext.ll (revision 095d49da76be09143582e07a807c86d3b4334dec)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4target datalayout = "n32"
5
6define i32 @sterix(i32, i8, i64) {
7; CHECK-LABEL: @sterix(
8; CHECK-NEXT:  entry:
9; CHECK-NEXT:    [[CONV:%.*]] = zext i32 [[TMP0:%.*]] to i64
10; CHECK-NEXT:    [[CONV1:%.*]] = sext i8 [[TMP1:%.*]] to i32
11; CHECK-NEXT:    [[MUL:%.*]] = mul i32 [[CONV1]], 1945964878
12; CHECK-NEXT:    [[SH_PROM:%.*]] = trunc i64 [[TMP2:%.*]] to i32
13; CHECK-NEXT:    [[SHR:%.*]] = lshr i32 [[MUL]], [[SH_PROM]]
14; CHECK-NEXT:    [[CONV2:%.*]] = zext i32 [[SHR]] to i64
15; CHECK-NEXT:    [[MUL3:%.*]] = mul nuw nsw i64 [[CONV]], [[CONV2]]
16; CHECK-NEXT:    [[TOBOOL_NOT:%.*]] = icmp samesign ult i64 [[MUL3]], 4294967296
17; CHECK-NEXT:    br i1 [[TOBOOL_NOT]], label [[LOR_RHS:%.*]], label [[LOR_END:%.*]]
18; CHECK:       lor.rhs:
19; CHECK-NEXT:    [[AND:%.*]] = and i64 [[TMP2]], [[MUL3]]
20; CHECK-NEXT:    [[TOBOOL7_NOT:%.*]] = icmp eq i64 [[AND]], 0
21; CHECK-NEXT:    [[TMP3:%.*]] = zext i1 [[TOBOOL7_NOT]] to i32
22; CHECK-NEXT:    br label [[LOR_END]]
23; CHECK:       lor.end:
24; CHECK-NEXT:    [[CONV8:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ [[TMP3]], [[LOR_RHS]] ]
25; CHECK-NEXT:    ret i32 [[CONV8]]
26;
27entry:
28  %conv = zext i32 %0 to i64
29  %conv1 = sext i8 %1 to i32
30  %mul = mul i32 %conv1, 1945964878
31  %sh_prom = trunc i64 %2 to i32
32  %shr = lshr i32 %mul, %sh_prom
33  %conv2 = zext i32 %shr to i64
34  %mul3 = mul nuw nsw i64 %conv, %conv2
35  %conv6 = and i64 %mul3, 4294967295
36  %tobool = icmp ne i64 %conv6, %mul3
37  br i1 %tobool, label %lor.end, label %lor.rhs
38
39lor.rhs:
40  %and = and i64 %2, %mul3
41  %conv4 = trunc i64 %and to i32
42  %tobool7 = icmp ne i32 %conv4, 0
43  %lnot = xor i1 %tobool7, true
44  br label %lor.end
45
46lor.end:
47  %3 = phi i1 [ true, %entry ], [ %lnot, %lor.rhs ]
48  %conv8 = zext i1 %3 to i32
49  ret i32 %conv8
50}
51
52; https://bugs.llvm.org/show_bug.cgi?id=33765
53
54@glob = external global i16
55
56define void @PR33765(i8 %beth) {
57; CHECK-LABEL: @PR33765(
58; CHECK-NEXT:    br i1 false, label [[IF_THEN9:%.*]], label [[IF_THEN9]]
59; CHECK:       if.then9:
60; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[BETH:%.*]] to i32
61; CHECK-NEXT:    [[MUL:%.*]] = mul nuw nsw i32 [[CONV]], [[CONV]]
62; CHECK-NEXT:    [[TINKY:%.*]] = load i16, ptr @glob, align 2
63; CHECK-NEXT:    [[TMP1:%.*]] = trunc nuw i32 [[MUL]] to i16
64; CHECK-NEXT:    [[CONV14:%.*]] = and i16 [[TINKY]], [[TMP1]]
65; CHECK-NEXT:    store i16 [[CONV14]], ptr @glob, align 2
66; CHECK-NEXT:    ret void
67;
68  %conv = zext i8 %beth to i32
69  %mul = mul nuw nsw i32 %conv, %conv
70  %conv3 = and i32 %mul, 255
71  %tobool8 = icmp ne i32 %mul, %conv3
72  br i1 %tobool8, label %if.then9, label %if.then9
73
74if.then9:
75  %tinky = load i16, ptr @glob
76  %conv13 = sext i16 %tinky to i32
77  %and = and i32 %mul, %conv13
78  %conv14 = trunc i32 %and to i16
79  store i16 %conv14, ptr @glob
80  ret void
81}
82
83; Repro case for bug involving mutating a list while
84; iterating it.
85
86declare i16 @aux(i8)
87
88define i16 @iter_breaker(i16 %a, i16 %b) {
89; CHECK-LABEL: @iter_breaker(
90; CHECK-NEXT:    [[UMUL:%.*]] = call { i16, i1 } @llvm.umul.with.overflow.i16(i16 [[A:%.*]], i16 [[B:%.*]])
91; CHECK-NEXT:    [[UMUL_VALUE:%.*]] = extractvalue { i16, i1 } [[UMUL]], 0
92; CHECK-NEXT:    [[DID_OVF:%.*]] = extractvalue { i16, i1 } [[UMUL]], 1
93; CHECK-NEXT:    br i1 [[DID_OVF]], label [[RET1:%.*]], label [[RET2:%.*]]
94; CHECK:       ret1:
95; CHECK-NEXT:    [[TRUNC_REMAIN:%.*]] = trunc i16 [[UMUL_VALUE]] to i8
96; CHECK-NEXT:    [[VAL:%.*]] = call i16 @aux(i8 [[TRUNC_REMAIN]])
97; CHECK-NEXT:    ret i16 [[VAL]]
98; CHECK:       ret2:
99; CHECK-NEXT:    ret i16 [[UMUL_VALUE]]
100;
101  %a_wide = zext i16 %a to i32
102  %b_wide = zext i16 %b to i32
103  %mul_wide = mul i32 %a_wide, %b_wide              ; uses of %mul_wide will be iterated
104
105  %trunc_remain = trunc i32 %mul_wide to i8         ; this use will be replaced w/ new value
106  ; when iteration visits it, switching
107  ; iteration to the uses of new value
108
109  %trunc_unnecessary = trunc i32 %mul_wide to i16   ; uses of %trunc_unnecessary will have
110  ; been updated to uses of new value
111
112  %did_ovf = icmp ugt i32 %mul_wide, 65535
113  br i1 %did_ovf, label %ret1, label %ret2
114
115ret1:
116  %val = call i16 @aux(i8 %trunc_remain)
117  ret i16 %val
118
119ret2:
120  ret i16 %trunc_unnecessary              ; crash visiting this use after corrupting iterator
121}
122
123; This miscompiled because of broken pattern matching.
124
125define i1 @PR46561(i1 %a, i1 %x, i1 %y, i8 %z) {
126; CHECK-LABEL: @PR46561(
127; CHECK-NEXT:  entry:
128; CHECK-NEXT:    br i1 [[A:%.*]], label [[COND_TRUE:%.*]], label [[END:%.*]]
129; CHECK:       cond.true:
130; CHECK-NEXT:    [[MULBOOL:%.*]] = and i1 [[X:%.*]], [[Y:%.*]]
131; CHECK-NEXT:    [[TMP0:%.*]] = trunc i8 [[Z:%.*]] to i1
132; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[MULBOOL]], [[TMP0]]
133; CHECK-NEXT:    [[TMP2:%.*]] = xor i1 [[TMP1]], true
134; CHECK-NEXT:    br label [[END]]
135; CHECK:       end:
136; CHECK-NEXT:    [[P:%.*]] = phi i1 [ [[TMP2]], [[COND_TRUE]] ], [ false, [[ENTRY:%.*]] ]
137; CHECK-NEXT:    ret i1 [[P]]
138;
139entry:
140  br i1 %a, label %cond.true, label %end
141
142cond.true:
143  %tz = trunc i8 %z to i1
144  %zx = zext i1 %x to i32
145  %zy = zext i1 %y to i32
146  %zz = zext i1 %tz to i32
147  %mul = mul i32 %zx, %zy
148  %cond = xor i32 %mul, %zz
149  br label %end
150
151end:
152  %p = phi i32 [ %cond, %cond.true ], [ -1, %entry ]
153  %r = icmp eq i32 %p, 0
154  ret i1 %r
155}
156