min-max-abs-cse.ll
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -S -O1 | FileCheck %s
; RUN: opt -passes='default<O1>' -S < %s | FileCheck %s
; In all tests, expect instcombine to canonicalize the select patterns
; for min/max/abs to allow CSE and subsequent simplification.
; sub (smax a,b), (smax a,b) --> 0
; FIXME: We should canonicalize min/max to a form
; where the cmp operands match the select operands.
define i8 @smax_nsw(i8 %a, i8 %b) {
; CHECK-LABEL: @smax_nsw(
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], [[B]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[SUB]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 0, i8 [[SUB]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[SUB]], i8 0
; CHECK-NEXT: [[R:%.*]] = sub nsw i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%sub = sub nsw i8 %a, %b
%cmp1 = icmp slt i8 %a, %b
%cmp2 = icmp sgt i8 %sub, 0
%m1 = select i1 %cmp1, i8 0, i8 %sub
%m2 = select i1 %cmp2, i8 %sub, i8 0
%r = sub i8 %m2, %m1
ret i8 %r
}
; or (abs a), (abs a) --> abs a
define i8 @abs_swapped(i8 %a) {
; CHECK-LABEL: @abs_swapped(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp sgt i8 %a, 0
%cmp2 = icmp slt i8 %a, 0
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = or i8 %m2, %m1
ret i8 %r
}
; xor (nabs a), (nabs a) --> 0
define i8 @nabs_swapped(i8 %a) {
; CHECK-LABEL: @nabs_swapped(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp slt i8 %a, 0
%cmp2 = icmp sgt i8 %a, 0
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = xor i8 %m2, %m1
ret i8 %r
}
; xor (abs a), (abs a) --> 0
define i8 @abs_different_constants(i8 %a) {
; CHECK-LABEL: @abs_different_constants(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A]], -1
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp sgt i8 %a, -1
%cmp2 = icmp slt i8 %a, 0
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = xor i8 %m2, %m1
ret i8 %r
}
; or (nabs a), (nabs a) --> nabs a
define i8 @nabs_different_constants(i8 %a) {
; CHECK-LABEL: @nabs_different_constants(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[A]], -1
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp slt i8 %a, 0
%cmp2 = icmp sgt i8 %a, -1
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = or i8 %m2, %m1
ret i8 %r
}