blob: 8a2e0caa425c274850c19294d9369c5180651a8d [file] [log] [blame]
Chris Lattner8105cfb2003-02-03 22:50:46 +00001//===- DataStructureAA.cpp - Data Structure Based 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 Lattner8105cfb2003-02-03 22:50:46 +00009//
10// This pass uses the top-down data structure graphs to implement a simple
11// context sensitive alias analysis.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DataStructure.h"
16#include "llvm/Analysis/DSGraph.h"
17#include "llvm/Analysis/AliasAnalysis.h"
18#include "llvm/Module.h"
Chris Lattner9a927292003-11-12 23:11:14 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner8105cfb2003-02-03 22:50:46 +000021namespace {
22 class DSAA : public Pass, public AliasAnalysis {
23 TDDataStructures *TD;
24 public:
25 DSAA() : TD(0) {}
26
27 //------------------------------------------------
28 // Implement the Pass API
29 //
30
31 // run - Build up the result graph, representing the pointer graph for the
32 // program.
33 //
34 bool run(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +000035 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000036 TD = &getAnalysis<TDDataStructures>();
37 return false;
38 }
39
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000041 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner8105cfb2003-02-03 22:50:46 +000042 AU.setPreservesAll(); // Does not transform code...
43 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
44 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
45 }
46
47 //------------------------------------------------
48 // Implement the AliasAnalysis API
49 //
50
Chris Lattnera612afc2003-02-26 19:29:36 +000051 AliasResult alias(const Value *V1, unsigned V1Size,
52 const Value *V2, unsigned V2Size);
Chris Lattner5c70dc02003-06-29 00:54:08 +000053
54 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
55
56 private:
57 DSGraph *getGraphForValue(const Value *V);
Chris Lattner8105cfb2003-02-03 22:50:46 +000058 };
59
60 // Register the pass...
61 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
62
63 // Register as an implementation of AliasAnalysis
64 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
65}
66
Chris Lattner5c70dc02003-06-29 00:54:08 +000067// getGraphForValue - Return the DSGraph to use for queries about the specified
68// value...
Chris Lattner8105cfb2003-02-03 22:50:46 +000069//
Chris Lattner5c70dc02003-06-29 00:54:08 +000070DSGraph *DSAA::getGraphForValue(const Value *V) {
Chris Lattner8105cfb2003-02-03 22:50:46 +000071 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000072 return &TD->getDSGraph(*I->getParent()->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000073 else if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000074 return &TD->getDSGraph(*A->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000075 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000076 return &TD->getDSGraph(*BB->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000077 return 0;
78}
79
Chris Lattner5c70dc02003-06-29 00:54:08 +000080// isSinglePhysicalObject - For now, the only case that we know that there is
81// only one memory object in the node is when there is a single global in the
82// node, and the only composition bit set is Global.
83//
84static bool isSinglePhysicalObject(DSNode *N) {
85 assert(N->isComplete() && "Can only tell if this is a complete object!");
86 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
87 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
88}
89
Chris Lattner8105cfb2003-02-03 22:50:46 +000090// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +000091AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
92 const Value *V2, unsigned V2Size) {
Chris Lattnerbd92b732003-06-19 21:15:11 +000093 if (V1 == V2) return MustAlias;
94
Chris Lattner5c70dc02003-06-29 00:54:08 +000095 DSGraph *G1 = getGraphForValue(V1);
96 DSGraph *G2 = getGraphForValue(V2);
97 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
Chris Lattner8105cfb2003-02-03 22:50:46 +000098
Chris Lattner5c70dc02003-06-29 00:54:08 +000099 // Get the graph to use...
100 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
Chris Lattnerbd92b732003-06-19 21:15:11 +0000101
Chris Lattner5c70dc02003-06-29 00:54:08 +0000102 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
103 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
104 if (I != GSM.end()) {
105 assert(I->second.getNode() && "Scalar map points to null node?");
106 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
107 if (J != GSM.end()) {
108 assert(J->second.getNode() && "Scalar map points to null node?");
109
110 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
111 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattnerbd92b732003-06-19 21:15:11 +0000112
Misha Brukman2f2d0652003-09-11 18:14:24 +0000113 // We can only make a judgment of one of the nodes is complete...
Chris Lattner5c70dc02003-06-29 00:54:08 +0000114 if (N1->isComplete() || N2->isComplete()) {
115 if (N1 != N2)
116 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000117
Chris Lattner10c45d62003-07-02 23:56:51 +0000118#if 0 // This does not correctly handle arrays!
Chris Lattner5c70dc02003-06-29 00:54:08 +0000119 // Both point to the same node and same offset, and there is only one
120 // physical memory object represented in the node, return must alias.
121 //
122 // FIXME: This isn't correct because we do not handle array indexing
123 // correctly.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000124
Chris Lattner5c70dc02003-06-29 00:54:08 +0000125 if (O1 == O2 && isSinglePhysicalObject(N1))
126 return MustAlias; // Exactly the same object & offset
Chris Lattner10c45d62003-07-02 23:56:51 +0000127#endif
Chris Lattnerbd92b732003-06-19 21:15:11 +0000128
Chris Lattner5c70dc02003-06-29 00:54:08 +0000129 // See if they point to different offsets... if so, we may be able to
130 // determine that they do not alias...
131 if (O1 != O2) {
132 if (O2 < O1) { // Ensure that O1 <= O2
133 std::swap(V1, V2);
134 std::swap(O1, O2);
135 std::swap(V1Size, V2Size);
Chris Lattnerbd92b732003-06-19 21:15:11 +0000136 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000137
138 // FIXME: This is not correct because we do not handle array
139 // indexing correctly with this check!
140 //if (O1+V1Size <= O2) return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000141 }
142 }
143 }
144 }
145
146 // FIXME: we could improve on this by checking the globals graph for aliased
147 // global queries...
Chris Lattnera612afc2003-02-26 19:29:36 +0000148 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000149}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000150
151
152/// getMustAliases - If there are any pointers known that must alias this
153/// pointer, return them now. This allows alias-set based alias analyses to
154/// perform a form a value numbering (which is exposed by load-vn). If an alias
155/// analysis supports this, it should ADD any must aliased pointers to the
156/// specified vector.
157///
158void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Chris Lattner10c45d62003-07-02 23:56:51 +0000159#if 0 // This does not correctly handle arrays!
Chris Lattner9cd04842003-07-02 04:39:13 +0000160 // Currently the only must alias information we can provide is to say that
161 // something is equal to a global value. If we already have a global value,
162 // don't get worked up about it.
163 if (!isa<GlobalValue>(P)) {
164 DSGraph *G = getGraphForValue(P);
165 if (!G) G = &TD->getGlobalsGraph();
166
167 // The only must alias information we can currently determine occurs when
168 // the node for P is a global node with only one entry.
Chris Lattner02da0322004-01-27 21:51:19 +0000169 DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
170 if (I != G->getScalarMap().end()) {
Chris Lattner9cd04842003-07-02 04:39:13 +0000171 DSNode *N = I->second.getNode();
172 if (N->isComplete() && isSinglePhysicalObject(N))
173 RetVals.push_back(N->getGlobals()[0]);
174 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000175 }
Chris Lattner10c45d62003-07-02 23:56:51 +0000176#endif
Chris Lattner5c70dc02003-06-29 00:54:08 +0000177 return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
178}
Brian Gaeked0fde302003-11-11 22:41:34 +0000179