File: posion.ll

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (252 lines) | stat: -rw-r--r-- 9,593 bytes parent folder | download | duplicates (10)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes=guard-widening,dce < %s | FileCheck %s

declare void @llvm.experimental.guard(i1,...)
declare i1 @dummy()

; This tests shows the incorrect behavior of guard widening in terms of
; interaction with poison values.

; Let x incoming parameter is used for rane checks.
; Test generates 5 checks. One of them (c2) is used to get the corretness
; of nuw/nsw flags for x3 and x5. Others are used in guards and represent
; the checks x + 10 u< L, x + 15 u< L, x + 20 u< L and x + 3 u< L.
; The first two checks are in the first basic block and guard widening
; considers them as profitable to combine.
; When c4 and c3 are considered, number of check becomes more than two
; and combineRangeCheck consider them as profitable even if they are in
; different basic blocks.
; Accoding to algorithm of combineRangeCheck it detects that c3 and c4
; are enough to cover c1 and c5, so it ends up with guard of c3 && c4
; while both of them are poison at entry. This is a bug.

define void @combine_range_checks(i32 %x) {
; CHECK-LABEL: @combine_range_checks(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[X_GW_FR:%.*]] = freeze i32 [[X:%.*]]
; CHECK-NEXT:    [[X2:%.*]] = add i32 [[X_GW_FR]], 0
; CHECK-NEXT:    [[C2:%.*]] = icmp ult i32 [[X2]], 200
; CHECK-NEXT:    [[X3:%.*]] = add i32 [[X_GW_FR]], 3
; CHECK-NEXT:    [[C3:%.*]] = icmp ult i32 [[X3]], 100
; CHECK-NEXT:    [[X4:%.*]] = add i32 [[X_GW_FR]], 20
; CHECK-NEXT:    [[C4:%.*]] = icmp ult i32 [[X4]], 100
; CHECK-NEXT:    [[WIDE_CHK2:%.*]] = and i1 [[C4]], [[C3]]
; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK2]]) [ "deopt"(i64 1) ]
; CHECK-NEXT:    br i1 [[C2]], label [[OK:%.*]], label [[OUT:%.*]]
; CHECK:       ok:
; CHECK-NEXT:    br label [[OUT]]
; CHECK:       out:
; CHECK-NEXT:    ret void
;
entry:
  %x1 = add i32 %x, 10
  %c1 = icmp ult i32 %x1, 100
  %x2 = add i32 %x, 0
  %c2 = icmp ult i32 %x2, 200
  %x3 = add nuw nsw i32 %x, 3
  %c3 = icmp ult i32 %x3, 100
  %x4 = add nuw nsw i32 %x, 20
  %c4 = icmp ult i32 %x4, 100
  %x5 = add i32 %x, 15
  %c5 = icmp ult i32 %x5, 100
  call void(i1, ...) @llvm.experimental.guard(i1 %c1) [ "deopt"(i64 1) ]
  call void(i1, ...) @llvm.experimental.guard(i1 %c5) [ "deopt"(i64 5) ]
  br i1 %c2, label %ok, label %out
ok:
  call void(i1, ...) @llvm.experimental.guard(i1 %c4) [ "deopt"(i64 4) ]
  call void(i1, ...) @llvm.experimental.guard(i1 %c3) [ "deopt"(i64 3) ]
  br label %out
out:
  ret void
}

