Skip to content

Commit c5b7262

Browse files
author
Eli Friedman
committed
Fix regression from my recent GlobalsAA fix.
There are two fixes here: one, AnalyzeUsesOfPointer can't return false until it has checked all the uses of the pointer. Two, if a global uses another global, we have to assume the address of the first global escapes. Fixes https://llvm.org/bugs/show_bug.cgi?id=30707 . Differential Revision: https://reviews.llvm.org/D25798 llvm-svn: 285034
1 parent e3e6585 commit c5b7262

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

llvm/lib/Analysis/GlobalsModRef.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V,
367367
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
368368
return true; // Allow comparison against null.
369369
} else if (Constant *C = dyn_cast<Constant>(I)) {
370-
return C->isConstantUsed();
370+
// Ignore constants which don't have any live uses.
371+
if (isa<GlobalValue>(C) || C->isConstantUsed())
372+
return true;
371373
} else {
372374
return true;
373375
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; RUN: opt < %s -globals-aa -gvn -S | FileCheck %s
2+
3+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4+
5+
@a = internal global i32* null, align 8
6+
@b = global i32** @a, align 8
7+
@c = global i32** @a, align 8
8+
@d = common global i32 0, align 4
9+
10+
; Make sure we globals-aa doesn't get confused and allow hoisting
11+
; the load from @a out of the loop.
12+
13+
; CHECK-LABEL: define i32 @main()
14+
; CHECK: for.body:
15+
; CHECK-NEXT: %2 = load i32**, i32*** @b, align 8
16+
; CHECK-NEXT: store i32* @d, i32** %2, align 8
17+
; CHECK-NEXT: %3 = load i32*, i32** @a, align 8
18+
; CHECK-NEXT: %cmp1 = icmp ne i32* %3, @d
19+
; CHECK-NEXT: br i1 %cmp1, label %if.then, label %if.end
20+
21+
define i32 @main() {
22+
entry:
23+
%0 = load i32, i32* @d, align 4
24+
br label %for.cond
25+
26+
for.cond: ; preds = %if.end, %entry
27+
%1 = phi i32 [ %inc, %if.end ], [ %0, %entry ]
28+
%cmp = icmp slt i32 %1, 1
29+
br i1 %cmp, label %for.body, label %for.end
30+
31+
for.body: ; preds = %for.cond
32+
%2 = load i32**, i32*** @b, align 8
33+
store i32* @d, i32** %2, align 8
34+
%3 = load i32*, i32** @a, align 8
35+
%cmp1 = icmp ne i32* %3, @d
36+
br i1 %cmp1, label %if.then, label %if.end
37+
38+
if.then: ; preds = %for.body
39+
br label %return
40+
41+
if.end: ; preds = %for.body
42+
%4 = load i32, i32* @d, align 4
43+
%inc = add nsw i32 %4, 1
44+
store i32 %inc, i32* @d, align 4
45+
br label %for.cond
46+
47+
for.end: ; preds = %for.cond
48+
br label %return
49+
50+
return: ; preds = %for.end, %if.then
51+
%retval.0 = phi i32 [ 1, %if.then ], [ 0, %for.end ]
52+
ret i32 %retval.0
53+
}
54+

0 commit comments

Comments
 (0)