blob: 57c3f317c11a144a17d46c51e3c7a322f0969075 [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/DenseMap.h"
Hal Finkel7529c552014-09-02 21:43:13 +000039#include "llvm/ADT/None.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000040#include "llvm/ADT/Optional.h"
Hal Finkel7529c552014-09-02 21:43:13 +000041#include "llvm/IR/Constants.h"
42#include "llvm/IR/Function.h"
Hal Finkel7529c552014-09-02 21:43:13 +000043#include "llvm/IR/InstVisitor.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000044#include "llvm/IR/Instructions.h"
Hal Finkel7529c552014-09-02 21:43:13 +000045#include "llvm/Pass.h"
Hal Finkel7d7087c2014-09-02 22:13:00 +000046#include "llvm/Support/Compiler.h"
George Burgess IV33305e72015-02-12 03:07:07 +000047#include "llvm/Support/Debug.h"
Hal Finkel7529c552014-09-02 21:43:13 +000048#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000049#include "llvm/Support/raw_ostream.h"
Hal Finkel7529c552014-09-02 21:43:13 +000050#include <algorithm>
51#include <cassert>
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include <memory>
Hal Finkel7529c552014-09-02 21:43:13 +000053#include <tuple>
54
55using namespace llvm;
56
George Burgess IV33305e72015-02-12 03:07:07 +000057#define DEBUG_TYPE "cfl-aa"
58
Chandler Carruth12884f72016-03-02 15:56:53 +000059CFLAAResult::CFLAAResult() : AAResultBase() {}
Chandler Carruth7b560d42015-09-09 17:55:00 +000060CFLAAResult::CFLAAResult(CFLAAResult &&Arg) : AAResultBase(std::move(Arg)) {}
Chandler Carruth342c6712016-02-20 03:52:02 +000061CFLAAResult::~CFLAAResult() {}
Chandler Carruth8b046a42015-08-14 02:42:20 +000062
George Burgess IVcae581d2016-04-13 23:27:37 +000063/// Information we have about a function and would like to keep around.
Chandler Carruth7b560d42015-09-09 17:55:00 +000064struct CFLAAResult::FunctionInfo {
Chandler Carruth8b046a42015-08-14 02:42:20 +000065 StratifiedSets<Value *> Sets;
66 // Lots of functions have < 4 returns. Adjust as necessary.
67 SmallVector<Value *, 4> ReturnedValues;
68
69 FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV)
70 : Sets(std::move(S)), ReturnedValues(std::move(RV)) {}
71};
72
George Burgess IVcae581d2016-04-13 23:27:37 +000073/// Try to go from a Value* to a Function*. Never returns nullptr.
Hal Finkel7529c552014-09-02 21:43:13 +000074static Optional<Function *> parentFunctionOfValue(Value *);
75
George Burgess IVcae581d2016-04-13 23:27:37 +000076/// Returns possible functions called by the Inst* into the given
77/// SmallVectorImpl. Returns true if targets found, false otherwise. This is
78/// templated so we can use it with CallInsts and InvokeInsts.
Hal Finkel7529c552014-09-02 21:43:13 +000079template <typename Inst>
80static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
81
George Burgess IVcae581d2016-04-13 23:27:37 +000082/// Some instructions need to have their users tracked. Instructions like
83/// `add` require you to get the users of the Instruction* itself, other
84/// instructions like `store` require you to get the users of the first
85/// operand. This function gets the "proper" value to track for each
86/// type of instruction we support.
Hal Finkel7529c552014-09-02 21:43:13 +000087static Optional<Value *> getTargetValue(Instruction *);
88
George Burgess IVcae581d2016-04-13 23:27:37 +000089/// Determines whether or not we an instruction is useless to us (e.g.
90/// FenceInst)
Hal Finkel7529c552014-09-02 21:43:13 +000091static bool hasUsefulEdges(Instruction *);
92
Hal Finkel1ae325f2014-09-02 23:50:01 +000093const StratifiedIndex StratifiedLink::SetSentinel =
George Burgess IV11d509d2015-03-15 00:52:21 +000094 std::numeric_limits<StratifiedIndex>::max();
Hal Finkel1ae325f2014-09-02 23:50:01 +000095
Hal Finkel7529c552014-09-02 21:43:13 +000096namespace {
George Burgess IVcae581d2016-04-13 23:27:37 +000097/// StratifiedInfo Attribute things.
Hal Finkel7529c552014-09-02 21:43:13 +000098typedef unsigned StratifiedAttr;
Hal Finkel7d7087c2014-09-02 22:13:00 +000099LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
100LLVM_CONSTEXPR unsigned AttrAllIndex = 0;
101LLVM_CONSTEXPR unsigned AttrGlobalIndex = 1;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000102LLVM_CONSTEXPR unsigned AttrUnknownIndex = 2;
103LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000104LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
105LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
Hal Finkel7529c552014-09-02 21:43:13 +0000106
Hal Finkel7d7087c2014-09-02 22:13:00 +0000107LLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000108LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000109LLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone;
Hal Finkel7529c552014-09-02 21:43:13 +0000110
George Burgess IVcae581d2016-04-13 23:27:37 +0000111/// StratifiedSets call for knowledge of "direction", so this is how we
112/// represent that locally.
Hal Finkel7529c552014-09-02 21:43:13 +0000113enum class Level { Same, Above, Below };
114
George Burgess IVcae581d2016-04-13 23:27:37 +0000115/// Edges can be one of four "weights" -- each weight must have an inverse
116/// weight (Assign has Assign; Reference has Dereference).
Hal Finkel7529c552014-09-02 21:43:13 +0000117enum class EdgeType {
George Burgess IVcae581d2016-04-13 23:27:37 +0000118 /// The weight assigned when assigning from or to a value. For example, in:
119 /// %b = getelementptr %a, 0
120 /// ...The relationships are %b assign %a, and %a assign %b. This used to be
121 /// two edges, but having a distinction bought us nothing.
Hal Finkel7529c552014-09-02 21:43:13 +0000122 Assign,
123
George Burgess IVcae581d2016-04-13 23:27:37 +0000124 /// The edge used when we have an edge going from some handle to a Value.
125 /// Examples of this include:
126 /// %b = load %a (%b Dereference %a)
127 /// %b = extractelement %a, 0 (%a Dereference %b)
Hal Finkel7529c552014-09-02 21:43:13 +0000128 Dereference,
129
George Burgess IVcae581d2016-04-13 23:27:37 +0000130 /// The edge used when our edge goes from a value to a handle that may have
131 /// contained it at some point. Examples:
132 /// %b = load %a (%a Reference %b)
133 /// %b = extractelement %a, 0 (%b Reference %a)
Hal Finkel7529c552014-09-02 21:43:13 +0000134 Reference
135};
136
137// \brief Encodes the notion of a "use"
138struct Edge {
George Burgess IVcae581d2016-04-13 23:27:37 +0000139 /// Which value the edge is coming from
Hal Finkel7529c552014-09-02 21:43:13 +0000140 Value *From;
141
George Burgess IVcae581d2016-04-13 23:27:37 +0000142 /// Which value the edge is pointing to
Hal Finkel7529c552014-09-02 21:43:13 +0000143 Value *To;
144
George Burgess IVcae581d2016-04-13 23:27:37 +0000145 /// Edge weight
Hal Finkel7529c552014-09-02 21:43:13 +0000146 EdgeType Weight;
147
George Burgess IVcae581d2016-04-13 23:27:37 +0000148 /// Whether we aliased any external values along the way that may be
149 /// invisible to the analysis (i.e. landingpad for exceptions, calls for
150 /// interprocedural analysis, etc.)
Hal Finkel7529c552014-09-02 21:43:13 +0000151 StratifiedAttrs AdditionalAttrs;
152
153 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
154 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
155};
156
George Burgess IVcae581d2016-04-13 23:27:37 +0000157/// Gets the edges our graph should have, based on an Instruction*
Hal Finkel7529c552014-09-02 21:43:13 +0000158class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000159 CFLAAResult &AA;
Hal Finkel7529c552014-09-02 21:43:13 +0000160 SmallVectorImpl<Edge> &Output;
161
162public:
Chandler Carruth7b560d42015-09-09 17:55:00 +0000163 GetEdgesVisitor(CFLAAResult &AA, SmallVectorImpl<Edge> &Output)
Hal Finkel7529c552014-09-02 21:43:13 +0000164 : AA(AA), Output(Output) {}
165
166 void visitInstruction(Instruction &) {
167 llvm_unreachable("Unsupported instruction encountered");
168 }
169
George Burgess IVb54a8d622015-03-10 02:40:06 +0000170 void visitPtrToIntInst(PtrToIntInst &Inst) {
171 auto *Ptr = Inst.getOperand(0);
172 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
173 }
174
175 void visitIntToPtrInst(IntToPtrInst &Inst) {
176 auto *Ptr = &Inst;
177 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
178 }
179
Hal Finkel7529c552014-09-02 21:43:13 +0000180 void visitCastInst(CastInst &Inst) {
George Burgess IV11d509d2015-03-15 00:52:21 +0000181 Output.push_back(
182 Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000183 }
184
185 void visitBinaryOperator(BinaryOperator &Inst) {
186 auto *Op1 = Inst.getOperand(0);
187 auto *Op2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000188 Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone));
189 Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000190 }
191
192 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
193 auto *Ptr = Inst.getPointerOperand();
194 auto *Val = Inst.getNewValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000195 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000196 }
197
198 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
199 auto *Ptr = Inst.getPointerOperand();
200 auto *Val = Inst.getValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000201 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000202 }
203
204 void visitPHINode(PHINode &Inst) {
George Burgess IV77351ba32016-01-28 00:54:01 +0000205 for (Value *Val : Inst.incoming_values())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000206 Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000207 }
208
209 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
210 auto *Op = Inst.getPointerOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000211 Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000212 }
213
214 void visitSelectInst(SelectInst &Inst) {
Daniel Berlin16f7a522015-01-26 17:31:17 +0000215 // Condition is not processed here (The actual statement producing
216 // the condition result is processed elsewhere). For select, the
217 // condition is evaluated, but not loaded, stored, or assigned
218 // simply as a result of being the condition of a select.
219
Hal Finkel7529c552014-09-02 21:43:13 +0000220 auto *TrueVal = Inst.getTrueValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000221 Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000222 auto *FalseVal = Inst.getFalseValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000223 Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000224 }
225
226 void visitAllocaInst(AllocaInst &) {}
227
228 void visitLoadInst(LoadInst &Inst) {
229 auto *Ptr = Inst.getPointerOperand();
230 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000231 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000232 }
233
234 void visitStoreInst(StoreInst &Inst) {
235 auto *Ptr = Inst.getPointerOperand();
236 auto *Val = Inst.getValueOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000237 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000238 }
239
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000240 void visitVAArgInst(VAArgInst &Inst) {
241 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
242 // two things:
243 // 1. Loads a value from *((T*)*Ptr).
244 // 2. Increments (stores to) *Ptr by some target-specific amount.
245 // For now, we'll handle this like a landingpad instruction (by placing the
246 // result in its own group, and having that group alias externals).
247 auto *Val = &Inst;
248 Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll));
249 }
250
Hal Finkel7529c552014-09-02 21:43:13 +0000251 static bool isFunctionExternal(Function *Fn) {
252 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
253 }
254
George Burgess IVcae581d2016-04-13 23:27:37 +0000255 /// Gets whether the sets at Index1 above, below, or equal to the sets at
256 /// Index2. Returns None if they are not in the same set chain.
Hal Finkel7529c552014-09-02 21:43:13 +0000257 static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
258 StratifiedIndex Index1,
259 StratifiedIndex Index2) {
260 if (Index1 == Index2)
261 return Level::Same;
262
263 const auto *Current = &Sets.getLink(Index1);
264 while (Current->hasBelow()) {
265 if (Current->Below == Index2)
266 return Level::Below;
267 Current = &Sets.getLink(Current->Below);
268 }
269
270 Current = &Sets.getLink(Index1);
271 while (Current->hasAbove()) {
272 if (Current->Above == Index2)
273 return Level::Above;
274 Current = &Sets.getLink(Current->Above);
275 }
276
George Burgess IV77351ba32016-01-28 00:54:01 +0000277 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000278 }
279
280 bool
281 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
282 Value *FuncValue,
283 const iterator_range<User::op_iterator> &Args) {
Hal Finkelca616ac2014-09-02 23:29:48 +0000284 const unsigned ExpectedMaxArgs = 8;
285 const unsigned MaxSupportedArgs = 50;
Hal Finkel7529c552014-09-02 21:43:13 +0000286 assert(Fns.size() > 0);
287
George Burgess IVcae581d2016-04-13 23:27:37 +0000288 // This algorithm is n^2, so an arbitrary upper-bound of 50 args was
289 // selected, so it doesn't take too long in insane cases.
George Burgess IVab03af22015-03-10 02:58:15 +0000290 if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000291 return false;
292
293 // Exit early if we'll fail anyway
294 for (auto *Fn : Fns) {
295 if (isFunctionExternal(Fn) || Fn->isVarArg())
296 return false;
297 auto &MaybeInfo = AA.ensureCached(Fn);
298 if (!MaybeInfo.hasValue())
299 return false;
300 }
301
302 SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
303 SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
304 for (auto *Fn : Fns) {
305 auto &Info = *AA.ensureCached(Fn);
306 auto &Sets = Info.Sets;
307 auto &RetVals = Info.ReturnedValues;
308
309 Parameters.clear();
310 for (auto &Param : Fn->args()) {
311 auto MaybeInfo = Sets.find(&Param);
312 // Did a new parameter somehow get added to the function/slip by?
313 if (!MaybeInfo.hasValue())
314 return false;
315 Parameters.push_back(*MaybeInfo);
316 }
317
318 // Adding an edge from argument -> return value for each parameter that
319 // may alias the return value
320 for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
321 auto &ParamInfo = Parameters[I];
322 auto &ArgVal = Arguments[I];
323 bool AddEdge = false;
324 StratifiedAttrs Externals;
325 for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
326 auto MaybeInfo = Sets.find(RetVals[X]);
327 if (!MaybeInfo.hasValue())
328 return false;
329
330 auto &RetInfo = *MaybeInfo;
331 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
332 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
333 auto MaybeRelation =
334 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
335 if (MaybeRelation.hasValue()) {
336 AddEdge = true;
337 Externals |= RetAttrs | ParamAttrs;
338 }
339 }
340 if (AddEdge)
Hal Finkelca616ac2014-09-02 23:29:48 +0000341 Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign,
George Burgess IV11d509d2015-03-15 00:52:21 +0000342 StratifiedAttrs().flip()));
Hal Finkel7529c552014-09-02 21:43:13 +0000343 }
344
345 if (Parameters.size() != Arguments.size())
346 return false;
347
George Burgess IVcae581d2016-04-13 23:27:37 +0000348 /// Adding edges between arguments for arguments that may end up aliasing
349 /// each other. This is necessary for functions such as
350 /// void foo(int** a, int** b) { *a = *b; }
351 /// (Technically, the proper sets for this would be those below
352 /// Arguments[I] and Arguments[X], but our algorithm will produce
353 /// extremely similar, and equally correct, results either way)
Hal Finkel7529c552014-09-02 21:43:13 +0000354 for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
355 auto &MainVal = Arguments[I];
356 auto &MainInfo = Parameters[I];
357 auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
358 for (unsigned X = I + 1; X != E; ++X) {
359 auto &SubInfo = Parameters[X];
360 auto &SubVal = Arguments[X];
361 auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
362 auto MaybeRelation =
363 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
364
365 if (!MaybeRelation.hasValue())
366 continue;
367
368 auto NewAttrs = SubAttrs | MainAttrs;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000369 Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
Hal Finkel7529c552014-09-02 21:43:13 +0000370 }
371 }
372 }
373 return true;
374 }
375
376 template <typename InstT> void visitCallLikeInst(InstT &Inst) {
George Burgess IV68b36e02015-08-28 00:16:18 +0000377 // TODO: Add support for noalias args/all the other fun function attributes
378 // that we can tack on.
Hal Finkel7529c552014-09-02 21:43:13 +0000379 SmallVector<Function *, 4> Targets;
380 if (getPossibleTargets(&Inst, Targets)) {
381 if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
382 return;
383 // Cleanup from interprocedural analysis
384 Output.clear();
385 }
386
George Burgess IV68b36e02015-08-28 00:16:18 +0000387 // Because the function is opaque, we need to note that anything
388 // could have happened to the arguments, and that the result could alias
389 // just about anything, too.
390 // The goal of the loop is in part to unify many Values into one set, so we
391 // don't care if the function is void there.
Hal Finkel7529c552014-09-02 21:43:13 +0000392 for (Value *V : Inst.arg_operands())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000393 Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll));
George Burgess IV68b36e02015-08-28 00:16:18 +0000394 if (Inst.getNumArgOperands() == 0 &&
395 Inst.getType() != Type::getVoidTy(Inst.getContext()))
396 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
Hal Finkel7529c552014-09-02 21:43:13 +0000397 }
398
399 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
400
401 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
402
George Burgess IVcae581d2016-04-13 23:27:37 +0000403 /// Because vectors/aggregates are immutable and unaddressable, there's
404 /// nothing we can do to coax a value out of them, other than calling
405 /// Extract{Element,Value}. We can effectively treat them as pointers to
406 /// arbitrary memory locations we can store in and load from.
Hal Finkel7529c552014-09-02 21:43:13 +0000407 void visitExtractElementInst(ExtractElementInst &Inst) {
408 auto *Ptr = Inst.getVectorOperand();
409 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000410 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000411 }
412
413 void visitInsertElementInst(InsertElementInst &Inst) {
414 auto *Vec = Inst.getOperand(0);
415 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000416 Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
417 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000418 }
419
420 void visitLandingPadInst(LandingPadInst &Inst) {
421 // Exceptions come from "nowhere", from our analysis' perspective.
422 // So we place the instruction its own group, noting that said group may
423 // alias externals
Hal Finkel8d1590d2014-09-02 22:52:30 +0000424 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
Hal Finkel7529c552014-09-02 21:43:13 +0000425 }
426
427 void visitInsertValueInst(InsertValueInst &Inst) {
428 auto *Agg = Inst.getOperand(0);
429 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000430 Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
431 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000432 }
433
434 void visitExtractValueInst(ExtractValueInst &Inst) {
435 auto *Ptr = Inst.getAggregateOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000436 Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000437 }
438
439 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
440 auto *From1 = Inst.getOperand(0);
441 auto *From2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000442 Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
443 Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000444 }
Pete Cooper36642532015-06-12 16:13:54 +0000445
446 void visitConstantExpr(ConstantExpr *CE) {
447 switch (CE->getOpcode()) {
448 default:
449 llvm_unreachable("Unknown instruction type encountered!");
450// Build the switch statement using the Instruction.def file.
451#define HANDLE_INST(NUM, OPCODE, CLASS) \
452 case Instruction::OPCODE: \
453 visit##OPCODE(*(CLASS *)CE); \
454 break;
455#include "llvm/IR/Instruction.def"
456 }
457 }
Hal Finkel7529c552014-09-02 21:43:13 +0000458};
459
George Burgess IVcae581d2016-04-13 23:27:37 +0000460/// For a given instruction, we need to know which Value* to get the
461/// users of in order to build our graph. In some cases (i.e. add),
462/// we simply need the Instruction*. In other cases (i.e. store),
463/// finding the users of the Instruction* is useless; we need to find
464/// the users of the first operand. This handles determining which
465/// value to follow for us.
466///
467/// Note: we *need* to keep this in sync with GetEdgesVisitor. Add
468/// something to GetEdgesVisitor, add it here -- remove something from
469/// GetEdgesVisitor, remove it here.
Hal Finkel7529c552014-09-02 21:43:13 +0000470class GetTargetValueVisitor
471 : public InstVisitor<GetTargetValueVisitor, Value *> {
472public:
473 Value *visitInstruction(Instruction &Inst) { return &Inst; }
474
475 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
476
477 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
478 return Inst.getPointerOperand();
479 }
480
481 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
482 return Inst.getPointerOperand();
483 }
484
485 Value *visitInsertElementInst(InsertElementInst &Inst) {
486 return Inst.getOperand(0);
487 }
488
489 Value *visitInsertValueInst(InsertValueInst &Inst) {
490 return Inst.getAggregateOperand();
491 }
492};
493
George Burgess IVcae581d2016-04-13 23:27:37 +0000494/// Set building requires a weighted bidirectional graph.
Hal Finkel7529c552014-09-02 21:43:13 +0000495template <typename EdgeTypeT> class WeightedBidirectionalGraph {
496public:
497 typedef std::size_t Node;
498
499private:
Hal Finkelca616ac2014-09-02 23:29:48 +0000500 const static Node StartNode = Node(0);
Hal Finkel7529c552014-09-02 21:43:13 +0000501
502 struct Edge {
503 EdgeTypeT Weight;
504 Node Other;
505
George Burgess IV11d509d2015-03-15 00:52:21 +0000506 Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {}
Hal Finkelca616ac2014-09-02 23:29:48 +0000507
Hal Finkel7529c552014-09-02 21:43:13 +0000508 bool operator==(const Edge &E) const {
509 return Weight == E.Weight && Other == E.Other;
510 }
511
512 bool operator!=(const Edge &E) const { return !operator==(E); }
513 };
514
515 struct NodeImpl {
516 std::vector<Edge> Edges;
517 };
518
519 std::vector<NodeImpl> NodeImpls;
520
521 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
522
523 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
524 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
525
526public:
George Burgess IVcae581d2016-04-13 23:27:37 +0000527 /// \brief Iterator for edges. Because this graph is bidirected, we don't
528 /// allow modification of the edges using this iterator. Additionally, the
529 /// iterator becomes invalid if you add edges to or from the node you're
530 /// getting the edges of.
Hal Finkel7529c552014-09-02 21:43:13 +0000531 struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
532 std::tuple<EdgeTypeT, Node *>> {
533 EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
534 : Current(Iter) {}
535
536 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
537
538 EdgeIterator &operator++() {
539 ++Current;
540 return *this;
541 }
542
543 EdgeIterator operator++(int) {
544 EdgeIterator Copy(Current);
545 operator++();
546 return Copy;
547 }
548
549 std::tuple<EdgeTypeT, Node> &operator*() {
550 Store = std::make_tuple(Current->Weight, Current->Other);
551 return Store;
552 }
553
554 bool operator==(const EdgeIterator &Other) const {
555 return Current == Other.Current;
556 }
557
558 bool operator!=(const EdgeIterator &Other) const {
559 return !operator==(Other);
560 }
561
562 private:
563 typename std::vector<Edge>::const_iterator Current;
564 std::tuple<EdgeTypeT, Node> Store;
565 };
566
George Burgess IVcae581d2016-04-13 23:27:37 +0000567 /// Wrapper for EdgeIterator with begin()/end() calls.
Hal Finkel7529c552014-09-02 21:43:13 +0000568 struct EdgeIterable {
569 EdgeIterable(const std::vector<Edge> &Edges)
570 : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
571
572 EdgeIterator begin() { return EdgeIterator(BeginIter); }
573
574 EdgeIterator end() { return EdgeIterator(EndIter); }
575
576 private:
577 typename std::vector<Edge>::const_iterator BeginIter;
578 typename std::vector<Edge>::const_iterator EndIter;
579 };
580
581 // ----- Actual graph-related things ----- //
582
Hal Finkelca616ac2014-09-02 23:29:48 +0000583 WeightedBidirectionalGraph() {}
Hal Finkel7529c552014-09-02 21:43:13 +0000584
585 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
586 : NodeImpls(std::move(Other.NodeImpls)) {}
587
588 WeightedBidirectionalGraph<EdgeTypeT> &
589 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
590 NodeImpls = std::move(Other.NodeImpls);
591 return *this;
592 }
593
594 Node addNode() {
595 auto Index = NodeImpls.size();
596 auto NewNode = Node(Index);
597 NodeImpls.push_back(NodeImpl());
598 return NewNode;
599 }
600
601 void addEdge(Node From, Node To, const EdgeTypeT &Weight,
602 const EdgeTypeT &ReverseWeight) {
603 assert(inbounds(From));
604 assert(inbounds(To));
605 auto &FromNode = getNode(From);
606 auto &ToNode = getNode(To);
Hal Finkelca616ac2014-09-02 23:29:48 +0000607 FromNode.Edges.push_back(Edge(Weight, To));
608 ToNode.Edges.push_back(Edge(ReverseWeight, From));
Hal Finkel7529c552014-09-02 21:43:13 +0000609 }
610
George Burgess IVcae581d2016-04-13 23:27:37 +0000611 iterator_range<EdgeIterator> edgesFor(const Node &N) const {
Hal Finkel7529c552014-09-02 21:43:13 +0000612 const auto &Node = getNode(N);
George Burgess IVcae581d2016-04-13 23:27:37 +0000613 return make_range(EdgeIterator(Node.Edges.begin()),
614 EdgeIterator(Node.Edges.end()));
Hal Finkel7529c552014-09-02 21:43:13 +0000615 }
616
617 bool empty() const { return NodeImpls.empty(); }
618 std::size_t size() const { return NodeImpls.size(); }
619
George Burgess IVcae581d2016-04-13 23:27:37 +0000620 /// Gets an arbitrary node in the graph as a starting point for traversal.
Hal Finkel7529c552014-09-02 21:43:13 +0000621 Node getEntryNode() {
622 assert(inbounds(StartNode));
623 return StartNode;
624 }
625};
626
627typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
628typedef DenseMap<Value *, GraphT::Node> NodeMapT;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000629}
Hal Finkel7529c552014-09-02 21:43:13 +0000630
Hal Finkel7529c552014-09-02 21:43:13 +0000631//===----------------------------------------------------------------------===//
632// Function declarations that require types defined in the namespace above
633//===----------------------------------------------------------------------===//
634
George Burgess IVcae581d2016-04-13 23:27:37 +0000635/// Given an argument number, returns the appropriate Attr index to set.
636static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum);
Hal Finkel7529c552014-09-02 21:43:13 +0000637
George Burgess IVcae581d2016-04-13 23:27:37 +0000638/// Given a Value, potentially return which AttrIndex it maps to.
Hal Finkel7529c552014-09-02 21:43:13 +0000639static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
640
George Burgess IVcae581d2016-04-13 23:27:37 +0000641/// Gets the inverse of a given EdgeType.
642static EdgeType flipWeight(EdgeType Initial);
Hal Finkel7529c552014-09-02 21:43:13 +0000643
George Burgess IVcae581d2016-04-13 23:27:37 +0000644/// Gets edges of the given Instruction*, writing them to the SmallVector*.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000645static void argsToEdges(CFLAAResult &, Instruction *, SmallVectorImpl<Edge> &);
Hal Finkel7529c552014-09-02 21:43:13 +0000646
George Burgess IVcae581d2016-04-13 23:27:37 +0000647/// Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000648static void argsToEdges(CFLAAResult &, ConstantExpr *, SmallVectorImpl<Edge> &);
Pete Cooper36642532015-06-12 16:13:54 +0000649
George Burgess IVcae581d2016-04-13 23:27:37 +0000650/// Gets the "Level" that one should travel in StratifiedSets
651/// given an EdgeType.
Hal Finkel7529c552014-09-02 21:43:13 +0000652static Level directionOfEdgeType(EdgeType);
653
George Burgess IVcae581d2016-04-13 23:27:37 +0000654/// Builds the graph needed for constructing the StratifiedSets for the
655/// given function
Chandler Carruth7b560d42015-09-09 17:55:00 +0000656static void buildGraphFrom(CFLAAResult &, Function *,
Hal Finkel7529c552014-09-02 21:43:13 +0000657 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
658
George Burgess IVcae581d2016-04-13 23:27:37 +0000659/// Gets the edges of a ConstantExpr as if it was an Instruction. This function
660/// also acts on any nested ConstantExprs, adding the edges of those to the
661/// given SmallVector as well.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000662static void constexprToEdges(CFLAAResult &, ConstantExpr &,
George Burgess IVab03af22015-03-10 02:58:15 +0000663 SmallVectorImpl<Edge> &);
664
George Burgess IVcae581d2016-04-13 23:27:37 +0000665/// Given an Instruction, this will add it to the graph, along with any
666/// Instructions that are potentially only available from said Instruction
667/// For example, given the following line:
668/// %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
669/// addInstructionToGraph would add both the `load` and `getelementptr`
670/// instructions to the graph appropriately.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000671static void addInstructionToGraph(CFLAAResult &, Instruction &,
George Burgess IVab03af22015-03-10 02:58:15 +0000672 SmallVectorImpl<Value *> &, NodeMapT &,
673 GraphT &);
674
George Burgess IVcae581d2016-04-13 23:27:37 +0000675/// Determines whether it would be pointless to add the given Value to our sets.
George Burgess IVab03af22015-03-10 02:58:15 +0000676static bool canSkipAddingToSets(Value *Val);
677
Hal Finkel7529c552014-09-02 21:43:13 +0000678static Optional<Function *> parentFunctionOfValue(Value *Val) {
679 if (auto *Inst = dyn_cast<Instruction>(Val)) {
680 auto *Bb = Inst->getParent();
681 return Bb->getParent();
682 }
683
684 if (auto *Arg = dyn_cast<Argument>(Val))
685 return Arg->getParent();
George Burgess IV77351ba32016-01-28 00:54:01 +0000686 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000687}
688
689template <typename Inst>
690static bool getPossibleTargets(Inst *Call,
691 SmallVectorImpl<Function *> &Output) {
692 if (auto *Fn = Call->getCalledFunction()) {
693 Output.push_back(Fn);
694 return true;
695 }
696
697 // TODO: If the call is indirect, we might be able to enumerate all potential
698 // targets of the call and return them, rather than just failing.
699 return false;
700}
701
702static Optional<Value *> getTargetValue(Instruction *Inst) {
703 GetTargetValueVisitor V;
704 return V.visit(Inst);
705}
706
707static bool hasUsefulEdges(Instruction *Inst) {
708 bool IsNonInvokeTerminator =
709 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
710 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
711}
712
Pete Cooper36642532015-06-12 16:13:54 +0000713static bool hasUsefulEdges(ConstantExpr *CE) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000714 // ConstantExpr doesn't have terminators, invokes, or fences, so only needs
Pete Cooper36642532015-06-12 16:13:54 +0000715 // to check for compares.
716 return CE->getOpcode() != Instruction::ICmp &&
717 CE->getOpcode() != Instruction::FCmp;
718}
719
Hal Finkel7529c552014-09-02 21:43:13 +0000720static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
721 if (isa<GlobalValue>(Val))
722 return AttrGlobalIndex;
723
724 if (auto *Arg = dyn_cast<Argument>(Val))
Daniel Berlin16f7a522015-01-26 17:31:17 +0000725 // Only pointer arguments should have the argument attribute,
726 // because things can't escape through scalars without us seeing a
727 // cast, and thus, interaction with them doesn't matter.
728 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
Hal Finkel7529c552014-09-02 21:43:13 +0000729 return argNumberToAttrIndex(Arg->getArgNo());
George Burgess IV77351ba32016-01-28 00:54:01 +0000730 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000731}
732
733static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
George Burgess IV3c898c22015-01-21 16:37:21 +0000734 if (ArgNum >= AttrMaxNumArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000735 return AttrAllIndex;
736 return ArgNum + AttrFirstArgIndex;
737}
738
739static EdgeType flipWeight(EdgeType Initial) {
740 switch (Initial) {
741 case EdgeType::Assign:
742 return EdgeType::Assign;
743 case EdgeType::Dereference:
744 return EdgeType::Reference;
745 case EdgeType::Reference:
746 return EdgeType::Dereference;
747 }
748 llvm_unreachable("Incomplete coverage of EdgeType enum");
749}
750
Chandler Carruth7b560d42015-09-09 17:55:00 +0000751static void argsToEdges(CFLAAResult &Analysis, Instruction *Inst,
Hal Finkel7529c552014-09-02 21:43:13 +0000752 SmallVectorImpl<Edge> &Output) {
George Burgess IVab03af22015-03-10 02:58:15 +0000753 assert(hasUsefulEdges(Inst) &&
754 "Expected instructions to have 'useful' edges");
Hal Finkel7529c552014-09-02 21:43:13 +0000755 GetEdgesVisitor v(Analysis, Output);
756 v.visit(Inst);
757}
758
Chandler Carruth7b560d42015-09-09 17:55:00 +0000759static void argsToEdges(CFLAAResult &Analysis, ConstantExpr *CE,
Pete Cooper36642532015-06-12 16:13:54 +0000760 SmallVectorImpl<Edge> &Output) {
761 assert(hasUsefulEdges(CE) && "Expected constant expr to have 'useful' edges");
762 GetEdgesVisitor v(Analysis, Output);
763 v.visitConstantExpr(CE);
764}
765
Hal Finkel7529c552014-09-02 21:43:13 +0000766static Level directionOfEdgeType(EdgeType Weight) {
767 switch (Weight) {
768 case EdgeType::Reference:
769 return Level::Above;
770 case EdgeType::Dereference:
771 return Level::Below;
772 case EdgeType::Assign:
773 return Level::Same;
774 }
775 llvm_unreachable("Incomplete switch coverage");
776}
777
Chandler Carruth7b560d42015-09-09 17:55:00 +0000778static void constexprToEdges(CFLAAResult &Analysis,
George Burgess IVab03af22015-03-10 02:58:15 +0000779 ConstantExpr &CExprToCollapse,
780 SmallVectorImpl<Edge> &Results) {
781 SmallVector<ConstantExpr *, 4> Worklist;
782 Worklist.push_back(&CExprToCollapse);
783
784 SmallVector<Edge, 8> ConstexprEdges;
Pete Cooper36642532015-06-12 16:13:54 +0000785 SmallPtrSet<ConstantExpr *, 4> Visited;
George Burgess IVab03af22015-03-10 02:58:15 +0000786 while (!Worklist.empty()) {
787 auto *CExpr = Worklist.pop_back_val();
George Burgess IVab03af22015-03-10 02:58:15 +0000788
Pete Cooper36642532015-06-12 16:13:54 +0000789 if (!hasUsefulEdges(CExpr))
George Burgess IVab03af22015-03-10 02:58:15 +0000790 continue;
791
792 ConstexprEdges.clear();
Pete Cooper36642532015-06-12 16:13:54 +0000793 argsToEdges(Analysis, CExpr, ConstexprEdges);
George Burgess IVab03af22015-03-10 02:58:15 +0000794 for (auto &Edge : ConstexprEdges) {
Pete Cooper36642532015-06-12 16:13:54 +0000795 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
796 if (Visited.insert(Nested).second)
797 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000798
Pete Cooper36642532015-06-12 16:13:54 +0000799 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
800 if (Visited.insert(Nested).second)
801 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000802 }
803
804 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
805 }
806}
807
Chandler Carruth7b560d42015-09-09 17:55:00 +0000808static void addInstructionToGraph(CFLAAResult &Analysis, Instruction &Inst,
George Burgess IVab03af22015-03-10 02:58:15 +0000809 SmallVectorImpl<Value *> &ReturnedValues,
810 NodeMapT &Map, GraphT &Graph) {
Hal Finkel7529c552014-09-02 21:43:13 +0000811 const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
812 auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
813 auto &Iter = Pair.first;
814 if (Pair.second) {
815 auto NewNode = Graph.addNode();
816 Iter->second = NewNode;
817 }
818 return Iter->second;
819 };
820
George Burgess IVab03af22015-03-10 02:58:15 +0000821 // We don't want the edges of most "return" instructions, but we *do* want
822 // to know what can be returned.
823 if (isa<ReturnInst>(&Inst))
824 ReturnedValues.push_back(&Inst);
825
826 if (!hasUsefulEdges(&Inst))
827 return;
828
Hal Finkel7529c552014-09-02 21:43:13 +0000829 SmallVector<Edge, 8> Edges;
George Burgess IVab03af22015-03-10 02:58:15 +0000830 argsToEdges(Analysis, &Inst, Edges);
Hal Finkel7529c552014-09-02 21:43:13 +0000831
George Burgess IVab03af22015-03-10 02:58:15 +0000832 // In the case of an unused alloca (or similar), edges may be empty. Note
833 // that it exists so we can potentially answer NoAlias.
834 if (Edges.empty()) {
835 auto MaybeVal = getTargetValue(&Inst);
836 assert(MaybeVal.hasValue());
837 auto *Target = *MaybeVal;
838 findOrInsertNode(Target);
839 return;
Hal Finkel7529c552014-09-02 21:43:13 +0000840 }
George Burgess IVab03af22015-03-10 02:58:15 +0000841
George Burgess IVcae581d2016-04-13 23:27:37 +0000842 auto addEdgeToGraph = [&](const Edge &E) {
George Burgess IVab03af22015-03-10 02:58:15 +0000843 auto To = findOrInsertNode(E.To);
844 auto From = findOrInsertNode(E.From);
845 auto FlippedWeight = flipWeight(E.Weight);
846 auto Attrs = E.AdditionalAttrs;
847 Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
848 std::make_pair(FlippedWeight, Attrs));
849 };
850
851 SmallVector<ConstantExpr *, 4> ConstantExprs;
852 for (const Edge &E : Edges) {
853 addEdgeToGraph(E);
854 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
855 ConstantExprs.push_back(Constexpr);
856 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
857 ConstantExprs.push_back(Constexpr);
858 }
859
860 for (ConstantExpr *CE : ConstantExprs) {
861 Edges.clear();
862 constexprToEdges(Analysis, *CE, Edges);
863 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
864 }
865}
866
Chandler Carruth7b560d42015-09-09 17:55:00 +0000867static void buildGraphFrom(CFLAAResult &Analysis, Function *Fn,
George Burgess IVab03af22015-03-10 02:58:15 +0000868 SmallVectorImpl<Value *> &ReturnedValues,
869 NodeMapT &Map, GraphT &Graph) {
George Burgess IVcae581d2016-04-13 23:27:37 +0000870 // (N.B. We may remove graph construction entirely, because it doesn't really
871 // buy us much.)
George Burgess IVab03af22015-03-10 02:58:15 +0000872 for (auto &Bb : Fn->getBasicBlockList())
873 for (auto &Inst : Bb.getInstList())
874 addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph);
875}
876
877static bool canSkipAddingToSets(Value *Val) {
878 // Constants can share instances, which may falsely unify multiple
879 // sets, e.g. in
880 // store i32* null, i32** %ptr1
881 // store i32* null, i32** %ptr2
882 // clearly ptr1 and ptr2 should not be unified into the same set, so
883 // we should filter out the (potentially shared) instance to
884 // i32* null.
885 if (isa<Constant>(Val)) {
George Burgess IVab03af22015-03-10 02:58:15 +0000886 // TODO: Because all of these things are constant, we can determine whether
887 // the data is *actually* mutable at graph building time. This will probably
888 // come for free/cheap with offset awareness.
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000889 bool CanStoreMutableData = isa<GlobalValue>(Val) ||
890 isa<ConstantExpr>(Val) ||
891 isa<ConstantAggregate>(Val);
George Burgess IVab03af22015-03-10 02:58:15 +0000892 return !CanStoreMutableData;
893 }
894
895 return false;
Hal Finkel7529c552014-09-02 21:43:13 +0000896}
897
Chandler Carruth8b046a42015-08-14 02:42:20 +0000898// Builds the graph + StratifiedSets for a function.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000899CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) {
Hal Finkel7529c552014-09-02 21:43:13 +0000900 NodeMapT Map;
901 GraphT Graph;
902 SmallVector<Value *, 4> ReturnedValues;
903
Chandler Carruth8b046a42015-08-14 02:42:20 +0000904 buildGraphFrom(*this, Fn, ReturnedValues, Map, Graph);
Hal Finkel7529c552014-09-02 21:43:13 +0000905
906 DenseMap<GraphT::Node, Value *> NodeValueMap;
Mehdi Aminic04fc7a2016-03-22 07:20:00 +0000907 NodeValueMap.reserve(Map.size());
Hal Finkel7529c552014-09-02 21:43:13 +0000908 for (const auto &Pair : Map)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000909 NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
Hal Finkel7529c552014-09-02 21:43:13 +0000910
911 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
912 auto ValIter = NodeValueMap.find(Node);
913 assert(ValIter != NodeValueMap.end());
914 return ValIter->second;
915 };
916
917 StratifiedSetsBuilder<Value *> Builder;
918
919 SmallVector<GraphT::Node, 16> Worklist;
920 for (auto &Pair : Map) {
921 Worklist.clear();
922
923 auto *Value = Pair.first;
924 Builder.add(Value);
925 auto InitialNode = Pair.second;
926 Worklist.push_back(InitialNode);
927 while (!Worklist.empty()) {
928 auto Node = Worklist.pop_back_val();
929 auto *CurValue = findValueOrDie(Node);
George Burgess IVab03af22015-03-10 02:58:15 +0000930 if (canSkipAddingToSets(CurValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000931 continue;
932
George Burgess IV7e5404c2016-04-05 21:40:45 +0000933 Optional<StratifiedAttr> MaybeCurIndex = valueToAttrIndex(CurValue);
934 if (MaybeCurIndex)
935 Builder.noteAttributes(CurValue, *MaybeCurIndex);
936
Hal Finkel7529c552014-09-02 21:43:13 +0000937 for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
938 auto Weight = std::get<0>(EdgeTuple);
939 auto Label = Weight.first;
940 auto &OtherNode = std::get<1>(EdgeTuple);
941 auto *OtherValue = findValueOrDie(OtherNode);
942
George Burgess IVab03af22015-03-10 02:58:15 +0000943 if (canSkipAddingToSets(OtherValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000944 continue;
945
946 bool Added;
947 switch (directionOfEdgeType(Label)) {
948 case Level::Above:
949 Added = Builder.addAbove(CurValue, OtherValue);
950 break;
951 case Level::Below:
952 Added = Builder.addBelow(CurValue, OtherValue);
953 break;
954 case Level::Same:
955 Added = Builder.addWith(CurValue, OtherValue);
956 break;
957 }
958
George Burgess IVb54a8d622015-03-10 02:40:06 +0000959 auto Aliasing = Weight.second;
George Burgess IV7e5404c2016-04-05 21:40:45 +0000960 if (MaybeCurIndex)
George Burgess IVb54a8d622015-03-10 02:40:06 +0000961 Aliasing.set(*MaybeCurIndex);
962 if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
963 Aliasing.set(*MaybeOtherIndex);
964 Builder.noteAttributes(CurValue, Aliasing);
965 Builder.noteAttributes(OtherValue, Aliasing);
966
967 if (Added)
Hal Finkel7529c552014-09-02 21:43:13 +0000968 Worklist.push_back(OtherNode);
Hal Finkel7529c552014-09-02 21:43:13 +0000969 }
970 }
971 }
972
973 // There are times when we end up with parameters not in our graph (i.e. if
974 // it's only used as the condition of a branch). Other bits of code depend on
975 // things that were present during construction being present in the graph.
976 // So, we add all present arguments here.
977 for (auto &Arg : Fn->args()) {
George Burgess IVab03af22015-03-10 02:58:15 +0000978 if (!Builder.add(&Arg))
979 continue;
980
981 auto Attrs = valueToAttrIndex(&Arg);
982 if (Attrs.hasValue())
983 Builder.noteAttributes(&Arg, *Attrs);
Hal Finkel7529c552014-09-02 21:43:13 +0000984 }
985
Hal Finkel85f26922014-09-03 00:06:47 +0000986 return FunctionInfo(Builder.build(), std::move(ReturnedValues));
Hal Finkel7529c552014-09-02 21:43:13 +0000987}
988
Chandler Carruth7b560d42015-09-09 17:55:00 +0000989void CFLAAResult::scan(Function *Fn) {
Hal Finkel8d1590d2014-09-02 22:52:30 +0000990 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
Hal Finkel7529c552014-09-02 21:43:13 +0000991 (void)InsertPair;
992 assert(InsertPair.second &&
993 "Trying to scan a function that has already been cached");
994
George Burgess IV6edb8912016-05-02 18:09:19 +0000995 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
996 // may get evaluated after operator[], potentially triggering a DenseMap
997 // resize and invalidating the reference returned by operator[]
998 auto FunInfo = buildSetsFrom(Fn);
999 Cache[Fn] = std::move(FunInfo);
1000
Hal Finkel7529c552014-09-02 21:43:13 +00001001 Handles.push_front(FunctionHandle(Fn, this));
1002}
1003
Chandler Carruth7b560d42015-09-09 17:55:00 +00001004void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); }
Chandler Carruth8b046a42015-08-14 02:42:20 +00001005
George Burgess IVcae581d2016-04-13 23:27:37 +00001006/// Ensures that the given function is available in the cache, and returns the
1007/// entry.
Chandler Carruth7b560d42015-09-09 17:55:00 +00001008const Optional<CFLAAResult::FunctionInfo> &
1009CFLAAResult::ensureCached(Function *Fn) {
Chandler Carruth8b046a42015-08-14 02:42:20 +00001010 auto Iter = Cache.find(Fn);
1011 if (Iter == Cache.end()) {
1012 scan(Fn);
1013 Iter = Cache.find(Fn);
1014 assert(Iter != Cache.end());
1015 assert(Iter->second.hasValue());
1016 }
1017 return Iter->second;
1018}
1019
Chandler Carruth7b560d42015-09-09 17:55:00 +00001020AliasResult CFLAAResult::query(const MemoryLocation &LocA,
1021 const MemoryLocation &LocB) {
Hal Finkel7529c552014-09-02 21:43:13 +00001022 auto *ValA = const_cast<Value *>(LocA.Ptr);
1023 auto *ValB = const_cast<Value *>(LocB.Ptr);
1024
1025 Function *Fn = nullptr;
1026 auto MaybeFnA = parentFunctionOfValue(ValA);
1027 auto MaybeFnB = parentFunctionOfValue(ValB);
1028 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
George Burgess IVcae581d2016-04-13 23:27:37 +00001029 // The only times this is known to happen are when globals + InlineAsm are
1030 // involved
George Burgess IV33305e72015-02-12 03:07:07 +00001031 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001032 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001033 }
1034
1035 if (MaybeFnA.hasValue()) {
1036 Fn = *MaybeFnA;
1037 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1038 "Interprocedural queries not supported");
1039 } else {
1040 Fn = *MaybeFnB;
1041 }
1042
1043 assert(Fn != nullptr);
1044 auto &MaybeInfo = ensureCached(Fn);
1045 assert(MaybeInfo.hasValue());
1046
1047 auto &Sets = MaybeInfo->Sets;
1048 auto MaybeA = Sets.find(ValA);
1049 if (!MaybeA.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001050 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001051
1052 auto MaybeB = Sets.find(ValB);
1053 if (!MaybeB.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001054 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001055
1056 auto SetA = *MaybeA;
1057 auto SetB = *MaybeB;
Hal Finkel7529c552014-09-02 21:43:13 +00001058 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1059 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
George Burgess IV33305e72015-02-12 03:07:07 +00001060
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001061 // Stratified set attributes are used as markets to signify whether a member
George Burgess IV33305e72015-02-12 03:07:07 +00001062 // of a StratifiedSet (or a member of a set above the current set) has
George Burgess IVcae581d2016-04-13 23:27:37 +00001063 // interacted with either arguments or globals. "Interacted with" meaning its
1064 // value may be different depending on the value of an argument or global. The
1065 // thought behind this is that, because arguments and globals may alias each
1066 // other, if AttrsA and AttrsB have touched args/globals, we must
1067 // conservatively say that they alias. However, if at least one of the sets
1068 // has no values that could legally be altered by changing the value of an
1069 // argument or global, then we don't have to be as conservative.
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001070 if (AttrsA.any() && AttrsB.any())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001071 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001072
Daniel Berlin16f7a522015-01-26 17:31:17 +00001073 // We currently unify things even if the accesses to them may not be in
George Burgess IVcae581d2016-04-13 23:27:37 +00001074 // bounds, so we can't return partial alias here because we don't know whether
1075 // the pointer is really within the object or not.
1076 // e.g. Given an out of bounds GEP and an alloca'd pointer, we may unify the
1077 // two. We can't return partial alias for this case. Since we do not currently
1078 // track enough information to differentiate.
1079 return SetA.Index == SetB.Index ? MayAlias : NoAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001080}
Mehdi Amini46a43552015-03-04 18:43:29 +00001081
Chandler Carruthb4faf132016-03-11 10:22:49 +00001082char CFLAA::PassID;
1083
Chandler Carruthb47f8012016-03-11 11:05:24 +00001084CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) {
Chandler Carruth12884f72016-03-02 15:56:53 +00001085 return CFLAAResult();
Chandler Carruth7b560d42015-09-09 17:55:00 +00001086}
1087
Chandler Carruth7b560d42015-09-09 17:55:00 +00001088char CFLAAWrapperPass::ID = 0;
Chandler Carruth12884f72016-03-02 15:56:53 +00001089INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false,
1090 true)
Chandler Carruth7b560d42015-09-09 17:55:00 +00001091
1092ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); }
1093
1094CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) {
1095 initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry());
1096}
1097
1098bool CFLAAWrapperPass::doInitialization(Module &M) {
Chandler Carruth12884f72016-03-02 15:56:53 +00001099 Result.reset(new CFLAAResult());
Chandler Carruth7b560d42015-09-09 17:55:00 +00001100 return false;
1101}
1102
1103bool CFLAAWrapperPass::doFinalization(Module &M) {
1104 Result.reset();
1105 return false;
1106}
1107
1108void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1109 AU.setPreservesAll();
Mehdi Amini46a43552015-03-04 18:43:29 +00001110}