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