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
+76 -2
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;
}