blob: 90564037777aee1b8d3680c5cd5993be88101bc7 [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
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;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000100LLVM_CONSTEXPR unsigned AttrEscapedIndex = 0;
101LLVM_CONSTEXPR unsigned AttrUnknownIndex = 1;
102LLVM_CONSTEXPR unsigned AttrGlobalIndex = 2;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000103LLVM_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 IVa1f9a2d2016-06-07 18:35:37 +0000108LLVM_CONSTEXPR StratifiedAttr AttrEscaped = 1 << AttrEscapedIndex;
George Burgess IVb54a8d622015-03-10 02:40:06 +0000109LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000110LLVM_CONSTEXPR StratifiedAttr AttrGlobal = 1 << AttrGlobalIndex;
Hal Finkel7529c552014-09-02 21:43:13 +0000111
George Burgess IVcae581d2016-04-13 23:27:37 +0000112/// StratifiedSets call for knowledge of "direction", so this is how we
113/// represent that locally.
Hal Finkel7529c552014-09-02 21:43:13 +0000114enum class Level { Same, Above, Below };
115
George Burgess IVcae581d2016-04-13 23:27:37 +0000116/// Edges can be one of four "weights" -- each weight must have an inverse
117/// weight (Assign has Assign; Reference has Dereference).
Hal Finkel7529c552014-09-02 21:43:13 +0000118enum class EdgeType {
George Burgess IVcae581d2016-04-13 23:27:37 +0000119 /// The weight assigned when assigning from or to a value. For example, in:
120 /// %b = getelementptr %a, 0
121 /// ...The relationships are %b assign %a, and %a assign %b. This used to be
122 /// two edges, but having a distinction bought us nothing.
Hal Finkel7529c552014-09-02 21:43:13 +0000123 Assign,
124
George Burgess IVcae581d2016-04-13 23:27:37 +0000125 /// The edge used when we have an edge going from some handle to a Value.
126 /// Examples of this include:
127 /// %b = load %a (%b Dereference %a)
128 /// %b = extractelement %a, 0 (%a Dereference %b)
Hal Finkel7529c552014-09-02 21:43:13 +0000129 Dereference,
130
George Burgess IVcae581d2016-04-13 23:27:37 +0000131 /// The edge used when our edge goes from a value to a handle that may have
132 /// contained it at some point. Examples:
133 /// %b = load %a (%a Reference %b)
134 /// %b = extractelement %a, 0 (%b Reference %a)
Hal Finkel7529c552014-09-02 21:43:13 +0000135 Reference
136};
137
138// \brief Encodes the notion of a "use"
139struct Edge {
George Burgess IVcae581d2016-04-13 23:27:37 +0000140 /// Which value the edge is coming from
Hal Finkel7529c552014-09-02 21:43:13 +0000141 Value *From;
142
George Burgess IVcae581d2016-04-13 23:27:37 +0000143 /// Which value the edge is pointing to
Hal Finkel7529c552014-09-02 21:43:13 +0000144 Value *To;
145
George Burgess IVcae581d2016-04-13 23:27:37 +0000146 /// Edge weight
Hal Finkel7529c552014-09-02 21:43:13 +0000147 EdgeType Weight;
148
George Burgess IVcae581d2016-04-13 23:27:37 +0000149 /// Whether we aliased any external values along the way that may be
150 /// invisible to the analysis (i.e. landingpad for exceptions, calls for
151 /// interprocedural analysis, etc.)
Hal Finkel7529c552014-09-02 21:43:13 +0000152 StratifiedAttrs AdditionalAttrs;
153
154 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
155 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
156};
157
George Burgess IVcae581d2016-04-13 23:27:37 +0000158/// Gets the edges our graph should have, based on an Instruction*
Hal Finkel7529c552014-09-02 21:43:13 +0000159class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000160 CFLAAResult &AA;
Hal Finkel7529c552014-09-02 21:43:13 +0000161 SmallVectorImpl<Edge> &Output;
George Burgess IV18b83fe2016-06-01 18:39:54 +0000162 const TargetLibraryInfo &TLI;
Hal Finkel7529c552014-09-02 21:43:13 +0000163
164public:
George Burgess IV18b83fe2016-06-01 18:39:54 +0000165 GetEdgesVisitor(CFLAAResult &AA, SmallVectorImpl<Edge> &Output,
166 const TargetLibraryInfo &TLI)
167 : AA(AA), Output(Output), TLI(TLI) {}
Hal Finkel7529c552014-09-02 21:43:13 +0000168
169 void visitInstruction(Instruction &) {
170 llvm_unreachable("Unsupported instruction encountered");
171 }
172
George Burgess IVb54a8d622015-03-10 02:40:06 +0000173 void visitPtrToIntInst(PtrToIntInst &Inst) {
174 auto *Ptr = Inst.getOperand(0);
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000175 Output.push_back(Edge(Ptr, &Inst, EdgeType::Assign, AttrEscaped));
George Burgess IVb54a8d622015-03-10 02:40:06 +0000176 }
177
178 void visitIntToPtrInst(IntToPtrInst &Inst) {
179 auto *Ptr = &Inst;
180 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
181 }
182
Hal Finkel7529c552014-09-02 21:43:13 +0000183 void visitCastInst(CastInst &Inst) {
George Burgess IV11d509d2015-03-15 00:52:21 +0000184 Output.push_back(
185 Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000186 }
187
188 void visitBinaryOperator(BinaryOperator &Inst) {
189 auto *Op1 = Inst.getOperand(0);
190 auto *Op2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000191 Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone));
192 Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000193 }
194
195 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
196 auto *Ptr = Inst.getPointerOperand();
197 auto *Val = Inst.getNewValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000198 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000199 }
200
201 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
202 auto *Ptr = Inst.getPointerOperand();
203 auto *Val = Inst.getValOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000204 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000205 }
206
207 void visitPHINode(PHINode &Inst) {
George Burgess IV77351ba32016-01-28 00:54:01 +0000208 for (Value *Val : Inst.incoming_values())
Hal Finkel8d1590d2014-09-02 22:52:30 +0000209 Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000210 }
211
212 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
213 auto *Op = Inst.getPointerOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000214 Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000215 }
216
217 void visitSelectInst(SelectInst &Inst) {
Daniel Berlin16f7a522015-01-26 17:31:17 +0000218 // Condition is not processed here (The actual statement producing
219 // the condition result is processed elsewhere). For select, the
220 // condition is evaluated, but not loaded, stored, or assigned
221 // simply as a result of being the condition of a select.
222
Hal Finkel7529c552014-09-02 21:43:13 +0000223 auto *TrueVal = Inst.getTrueValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000224 Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000225 auto *FalseVal = Inst.getFalseValue();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000226 Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000227 }
228
229 void visitAllocaInst(AllocaInst &) {}
230
231 void visitLoadInst(LoadInst &Inst) {
232 auto *Ptr = Inst.getPointerOperand();
233 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000234 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000235 }
236
237 void visitStoreInst(StoreInst &Inst) {
238 auto *Ptr = Inst.getPointerOperand();
239 auto *Val = Inst.getValueOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000240 Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000241 }
242
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000243 void visitVAArgInst(VAArgInst &Inst) {
244 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
245 // two things:
246 // 1. Loads a value from *((T*)*Ptr).
247 // 2. Increments (stores to) *Ptr by some target-specific amount.
248 // For now, we'll handle this like a landingpad instruction (by placing the
249 // result in its own group, and having that group alias externals).
250 auto *Val = &Inst;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000251 Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrUnknown));
Hal Finkeldb5f86a2014-10-14 20:51:26 +0000252 }
253
Hal Finkel7529c552014-09-02 21:43:13 +0000254 static bool isFunctionExternal(Function *Fn) {
255 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
256 }
257
George Burgess IVcae581d2016-04-13 23:27:37 +0000258 /// Gets whether the sets at Index1 above, below, or equal to the sets at
259 /// Index2. Returns None if they are not in the same set chain.
Hal Finkel7529c552014-09-02 21:43:13 +0000260 static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
261 StratifiedIndex Index1,
262 StratifiedIndex Index2) {
263 if (Index1 == Index2)
264 return Level::Same;
265
266 const auto *Current = &Sets.getLink(Index1);
267 while (Current->hasBelow()) {
268 if (Current->Below == Index2)
269 return Level::Below;
270 Current = &Sets.getLink(Current->Below);
271 }
272
273 Current = &Sets.getLink(Index1);
274 while (Current->hasAbove()) {
275 if (Current->Above == Index2)
276 return Level::Above;
277 Current = &Sets.getLink(Current->Above);
278 }
279
George Burgess IV77351ba32016-01-28 00:54:01 +0000280 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000281 }
282
283 bool
284 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
285 Value *FuncValue,
286 const iterator_range<User::op_iterator> &Args) {
Hal Finkelca616ac2014-09-02 23:29:48 +0000287 const unsigned ExpectedMaxArgs = 8;
288 const unsigned MaxSupportedArgs = 50;
Hal Finkel7529c552014-09-02 21:43:13 +0000289 assert(Fns.size() > 0);
290
George Burgess IVcae581d2016-04-13 23:27:37 +0000291 // This algorithm is n^2, so an arbitrary upper-bound of 50 args was
292 // selected, so it doesn't take too long in insane cases.
George Burgess IVab03af22015-03-10 02:58:15 +0000293 if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs)
Hal Finkel7529c552014-09-02 21:43:13 +0000294 return false;
295
296 // Exit early if we'll fail anyway
297 for (auto *Fn : Fns) {
298 if (isFunctionExternal(Fn) || Fn->isVarArg())
299 return false;
300 auto &MaybeInfo = AA.ensureCached(Fn);
301 if (!MaybeInfo.hasValue())
302 return false;
303 }
304
305 SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
306 SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
307 for (auto *Fn : Fns) {
308 auto &Info = *AA.ensureCached(Fn);
309 auto &Sets = Info.Sets;
310 auto &RetVals = Info.ReturnedValues;
311
312 Parameters.clear();
313 for (auto &Param : Fn->args()) {
314 auto MaybeInfo = Sets.find(&Param);
315 // Did a new parameter somehow get added to the function/slip by?
316 if (!MaybeInfo.hasValue())
317 return false;
318 Parameters.push_back(*MaybeInfo);
319 }
320
321 // Adding an edge from argument -> return value for each parameter that
322 // may alias the return value
323 for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
324 auto &ParamInfo = Parameters[I];
325 auto &ArgVal = Arguments[I];
326 bool AddEdge = false;
327 StratifiedAttrs Externals;
328 for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
329 auto MaybeInfo = Sets.find(RetVals[X]);
330 if (!MaybeInfo.hasValue())
331 return false;
332
333 auto &RetInfo = *MaybeInfo;
334 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
335 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
336 auto MaybeRelation =
337 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
338 if (MaybeRelation.hasValue()) {
339 AddEdge = true;
340 Externals |= RetAttrs | ParamAttrs;
341 }
342 }
343 if (AddEdge)
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000344 Output.push_back(
345 Edge(FuncValue, ArgVal, EdgeType::Assign, Externals));
Hal Finkel7529c552014-09-02 21:43:13 +0000346 }
347
348 if (Parameters.size() != Arguments.size())
349 return false;
350
George Burgess IVcae581d2016-04-13 23:27:37 +0000351 /// Adding edges between arguments for arguments that may end up aliasing
352 /// each other. This is necessary for functions such as
353 /// void foo(int** a, int** b) { *a = *b; }
354 /// (Technically, the proper sets for this would be those below
355 /// Arguments[I] and Arguments[X], but our algorithm will produce
356 /// extremely similar, and equally correct, results either way)
Hal Finkel7529c552014-09-02 21:43:13 +0000357 for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
358 auto &MainVal = Arguments[I];
359 auto &MainInfo = Parameters[I];
360 auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
361 for (unsigned X = I + 1; X != E; ++X) {
362 auto &SubInfo = Parameters[X];
363 auto &SubVal = Arguments[X];
364 auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
365 auto MaybeRelation =
366 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
367
368 if (!MaybeRelation.hasValue())
369 continue;
370
371 auto NewAttrs = SubAttrs | MainAttrs;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000372 Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
Hal Finkel7529c552014-09-02 21:43:13 +0000373 }
374 }
375 }
376 return true;
377 }
378
379 template <typename InstT> void visitCallLikeInst(InstT &Inst) {
George Burgess IV18b83fe2016-06-01 18:39:54 +0000380 // Check if Inst is a call to a library function that allocates/deallocates
381 // on the heap. Those kinds of functions do not introduce any aliases.
382 // TODO: address other common library functions such as realloc(), strdup(),
383 // etc.
384 if (isMallocLikeFn(&Inst, &TLI) || isCallocLikeFn(&Inst, &TLI)) {
385 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrNone));
386 return;
387 } else if (isFreeCall(&Inst, &TLI)) {
388 assert(Inst.getNumArgOperands() == 1);
389 auto argVal = Inst.arg_begin()->get();
390 Output.push_back(Edge(argVal, argVal, EdgeType::Assign, AttrNone));
391 return;
392 }
393
George Burgess IV68b36e02015-08-28 00:16:18 +0000394 // TODO: Add support for noalias args/all the other fun function attributes
395 // that we can tack on.
Hal Finkel7529c552014-09-02 21:43:13 +0000396 SmallVector<Function *, 4> Targets;
397 if (getPossibleTargets(&Inst, Targets)) {
398 if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
399 return;
400 // Cleanup from interprocedural analysis
401 Output.clear();
402 }
403
George Burgess IV68b36e02015-08-28 00:16:18 +0000404 // Because the function is opaque, we need to note that anything
405 // could have happened to the arguments, and that the result could alias
406 // just about anything, too.
407 // The goal of the loop is in part to unify many Values into one set, so we
408 // don't care if the function is void there.
Hal Finkel7529c552014-09-02 21:43:13 +0000409 for (Value *V : Inst.arg_operands())
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000410 Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrUnknown));
George Burgess IV68b36e02015-08-28 00:16:18 +0000411 if (Inst.getNumArgOperands() == 0 &&
412 Inst.getType() != Type::getVoidTy(Inst.getContext()))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000413 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrUnknown));
Hal Finkel7529c552014-09-02 21:43:13 +0000414 }
415
416 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
417
418 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
419
George Burgess IVcae581d2016-04-13 23:27:37 +0000420 /// Because vectors/aggregates are immutable and unaddressable, there's
421 /// nothing we can do to coax a value out of them, other than calling
422 /// Extract{Element,Value}. We can effectively treat them as pointers to
423 /// arbitrary memory locations we can store in and load from.
Hal Finkel7529c552014-09-02 21:43:13 +0000424 void visitExtractElementInst(ExtractElementInst &Inst) {
425 auto *Ptr = Inst.getVectorOperand();
426 auto *Val = &Inst;
Hal Finkel8d1590d2014-09-02 22:52:30 +0000427 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000428 }
429
430 void visitInsertElementInst(InsertElementInst &Inst) {
431 auto *Vec = Inst.getOperand(0);
432 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000433 Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
434 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000435 }
436
437 void visitLandingPadInst(LandingPadInst &Inst) {
438 // Exceptions come from "nowhere", from our analysis' perspective.
439 // So we place the instruction its own group, noting that said group may
440 // alias externals
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000441 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrUnknown));
Hal Finkel7529c552014-09-02 21:43:13 +0000442 }
443
444 void visitInsertValueInst(InsertValueInst &Inst) {
445 auto *Agg = Inst.getOperand(0);
446 auto *Val = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000447 Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
448 Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000449 }
450
451 void visitExtractValueInst(ExtractValueInst &Inst) {
452 auto *Ptr = Inst.getAggregateOperand();
Hal Finkel8d1590d2014-09-02 22:52:30 +0000453 Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000454 }
455
456 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
457 auto *From1 = Inst.getOperand(0);
458 auto *From2 = Inst.getOperand(1);
Hal Finkel8d1590d2014-09-02 22:52:30 +0000459 Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
460 Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
Hal Finkel7529c552014-09-02 21:43:13 +0000461 }
Pete Cooper36642532015-06-12 16:13:54 +0000462
463 void visitConstantExpr(ConstantExpr *CE) {
464 switch (CE->getOpcode()) {
465 default:
466 llvm_unreachable("Unknown instruction type encountered!");
467// Build the switch statement using the Instruction.def file.
468#define HANDLE_INST(NUM, OPCODE, CLASS) \
469 case Instruction::OPCODE: \
470 visit##OPCODE(*(CLASS *)CE); \
471 break;
472#include "llvm/IR/Instruction.def"
473 }
474 }
Hal Finkel7529c552014-09-02 21:43:13 +0000475};
476
George Burgess IVcae581d2016-04-13 23:27:37 +0000477/// For a given instruction, we need to know which Value* to get the
478/// users of in order to build our graph. In some cases (i.e. add),
479/// we simply need the Instruction*. In other cases (i.e. store),
480/// finding the users of the Instruction* is useless; we need to find
481/// the users of the first operand. This handles determining which
482/// value to follow for us.
483///
484/// Note: we *need* to keep this in sync with GetEdgesVisitor. Add
485/// something to GetEdgesVisitor, add it here -- remove something from
486/// GetEdgesVisitor, remove it here.
Hal Finkel7529c552014-09-02 21:43:13 +0000487class GetTargetValueVisitor
488 : public InstVisitor<GetTargetValueVisitor, Value *> {
489public:
490 Value *visitInstruction(Instruction &Inst) { return &Inst; }
491
492 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
493
494 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
495 return Inst.getPointerOperand();
496 }
497
498 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
499 return Inst.getPointerOperand();
500 }
501
502 Value *visitInsertElementInst(InsertElementInst &Inst) {
503 return Inst.getOperand(0);
504 }
505
506 Value *visitInsertValueInst(InsertValueInst &Inst) {
507 return Inst.getAggregateOperand();
508 }
509};
510
George Burgess IVdc96feb2016-06-13 19:21:18 +0000511/// The Program Expression Graph (PEG) of CFL analysis
512class CFLGraph {
George Burgess IVdc96feb2016-06-13 19:21:18 +0000513 typedef Value *Node;
Hal Finkel7529c552014-09-02 21:43:13 +0000514
515 struct Edge {
George Burgess IVdc96feb2016-06-13 19:21:18 +0000516 StratifiedAttrs Attr;
517 EdgeType Type;
Hal Finkel7529c552014-09-02 21:43:13 +0000518 Node Other;
George Burgess IVdc96feb2016-06-13 19:21:18 +0000519 };
Hal Finkel7529c552014-09-02 21:43:13 +0000520
George Burgess IVdc96feb2016-06-13 19:21:18 +0000521 typedef std::vector<Edge> EdgeList;
522 typedef DenseMap<Node, EdgeList> NodeMap;
523 NodeMap NodeImpls;
Hal Finkelca616ac2014-09-02 23:29:48 +0000524
George Burgess IVdc96feb2016-06-13 19:21:18 +0000525 // Gets the inverse of a given EdgeType.
526 static EdgeType flipWeight(EdgeType Initial) {
527 switch (Initial) {
528 case EdgeType::Assign:
529 return EdgeType::Assign;
530 case EdgeType::Dereference:
531 return EdgeType::Reference;
532 case EdgeType::Reference:
533 return EdgeType::Dereference;
Hal Finkel7529c552014-09-02 21:43:13 +0000534 }
George Burgess IVdc96feb2016-06-13 19:21:18 +0000535 llvm_unreachable("Incomplete coverage of EdgeType enum");
536 }
Hal Finkel7529c552014-09-02 21:43:13 +0000537
George Burgess IVdc96feb2016-06-13 19:21:18 +0000538 const EdgeList *getNode(Node N) const {
539 auto Itr = NodeImpls.find(N);
540 if (Itr == NodeImpls.end())
541 return nullptr;
542 return &Itr->second;
543 }
544 EdgeList &getOrCreateNode(Node N) { return NodeImpls[N]; }
Hal Finkel7529c552014-09-02 21:43:13 +0000545
George Burgess IVdc96feb2016-06-13 19:21:18 +0000546 static Node nodeDeref(const NodeMap::value_type &P) { return P.first; }
547 typedef std::pointer_to_unary_function<const NodeMap::value_type &, Node>
548 NodeDerefFun;
Hal Finkel7529c552014-09-02 21:43:13 +0000549
550public:
George Burgess IVdc96feb2016-06-13 19:21:18 +0000551 typedef EdgeList::const_iterator const_edge_iterator;
552 typedef mapped_iterator<NodeMap::const_iterator, NodeDerefFun>
553 const_node_iterator;
Hal Finkel7529c552014-09-02 21:43:13 +0000554
George Burgess IVdc96feb2016-06-13 19:21:18 +0000555 void addNode(Node N) { getOrCreateNode(N); }
Hal Finkel7529c552014-09-02 21:43:13 +0000556
George Burgess IVdc96feb2016-06-13 19:21:18 +0000557 void addEdge(Node From, Node To, EdgeType Type,
558 StratifiedAttrs Attr = StratifiedAttrs()) {
Hal Finkel7529c552014-09-02 21:43:13 +0000559
George Burgess IVdc96feb2016-06-13 19:21:18 +0000560 // We can't getOrCreateNode() twice in a row here since the second call may
561 // invalidate the reference returned from the first call
562 getOrCreateNode(From);
563 auto &ToEdges = getOrCreateNode(To);
564 auto &FromEdges = getOrCreateNode(From);
Hal Finkel7529c552014-09-02 21:43:13 +0000565
George Burgess IVdc96feb2016-06-13 19:21:18 +0000566 FromEdges.push_back(Edge{Attr, Type, To});
567 ToEdges.push_back(Edge{Attr, flipWeight(Type), From});
Hal Finkel7529c552014-09-02 21:43:13 +0000568 }
569
George Burgess IVdc96feb2016-06-13 19:21:18 +0000570 iterator_range<const_edge_iterator> edgesFor(Node N) const {
571 auto Edges = getNode(N);
572 assert(Edges != nullptr);
573 return make_range(Edges->begin(), Edges->end());
Hal Finkel7529c552014-09-02 21:43:13 +0000574 }
575
George Burgess IVdc96feb2016-06-13 19:21:18 +0000576 iterator_range<const_node_iterator> nodes() const {
577 return make_range<const_node_iterator>(
578 map_iterator(NodeImpls.begin(), NodeDerefFun(nodeDeref)),
579 map_iterator(NodeImpls.end(), NodeDerefFun(nodeDeref)));
Hal Finkel7529c552014-09-02 21:43:13 +0000580 }
581
582 bool empty() const { return NodeImpls.empty(); }
583 std::size_t size() const { return NodeImpls.size(); }
Hal Finkel7529c552014-09-02 21:43:13 +0000584};
George Burgess IVe17756e2016-06-14 18:02:27 +0000585
586class CFLGraphBuilder {
587 // Input of the builder
588 CFLAAResult &Analysis;
589 const TargetLibraryInfo &TLI;
590
591 // Output of the builder
592 CFLGraph Graph;
593 SmallVector<Value *, 4> ReturnedValues;
594
595 // Auxiliary structures used by the builder
596
597 // Helper functions
598
599 // Determines whether or not we an instruction is useless to us (e.g.
600 // FenceInst)
601 static bool hasUsefulEdges(Instruction *Inst) {
602 bool IsNonInvokeTerminator =
603 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
604 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
605 !IsNonInvokeTerminator;
606 }
607
608 static bool hasUsefulEdges(ConstantExpr *CE) {
609 // ConstantExpr doesn't have terminators, invokes, or fences, so only needs
610 // to check for compares.
611 return CE->getOpcode() != Instruction::ICmp &&
612 CE->getOpcode() != Instruction::FCmp;
613 }
614
615 // Gets edges of the given Instruction*, writing them to the SmallVector*.
616 void argsToEdges(Instruction *Inst, SmallVectorImpl<Edge> &Output) {
617 assert(hasUsefulEdges(Inst) &&
618 "Expected instructions to have 'useful' edges");
619 GetEdgesVisitor v(Analysis, Output, TLI);
620 v.visit(Inst);
621 }
622
623 // Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
624 void argsToEdges(ConstantExpr *CE, SmallVectorImpl<Edge> &Output) {
625 assert(hasUsefulEdges(CE) &&
626 "Expected constant expr to have 'useful' edges");
627 GetEdgesVisitor v(Analysis, Output, TLI);
628 v.visitConstantExpr(CE);
629 }
630
631 // Gets the edges of a ConstantExpr as if it was an Instruction. This function
632 // also acts on any nested ConstantExprs, adding the edges of those to the
633 // given SmallVector as well.
634 void constexprToEdges(ConstantExpr &CExprToCollapse,
635 SmallVectorImpl<Edge> &Results) {
636 SmallVector<ConstantExpr *, 4> Worklist;
637 Worklist.push_back(&CExprToCollapse);
638
639 SmallVector<Edge, 8> ConstexprEdges;
640 SmallPtrSet<ConstantExpr *, 4> Visited;
641 while (!Worklist.empty()) {
642 auto *CExpr = Worklist.pop_back_val();
643
644 if (!hasUsefulEdges(CExpr))
645 continue;
646
647 ConstexprEdges.clear();
648 argsToEdges(CExpr, ConstexprEdges);
649 for (auto &Edge : ConstexprEdges) {
650 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
651 if (Visited.insert(Nested).second)
652 Worklist.push_back(Nested);
653
654 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
655 if (Visited.insert(Nested).second)
656 Worklist.push_back(Nested);
657 }
658
659 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
660 }
661 }
662
663 // Builds the graph needed for constructing the StratifiedSets for the given
664 // function
665 void buildGraphFrom(Function &Fn) {
666 for (auto &Bb : Fn.getBasicBlockList())
667 for (auto &Inst : Bb.getInstList())
668 addInstructionToGraph(Inst);
669 }
670
671 // Given an Instruction, this will add it to the graph, along with any
672 // Instructions that are potentially only available from said Instruction
673 // For example, given the following line:
674 // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
675 // addInstructionToGraph would add both the `load` and `getelementptr`
676 // instructions to the graph appropriately.
677 void addInstructionToGraph(Instruction &Inst) {
678 // We don't want the edges of most "return" instructions, but we *do* want
679 // to know what can be returned.
680 if (isa<ReturnInst>(&Inst))
681 ReturnedValues.push_back(&Inst);
682
683 if (!hasUsefulEdges(&Inst))
684 return;
685
686 SmallVector<Edge, 8> Edges;
687 argsToEdges(&Inst, Edges);
688
689 // In the case of an unused alloca (or similar), edges may be empty. Note
690 // that it exists so we can potentially answer NoAlias.
691 if (Edges.empty()) {
692 auto MaybeVal = getTargetValue(&Inst);
693 assert(MaybeVal.hasValue());
694 auto *Target = *MaybeVal;
695 Graph.addNode(Target);
696 return;
697 }
698
699 auto addEdgeToGraph = [this](const Edge &E) {
700 Graph.addEdge(E.From, E.To, E.Weight, E.AdditionalAttrs);
701 };
702
703 SmallVector<ConstantExpr *, 4> ConstantExprs;
704 for (const Edge &E : Edges) {
705 addEdgeToGraph(E);
706 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
707 ConstantExprs.push_back(Constexpr);
708 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
709 ConstantExprs.push_back(Constexpr);
710 }
711
712 for (ConstantExpr *CE : ConstantExprs) {
713 Edges.clear();
714 constexprToEdges(*CE, Edges);
715 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
716 }
717 }
718
719public:
720 CFLGraphBuilder(CFLAAResult &Analysis, const TargetLibraryInfo &TLI,
721 Function &Fn)
722 : Analysis(Analysis), TLI(TLI) {
723 buildGraphFrom(Fn);
724 }
725
726 const CFLGraph &getCFLGraph() { return Graph; }
727 SmallVector<Value *, 4> takeReturnValues() {
728 return std::move(ReturnedValues);
729 }
730};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000731}
Hal Finkel7529c552014-09-02 21:43:13 +0000732
Hal Finkel7529c552014-09-02 21:43:13 +0000733//===----------------------------------------------------------------------===//
734// Function declarations that require types defined in the namespace above
735//===----------------------------------------------------------------------===//
736
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000737/// Given a StratifiedAttrs, returns true if it marks the corresponding values
738/// as globals or arguments
739static bool isGlobalOrArgAttr(StratifiedAttrs Attr);
Hal Finkel7529c552014-09-02 21:43:13 +0000740
George Burgess IV652ec4f2016-06-09 23:15:04 +0000741/// Given a StratifiedAttrs, returns true if the corresponding values come from
742/// an unknown source (such as opaque memory or an integer cast)
743static bool isUnknownAttr(StratifiedAttrs Attr);
744
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000745/// Given an argument number, returns the appropriate StratifiedAttr to set.
746static StratifiedAttr argNumberToAttr(unsigned ArgNum);
747
748/// Given a Value, potentially return which StratifiedAttr it maps to.
749static Optional<StratifiedAttr> valueToAttr(Value *Val);
Hal Finkel7529c552014-09-02 21:43:13 +0000750
George Burgess IVcae581d2016-04-13 23:27:37 +0000751/// Gets the "Level" that one should travel in StratifiedSets
752/// given an EdgeType.
Hal Finkel7529c552014-09-02 21:43:13 +0000753static Level directionOfEdgeType(EdgeType);
754
George Burgess IVcae581d2016-04-13 23:27:37 +0000755/// Determines whether it would be pointless to add the given Value to our sets.
George Burgess IVab03af22015-03-10 02:58:15 +0000756static bool canSkipAddingToSets(Value *Val);
757
Hal Finkel7529c552014-09-02 21:43:13 +0000758static Optional<Function *> parentFunctionOfValue(Value *Val) {
759 if (auto *Inst = dyn_cast<Instruction>(Val)) {
760 auto *Bb = Inst->getParent();
761 return Bb->getParent();
762 }
763
764 if (auto *Arg = dyn_cast<Argument>(Val))
765 return Arg->getParent();
George Burgess IV77351ba32016-01-28 00:54:01 +0000766 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000767}
768
769template <typename Inst>
770static bool getPossibleTargets(Inst *Call,
771 SmallVectorImpl<Function *> &Output) {
772 if (auto *Fn = Call->getCalledFunction()) {
773 Output.push_back(Fn);
774 return true;
775 }
776
777 // TODO: If the call is indirect, we might be able to enumerate all potential
778 // targets of the call and return them, rather than just failing.
779 return false;
780}
781
782static Optional<Value *> getTargetValue(Instruction *Inst) {
783 GetTargetValueVisitor V;
784 return V.visit(Inst);
785}
786
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000787static bool isGlobalOrArgAttr(StratifiedAttrs Attr) {
788 return Attr.reset(AttrEscapedIndex).reset(AttrUnknownIndex).any();
789}
790
George Burgess IV652ec4f2016-06-09 23:15:04 +0000791static bool isUnknownAttr(StratifiedAttrs Attr) {
792 return Attr.test(AttrUnknownIndex);
793}
794
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000795static Optional<StratifiedAttr> valueToAttr(Value *Val) {
Hal Finkel7529c552014-09-02 21:43:13 +0000796 if (isa<GlobalValue>(Val))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000797 return AttrGlobal;
Hal Finkel7529c552014-09-02 21:43:13 +0000798
799 if (auto *Arg = dyn_cast<Argument>(Val))
Daniel Berlin16f7a522015-01-26 17:31:17 +0000800 // Only pointer arguments should have the argument attribute,
801 // because things can't escape through scalars without us seeing a
802 // cast, and thus, interaction with them doesn't matter.
803 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000804 return argNumberToAttr(Arg->getArgNo());
George Burgess IV77351ba32016-01-28 00:54:01 +0000805 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000806}
807
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000808static StratifiedAttr argNumberToAttr(unsigned ArgNum) {
George Burgess IV3c898c22015-01-21 16:37:21 +0000809 if (ArgNum >= AttrMaxNumArgs)
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000810 return AttrUnknown;
811 return 1 << (ArgNum + AttrFirstArgIndex);
Hal Finkel7529c552014-09-02 21:43:13 +0000812}
813
Hal Finkel7529c552014-09-02 21:43:13 +0000814static Level directionOfEdgeType(EdgeType Weight) {
815 switch (Weight) {
816 case EdgeType::Reference:
817 return Level::Above;
818 case EdgeType::Dereference:
819 return Level::Below;
820 case EdgeType::Assign:
821 return Level::Same;
822 }
823 llvm_unreachable("Incomplete switch coverage");
824}
825
George Burgess IVab03af22015-03-10 02:58:15 +0000826static bool canSkipAddingToSets(Value *Val) {
827 // Constants can share instances, which may falsely unify multiple
828 // sets, e.g. in
829 // store i32* null, i32** %ptr1
830 // store i32* null, i32** %ptr2
831 // clearly ptr1 and ptr2 should not be unified into the same set, so
832 // we should filter out the (potentially shared) instance to
833 // i32* null.
834 if (isa<Constant>(Val)) {
George Burgess IVab03af22015-03-10 02:58:15 +0000835 // TODO: Because all of these things are constant, we can determine whether
836 // the data is *actually* mutable at graph building time. This will probably
837 // come for free/cheap with offset awareness.
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000838 bool CanStoreMutableData = isa<GlobalValue>(Val) ||
839 isa<ConstantExpr>(Val) ||
840 isa<ConstantAggregate>(Val);
George Burgess IVab03af22015-03-10 02:58:15 +0000841 return !CanStoreMutableData;
842 }
843
844 return false;
Hal Finkel7529c552014-09-02 21:43:13 +0000845}
846
Chandler Carruth8b046a42015-08-14 02:42:20 +0000847// Builds the graph + StratifiedSets for a function.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000848CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) {
George Burgess IVe17756e2016-06-14 18:02:27 +0000849 CFLGraphBuilder GraphBuilder(*this, TLI, *Fn);
850 StratifiedSetsBuilder<Value *> SetBuilder;
Hal Finkel7529c552014-09-02 21:43:13 +0000851
George Burgess IVe17756e2016-06-14 18:02:27 +0000852 auto &Graph = GraphBuilder.getCFLGraph();
George Burgess IVdc96feb2016-06-13 19:21:18 +0000853 SmallVector<Value *, 16> Worklist;
George Burgess IV652ec4f2016-06-09 23:15:04 +0000854 SmallPtrSet<Value *, 16> Globals;
George Burgess IVdc96feb2016-06-13 19:21:18 +0000855 for (auto Node : Graph.nodes())
856 Worklist.push_back(Node);
857
858 while (!Worklist.empty()) {
859 auto *CurValue = Worklist.pop_back_val();
George Burgess IVe17756e2016-06-14 18:02:27 +0000860 SetBuilder.add(CurValue);
George Burgess IVdc96feb2016-06-13 19:21:18 +0000861 if (canSkipAddingToSets(CurValue))
862 continue;
863
864 if (isa<GlobalValue>(CurValue))
865 Globals.insert(CurValue);
866
867 for (const auto &Edge : Graph.edgesFor(CurValue)) {
868 auto Label = Edge.Type;
869 auto *OtherValue = Edge.Other;
870
871 if (canSkipAddingToSets(OtherValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000872 continue;
George Burgess IVdc96feb2016-06-13 19:21:18 +0000873 if (isa<GlobalValue>(OtherValue))
874 Globals.insert(OtherValue);
Hal Finkel7529c552014-09-02 21:43:13 +0000875
George Burgess IVdc96feb2016-06-13 19:21:18 +0000876 bool Added;
877 switch (directionOfEdgeType(Label)) {
878 case Level::Above:
George Burgess IVe17756e2016-06-14 18:02:27 +0000879 Added = SetBuilder.addAbove(CurValue, OtherValue);
George Burgess IVdc96feb2016-06-13 19:21:18 +0000880 break;
881 case Level::Below:
George Burgess IVe17756e2016-06-14 18:02:27 +0000882 Added = SetBuilder.addBelow(CurValue, OtherValue);
George Burgess IVdc96feb2016-06-13 19:21:18 +0000883 break;
884 case Level::Same:
George Burgess IVe17756e2016-06-14 18:02:27 +0000885 Added = SetBuilder.addWith(CurValue, OtherValue);
George Burgess IVdc96feb2016-06-13 19:21:18 +0000886 break;
Hal Finkel7529c552014-09-02 21:43:13 +0000887 }
George Burgess IVdc96feb2016-06-13 19:21:18 +0000888
889 auto Aliasing = Edge.Attr;
George Burgess IVe17756e2016-06-14 18:02:27 +0000890 SetBuilder.noteAttributes(CurValue, Aliasing);
891 SetBuilder.noteAttributes(OtherValue, Aliasing);
George Burgess IVdc96feb2016-06-13 19:21:18 +0000892
893 if (Added)
894 Worklist.push_back(OtherValue);
Hal Finkel7529c552014-09-02 21:43:13 +0000895 }
896 }
897
George Burgess IV652ec4f2016-06-09 23:15:04 +0000898 // Special handling for globals and arguments
George Burgess IVe17756e2016-06-14 18:02:27 +0000899 auto ProcessGlobalOrArgValue = [&SetBuilder](Value &Val) {
900 SetBuilder.add(&Val);
George Burgess IV652ec4f2016-06-09 23:15:04 +0000901 auto Attr = valueToAttr(&Val);
902 if (Attr.hasValue()) {
George Burgess IVe17756e2016-06-14 18:02:27 +0000903 SetBuilder.noteAttributes(&Val, *Attr);
George Burgess IV652ec4f2016-06-09 23:15:04 +0000904 // TODO: do we need to filter out non-pointer values here?
George Burgess IVe17756e2016-06-14 18:02:27 +0000905 SetBuilder.addAttributesBelow(&Val, AttrUnknown);
George Burgess IV652ec4f2016-06-09 23:15:04 +0000906 }
907 };
George Burgess IVab03af22015-03-10 02:58:15 +0000908
George Burgess IV652ec4f2016-06-09 23:15:04 +0000909 for (auto &Arg : Fn->args())
910 ProcessGlobalOrArgValue(Arg);
911 for (auto *Global : Globals)
912 ProcessGlobalOrArgValue(*Global);
Hal Finkel7529c552014-09-02 21:43:13 +0000913
George Burgess IVe17756e2016-06-14 18:02:27 +0000914 return FunctionInfo(SetBuilder.build(), GraphBuilder.takeReturnValues());
Hal Finkel7529c552014-09-02 21:43:13 +0000915}
916
Chandler Carruth7b560d42015-09-09 17:55:00 +0000917void CFLAAResult::scan(Function *Fn) {
Hal Finkel8d1590d2014-09-02 22:52:30 +0000918 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
Hal Finkel7529c552014-09-02 21:43:13 +0000919 (void)InsertPair;
920 assert(InsertPair.second &&
921 "Trying to scan a function that has already been cached");
922
George Burgess IV6edb8912016-05-02 18:09:19 +0000923 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
924 // may get evaluated after operator[], potentially triggering a DenseMap
925 // resize and invalidating the reference returned by operator[]
926 auto FunInfo = buildSetsFrom(Fn);
927 Cache[Fn] = std::move(FunInfo);
928
Hal Finkel7529c552014-09-02 21:43:13 +0000929 Handles.push_front(FunctionHandle(Fn, this));
930}
931
Chandler Carruth7b560d42015-09-09 17:55:00 +0000932void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); }
Chandler Carruth8b046a42015-08-14 02:42:20 +0000933
George Burgess IVcae581d2016-04-13 23:27:37 +0000934/// Ensures that the given function is available in the cache, and returns the
935/// entry.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000936const Optional<CFLAAResult::FunctionInfo> &
937CFLAAResult::ensureCached(Function *Fn) {
Chandler Carruth8b046a42015-08-14 02:42:20 +0000938 auto Iter = Cache.find(Fn);
939 if (Iter == Cache.end()) {
940 scan(Fn);
941 Iter = Cache.find(Fn);
942 assert(Iter != Cache.end());
943 assert(Iter->second.hasValue());
944 }
945 return Iter->second;
946}
947
Chandler Carruth7b560d42015-09-09 17:55:00 +0000948AliasResult CFLAAResult::query(const MemoryLocation &LocA,
949 const MemoryLocation &LocB) {
Hal Finkel7529c552014-09-02 21:43:13 +0000950 auto *ValA = const_cast<Value *>(LocA.Ptr);
951 auto *ValB = const_cast<Value *>(LocB.Ptr);
952
953 Function *Fn = nullptr;
954 auto MaybeFnA = parentFunctionOfValue(ValA);
955 auto MaybeFnB = parentFunctionOfValue(ValB);
956 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
George Burgess IVcae581d2016-04-13 23:27:37 +0000957 // The only times this is known to happen are when globals + InlineAsm are
958 // involved
George Burgess IV33305e72015-02-12 03:07:07 +0000959 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000960 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +0000961 }
962
963 if (MaybeFnA.hasValue()) {
964 Fn = *MaybeFnA;
965 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
966 "Interprocedural queries not supported");
967 } else {
968 Fn = *MaybeFnB;
969 }
970
971 assert(Fn != nullptr);
972 auto &MaybeInfo = ensureCached(Fn);
973 assert(MaybeInfo.hasValue());
974
975 auto &Sets = MaybeInfo->Sets;
976 auto MaybeA = Sets.find(ValA);
977 if (!MaybeA.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000978 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +0000979
980 auto MaybeB = Sets.find(ValB);
981 if (!MaybeB.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000982 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +0000983
984 auto SetA = *MaybeA;
985 auto SetB = *MaybeB;
Hal Finkel7529c552014-09-02 21:43:13 +0000986 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
987 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
George Burgess IV33305e72015-02-12 03:07:07 +0000988
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000989 // If both values are local (meaning the corresponding set has attribute
990 // AttrNone or AttrEscaped), then we know that CFLAA fully models them: they
991 // may-alias each other if and only if they are in the same set
992 // If at least one value is non-local (meaning it either is global/argument or
993 // it comes from unknown sources like integer cast), the situation becomes a
994 // bit more interesting. We follow three general rules described below:
995 // - Non-local values may alias each other
996 // - AttrNone values do not alias any non-local values
George Burgess IV652ec4f2016-06-09 23:15:04 +0000997 // - AttrEscaped do not alias globals/arguments, but they may alias
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000998 // AttrUnknown values
999 if (SetA.Index == SetB.Index)
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001000 return MayAlias;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001001 if (AttrsA.none() || AttrsB.none())
1002 return NoAlias;
George Burgess IV652ec4f2016-06-09 23:15:04 +00001003 if (isUnknownAttr(AttrsA) || isUnknownAttr(AttrsB))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001004 return MayAlias;
1005 if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB))
1006 return MayAlias;
1007 return NoAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001008}
Mehdi Amini46a43552015-03-04 18:43:29 +00001009
Chandler Carruthb4faf132016-03-11 10:22:49 +00001010char CFLAA::PassID;
1011
Chandler Carruthb47f8012016-03-11 11:05:24 +00001012CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV18b83fe2016-06-01 18:39:54 +00001013 return CFLAAResult(AM.getResult<TargetLibraryAnalysis>(F));
Chandler Carruth7b560d42015-09-09 17:55:00 +00001014}
1015
Chandler Carruth7b560d42015-09-09 17:55:00 +00001016char CFLAAWrapperPass::ID = 0;
Chandler Carruth12884f72016-03-02 15:56:53 +00001017INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false,
1018 true)
Chandler Carruth7b560d42015-09-09 17:55:00 +00001019
1020ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); }
1021
1022CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) {
1023 initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry());
1024}
1025
George Burgess IV18b83fe2016-06-01 18:39:54 +00001026void CFLAAWrapperPass::initializePass() {
1027 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
1028 Result.reset(new CFLAAResult(TLIWP.getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +00001029}
1030
1031void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1032 AU.setPreservesAll();
George Burgess IV18b83fe2016-06-01 18:39:54 +00001033 AU.addRequired<TargetLibraryInfoWrapperPass>();
Mehdi Amini46a43552015-03-04 18:43:29 +00001034}