; This is similar to @combine_range_checks but shows that simple freeze
; over c3 and c4 will not help due to with X = SMAX_INT, guard with c1 will
; go to deoptimization. But after guard widening freeze of c3 and c4 may return
; true due to c3 and c4 are poisons and we pass guard executing side effect store
; which never been executed in original program.
define void @combine_range_checks_with_side_effect(i32 %x, ptr %p) {
; CHECK-LABEL: @combine_range_checks_with_side_effect(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[X_GW_FR:%.*]] = freeze i32 [[X:%.*]]
; CHECK-NEXT:    [[X2:%.*]] = add i32 [[X_GW_FR]], 0
; CHECK-NEXT:    [[C2:%.*]] = icmp ult i32 [[X2]], 200
; CHECK-NEXT:    [[X3:%.*]] = add i32 [[X_GW_FR]], 3
; CHECK-NEXT:    [[C3:%.*]] = icmp ult i32 [[X3]], 100
; CHECK-NEXT:    [[X4:%.*]] = add i32 [[X_GW_FR]], 20
; CHECK-NEXT:    [[C4:%.*]] = icmp ult i32 [[X4]], 100
; CHECK-NEXT:    [[WIDE_CHK2:%.*]] = and i1 [[C4]], [[C3]]
; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK2]]) [ "deopt"(i64 1) ]
; CHECK-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; CHECK-NEXT:    br i1 [[C2]], label [[OK:%.*]], label [[OUT:%.*]]
; CHECK:       ok:
; CHECK-NEXT:    br label [[OUT]]
; CHECK:       out:
; CHECK-NEXT:    ret void
;
entry:
  %x1 = add i32 %x, 10
  %c1 = icmp ult i32 %x1, 100
  %x2 = add i32 %x, 0
  %c2 = icmp ult i32 %x2, 200
  %x3 = add nuw nsw i32 %x, 3
  %c3 = icmp ult i32 %x3, 100
  %x4 = add nuw nsw i32 %x, 20
  %c4 = icmp ult i32 %x4, 100
  %x5 = add i32 %x, 15
  %c5 = icmp ult i32 %x5, 100
  call void(i1, ...) @llvm.experimental.guard(i1 %c1) [ "deopt"(i64 1) ]
  call void(i1, ...) @llvm.experimental.guard(i1 %c5) [ "deopt"(i64 5) ]
  store i32 0, ptr %p
  br i1 %c2, label %ok, label %out
ok:
  call void(i1, ...) @llvm.experimental.guard(i1 %c4) [ "deopt"(i64 4) ]
  call void(i1, ...) @llvm.experimental.guard(i1 %c3) [ "deopt"(i64 3) ]
  br label %out
out:
  ret void
}


; The test shows the bug in guard widening. Critical pieces.
; There is a %cond_1 check which provides the correctness of nuw nsw in %b.shift.
; %b.shift and %cond_2 are poisons and after guard widening it leads to UB
; for both arithmetic and logcal and.
define void @simple_case(i32 %a, i32 %b, i1 %cnd) {
; CHECK-LABEL: @simple_case(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[B_GW_FR:%.*]] = freeze i32 [[B:%.*]]
; CHECK-NEXT:    [[COND_0:%.*]] = icmp ult i32 [[A:%.*]], 10
; CHECK-NEXT:    [[B_SHIFT:%.*]] = add i32 [[B_GW_FR]], 5
; CHECK-NEXT:    [[COND_2:%.*]] = icmp ult i32 [[B_SHIFT]], 10
; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[COND_0]], [[COND_2]]
; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
; CHECK-NEXT:    br label [[LOOP:%.*]]
; CHECK:       loop:
; CHECK-NEXT:    [[COND_1:%.*]] = icmp ult i32 [[B_GW_FR]], 10
; CHECK-NEXT:    br i1 [[COND_1]], label [[OK:%.*]], label [[LEAVE_LOOPEXIT:%.*]]
; CHECK:       ok:
; CHECK-NEXT:    br i1 [[CND:%.*]], label [[LOOP]], label [[LEAVE_LOOPEXIT]]
; CHECK:       leave.loopexit:
; CHECK-NEXT:    br label [[LEAVE:%.*]]
; CHECK:       leave:
; CHECK-NEXT:    ret void
;
entry:
  %cond_0 = icmp ult i32 %a, 10
  %b.shift = add nuw nsw i32 %b, 5
  %cond_2 = icmp ult i32 %b.shift, 10
  call void (i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"() ]
  br label %loop

loop:
  %cond_1 = icmp ult i32 %b, 10
  br i1 %cond_1, label %ok, label %leave.loopexit
ok:
  call void (i1, ...) @llvm.experimental.guard(i1 %cond_2) [ "deopt"() ]
  br i1 %cnd, label %loop, label %leave.loopexit

leave.loopexit:
  br label %leave

leave:
  ret void
}

declare ptr @fake_personality_function()

