blob: 3a6f1ca258541bde2f57bafdb9ebc20d4cd4a013 [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"
George Burgess IV18b83fe2016-06-01 18:39:54 +000041#include "llvm/Analysis/MemoryBuiltins.h"
42#include "llvm/Analysis/TargetLibraryInfo.h"
Hal Finkel7529c552014-09-02 21:43:13 +000043#include "llvm/IR/Constants.h"
44#include "llvm/IR/Function.h"
Hal Finkel7529c552014-09-02 21:43:13 +000045#include "llvm/IR/InstVisitor.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000046#include "llvm/IR/Instructions.h"
Hal Finkel7529c552014-09-02 21:43:13 +000047#include "llvm/Pass.h"
Hal Finkel7d7087c2014-09-02 22:13:00 +000048#include "llvm/Support/Compiler.h"
George Burgess IV33305e72015-02-12 03:07:07 +000049#include "llvm/Support/Debug.h"
Hal Finkel7529c552014-09-02 21:43:13 +000050#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000051#include "llvm/Support/raw_ostream.h"
Hal Finkel7529c552014-09-02 21:43:13 +000052#include <algorithm>
53#include <cassert>
Benjamin Kramer799003b2015-03-23 19:32:43 +000054#include <memory>
Hal Finkel7529c552014-09-02 21:43:13 +000055#include <tuple>
56
57using namespace llvm;
58
George Burgess IV33305e72015-02-12 03:07:07 +000059#define DEBUG_TYPE "cfl-aa"
60
George Burgess IV18b83fe2016-06-01 18:39:54 +000061CFLAAResult::CFLAAResult(const TargetLibraryInfo &TLI)
62 : AAResultBase(), TLI(TLI) {}
63CFLAAResult::CFLAAResult(CFLAAResult &&Arg)
64 : AAResultBase(std::move(Arg)), TLI(Arg.TLI) {}
Chandler Carruth342c6712016-02-20 03:52:02 +000065CFLAAResult::~CFLAAResult() {}
Chandler Carruth8b046a42015-08-14 02:42:20 +000066
George Burgess IVcae581d2016-04-13 23:27:37 +000067/// Information we have about a function and would like to keep around.
Chandler Carruth7b560d42015-09-09 17:55:00 +000068struct CFLAAResult::FunctionInfo {
Chandler Carruth8b046a42015-08-14 02:42:20 +000069 StratifiedSets<Value *> Sets;
70 // Lots of functions have < 4 returns. Adjust as necessary.
71 SmallVector<Value *, 4> ReturnedValues;
72
73 FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV)
74 : Sets(std::move(S)), ReturnedValues(std::move(RV)) {}
75};
76
George Burgess IVcae581d2016-04-13 23:27:37 +000077/// Try to go from a Value* to a Function*. Never returns nullptr.
Hal Finkel7529c552014-09-02 21:43:13 +000078static Optional<Function *> parentFunctionOfValue(Value *);
79
George Burgess IVcae581d2016-04-13 23:27:37 +000080/// Returns possible functions called by the Inst* into the given
81/// SmallVectorImpl. Returns true if targets found, false otherwise. This is
82/// templated so we can use it with CallInsts and InvokeInsts.
Hal Finkel7529c552014-09-02 21:43:13 +000083template <typename Inst>
84static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
85
George Burgess IVcae581d2016-04-13 23:27:37 +000086/// Some instructions need to have their users tracked. Instructions like
87/// `add` require you to get the users of the Instruction* itself, other
88/// instructions like `store` require you to get the users of the first
89/// operand. This function gets the "proper" value to track for each
90/// type of instruction we support.
Hal Finkel7529c552014-09-02 21:43:13 +000091static Optional<Value *> getTargetValue(Instruction *);
92
George Burgess IVcae581d2016-04-13 23:27:37 +000093/// Determines whether or not we an instruction is useless to us (e.g.
94/// FenceInst)
Hal Finkel7529c552014-09-02 21:43:13 +000095static bool hasUsefulEdges(Instruction *);
96
Hal Finkel1ae325f2014-09-02 23:50:01 +000097const StratifiedIndex StratifiedLink::SetSentinel =
George Burgess IV11d509d2015-03-15 00:52:21 +000098 std::numeric_limits<StratifiedIndex>::max();
Hal Finkel1ae325f2014-09-02 23:50:01 +000099
Hal Finkel7529c552014-09-02 21:43:13 +0000100namespace {
George Burgess IVcae581d2016-04-13 23:27:37 +0000101/// StratifiedInfo Attribute things.
Hal Finkel7529c552014-09-02 21:43:13 +0000102typedef unsigned StratifiedAttr;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000103LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000104LLVM_CONSTEXPR unsigned AttrEscapedIndex = 0;
105LLVM_CONSTEXPR unsigned AttrUnknownIndex = 1;
106LLVM_CONSTEXPR unsigned AttrGlobalIndex = 2;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000107LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3;
Hal Finkel7d7087c2014-09-02 22:13:00 +0000108LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
109LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
Hal Finkel7529c552014-09-02 21:43:13 +0000110
Hal Finkel7d7087c2014-09-02 22:13:00 +0000111LLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000112LLVM_CONSTEXPR StratifiedAttr AttrEscaped = 1 << AttrEscapedIndex;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000113LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000114LLVM_CONSTEXPR StratifiedAttr AttrGlobal = 1 << AttrGlobalIndex;
Hal Finkel7529c552014-09-02 21:43:13 +0000115
George Burgess IVcae581d2016-04-13 23:27:37 +0000116/// StratifiedSets call for knowledge of "direction", so this is how we
117/// represent that locally.
Hal Finkel7529c552014-09-02 21:43:13 +0000118enum class Level { Same, Above, Below };
119
George Burgess IVcae581d2016-04-13 23:27:37 +0000120/// Edges can be one of four "weights" -- each weight must have an inverse
121/// weight (Assign has Assign; Reference has Dereference).
Hal Finkel7529c552014-09-02 21:43:13 +0000122enum class EdgeType {
George Burgess IVcae581d2016-04-13 23:27:37 +0000123 /// The weight assigned when assigning from or to a value. For example, in:
124 /// %b = getelementptr %a, 0
125 /// ...The relationships are %b assign %a, and %a assign %b. This used to be
126 /// two edges, but having a distinction bought us nothing.
Hal Finkel7529c552014-09-02 21:43:13 +0000127 Assign,
128
George Burgess IVcae581d2016-04-13 23:27:37 +0000129 /// The edge used when we have an edge going from some handle to a Value.
130 /// Examples of this include:
131 /// %b = load %a (%b Dereference %a)
132 /// %b = extractelement %a, 0 (%a Dereference %b)
Hal Finkel7529c552014-09-02 21:43:13 +0000133 Dereference,
134
George Burgess IVcae581d2016-04-13 23:27:37 +0000135 /// The edge used when our edge goes from a value to a handle that may have
136 /// contained it at some point. Examples:
137 /// %b = load %a (%a Reference %b)
138 /// %b = extractelement %a, 0 (%b Reference %a)
Hal Finkel7529c552014-09-02 21:43:13 +0000139 Reference
140};
141
142// \brief Encodes the notion of a "use"
143struct Edge {
George Burgess IVcae581d2016-04-13 23:27:37 +0000144 /// Which value the edge is coming from
Hal Finkel7529c552014-09-02 21:43:13 +0000145 Value *From;
146
George Burgess IVcae581d2016-04-13 23:27:37 +0000147 /// Which value the edge is pointing to
Hal Finkel7529c552014-09-02 21:43:13 +0000148 Value *To;
149
George Burgess IVcae581d2016-04-13 23:27:37 +0000150 /// Edge weight
Hal Finkel7529c552014-09-02 21:43:13 +0000151 EdgeType Weight;
152
George Burgess IVcae581d2016-04-13 23:27:37 +0000153 /// Whether we aliased any external values along the way that may be
154 /// invisible to the analysis (i.e. landingpad for exceptions, calls for
155 /// interprocedural analysis, etc.)
Hal Finkel7529c552014-09-02 21:43:13 +0000156 StratifiedAttrs AdditionalAttrs;
157
158 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
159 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
160};
161
George Burgess IVcae581d2016-04-13 23:27:37 +0000162/// Gets the edges our graph should have, based on an Instruction*
Hal Finkel7529c552014-09-02 21:43:13 +0000163class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000164 CFLAAResult &AA;
Hal Finkel7529c552014-09-02 21:43:13 +0000165 SmallVectorImpl<Edge> &Output;
George Burgess IV18b83fe2016-06-01 18:39:54 +0000166 const TargetLibraryInfo &TLI;
Hal Finkel7529c552014-09-02 21:43:13 +0000167
168public:
George Burgess IV18b83fe2016-06-01 18:39:54 +0000169 GetEdgesVisitor(CFLAAResult &AA, SmallVectorImpl<Edge> &Output,
170 const TargetLibraryInfo &TLI)
171 : AA(AA), Output(Output), TLI(TLI) {}
Hal Finkel7529c552014-09-02 21:43:13 +0000172
173 void visitInstruction(Instruction &) {
174 llvm_unreachable("Unsupported instruction encountered");
175 }
176
George Burgess IVb54a8d622015-03-10 02:40:06 +0000177 void visitPtrToIntInst(PtrToIntInst &Inst) {
178 auto *Ptr = Inst.getOperand(0);
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000179 Output.push_back(Edge(Ptr, &Inst, EdgeType::Assign, AttrEscaped));
George Burgess IVb54a8d622015-03-10 02:40:06 +0000180 }
181
182 void visitIntToPtrInst(IntToPtrInst &Inst) {
183 auto *Ptr = &Inst;
184 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
185 }
186
Hal Finkel7529c552014-09-02 21:43:13 +0000187 void visitCastInst(CastInst &Inst) {
George Burgess IV11d509d2015-03-15 00:52:21 +0000188 Output.push_back(
189 Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000190 }
191
192 void visitBinaryOperator(BinaryOperator &Inst) {
193 auto *Op1 = Inst.getOperand(0);
194 auto *Op2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000195 Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone));
196 Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000197 }
198
199 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
200 auto *Ptr = Inst.getPointerOperand();
201 auto *Val = Inst.getNewValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000202 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000203 }
204
205 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
206 auto *Ptr = Inst.getPointerOperand();
207 auto *Val = Inst.getValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000208 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000209 }
210
211 void visitPHINode(PHINode &Inst) {
George Burgess IV77351ba32016-01-28 00:54:01 +0000212 for (Value *Val : Inst.incoming_values())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000213 Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000214 }
215
216 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
217 auto *Op = Inst.getPointerOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000218 Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000219 }
220
221 void visitSelectInst(SelectInst &Inst) {
Daniel Berlin16f7a522015-01-26 17:31:17 +0000222 // Condition is not processed here (The actual statement producing
223 // the condition result is processed elsewhere). For select, the
224 // condition is evaluated, but not loaded, stored, or assigned
225 // simply as a result of being the condition of a select.
226
Hal Finkel7529c552014-09-02 21:43:13 +0000227 auto *TrueVal = Inst.getTrueValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000228 Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000229 auto *FalseVal = Inst.getFalseValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000230 Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000231 }
232
233 void visitAllocaInst(AllocaInst &) {}
234
235 void visitLoadInst(LoadInst &Inst) {
236 auto *Ptr = Inst.getPointerOperand();
237 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000238 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000239 }
240
241 void visitStoreInst(StoreInst &Inst) {
242 auto *Ptr = Inst.getPointerOperand();
243 auto *Val = Inst.getValueOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000244 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000245 }
246
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000247 void visitVAArgInst(VAArgInst &Inst) {
248 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
249 // two things:
250 // 1. Loads a value from *((T*)*Ptr).
251 // 2. Increments (stores to) *Ptr by some target-specific amount.
252 // For now, we'll handle this like a landingpad instruction (by placing the
253 // result in its own group, and having that group alias externals).
254 auto *Val = &Inst;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000255 Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrUnknown));
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000256 }
257
Hal Finkel7529c552014-09-02 21:43:13 +0000258 static bool isFunctionExternal(Function *Fn) {
259 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
260 }
261
George Burgess IVcae581d2016-04-13 23:27:37 +0000262 /// Gets whether the sets at Index1 above, below, or equal to the sets at
263 /// Index2. Returns None if they are not in the same set chain.
Hal Finkel7529c552014-09-02 21:43:13 +0000264 static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
265 StratifiedIndex Index1,
266 StratifiedIndex Index2) {
267 if (Index1 == Index2)
268 return Level::Same;
269
270 const auto *Current = &Sets.getLink(Index1);
271 while (Current->hasBelow()) {
272 if (Current->Below == Index2)
273 return Level::Below;
274 Current = &Sets.getLink(Current->Below);
275 }
276
277 Current = &Sets.getLink(Index1);
278 while (Current->hasAbove()) {
279 if (Current->Above == Index2)
280 return Level::Above;
281 Current = &Sets.getLink(Current->Above);
282 }
283
George Burgess IV77351ba32016-01-28 00:54:01 +0000284 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000285 }
286
287 bool
288 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
289 Value *FuncValue,
290 const iterator_range<User::op_iterator> &Args) {
Hal Finkelca616ac2014-09-02 23:29:48 +0000291 const unsigned ExpectedMaxArgs = 8;
292 const unsigned MaxSupportedArgs = 50;
Hal Finkel7529c552014-09-02 21:43:13 +0000293 assert(Fns.size() > 0);
294
George Burgess IVcae581d2016-04-13 23:27:37 +0000295 // This algorithm is n^2, so an arbitrary upper-bound of 50 args was
296 // selected, so it doesn't take too long in insane cases.
George Burgess IVab03af22015-03-10 02:58:15 +0000297 if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000298 return false;
299
300 // Exit early if we'll fail anyway
301 for (auto *Fn : Fns) {
302 if (isFunctionExternal(Fn) || Fn->isVarArg())
303 return false;
304 auto &MaybeInfo = AA.ensureCached(Fn);
305 if (!MaybeInfo.hasValue())
306 return false;
307 }
308
309 SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
310 SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
311 for (auto *Fn : Fns) {
312 auto &Info = *AA.ensureCached(Fn);
313 auto &Sets = Info.Sets;
314 auto &RetVals = Info.ReturnedValues;
315
316 Parameters.clear();
317 for (auto &Param : Fn->args()) {
318 auto MaybeInfo = Sets.find(&Param);
319 // Did a new parameter somehow get added to the function/slip by?
320 if (!MaybeInfo.hasValue())
321 return false;
322 Parameters.push_back(*MaybeInfo);
323 }
324
325 // Adding an edge from argument -> return value for each parameter that
326 // may alias the return value
327 for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
328 auto &ParamInfo = Parameters[I];
329 auto &ArgVal = Arguments[I];
330 bool AddEdge = false;
331 StratifiedAttrs Externals;
332 for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
333 auto MaybeInfo = Sets.find(RetVals[X]);
334 if (!MaybeInfo.hasValue())
335 return false;
336
337 auto &RetInfo = *MaybeInfo;
338 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
339 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
340 auto MaybeRelation =
341 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
342 if (MaybeRelation.hasValue()) {
343 AddEdge = true;
344 Externals |= RetAttrs | ParamAttrs;
345 }
346 }
347 if (AddEdge)
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000348 Output.push_back(
349 Edge(FuncValue, ArgVal, EdgeType::Assign, Externals));
Hal Finkel7529c552014-09-02 21:43:13 +0000350 }
351
352 if (Parameters.size() != Arguments.size())
353 return false;
354
George Burgess IVcae581d2016-04-13 23:27:37 +0000355 /// Adding edges between arguments for arguments that may end up aliasing
356 /// each other. This is necessary for functions such as
357 /// void foo(int** a, int** b) { *a = *b; }
358 /// (Technically, the proper sets for this would be those below
359 /// Arguments[I] and Arguments[X], but our algorithm will produce
360 /// extremely similar, and equally correct, results either way)
Hal Finkel7529c552014-09-02 21:43:13 +0000361 for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
362 auto &MainVal = Arguments[I];
363 auto &MainInfo = Parameters[I];
364 auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
365 for (unsigned X = I + 1; X != E; ++X) {
366 auto &SubInfo = Parameters[X];
367 auto &SubVal = Arguments[X];
368 auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
369 auto MaybeRelation =
370 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
371
372 if (!MaybeRelation.hasValue())
373 continue;
374
375 auto NewAttrs = SubAttrs | MainAttrs;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000376 Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
Hal Finkel7529c552014-09-02 21:43:13 +0000377 }
378 }
379 }
380 return true;
381 }
382
383 template <typename InstT> void visitCallLikeInst(InstT &Inst) {
George Burgess IV18b83fe2016-06-01 18:39:54 +0000384 // Check if Inst is a call to a library function that allocates/deallocates
385 // on the heap. Those kinds of functions do not introduce any aliases.
386 // TODO: address other common library functions such as realloc(), strdup(),
387 // etc.
388 if (isMallocLikeFn(&Inst, &TLI) || isCallocLikeFn(&Inst, &TLI)) {
389 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrNone));
390 return;
391 } else if (isFreeCall(&Inst, &TLI)) {
392 assert(Inst.getNumArgOperands() == 1);
393 auto argVal = Inst.arg_begin()->get();
394 Output.push_back(Edge(argVal, argVal, EdgeType::Assign, AttrNone));
395 return;
396 }
397
George Burgess IV68b36e02015-08-28 00:16:18 +0000398 // TODO: Add support for noalias args/all the other fun function attributes
399 // that we can tack on.
Hal Finkel7529c552014-09-02 21:43:13 +0000400 SmallVector<Function *, 4> Targets;
401 if (getPossibleTargets(&Inst, Targets)) {
402 if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
403 return;
404 // Cleanup from interprocedural analysis
405 Output.clear();
406 }
407
George Burgess IV68b36e02015-08-28 00:16:18 +0000408 // Because the function is opaque, we need to note that anything
409 // could have happened to the arguments, and that the result could alias
410 // just about anything, too.
411 // The goal of the loop is in part to unify many Values into one set, so we
412 // don't care if the function is void there.
Hal Finkel7529c552014-09-02 21:43:13 +0000413 for (Value *V : Inst.arg_operands())
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000414 Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrUnknown));
George Burgess IV68b36e02015-08-28 00:16:18 +0000415 if (Inst.getNumArgOperands() == 0 &&
416 Inst.getType() != Type::getVoidTy(Inst.getContext()))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000417 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrUnknown));
Hal Finkel7529c552014-09-02 21:43:13 +0000418 }
419
420 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
421
422 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
423
George Burgess IVcae581d2016-04-13 23:27:37 +0000424 /// Because vectors/aggregates are immutable and unaddressable, there's
425 /// nothing we can do to coax a value out of them, other than calling
426 /// Extract{Element,Value}. We can effectively treat them as pointers to
427 /// arbitrary memory locations we can store in and load from.
Hal Finkel7529c552014-09-02 21:43:13 +0000428 void visitExtractElementInst(ExtractElementInst &Inst) {
429 auto *Ptr = Inst.getVectorOperand();
430 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000431 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000432 }
433
434 void visitInsertElementInst(InsertElementInst &Inst) {
435 auto *Vec = Inst.getOperand(0);
436 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000437 Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
438 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000439 }
440
441 void visitLandingPadInst(LandingPadInst &Inst) {
442 // Exceptions come from "nowhere", from our analysis' perspective.
443 // So we place the instruction its own group, noting that said group may
444 // alias externals
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000445 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrUnknown));
Hal Finkel7529c552014-09-02 21:43:13 +0000446 }
447
448 void visitInsertValueInst(InsertValueInst &Inst) {
449 auto *Agg = Inst.getOperand(0);
450 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000451 Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
452 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000453 }
454
455 void visitExtractValueInst(ExtractValueInst &Inst) {
456 auto *Ptr = Inst.getAggregateOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000457 Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000458 }
459
460 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
461 auto *From1 = Inst.getOperand(0);
462 auto *From2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000463 Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
464 Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000465 }
Pete Cooper36642532015-06-12 16:13:54 +0000466
467 void visitConstantExpr(ConstantExpr *CE) {
468 switch (CE->getOpcode()) {
469 default:
470 llvm_unreachable("Unknown instruction type encountered!");
471// Build the switch statement using the Instruction.def file.
472#define HANDLE_INST(NUM, OPCODE, CLASS) \
473 case Instruction::OPCODE: \
474 visit##OPCODE(*(CLASS *)CE); \
475 break;
476#include "llvm/IR/Instruction.def"
477 }
478 }
Hal Finkel7529c552014-09-02 21:43:13 +0000479};
480
George Burgess IVcae581d2016-04-13 23:27:37 +0000481/// For a given instruction, we need to know which Value* to get the
482/// users of in order to build our graph. In some cases (i.e. add),
483/// we simply need the Instruction*. In other cases (i.e. store),
484/// finding the users of the Instruction* is useless; we need to find
485/// the users of the first operand. This handles determining which
486/// value to follow for us.
487///
488/// Note: we *need* to keep this in sync with GetEdgesVisitor. Add
489/// something to GetEdgesVisitor, add it here -- remove something from
490/// GetEdgesVisitor, remove it here.
Hal Finkel7529c552014-09-02 21:43:13 +0000491class GetTargetValueVisitor
492 : public InstVisitor<GetTargetValueVisitor, Value *> {
493public:
494 Value *visitInstruction(Instruction &Inst) { return &Inst; }
495
496 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
497
498 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
499 return Inst.getPointerOperand();
500 }
501
502 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
503 return Inst.getPointerOperand();
504 }
505
506 Value *visitInsertElementInst(InsertElementInst &Inst) {
507 return Inst.getOperand(0);
508 }
509
510 Value *visitInsertValueInst(InsertValueInst &Inst) {
511 return Inst.getAggregateOperand();
512 }
513};
514
George Burgess IVcae581d2016-04-13 23:27:37 +0000515/// Set building requires a weighted bidirectional graph.
Hal Finkel7529c552014-09-02 21:43:13 +0000516template <typename EdgeTypeT> class WeightedBidirectionalGraph {
517public:
518 typedef std::size_t Node;
519
520private:
Hal Finkelca616ac2014-09-02 23:29:48 +0000521 const static Node StartNode = Node(0);
Hal Finkel7529c552014-09-02 21:43:13 +0000522
523 struct Edge {
524 EdgeTypeT Weight;
525 Node Other;
526
George Burgess IV11d509d2015-03-15 00:52:21 +0000527 Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {}
Hal Finkelca616ac2014-09-02 23:29:48 +0000528
Hal Finkel7529c552014-09-02 21:43:13 +0000529 bool operator==(const Edge &E) const {
530 return Weight == E.Weight && Other == E.Other;
531 }
532
533 bool operator!=(const Edge &E) const { return !operator==(E); }
534 };
535
536 struct NodeImpl {
537 std::vector<Edge> Edges;
538 };
539
540 std::vector<NodeImpl> NodeImpls;
541
542 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
543
544 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
545 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
546
547public:
George Burgess IVcae581d2016-04-13 23:27:37 +0000548 /// \brief Iterator for edges. Because this graph is bidirected, we don't
549 /// allow modification of the edges using this iterator. Additionally, the
550 /// iterator becomes invalid if you add edges to or from the node you're
551 /// getting the edges of.
Hal Finkel7529c552014-09-02 21:43:13 +0000552 struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
553 std::tuple<EdgeTypeT, Node *>> {
554 EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
555 : Current(Iter) {}
556
557 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
558
559 EdgeIterator &operator++() {
560 ++Current;
561 return *this;
562 }
563
564 EdgeIterator operator++(int) {
565 EdgeIterator Copy(Current);
566 operator++();
567 return Copy;
568 }
569
570 std::tuple<EdgeTypeT, Node> &operator*() {
571 Store = std::make_tuple(Current->Weight, Current->Other);
572 return Store;
573 }
574
575 bool operator==(const EdgeIterator &Other) const {
576 return Current == Other.Current;
577 }
578
579 bool operator!=(const EdgeIterator &Other) const {
580 return !operator==(Other);
581 }
582
583 private:
584 typename std::vector<Edge>::const_iterator Current;
585 std::tuple<EdgeTypeT, Node> Store;
586 };
587
George Burgess IVcae581d2016-04-13 23:27:37 +0000588 /// Wrapper for EdgeIterator with begin()/end() calls.
Hal Finkel7529c552014-09-02 21:43:13 +0000589 struct EdgeIterable {
590 EdgeIterable(const std::vector<Edge> &Edges)
591 : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
592
593 EdgeIterator begin() { return EdgeIterator(BeginIter); }
594
595 EdgeIterator end() { return EdgeIterator(EndIter); }
596
597 private:
598 typename std::vector<Edge>::const_iterator BeginIter;
599 typename std::vector<Edge>::const_iterator EndIter;
600 };
601
602 // ----- Actual graph-related things ----- //
603
Hal Finkelca616ac2014-09-02 23:29:48 +0000604 WeightedBidirectionalGraph() {}
Hal Finkel7529c552014-09-02 21:43:13 +0000605
606 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
607 : NodeImpls(std::move(Other.NodeImpls)) {}
608
609 WeightedBidirectionalGraph<EdgeTypeT> &
610 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
611 NodeImpls = std::move(Other.NodeImpls);
612 return *this;
613 }
614
615 Node addNode() {
616 auto Index = NodeImpls.size();
617 auto NewNode = Node(Index);
618 NodeImpls.push_back(NodeImpl());
619 return NewNode;
620 }
621
622 void addEdge(Node From, Node To, const EdgeTypeT &Weight,
623 const EdgeTypeT &ReverseWeight) {
624 assert(inbounds(From));
625 assert(inbounds(To));
626 auto &FromNode = getNode(From);
627 auto &ToNode = getNode(To);
Hal Finkelca616ac2014-09-02 23:29:48 +0000628 FromNode.Edges.push_back(Edge(Weight, To));
629 ToNode.Edges.push_back(Edge(ReverseWeight, From));
Hal Finkel7529c552014-09-02 21:43:13 +0000630 }
631
George Burgess IVcae581d2016-04-13 23:27:37 +0000632 iterator_range<EdgeIterator> edgesFor(const Node &N) const {
Hal Finkel7529c552014-09-02 21:43:13 +0000633 const auto &Node = getNode(N);
George Burgess IVcae581d2016-04-13 23:27:37 +0000634 return make_range(EdgeIterator(Node.Edges.begin()),
635 EdgeIterator(Node.Edges.end()));
Hal Finkel7529c552014-09-02 21:43:13 +0000636 }
637
638 bool empty() const { return NodeImpls.empty(); }
639 std::size_t size() const { return NodeImpls.size(); }
640
George Burgess IVcae581d2016-04-13 23:27:37 +0000641 /// Gets an arbitrary node in the graph as a starting point for traversal.
Hal Finkel7529c552014-09-02 21:43:13 +0000642 Node getEntryNode() {
643 assert(inbounds(StartNode));
644 return StartNode;
645 }
646};
647
648typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
649typedef DenseMap<Value *, GraphT::Node> NodeMapT;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000650}
Hal Finkel7529c552014-09-02 21:43:13 +0000651
Hal Finkel7529c552014-09-02 21:43:13 +0000652//===----------------------------------------------------------------------===//
653// Function declarations that require types defined in the namespace above
654//===----------------------------------------------------------------------===//
655
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000656/// Given a StratifiedAttrs, returns true if it marks the corresponding values
657/// as globals or arguments
658static bool isGlobalOrArgAttr(StratifiedAttrs Attr);
Hal Finkel7529c552014-09-02 21:43:13 +0000659
George Burgess IV652ec4f2016-06-09 23:15:04 +0000660/// Given a StratifiedAttrs, returns true if the corresponding values come from
661/// an unknown source (such as opaque memory or an integer cast)
662static bool isUnknownAttr(StratifiedAttrs Attr);
663
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000664/// Given an argument number, returns the appropriate StratifiedAttr to set.
665static StratifiedAttr argNumberToAttr(unsigned ArgNum);
666
667/// Given a Value, potentially return which StratifiedAttr it maps to.
668static Optional<StratifiedAttr> valueToAttr(Value *Val);
Hal Finkel7529c552014-09-02 21:43:13 +0000669
George Burgess IVcae581d2016-04-13 23:27:37 +0000670/// Gets the inverse of a given EdgeType.
671static EdgeType flipWeight(EdgeType Initial);
Hal Finkel7529c552014-09-02 21:43:13 +0000672
George Burgess IVcae581d2016-04-13 23:27:37 +0000673/// Gets edges of the given Instruction*, writing them to the SmallVector*.
George Burgess IV18b83fe2016-06-01 18:39:54 +0000674static void argsToEdges(CFLAAResult &, Instruction *, SmallVectorImpl<Edge> &,
675 const TargetLibraryInfo &);
Hal Finkel7529c552014-09-02 21:43:13 +0000676
George Burgess IVcae581d2016-04-13 23:27:37 +0000677/// Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
George Burgess IV18b83fe2016-06-01 18:39:54 +0000678static void argsToEdges(CFLAAResult &, ConstantExpr *, SmallVectorImpl<Edge> &,
679 const TargetLibraryInfo &);
Pete Cooper36642532015-06-12 16:13:54 +0000680
George Burgess IVcae581d2016-04-13 23:27:37 +0000681/// Gets the "Level" that one should travel in StratifiedSets
682/// given an EdgeType.
Hal Finkel7529c552014-09-02 21:43:13 +0000683static Level directionOfEdgeType(EdgeType);
684
George Burgess IVcae581d2016-04-13 23:27:37 +0000685/// Builds the graph needed for constructing the StratifiedSets for the
686/// given function
Chandler Carruth7b560d42015-09-09 17:55:00 +0000687static void buildGraphFrom(CFLAAResult &, Function *,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000688 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &,
689 const TargetLibraryInfo &);
Hal Finkel7529c552014-09-02 21:43:13 +0000690
George Burgess IVcae581d2016-04-13 23:27:37 +0000691/// Gets the edges of a ConstantExpr as if it was an Instruction. This function
692/// also acts on any nested ConstantExprs, adding the edges of those to the
693/// given SmallVector as well.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000694static void constexprToEdges(CFLAAResult &, ConstantExpr &,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000695 SmallVectorImpl<Edge> &,
696 const TargetLibraryInfo &);
George Burgess IVab03af22015-03-10 02:58:15 +0000697
George Burgess IVcae581d2016-04-13 23:27:37 +0000698/// Given an Instruction, this will add it to the graph, along with any
699/// Instructions that are potentially only available from said Instruction
700/// For example, given the following line:
701/// %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
702/// addInstructionToGraph would add both the `load` and `getelementptr`
703/// instructions to the graph appropriately.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000704static void addInstructionToGraph(CFLAAResult &, Instruction &,
George Burgess IVab03af22015-03-10 02:58:15 +0000705 SmallVectorImpl<Value *> &, NodeMapT &,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000706 GraphT &, const TargetLibraryInfo &);
George Burgess IVab03af22015-03-10 02:58:15 +0000707
George Burgess IVcae581d2016-04-13 23:27:37 +0000708/// Determines whether it would be pointless to add the given Value to our sets.
George Burgess IVab03af22015-03-10 02:58:15 +0000709static bool canSkipAddingToSets(Value *Val);
710
Hal Finkel7529c552014-09-02 21:43:13 +0000711static Optional<Function *> parentFunctionOfValue(Value *Val) {
712 if (auto *Inst = dyn_cast<Instruction>(Val)) {
713 auto *Bb = Inst->getParent();
714 return Bb->getParent();
715 }
716
717 if (auto *Arg = dyn_cast<Argument>(Val))
718 return Arg->getParent();
George Burgess IV77351ba32016-01-28 00:54:01 +0000719 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000720}
721
722template <typename Inst>
723static bool getPossibleTargets(Inst *Call,
724 SmallVectorImpl<Function *> &Output) {
725 if (auto *Fn = Call->getCalledFunction()) {
726 Output.push_back(Fn);
727 return true;
728 }
729
730 // TODO: If the call is indirect, we might be able to enumerate all potential
731 // targets of the call and return them, rather than just failing.
732 return false;
733}
734
735static Optional<Value *> getTargetValue(Instruction *Inst) {
736 GetTargetValueVisitor V;
737 return V.visit(Inst);
738}
739
740static bool hasUsefulEdges(Instruction *Inst) {
741 bool IsNonInvokeTerminator =
742 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
743 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
744}
745
Pete Cooper36642532015-06-12 16:13:54 +0000746static bool hasUsefulEdges(ConstantExpr *CE) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000747 // ConstantExpr doesn't have terminators, invokes, or fences, so only needs
Pete Cooper36642532015-06-12 16:13:54 +0000748 // to check for compares.
749 return CE->getOpcode() != Instruction::ICmp &&
750 CE->getOpcode() != Instruction::FCmp;
751}
752
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000753static bool isGlobalOrArgAttr(StratifiedAttrs Attr) {
754 return Attr.reset(AttrEscapedIndex).reset(AttrUnknownIndex).any();
755}
756
George Burgess IV652ec4f2016-06-09 23:15:04 +0000757static bool isUnknownAttr(StratifiedAttrs Attr) {
758 return Attr.test(AttrUnknownIndex);
759}
760
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000761static Optional<StratifiedAttr> valueToAttr(Value *Val) {
Hal Finkel7529c552014-09-02 21:43:13 +0000762 if (isa<GlobalValue>(Val))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000763 return AttrGlobal;
Hal Finkel7529c552014-09-02 21:43:13 +0000764
765 if (auto *Arg = dyn_cast<Argument>(Val))
Daniel Berlin16f7a522015-01-26 17:31:17 +0000766 // Only pointer arguments should have the argument attribute,
767 // because things can't escape through scalars without us seeing a
768 // cast, and thus, interaction with them doesn't matter.
769 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000770 return argNumberToAttr(Arg->getArgNo());
George Burgess IV77351ba32016-01-28 00:54:01 +0000771 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000772}
773
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000774static StratifiedAttr argNumberToAttr(unsigned ArgNum) {
George Burgess IV3c898c22015-01-21 16:37:21 +0000775 if (ArgNum >= AttrMaxNumArgs)
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000776 return AttrUnknown;
777 return 1 << (ArgNum + AttrFirstArgIndex);
Hal Finkel7529c552014-09-02 21:43:13 +0000778}
779
780static EdgeType flipWeight(EdgeType Initial) {
781 switch (Initial) {
782 case EdgeType::Assign:
783 return EdgeType::Assign;
784 case EdgeType::Dereference:
785 return EdgeType::Reference;
786 case EdgeType::Reference:
787 return EdgeType::Dereference;
788 }
789 llvm_unreachable("Incomplete coverage of EdgeType enum");
790}
791
Chandler Carruth7b560d42015-09-09 17:55:00 +0000792static void argsToEdges(CFLAAResult &Analysis, Instruction *Inst,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000793 SmallVectorImpl<Edge> &Output,
794 const TargetLibraryInfo &TLI) {
George Burgess IVab03af22015-03-10 02:58:15 +0000795 assert(hasUsefulEdges(Inst) &&
796 "Expected instructions to have 'useful' edges");
George Burgess IV18b83fe2016-06-01 18:39:54 +0000797 GetEdgesVisitor v(Analysis, Output, TLI);
Hal Finkel7529c552014-09-02 21:43:13 +0000798 v.visit(Inst);
799}
800
Chandler Carruth7b560d42015-09-09 17:55:00 +0000801static void argsToEdges(CFLAAResult &Analysis, ConstantExpr *CE,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000802 SmallVectorImpl<Edge> &Output,
803 const TargetLibraryInfo &TLI) {
Pete Cooper36642532015-06-12 16:13:54 +0000804 assert(hasUsefulEdges(CE) && "Expected constant expr to have 'useful' edges");
George Burgess IV18b83fe2016-06-01 18:39:54 +0000805 GetEdgesVisitor v(Analysis, Output, TLI);
Pete Cooper36642532015-06-12 16:13:54 +0000806 v.visitConstantExpr(CE);
807}
808
Hal Finkel7529c552014-09-02 21:43:13 +0000809static Level directionOfEdgeType(EdgeType Weight) {
810 switch (Weight) {
811 case EdgeType::Reference:
812 return Level::Above;
813 case EdgeType::Dereference:
814 return Level::Below;
815 case EdgeType::Assign:
816 return Level::Same;
817 }
818 llvm_unreachable("Incomplete switch coverage");
819}
820
Chandler Carruth7b560d42015-09-09 17:55:00 +0000821static void constexprToEdges(CFLAAResult &Analysis,
George Burgess IVab03af22015-03-10 02:58:15 +0000822 ConstantExpr &CExprToCollapse,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000823 SmallVectorImpl<Edge> &Results,
824 const TargetLibraryInfo &TLI) {
George Burgess IVab03af22015-03-10 02:58:15 +0000825 SmallVector<ConstantExpr *, 4> Worklist;
826 Worklist.push_back(&CExprToCollapse);
827
828 SmallVector<Edge, 8> ConstexprEdges;
Pete Cooper36642532015-06-12 16:13:54 +0000829 SmallPtrSet<ConstantExpr *, 4> Visited;
George Burgess IVab03af22015-03-10 02:58:15 +0000830 while (!Worklist.empty()) {
831 auto *CExpr = Worklist.pop_back_val();
George Burgess IVab03af22015-03-10 02:58:15 +0000832
Pete Cooper36642532015-06-12 16:13:54 +0000833 if (!hasUsefulEdges(CExpr))
George Burgess IVab03af22015-03-10 02:58:15 +0000834 continue;
835
836 ConstexprEdges.clear();
George Burgess IV18b83fe2016-06-01 18:39:54 +0000837 argsToEdges(Analysis, CExpr, ConstexprEdges, TLI);
George Burgess IVab03af22015-03-10 02:58:15 +0000838 for (auto &Edge : ConstexprEdges) {
Pete Cooper36642532015-06-12 16:13:54 +0000839 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
840 if (Visited.insert(Nested).second)
841 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000842
Pete Cooper36642532015-06-12 16:13:54 +0000843 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
844 if (Visited.insert(Nested).second)
845 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000846 }
847
848 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
849 }
850}
851
Chandler Carruth7b560d42015-09-09 17:55:00 +0000852static void addInstructionToGraph(CFLAAResult &Analysis, Instruction &Inst,
George Burgess IVab03af22015-03-10 02:58:15 +0000853 SmallVectorImpl<Value *> &ReturnedValues,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000854 NodeMapT &Map, GraphT &Graph,
855 const TargetLibraryInfo &TLI) {
Hal Finkel7529c552014-09-02 21:43:13 +0000856 const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
857 auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
858 auto &Iter = Pair.first;
859 if (Pair.second) {
860 auto NewNode = Graph.addNode();
861 Iter->second = NewNode;
862 }
863 return Iter->second;
864 };
865
George Burgess IVab03af22015-03-10 02:58:15 +0000866 // We don't want the edges of most "return" instructions, but we *do* want
867 // to know what can be returned.
868 if (isa<ReturnInst>(&Inst))
869 ReturnedValues.push_back(&Inst);
870
871 if (!hasUsefulEdges(&Inst))
872 return;
873
Hal Finkel7529c552014-09-02 21:43:13 +0000874 SmallVector<Edge, 8> Edges;
George Burgess IV18b83fe2016-06-01 18:39:54 +0000875 argsToEdges(Analysis, &Inst, Edges, TLI);
Hal Finkel7529c552014-09-02 21:43:13 +0000876
George Burgess IVab03af22015-03-10 02:58:15 +0000877 // In the case of an unused alloca (or similar), edges may be empty. Note
878 // that it exists so we can potentially answer NoAlias.
879 if (Edges.empty()) {
880 auto MaybeVal = getTargetValue(&Inst);
881 assert(MaybeVal.hasValue());
882 auto *Target = *MaybeVal;
883 findOrInsertNode(Target);
884 return;
Hal Finkel7529c552014-09-02 21:43:13 +0000885 }
George Burgess IVab03af22015-03-10 02:58:15 +0000886
George Burgess IVcae581d2016-04-13 23:27:37 +0000887 auto addEdgeToGraph = [&](const Edge &E) {
George Burgess IVab03af22015-03-10 02:58:15 +0000888 auto To = findOrInsertNode(E.To);
889 auto From = findOrInsertNode(E.From);
890 auto FlippedWeight = flipWeight(E.Weight);
891 auto Attrs = E.AdditionalAttrs;
892 Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
893 std::make_pair(FlippedWeight, Attrs));
894 };
895
896 SmallVector<ConstantExpr *, 4> ConstantExprs;
897 for (const Edge &E : Edges) {
898 addEdgeToGraph(E);
899 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
900 ConstantExprs.push_back(Constexpr);
901 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
902 ConstantExprs.push_back(Constexpr);
903 }
904
905 for (ConstantExpr *CE : ConstantExprs) {
906 Edges.clear();
George Burgess IV18b83fe2016-06-01 18:39:54 +0000907 constexprToEdges(Analysis, *CE, Edges, TLI);
George Burgess IVab03af22015-03-10 02:58:15 +0000908 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
909 }
910}
911
Chandler Carruth7b560d42015-09-09 17:55:00 +0000912static void buildGraphFrom(CFLAAResult &Analysis, Function *Fn,
George Burgess IVab03af22015-03-10 02:58:15 +0000913 SmallVectorImpl<Value *> &ReturnedValues,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000914 NodeMapT &Map, GraphT &Graph,
915 const TargetLibraryInfo &TLI) {
George Burgess IVcae581d2016-04-13 23:27:37 +0000916 // (N.B. We may remove graph construction entirely, because it doesn't really
917 // buy us much.)
George Burgess IVab03af22015-03-10 02:58:15 +0000918 for (auto &Bb : Fn->getBasicBlockList())
919 for (auto &Inst : Bb.getInstList())
George Burgess IV18b83fe2016-06-01 18:39:54 +0000920 addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph, TLI);
George Burgess IVab03af22015-03-10 02:58:15 +0000921}
922
923static bool canSkipAddingToSets(Value *Val) {
924 // Constants can share instances, which may falsely unify multiple
925 // sets, e.g. in
926 // store i32* null, i32** %ptr1
927 // store i32* null, i32** %ptr2
928 // clearly ptr1 and ptr2 should not be unified into the same set, so
929 // we should filter out the (potentially shared) instance to
930 // i32* null.
931 if (isa<Constant>(Val)) {
George Burgess IVab03af22015-03-10 02:58:15 +0000932 // TODO: Because all of these things are constant, we can determine whether
933 // the data is *actually* mutable at graph building time. This will probably
934 // come for free/cheap with offset awareness.
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000935 bool CanStoreMutableData = isa<GlobalValue>(Val) ||
936 isa<ConstantExpr>(Val) ||
937 isa<ConstantAggregate>(Val);
George Burgess IVab03af22015-03-10 02:58:15 +0000938 return !CanStoreMutableData;
939 }
940
941 return false;
Hal Finkel7529c552014-09-02 21:43:13 +0000942}
943
Chandler Carruth8b046a42015-08-14 02:42:20 +0000944// Builds the graph + StratifiedSets for a function.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000945CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) {
Hal Finkel7529c552014-09-02 21:43:13 +0000946 NodeMapT Map;
947 GraphT Graph;
948 SmallVector<Value *, 4> ReturnedValues;
949
George Burgess IV18b83fe2016-06-01 18:39:54 +0000950 buildGraphFrom(*this, Fn, ReturnedValues, Map, Graph, TLI);
Hal Finkel7529c552014-09-02 21:43:13 +0000951
952 DenseMap<GraphT::Node, Value *> NodeValueMap;
Mehdi Aminic04fc7a2016-03-22 07:20:00 +0000953 NodeValueMap.reserve(Map.size());
Hal Finkel7529c552014-09-02 21:43:13 +0000954 for (const auto &Pair : Map)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000955 NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
Hal Finkel7529c552014-09-02 21:43:13 +0000956
957 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
958 auto ValIter = NodeValueMap.find(Node);
959 assert(ValIter != NodeValueMap.end());
960 return ValIter->second;
961 };
962
963 StratifiedSetsBuilder<Value *> Builder;
964
965 SmallVector<GraphT::Node, 16> Worklist;
George Burgess IV652ec4f2016-06-09 23:15:04 +0000966 SmallPtrSet<Value *, 16> Globals;
Hal Finkel7529c552014-09-02 21:43:13 +0000967 for (auto &Pair : Map) {
968 Worklist.clear();
969
970 auto *Value = Pair.first;
971 Builder.add(Value);
972 auto InitialNode = Pair.second;
973 Worklist.push_back(InitialNode);
974 while (!Worklist.empty()) {
975 auto Node = Worklist.pop_back_val();
976 auto *CurValue = findValueOrDie(Node);
George Burgess IVab03af22015-03-10 02:58:15 +0000977 if (canSkipAddingToSets(CurValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000978 continue;
979
George Burgess IV652ec4f2016-06-09 23:15:04 +0000980 if (isa<GlobalValue>(CurValue))
981 Globals.insert(CurValue);
George Burgess IV7e5404c2016-04-05 21:40:45 +0000982
Hal Finkel7529c552014-09-02 21:43:13 +0000983 for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
984 auto Weight = std::get<0>(EdgeTuple);
985 auto Label = Weight.first;
986 auto &OtherNode = std::get<1>(EdgeTuple);
987 auto *OtherValue = findValueOrDie(OtherNode);
988
George Burgess IVab03af22015-03-10 02:58:15 +0000989 if (canSkipAddingToSets(OtherValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000990 continue;
George Burgess IV652ec4f2016-06-09 23:15:04 +0000991 if (isa<GlobalValue>(OtherValue))
992 Globals.insert(OtherValue);
Hal Finkel7529c552014-09-02 21:43:13 +0000993
994 bool Added;
995 switch (directionOfEdgeType(Label)) {
996 case Level::Above:
997 Added = Builder.addAbove(CurValue, OtherValue);
998 break;
999 case Level::Below:
1000 Added = Builder.addBelow(CurValue, OtherValue);
1001 break;
1002 case Level::Same:
1003 Added = Builder.addWith(CurValue, OtherValue);
1004 break;
1005 }
1006
George Burgess IVb54a8d622015-03-10 02:40:06 +00001007 auto Aliasing = Weight.second;
George Burgess IVb54a8d622015-03-10 02:40:06 +00001008 Builder.noteAttributes(CurValue, Aliasing);
1009 Builder.noteAttributes(OtherValue, Aliasing);
1010
1011 if (Added)
Hal Finkel7529c552014-09-02 21:43:13 +00001012 Worklist.push_back(OtherNode);
Hal Finkel7529c552014-09-02 21:43:13 +00001013 }
1014 }
1015 }
1016
George Burgess IV652ec4f2016-06-09 23:15:04 +00001017 // Special handling for globals and arguments
1018 auto ProcessGlobalOrArgValue = [&Builder](Value &Val) {
1019 Builder.add(&Val);
1020 auto Attr = valueToAttr(&Val);
1021 if (Attr.hasValue()) {
1022 Builder.noteAttributes(&Val, *Attr);
1023 // TODO: do we need to filter out non-pointer values here?
1024 Builder.addAttributesBelow(&Val, AttrUnknown);
1025 }
1026 };
George Burgess IVab03af22015-03-10 02:58:15 +00001027
George Burgess IV652ec4f2016-06-09 23:15:04 +00001028 for (auto &Arg : Fn->args())
1029 ProcessGlobalOrArgValue(Arg);
1030 for (auto *Global : Globals)
1031 ProcessGlobalOrArgValue(*Global);
Hal Finkel7529c552014-09-02 21:43:13 +00001032
Hal Finkel85f26922014-09-03 00:06:47 +00001033 return FunctionInfo(Builder.build(), std::move(ReturnedValues));
Hal Finkel7529c552014-09-02 21:43:13 +00001034}
1035
Chandler Carruth7b560d42015-09-09 17:55:00 +00001036void CFLAAResult::scan(Function *Fn) {
Hal Finkel8d1590d2014-09-02 22:52:30 +00001037 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
Hal Finkel7529c552014-09-02 21:43:13 +00001038 (void)InsertPair;
1039 assert(InsertPair.second &&
1040 "Trying to scan a function that has already been cached");
1041
George Burgess IV6edb8912016-05-02 18:09:19 +00001042 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
1043 // may get evaluated after operator[], potentially triggering a DenseMap
1044 // resize and invalidating the reference returned by operator[]
1045 auto FunInfo = buildSetsFrom(Fn);
1046 Cache[Fn] = std::move(FunInfo);
1047
Hal Finkel7529c552014-09-02 21:43:13 +00001048 Handles.push_front(FunctionHandle(Fn, this));
1049}
1050
Chandler Carruth7b560d42015-09-09 17:55:00 +00001051void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); }
Chandler Carruth8b046a42015-08-14 02:42:20 +00001052
George Burgess IVcae581d2016-04-13 23:27:37 +00001053/// Ensures that the given function is available in the cache, and returns the
1054/// entry.
Chandler Carruth7b560d42015-09-09 17:55:00 +00001055const Optional<CFLAAResult::FunctionInfo> &
1056CFLAAResult::ensureCached(Function *Fn) {
Chandler Carruth8b046a42015-08-14 02:42:20 +00001057 auto Iter = Cache.find(Fn);
1058 if (Iter == Cache.end()) {
1059 scan(Fn);
1060 Iter = Cache.find(Fn);
1061 assert(Iter != Cache.end());
1062 assert(Iter->second.hasValue());
1063 }
1064 return Iter->second;
1065}
1066
Chandler Carruth7b560d42015-09-09 17:55:00 +00001067AliasResult CFLAAResult::query(const MemoryLocation &LocA,
1068 const MemoryLocation &LocB) {
Hal Finkel7529c552014-09-02 21:43:13 +00001069 auto *ValA = const_cast<Value *>(LocA.Ptr);
1070 auto *ValB = const_cast<Value *>(LocB.Ptr);
1071
1072 Function *Fn = nullptr;
1073 auto MaybeFnA = parentFunctionOfValue(ValA);
1074 auto MaybeFnB = parentFunctionOfValue(ValB);
1075 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
George Burgess IVcae581d2016-04-13 23:27:37 +00001076 // The only times this is known to happen are when globals + InlineAsm are
1077 // involved
George Burgess IV33305e72015-02-12 03:07:07 +00001078 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001079 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001080 }
1081
1082 if (MaybeFnA.hasValue()) {
1083 Fn = *MaybeFnA;
1084 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1085 "Interprocedural queries not supported");
1086 } else {
1087 Fn = *MaybeFnB;
1088 }
1089
1090 assert(Fn != nullptr);
1091 auto &MaybeInfo = ensureCached(Fn);
1092 assert(MaybeInfo.hasValue());
1093
1094 auto &Sets = MaybeInfo->Sets;
1095 auto MaybeA = Sets.find(ValA);
1096 if (!MaybeA.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001097 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001098
1099 auto MaybeB = Sets.find(ValB);
1100 if (!MaybeB.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001101 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001102
1103 auto SetA = *MaybeA;
1104 auto SetB = *MaybeB;
Hal Finkel7529c552014-09-02 21:43:13 +00001105 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1106 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
George Burgess IV33305e72015-02-12 03:07:07 +00001107
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001108 // If both values are local (meaning the corresponding set has attribute
1109 // AttrNone or AttrEscaped), then we know that CFLAA fully models them: they
1110 // may-alias each other if and only if they are in the same set
1111 // If at least one value is non-local (meaning it either is global/argument or
1112 // it comes from unknown sources like integer cast), the situation becomes a
1113 // bit more interesting. We follow three general rules described below:
1114 // - Non-local values may alias each other
1115 // - AttrNone values do not alias any non-local values
George Burgess IV652ec4f2016-06-09 23:15:04 +00001116 // - AttrEscaped do not alias globals/arguments, but they may alias
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001117 // AttrUnknown values
1118 if (SetA.Index == SetB.Index)
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001119 return MayAlias;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001120 if (AttrsA.none() || AttrsB.none())
1121 return NoAlias;
George Burgess IV652ec4f2016-06-09 23:15:04 +00001122 if (isUnknownAttr(AttrsA) || isUnknownAttr(AttrsB))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001123 return MayAlias;
1124 if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB))
1125 return MayAlias;
1126 return NoAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001127}
Mehdi Amini46a43552015-03-04 18:43:29 +00001128
Chandler Carruthb4faf132016-03-11 10:22:49 +00001129char CFLAA::PassID;
1130
Chandler Carruthb47f8012016-03-11 11:05:24 +00001131CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV18b83fe2016-06-01 18:39:54 +00001132 return CFLAAResult(AM.getResult<TargetLibraryAnalysis>(F));
Chandler Carruth7b560d42015-09-09 17:55:00 +00001133}
1134
Chandler Carruth7b560d42015-09-09 17:55:00 +00001135char CFLAAWrapperPass::ID = 0;
Chandler Carruth12884f72016-03-02 15:56:53 +00001136INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false,
1137 true)
Chandler Carruth7b560d42015-09-09 17:55:00 +00001138
1139ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); }
1140
1141CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) {
1142 initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry());
1143}
1144
George Burgess IV18b83fe2016-06-01 18:39:54 +00001145void CFLAAWrapperPass::initializePass() {
1146 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
1147 Result.reset(new CFLAAResult(TLIWP.getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +00001148}
1149
1150void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1151 AU.setPreservesAll();
George Burgess IV18b83fe2016-06-01 18:39:54 +00001152 AU.addRequired<TargetLibraryInfoWrapperPass>();
Mehdi Amini46a43552015-03-04 18:43:29 +00001153}