2006-11-14 05:21:04 +00:00
|
|
|
//===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-11-14 05:21:04 +00: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-03 16:50:05 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Pass.h"
|
2006-11-14 05:21:04 +00:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-10-25 06:33:48 +00:00
|
|
|
class AliasDebugger : public ModulePass, public AliasAnalysis {
|
2006-11-14 05:21:04 +00: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 01:11:54 +00:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2010-10-19 17:21:58 +00:00
|
|
|
AliasDebugger() : ModulePass(ID) {
|
|
|
|
initializeAliasDebuggerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnModule(Module &M) override {
|
2015-03-04 18:43:29 +00:00
|
|
|
InitializeAliasAnalysis(this, &M.getDataLayout()); // set up super class
|
2006-11-14 05:21:04 +00:00
|
|
|
|
|
|
|
for(Module::global_iterator I = M.global_begin(),
|
2010-05-28 22:31:51 +00:00
|
|
|
E = M.global_end(); I != E; ++I) {
|
2006-11-14 05:21:04 +00:00
|
|
|
Vals.insert(&*I);
|
2010-05-28 22:31:51 +00:00
|
|
|
for (User::const_op_iterator OI = I->op_begin(),
|
|
|
|
OE = I->op_end(); OI != OE; ++OI)
|
|
|
|
Vals.insert(*OI);
|
|
|
|
}
|
2006-11-14 05:21:04 +00:00
|
|
|
|
|
|
|
for(Module::iterator I = M.begin(),
|
|
|
|
E = M.end(); I != E; ++I){
|
|
|
|
Vals.insert(&*I);
|
2007-01-30 20:08:39 +00:00
|
|
|
if(!I->isDeclaration()) {
|
2006-11-14 05:21:04 +00: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-28 22:31:51 +00:00
|
|
|
BI != BE; ++BI) {
|
2006-11-14 05:21:04 +00:00
|
|
|
Vals.insert(&*BI);
|
2010-05-28 22:31:51 +00:00
|
|
|
for (User::const_op_iterator OI = BI->op_begin(),
|
|
|
|
OE = BI->op_end(); OI != OE; ++OI)
|
|
|
|
Vals.insert(*OI);
|
|
|
|
}
|
2006-11-14 05:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2006-11-14 05:21:04 +00:00
|
|
|
AliasAnalysis::getAnalysisUsage(AU);
|
|
|
|
AU.setPreservesAll(); // Does not transform code
|
|
|
|
}
|
|
|
|
|
2010-01-20 20:09:02 +00: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.
|
2014-03-05 07:30:04 +00:00
|
|
|
void *getAdjustedAnalysisPointer(AnalysisID PI) override {
|
2010-08-06 18:33:48 +00:00
|
|
|
if (PI == &AliasAnalysis::ID)
|
2010-01-20 20:09:02 +00:00
|
|
|
return (AliasAnalysis*)this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2006-11-14 05:21:04 +00:00
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
|
|
|
//
|
2014-03-05 07:30:04 +00:00
|
|
|
AliasResult alias(const Location &LocA, const Location &LocB) override {
|
2010-09-14 21:25:10 +00:00
|
|
|
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 05:21:04 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 21:48:53 +00:00
|
|
|
ModRefResult getModRefInfo(ImmutableCallSite CS,
|
2014-03-05 07:30:04 +00:00
|
|
|
const Location &Loc) override {
|
2010-09-14 21:25:10 +00:00
|
|
|
assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
|
|
|
|
return AliasAnalysis::getModRefInfo(CS, Loc);
|
2006-11-14 05:21:04 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 21:48:53 +00:00
|
|
|
ModRefResult getModRefInfo(ImmutableCallSite CS1,
|
2014-03-05 07:30:04 +00:00
|
|
|
ImmutableCallSite CS2) override {
|
2006-11-14 05:21:04 +00:00
|
|
|
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
|
|
}
|
2014-03-05 07:30:04 +00:00
|
|
|
|
|
|
|
bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override {
|
2010-09-14 21:25:10 +00:00
|
|
|
assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
|
2010-11-08 16:45:26 +00:00
|
|
|
return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
|
2006-11-14 05:21:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void deleteValue(Value *V) override {
|
2006-11-14 05:21:04 +00:00
|
|
|
assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
|
|
|
|
AliasAnalysis::deleteValue(V);
|
|
|
|
}
|
2014-03-05 07:30:04 +00:00
|
|
|
void copyValue(Value *From, Value *To) override {
|
2006-11-14 05:21:04 +00:00
|
|
|
Vals.insert(To);
|
|
|
|
AliasAnalysis::copyValue(From, To);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char AliasDebugger::ID = 0;
|
2010-07-21 23:07:00 +00:00
|
|
|
INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
|
2010-10-07 22:25:06 +00:00
|
|
|
"AA use debugger", false, true, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2006-11-14 05:21:04 +00:00
|
|
|
Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
|
|
|
|
|