2002-10-01 22:34:45 +00:00
|
|
|
//===- DataStructure.h - Build data structure graphs ------------*- C++ -*-===//
|
2003-10-20 20:19:47 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-26 22:38:45 +00:00
|
|
|
//
|
|
|
|
// Implement the LLVM data structure analysis library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_DATA_STRUCTURE_H
|
|
|
|
#define LLVM_ANALYSIS_DATA_STRUCTURE_H
|
|
|
|
|
|
|
|
#include "llvm/Pass.h"
|
2003-11-02 22:27:28 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2005-01-09 04:18:28 +00:00
|
|
|
#include "llvm/ADT/hash_map"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/hash_set"
|
2005-03-19 22:12:33 +00:00
|
|
|
#include "llvm/ADT/EquivalenceClasses.h"
|
2002-03-26 22:38:45 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-03-26 22:38:45 +00:00
|
|
|
class Type;
|
2003-09-20 16:34:13 +00:00
|
|
|
class Instruction;
|
2005-03-19 22:12:33 +00:00
|
|
|
class GlobalValue;
|
2002-10-02 22:14:17 +00:00
|
|
|
class DSGraph;
|
2005-03-21 04:46:35 +00:00
|
|
|
class DSCallSite;
|
2002-10-21 19:47:18 +00:00
|
|
|
class DSNode;
|
2005-02-04 19:57:57 +00:00
|
|
|
class DSNodeHandle;
|
2002-07-10 22:42:17 +00:00
|
|
|
|
2002-10-01 22:34:45 +00:00
|
|
|
// FIXME: move this stuff to a private header
|
|
|
|
namespace DataStructureAnalysis {
|
2004-03-11 23:08:20 +00:00
|
|
|
/// isPointerType - Return true if this first class type is big enough to hold
|
|
|
|
/// a pointer.
|
|
|
|
///
|
2002-10-01 22:34:45 +00:00
|
|
|
bool isPointerType(const Type *Ty);
|
2002-07-10 22:42:17 +00:00
|
|
|
}
|
2002-03-26 22:38:45 +00:00
|
|
|
|
|
|
|
|
2002-07-10 22:42:17 +00:00
|
|
|
// LocalDataStructures - The analysis that computes the local data structure
|
|
|
|
// graphs for all of the functions in the program.
|
2002-03-26 22:38:45 +00:00
|
|
|
//
|
2002-07-18 00:11:28 +00:00
|
|
|
// FIXME: This should be a Function pass that can be USED by a Pass, and would
|
|
|
|
// be automatically preserved. Until we can do that, this is a Pass.
|
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
class LocalDataStructures : public ModulePass {
|
2002-07-10 22:42:17 +00:00
|
|
|
// DSInfo, one graph for each function
|
2003-06-30 05:10:09 +00:00
|
|
|
hash_map<Function*, DSGraph*> DSInfo;
|
2002-11-09 20:01:01 +00:00
|
|
|
DSGraph *GlobalsGraph;
|
2005-03-19 22:12:33 +00:00
|
|
|
|
|
|
|
/// GlobalECs - The equivalence classes for each global value that is merged
|
|
|
|
/// with other global values in the DSGraphs.
|
|
|
|
EquivalenceClasses<GlobalValue*> GlobalECs;
|
2002-03-26 22:38:45 +00:00
|
|
|
public:
|
2002-07-10 22:42:17 +00:00
|
|
|
~LocalDataStructures() { releaseMemory(); }
|
2002-03-26 22:38:45 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
2002-03-26 22:38:45 +00:00
|
|
|
|
2002-11-10 06:53:19 +00:00
|
|
|
bool hasGraph(const Function &F) const {
|
2003-06-30 05:10:09 +00:00
|
|
|
return DSInfo.find(const_cast<Function*>(&F)) != DSInfo.end();
|
2002-11-10 06:53:19 +00:00
|
|
|
}
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getDSGraph - Return the data structure graph for the specified function.
|
|
|
|
///
|
2002-07-30 22:02:04 +00:00
|
|
|
DSGraph &getDSGraph(const Function &F) const {
|
2003-06-30 05:10:09 +00:00
|
|
|
hash_map<Function*, DSGraph*>::const_iterator I =
|
|
|
|
DSInfo.find(const_cast<Function*>(&F));
|
2002-07-10 22:42:17 +00:00
|
|
|
assert(I != DSInfo.end() && "Function not in module!");
|
|
|
|
return *I->second;
|
2002-03-29 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
DSGraph &getGlobalsGraph() const { return *GlobalsGraph; }
|
|
|
|
|
2005-03-19 22:12:33 +00:00
|
|
|
EquivalenceClasses<GlobalValue*> &getGlobalECs() { return GlobalECs; }
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// print - Print out the analysis results...
|
|
|
|
///
|
2002-07-27 01:12:15 +00:00
|
|
|
void print(std::ostream &O, const Module *M) const;
|
2002-03-26 22:38:45 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// releaseMemory - if the pass pipeline is done with this pass, we can
|
|
|
|
/// release our memory...
|
|
|
|
///
|
2002-03-26 22:38:45 +00:00
|
|
|
virtual void releaseMemory();
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getAnalysisUsage - This obviously provides a data structure graph.
|
|
|
|
///
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2003-11-02 22:27:28 +00:00
|
|
|
AU.addRequired<TargetData>();
|
2002-03-26 22:38:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-11-09 19:21:56 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// BUDataStructures - The analysis that computes the interprocedurally closed
|
|
|
|
/// data structure graphs for all of the functions in the program. This pass
|
|
|
|
/// only performs a "Bottom Up" propagation (hence the name).
|
|
|
|
///
|
2004-09-20 04:48:05 +00:00
|
|
|
class BUDataStructures : public ModulePass {
|
2003-11-13 01:42:38 +00:00
|
|
|
protected:
|
2002-07-18 00:11:28 +00:00
|
|
|
// DSInfo, one graph for each function
|
2003-06-30 05:10:09 +00:00
|
|
|
hash_map<Function*, DSGraph*> DSInfo;
|
2002-11-09 21:12:07 +00:00
|
|
|
DSGraph *GlobalsGraph;
|
2003-09-20 16:34:13 +00:00
|
|
|
hash_multimap<Instruction*, Function*> ActualCallees;
|
2005-02-04 19:57:57 +00:00
|
|
|
|
|
|
|
// This map is only maintained during construction of BU Graphs
|
|
|
|
std::map<std::vector<Function*>,
|
2005-02-07 16:09:01 +00:00
|
|
|
std::pair<DSGraph*, std::vector<DSNodeHandle> > > *IndCallGraphMap;
|
2005-03-19 22:12:33 +00:00
|
|
|
|
|
|
|
/// GlobalECs - The equivalence classes for each global value that is merged
|
|
|
|
/// with other global values in the DSGraphs.
|
|
|
|
EquivalenceClasses<GlobalValue*> GlobalECs;
|
2002-07-18 00:11:28 +00:00
|
|
|
public:
|
2005-03-23 21:59:34 +00:00
|
|
|
~BUDataStructures() { releaseMyMemory(); }
|
2002-07-18 00:11:28 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
2002-07-18 00:11:28 +00:00
|
|
|
|
2002-11-10 06:53:19 +00:00
|
|
|
bool hasGraph(const Function &F) const {
|
2003-06-30 05:10:09 +00:00
|
|
|
return DSInfo.find(const_cast<Function*>(&F)) != DSInfo.end();
|
2002-11-10 06:53:19 +00:00
|
|
|
}
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getDSGraph - Return the data structure graph for the specified function.
|
|
|
|
///
|
2002-07-30 22:02:04 +00:00
|
|
|
DSGraph &getDSGraph(const Function &F) const {
|
2003-06-30 05:10:09 +00:00
|
|
|
hash_map<Function*, DSGraph*>::const_iterator I =
|
|
|
|
DSInfo.find(const_cast<Function*>(&F));
|
2002-07-18 00:11:28 +00:00
|
|
|
assert(I != DSInfo.end() && "Function not in module!");
|
|
|
|
return *I->second;
|
|
|
|
}
|
2002-07-30 22:02:04 +00:00
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
DSGraph &getGlobalsGraph() const { return *GlobalsGraph; }
|
|
|
|
|
2005-03-19 22:12:33 +00:00
|
|
|
EquivalenceClasses<GlobalValue*> &getGlobalECs() { return GlobalECs; }
|
|
|
|
|
|
|
|
|
2005-01-24 19:55:34 +00:00
|
|
|
/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
|
|
|
|
/// These correspond to the interfaces defined in the AliasAnalysis class.
|
|
|
|
void deleteValue(Value *V);
|
|
|
|
void copyValue(Value *From, Value *To);
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// print - Print out the analysis results...
|
|
|
|
///
|
2002-07-27 01:12:15 +00:00
|
|
|
void print(std::ostream &O, const Module *M) const;
|
2002-07-18 00:11:28 +00:00
|
|
|
|
2005-03-23 21:59:34 +00:00
|
|
|
// FIXME: Once the pass manager is straightened out, rename this to
|
|
|
|
// releaseMemory.
|
|
|
|
void releaseMyMemory();
|
2002-07-18 00:11:28 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<LocalDataStructures>();
|
2002-07-18 00:11:28 +00:00
|
|
|
}
|
2003-07-01 16:27:15 +00:00
|
|
|
|
2003-09-20 16:34:13 +00:00
|
|
|
typedef hash_multimap<Instruction*, Function*> ActualCalleesTy;
|
2003-07-01 16:27:15 +00:00
|
|
|
const ActualCalleesTy &getActualCallees() const {
|
|
|
|
return ActualCallees;
|
|
|
|
}
|
|
|
|
|
2002-07-18 00:11:28 +00:00
|
|
|
private:
|
2003-06-30 05:10:09 +00:00
|
|
|
void calculateGraph(DSGraph &G);
|
2002-11-12 15:57:28 +00:00
|
|
|
|
2002-11-11 21:34:34 +00:00
|
|
|
DSGraph &getOrCreateGraph(Function *F);
|
|
|
|
|
|
|
|
unsigned calculateGraphs(Function *F, std::vector<Function*> &Stack,
|
|
|
|
unsigned &NextID,
|
2003-02-01 04:52:08 +00:00
|
|
|
hash_map<Function*, unsigned> &ValMap);
|
2002-07-18 00:11:28 +00:00
|
|
|
};
|
|
|
|
|
2002-11-09 19:21:56 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// TDDataStructures - Analysis that computes new data structure graphs
|
|
|
|
/// for each function using the closed graphs for the callers computed
|
|
|
|
/// by the bottom-up pass.
|
|
|
|
///
|
2004-09-20 04:48:05 +00:00
|
|
|
class TDDataStructures : public ModulePass {
|
2002-07-18 16:12:08 +00:00
|
|
|
// DSInfo, one graph for each function
|
2003-06-30 05:10:09 +00:00
|
|
|
hash_map<Function*, DSGraph*> DSInfo;
|
2003-07-01 21:12:10 +00:00
|
|
|
hash_set<Function*> ArgsRemainIncomplete;
|
2002-11-09 21:12:07 +00:00
|
|
|
DSGraph *GlobalsGraph;
|
2005-03-19 22:12:33 +00:00
|
|
|
|
|
|
|
/// GlobalECs - The equivalence classes for each global value that is merged
|
|
|
|
/// with other global values in the DSGraphs.
|
|
|
|
EquivalenceClasses<GlobalValue*> GlobalECs;
|
2005-03-21 04:46:35 +00:00
|
|
|
|
|
|
|
/// CallerCallEdges - For a particular graph, we keep a list of these records
|
|
|
|
/// which indicates which graphs call this function and from where.
|
|
|
|
struct CallerCallEdge {
|
|
|
|
DSGraph *CallerGraph; // The graph of the caller function.
|
|
|
|
const DSCallSite *CS; // The actual call site.
|
|
|
|
Function *CalledFunction; // The actual function being called.
|
|
|
|
|
|
|
|
CallerCallEdge(DSGraph *G, const DSCallSite *cs, Function *CF)
|
|
|
|
: CallerGraph(G), CS(cs), CalledFunction(CF) {}
|
|
|
|
|
|
|
|
bool operator<(const CallerCallEdge &RHS) const {
|
|
|
|
return CallerGraph < RHS.CallerGraph ||
|
|
|
|
(CallerGraph == RHS.CallerGraph && CS < RHS.CS);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<DSGraph*, std::vector<CallerCallEdge> > CallerEdges;
|
|
|
|
|
2005-03-21 20:29:56 +00:00
|
|
|
|
|
|
|
// IndCallMap - We memoize the results of indirect call inlining operations
|
|
|
|
// that have multiple targets here to avoid N*M inlining. The key to the map
|
|
|
|
// is a sorted set of callee functions, the value is the DSGraph that holds
|
|
|
|
// all of the caller graphs merged together, and the DSCallSite to merge with
|
|
|
|
// the arguments for each function.
|
|
|
|
std::map<std::vector<Function*>, DSGraph*> IndCallMap;
|
|
|
|
|
2002-07-18 16:12:08 +00:00
|
|
|
public:
|
2003-02-03 22:51:28 +00:00
|
|
|
~TDDataStructures() { releaseMyMemory(); }
|
2002-07-18 16:12:08 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
2002-07-18 16:12:08 +00:00
|
|
|
|
2002-11-10 06:53:19 +00:00
|
|
|
bool hasGraph(const Function &F) const {
|
2003-06-30 05:10:09 +00:00
|
|
|
return DSInfo.find(const_cast<Function*>(&F)) != DSInfo.end();
|
2002-11-10 06:53:19 +00:00
|
|
|
}
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getDSGraph - Return the data structure graph for the specified function.
|
|
|
|
///
|
2002-07-30 22:02:04 +00:00
|
|
|
DSGraph &getDSGraph(const Function &F) const {
|
2003-06-30 05:10:09 +00:00
|
|
|
hash_map<Function*, DSGraph*>::const_iterator I =
|
|
|
|
DSInfo.find(const_cast<Function*>(&F));
|
2002-07-18 16:12:08 +00:00
|
|
|
assert(I != DSInfo.end() && "Function not in module!");
|
|
|
|
return *I->second;
|
|
|
|
}
|
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
DSGraph &getGlobalsGraph() const { return *GlobalsGraph; }
|
2005-03-19 22:12:33 +00:00
|
|
|
EquivalenceClasses<GlobalValue*> &getGlobalECs() { return GlobalECs; }
|
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
|
2005-01-24 19:55:34 +00:00
|
|
|
/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
|
|
|
|
/// These correspond to the interfaces defined in the AliasAnalysis class.
|
|
|
|
void deleteValue(Value *V);
|
|
|
|
void copyValue(Value *From, Value *To);
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// print - Print out the analysis results...
|
|
|
|
///
|
2002-07-27 01:12:15 +00:00
|
|
|
void print(std::ostream &O, const Module *M) const;
|
2002-07-18 16:12:08 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// If the pass pipeline is done with this pass, we can release our memory...
|
|
|
|
///
|
2003-02-03 22:51:28 +00:00
|
|
|
virtual void releaseMyMemory();
|
2002-07-18 16:12:08 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getAnalysisUsage - This obviously provides a data structure graph.
|
|
|
|
///
|
2002-07-18 16:12:08 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<BUDataStructures>();
|
2002-07-18 16:12:08 +00:00
|
|
|
}
|
2003-02-03 22:51:28 +00:00
|
|
|
|
2002-07-18 16:12:08 +00:00
|
|
|
private:
|
2003-09-21 00:28:18 +00:00
|
|
|
void markReachableFunctionsExternallyAccessible(DSNode *N,
|
|
|
|
hash_set<DSNode*> &Visited);
|
|
|
|
|
2005-03-21 04:46:35 +00:00
|
|
|
void InlineCallersIntoGraph(DSGraph &G);
|
2002-11-08 21:24:51 +00:00
|
|
|
DSGraph &getOrCreateDSGraph(Function &F);
|
2003-07-01 16:27:15 +00:00
|
|
|
void ComputePostOrder(Function &F, hash_set<DSGraph*> &Visited,
|
|
|
|
std::vector<DSGraph*> &PostOrder,
|
|
|
|
const BUDataStructures::ActualCalleesTy &ActualCallees);
|
2002-07-18 16:12:08 +00:00
|
|
|
};
|
2002-10-01 22:34:45 +00:00
|
|
|
|
2003-11-13 01:42:38 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// CompleteBUDataStructures - This is the exact same as the bottom-up graphs,
|
|
|
|
/// but we use take a completed call graph and inline all indirect callees into
|
|
|
|
/// their callers graphs, making the result more useful for things like pool
|
|
|
|
/// allocation.
|
|
|
|
///
|
2003-11-13 01:42:38 +00:00
|
|
|
struct CompleteBUDataStructures : public BUDataStructures {
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
2003-11-13 01:42:38 +00:00
|
|
|
|
|
|
|
bool hasGraph(const Function &F) const {
|
|
|
|
return DSInfo.find(const_cast<Function*>(&F)) != DSInfo.end();
|
|
|
|
}
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getDSGraph - Return the data structure graph for the specified function.
|
|
|
|
///
|
2003-11-13 01:42:38 +00:00
|
|
|
DSGraph &getDSGraph(const Function &F) const {
|
|
|
|
hash_map<Function*, DSGraph*>::const_iterator I =
|
|
|
|
DSInfo.find(const_cast<Function*>(&F));
|
|
|
|
assert(I != DSInfo.end() && "Function not in module!");
|
|
|
|
return *I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<BUDataStructures>();
|
|
|
|
|
|
|
|
// FIXME: TEMPORARY (remove once finalization of indirect call sites in the
|
|
|
|
// globals graph has been implemented in the BU pass)
|
|
|
|
AU.addRequired<TDDataStructures>();
|
|
|
|
}
|
2003-11-13 05:05:34 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// print - Print out the analysis results...
|
|
|
|
///
|
2003-11-13 05:05:34 +00:00
|
|
|
void print(std::ostream &O, const Module *M) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned calculateSCCGraphs(DSGraph &FG, std::vector<DSGraph*> &Stack,
|
|
|
|
unsigned &NextID,
|
|
|
|
hash_map<DSGraph*, unsigned> &ValMap);
|
|
|
|
DSGraph &getOrCreateGraph(Function &F);
|
|
|
|
void processGraph(DSGraph &G);
|
2003-11-13 01:42:38 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-10-01 22:34:45 +00:00
|
|
|
#endif
|