53d748d5f6e3ae7d575322de886e3d24cedc66a4
[oota-llvm.git] / lib / Analysis / CFLAliasAnalysis.cpp
1 //===- 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
17 // location.  The "actions" can be one of Dereference, Reference, Assign, or
18 // Assign.
19 //
20 // Two variables are considered as aliasing iff you can reach one value's node
21 // from the other value's node and the language formed by concatenating all of
22 // the edge labels (actions) conforms to a context-free grammar.
23 //
24 // Because this algorithm requires a graph search on each query, we execute the
25 // algorithm outlined in "Fast algorithms..." (mentioned above)
26 // in order to transform the graph into sets of variables that may alias in
27 // ~nlogn time (n = number of variables.), which makes queries take constant
28 // time.
29 //===----------------------------------------------------------------------===//
30
31 #include "StratifiedSets.h"
32 #include "llvm/ADT/BitVector.h"
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/None.h"
35 #include "llvm/ADT/Optional.h"
36 #include "llvm/Analysis/AliasAnalysis.h"
37 #include "llvm/Analysis/Passes.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/InstVisitor.h"
41 #include "llvm/IR/Instructions.h"
42 #include "llvm/IR/ValueHandle.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/Allocator.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/raw_ostream.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <forward_list>
52 #include <memory>
53 #include <tuple>
54
55 using namespace llvm;
56
57 #define DEBUG_TYPE "cfl-aa"
58
59 // Try to go from a Value* to a Function*. Never returns nullptr.
60 static Optional<Function *> parentFunctionOfValue(Value *);
61
62 // Returns possible functions called by the Inst* into the given
63 // SmallVectorImpl. Returns true if targets found, false otherwise.
64 // This is templated because InvokeInst/CallInst give us the same
65 // set of functions that we care about, and I don't like repeating
66 // myself.
67 template <typename Inst>
68 static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
69
70 // Some instructions need to have their users tracked. Instructions like
71 // `add` require you to get the users of the Instruction* itself, other
72 // instructions like `store` require you to get the users of the first
73 // operand. This function gets the "proper" value to track for each
74 // type of instruction we support.
75 static Optional<Value *> getTargetValue(Instruction *);
76
77 // There are certain instructions (i.e. FenceInst, etc.) that we ignore.
78 // This notes that we should ignore those.
79 static bool hasUsefulEdges(Instruction *);
80
81 const StratifiedIndex StratifiedLink::SetSentinel =
82     std::numeric_limits<StratifiedIndex>::max();
83
84 namespace {
85 // StratifiedInfo Attribute things.
86 typedef unsigned StratifiedAttr;
87 LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
88 LLVM_CONSTEXPR unsigned AttrAllIndex = 0;
89 LLVM_CONSTEXPR unsigned AttrGlobalIndex = 1;
90 LLVM_CONSTEXPR unsigned AttrUnknownIndex = 2;
91 LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3;
92 LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
93 LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
94
95 LLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
96 LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
97 LLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone;
98
99 // \brief StratifiedSets call for knowledge of "direction", so this is how we
100 // represent that locally.
101 enum class Level { Same, Above, Below };
102
103 // \brief Edges can be one of four "weights" -- each weight must have an inverse
104 // weight (Assign has Assign; Reference has Dereference).
105 enum class EdgeType {
106   // The weight assigned when assigning from or to a value. For example, in:
107   // %b = getelementptr %a, 0
108   // ...The relationships are %b assign %a, and %a assign %b. This used to be
109   // two edges, but having a distinction bought us nothing.
110   Assign,
111
112   // The edge used when we have an edge going from some handle to a Value.
113   // Examples of this include:
114   // %b = load %a              (%b Dereference %a)
115   // %b = extractelement %a, 0 (%a Dereference %b)
116   Dereference,
117
118   // The edge used when our edge goes from a value to a handle that may have
119   // contained it at some point. Examples:
120   // %b = load %a              (%a Reference %b)
121   // %b = extractelement %a, 0 (%b Reference %a)
122   Reference
123 };
124
125 // \brief Encodes the notion of a "use"
126 struct Edge {
127   // \brief Which value the edge is coming from
128   Value *From;
129
130   // \brief Which value the edge is pointing to
131   Value *To;
132
133   // \brief Edge weight
134   EdgeType Weight;
135
136   // \brief Whether we aliased any external values along the way that may be
137   // invisible to the analysis (i.e. landingpad for exceptions, calls for
138   // interprocedural analysis, etc.)
139   StratifiedAttrs AdditionalAttrs;
140
141   Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
142       : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
143 };
144
145 // \brief Information we have about a function and would like to keep around
146 struct FunctionInfo {
147   StratifiedSets<Value *> Sets;
148   // Lots of functions have < 4 returns. Adjust as necessary.
149   SmallVector<Value *, 4> ReturnedValues;
150
151   FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV)
152       : Sets(std::move(S)), ReturnedValues(std::move(RV)) {}
153 };
154
155 struct CFLAliasAnalysis;
156
157 struct FunctionHandle : public CallbackVH {
158   FunctionHandle(Function *Fn, CFLAliasAnalysis *CFLAA)
159       : CallbackVH(Fn), CFLAA(CFLAA) {
160     assert(Fn != nullptr);
161     assert(CFLAA != nullptr);
162   }
163
164   virtual ~FunctionHandle() {}
165
166   void deleted() override { removeSelfFromCache(); }
167   void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
168
169 private:
170   CFLAliasAnalysis *CFLAA;
171
172   void removeSelfFromCache();
173 };
174
175 struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
176 private:
177   /// \brief Cached mapping of Functions to their StratifiedSets.
178   /// If a function's sets are currently being built, it is marked
179   /// in the cache as an Optional without a value. This way, if we
180   /// have any kind of recursion, it is discernable from a function
181   /// that simply has empty sets.
182   DenseMap<Function *, Optional<FunctionInfo>> Cache;
183   std::forward_list<FunctionHandle> Handles;
184
185 public:
186   static char ID;
187
188   CFLAliasAnalysis() : ImmutablePass(ID) {
189     initializeCFLAliasAnalysisPass(*PassRegistry::getPassRegistry());
190   }
191
192   virtual ~CFLAliasAnalysis() {}
193
194   void getAnalysisUsage(AnalysisUsage &AU) const override {
195     AliasAnalysis::getAnalysisUsage(AU);
196   }
197
198   void *getAdjustedAnalysisPointer(const void *ID) override {
199     if (ID == &AliasAnalysis::ID)
200       return (AliasAnalysis *)this;
201     return this;
202   }
203
204   /// \brief Inserts the given Function into the cache.
205   void scan(Function *Fn);
206
207   void evict(Function *Fn) { Cache.erase(Fn); }
208
209   /// \brief Ensures that the given function is available in the cache.
210   /// Returns the appropriate entry from the cache.
211   const Optional<FunctionInfo> &ensureCached(Function *Fn) {
212     auto Iter = Cache.find(Fn);
213     if (Iter == Cache.end()) {
214       scan(Fn);
215       Iter = Cache.find(Fn);
216       assert(Iter != Cache.end());
217       assert(Iter->second.hasValue());
218     }
219     return Iter->second;
220   }
221
222   AliasResult query(const Location &LocA, const Location &LocB);
223
224   AliasResult alias(const Location &LocA, const Location &LocB) override {
225     if (LocA.Ptr == LocB.Ptr) {
226       if (LocA.Size == LocB.Size) {
227         return MustAlias;
228       } else {
229         return PartialAlias;
230       }
231     }
232
233     // Comparisons between global variables and other constants should be
234     // handled by BasicAA.
235     // TODO: ConstantExpr handling -- CFLAA may report NoAlias when comparing
236     // a GlobalValue and ConstantExpr, but every query needs to have at least
237     // one Value tied to a Function, and neither GlobalValues nor ConstantExprs
238     // are.
239     if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr)) {
240       return AliasAnalysis::alias(LocA, LocB);
241     }
242
243     AliasResult QueryResult = query(LocA, LocB);
244     if (QueryResult == MayAlias)
245       return AliasAnalysis::alias(LocA, LocB);
246
247     return QueryResult;
248   }
249
250   bool doInitialization(Module &M) override;
251 };
252
253 void FunctionHandle::removeSelfFromCache() {
254   assert(CFLAA != nullptr);
255   auto *Val = getValPtr();
256   CFLAA->evict(cast<Function>(Val));
257   setValPtr(nullptr);
258 }
259
260 // \brief Gets the edges our graph should have, based on an Instruction*
261 class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
262   CFLAliasAnalysis &AA;
263   SmallVectorImpl<Edge> &Output;
264
265 public:
266   GetEdgesVisitor(CFLAliasAnalysis &AA, SmallVectorImpl<Edge> &Output)
267       : AA(AA), Output(Output) {}
268
269   void visitInstruction(Instruction &) {
270     llvm_unreachable("Unsupported instruction encountered");
271   }
272
273   void visitPtrToIntInst(PtrToIntInst &Inst) {
274     auto *Ptr = Inst.getOperand(0);
275     Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
276   }
277
278   void visitIntToPtrInst(IntToPtrInst &Inst) {
279     auto *Ptr = &Inst;
280     Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
281   }
282
283   void visitCastInst(CastInst &Inst) {
284     Output.push_back(
285         Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone));
286   }
287
288   void visitBinaryOperator(BinaryOperator &Inst) {
289     auto *Op1 = Inst.getOperand(0);
290     auto *Op2 = Inst.getOperand(1);
291     Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone));
292     Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone));
293   }
294
295   void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
296     auto *Ptr = Inst.getPointerOperand();
297     auto *Val = Inst.getNewValOperand();
298     Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
299   }
300
301   void visitAtomicRMWInst(AtomicRMWInst &Inst) {
302     auto *Ptr = Inst.getPointerOperand();
303     auto *Val = Inst.getValOperand();
304     Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
305   }
306
307   void visitPHINode(PHINode &Inst) {
308     for (unsigned I = 0, E = Inst.getNumIncomingValues(); I != E; ++I) {
309       Value *Val = Inst.getIncomingValue(I);
310       Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
311     }
312   }
313
314   void visitGetElementPtrInst(GetElementPtrInst &Inst) {
315     auto *Op = Inst.getPointerOperand();
316     Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone));
317     for (auto I = Inst.idx_begin(), E = Inst.idx_end(); I != E; ++I)
318       Output.push_back(Edge(&Inst, *I, EdgeType::Assign, AttrNone));
319   }
320
321   void visitSelectInst(SelectInst &Inst) {
322     // Condition is not processed here (The actual statement producing
323     // the condition result is processed elsewhere). For select, the
324     // condition is evaluated, but not loaded, stored, or assigned
325     // simply as a result of being the condition of a select.
326
327     auto *TrueVal = Inst.getTrueValue();
328     Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
329     auto *FalseVal = Inst.getFalseValue();
330     Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
331   }
332
333   void visitAllocaInst(AllocaInst &) {}
334
335   void visitLoadInst(LoadInst &Inst) {
336     auto *Ptr = Inst.getPointerOperand();
337     auto *Val = &Inst;
338     Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
339   }
340
341   void visitStoreInst(StoreInst &Inst) {
342     auto *Ptr = Inst.getPointerOperand();
343     auto *Val = Inst.getValueOperand();
344     Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
345   }
346
347   void visitVAArgInst(VAArgInst &Inst) {
348     // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
349     // two things:
350     //  1. Loads a value from *((T*)*Ptr).
351     //  2. Increments (stores to) *Ptr by some target-specific amount.
352     // For now, we'll handle this like a landingpad instruction (by placing the
353     // result in its own group, and having that group alias externals).
354     auto *Val = &Inst;
355     Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll));
356   }
357
358   static bool isFunctionExternal(Function *Fn) {
359     return Fn->isDeclaration() || !Fn->hasLocalLinkage();
360   }
361
362   // Gets whether the sets at Index1 above, below, or equal to the sets at
363   // Index2. Returns None if they are not in the same set chain.
364   static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
365                                           StratifiedIndex Index1,
366                                           StratifiedIndex Index2) {
367     if (Index1 == Index2)
368       return Level::Same;
369
370     const auto *Current = &Sets.getLink(Index1);
371     while (Current->hasBelow()) {
372       if (Current->Below == Index2)
373         return Level::Below;
374       Current = &Sets.getLink(Current->Below);
375     }
376
377     Current = &Sets.getLink(Index1);
378     while (Current->hasAbove()) {
379       if (Current->Above == Index2)
380         return Level::Above;
381       Current = &Sets.getLink(Current->Above);
382     }
383
384     return NoneType();
385   }
386
387   bool
388   tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
389                              Value *FuncValue,
390                              const iterator_range<User::op_iterator> &Args) {
391     const unsigned ExpectedMaxArgs = 8;
392     const unsigned MaxSupportedArgs = 50;
393     assert(Fns.size() > 0);
394
395     // I put this here to give us an upper bound on time taken by IPA. Is it
396     // really (realistically) needed? Keep in mind that we do have an n^2 algo.
397     if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs)
398       return false;
399
400     // Exit early if we'll fail anyway
401     for (auto *Fn : Fns) {
402       if (isFunctionExternal(Fn) || Fn->isVarArg())
403         return false;
404       auto &MaybeInfo = AA.ensureCached(Fn);
405       if (!MaybeInfo.hasValue())
406         return false;
407     }
408
409     SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
410     SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
411     for (auto *Fn : Fns) {
412       auto &Info = *AA.ensureCached(Fn);
413       auto &Sets = Info.Sets;
414       auto &RetVals = Info.ReturnedValues;
415
416       Parameters.clear();
417       for (auto &Param : Fn->args()) {
418         auto MaybeInfo = Sets.find(&Param);
419         // Did a new parameter somehow get added to the function/slip by?
420         if (!MaybeInfo.hasValue())
421           return false;
422         Parameters.push_back(*MaybeInfo);
423       }
424
425       // Adding an edge from argument -> return value for each parameter that
426       // may alias the return value
427       for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
428         auto &ParamInfo = Parameters[I];
429         auto &ArgVal = Arguments[I];
430         bool AddEdge = false;
431         StratifiedAttrs Externals;
432         for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
433           auto MaybeInfo = Sets.find(RetVals[X]);
434           if (!MaybeInfo.hasValue())
435             return false;
436
437           auto &RetInfo = *MaybeInfo;
438           auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
439           auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
440           auto MaybeRelation =
441               getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
442           if (MaybeRelation.hasValue()) {
443             AddEdge = true;
444             Externals |= RetAttrs | ParamAttrs;
445           }
446         }
447         if (AddEdge)
448           Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign,
449                                 StratifiedAttrs().flip()));
450       }
451
452       if (Parameters.size() != Arguments.size())
453         return false;
454
455       // Adding edges between arguments for arguments that may end up aliasing
456       // each other. This is necessary for functions such as
457       // void foo(int** a, int** b) { *a = *b; }
458       // (Technically, the proper sets for this would be those below
459       // Arguments[I] and Arguments[X], but our algorithm will produce
460       // extremely similar, and equally correct, results either way)
461       for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
462         auto &MainVal = Arguments[I];
463         auto &MainInfo = Parameters[I];
464         auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
465         for (unsigned X = I + 1; X != E; ++X) {
466           auto &SubInfo = Parameters[X];
467           auto &SubVal = Arguments[X];
468           auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
469           auto MaybeRelation =
470               getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
471
472           if (!MaybeRelation.hasValue())
473             continue;
474
475           auto NewAttrs = SubAttrs | MainAttrs;
476           Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
477         }
478       }
479     }
480     return true;
481   }
482
483   template <typename InstT> void visitCallLikeInst(InstT &Inst) {
484     SmallVector<Function *, 4> Targets;
485     if (getPossibleTargets(&Inst, Targets)) {
486       if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
487         return;
488       // Cleanup from interprocedural analysis
489       Output.clear();
490     }
491
492     for (Value *V : Inst.arg_operands())
493       Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll));
494   }
495
496   void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
497
498   void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
499
500   // Because vectors/aggregates are immutable and unaddressable,
501   // there's nothing we can do to coax a value out of them, other
502   // than calling Extract{Element,Value}. We can effectively treat
503   // them as pointers to arbitrary memory locations we can store in
504   // and load from.
505   void visitExtractElementInst(ExtractElementInst &Inst) {
506     auto *Ptr = Inst.getVectorOperand();
507     auto *Val = &Inst;
508     Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
509   }
510
511   void visitInsertElementInst(InsertElementInst &Inst) {
512     auto *Vec = Inst.getOperand(0);
513     auto *Val = Inst.getOperand(1);
514     Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
515     Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
516   }
517
518   void visitLandingPadInst(LandingPadInst &Inst) {
519     // Exceptions come from "nowhere", from our analysis' perspective.
520     // So we place the instruction its own group, noting that said group may
521     // alias externals
522     Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
523   }
524
525   void visitInsertValueInst(InsertValueInst &Inst) {
526     auto *Agg = Inst.getOperand(0);
527     auto *Val = Inst.getOperand(1);
528     Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
529     Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
530   }
531
532   void visitExtractValueInst(ExtractValueInst &Inst) {
533     auto *Ptr = Inst.getAggregateOperand();
534     Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
535   }
536
537   void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
538     auto *From1 = Inst.getOperand(0);
539     auto *From2 = Inst.getOperand(1);
540     Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
541     Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
542   }
543 };
544
545 // For a given instruction, we need to know which Value* to get the
546 // users of in order to build our graph. In some cases (i.e. add),
547 // we simply need the Instruction*. In other cases (i.e. store),
548 // finding the users of the Instruction* is useless; we need to find
549 // the users of the first operand. This handles determining which
550 // value to follow for us.
551 //
552 // Note: we *need* to keep this in sync with GetEdgesVisitor. Add
553 // something to GetEdgesVisitor, add it here -- remove something from
554 // GetEdgesVisitor, remove it here.
555 class GetTargetValueVisitor
556     : public InstVisitor<GetTargetValueVisitor, Value *> {
557 public:
558   Value *visitInstruction(Instruction &Inst) { return &Inst; }
559
560   Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
561
562   Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
563     return Inst.getPointerOperand();
564   }
565
566   Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
567     return Inst.getPointerOperand();
568   }
569
570   Value *visitInsertElementInst(InsertElementInst &Inst) {
571     return Inst.getOperand(0);
572   }
573
574   Value *visitInsertValueInst(InsertValueInst &Inst) {
575     return Inst.getAggregateOperand();
576   }
577 };
578
579 // Set building requires a weighted bidirectional graph.
580 template <typename EdgeTypeT> class WeightedBidirectionalGraph {
581 public:
582   typedef std::size_t Node;
583
584 private:
585   const static Node StartNode = Node(0);
586
587   struct Edge {
588     EdgeTypeT Weight;
589     Node Other;
590
591     Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {}
592
593     bool operator==(const Edge &E) const {
594       return Weight == E.Weight && Other == E.Other;
595     }
596
597     bool operator!=(const Edge &E) const { return !operator==(E); }
598   };
599
600   struct NodeImpl {
601     std::vector<Edge> Edges;
602   };
603
604   std::vector<NodeImpl> NodeImpls;
605
606   bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
607
608   const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
609   NodeImpl &getNode(Node N) { return NodeImpls[N]; }
610
611 public:
612   // ----- Various Edge iterators for the graph ----- //
613
614   // \brief Iterator for edges. Because this graph is bidirected, we don't
615   // allow modificaiton of the edges using this iterator. Additionally, the
616   // iterator becomes invalid if you add edges to or from the node you're
617   // getting the edges of.
618   struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
619                                              std::tuple<EdgeTypeT, Node *>> {
620     EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
621         : Current(Iter) {}
622
623     EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
624
625     EdgeIterator &operator++() {
626       ++Current;
627       return *this;
628     }
629
630     EdgeIterator operator++(int) {
631       EdgeIterator Copy(Current);
632       operator++();
633       return Copy;
634     }
635
636     std::tuple<EdgeTypeT, Node> &operator*() {
637       Store = std::make_tuple(Current->Weight, Current->Other);
638       return Store;
639     }
640
641     bool operator==(const EdgeIterator &Other) const {
642       return Current == Other.Current;
643     }
644
645     bool operator!=(const EdgeIterator &Other) const {
646       return !operator==(Other);
647     }
648
649   private:
650     typename std::vector<Edge>::const_iterator Current;
651     std::tuple<EdgeTypeT, Node> Store;
652   };
653
654   // Wrapper for EdgeIterator with begin()/end() calls.
655   struct EdgeIterable {
656     EdgeIterable(const std::vector<Edge> &Edges)
657         : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
658
659     EdgeIterator begin() { return EdgeIterator(BeginIter); }
660
661     EdgeIterator end() { return EdgeIterator(EndIter); }
662
663   private:
664     typename std::vector<Edge>::const_iterator BeginIter;
665     typename std::vector<Edge>::const_iterator EndIter;
666   };
667
668   // ----- Actual graph-related things ----- //
669
670   WeightedBidirectionalGraph() {}
671
672   WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
673       : NodeImpls(std::move(Other.NodeImpls)) {}
674
675   WeightedBidirectionalGraph<EdgeTypeT> &
676   operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
677     NodeImpls = std::move(Other.NodeImpls);
678     return *this;
679   }
680
681   Node addNode() {
682     auto Index = NodeImpls.size();
683     auto NewNode = Node(Index);
684     NodeImpls.push_back(NodeImpl());
685     return NewNode;
686   }
687
688   void addEdge(Node From, Node To, const EdgeTypeT &Weight,
689                const EdgeTypeT &ReverseWeight) {
690     assert(inbounds(From));
691     assert(inbounds(To));
692     auto &FromNode = getNode(From);
693     auto &ToNode = getNode(To);
694     FromNode.Edges.push_back(Edge(Weight, To));
695     ToNode.Edges.push_back(Edge(ReverseWeight, From));
696   }
697
698   EdgeIterable edgesFor(const Node &N) const {
699     const auto &Node = getNode(N);
700     return EdgeIterable(Node.Edges);
701   }
702
703   bool empty() const { return NodeImpls.empty(); }
704   std::size_t size() const { return NodeImpls.size(); }
705
706   // \brief Gets an arbitrary node in the graph as a starting point for
707   // traversal.
708   Node getEntryNode() {
709     assert(inbounds(StartNode));
710     return StartNode;
711   }
712 };
713
714 typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
715 typedef DenseMap<Value *, GraphT::Node> NodeMapT;
716 }
717
718 // -- Setting up/registering CFLAA pass -- //
719 char CFLAliasAnalysis::ID = 0;
720
721 INITIALIZE_AG_PASS(CFLAliasAnalysis, AliasAnalysis, "cfl-aa",
722                    "CFL-Based AA implementation", false, true, false)
723
724 ImmutablePass *llvm::createCFLAliasAnalysisPass() {
725   return new CFLAliasAnalysis();
726 }
727
728 //===----------------------------------------------------------------------===//
729 // Function declarations that require types defined in the namespace above
730 //===----------------------------------------------------------------------===//
731
732 // Given an argument number, returns the appropriate Attr index to set.
733 static StratifiedAttr argNumberToAttrIndex(StratifiedAttr);
734
735 // Given a Value, potentially return which AttrIndex it maps to.
736 static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
737
738 // Gets the inverse of a given EdgeType.
739 static EdgeType flipWeight(EdgeType);
740
741 // Gets edges of the given Instruction*, writing them to the SmallVector*.
742 static void argsToEdges(CFLAliasAnalysis &, Instruction *,
743                         SmallVectorImpl<Edge> &);
744
745 // Gets the "Level" that one should travel in StratifiedSets
746 // given an EdgeType.
747 static Level directionOfEdgeType(EdgeType);
748
749 // Builds the graph needed for constructing the StratifiedSets for the
750 // given function
751 static void buildGraphFrom(CFLAliasAnalysis &, Function *,
752                            SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
753
754 // Gets the edges of a ConstantExpr as if it was an Instruction. This
755 // function also acts on any nested ConstantExprs, adding the edges
756 // of those to the given SmallVector as well.
757 static void constexprToEdges(CFLAliasAnalysis &, ConstantExpr &,
758                              SmallVectorImpl<Edge> &);
759
760 // Given an Instruction, this will add it to the graph, along with any
761 // Instructions that are potentially only available from said Instruction
762 // For example, given the following line:
763 //   %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
764 // addInstructionToGraph would add both the `load` and `getelementptr`
765 // instructions to the graph appropriately.
766 static void addInstructionToGraph(CFLAliasAnalysis &, Instruction &,
767                                   SmallVectorImpl<Value *> &, NodeMapT &,
768                                   GraphT &);
769
770 // Notes whether it would be pointless to add the given Value to our sets.
771 static bool canSkipAddingToSets(Value *Val);
772
773 // Builds the graph + StratifiedSets for a function.
774 static FunctionInfo buildSetsFrom(CFLAliasAnalysis &, Function *);
775
776 static Optional<Function *> parentFunctionOfValue(Value *Val) {
777   if (auto *Inst = dyn_cast<Instruction>(Val)) {
778     auto *Bb = Inst->getParent();
779     return Bb->getParent();
780   }
781
782   if (auto *Arg = dyn_cast<Argument>(Val))
783     return Arg->getParent();
784   return NoneType();
785 }
786
787 template <typename Inst>
788 static bool getPossibleTargets(Inst *Call,
789                                SmallVectorImpl<Function *> &Output) {
790   if (auto *Fn = Call->getCalledFunction()) {
791     Output.push_back(Fn);
792     return true;
793   }
794
795   // TODO: If the call is indirect, we might be able to enumerate all potential
796   // targets of the call and return them, rather than just failing.
797   return false;
798 }
799
800 static Optional<Value *> getTargetValue(Instruction *Inst) {
801   GetTargetValueVisitor V;
802   return V.visit(Inst);
803 }
804
805 static bool hasUsefulEdges(Instruction *Inst) {
806   bool IsNonInvokeTerminator =
807       isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
808   return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
809 }
810
811 static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
812   if (isa<GlobalValue>(Val))
813     return AttrGlobalIndex;
814
815   if (auto *Arg = dyn_cast<Argument>(Val))
816     // Only pointer arguments should have the argument attribute,
817     // because things can't escape through scalars without us seeing a
818     // cast, and thus, interaction with them doesn't matter.
819     if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy())
820       return argNumberToAttrIndex(Arg->getArgNo());
821   return NoneType();
822 }
823
824 static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
825   if (ArgNum >= AttrMaxNumArgs)
826     return AttrAllIndex;
827   return ArgNum + AttrFirstArgIndex;
828 }
829
830 static EdgeType flipWeight(EdgeType Initial) {
831   switch (Initial) {
832   case EdgeType::Assign:
833     return EdgeType::Assign;
834   case EdgeType::Dereference:
835     return EdgeType::Reference;
836   case EdgeType::Reference:
837     return EdgeType::Dereference;
838   }
839   llvm_unreachable("Incomplete coverage of EdgeType enum");
840 }
841
842 static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst,
843                         SmallVectorImpl<Edge> &Output) {
844   assert(hasUsefulEdges(Inst) &&
845          "Expected instructions to have 'useful' edges");
846   GetEdgesVisitor v(Analysis, Output);
847   v.visit(Inst);
848 }
849
850 static Level directionOfEdgeType(EdgeType Weight) {
851   switch (Weight) {
852   case EdgeType::Reference:
853     return Level::Above;
854   case EdgeType::Dereference:
855     return Level::Below;
856   case EdgeType::Assign:
857     return Level::Same;
858   }
859   llvm_unreachable("Incomplete switch coverage");
860 }
861
862 static void constexprToEdges(CFLAliasAnalysis &Analysis,
863                              ConstantExpr &CExprToCollapse,
864                              SmallVectorImpl<Edge> &Results) {
865   SmallVector<ConstantExpr *, 4> Worklist;
866   Worklist.push_back(&CExprToCollapse);
867
868   SmallVector<Edge, 8> ConstexprEdges;
869   while (!Worklist.empty()) {
870     auto *CExpr = Worklist.pop_back_val();
871     std::unique_ptr<Instruction> Inst(CExpr->getAsInstruction());
872
873     if (!hasUsefulEdges(Inst.get()))
874       continue;
875
876     ConstexprEdges.clear();
877     argsToEdges(Analysis, Inst.get(), ConstexprEdges);
878     for (auto &Edge : ConstexprEdges) {
879       if (Edge.From == Inst.get())
880         Edge.From = CExpr;
881       else if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From))
882         Worklist.push_back(Nested);
883
884       if (Edge.To == Inst.get())
885         Edge.To = CExpr;
886       else if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
887         Worklist.push_back(Nested);
888     }
889
890     Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
891   }
892 }
893
894 static void addInstructionToGraph(CFLAliasAnalysis &Analysis, Instruction &Inst,
895                                   SmallVectorImpl<Value *> &ReturnedValues,
896                                   NodeMapT &Map, GraphT &Graph) {
897   const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
898     auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
899     auto &Iter = Pair.first;
900     if (Pair.second) {
901       auto NewNode = Graph.addNode();
902       Iter->second = NewNode;
903     }
904     return Iter->second;
905   };
906
907   // We don't want the edges of most "return" instructions, but we *do* want
908   // to know what can be returned.
909   if (isa<ReturnInst>(&Inst))
910     ReturnedValues.push_back(&Inst);
911
912   if (!hasUsefulEdges(&Inst))
913     return;
914
915   SmallVector<Edge, 8> Edges;
916   argsToEdges(Analysis, &Inst, Edges);
917
918   // In the case of an unused alloca (or similar), edges may be empty. Note
919   // that it exists so we can potentially answer NoAlias.
920   if (Edges.empty()) {
921     auto MaybeVal = getTargetValue(&Inst);
922     assert(MaybeVal.hasValue());
923     auto *Target = *MaybeVal;
924     findOrInsertNode(Target);
925     return;
926   }
927
928   const auto addEdgeToGraph = [&Graph, &findOrInsertNode](const Edge &E) {
929     auto To = findOrInsertNode(E.To);
930     auto From = findOrInsertNode(E.From);
931     auto FlippedWeight = flipWeight(E.Weight);
932     auto Attrs = E.AdditionalAttrs;
933     Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
934                   std::make_pair(FlippedWeight, Attrs));
935   };
936
937   SmallVector<ConstantExpr *, 4> ConstantExprs;
938   for (const Edge &E : Edges) {
939     addEdgeToGraph(E);
940     if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To))
941       ConstantExprs.push_back(Constexpr);
942     if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From))
943       ConstantExprs.push_back(Constexpr);
944   }
945
946   for (ConstantExpr *CE : ConstantExprs) {
947     Edges.clear();
948     constexprToEdges(Analysis, *CE, Edges);
949     std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
950   }
951 }
952
953 // Aside: We may remove graph construction entirely, because it doesn't really
954 // buy us much that we don't already have. I'd like to add interprocedural
955 // analysis prior to this however, in case that somehow requires the graph
956 // produced by this for efficient execution
957 static void buildGraphFrom(CFLAliasAnalysis &Analysis, Function *Fn,
958                            SmallVectorImpl<Value *> &ReturnedValues,
959                            NodeMapT &Map, GraphT &Graph) {
960   for (auto &Bb : Fn->getBasicBlockList())
961     for (auto &Inst : Bb.getInstList())
962       addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph);
963 }
964
965 static bool canSkipAddingToSets(Value *Val) {
966   // Constants can share instances, which may falsely unify multiple
967   // sets, e.g. in
968   // store i32* null, i32** %ptr1
969   // store i32* null, i32** %ptr2
970   // clearly ptr1 and ptr2 should not be unified into the same set, so
971   // we should filter out the (potentially shared) instance to
972   // i32* null.
973   if (isa<Constant>(Val)) {
974     bool Container = isa<ConstantVector>(Val) || isa<ConstantArray>(Val) ||
975                      isa<ConstantStruct>(Val);
976     // TODO: Because all of these things are constant, we can determine whether
977     // the data is *actually* mutable at graph building time. This will probably
978     // come for free/cheap with offset awareness.
979     bool CanStoreMutableData =
980         isa<GlobalValue>(Val) || isa<ConstantExpr>(Val) || Container;
981     return !CanStoreMutableData;
982   }
983
984   return false;
985 }
986
987 static FunctionInfo buildSetsFrom(CFLAliasAnalysis &Analysis, Function *Fn) {
988   NodeMapT Map;
989   GraphT Graph;
990   SmallVector<Value *, 4> ReturnedValues;
991
992   buildGraphFrom(Analysis, Fn, ReturnedValues, Map, Graph);
993
994   DenseMap<GraphT::Node, Value *> NodeValueMap;
995   NodeValueMap.resize(Map.size());
996   for (const auto &Pair : Map)
997     NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
998
999   const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
1000     auto ValIter = NodeValueMap.find(Node);
1001     assert(ValIter != NodeValueMap.end());
1002     return ValIter->second;
1003   };
1004
1005   StratifiedSetsBuilder<Value *> Builder;
1006
1007   SmallVector<GraphT::Node, 16> Worklist;
1008   for (auto &Pair : Map) {
1009     Worklist.clear();
1010
1011     auto *Value = Pair.first;
1012     Builder.add(Value);
1013     auto InitialNode = Pair.second;
1014     Worklist.push_back(InitialNode);
1015     while (!Worklist.empty()) {
1016       auto Node = Worklist.pop_back_val();
1017       auto *CurValue = findValueOrDie(Node);
1018       if (canSkipAddingToSets(CurValue))
1019         continue;
1020
1021       for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
1022         auto Weight = std::get<0>(EdgeTuple);
1023         auto Label = Weight.first;
1024         auto &OtherNode = std::get<1>(EdgeTuple);
1025         auto *OtherValue = findValueOrDie(OtherNode);
1026
1027         if (canSkipAddingToSets(OtherValue))
1028           continue;
1029
1030         bool Added;
1031         switch (directionOfEdgeType(Label)) {
1032         case Level::Above:
1033           Added = Builder.addAbove(CurValue, OtherValue);
1034           break;
1035         case Level::Below:
1036           Added = Builder.addBelow(CurValue, OtherValue);
1037           break;
1038         case Level::Same:
1039           Added = Builder.addWith(CurValue, OtherValue);
1040           break;
1041         }
1042
1043         auto Aliasing = Weight.second;
1044         if (auto MaybeCurIndex = valueToAttrIndex(CurValue))
1045           Aliasing.set(*MaybeCurIndex);
1046         if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
1047           Aliasing.set(*MaybeOtherIndex);
1048         Builder.noteAttributes(CurValue, Aliasing);
1049         Builder.noteAttributes(OtherValue, Aliasing);
1050
1051         if (Added)
1052           Worklist.push_back(OtherNode);
1053       }
1054     }
1055   }
1056
1057   // There are times when we end up with parameters not in our graph (i.e. if
1058   // it's only used as the condition of a branch). Other bits of code depend on
1059   // things that were present during construction being present in the graph.
1060   // So, we add all present arguments here.
1061   for (auto &Arg : Fn->args()) {
1062     if (!Builder.add(&Arg))
1063       continue;
1064
1065     auto Attrs = valueToAttrIndex(&Arg);
1066     if (Attrs.hasValue())
1067       Builder.noteAttributes(&Arg, *Attrs);
1068   }
1069
1070   return FunctionInfo(Builder.build(), std::move(ReturnedValues));
1071 }
1072
1073 void CFLAliasAnalysis::scan(Function *Fn) {
1074   auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
1075   (void)InsertPair;
1076   assert(InsertPair.second &&
1077          "Trying to scan a function that has already been cached");
1078
1079   FunctionInfo Info(buildSetsFrom(*this, Fn));
1080   Cache[Fn] = std::move(Info);
1081   Handles.push_front(FunctionHandle(Fn, this));
1082 }
1083
1084 AliasAnalysis::AliasResult
1085 CFLAliasAnalysis::query(const AliasAnalysis::Location &LocA,
1086                         const AliasAnalysis::Location &LocB) {
1087   auto *ValA = const_cast<Value *>(LocA.Ptr);
1088   auto *ValB = const_cast<Value *>(LocB.Ptr);
1089
1090   Function *Fn = nullptr;
1091   auto MaybeFnA = parentFunctionOfValue(ValA);
1092   auto MaybeFnB = parentFunctionOfValue(ValB);
1093   if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
1094     // The only times this is known to happen are when globals + InlineAsm
1095     // are involved
1096     DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
1097     return AliasAnalysis::MayAlias;
1098   }
1099
1100   if (MaybeFnA.hasValue()) {
1101     Fn = *MaybeFnA;
1102     assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1103            "Interprocedural queries not supported");
1104   } else {
1105     Fn = *MaybeFnB;
1106   }
1107
1108   assert(Fn != nullptr);
1109   auto &MaybeInfo = ensureCached(Fn);
1110   assert(MaybeInfo.hasValue());
1111
1112   auto &Sets = MaybeInfo->Sets;
1113   auto MaybeA = Sets.find(ValA);
1114   if (!MaybeA.hasValue())
1115     return AliasAnalysis::MayAlias;
1116
1117   auto MaybeB = Sets.find(ValB);
1118   if (!MaybeB.hasValue())
1119     return AliasAnalysis::MayAlias;
1120
1121   auto SetA = *MaybeA;
1122   auto SetB = *MaybeB;
1123   auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1124   auto AttrsB = Sets.getLink(SetB.Index).Attrs;
1125
1126   // Stratified set attributes are used as markets to signify whether a member
1127   // of a StratifiedSet (or a member of a set above the current set) has
1128   // interacted with either arguments or globals. "Interacted with" meaning
1129   // its value may be different depending on the value of an argument or
1130   // global. The thought behind this is that, because arguments and globals
1131   // may alias each other, if AttrsA and AttrsB have touched args/globals,
1132   // we must conservatively say that they alias. However, if at least one of
1133   // the sets has no values that could legally be altered by changing the value
1134   // of an argument or global, then we don't have to be as conservative.
1135   if (AttrsA.any() && AttrsB.any())
1136     return AliasAnalysis::MayAlias;
1137
1138   // We currently unify things even if the accesses to them may not be in
1139   // bounds, so we can't return partial alias here because we don't
1140   // know whether the pointer is really within the object or not.
1141   // IE Given an out of bounds GEP and an alloca'd pointer, we may
1142   // unify the two. We can't return partial alias for this case.
1143   // Since we do not currently track enough information to
1144   // differentiate
1145
1146   if (SetA.Index == SetB.Index)
1147     return AliasAnalysis::MayAlias;
1148
1149   return AliasAnalysis::NoAlias;
1150 }
1151
1152 bool CFLAliasAnalysis::doInitialization(Module &M) {
1153   InitializeAliasAnalysis(this, &M.getDataLayout());
1154   return true;
1155 }