Skip to content

Commit 3dc1907

Browse files
committed
[ConstantFold] Use ConstantFoldLoadFromUniformValue() in more places
In particular, this also preserves undef when loading from padding, rather than converting it to zero through a different codepath. This is the remaining part of D115924.
1 parent 4e62d21 commit 3dc1907

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

clang/test/CodeGen/aapcs-align.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ void g6() {
134134
f6m(1, 2, 3, 4, 5, s);
135135
}
136136
// CHECK: define{{.*}} void @g6
137-
// CHECK: call void @f6(i32 1, [4 x i32] [i32 6, i32 7, i32 0, i32 0])
138-
// CHECK: call void @f6m(i32 1, i32 2, i32 3, i32 4, i32 5, [4 x i32] [i32 6, i32 7, i32 0, i32 0])
137+
// CHECK: call void @f6(i32 1, [4 x i32] [i32 6, i32 7, i32 0, i32 undef])
138+
// CHECK: call void @f6m(i32 1, i32 2, i32 3, i32 4, i32 5, [4 x i32] [i32 6, i32 7, i32 0, i32 undef])
139139
// CHECK: declare void @f6(i32, [4 x i32])
140140
// CHECK: declare void @f6m(i32, i32, i32, i32, i32, [4 x i32])
141141
}

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,8 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
106106
"Invalid constantexpr bitcast!");
107107

108108
// Catch the obvious splat cases.
109-
if (C->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy())
110-
return Constant::getNullValue(DestTy);
111-
if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy() &&
112-
!DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
113-
return Constant::getAllOnesValue(DestTy);
109+
if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy))
110+
return Res;
114111

115112
if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
116113
// Handle a vector->scalar integer/fp cast.
@@ -362,16 +359,8 @@ Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
362359

363360
// Catch the obvious splat cases (since all-zeros can coerce non-integral
364361
// pointers legally).
365-
if (C->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy())
366-
return Constant::getNullValue(DestTy);
367-
if (C->isAllOnesValue() &&
368-
(DestTy->isIntegerTy() || DestTy->isFloatingPointTy() ||
369-
DestTy->isVectorTy()) &&
370-
!DestTy->isX86_AMXTy() && !DestTy->isX86_MMXTy() &&
371-
!DestTy->isPtrOrPtrVectorTy())
372-
// Get ones when the input is trivial, but
373-
// only for supported types inside getAllOnesValue.
374-
return Constant::getAllOnesValue(DestTy);
362+
if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy))
363+
return Res;
375364

376365
// If the type sizes are the same and a cast is legal, just directly
377366
// cast the constant.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ define i8 @load_neg_one_at_unknown_offset() {
320320

321321
define i32 @load_padding() {
322322
; CHECK-LABEL: @load_padding(
323-
; CHECK-NEXT: ret i32 0
323+
; CHECK-NEXT: ret i32 undef
324324
;
325325
%v = load i32, i32* getelementptr (i32, i32* bitcast ({ i32, [4 x i8] }* @g_with_padding to i32*), i64 1)
326326
ret i32 %v

0 commit comments

Comments
 (0)