Extend the AliasAnalysis::pointsToConstantMemory interface to allow it

to optionally look for constant or local (alloca) memory.

Teach BasicAliasAnalysis::pointsToConstantMemory to look through Select
and Phi nodes, and to support looking for local memory.

Remove FunctionAttrs' PointsToLocalOrConstantMemory function, now that
AliasAnalysis knows all the tricks that it knew.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@118412 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman
2010-11-08 16:45:26 +00:00
parent c80cbf2540
commit a25e5dbcc2
9 changed files with 85 additions and 83 deletions

View File

@ -154,15 +154,16 @@ public:
return isNoAlias(Location(V1, V1Size), Location(V2, V2Size)); return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
} }
/// pointsToConstantMemory - If the specified memory location is known to be /// pointsToConstantMemory - If the specified memory location is
/// constant, return true. This allows disambiguation of store /// known to be constant, return true. If OrLocal is true and the
/// instructions from constant pointers. /// specified memory location is known to be "local" (derived from
/// /// an alloca), return true. Otherwise return false.
virtual bool pointsToConstantMemory(const Location &Loc); virtual bool pointsToConstantMemory(const Location &Loc,
bool OrLocal = false);
/// pointsToConstantMemory - A convenient wrapper. /// pointsToConstantMemory - A convenient wrapper.
bool pointsToConstantMemory(const Value *P) { bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
return pointsToConstantMemory(Location(P)); return pointsToConstantMemory(Location(P), OrLocal);
} }
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//

View File

@ -49,9 +49,10 @@ AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
return AA->alias(LocA, LocB); return AA->alias(LocA, LocB);
} }
bool AliasAnalysis::pointsToConstantMemory(const Location &Loc) { bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
bool OrLocal) {
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
return AA->pointsToConstantMemory(Loc); return AA->pointsToConstantMemory(Loc, OrLocal);
} }
void AliasAnalysis::deleteValue(Value *V) { void AliasAnalysis::deleteValue(Value *V) {

View File

@ -95,8 +95,8 @@ namespace {
} }
// FIXME: We could count these too... // FIXME: We could count these too...
bool pointsToConstantMemory(const Location &Loc) { bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc); return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal);
} }
// Forwarding functions: just delegate to a real AA implementation, counting // Forwarding functions: just delegate to a real AA implementation, counting

View File

@ -113,9 +113,9 @@ namespace {
return AliasAnalysis::getModRefInfo(CS1,CS2); return AliasAnalysis::getModRefInfo(CS1,CS2);
} }
bool pointsToConstantMemory(const Location &Loc) { bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before"); assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
return AliasAnalysis::pointsToConstantMemory(Loc); return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
} }
virtual void deleteValue(Value *V) { virtual void deleteValue(Value *V) {

View File

@ -456,7 +456,7 @@ namespace {
/// pointsToConstantMemory - Chase pointers until we find a (constant /// pointsToConstantMemory - Chase pointers until we find a (constant
/// global) or not. /// global) or not.
virtual bool pointsToConstantMemory(const Location &Loc); virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
/// getModRefBehavior - Return the behavior when calling the given /// getModRefBehavior - Return the behavior when calling the given
/// call site. /// call site.
@ -517,18 +517,61 @@ ImmutablePass *llvm::createBasicAliasAnalysisPass() {
return new BasicAliasAnalysis(); return new BasicAliasAnalysis();
} }
/// pointsToConstantMemory - Returns whether the given pointer value
/// points to memory that is local to the function, with global constants being
/// considered local to all functions.
bool
BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc, bool OrLocal) {
assert(Visited.empty() && "Visited must be cleared after use!");
/// pointsToConstantMemory - Chase pointers until we find a (constant SmallVector<const Value *, 16> Worklist;
/// global) or not. Worklist.push_back(Loc.Ptr);
bool BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc) { do {
if (const GlobalVariable *GV = const Value *V = Worklist.pop_back_val()->getUnderlyingObject();
dyn_cast<GlobalVariable>(Loc.Ptr->getUnderlyingObject())) if (!Visited.insert(V)) {
// Note: this doesn't require GV to be "ODR" because it isn't legal for a Visited.clear();
// global to be marked constant in some modules and non-constant in others. return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
// GV may even be a declaration, not a definition. }
return GV->isConstant();
return AliasAnalysis::pointsToConstantMemory(Loc); // An alloca instruction defines local memory.
if (OrLocal && isa<AllocaInst>(V))
continue;
// A global constant counts as local memory for our purposes.
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
// Note: this doesn't require GV to be "ODR" because it isn't legal for a
// global to be marked constant in some modules and non-constant in
// others. GV may even be a declaration, not a definition.
if (!GV->isConstant()) {
Visited.clear();
return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
}
continue;
}
// If both select values point to local memory, then so does the select.
if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
Worklist.push_back(SI->getTrueValue());
Worklist.push_back(SI->getFalseValue());
continue;
}
// If all values incoming to a phi node point to local memory, then so does
// the phi.
if (const PHINode *PN = dyn_cast<PHINode>(V)) {
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Worklist.push_back(PN->getIncomingValue(i));
continue;
}
// Otherwise be conservative.
Visited.clear();
return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
} while (!Worklist.empty());
Visited.clear();
return true;
} }
/// getModRefBehavior - Return the behavior when calling the given call site. /// getModRefBehavior - Return the behavior when calling the given call site.

