blob: 99190633547c77bc75c1e71debf9131520f2e2a6 [file] [log] [blame]
Sanjay Patel183f90a2016-11-29 18:35:04 +00001; RUN: llc < %s -mtriple=aarch64-unknown-unknown | FileCheck %s
2
3; Compare if negative and select of constants where one constant is zero.
4
5define i32 @neg_sel_constants(i32 %a) {
6; CHECK-LABEL: neg_sel_constants:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00007; CHECK: // %bb.0:
Sanjay Patel183f90a2016-11-29 18:35:04 +00008; CHECK-NEXT: mov w8, #5
9; CHECK-NEXT: and w0, w8, w0, asr #31
10; CHECK-NEXT: ret
11;
12 %tmp.1 = icmp slt i32 %a, 0
13 %retval = select i1 %tmp.1, i32 5, i32 0
14 ret i32 %retval
15}
16
17; Compare if negative and select of constants where one constant is zero and the other is a single bit.
18
19define i32 @neg_sel_special_constant(i32 %a) {
20; CHECK-LABEL: neg_sel_special_constant:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000021; CHECK: // %bb.0:
Sanjay Patel183f90a2016-11-29 18:35:04 +000022; CHECK-NEXT: lsr w8, w0, #22
23; CHECK-NEXT: and w0, w8, #0x200
24; CHECK-NEXT: ret
25;
26 %tmp.1 = icmp slt i32 %a, 0
27 %retval = select i1 %tmp.1, i32 512, i32 0
28 ret i32 %retval
29}
30
31; Compare if negative and select variable or zero.
32
33define i32 @neg_sel_variable_and_zero(i32 %a, i32 %b) {
34; CHECK-LABEL: neg_sel_variable_and_zero:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000035; CHECK: // %bb.0:
Sanjay Patel183f90a2016-11-29 18:35:04 +000036; CHECK-NEXT: and w0, w1, w0, asr #31
37; CHECK-NEXT: ret
38;
39 %tmp.1 = icmp slt i32 %a, 0
40 %retval = select i1 %tmp.1, i32 %b, i32 0
41 ret i32 %retval
42}
43
44; Compare if not positive and select the same variable as being compared: smin(a, 0).
45
46define i32 @not_pos_sel_same_variable(i32 %a) {
47; CHECK-LABEL: not_pos_sel_same_variable:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000048; CHECK: // %bb.0:
Sanjay Patel183f90a2016-11-29 18:35:04 +000049; CHECK-NEXT: and w0, w0, w0, asr #31
50; CHECK-NEXT: ret
51;
52 %tmp = icmp slt i32 %a, 1
53 %min = select i1 %tmp, i32 %a, i32 0
54 ret i32 %min
55}
56
Sanjay Patelafee21a2016-12-14 22:59:14 +000057; Flipping the comparison condition can be handled by getting the bitwise not of the sign mask.
Sanjay Patel183f90a2016-11-29 18:35:04 +000058
59; Compare if positive and select of constants where one constant is zero.
60
61define i32 @pos_sel_constants(i32 %a) {
62; CHECK-LABEL: pos_sel_constants:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000063; CHECK: // %bb.0:
Sanjay Patel183f90a2016-11-29 18:35:04 +000064; CHECK-NEXT: mov w8, #5
Sanjay Patelafee21a2016-12-14 22:59:14 +000065; CHECK-NEXT: bic w0, w8, w0, asr #31
Sanjay Patel183f90a2016-11-29 18:35:04 +000066; CHECK-NEXT: ret
67;
68 %tmp.1 = icmp sgt i32 %a, -1
69 %retval = select i1 %tmp.1, i32 5, i32 0
70 ret i32 %retval
71}
72
73; Compare if positive and select of constants where one constant is zero and the other is a single bit.
74
75define i32 @pos_sel_special_constant(i32 %a) {
76; CHECK-LABEL: pos_sel_special_constant:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000077; CHECK: // %bb.0:
Sanjay Patelafee21a2016-12-14 22:59:14 +000078; CHECK-NEXT: orr w8, wzr, #0x200
79; CHECK-NEXT: bic w0, w8, w0, lsr #22
Sanjay Patel183f90a2016-11-29 18:35:04 +000080; CHECK-NEXT: ret
81;
82 %tmp.1 = icmp sgt i32 %a, -1
83 %retval = select i1 %tmp.1, i32 512, i32 0
84 ret i32 %retval
85}
86
87; Compare if positive and select variable or zero.
88
89define i32 @pos_sel_variable_and_zero(i32 %a, i32 %b) {
90; CHECK-LABEL: pos_sel_variable_and_zero:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000091; CHECK: // %bb.0:
Sanjay Patelafee21a2016-12-14 22:59:14 +000092; CHECK-NEXT: bic w0, w1, w0, asr #31
Sanjay Patel183f90a2016-11-29 18:35:04 +000093; CHECK-NEXT: ret
94;
95 %tmp.1 = icmp sgt i32 %a, -1
96 %retval = select i1 %tmp.1, i32 %b, i32 0
97 ret i32 %retval
98}
99
100; Compare if not negative or zero and select the same variable as being compared: smax(a, 0).
101
102define i32 @not_neg_sel_same_variable(i32 %a) {
103; CHECK-LABEL: not_neg_sel_same_variable:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000104; CHECK: // %bb.0:
Sanjay Patelafee21a2016-12-14 22:59:14 +0000105; CHECK-NEXT: bic w0, w0, w0, asr #31
Sanjay Patel183f90a2016-11-29 18:35:04 +0000106; CHECK-NEXT: ret
107;
108 %tmp = icmp sgt i32 %a, 0
109 %min = select i1 %tmp, i32 %a, i32 0
110 ret i32 %min
111}
112
113; https://llvm.org/bugs/show_bug.cgi?id=31175
114
115; ret = (x-y) > 0 ? x-y : 0
116define i32 @PR31175(i32 %x, i32 %y) {
117; CHECK-LABEL: PR31175:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000118; CHECK: // %bb.0:
Sanjay Patel183f90a2016-11-29 18:35:04 +0000119; CHECK-NEXT: sub w8, w0, w1
Sanjay Patelafee21a2016-12-14 22:59:14 +0000120; CHECK-NEXT: bic w0, w8, w8, asr #31
Sanjay Patel183f90a2016-11-29 18:35:04 +0000121; CHECK-NEXT: ret
122;
123 %sub = sub nsw i32 %x, %y
124 %cmp = icmp sgt i32 %sub, 0
125 %sel = select i1 %cmp, i32 %sub, i32 0
126 ret i32 %sel
127}
128