1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2 ; RUN: opt -debugify -instcombine -S < %s | FileCheck %s -check-prefix DBGINFO
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32"
6 define i32 @mul(i32 %x, i32 %y) {
8 ; CHECK-NEXT: [[C:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
9 ; CHECK-NEXT: [[D:%.*]] = and i32 [[C]], 255
10 ; CHECK-NEXT: ret i32 [[D]]
12 ; Test that when zext is evaluated in different type
13 ; we preserve the debug information in the resulting
15 ; DBGINFO-LABEL: @mul(
16 ; DBGINFO-NEXT: [[C:%.*]] = mul i32 {{.*}}
17 ; DBGINFO-NEXT: [[D:%.*]] = and i32 {{.*}}
18 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 [[C]]
19 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 [[D]]
21 %A = trunc i32 %x to i8
22 %B = trunc i32 %y to i8
24 %D = zext i8 %C to i32
28 define i32 @select1(i1 %cond, i32 %x, i32 %y, i32 %z) {
29 ; CHECK-LABEL: @select1(
30 ; CHECK-NEXT: [[D:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
31 ; CHECK-NEXT: [[E:%.*]] = select i1 [[COND:%.*]], i32 [[Z:%.*]], i32 [[D]]
32 ; CHECK-NEXT: [[F:%.*]] = and i32 [[E]], 255
33 ; CHECK-NEXT: ret i32 [[F]]
35 %A = trunc i32 %x to i8
36 %B = trunc i32 %y to i8
37 %C = trunc i32 %z to i8
39 %E = select i1 %cond, i8 %C, i8 %D
40 %F = zext i8 %E to i32
44 define i8 @select2(i1 %cond, i8 %x, i8 %y, i8 %z) {
45 ; CHECK-LABEL: @select2(
46 ; CHECK-NEXT: [[D:%.*]] = add i8 [[X:%.*]], [[Y:%.*]]
47 ; CHECK-NEXT: [[E:%.*]] = select i1 [[COND:%.*]], i8 [[Z:%.*]], i8 [[D]]
48 ; CHECK-NEXT: ret i8 [[E]]
50 %A = zext i8 %x to i32
51 %B = zext i8 %y to i32
52 %C = zext i8 %z to i32
54 %E = select i1 %cond, i32 %C, i32 %D
55 %F = trunc i32 %E to i8
59 ; The next 3 tests could be handled in instcombine, but evaluating values
60 ; with multiple uses may be very slow. Let some other pass deal with it.
62 define i32 @eval_trunc_multi_use_in_one_inst(i32 %x) {
63 ; CHECK-LABEL: @eval_trunc_multi_use_in_one_inst(
64 ; CHECK-NEXT: [[Z:%.*]] = zext i32 [[X:%.*]] to i64
65 ; CHECK-NEXT: [[A:%.*]] = add nuw nsw i64 [[Z]], 15
66 ; CHECK-NEXT: [[M:%.*]] = mul i64 [[A]], [[A]]
67 ; CHECK-NEXT: [[T:%.*]] = trunc i64 [[M]] to i32
68 ; CHECK-NEXT: ret i32 [[T]]
70 %z = zext i32 %x to i64
71 %a = add nsw nuw i64 %z, 15
73 %t = trunc i64 %m to i32
77 define i32 @eval_zext_multi_use_in_one_inst(i32 %x) {
78 ; CHECK-LABEL: @eval_zext_multi_use_in_one_inst(
79 ; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i16
80 ; CHECK-NEXT: [[A:%.*]] = and i16 [[T]], 5
81 ; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]
82 ; CHECK-NEXT: [[R:%.*]] = zext i16 [[M]] to i32
83 ; CHECK-NEXT: ret i32 [[R]]
85 %t = trunc i32 %x to i16
87 %m = mul nuw nsw i16 %a, %a
88 %r = zext i16 %m to i32
92 define i32 @eval_sext_multi_use_in_one_inst(i32 %x) {
93 ; CHECK-LABEL: @eval_sext_multi_use_in_one_inst(
94 ; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i16
95 ; CHECK-NEXT: [[A:%.*]] = and i16 [[T]], 14
96 ; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]
97 ; CHECK-NEXT: [[O:%.*]] = or i16 [[M]], -32768
98 ; CHECK-NEXT: [[R:%.*]] = sext i16 [[O]] to i32
99 ; CHECK-NEXT: ret i32 [[R]]
101 %t = trunc i32 %x to i16
103 %m = mul nuw nsw i16 %a, %a
104 %o = or i16 %m, 32768
105 %r = sext i16 %o to i32
109 ; If we have a transform to shrink the above 3 cases, make sure it's not
110 ; also trying to look through multiple uses in this test and crashing.
112 define void @PR36225(i32 %a, i32 %b) {
113 ; CHECK-LABEL: @PR36225(
115 ; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
117 ; CHECK-NEXT: br i1 undef, label [[FOR_BODY3_US:%.*]], label [[FOR_BODY3:%.*]]
118 ; CHECK: for.body3.us:
119 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[B:%.*]], 0
120 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4
121 ; CHECK-NEXT: switch i3 undef, label [[EXIT:%.*]] [
122 ; CHECK-NEXT: i3 0, label [[FOR_END:%.*]]
123 ; CHECK-NEXT: i3 -1, label [[FOR_END]]
126 ; CHECK-NEXT: switch i3 undef, label [[EXIT]] [
127 ; CHECK-NEXT: i3 0, label [[FOR_END]]
128 ; CHECK-NEXT: i3 -1, label [[FOR_END]]
131 ; CHECK-NEXT: [[H:%.*]] = phi i8 [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ 0, [[FOR_BODY3]] ], [ 0, [[FOR_BODY3]] ]
132 ; CHECK-NEXT: [[TMP0:%.*]] = zext i8 [[H]] to i32
133 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[A:%.*]]
134 ; CHECK-NEXT: br i1 [[CMP]], label [[EXIT]], label [[EXIT2:%.*]]
136 ; CHECK-NEXT: unreachable
138 ; CHECK-NEXT: unreachable
144 %tobool = icmp eq i32 %b, 0
145 br i1 undef, label %for.body3.us, label %for.body3
148 %spec.select = select i1 %tobool, i8 0, i8 4
149 switch i3 undef, label %exit [
151 i3 -1, label %for.end
155 switch i3 undef, label %exit [
157 i3 -1, label %for.end
161 %h = phi i8 [ %spec.select, %for.body3.us ], [ %spec.select, %for.body3.us ], [ 0, %for.body3 ], [ 0, %for.body3 ]
162 %conv = sext i8 %h to i32
163 %cmp = icmp sgt i32 %a, %conv
164 br i1 %cmp, label %exit, label %exit2
173 ; Check that we don't drop debug info when a zext is removed.
174 define i1 @foo(i1 zeroext %b) {
175 ; DBGINFO-LABEL: @foo(
176 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i1 %b
177 ; DBGINFO-NEXT: ret i1 %b
179 %frombool = zext i1 %b to i8