CFLAliasAnalysis.cpp revision 277323
1212795Sdim//===- CFLAliasAnalysis.cpp - CFL-Based Alias Analysis Implementation ------==//
2212795Sdim//
3212795Sdim//                     The LLVM Compiler Infrastructure
4212795Sdim//
5212795Sdim// This file is distributed under the University of Illinois Open Source
6212795Sdim// License. See LICENSE.TXT for details.
7212795Sdim//
8212795Sdim//===----------------------------------------------------------------------===//
9212795Sdim//
10212795Sdim// This file implements a CFL-based context-insensitive alias analysis
11212795Sdim// algorithm. It does not depend on types. The algorithm is a mixture of the one
12212795Sdim// described in "Demand-driven alias analysis for C" by Xin Zheng and Radu
13212795Sdim// Rugina, and "Fast algorithms for Dyck-CFL-reachability with applications to
14212795Sdim// Alias Analysis" by Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the
15212795Sdim// papers, we build a graph of the uses of a variable, where each node is a
16212795Sdim// memory location, and each edge is an action that happened on that memory
17212795Sdim// location.  The "actions" can be one of Dereference, Reference, Assign, or
18239462Sdim// Assign.
19212795Sdim//
20212795Sdim// Two variables are considered as aliasing iff you can reach one value's node
21212795Sdim// from the other value's node and the language formed by concatenating all of
22212795Sdim// the edge labels (actions) conforms to a context-free grammar.
23212795Sdim//
24212795Sdim// Because this algorithm requires a graph search on each query, we execute the
25212795Sdim// algorithm outlined in "Fast algorithms..." (mentioned above)
26234353Sdim// in order to transform the graph into sets of variables that may alias in
27212795Sdim// ~nlogn time (n = number of variables.), which makes queries take constant
28212795Sdim// time.
29212795Sdim//===----------------------------------------------------------------------===//
30212795Sdim
31212795Sdim#include "StratifiedSets.h"
32212795Sdim#include "llvm/ADT/BitVector.h"
33212795Sdim#include "llvm/ADT/DenseMap.h"
34212795Sdim#include "llvm/ADT/None.h"
35212795Sdim#include "llvm/ADT/Optional.h"
36212795Sdim#include "llvm/Analysis/AliasAnalysis.h"
37212795Sdim#include "llvm/Analysis/Passes.h"
38212795Sdim#include "llvm/IR/Constants.h"
39212795Sdim#include "llvm/IR/Function.h"
40212795Sdim#include "llvm/IR/InstVisitor.h"
41212795Sdim#include "llvm/IR/Instructions.h"
42212795Sdim#include "llvm/IR/ValueHandle.h"
43212795Sdim#include "llvm/Pass.h"
44212795Sdim#include "llvm/Support/Allocator.h"
45212795Sdim#include "llvm/Support/Compiler.h"
46212795Sdim#include "llvm/Support/ErrorHandling.h"
47212795Sdim#include <algorithm>
48212795Sdim#include <cassert>
49212795Sdim#include <forward_list>
50212795Sdim#include <tuple>
51212795Sdim
52212795Sdimusing namespace llvm;
53212795Sdim
54212795Sdim// Try to go from a Value* to a Function*. Never returns nullptr.
55212795Sdimstatic Optional<Function *> parentFunctionOfValue(Value *);
56212795Sdim
57212795Sdim// Returns possible functions called by the Inst* into the given
58212795Sdim// SmallVectorImpl. Returns true if targets found, false otherwise.
59212795Sdim// This is templated because InvokeInst/CallInst give us the same
60212795Sdim// set of functions that we care about, and I don't like repeating
61212795Sdim// myself.
62212795Sdimtemplate <typename Inst>
63212795Sdimstatic bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
64212795Sdim
65212795Sdim// Some instructions need to have their users tracked. Instructions like
66212795Sdim// `add` require you to get the users of the Instruction* itself, other
67212795Sdim// instructions like `store` require you to get the users of the first
68212795Sdim// operand. This function gets the "proper" value to track for each
69234353Sdim// type of instruction we support.
70234353Sdimstatic Optional<Value *> getTargetValue(Instruction *);
71212795Sdim
72212795Sdim// There are certain instructions (i.e. FenceInst, etc.) that we ignore.
73212795Sdim// This notes that we should ignore those.
74212795Sdimstatic bool hasUsefulEdges(Instruction *);
75212795Sdim
76212795Sdimconst StratifiedIndex StratifiedLink::SetSentinel =
77212795Sdim  std::numeric_limits<StratifiedIndex>::max();
78212795Sdim
79212795Sdimnamespace {
80212795Sdim// StratifiedInfo Attribute things.
81212795Sdimtypedef unsigned StratifiedAttr;
82212795SdimLLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
83212795SdimLLVM_CONSTEXPR unsigned AttrAllIndex = 0;
84212795SdimLLVM_CONSTEXPR unsigned AttrGlobalIndex = 1;
85212795SdimLLVM_CONSTEXPR unsigned AttrFirstArgIndex = 2;
86212795SdimLLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
87212795SdimLLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
88212795Sdim
89212795SdimLLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
90212795SdimLLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone;
91212795Sdim
92212795Sdim// \brief StratifiedSets call for knowledge of "direction", so this is how we
93212795Sdim// represent that locally.
94212795Sdimenum class Level { Same, Above, Below };
95212795Sdim
96212795Sdim// \brief Edges can be one of four "weights" -- each weight must have an inverse
97212795Sdim// weight (Assign has Assign; Reference has Dereference).
98212795Sdimenum class EdgeType {
99212795Sdim  // The weight assigned when assigning from or to a value. For example, in:
100212795Sdim  // %b = getelementptr %a, 0
101212795Sdim  // ...The relationships are %b assign %a, and %a assign %b. This used to be
102212795Sdim  // two edges, but having a distinction bought us nothing.
103212795Sdim  Assign,
104212795Sdim
105212795Sdim  // The edge used when we have an edge going from some handle to a Value.
106212795Sdim  // Examples of this include:
107212795Sdim  // %b = load %a              (%b Dereference %a)
108212795Sdim  // %b = extractelement %a, 0 (%a Dereference %b)
109212795Sdim  Dereference,
110212795Sdim
111212795Sdim  // The edge used when our edge goes from a value to a handle that may have
112212795Sdim  // contained it at some point. Examples:
113212795Sdim  // %b = load %a              (%a Reference %b)
114212795Sdim  // %b = extractelement %a, 0 (%b Reference %a)
115212795Sdim  Reference
116212795Sdim};
117212795Sdim
118212795Sdim// \brief Encodes the notion of a "use"
119212795Sdimstruct Edge {
120212795Sdim  // \brief Which value the edge is coming from
121212795Sdim  Value *From;
122212795Sdim
123212795Sdim  // \brief Which value the edge is pointing to
124212795Sdim  Value *To;
125212795Sdim
126212795Sdim  // \brief Edge weight
127212795Sdim  EdgeType Weight;
128212795Sdim
129212795Sdim  // \brief Whether we aliased any external values along the way that may be
130212795Sdim  // invisible to the analysis (i.e. landingpad for exceptions, calls for
131212795Sdim  // interprocedural analysis, etc.)
132212795Sdim  StratifiedAttrs AdditionalAttrs;
133212795Sdim
134212795Sdim  Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
135234353Sdim      : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
136212795Sdim};
137212795Sdim
138212795Sdim// \brief Information we have about a function and would like to keep around
139212795Sdimstruct FunctionInfo {
140212795Sdim  StratifiedSets<Value *> Sets;
141212795Sdim  // Lots of functions have < 4 returns. Adjust as necessary.
142212795Sdim  SmallVector<Value *, 4> ReturnedValues;
143212795Sdim
144212795Sdim  FunctionInfo(StratifiedSets<Value *> &&S,
145212795Sdim               SmallVector<Value *, 4> &&RV)
146212795Sdim    : Sets(std::move(S)), ReturnedValues(std::move(RV)) {}
147212795Sdim};
148212795Sdim
149212795Sdimstruct CFLAliasAnalysis;
150212795Sdim
151212795Sdimstruct FunctionHandle : public CallbackVH {
152212795Sdim  FunctionHandle(Function *Fn, CFLAliasAnalysis *CFLAA)
153212795Sdim      : CallbackVH(Fn), CFLAA(CFLAA) {
154212795Sdim    assert(Fn != nullptr);
155212795Sdim    assert(CFLAA != nullptr);
156212795Sdim  }
157212795Sdim
158212795Sdim  virtual ~FunctionHandle() {}
159212795Sdim
160212795Sdim  void deleted() override { removeSelfFromCache(); }
161212795Sdim  void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
162212795Sdim
163212795Sdimprivate:
164212795Sdim  CFLAliasAnalysis *CFLAA;
165212795Sdim
166212795Sdim  void removeSelfFromCache();
167212795Sdim};
168212795Sdim
169212795Sdimstruct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
170212795Sdimprivate:
171212795Sdim  /// \brief Cached mapping of Functions to their StratifiedSets.
172212795Sdim  /// If a function's sets are currently being built, it is marked
173212795Sdim  /// in the cache as an Optional without a value. This way, if we
174212795Sdim  /// have any kind of recursion, it is discernable from a function
175212795Sdim  /// that simply has empty sets.
176212795Sdim  DenseMap<Function *, Optional<FunctionInfo>> Cache;
177212795Sdim  std::forward_list<FunctionHandle> Handles;
178234353Sdim
179212795Sdimpublic:
180212795Sdim  static char ID;
181212795Sdim
182212795Sdim  CFLAliasAnalysis() : ImmutablePass(ID) {
183212795Sdim    initializeCFLAliasAnalysisPass(*PassRegistry::getPassRegistry());
184212795Sdim  }
185212795Sdim
186212795Sdim  virtual ~CFLAliasAnalysis() {}
187212795Sdim
188212795Sdim  void getAnalysisUsage(AnalysisUsage &AU) const override {
189212795Sdim    AliasAnalysis::getAnalysisUsage(AU);
190212795Sdim  }
191212795Sdim
192212795Sdim  void *getAdjustedAnalysisPointer(const void *ID) override {
193212795Sdim    if (ID == &AliasAnalysis::ID)
194212795Sdim      return (AliasAnalysis *)this;
195234353Sdim    return this;
196239462Sdim  }
197234353Sdim
198234353Sdim  /// \brief Inserts the given Function into the cache.
199234353Sdim  void scan(Function *Fn);
200239462Sdim
201234353Sdim  void evict(Function *Fn) { Cache.erase(Fn); }
202234353Sdim
203234353Sdim  /// \brief Ensures that the given function is available in the cache.
204234353Sdim  /// Returns the appropriate entry from the cache.
205234353Sdim  const Optional<FunctionInfo> &ensureCached(Function *Fn) {
206234353Sdim    auto Iter = Cache.find(Fn);
207239462Sdim    if (Iter == Cache.end()) {
208239462Sdim      scan(Fn);
209234353Sdim      Iter = Cache.find(Fn);
210239462Sdim      assert(Iter != Cache.end());
211239462Sdim      assert(Iter->second.hasValue());
212239462Sdim    }
213239462Sdim    return Iter->second;
214239462Sdim  }
215234353Sdim
216234353Sdim  AliasResult query(const Location &LocA, const Location &LocB);
217239462Sdim
218234353Sdim  AliasResult alias(const Location &LocA, const Location &LocB) override {
219239462Sdim    if (LocA.Ptr == LocB.Ptr) {
220234353Sdim      if (LocA.Size == LocB.Size) {
221234353Sdim        return MustAlias;
222239462Sdim      } else {
223234353Sdim        return PartialAlias;
224239462Sdim      }
225234353Sdim    }
226234353Sdim
227239462Sdim    // Comparisons between global variables and other constants should be
228239462Sdim    // handled by BasicAA.
229239462Sdim    if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr)) {
230239462Sdim      return MayAlias;
231239462Sdim    }
232234353Sdim
233234353Sdim    return query(LocA, LocB);
234234353Sdim  }
235234353Sdim
236234353Sdim  void initializePass() override { InitializeAliasAnalysis(this); }
237234353Sdim};
238234353Sdim
239234353Sdimvoid FunctionHandle::removeSelfFromCache() {
240239462Sdim  assert(CFLAA != nullptr);
241239462Sdim  auto *Val = getValPtr();
242239462Sdim  CFLAA->evict(cast<Function>(Val));
243239462Sdim  setValPtr(nullptr);
244239462Sdim}
245239462Sdim
246239462Sdim// \brief Gets the edges our graph should have, based on an Instruction*
247239462Sdimclass GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
248234353Sdim  CFLAliasAnalysis &AA;
249234353Sdim  SmallVectorImpl<Edge> &Output;
250239462Sdim
251234353Sdimpublic:
252239462Sdim  GetEdgesVisitor(CFLAliasAnalysis &AA, SmallVectorImpl<Edge> &Output)
253234353Sdim      : AA(AA), Output(Output) {}
254239462Sdim
255234353Sdim  void visitInstruction(Instruction &) {
256234353Sdim    llvm_unreachable("Unsupported instruction encountered");
257239462Sdim  }
258234353Sdim
259234353Sdim  void visitCastInst(CastInst &Inst) {
260239462Sdim    Output.push_back(Edge(&Inst, Inst.getOperand(0), EdgeType::Assign,
261239462Sdim                          AttrNone));
262239462Sdim  }
263239462Sdim
264239462Sdim  void visitBinaryOperator(BinaryOperator &Inst) {
265234353Sdim    auto *Op1 = Inst.getOperand(0);
266234353Sdim    auto *Op2 = Inst.getOperand(1);
267234353Sdim    Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone));
268234353Sdim    Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone));
269234353Sdim  }
270234353Sdim
271234353Sdim  void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
272234353Sdim    auto *Ptr = Inst.getPointerOperand();
273234353Sdim    auto *Val = Inst.getNewValOperand();
274234353Sdim    Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
275234353Sdim  }
276234353Sdim
277239462Sdim  void visitAtomicRMWInst(AtomicRMWInst &Inst) {
278239462Sdim    auto *Ptr = Inst.getPointerOperand();
279239462Sdim    auto *Val = Inst.getValOperand();
280239462Sdim    Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
281234353Sdim  }
282239462Sdim
283234353Sdim  void visitPHINode(PHINode &Inst) {
284239462Sdim    for (unsigned I = 0, E = Inst.getNumIncomingValues(); I != E; ++I) {
285234353Sdim      Value *Val = Inst.getIncomingValue(I);
286234353Sdim      Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
287234353Sdim    }
288234353Sdim  }
289234353Sdim
290234353Sdim  void visitGetElementPtrInst(GetElementPtrInst &Inst) {
291234353Sdim    auto *Op = Inst.getPointerOperand();
292239462Sdim    Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone));
293239462Sdim    for (auto I = Inst.idx_begin(), E = Inst.idx_end(); I != E; ++I)
294234353Sdim      Output.push_back(Edge(&Inst, *I, EdgeType::Assign, AttrNone));
295239462Sdim  }
296234353Sdim
297234353Sdim  void visitSelectInst(SelectInst &Inst) {
298239462Sdim    auto *Condition = Inst.getCondition();
299234353Sdim    Output.push_back(Edge(&Inst, Condition, EdgeType::Assign, AttrNone));
300239462Sdim    auto *TrueVal = Inst.getTrueValue();
301234353Sdim    Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone));
302234353Sdim    auto *FalseVal = Inst.getFalseValue();
303234353Sdim    Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone));
304234353Sdim  }
305234353Sdim
306234353Sdim  void visitAllocaInst(AllocaInst &) {}
307239462Sdim
308234353Sdim  void visitLoadInst(LoadInst &Inst) {
309234353Sdim    auto *Ptr = Inst.getPointerOperand();
310239462Sdim    auto *Val = &Inst;
311234353Sdim    Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
312239462Sdim  }
313234353Sdim
314234353Sdim  void visitStoreInst(StoreInst &Inst) {
315234353Sdim    auto *Ptr = Inst.getPointerOperand();
316234353Sdim    auto *Val = Inst.getValueOperand();
317239462Sdim    Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone));
318234353Sdim  }
319239462Sdim
320239462Sdim  void visitVAArgInst(VAArgInst &Inst) {
321239462Sdim    // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
322239462Sdim    // two things:
323239462Sdim    //  1. Loads a value from *((T*)*Ptr).
324239462Sdim    //  2. Increments (stores to) *Ptr by some target-specific amount.
325239462Sdim    // For now, we'll handle this like a landingpad instruction (by placing the
326239462Sdim    // result in its own group, and having that group alias externals).
327239462Sdim    auto *Val = &Inst;
328239462Sdim    Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll));
329239462Sdim  }
330239462Sdim
331239462Sdim  static bool isFunctionExternal(Function *Fn) {
332239462Sdim    return Fn->isDeclaration() || !Fn->hasLocalLinkage();
333239462Sdim  }
334239462Sdim
335239462Sdim  // Gets whether the sets at Index1 above, below, or equal to the sets at
336239462Sdim  // Index2. Returns None if they are not in the same set chain.
337239462Sdim  static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
338239462Sdim                                          StratifiedIndex Index1,
339239462Sdim                                          StratifiedIndex Index2) {
340239462Sdim    if (Index1 == Index2)
341239462Sdim      return Level::Same;
342239462Sdim
343239462Sdim    const auto *Current = &Sets.getLink(Index1);
344239462Sdim    while (Current->hasBelow()) {
345239462Sdim      if (Current->Below == Index2)
346234353Sdim        return Level::Below;
347234353Sdim      Current = &Sets.getLink(Current->Below);
348234353Sdim    }
349234353Sdim
350239462Sdim    Current = &Sets.getLink(Index1);
351234353Sdim    while (Current->hasAbove()) {
352234353Sdim      if (Current->Above == Index2)
353234353Sdim        return Level::Above;
354234353Sdim      Current = &Sets.getLink(Current->Above);
355234353Sdim    }
356234353Sdim
357234353Sdim    return NoneType();
358239462Sdim  }
359239462Sdim
360239462Sdim  bool
361239462Sdim  tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
362234353Sdim                             Value *FuncValue,
363239462Sdim                             const iterator_range<User::op_iterator> &Args) {
364239462Sdim    const unsigned ExpectedMaxArgs = 8;
365239462Sdim    const unsigned MaxSupportedArgs = 50;
366239462Sdim    assert(Fns.size() > 0);
367239462Sdim
368234353Sdim    // I put this here to give us an upper bound on time taken by IPA. Is it
369234353Sdim    // really (realistically) needed? Keep in mind that we do have an n^2 algo.
370234353Sdim    if (std::distance(Args.begin(), Args.end()) > (int) MaxSupportedArgs)
371234353Sdim      return false;
372234353Sdim
373234353Sdim    // Exit early if we'll fail anyway
374234353Sdim    for (auto *Fn : Fns) {
375234353Sdim      if (isFunctionExternal(Fn) || Fn->isVarArg())
376234353Sdim        return false;
377234353Sdim      auto &MaybeInfo = AA.ensureCached(Fn);
378234353Sdim      if (!MaybeInfo.hasValue())
379234353Sdim        return false;
380234353Sdim    }
381234353Sdim
382234353Sdim    SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
383234353Sdim    SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
384234353Sdim    for (auto *Fn : Fns) {
385234353Sdim      auto &Info = *AA.ensureCached(Fn);
386234353Sdim      auto &Sets = Info.Sets;
387234353Sdim      auto &RetVals = Info.ReturnedValues;
388234353Sdim
389234353Sdim      Parameters.clear();
390234353Sdim      for (auto &Param : Fn->args()) {
391234353Sdim        auto MaybeInfo = Sets.find(&Param);
392234353Sdim        // Did a new parameter somehow get added to the function/slip by?
393234353Sdim        if (!MaybeInfo.hasValue())
394234353Sdim          return false;
395234353Sdim        Parameters.push_back(*MaybeInfo);
396234353Sdim      }
397234353Sdim
398234353Sdim      // Adding an edge from argument -> return value for each parameter that
399234353Sdim      // may alias the return value
400234353Sdim      for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
401234353Sdim        auto &ParamInfo = Parameters[I];
402234353Sdim        auto &ArgVal = Arguments[I];
403234353Sdim        bool AddEdge = false;
404234353Sdim        StratifiedAttrs Externals;
405234353Sdim        for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
406234353Sdim          auto MaybeInfo = Sets.find(RetVals[X]);
407234353Sdim          if (!MaybeInfo.hasValue())
408234353Sdim            return false;
409234353Sdim
410234353Sdim          auto &RetInfo = *MaybeInfo;
411234353Sdim          auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
412234353Sdim          auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
413234353Sdim          auto MaybeRelation =
414234353Sdim              getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
415234353Sdim          if (MaybeRelation.hasValue()) {
416234353Sdim            AddEdge = true;
417234353Sdim            Externals |= RetAttrs | ParamAttrs;
418234353Sdim          }
419234353Sdim        }
420234353Sdim        if (AddEdge)
421234353Sdim          Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign,
422234353Sdim                            StratifiedAttrs().flip()));
423234353Sdim      }
424234353Sdim
425234353Sdim      if (Parameters.size() != Arguments.size())
426234353Sdim        return false;
427234353Sdim
428234353Sdim      // Adding edges between arguments for arguments that may end up aliasing
429239462Sdim      // each other. This is necessary for functions such as
430239462Sdim      // void foo(int** a, int** b) { *a = *b; }
431234353Sdim      // (Technically, the proper sets for this would be those below
432234353Sdim      // Arguments[I] and Arguments[X], but our algorithm will produce
433239462Sdim      // extremely similar, and equally correct, results either way)
434239462Sdim      for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
435234353Sdim        auto &MainVal = Arguments[I];
436234353Sdim        auto &MainInfo = Parameters[I];
437234353Sdim        auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
438234353Sdim        for (unsigned X = I + 1; X != E; ++X) {
439234353Sdim          auto &SubInfo = Parameters[X];
440234353Sdim          auto &SubVal = Arguments[X];
441234353Sdim          auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
442234353Sdim          auto MaybeRelation =
443234353Sdim              getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
444234353Sdim
445234353Sdim          if (!MaybeRelation.hasValue())
446234353Sdim            continue;
447234353Sdim
448234353Sdim          auto NewAttrs = SubAttrs | MainAttrs;
449234353Sdim          Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
450234353Sdim        }
451234353Sdim      }
452234353Sdim    }
453234353Sdim    return true;
454234353Sdim  }
455234353Sdim
456234353Sdim  template <typename InstT> void visitCallLikeInst(InstT &Inst) {
457234353Sdim    SmallVector<Function *, 4> Targets;
458234353Sdim    if (getPossibleTargets(&Inst, Targets)) {
459234353Sdim      if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
460234353Sdim        return;
461234353Sdim      // Cleanup from interprocedural analysis
462234353Sdim      Output.clear();
463212795Sdim    }
464212795Sdim
465234353Sdim    for (Value *V : Inst.arg_operands())
466234353Sdim      Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll));
467212795Sdim  }
468212795Sdim
469212795Sdim  void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
470212795Sdim
471212795Sdim  void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
472234353Sdim
473234353Sdim  // Because vectors/aggregates are immutable and unaddressable,
474212795Sdim  // there's nothing we can do to coax a value out of them, other
475212795Sdim  // than calling Extract{Element,Value}. We can effectively treat
476212795Sdim  // them as pointers to arbitrary memory locations we can store in
477212795Sdim  // and load from.
478212795Sdim  void visitExtractElementInst(ExtractElementInst &Inst) {
479212795Sdim    auto *Ptr = Inst.getVectorOperand();
480212795Sdim    auto *Val = &Inst;
481212795Sdim    Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
482212795Sdim  }
483212795Sdim
484212795Sdim  void visitInsertElementInst(InsertElementInst &Inst) {
485212795Sdim    auto *Vec = Inst.getOperand(0);
486212795Sdim    auto *Val = Inst.getOperand(1);
487212795Sdim    Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone));
488212795Sdim    Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
489212795Sdim  }
490212795Sdim
491  void visitLandingPadInst(LandingPadInst &Inst) {
492    // Exceptions come from "nowhere", from our analysis' perspective.
493    // So we place the instruction its own group, noting that said group may
494    // alias externals
495    Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
496  }
497
498  void visitInsertValueInst(InsertValueInst &Inst) {
499    auto *Agg = Inst.getOperand(0);
500    auto *Val = Inst.getOperand(1);
501    Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone));
502    Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone));
503  }
504
505  void visitExtractValueInst(ExtractValueInst &Inst) {
506    auto *Ptr = Inst.getAggregateOperand();
507    Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
508  }
509
510  void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
511    auto *From1 = Inst.getOperand(0);
512    auto *From2 = Inst.getOperand(1);
513    Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
514    Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
515  }
516};
517
518// For a given instruction, we need to know which Value* to get the
519// users of in order to build our graph. In some cases (i.e. add),
520// we simply need the Instruction*. In other cases (i.e. store),
521// finding the users of the Instruction* is useless; we need to find
522// the users of the first operand. This handles determining which
523// value to follow for us.
524//
525// Note: we *need* to keep this in sync with GetEdgesVisitor. Add
526// something to GetEdgesVisitor, add it here -- remove something from
527// GetEdgesVisitor, remove it here.
528class GetTargetValueVisitor
529    : public InstVisitor<GetTargetValueVisitor, Value *> {
530public:
531  Value *visitInstruction(Instruction &Inst) { return &Inst; }
532
533  Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
534
535  Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
536    return Inst.getPointerOperand();
537  }
538
539  Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
540    return Inst.getPointerOperand();
541  }
542
543  Value *visitInsertElementInst(InsertElementInst &Inst) {
544    return Inst.getOperand(0);
545  }
546
547  Value *visitInsertValueInst(InsertValueInst &Inst) {
548    return Inst.getAggregateOperand();
549  }
550};
551
552// Set building requires a weighted bidirectional graph.
553template <typename EdgeTypeT> class WeightedBidirectionalGraph {
554public:
555  typedef std::size_t Node;
556
557private:
558  const static Node StartNode = Node(0);
559
560  struct Edge {
561    EdgeTypeT Weight;
562    Node Other;
563
564    Edge(const EdgeTypeT &W, const Node &N)
565      : Weight(W), Other(N) {}
566
567    bool operator==(const Edge &E) const {
568      return Weight == E.Weight && Other == E.Other;
569    }
570
571    bool operator!=(const Edge &E) const { return !operator==(E); }
572  };
573
574  struct NodeImpl {
575    std::vector<Edge> Edges;
576  };
577
578  std::vector<NodeImpl> NodeImpls;
579
580  bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
581
582  const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
583  NodeImpl &getNode(Node N) { return NodeImpls[N]; }
584
585public:
586  // ----- Various Edge iterators for the graph ----- //
587
588  // \brief Iterator for edges. Because this graph is bidirected, we don't
589  // allow modificaiton of the edges using this iterator. Additionally, the
590  // iterator becomes invalid if you add edges to or from the node you're
591  // getting the edges of.
592  struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
593                                             std::tuple<EdgeTypeT, Node *>> {
594    EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
595        : Current(Iter) {}
596
597    EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
598
599    EdgeIterator &operator++() {
600      ++Current;
601      return *this;
602    }
603
604    EdgeIterator operator++(int) {
605      EdgeIterator Copy(Current);
606      operator++();
607      return Copy;
608    }
609
610    std::tuple<EdgeTypeT, Node> &operator*() {
611      Store = std::make_tuple(Current->Weight, Current->Other);
612      return Store;
613    }
614
615    bool operator==(const EdgeIterator &Other) const {
616      return Current == Other.Current;
617    }
618
619    bool operator!=(const EdgeIterator &Other) const {
620      return !operator==(Other);
621    }
622
623  private:
624    typename std::vector<Edge>::const_iterator Current;
625    std::tuple<EdgeTypeT, Node> Store;
626  };
627
628  // Wrapper for EdgeIterator with begin()/end() calls.
629  struct EdgeIterable {
630    EdgeIterable(const std::vector<Edge> &Edges)
631        : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
632
633    EdgeIterator begin() { return EdgeIterator(BeginIter); }
634
635    EdgeIterator end() { return EdgeIterator(EndIter); }
636
637  private:
638    typename std::vector<Edge>::const_iterator BeginIter;
639    typename std::vector<Edge>::const_iterator EndIter;
640  };
641
642  // ----- Actual graph-related things ----- //
643
644  WeightedBidirectionalGraph() {}
645
646  WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
647      : NodeImpls(std::move(Other.NodeImpls)) {}
648
649  WeightedBidirectionalGraph<EdgeTypeT> &
650  operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
651    NodeImpls = std::move(Other.NodeImpls);
652    return *this;
653  }
654
655  Node addNode() {
656    auto Index = NodeImpls.size();
657    auto NewNode = Node(Index);
658    NodeImpls.push_back(NodeImpl());
659    return NewNode;
660  }
661
662  void addEdge(Node From, Node To, const EdgeTypeT &Weight,
663               const EdgeTypeT &ReverseWeight) {
664    assert(inbounds(From));
665    assert(inbounds(To));
666    auto &FromNode = getNode(From);
667    auto &ToNode = getNode(To);
668    FromNode.Edges.push_back(Edge(Weight, To));
669    ToNode.Edges.push_back(Edge(ReverseWeight, From));
670  }
671
672  EdgeIterable edgesFor(const Node &N) const {
673    const auto &Node = getNode(N);
674    return EdgeIterable(Node.Edges);
675  }
676
677  bool empty() const { return NodeImpls.empty(); }
678  std::size_t size() const { return NodeImpls.size(); }
679
680  // \brief Gets an arbitrary node in the graph as a starting point for
681  // traversal.
682  Node getEntryNode() {
683    assert(inbounds(StartNode));
684    return StartNode;
685  }
686};
687
688typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
689typedef DenseMap<Value *, GraphT::Node> NodeMapT;
690}
691
692// -- Setting up/registering CFLAA pass -- //
693char CFLAliasAnalysis::ID = 0;
694
695INITIALIZE_AG_PASS(CFLAliasAnalysis, AliasAnalysis, "cfl-aa",
696                   "CFL-Based AA implementation", false, true, false)
697
698ImmutablePass *llvm::createCFLAliasAnalysisPass() {
699  return new CFLAliasAnalysis();
700}
701
702//===----------------------------------------------------------------------===//
703// Function declarations that require types defined in the namespace above
704//===----------------------------------------------------------------------===//
705
706// Given an argument number, returns the appropriate Attr index to set.
707static StratifiedAttr argNumberToAttrIndex(StratifiedAttr);
708
709// Given a Value, potentially return which AttrIndex it maps to.
710static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
711
712// Gets the inverse of a given EdgeType.
713static EdgeType flipWeight(EdgeType);
714
715// Gets edges of the given Instruction*, writing them to the SmallVector*.
716static void argsToEdges(CFLAliasAnalysis &, Instruction *,
717                        SmallVectorImpl<Edge> &);
718
719// Gets the "Level" that one should travel in StratifiedSets
720// given an EdgeType.
721static Level directionOfEdgeType(EdgeType);
722
723// Builds the graph needed for constructing the StratifiedSets for the
724// given function
725static void buildGraphFrom(CFLAliasAnalysis &, Function *,
726                           SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
727
728// Builds the graph + StratifiedSets for a function.
729static FunctionInfo buildSetsFrom(CFLAliasAnalysis &, Function *);
730
731static Optional<Function *> parentFunctionOfValue(Value *Val) {
732  if (auto *Inst = dyn_cast<Instruction>(Val)) {
733    auto *Bb = Inst->getParent();
734    return Bb->getParent();
735  }
736
737  if (auto *Arg = dyn_cast<Argument>(Val))
738    return Arg->getParent();
739  return NoneType();
740}
741
742template <typename Inst>
743static bool getPossibleTargets(Inst *Call,
744                               SmallVectorImpl<Function *> &Output) {
745  if (auto *Fn = Call->getCalledFunction()) {
746    Output.push_back(Fn);
747    return true;
748  }
749
750  // TODO: If the call is indirect, we might be able to enumerate all potential
751  // targets of the call and return them, rather than just failing.
752  return false;
753}
754
755static Optional<Value *> getTargetValue(Instruction *Inst) {
756  GetTargetValueVisitor V;
757  return V.visit(Inst);
758}
759
760static bool hasUsefulEdges(Instruction *Inst) {
761  bool IsNonInvokeTerminator =
762      isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
763  return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
764}
765
766static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
767  if (isa<GlobalValue>(Val))
768    return AttrGlobalIndex;
769
770  if (auto *Arg = dyn_cast<Argument>(Val))
771    if (!Arg->hasNoAliasAttr())
772      return argNumberToAttrIndex(Arg->getArgNo());
773  return NoneType();
774}
775
776static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
777  if (ArgNum > AttrMaxNumArgs)
778    return AttrAllIndex;
779  return ArgNum + AttrFirstArgIndex;
780}
781
782static EdgeType flipWeight(EdgeType Initial) {
783  switch (Initial) {
784  case EdgeType::Assign:
785    return EdgeType::Assign;
786  case EdgeType::Dereference:
787    return EdgeType::Reference;
788  case EdgeType::Reference:
789    return EdgeType::Dereference;
790  }
791  llvm_unreachable("Incomplete coverage of EdgeType enum");
792}
793
794static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst,
795                        SmallVectorImpl<Edge> &Output) {
796  GetEdgesVisitor v(Analysis, Output);
797  v.visit(Inst);
798}
799
800static Level directionOfEdgeType(EdgeType Weight) {
801  switch (Weight) {
802  case EdgeType::Reference:
803    return Level::Above;
804  case EdgeType::Dereference:
805    return Level::Below;
806  case EdgeType::Assign:
807    return Level::Same;
808  }
809  llvm_unreachable("Incomplete switch coverage");
810}
811
812// Aside: We may remove graph construction entirely, because it doesn't really
813// buy us much that we don't already have. I'd like to add interprocedural
814// analysis prior to this however, in case that somehow requires the graph
815// produced by this for efficient execution
816static void buildGraphFrom(CFLAliasAnalysis &Analysis, Function *Fn,
817                           SmallVectorImpl<Value *> &ReturnedValues,
818                           NodeMapT &Map, GraphT &Graph) {
819  const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
820    auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
821    auto &Iter = Pair.first;
822    if (Pair.second) {
823      auto NewNode = Graph.addNode();
824      Iter->second = NewNode;
825    }
826    return Iter->second;
827  };
828
829  SmallVector<Edge, 8> Edges;
830  for (auto &Bb : Fn->getBasicBlockList()) {
831    for (auto &Inst : Bb.getInstList()) {
832      // We don't want the edges of most "return" instructions, but we *do* want
833      // to know what can be returned.
834      if (auto *Ret = dyn_cast<ReturnInst>(&Inst))
835        ReturnedValues.push_back(Ret);
836
837      if (!hasUsefulEdges(&Inst))
838        continue;
839
840      Edges.clear();
841      argsToEdges(Analysis, &Inst, Edges);
842
843      // In the case of an unused alloca (or similar), edges may be empty. Note
844      // that it exists so we can potentially answer NoAlias.
845      if (Edges.empty()) {
846        auto MaybeVal = getTargetValue(&Inst);
847        assert(MaybeVal.hasValue());
848        auto *Target = *MaybeVal;
849        findOrInsertNode(Target);
850        continue;
851      }
852
853      for (const Edge &E : Edges) {
854        auto To = findOrInsertNode(E.To);
855        auto From = findOrInsertNode(E.From);
856        auto FlippedWeight = flipWeight(E.Weight);
857        auto Attrs = E.AdditionalAttrs;
858        Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs),
859                                std::make_pair(FlippedWeight, Attrs));
860      }
861    }
862  }
863}
864
865static FunctionInfo buildSetsFrom(CFLAliasAnalysis &Analysis, Function *Fn) {
866  NodeMapT Map;
867  GraphT Graph;
868  SmallVector<Value *, 4> ReturnedValues;
869
870  buildGraphFrom(Analysis, Fn, ReturnedValues, Map, Graph);
871
872  DenseMap<GraphT::Node, Value *> NodeValueMap;
873  NodeValueMap.resize(Map.size());
874  for (const auto &Pair : Map)
875    NodeValueMap.insert(std::make_pair(Pair.second, Pair.first));
876
877  const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
878    auto ValIter = NodeValueMap.find(Node);
879    assert(ValIter != NodeValueMap.end());
880    return ValIter->second;
881  };
882
883  StratifiedSetsBuilder<Value *> Builder;
884
885  SmallVector<GraphT::Node, 16> Worklist;
886  for (auto &Pair : Map) {
887    Worklist.clear();
888
889    auto *Value = Pair.first;
890    Builder.add(Value);
891    auto InitialNode = Pair.second;
892    Worklist.push_back(InitialNode);
893    while (!Worklist.empty()) {
894      auto Node = Worklist.pop_back_val();
895      auto *CurValue = findValueOrDie(Node);
896      if (isa<Constant>(CurValue) && !isa<GlobalValue>(CurValue))
897        continue;
898
899      for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
900        auto Weight = std::get<0>(EdgeTuple);
901        auto Label = Weight.first;
902        auto &OtherNode = std::get<1>(EdgeTuple);
903        auto *OtherValue = findValueOrDie(OtherNode);
904
905        if (isa<Constant>(OtherValue) && !isa<GlobalValue>(OtherValue))
906          continue;
907
908        bool Added;
909        switch (directionOfEdgeType(Label)) {
910        case Level::Above:
911          Added = Builder.addAbove(CurValue, OtherValue);
912          break;
913        case Level::Below:
914          Added = Builder.addBelow(CurValue, OtherValue);
915          break;
916        case Level::Same:
917          Added = Builder.addWith(CurValue, OtherValue);
918          break;
919        }
920
921        if (Added) {
922          auto Aliasing = Weight.second;
923          if (auto MaybeCurIndex = valueToAttrIndex(CurValue))
924            Aliasing.set(*MaybeCurIndex);
925          if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
926            Aliasing.set(*MaybeOtherIndex);
927          Builder.noteAttributes(CurValue, Aliasing);
928          Builder.noteAttributes(OtherValue, Aliasing);
929          Worklist.push_back(OtherNode);
930        }
931      }
932    }
933  }
934
935  // There are times when we end up with parameters not in our graph (i.e. if
936  // it's only used as the condition of a branch). Other bits of code depend on
937  // things that were present during construction being present in the graph.
938  // So, we add all present arguments here.
939  for (auto &Arg : Fn->args()) {
940    Builder.add(&Arg);
941  }
942
943  return FunctionInfo(Builder.build(), std::move(ReturnedValues));
944}
945
946void CFLAliasAnalysis::scan(Function *Fn) {
947  auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
948  (void)InsertPair;
949  assert(InsertPair.second &&
950         "Trying to scan a function that has already been cached");
951
952  FunctionInfo Info(buildSetsFrom(*this, Fn));
953  Cache[Fn] = std::move(Info);
954  Handles.push_front(FunctionHandle(Fn, this));
955}
956
957AliasAnalysis::AliasResult
958CFLAliasAnalysis::query(const AliasAnalysis::Location &LocA,
959                        const AliasAnalysis::Location &LocB) {
960  auto *ValA = const_cast<Value *>(LocA.Ptr);
961  auto *ValB = const_cast<Value *>(LocB.Ptr);
962
963  Function *Fn = nullptr;
964  auto MaybeFnA = parentFunctionOfValue(ValA);
965  auto MaybeFnB = parentFunctionOfValue(ValB);
966  if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
967    llvm_unreachable("Don't know how to extract the parent function "
968                     "from values A or B");
969  }
970
971  if (MaybeFnA.hasValue()) {
972    Fn = *MaybeFnA;
973    assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
974           "Interprocedural queries not supported");
975  } else {
976    Fn = *MaybeFnB;
977  }
978
979  assert(Fn != nullptr);
980  auto &MaybeInfo = ensureCached(Fn);
981  assert(MaybeInfo.hasValue());
982
983  auto &Sets = MaybeInfo->Sets;
984  auto MaybeA = Sets.find(ValA);
985  if (!MaybeA.hasValue())
986    return AliasAnalysis::MayAlias;
987
988  auto MaybeB = Sets.find(ValB);
989  if (!MaybeB.hasValue())
990    return AliasAnalysis::MayAlias;
991
992  auto SetA = *MaybeA;
993  auto SetB = *MaybeB;
994
995  if (SetA.Index == SetB.Index)
996    return AliasAnalysis::PartialAlias;
997
998  auto AttrsA = Sets.getLink(SetA.Index).Attrs;
999  auto AttrsB = Sets.getLink(SetB.Index).Attrs;
1000  // Stratified set attributes are used as markets to signify whether a member
1001  // of a StratifiedSet (or a member of a set above the current set) has
1002  // interacted with either arguments or globals. "Interacted with" meaning
1003  // its value may be different depending on the value of an argument or
1004  // global. The thought behind this is that, because arguments and globals
1005  // may alias each other, if AttrsA and AttrsB have touched args/globals,
1006  // we must conservatively say that they alias. However, if at least one of
1007  // the sets has no values that could legally be altered by changing the value
1008  // of an argument or global, then we don't have to be as conservative.
1009  if (AttrsA.any() && AttrsB.any())
1010    return AliasAnalysis::MayAlias;
1011
1012  return AliasAnalysis::NoAlias;
1013}
1014