blob: 8a8391ff2a2aae5efd9f6aa1d50392f911d363ba [file] [log] [blame]
Chris Lattner1c7ce2c2002-10-01 22:34:12 +00001//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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
Chris Lattner4dabb2c2004-07-07 06:32:21 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
18#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000019#include "llvm/Analysis/AliasAnalysis.h"
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000020#include "llvm/Analysis/Passes.h"
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000021#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
Chris Lattner86a54842006-01-22 22:53:01 +000023#include <iostream>
Chris Lattner9a927292003-11-12 23:11:14 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Misha Brukmanf4445df2002-12-12 05:34:10 +000026namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000027 class Steens : public ModulePass, public AliasAnalysis {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000028 DSGraph *ResultGraph;
Chris Lattnerf4f62272005-03-19 22:23:45 +000029
30 EquivalenceClasses<GlobalValue*> GlobalECs; // Always empty
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000031 public:
Chris Lattner021decc2005-04-02 19:17:18 +000032 Steens() : ResultGraph(0) {}
Chris Lattnerbe1094e2003-02-13 21:44:18 +000033 ~Steens() {
34 releaseMyMemory();
35 assert(ResultGraph == 0 && "releaseMemory not called?");
36 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000037
38 //------------------------------------------------
39 // Implement the Pass API
40 //
41
42 // run - Build up the result graph, representing the pointer graph for the
43 // program.
44 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000045 bool runOnModule(Module &M);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000046
Chris Lattnerbe1094e2003-02-13 21:44:18 +000047 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000048
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000050 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000051 AU.setPreservesAll(); // Does not transform code...
52 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000053 }
54
55 // print - Implement the Pass::print method...
56 void print(std::ostream &O, const Module *M) const {
57 assert(ResultGraph && "Result graph has not yet been computed!");
58 ResultGraph->writeGraphToFile(O, "steensgaards");
59 }
60
61 //------------------------------------------------
62 // Implement the AliasAnalysis API
Misha Brukman2b37d7c2005-04-21 21:13:18 +000063 //
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000064
Chris Lattnera612afc2003-02-26 19:29:36 +000065 AliasResult alias(const Value *V1, unsigned V1Size,
66 const Value *V2, unsigned V2Size);
Chris Lattnerb7523412005-03-26 22:43:20 +000067
68 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
69
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000070 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000071 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000072 DSNodeHandle &RetVal);
73 };
74
75 // Register the pass...
76 RegisterOpt<Steens> X("steens-aa",
Chris Lattner6022ef42003-02-08 23:03:17 +000077 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000078
79 // Register as an implementation of AliasAnalysis
80 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
81}
82
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000083ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
84
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000085/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
86/// with the specified call site descriptor. This function links the arguments
87/// and the return value for the call site context-insensitively.
88///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000089void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000090 DSNodeHandle &RetVal) {
91 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner8d327672003-06-30 03:36:09 +000092 DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000093
Vikram S. Adve42fd1692002-10-20 18:07:37 +000094 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000095 if (Call.getRetVal().getNode() && RetVal.getNode())
96 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000097
98 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000099 unsigned PtrArgIdx = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +0000100 for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
Chris Lattner88c72942003-02-10 18:16:19 +0000101 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
Chris Lattner8d327672003-06-30 03:36:09 +0000102 DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000103 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000104 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000105 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000106}
107
108
109/// run - Build up the result graph, representing the pointer graph for the
110/// program.
111///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000112bool Steens::runOnModule(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +0000113 InitializeAliasAnalysis(this);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000114 assert(ResultGraph == 0 && "Result graph already allocated!");
115 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
116
117 // Create a new, empty, graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000118 ResultGraph = new DSGraph(GlobalECs, getTargetData());
Chris Lattner021decc2005-04-02 19:17:18 +0000119 ResultGraph->spliceFrom(LDS.getGlobalsGraph());
Chris Lattneraf283512003-02-09 19:26:47 +0000120
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000121 // Loop over the rest of the module, merging graphs for non-external functions
122 // into this graph.
123 //
124 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera2197132005-03-22 00:36:51 +0000125 if (!I->isExternal())
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000126 ResultGraph->spliceFrom(LDS.getDSGraph(*I));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000127
Chris Lattner7080c3e2005-03-22 00:04:21 +0000128 ResultGraph->removeTriviallyDeadNodes();
129
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000130 // FIXME: Must recalculate and use the Incomplete markers!!
131
132 // Now that we have all of the graphs inlined, we can go about eliminating
133 // call nodes...
134 //
Chris Lattnera9548d92005-01-30 23:51:02 +0000135 std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
Chris Lattner01fb0c72002-11-08 21:27:37 +0000136 assert(Calls.empty() && "Aux call list is already in use??");
137
Chris Lattnera9548d92005-01-30 23:51:02 +0000138 // Start with a copy of the original call sites.
Chris Lattner01fb0c72002-11-08 21:27:37 +0000139 Calls = ResultGraph->getFunctionCalls();
140
Chris Lattnera9548d92005-01-30 23:51:02 +0000141 for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
142 CI != E;) {
143 DSCallSite &CurCall = *CI++;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000144
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000145 // Loop over the called functions, eliminating as many as possible...
Chris Lattner9454dda2005-03-20 02:39:49 +0000146 std::vector<Function*> CallTargets;
Chris Lattner923fc052003-02-05 21:59:58 +0000147 if (CurCall.isDirectCall())
148 CallTargets.push_back(CurCall.getCalleeFunc());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000149 else
Chris Lattner9454dda2005-03-20 02:39:49 +0000150 CurCall.getCalleeNode()->addFullFunctionList(CallTargets);
Chris Lattner923fc052003-02-05 21:59:58 +0000151
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000152 for (unsigned c = 0; c != CallTargets.size(); ) {
153 // If we can eliminate this function call, do so!
Chris Lattner9454dda2005-03-20 02:39:49 +0000154 Function *F = CallTargets[c];
155 if (!F->isExternal()) {
Chris Lattner560af8a2005-03-22 00:25:52 +0000156 ResolveFunctionCall(F, CurCall, ResultGraph->getReturnNodes()[F]);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000157 CallTargets[c] = CallTargets.back();
158 CallTargets.pop_back();
159 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000160 ++c; // Cannot eliminate this call, skip over it...
161 }
162
Chris Lattner0f777ab2003-02-10 00:14:57 +0000163 if (CallTargets.empty()) { // Eliminated all calls?
Chris Lattnera9548d92005-01-30 23:51:02 +0000164 std::list<DSCallSite>::iterator I = CI;
165 Calls.erase(--I); // Remove entry
166 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000167 }
168
Chris Lattner9342a932005-03-29 19:16:59 +0000169 // Remove our knowledge of what the return values of the functions are, except
170 // for functions that are externally visible from this module (e.g. main). We
171 // keep these functions so that their arguments are marked incomplete.
172 for (DSGraph::ReturnNodesTy::iterator I =
173 ResultGraph->getReturnNodes().begin(),
174 E = ResultGraph->getReturnNodes().end(); I != E; )
175 if (I->first->hasInternalLinkage())
176 ResultGraph->getReturnNodes().erase(I++);
177 else
178 ++I;
Chris Lattner72d29a42003-02-11 23:11:51 +0000179
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000180 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
181 // incoming arguments...
182 ResultGraph->maskIncompleteMarkers();
Chris Lattner9342a932005-03-29 19:16:59 +0000183 ResultGraph->markIncompleteNodes(DSGraph::IgnoreGlobals |
184 DSGraph::MarkFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000185
186 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000187 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner021decc2005-04-02 19:17:18 +0000188 //ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000189
Chris Lattner47a307a2003-02-09 18:42:16 +0000190 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000191 return false;
192}
193
Chris Lattnera612afc2003-02-26 19:29:36 +0000194AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
195 const Value *V2, unsigned V2Size) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000196 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000197
Chris Lattner8d327672003-06-30 03:36:09 +0000198 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000199
Chris Lattner8d327672003-06-30 03:36:09 +0000200 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000201 DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
Chris Lattner943814b2005-03-23 01:48:09 +0000202 if (I != GSM.end() && !I->second.isNull() &&
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000203 J != GSM.end() && !J->second.isNull()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000204 DSNodeHandle &V1H = I->second;
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000205 DSNodeHandle &V2H = J->second;
206
207 // If at least one of the nodes is complete, we can say something about
208 // this. If one is complete and the other isn't, then they are obviously
209 // different nodes. If they are both complete, we can't say anything
210 // useful.
211 if (I->second.getNode()->isComplete() ||
Chris Lattner943814b2005-03-23 01:48:09 +0000212 J->second.getNode()->isComplete()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000213 // If the two pointers point to different data structure graph nodes, they
214 // cannot alias!
Chris Lattner943814b2005-03-23 01:48:09 +0000215 if (V1H.getNode() != V2H.getNode())
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000216 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000217
Chris Lattner943814b2005-03-23 01:48:09 +0000218 // See if they point to different offsets... if so, we may be able to
219 // determine that they do not alias...
220 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
221 if (O1 != O2) {
222 if (O2 < O1) { // Ensure that O1 <= O2
223 std::swap(V1, V2);
224 std::swap(O1, O2);
225 std::swap(V1Size, V2Size);
226 }
227
228 if (O1+V1Size <= O2)
229 return NoAlias;
230 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000231 }
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 Lattner1bf34082004-05-23 21:13:51 +0000237 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000238}
Chris Lattnerb7523412005-03-26 22:43:20 +0000239
240AliasAnalysis::ModRefResult
241Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
242 AliasAnalysis::ModRefResult Result = ModRef;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000243
Chris Lattnerb7523412005-03-26 22:43:20 +0000244 // Find the node in question.
245 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
246 DSGraph::ScalarMapTy::iterator I = GSM.find(P);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000247
Chris Lattnerb7523412005-03-26 22:43:20 +0000248 if (I != GSM.end() && !I->second.isNull()) {
249 DSNode *N = I->second.getNode();
250 if (N->isComplete()) {
251 // If this is a direct call to an external function, and if the pointer
252 // points to a complete node, the external function cannot modify or read
253 // the value (we know it's not passed out of the program!).
254 if (Function *F = CS.getCalledFunction())
255 if (F->isExternal())
256 return NoModRef;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000257
Chris Lattnerb7523412005-03-26 22:43:20 +0000258 // Otherwise, if the node is complete, but it is only M or R, return this.
259 // This can be useful for globals that should be marked const but are not.
260 if (!N->isModified())
261 Result = (ModRefResult)(Result & ~Mod);
262 if (!N->isRead())
263 Result = (ModRefResult)(Result & ~Ref);
264 }
265 }
266
267 return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
268}