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: [[DONT_NEED_TO_CLAMP_POSITIVE:%.*]] = icmp slt i32 [[X:%.*]], 32768
11 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[DONT_NEED_TO_CLAMP_POSITIVE]], i32 -32768, i32 32767
12 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X]], 32768
13 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X_OFF]], 65536
14 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 [[CLAMP_LIMIT]]
15 ; CHECK-NEXT: ret i32 [[R]]
17 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
18 %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
19 %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
20 %dont_need_to_clamp = and i1 %dont_need_to_clamp_positive, %dont_need_to_clamp_negative
21 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
24 define i32 @t1_select_cond_and_v1(i32 %X) {
25 ; CHECK-LABEL: @t1_select_cond_and_v1(
26 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP_NEGATIVE:%.*]] = icmp sgt i32 [[X:%.*]], -32769
27 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[DONT_NEED_TO_CLAMP_NEGATIVE]], i32 32767, i32 -32768
28 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X]], 32768
29 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X_OFF]], 65536
30 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 [[CLAMP_LIMIT]]
31 ; CHECK-NEXT: ret i32 [[R]]
33 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
34 %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
35 %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
36 %dont_need_to_clamp = and i1 %dont_need_to_clamp_positive, %dont_need_to_clamp_negative
37 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
41 ;-------------------------------------------------------------------------------
43 define i32 @t2_select_cond_or_v0(i32 %X) {
44 ; CHECK-LABEL: @t2_select_cond_or_v0(
45 ; CHECK-NEXT: [[NEED_TO_CLAMP_POSITIVE:%.*]] = icmp sgt i32 [[X:%.*]], 32767
46 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[NEED_TO_CLAMP_POSITIVE]], i32 32767, i32 -32768
47 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X]], 32768
48 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X_OFF]], 65535
49 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[CLAMP_LIMIT]], i32 [[X]]
50 ; CHECK-NEXT: ret i32 [[R]]
52 %need_to_clamp_positive = icmp sgt i32 %X, 32767
53 %need_to_clamp_negative = icmp slt i32 %X, -32768
54 %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
55 %need_to_clamp = or i1 %need_to_clamp_positive, %need_to_clamp_negative
56 %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
59 define i32 @t3_select_cond_or_v1(i32 %X) {
60 ; CHECK-LABEL: @t3_select_cond_or_v1(
61 ; CHECK-NEXT: [[NEED_TO_CLAMP_NEGATIVE:%.*]] = icmp slt i32 [[X:%.*]], -32768
62 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[NEED_TO_CLAMP_NEGATIVE]], i32 -32768, i32 32767
63 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X]], 32768
64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X_OFF]], 65535
65 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[CLAMP_LIMIT]], i32 [[X]]
66 ; CHECK-NEXT: ret i32 [[R]]
68 %need_to_clamp_positive = icmp sgt i32 %X, 32767
69 %need_to_clamp_negative = icmp slt i32 %X, -32768
70 %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
71 %need_to_clamp = or i1 %need_to_clamp_positive, %need_to_clamp_negative
72 %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
76 ;-------------------------------------------------------------------------------
78 define i32 @t4_select_cond_xor_v0(i32 %X) {
79 ; CHECK-LABEL: @t4_select_cond_xor_v0(
80 ; CHECK-NEXT: [[NEED_TO_CLAMP_POSITIVE:%.*]] = icmp sgt i32 [[X:%.*]], 32767
81 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP_NEGATIVE:%.*]] = icmp sgt i32 [[X]], -32768
82 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[NEED_TO_CLAMP_POSITIVE]], i32 32767, i32 -32768
83 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP:%.*]] = xor i1 [[NEED_TO_CLAMP_POSITIVE]], [[DONT_NEED_TO_CLAMP_NEGATIVE]]
84 ; CHECK-NEXT: [[R:%.*]] = select i1 [[DONT_NEED_TO_CLAMP]], i32 [[X]], i32 [[CLAMP_LIMIT]]
85 ; CHECK-NEXT: ret i32 [[R]]
87 %need_to_clamp_positive = icmp sgt i32 %X, 32767
88 %dont_need_to_clamp_negative = icmp sgt i32 %X, -32768
89 %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
90 %dont_need_to_clamp = xor i1 %need_to_clamp_positive, %dont_need_to_clamp_negative
91 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
94 define i32 @t4_select_cond_xor_v1(i32 %X) {
95 ; CHECK-LABEL: @t4_select_cond_xor_v1(
96 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP_NEGATIVE:%.*]] = icmp sgt i32 [[X:%.*]], -32768
97 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[DONT_NEED_TO_CLAMP_NEGATIVE]], i32 32767, i32 -32768
98 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X]], 32767
99 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X_OFF]], 65535
100 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 [[CLAMP_LIMIT]]
101 ; CHECK-NEXT: ret i32 [[R]]
103 %need_to_clamp_positive = icmp sgt i32 %X, 32767
104 %dont_need_to_clamp_negative = icmp sgt i32 %X, -32768
105 %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
106 %dont_need_to_clamp = xor i1 %need_to_clamp_positive, %dont_need_to_clamp_negative
107 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
111 define i32 @t5_select_cond_xor_v2(i32 %X) {
112 ; CHECK-LABEL: @t5_select_cond_xor_v2(
113 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP_POSITIVE:%.*]] = icmp slt i32 [[X:%.*]], 32768
114 ; CHECK-NEXT: [[NEED_TO_CLAMP_NEGATIVE:%.*]] = icmp slt i32 [[X]], -32767
115 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[NEED_TO_CLAMP_NEGATIVE]], i32 -32768, i32 32767
116 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP:%.*]] = xor i1 [[DONT_NEED_TO_CLAMP_POSITIVE]], [[NEED_TO_CLAMP_NEGATIVE]]
117 ; CHECK-NEXT: [[R:%.*]] = select i1 [[DONT_NEED_TO_CLAMP]], i32 [[X]], i32 [[CLAMP_LIMIT]]
118 ; CHECK-NEXT: ret i32 [[R]]
120 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
121 %need_to_clamp_negative = icmp sle i32 %X, -32768
122 %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
123 %dont_need_to_clamp = xor i1 %dont_need_to_clamp_positive, %need_to_clamp_negative
124 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
127 define i32 @t5_select_cond_xor_v3(i32 %X) {
128 ; CHECK-LABEL: @t5_select_cond_xor_v3(
129 ; CHECK-NEXT: [[DONT_NEED_TO_CLAMP_POSITIVE:%.*]] = icmp slt i32 [[X:%.*]], 32768
130 ; CHECK-NEXT: [[CLAMP_LIMIT:%.*]] = select i1 [[DONT_NEED_TO_CLAMP_POSITIVE]], i32 -32768, i32 32767
131 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X]], 32767
132 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X_OFF]], 65535
133 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 [[CLAMP_LIMIT]]
134 ; CHECK-NEXT: ret i32 [[R]]
136 %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
137 %need_to_clamp_negative = icmp sle i32 %X, -32768
138 %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
139 %dont_need_to_clamp = xor i1 %dont_need_to_clamp_positive, %need_to_clamp_negative
140 %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit