1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3; RUN: opt -debugify -instcombine -S < %s | FileCheck %s -check-prefix DBGINFO
4
5target 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
7define i32 @mul(i32 %x, i32 %y) {
8; CHECK-LABEL: @mul(
9; CHECK-NEXT:    [[C:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
10; CHECK-NEXT:    [[D:%.*]] = and i32 [[C]], 255
11; CHECK-NEXT:    ret i32 [[D]]
12
13; Test that when zext is evaluated in different type
14; we preserve the debug information in the resulting
15; instruction.
16; DBGINFO-LABEL: @mul(
17; 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))
18; 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))
19; DBGINFO-NEXT:    [[C:%.*]] = mul i32 {{.*}}
20; DBGINFO-NEXT:    [[D:%.*]] = and i32 {{.*}}
21; DBGINFO-NEXT:    call void @llvm.dbg.value(metadata i32 [[C]]
22; DBGINFO-NEXT:    call void @llvm.dbg.value(metadata i32 [[D]]
23
24  %A = trunc i32 %x to i8
25  %B = trunc i32 %y to i8
26  %C = mul i8 %A, %B
27  %D = zext i8 %C to i32
28  ret i32 %D
29}
30
31define i32 @select1(i1 %cond, i32 %x, i32 %y, i32 %z) {
32; CHECK-LABEL: @select1(
33; CHECK-NEXT:    [[D:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
34; CHECK-NEXT:    [[E:%.*]] = select i1 [[COND:%.*]], i32 [[Z:%.*]], i32 [[D]]
35; CHECK-NEXT:    [[F:%.*]] = and i32 [[E]], 255
36; CHECK-NEXT:    ret i32 [[F]]
37;
38  %A = trunc i32 %x to i8
39  %B = trunc i32 %y to i8
40  %C = trunc i32 %z to i8
41  %D = add i8 %A, %B
42  %E = select i1 %cond, i8 %C, i8 %D
43  %F = zext i8 %E to i32
44  ret i32 %F
45}
46
47define i8 @select2(i1 %cond, i8 %x, i8 %y, i8 %z) {
48; CHECK-LABEL: @select2(
49; CHECK-NEXT:    [[D:%.*]] = add i8 [[X:%.*]], [[Y:%.*]]
50; CHECK-NEXT:    [[E:%.*]] = select i1 [[COND:%.*]], i8 [[Z:%.*]], i8 [[D]]
51; CHECK-NEXT:    ret i8 [[E]]
52;
53  %A = zext i8 %x to i32
54  %B = zext i8 %y to i32
55  %C = zext i8 %z to i32
56  %D = add i32 %A, %B
57  %E = select i1 %cond, i32 %C, i32 %D
58  %F = trunc i32 %E to i8
59  ret i8 %F
60}
61
62; The next 3 tests could be handled in instcombine, but evaluating values
63; with multiple uses may be very slow. Let some other pass deal with it.
64
65define i32 @eval_trunc_multi_use_in_one_inst(i32 %x) {
66; CHECK-LABEL: @eval_trunc_multi_use_in_one_inst(
67; CHECK-NEXT:    [[Z:%.*]] = zext i32 [[X:%.*]] to i64
68; CHECK-NEXT:    [[A:%.*]] = add nuw nsw i64 [[Z]], 15
69; CHECK-NEXT:    [[M:%.*]] = mul i64 [[A]], [[A]]
70; CHECK-NEXT:    [[T:%.*]] = trunc i64 [[M]] to i32
71; CHECK-NEXT:    ret i32 [[T]]
72;
73  %z = zext i32 %x to i64
74  %a = add nsw nuw i64 %z, 15
75  %m = mul i64 %a, %a
76  %t = trunc i64 %m to i32
77  ret i32 %t
78}
79
80define i32 @eval_zext_multi_use_in_one_inst(i32 %x) {
81; CHECK-LABEL: @eval_zext_multi_use_in_one_inst(
82; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[X:%.*]] to i16
83; CHECK-NEXT:    [[A:%.*]] = and i16 [[T]], 5
84; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]
85; CHECK-NEXT:    [[R:%.*]] = zext i16 [[M]] to i32
86; CHECK-NEXT:    ret i32 [[R]]
87;
88  %t = trunc i32 %x to i16
89  %a = and i16 %t, 5
90  %m = mul nuw nsw i16 %a, %a
91  %r = zext i16 %m to i32
92  ret i32 %r
93}
94
95define i32 @eval_sext_multi_use_in_one_inst(i32 %x) {
96; CHECK-LABEL: @eval_sext_multi_use_in_one_inst(
97; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[X:%.*]] to i16
98; CHECK-NEXT:    [[A:%.*]] = and i16 [[T]], 14
99; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]
100; CHECK-NEXT:    [[O:%.*]] = or i16 [[M]], -32768
101; CHECK-NEXT:    [[R:%.*]] = sext i16 [[O]] to i32
102; CHECK-NEXT:    ret i32 [[R]]
103;
104  %t = trunc i32 %x to i16
105  %a = and i16 %t, 14
106  %m = mul nuw nsw i16 %a, %a
107  %o = or i16 %m, 32768
108  %r = sext i16 %o to i32
109  ret i32 %r
110}
111
112; If we have a transform to shrink the above 3 cases, make sure it's not
113; also trying to look through multiple uses in this test and crashing.
114
115define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
116; CHECK-LABEL: @PR36225(
117; CHECK-NEXT:  entry:
118; CHECK-NEXT:    br label [[WHILE_BODY:%.*]]
119; CHECK:       while.body:
120; CHECK-NEXT:    br i1 [[C1:%.*]], label [[FOR_BODY3_US:%.*]], label [[FOR_BODY3:%.*]]
121; CHECK:       for.body3.us:
122; CHECK-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[B:%.*]], 0
123; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4
124; CHECK-NEXT:    switch i3 [[V1:%.*]], label [[EXIT:%.*]] [
125; CHECK-NEXT:    i3 0, label [[FOR_END:%.*]]
126; CHECK-NEXT:    i3 -1, label [[FOR_END]]
127; CHECK-NEXT:    ]
128; CHECK:       for.body3:
129; CHECK-NEXT:    switch i3 [[V2:%.*]], label [[EXIT]] [
130; CHECK-NEXT:    i3 0, label [[FOR_END]]
131; CHECK-NEXT:    i3 -1, label [[FOR_END]]
132; CHECK-NEXT:    ]
133; CHECK:       for.end:
134; CHECK-NEXT:    [[H:%.*]] = phi i8 [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ 0, [[FOR_BODY3]] ], [ 0, [[FOR_BODY3]] ]
135; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[H]] to i32
136; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[CONV]], [[A:%.*]]
137; CHECK-NEXT:    br i1 [[CMP]], label [[EXIT]], label [[EXIT2:%.*]]
138; CHECK:       exit2:
139; CHECK-NEXT:    unreachable
140; CHECK:       exit:
141; CHECK-NEXT:    unreachable
142;
143entry:
144  br label %while.body
145
146while.body:
147  %tobool = icmp eq i32 %b, 0
148  br i1 %c1, label %for.body3.us, label %for.body3
149
150for.body3.us:
151  %spec.select = select i1 %tobool, i8 0, i8 4
152  switch i3 %v1, label %exit [
153  i3 0, label %for.end
154  i3 -1, label %for.end
155  ]
156
157for.body3:
158  switch i3 %v2, label %exit [
159  i3 0, label %for.end
160  i3 -1, label %for.end
161  ]
162
163for.end:
164  %h = phi i8 [ %spec.select, %for.body3.us ], [ %spec.select, %for.body3.us ], [ 0, %for.body3 ], [ 0, %for.body3 ]
165  %conv = sext i8 %h to i32
166  %cmp = icmp sgt i32 %a, %conv
167  br i1 %cmp, label %exit, label %exit2
168
169exit2:
170  unreachable
171
172exit:
173  unreachable
174}
175
176; Check that we don't drop debug info when a zext is removed.
177define i1 @foo(i1 zeroext %b) {
178; CHECK-LABEL: @foo(
179; CHECK-NEXT:    ret i1 [[B:%.*]]
180;
181; DBGINFO-LABEL: @foo(
182; DBGINFO-NEXT:    call void @llvm.dbg.value(metadata i1 [[B:%.*]], metadata [[META108:![0-9]+]], metadata !DIExpression(DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value)), !dbg [[DBG109:![0-9]+]]
183; DBGINFO-NEXT:    ret i1 [[B]], !dbg [[DBG110:![0-9]+]]
184;
185
186  %frombool = zext i1 %b to i8
187  ret i1 %b
188}
189