Integrate the readonly/readnone logic more deeply

into alias analysis.  This meant updating the API
which now has versions of the getModRefBehavior,
doesNotAccessMemory and onlyReadsMemory methods
which take a callsite parameter.  These should be
used unless the callsite is not known, since in
general they can do a better job than the versions
that take a function.  Also, users should no longer
call the version of getModRefBehavior that takes
both a function and a callsite.  To reduce the
chance of misuse it is now protected.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44487 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan Sands 2007-12-01 07:51:45 +00:00
parent e3110d0825
commit dff6710717
12 changed files with 122 additions and 90 deletions

View File

@ -186,40 +186,57 @@ public:
}; };
}; };
/// getModRefBehavior - Return the behavior of the specified function if /// getModRefBehavior - Return the behavior when calling the given call site.
/// called from the specified call site. The call site may be null in which ModRefBehavior getModRefBehavior(CallSite CS,
/// case the most generic behavior of this function should be returned.
virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
std::vector<PointerAccessInfo> *Info = 0); std::vector<PointerAccessInfo> *Info = 0);
/// doesNotAccessMemory - If the specified function is known to never read or /// getModRefBehavior - Return the behavior when calling the given function.
/// write memory, return true. If the function only reads from known-constant /// For use when the call site is not known.
/// memory, it is also legal to return true. Functions that unwind the stack ModRefBehavior getModRefBehavior(Function *F,
/// are not legal for this predicate. std::vector<PointerAccessInfo> *Info = 0);
/// doesNotAccessMemory - If the specified call is known to never read or
/// write memory, return true. If the call only reads from known-constant
/// memory, it is also legal to return true. Calls that unwind the stack
/// are legal for this predicate.
/// ///
/// Many optimizations (such as CSE and LICM) can be performed on calls to it, /// Many optimizations (such as CSE and LICM) can be performed on such calls
/// without worrying about aliasing properties, and many functions have this /// without worrying about aliasing properties, and many calls have this
/// property (e.g. 'sin' and 'cos'). /// property (e.g. calls to 'sin' and 'cos').
/// ///
/// This property corresponds to the GCC 'const' attribute. /// This property corresponds to the GCC 'const' attribute.
/// ///
bool doesNotAccessMemory(Function *F) { bool doesNotAccessMemory(CallSite CS) {
return getModRefBehavior(F, CallSite()) == DoesNotAccessMemory; return getModRefBehavior(CS) == DoesNotAccessMemory;
} }
/// onlyReadsMemory - If the specified function is known to only read from /// doesNotAccessMemory - If the specified function is known to never read or
/// non-volatile memory (or not access memory at all), return true. Functions /// write memory, return true. For use when the call site is not known.
/// that unwind the stack are not legal for this predicate. ///
bool doesNotAccessMemory(Function *F) {
return getModRefBehavior(F) == DoesNotAccessMemory;
}
/// onlyReadsMemory - If the specified call is known to only read from
/// non-volatile memory (or not access memory at all), return true. Calls
/// that unwind the stack are legal for this predicate.
/// ///
/// This property allows many common optimizations to be performed in the /// This property allows many common optimizations to be performed in the
/// absence of interfering store instructions, such as CSE of strlen calls. /// absence of interfering store instructions, such as CSE of strlen calls.
/// ///
/// This property corresponds to the GCC 'pure' attribute. /// This property corresponds to the GCC 'pure' attribute.
/// ///
bool onlyReadsMemory(CallSite CS) {
ModRefBehavior MRB = getModRefBehavior(CS);
return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
}
/// onlyReadsMemory - If the specified function is known to only read from
/// non-volatile memory (or not access memory at all), return true. For use
/// when the call site is not known.
///
bool onlyReadsMemory(Function *F) { bool onlyReadsMemory(Function *F) {
/// FIXME: If the analysis returns more precise info, we can reduce it to ModRefBehavior MRB = getModRefBehavior(F);
/// this.
ModRefBehavior MRB = getModRefBehavior(F, CallSite());
return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
} }
@ -250,6 +267,14 @@ public:
/// ///
virtual bool hasNoModRefInfoForCalls() const; virtual bool hasNoModRefInfoForCalls() const;
protected:
/// getModRefBehavior - Return the behavior of the specified function if
/// called from the specified call site. The call site may be null in which
/// case the most generic behavior of this function should be returned.
virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
std::vector<PointerAccessInfo> *Info = 0);
public:
/// Convenience functions... /// Convenience functions...
ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size); ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size); ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);

View File

