blob: 4fdacd33df71a2978bdc90b89a7d0d38cb28d4c5 [file] [log] [blame]
Hal Finkel7529c552014-09-02 21:43:13 +00001//===- CFLAliasAnalysis.cpp - CFL-Based Alias Analysis Implementation ------==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a CFL-based context-insensitive alias analysis
11// algorithm. It does not depend on types. The algorithm is a mixture of the one
12// described in "Demand-driven alias analysis for C" by Xin Zheng and Radu
13// Rugina, and "Fast algorithms for Dyck-CFL-reachability with applications to
14// Alias Analysis" by Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the
15// papers, we build a graph of the uses of a variable, where each node is a
16// memory location, and each edge is an action that happened on that memory
17// location. The "actions" can be one of Dereference, Reference, Assign, or
18// Assign.
19//
20// Two variables are considered as aliasing iff you can reach one value's node
21// from the other value's node and the language formed by concatenating all of
22// the edge labels (actions) conforms to a context-free grammar.
23//
24// Because this algorithm requires a graph search on each query, we execute the
25// algorithm outlined in "Fast algorithms..." (mentioned above)
26// in order to transform the graph into sets of variables that may alias in
27// ~nlogn time (n = number of variables.), which makes queries take constant
28// time.
29//===----------------------------------------------------------------------===//
30
31#include "StratifiedSets.h"
32#include "llvm/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"
46#include <algorithm>
47#include <cassert>
48#include <forward_list>
49#include <tuple>
50
51using namespace llvm;
52
53// Try to go from a Value* to a Function*. Never returns nullptr.
54static Optional<Function *> parentFunctionOfValue(Value *);
55
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
60// myself.
61template <typename Inst>
62static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &);
63
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.
69static Optional<Value *> getTargetValue(Instruction *);
70
71// There are certain instructions (i.e. FenceInst, etc.) that we ignore.
72// This notes that we should ignore those.
73static bool hasUsefulEdges(Instruction *);
74
75namespace {
76// StratifiedInfo Attribute things.
77typedef unsigned StratifiedAttr;
78constexpr unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
79constexpr unsigned AttrAllIndex = 0;
80constexpr unsigned AttrGlobalIndex = 1;
81constexpr unsigned AttrFirstArgIndex = 2;
82constexpr unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
83constexpr unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
84
85constexpr StratifiedAttr AttrNone = 0;
86constexpr StratifiedAttr AttrAll = ~AttrNone;
87
88// \brief StratifiedSets call for knowledge of "direction", so this is how we
89// represent that locally.
90enum class Level { Same, Above, Below };
91
92// \brief Edges can be one of four "weights" -- each weight must have an inverse
93// weight (Assign has Assign; Reference has Dereference).
94enum class EdgeType {
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.
99 Assign,
100
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)
105 Dereference,
106
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)
111 Reference
112};
113
114// \brief Encodes the notion of a "use"
115struct Edge {
116 // \brief Which value the edge is coming from
117 Value *From;
118
119 // \brief Which value the edge is pointing to
120 Value *To;
121
122 // \brief Edge weight
123 EdgeType Weight;
124
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;
129
130 Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A)
131 : From(From), To(To), Weight(W), AdditionalAttrs(A) {}
132};
133
134// \brief Information we have about a function and would like to keep around
135struct FunctionInfo {
136 StratifiedSets<Value *> Sets;
137 // Lots of functions have < 4 returns. Adjust as necessary.
138 SmallVector<Value *, 4> ReturnedValues;
139};
140
141struct CFLAliasAnalysis;
142
143struct FunctionHandle : public CallbackVH {
144 FunctionHandle(Function *Fn, CFLAliasAnalysis *CFLAA)
145 : CallbackVH(Fn), CFLAA(CFLAA) {
146 assert(Fn != nullptr);
147 assert(CFLAA != nullptr);
148 }
149
150 virtual ~FunctionHandle() {}
151
152 virtual void deleted() override { removeSelfFromCache(); }
153 virtual void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
154
155private:
156 CFLAliasAnalysis *CFLAA;
157
158 void removeSelfFromCache();
159};
160
161struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
162private:
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;
170
171public:
172 static char ID;
173
174 CFLAliasAnalysis() : ImmutablePass(ID) {
175 initializeCFLAliasAnalysisPass(*PassRegistry::getPassRegistry());
176 }
177
178 virtual ~CFLAliasAnalysis() {}
179
180 void getAnalysisUsage(AnalysisUsage &AU) const {
181 AliasAnalysis::getAnalysisUsage(AU);
182 }
183
184 void *getAdjustedAnalysisPointer(const void *ID) override {
185 if (ID == &AliasAnalysis::ID)
186 return (AliasAnalysis *)this;
187 return this;
188 }
189
190 /// \brief Inserts the given Function into the cache.
191 void scan(Function *Fn);
192
193 void evict(Function *Fn) { Cache.erase(Fn); }
194
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()) {
200 scan(Fn);
201 Iter = Cache.find(Fn);
202 assert(Iter != Cache.end());
203 assert(Iter->second.hasValue());
204 }
205 return Iter->second;
206 }
207
208 AliasResult query(const Location &LocA, const Location &LocB);
209
210 AliasResult alias(const Location &LocA, const Location &LocB) override {
211 if (LocA.Ptr == LocB.Ptr) {
212 if (LocA.Size == LocB.Size) {
213 return MustAlias;
214 } else {
215 return PartialAlias;
216 }
217 }
218
219 // Comparisons between global variables and other constants should be
220 // handled by BasicAA.
221 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr)) {
222 return MayAlias;
223 }
224
225 return query(LocA, LocB);
226 }
227
228 void initializePass() override { InitializeAliasAnalysis(this); }
229};
230
231void FunctionHandle::removeSelfFromCache() {
232 assert(CFLAA != nullptr);
233 auto *Val = getValPtr();
234 CFLAA->evict(cast<Function>(Val));
235 setValPtr(nullptr);
236}
237
238// \brief Gets the edges our graph should have, based on an Instruction*
239class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
240 CFLAliasAnalysis &AA;
241 SmallVectorImpl<Edge> &Output;
242
243public:
244 GetEdgesVisitor(CFLAliasAnalysis &AA, SmallVectorImpl<Edge> &Output)
245 : AA(AA), Output(Output) {}
246
247 void visitInstruction(Instruction &) {
248 llvm_unreachable("Unsupported instruction encountered");
249 }
250
251 void visitCastInst(CastInst &Inst) {
252 Output.push_back({&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone});
253 }
254
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});
260 }
261
262 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
263 auto *Ptr = Inst.getPointerOperand();
264 auto *Val = Inst.getNewValOperand();
265 Output.push_back({Ptr, Val, EdgeType::Dereference, AttrNone});
266 }
267
268 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
269 auto *Ptr = Inst.getPointerOperand();
270 auto *Val = Inst.getValOperand();
271 Output.push_back({Ptr, Val, EdgeType::Dereference, AttrNone});
272 }
273
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});
278 }
279 }
280
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});
286 }
287
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});
295 }
296
297 void visitAllocaInst(AllocaInst &) {}
298
299 void visitLoadInst(LoadInst &Inst) {
300 auto *Ptr = Inst.getPointerOperand();
301 auto *Val = &Inst;
302 Output.push_back({Val, Ptr, EdgeType::Reference, AttrNone});
303 }
304
305 void visitStoreInst(StoreInst &Inst) {
306 auto *Ptr = Inst.getPointerOperand();
307 auto *Val = Inst.getValueOperand();
308 Output.push_back({Ptr, Val, EdgeType::Dereference, AttrNone});
309 }
310
311 static bool isFunctionExternal(Function *Fn) {
312 return Fn->isDeclaration() || !Fn->hasLocalLinkage();
313 }
314
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)
321 return Level::Same;
322
323 const auto *Current = &Sets.getLink(Index1);
324 while (Current->hasBelow()) {
325 if (Current->Below == Index2)
326 return Level::Below;
327 Current = &Sets.getLink(Current->Below);
328 }
329
330 Current = &Sets.getLink(Index1);
331 while (Current->hasAbove()) {
332 if (Current->Above == Index2)
333 return Level::Above;
334 Current = &Sets.getLink(Current->Above);
335 }
336
337 return NoneType();
338 }
339
340 bool
341 tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns,
342 Value *FuncValue,
343 const iterator_range<User::op_iterator> &Args) {
344 constexpr unsigned ExpectedMaxArgs = 8;
345 constexpr unsigned MaxSupportedArgs = 50;
346 assert(Fns.size() > 0);
347
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)
351 return false;
352
353 // Exit early if we'll fail anyway
354 for (auto *Fn : Fns) {
355 if (isFunctionExternal(Fn) || Fn->isVarArg())
356 return false;
357 auto &MaybeInfo = AA.ensureCached(Fn);
358 if (!MaybeInfo.hasValue())
359 return false;
360 }
361
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;
368
369 Parameters.clear();
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())
374 return false;
375 Parameters.push_back(*MaybeInfo);
376 }
377
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())
388 return false;
389
390 auto &RetInfo = *MaybeInfo;
391 auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs;
392 auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs;
393 auto MaybeRelation =
394 getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index);
395 if (MaybeRelation.hasValue()) {
396 AddEdge = true;
397 Externals |= RetAttrs | ParamAttrs;
398 }
399 }
400 if (AddEdge)
401 Output.push_back({FuncValue, ArgVal, EdgeType::Assign,
402 StratifiedAttrs().flip()});
403 }
404
405 if (Parameters.size() != Arguments.size())
406 return false;
407
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;
422 auto MaybeRelation =
423 getIndexRelation(Sets, MainInfo.Index, SubInfo.Index);
424
425 if (!MaybeRelation.hasValue())
426 continue;
427
428 auto NewAttrs = SubAttrs | MainAttrs;
429 Output.push_back({MainVal, SubVal, EdgeType::Assign, NewAttrs});
430 }
431 }
432 }
433 return true;
434 }
435
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()))
440 return;
441 // Cleanup from interprocedural analysis
442 Output.clear();
443 }
444
445 for (Value *V : Inst.arg_operands())
446 Output.push_back({&Inst, V, EdgeType::Assign, AttrAll});
447 }
448
449 void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); }
450
451 void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); }
452
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
457 // and load from.
458 void visitExtractElementInst(ExtractElementInst &Inst) {
459 auto *Ptr = Inst.getVectorOperand();
460 auto *Val = &Inst;
461 Output.push_back({Val, Ptr, EdgeType::Reference, AttrNone});
462 }
463
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});
469 }
470
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
474 // alias externals
475 Output.push_back({&Inst, &Inst, EdgeType::Assign, AttrAll});
476 }
477
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});
483 }
484
485 void visitExtractValueInst(ExtractValueInst &Inst) {
486 auto *Ptr = Inst.getAggregateOperand();
487 Output.push_back({&Inst, Ptr, EdgeType::Reference, AttrNone});
488 }
489
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});
495 }
496};
497
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.
504//
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.
508class GetTargetValueVisitor
509 : public InstVisitor<GetTargetValueVisitor, Value *> {
510public:
511 Value *visitInstruction(Instruction &Inst) { return &Inst; }
512
513 Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); }
514
515 Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
516 return Inst.getPointerOperand();
517 }
518
519 Value *visitAtomicRMWInst(AtomicRMWInst &Inst) {
520 return Inst.getPointerOperand();
521 }
522
523 Value *visitInsertElementInst(InsertElementInst &Inst) {
524 return Inst.getOperand(0);
525 }
526
527 Value *visitInsertValueInst(InsertValueInst &Inst) {
528 return Inst.getAggregateOperand();
529 }
530};
531
532// Set building requires a weighted bidirectional graph.
533template <typename EdgeTypeT> class WeightedBidirectionalGraph {
534public:
535 typedef std::size_t Node;
536
537private:
538 constexpr static Node StartNode = Node(0);
539
540 struct Edge {
541 EdgeTypeT Weight;
542 Node Other;
543
544 bool operator==(const Edge &E) const {
545 return Weight == E.Weight && Other == E.Other;
546 }
547
548 bool operator!=(const Edge &E) const { return !operator==(E); }
549 };
550
551 struct NodeImpl {
552 std::vector<Edge> Edges;
553 };
554
555 std::vector<NodeImpl> NodeImpls;
556
557 bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); }
558
559 const NodeImpl &getNode(Node N) const { return NodeImpls[N]; }
560 NodeImpl &getNode(Node N) { return NodeImpls[N]; }
561
562public:
563 // ----- Various Edge iterators for the graph ----- //
564
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)
572 : Current(Iter) {}
573
574 EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {}
575
576 EdgeIterator &operator++() {
577 ++Current;
578 return *this;
579 }
580
581 EdgeIterator operator++(int) {
582 EdgeIterator Copy(Current);
583 operator++();
584 return Copy;
585 }
586
587 std::tuple<EdgeTypeT, Node> &operator*() {
588 Store = std::make_tuple(Current->Weight, Current->Other);
589 return Store;
590 }
591
592 bool operator==(const EdgeIterator &Other) const {
593 return Current == Other.Current;
594 }
595
596 bool operator!=(const EdgeIterator &Other) const {
597 return !operator==(Other);
598 }
599
600 private:
601 typename std::vector<Edge>::const_iterator Current;
602 std::tuple<EdgeTypeT, Node> Store;
603 };
604
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()) {}
609
610 EdgeIterator begin() { return EdgeIterator(BeginIter); }
611
612 EdgeIterator end() { return EdgeIterator(EndIter); }
613
614 private:
615 typename std::vector<Edge>::const_iterator BeginIter;
616 typename std::vector<Edge>::const_iterator EndIter;
617 };
618
619 // ----- Actual graph-related things ----- //
620
621 WeightedBidirectionalGraph() = default;
622
623 WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other)
624 : NodeImpls(std::move(Other.NodeImpls)) {}
625
626 WeightedBidirectionalGraph<EdgeTypeT> &
627 operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) {
628 NodeImpls = std::move(Other.NodeImpls);
629 return *this;
630 }
631
632 Node addNode() {
633 auto Index = NodeImpls.size();
634 auto NewNode = Node(Index);
635 NodeImpls.push_back(NodeImpl());
636 return NewNode;
637 }
638
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});
647 }
648
649 EdgeIterable edgesFor(const Node &N) const {
650 const auto &Node = getNode(N);
651 return EdgeIterable(Node.Edges);
652 }
653
654 bool empty() const { return NodeImpls.empty(); }
655 std::size_t size() const { return NodeImpls.size(); }
656
657 // \brief Gets an arbitrary node in the graph as a starting point for
658 // traversal.
659 Node getEntryNode() {
660 assert(inbounds(StartNode));
661 return StartNode;
662 }
663};
664
665typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT;
666typedef DenseMap<Value *, GraphT::Node> NodeMapT;
667}
668
669// -- Setting up/registering CFLAA pass -- //
670char CFLAliasAnalysis::ID = 0;
671
672INITIALIZE_AG_PASS(CFLAliasAnalysis, AliasAnalysis, "cfl-aa",
673 "CFL-Based AA implementation", false, true, false)
674
675ImmutablePass *llvm::createCFLAliasAnalysisPass() {
676 return new CFLAliasAnalysis();
677}
678
679//===----------------------------------------------------------------------===//
680// Function declarations that require types defined in the namespace above
681//===----------------------------------------------------------------------===//
682
683// Given an argument number, returns the appropriate Attr index to set.
684static StratifiedAttr argNumberToAttrIndex(StratifiedAttr);
685
686// Given a Value, potentially return which AttrIndex it maps to.
687static Optional<StratifiedAttr> valueToAttrIndex(Value *Val);
688
689// Gets the inverse of a given EdgeType.
690static EdgeType flipWeight(EdgeType);
691
692// Gets edges of the given Instruction*, writing them to the SmallVector*.
693static void argsToEdges(CFLAliasAnalysis &, Instruction *,
694 SmallVectorImpl<Edge> &);
695
696// Gets the "Level" that one should travel in StratifiedSets
697// given an EdgeType.
698static Level directionOfEdgeType(EdgeType);
699
700// Builds the graph needed for constructing the StratifiedSets for the
701// given function
702static void buildGraphFrom(CFLAliasAnalysis &, Function *,
703 SmallVectorImpl<Value *> &, NodeMapT &, GraphT &);
704
705// Builds the graph + StratifiedSets for a function.
706static FunctionInfo buildSetsFrom(CFLAliasAnalysis &, Function *);
707
708static Optional<Function *> parentFunctionOfValue(Value *Val) {
709 if (auto *Inst = dyn_cast<Instruction>(Val)) {
710 auto *Bb = Inst->getParent();
711 return Bb->getParent();
712 }
713
714 if (auto *Arg = dyn_cast<Argument>(Val))
715 return Arg->getParent();
716 return NoneType();
717}
718
719template <typename Inst>
720static bool getPossibleTargets(Inst *Call,
721 SmallVectorImpl<Function *> &Output) {
722 if (auto *Fn = Call->getCalledFunction()) {
723 Output.push_back(Fn);
724 return true;
725 }
726
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.
729 return false;
730}
731
732static Optional<Value *> getTargetValue(Instruction *Inst) {
733 GetTargetValueVisitor V;
734 return V.visit(Inst);
735}
736
737static bool hasUsefulEdges(Instruction *Inst) {
738 bool IsNonInvokeTerminator =
739 isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst);
740 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
741}
742
743static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
744 if (isa<GlobalValue>(Val))
745 return AttrGlobalIndex;
746
747 if (auto *Arg = dyn_cast<Argument>(Val))
748 if (!Arg->hasNoAliasAttr())
749 return argNumberToAttrIndex(Arg->getArgNo());
750 return NoneType();
751}
752
753static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) {
754 if (ArgNum > AttrMaxNumArgs)
755 return AttrAllIndex;
756 return ArgNum + AttrFirstArgIndex;
757}
758
759static EdgeType flipWeight(EdgeType Initial) {
760 switch (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;
767 }
768 llvm_unreachable("Incomplete coverage of EdgeType enum");
769}
770
771static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst,
772 SmallVectorImpl<Edge> &Output) {
773 GetEdgesVisitor v(Analysis, Output);
774 v.visit(Inst);
775}
776
777static Level directionOfEdgeType(EdgeType Weight) {
778 switch (Weight) {
779 case EdgeType::Reference:
780 return Level::Above;
781 case EdgeType::Dereference:
782 return Level::Below;
783 case EdgeType::Assign:
784 return Level::Same;
785 }
786 llvm_unreachable("Incomplete switch coverage");
787}
788
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
793static 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;
799 if (Pair.second) {
800 auto NewNode = Graph.addNode();
801 Iter->second = NewNode;
802 }
803 return Iter->second;
804 };
805
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);
813
814 if (!hasUsefulEdges(&Inst))
815 continue;
816
817 Edges.clear();
818 argsToEdges(Analysis, &Inst, Edges);
819
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.
822 if (Edges.empty()) {
823 auto MaybeVal = getTargetValue(&Inst);
824 assert(MaybeVal.hasValue());
825 auto *Target = *MaybeVal;
826 findOrInsertNode(Target);
827 continue;
828 }
829
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});
836 }
837 }
838 }
839}
840
841static FunctionInfo buildSetsFrom(CFLAliasAnalysis &Analysis, Function *Fn) {
842 NodeMapT Map;
843 GraphT Graph;
844 SmallVector<Value *, 4> ReturnedValues;
845
846 buildGraphFrom(Analysis, Fn, ReturnedValues, Map, Graph);
847
848 DenseMap<GraphT::Node, Value *> NodeValueMap;
849 NodeValueMap.resize(Map.size());
850 for (const auto &Pair : Map)
851 NodeValueMap.insert({Pair.second, Pair.first});
852
853 const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) {
854 auto ValIter = NodeValueMap.find(Node);
855 assert(ValIter != NodeValueMap.end());
856 return ValIter->second;
857 };
858
859 StratifiedSetsBuilder<Value *> Builder;
860
861 SmallVector<GraphT::Node, 16> Worklist;
862 for (auto &Pair : Map) {
863 Worklist.clear();
864
865 auto *Value = Pair.first;
866 Builder.add(Value);
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))
873 continue;
874
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);
880
881 if (isa<Constant>(OtherValue) && !isa<GlobalValue>(OtherValue))
882 continue;
883
884 bool Added;
885 switch (directionOfEdgeType(Label)) {
886 case Level::Above:
887 Added = Builder.addAbove(CurValue, OtherValue);
888 break;
889 case Level::Below:
890 Added = Builder.addBelow(CurValue, OtherValue);
891 break;
892 case Level::Same:
893 Added = Builder.addWith(CurValue, OtherValue);
894 break;
895 }
896
897 if (Added) {
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);
906 }
907 }
908 }
909 }
910
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()) {
916 Builder.add(&Arg);
917 }
918
919 return {Builder.build(), std::move(ReturnedValues)};
920}
921
922void CFLAliasAnalysis::scan(Function *Fn) {
923 auto InsertPair = Cache.insert({Fn, Optional<FunctionInfo>()});
924 (void)InsertPair;
925 assert(InsertPair.second &&
926 "Trying to scan a function that has already been cached");
927
928 FunctionInfo Info(buildSetsFrom(*this, Fn));
929 Cache[Fn] = std::move(Info);
930 Handles.push_front(FunctionHandle(Fn, this));
931}
932
933AliasAnalysis::AliasResult
934CFLAliasAnalysis::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);
938
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");
945 }
946
947 if (MaybeFnA.hasValue()) {
948 Fn = *MaybeFnA;
949 assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) &&
950 "Interprocedural queries not supported");
951 } else {
952 Fn = *MaybeFnB;
953 }
954
955 assert(Fn != nullptr);
956 auto &MaybeInfo = ensureCached(Fn);
957 assert(MaybeInfo.hasValue());
958
959 auto &Sets = MaybeInfo->Sets;
960 auto MaybeA = Sets.find(ValA);
961 if (!MaybeA.hasValue())
962 return AliasAnalysis::MayAlias;
963
964 auto MaybeB = Sets.find(ValB);
965 if (!MaybeB.hasValue())
966 return AliasAnalysis::MayAlias;
967
968 auto SetA = *MaybeA;
969 auto SetB = *MaybeB;
970
971 if (SetA.Index == SetB.Index)
972 return AliasAnalysis::PartialAlias;
973
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;
979
980 return AliasAnalysis::NoAlias;
981}