Skip to content

Commit 05c75e6

Browse files
committed
!fixup check underlying obj
1 parent fe37d44 commit 05c75e6

File tree

2 files changed

+16
-44
lines changed

2 files changed

+16
-44
lines changed

llvm/lib/Transforms/Utils/GlobalStatus.cpp

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/Transforms/Utils/GlobalStatus.h"
1010
#include "llvm/ADT/SmallPtrSet.h"
11+
#include "llvm/Analysis/ValueTracking.h"
1112
#include "llvm/IR/BasicBlock.h"
1213
#include "llvm/IR/Constant.h"
1314
#include "llvm/IR/Constants.h"
@@ -17,6 +18,7 @@
1718
#include "llvm/IR/Instruction.h"
1819
#include "llvm/IR/Instructions.h"
1920
#include "llvm/IR/IntrinsicInst.h"
21+
#include "llvm/IR/PatternMatch.h"
2022
#include "llvm/IR/Use.h"
2123
#include "llvm/IR/User.h"
2224
#include "llvm/IR/Value.h"
@@ -26,6 +28,7 @@
2628
#include <cassert>
2729

2830
using namespace llvm;
31+
using namespace llvm::PatternMatch;
2932

3033
/// Return the stronger of the two ordering. If the two orderings are acquire
3134
/// and release, then return AcquireRelease.
@@ -61,45 +64,6 @@ bool llvm::isSafeToDestroyConstant(const Constant *C) {
6164
return true;
6265
}
6366

64-
static bool
65-
maybePointerToDifferentObjectRecursive(Value *V,
66-
SmallPtrSetImpl<Value *> &Visited) {
67-
if (!Visited.insert(V).second)
68-
return false;
69-
70-
if (Visited.size() > 32)
71-
return true;
72-
73-
// PtrToInt may be used to construct a pointer to a different object. Loads
74-
// and calls may return a pointer for a different object.
75-
if (isa<PtrToIntInst, LoadInst, CallInst>(V))
76-
return true;
77-
78-
if (auto *CE = dyn_cast<ConstantExpr>(V)) {
79-
if (CE->getOpcode() == Instruction::PtrToInt)
80-
return true;
81-
82-
for (auto &Op : CE->operands()) {
83-
if (maybePointerToDifferentObjectRecursive(Op.get(), Visited))
84-
return true;
85-
}
86-
return false;
87-
}
88-
89-
if (auto *U = dyn_cast<User>(V)) {
90-
for (auto &Op : U->operands()) {
91-
if (maybePointerToDifferentObjectRecursive(Op.get(), Visited))
92-
return true;
93-
}
94-
}
95-
return false;
96-
}
97-
98-
bool maybePointerToDifferentObject(Value *V) {
99-
SmallPtrSet<Value *, 32> Visited;
100-
return maybePointerToDifferentObjectRecursive(V, Visited);
101-
}
102-
10367
static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
10468
SmallPtrSetImpl<const Value *> &VisitedUsers) {
10569
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
@@ -198,10 +162,15 @@ static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
198162
} else if (isa<CmpInst>(I)) {
199163
GS.IsCompared = true;
200164

201-
if (VisitedUsers.insert(I).second) {
202-
for (Value *Op : I->operands())
203-
if (Op != V && maybePointerToDifferentObject(Op))
204-
return true;
165+
const Value *UO = nullptr;
166+
for (Value *Op : I->operands()) {
167+
if (match(Op, m_Zero()))
168+
continue;
169+
const Value *OpUO = getUnderlyingObject(Op);
170+
if (!UO)
171+
UO = OpUO;
172+
if (!OpUO || UO != OpUO)
173+
return true;
205174
}
206175
} else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
207176
if (MTI->isVolatile())

llvm/test/Transforms/GlobalOpt/globals-compares-with-ptrtoint.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
; CHECK: @C = internal global i64 0
2222
; CHECK: @D = internal global i64 0
2323
; CHECK: @G = internal global i64 0
24+
; CHECK: @H = internal global i64 0
25+
; CHECK: @J = internal global [2 x ptr] zeroinitializer
2426
;.
2527
define i64 @A_and_B_cmp_ptrtoint_constant_expr() {
2628
; CHECK-LABEL: define i64 @A_and_B_cmp_ptrtoint_constant_expr() local_unnamed_addr {
@@ -153,7 +155,8 @@ define ptr @compare_arg(ptr %a) {
153155
; CHECK-NEXT: [[C:%.*]] = icmp eq ptr [[A]], @J
154156
; CHECK-NEXT: br i1 [[C]], label %[[THEN:.*]], label %[[ELSE:.*]]
155157
; CHECK: [[THEN]]:
156-
; CHECK-NEXT: ret ptr null
158+
; CHECK-NEXT: [[L:%.*]] = load ptr, ptr @J, align 8
159+
; CHECK-NEXT: ret ptr [[L]]
157160
; CHECK: [[ELSE]]:
158161
; CHECK-NEXT: ret ptr null
159162
;

0 commit comments

Comments
 (0)