Skip to content

Commit cb8adf7

Browse files
committed
[Attributor] Simplify loads from constant globals
If a global is constant and the initializer is known we can simplify loads from it as the value has to be the initializer.
1 parent 39c4ac1 commit cb8adf7

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ Constant *AA::getInitialValueForObj(Value &Obj, Type &Ty,
225225
if (isAllocationFn(&Obj, TLI))
226226
return getInitialValueOfAllocation(&cast<CallBase>(Obj), TLI, &Ty);
227227
auto *GV = dyn_cast<GlobalVariable>(&Obj);
228-
if (!GV || !GV->hasLocalLinkage())
228+
if (!GV)
229+
return nullptr;
230+
if (!GV->hasLocalLinkage() && !(GV->isConstant() && GV->hasInitializer()))
229231
return nullptr;
230232
if (!GV->hasInitializer())
231233
return UndefValue::get(&Ty);
@@ -365,7 +367,8 @@ static bool getPotentialCopiesOfMemoryValue(
365367
return false;
366368
}
367369
if (auto *GV = dyn_cast<GlobalVariable>(Obj))
368-
if (!GV->hasLocalLinkage()) {
370+
if (!GV->hasLocalLinkage() &&
371+
!(GV->isConstant() && GV->hasInitializer())) {
369372
LLVM_DEBUG(dbgs() << "Underlying object is global with external "
370373
"linkage, not supported yet: "
371374
<< *Obj << "\n";);
@@ -1022,6 +1025,8 @@ Optional<Constant *>
10221025
Attributor::getAssumedConstant(const IRPosition &IRP,
10231026
const AbstractAttribute &AA,
10241027
bool &UsedAssumedInformation) {
1028+
if (auto *C = dyn_cast<Constant>(&IRP.getAssociatedValue()))
1029+
return C;
10251030
// First check all callbacks provided by outside AAs. If any of them returns
10261031
// a non-null value that is different from the associated value, or None, we
10271032
// assume it's simpliied.

llvm/test/Transforms/Attributor/ArgumentPromotion/chained.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
2-
; RUN: opt -attributor -enable-new-pm=0 -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=2 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
3-
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=2 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
2+
; RUN: opt -attributor -enable-new-pm=0 -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=4 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
3+
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=4 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
44
; RUN: opt -attributor-cgscc -enable-new-pm=0 -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM
55
; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM
66

llvm/test/Transforms/OpenMP/barrier_removal.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ define void @neg_empty_2() {
9797
define void @pos_constant_loads() {
9898
; CHECK-LABEL: define {{[^@]+}}@pos_constant_loads() {
9999
; CHECK-NEXT: [[ARG:%.*]] = load i32 addrspace(4)*, i32 addrspace(4)** addrspacecast (i32 addrspace(4)* addrspace(4)* @GPtr4 to i32 addrspace(4)**), align 8
100-
; CHECK-NEXT: [[A:%.*]] = load i32, i32* @GC1, align 4
101100
; CHECK-NEXT: [[B:%.*]] = load i32, i32* addrspacecast (i32 addrspace(4)* @GC2 to i32*), align 4
102101
; CHECK-NEXT: [[ARGC:%.*]] = addrspacecast i32 addrspace(4)* [[ARG]] to i32*
103102
; CHECK-NEXT: [[C:%.*]] = load i32, i32* [[ARGC]], align 4
104103
; CHECK-NEXT: call void @aligned_barrier()
105-
; CHECK-NEXT: [[D:%.*]] = add i32 [[A]], [[B]]
104+
; CHECK-NEXT: [[D:%.*]] = add i32 42, [[B]]
106105
; CHECK-NEXT: [[E:%.*]] = add i32 [[D]], [[C]]
107106
; CHECK-NEXT: call void @useI32(i32 [[E]])
108107
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)