File: select-implied.ll

package info (click to toggle)
llvm-toolchain-5.0 1:5.0.1-2~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 553,688 kB
  • sloc: cpp: 2,878,786; ansic: 584,110; asm: 246,252; python: 124,751; objc: 106,925; sh: 21,542; lisp: 8,628; pascal: 5,885; ml: 5,544; perl: 5,312; makefile: 2,208; cs: 2,022; xml: 686; php: 212; csh: 117
file content (200 lines) | stat: -rw-r--r-- 4,141 bytes parent folder | download | duplicates (7)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
; RUN: opt < %s -instcombine -S | FileCheck %s

; A == B implies A >u B is false.
; CHECK-LABEL: @test1
; CHECK-NOT: select
; CHECK: call void @foo(i32 10)
define void @test1(i32 %a, i32 %b) {
  %cmp1 = icmp eq i32 %a, %b
  br i1 %cmp1, label %taken, label %end

taken:
  %cmp2 = icmp ugt i32 %a, %b
  %c = select i1 %cmp2, i32 0, i32 10
  call void @foo(i32 %c)
  br label %end

end:
  ret void
}

; If A == B is false then A != B is true.
; CHECK-LABEL: @test2
; CHECK-NOT: select
; CHECK: call void @foo(i32 20)
define void @test2(i32 %a, i32 %b) {
  %cmp1 = icmp eq i32 %a, %b
  br i1 %cmp1, label %end, label %taken

taken:
  %cmp2 = icmp ne i32 %a, %b
  %c = select i1 %cmp2, i32 20, i32 0
  call void @foo(i32 %c)
  br label %end

end:
  ret void
}

; A >u 10 implies A >u 10 is true.
; CHECK-LABEL: @test3
; CHECK-NOT: select
; CHECK: call void @foo(i32 30)
define void @test3(i32 %a) {
  %cmp1 = icmp ugt i32 %a, 10
  br i1 %cmp1, label %taken, label %end

taken:
  %cmp2 = icmp ugt i32 %a, 10
  %c = select i1 %cmp2, i32 30, i32 0
  call void @foo(i32 %c)
  br label %end

end:
  ret void
}

; CHECK-LABEL: @PR23333
; CHECK-NOT: select
; CHECK: ret i8 1
define i8 @PR23333(i8 addrspace(1)* %ptr) {
   %cmp = icmp eq i8 addrspace(1)* %ptr, null
   br i1 %cmp, label %taken, label %end

taken:
   %cmp2 = icmp ne i8 addrspace(1)* %ptr, null
   %res = select i1 %cmp2, i8 2, i8 1
   ret i8 %res

end:
   ret i8 0
}

; We know the condition of the select is true based on a dominating condition.
; Therefore, we can replace %cond with %len. However, now the inner icmp is
; always false and can be elided.
; CHECK-LABEL: @test4
; CHECK-NOT: select
define void @test4(i32 %len) {
entry:
  %0 = call i32 @bar(i32 %len);
  %cmp = icmp ult i32 %len, 4
  br i1 %cmp, label %bb, label %b1
bb:
  %cond = select i1 %cmp, i32 %len, i32 8
; CHECK-NOT:  %cmp11 = icmp eq i32 %{{.*}}, 8
  %cmp11 = icmp eq i32 %cond, 8
; CHECK: br i1 false, label %b0, label %b1
  br i1 %cmp11, label %b0, label %b1

b0:
  call void @foo(i32 %len)
  br label %b1

b1:
; CHECK: phi i32 [ %len, %bb ], [ undef, %b0 ], [ %0, %entry ]
  %1 = phi i32 [ %cond, %bb ], [ undef, %b0 ], [ %0, %entry ]
  br label %ret

ret:
  call void @foo(i32 %1)
  ret void
}

; A >u 10 implies A >u 9 is true.
; CHECK-LABEL: @test5
; CHECK-NOT: select
; CHECK: call void @foo(i32 30)
define void @test5(i32 %a) {
  %cmp1 = icmp ugt i32 %a, 10
  br i1 %cmp1, label %taken, label %end

taken:
  %cmp2 = icmp ugt i32 %a, 9
  %c = select i1 %cmp2, i32 30, i32 0
  call void @foo(i32 %c)
  br label %end

end:
  ret void
}

declare void @foo(i32)
declare i32 @bar(i32)

; CHECK-LABEL: @test_and
; CHECK: tpath:
; CHECK-NOT: select
; CHECK: ret i32 313
define i32 @test_and(i32 %a, i32 %b) {
entry:
  %cmp1 = icmp ne i32 %a, 0
  %cmp2 = icmp ne i32 %b, 0
  %and = and i1 %cmp1, %cmp2
  br i1 %and, label %tpath, label %end

tpath:
  %cmp3 = icmp eq i32 %a, 0 ;; <-- implied false
  %c = select i1 %cmp3, i32 0, i32 313
  ret i32 %c

end:
  ret i32 0
}

; cmp1 and cmp2 are false on the 'fpath' path and thus cmp3 is true.
; CHECK-LABEL: @test_or1
; CHECK: fpath:
; CHECK-NOT: select
; CHECK: ret i32 37
define i32 @test_or1(i32 %a, i32 %b) {
entry:
  %cmp1 = icmp eq i32 %a, 0
  %cmp2 = icmp eq i32 %b, 0
  %or = or i1 %cmp1, %cmp2
  br i1 %or, label %end, label %fpath

fpath:
  %cmp3 = icmp ne i32 %a, 0  ;; <-- implied true
  %c = select i1 %cmp3, i32 37, i32 0
  ret i32 %c

end:
  ret i32 0
}

; LHS ==> RHS by definition (true -> true)
; CHECK-LABEL: @test6
; CHECK: taken:
; CHECK-NOT: select
; CHECK: call void @foo(i32 10)
define void @test6(i32 %a, i32 %b) {
  %cmp1 = icmp eq i32 %a, %b
  br i1 %cmp1, label %taken, label %end

taken:
  %c = select i1 %cmp1, i32 10, i32 0
  call void @foo(i32 %c)
  br label %end

end:
  ret void
}

; LHS ==> RHS by definition (false -> false)
; CHECK-LABEL: @test7
; CHECK: taken:
; CHECK-NOT: select
; CHECK: call void @foo(i32 11)
define void @test7(i32 %a, i32 %b) {
  %cmp1 = icmp eq i32 %a, %b
  br i1 %cmp1, label %end, label %taken

taken:
  %c = select i1 %cmp1, i32 0, i32 11
  call void @foo(i32 %c)
  br label %end

end:
  ret void
}