1 //===- CFLAliasAnalysis.cpp - CFL-Based Alias Analysis Implementation ------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
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
28 //===----------------------------------------------------------------------===//
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"
50 #include <forward_list>
56 #define DEBUG_TYPE "cfl-aa"
58 // Try to go from a Value* to a Function*. Never returns nullptr.
59 static Optional<Function *> parentFunctionOfValue(Value *);
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
66 template <typename Inst>
67 static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
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 *);
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 *);
80 const StratifiedIndex StratifiedLink::SetSentinel =
81 std::numeric_limits<StratifiedIndex>::max();
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;
94 LLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
95 LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
96 LLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone;
98 // \brief StratifiedSets call for knowledge of "direction", so this is how we
99 // represent that locally.
100 enum class Level { Same, Above, Below };
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.
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)
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)
124 // \brief Encodes the notion of a "use"
126 // \brief Which value the edge is coming from
129 // \brief Which value the edge is pointing to
132 // \brief Edge weight
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;
140 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
141 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
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;
150 FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV)
151 : Sets(std::move(S)), ReturnedValues(std::move(RV)) {}
154 struct CFLAliasAnalysis;
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);
163 void deleted() override { removeSelfFromCache(); }
164 void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
167 CFLAliasAnalysis *CFLAA;
169 void removeSelfFromCache();
172 struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
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;
185 CFLAliasAnalysis() : ImmutablePass(ID) {
186 initializeCFLAliasAnalysisPass(*PassRegistry::getPassRegistry());
189 ~CFLAliasAnalysis() override {}
191 void getAnalysisUsage(AnalysisUsage &AU) const override {
192 AliasAnalysis::getAnalysisUsage(AU);
195 void *getAdjustedAnalysisPointer(const void *ID) override {
196 if (ID == &AliasAnalysis::ID)
197 return (AliasAnalysis *)this;
201 /// \brief Inserts the given Function into the cache.
202 void scan(Function *Fn);
204 void evict(Function *Fn) { Cache.erase(Fn); }
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()) {
212 Iter = Cache.find(Fn);
213 assert(Iter != Cache.end());
214 assert(Iter->second.hasValue());
219 AliasResult query(const MemoryLocation &LocA, const MemoryLocation &LocB);
221 AliasResult alias(const MemoryLocation &LocA,
222 const MemoryLocation &LocB) override {
223 if (LocA.Ptr == LocB.Ptr) {
224 if (LocA.Size == LocB.Size) {
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
237 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr)) {
238 return AliasAnalysis::alias(LocA, LocB);
241 AliasResult QueryResult = query(LocA, LocB);
242 if (QueryResult == MayAlias)
243 return AliasAnalysis::alias(LocA, LocB);
248 bool doInitialization(Module &M) override;
251 void FunctionHandle::removeSelfFromCache() {
252 assert(CFLAA != nullptr);
253 auto *Val = getValPtr();
254 CFLAA->evict(cast<Function>(Val));
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;
264 GetEdgesVisitor(CFLAliasAnalysis &AA, SmallVectorImpl<Edge> &Output)
265 : AA(AA), Output(Output) {}
267 void visitInstruction(Instruction &) {
268 llvm_unreachable("Unsupported instruction encountered");
271 void visitPtrToIntInst(PtrToIntInst &Inst) {
272 auto *Ptr = Inst.getOperand(0);
273 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
276 void visitIntToPtrInst(IntToPtrInst &Inst) {
278 Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
281 void visitCastInst(CastInst &Inst) {
283 Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone));
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));
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));
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));
305 void visitPHINode(PHINode &Inst) {
306 for (Value *Val : Inst.incoming_values()) {
307 Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone));
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));
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.
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));
330 void visitAllocaInst(AllocaInst &) {}
332 void visitLoadInst(LoadInst &Inst) {
333 auto *Ptr = Inst.getPointerOperand();
335 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
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));
344 void visitVAArgInst(VAArgInst &Inst) {
345 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does
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).
352 Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll));
355 static bool isFunctionExternal(Function *Fn) {
356 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
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)
367 const auto *Current = &Sets.getLink(Index1);
368 while (Current->hasBelow()) {
369 if (Current->Below == Index2)
371 Current = &Sets.getLink(Current->Below);
374 Current = &Sets.getLink(Index1);
375 while (Current->hasAbove()) {
376 if (Current->Above == Index2)
378 Current = &Sets.getLink(Current->Above);
385 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
387 const iterator_range<User::op_iterator> &Args) {
388 const unsigned ExpectedMaxArgs = 8;
389 const unsigned MaxSupportedArgs = 50;
390 assert(Fns.size() > 0);
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)
397 // Exit early if we'll fail anyway
398 for (auto *Fn : Fns) {
399 if (isFunctionExternal(Fn) || Fn->isVarArg())
401 auto &MaybeInfo = AA.ensureCached(Fn);
402 if (!MaybeInfo.hasValue())
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;
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())
419 Parameters.push_back(*MaybeInfo);
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())
434 auto &RetInfo = *MaybeInfo;
435 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
436 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
438 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
439 if (MaybeRelation.hasValue()) {
441 Externals |= RetAttrs | ParamAttrs;
445 Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign,
446 StratifiedAttrs().flip()));
449 if (Parameters.size() != Arguments.size())
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;
467 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
469 if (!MaybeRelation.hasValue())
472 auto NewAttrs = SubAttrs | MainAttrs;
473 Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs));
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()))
485 // Cleanup from interprocedural analysis
489 for (Value *V : Inst.arg_operands())
490 Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll));
493 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
495 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
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
502 void visitExtractElementInst(ExtractElementInst &Inst) {
503 auto *Ptr = Inst.getVectorOperand();
505 Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone));
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));
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
519 Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll));
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));
529 void visitExtractValueInst(ExtractValueInst &Inst) {
530 auto *Ptr = Inst.getAggregateOperand();
531 Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone));
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));
541 void visitConstantExpr(ConstantExpr *CE) {
542 switch (CE->getOpcode()) {
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); \
550 #include "llvm/IR/Instruction.def"
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.
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 *> {
568 Value *visitInstruction(Instruction &Inst) { return &Inst; }
570 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
572 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
573 return Inst.getPointerOperand();
576 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
577 return Inst.getPointerOperand();
580 Value *visitInsertElementInst(InsertElementInst &Inst) {
581 return Inst.getOperand(0);
584 Value *visitInsertValueInst(InsertValueInst &Inst) {
585 return Inst.getAggregateOperand();
589 // Set building requires a weighted bidirectional graph.
590 template <typename EdgeTypeT> class WeightedBidirectionalGraph {
592 typedef std::size_t Node;
595 const static Node StartNode = Node(0);
601 Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {}
603 bool operator==(const Edge &E) const {
604 return Weight == E.Weight && Other == E.Other;
607 bool operator!=(const Edge &E) const { return !operator==(E); }
611 std::vector<Edge> Edges;
614 std::vector<NodeImpl> NodeImpls;
616 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
618 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
619 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
622 // ----- Various Edge iterators for the graph ----- //
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)
633 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
635 EdgeIterator &operator++() {
640 EdgeIterator operator++(int) {
641 EdgeIterator Copy(Current);
646 std::tuple<EdgeTypeT, Node> &operator*() {
647 Store = std::make_tuple(Current->Weight, Current->Other);
651 bool operator==(const EdgeIterator &Other) const {
652 return Current == Other.Current;
655 bool operator!=(const EdgeIterator &Other) const {
656 return !operator==(Other);
660 typename std::vector<Edge>::const_iterator Current;
661 std::tuple<EdgeTypeT, Node> Store;
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()) {}
669 EdgeIterator begin() { return EdgeIterator(BeginIter); }
671 EdgeIterator end() { return EdgeIterator(EndIter); }
674 typename std::vector<Edge>::const_iterator BeginIter;
675 typename std::vector<Edge>::const_iterator EndIter;
678 // ----- Actual graph-related things ----- //
680 WeightedBidirectionalGraph() {}
682 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
683 : NodeImpls(std::move(Other.NodeImpls)) {}
685 WeightedBidirectionalGraph<EdgeTypeT> &
686 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
687 NodeImpls = std::move(Other.NodeImpls);
692 auto Index = NodeImpls.size();
693 auto NewNode = Node(Index);
694 NodeImpls.push_back(NodeImpl());
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));
708 EdgeIterable edgesFor(const Node &N) const {
709 const auto &Node = getNode(N);
710 return EdgeIterable(Node.Edges);
713 bool empty() const { return NodeImpls.empty(); }
714 std::size_t size() const { return NodeImpls.size(); }
716 // \brief Gets an arbitrary node in the graph as a starting point for
718 Node getEntryNode() {
719 assert(inbounds(StartNode));
724 typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
725 typedef DenseMap<Value *, GraphT::Node> NodeMapT;
728 // -- Setting up/registering CFLAA pass -- //
729 char CFLAliasAnalysis::ID = 0;
731 INITIALIZE_AG_PASS(CFLAliasAnalysis, AliasAnalysis, "cfl-aa",
732 "CFL-Based AA implementation", false, true, false)
734 ImmutablePass *llvm::createCFLAliasAnalysisPass() {
735 return new CFLAliasAnalysis();
738 //===----------------------------------------------------------------------===//
739 // Function declarations that require types defined in the namespace above
740 //===----------------------------------------------------------------------===//
742 // Given an argument number, returns the appropriate Attr index to set.
743 static StratifiedAttr argNumberToAttrIndex(StratifiedAttr);
745 // Given a Value, potentially return which AttrIndex it maps to.
746 static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
748 // Gets the inverse of a given EdgeType.
749 static EdgeType flipWeight(EdgeType);
751 // Gets edges of the given Instruction*, writing them to the SmallVector*.
752 static void argsToEdges(CFLAliasAnalysis &, Instruction *,
753 SmallVectorImpl<Edge> &);
755 // Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
756 static void argsToEdges(CFLAliasAnalysis &, ConstantExpr *,
757 SmallVectorImpl<Edge> &);
759 // Gets the "Level" that one should travel in StratifiedSets
760 // given an EdgeType.
761 static Level directionOfEdgeType(EdgeType);
763 // Builds the graph needed for constructing the StratifiedSets for the
765 static void buildGraphFrom(CFLAliasAnalysis &, Function *,
766 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
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> &);
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 &,
784 // Notes whether it would be pointless to add the given Value to our sets.
785 static bool canSkipAddingToSets(Value *Val);
787 // Builds the graph + StratifiedSets for a function.
788 static FunctionInfo buildSetsFrom(CFLAliasAnalysis &, Function *);
790 static Optional<Function *> parentFunctionOfValue(Value *Val) {
791 if (auto *Inst = dyn_cast<Instruction>(Val)) {
792 auto *Bb = Inst->getParent();
793 return Bb->getParent();
796 if (auto *Arg = dyn_cast<Argument>(Val))
797 return Arg->getParent();
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);
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.
814 static Optional<Value *> getTargetValue(Instruction *Inst) {
815 GetTargetValueVisitor V;
816 return V.visit(Inst);
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;
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;
832 static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
833 if (isa<GlobalValue>(Val))
834 return AttrGlobalIndex;
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());
845 static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
846 if (ArgNum >= AttrMaxNumArgs)
848 return ArgNum + AttrFirstArgIndex;
851 static EdgeType flipWeight(EdgeType 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;
860 llvm_unreachable("Incomplete coverage of EdgeType enum");
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);
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);
878 static Level directionOfEdgeType(EdgeType Weight) {
880 case EdgeType::Reference:
882 case EdgeType::Dereference:
884 case EdgeType::Assign:
887 llvm_unreachable("Incomplete switch coverage");
890 static void constexprToEdges(CFLAliasAnalysis &Analysis,
891 ConstantExpr &CExprToCollapse,
892 SmallVectorImpl<Edge> &Results) {
893 SmallVector<ConstantExpr *, 4> Worklist;
894 Worklist.push_back(&CExprToCollapse);
896 SmallVector<Edge, 8> ConstexprEdges;
897 SmallPtrSet<ConstantExpr *, 4> Visited;
898 while (!Worklist.empty()) {
899 auto *CExpr = Worklist.pop_back_val();
901 if (!hasUsefulEdges(CExpr))
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);
911 if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To))
912 if (Visited.insert(Nested).second)
913 Worklist.push_back(Nested);
916 Results.append(ConstexprEdges.begin(), ConstexprEdges.end());
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;
927 auto NewNode = Graph.addNode();
928 Iter->second = NewNode;
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);
938 if (!hasUsefulEdges(&Inst))
941 SmallVector<Edge, 8> Edges;
942 argsToEdges(Analysis, &Inst, Edges);
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.
947 auto MaybeVal = getTargetValue(&Inst);
948 assert(MaybeVal.hasValue());
949 auto *Target = *MaybeVal;
950 findOrInsertNode(Target);
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));
963 SmallVector<ConstantExpr *, 4> ConstantExprs;
964 for (const Edge &E : Edges) {
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);
972 for (ConstantExpr *CE : ConstantExprs) {
974 constexprToEdges(Analysis, *CE, Edges);
975 std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph);
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);
991 static bool canSkipAddingToSets(Value *Val) {
992 // Constants can share instances, which may falsely unify multiple
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
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;
1013 static FunctionInfo buildSetsFrom(CFLAliasAnalysis &Analysis, Function *Fn) {
1016 SmallVector<Value *, 4> ReturnedValues;
1018 buildGraphFrom(Analysis, Fn, ReturnedValues, Map, Graph);
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));
1025 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
1026 auto ValIter = NodeValueMap.find(Node);
1027 assert(ValIter != NodeValueMap.end());
1028 return ValIter->second;
1031 StratifiedSetsBuilder<Value *> Builder;
1033 SmallVector<GraphT::Node, 16> Worklist;
1034 for (auto &Pair : Map) {
1037 auto *Value = Pair.first;
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))
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);
1053 if (canSkipAddingToSets(OtherValue))
1057 switch (directionOfEdgeType(Label)) {
1059 Added = Builder.addAbove(CurValue, OtherValue);
1062 Added = Builder.addBelow(CurValue, OtherValue);
1065 Added = Builder.addWith(CurValue, OtherValue);
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);
1078 Worklist.push_back(OtherNode);
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))
1091 auto Attrs = valueToAttrIndex(&Arg);
1092 if (Attrs.hasValue())
1093 Builder.noteAttributes(&Arg, *Attrs);
1096 return FunctionInfo(Builder.build(), std::move(ReturnedValues));
1099 void CFLAliasAnalysis::scan(Function *Fn) {
1100 auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>()));
1102 assert(InsertPair.second &&
1103 "Trying to scan a function that has already been cached");
1105 FunctionInfo Info(buildSetsFrom(*this, Fn));
1106 Cache[Fn] = std::move(Info);
1107 Handles.push_front(FunctionHandle(Fn, this));
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);
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
1121 DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n");
1125 if (MaybeFnA.hasValue()) {
1127 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
1128 "Interprocedural queries not supported");
1133 assert(Fn != nullptr);
1134 auto &MaybeInfo = ensureCached(Fn);
1135 assert(MaybeInfo.hasValue());
1137 auto &Sets = MaybeInfo->Sets;
1138 auto MaybeA = Sets.find(ValA);
1139 if (!MaybeA.hasValue())
1142 auto MaybeB = Sets.find(ValB);
1143 if (!MaybeB.hasValue())
1146 auto SetA = *MaybeA;
1147 auto SetB = *MaybeB;
1148 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
1149 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
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())
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
1171 if (SetA.Index == SetB.Index)
1177 bool CFLAliasAnalysis::doInitialization(Module &M) {
1178 InitializeAliasAnalysis(this, &M.getDataLayout());