Skip to content

Commit 1881711

Browse files
committed
[InstCombine] Remove memset of undef value
This removes memset with undef char. We already do this for stores of undef value. This comes with the caveat that this optimization is not, strictly speaking, legal for undef values, because we might be overwriting a poison value. However, our entire load/store model currently still operates on undef values, so we need to support undef here as well for internal consistency. Once #52930 is resolved, these and related folds can be limited to poison -- I've added FIXMEs to that effect. Differential Revision: https://reviews.llvm.org/D124173
1 parent 24a133e commit 1881711

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
258258
return MI;
259259
}
260260

261+
// Remove memset with an undef value.
262+
// FIXME: This is technically incorrect because it might overwrite a poison
263+
// value. Change to PoisonValue once #52930 is resolved.
264+
if (isa<UndefValue>(MI->getValue())) {
265+
// Set the size of the copy to 0, it will be deleted on the next iteration.
266+
MI->setLength(Constant::getNullValue(MI->getLength()->getType()));
267+
return MI;
268+
}
269+
261270
// Extract the length and alignment and fill if they are constant.
262271
ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
263272
ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,8 @@ Instruction *InstCombinerImpl::visitStoreInst(StoreInst &SI) {
14351435
}
14361436

14371437
// store undef, Ptr -> noop
1438+
// FIXME: This is technically incorrect because it might overwrite a poison
1439+
// value. Change to PoisonValue once #52930 is resolved.
14381440
if (isa<UndefValue>(Val))
14391441
return eraseInstFromFunction(SI);
14401442

llvm/test/Transforms/InstCombine/memset.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ define void @memset_to_constant() {
3333
ret void
3434
}
3535

36+
; FIXME: This is technically incorrect because it might overwrite a poison
37+
; value. Stop folding it once #52930 is resolved.
3638
define void @memset_undef(i8* %p) {
3739
; CHECK-LABEL: @memset_undef(
38-
; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(8) [[P:%.*]], i8 undef, i32 8, i1 false)
3940
; CHECK-NEXT: ret void
4041
;
4142
call void @llvm.memset.p0i8.i32(i8* %p, i8 undef, i32 8, i1 false)
@@ -53,7 +54,6 @@ define void @memset_undef_volatile(i8* %p) {
5354

5455
define void @memset_poison(i8* %p) {
5556
; CHECK-LABEL: @memset_poison(
56-
; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(8) [[P:%.*]], i8 poison, i32 8, i1 false)
5757
; CHECK-NEXT: ret void
5858
;
5959
call void @llvm.memset.p0i8.i32(i8* %p, i8 poison, i32 8, i1 false)

llvm/test/Transforms/InstCombine/store.ll

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33

4+
; FIXME: This is technically incorrect because it might overwrite a poison
5+
; value. Stop folding it once #52930 is resolved.
46
define void @store_of_undef(i32* %P) {
57
; CHECK-LABEL: @store_of_undef(
68
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)