[llvm] 874c032 - [Attributor] Use ConstantFoldLoadFromConst()
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 10 01:19:57 PDT 2022
Author: Nikita Popov
Date: 2022-10-10T10:17:37+02:00
New Revision: 874c0327e7e90d42198fd3aba5e3d636e0ba87c3
URL: https://github.com/llvm/llvm-project/commit/874c0327e7e90d42198fd3aba5e3d636e0ba87c3
DIFF: https://github.com/llvm/llvm-project/commit/874c0327e7e90d42198fd3aba5e3d636e0ba87c3.diff
LOG: [Attributor] Use ConstantFoldLoadFromConst()
When determining the initial value of the object, use the constant
folding API to load a given type at a given offset in the global
initializer. This makes it work for cases where the load doesn't
directly correspond to an aggregate member.
Differential Revision: https://reviews.llvm.org/D135435
Added:
Modified:
llvm/lib/Transforms/IPO/Attributor.cpp
llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp
index 4e6a13913c9d..a48f956f943f 100644
--- a/llvm/lib/Transforms/IPO/Attributor.cpp
+++ b/llvm/lib/Transforms/IPO/Attributor.cpp
@@ -237,24 +237,12 @@ Constant *AA::getInitialValueForObj(Value &Obj, Type &Ty,
if (!GV->hasInitializer())
return UndefValue::get(&Ty);
- // Handle constant initializers by extracting the relevant parts for
- // aggregates.
- Constant *C = GV->getInitializer();
- if (OASPtr && !OASPtr->offsetOrSizeAreUnknown() &&
- isa<ConstantAggregate>(C)) {
- Type *CTy = C->getType();
+ if (OASPtr && !OASPtr->offsetOrSizeAreUnknown()) {
APInt Offset = APInt(64, OASPtr->getOffset());
- Optional<APInt> Idx = DL.getGEPIndexForOffset(CTy, Offset);
- // Check if the indexing worked out properly.
- // TODO: Handle partial accesses, e.g., Offset is > 0 or Size < CTy.size().
- if (Idx && Offset.isZero() &&
- DL.getTypeSizeInBits(CTy) == uint64_t(OASPtr->getSize() * 8)) {
- if (auto *Folded =
- ConstantFoldExtractValueInstruction(C, Idx->getZExtValue()))
- C = Folded;
- }
+ return ConstantFoldLoadFromConst(GV->getInitializer(), &Ty, Offset, DL);
}
- return dyn_cast_or_null<Constant>(getWithType(*C, Ty));
+
+ return ConstantFoldLoadFromUniformValue(GV->getInitializer(), &Ty);
}
bool AA::isValidInScope(const Value &V, const Function *Scope) {
diff --git a/llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll b/llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll
index a8139321c401..28940d7328f3 100644
--- a/llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll
+++ b/llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll
@@ -99,29 +99,20 @@ define i32 @testOneFieldGlobalS_type_mismatch() {
; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS_type_mismatch
; CHECK-SAME: () #[[ATTR2:[0-9]+]] {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[I:%.*]] = load double, ptr @GlobalS, align 8
-; CHECK-NEXT: [[IC:%.*]] = fptosi double [[I]] to i32
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[IC]], 42
-; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
+; CHECK-NEXT: br label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 0, 1
-; CHECK-NEXT: br label [[IF_END]]
+; CHECK-NEXT: br label [[IF_END:%.*]]
; CHECK: if.end:
-; CHECK-NEXT: [[R_0:%.*]] = phi i32 [ [[ADD]], [[IF_THEN]] ], [ 0, [[ENTRY:%.*]] ]
-; CHECK-NEXT: [[I1:%.*]] = load i64, ptr getelementptr inbounds ([[STRUCT_S:%.*]], ptr @GlobalS, i32 0, i32 1), align 8
-; CHECK-NEXT: [[I1C:%.*]] = sitofp i64 [[I1]] to double
-; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[I1C]], 3.140000e+00
-; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN2:%.*]], label [[IF_END4:%.*]]
+; CHECK-NEXT: br label [[IF_END4:%.*]]
; CHECK: if.then2:
-; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[R_0]], 2
-; CHECK-NEXT: br label [[IF_END4]]
+; CHECK-NEXT: unreachable
; CHECK: if.end4:
-; CHECK-NEXT: [[R_1:%.*]] = phi i32 [ [[ADD3]], [[IF_THEN2]] ], [ [[R_0]], [[IF_END]] ]
; CHECK-NEXT: br label [[IF_END7:%.*]]
; CHECK: if.then5:
; CHECK-NEXT: unreachable
; CHECK: if.end7:
-; CHECK-NEXT: ret i32 [[R_1]]
+; CHECK-NEXT: ret i32 1
;
entry:
%i = load double, ptr @GlobalS, align 8
@@ -165,31 +156,25 @@ define i32 @testOneFieldGlobalS_byte_offset_wrong() {
; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS_byte_offset_wrong
; CHECK-SAME: () #[[ATTR2]] {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[I:%.*]] = load i32, ptr getelementptr inbounds (i32, ptr @GlobalS, i32 1), align 8
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[I]], 42
-; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
+; CHECK-NEXT: br label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 0, 1
-; CHECK-NEXT: br label [[IF_END]]
+; CHECK-NEXT: br label [[IF_END:%.*]]
; CHECK: if.end:
-; CHECK-NEXT: [[R_0:%.*]] = phi i32 [ [[ADD]], [[IF_THEN]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[I1:%.*]] = load double, ptr getelementptr (double, ptr @GlobalS, i32 3), align 8
; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[I1]], 3.140000e+00
; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN2:%.*]], label [[IF_END4:%.*]]
; CHECK: if.then2:
-; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[R_0]], 2
+; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 1, 2
; CHECK-NEXT: br label [[IF_END4]]
; CHECK: if.end4:
-; CHECK-NEXT: [[R_1:%.*]] = phi i32 [ [[ADD3]], [[IF_THEN2]] ], [ [[R_0]], [[IF_END]] ]
-; CHECK-NEXT: [[I2:%.*]] = load ptr, ptr getelementptr (ptr, ptr @GlobalS, i32 11), align 8
-; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne ptr [[I2]], null
-; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN5:%.*]], label [[IF_END7:%.*]]
+; CHECK-NEXT: [[R_1:%.*]] = phi i32 [ [[ADD3]], [[IF_THEN2]] ], [ 1, [[IF_END]] ]
+; CHECK-NEXT: br label [[IF_THEN5:%.*]]
; CHECK: if.then5:
; CHECK-NEXT: [[ADD6:%.*]] = add nsw i32 [[R_1]], 4
-; CHECK-NEXT: br label [[IF_END7]]
+; CHECK-NEXT: br label [[IF_END7:%.*]]
; CHECK: if.end7:
-; CHECK-NEXT: [[R_2:%.*]] = phi i32 [ [[ADD6]], [[IF_THEN5]] ], [ [[R_1]], [[IF_END4]] ]
-; CHECK-NEXT: ret i32 [[R_2]]
+; CHECK-NEXT: ret i32 [[ADD6]]
;
entry:
%i = load i32, ptr getelementptr (i32, ptr @GlobalS, i32 1), align 8
More information about the llvm-commits
mailing list