Teach BasicAA how to analyze Select instructions, and make it more

aggressive on PHI instructions.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85158 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-10-26 21:55:43 +00:00
parent cadd4b9ced
commit 6665b0ea68
2 changed files with 149 additions and 2 deletions

View File

@ -217,7 +217,7 @@ namespace {
private:
// VisitedPHIs - Track PHI nodes visited by a aliasCheck() call.
SmallPtrSet<const PHINode*, 16> VisitedPHIs;
SmallPtrSet<const Value*, 16> VisitedPHIs;
// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
// against another.
@ -229,6 +229,10 @@ namespace {
AliasResult aliasPHI(const PHINode *PN, unsigned PNSize,
const Value *V2, unsigned V2Size);
/// aliasSelect - Disambiguate a Select instruction against another value.
AliasResult aliasSelect(const SelectInst *SI, unsigned SISize,
const Value *V2, unsigned V2Size);
AliasResult aliasCheck(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size);
@ -519,6 +523,41 @@ BasicAliasAnalysis::aliasGEP(const Value *V1, unsigned V1Size,
return MayAlias;
}
// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select instruction
// against another.
AliasAnalysis::AliasResult
BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize,
const Value *V2, unsigned V2Size) {
// If the values are Selects with the same condition, we can do a more precise
// check: just check for aliases between the values on corresponding arms.
if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
if (SI->getCondition() == SI2->getCondition()) {
AliasResult Alias =
aliasCheck(SI->getTrueValue(), SISize,
SI2->getTrueValue(), V2Size);
if (Alias == MayAlias)
return MayAlias;
AliasResult ThisAlias =
aliasCheck(SI->getFalseValue(), SISize,
SI2->getFalseValue(), V2Size);
if (ThisAlias != Alias)
return MayAlias;
return Alias;
}
// If both arms of the Select node NoAlias or MustAlias V2, then returns
// NoAlias / MustAlias. Otherwise, returns MayAlias.
AliasResult Alias =
aliasCheck(SI->getTrueValue(), SISize, V2, V2Size);
if (Alias == MayAlias)
return MayAlias;
AliasResult ThisAlias =
aliasCheck(SI->getFalseValue(), SISize, V2, V2Size);
if (ThisAlias != Alias)
return MayAlias;
return Alias;
}
// aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
// against another.
AliasAnalysis::AliasResult
@ -528,6 +567,28 @@ BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize,
if (!VisitedPHIs.insert(PN))
return MayAlias;
// If the values are PHIs in the same block, we can do a more precise
// as well as efficient check: just check for aliases between the values
// on corresponding edges.
if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
if (PN2->getParent() == PN->getParent()) {
AliasResult Alias =
aliasCheck(PN->getIncomingValue(0), PNSize,
PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)),
V2Size);
if (Alias == MayAlias)
return MayAlias;
for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
AliasResult ThisAlias =
aliasCheck(PN->getIncomingValue(i), PNSize,
PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
V2Size);
if (ThisAlias != Alias)
return MayAlias;
}
return Alias;
}
SmallPtrSet<Value*, 4> UniqueSrc;
SmallVector<Value*, 4> V1Srcs;
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
@ -542,7 +603,7 @@ BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize,
V1Srcs.push_back(PV1);
}
AliasResult Alias = aliasCheck(V1Srcs[0], PNSize, V2, V2Size);
AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize);
// Early exit if the check of the first PHI source against V2 is MayAlias.
// Other results are not possible.
if (Alias == MayAlias)
@ -552,6 +613,12 @@ BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize,
// NoAlias / MustAlias. Otherwise, returns MayAlias.
for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
Value *V = V1Srcs[i];
// If V2 is a PHI, the recursive case will have been caught in the
// above aliasCheck call, so these subsequent calls to aliasCheck
// don't need to assume that V2 is being visited recursively.
VisitedPHIs.erase(V2);
AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize);
if (ThisAlias != Alias || ThisAlias == MayAlias)
return MayAlias;
@ -628,6 +695,13 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
if (const PHINode *PN = dyn_cast<PHINode>(V1))
return aliasPHI(PN, V1Size, V2, V2Size);
if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
std::swap(V1, V2);
std::swap(V1Size, V2Size);
}
if (const SelectInst *S1 = dyn_cast<SelectInst>(V1))
return aliasSelect(S1, V1Size, V2, V2Size);
return MayAlias;
}

View File

@ -0,0 +1,73 @@
; RUN: opt < %s -aa-eval -print-all-alias-modref-info -disable-output \
; RUN: |& grep {NoAlias: double\\* \[%\]a, double\\* \[%\]b\$} | count 4
; BasicAA should detect NoAliases in PHIs and Selects.
; Two PHIs in the same block.
define void @foo(i1 %m, double* noalias %x, double* noalias %y) {
entry:
br i1 %m, label %true, label %false
true:
br label %exit
false:
br label %exit
exit:
%a = phi double* [ %x, %true ], [ %y, %false ]
%b = phi double* [ %x, %false ], [ %y, %true ]
volatile store double 0.0, double* %a
volatile store double 1.0, double* %b
ret void
}
; Two selects with the same condition.
define void @bar(i1 %m, double* noalias %x, double* noalias %y) {
entry:
%a = select i1 %m, double* %x, double* %y
%b = select i1 %m, double* %y, double* %x
volatile store double 0.000000e+00, double* %a
volatile store double 1.000000e+00, double* %b
ret void
}
; Two PHIs with disjoint sets of inputs.
define void @qux(i1 %m, double* noalias %x, double* noalias %y,
i1 %n, double* noalias %v, double* noalias %w) {
entry:
br i1 %m, label %true, label %false
true:
br label %exit
false:
br label %exit
exit:
%a = phi double* [ %x, %true ], [ %y, %false ]
br i1 %n, label %ntrue, label %nfalse
ntrue:
br label %nexit
nfalse:
br label %nexit
nexit:
%b = phi double* [ %v, %ntrue ], [ %w, %nfalse ]
volatile store double 0.0, double* %a
volatile store double 1.0, double* %b
ret void
}
; Two selects with disjoint sets of arms.
define void @fin(i1 %m, double* noalias %x, double* noalias %y,
i1 %n, double* noalias %v, double* noalias %w) {
entry:
%a = select i1 %m, double* %x, double* %y
%b = select i1 %n, double* %v, double* %w
volatile store double 0.000000e+00, double* %a
volatile store double 1.000000e+00, double* %b
ret void
}