//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This simple pass provides alias and mod/ref information for global values
//
//===----------------------------------------------------------------------===//
-#define DEBUG_TYPE "globalsmodref-aa"
#include "llvm/Analysis/Passes.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Support/InstIterator.h"
-#include "Support/CommandLine.h"
-#include "Support/Debug.h"
-#include "Support/Statistic.h"
-#include "Support/SCCIterator.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/SCCIterator.h"
#include <set>
using namespace llvm;
return I->second;
return 0;
}
-
+
/// FunctionEffect - Capture whether or not this function reads or writes to
/// ANY memory. If not, we can do a lot of aggressive analysis on it.
unsigned FunctionEffect;
+
+ FunctionRecord() : FunctionEffect(0) {}
};
/// GlobalsModRef - The actual analysis pass.
- class GlobalsModRef : public Pass, public AliasAnalysis {
+ class GlobalsModRef : public ModulePass, public AliasAnalysis {
/// NonAddressTakenGlobals - The globals that do not have their addresses
/// taken.
std::set<GlobalValue*> NonAddressTakenGlobals;
std::map<Function*, FunctionRecord> FunctionInfo;
public:
- bool run(Module &M) {
+ bool runOnModule(Module &M) {
InitializeAliasAnalysis(this); // set up super class
AnalyzeGlobals(M); // find non-addr taken globals
AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
//------------------------------------------------
// Implement the AliasAnalysis API
- //
+ //
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; }
- bool doesNotAccessMemory(Function *F) {
+ /// 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) {
if (FunctionRecord *FR = getFunctionInfo(F))
if (FR->FunctionEffect == 0)
- return true;
- return AliasAnalysis::doesNotAccessMemory(F);
+ return DoesNotAccessMemory;
+ else if ((FR->FunctionEffect & Mod) == 0)
+ return OnlyReadsMemory;
+ return AliasAnalysis::getModRefBehavior(F, CS, Info);
}
- bool onlyReadsMemory(Function *F) {
- if (FunctionRecord *FR = getFunctionInfo(F))
- if ((FR->FunctionEffect & Mod) == 0)
- return true;
- return AliasAnalysis::onlyReadsMemory(F);
- }
-
virtual void deleteValue(Value *V);
virtual void copyValue(Value *From, Value *To);
bool AnalyzeUsesOfGlobal(Value *V, std::vector<Function*> &Readers,
std::vector<Function*> &Writers);
};
-
+
RegisterOpt<GlobalsModRef> X("globalsmodref-aa",
"Simple mod/ref analysis for globals");
RegisterAnalysisGroup<AliasAnalysis, GlobalsModRef> Y;
Readers.clear(); Writers.clear();
}
- for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
+ for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+ I != E; ++I)
if (I->hasInternalLinkage()) {
if (!AnalyzeUsesOfGlobal(I, Readers, Writers)) {
// Remember that we are tracking this global, and the mod/ref fns
// passing into the function.
for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
if (CI->getOperand(i) == V) return true;
- } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
- // Make sure that this is just the function being called, not that it is
- // passing into the function.
- 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)) {
// Make sure that this is just the function being called, not that it is
// passing into the function.
return true;
} else {
return true;
- }
+ }
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(*UI)) {
if (AnalyzeUsesOfGlobal(GV, Readers, Writers)) return true;
} else {
/// AnalyzeCallGraph - At this point, we know the functions where globals are
/// immediately stored to and read from. Propagate this information up the call
/// graph to all callers and compute the mod/ref info for all memory for each
-/// function.
+/// function.
void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
- DEBUG(std::cerr << "GlobalsModRef: Analyze Call Graph\n");
-
// We do a bottom-up SCC traversal of the call graph. In other words, we
// visit all callees before callers (leaf-first).
for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I)
- // Do not call AnalyzeSCC on the external function node.
- if ((*I).size() != 1 || (*I)[0]->getFunction())
+ if ((*I).size() != 1) {
AnalyzeSCC(*I);
+ } else if (Function *F = (*I)[0]->getFunction()) {
+ if (!F->isExternal()) {
+ // Nonexternal function.
+ AnalyzeSCC(*I);
+ } else {
+ // Otherwise external function. Handle intrinsics and other special
+ // cases here.
+ if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F))
+ // If it does not access memory, process the function, causing us to
+ // realize it doesn't do anything (the body is empty).
+ AnalyzeSCC(*I);
+ else {
+ // Otherwise, don't process it. This will cause us to conservatively
+ // assume the worst.
+ }
+ }
+ } else {
+ // Do not process the external node, assume the worst.
+ }
}
void GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) {
FR.GlobalInfo[GI->first] |= GI->second;
} else {
- CallsExternal = true;
- break;
+ // Okay, if we can't say anything about it, maybe some other alias
+ // analysis can.
+ ModRefBehavior MRB =
+ AliasAnalysis::getModRefBehavior(Callee, CallSite());
+ if (MRB != DoesNotAccessMemory) {
+ // FIXME: could make this more aggressive for functions that just
+ // read memory. We should just say they read all globals.
+ CallsExternal = true;
+ break;
+ }
}
} else {
CallsExternal = true;
FunctionInfo.erase(SCC[i]->getFunction());
return;
}
-
+
// Otherwise, unless we already know that this function mod/refs memory, scan
// the function bodies to see if there are any explicit loads or stores.
if (FunctionEffect != ModRef) {
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());
+ E = inst_end(SCC[i]->getFunction());
II != E && FunctionEffect != ModRef; ++II)
if (isa<LoadInst>(*II))
FunctionEffect |= Ref;
else if (isa<StoreInst>(*II))
FunctionEffect |= Mod;
+ else if (isa<MallocInst>(*II) || isa<FreeInst>(*II))
+ FunctionEffect |= ModRef;
}
if ((FunctionEffect & Mod) == 0)
// If we are at some type of object... return it.
if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
-
+
// Traverse through different addressing mechanisms...
if (const Instruction *I = dyn_cast<Instruction>(V)) {
if (isa<CastInst>(I) || isa<GetElementPtrInst>(I))