2006-11-14 13:21:04 +08:00
|
|
|
//===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2006-11-14 13:21:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This simple pass checks alias analysis users to ensure that if they
|
|
|
|
// create a new value, they do not query AA without informing it of the value.
|
|
|
|
// It acts as a shim over any other AA pass you want.
|
|
|
|
//
|
|
|
|
// Yes keeping track of every value in the program is expensive, but this is
|
|
|
|
// a debugging pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2006-11-14 13:21:04 +08:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-10-25 14:33:48 +08:00
|
|
|
class AliasDebugger : public ModulePass, public AliasAnalysis {
|
2006-11-14 13:21:04 +08:00
|
|
|
|
|
|
|
//What we do is simple. Keep track of every value the AA could
|
|
|
|
//know about, and verify that queries are one of those.
|
|
|
|
//A query to a value that didn't exist when the AA was created
|
|
|
|
//means someone forgot to update the AA when creating new values
|
|
|
|
|
|
|
|
std::set<const Value*> Vals;
|
|
|
|
|
|
|
|
public:
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2010-10-20 01:21:58 +08:00
|
|
|
AliasDebugger() : ModulePass(ID) {
|
|
|
|
initializeAliasDebuggerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-03-18 08:39:19 +08:00
|
|
|
|
2006-11-14 13:21:04 +08:00
|
|
|
bool runOnModule(Module &M) {
|
|
|
|
InitializeAliasAnalysis(this); // set up super class
|
|
|
|
|
|
|
|
for(Module::global_iterator I = M.global_begin(),
|
2010-05-29 06:31:51 +08:00
|
|
|
E = M.global_end(); I != E; ++I) {
|
2006-11-14 13:21:04 +08:00
|
|
|
Vals.insert(&*I);
|
2010-05-29 06:31:51 +08:00
|
|
|
for (User::const_op_iterator OI = I->op_begin(),
|
|
|
|
OE = I->op_end(); OI != OE; ++OI)
|
|
|
|
Vals.insert(*OI);
|
|
|
|
}
|
2006-11-14 13:21:04 +08:00
|
|
|
|
|
|
|
for(Module::iterator I = M.begin(),
|
|
|
|
E = M.end(); I != E; ++I){
|
|
|
|
Vals.insert(&*I);
|
2007-01-31 04:08:39 +08:00
|
|
|
if(!I->isDeclaration()) {
|
2006-11-14 13:21:04 +08:00
|
|
|
for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
|
|
|
|
AI != AE; ++AI)
|
|
|
|
Vals.insert(&*AI);
|
|
|
|
for (Function::const_iterator FI = I->begin(), FE = I->end();
|
|
|
|
FI != FE; ++FI)
|
|
|
|
for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
|
2010-05-29 06:31:51 +08:00
|
|
|
BI != BE; ++BI) {
|
2006-11-14 13:21:04 +08:00
|
|
|
Vals.insert(&*BI);
|
2010-05-29 06:31:51 +08:00
|
|
|
for (User::const_op_iterator OI = BI->op_begin(),
|
|
|
|
OE = BI->op_end(); OI != OE; ++OI)
|
|
|
|
Vals.insert(*OI);
|
|
|
|
}
|
2006-11-14 13:21:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AliasAnalysis::getAnalysisUsage(AU);
|
|
|
|
AU.setPreservesAll(); // Does not transform code
|
|
|
|
}
|
|
|
|
|
2010-01-21 04:09:02 +08:00
|
|
|
/// getAdjustedAnalysisPointer - This method is used when a pass implements
|
|
|
|
/// an analysis interface through multiple inheritance. If needed, it
|
|
|
|
/// should override this to adjust the this pointer as needed for the
|
|
|
|
/// specified pass info.
|
2010-08-07 02:33:48 +08:00
|
|
|
virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
|
|
|
|
if (PI == &AliasAnalysis::ID)
|
2010-01-21 04:09:02 +08:00
|
|
|
return (AliasAnalysis*)this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2006-11-14 13:21:04 +08:00
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
|
|
|
//
|
2010-09-15 05:25:10 +08:00
|
|
|
AliasResult alias(const Location &LocA, const Location &LocB) {
|
|
|
|
assert(Vals.find(LocA.Ptr) != Vals.end() &&
|
|
|
|
"Never seen value in AA before");
|
|
|
|
assert(Vals.find(LocB.Ptr) != Vals.end() &&
|
|
|
|
"Never seen value in AA before");
|
|
|
|
return AliasAnalysis::alias(LocA, LocB);
|
2006-11-14 13:21:04 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 05:48:53 +08:00
|
|
|
ModRefResult getModRefInfo(ImmutableCallSite CS,
|
2010-09-15 05:25:10 +08:00
|
|
|
const Location &Loc) {
|
|
|
|
assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
|
|
|
|
return AliasAnalysis::getModRefInfo(CS, Loc);
|
2006-11-14 13:21:04 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 05:48:53 +08:00
|
|
|
ModRefResult getModRefInfo(ImmutableCallSite CS1,
|
|
|
|
ImmutableCallSite CS2) {
|
2006-11-14 13:21:04 +08:00
|
|
|
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
|
|
}
|
|
|
|
|
2010-11-09 00:45:26 +08:00
|
|
|
bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
|
2010-09-15 05:25:10 +08:00
|
|
|
assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
|
2010-11-09 00:45:26 +08:00
|
|
|
return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
|
2006-11-14 13:21:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void deleteValue(Value *V) {
|
|
|
|
assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
|
|
|
|
AliasAnalysis::deleteValue(V);
|
|
|
|
}
|
|
|
|
virtual void copyValue(Value *From, Value *To) {
|
|
|
|
Vals.insert(To);
|
|
|
|
AliasAnalysis::copyValue(From, To);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char AliasDebugger::ID = 0;
|
2010-07-22 07:07:00 +08:00
|
|
|
INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
|
2010-10-08 06:25:06 +08:00
|
|
|
"AA use debugger", false, true, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2006-11-14 13:21:04 +08:00
|
|
|
Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
|
|
|
|
|