#define DEBUG_TYPE "globalsmodref-aa"
#include "llvm/Analysis/Passes.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Instructions.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/SCCIterator.h"
#include <set>
using namespace llvm;
STATISTIC(NumNonAddrTakenGlobalVars,
"Number of global vars without address taken");
STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
namespace {
struct VISIBILITY_HIDDEN FunctionRecord {
std::map<GlobalValue*, unsigned> GlobalInfo;
bool MayReadAnyGlobal;
unsigned getInfoForGlobal(GlobalValue *GV) const {
unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV);
if (I != GlobalInfo.end())
Effect |= I->second;
return Effect;
}
unsigned FunctionEffect;
FunctionRecord() : MayReadAnyGlobal (false), FunctionEffect(0) {}
};
class VISIBILITY_HIDDEN GlobalsModRef
: public ModulePass, public AliasAnalysis {
std::set<GlobalValue*> NonAddressTakenGlobals;
std::set<GlobalValue*> IndirectGlobals;
std::map<Value*, GlobalValue*> AllocsForIndirectGlobals;
std::map<Function*, FunctionRecord> FunctionInfo;
public:
static char ID;
GlobalsModRef() : ModulePass(&ID) {}
bool runOnModule(Module &M) {
InitializeAliasAnalysis(this); AnalyzeGlobals(M); AnalyzeCallGraph(getAnalysis<CallGraph>(), M); return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AliasAnalysis::getAnalysisUsage(AU);
AU.addRequired<CallGraph>();
AU.setPreservesAll(); }
AliasResult alias(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size);
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
return AliasAnalysis::getModRefInfo(CS1,CS2);
}
bool hasNoModRefInfoForCalls() const { return false; }
ModRefBehavior getModRefBehavior(Function *F,
std::vector<PointerAccessInfo> *Info) {
if (FunctionRecord *FR = getFunctionInfo(F)) {
if (FR->FunctionEffect == 0)
return DoesNotAccessMemory;
else if ((FR->FunctionEffect & Mod) == 0)
return OnlyReadsMemory;
}
return AliasAnalysis::getModRefBehavior(F, Info);
}
ModRefBehavior getModRefBehavior(CallSite CS,
std::vector<PointerAccessInfo> *Info) {
Function* F = CS.getCalledFunction();
if (!F) return AliasAnalysis::getModRefBehavior(CS, Info);
if (FunctionRecord *FR = getFunctionInfo(F)) {
if (FR->FunctionEffect == 0)
return DoesNotAccessMemory;
else if ((FR->FunctionEffect & Mod) == 0)
return OnlyReadsMemory;
}
return AliasAnalysis::getModRefBehavior(CS, Info);
}
virtual void deleteValue(Value *V);
virtual void copyValue(Value *From, Value *To);
private:
FunctionRecord *getFunctionInfo(Function *F) {
std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F);
if (I != FunctionInfo.end())
return &I->second;
return 0;
}
void AnalyzeGlobals(Module &M);
void AnalyzeCallGraph(CallGraph &CG, Module &M);
bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
std::vector<Function*> &Writers,
GlobalValue *OkayStoreDest = 0);
bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
};
}
char GlobalsModRef::ID = 0;
static RegisterPass<GlobalsModRef>
X("globalsmodref-aa", "Simple mod/ref analysis for globals", false, true);
static RegisterAnalysisGroup<AliasAnalysis> Y(X);
Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
void GlobalsModRef::AnalyzeGlobals(Module &M) {
std::vector<Function*> Readers, Writers;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
if (I->hasLocalLinkage()) {
if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
NonAddressTakenGlobals.insert(I);
++NumNonAddrTakenFunctions;
}
Readers.clear(); Writers.clear();
}
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I)
if (I->hasLocalLinkage()) {
if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
NonAddressTakenGlobals.insert(I);
for (unsigned i = 0, e = Readers.size(); i != e; ++i)
FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
if (!I->isConstant()) for (unsigned i = 0, e = Writers.size(); i != e; ++i)
FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
++NumNonAddrTakenGlobalVars;
if (isa<PointerType>(I->getType()->getElementType()) &&
AnalyzeIndirectGlobalMemory(I))
++NumIndirectGlobalVars;
}
Readers.clear(); Writers.clear();
}
}
bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
std::vector<Function*> &Readers,
std::vector<Function*> &Writers,
GlobalValue *OkayStoreDest) {
if (!isa<PointerType>(V->getType())) return true;
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
Readers.push_back(LI->getParent()->getParent());
} else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
if (V == SI->getOperand(1)) {
Writers.push_back(SI->getParent()->getParent());
} else if (SI->getOperand(1) != OkayStoreDest) {
return true; }
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
} else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
if (CI->getOperand(i) == V) return true;
} else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
if (II->getOperand(i) == V) return true;
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
if (CE->getOpcode() == Instruction::GetElementPtr ||
CE->getOpcode() == Instruction::BitCast) {
if (AnalyzeUsesOfPointer(CE, Readers, Writers))
return true;
} else {
return true;
}
} else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
return true; } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) {
Writers.push_back(F->getParent()->getParent());
} else {
return true;
}
return false;
}
bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
std::vector<Value*> AllocRelatedValues;
for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
if (LoadInst *LI = dyn_cast<LoadInst>(*I)) {
std::vector<Function*> ReadersWriters;
if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
return false; } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) {
if (SI->getOperand(0) == GV) return false;
if (isa<ConstantPointerNull>(SI->getOperand(0)))
continue;
Value *Ptr = SI->getOperand(0)->getUnderlyingObject();
if (isa<MallocInst>(Ptr)) {
} else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
Function *F = CI->getCalledFunction();
if (!F || !F->isDeclaration()) return false; if (F->getName() != "calloc") return false; } else {
return false; }
std::vector<Function*> ReadersWriters;
if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
return false;
AllocRelatedValues.push_back(Ptr);
} else {
return false;
}
}
while (!AllocRelatedValues.empty()) {
AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
AllocRelatedValues.pop_back();
}
IndirectGlobals.insert(GV);
return true;
}
void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I != E;
++I) {
std::vector<CallGraphNode *> &SCC = *I;
assert(!SCC.empty() && "SCC with no functions?");
if (!SCC[0]->getFunction()) {
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
FunctionInfo.erase(SCC[i]->getFunction());
continue;
}
FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
bool KnowNothing = false;
unsigned FunctionEffect = 0;
for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
Function *F = SCC[i]->getFunction();
if (!F) {
KnowNothing = true;
break;
}
if (F->isDeclaration()) {
if (F->doesNotAccessMemory()) {
} else if (F->onlyReadsMemory()) {
FunctionEffect |= Ref;
if (!F->isIntrinsic())
FR.MayReadAnyGlobal = true;
} else {
FunctionEffect |= ModRef;
KnowNothing = !F->isIntrinsic();
}
continue;
}
for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
CI != E && !KnowNothing; ++CI)
if (Function *Callee = CI->second->getFunction()) {
if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
FunctionEffect |= CalleeFR->FunctionEffect;
for (std::map<GlobalValue*, unsigned>::iterator GI =
CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
GI != E; ++GI)
FR.GlobalInfo[GI->first] |= GI->second;
FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
} else {
CallGraphNode *CalleeNode = CG[Callee];
if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
KnowNothing = true;
}
} else {
KnowNothing = true;
}
}
if (KnowNothing) {
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
FunctionInfo.erase(SCC[i]->getFunction());
continue;
}
for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
E = inst_end(SCC[i]->getFunction());
II != E && FunctionEffect != ModRef; ++II)
if (isa<LoadInst>(*II)) {
FunctionEffect |= Ref;
if (cast<LoadInst>(*II).isVolatile())
FunctionEffect |= Mod;
} else if (isa<StoreInst>(*II)) {
FunctionEffect |= Mod;
if (cast<StoreInst>(*II).isVolatile())
FunctionEffect |= Ref;
} else if (isa<MallocInst>(*II) || isa<FreeInst>(*II)) {
FunctionEffect |= ModRef;
}
if ((FunctionEffect & Mod) == 0)
++NumReadMemFunctions;
if (FunctionEffect == 0)
++NumNoMemFunctions;
FR.FunctionEffect = FunctionEffect;
for (unsigned i = 1, e = SCC.size(); i != e; ++i)
FunctionInfo[SCC[i]->getFunction()] = FR;
}
}
AliasAnalysis::AliasResult
GlobalsModRef::alias(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size) {
Value *UV1 = const_cast<Value*>(V1->getUnderlyingObject());
Value *UV2 = const_cast<Value*>(V2->getUnderlyingObject());
GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
if (GV1 || GV2) {
if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
if ((GV1 || GV2) && GV1 != GV2)
return NoAlias;
}
GV1 = GV2 = 0;
if (LoadInst *LI = dyn_cast<LoadInst>(UV1))
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
if (IndirectGlobals.count(GV))
GV1 = GV;
if (LoadInst *LI = dyn_cast<LoadInst>(UV2))
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
if (IndirectGlobals.count(GV))
GV2 = GV;
if (AllocsForIndirectGlobals.count(UV1))
GV1 = AllocsForIndirectGlobals[UV1];
if (AllocsForIndirectGlobals.count(UV2))
GV2 = AllocsForIndirectGlobals[UV2];
if ((GV1 || GV2) && GV1 != GV2)
return NoAlias;
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
}
AliasAnalysis::ModRefResult
GlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
unsigned Known = ModRef;
if (GlobalValue *GV = dyn_cast<GlobalValue>(P->getUnderlyingObject()))
if (GV->hasLocalLinkage())
if (Function *F = CS.getCalledFunction())
if (NonAddressTakenGlobals.count(GV))
if (FunctionRecord *FR = getFunctionInfo(F))
Known = FR->getInfoForGlobal(GV);
if (Known == NoModRef)
return NoModRef; return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
}
void GlobalsModRef::deleteValue(Value *V) {
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
if (NonAddressTakenGlobals.erase(GV)) {
if (IndirectGlobals.erase(GV)) {
for (std::map<Value*, GlobalValue*>::iterator
I = AllocsForIndirectGlobals.begin(),
E = AllocsForIndirectGlobals.end(); I != E; ) {
if (I->second == GV) {
AllocsForIndirectGlobals.erase(I++);
} else {
++I;
}
}
}
}
}
AllocsForIndirectGlobals.erase(V);
AliasAnalysis::deleteValue(V);
}
void GlobalsModRef::copyValue(Value *From, Value *To) {
AliasAnalysis::copyValue(From, To);
}