2004-06-28 14:33:13 +08:00
|
|
|
//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-06-28 14:33:13 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-06-28 14:33:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This simple pass provides alias and mod/ref information for global values
|
2004-07-27 14:40:37 +08:00
|
|
|
// that do not have their address taken, and keeps track of whether functions
|
|
|
|
// read or write memory (are "pure"). For this simple (but very common) case,
|
|
|
|
// we can provide pretty accurate and useful information.
|
2004-06-28 14:33:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 06:30:33 +08:00
|
|
|
#define DEBUG_TYPE "globalsmodref-aa"
|
2004-06-28 14:33:13 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Constants.h"
|
2006-10-02 06:36:45 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-06-28 14:33:13 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2009-10-27 07:58:56 +08:00
|
|
|
#include "llvm/Analysis/MallocFreeHelper.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-02-06 07:42:17 +08:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2004-06-28 14:33:13 +08:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-20 06:30:33 +08:00
|
|
|
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");
|
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
namespace {
|
2004-07-27 14:40:37 +08:00
|
|
|
/// FunctionRecord - One instance of this structure is stored for every
|
|
|
|
/// function in the program. Later, the entries for these functions are
|
|
|
|
/// removed if the function is found to call an external function (in which
|
|
|
|
/// case we know nothing about it.
|
2009-10-25 14:33:48 +08:00
|
|
|
struct FunctionRecord {
|
2004-07-27 14:40:37 +08:00
|
|
|
/// GlobalInfo - Maintain mod/ref info for all of the globals without
|
|
|
|
/// addresses taken that are read or written (transitively) by this
|
|
|
|
/// function.
|
|
|
|
std::map<GlobalValue*, unsigned> GlobalInfo;
|
|
|
|
|
2008-09-12 15:29:58 +08:00
|
|
|
/// MayReadAnyGlobal - May read global variables, but it is not known which.
|
|
|
|
bool MayReadAnyGlobal;
|
|
|
|
|
2004-07-27 14:40:37 +08:00
|
|
|
unsigned getInfoForGlobal(GlobalValue *GV) const {
|
2008-09-12 15:29:58 +08:00
|
|
|
unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
|
2004-07-27 14:40:37 +08:00
|
|
|
std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV);
|
|
|
|
if (I != GlobalInfo.end())
|
2008-09-12 15:29:58 +08:00
|
|
|
Effect |= I->second;
|
|
|
|
return Effect;
|
2004-07-27 14:40:37 +08:00
|
|
|
}
|
2005-04-22 05:13:18 +08:00
|
|
|
|
2004-07-27 14:40:37 +08:00
|
|
|
/// 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;
|
2004-07-27 15:46:26 +08:00
|
|
|
|
2008-09-12 15:29:58 +08:00
|
|
|
FunctionRecord() : MayReadAnyGlobal (false), FunctionEffect(0) {}
|
2004-07-27 14:40:37 +08:00
|
|
|
};
|
2004-06-28 14:33:13 +08:00
|
|
|
|
2004-07-27 14:40:37 +08:00
|
|
|
/// GlobalsModRef - The actual analysis pass.
|
2009-10-25 14:33:48 +08:00
|
|
|
class GlobalsModRef : public ModulePass, public AliasAnalysis {
|
2004-07-27 14:40:37 +08:00
|
|
|
/// NonAddressTakenGlobals - The globals that do not have their addresses
|
|
|
|
/// taken.
|
|
|
|
std::set<GlobalValue*> NonAddressTakenGlobals;
|
2004-06-28 14:33:13 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
/// IndirectGlobals - The memory pointed to by this global is known to be
|
|
|
|
/// 'owned' by the global.
|
|
|
|
std::set<GlobalValue*> IndirectGlobals;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
/// AllocsForIndirectGlobals - If an instruction allocates memory for an
|
|
|
|
/// indirect global, this map indicates which one.
|
|
|
|
std::map<Value*, GlobalValue*> AllocsForIndirectGlobals;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
/// FunctionInfo - For each function, keep track of what globals are
|
|
|
|
/// modified or read.
|
2004-07-27 14:40:37 +08:00
|
|
|
std::map<Function*, FunctionRecord> FunctionInfo;
|
2004-06-28 14:33:13 +08:00
|
|
|
|
|
|
|
public:
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID;
|
2008-09-05 01:05:41 +08:00
|
|
|
GlobalsModRef() : ModulePass(&ID) {}
|
2008-03-18 08:39:19 +08:00
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
bool runOnModule(Module &M) {
|
2004-06-28 14:33:13 +08:00
|
|
|
InitializeAliasAnalysis(this); // set up super class
|
|
|
|
AnalyzeGlobals(M); // find non-addr taken globals
|
|
|
|
AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AliasAnalysis::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<CallGraph>();
|
|
|
|
AU.setPreservesAll(); // Does not transform code
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-06-28 14:33:13 +08:00
|
|
|
AliasResult alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size);
|
|
|
|
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
|
2004-12-07 16:11:24 +08:00
|
|
|
ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
|
|
|
|
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
|
|
}
|
2004-06-28 14:33:13 +08:00
|
|
|
bool hasNoModRefInfoForCalls() const { return false; }
|
|
|
|
|
2004-12-15 15:22:13 +08:00
|
|
|
/// 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.
|
2009-02-06 07:36:27 +08:00
|
|
|
ModRefBehavior getModRefBehavior(Function *F,
|
2004-12-18 01:12:24 +08:00
|
|
|
std::vector<PointerAccessInfo> *Info) {
|
2008-02-20 19:08:44 +08:00
|
|
|
if (FunctionRecord *FR = getFunctionInfo(F)) {
|
2004-07-27 14:40:37 +08:00
|
|
|
if (FR->FunctionEffect == 0)
|
2004-12-15 15:22:13 +08:00
|
|
|
return DoesNotAccessMemory;
|
2005-04-22 12:01:18 +08:00
|
|
|
else if ((FR->FunctionEffect & Mod) == 0)
|
|
|
|
return OnlyReadsMemory;
|
2008-02-20 19:08:44 +08:00
|
|
|
}
|
2009-02-06 07:36:27 +08:00
|
|
|
return AliasAnalysis::getModRefBehavior(F, Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
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);
|
2004-07-27 14:40:37 +08:00
|
|
|
}
|
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
virtual void deleteValue(Value *V);
|
|
|
|
virtual void copyValue(Value *From, Value *To);
|
|
|
|
|
|
|
|
private:
|
2004-07-27 14:40:37 +08:00
|
|
|
/// getFunctionInfo - Return the function info for the function, or null if
|
2008-09-03 20:55:42 +08:00
|
|
|
/// we don't have anything useful to say about it.
|
2004-07-27 14:40:37 +08:00
|
|
|
FunctionRecord *getFunctionInfo(Function *F) {
|
|
|
|
std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F);
|
|
|
|
if (I != FunctionInfo.end())
|
|
|
|
return &I->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
void AnalyzeGlobals(Module &M);
|
|
|
|
void AnalyzeCallGraph(CallGraph &CG, Module &M);
|
2006-10-02 06:36:45 +08:00
|
|
|
bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
|
|
|
|
std::vector<Function*> &Writers,
|
|
|
|
GlobalValue *OkayStoreDest = 0);
|
|
|
|
bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
|
2004-06-28 14:33:13 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char GlobalsModRef::ID = 0;
|
|
|
|
static RegisterPass<GlobalsModRef>
|
|
|
|
X("globalsmodref-aa", "Simple mod/ref analysis for globals", false, true);
|
|
|
|
static RegisterAnalysisGroup<AliasAnalysis> Y(X);
|
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
|
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
/// AnalyzeGlobals - Scan through the users of all of the internal
|
2008-09-03 20:55:42 +08:00
|
|
|
/// GlobalValue's in the program. If none of them have their "address taken"
|
2004-06-28 14:33:13 +08:00
|
|
|
/// (really, their address passed to something nontrivial), record this fact,
|
|
|
|
/// and record the functions that they are used directly in.
|
|
|
|
void GlobalsModRef::AnalyzeGlobals(Module &M) {
|
|
|
|
std::vector<Function*> Readers, Writers;
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2009-01-16 04:18:42 +08:00
|
|
|
if (I->hasLocalLinkage()) {
|
2006-10-02 06:36:45 +08:00
|
|
|
if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
|
2004-07-27 14:40:37 +08:00
|
|
|
// Remember that we are tracking this global.
|
|
|
|
NonAddressTakenGlobals.insert(I);
|
2004-06-28 14:33:13 +08:00
|
|
|
++NumNonAddrTakenFunctions;
|
|
|
|
}
|
|
|
|
Readers.clear(); Writers.clear();
|
|
|
|
}
|
|
|
|
|
2005-03-24 07:51:12 +08:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I)
|
2009-01-16 04:18:42 +08:00
|
|
|
if (I->hasLocalLinkage()) {
|
2006-10-02 06:36:45 +08:00
|
|
|
if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
|
2004-06-28 14:33:13 +08:00
|
|
|
// Remember that we are tracking this global, and the mod/ref fns
|
2004-07-27 14:40:37 +08:00
|
|
|
NonAddressTakenGlobals.insert(I);
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2004-07-27 14:40:37 +08:00
|
|
|
for (unsigned i = 0, e = Readers.size(); i != e; ++i)
|
|
|
|
FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
|
|
|
|
|
|
|
|
if (!I->isConstant()) // No need to keep track of writers to constants
|
|
|
|
for (unsigned i = 0, e = Writers.size(); i != e; ++i)
|
|
|
|
FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
|
2004-06-28 14:33:13 +08:00
|
|
|
++NumNonAddrTakenGlobalVars;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// If this global holds a pointer type, see if it is an indirect global.
|
|
|
|
if (isa<PointerType>(I->getType()->getElementType()) &&
|
|
|
|
AnalyzeIndirectGlobalMemory(I))
|
|
|
|
++NumIndirectGlobalVars;
|
2004-06-28 14:33:13 +08:00
|
|
|
}
|
|
|
|
Readers.clear(); Writers.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
|
|
|
|
/// If this is used by anything complex (i.e., the address escapes), return
|
|
|
|
/// true. Also, while we are at it, keep track of those functions that read and
|
|
|
|
/// write to the value.
|
|
|
|
///
|
|
|
|
/// If OkayStoreDest is non-null, stores into this global are allowed.
|
|
|
|
bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
|
|
|
|
std::vector<Function*> &Readers,
|
|
|
|
std::vector<Function*> &Writers,
|
|
|
|
GlobalValue *OkayStoreDest) {
|
2004-07-27 14:40:37 +08:00
|
|
|
if (!isa<PointerType>(V->getType())) return true;
|
2004-06-28 14:33:13 +08:00
|
|
|
|
|
|
|
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)) {
|
2006-10-02 06:36:45 +08:00
|
|
|
if (V == SI->getOperand(1)) {
|
|
|
|
Writers.push_back(SI->getParent()->getParent());
|
|
|
|
} else if (SI->getOperand(1) != OkayStoreDest) {
|
|
|
|
return true; // Storing the pointer
|
|
|
|
}
|
2004-06-28 14:33:13 +08:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
|
2006-10-02 06:36:45 +08:00
|
|
|
if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
|
2009-09-19 05:34:51 +08:00
|
|
|
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) {
|
|
|
|
if (AnalyzeUsesOfPointer(BCI, Readers, Writers, OkayStoreDest))
|
|
|
|
return true;
|
2009-10-27 07:43:48 +08:00
|
|
|
} else if (isFreeCall(*UI)) {
|
2009-10-24 12:23:03 +08:00
|
|
|
Writers.push_back(cast<Instruction>(*UI)->getParent()->getParent());
|
2004-06-28 14:33:13 +08:00
|
|
|
} 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.
|
2009-09-03 10:02:59 +08:00
|
|
|
for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
|
2004-06-28 14:33:13 +08:00
|
|
|
if (II->getOperand(i) == V) return true;
|
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
|
2008-09-03 20:55:42 +08:00
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr ||
|
2006-11-27 09:05:10 +08:00
|
|
|
CE->getOpcode() == Instruction::BitCast) {
|
2006-10-02 06:36:45 +08:00
|
|
|
if (AnalyzeUsesOfPointer(CE, Readers, Writers))
|
2004-06-28 14:33:13 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
2005-04-22 05:13:18 +08:00
|
|
|
}
|
2006-12-23 14:05:41 +08:00
|
|
|
} else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
|
|
|
|
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
|
2006-10-02 06:36:45 +08:00
|
|
|
return true; // Allow comparison against null.
|
2004-06-28 14:33:13 +08:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
|
|
|
|
/// which holds a pointer type. See if the global always points to non-aliased
|
|
|
|
/// heap memory: that is, all initializers of the globals are allocations, and
|
|
|
|
/// those allocations have no use other than initialization of the global.
|
|
|
|
/// Further, all loads out of GV must directly use the memory, not store the
|
|
|
|
/// pointer somewhere. If this is true, we consider the memory pointed to by
|
|
|
|
/// GV to be owned by GV and can disambiguate other pointers from it.
|
|
|
|
bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
|
|
|
|
// Keep track of values related to the allocation of the memory, f.e. the
|
|
|
|
// value produced by the malloc call and any casts.
|
|
|
|
std::vector<Value*> AllocRelatedValues;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// Walk the user list of the global. If we find anything other than a direct
|
|
|
|
// load or store, bail out.
|
|
|
|
for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(*I)) {
|
|
|
|
// The pointer loaded from the global can only be used in simple ways:
|
|
|
|
// we allow addressing of it and loading storing to it. We do *not* allow
|
|
|
|
// storing the loaded pointer somewhere else or passing to a function.
|
|
|
|
std::vector<Function*> ReadersWriters;
|
|
|
|
if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
|
|
|
|
return false; // Loaded pointer escapes.
|
|
|
|
// TODO: Could try some IP mod/ref of the loaded pointer.
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) {
|
|
|
|
// Storing the global itself.
|
|
|
|
if (SI->getOperand(0) == GV) return false;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// If storing the null pointer, ignore it.
|
|
|
|
if (isa<ConstantPointerNull>(SI->getOperand(0)))
|
|
|
|
continue;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// Check the value being stored.
|
2008-10-01 23:25:41 +08:00
|
|
|
Value *Ptr = SI->getOperand(0)->getUnderlyingObject();
|
2006-10-02 06:36:45 +08:00
|
|
|
|
2009-10-17 09:18:07 +08:00
|
|
|
if (isMalloc(Ptr)) {
|
2006-10-02 06:36:45 +08:00
|
|
|
// Okay, easy case.
|
|
|
|
} else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
|
|
|
|
Function *F = CI->getCalledFunction();
|
2007-01-31 04:08:39 +08:00
|
|
|
if (!F || !F->isDeclaration()) return false; // Too hard to analyze.
|
2006-10-02 06:36:45 +08:00
|
|
|
if (F->getName() != "calloc") return false; // Not calloc.
|
|
|
|
} else {
|
|
|
|
return false; // Too hard to analyze.
|
|
|
|
}
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// Analyze all uses of the allocation. If any of them are used in a
|
|
|
|
// non-simple way (e.g. stored to another global) bail out.
|
|
|
|
std::vector<Function*> ReadersWriters;
|
|
|
|
if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
|
|
|
|
return false; // Loaded pointer escapes.
|
|
|
|
|
|
|
|
// Remember that this allocation is related to the indirect global.
|
|
|
|
AllocRelatedValues.push_back(Ptr);
|
|
|
|
} else {
|
|
|
|
// Something complex, bail out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// Okay, this is an indirect global. Remember all of the allocations for
|
|
|
|
// this global in AllocsForIndirectGlobals.
|
|
|
|
while (!AllocRelatedValues.empty()) {
|
|
|
|
AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
|
|
|
|
AllocRelatedValues.pop_back();
|
|
|
|
}
|
|
|
|
IndirectGlobals.insert(GV);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
/// AnalyzeCallGraph - At this point, we know the functions where globals are
|
|
|
|
/// immediately stored to and read from. Propagate this information up the call
|
2004-07-27 14:40:37 +08:00
|
|
|
/// graph to all callers and compute the mod/ref info for all memory for each
|
2005-04-22 05:13:18 +08:00
|
|
|
/// function.
|
2004-06-28 14:33:13 +08:00
|
|
|
void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
|
|
|
|
// We do a bottom-up SCC traversal of the call graph. In other words, we
|
|
|
|
// visit all callees before callers (leaf-first).
|
2008-09-03 20:55:42 +08:00
|
|
|
for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I != E;
|
|
|
|
++I) {
|
|
|
|
std::vector<CallGraphNode *> &SCC = *I;
|
2008-09-05 03:16:20 +08:00
|
|
|
assert(!SCC.empty() && "SCC with no functions?");
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2008-09-05 03:16:20 +08:00
|
|
|
if (!SCC[0]->getFunction()) {
|
|
|
|
// Calls externally - can't say anything useful. Remove any existing
|
|
|
|
// function records (may have been created when scanning globals).
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
|
|
|
|
FunctionInfo.erase(SCC[i]->getFunction());
|
2008-09-04 03:37:16 +08:00
|
|
|
continue;
|
2008-09-05 03:16:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
|
2004-06-28 14:33:13 +08:00
|
|
|
|
2008-09-03 20:55:42 +08:00
|
|
|
bool KnowNothing = false;
|
|
|
|
unsigned FunctionEffect = 0;
|
|
|
|
|
|
|
|
// Collect the mod/ref properties due to called functions. We only compute
|
|
|
|
// one mod-ref set.
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
|
|
|
|
Function *F = SCC[i]->getFunction();
|
|
|
|
if (!F) {
|
|
|
|
KnowNothing = true;
|
2004-07-27 14:40:37 +08:00
|
|
|
break;
|
2004-06-28 14:33:13 +08:00
|
|
|
}
|
2004-07-27 14:40:37 +08:00
|
|
|
|
2008-09-03 20:55:42 +08:00
|
|
|
if (F->isDeclaration()) {
|
|
|
|
// Try to get mod/ref behaviour from function attributes.
|
2008-09-03 23:31:24 +08:00
|
|
|
if (F->doesNotAccessMemory()) {
|
|
|
|
// Can't do better than that!
|
|
|
|
} else if (F->onlyReadsMemory()) {
|
2008-09-03 20:55:42 +08:00
|
|
|
FunctionEffect |= Ref;
|
2008-09-12 15:29:58 +08:00
|
|
|
if (!F->isIntrinsic())
|
2008-09-11 23:43:12 +08:00
|
|
|
// This function might call back into the module and read a global -
|
2008-09-12 15:29:58 +08:00
|
|
|
// consider every global as possibly being read by this function.
|
|
|
|
FR.MayReadAnyGlobal = true;
|
2008-09-03 23:31:24 +08:00
|
|
|
} else {
|
2008-09-12 03:35:55 +08:00
|
|
|
FunctionEffect |= ModRef;
|
|
|
|
// Can't say anything useful unless it's an intrinsic - they don't
|
|
|
|
// read or write global variables of the kind considered here.
|
|
|
|
KnowNothing = !F->isIntrinsic();
|
2008-09-03 20:55:42 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
|
2008-09-05 03:16:20 +08:00
|
|
|
CI != E && !KnowNothing; ++CI)
|
2008-09-03 20:55:42 +08:00
|
|
|
if (Function *Callee = CI->second->getFunction()) {
|
|
|
|
if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
|
|
|
|
// Propagate function effect up.
|
|
|
|
FunctionEffect |= CalleeFR->FunctionEffect;
|
|
|
|
|
|
|
|
// Incorporate callee's effects on globals into our info.
|
|
|
|
for (std::map<GlobalValue*, unsigned>::iterator GI =
|
|
|
|
CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
|
|
|
|
GI != E; ++GI)
|
2008-09-05 03:16:20 +08:00
|
|
|
FR.GlobalInfo[GI->first] |= GI->second;
|
2008-09-12 15:29:58 +08:00
|
|
|
FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
|
2008-09-03 20:55:42 +08:00
|
|
|
} else {
|
|
|
|
// Can't say anything about it. However, if it is inside our SCC,
|
|
|
|
// then nothing needs to be done.
|
|
|
|
CallGraphNode *CalleeNode = CG[Callee];
|
|
|
|
if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
|
|
|
|
KnowNothing = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
KnowNothing = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't say anything useful about this SCC, remove all SCC functions
|
|
|
|
// from the FunctionInfo map.
|
|
|
|
if (KnowNothing) {
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
|
|
|
|
FunctionInfo.erase(SCC[i]->getFunction());
|
2008-09-04 00:10:55 +08:00
|
|
|
continue;
|
2008-09-03 20:55:42 +08:00
|
|
|
}
|
2005-04-22 05:13:18 +08:00
|
|
|
|
2008-09-03 20:55:42 +08:00
|
|
|
// Scan the function bodies for explicit loads or stores.
|
2004-07-27 14:40:37 +08:00
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
|
|
|
|
for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
|
2005-04-22 05:13:18 +08:00
|
|
|
E = inst_end(SCC[i]->getFunction());
|
2004-07-27 14:40:37 +08:00
|
|
|
II != E && FunctionEffect != ModRef; ++II)
|
2008-09-13 20:45:50 +08:00
|
|
|
if (isa<LoadInst>(*II)) {
|
2004-07-27 14:40:37 +08:00
|
|
|
FunctionEffect |= Ref;
|
2008-09-13 20:45:50 +08:00
|
|
|
if (cast<LoadInst>(*II).isVolatile())
|
|
|
|
// Volatile loads may have side-effects, so mark them as writing
|
|
|
|
// memory (for example, a flag inside the processor).
|
|
|
|
FunctionEffect |= Mod;
|
|
|
|
} else if (isa<StoreInst>(*II)) {
|
2004-07-27 14:40:37 +08:00
|
|
|
FunctionEffect |= Mod;
|
2008-09-13 20:45:50 +08:00
|
|
|
if (cast<StoreInst>(*II).isVolatile())
|
|
|
|
// Treat volatile stores as reading memory somewhere.
|
|
|
|
FunctionEffect |= Ref;
|
2009-10-27 07:43:48 +08:00
|
|
|
} else if (isMalloc(&cast<Instruction>(*II)) ||
|
2009-10-24 12:23:03 +08:00
|
|
|
isFreeCall(&cast<Instruction>(*II))) {
|
2005-04-22 13:36:59 +08:00
|
|
|
FunctionEffect |= ModRef;
|
2008-09-13 20:45:50 +08:00
|
|
|
}
|
2004-07-27 14:40:37 +08:00
|
|
|
|
2008-09-03 20:55:42 +08:00
|
|
|
if ((FunctionEffect & Mod) == 0)
|
|
|
|
++NumReadMemFunctions;
|
|
|
|
if (FunctionEffect == 0)
|
|
|
|
++NumNoMemFunctions;
|
2008-09-05 03:16:20 +08:00
|
|
|
FR.FunctionEffect = FunctionEffect;
|
2004-07-27 14:40:37 +08:00
|
|
|
|
2008-09-03 20:55:42 +08:00
|
|
|
// Finally, now that we know the full effect on this SCC, clone the
|
|
|
|
// information to each function in the SCC.
|
|
|
|
for (unsigned i = 1, e = SCC.size(); i != e; ++i)
|
2008-09-05 03:16:20 +08:00
|
|
|
FunctionInfo[SCC[i]->getFunction()] = FR;
|
2008-09-03 20:55:42 +08:00
|
|
|
}
|
2004-06-28 14:33:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// alias - If one of the pointers is to a global that we are tracking, and the
|
|
|
|
/// other is some random pointer, we know there cannot be an alias, because the
|
|
|
|
/// address of the global isn't taken.
|
|
|
|
AliasAnalysis::AliasResult
|
|
|
|
GlobalsModRef::alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size) {
|
2006-10-02 06:36:45 +08:00
|
|
|
// Get the base object these pointers point to.
|
2008-10-01 23:25:41 +08:00
|
|
|
Value *UV1 = const_cast<Value*>(V1->getUnderlyingObject());
|
|
|
|
Value *UV2 = const_cast<Value*>(V2->getUnderlyingObject());
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// If either of the underlying values is a global, they may be non-addr-taken
|
|
|
|
// globals, which we can answer queries about.
|
|
|
|
GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
|
|
|
|
GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
|
|
|
|
if (GV1 || GV2) {
|
|
|
|
// If the global's address is taken, pretend we don't know it's a pointer to
|
|
|
|
// the global.
|
|
|
|
if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
|
|
|
|
if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
|
|
|
|
|
|
|
|
// If the the two pointers are derived from two different non-addr-taken
|
|
|
|
// globals, or if one is and the other isn't, we know these can't alias.
|
|
|
|
if ((GV1 || GV2) && GV1 != GV2)
|
|
|
|
return NoAlias;
|
|
|
|
|
|
|
|
// Otherwise if they are both derived from the same addr-taken global, we
|
|
|
|
// can't know the two accesses don't overlap.
|
|
|
|
}
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// These pointers may be based on the memory owned by an indirect global. If
|
|
|
|
// so, we may be able to handle this. First check to see if the base pointer
|
|
|
|
// is a direct load from an indirect global.
|
|
|
|
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;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// These pointers may also be from an allocation for the indirect global. If
|
|
|
|
// so, also handle them.
|
|
|
|
if (AllocsForIndirectGlobals.count(UV1))
|
|
|
|
GV1 = AllocsForIndirectGlobals[UV1];
|
|
|
|
if (AllocsForIndirectGlobals.count(UV2))
|
|
|
|
GV2 = AllocsForIndirectGlobals[UV2];
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// Now that we know whether the two pointers are related to indirect globals,
|
|
|
|
// use this to disambiguate the pointers. If either pointer is based on an
|
|
|
|
// indirect global and if they are not both based on the same indirect global,
|
|
|
|
// they cannot alias.
|
2004-06-28 14:33:13 +08:00
|
|
|
if ((GV1 || GV2) && GV1 != GV2)
|
|
|
|
return NoAlias;
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2004-06-28 14:33:13 +08:00
|
|
|
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
GlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
|
|
|
unsigned Known = ModRef;
|
|
|
|
|
|
|
|
// If we are asking for mod/ref info of a direct call with a pointer to a
|
2004-07-27 14:40:37 +08:00
|
|
|
// global we are tracking, return information if we have it.
|
2008-10-01 23:25:41 +08:00
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(P->getUnderlyingObject()))
|
2009-01-16 04:18:42 +08:00
|
|
|
if (GV->hasLocalLinkage())
|
2004-07-27 14:40:37 +08:00
|
|
|
if (Function *F = CS.getCalledFunction())
|
|
|
|
if (NonAddressTakenGlobals.count(GV))
|
|
|
|
if (FunctionRecord *FR = getFunctionInfo(F))
|
|
|
|
Known = FR->getInfoForGlobal(GV);
|
2004-06-28 14:33:13 +08:00
|
|
|
|
|
|
|
if (Known == NoModRef)
|
|
|
|
return NoModRef; // No need to query other mod/ref analyses
|
|
|
|
return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods to update the analysis as a result of the client transformation.
|
|
|
|
//
|
|
|
|
void GlobalsModRef::deleteValue(Value *V) {
|
2006-10-02 06:36:45 +08:00
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
|
|
|
if (NonAddressTakenGlobals.erase(GV)) {
|
|
|
|
// This global might be an indirect global. If so, remove it and remove
|
|
|
|
// any AllocRelatedValues for it.
|
|
|
|
if (IndirectGlobals.erase(GV)) {
|
|
|
|
// Remove any entries in AllocsForIndirectGlobals for this global.
|
|
|
|
for (std::map<Value*, GlobalValue*>::iterator
|
|
|
|
I = AllocsForIndirectGlobals.begin(),
|
|
|
|
E = AllocsForIndirectGlobals.end(); I != E; ) {
|
|
|
|
if (I->second == GV) {
|
|
|
|
AllocsForIndirectGlobals.erase(I++);
|
|
|
|
} else {
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2006-10-02 06:36:45 +08:00
|
|
|
// Otherwise, if this is an allocation related to an indirect global, remove
|
|
|
|
// it.
|
|
|
|
AllocsForIndirectGlobals.erase(V);
|
2008-09-03 20:55:42 +08:00
|
|
|
|
2007-12-01 02:52:58 +08:00
|
|
|
AliasAnalysis::deleteValue(V);
|
2004-06-28 14:33:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalsModRef::copyValue(Value *From, Value *To) {
|
2007-12-01 02:52:58 +08:00
|
|
|
AliasAnalysis::copyValue(From, To);
|
2004-06-28 14:33:13 +08:00
|
|
|
}
|