define void @case_with_invoke(i1 %c, i1 %gc) personality ptr @fake_personality_function {
; CHECK-LABEL: @case_with_invoke(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    br i1 [[C:%.*]], label [[NORMAL:%.*]], label [[INVOK:%.*]]
; CHECK:       invok:
; CHECK-NEXT:    [[INVOKE_RESULT:%.*]] = invoke i1 @dummy()
; CHECK-NEXT:    to label [[NORMAL]] unwind label [[EXCEPTION:%.*]]
; CHECK:       normal:
; CHECK-NEXT:    [[PHI_C:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ [[INVOKE_RESULT]], [[INVOK]] ]
; CHECK-NEXT:    [[PHI_C_GW_FR:%.*]] = freeze i1 [[PHI_C]]
; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[GC:%.*]], [[PHI_C_GW_FR]]
; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
; CHECK-NEXT:    ret void
; CHECK:       exception:
; CHECK-NEXT:    [[LANDING_PAD:%.*]] = landingpad { ptr, i32 }
; CHECK-NEXT:    cleanup
; CHECK-NEXT:    ret void
;
entry:
  br i1 %c, label %normal, label %invok

invok:
  %invoke.result = invoke i1 @dummy() to label %normal unwind label %exception

normal:
  %phi.c = phi i1 [true, %entry], [%invoke.result, %invok]
  call void (i1, ...) @llvm.experimental.guard(i1 %gc) [ "deopt"() ]
  call void (i1, ...) @llvm.experimental.guard(i1 %phi.c) [ "deopt"() ]
  ret void

exception:
  %landing_pad = landingpad { ptr, i32 } cleanup
  ret void
}

define void @case_with_invoke_in_latch(i1 %c, i1 %gc) personality ptr @fake_personality_function {
; CHECK-LABEL: @case_with_invoke_in_latch(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    br label [[HEADER:%.*]]
; CHECK:       header:
; CHECK-NEXT:    [[PHI_C:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ [[INVOKE_RESULT:%.*]], [[HEADER]] ]
; CHECK-NEXT:    [[PHI_C_GW_FR:%.*]] = freeze i1 [[PHI_C]]
; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[GC:%.*]], [[PHI_C_GW_FR]]
; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
; CHECK-NEXT:    [[INVOKE_RESULT]] = invoke i1 @dummy()
; CHECK-NEXT:    to label [[HEADER]] unwind label [[EXCEPTION:%.*]]
; CHECK:       exception:
; CHECK-NEXT:    [[LANDING_PAD:%.*]] = landingpad { ptr, i32 }
; CHECK-NEXT:    cleanup
; CHECK-NEXT:    ret void
;
entry:
  br label %header

header:
  %phi.c = phi i1 [false, %entry], [%invoke.result, %header]
  call void (i1, ...) @llvm.experimental.guard(i1 %gc) [ "deopt"() ]
  call void (i1, ...) @llvm.experimental.guard(i1 %phi.c) [ "deopt"() ]
  %invoke.result = invoke i1 @dummy() to label %header unwind label %exception

exception:
  %landing_pad = landingpad { ptr, i32 } cleanup
  ret void
}

declare void @dummy_vec(<4 x i1> %arg)

define void @freeze_poison(i1 %c, i1 %g) {
; CHECK-LABEL: @freeze_poison(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[DOTGW_FR:%.*]] = freeze i1 poison
; CHECK-NEXT:    br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
; CHECK:       left:
; CHECK-NEXT:    call void @dummy_vec(<4 x i1> <i1 false, i1 poison, i1 poison, i1 poison>)
; CHECK-NEXT:    ret void
; CHECK:       right:
; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[G:%.*]], [[DOTGW_FR]]
; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
; CHECK-NEXT:    ret void
;
entry:
  br i1 %c, label %left, label %right

left:
  call void @dummy_vec(<4 x i1> <i1 0, i1 poison, i1 poison, i1 poison>)
  ret void


right:
  call void (i1, ...) @llvm.experimental.guard(i1 %g) [ "deopt"() ]
  call void (i1, ...) @llvm.experimental.guard(i1 poison) [ "deopt"() ]
  ret void

}