blob: 3e3e4989ca69cd23a8d9dc701f0008c8ce173938 [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 for (auto I = Inst.idx_begin(), E = Inst.idx_end(); I != E; ++I)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000213 Output.push_back(Edge(&Inst, *I, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000214 }
215
216 void visitSelectInst(SelectInst &Inst) {
Daniel Berlin16f7a522015-01-26 17:31:17 +0000217 // Condition is not processed here (The actual statement producing
218 // the condition result is processed elsewhere). For select, the
219 // condition is evaluated, but not loaded, stored, or assigned
220 // simply as a result of being the condition of a select.
221
Hal Finkel7529c552014-09-02 21:43:13 +0000222 auto *TrueVal = Inst.getTrueValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000223 Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000224 auto *FalseVal = Inst.getFalseValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000225 Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000226 }
227
228 void visitAllocaInst(AllocaInst &) {}
229
230 void visitLoadInst(LoadInst &Inst) {
231 auto *Ptr = Inst.getPointerOperand();
232 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000233 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000234 }
235
236 void visitStoreInst(StoreInst &Inst) {
237 auto *Ptr = Inst.getPointerOperand();
238 auto *Val = Inst.getValueOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000239 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000240 }
241
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000242 void visitVAArgInst(VAArgInst &Inst) {
243 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
244 // two things:
245 // 1. Loads a value from *((T*)*Ptr).
246 // 2. Increments (stores to) *Ptr by some target-specific amount.
247 // For now, we'll handle this like a landingpad instruction (by placing the
248 // result in its own group, and having that group alias externals).
249 auto *Val = &Inst;
250 Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll));
251 }
252
Hal Finkel7529c552014-09-02 21:43:13 +0000253 static bool isFunctionExternal(Function *Fn) {
254 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
255 }
256
George Burgess IVcae581d2016-04-13 23:27:37 +0000257 /// Gets whether the sets at Index1 above, below, or equal to the sets at
258 /// Index2. Returns None if they are not in the same set chain.
Hal Finkel7529c552014-09-02 21:43:13 +0000259 static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
260 StratifiedIndex Index1,
261 StratifiedIndex Index2) {
262 if (Index1 == Index2)
263 return Level::Same;
264
265 const auto *Current = &Sets.getLink(Index1);
266 while (Current->hasBelow()) {
267 if (Current->Below == Index2)
268 return Level::Below;
269 Current = &Sets.getLink(Current->Below);
270 }
271
272 Current = &Sets.getLink(Index1);
273 while (Current->hasAbove()) {
274 if (Current->Above == Index2)
275 return Level::Above;
276 Current = &Sets.getLink(Current->Above);
277 }
278
George Burgess IV77351ba32016-01-28 00:54:01 +0000279 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000280 }
281
282 bool
283 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
284 Value *FuncValue,
285 const iterator_range<User::op_iterator> &Args) {
Hal Finkelca616ac2014-09-02 23:29:48 +0000286 const unsigned ExpectedMaxArgs = 8;
287 const unsigned MaxSupportedArgs = 50;
Hal Finkel7529c552014-09-02 21:43:13 +0000288 assert(Fns.size() > 0);
289
George Burgess IVcae581d2016-04-13 23:27:37 +0000290 // This algorithm is n^2, so an arbitrary upper-bound of 50 args was
291 // selected, so it doesn't take too long in insane cases.
George Burgess IVab03af22015-03-10 02:58:15 +0000292 if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000293 return false;
294
295 // Exit early if we'll fail anyway
296 for (auto *Fn : Fns) {
297 if (isFunctionExternal(Fn) || Fn->isVarArg())
298 return false;
299 auto &MaybeInfo = AA.ensureCached(Fn);
300 if (!MaybeInfo.hasValue())
301 return false;
302 }
303
304 SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
305 SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
306 for (auto *Fn : Fns) {
307 auto &Info = *AA.ensureCached(Fn);
308 auto &Sets = Info.Sets;
309 auto &RetVals = Info.ReturnedValues;
310
311 Parameters.clear();
312 for (auto &Param : Fn->args()) {
313 auto MaybeInfo = Sets.find(&Param);
314 // Did a new parameter somehow get added to the function/slip by?
315 if (!MaybeInfo.hasValue())
316 return false;
317 Parameters.push_back(*MaybeInfo);
318 }
319
320 // Adding an edge from argument -> return value for each parameter that
321 // may alias the return value
322 for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
323 auto &ParamInfo = Parameters[I];
324 auto &ArgVal = Arguments[I];
325 bool AddEdge = false;
326 StratifiedAttrs Externals;
327 for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
328 auto MaybeInfo = Sets.find(RetVals[X]);
329 if (!MaybeInfo.hasValue())
330 return false;
331
332 auto &RetInfo = *MaybeInfo;
333 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
334 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
335 auto MaybeRelation =
336 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
337 if (MaybeRelation.hasValue()) {
338 AddEdge = true;
339 Externals |= RetAttrs | ParamAttrs;
340 }
341 }
342 if (AddEdge)
Hal Finkelca616ac2014-09-02 23:29:48 +0000343 Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign,
George Burgess IV11d509d2015-03-15 00:52:21 +0000344 StratifiedAttrs().flip()));
Hal Finkel7529c552014-09-02 21:43:13 +0000345 }
346
347 if (Parameters.size() != Arguments.size())
348 return false;
349
George Burgess IVcae581d2016-04-13 23:27:37 +0000350 /// Adding edges between arguments for arguments that may end up aliasing
351 /// each other. This is necessary for functions such as
352 /// void foo(int** a, int** b) { *a = *b; }
353 /// (Technically, the proper sets for this would be those below
354 /// Arguments[I] and Arguments[X], but our algorithm will produce
355 /// extremely similar, and equally correct, results either way)
Hal Finkel7529c552014-09-02 21:43:13 +0000356 for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
357 auto &MainVal = Arguments[I];
358 auto &MainInfo = Parameters[I];
359 auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
360 for (unsigned X = I + 1; X != E; ++X) {
361 auto &SubInfo = Parameters[X];
362 auto &SubVal = Arguments[X];
363 auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
364 auto MaybeRelation =
365 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
366
367 if (!MaybeRelation.hasValue())
368 continue;
369
370 auto NewAttrs = SubAttrs | MainAttrs;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000371 Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
Hal Finkel7529c552014-09-02 21:43:13 +0000372 }
373 }
374 }
375 return true;
376 }
377
378 template <typename InstT> void visitCallLikeInst(InstT &Inst) {
George Burgess IV68b36e02015-08-28 00:16:18 +0000379 // TODO: Add support for noalias args/all the other fun function attributes
380 // that we can tack on.
Hal Finkel7529c552014-09-02 21:43:13 +0000381 SmallVector<Function *, 4> Targets;
382 if (getPossibleTargets(&Inst, Targets)) {
383 if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
384 return;
385 // Cleanup from interprocedural analysis
386 Output.clear();
387 }
388
George Burgess IV68b36e02015-08-28 00:16:18 +0000389 // Because the function is opaque, we need to note that anything
390 // could have happened to the arguments, and that the result could alias
391 // just about anything, too.
392 // The goal of the loop is in part to unify many Values into one set, so we
393 // don't care if the function is void there.
Hal Finkel7529c552014-09-02 21:43:13 +0000394 for (Value *V : Inst.arg_operands())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000395 Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll));
George Burgess IV68b36e02015-08-28 00:16:18 +0000396 if (Inst.getNumArgOperands() == 0 &&
397 Inst.getType() != Type::getVoidTy(Inst.getContext()))
398 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
Hal Finkel7529c552014-09-02 21:43:13 +0000399 }
400
401 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
402
403 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
404
George Burgess IVcae581d2016-04-13 23:27:37 +0000405 /// Because vectors/aggregates are immutable and unaddressable, there's
406 /// nothing we can do to coax a value out of them, other than calling
407 /// Extract{Element,Value}. We can effectively treat them as pointers to
408 /// arbitrary memory locations we can store in and load from.
Hal Finkel7529c552014-09-02 21:43:13 +0000409 void visitExtractElementInst(ExtractElementInst &Inst) {
410 auto *Ptr = Inst.getVectorOperand();
411 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000412 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000413 }
414
415 void visitInsertElementInst(InsertElementInst &Inst) {
416 auto *Vec = Inst.getOperand(0);
417 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000418 Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
419 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000420 }
421
422 void visitLandingPadInst(LandingPadInst &Inst) {
423 // Exceptions come from "nowhere", from our analysis' perspective.
424 // So we place the instruction its own group, noting that said group may
425 // alias externals
Hal Finkel8d1590d2014-09-02 22:52:30 +0000426 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
Hal Finkel7529c552014-09-02 21:43:13 +0000427 }
428
429 void visitInsertValueInst(InsertValueInst &Inst) {
430 auto *Agg = Inst.getOperand(0);
431 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000432 Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
433 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000434 }
435
436 void visitExtractValueInst(ExtractValueInst &Inst) {
437 auto *Ptr = Inst.getAggregateOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000438 Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000439 }
440
441 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
442 auto *From1 = Inst.getOperand(0);
443 auto *From2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000444 Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
445 Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000446 }
Pete Cooper36642532015-06-12 16:13:54 +0000447
448 void visitConstantExpr(ConstantExpr *CE) {
449 switch (CE->getOpcode()) {
450 default:
451 llvm_unreachable("Unknown instruction type encountered!");
452// Build the switch statement using the Instruction.def file.
453#define HANDLE_INST(NUM, OPCODE, CLASS) \
454 case Instruction::OPCODE: \
455 visit##OPCODE(*(CLASS *)CE); \
456 break;
457#include "llvm/IR/Instruction.def"
458 }
459 }
Hal Finkel7529c552014-09-02 21:43:13 +0000460};
461
George Burgess IVcae581d2016-04-13 23:27:37 +0000462/// For a given instruction, we need to know which Value* to get the
463/// users of in order to build our graph. In some cases (i.e. add),
464/// we simply need the Instruction*. In other cases (i.e. store),
465/// finding the users of the Instruction* is useless; we need to find
466/// the users of the first operand. This handles determining which
467/// value to follow for us.
468///
469/// Note: we *need* to keep this in sync with GetEdgesVisitor. Add
470/// something to GetEdgesVisitor, add it here -- remove something from
471/// GetEdgesVisitor, remove it here.
Hal Finkel7529c552014-09-02 21:43:13 +0000472class GetTargetValueVisitor
473 : public InstVisitor<GetTargetValueVisitor, Value *> {
474public:
475 Value *visitInstruction(Instruction &Inst) { return &Inst; }
476
477 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
478
479 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
480 return Inst.getPointerOperand();
481 }
482
483 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
484 return Inst.getPointerOperand();
485 }
486
487 Value *visitInsertElementInst(InsertElementInst &Inst) {
488 return Inst.getOperand(0);
489 }
490
491 Value *visitInsertValueInst(InsertValueInst &Inst) {
492 return Inst.getAggregateOperand();
493 }
494};
495
George Burgess IVcae581d2016-04-13 23:27:37 +0000496/// Set building requires a weighted bidirectional graph.
Hal Finkel7529c552014-09-02 21:43:13 +0000497template <typename EdgeTypeT> class WeightedBidirectionalGraph {
498public:
499 typedef std::size_t Node;
500
501private:
Hal Finkelca616ac2014-09-02 23:29:48 +0000502 const static Node StartNode = Node(0);
Hal Finkel7529c552014-09-02 21:43:13 +0000503
504 struct Edge {
505 EdgeTypeT Weight;
506 Node Other;
507
George Burgess IV11d509d2015-03-15 00:52:21 +0000508 Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {}
Hal Finkelca616ac2014-09-02 23:29:48 +0000509
Hal Finkel7529c552014-09-02 21:43:13 +0000510 bool operator==(const Edge &E) const {
511 return Weight == E.Weight && Other == E.Other;
512 }
513
514 bool operator!=(const Edge &E) const { return !operator==(E); }
515 };
516
517 struct NodeImpl {
518 std::vector<Edge> Edges;
519 };
520
521 std::vector<NodeImpl> NodeImpls;
522
523 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
524
525 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
526 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
527
528public:
George Burgess IVcae581d2016-04-13 23:27:37 +0000529 /// \brief Iterator for edges. Because this graph is bidirected, we don't
530 /// allow modification of the edges using this iterator. Additionally, the
531 /// iterator becomes invalid if you add edges to or from the node you're
532 /// getting the edges of.
Hal Finkel7529c552014-09-02 21:43:13 +0000533 struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
534 std::tuple<EdgeTypeT, Node *>> {
535 EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
536 : Current(Iter) {}
537
538 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
539
540 EdgeIterator &operator++() {
541 ++Current;
542 return *this;
543 }
544
545 EdgeIterator operator++(int) {
546 EdgeIterator Copy(Current);
547 operator++();
548 return Copy;
549 }
550
551 std::tuple<EdgeTypeT, Node> &operator*() {
552 Store = std::make_tuple(Current->Weight, Current->Other);
553 return Store;
554 }
555
556 bool operator==(const EdgeIterator &Other) const {
557 return Current == Other.Current;
558 }
559
560 bool operator!=(const EdgeIterator &Other) const {
561 return !operator==(Other);
562 }
563
564 private:
565 typename std::vector<Edge>::const_iterator Current;
566 std::tuple<EdgeTypeT, Node> Store;
567 };
568
George Burgess IVcae581d2016-04-13 23:27:37 +0000569 /// Wrapper for EdgeIterator with begin()/end() calls.
Hal Finkel7529c552014-09-02 21:43:13 +0000570 struct EdgeIterable {
571 EdgeIterable(const std::vector<Edge> &Edges)
572 : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
573
574 EdgeIterator begin() { return EdgeIterator(BeginIter); }
575
576 EdgeIterator end() { return EdgeIterator(EndIter); }
577
578 private:
579 typename std::vector<Edge>::const_iterator BeginIter;
580 typename std::vector<Edge>::const_iterator EndIter;
581 };
582
583 // ----- Actual graph-related things ----- //
584
Hal Finkelca616ac2014-09-02 23:29:48 +0000585 WeightedBidirectionalGraph() {}
Hal Finkel7529c552014-09-02 21:43:13 +0000586
587 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
588 : NodeImpls(std::move(Other.NodeImpls)) {}
589
590 WeightedBidirectionalGraph<EdgeTypeT> &
591 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
592 NodeImpls = std::move(Other.NodeImpls);
593 return *this;
594 }
595
596 Node addNode() {
597 auto Index = NodeImpls.size();
598 auto NewNode = Node(Index);
599 NodeImpls.push_back(NodeImpl());
600 return NewNode;
601 }
602
603 void addEdge(Node From, Node To, const EdgeTypeT &Weight,
604 const EdgeTypeT &ReverseWeight) {
605 assert(inbounds(From));
606 assert(inbounds(To));
607 auto &FromNode = getNode(From);
608 auto &ToNode = getNode(To);
Hal Finkelca616ac2014-09-02 23:29:48 +0000609 FromNode.Edges.push_back(Edge(Weight, To));
610 ToNode.Edges.push_back(Edge(ReverseWeight, From));
Hal Finkel7529c552014-09-02 21:43:13 +0000611 }
612
George Burgess IVcae581d2016-04-13 23:27:37 +0000613 iterator_range<EdgeIterator> edgesFor(const Node &N) const {
Hal Finkel7529c552014-09-02 21:43:13 +0000614 const auto &Node = getNode(N);
George Burgess IVcae581d2016-04-13 23:27:37 +0000615 return make_range(EdgeIterator(Node.Edges.begin()),
616 EdgeIterator(Node.Edges.end()));
Hal Finkel7529c552014-09-02 21:43:13 +0000617 }
618
619 bool empty() const { return NodeImpls.empty(); }
620 std::size_t size() const { return NodeImpls.size(); }
621
George Burgess IVcae581d2016-04-13 23:27:37 +0000622 /// Gets an arbitrary node in the graph as a starting point for traversal.
Hal Finkel7529c552014-09-02 21:43:13 +0000623 Node getEntryNode() {
624 assert(inbounds(StartNode));
625 return StartNode;
626 }
627};
628
629typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
630typedef DenseMap<Value *, GraphT::Node> NodeMapT;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000631}
Hal Finkel7529c552014-09-02 21:43:13 +0000632
Hal Finkel7529c552014-09-02 21:43:13 +0000633//===----------------------------------------------------------------------===//
634// Function declarations that require types defined in the namespace above
635//===----------------------------------------------------------------------===//
636
George Burgess IVcae581d2016-04-13 23:27:37 +0000637/// Given an argument number, returns the appropriate Attr index to set.
638static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum);
Hal Finkel7529c552014-09-02 21:43:13 +0000639
George Burgess IVcae581d2016-04-13 23:27:37 +0000640/// Given a Value, potentially return which AttrIndex it maps to.
Hal Finkel7529c552014-09-02 21:43:13 +0000641static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
642
George Burgess IVcae581d2016-04-13 23:27:37 +0000643/// Gets the inverse of a given EdgeType.
644static EdgeType flipWeight(EdgeType Initial);
Hal Finkel7529c552014-09-02 21:43:13 +0000645
George Burgess IVcae581d2016-04-13 23:27:37 +0000646/// Gets edges of the given Instruction*, writing them to the SmallVector*.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000647static void argsToEdges(CFLAAResult &, Instruction *, SmallVectorImpl<Edge> &);
Hal Finkel7529c552014-09-02 21:43:13 +0000648
George Burgess IVcae581d2016-04-13 23:27:37 +0000649/// Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000650static void argsToEdges(CFLAAResult &, ConstantExpr *, SmallVectorImpl<Edge> &);
Pete Cooper36642532015-06-12 16:13:54 +0000651
George Burgess IVcae581d2016-04-13 23:27:37 +0000652/// Gets the "Level" that one should travel in StratifiedSets
653/// given an EdgeType.
Hal Finkel7529c552014-09-02 21:43:13 +0000654static Level directionOfEdgeType(EdgeType);
655
George Burgess IVcae581d2016-04-13 23:27:37 +0000656/// Builds the graph needed for constructing the StratifiedSets for the
657/// given function
Chandler Carruth7b560d42015-09-09 17:55:00 +0000658static void buildGraphFrom(CFLAAResult &, Function *,
Hal Finkel7529c552014-09-02 21:43:13 +0000659 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
660
George Burgess IVcae581d2016-04-13 23:27:37 +0000661/// Gets the edges of a ConstantExpr as if it was an Instruction. This function
662/// also acts on any nested ConstantExprs, adding the edges of those to the
663/// given SmallVector as well.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000664static void constexprToEdges(CFLAAResult &, ConstantExpr &,
George Burgess IVab03af22015-03-10 02:58:15 +0000665 SmallVectorImpl<Edge> &);
666
George Burgess IVcae581d2016-04-13 23:27:37 +0000667/// Given an Instruction, this will add it to the graph, along with any
668/// Instructions that are potentially only available from said Instruction
669/// For example, given the following line:
670/// %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
671/// addInstructionToGraph would add both the `load` and `getelementptr`
672/// instructions to the graph appropriately.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000673static void addInstructionToGraph(CFLAAResult &, Instruction &,
George Burgess IVab03af22015-03-10 02:58:15 +0000674 SmallVectorImpl<Value *> &, NodeMapT &,
675 GraphT &);
676
George Burgess IVcae581d2016-04-13 23:27:37 +0000677/// Determines whether it would be pointless to add the given Value to our sets.
George Burgess IVab03af22015-03-10 02:58:15 +0000678static bool canSkipAddingToSets(Value *Val);
679
Hal Finkel7529c552014-09-02 21:43:13 +0000680static Optional<Function *> parentFunctionOfValue(Value *Val) {
681 if (auto *Inst = dyn_cast<Instruction>(Val)) {
682 auto *Bb = Inst->getParent();
683 return Bb->getParent();
684 }
685
686 if (auto *Arg = dyn_cast<Argument>(Val))
687 return Arg->getParent();
George Burgess IV77351ba32016-01-28 00:54:01 +0000688 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000689}
690
691template <typename Inst>
692static bool getPossibleTargets(Inst *Call,
693 SmallVectorImpl<Function *> &Output) {
694 if (auto *Fn = Call->getCalledFunction()) {
695 Output.push_back(Fn);
696 return true;
697 }
698
699 // TODO: If the call is indirect, we might be able to enumerate all potential
700 // targets of the call and return them, rather than just failing.
701 return false;
702}
703
704static Optional<Value *> getTargetValue(Instruction *Inst) {
705 GetTargetValueVisitor V;
706 return V.visit(Inst);
707}
708
709static bool hasUsefulEdges(Instruction *Inst) {
710 bool IsNonInvokeTerminator =
711 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
712 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
713}
714
Pete Cooper36642532015-06-12 16:13:54 +0000715static bool hasUsefulEdges(ConstantExpr *CE) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000716 // ConstantExpr doesn't have terminators, invokes, or fences, so only needs
Pete Cooper36642532015-06-12 16:13:54 +0000717 // to check for compares.
718 return CE->getOpcode() != Instruction::ICmp &&
719 CE->getOpcode() != Instruction::FCmp;
720}
721
Hal Finkel7529c552014-09-02 21:43:13 +0000722static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
723 if (isa<GlobalValue>(Val))
724 return AttrGlobalIndex;
725
726 if (auto *Arg = dyn_cast<Argument>(Val))
Daniel Berlin16f7a522015-01-26 17:31:17 +0000727 // Only pointer arguments should have the argument attribute,
728 // because things can't escape through scalars without us seeing a
729 // cast, and thus, interaction with them doesn't matter.
730 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
Hal Finkel7529c552014-09-02 21:43:13 +0000731 return argNumberToAttrIndex(Arg->getArgNo());
George Burgess IV77351ba32016-01-28 00:54:01 +0000732 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000733}
734
735static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
George Burgess IV3c898c22015-01-21 16:37:21 +0000736 if (ArgNum >= AttrMaxNumArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000737 return AttrAllIndex;
738 return ArgNum + AttrFirstArgIndex;
739}
740
741static EdgeType flipWeight(EdgeType Initial) {
742 switch (Initial) {
743 case EdgeType::Assign:
744 return EdgeType::Assign;
745 case EdgeType::Dereference:
746 return EdgeType::Reference;
747 case EdgeType::Reference:
748 return EdgeType::Dereference;
749 }
750 llvm_unreachable("Incomplete coverage of EdgeType enum");
751}
752
Chandler Carruth7b560d42015-09-09 17:55:00 +0000753static void argsToEdges(CFLAAResult &Analysis, Instruction *Inst,
Hal Finkel7529c552014-09-02 21:43:13 +0000754 SmallVectorImpl<Edge> &Output) {
George Burgess IVab03af22015-03-10 02:58:15 +0000755 assert(hasUsefulEdges(Inst) &&
756 "Expected instructions to have 'useful' edges");
Hal Finkel7529c552014-09-02 21:43:13 +0000757 GetEdgesVisitor v(Analysis, Output);
758 v.visit(Inst);
759}
760
Chandler Carruth7b560d42015-09-09 17:55:00 +0000761static void argsToEdges(CFLAAResult &Analysis, ConstantExpr *CE,
Pete Cooper36642532015-06-12 16:13:54 +0000762 SmallVectorImpl<Edge> &Output) {
763 assert(hasUsefulEdges(CE) && "Expected constant expr to have 'useful' edges");
764 GetEdgesVisitor v(Analysis, Output);
765 v.visitConstantExpr(CE);
766}
767
Hal Finkel7529c552014-09-02 21:43:13 +0000768static Level directionOfEdgeType(EdgeType Weight) {
769 switch (Weight) {
770 case EdgeType::Reference:
771 return Level::Above;
772 case EdgeType::Dereference:
773 return Level::Below;
774 case EdgeType::Assign:
775 return Level::Same;
776 }
777 llvm_unreachable("Incomplete switch coverage");
778}
779
Chandler Carruth7b560d42015-09-09 17:55:00 +0000780static void constexprToEdges(CFLAAResult &Analysis,
George Burgess IVab03af22015-03-10 02:58:15 +0000781 ConstantExpr &CExprToCollapse,
782 SmallVectorImpl<Edge> &Results) {
783 SmallVector<ConstantExpr *, 4> Worklist;
784 Worklist.push_back(&CExprToCollapse);
785
786 SmallVector<Edge, 8> ConstexprEdges;
Pete Cooper36642532015-06-12 16:13:54 +0000787 SmallPtrSet<ConstantExpr *, 4> Visited;
George Burgess IVab03af22015-03-10 02:58:15 +0000788 while (!Worklist.empty()) {
789 auto *CExpr = Worklist.pop_back_val();
George Burgess IVab03af22015-03-10 02:58:15 +0000790
Pete Cooper36642532015-06-12 16:13:54 +0000791 if (!hasUsefulEdges(CExpr))
George Burgess IVab03af22015-03-10 02:58:15 +0000792 continue;
793
794 ConstexprEdges.clear();
Pete Cooper36642532015-06-12 16:13:54 +0000795 argsToEdges(Analysis, CExpr, ConstexprEdges);
George Burgess IVab03af22015-03-10 02:58:15 +0000796 for (auto &Edge : ConstexprEdges) {
Pete Cooper36642532015-06-12 16:13:54 +0000797 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
798 if (Visited.insert(Nested).second)
799 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000800
Pete Cooper36642532015-06-12 16:13:54 +0000801 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
802 if (Visited.insert(Nested).second)
803 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000804 }
805
806 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
807 }
808}
809
Chandler Carruth7b560d42015-09-09 17:55:00 +0000810static void addInstructionToGraph(CFLAAResult &Analysis, Instruction &Inst,
George Burgess IVab03af22015-03-10 02:58:15 +0000811 SmallVectorImpl<Value *> &ReturnedValues,
812 NodeMapT &Map, GraphT &Graph) {
Hal Finkel7529c552014-09-02 21:43:13 +0000813 const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
814 auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
815 auto &Iter = Pair.first;
816 if (Pair.second) {
817 auto NewNode = Graph.addNode();
818 Iter->second = NewNode;
819 }
820 return Iter->second;
821 };
822
George Burgess IVab03af22015-03-10 02:58:15 +0000823 // We don't want the edges of most "return" instructions, but we *do* want
824 // to know what can be returned.
825 if (isa<ReturnInst>(&Inst))
826 ReturnedValues.push_back(&Inst);
827
828 if (!hasUsefulEdges(&Inst))
829 return;
830
Hal Finkel7529c552014-09-02 21:43:13 +0000831 SmallVector<Edge, 8> Edges;
George Burgess IVab03af22015-03-10 02:58:15 +0000832 argsToEdges(Analysis, &Inst, Edges);
Hal Finkel7529c552014-09-02 21:43:13 +0000833
George Burgess IVab03af22015-03-10 02:58:15 +0000834 // In the case of an unused alloca (or similar), edges may be empty. Note
835 // that it exists so we can potentially answer NoAlias.
836 if (Edges.empty()) {
837 auto MaybeVal = getTargetValue(&Inst);
838 assert(MaybeVal.hasValue());
839 auto *Target = *MaybeVal;
840 findOrInsertNode(Target);
841 return;
Hal Finkel7529c552014-09-02 21:43:13 +0000842 }
George Burgess IVab03af22015-03-10 02:58:15 +0000843
George Burgess IVcae581d2016-04-13 23:27:37 +0000844 auto addEdgeToGraph = [&](const Edge &E) {
George Burgess IVab03af22015-03-10 02:58:15 +0000845 auto To = findOrInsertNode(E.To);
846 auto From = findOrInsertNode(E.From);
847 auto FlippedWeight = flipWeight(E.Weight);
848 auto Attrs = E.AdditionalAttrs;
849 Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
850 std::make_pair(FlippedWeight, Attrs));
851 };
852
853 SmallVector<ConstantExpr *, 4> ConstantExprs;
854 for (const Edge &E : Edges) {
855 addEdgeToGraph(E);
856 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
857 ConstantExprs.push_back(Constexpr);
858 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
859 ConstantExprs.push_back(Constexpr);
860 }
861
862 for (ConstantExpr *CE : ConstantExprs) {
863 Edges.clear();
864 constexprToEdges(Analysis, *CE, Edges);
865 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
866 }
867}
868
Chandler Carruth7b560d42015-09-09 17:55:00 +0000869static void buildGraphFrom(CFLAAResult &Analysis, Function *Fn,
George Burgess IVab03af22015-03-10 02:58:15 +0000870 SmallVectorImpl<Value *> &ReturnedValues,
871 NodeMapT &Map, GraphT &Graph) {
George Burgess IVcae581d2016-04-13 23:27:37 +0000872 // (N.B. We may remove graph construction entirely, because it doesn't really
873 // buy us much.)
George Burgess IVab03af22015-03-10 02:58:15 +0000874 for (auto &Bb : Fn->getBasicBlockList())
875 for (auto &Inst : Bb.getInstList())
876 addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph);
877}
878
879static bool canSkipAddingToSets(Value *Val) {
880 // Constants can share instances, which may falsely unify multiple
881 // sets, e.g. in
882 // store i32* null, i32** %ptr1
883 // store i32* null, i32** %ptr2
884 // clearly ptr1 and ptr2 should not be unified into the same set, so
885 // we should filter out the (potentially shared) instance to
886 // i32* null.
887 if (isa<Constant>(Val)) {
George Burgess IVab03af22015-03-10 02:58:15 +0000888 // TODO: Because all of these things are constant, we can determine whether
889 // the data is *actually* mutable at graph building time. This will probably
890 // come for free/cheap with offset awareness.
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000891 bool CanStoreMutableData = isa<GlobalValue>(Val) ||
892 isa<ConstantExpr>(Val) ||
893 isa<ConstantAggregate>(Val);
George Burgess IVab03af22015-03-10 02:58:15 +0000894 return !CanStoreMutableData;
895 }
896
897 return false;
Hal Finkel7529c552014-09-02 21:43:13 +0000898}
899
Chandler Carruth8b046a42015-08-14 02:42:20 +0000900// Builds the graph + StratifiedSets for a function.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000901CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) {
Hal Finkel7529c552014-09-02 21:43:13 +0000902 NodeMapT Map;
903 GraphT Graph;
904 SmallVector<Value *, 4> ReturnedValues;
905
Chandler Carruth8b046a42015-08-14 02:42:20 +0000906 buildGraphFrom(*this, Fn, ReturnedValues, Map, Graph);
Hal Finkel7529c552014-09-02 21:43:13 +0000907
908 DenseMap<GraphT::Node, Value *> NodeValueMap;
Mehdi Aminic04fc7a2016-03-22 07:20:00 +0000909 NodeValueMap.reserve(Map.size());
Hal Finkel7529c552014-09-02 21:43:13 +0000910 for (const auto &Pair : Map)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000911 NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
Hal Finkel7529c552014-09-02 21:43:13 +0000912
913 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
914 auto ValIter = NodeValueMap.find(Node);
915 assert(ValIter != NodeValueMap.end());
916 return ValIter->second;
917 };
918
919 StratifiedSetsBuilder<Value *> Builder;
920
921 SmallVector<GraphT::Node, 16> Worklist;
922 for (auto &Pair : Map) {
923 Worklist.clear();
924
925 auto *Value = Pair.first;
926 Builder.add(Value);
927 auto InitialNode = Pair.second;
928 Worklist.push_back(InitialNode);
929 while (!Worklist.empty()) {
930 auto Node = Worklist.pop_back_val();
931 auto *CurValue = findValueOrDie(Node);
George Burgess IVab03af22015-03-10 02:58:15 +0000932 if (canSkipAddingToSets(CurValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000933 continue;
934
George Burgess IV7e5404c2016-04-05 21:40:45 +0000935 Optional<StratifiedAttr> MaybeCurIndex = valueToAttrIndex(CurValue);
936 if (MaybeCurIndex)
937 Builder.noteAttributes(CurValue, *MaybeCurIndex);
938
Hal Finkel7529c552014-09-02 21:43:13 +0000939 for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
940 auto Weight = std::get<0>(EdgeTuple);
941 auto Label = Weight.first;
942 auto &OtherNode = std::get<1>(EdgeTuple);
943 auto *OtherValue = findValueOrDie(OtherNode);
944
George Burgess IVab03af22015-03-10 02:58:15 +0000945 if (canSkipAddingToSets(OtherValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000946 continue;
947
948 bool Added;
949 switch (directionOfEdgeType(Label)) {
950 case Level::Above:
951 Added = Builder.addAbove(CurValue, OtherValue);
952 break;
953 case Level::Below:
954 Added = Builder.addBelow(CurValue, OtherValue);
955 break;
956 case Level::Same:
957 Added = Builder.addWith(CurValue, OtherValue);
958 break;
959 }
960
George Burgess IVb54a8d622015-03-10 02:40:06 +0000961 auto Aliasing = Weight.second;
George Burgess IV7e5404c2016-04-05 21:40:45 +0000962 if (MaybeCurIndex)
George Burgess IVb54a8d622015-03-10 02:40:06 +0000963 Aliasing.set(*MaybeCurIndex);
964 if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
965 Aliasing.set(*MaybeOtherIndex);
966 Builder.noteAttributes(CurValue, Aliasing);
967 Builder.noteAttributes(OtherValue, Aliasing);
968
969 if (Added)
Hal Finkel7529c552014-09-02 21:43:13 +0000970 Worklist.push_back(OtherNode);
Hal Finkel7529c552014-09-02 21:43:13 +0000971 }
972 }
973 }
974
975 // There are times when we end up with parameters not in our graph (i.e. if
976 // it's only used as the condition of a branch). Other bits of code depend on
977 // things that were present during construction being present in the graph.
978 // So, we add all present arguments here.
979 for (auto &Arg : Fn->args()) {
George Burgess IVab03af22015-03-10 02:58:15 +0000980 if (!Builder.add(&Arg))
981 continue;
982
983 auto Attrs = valueToAttrIndex(&Arg);
984 if (Attrs.hasValue())
985 Builder.noteAttributes(&Arg, *Attrs);
Hal Finkel7529c552014-09-02 21:43:13 +0000986 }
987
Hal Finkel85f26922014-09-03 00:06:47 +0000988 return FunctionInfo(Builder.build(), std::move(ReturnedValues));
Hal Finkel7529c552014-09-02 21:43:13 +0000989}
990
Chandler Carruth7b560d42015-09-09 17:55:00 +0000991void CFLAAResult::scan(Function *Fn) {
Hal Finkel8d1590d2014-09-02 22:52:30 +0000992 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
Hal Finkel7529c552014-09-02 21:43:13 +0000993 (void)InsertPair;
994 assert(InsertPair.second &&
995 "Trying to scan a function that has already been cached");
996
George Burgess IVcae581d2016-04-13 23:27:37 +0000997 Cache[Fn] = buildSetsFrom(Fn);
Hal Finkel7529c552014-09-02 21:43:13 +0000998 Handles.push_front(FunctionHandle(Fn, this));
999}
1000
Chandler Carruth7b560d42015-09-09 17:55:00 +00001001void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); }
Chandler Carruth8b046a42015-08-14 02:42:20 +00001002
George Burgess IVcae581d2016-04-13 23:27:37 +00001003/// Ensures that the given function is available in the cache, and returns the
1004/// entry.
Chandler Carruth7b560d42015-09-09 17:55:00 +00001005const Optional<CFLAAResult::FunctionInfo> &
1006CFLAAResult::ensureCached(Function *Fn) {
Chandler Carruth8b046a42015-08-14 02:42:20 +00001007 auto Iter = Cache.find(Fn);
1008 if (Iter == Cache.end()) {
1009 scan(Fn);
1010 Iter = Cache.find(Fn);
1011 assert(Iter != Cache.end());
1012 assert(Iter->second.hasValue());
1013 }
1014 return Iter->second;
1015}
1016
Chandler Carruth7b560d42015-09-09 17:55:00 +00001017AliasResult CFLAAResult::query(const MemoryLocation &LocA,
1018 const MemoryLocation &LocB) {
Hal Finkel7529c552014-09-02 21:43:13 +00001019 auto *ValA = const_cast<Value *>(LocA.Ptr);
1020 auto *ValB = const_cast<Value *>(LocB.Ptr);
1021
1022 Function *Fn = nullptr;
1023 auto MaybeFnA = parentFunctionOfValue(ValA);
1024 auto MaybeFnB = parentFunctionOfValue(ValB);
1025 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
George Burgess IVcae581d2016-04-13 23:27:37 +00001026 // The only times this is known to happen are when globals + InlineAsm are
1027 // involved
George Burgess IV33305e72015-02-12 03:07:07 +00001028 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001029 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001030 }
1031
1032 if (MaybeFnA.hasValue()) {
1033 Fn = *MaybeFnA;
1034 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1035 "Interprocedural queries not supported");
1036 } else {
1037 Fn = *MaybeFnB;
1038 }
1039
1040 assert(Fn != nullptr);
1041 auto &MaybeInfo = ensureCached(Fn);
1042 assert(MaybeInfo.hasValue());
1043
1044 auto &Sets = MaybeInfo->Sets;
1045 auto MaybeA = Sets.find(ValA);
1046 if (!MaybeA.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001047 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001048
1049 auto MaybeB = Sets.find(ValB);
1050 if (!MaybeB.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001051 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001052
1053 auto SetA = *MaybeA;
1054 auto SetB = *MaybeB;
Hal Finkel7529c552014-09-02 21:43:13 +00001055 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1056 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
George Burgess IV33305e72015-02-12 03:07:07 +00001057
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001058 // Stratified set attributes are used as markets to signify whether a member
George Burgess IV33305e72015-02-12 03:07:07 +00001059 // of a StratifiedSet (or a member of a set above the current set) has
George Burgess IVcae581d2016-04-13 23:27:37 +00001060 // interacted with either arguments or globals. "Interacted with" meaning its
1061 // value may be different depending on the value of an argument or global. The
1062 // thought behind this is that, because arguments and globals may alias each
1063 // other, if AttrsA and AttrsB have touched args/globals, we must
1064 // conservatively say that they alias. However, if at least one of the sets
1065 // has no values that could legally be altered by changing the value of an
1066 // argument or global, then we don't have to be as conservative.
Hal Finkel8eae3ad2014-10-06 14:42:56 +00001067 if (AttrsA.any() && AttrsB.any())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001068 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001069
Daniel Berlin16f7a522015-01-26 17:31:17 +00001070 // We currently unify things even if the accesses to them may not be in
George Burgess IVcae581d2016-04-13 23:27:37 +00001071 // bounds, so we can't return partial alias here because we don't know whether
1072 // the pointer is really within the object or not.
1073 // e.g. Given an out of bounds GEP and an alloca'd pointer, we may unify the
1074 // two. We can't return partial alias for this case. Since we do not currently
1075 // track enough information to differentiate.
1076 return SetA.Index == SetB.Index ? MayAlias : NoAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001077}
Mehdi Amini46a43552015-03-04 18:43:29 +00001078
Chandler Carruthb4faf132016-03-11 10:22:49 +00001079char CFLAA::PassID;
1080
Chandler Carruthb47f8012016-03-11 11:05:24 +00001081CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) {
Chandler Carruth12884f72016-03-02 15:56:53 +00001082 return CFLAAResult();
Chandler Carruth7b560d42015-09-09 17:55:00 +00001083}
1084
Chandler Carruth7b560d42015-09-09 17:55:00 +00001085char CFLAAWrapperPass::ID = 0;
Chandler Carruth12884f72016-03-02 15:56:53 +00001086INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false,
1087 true)
Chandler Carruth7b560d42015-09-09 17:55:00 +00001088
1089ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); }
1090
1091CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) {
1092 initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry());
1093}
1094
1095bool CFLAAWrapperPass::doInitialization(Module &M) {
Chandler Carruth12884f72016-03-02 15:56:53 +00001096 Result.reset(new CFLAAResult());
Chandler Carruth7b560d42015-09-09 17:55:00 +00001097 return false;
1098}
1099
1100bool CFLAAWrapperPass::doFinalization(Module &M) {
1101 Result.reset();
1102 return false;
1103}
1104
1105void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1106 AU.setPreservesAll();
Mehdi Amini46a43552015-03-04 18:43:29 +00001107}