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, Assign, or
20 // Two variables are considered as aliasing iff you can reach one value's node
21 // from the other value's node and the language formed by concatenating all of
22 // the edge labels (actions) conforms to a context-free grammar.
24 // Because this algorithm requires a graph search on each query, we execute the
25 // algorithm outlined in "Fast algorithms..." (mentioned above)
26 // in order to transform the graph into sets of variables that may alias in
27 // ~nlogn time (n = number of variables.), which makes queries take constant
29 //===----------------------------------------------------------------------===//
31 #include "StratifiedSets.h"
32 #include "llvm/Analysis/Passes.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/Optional.h"
36 #include "llvm/ADT/None.h"
37 #include "llvm/Analysis/AliasAnalysis.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/InstVisitor.h"
42 #include "llvm/IR/ValueHandle.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/Allocator.h"
45 #include "llvm/Support/ErrorHandling.h"
48 #include <forward_list>
53 // Try to go from a Value* to a Function*. Never returns nullptr.
54 static Optional<Function *> parentFunctionOfValue(Value *);
56 // Returns possible functions called by the Inst* into the given
57 // SmallVectorImpl. Returns true if targets found, false otherwise.
58 // This is templated because InvokeInst/CallInst give us the same
59 // set of functions that we care about, and I don't like repeating
61 template <typename Inst>
62 static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
64 // Some instructions need to have their users tracked. Instructions like
65 // `add` require you to get the users of the Instruction* itself, other
66 // instructions like `store` require you to get the users of the first
67 // operand. This function gets the "proper" value to track for each
68 // type of instruction we support.
69 static Optional<Value *> getTargetValue(Instruction *);
71 // There are certain instructions (i.e. FenceInst, etc.) that we ignore.
72 // This notes that we should ignore those.
73 static bool hasUsefulEdges(Instruction *);
76 // StratifiedInfo Attribute things.
77 typedef unsigned StratifiedAttr;
78 constexpr unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
79 constexpr unsigned AttrAllIndex = 0;
80 constexpr unsigned AttrGlobalIndex = 1;
81 constexpr unsigned AttrFirstArgIndex = 2;
82 constexpr unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
83 constexpr unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
85 constexpr StratifiedAttr AttrNone = 0;
86 constexpr StratifiedAttr AttrAll = ~AttrNone;
88 // \brief StratifiedSets call for knowledge of "direction", so this is how we
89 // represent that locally.
90 enum class Level { Same, Above, Below };
92 // \brief Edges can be one of four "weights" -- each weight must have an inverse
93 // weight (Assign has Assign; Reference has Dereference).
95 // The weight assigned when assigning from or to a value. For example, in:
96 // %b = getelementptr %a, 0
97 // ...The relationships are %b assign %a, and %a assign %b. This used to be
98 // two edges, but having a distinction bought us nothing.
101 // The edge used when we have an edge going from some handle to a Value.
102 // Examples of this include:
103 // %b = load %a (%b Dereference %a)
104 // %b = extractelement %a, 0 (%a Dereference %b)
107 // The edge used when our edge goes from a value to a handle that may have
108 // contained it at some point. Examples:
109 // %b = load %a (%a Reference %b)
110 // %b = extractelement %a, 0 (%b Reference %a)
114 // \brief Encodes the notion of a "use"
116 // \brief Which value the edge is coming from
119 // \brief Which value the edge is pointing to
122 // \brief Edge weight
125 // \brief Whether we aliased any external values along the way that may be
126 // invisible to the analysis (i.e. landingpad for exceptions, calls for
127 // interprocedural analysis, etc.)
128 StratifiedAttrs AdditionalAttrs;
130 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
131 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
134 // \brief Information we have about a function and would like to keep around
135 struct FunctionInfo {
136 StratifiedSets<Value *> Sets;
137 // Lots of functions have < 4 returns. Adjust as necessary.
138 SmallVector<Value *, 4> ReturnedValues;
141 struct CFLAliasAnalysis;
143 struct FunctionHandle : public CallbackVH {
144 FunctionHandle(Function *Fn, CFLAliasAnalysis *CFLAA)
145 : CallbackVH(Fn), CFLAA(CFLAA) {
146 assert(Fn != nullptr);
147 assert(CFLAA != nullptr);
150 virtual ~FunctionHandle() {}
152 virtual void deleted() override { removeSelfFromCache(); }
153 virtual void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
156 CFLAliasAnalysis *CFLAA;
158 void removeSelfFromCache();
161 struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
163 /// \brief Cached mapping of Functions to their StratifiedSets.
164 /// If a function's sets are currently being built, it is marked
165 /// in the cache as an Optional without a value. This way, if we
166 /// have any kind of recursion, it is discernable from a function
167 /// that simply has empty sets.
168 DenseMap<Function *, Optional<FunctionInfo>> Cache;
169 std::forward_list<FunctionHandle> Handles;
174 CFLAliasAnalysis() : ImmutablePass(ID) {
175 initializeCFLAliasAnalysisPass(*PassRegistry::getPassRegistry());
178 virtual ~CFLAliasAnalysis() {}
180 void getAnalysisUsage(AnalysisUsage &AU) const {
181 AliasAnalysis::getAnalysisUsage(AU);
184 void *getAdjustedAnalysisPointer(const void *ID) override {
185 if (ID == &AliasAnalysis::ID)
186 return (AliasAnalysis *)this;
190 /// \brief Inserts the given Function into the cache.
191 void scan(Function *Fn);
193 void evict(Function *Fn) { Cache.erase(Fn); }
195 /// \brief Ensures that the given function is available in the cache.
196 /// Returns the appropriate entry from the cache.
197 const Optional<FunctionInfo> &ensureCached(Function *Fn) {
198 auto Iter = Cache.find(Fn);
199 if (Iter == Cache.end()) {
201 Iter = Cache.find(Fn);
202 assert(Iter != Cache.end());
203 assert(Iter->second.hasValue());
208 AliasResult query(const Location &LocA, const Location &LocB);
210 AliasResult alias(const Location &LocA, const Location &LocB) override {
211 if (LocA.Ptr == LocB.Ptr) {
212 if (LocA.Size == LocB.Size) {
219 // Comparisons between global variables and other constants should be
220 // handled by BasicAA.
221 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr)) {
225 return query(LocA, LocB);
228 void initializePass() override { InitializeAliasAnalysis(this); }
231 void FunctionHandle::removeSelfFromCache() {
232 assert(CFLAA != nullptr);
233 auto *Val = getValPtr();
234 CFLAA->evict(cast<Function>(Val));
238 // \brief Gets the edges our graph should have, based on an Instruction*
239 class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
240 CFLAliasAnalysis &AA;
241 SmallVectorImpl<Edge> &Output;
244 GetEdgesVisitor(CFLAliasAnalysis &AA, SmallVectorImpl<Edge> &Output)
245 : AA(AA), Output(Output) {}
247 void visitInstruction(Instruction &) {
248 llvm_unreachable("Unsupported instruction encountered");
251 void visitCastInst(CastInst &Inst) {
252 Output.push_back({&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone});
255 void visitBinaryOperator(BinaryOperator &Inst) {
256 auto *Op1 = Inst.getOperand(0);
257 auto *Op2 = Inst.getOperand(1);
258 Output.push_back({&Inst, Op1, EdgeType::Assign, AttrNone});
259 Output.push_back({&Inst, Op2, EdgeType::Assign, AttrNone});
262 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
263 auto *Ptr = Inst.getPointerOperand();
264 auto *Val = Inst.getNewValOperand();
265 Output.push_back({Ptr, Val, EdgeType::Dereference, AttrNone});
268 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
269 auto *Ptr = Inst.getPointerOperand();
270 auto *Val = Inst.getValOperand();
271 Output.push_back({Ptr, Val, EdgeType::Dereference, AttrNone});
274 void visitPHINode(PHINode &Inst) {
275 for (unsigned I = 0, E = Inst.getNumIncomingValues(); I != E; ++I) {
276 Value *Val = Inst.getIncomingValue(I);
277 Output.push_back({&Inst, Val, EdgeType::Assign, AttrNone});
281 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
282 auto *Op = Inst.getPointerOperand();
283 Output.push_back({&Inst, Op, EdgeType::Assign, AttrNone});
284 for (auto I = Inst.idx_begin(), E = Inst.idx_end(); I != E; ++I)
285 Output.push_back({&Inst, *I, EdgeType::Assign, AttrNone});
288 void visitSelectInst(SelectInst &Inst) {
289 auto *Condition = Inst.getCondition();
290 Output.push_back({&Inst, Condition, EdgeType::Assign, AttrNone});
291 auto *TrueVal = Inst.getTrueValue();
292 Output.push_back({&Inst, TrueVal, EdgeType::Assign, AttrNone});
293 auto *FalseVal = Inst.getFalseValue();
294 Output.push_back({&Inst, FalseVal, EdgeType::Assign, AttrNone});
297 void visitAllocaInst(AllocaInst &) {}
299 void visitLoadInst(LoadInst &Inst) {
300 auto *Ptr = Inst.getPointerOperand();
302 Output.push_back({Val, Ptr, EdgeType::Reference, AttrNone});
305 void visitStoreInst(StoreInst &Inst) {
306 auto *Ptr = Inst.getPointerOperand();
307 auto *Val = Inst.getValueOperand();
308 Output.push_back({Ptr, Val, EdgeType::Dereference, AttrNone});
311 static bool isFunctionExternal(Function *Fn) {
312 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
315 // Gets whether the sets at Index1 above, below, or equal to the sets at
316 // Index2. Returns None if they are not in the same set chain.
317 static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets,
318 StratifiedIndex Index1,
319 StratifiedIndex Index2) {
320 if (Index1 == Index2)
323 const auto *Current = &Sets.getLink(Index1);
324 while (Current->hasBelow()) {
325 if (Current->Below == Index2)
327 Current = &Sets.getLink(Current->Below);
330 Current = &Sets.getLink(Index1);
331 while (Current->hasAbove()) {
332 if (Current->Above == Index2)
334 Current = &Sets.getLink(Current->Above);
341 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
343 const iterator_range<User::op_iterator> &Args) {
344 constexpr unsigned ExpectedMaxArgs = 8;
345 constexpr unsigned MaxSupportedArgs = 50;
346 assert(Fns.size() > 0);
348 // I put this here to give us an upper bound on time taken by IPA. Is it
349 // really (realistically) needed? Keep in mind that we do have an n^2 algo.
350 if (std::distance(Args.begin(), Args.end()) > MaxSupportedArgs)
353 // Exit early if we'll fail anyway
354 for (auto *Fn : Fns) {
355 if (isFunctionExternal(Fn) || Fn->isVarArg())
357 auto &MaybeInfo = AA.ensureCached(Fn);
358 if (!MaybeInfo.hasValue())
362 SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end());
363 SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters;
364 for (auto *Fn : Fns) {
365 auto &Info = *AA.ensureCached(Fn);
366 auto &Sets = Info.Sets;
367 auto &RetVals = Info.ReturnedValues;
370 for (auto &Param : Fn->args()) {
371 auto MaybeInfo = Sets.find(&Param);
372 // Did a new parameter somehow get added to the function/slip by?
373 if (!MaybeInfo.hasValue())
375 Parameters.push_back(*MaybeInfo);
378 // Adding an edge from argument -> return value for each parameter that
379 // may alias the return value
380 for (unsigned I = 0, E = Parameters.size(); I != E; ++I) {
381 auto &ParamInfo = Parameters[I];
382 auto &ArgVal = Arguments[I];
383 bool AddEdge = false;
384 StratifiedAttrs Externals;
385 for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) {
386 auto MaybeInfo = Sets.find(RetVals[X]);
387 if (!MaybeInfo.hasValue())
390 auto &RetInfo = *MaybeInfo;
391 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
392 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
394 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
395 if (MaybeRelation.hasValue()) {
397 Externals |= RetAttrs | ParamAttrs;
401 Output.push_back({FuncValue, ArgVal, EdgeType::Assign,
402 StratifiedAttrs().flip()});
405 if (Parameters.size() != Arguments.size())
408 // Adding edges between arguments for arguments that may end up aliasing
409 // each other. This is necessary for functions such as
410 // void foo(int** a, int** b) { *a = *b; }
411 // (Technically, the proper sets for this would be those below
412 // Arguments[I] and Arguments[X], but our algorithm will produce
413 // extremely similar, and equally correct, results either way)
414 for (unsigned I = 0, E = Arguments.size(); I != E; ++I) {
415 auto &MainVal = Arguments[I];
416 auto &MainInfo = Parameters[I];
417 auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs;
418 for (unsigned X = I + 1; X != E; ++X) {
419 auto &SubInfo = Parameters[X];
420 auto &SubVal = Arguments[X];
421 auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs;
423 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
425 if (!MaybeRelation.hasValue())
428 auto NewAttrs = SubAttrs | MainAttrs;
429 Output.push_back({MainVal, SubVal, EdgeType::Assign, NewAttrs});
436 template <typename InstT> void visitCallLikeInst(InstT &Inst) {
437 SmallVector<Function *, 4> Targets;
438 if (getPossibleTargets(&Inst, Targets)) {
439 if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands()))
441 // Cleanup from interprocedural analysis
445 for (Value *V : Inst.arg_operands())
446 Output.push_back({&Inst, V, EdgeType::Assign, AttrAll});
449 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
451 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
453 // Because vectors/aggregates are immutable and unaddressable,
454 // there's nothing we can do to coax a value out of them, other
455 // than calling Extract{Element,Value}. We can effectively treat
456 // them as pointers to arbitrary memory locations we can store in
458 void visitExtractElementInst(ExtractElementInst &Inst) {
459 auto *Ptr = Inst.getVectorOperand();
461 Output.push_back({Val, Ptr, EdgeType::Reference, AttrNone});
464 void visitInsertElementInst(InsertElementInst &Inst) {
465 auto *Vec = Inst.getOperand(0);
466 auto *Val = Inst.getOperand(1);
467 Output.push_back({&Inst, Vec, EdgeType::Assign, AttrNone});
468 Output.push_back({&Inst, Val, EdgeType::Dereference, AttrNone});
471 void visitLandingPadInst(LandingPadInst &Inst) {
472 // Exceptions come from "nowhere", from our analysis' perspective.
473 // So we place the instruction its own group, noting that said group may
475 Output.push_back({&Inst, &Inst, EdgeType::Assign, AttrAll});
478 void visitInsertValueInst(InsertValueInst &Inst) {
479 auto *Agg = Inst.getOperand(0);
480 auto *Val = Inst.getOperand(1);
481 Output.push_back({&Inst, Agg, EdgeType::Assign, AttrNone});
482 Output.push_back({&Inst, Val, EdgeType::Dereference, AttrNone});
485 void visitExtractValueInst(ExtractValueInst &Inst) {
486 auto *Ptr = Inst.getAggregateOperand();
487 Output.push_back({&Inst, Ptr, EdgeType::Reference, AttrNone});
490 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
491 auto *From1 = Inst.getOperand(0);
492 auto *From2 = Inst.getOperand(1);
493 Output.push_back({&Inst, From1, EdgeType::Assign, AttrNone});
494 Output.push_back({&Inst, From2, EdgeType::Assign, AttrNone});
498 // For a given instruction, we need to know which Value* to get the
499 // users of in order to build our graph. In some cases (i.e. add),
500 // we simply need the Instruction*. In other cases (i.e. store),
501 // finding the users of the Instruction* is useless; we need to find
502 // the users of the first operand. This handles determining which
503 // value to follow for us.
505 // Note: we *need* to keep this in sync with GetEdgesVisitor. Add
506 // something to GetEdgesVisitor, add it here -- remove something from
507 // GetEdgesVisitor, remove it here.
508 class GetTargetValueVisitor
509 : public InstVisitor<GetTargetValueVisitor, Value *> {
511 Value *visitInstruction(Instruction &Inst) { return &Inst; }
513 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
515 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
516 return Inst.getPointerOperand();
519 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
520 return Inst.getPointerOperand();
523 Value *visitInsertElementInst(InsertElementInst &Inst) {
524 return Inst.getOperand(0);
527 Value *visitInsertValueInst(InsertValueInst &Inst) {
528 return Inst.getAggregateOperand();
532 // Set building requires a weighted bidirectional graph.
533 template <typename EdgeTypeT> class WeightedBidirectionalGraph {
535 typedef std::size_t Node;
538 constexpr static Node StartNode = Node(0);
544 bool operator==(const Edge &E) const {
545 return Weight == E.Weight && Other == E.Other;
548 bool operator!=(const Edge &E) const { return !operator==(E); }
552 std::vector<Edge> Edges;
555 std::vector<NodeImpl> NodeImpls;
557 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
559 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
560 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
563 // ----- Various Edge iterators for the graph ----- //
565 // \brief Iterator for edges. Because this graph is bidirected, we don't
566 // allow modificaiton of the edges using this iterator. Additionally, the
567 // iterator becomes invalid if you add edges to or from the node you're
568 // getting the edges of.
569 struct EdgeIterator : public std::iterator<std::forward_iterator_tag,
570 std::tuple<EdgeTypeT, Node *>> {
571 EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter)
574 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
576 EdgeIterator &operator++() {
581 EdgeIterator operator++(int) {
582 EdgeIterator Copy(Current);
587 std::tuple<EdgeTypeT, Node> &operator*() {
588 Store = std::make_tuple(Current->Weight, Current->Other);
592 bool operator==(const EdgeIterator &Other) const {
593 return Current == Other.Current;
596 bool operator!=(const EdgeIterator &Other) const {
597 return !operator==(Other);
601 typename std::vector<Edge>::const_iterator Current;
602 std::tuple<EdgeTypeT, Node> Store;
605 // Wrapper for EdgeIterator with begin()/end() calls.
606 struct EdgeIterable {
607 EdgeIterable(const std::vector<Edge> &Edges)
608 : BeginIter(Edges.begin()), EndIter(Edges.end()) {}
610 EdgeIterator begin() { return EdgeIterator(BeginIter); }
612 EdgeIterator end() { return EdgeIterator(EndIter); }
615 typename std::vector<Edge>::const_iterator BeginIter;
616 typename std::vector<Edge>::const_iterator EndIter;
619 // ----- Actual graph-related things ----- //
621 WeightedBidirectionalGraph() = default;
623 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
624 : NodeImpls(std::move(Other.NodeImpls)) {}
626 WeightedBidirectionalGraph<EdgeTypeT> &
627 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
628 NodeImpls = std::move(Other.NodeImpls);
633 auto Index = NodeImpls.size();
634 auto NewNode = Node(Index);
635 NodeImpls.push_back(NodeImpl());
639 void addEdge(Node From, Node To, const EdgeTypeT &Weight,
640 const EdgeTypeT &ReverseWeight) {
641 assert(inbounds(From));
642 assert(inbounds(To));
643 auto &FromNode = getNode(From);
644 auto &ToNode = getNode(To);
645 FromNode.Edges.push_back(Edge{Weight, To});
646 ToNode.Edges.push_back(Edge{ReverseWeight, From});
649 EdgeIterable edgesFor(const Node &N) const {
650 const auto &Node = getNode(N);
651 return EdgeIterable(Node.Edges);
654 bool empty() const { return NodeImpls.empty(); }
655 std::size_t size() const { return NodeImpls.size(); }
657 // \brief Gets an arbitrary node in the graph as a starting point for
659 Node getEntryNode() {
660 assert(inbounds(StartNode));
665 typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
666 typedef DenseMap<Value *, GraphT::Node> NodeMapT;
669 // -- Setting up/registering CFLAA pass -- //
670 char CFLAliasAnalysis::ID = 0;
672 INITIALIZE_AG_PASS(CFLAliasAnalysis, AliasAnalysis, "cfl-aa",
673 "CFL-Based AA implementation", false, true, false)
675 ImmutablePass *llvm::createCFLAliasAnalysisPass() {
676 return new CFLAliasAnalysis();
679 //===----------------------------------------------------------------------===//
680 // Function declarations that require types defined in the namespace above
681 //===----------------------------------------------------------------------===//
683 // Given an argument number, returns the appropriate Attr index to set.
684 static StratifiedAttr argNumberToAttrIndex(StratifiedAttr);
686 // Given a Value, potentially return which AttrIndex it maps to.
687 static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
689 // Gets the inverse of a given EdgeType.
690 static EdgeType flipWeight(EdgeType);
692 // Gets edges of the given Instruction*, writing them to the SmallVector*.
693 static void argsToEdges(CFLAliasAnalysis &, Instruction *,
694 SmallVectorImpl<Edge> &);
696 // Gets the "Level" that one should travel in StratifiedSets
697 // given an EdgeType.
698 static Level directionOfEdgeType(EdgeType);
700 // Builds the graph needed for constructing the StratifiedSets for the
702 static void buildGraphFrom(CFLAliasAnalysis &, Function *,
703 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
705 // Builds the graph + StratifiedSets for a function.
706 static FunctionInfo buildSetsFrom(CFLAliasAnalysis &, Function *);
708 static Optional<Function *> parentFunctionOfValue(Value *Val) {
709 if (auto *Inst = dyn_cast<Instruction>(Val)) {
710 auto *Bb = Inst->getParent();
711 return Bb->getParent();
714 if (auto *Arg = dyn_cast<Argument>(Val))
715 return Arg->getParent();
719 template <typename Inst>
720 static bool getPossibleTargets(Inst *Call,
721 SmallVectorImpl<Function *> &Output) {
722 if (auto *Fn = Call->getCalledFunction()) {
723 Output.push_back(Fn);
727 // TODO: If the call is indirect, we might be able to enumerate all potential
728 // targets of the call and return them, rather than just failing.
732 static Optional<Value *> getTargetValue(Instruction *Inst) {
733 GetTargetValueVisitor V;
734 return V.visit(Inst);
737 static bool hasUsefulEdges(Instruction *Inst) {
738 bool IsNonInvokeTerminator =
739 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
740 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
743 static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
744 if (isa<GlobalValue>(Val))
745 return AttrGlobalIndex;
747 if (auto *Arg = dyn_cast<Argument>(Val))
748 if (!Arg->hasNoAliasAttr())
749 return argNumberToAttrIndex(Arg->getArgNo());
753 static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
754 if (ArgNum > AttrMaxNumArgs)
756 return ArgNum + AttrFirstArgIndex;
759 static EdgeType flipWeight(EdgeType Initial) {
761 case EdgeType::Assign:
762 return EdgeType::Assign;
763 case EdgeType::Dereference:
764 return EdgeType::Reference;
765 case EdgeType::Reference:
766 return EdgeType::Dereference;
768 llvm_unreachable("Incomplete coverage of EdgeType enum");
771 static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst,
772 SmallVectorImpl<Edge> &Output) {
773 GetEdgesVisitor v(Analysis, Output);
777 static Level directionOfEdgeType(EdgeType Weight) {
779 case EdgeType::Reference:
781 case EdgeType::Dereference:
783 case EdgeType::Assign:
786 llvm_unreachable("Incomplete switch coverage");
789 // Aside: We may remove graph construction entirely, because it doesn't really
790 // buy us much that we don't already have. I'd like to add interprocedural
791 // analysis prior to this however, in case that somehow requires the graph
792 // produced by this for efficient execution
793 static void buildGraphFrom(CFLAliasAnalysis &Analysis, Function *Fn,
794 SmallVectorImpl<Value *> &ReturnedValues,
795 NodeMapT &Map, GraphT &Graph) {
796 const auto findOrInsertNode = [&Map, &Graph](Value *Val) {
797 auto Pair = Map.insert(std::make_pair(Val, GraphT::Node()));
798 auto &Iter = Pair.first;
800 auto NewNode = Graph.addNode();
801 Iter->second = NewNode;
806 SmallVector<Edge, 8> Edges;
807 for (auto &Bb : Fn->getBasicBlockList()) {
808 for (auto &Inst : Bb.getInstList()) {
809 // We don't want the edges of most "return" instructions, but we *do* want
810 // to know what can be returned.
811 if (auto *Ret = dyn_cast<ReturnInst>(&Inst))
812 ReturnedValues.push_back(Ret);
814 if (!hasUsefulEdges(&Inst))
818 argsToEdges(Analysis, &Inst, Edges);
820 // In the case of an unused alloca (or similar), edges may be empty. Note
821 // that it exists so we can potentially answer NoAlias.
823 auto MaybeVal = getTargetValue(&Inst);
824 assert(MaybeVal.hasValue());
825 auto *Target = *MaybeVal;
826 findOrInsertNode(Target);
830 for (const Edge &E : Edges) {
831 auto To = findOrInsertNode(E.To);
832 auto From = findOrInsertNode(E.From);
833 auto FlippedWeight = flipWeight(E.Weight);
834 auto Attrs = E.AdditionalAttrs;
835 Graph.addEdge(From, To, {E.Weight, Attrs}, {FlippedWeight, Attrs});
841 static FunctionInfo buildSetsFrom(CFLAliasAnalysis &Analysis, Function *Fn) {
844 SmallVector<Value *, 4> ReturnedValues;
846 buildGraphFrom(Analysis, Fn, ReturnedValues, Map, Graph);
848 DenseMap<GraphT::Node, Value *> NodeValueMap;
849 NodeValueMap.resize(Map.size());
850 for (const auto &Pair : Map)
851 NodeValueMap.insert({Pair.second, Pair.first});
853 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
854 auto ValIter = NodeValueMap.find(Node);
855 assert(ValIter != NodeValueMap.end());
856 return ValIter->second;
859 StratifiedSetsBuilder<Value *> Builder;
861 SmallVector<GraphT::Node, 16> Worklist;
862 for (auto &Pair : Map) {
865 auto *Value = Pair.first;
867 auto InitialNode = Pair.second;
868 Worklist.push_back(InitialNode);
869 while (!Worklist.empty()) {
870 auto Node = Worklist.pop_back_val();
871 auto *CurValue = findValueOrDie(Node);
872 if (isa<Constant>(CurValue) && !isa<GlobalValue>(CurValue))
875 for (const auto &EdgeTuple : Graph.edgesFor(Node)) {
876 auto Weight = std::get<0>(EdgeTuple);
877 auto Label = Weight.first;
878 auto &OtherNode = std::get<1>(EdgeTuple);
879 auto *OtherValue = findValueOrDie(OtherNode);
881 if (isa<Constant>(OtherValue) && !isa<GlobalValue>(OtherValue))
885 switch (directionOfEdgeType(Label)) {
887 Added = Builder.addAbove(CurValue, OtherValue);
890 Added = Builder.addBelow(CurValue, OtherValue);
893 Added = Builder.addWith(CurValue, OtherValue);
898 auto Aliasing = Weight.second;
899 if (auto MaybeCurIndex = valueToAttrIndex(CurValue))
900 Aliasing.set(*MaybeCurIndex);
901 if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
902 Aliasing.set(*MaybeOtherIndex);
903 Builder.noteAttributes(CurValue, Aliasing);
904 Builder.noteAttributes(OtherValue, Aliasing);
905 Worklist.push_back(OtherNode);
911 // There are times when we end up with parameters not in our graph (i.e. if
912 // it's only used as the condition of a branch). Other bits of code depend on
913 // things that were present during construction being present in the graph.
914 // So, we add all present arguments here.
915 for (auto &Arg : Fn->args()) {
919 return {Builder.build(), std::move(ReturnedValues)};
922 void CFLAliasAnalysis::scan(Function *Fn) {
923 auto InsertPair = Cache.insert({Fn, Optional<FunctionInfo>()});
925 assert(InsertPair.second &&
926 "Trying to scan a function that has already been cached");
928 FunctionInfo Info(buildSetsFrom(*this, Fn));
929 Cache[Fn] = std::move(Info);
930 Handles.push_front(FunctionHandle(Fn, this));
933 AliasAnalysis::AliasResult
934 CFLAliasAnalysis::query(const AliasAnalysis::Location &LocA,
935 const AliasAnalysis::Location &LocB) {
936 auto *ValA = const_cast<Value *>(LocA.Ptr);
937 auto *ValB = const_cast<Value *>(LocB.Ptr);
939 Function *Fn = nullptr;
940 auto MaybeFnA = parentFunctionOfValue(ValA);
941 auto MaybeFnB = parentFunctionOfValue(ValB);
942 if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) {
943 llvm_unreachable("Don't know how to extract the parent function "
944 "from values A or B");
947 if (MaybeFnA.hasValue()) {
949 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
950 "Interprocedural queries not supported");
955 assert(Fn != nullptr);
956 auto &MaybeInfo = ensureCached(Fn);
957 assert(MaybeInfo.hasValue());
959 auto &Sets = MaybeInfo->Sets;
960 auto MaybeA = Sets.find(ValA);
961 if (!MaybeA.hasValue())
962 return AliasAnalysis::MayAlias;
964 auto MaybeB = Sets.find(ValB);
965 if (!MaybeB.hasValue())
966 return AliasAnalysis::MayAlias;
971 if (SetA.Index == SetB.Index)
972 return AliasAnalysis::PartialAlias;
974 auto AttrsA = Sets.getLink(SetA.Index).Attrs;
975 auto AttrsB = Sets.getLink(SetB.Index).Attrs;
976 auto CombinedAttrs = AttrsA | AttrsB;
977 if (CombinedAttrs.any())
978 return AliasAnalysis::PartialAlias;
980 return AliasAnalysis::NoAlias;