blob: 24bc4e937a9b38460e70b9ba8242f6ad5e10532c [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 IVa1f9a2d2016-06-07 18:35:37 +0000660/// Given an argument number, returns the appropriate StratifiedAttr to set.
661static StratifiedAttr argNumberToAttr(unsigned ArgNum);
662
663/// Given a Value, potentially return which StratifiedAttr it maps to.
664static Optional<StratifiedAttr> valueToAttr(Value *Val);
Hal Finkel7529c552014-09-02 21:43:13 +0000665
George Burgess IVcae581d2016-04-13 23:27:37 +0000666/// Gets the inverse of a given EdgeType.
667static EdgeType flipWeight(EdgeType Initial);
Hal Finkel7529c552014-09-02 21:43:13 +0000668
George Burgess IVcae581d2016-04-13 23:27:37 +0000669/// Gets edges of the given Instruction*, writing them to the SmallVector*.
George Burgess IV18b83fe2016-06-01 18:39:54 +0000670static void argsToEdges(CFLAAResult &, Instruction *, SmallVectorImpl<Edge> &,
671 const TargetLibraryInfo &);
Hal Finkel7529c552014-09-02 21:43:13 +0000672
George Burgess IVcae581d2016-04-13 23:27:37 +0000673/// Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
George Burgess IV18b83fe2016-06-01 18:39:54 +0000674static void argsToEdges(CFLAAResult &, ConstantExpr *, SmallVectorImpl<Edge> &,
675 const TargetLibraryInfo &);
Pete Cooper36642532015-06-12 16:13:54 +0000676
George Burgess IVcae581d2016-04-13 23:27:37 +0000677/// Gets the "Level" that one should travel in StratifiedSets
678/// given an EdgeType.
Hal Finkel7529c552014-09-02 21:43:13 +0000679static Level directionOfEdgeType(EdgeType);
680
George Burgess IVcae581d2016-04-13 23:27:37 +0000681/// Builds the graph needed for constructing the StratifiedSets for the
682/// given function
Chandler Carruth7b560d42015-09-09 17:55:00 +0000683static void buildGraphFrom(CFLAAResult &, Function *,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000684 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &,
685 const TargetLibraryInfo &);
Hal Finkel7529c552014-09-02 21:43:13 +0000686
George Burgess IVcae581d2016-04-13 23:27:37 +0000687/// Gets the edges of a ConstantExpr as if it was an Instruction. This function
688/// also acts on any nested ConstantExprs, adding the edges of those to the
689/// given SmallVector as well.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000690static void constexprToEdges(CFLAAResult &, ConstantExpr &,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000691 SmallVectorImpl<Edge> &,
692 const TargetLibraryInfo &);
George Burgess IVab03af22015-03-10 02:58:15 +0000693
George Burgess IVcae581d2016-04-13 23:27:37 +0000694/// Given an Instruction, this will add it to the graph, along with any
695/// Instructions that are potentially only available from said Instruction
696/// For example, given the following line:
697/// %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
698/// addInstructionToGraph would add both the `load` and `getelementptr`
699/// instructions to the graph appropriately.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000700static void addInstructionToGraph(CFLAAResult &, Instruction &,
George Burgess IVab03af22015-03-10 02:58:15 +0000701 SmallVectorImpl<Value *> &, NodeMapT &,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000702 GraphT &, const TargetLibraryInfo &);
George Burgess IVab03af22015-03-10 02:58:15 +0000703
George Burgess IVcae581d2016-04-13 23:27:37 +0000704/// Determines whether it would be pointless to add the given Value to our sets.
George Burgess IVab03af22015-03-10 02:58:15 +0000705static bool canSkipAddingToSets(Value *Val);
706
Hal Finkel7529c552014-09-02 21:43:13 +0000707static Optional<Function *> parentFunctionOfValue(Value *Val) {
708 if (auto *Inst = dyn_cast<Instruction>(Val)) {
709 auto *Bb = Inst->getParent();
710 return Bb->getParent();
711 }
712
713 if (auto *Arg = dyn_cast<Argument>(Val))
714 return Arg->getParent();
George Burgess IV77351ba32016-01-28 00:54:01 +0000715 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000716}
717
718template <typename Inst>
719static bool getPossibleTargets(Inst *Call,
720 SmallVectorImpl<Function *> &Output) {
721 if (auto *Fn = Call->getCalledFunction()) {
722 Output.push_back(Fn);
723 return true;
724 }
725
726 // TODO: If the call is indirect, we might be able to enumerate all potential
727 // targets of the call and return them, rather than just failing.
728 return false;
729}
730
731static Optional<Value *> getTargetValue(Instruction *Inst) {
732 GetTargetValueVisitor V;
733 return V.visit(Inst);
734}
735
736static bool hasUsefulEdges(Instruction *Inst) {
737 bool IsNonInvokeTerminator =
738 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
739 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
740}
741
Pete Cooper36642532015-06-12 16:13:54 +0000742static bool hasUsefulEdges(ConstantExpr *CE) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000743 // ConstantExpr doesn't have terminators, invokes, or fences, so only needs
Pete Cooper36642532015-06-12 16:13:54 +0000744 // to check for compares.
745 return CE->getOpcode() != Instruction::ICmp &&
746 CE->getOpcode() != Instruction::FCmp;
747}
748
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000749static bool isGlobalOrArgAttr(StratifiedAttrs Attr) {
750 return Attr.reset(AttrEscapedIndex).reset(AttrUnknownIndex).any();
751}
752
753static Optional<StratifiedAttr> valueToAttr(Value *Val) {
Hal Finkel7529c552014-09-02 21:43:13 +0000754 if (isa<GlobalValue>(Val))
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000755 return AttrGlobal;
Hal Finkel7529c552014-09-02 21:43:13 +0000756
757 if (auto *Arg = dyn_cast<Argument>(Val))
Daniel Berlin16f7a522015-01-26 17:31:17 +0000758 // Only pointer arguments should have the argument attribute,
759 // because things can't escape through scalars without us seeing a
760 // cast, and thus, interaction with them doesn't matter.
761 if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000762 return argNumberToAttr(Arg->getArgNo());
George Burgess IV77351ba32016-01-28 00:54:01 +0000763 return None;
Hal Finkel7529c552014-09-02 21:43:13 +0000764}
765
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000766static StratifiedAttr argNumberToAttr(unsigned ArgNum) {
George Burgess IV3c898c22015-01-21 16:37:21 +0000767 if (ArgNum >= AttrMaxNumArgs)
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000768 return AttrUnknown;
769 return 1 << (ArgNum + AttrFirstArgIndex);
Hal Finkel7529c552014-09-02 21:43:13 +0000770}
771
772static EdgeType flipWeight(EdgeType Initial) {
773 switch (Initial) {
774 case EdgeType::Assign:
775 return EdgeType::Assign;
776 case EdgeType::Dereference:
777 return EdgeType::Reference;
778 case EdgeType::Reference:
779 return EdgeType::Dereference;
780 }
781 llvm_unreachable("Incomplete coverage of EdgeType enum");
782}
783
Chandler Carruth7b560d42015-09-09 17:55:00 +0000784static void argsToEdges(CFLAAResult &Analysis, Instruction *Inst,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000785 SmallVectorImpl<Edge> &Output,
786 const TargetLibraryInfo &TLI) {
George Burgess IVab03af22015-03-10 02:58:15 +0000787 assert(hasUsefulEdges(Inst) &&
788 "Expected instructions to have 'useful' edges");
George Burgess IV18b83fe2016-06-01 18:39:54 +0000789 GetEdgesVisitor v(Analysis, Output, TLI);
Hal Finkel7529c552014-09-02 21:43:13 +0000790 v.visit(Inst);
791}
792
Chandler Carruth7b560d42015-09-09 17:55:00 +0000793static void argsToEdges(CFLAAResult &Analysis, ConstantExpr *CE,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000794 SmallVectorImpl<Edge> &Output,
795 const TargetLibraryInfo &TLI) {
Pete Cooper36642532015-06-12 16:13:54 +0000796 assert(hasUsefulEdges(CE) && "Expected constant expr to have 'useful' edges");
George Burgess IV18b83fe2016-06-01 18:39:54 +0000797 GetEdgesVisitor v(Analysis, Output, TLI);
Pete Cooper36642532015-06-12 16:13:54 +0000798 v.visitConstantExpr(CE);
799}
800
Hal Finkel7529c552014-09-02 21:43:13 +0000801static Level directionOfEdgeType(EdgeType Weight) {
802 switch (Weight) {
803 case EdgeType::Reference:
804 return Level::Above;
805 case EdgeType::Dereference:
806 return Level::Below;
807 case EdgeType::Assign:
808 return Level::Same;
809 }
810 llvm_unreachable("Incomplete switch coverage");
811}
812
Chandler Carruth7b560d42015-09-09 17:55:00 +0000813static void constexprToEdges(CFLAAResult &Analysis,
George Burgess IVab03af22015-03-10 02:58:15 +0000814 ConstantExpr &CExprToCollapse,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000815 SmallVectorImpl<Edge> &Results,
816 const TargetLibraryInfo &TLI) {
George Burgess IVab03af22015-03-10 02:58:15 +0000817 SmallVector<ConstantExpr *, 4> Worklist;
818 Worklist.push_back(&CExprToCollapse);
819
820 SmallVector<Edge, 8> ConstexprEdges;
Pete Cooper36642532015-06-12 16:13:54 +0000821 SmallPtrSet<ConstantExpr *, 4> Visited;
George Burgess IVab03af22015-03-10 02:58:15 +0000822 while (!Worklist.empty()) {
823 auto *CExpr = Worklist.pop_back_val();
George Burgess IVab03af22015-03-10 02:58:15 +0000824
Pete Cooper36642532015-06-12 16:13:54 +0000825 if (!hasUsefulEdges(CExpr))
George Burgess IVab03af22015-03-10 02:58:15 +0000826 continue;
827
828 ConstexprEdges.clear();
George Burgess IV18b83fe2016-06-01 18:39:54 +0000829 argsToEdges(Analysis, CExpr, ConstexprEdges, TLI);
George Burgess IVab03af22015-03-10 02:58:15 +0000830 for (auto &Edge : ConstexprEdges) {
Pete Cooper36642532015-06-12 16:13:54 +0000831 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
832 if (Visited.insert(Nested).second)
833 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000834
Pete Cooper36642532015-06-12 16:13:54 +0000835 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
836 if (Visited.insert(Nested).second)
837 Worklist.push_back(Nested);
George Burgess IVab03af22015-03-10 02:58:15 +0000838 }
839
840 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
841 }
842}
843
Chandler Carruth7b560d42015-09-09 17:55:00 +0000844static void addInstructionToGraph(CFLAAResult &Analysis, Instruction &Inst,
George Burgess IVab03af22015-03-10 02:58:15 +0000845 SmallVectorImpl<Value *> &ReturnedValues,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000846 NodeMapT &Map, GraphT &Graph,
847 const TargetLibraryInfo &TLI) {
Hal Finkel7529c552014-09-02 21:43:13 +0000848 const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
849 auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
850 auto &Iter = Pair.first;
851 if (Pair.second) {
852 auto NewNode = Graph.addNode();
853 Iter->second = NewNode;
854 }
855 return Iter->second;
856 };
857
George Burgess IVab03af22015-03-10 02:58:15 +0000858 // We don't want the edges of most "return" instructions, but we *do* want
859 // to know what can be returned.
860 if (isa<ReturnInst>(&Inst))
861 ReturnedValues.push_back(&Inst);
862
863 if (!hasUsefulEdges(&Inst))
864 return;
865
Hal Finkel7529c552014-09-02 21:43:13 +0000866 SmallVector<Edge, 8> Edges;
George Burgess IV18b83fe2016-06-01 18:39:54 +0000867 argsToEdges(Analysis, &Inst, Edges, TLI);
Hal Finkel7529c552014-09-02 21:43:13 +0000868
George Burgess IVab03af22015-03-10 02:58:15 +0000869 // In the case of an unused alloca (or similar), edges may be empty. Note
870 // that it exists so we can potentially answer NoAlias.
871 if (Edges.empty()) {
872 auto MaybeVal = getTargetValue(&Inst);
873 assert(MaybeVal.hasValue());
874 auto *Target = *MaybeVal;
875 findOrInsertNode(Target);
876 return;
Hal Finkel7529c552014-09-02 21:43:13 +0000877 }
George Burgess IVab03af22015-03-10 02:58:15 +0000878
George Burgess IVcae581d2016-04-13 23:27:37 +0000879 auto addEdgeToGraph = [&](const Edge &E) {
George Burgess IVab03af22015-03-10 02:58:15 +0000880 auto To = findOrInsertNode(E.To);
881 auto From = findOrInsertNode(E.From);
882 auto FlippedWeight = flipWeight(E.Weight);
883 auto Attrs = E.AdditionalAttrs;
884 Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
885 std::make_pair(FlippedWeight, Attrs));
886 };
887
888 SmallVector<ConstantExpr *, 4> ConstantExprs;
889 for (const Edge &E : Edges) {
890 addEdgeToGraph(E);
891 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
892 ConstantExprs.push_back(Constexpr);
893 if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
894 ConstantExprs.push_back(Constexpr);
895 }
896
897 for (ConstantExpr *CE : ConstantExprs) {
898 Edges.clear();
George Burgess IV18b83fe2016-06-01 18:39:54 +0000899 constexprToEdges(Analysis, *CE, Edges, TLI);
George Burgess IVab03af22015-03-10 02:58:15 +0000900 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
901 }
902}
903
Chandler Carruth7b560d42015-09-09 17:55:00 +0000904static void buildGraphFrom(CFLAAResult &Analysis, Function *Fn,
George Burgess IVab03af22015-03-10 02:58:15 +0000905 SmallVectorImpl<Value *> &ReturnedValues,
George Burgess IV18b83fe2016-06-01 18:39:54 +0000906 NodeMapT &Map, GraphT &Graph,
907 const TargetLibraryInfo &TLI) {
George Burgess IVcae581d2016-04-13 23:27:37 +0000908 // (N.B. We may remove graph construction entirely, because it doesn't really
909 // buy us much.)
George Burgess IVab03af22015-03-10 02:58:15 +0000910 for (auto &Bb : Fn->getBasicBlockList())
911 for (auto &Inst : Bb.getInstList())
George Burgess IV18b83fe2016-06-01 18:39:54 +0000912 addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph, TLI);
George Burgess IVab03af22015-03-10 02:58:15 +0000913}
914
915static bool canSkipAddingToSets(Value *Val) {
916 // Constants can share instances, which may falsely unify multiple
917 // sets, e.g. in
918 // store i32* null, i32** %ptr1
919 // store i32* null, i32** %ptr2
920 // clearly ptr1 and ptr2 should not be unified into the same set, so
921 // we should filter out the (potentially shared) instance to
922 // i32* null.
923 if (isa<Constant>(Val)) {
George Burgess IVab03af22015-03-10 02:58:15 +0000924 // TODO: Because all of these things are constant, we can determine whether
925 // the data is *actually* mutable at graph building time. This will probably
926 // come for free/cheap with offset awareness.
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000927 bool CanStoreMutableData = isa<GlobalValue>(Val) ||
928 isa<ConstantExpr>(Val) ||
929 isa<ConstantAggregate>(Val);
George Burgess IVab03af22015-03-10 02:58:15 +0000930 return !CanStoreMutableData;
931 }
932
933 return false;
Hal Finkel7529c552014-09-02 21:43:13 +0000934}
935
Chandler Carruth8b046a42015-08-14 02:42:20 +0000936// Builds the graph + StratifiedSets for a function.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000937CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) {
Hal Finkel7529c552014-09-02 21:43:13 +0000938 NodeMapT Map;
939 GraphT Graph;
940 SmallVector<Value *, 4> ReturnedValues;
941
George Burgess IV18b83fe2016-06-01 18:39:54 +0000942 buildGraphFrom(*this, Fn, ReturnedValues, Map, Graph, TLI);
Hal Finkel7529c552014-09-02 21:43:13 +0000943
944 DenseMap<GraphT::Node, Value *> NodeValueMap;
Mehdi Aminic04fc7a2016-03-22 07:20:00 +0000945 NodeValueMap.reserve(Map.size());
Hal Finkel7529c552014-09-02 21:43:13 +0000946 for (const auto &Pair : Map)
Hal Finkel8d1590d2014-09-02 22:52:30 +0000947 NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
Hal Finkel7529c552014-09-02 21:43:13 +0000948
949 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
950 auto ValIter = NodeValueMap.find(Node);
951 assert(ValIter != NodeValueMap.end());
952 return ValIter->second;
953 };
954
955 StratifiedSetsBuilder<Value *> Builder;
956
957 SmallVector<GraphT::Node, 16> Worklist;
958 for (auto &Pair : Map) {
959 Worklist.clear();
960
961 auto *Value = Pair.first;
962 Builder.add(Value);
963 auto InitialNode = Pair.second;
964 Worklist.push_back(InitialNode);
965 while (!Worklist.empty()) {
966 auto Node = Worklist.pop_back_val();
967 auto *CurValue = findValueOrDie(Node);
George Burgess IVab03af22015-03-10 02:58:15 +0000968 if (canSkipAddingToSets(CurValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000969 continue;
970
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000971 Optional<StratifiedAttr> MaybeCurAttr = valueToAttr(CurValue);
972 if (MaybeCurAttr)
973 Builder.noteAttributes(CurValue, *MaybeCurAttr);
George Burgess IV7e5404c2016-04-05 21:40:45 +0000974
Hal Finkel7529c552014-09-02 21:43:13 +0000975 for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
976 auto Weight = std::get<0>(EdgeTuple);
977 auto Label = Weight.first;
978 auto &OtherNode = std::get<1>(EdgeTuple);
979 auto *OtherValue = findValueOrDie(OtherNode);
980
George Burgess IVab03af22015-03-10 02:58:15 +0000981 if (canSkipAddingToSets(OtherValue))
Hal Finkel7529c552014-09-02 21:43:13 +0000982 continue;
983
984 bool Added;
985 switch (directionOfEdgeType(Label)) {
986 case Level::Above:
987 Added = Builder.addAbove(CurValue, OtherValue);
988 break;
989 case Level::Below:
990 Added = Builder.addBelow(CurValue, OtherValue);
991 break;
992 case Level::Same:
993 Added = Builder.addWith(CurValue, OtherValue);
994 break;
995 }
996
George Burgess IVb54a8d622015-03-10 02:40:06 +0000997 auto Aliasing = Weight.second;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +0000998 if (MaybeCurAttr)
999 Aliasing |= *MaybeCurAttr;
1000 if (auto MaybeOtherAttr = valueToAttr(OtherValue))
1001 Aliasing |= *MaybeOtherAttr;
George Burgess IVb54a8d622015-03-10 02:40:06 +00001002 Builder.noteAttributes(CurValue, Aliasing);
1003 Builder.noteAttributes(OtherValue, Aliasing);
1004
1005 if (Added)
Hal Finkel7529c552014-09-02 21:43:13 +00001006 Worklist.push_back(OtherNode);
Hal Finkel7529c552014-09-02 21:43:13 +00001007 }
1008 }
1009 }
1010
1011 // There are times when we end up with parameters not in our graph (i.e. if
1012 // it's only used as the condition of a branch). Other bits of code depend on
1013 // things that were present during construction being present in the graph.
1014 // So, we add all present arguments here.
1015 for (auto &Arg : Fn->args()) {
George Burgess IVab03af22015-03-10 02:58:15 +00001016 if (!Builder.add(&Arg))
1017 continue;
1018
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001019 auto Attr = valueToAttr(&Arg);
1020 if (Attr.hasValue())
1021 Builder.noteAttributes(&Arg, *Attr);
Hal Finkel7529c552014-09-02 21:43:13 +00001022 }
1023
Hal Finkel85f26922014-09-03 00:06:47 +00001024 return FunctionInfo(Builder.build(), std::move(ReturnedValues));
Hal Finkel7529c552014-09-02 21:43:13 +00001025}
1026
Chandler Carruth7b560d42015-09-09 17:55:00 +00001027void CFLAAResult::scan(Function *Fn) {
Hal Finkel8d1590d2014-09-02 22:52:30 +00001028 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
Hal Finkel7529c552014-09-02 21:43:13 +00001029 (void)InsertPair;
1030 assert(InsertPair.second &&
1031 "Trying to scan a function that has already been cached");
1032
George Burgess IV6edb8912016-05-02 18:09:19 +00001033 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
1034 // may get evaluated after operator[], potentially triggering a DenseMap
1035 // resize and invalidating the reference returned by operator[]
1036 auto FunInfo = buildSetsFrom(Fn);
1037 Cache[Fn] = std::move(FunInfo);
1038
Hal Finkel7529c552014-09-02 21:43:13 +00001039 Handles.push_front(FunctionHandle(Fn, this));
1040}
1041
Chandler Carruth7b560d42015-09-09 17:55:00 +00001042void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); }
Chandler Carruth8b046a42015-08-14 02:42:20 +00001043
George Burgess IVcae581d2016-04-13 23:27:37 +00001044/// Ensures that the given function is available in the cache, and returns the
1045/// entry.
Chandler Carruth7b560d42015-09-09 17:55:00 +00001046const Optional<CFLAAResult::FunctionInfo> &
1047CFLAAResult::ensureCached(Function *Fn) {
Chandler Carruth8b046a42015-08-14 02:42:20 +00001048 auto Iter = Cache.find(Fn);
1049 if (Iter == Cache.end()) {
1050 scan(Fn);
1051 Iter = Cache.find(Fn);
1052 assert(Iter != Cache.end());
1053 assert(Iter->second.hasValue());
1054 }
1055 return Iter->second;
1056}
1057
Chandler Carruth7b560d42015-09-09 17:55:00 +00001058AliasResult CFLAAResult::query(const MemoryLocation &LocA,
1059 const MemoryLocation &LocB) {
Hal Finkel7529c552014-09-02 21:43:13 +00001060 auto *ValA = const_cast<Value *>(LocA.Ptr);
1061 auto *ValB = const_cast<Value *>(LocB.Ptr);
1062
1063 Function *Fn = nullptr;
1064 auto MaybeFnA = parentFunctionOfValue(ValA);
1065 auto MaybeFnB = parentFunctionOfValue(ValB);
1066 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
George Burgess IVcae581d2016-04-13 23:27:37 +00001067 // The only times this is known to happen are when globals + InlineAsm are
1068 // involved
George Burgess IV33305e72015-02-12 03:07:07 +00001069 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001070 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001071 }
1072
1073 if (MaybeFnA.hasValue()) {
1074 Fn = *MaybeFnA;
1075 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1076 "Interprocedural queries not supported");
1077 } else {
1078 Fn = *MaybeFnB;
1079 }
1080
1081 assert(Fn != nullptr);
1082 auto &MaybeInfo = ensureCached(Fn);
1083 assert(MaybeInfo.hasValue());
1084
1085 auto &Sets = MaybeInfo->Sets;
1086 auto MaybeA = Sets.find(ValA);
1087 if (!MaybeA.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001088 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001089
1090 auto MaybeB = Sets.find(ValB);
1091 if (!MaybeB.hasValue())
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001092 return MayAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001093
1094 auto SetA = *MaybeA;
1095 auto SetB = *MaybeB;
Hal Finkel7529c552014-09-02 21:43:13 +00001096 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1097 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
George Burgess IV33305e72015-02-12 03:07:07 +00001098
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001099 // If both values are local (meaning the corresponding set has attribute
1100 // AttrNone or AttrEscaped), then we know that CFLAA fully models them: they
1101 // may-alias each other if and only if they are in the same set
1102 // If at least one value is non-local (meaning it either is global/argument or
1103 // it comes from unknown sources like integer cast), the situation becomes a
1104 // bit more interesting. We follow three general rules described below:
1105 // - Non-local values may alias each other
1106 // - AttrNone values do not alias any non-local values
1107 // - AttrEscaped values do not alias globals/arguments, but they may alias
1108 // AttrUnknown values
1109 if (SetA.Index == SetB.Index)
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00001110 return MayAlias;
George Burgess IVa1f9a2d2016-06-07 18:35:37 +00001111 if (AttrsA.none() || AttrsB.none())
1112 return NoAlias;
1113 if (AttrsA.test(AttrUnknownIndex) || AttrsB.test(AttrUnknownIndex))
1114 return MayAlias;
1115 if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB))
1116 return MayAlias;
1117 return NoAlias;
Hal Finkel7529c552014-09-02 21:43:13 +00001118}
Mehdi Amini46a43552015-03-04 18:43:29 +00001119
Chandler Carruthb4faf132016-03-11 10:22:49 +00001120char CFLAA::PassID;
1121
Chandler Carruthb47f8012016-03-11 11:05:24 +00001122CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV18b83fe2016-06-01 18:39:54 +00001123 return CFLAAResult(AM.getResult<TargetLibraryAnalysis>(F));
Chandler Carruth7b560d42015-09-09 17:55:00 +00001124}
1125
Chandler Carruth7b560d42015-09-09 17:55:00 +00001126char CFLAAWrapperPass::ID = 0;
Chandler Carruth12884f72016-03-02 15:56:53 +00001127INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false,
1128 true)
Chandler Carruth7b560d42015-09-09 17:55:00 +00001129
1130ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); }
1131
1132CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) {
1133 initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry());
1134}
1135
George Burgess IV18b83fe2016-06-01 18:39:54 +00001136void CFLAAWrapperPass::initializePass() {
1137 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
1138 Result.reset(new CFLAAResult(TLIWP.getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +00001139}
1140
1141void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1142 AU.setPreservesAll();
George Burgess IV18b83fe2016-06-01 18:39:54 +00001143 AU.addRequired<TargetLibraryInfoWrapperPass>();
Mehdi Amini46a43552015-03-04 18:43:29 +00001144}