@ -27,6 +27,7 @@
#include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/BasicBlock.h" #include "llvm/BasicBlock.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/Type.h" #include "llvm/Type.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
@ -112,16 +113,40 @@ AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
return pointsToConstantMemory(P) ? NoModRef : Mod; return pointsToConstantMemory(P) ? NoModRef : Mod;
} }
AliasAnalysis::ModRefBehavior
AliasAnalysis::getModRefBehavior(CallSite CS,
std::vector<PointerAccessInfo> *Info) {
if (CS.paramHasAttr(0, ParamAttr::ReadNone))
// Can't do better than this.
return DoesNotAccessMemory;
ModRefBehavior MRB = UnknownModRefBehavior;
if (Function *F = CS.getCalledFunction())
MRB = getModRefBehavior(F, CS, Info);
if (MRB != DoesNotAccessMemory && CS.paramHasAttr(0, ParamAttr::ReadOnly))
return OnlyReadsMemory;
return MRB;
}
AliasAnalysis::ModRefBehavior
AliasAnalysis::getModRefBehavior(Function *F,
std::vector<PointerAccessInfo> *Info) {
if (F->paramHasAttr(0, ParamAttr::ReadNone))
// Can't do better than this.
return DoesNotAccessMemory;
ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
if (MRB != DoesNotAccessMemory && F->paramHasAttr(0, ParamAttr::ReadOnly))
return OnlyReadsMemory;
return MRB;
}
AliasAnalysis::ModRefResult AliasAnalysis::ModRefResult
AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
ModRefResult Mask = ModRef; ModRefResult Mask = ModRef;
if (Function *F = CS.getCalledFunction()) { ModRefBehavior MRB = getModRefBehavior(CS);
ModRefBehavior MRB = getModRefBehavior(F, CallSite());
if (MRB == OnlyReadsMemory) if (MRB == OnlyReadsMemory)
Mask = Ref; Mask = Ref;
else if (MRB == DoesNotAccessMemory) else if (MRB == DoesNotAccessMemory)
return NoModRef; return NoModRef;
}
if (!AA) return Mask; if (!AA) return Mask;

View File

@ -89,9 +89,15 @@ namespace {
bool pointsToConstantMemory(const Value *P) { bool pointsToConstantMemory(const Value *P) {
return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P); return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
} }
bool doesNotAccessMemory(CallSite CS) {
return getAnalysis<AliasAnalysis>().doesNotAccessMemory(CS);
}
bool doesNotAccessMemory(Function *F) { bool doesNotAccessMemory(Function *F) {
return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F); return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F);
} }
bool onlyReadsMemory(CallSite CS) {
return getAnalysis<AliasAnalysis>().onlyReadsMemory(CS);
}
bool onlyReadsMemory(Function *F) { bool onlyReadsMemory(Function *F) {
return getAnalysis<AliasAnalysis>().onlyReadsMemory(F); return getAnalysis<AliasAnalysis>().onlyReadsMemory(F);
} }

View File

