blob: aa7f54b8904f08cf0db326e0fea0eb937c3670f3 [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 Lattnere6c0b5d2003-02-04 00:03:37 +000028 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
Chris Lattnerf4f62272005-03-19 22:23:45 +000029
30 EquivalenceClasses<GlobalValue*> GlobalECs; // Always empty
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000031 public:
Chris Lattnera612afc2003-02-26 19:29:36 +000032 Steens() : ResultGraph(0), GlobalsGraph(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
63 //
64
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());
119 GlobalsGraph = new DSGraph(GlobalECs, getTargetData());
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000120 ResultGraph->setGlobalsGraph(GlobalsGraph);
Chris Lattneraf283512003-02-09 19:26:47 +0000121
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000122 // Loop over the rest of the module, merging graphs for non-external functions
123 // into this graph.
124 //
125 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera2197132005-03-22 00:36:51 +0000126 if (!I->isExternal())
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000127 ResultGraph->spliceFrom(LDS.getDSGraph(*I));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000128
Chris Lattner7080c3e2005-03-22 00:04:21 +0000129 ResultGraph->removeTriviallyDeadNodes();
130
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000131 // FIXME: Must recalculate and use the Incomplete markers!!
132
133 // Now that we have all of the graphs inlined, we can go about eliminating
134 // call nodes...
135 //
Chris Lattnera9548d92005-01-30 23:51:02 +0000136 std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
Chris Lattner01fb0c72002-11-08 21:27:37 +0000137 assert(Calls.empty() && "Aux call list is already in use??");
138
Chris Lattnera9548d92005-01-30 23:51:02 +0000139 // Start with a copy of the original call sites.
Chris Lattner01fb0c72002-11-08 21:27:37 +0000140 Calls = ResultGraph->getFunctionCalls();
141
Chris Lattnera9548d92005-01-30 23:51:02 +0000142 for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
143 CI != E;) {
144 DSCallSite &CurCall = *CI++;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000145
146 // Loop over the called functions, eliminating as many as possible...
Chris Lattner9454dda2005-03-20 02:39:49 +0000147 std::vector<Function*> CallTargets;
Chris Lattner923fc052003-02-05 21:59:58 +0000148 if (CurCall.isDirectCall())
149 CallTargets.push_back(CurCall.getCalleeFunc());
150 else
Chris Lattner9454dda2005-03-20 02:39:49 +0000151 CurCall.getCalleeNode()->addFullFunctionList(CallTargets);
Chris Lattner923fc052003-02-05 21:59:58 +0000152
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000153 for (unsigned c = 0; c != CallTargets.size(); ) {
154 // If we can eliminate this function call, do so!
Chris Lattner9454dda2005-03-20 02:39:49 +0000155 Function *F = CallTargets[c];
156 if (!F->isExternal()) {
Chris Lattner560af8a2005-03-22 00:25:52 +0000157 ResolveFunctionCall(F, CurCall, ResultGraph->getReturnNodes()[F]);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000158 CallTargets[c] = CallTargets.back();
159 CallTargets.pop_back();
160 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000161 ++c; // Cannot eliminate this call, skip over it...
162 }
163
Chris Lattner0f777ab2003-02-10 00:14:57 +0000164 if (CallTargets.empty()) { // Eliminated all calls?
Chris Lattnera9548d92005-01-30 23:51:02 +0000165 std::list<DSCallSite>::iterator I = CI;
166 Calls.erase(--I); // Remove entry
167 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000168 }
169
Chris Lattner560af8a2005-03-22 00:25:52 +0000170 // Remove our knowledge of what the return values of the functions are.
171 ResultGraph->getReturnNodes().clear();
Chris Lattner72d29a42003-02-11 23:11:51 +0000172
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000173 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
174 // incoming arguments...
175 ResultGraph->maskIncompleteMarkers();
Chris Lattnerd94b4d52005-03-24 18:42:28 +0000176 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs |
177 DSGraph::IgnoreGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000178
179 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000180 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner394471f2003-01-23 22:05:33 +0000181 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000182
Chris Lattner47a307a2003-02-09 18:42:16 +0000183 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000184 return false;
185}
186
Chris Lattnera612afc2003-02-26 19:29:36 +0000187AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
188 const Value *V2, unsigned V2Size) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000189 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000190
Chris Lattner8d327672003-06-30 03:36:09 +0000191 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000192
Chris Lattner8d327672003-06-30 03:36:09 +0000193 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000194 DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
Chris Lattner943814b2005-03-23 01:48:09 +0000195 if (I != GSM.end() && !I->second.isNull() &&
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000196 J != GSM.end() && !J->second.isNull()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000197 DSNodeHandle &V1H = I->second;
Chris Lattner40ee8ce2005-03-27 21:56:55 +0000198 DSNodeHandle &V2H = J->second;
199
200 // If at least one of the nodes is complete, we can say something about
201 // this. If one is complete and the other isn't, then they are obviously
202 // different nodes. If they are both complete, we can't say anything
203 // useful.
204 if (I->second.getNode()->isComplete() ||
Chris Lattner943814b2005-03-23 01:48:09 +0000205 J->second.getNode()->isComplete()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000206 // If the two pointers point to different data structure graph nodes, they
207 // cannot alias!
Chris Lattner943814b2005-03-23 01:48:09 +0000208 if (V1H.getNode() != V2H.getNode())
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000209 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000210
Chris Lattner943814b2005-03-23 01:48:09 +0000211 // See if they point to different offsets... if so, we may be able to
212 // determine that they do not alias...
213 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
214 if (O1 != O2) {
215 if (O2 < O1) { // Ensure that O1 <= O2
216 std::swap(V1, V2);
217 std::swap(O1, O2);
218 std::swap(V1Size, V2Size);
219 }
220
221 if (O1+V1Size <= O2)
222 return NoAlias;
223 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000224 }
225 }
226
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000227 // If we cannot determine alias properties based on our graph, fall back on
228 // some other AA implementation.
229 //
Chris Lattner1bf34082004-05-23 21:13:51 +0000230 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000231}
Chris Lattnerb7523412005-03-26 22:43:20 +0000232
233AliasAnalysis::ModRefResult
234Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
235 AliasAnalysis::ModRefResult Result = ModRef;
236
237 // Find the node in question.
238 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
239 DSGraph::ScalarMapTy::iterator I = GSM.find(P);
240
241 if (I != GSM.end() && !I->second.isNull()) {
242 DSNode *N = I->second.getNode();
243 if (N->isComplete()) {
244 // If this is a direct call to an external function, and if the pointer
245 // points to a complete node, the external function cannot modify or read
246 // the value (we know it's not passed out of the program!).
247 if (Function *F = CS.getCalledFunction())
248 if (F->isExternal())
249 return NoModRef;
250
251 // Otherwise, if the node is complete, but it is only M or R, return this.
252 // This can be useful for globals that should be marked const but are not.
253 if (!N->isModified())
254 Result = (ModRefResult)(Result & ~Mod);
255 if (!N->isRead())
256 Result = (ModRefResult)(Result & ~Ref);
257 }
258 }
259
260 return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
261}