View File

@ -50,7 +50,10 @@ namespace {
return UnknownModRefBehavior; return UnknownModRefBehavior;
} }
virtual bool pointsToConstantMemory(const Location &Loc) { return false; } virtual bool pointsToConstantMemory(const Location &Loc,
bool OrLocal) {
return false;
}
virtual ModRefResult getModRefInfo(ImmutableCallSite CS, virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
const Location &Loc) { const Location &Loc) {
return ModRef; return ModRef;

View File

@ -138,7 +138,7 @@ namespace {
private: private:
virtual void getAnalysisUsage(AnalysisUsage &AU) const; virtual void getAnalysisUsage(AnalysisUsage &AU) const;
virtual AliasResult alias(const Location &LocA, const Location &LocB); virtual AliasResult alias(const Location &LocA, const Location &LocB);
virtual bool pointsToConstantMemory(const Location &Loc); virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
virtual ModRefResult getModRefInfo(ImmutableCallSite CS, virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
const Location &Loc); const Location &Loc);
virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
@ -225,19 +225,20 @@ TypeBasedAliasAnalysis::alias(const Location &LocA,
return NoAlias; return NoAlias;
} }
bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc) { bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc,
bool OrLocal) {
if (!EnableTBAA) if (!EnableTBAA)
return AliasAnalysis::pointsToConstantMemory(Loc); return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
const MDNode *M = Loc.TBAATag; const MDNode *M = Loc.TBAATag;
if (!M) return AliasAnalysis::pointsToConstantMemory(Loc); if (!M) return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
// If this is an "immutable" type, we can assume the pointer is pointing // If this is an "immutable" type, we can assume the pointer is pointing
// to constant memory. // to constant memory.
if (TBAANode(M).TypeIsImmutable()) if (TBAANode(M).TypeIsImmutable())
return true; return true;
return AliasAnalysis::pointsToConstantMemory(Loc); return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
} }
AliasAnalysis::ModRefResult AliasAnalysis::ModRefResult

View File

@ -66,8 +66,6 @@ namespace {
CallGraphSCCPass::getAnalysisUsage(AU); CallGraphSCCPass::getAnalysisUsage(AU);
} }
bool PointsToLocalOrConstantMemory(Value *V);
private: private:
AliasAnalysis *AA; AliasAnalysis *AA;
}; };
@ -83,53 +81,6 @@ INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
/// PointsToLocalOrConstantMemory - Returns whether the given pointer value
/// points to memory that is local to the function, with global constants being
/// considered local to all functions.
bool FunctionAttrs::PointsToLocalOrConstantMemory(Value *V) {
SmallVector<Value*, 16> Worklist;
unsigned MaxLookup = 8;
Worklist.push_back(V);
do {
V = Worklist.pop_back_val()->getUnderlyingObject();
// An alloca instruction defines local memory.
if (isa<AllocaInst>(V))
continue;
// A global constant counts as local memory for our purposes.
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
if (!GV->isConstant())
return false;
continue;
}
// If both select values point to local memory, then so does the select.
if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
Worklist.push_back(SI->getTrueValue());
Worklist.push_back(SI->getFalseValue());
continue;
}
// If all values incoming to a phi node point to local memory, then so does
// the phi.
if (PHINode *PN = dyn_cast<PHINode>(V)) {
// Don't bother inspecting phi nodes with many operands.
if (PN->getNumIncomingValues() > MaxLookup)
return false;
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Worklist.push_back(PN->getIncomingValue(i));
continue;
}
return false;
} while (!Worklist.empty() && --MaxLookup);
return Worklist.empty();
}
/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
SmallPtrSet<Function*, 8> SCCNodes; SmallPtrSet<Function*, 8> SCCNodes;
@ -190,7 +141,7 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
CI != CE; ++CI) { CI != CE; ++CI) {
Value *Arg = *CI; Value *Arg = *CI;
if (Arg->getType()->isPointerTy() && if (Arg->getType()->isPointerTy() &&
!PointsToLocalOrConstantMemory(Arg)) !AA->pointsToConstantMemory(Arg, /*OrLocal=*/true))
// Writes memory. Just give up. // Writes memory. Just give up.
return false; return false;
} }
@ -203,12 +154,14 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
} else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
// Ignore non-volatile loads from local memory. // Ignore non-volatile loads from local memory.
if (!LI->isVolatile() && if (!LI->isVolatile() &&
PointsToLocalOrConstantMemory(LI->getPointerOperand())) AA->pointsToConstantMemory(LI->getPointerOperand(),
/*OrLocal=*/true))
continue; continue;
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
// Ignore non-volatile stores to local memory. // Ignore non-volatile stores to local memory.
if (!SI->isVolatile() && if (!SI->isVolatile() &&
PointsToLocalOrConstantMemory(SI->getPointerOperand())) AA->pointsToConstantMemory(SI->getPointerOperand(),
/*OrLocal=*/true))
continue; continue;
} }

View File

@ -1,4 +1,4 @@
; RUN: opt < %s -functionattrs -S | grep readnone ; RUN: opt < %s -basicaa -functionattrs -S | grep readnone
@s = external constant i8 ; <i8*> [#uses=1] @s = external constant i8 ; <i8*> [#uses=1]