blob: 426795e8bdeee96d694ca9748476fa2e083be09f [file] [log] [blame]
Chris Lattner1c7ce2c2002-10-01 22:34:12 +00001//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner1c7ce2c2002-10-01 22:34:12 +00009//
10// This pass uses the data structure graphs to implement a simple context
11// insensitive alias analysis. It does this by computing the local analysis
12// graphs for all of the functions, then merging them together into a single big
13// graph without cloning.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc5f21de2002-10-02 22:14:38 +000018#include "llvm/Analysis/DSGraph.h"
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Module.h"
Chris Lattner6806f562003-08-01 22:15:03 +000021#include "Support/Debug.h"
Chris Lattner9a927292003-11-12 23:11:14 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Misha Brukmanf4445df2002-12-12 05:34:10 +000024namespace {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000025 class Steens : public Pass, public AliasAnalysis {
26 DSGraph *ResultGraph;
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000027 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000028 public:
Chris Lattnera612afc2003-02-26 19:29:36 +000029 Steens() : ResultGraph(0), GlobalsGraph(0) {}
Chris Lattnerbe1094e2003-02-13 21:44:18 +000030 ~Steens() {
31 releaseMyMemory();
32 assert(ResultGraph == 0 && "releaseMemory not called?");
33 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000034
35 //------------------------------------------------
36 // Implement the Pass API
37 //
38
39 // run - Build up the result graph, representing the pointer graph for the
40 // program.
41 //
42 bool run(Module &M);
43
Chris Lattnerbe1094e2003-02-13 21:44:18 +000044 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000045
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000047 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000048 AU.setPreservesAll(); // Does not transform code...
49 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
50 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
51 }
52
53 // print - Implement the Pass::print method...
54 void print(std::ostream &O, const Module *M) const {
55 assert(ResultGraph && "Result graph has not yet been computed!");
56 ResultGraph->writeGraphToFile(O, "steensgaards");
57 }
58
59 //------------------------------------------------
60 // Implement the AliasAnalysis API
61 //
62
63 // alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +000064 AliasResult alias(const Value *V1, unsigned V1Size,
65 const Value *V2, unsigned V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000066
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000067 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000068 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000069 DSNodeHandle &RetVal);
70 };
71
72 // Register the pass...
73 RegisterOpt<Steens> X("steens-aa",
Chris Lattner6022ef42003-02-08 23:03:17 +000074 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000075
76 // Register as an implementation of AliasAnalysis
77 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
78}
79
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000080/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
81/// with the specified call site descriptor. This function links the arguments
82/// and the return value for the call site context-insensitively.
83///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000084void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000085 DSNodeHandle &RetVal) {
86 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner8d327672003-06-30 03:36:09 +000087 DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000088
Vikram S. Adve42fd1692002-10-20 18:07:37 +000089 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000090 if (Call.getRetVal().getNode() && RetVal.getNode())
91 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000092
93 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000094 unsigned PtrArgIdx = 0;
Chris Lattner88c72942003-02-10 18:16:19 +000095 for (Function::aiterator AI = F->abegin(), AE = F->aend();
96 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
Chris Lattner8d327672003-06-30 03:36:09 +000097 DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000098 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000099 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000100 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000101}
102
103
104/// run - Build up the result graph, representing the pointer graph for the
105/// program.
106///
107bool Steens::run(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +0000108 InitializeAliasAnalysis(this);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000109 assert(ResultGraph == 0 && "Result graph already allocated!");
110 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
111
112 // Create a new, empty, graph...
Chris Lattner15869aa2003-11-02 22:27:28 +0000113 ResultGraph = new DSGraph(getTargetData());
114 GlobalsGraph = new DSGraph(getTargetData());
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000115 ResultGraph->setGlobalsGraph(GlobalsGraph);
Chris Lattneraf283512003-02-09 19:26:47 +0000116 ResultGraph->setPrintAuxCalls();
117
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000118 // RetValMap - Keep track of the return values for all functions that return
119 // valid pointers.
120 //
Chris Lattner5a540632003-06-30 03:15:25 +0000121 DSGraph::ReturnNodesTy RetValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000122
123 // Loop over the rest of the module, merging graphs for non-external functions
124 // into this graph.
125 //
Chris Lattner0f777ab2003-02-10 00:14:57 +0000126 unsigned Count = 0;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000127 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
128 if (!I->isExternal()) {
Chris Lattner5a540632003-06-30 03:15:25 +0000129 DSGraph::ScalarMapTy ValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000130 { // Scope to free NodeMap memory ASAP
Chris Lattner5a540632003-06-30 03:15:25 +0000131 DSGraph::NodeMapTy NodeMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000132 const DSGraph &FDSG = LDS.getDSGraph(*I);
Chris Lattner091f7762004-01-23 01:44:53 +0000133 ResultGraph->cloneInto(FDSG, ValMap, RetValMap, NodeMap,
134 DSGraph::UpdateInlinedGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000135 }
136
Chris Lattnerc875f022002-11-03 21:27:48 +0000137 // Incorporate the inlined Function's ScalarMap into the global
138 // ScalarMap...
Chris Lattner5a540632003-06-30 03:15:25 +0000139 DSGraph::ScalarMapTy &GVM = ResultGraph->getScalarMap();
Chris Lattner8d327672003-06-30 03:36:09 +0000140 for (DSGraph::ScalarMapTy::iterator I = ValMap.begin(),
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000141 E = ValMap.end(); I != E; ++I)
142 GVM[I->first].mergeWith(I->second);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000143
144 if ((++Count & 1) == 0) // Prune nodes out every other time...
145 ResultGraph->removeTriviallyDeadNodes();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000146 }
147
148 // FIXME: Must recalculate and use the Incomplete markers!!
149
150 // Now that we have all of the graphs inlined, we can go about eliminating
151 // call nodes...
152 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000153 std::vector<DSCallSite> &Calls =
Chris Lattner01fb0c72002-11-08 21:27:37 +0000154 ResultGraph->getAuxFunctionCalls();
155 assert(Calls.empty() && "Aux call list is already in use??");
156
157 // Start with a copy of the original call sites...
158 Calls = ResultGraph->getFunctionCalls();
159
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000160 for (unsigned i = 0; i != Calls.size(); ) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000161 DSCallSite &CurCall = Calls[i];
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000162
163 // Loop over the called functions, eliminating as many as possible...
Chris Lattner923fc052003-02-05 21:59:58 +0000164 std::vector<GlobalValue*> CallTargets;
165 if (CurCall.isDirectCall())
166 CallTargets.push_back(CurCall.getCalleeFunc());
167 else
168 CallTargets = CurCall.getCalleeNode()->getGlobals();
169
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000170 for (unsigned c = 0; c != CallTargets.size(); ) {
171 // If we can eliminate this function call, do so!
172 bool Eliminated = false;
173 if (Function *F = dyn_cast<Function>(CallTargets[c]))
174 if (!F->isExternal()) {
175 ResolveFunctionCall(F, CurCall, RetValMap[F]);
176 Eliminated = true;
177 }
Chris Lattner0f777ab2003-02-10 00:14:57 +0000178 if (Eliminated) {
179 CallTargets[c] = CallTargets.back();
180 CallTargets.pop_back();
181 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000182 ++c; // Cannot eliminate this call, skip over it...
183 }
184
Chris Lattner0f777ab2003-02-10 00:14:57 +0000185 if (CallTargets.empty()) { // Eliminated all calls?
186 CurCall = Calls.back(); // Remove entry
187 Calls.pop_back();
188 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000189 ++i; // Skip this call site...
190 }
191
Chris Lattner72d29a42003-02-11 23:11:51 +0000192 RetValMap.clear();
193
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000194 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
195 // incoming arguments...
196 ResultGraph->maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000197 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000198
199 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000200 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner394471f2003-01-23 22:05:33 +0000201 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000202
Chris Lattner47a307a2003-02-09 18:42:16 +0000203 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000204 return false;
205}
206
207// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +0000208AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
209 const Value *V2, unsigned V2Size) {
210 // FIXME: HANDLE Size argument!
Misha Brukmanf4445df2002-12-12 05:34:10 +0000211 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000212
Chris Lattner8d327672003-06-30 03:36:09 +0000213 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000214
Chris Lattner8d327672003-06-30 03:36:09 +0000215 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
Chris Lattner0a91e632003-02-03 22:51:53 +0000216 if (I != GSM.end() && I->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000217 DSNodeHandle &V1H = I->second;
Chris Lattner8d327672003-06-30 03:36:09 +0000218 DSGraph::ScalarMapTy::iterator J=GSM.find(const_cast<Value*>(V2));
Chris Lattner0a91e632003-02-03 22:51:53 +0000219 if (J != GSM.end() && J->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000220 DSNodeHandle &V2H = J->second;
221 // If the two pointers point to different data structure graph nodes, they
222 // cannot alias!
Chris Lattner6022ef42003-02-08 23:03:17 +0000223 if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000224 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000225
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000226 // FIXME: If the two pointers point to the same node, and the offsets are
227 // different, and the LinkIndex vector doesn't alias the section, then the
228 // two pointers do not alias. We need access size information for the two
229 // accesses though!
230 //
231 }
232 }
233
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000234 // If we cannot determine alias properties based on our graph, fall back on
235 // some other AA implementation.
236 //
Chris Lattnera612afc2003-02-26 19:29:36 +0000237 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000238}