@ -114,8 +114,7 @@ void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) { void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
CallSites.push_back(CS); CallSites.push_back(CS);
if (Function *F = CS.getCalledFunction()) { AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(F, CS);
if (Behavior == AliasAnalysis::DoesNotAccessMemory) if (Behavior == AliasAnalysis::DoesNotAccessMemory)
return; return;
else if (Behavior == AliasAnalysis::OnlyReadsMemory) { else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
@ -123,7 +122,6 @@ void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
AccessTy |= Refs; AccessTy |= Refs;
return; return;
} }
}
// FIXME: This should use mod/ref information to make this not suck so bad // FIXME: This should use mod/ref information to make this not suck so bad
AliasTy = MayAlias; AliasTy = MayAlias;
@ -166,8 +164,7 @@ bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
} }
bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const { bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
if (Function *F = CS.getCalledFunction()) if (AA.doesNotAccessMemory(CS))
if (AA.doesNotAccessMemory(F))
return false; return false;
if (AA.hasNoModRefInfoForCalls()) if (AA.hasNoModRefInfoForCalls())
@ -297,8 +294,7 @@ bool AliasSetTracker::add(FreeInst *FI) {
bool AliasSetTracker::add(CallSite CS) { bool AliasSetTracker::add(CallSite CS) {
if (Function *F = CS.getCalledFunction()) if (AA.doesNotAccessMemory(CS))
if (AA.doesNotAccessMemory(F))
return true; // doesn't alias anything return true; // doesn't alias anything
AliasSet *AS = findAliasSetForCallSite(CS); AliasSet *AS = findAliasSetForCallSite(CS);
@ -419,8 +415,7 @@ bool AliasSetTracker::remove(FreeInst *FI) {
} }
bool AliasSetTracker::remove(CallSite CS) { bool AliasSetTracker::remove(CallSite CS) {
if (Function *F = CS.getCalledFunction()) if (AA.doesNotAccessMemory(CS))
if (AA.doesNotAccessMemory(F))
return false; // doesn't alias anything return false; // doesn't alias anything
AliasSet *AS = findAliasSetForCallSite(CS); AliasSet *AS = findAliasSetForCallSite(CS);
@ -455,13 +450,10 @@ void AliasSetTracker::deleteValue(Value *PtrVal) {
// If this is a call instruction, remove the callsite from the appropriate // If this is a call instruction, remove the callsite from the appropriate
// AliasSet. // AliasSet.
CallSite CS = CallSite::get(PtrVal); CallSite CS = CallSite::get(PtrVal);
if (CS.getInstruction()) { if (CS.getInstruction())
Function *F = CS.getCalledFunction(); if (!AA.doesNotAccessMemory(CS))
if (!F || !AA.doesNotAccessMemory(F)) {
if (AliasSet *AS = findAliasSetForCallSite(CS)) if (AliasSet *AS = findAliasSetForCallSite(CS))
AS->removeCallSite(CS); AS->removeCallSite(CS);
}
}
// First, look up the PointerRec for this pointer. // First, look up the PointerRec for this pointer.
hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal); hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);

View File

@ -839,11 +839,6 @@ BasicAliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
return UnknownModRefBehavior; return UnknownModRefBehavior;
} }
if (F->paramHasAttr(0, ParamAttr::ReadNone))
return DoesNotAccessMemory;
if (F->paramHasAttr(0, ParamAttr::ReadOnly))
return OnlyReadsMemory;
return UnknownModRefBehavior; return UnknownModRefBehavior;
} }

View File

