blob: 969147621e8887df36fca89e57e81607be8f92da [file] [log] [blame]
Hal Finkel7529c552014-09-02 21:43:13 +00001//===- CFLAliasAnalysis.cpp - CFL-Based Alias Analysis Implementation ------==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a CFL-based context-insensitive alias analysis
11// algorithm. It does not depend on types. The algorithm is a mixture of the one
12// described in "Demand-driven alias analysis for C" by Xin Zheng and Radu
13// Rugina, and "Fast algorithms for Dyck-CFL-reachability with applications to
14// Alias Analysis" by Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the
15// papers, we build a graph of the uses of a variable, where each node is a
16// memory location, and each edge is an action that happened on that memory
Chad Rosier38c6ad22015-06-19 17:32:57 +000017// location. The "actions" can be one of Dereference, Reference, or Assign.
Hal Finkel7529c552014-09-02 21:43:13 +000018//
19// Two variables are considered as aliasing iff you can reach one value's node
20// from the other value's node and the language formed by concatenating all of
21// the edge labels (actions) conforms to a context-free grammar.
22//
23// Because this algorithm requires a graph search on each query, we execute the
24// algorithm outlined in "Fast algorithms..." (mentioned above)
25// in order to transform the graph into sets of variables that may alias in
George Burgess IV77351ba32016-01-28 00:54:01 +000026// ~nlogn time (n = number of variables), which makes queries take constant
Hal Finkel7529c552014-09-02 21:43:13 +000027// time.
28//===----------------------------------------------------------------------===//
29
George Burgess IV77351ba32016-01-28 00:54:01 +000030// N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and
31// CFLAA is interprocedural. This is *technically* A Bad Thing, because
32// FunctionPasses are only allowed to inspect the Function that they're being
33// run on. Realistically, this likely isn't a problem until we allow
34// FunctionPasses to run concurrently.
35
Chandler Carruth8b046a42015-08-14 02:42:20 +000036#include "llvm/Analysis/CFLAliasAnalysis.h"
Hal Finkel7529c552014-09-02 21:43:13 +000037#include "StratifiedSets.h"
Hal Finkel7529c552014-09-02 21:43:13 +000038#include "llvm/ADT/BitVector.h"
39#include "llvm/ADT/DenseMap.h"
Hal Finkel7529c552014-09-02 21:43:13 +000040#include "llvm/ADT/None.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000041#include "llvm/ADT/Optional.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000042#include "llvm/Analysis/TargetLibraryInfo.h"
Hal Finkel7529c552014-09-02 21:43:13 +000043#include "llvm/IR/Constants.h"
44#include "llvm/IR/Function.h"
Hal Finkel7529c552014-09-02 21:43:13 +000045#include "llvm/IR/InstVisitor.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000046#include "llvm/IR/Instructions.h"
Hal Finkel7529c552014-09-02 21:43:13 +000047#include "llvm/Pass.h"
48#include "llvm/Support/Allocator.h"
Hal Finkel7d7087c2014-09-02 22:13:00 +000049#include "llvm/Support/Compiler.h"
George Burgess IV33305e72015-02-12 03:07:07 +000050#include "llvm/Support/Debug.h"
Hal Finkel7529c552014-09-02 21:43:13 +000051#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include "llvm/Support/raw_ostream.h"
Hal Finkel7529c552014-09-02 21:43:13 +000053#include <algorithm>
54#include <cassert>
Benjamin Kramer799003b2015-03-23 19:32:43 +000055#include <memory>
Hal Finkel7529c552014-09-02 21:43:13 +000056#include <tuple>
57
58using namespace llvm;
59
George Burgess IV33305e72015-02-12 03:07:07 +000060#define DEBUG_TYPE "cfl-aa"
61
Chandler Carruth7b560d42015-09-09 17:55:00 +000062CFLAAResult::CFLAAResult(const TargetLibraryInfo &TLI) : AAResultBase(TLI) {}
63CFLAAResult::CFLAAResult(CFLAAResult &&Arg) : AAResultBase(std::move(Arg)) {}
Chandler Carruth342c6712016-02-20 03:52:02 +000064CFLAAResult::~CFLAAResult() {}
Chandler Carruth8b046a42015-08-14 02:42:20 +000065
66// \brief Information we have about a function and would like to keep around
Chandler Carruth7b560d42015-09-09 17:55:00 +000067struct CFLAAResult::FunctionInfo {
Chandler Carruth8b046a42015-08-14 02:42:20 +000068 StratifiedSets<Value *> Sets;
69 // Lots of functions have < 4 returns. Adjust as necessary.
70 SmallVector<Value *, 4> ReturnedValues;
71
72 FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV)
73 : Sets(std::move(S)), ReturnedValues(std::move(RV)) {}
74};
75
Hal Finkel7529c552014-09-02 21:43:13 +000076// Try to go from a Value* to a Function*. Never returns nullptr.
77static Optional<Function *> parentFunctionOfValue(Value *);
78
79// Returns possible functions called by the Inst* into the given
80// SmallVectorImpl. Returns true if targets found, false otherwise.
81// This is templated because InvokeInst/CallInst give us the same
82// set of functions that we care about, and I don't like repeating
83// myself.
84template <typename Inst>
85static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
86
87// Some instructions need to have their users tracked. Instructions like
88// `add` require you to get the users of the Instruction* itself, other
89// instructions like `store` require you to get the users of the first
90// operand. This function gets the "proper" value to track for each
91// type of instruction we support.
92static Optional<Value *> getTargetValue(Instruction *);
93
94// There are certain instructions (i.e. FenceInst, etc.) that we ignore.
95// This notes that we should ignore those.
96static bool hasUsefulEdges(Instruction *);
97
Hal Finkel1ae325f2014-09-02 23:50:01 +000098const StratifiedIndex StratifiedLink::SetSentinel =
George Burgess IV11d509d2015-03-15 00:52:21 +000099 std::numeric_limits<StratifiedIndex>::max();
Hal Finkel1ae325f2014-09-02 23:50:01 +0000100
Hal Finkel7529c552014-09-02 21:43:13 +0000101namespace {
102// StratifiedInfo Attribute things.
103typedef unsigned StratifiedAttr;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000104LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
105LLVM_CONSTEXPR unsigned AttrAllIndex = 0;
106LLVM_CONSTEXPR unsigned AttrGlobalIndex = 1;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000107LLVM_CONSTEXPR unsigned AttrUnknownIndex = 2;
108LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000109LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
110LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
Hal Finkel7529c552014-09-02 21:43:13 +0000111
Hal Finkel7d7087c2014-09-02 22:13:00 +0000112LLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000113LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000114LLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone;
Hal Finkel7529c552014-09-02 21:43:13 +0000115
116// \brief StratifiedSets call for knowledge of "direction", so this is how we
117// represent that locally.
118enum class Level { Same, Above, Below };
119
120// \brief Edges can be one of four "weights" -- each weight must have an inverse
121// weight (Assign has Assign; Reference has Dereference).
122enum class EdgeType {
123 // The weight assigned when assigning from or to a value. For example, in:
124 // %b = getelementptr %a, 0
125 // ...The relationships are %b assign %a, and %a assign %b. This used to be
126 // two edges, but having a distinction bought us nothing.
127 Assign,
128
129 // The edge used when we have an edge going from some handle to a Value.
130 // Examples of this include:
131 // %b = load %a (%b Dereference %a)
132 // %b = extractelement %a, 0 (%a Dereference %b)
133 Dereference,
134
135 // The edge used when our edge goes from a value to a handle that may have
136 // contained it at some point. Examples:
137 // %b = load %a (%a Reference %b)
138 // %b = extractelement %a, 0 (%b Reference %a)
139 Reference
140};
141
142// \brief Encodes the notion of a "use"
143struct Edge {
144 // \brief Which value the edge is coming from
145 Value *From;
146
147 // \brief Which value the edge is pointing to
148 Value *To;
149
150 // \brief Edge weight
151 EdgeType Weight;
152
153 // \brief Whether we aliased any external values along the way that may be
154 // invisible to the analysis (i.e. landingpad for exceptions, calls for
155 // interprocedural analysis, etc.)
156 StratifiedAttrs AdditionalAttrs;
157
158 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
159 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
160};
161
Hal Finkel7529c552014-09-02 21:43:13 +0000162// \brief Gets the edges our graph should have, based on an Instruction*
163class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000164 CFLAAResult &AA;
Hal Finkel7529c552014-09-02 21:43:13 +0000165 SmallVectorImpl<Edge> &Output;
166
167public:
Chandler Carruth7b560d42015-09-09 17:55:00 +0000168 GetEdgesVisitor(CFLAAResult &AA, SmallVectorImpl<Edge> &Output)
Hal Finkel7529c552014-09-02 21:43:13 +0000169 : AA(AA), Output(Output) {}
170
171 void visitInstruction(Instruction &) {
172 llvm_unreachable("Unsupported instruction encountered");
173 }
174
George Burgess IVb54a8d622015-03-10 02:40:06 +0000175 void visitPtrToIntInst(PtrToIntInst &Inst) {
176 auto *Ptr = Inst.getOperand(0);
177 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
178 }
179
180 void visitIntToPtrInst(IntToPtrInst &Inst) {
181 auto *Ptr = &Inst;
182 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
183 }
184
Hal Finkel7529c552014-09-02 21:43:13 +0000185 void visitCastInst(CastInst &Inst) {
George Burgess IV11d509d2015-03-15 00:52:21 +0000186 Output.push_back(
187 Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000188 }
189
190 void visitBinaryOperator(BinaryOperator &Inst) {
191 auto *Op1 = Inst.getOperand(0);
192 auto *Op2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000193 Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone));
194 Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000195 }
196
197 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
198 auto *Ptr = Inst.getPointerOperand();
199 auto *Val = Inst.getNewValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000200 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000201 }
202
203 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
204 auto *Ptr = Inst.getPointerOperand();
205 auto *Val = Inst.getValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000206 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000207 }
208
209 void visitPHINode(PHINode &Inst) {
George Burgess IV77351ba32016-01-28 00:54:01 +0000210 for (Value *Val : Inst.incoming_values())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000211 Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000212 }
213
214 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
215 auto *Op = Inst.getPointerOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000216 Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000217 for (auto I = Inst.idx_begin(), E = Inst.idx_end(); I != E; ++I)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000218 Output.push_back(Edge(&Inst, *I, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000219 }
220
221 void visitSelectInst(SelectInst &Inst) {
Daniel Berlin16f7a522015-01-26 17:31:17 +0000222 // Condition is not processed here (The actual statement producing
223 // the condition result is processed elsewhere). For select, the
224 // condition is evaluated, but not loaded, stored, or assigned
225 // simply as a result of being the condition of a select.
226
Hal Finkel7529c552014-09-02 21:43:13 +0000227 auto *TrueVal = Inst.getTrueValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000228 Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000229 auto *FalseVal = Inst.getFalseValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000230 Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000231 }
232
233 void visitAllocaInst(AllocaInst &) {}
234
235 void visitLoadInst(LoadInst &Inst) {
236 auto *Ptr = Inst.getPointerOperand();
237 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000238 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000239 }
240
241 void visitStoreInst(StoreInst &Inst) {
242 auto *Ptr = Inst.getPointerOperand();
243 auto *Val = Inst.getValueOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000244 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000245 }
246
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000247 void visitVAArgInst(VAArgInst &Inst) {
248 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
249 // two things:
250 // 1. Loads a value from *((T*)*Ptr).
251 // 2. Increments (stores to) *Ptr by some target-specific amount.
252 // For now, we'll handle this like a landingpad instruction (by placing the
253 // result in its own group, and having that group alias externals).
254 auto *Val = &Inst;
255 Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll));
256 }
257
Hal Finkel7529c552014-09-02 21:43:13 +0000258 static bool isFunctionExternal(Function *Fn) {
259 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
260 }
261
262 // Gets whether the sets at Index1 above, below, or equal to the sets at
263 // Index2. Returns None if they are not in the same set chain.
264 static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
265 StratifiedIndex Index1,
266 StratifiedIndex Index2) {
267 if (Index1 == Index2)
268 return Level::Same;
269
270 const auto *Current = &Sets.getLink(Index1);
271 while (Current->hasBelow()) {
272 if (Current->Below == Index2)
273 return Level::Below;
274 Current = &Sets.getLink(Current->Below);
275 }
276
277 Current = &Sets.getLink(Index1);
278 while (Current->hasAbove()) {
279 if (Current->Above == Index2)
280 return Level::Above;
281 Current = &Sets.getLink(Current->Above);
282 }
283
George Burgess IV77351ba32016-01-28 00:54:01 +0000284 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000285 }
286
287 bool
288 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
289 Value *FuncValue,
290 const iterator_range<User::op_iterator> &Args) {
Hal Finkelca616ac2014-09-02 23:29:48 +0000291 const unsigned ExpectedMaxArgs = 8;
292 const unsigned MaxSupportedArgs = 50;
Hal Finkel7529c552014-09-02 21:43:13 +0000293 assert(Fns.size() > 0);
294
295 // I put this here to give us an upper bound on time taken by IPA. Is it
296 // really (realistically) needed? Keep in mind that we do have an n^2 algo.
George Burgess IVab03af22015-03-10 02:58:15 +0000297 if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000298 return false;
299
300 // Exit early if we'll fail anyway
301 for (auto *Fn : Fns) {
302 if (isFunctionExternal(Fn) || Fn->isVarArg())
303 return false;
304 auto &MaybeInfo = AA.ensureCached(Fn);
305 if (!MaybeInfo.hasValue())
306 return false;
307 }
308
309 SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
310 SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
311 for (auto *Fn : Fns) {
312 auto &Info = *AA.ensureCached(Fn);
313 auto &Sets = Info.Sets;
314 auto &RetVals = Info.ReturnedValues;
315
316 Parameters.clear();
317 for (auto &Param : Fn->args()) {
318 auto MaybeInfo = Sets.find(&Param);
319 // Did a new parameter somehow get added to the function/slip by?
320 if (!MaybeInfo.hasValue())
321 return false;
322 Parameters.push_back(*MaybeInfo);
323 }
324
325 // Adding an edge from argument -> return value for each parameter that
326 // may alias the return value
327 for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
328 auto &ParamInfo = Parameters[I];
329 auto &ArgVal = Arguments[I];
330 bool AddEdge = false;
331 StratifiedAttrs Externals;
332 for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
333 auto MaybeInfo = Sets.find(RetVals[X]);
334 if (!MaybeInfo.hasValue())
335 return false;
336
337 auto &RetInfo = *MaybeInfo;
338 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
339 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
340 auto MaybeRelation =
341 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
342 if (MaybeRelation.hasValue()) {
343 AddEdge = true;
344 Externals |= RetAttrs | ParamAttrs;
345 }
346 }
347 if (AddEdge)
Hal Finkelca616ac2014-09-02 23:29:48 +0000348 Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign,
George Burgess IV11d509d2015-03-15 00:52:21 +0000349 StratifiedAttrs().flip()));
Hal Finkel7529c552014-09-02 21:43:13 +0000350 }
351
352 if (Parameters.size() != Arguments.size())
353 return false;
354
355 // Adding edges between arguments for arguments that may end up aliasing
356 // each other. This is necessary for functions such as
357 // void foo(int** a, int** b) { *a = *b; }
358 // (Technically, the proper sets for this would be those below
359 // Arguments[I] and Arguments[X], but our algorithm will produce
360 // extremely similar, and equally correct, results either way)
361 for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
362 auto &MainVal = Arguments[I];
363 auto &MainInfo = Parameters[I];
364 auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
365 for (unsigned X = I + 1; X != E; ++X) {
366 auto &SubInfo = Parameters[X];
367 auto &SubVal = Arguments[X];
368 auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
369 auto MaybeRelation =
370 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
371
372 if (!MaybeRelation.hasValue())
373 continue;
374
375 auto NewAttrs = SubAttrs | MainAttrs;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000376 Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
Hal Finkel7529c552014-09-02 21:43:13 +0000377 }
378 }
379 }
380 return true;
381 }
382
383 template <typename InstT> void visitCallLikeInst(InstT &Inst) {
George Burgess IV68b36e02015-08-28 00:16:18 +0000384 // TODO: Add support for noalias args/all the other fun function attributes
385 // that we can tack on.
Hal Finkel7529c552014-09-02 21:43:13 +0000386 SmallVector<Function *, 4> Targets;
387 if (getPossibleTargets(&Inst, Targets)) {
388 if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
389 return;
390 // Cleanup from interprocedural analysis
391 Output.clear();
392 }
393
George Burgess IV68b36e02015-08-28 00:16:18 +0000394 // Because the function is opaque, we need to note that anything
395 // could have happened to the arguments, and that the result could alias
396 // just about anything, too.
397 // The goal of the loop is in part to unify many Values into one set, so we
398 // don't care if the function is void there.
Hal Finkel7529c552014-09-02 21:43:13 +0000399 for (Value *V : Inst.arg_operands())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000400 Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll));
George Burgess IV68b36e02015-08-28 00:16:18 +0000401 if (Inst.getNumArgOperands() == 0 &&
402 Inst.getType() != Type::getVoidTy(Inst.getContext()))
403 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
Hal Finkel7529c552014-09-02 21:43:13 +0000404 }
405
406 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
407
408 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
409
410 // Because vectors/aggregates are immutable and unaddressable,
411 // there's nothing we can do to coax a value out of them, other
412 // than calling Extract{Element,Value}. We can effectively treat
413 // them as pointers to arbitrary memory locations we can store in
414 // and load from.
415 void visitExtractElementInst(ExtractElementInst &Inst) {
416 auto *Ptr = Inst.getVectorOperand();
417 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000418 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000419 }
420
421 void visitInsertElementInst(InsertElementInst &Inst) {
422 auto *Vec = Inst.getOperand(0);
423 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000424 Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
425 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000426 }
427
428 void visitLandingPadInst(LandingPadInst &Inst) {
429 // Exceptions come from "nowhere", from our analysis' perspective.
430 // So we place the instruction its own group, noting that said group may
431 // alias externals
Hal Finkel8d1590d2014-09-02 22:52:30 +0000432 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
Hal Finkel7529c552014-09-02 21:43:13 +0000433 }
434
435 void visitInsertValueInst(InsertValueInst &Inst) {
436 auto *Agg = Inst.getOperand(0);
437 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000438 Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
439 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000440 }
441
442 void visitExtractValueInst(ExtractValueInst &Inst) {
443 auto *Ptr = Inst.getAggregateOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000444 Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000445 }
446
447 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
448 auto *From1 = Inst.getOperand(0);
449 auto *From2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000450 Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
451 Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000452 }
Pete Cooper36642532015-06-12 16:13:54 +0000453
454 void visitConstantExpr(ConstantExpr *CE) {
455 switch (CE->getOpcode()) {
456 default:
457 llvm_unreachable("Unknown instruction type encountered!");
458// Build the switch statement using the Instruction.def file.
459#define HANDLE_INST(NUM, OPCODE, CLASS) \
460 case Instruction::OPCODE: \
461 visit##OPCODE(*(CLASS *)CE); \
462 break;
463#include "llvm/IR/Instruction.def"
464 }
465 }
Hal Finkel7529c552014-09-02 21:43:13 +0000466};
467
468// For a given instruction, we need to know which Value* to get the
469// users of in order to build our graph. In some cases (i.e. add),
470// we simply need the Instruction*. In other cases (i.e. store),
471// finding the users of the Instruction* is useless; we need to find
472// the users of the first operand. This handles determining which
473// value to follow for us.
474//
475// Note: we *need* to keep this in sync with GetEdgesVisitor. Add
476// something to GetEdgesVisitor, add it here -- remove something from
477// GetEdgesVisitor, remove it here.
478class GetTargetValueVisitor
479 : public InstVisitor<GetTargetValueVisitor, Value *> {
480public:
481 Value *visitInstruction(Instruction &Inst) { return &Inst; }
482
483 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
484
485 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
486 return Inst.getPointerOperand();
487 }
488
489 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
490 return Inst.getPointerOperand();
491 }
492
493 Value *visitInsertElementInst(InsertElementInst &Inst) {
494 return Inst.getOperand(0);
495 }
496
497 Value *visitInsertValueInst(InsertValueInst &Inst) {
498 return Inst.getAggregateOperand();
499 }
500};
501
502// Set building requires a weighted bidirectional graph.
503template <typename EdgeTypeT> class WeightedBidirectionalGraph {
504public:
505 typedef std::size_t Node;
506
507private:
Hal Finkelca616ac2014-09-02 23:29:48 +0000508 const static Node StartNode = Node(0);
Hal Finkel7529c552014-09-02 21:43:13 +0000509
510 struct Edge {
511 EdgeTypeT Weight;
512 Node Other;
513
George Burgess IV11d509d2015-03-15 00:52:21 +0000514 Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {}
Hal Finkelca616ac2014-09-02 23:29:48 +0000515
Hal Finkel7529c552014-09-02 21:43:13 +0000516 bool operator==(const Edge &E) const {
517 return Weight == E.Weight && Other == E.Other;
518 }
519
520 bool operator!=(const Edge &E) const { return !operator==(E); }
521 };
522
523 struct NodeImpl {
524 std::vector<Edge> Edges;
525 };
526
527 std::vector<NodeImpl> NodeImpls;
528
529 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
530
531 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
532 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
533
534public:
535 // ----- Various Edge iterators for the graph ----- //
536
537 // \brief Iterator for edges. Because this graph is bidirected, we don't
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000538 // allow modification of the edges using this iterator. Additionally, the
Hal Finkel7529c552014-09-02 21:43:13 +0000539 // iterator becomes invalid if you add edges to or from the node you're
540 // getting the edges of.
541 struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
542 std::tuple<EdgeTypeT, Node *>> {
543 EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
544 : Current(Iter) {}
545
546 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
547
548 EdgeIterator &operator++() {
549 ++Current;
550 return *this;
551 }
552
553 EdgeIterator operator++(int) {
554 EdgeIterator Copy(Current);
555 operator++();
556 return Copy;
557 }
558
559 std::tuple<EdgeTypeT, Node> &operator*() {
560 Store = std::make_tuple(Current->Weight, Current->Other);
561 return Store;
562 }
563
564 bool operator==(const EdgeIterator &Other) const {
565 return Current == Other.Current;
566 }
567
568 bool operator!=(const EdgeIterator &Other) const {
569 return !operator==(Other);
570 }
571
572 private:
573 typename std::vector<Edge>::const_iterator Current;
574 std::tuple<EdgeTypeT, Node> Store;
575 };
576
577 // Wrapper for EdgeIterator with begin()/end() calls.
578 struct EdgeIterable {
579 EdgeIterable(const std::vector<Edge> &Edges)
580 : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
581
582 EdgeIterator begin() { return EdgeIterator(BeginIter); }
583
584 EdgeIterator end() { return EdgeIterator(EndIter); }
585
586 private:
587 typename std::vector<Edge>::const_iterator BeginIter;
588 typename std::vector<Edge>::const_iterator EndIter;
589 };
590
591 // ----- Actual graph-related things ----- //
592
Hal Finkelca616ac2014-09-02 23:29:48 +0000593 WeightedBidirectionalGraph() {}
Hal Finkel7529c552014-09-02 21:43:13 +0000594
595 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
596 : NodeImpls(std::move(Other.NodeImpls)) {}
597
598 WeightedBidirectionalGraph<EdgeTypeT> &
599 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
600 NodeImpls = std::move(Other.NodeImpls);
601 return *this;
602 }
603
604 Node addNode() {
605 auto Index = NodeImpls.size();
606 auto NewNode = Node(Index);
607 NodeImpls.push_back(NodeImpl());
608 return NewNode;
609 }
610
611 void addEdge(Node From, Node To, const EdgeTypeT &Weight,
612 const EdgeTypeT &ReverseWeight) {
613 assert(inbounds(From));
614 assert(inbounds(To));
615 auto &FromNode = getNode(From);
616 auto &ToNode = getNode(To);
Hal Finkelca616ac2014-09-02 23:29:48 +0000617 FromNode.Edges.push_back(Edge(Weight, To));
618 ToNode.Edges.push_back(Edge(ReverseWeight, From));
Hal Finkel7529c552014-09-02 21:43:13 +0000619 }
620
621 EdgeIterable edgesFor(const Node &N) const {
622 const auto &Node = getNode(N);
623 return EdgeIterable(Node.Edges);
624 }
625
626 bool empty() const { return NodeImpls.empty(); }
627 std::size_t size() const { return NodeImpls.size(); }
628
629 // \brief Gets an arbitrary node in the graph as a starting point for
630 // traversal.
631 Node getEntryNode() {
632 assert(inbounds(StartNode));
633 return StartNode;
634 }
635};
636
637typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
638typedef DenseMap<Value *, GraphT::Node> NodeMapT;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000639}
Hal Finkel7529c552014-09-02 21:43:13 +0000640
Hal Finkel7529c552014-09-02 21:43:13 +0000641//===----------------------------------------------------------------------===//
642// Function declarations that require types defined in the namespace above
643//===----------------------------------------------------------------------===//
644
645// Given an argument number, returns the appropriate Attr index to set.
646static StratifiedAttr argNumberToAttrIndex(StratifiedAttr);
647
648// Given a Value, potentially return which AttrIndex it maps to.
649static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
650
651// Gets the inverse of a given EdgeType.
652static EdgeType flipWeight(EdgeType);
653
654// Gets edges of the given Instruction*, writing them to the SmallVector*.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000655static void argsToEdges(CFLAAResult &, Instruction *, SmallVectorImpl<Edge> &);
Hal Finkel7529c552014-09-02 21:43:13 +0000656
Pete Cooper36642532015-06-12 16:13:54 +0000657// Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000658static void argsToEdges(CFLAAResult &, ConstantExpr *, SmallVectorImpl<Edge> &);
Pete Cooper36642532015-06-12 16:13:54 +0000659
Hal Finkel7529c552014-09-02 21:43:13 +0000660// Gets the "Level" that one should travel in StratifiedSets
661// given an EdgeType.
662static Level directionOfEdgeType(EdgeType);
663
664// Builds the graph needed for constructing the StratifiedSets for the
665// given function
Chandler Carruth7b560d42015-09-09 17:55:00 +0000666static void buildGraphFrom(CFLAAResult &, Function *,
Hal Finkel7529c552014-09-02 21:43:13 +0000667 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
668
George Burgess IVab03af22015-03-10 02:58:15 +0000669// Gets the edges of a ConstantExpr as if it was an Instruction. This
670// function also acts on any nested ConstantExprs, adding the edges
671// of those to the given SmallVector as well.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000672static void constexprToEdges(CFLAAResult &, ConstantExpr &,
George Burgess IVab03af22015-03-10 02:58:15 +0000673 SmallVectorImpl<Edge> &);
674
675// Given an Instruction, this will add it to the graph, along with any
676// Instructions that are potentially only available from said Instruction
677// For example, given the following line:
678// %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
679// addInstructionToGraph would add both the `load` and `getelementptr`
680// instructions to the graph appropriately.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000681static void addInstructionToGraph(CFLAAResult &, Instruction &,
George Burgess IVab03af22015-03-10 02:58:15 +0000682 SmallVectorImpl<Value *> &, NodeMapT &,
683 GraphT &);
684
685// Notes whether it would be pointless to add the given Value to our sets.
686static bool canSkipAddingToSets(Value *Val);
687
Hal Finkel7529c552014-09-02 21:43:13 +0000688static Optional<Function *> parentFunctionOfValue(Value *Val) {
689 if (auto *Inst = dyn_cast<Instruction>(Val)) {
690 auto *Bb = Inst->getParent();
691 return Bb->getParent();
692 }
693
694 if (auto *Arg = dyn_cast<Argument>(Val))
695 return Arg->getParent();
George Burgess IV77351ba32016-01-28 00:54:01 +0000696 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000697}
698
699template <typename Inst>
700static bool getPossibleTargets(Inst *Call,
701 SmallVectorImpl<Function *> &Output) {
702 if (auto *Fn = Call->getCalledFunction()) {
703 Output.push_back(Fn);
704 return true;
705 }
706
707 // TODO: If the call is indirect, we might be able to enumerate all potential
708 // targets of the call and return them, rather than just failing.
709 return false;
710}
711
712static Optional<Value *> getTargetValue(Instruction *Inst) {
713 GetTargetValueVisitor V;
714 return V.visit(Inst);
715}
716
717static bool hasUsefulEdges(Instruction *Inst) {
718 bool IsNonInvokeTerminator =
719 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
720 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
721}
722
Pete Cooper36642532015-06-12 16:13:54 +0000723static bool hasUsefulEdges(ConstantExpr *CE) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000724 // ConstantExpr doesn't have terminators, invokes, or fences, so only needs
Pete Cooper36642532015-06-12 16:13:54 +0000725 // to check for compares.
726 return CE->getOpcode() != Instruction::ICmp &&
727 CE->getOpcode() != Instruction::FCmp;
728}
729
Hal Finkel7529c552014-09-02 21:43:13 +0000730static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
731 if (isa<GlobalValue>(Val))
732 return AttrGlobalIndex;
733
734 if (auto *Arg = dyn_cast<Argument>(Val))
Daniel Berlin16f7a522015-01-26 17:31:17 +0000735 // Only pointer arguments should have the argument attribute,
736 // because things can't escape through scalars without us seeing a
737 // cast, and thus, interaction with them doesn't matter.
738 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
Hal Finkel7529c552014-09-02 21:43:13 +0000739 return argNumberToAttrIndex(Arg->getArgNo());
George Burgess IV77351ba32016-01-28 00:54:01 +0000740 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000741}
742
743static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
George Burgess IV3c898c22015-01-21 16:37:21 +0000744 if (ArgNum >= AttrMaxNumArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000745 return AttrAllIndex;
746 return ArgNum + AttrFirstArgIndex;
747}
748
749static EdgeType flipWeight(EdgeType Initial) {
750 switch (Initial) {
751 case EdgeType::Assign:
752 return EdgeType::Assign;
753 case EdgeType::Dereference:
754 return EdgeType::Reference;
755 case EdgeType::Reference:
756 return EdgeType::Dereference;
757 }
758 llvm_unreachable("Incomplete coverage of EdgeType enum");
759}
760
Chandler Carruth7b560d42015-09-09 17:55:00 +0000761static void argsToEdges(CFLAAResult &Analysis, Instruction *Inst,
Hal Finkel7529c552014-09-02 21:43:13 +0000762 SmallVectorImpl<Edge> &Output) {
George Burgess IVab03af22015-03-10 02:58:15 +0000763 assert(hasUsefulEdges(Inst) &&
764 "Expected instructions to have 'useful' edges");
Hal Finkel7529c552014-09-02 21:43:13 +0000765 GetEdgesVisitor v(Analysis, Output);
766 v.visit(Inst);
767}
768
Chandler Carruth7b560d42015-09-09 17:55:00 +0000769static void argsToEdges(CFLAAResult &Analysis, ConstantExpr *CE,
Pete Cooper36642532015-06-12 16:13:54 +0000770 SmallVectorImpl<Edge> &Output) {
771 assert(hasUsefulEdges(CE) && "Expected constant expr to have 'useful' edges");
772 GetEdgesVisitor v(Analysis, Output);
773 v.visitConstantExpr(CE);
774}
775
Hal Finkel7529c552014-09-02 21:43:13 +0000776static Level directionOfEdgeType(EdgeType Weight) {
777 switch (Weight) {
778 case EdgeType::Reference:
779 return Level::Above;
780 case EdgeType::Dereference:
781 return Level::Below;
782 case EdgeType::Assign:
783 return Level::Same;
784 }
785 llvm_unreachable("Incomplete switch coverage");
786}
787
Chandler Carruth7b560d42015-09-09 17:55:00 +0000788static void constexprToEdges(CFLAAResult &Analysis,
George Burgess IVab03af22015-03-10 02:58:15 +0000789 ConstantExpr &CExprToCollapse,
790 SmallVectorImpl<Edge> &Results) {
791 SmallVector<ConstantExpr *, 4> Worklist;
792 Worklist.push_back(&CExprToCollapse);
793
794 SmallVector<Edge, 8> ConstexprEdges;
Pete Cooper36642532015-06-12 16:13:54 +0000795 SmallPtrSet<ConstantExpr *, 4> Visited;
George Burgess IVab03af22015-03-10 02:58:15 +0000796 while (!Worklist.empty()) {
797 auto *CExpr = Worklist.pop_back_val();
George Burgess IVab03af22015-03-10 02:58:15 +0000798
Pete Cooper36642532015-06-12 16:13:54 +0000799 if (!hasUsefulEdges(CExpr))
George Burgess IVab03af22015-03-10 02:58:15 +0000800 continue;
801
802 ConstexprEdges.clear();
Pete Cooper36642532015-06-12 16:13:54 +0000803 argsToEdges(Analysis, CExpr, ConstexprEdges);
George Burgess IVab03af22015-03-10 02:58:15 +0000804 for (auto &Edge : ConstexprEdges) {
Pete Cooper36642532015-06-12 16:13:54 +0000805 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
806 if (Visited.insert(Nested).second)
807 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000808
Pete Cooper36642532015-06-12 16:13:54 +0000809 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
810 if (Visited.insert(Nested).second)
811 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000812 }
813
814 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
815 }
816}
817
Chandler Carruth7b560d42015-09-09 17:55:00 +0000818static void addInstructionToGraph(CFLAAResult &Analysis, Instruction &Inst,
George Burgess IVab03af22015-03-10 02:58:15 +0000819 SmallVectorImpl<Value *> &ReturnedValues,
820 NodeMapT &Map, GraphT &Graph) {
Hal Finkel7529c552014-09-02 21:43:13 +0000821 const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
822 auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
823 auto &Iter = Pair.first;
824 if (Pair.second) {
825 auto NewNode = Graph.addNode();
826 Iter->second = NewNode;
827 }
828 return Iter->second;
829 };
830
George Burgess IVab03af22015-03-10 02:58:15 +0000831 // We don't want the edges of most "return" instructions, but we *do* want
832 // to know what can be returned.
833 if (isa<ReturnInst>(&Inst))
834 ReturnedValues.push_back(&Inst);
835
836 if (!hasUsefulEdges(&Inst))
837 return;
838
Hal Finkel7529c552014-09-02 21:43:13 +0000839 SmallVector<Edge, 8> Edges;
George Burgess IVab03af22015-03-10 02:58:15 +0000840 argsToEdges(Analysis, &Inst, Edges);
Hal Finkel7529c552014-09-02 21:43:13 +0000841
George Burgess IVab03af22015-03-10 02:58:15 +0000842 // In the case of an unused alloca (or similar), edges may be empty. Note
843 // that it exists so we can potentially answer NoAlias.
844 if (Edges.empty()) {
845 auto MaybeVal = getTargetValue(&Inst);
846 assert(MaybeVal.hasValue());
847 auto *Target = *MaybeVal;
848 findOrInsertNode(Target);
849 return;
Hal Finkel7529c552014-09-02 21:43:13 +0000850 }
George Burgess IVab03af22015-03-10 02:58:15 +0000851
852 const auto addEdgeToGraph = [&Graph, &findOrInsertNode](const Edge &E) {
853 auto To = findOrInsertNode(E.To);
854 auto From = findOrInsertNode(E.From);
855 auto FlippedWeight = flipWeight(E.Weight);
856 auto Attrs = E.AdditionalAttrs;
857 Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
858 std::make_pair(FlippedWeight, Attrs));
859 };
860
861 SmallVector<ConstantExpr *, 4> ConstantExprs;
862 for (const Edge &E : Edges) {
863 addEdgeToGraph(E);
864 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
865 ConstantExprs.push_back(Constexpr);
866 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
867 ConstantExprs.push_back(Constexpr);
868 }
869
870 for (ConstantExpr *CE : ConstantExprs) {
871 Edges.clear();
872 constexprToEdges(Analysis, *CE, Edges);
873 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
874 }
875}
876
877// Aside: We may remove graph construction entirely, because it doesn't really
878// buy us much that we don't already have. I'd like to add interprocedural
879// analysis prior to this however, in case that somehow requires the graph
880// produced by this for efficient execution
Chandler Carruth7b560d42015-09-09 17:55:00 +0000881static void buildGraphFrom(CFLAAResult &Analysis, Function *Fn,
George Burgess IVab03af22015-03-10 02:58:15 +0000882 SmallVectorImpl<Value *> &ReturnedValues,
883 NodeMapT &Map, GraphT &Graph) {
884 for (auto &Bb : Fn->getBasicBlockList())
885 for (auto &Inst : Bb.getInstList())
886 addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph);
887}
888
889static bool canSkipAddingToSets(Value *Val) {
890 // Constants can share instances, which may falsely unify multiple
891 // sets, e.g. in
892 // store i32* null, i32** %ptr1
893 // store i32* null, i32** %ptr2
894 // clearly ptr1 and ptr2 should not be unified into the same set, so
895 // we should filter out the (potentially shared) instance to
896 // i32* null.
897 if (isa<Constant>(Val)) {
898 bool Container = isa<ConstantVector>(Val) || isa<ConstantArray>(Val) ||
899 isa<ConstantStruct>(Val);
900 // TODO: Because all of these things are constant, we can determine whether
901 // the data is *actually* mutable at graph building time. This will probably
902 // come for free/cheap with offset awareness.
903 bool CanStoreMutableData =
904 isa<GlobalValue>(Val) || isa<ConstantExpr>(Val) || Container;
905 return !CanStoreMutableData;
906 }
907
908 return false;
Hal Finkel7529c552014-09-02 21:43:13 +0000909}
910
Chandler Carruth8b046a42015-08-14 02:42:20 +0000911// Builds the graph + StratifiedSets for a function.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000912CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) {
Hal Finkel7529c552014-09-02 21:43:13 +0000913 NodeMapT Map;
914 GraphT Graph;
915 SmallVector<Value *, 4> ReturnedValues;
916
Chandler Carruth8b046a42015-08-14 02:42:20 +0000917 buildGraphFrom(*this, Fn, ReturnedValues, Map, Graph);
Hal Finkel7529c552014-09-02 21:43:13 +0000918
919 DenseMap<GraphT::Node, Value *> NodeValueMap;
920 NodeValueMap.resize(Map.size());
921 for (const auto &Pair : Map)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000922 NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
Hal Finkel7529c552014-09-02 21:43:13 +0000923
924 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
925 auto ValIter = NodeValueMap.find(Node);
926 assert(ValIter != NodeValueMap.end());
927 return ValIter->second;
928 };
929
930 StratifiedSetsBuilder<Value *> Builder;
931
932 SmallVector<GraphT::Node, 16> Worklist;
933 for (auto &Pair : Map) {
934 Worklist.clear();
935
936 auto *Value = Pair.first;
937 Builder.add(Value);
938 auto InitialNode = Pair.second;
939 Worklist.push_back(InitialNode);
940 while (!Worklist.empty()) {
941 auto Node = Worklist.pop_back_val();
942 auto *CurValue = findValueOrDie(Node);
George Burgess IVab03af22015-03-10 02:58:15 +0000943 if (canSkipAddingToSets(CurValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000944 continue;
945
946 for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
947 auto Weight = std::get<0>(EdgeTuple);
948 auto Label = Weight.first;
949 auto &OtherNode = std::get<1>(EdgeTuple);
950 auto *OtherValue = findValueOrDie(OtherNode);
951
George Burgess IVab03af22015-03-10 02:58:15 +0000952 if (canSkipAddingToSets(OtherValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000953 continue;
954
955 bool Added;
956 switch (directionOfEdgeType(Label)) {
957 case Level::Above:
958 Added = Builder.addAbove(CurValue, OtherValue);
959 break;
960 case Level::Below:
961 Added = Builder.addBelow(CurValue, OtherValue);
962 break;
963 case Level::Same:
964 Added = Builder.addWith(CurValue, OtherValue);
965 break;
966 }
967
George Burgess IVb54a8d622015-03-10 02:40:06 +0000968 auto Aliasing = Weight.second;
969 if (auto MaybeCurIndex = valueToAttrIndex(CurValue))
970 Aliasing.set(*MaybeCurIndex);
971 if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
972 Aliasing.set(*MaybeOtherIndex);
973 Builder.noteAttributes(CurValue, Aliasing);
974 Builder.noteAttributes(OtherValue, Aliasing);
975
976 if (Added)
Hal Finkel7529c552014-09-02 21:43:13 +0000977 Worklist.push_back(OtherNode);
Hal Finkel7529c552014-09-02 21:43:13 +0000978 }
979 }
980 }
981
982 // There are times when we end up with parameters not in our graph (i.e. if
983 // it's only used as the condition of a branch). Other bits of code depend on
984 // things that were present during construction being present in the graph.
985 // So, we add all present arguments here.
986 for (auto &Arg : Fn->args()) {
George Burgess IVab03af22015-03-10 02:58:15 +0000987 if (!Builder.add(&Arg))
988 continue;
989
990 auto Attrs = valueToAttrIndex(&Arg);
991 if (Attrs.hasValue())
992 Builder.noteAttributes(&Arg, *Attrs);
Hal Finkel7529c552014-09-02 21:43:13 +0000993 }
994
Hal Finkel85f26922014-09-03 00:06:47 +0000995 return FunctionInfo(Builder.build(), std::move(ReturnedValues));
Hal Finkel7529c552014-09-02 21:43:13 +0000996}
997
Chandler Carruth7b560d42015-09-09 17:55:00 +0000998void CFLAAResult::scan(Function *Fn) {
Hal Finkel8d1590d2014-09-02 22:52:30 +0000999 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
Hal Finkel7529c552014-09-02 21:43:13 +00001000 (void)InsertPair;
1001 assert(InsertPair.second &&
1002 "Trying to scan a function that has already been cached");
1003
Chandler Carruth8b046a42015-08-14 02:42:20 +00001004 FunctionInfo Info(buildSetsFrom(Fn));
Hal Finkel7529c552014-09-02 21:43:13 +00001005 Cache[Fn] = std::move(Info);
1006 Handles.push_front(FunctionHandle(Fn, this));
1007}
1008
Chandler Carruth7b560d42015-09-09 17:55:00 +00001009void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); }
Chandler Carruth8b046a42015-08-14 02:42:20 +00001010
1011/// \brief Ensures that the given function is available in the cache.
1012/// Returns the appropriate entry from the cache.
Chandler Carruth7b560d42015-09-09 17:55:00 +00001013const Optional<CFLAAResult::FunctionInfo> &
1014CFLAAResult::ensureCached(Function *Fn) {
Chandler Carruth8b046a42015-08-14 02:42:20 +00001015 auto Iter = Cache.find(Fn);
1016 if (Iter == Cache.end()) {
1017 scan(Fn);
1018 Iter = Cache.find(Fn);
1019 assert(Iter != Cache.end());
1020 assert(Iter->second.hasValue());
1021 }
1022 return Iter->second;
1023}
1024
Chandler Carruth7b560d42015-09-09 17:55:00 +00001025AliasResult CFLAAResult::query(const MemoryLocation &LocA,
1026 const MemoryLocation &LocB) {
Hal Finkel7529c552014-09-02 21:43:13 +00001027 auto *ValA = const_cast<Value *>(LocA.Ptr);
1028 auto *ValB = const_cast<Value *>(LocB.Ptr);
1029
1030 Function *Fn = nullptr;
1031 auto MaybeFnA = parentFunctionOfValue(ValA);
1032 auto MaybeFnB = parentFunctionOfValue(ValB);
1033 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
George Burgess IV33305e72015-02-12 03:07:07 +00001034 // The only times this is known to happen are when globals + InlineAsm
1035 // are involved
1036 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001037 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001038 }
1039
1040 if (MaybeFnA.hasValue()) {
1041 Fn = *MaybeFnA;
1042 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1043 "Interprocedural queries not supported");
1044 } else {
1045 Fn = *MaybeFnB;
1046 }
1047
1048 assert(Fn != nullptr);
1049 auto &MaybeInfo = ensureCached(Fn);
1050 assert(MaybeInfo.hasValue());
1051
1052 auto &Sets = MaybeInfo->Sets;
1053 auto MaybeA = Sets.find(ValA);
1054 if (!MaybeA.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001055 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001056
1057 auto MaybeB = Sets.find(ValB);
1058 if (!MaybeB.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001059 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001060
1061 auto SetA = *MaybeA;
1062 auto SetB = *MaybeB;
Hal Finkel7529c552014-09-02 21:43:13 +00001063 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1064 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
George Burgess IV33305e72015-02-12 03:07:07 +00001065
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001066 // Stratified set attributes are used as markets to signify whether a member
George Burgess IV33305e72015-02-12 03:07:07 +00001067 // of a StratifiedSet (or a member of a set above the current set) has
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001068 // interacted with either arguments or globals. "Interacted with" meaning
George Burgess IV33305e72015-02-12 03:07:07 +00001069 // its value may be different depending on the value of an argument or
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001070 // global. The thought behind this is that, because arguments and globals
1071 // may alias each other, if AttrsA and AttrsB have touched args/globals,
George Burgess IV33305e72015-02-12 03:07:07 +00001072 // we must conservatively say that they alias. However, if at least one of
1073 // the sets has no values that could legally be altered by changing the value
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001074 // of an argument or global, then we don't have to be as conservative.
1075 if (AttrsA.any() && AttrsB.any())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001076 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001077
Daniel Berlin16f7a522015-01-26 17:31:17 +00001078 // We currently unify things even if the accesses to them may not be in
1079 // bounds, so we can't return partial alias here because we don't
1080 // know whether the pointer is really within the object or not.
1081 // IE Given an out of bounds GEP and an alloca'd pointer, we may
1082 // unify the two. We can't return partial alias for this case.
1083 // Since we do not currently track enough information to
1084 // differentiate
1085
1086 if (SetA.Index == SetB.Index)
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001087 return MayAlias;
Daniel Berlin16f7a522015-01-26 17:31:17 +00001088
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001089 return NoAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001090}
Mehdi Amini46a43552015-03-04 18:43:29 +00001091
Chandler Carruth7b560d42015-09-09 17:55:00 +00001092CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> *AM) {
1093 return CFLAAResult(AM->getResult<TargetLibraryAnalysis>(F));
1094}
1095
1096char CFLAA::PassID;
1097
1098char CFLAAWrapperPass::ID = 0;
1099INITIALIZE_PASS_BEGIN(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis",
1100 false, true)
1101INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1102INITIALIZE_PASS_END(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis",
1103 false, true)
1104
1105ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); }
1106
1107CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) {
1108 initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry());
1109}
1110
1111bool CFLAAWrapperPass::doInitialization(Module &M) {
1112 Result.reset(
1113 new CFLAAResult(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
1114 return false;
1115}
1116
1117bool CFLAAWrapperPass::doFinalization(Module &M) {
1118 Result.reset();
1119 return false;
1120}
1121
1122void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1123 AU.setPreservesAll();
1124 AU.addRequired<TargetLibraryInfoWrapperPass>();
Mehdi Amini46a43552015-03-04 18:43:29 +00001125}