Skip to content

Commit 13321cc

Browse files
committed
[GlobalOpt] Look through constant expressions.
Allow looking through constant expressions. Constant expressions cannot read, modify or leak the global themselves. I might be missing something, but using analyzeGlobalAux should ensure all (instruction) users that may read, modify or leak the global are checked. This fixes another regression exposed by llvm#123518.
1 parent 32126a3 commit 13321cc

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

llvm/lib/Transforms/Utils/GlobalStatus.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
7878
return true;
7979
} else {
8080
// Ignore dead constant users.
81-
if (!isSafeToDestroyConstant(C))
82-
return true;
81+
if (!isSafeToDestroyConstant(C)) {
82+
if (CE) {
83+
if (VisitedUsers.insert(CE).second)
84+
if (analyzeGlobalAux(CE, GS, VisitedUsers))
85+
return true;
86+
} else
87+
return true;
88+
}
8389
}
8490
} else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
8591
if (!GS.HasMultipleAccessingFunctions) {
@@ -166,9 +172,12 @@ static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
166172
if (MTI->getArgOperand(1) == V)
167173
GS.IsLoaded = true;
168174
} else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
169-
assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
175+
if (MSI->getArgOperand(0) != V || MSI->getArgOperand(1) == V)
176+
return true;
170177
if (MSI->isVolatile())
171178
return true;
179+
assert(MSI->getArgOperand(0) == V &&
180+
"First argument must be the pointer!");
172181
GS.StoredType = GlobalStatus::Stored;
173182
} else if (const auto *CB = dyn_cast<CallBase>(I)) {
174183
if (CB->getIntrinsicID() == Intrinsic::threadlocal_address) {

llvm/test/Transforms/GlobalOpt/cleanup-pointer-root-users-ptrtoint-add-constexpr.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ declare i32 @fn1()
1212
define void @stores_single_use_gep_constexpr() {
1313
; CHECK-LABEL: @stores_single_use_gep_constexpr(
1414
; CHECK-NEXT: entry:
15-
; CHECK-NEXT: store ptr @fn0, ptr @global.20ptr, align 8
16-
; CHECK-NEXT: store ptr @fn1, ptr getelementptr inbounds ([[STRUCT_GLOBAL_20PTR:%.*]], ptr @global.20ptr, i64 0, i32 1), align 8
1715
; CHECK-NEXT: ret void
1816
;
1917
entry:

llvm/test/Transforms/GlobalOpt/read-with-constexpr-users.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
@H = internal global [2 x i64 ] zeroinitializer
66

77
;.
8-
; CHECK: @G = internal global [2 x i64] zeroinitializer
8+
; CHECK: @G = internal constant [2 x i64] zeroinitializer
99
; CHECK: @H = internal global [2 x i64] zeroinitializer
1010
;.
1111
define i64 @G_used_by_gep_inttoptr_exprs() {
1212
; CHECK-LABEL: define i64 @G_used_by_gep_inttoptr_exprs() local_unnamed_addr {
13-
; CHECK-NEXT: [[L:%.*]] = load i64, ptr @G, align 8
1413
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr inttoptr (i64 add (i64 ptrtoint (ptr getelementptr inbounds nuw (i8, ptr @G, i64 16) to i64), i64 8) to ptr), i64 1
1514
; CHECK-NEXT: [[C:%.*]] = icmp eq ptr [[GEP]], @G
16-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C]], i64 [[L]], i64 9
15+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C]], i64 0, i64 9
1716
; CHECK-NEXT: ret i64 [[SEL]]
1817
;
1918
%l = load i64, ptr @G, align 8

0 commit comments

Comments
 (0)