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: call void @llvm.dbg.value(metadata i32 %x, {{.*}} !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value))
17 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 %y, {{.*}} !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value))
18 ; DBGINFO-NEXT: [[C:%.*]] = mul i32 {{.*}}
19 ; DBGINFO-NEXT: [[D:%.*]] = and i32 {{.*}}
20 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 [[C]]
21 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 [[D]]
23 %A = trunc i32 %x to i8
24 %B = trunc i32 %y to i8
26 %D = zext i8 %C to i32
30 define i32 @select1(i1 %cond, i32 %x, i32 %y, i32 %z) {
31 ; CHECK-LABEL: @select1(
32 ; CHECK-NEXT: [[D:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
33 ; CHECK-NEXT: [[E:%.*]] = select i1 [[COND:%.*]], i32 [[Z:%.*]], i32 [[D]]
34 ; CHECK-NEXT: [[F:%.*]] = and i32 [[E]], 255
35 ; CHECK-NEXT: ret i32 [[F]]
37 %A = trunc i32 %x to i8
38 %B = trunc i32 %y to i8
39 %C = trunc i32 %z to i8
41 %E = select i1 %cond, i8 %C, i8 %D
42 %F = zext i8 %E to i32
46 define i8 @select2(i1 %cond, i8 %x, i8 %y, i8 %z) {
47 ; CHECK-LABEL: @select2(
48 ; CHECK-NEXT: [[D:%.*]] = add i8 [[X:%.*]], [[Y:%.*]]
49 ; CHECK-NEXT: [[E:%.*]] = select i1 [[COND:%.*]], i8 [[Z:%.*]], i8 [[D]]
50 ; CHECK-NEXT: ret i8 [[E]]
52 %A = zext i8 %x to i32
53 %B = zext i8 %y to i32
54 %C = zext i8 %z to i32
56 %E = select i1 %cond, i32 %C, i32 %D
57 %F = trunc i32 %E to i8
61 ; The next 3 tests could be handled in instcombine, but evaluating values
62 ; with multiple uses may be very slow. Let some other pass deal with it.
64 define i32 @eval_trunc_multi_use_in_one_inst(i32 %x) {
65 ; CHECK-LABEL: @eval_trunc_multi_use_in_one_inst(
66 ; CHECK-NEXT: [[Z:%.*]] = zext i32 [[X:%.*]] to i64
67 ; CHECK-NEXT: [[A:%.*]] = add nuw nsw i64 [[Z]], 15
68 ; CHECK-NEXT: [[M:%.*]] = mul i64 [[A]], [[A]]
69 ; CHECK-NEXT: [[T:%.*]] = trunc i64 [[M]] to i32
70 ; CHECK-NEXT: ret i32 [[T]]
72 %z = zext i32 %x to i64
73 %a = add nsw nuw i64 %z, 15
75 %t = trunc i64 %m to i32
79 define i32 @eval_zext_multi_use_in_one_inst(i32 %x) {
80 ; CHECK-LABEL: @eval_zext_multi_use_in_one_inst(
81 ; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i16
82 ; CHECK-NEXT: [[A:%.*]] = and i16 [[T]], 5
83 ; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]
84 ; CHECK-NEXT: [[R:%.*]] = zext i16 [[M]] to i32
85 ; CHECK-NEXT: ret i32 [[R]]
87 %t = trunc i32 %x to i16
89 %m = mul nuw nsw i16 %a, %a
90 %r = zext i16 %m to i32
94 define i32 @eval_sext_multi_use_in_one_inst(i32 %x) {
95 ; CHECK-LABEL: @eval_sext_multi_use_in_one_inst(
96 ; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i16
97 ; CHECK-NEXT: [[A:%.*]] = and i16 [[T]], 14
98 ; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]
99 ; CHECK-NEXT: [[O:%.*]] = or i16 [[M]], -32768
100 ; CHECK-NEXT: [[R:%.*]] = sext i16 [[O]] to i32
101 ; CHECK-NEXT: ret i32 [[R]]
103 %t = trunc i32 %x to i16
105 %m = mul nuw nsw i16 %a, %a
106 %o = or i16 %m, 32768
107 %r = sext i16 %o to i32
111 ; If we have a transform to shrink the above 3 cases, make sure it's not
112 ; also trying to look through multiple uses in this test and crashing.
114 define void @PR36225(i32 %a, i32 %b) {
115 ; CHECK-LABEL: @PR36225(
117 ; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
119 ; CHECK-NEXT: br i1 undef, label [[FOR_BODY3_US:%.*]], label [[FOR_BODY3:%.*]]
120 ; CHECK: for.body3.us:
121 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[B:%.*]], 0
122 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4
123 ; CHECK-NEXT: switch i3 undef, label [[EXIT:%.*]] [
124 ; CHECK-NEXT: i3 0, label [[FOR_END:%.*]]
125 ; CHECK-NEXT: i3 -1, label [[FOR_END]]
128 ; CHECK-NEXT: switch i3 undef, label [[EXIT]] [
129 ; CHECK-NEXT: i3 0, label [[FOR_END]]
130 ; CHECK-NEXT: i3 -1, label [[FOR_END]]
133 ; CHECK-NEXT: [[H:%.*]] = phi i8 [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ 0, [[FOR_BODY3]] ], [ 0, [[FOR_BODY3]] ]
134 ; CHECK-NEXT: [[TMP0:%.*]] = zext i8 [[H]] to i32
135 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[A:%.*]]
136 ; CHECK-NEXT: br i1 [[CMP]], label [[EXIT]], label [[EXIT2:%.*]]
138 ; CHECK-NEXT: unreachable
140 ; CHECK-NEXT: unreachable
146 %tobool = icmp eq i32 %b, 0
147 br i1 undef, label %for.body3.us, label %for.body3
150 %spec.select = select i1 %tobool, i8 0, i8 4
151 switch i3 undef, label %exit [
153 i3 -1, label %for.end
157 switch i3 undef, label %exit [
159 i3 -1, label %for.end
163 %h = phi i8 [ %spec.select, %for.body3.us ], [ %spec.select, %for.body3.us ], [ 0, %for.body3 ], [ 0, %for.body3 ]
164 %conv = sext i8 %h to i32
165 %cmp = icmp sgt i32 %a, %conv
166 br i1 %cmp, label %exit, label %exit2
175 ; Check that we don't drop debug info when a zext is removed.
176 define i1 @foo(i1 zeroext %b) {
177 ; DBGINFO-LABEL: @foo(
178 ; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i1 %b, {{.*}} !DIExpression(DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value))
179 ; DBGINFO-NEXT: ret i1 %b
181 %frombool = zext i1 %b to i8