blob: 9f0574df1a37ea8f88d47da9c247d91d4f5d98b1 [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
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 Lattner9a927292003-11-12 23:11:14 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Misha Brukmanf4445df2002-12-12 05:34:10 +000025namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000026 class Steens : public ModulePass, public AliasAnalysis {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000027 DSGraph *ResultGraph;
Chris Lattnerf4f62272005-03-19 22:23:45 +000028
29 EquivalenceClasses<GlobalValue*> GlobalECs; // Always empty
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000030 public:
Chris Lattner021decc2005-04-02 19:17:18 +000031 Steens() : ResultGraph(0) {}
Chris Lattnerbe1094e2003-02-13 21:44:18 +000032 ~Steens() {
33 releaseMyMemory();
34 assert(ResultGraph == 0 && "releaseMemory not called?");
35 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000036
37 //------------------------------------------------
38 // Implement the Pass API
39 //
40
41 // run - Build up the result graph, representing the pointer graph for the
42 // program.
43 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000044 bool runOnModule(Module &M);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000045
Chris Lattnerbe1094e2003-02-13 21:44:18 +000046 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000047
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000049 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000050 AU.setPreservesAll(); // Does not transform code...
51 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000052 }
53
54 // print - Implement the Pass::print method...
55 void print(std::ostream &O, const Module *M) const {
56 assert(ResultGraph && "Result graph has not yet been computed!");
57 ResultGraph->writeGraphToFile(O, "steensgaards");
58 }
59
60 //------------------------------------------------
61 // Implement the AliasAnalysis API
62 //
63
Chris Lattnera612afc2003-02-26 19:29:36 +000064 AliasResult alias(const Value *V1, unsigned V1Size,
65 const Value *V2, unsigned V2Size);
Chris Lattnerb7523412005-03-26 22:43:20 +000066
67 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
68
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000069 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000070 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000071 DSNodeHandle &RetVal);
72 };
73
74 // Register the pass...
75 RegisterOpt<Steens> X("steens-aa",
Chris Lattner6022ef42003-02-08 23:03:17 +000076 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000077
78 // Register as an implementation of AliasAnalysis
79 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
80}
81
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000082ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
83
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000084/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
85/// with the specified call site descriptor. This function links the arguments
86/// and the return value for the call site context-insensitively.
87///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000088void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000089 DSNodeHandle &RetVal) {
90 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner8d327672003-06-30 03:36:09 +000091 DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000092
Vikram S. Adve42fd1692002-10-20 18:07:37 +000093 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000094 if (Call.getRetVal().getNode() && RetVal.getNode())
95 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000096
97 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000098 unsigned PtrArgIdx = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +000099 for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
Chris Lattner88c72942003-02-10 18:16:19 +0000100 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
Chris Lattner8d327672003-06-30 03:36:09 +0000101 DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000102 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000103 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000104 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000105}
106
107
108/// run - Build up the result graph, representing the pointer graph for the
109/// program.
110///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000111bool Steens::runOnModule(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +0000112 InitializeAliasAnalysis(this);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000113 assert(ResultGraph == 0 && "Result graph already allocated!");
114 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
115
116 // Create a new, empty, graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000117 ResultGraph = new DSGraph(GlobalECs, getTargetData());
Chris Lattner021decc2005-04-02 19:17:18 +0000118 ResultGraph->spliceFrom(LDS.getGlobalsGraph());
Chris Lattneraf283512003-02-09 19:26:47 +0000119
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000120 // Loop over the rest of the module, merging graphs for non-external functions
121 // into this graph.
122 //
123 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera2197132005-03-22 00:36:51 +0000124 if (!I->isExternal())
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000125 ResultGraph->spliceFrom(LDS.getDSGraph(*I));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000126
Chris Lattner7080c3e2005-03-22 00:04:21 +0000127 ResultGraph->removeTriviallyDeadNodes();
128
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000129 // FIXME: Must recalculate and use the Incomplete markers!!
130
131 // Now that we have all of the graphs inlined, we can go about eliminating
132 // call nodes...
133 //
Chris Lattnera9548d92005-01-30 23:51:02 +0000134 std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
Chris Lattner01fb0c72002-11-08 21:27:37 +0000135 assert(Calls.empty() && "Aux call list is already in use??");
136
Chris Lattnera9548d92005-01-30 23:51:02 +0000137 // Start with a copy of the original call sites.
Chris Lattner01fb0c72002-11-08 21:27:37 +0000138 Calls = ResultGraph->getFunctionCalls();
139
Chris Lattnera9548d92005-01-30 23:51:02 +0000140 for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
141 CI != E;) {
142 DSCallSite &CurCall = *CI++;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000143
144 // Loop over the called functions, eliminating as many as possible...
Chris Lattner9454dda2005-03-20 02:39:49 +0000145 std::vector<Function*> CallTargets;
Chris Lattner923fc052003-02-05 21:59:58 +0000146 if (CurCall.isDirectCall())
147 CallTargets.push_back(CurCall.getCalleeFunc());
148 else
Chris Lattner9454dda2005-03-20 02:39:49 +0000149 CurCall.getCalleeNode()->addFullFunctionList(CallTargets);
Chris Lattner923fc052003-02-05 21:59:58 +0000150
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000151 for (unsigned c = 0; c != CallTargets.size(); ) {
152 // If we can eliminate this function call, do so!
Chris Lattner9454dda2005-03-20 02:39:49 +0000153 Function *F = CallTargets[c];
154 if (!F->isExternal()) {
Chris Lattner560af8a2005-03-22 00:25:52 +0000155 ResolveFunctionCall(F, CurCall, ResultGraph->getReturnNodes()[F]);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000156 CallTargets[c] = CallTargets.back();
157 CallTargets.pop_back();
158 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000159 ++c; // Cannot eliminate this call, skip over it...
160 }
161
Chris Lattner0f777ab2003-02-10 00:14:57 +0000162 if (CallTargets.empty()) { // Eliminated all calls?
Chris Lattnera9548d92005-01-30 23:51:02 +0000163 std::list<DSCallSite>::iterator I = CI;
164 Calls.erase(--I); // Remove entry
165 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000166 }
167
Chris Lattner9342a932005-03-29 19:16:59 +0000168 // Remove our knowledge of what the return values of the functions are, except
169 // for functions that are externally visible from this module (e.g. main). We
170 // keep these functions so that their arguments are marked incomplete.
171 for (DSGraph::ReturnNodesTy::iterator I =
172 ResultGraph->getReturnNodes().begin(),
173 E = ResultGraph->getReturnNodes().end(); I != E; )
174 if (I->first->hasInternalLinkage())
175 ResultGraph->getReturnNodes().erase(I++);
176 else
177 ++I;
Chris Lattner72d29a42003-02-11 23:11:51 +0000178
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000179 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
180 // incoming arguments...
181 ResultGraph->maskIncompleteMarkers();
Chris Lattner9342a932005-03-29 19:16:59 +0000182 ResultGraph->markIncompleteNodes(DSGraph::IgnoreGlobals |
183 DSGraph::MarkFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000184
185 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000186 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner021decc2005-04-02 19:17:18 +0000187 //ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000188
Chris Lattner47a307a2003-02-09 18:42:16 +0000189 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000190 return false;
191}
192
Chris Lattnera612afc2003-02-26 19:29:36 +0000193AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
194 const Value *V2, unsigned V2Size) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000195 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000196
Chris Lattner8d327672003-06-30 03:36:09 +0000197 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000198
Chris Lattner8d327672003-06-30 03:36:09 +0000199 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000200 DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
Chris Lattner943814b2005-03-23 01:48:09 +0000201 if (I != GSM.end() && !I->second.isNull() &&
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000202 J != GSM.end() && !J->second.isNull()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000203 DSNodeHandle &V1H = I->second;
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000204 DSNodeHandle &V2H = J->second;
205
206 // If at least one of the nodes is complete, we can say something about
207 // this. If one is complete and the other isn't, then they are obviously
208 // different nodes. If they are both complete, we can't say anything
209 // useful.
210 if (I->second.getNode()->isComplete() ||
Chris Lattner943814b2005-03-23 01:48:09 +0000211 J->second.getNode()->isComplete()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000212 // If the two pointers point to different data structure graph nodes, they
213 // cannot alias!
Chris Lattner943814b2005-03-23 01:48:09 +0000214 if (V1H.getNode() != V2H.getNode())
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000215 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000216
Chris Lattner943814b2005-03-23 01:48:09 +0000217 // See if they point to different offsets... if so, we may be able to
218 // determine that they do not alias...
219 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
220 if (O1 != O2) {
221 if (O2 < O1) { // Ensure that O1 <= O2
222 std::swap(V1, V2);
223 std::swap(O1, O2);
224 std::swap(V1Size, V2Size);
225 }
226
227 if (O1+V1Size <= O2)
228 return NoAlias;
229 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000230 }
231 }
232
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000233 // If we cannot determine alias properties based on our graph, fall back on
234 // some other AA implementation.
235 //
Chris Lattner1bf34082004-05-23 21:13:51 +0000236 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000237}
Chris Lattnerb7523412005-03-26 22:43:20 +0000238
239AliasAnalysis::ModRefResult
240Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
241 AliasAnalysis::ModRefResult Result = ModRef;
242
243 // Find the node in question.
244 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
245 DSGraph::ScalarMapTy::iterator I = GSM.find(P);
246
247 if (I != GSM.end() && !I->second.isNull()) {
248 DSNode *N = I->second.getNode();
249 if (N->isComplete()) {
250 // If this is a direct call to an external function, and if the pointer
251 // points to a complete node, the external function cannot modify or read
252 // the value (we know it's not passed out of the program!).
253 if (Function *F = CS.getCalledFunction())
254 if (F->isExternal())
255 return NoModRef;
256
257 // Otherwise, if the node is complete, but it is only M or R, return this.
258 // This can be useful for globals that should be marked const but are not.
259 if (!N->isModified())
260 Result = (ModRefResult)(Result & ~Mod);
261 if (!N->isRead())
262 Result = (ModRefResult)(Result & ~Ref);
263 }
264 }
265
266 return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
267}