1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; These patterns are all just traditional clamp pattern.
5 ; But they are not canonical, the and/or/xor is more canonically represented
8 define i32 @t0_select_cond_and_v0(i32 %X) {
9 ; CHECK-LABEL: @t0_select_cond_and_v0(
10 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
11 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
12 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
13 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
14 ; CHECK-NEXT: ret i32 [[R]]
16 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
17 %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
18 %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
19 %dont_need_to_clamp = and i1 %dont_need_to_clamp_positive, %dont_need_to_clamp_negative
20 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
24 define i32 @t0_select_cond_and_v0_logical(i32 %X) {
25 ; CHECK-LABEL: @t0_select_cond_and_v0_logical(
26 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
27 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
28 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
29 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
30 ; CHECK-NEXT: ret i32 [[R]]
32 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
33 %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
34 %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
35 %dont_need_to_clamp = select i1 %dont_need_to_clamp_positive, i1 %dont_need_to_clamp_negative, i1 false
36 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
39 define i32 @t1_select_cond_and_v1(i32 %X) {
40 ; CHECK-LABEL: @t1_select_cond_and_v1(
41 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
42 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
43 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
44 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
45 ; CHECK-NEXT: ret i32 [[R]]
47 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
48 %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
49 %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
50 %dont_need_to_clamp = and i1 %dont_need_to_clamp_positive, %dont_need_to_clamp_negative
51 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
55 define i32 @t1_select_cond_and_v1_logical(i32 %X) {
56 ; CHECK-LABEL: @t1_select_cond_and_v1_logical(
57 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
58 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
59 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
60 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
61 ; CHECK-NEXT: ret i32 [[R]]
63 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
64 %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
65 %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
66 %dont_need_to_clamp = select i1 %dont_need_to_clamp_positive, i1 %dont_need_to_clamp_negative, i1 false
67 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
71 ;-------------------------------------------------------------------------------
73 define i32 @t2_select_cond_or_v0(i32 %X) {
74 ; CHECK-LABEL: @t2_select_cond_or_v0(
75 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
76 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
77 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
78 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
79 ; CHECK-NEXT: ret i32 [[R]]
81 %need_to_clamp_positive = icmp sgt i32 %X, 32767
82 %need_to_clamp_negative = icmp slt i32 %X, -32768
83 %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
84 %need_to_clamp = or i1 %need_to_clamp_positive, %need_to_clamp_negative
85 %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
89 define i32 @t2_select_cond_or_v0_logical(i32 %X) {
90 ; CHECK-LABEL: @t2_select_cond_or_v0_logical(
91 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
92 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
93 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
94 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
95 ; CHECK-NEXT: ret i32 [[R]]
97 %need_to_clamp_positive = icmp sgt i32 %X, 32767
98 %need_to_clamp_negative = icmp slt i32 %X, -32768
99 %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
100 %need_to_clamp = select i1 %need_to_clamp_positive, i1 true, i1 %need_to_clamp_negative
101 %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
104 define i32 @t3_select_cond_or_v1(i32 %X) {
105 ; CHECK-LABEL: @t3_select_cond_or_v1(
106 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
107 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
108 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
109 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
110 ; CHECK-NEXT: ret i32 [[R]]
112 %need_to_clamp_positive = icmp sgt i32 %X, 32767
113 %need_to_clamp_negative = icmp slt i32 %X, -32768
114 %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
115 %need_to_clamp = or i1 %need_to_clamp_positive, %need_to_clamp_negative
116 %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
120 define i32 @t3_select_cond_or_v1_logical(i32 %X) {
121 ; CHECK-LABEL: @t3_select_cond_or_v1_logical(
122 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
123 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
124 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
125 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
126 ; CHECK-NEXT: ret i32 [[R]]
128 %need_to_clamp_positive = icmp sgt i32 %X, 32767
129 %need_to_clamp_negative = icmp slt i32 %X, -32768
130 %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
131 %need_to_clamp = select i1 %need_to_clamp_positive, i1 true, i1 %need_to_clamp_negative
132 %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
136 ;-------------------------------------------------------------------------------
138 define i32 @t4_select_cond_xor_v0(i32 %X) {
139 ; CHECK-LABEL: @t4_select_cond_xor_v0(
140 ; CHECK-NEXT: [[DOTINV:%.*]] = icmp sgt i32 [[X:%.*]], -32768
141 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], i32 [[X]], i32 -32768
142 ; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
143 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
144 ; CHECK-NEXT: ret i32 [[R]]
146 %need_to_clamp_positive = icmp sgt i32 %X, 32767
147 %dont_need_to_clamp_negative = icmp sgt i32 %X, -32768
148 %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
149 %dont_need_to_clamp = xor i1 %need_to_clamp_positive, %dont_need_to_clamp_negative
150 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
153 define i32 @t4_select_cond_xor_v1(i32 %X) {
154 ; CHECK-LABEL: @t4_select_cond_xor_v1(
155 ; CHECK-NEXT: [[DOTINV:%.*]] = icmp sgt i32 [[X:%.*]], -32768
156 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], i32 [[X]], i32 -32768
157 ; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
158 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
159 ; CHECK-NEXT: ret i32 [[R]]
161 %need_to_clamp_positive = icmp sgt i32 %X, 32767
162 %dont_need_to_clamp_negative = icmp sgt i32 %X, -32768
163 %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
164 %dont_need_to_clamp = xor i1 %need_to_clamp_positive, %dont_need_to_clamp_negative
165 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
169 define i32 @t5_select_cond_xor_v2(i32 %X) {
170 ; CHECK-LABEL: @t5_select_cond_xor_v2(
171 ; CHECK-NEXT: [[DOTINV:%.*]] = icmp sgt i32 [[X:%.*]], -32768
172 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], i32 [[X]], i32 -32768
173 ; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
174 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
175 ; CHECK-NEXT: ret i32 [[R]]
177 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
178 %need_to_clamp_negative = icmp sle i32 %X, -32768
179 %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
180 %dont_need_to_clamp = xor i1 %dont_need_to_clamp_positive, %need_to_clamp_negative
181 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
184 define i32 @t5_select_cond_xor_v3(i32 %X) {
185 ; CHECK-LABEL: @t5_select_cond_xor_v3(
186 ; CHECK-NEXT: [[DOTINV:%.*]] = icmp sgt i32 [[X:%.*]], -32768
187 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], i32 [[X]], i32 -32768
188 ; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
189 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
190 ; CHECK-NEXT: ret i32 [[R]]
192 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
193 %need_to_clamp_negative = icmp sle i32 %X, -32768
194 %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
195 %dont_need_to_clamp = xor i1 %dont_need_to_clamp_positive, %need_to_clamp_negative
196 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit