2002-10-01 22:34:12 +00:00
|
|
|
//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// 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.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-01 22:34:12 +00:00
|
|
|
//
|
|
|
|
// This pass uses the data structure graphs to implement a simple context
|
|
|
|
// insensitive alias analysis. It does this by computing the local analysis
|
|
|
|
// graphs for all of the functions, then merging them together into a single big
|
|
|
|
// graph without cloning.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-07 06:32:21 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2002-10-01 22:34:12 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2005-01-09 20:42:52 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2002-10-01 22:34:12 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2006-01-22 22:53:01 +00:00
|
|
|
#include <iostream>
|
2003-11-12 23:11:14 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
namespace {
|
2004-09-20 04:48:05 +00:00
|
|
|
class Steens : public ModulePass, public AliasAnalysis {
|
2002-10-01 22:34:12 +00:00
|
|
|
DSGraph *ResultGraph;
|
Create an equivalence class of global variables that DSA will never be able
to tell apart anyway, and only track the leader for of these equivalence
classes in our graphs.
This dramatically reduces the number of GlobalValue*'s that appear in scalar
maps, which A) reduces memory usage, by eliminating many many scalarmap entries
and B) reduces time for operations that need to execute an operation for each
global in the scalar map.
As an example, this reduces the memory used to analyze 176.gcc from 1GB to
511MB, which (while it's still way too much) is better because it doesn't hit
swap anymore. On eon, this shrinks the local graphs from 14MB to 6.8MB,
shrinks the bu+td graphs of povray from 50M to 40M, shrinks the TD graphs of
130.li from 8.8M to 3.6M, etc.
This change also speeds up DSA on large programs where this makes a big
difference. For example, 130.li goes from 1.17s -> 0.56s, 134.perl goes
from 2.14 -> 0.93s, povray goes from 15.63s->7.99s (!!!).
This also apparently either fixes the problem that caused DSA to crash on
perlbmk and gcc, or it hides it, because DSA now works on these. These
both take entirely too much time in the TD pass (147s for perl, 538s for
gcc, vs 7.67/5.9s in the bu pass for either one), but this is a known
problem that I'll deal with later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20696 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-19 22:23:45 +00:00
|
|
|
|
|
|
|
EquivalenceClasses<GlobalValue*> GlobalECs; // Always empty
|
2002-10-01 22:34:12 +00:00
|
|
|
public:
|
2005-04-02 19:17:18 +00:00
|
|
|
Steens() : ResultGraph(0) {}
|
2003-02-13 21:44:18 +00:00
|
|
|
~Steens() {
|
|
|
|
releaseMyMemory();
|
|
|
|
assert(ResultGraph == 0 && "releaseMemory not called?");
|
|
|
|
}
|
2002-10-01 22:34:12 +00:00
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the Pass API
|
|
|
|
//
|
|
|
|
|
|
|
|
// run - Build up the result graph, representing the pointer graph for the
|
|
|
|
// program.
|
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module &M);
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2003-02-13 21:44:18 +00:00
|
|
|
virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
|
2002-10-01 22:34:12 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2003-02-26 19:29:36 +00:00
|
|
|
AliasAnalysis::getAnalysisUsage(AU);
|
2002-10-01 22:34:12 +00:00
|
|
|
AU.setPreservesAll(); // Does not transform code...
|
|
|
|
AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
|
|
|
|
}
|
|
|
|
|
|
|
|
// print - Implement the Pass::print method...
|
|
|
|
void print(std::ostream &O, const Module *M) const {
|
|
|
|
assert(ResultGraph && "Result graph has not yet been computed!");
|
|
|
|
ResultGraph->writeGraphToFile(O, "steensgaards");
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2003-02-26 19:29:36 +00:00
|
|
|
AliasResult alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size);
|
2005-03-26 22:43:20 +00:00
|
|
|
|
|
|
|
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
|
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
private:
|
2002-10-20 18:07:37 +00:00
|
|
|
void ResolveFunctionCall(Function *F, const DSCallSite &Call,
|
2002-10-01 22:34:12 +00:00
|
|
|
DSNodeHandle &RetVal);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register the pass...
|
|
|
|
RegisterOpt<Steens> X("steens-aa",
|
2003-02-08 23:03:17 +00:00
|
|
|
"Steensgaard's alias analysis (DSGraph based)");
|
2002-10-01 22:34:12 +00:00
|
|
|
|
|
|
|
// Register as an implementation of AliasAnalysis
|
|
|
|
RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
|
|
|
|
}
|
|
|
|
|
2005-01-09 20:42:52 +00:00
|
|
|
ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
|
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
|
|
|
|
/// with the specified call site descriptor. This function links the arguments
|
|
|
|
/// and the return value for the call site context-insensitively.
|
|
|
|
///
|
2003-02-04 00:03:37 +00:00
|
|
|
void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
|
2002-10-01 22:34:12 +00:00
|
|
|
DSNodeHandle &RetVal) {
|
|
|
|
assert(ResultGraph != 0 && "Result graph not allocated!");
|
2003-06-30 03:36:09 +00:00
|
|
|
DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2002-10-20 18:07:37 +00:00
|
|
|
// Handle the return value of the function...
|
2002-10-21 02:08:03 +00:00
|
|
|
if (Call.getRetVal().getNode() && RetVal.getNode())
|
|
|
|
RetVal.mergeWith(Call.getRetVal());
|
2002-10-01 22:34:12 +00:00
|
|
|
|
|
|
|
// Loop over all pointer arguments, resolving them to their provided pointers
|
2002-10-20 21:41:02 +00:00
|
|
|
unsigned PtrArgIdx = 0;
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
2003-02-10 18:16:19 +00:00
|
|
|
AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
|
2003-06-30 03:36:09 +00:00
|
|
|
DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
|
2002-10-01 22:34:12 +00:00
|
|
|
if (I != ValMap.end()) // If its a pointer argument...
|
2003-02-04 00:03:37 +00:00
|
|
|
I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
|
2002-10-01 22:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// run - Build up the result graph, representing the pointer graph for the
|
|
|
|
/// program.
|
|
|
|
///
|
2004-09-20 04:48:05 +00:00
|
|
|
bool Steens::runOnModule(Module &M) {
|
2003-02-26 19:29:36 +00:00
|
|
|
InitializeAliasAnalysis(this);
|
2002-10-01 22:34:12 +00:00
|
|
|
assert(ResultGraph == 0 && "Result graph already allocated!");
|
|
|
|
LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
|
|
|
|
|
|
|
|
// Create a new, empty, graph...
|
Create an equivalence class of global variables that DSA will never be able
to tell apart anyway, and only track the leader for of these equivalence
classes in our graphs.
This dramatically reduces the number of GlobalValue*'s that appear in scalar
maps, which A) reduces memory usage, by eliminating many many scalarmap entries
and B) reduces time for operations that need to execute an operation for each
global in the scalar map.
As an example, this reduces the memory used to analyze 176.gcc from 1GB to
511MB, which (while it's still way too much) is better because it doesn't hit
swap anymore. On eon, this shrinks the local graphs from 14MB to 6.8MB,
shrinks the bu+td graphs of povray from 50M to 40M, shrinks the TD graphs of
130.li from 8.8M to 3.6M, etc.
This change also speeds up DSA on large programs where this makes a big
difference. For example, 130.li goes from 1.17s -> 0.56s, 134.perl goes
from 2.14 -> 0.93s, povray goes from 15.63s->7.99s (!!!).
This also apparently either fixes the problem that caused DSA to crash on
perlbmk and gcc, or it hides it, because DSA now works on these. These
both take entirely too much time in the TD pass (147s for perl, 538s for
gcc, vs 7.67/5.9s in the bu pass for either one), but this is a known
problem that I'll deal with later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20696 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-19 22:23:45 +00:00
|
|
|
ResultGraph = new DSGraph(GlobalECs, getTargetData());
|
2005-04-02 19:17:18 +00:00
|
|
|
ResultGraph->spliceFrom(LDS.getGlobalsGraph());
|
2003-02-09 19:26:47 +00:00
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
// Loop over the rest of the module, merging graphs for non-external functions
|
|
|
|
// into this graph.
|
|
|
|
//
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2005-03-22 00:36:51 +00:00
|
|
|
if (!I->isExternal())
|
2005-03-27 21:56:55 +00:00
|
|
|
ResultGraph->spliceFrom(LDS.getDSGraph(*I));
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2005-03-22 00:04:21 +00:00
|
|
|
ResultGraph->removeTriviallyDeadNodes();
|
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
// FIXME: Must recalculate and use the Incomplete markers!!
|
|
|
|
|
|
|
|
// Now that we have all of the graphs inlined, we can go about eliminating
|
|
|
|
// call nodes...
|
|
|
|
//
|
2005-01-30 23:51:02 +00:00
|
|
|
std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
|
2002-11-08 21:27:37 +00:00
|
|
|
assert(Calls.empty() && "Aux call list is already in use??");
|
|
|
|
|
2005-01-30 23:51:02 +00:00
|
|
|
// Start with a copy of the original call sites.
|
2002-11-08 21:27:37 +00:00
|
|
|
Calls = ResultGraph->getFunctionCalls();
|
|
|
|
|
2005-01-30 23:51:02 +00:00
|
|
|
for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
|
|
|
|
CI != E;) {
|
|
|
|
DSCallSite &CurCall = *CI++;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
// Loop over the called functions, eliminating as many as possible...
|
2005-03-20 02:39:49 +00:00
|
|
|
std::vector<Function*> CallTargets;
|
2003-02-05 21:59:58 +00:00
|
|
|
if (CurCall.isDirectCall())
|
|
|
|
CallTargets.push_back(CurCall.getCalleeFunc());
|
2005-04-21 21:13:18 +00:00
|
|
|
else
|
2005-03-20 02:39:49 +00:00
|
|
|
CurCall.getCalleeNode()->addFullFunctionList(CallTargets);
|
2003-02-05 21:59:58 +00:00
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
for (unsigned c = 0; c != CallTargets.size(); ) {
|
|
|
|
// If we can eliminate this function call, do so!
|
2005-03-20 02:39:49 +00:00
|
|
|
Function *F = CallTargets[c];
|
|
|
|
if (!F->isExternal()) {
|
2005-03-22 00:25:52 +00:00
|
|
|
ResolveFunctionCall(F, CurCall, ResultGraph->getReturnNodes()[F]);
|
2003-02-10 00:14:57 +00:00
|
|
|
CallTargets[c] = CallTargets.back();
|
|
|
|
CallTargets.pop_back();
|
|
|
|
} else
|
2002-10-01 22:34:12 +00:00
|
|
|
++c; // Cannot eliminate this call, skip over it...
|
|
|
|
}
|
|
|
|
|
2003-02-10 00:14:57 +00:00
|
|
|
if (CallTargets.empty()) { // Eliminated all calls?
|
2005-01-30 23:51:02 +00:00
|
|
|
std::list<DSCallSite>::iterator I = CI;
|
|
|
|
Calls.erase(--I); // Remove entry
|
|
|
|
}
|
2002-10-01 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
2005-03-29 19:16:59 +00:00
|
|
|
// Remove our knowledge of what the return values of the functions are, except
|
|
|
|
// for functions that are externally visible from this module (e.g. main). We
|
|
|
|
// keep these functions so that their arguments are marked incomplete.
|
|
|
|
for (DSGraph::ReturnNodesTy::iterator I =
|
|
|
|
ResultGraph->getReturnNodes().begin(),
|
|
|
|
E = ResultGraph->getReturnNodes().end(); I != E; )
|
|
|
|
if (I->first->hasInternalLinkage())
|
|
|
|
ResultGraph->getReturnNodes().erase(I++);
|
|
|
|
else
|
|
|
|
++I;
|
2003-02-11 23:11:51 +00:00
|
|
|
|
2002-10-01 22:34:12 +00:00
|
|
|
// Update the "incomplete" markers on the nodes, ignoring unknownness due to
|
|
|
|
// incoming arguments...
|
|
|
|
ResultGraph->maskIncompleteMarkers();
|
2005-03-29 19:16:59 +00:00
|
|
|
ResultGraph->markIncompleteNodes(DSGraph::IgnoreGlobals |
|
|
|
|
DSGraph::MarkFormalArgs);
|
2002-10-01 22:34:12 +00:00
|
|
|
|
|
|
|
// Remove any nodes that are dead after all of the merging we have done...
|
2003-02-04 00:03:37 +00:00
|
|
|
// FIXME: We should be able to disable the globals graph for steens!
|
2005-04-02 19:17:18 +00:00
|
|
|
//ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2003-02-09 18:42:16 +00:00
|
|
|
DEBUG(print(std::cerr, &M));
|
2002-10-01 22:34:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-02-26 19:29:36 +00:00
|
|
|
AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size) {
|
2002-12-12 05:34:10 +00:00
|
|
|
assert(ResultGraph && "Result graph has not been computed yet!");
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2003-06-30 03:36:09 +00:00
|
|
|
DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
|
2002-10-01 22:34:12 +00:00
|
|
|
|
2003-06-30 03:36:09 +00:00
|
|
|
DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
|
2005-03-27 21:56:55 +00:00
|
|
|
DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
|
2005-03-23 01:48:09 +00:00
|
|
|
if (I != GSM.end() && !I->second.isNull() &&
|
2005-03-27 21:56:55 +00:00
|
|
|
J != GSM.end() && !J->second.isNull()) {
|
2002-10-01 22:34:12 +00:00
|
|
|
DSNodeHandle &V1H = I->second;
|
2005-03-27 21:56:55 +00:00
|
|
|
DSNodeHandle &V2H = J->second;
|
|
|
|
|
|
|
|
// If at least one of the nodes is complete, we can say something about
|
|
|
|
// this. If one is complete and the other isn't, then they are obviously
|
|
|
|
// different nodes. If they are both complete, we can't say anything
|
|
|
|
// useful.
|
|
|
|
if (I->second.getNode()->isComplete() ||
|
2005-03-23 01:48:09 +00:00
|
|
|
J->second.getNode()->isComplete()) {
|
2002-10-01 22:34:12 +00:00
|
|
|
// If the two pointers point to different data structure graph nodes, they
|
|
|
|
// cannot alias!
|
2005-03-23 01:48:09 +00:00
|
|
|
if (V1H.getNode() != V2H.getNode())
|
2002-10-01 22:34:12 +00:00
|
|
|
return NoAlias;
|
2003-02-08 23:03:17 +00:00
|
|
|
|
2005-03-23 01:48:09 +00:00
|
|
|
// See if they point to different offsets... if so, we may be able to
|
|
|
|
// determine that they do not alias...
|
|
|
|
unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
|
|
|
|
if (O1 != O2) {
|
|
|
|
if (O2 < O1) { // Ensure that O1 <= O2
|
|
|
|
std::swap(V1, V2);
|
|
|
|
std::swap(O1, O2);
|
|
|
|
std::swap(V1Size, V2Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (O1+V1Size <= O2)
|
|
|
|
return NoAlias;
|
|
|
|
}
|
2002-10-01 22:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we cannot determine alias properties based on our graph, fall back on
|
|
|
|
// some other AA implementation.
|
|
|
|
//
|
2004-05-23 21:13:51 +00:00
|
|
|
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
2002-10-01 22:34:12 +00:00
|
|
|
}
|
2005-03-26 22:43:20 +00:00
|
|
|
|
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
|
|
|
AliasAnalysis::ModRefResult Result = ModRef;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-26 22:43:20 +00:00
|
|
|
// Find the node in question.
|
|
|
|
DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
|
|
|
|
DSGraph::ScalarMapTy::iterator I = GSM.find(P);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-26 22:43:20 +00:00
|
|
|
if (I != GSM.end() && !I->second.isNull()) {
|
|
|
|
DSNode *N = I->second.getNode();
|
|
|
|
if (N->isComplete()) {
|
|
|
|
// If this is a direct call to an external function, and if the pointer
|
|
|
|
// points to a complete node, the external function cannot modify or read
|
|
|
|
// the value (we know it's not passed out of the program!).
|
|
|
|
if (Function *F = CS.getCalledFunction())
|
|
|
|
if (F->isExternal())
|
|
|
|
return NoModRef;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-26 22:43:20 +00:00
|
|
|
// Otherwise, if the node is complete, but it is only M or R, return this.
|
|
|
|
// This can be useful for globals that should be marked const but are not.
|
|
|
|
if (!N->isModified())
|
|
|
|
Result = (ModRefResult)(Result & ~Mod);
|
|
|
|
if (!N->isRead())
|
|
|
|
Result = (ModRefResult)(Result & ~Ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
|
|
|
|
}
|