@ -394,7 +394,7 @@ void GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) {
// Okay, if we can't say anything about it, maybe some other alias // Okay, if we can't say anything about it, maybe some other alias
// analysis can. // analysis can.
ModRefBehavior MRB = ModRefBehavior MRB =
AliasAnalysis::getModRefBehavior(Callee, CallSite()); AliasAnalysis::getModRefBehavior(Callee);
if (MRB != DoesNotAccessMemory) { if (MRB != DoesNotAccessMemory) {
// FIXME: could make this more aggressive for functions that just // FIXME: could make this more aggressive for functions that just
// read memory. We should just say they read all globals. // read memory. We should just say they read all globals.

View File

@ -148,7 +148,7 @@ void LoadVN::getCallEqualNumberNodes(CallInst *CI,
Function *CF = CI->getCalledFunction(); Function *CF = CI->getCalledFunction();
if (CF == 0) return; // Indirect call. if (CF == 0) return; // Indirect call.
AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CF, CI); AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CI);
if (MRB != AliasAnalysis::DoesNotAccessMemory && if (MRB != AliasAnalysis::DoesNotAccessMemory &&
MRB != AliasAnalysis::OnlyReadsMemory) MRB != AliasAnalysis::OnlyReadsMemory)
return; // Nothing we can do for now. return; // Nothing we can do for now.
@ -227,8 +227,7 @@ void LoadVN::getCallEqualNumberNodes(CallInst *CI,
CantEqual = true; CantEqual = true;
break; break;
} else if (CallInst *CI = dyn_cast<CallInst>(I)) { } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
if (CI->getCalledFunction() == 0 || if (!AA.onlyReadsMemory(CI)) {
!AA.onlyReadsMemory(CI->getCalledFunction())) {
CantEqual = true; CantEqual = true;
break; break;
} }

View File

@ -94,11 +94,9 @@ Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
// FreeInsts erase the entire structure // FreeInsts erase the entire structure
pointerSize = ~0UL; pointerSize = ~0UL;
} else if (CallSite::get(QI).getInstruction() != 0 && } else if (isa<CallInst>(QI)) {
cast<CallInst>(QI)->getCalledFunction()) {
AliasAnalysis::ModRefBehavior result = AliasAnalysis::ModRefBehavior result =
AA.getModRefBehavior(cast<CallInst>(QI)->getCalledFunction(), AA.getModRefBehavior(CallSite::get(QI));
CallSite::get(QI));
if (result != AliasAnalysis::DoesNotAccessMemory && if (result != AliasAnalysis::DoesNotAccessMemory &&
result != AliasAnalysis::OnlyReadsMemory) { result != AliasAnalysis::OnlyReadsMemory) {
if (!start && !block) { if (!start && !block) {

View File

@ -198,8 +198,7 @@ bool ADCE::doADCE() {
for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
Instruction *I = II++; Instruction *I = II++;
if (CallInst *CI = dyn_cast<CallInst>(I)) { if (CallInst *CI = dyn_cast<CallInst>(I)) {
Function *F = CI->getCalledFunction(); if (AA.onlyReadsMemory(CI)) {
if (F && AA.onlyReadsMemory(F)) {
if (CI->use_empty()) { if (CI->use_empty()) {
BB->getInstList().erase(CI); BB->getInstList().erase(CI);
++NumCallRemoved; ++NumCallRemoved;

View File

@ -306,8 +306,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
// If this call does not access memory, it can't // If this call does not access memory, it can't
// be undeadifying any of our pointers. // be undeadifying any of our pointers.
CallSite CS = CallSite::get(BBI); CallSite CS = CallSite::get(BBI);
if (CS.getCalledFunction() && if (AA.doesNotAccessMemory(CS))
AA.doesNotAccessMemory(CS.getCalledFunction()))
continue; continue;
unsigned modRef = 0; unsigned modRef = 0;

View File

@ -341,8 +341,7 @@ Expression::ExpressionOpcode
uint32_t ValueTable::hash_operand(Value* v) { uint32_t ValueTable::hash_operand(Value* v) {
if (CallInst* CI = dyn_cast<CallInst>(v)) if (CallInst* CI = dyn_cast<CallInst>(v))
if (CI->getCalledFunction() && if (!AA->doesNotAccessMemory(CI))
!AA->doesNotAccessMemory(CI->getCalledFunction()))
return nextValueNumber++; return nextValueNumber++;
return lookup_or_add(v); return lookup_or_add(v);
@ -485,9 +484,7 @@ uint32_t ValueTable::lookup_or_add(Value* V) {
return VI->second; return VI->second;
if (CallInst* C = dyn_cast<CallInst>(V)) { if (CallInst* C = dyn_cast<CallInst>(V)) {
if (C->getCalledFunction() && if (AA->onlyReadsMemory(C)) { // includes doesNotAccessMemory
(AA->doesNotAccessMemory(C->getCalledFunction()) ||
AA->onlyReadsMemory(C->getCalledFunction()))) {
Expression e = create_expression(C); Expression e = create_expression(C);
DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
@ -1051,8 +1048,7 @@ bool GVN::processInstruction(Instruction* I,
if (CallInst* CI = dyn_cast<CallInst>(I)) { if (CallInst* CI = dyn_cast<CallInst>(I)) {
AliasAnalysis& AA = getAnalysis<AliasAnalysis>(); AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
if (CI->getCalledFunction() && if (!AA.doesNotAccessMemory(CI)) {
!AA.doesNotAccessMemory(CI->getCalledFunction())) {
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
if (cast<Instruction>(repl)->getParent() != CI->getParent() || if (cast<Instruction>(repl)->getParent() != CI->getParent() ||
MD.getDependency(CI) != MD.getDependency(cast<CallInst>(repl))) { MD.getDependency(CI) != MD.getDependency(cast<CallInst>(repl))) {

View File

@ -375,8 +375,7 @@ bool LICM::canSinkOrHoistInst(Instruction &I) {
return !pointerInvalidatedByLoop(LI->getOperand(0), Size); return !pointerInvalidatedByLoop(LI->getOperand(0), Size);
} else if (CallInst *CI = dyn_cast<CallInst>(&I)) { } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
// Handle obvious cases efficiently. // Handle obvious cases efficiently.
if (Function *Callee = CI->getCalledFunction()) { AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI);
AliasAnalysis::ModRefBehavior Behavior =AA->getModRefBehavior(Callee, CI);
if (Behavior == AliasAnalysis::DoesNotAccessMemory) if (Behavior == AliasAnalysis::DoesNotAccessMemory)
return true; return true;
else if (Behavior == AliasAnalysis::OnlyReadsMemory) { else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
@ -393,7 +392,6 @@ bool LICM::canSinkOrHoistInst(Instruction &I) {
} }
if (!FoundMod) return true; if (!FoundMod) return true;
} }
}
// FIXME: This should use mod/ref information to see if we can hoist or sink // FIXME: This should use mod/ref information to see if we can hoist or sink
// the call. // the call.