Skip to content

Commit ac3124f

Browse files
committed
[Clang][BasicAA][AIE2] Enable full PHI AA for AIE
Also increase the max limit of the search depth in DecomposeGEPExpression. With this change we can allow LICM to hoist more instructions.
1 parent 623145b commit ac3124f

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

clang/lib/Driver/ToolChains/AIE.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ void AIEToolChain::addClangTargetOptions(
113113
// Make sure to perform most optimizations before mandatory inlinings,
114114
// otherwise noalias attributes can get lost and hurt AA results.
115115
CC1Args.append({"-mllvm", "-mandatory-inlining-before-opt=false"});
116+
117+
// Perform complete AA analysis on phi nodes.
118+
CC1Args.append({"-mllvm", "-basic-aa-full-phi-analysis=true"});
119+
120+
// Extend the max limit of the search depth in BasicAA
121+
CC1Args.append({"-mllvm", "-basic-aa-max-lookup-search-depth=10"});
116122
}
117123

118124
// Avoid using newer dwarf versions, as the simulator doesn't understand newer

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,26 @@ using namespace llvm;
6868
static cl::opt<bool> EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden,
6969
cl::init(true));
7070

71+
/// Enable full analysis of PHI nodes.
72+
static cl::opt<bool> EnableFullPHIAnalysis("basic-aa-full-phi-analysis",
73+
cl::Hidden, cl::init(false));
74+
7175
static cl::opt<bool> EnableSeparateStorageAnalysis("basic-aa-separate-storage",
7276
cl::Hidden, cl::init(true));
7377

78+
// The max limit of the search depth in DecomposeGEPExpression() and
79+
// getUnderlyingObject().
80+
static cl::opt<unsigned>
81+
MaxLookupSearchDepth("basic-aa-max-lookup-search-depth", cl::Hidden,
82+
cl::init(6));
83+
7484
/// SearchLimitReached / SearchTimes shows how often the limit of
7585
/// to decompose GEPs is reached. It will affect the precision
7686
/// of basic alias analysis.
7787
STATISTIC(SearchLimitReached, "Number of times the limit to "
7888
"decompose GEPs is reached");
7989
STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
8090

81-
// The max limit of the search depth in DecomposeGEPExpression() and
82-
// getUnderlyingObject().
83-
static const unsigned MaxLookupSearchDepth = 6;
84-
8591
bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
8692
FunctionAnalysisManager::Invalidator &Inv) {
8793
// We don't care if this analysis itself is preserved, it has no state. But
@@ -1389,7 +1395,7 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
13891395
if (PV1 == PN)
13901396
continue;
13911397

1392-
if (isa<PHINode>(PV1)) {
1398+
if (!EnableFullPHIAnalysis && isa<PHINode>(PV1)) {
13931399
if (OnePhi && OnePhi != PV1) {
13941400
// To control potential compile time explosion, we choose to be
13951401
// conserviate when we have more than one Phi input. It is important

llvm/test/Analysis/BasicAA/phi-aa.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
2+
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -basic-aa-full-phi-analysis \
3+
; RUN: -disable-output 2>&1 | FileCheck %s --check-prefix=FULL-PHI
4+
25
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
36
target triple = "x86_64-unknown-linux-gnu"
47

@@ -85,6 +88,9 @@ declare void @inc(ptr)
8588
; CHECK: MayAlias: i32* %val1, i32* @Y
8689
; CHECK: MayAlias: i32* %val2, i32* @Y
8790
; CHECK: MayAlias: i32* %val3, i32* @Y
91+
; FULL-PHI: NoAlias: i32* %val1, i32* @Y
92+
; FULL-PHI: NoAlias: i32* %val2, i32* @Y
93+
; FULL-PHI: NoAlias: i32* %val3, i32* @Y
8894
define void @loop_phi_chain(i32 %a, i32 %b, i32 %c) {
8995
entry:
9096
br label %loop1
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
2+
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -basic-aa-full-phi-analysis \
3+
; RUN: -disable-output 2>&1 | FileCheck %s --check-prefix=FULL-PHI
4+
5+
6+
; The goal of this test is to test the differences in the behavior of
7+
; the BasicAA with and without full phi analysis in a dual diamond scenario.
8+
; The basic shape of the test is the following (simplified without phis):
9+
; if (...) {
10+
; if (...) {
11+
; %P = getelementptr @X ...
12+
; } else {
13+
; %P = getelementptr @X ...
14+
; }
15+
; } else {
16+
; %P = getelementptr @X ...
17+
; }
18+
;
19+
; // Use of %P and @Y
20+
;
21+
22+
@X = common global i32 0
23+
@Y = common global i32 0
24+
25+
; CHECK: MayAlias: i32* %P, i32* @Y
26+
; FULL-PHI: NoAlias: i32* %P, i32* @Y
27+
define void @foo(i32 %cond) nounwind {
28+
entry:
29+
%"alloca point" = bitcast i32 0 to i32
30+
%tmp = icmp ne i32 %cond, 0
31+
br i1 %tmp, label %bbtrue, label %bbfalse
32+
33+
bbtrue:
34+
%tmp1 = icmp ne i32 %cond, 2
35+
br i1 %tmp, label %bbtruetrue, label %bbtruefalse
36+
37+
bbtruetrue:
38+
%p1 = getelementptr i32, ptr @X, i64 3
39+
br label %bbtrueend
40+
41+
bbtruefalse:
42+
%p2 = getelementptr i32, ptr @X, i64 4
43+
br label %bbtrueend
44+
45+
bbtrueend:
46+
%p3 = phi ptr [ %p1, %bbtruetrue ], [ %p2, %bbtruefalse ]
47+
br label %bblast
48+
49+
bbfalse:
50+
%p4 = getelementptr i32, ptr @X, i64 2
51+
br label %bblast
52+
53+
bblast:
54+
%P = phi ptr [ %p3, %bbtrueend ], [ %p4, %bbfalse ]
55+
%tmp2 = load i32, ptr @Y, align 4
56+
store i32 123, ptr %P, align 4
57+
%tmp3 = load i32, ptr @Y, align 4
58+
br label %return
59+
60+
return:
61+
ret void
62+
}

0 commit comments

Comments
 (0)