Skip to content

Commit d4b4747

Browse files
committed
ConstantFolding: fold OOB accesses to poison instead of undef
1 parent f95a6ae commit d4b4747

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ Constant *FoldReinterpretLoadFromConst(Constant *C, Type *LoadTy,
591591

592592
// If we're not accessing anything in this constant, the result is undefined.
593593
if (Offset <= -1 * static_cast<int64_t>(BytesLoaded))
594-
return UndefValue::get(IntType);
594+
return PoisonValue::get(IntType);
595595

596596
// TODO: We should be able to support scalable types.
597597
TypeSize InitializerSize = DL.getTypeAllocSize(C->getType());
@@ -600,7 +600,7 @@ Constant *FoldReinterpretLoadFromConst(Constant *C, Type *LoadTy,
600600

601601
// If we're not accessing anything in this constant, the result is undefined.
602602
if (Offset >= (int64_t)InitializerSize.getFixedValue())
603-
return UndefValue::get(IntType);
603+
return PoisonValue::get(IntType);
604604

605605
unsigned char RawBytes[32] = {0};
606606
unsigned char *CurPtr = RawBytes;
@@ -702,11 +702,11 @@ Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty,
702702
if (Constant *Result = ConstantFoldLoadThroughBitcast(AtOffset, Ty, DL))
703703
return Result;
704704

705-
// Explicitly check for out-of-bounds access, so we return undef even if the
705+
// Explicitly check for out-of-bounds access, so we return poison even if the
706706
// constant is a uniform value.
707707
TypeSize Size = DL.getTypeAllocSize(C->getType());
708708
if (!Size.isScalable() && Offset.sge(Size.getFixedSize()))
709-
return UndefValue::get(Ty);
709+
return PoisonValue::get(Ty);
710710

711711
// Try an offset-independent fold of a uniform value.
712712
if (Constant *Result = ConstantFoldLoadFromUniformValue(C, Ty))

llvm/test/Transforms/InstCombine/strcall-no-nul.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ define void @fold_strncmp_past_end(i32* %pcmp) {
105105
}
106106

107107

108-
; Fold strrchr(a5 + 5, '\0') to null.
108+
; Fold strrchr(a5 + 5, '\0') to poison (it's UB).
109109

110110
define i8* @fold_strrchr_past_end(i32 %c) {
111111
; CHECK-LABEL: @fold_strrchr_past_end(
112-
; CHECK-NEXT: ret i8* null
112+
; CHECK-NEXT: ret i8* poison
113113
;
114114
%p5 = getelementptr [5 x i8], [5 x i8]* @a5, i32 0, i32 5
115115
%r = call i8* @strrchr(i8* %p5, i32 0)

llvm/test/Transforms/InstSimplify/ConstProp/loads.ll

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ define i64 @test16.3() {
267267
ret i64 %v
268268
}
269269

270-
@g7 = constant {[0 x i32], [0 x i8], ptr} { [0 x i32] undef, [0 x i8] undef, ptr null }
270+
@g7 = constant {[0 x i32], [0 x i8], ptr} { [0 x i32] poison, [0 x i8] poison, ptr null }
271271

272272
define ptr @test_leading_zero_size_elems() {
273273
; CHECK-LABEL: @test_leading_zero_size_elems(
@@ -277,7 +277,7 @@ define ptr @test_leading_zero_size_elems() {
277277
ret ptr %v
278278
}
279279

280-
@g8 = constant {[4294967295 x [0 x i32]], i64} { [4294967295 x [0 x i32]] undef, i64 123 }
280+
@g8 = constant {[4294967295 x [0 x i32]], i64} { [4294967295 x [0 x i32]] poison, i64 123 }
281281

282282
define i64 @test_leading_zero_size_elems_big() {
283283
; CHECK-LABEL: @test_leading_zero_size_elems_big(
@@ -291,7 +291,7 @@ define i64 @test_leading_zero_size_elems_big() {
291291

292292
define i64 @test_array_of_zero_size_array() {
293293
; CHECK-LABEL: @test_array_of_zero_size_array(
294-
; CHECK-NEXT: ret i64 undef
294+
; CHECK-NEXT: ret i64 poison
295295
;
296296
%v = load i64, ptr @g9
297297
ret i64 %v
@@ -317,7 +317,7 @@ define ptr @test_poison_aggregate() {
317317
ret ptr %v
318318
}
319319

320-
@g11 = constant <{ [8 x i8], [8 x i8] }> <{ [8 x i8] undef, [8 x i8] zeroinitializer }>, align 4
320+
@g11 = constant <{ [8 x i8], [8 x i8] }> <{ [8 x i8] poison, [8 x i8] zeroinitializer }>, align 4
321321

322322
define ptr @test_trailing_zero_gep_index() {
323323
; CHECK-LABEL: @test_trailing_zero_gep_index(
@@ -360,24 +360,24 @@ define i8 @load_neg_one_at_unknown_offset() {
360360
ret i8 %v
361361
}
362362

363-
@g_with_padding = constant { i32, [4 x i8] } { i32 0, [4 x i8] undef }
363+
@g_with_padding = constant { i32, [4 x i8] } { i32 0, [4 x i8] poison }
364364

365365
define i32 @load_padding() {
366366
; CHECK-LABEL: @load_padding(
367-
; CHECK-NEXT: ret i32 undef
367+
; CHECK-NEXT: ret i32 poison
368368
;
369369
%v = load i32, ptr getelementptr (i32, ptr @g_with_padding, i64 1)
370370
ret i32 %v
371371
}
372372

373-
@g_all_undef = constant { i32, [4 x i8] } undef
373+
@g_all_poison = constant { i32, [4 x i8] } poison
374374

375-
; Same as the previous case, but with an all-undef initializer.
376-
define i32 @load_all_undef() {
377-
; CHECK-LABEL: @load_all_undef(
378-
; CHECK-NEXT: ret i32 undef
375+
; Same as the previous case, but with an all-poison initializer.
376+
define i32 @load_all_poison() {
377+
; CHECK-LABEL: @load_all_poison(
378+
; CHECK-NEXT: ret i32 poison
379379
;
380-
%v = load i32, ptr getelementptr (i32, ptr @g_all_undef, i64 1)
380+
%v = load i32, ptr getelementptr (i32, ptr @g_all_poison, i64 1)
381381
ret i32 %v
382382
}
383383

llvm/test/Transforms/InstSimplify/load.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
33

44
@zeroinit = constant {} zeroinitializer
5-
@undef = constant {} undef
5+
@poison = constant {} poison
66

77
define i32 @crash_on_zeroinit() {
88
; CHECK-LABEL: @crash_on_zeroinit(
9-
; CHECK-NEXT: ret i32 undef
9+
; CHECK-NEXT: ret i32 poison
1010
;
1111
%load = load i32, ptr @zeroinit
1212
ret i32 %load
1313
}
1414

15-
define i32 @crash_on_undef() {
16-
; CHECK-LABEL: @crash_on_undef(
17-
; CHECK-NEXT: ret i32 undef
15+
define i32 @crash_on_poison() {
16+
; CHECK-LABEL: @crash_on_poison(
17+
; CHECK-NEXT: ret i32 poison
1818
;
19-
%load = load i32, ptr @undef
19+
%load = load i32, ptr @poison
2020
ret i32 %load
2121
}
2222

@@ -35,7 +35,7 @@ define <8 x i32> @partial_load() {
3535
; This does an out of bounds load from the global constant
3636
define <3 x float> @load_vec3() {
3737
; CHECK-LABEL: @load_vec3(
38-
; CHECK-NEXT: ret <3 x float> undef
38+
; CHECK-NEXT: ret <3 x float> poison
3939
;
4040
%1 = load <3 x float>, ptr getelementptr inbounds (<3 x float>, ptr @constvec, i64 1)
4141
ret <3 x float> %1

0 commit comments

Comments
 (0)