1 //===- CFLGraph.h - Abstract stratified sets implementation. -----*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file defines CFLGraph, an auxiliary data structure used by CFL-based
11 /// alias analysis.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_ANALYSIS_CFLGRAPH_H
16 #define LLVM_LIB_ANALYSIS_CFLGRAPH_H
17 
18 #include "AliasAnalysisSummary.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/Analysis/MemoryBuiltins.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/Argument.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GlobalValue.h"
31 #include "llvm/IR/InstVisitor.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Operator.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/Value.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include <cassert>
41 #include <cstdint>
42 #include <vector>
43 
44 namespace llvm {
45 namespace cflaa {
46 
47 /// The Program Expression Graph (PEG) of CFL analysis
48 /// CFLGraph is auxiliary data structure used by CFL-based alias analysis to
49 /// describe flow-insensitive pointer-related behaviors. Given an LLVM function,
50 /// the main purpose of this graph is to abstract away unrelated facts and
51 /// translate the rest into a form that can be easily digested by CFL analyses.
52 /// Each Node in the graph is an InstantiatedValue, and each edge represent a
53 /// pointer assignment between InstantiatedValue. Pointer
54 /// references/dereferences are not explicitly stored in the graph: we
55 /// implicitly assume that for each node (X, I) it has a dereference edge to (X,
56 /// I+1) and a reference edge to (X, I-1).
57 class CFLGraph {
58 public:
59   using Node = InstantiatedValue;
60 
61   struct Edge {
62     Node Other;
63     int64_t Offset;
64   };
65 
66   using EdgeList = std::vector<Edge>;
67 
68   struct NodeInfo {
69     EdgeList Edges, ReverseEdges;
70     AliasAttrs Attr;
71   };
72 
73   class ValueInfo {
74     std::vector<NodeInfo> Levels;
75 
76   public:
addNodeToLevel(unsigned Level)77     bool addNodeToLevel(unsigned Level) {
78       auto NumLevels = Levels.size();
79       if (NumLevels > Level)
80         return false;
81       Levels.resize(Level + 1);
82       return true;
83     }
84 
getNodeInfoAtLevel(unsigned Level)85     NodeInfo &getNodeInfoAtLevel(unsigned Level) {
86       assert(Level < Levels.size());
87       return Levels[Level];
88     }
getNodeInfoAtLevel(unsigned Level)89     const NodeInfo &getNodeInfoAtLevel(unsigned Level) const {
90       assert(Level < Levels.size());
91       return Levels[Level];
92     }
93 
getNumLevels()94     unsigned getNumLevels() const { return Levels.size(); }
95   };
96 
97 private:
98   using ValueMap = DenseMap<Value *, ValueInfo>;
99 
100   ValueMap ValueImpls;
101 
getNode(Node N)102   NodeInfo *getNode(Node N) {
103     auto Itr = ValueImpls.find(N.Val);
104     if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
105       return nullptr;
106     return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
107   }
108 
109 public:
110   using const_value_iterator = ValueMap::const_iterator;
111 
112   bool addNode(Node N, AliasAttrs Attr = AliasAttrs()) {
113     assert(N.Val != nullptr);
114     auto &ValInfo = ValueImpls[N.Val];
115     auto Changed = ValInfo.addNodeToLevel(N.DerefLevel);
116     ValInfo.getNodeInfoAtLevel(N.DerefLevel).Attr |= Attr;
117     return Changed;
118   }
119 
addAttr(Node N,AliasAttrs Attr)120   void addAttr(Node N, AliasAttrs Attr) {
121     auto *Info = getNode(N);
122     assert(Info != nullptr);
123     Info->Attr |= Attr;
124   }
125 
126   void addEdge(Node From, Node To, int64_t Offset = 0) {
127     auto *FromInfo = getNode(From);
128     assert(FromInfo != nullptr);
129     auto *ToInfo = getNode(To);
130     assert(ToInfo != nullptr);
131 
132     FromInfo->Edges.push_back(Edge{To, Offset});
133     ToInfo->ReverseEdges.push_back(Edge{From, Offset});
134   }
135 
getNode(Node N)136   const NodeInfo *getNode(Node N) const {
137     auto Itr = ValueImpls.find(N.Val);
138     if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
139       return nullptr;
140     return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
141   }
142 
attrFor(Node N)143   AliasAttrs attrFor(Node N) const {
144     auto *Info = getNode(N);
145     assert(Info != nullptr);
146     return Info->Attr;
147   }
148 
value_mappings()149   iterator_range<const_value_iterator> value_mappings() const {
150     return make_range<const_value_iterator>(ValueImpls.begin(),
151                                             ValueImpls.end());
152   }
153 };
154 
155 /// A builder class used to create CFLGraph instance from a given function
156 /// The CFL-AA that uses this builder must provide its own type as a template
157 /// argument. This is necessary for interprocedural processing: CFLGraphBuilder
158 /// needs a way of obtaining the summary of other functions when callinsts are
159 /// encountered.
160 /// As a result, we expect the said CFL-AA to expose a getAliasSummary() public
161 /// member function that takes a Function& and returns the corresponding summary
162 /// as a const AliasSummary*.
163 template <typename CFLAA> class CFLGraphBuilder {
164   // Input of the builder
165   CFLAA &Analysis;
166   const TargetLibraryInfo &TLI;
167 
168   // Output of the builder
169   CFLGraph Graph;
170   SmallVector<Value *, 4> ReturnedValues;
171 
172   // Helper class
173   /// Gets the edges our graph should have, based on an Instruction*
174   class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
175     CFLAA &AA;
176     const DataLayout &DL;
177     const TargetLibraryInfo &TLI;
178 
179     CFLGraph &Graph;
180     SmallVectorImpl<Value *> &ReturnValues;
181 
hasUsefulEdges(ConstantExpr * CE)182     static bool hasUsefulEdges(ConstantExpr *CE) {
183       // ConstantExpr doesn't have terminators, invokes, or fences, so only
184       // needs to check for compares.
185       return CE->getOpcode() != Instruction::ICmp &&
186              CE->getOpcode() != Instruction::FCmp;
187     }
188 
189     // Returns possible functions called by CS into the given SmallVectorImpl.
190     // Returns true if targets found, false otherwise.
getPossibleTargets(CallBase & Call,SmallVectorImpl<Function * > & Output)191     static bool getPossibleTargets(CallBase &Call,
192                                    SmallVectorImpl<Function *> &Output) {
193       if (auto *Fn = Call.getCalledFunction()) {
194         Output.push_back(Fn);
195         return true;
196       }
197 
198       // TODO: If the call is indirect, we might be able to enumerate all
199       // potential targets of the call and return them, rather than just
200       // failing.
201       return false;
202     }
203 
204     void addNode(Value *Val, AliasAttrs Attr = AliasAttrs()) {
205       assert(Val != nullptr && Val->getType()->isPointerTy());
206       if (auto GVal = dyn_cast<GlobalValue>(Val)) {
207         if (Graph.addNode(InstantiatedValue{GVal, 0},
208                           getGlobalOrArgAttrFromValue(*GVal)))
209           Graph.addNode(InstantiatedValue{GVal, 1}, getAttrUnknown());
210       } else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) {
211         if (hasUsefulEdges(CExpr)) {
212           if (Graph.addNode(InstantiatedValue{CExpr, 0}))
213             visitConstantExpr(CExpr);
214         }
215       } else
216         Graph.addNode(InstantiatedValue{Val, 0}, Attr);
217     }
218 
219     void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) {
220       assert(From != nullptr && To != nullptr);
221       if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
222         return;
223       addNode(From);
224       if (To != From) {
225         addNode(To);
226         Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 0},
227                       Offset);
228       }
229     }
230 
addDerefEdge(Value * From,Value * To,bool IsRead)231     void addDerefEdge(Value *From, Value *To, bool IsRead) {
232       assert(From != nullptr && To != nullptr);
233       // FIXME: This is subtly broken, due to how we model some instructions
234       // (e.g. extractvalue, extractelement) as loads. Since those take
235       // non-pointer operands, we'll entirely skip adding edges for those.
236       //
237       // addAssignEdge seems to have a similar issue with insertvalue, etc.
238       if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
239         return;
240       addNode(From);
241       addNode(To);
242       if (IsRead) {
243         Graph.addNode(InstantiatedValue{From, 1});
244         Graph.addEdge(InstantiatedValue{From, 1}, InstantiatedValue{To, 0});
245       } else {
246         Graph.addNode(InstantiatedValue{To, 1});
247         Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 1});
248       }
249     }
250 
addLoadEdge(Value * From,Value * To)251     void addLoadEdge(Value *From, Value *To) { addDerefEdge(From, To, true); }
addStoreEdge(Value * From,Value * To)252     void addStoreEdge(Value *From, Value *To) { addDerefEdge(From, To, false); }
253 
254   public:
GetEdgesVisitor(CFLGraphBuilder & Builder,const DataLayout & DL)255     GetEdgesVisitor(CFLGraphBuilder &Builder, const DataLayout &DL)
256         : AA(Builder.Analysis), DL(DL), TLI(Builder.TLI), Graph(Builder.Graph),
257           ReturnValues(Builder.ReturnedValues) {}
258 
visitInstruction(Instruction &)259     void visitInstruction(Instruction &) {
260       llvm_unreachable("Unsupported instruction encountered");
261     }
262 
visitReturnInst(ReturnInst & Inst)263     void visitReturnInst(ReturnInst &Inst) {
264       if (auto RetVal = Inst.getReturnValue()) {
265         if (RetVal->getType()->isPointerTy()) {
266           addNode(RetVal);
267           ReturnValues.push_back(RetVal);
268         }
269       }
270     }
271 
visitPtrToIntInst(PtrToIntInst & Inst)272     void visitPtrToIntInst(PtrToIntInst &Inst) {
273       auto *Ptr = Inst.getOperand(0);
274       addNode(Ptr, getAttrEscaped());
275     }
276 
visitIntToPtrInst(IntToPtrInst & Inst)277     void visitIntToPtrInst(IntToPtrInst &Inst) {
278       auto *Ptr = &Inst;
279       addNode(Ptr, getAttrUnknown());
280     }
281 
visitCastInst(CastInst & Inst)282     void visitCastInst(CastInst &Inst) {
283       auto *Src = Inst.getOperand(0);
284       addAssignEdge(Src, &Inst);
285     }
286 
visitFreezeInst(FreezeInst & Inst)287     void visitFreezeInst(FreezeInst &Inst) {
288       // Accessing freeze(ptr) is equivalent to accessing ptr.
289       // The former raises UB iff latter raises UB.
290       auto *Src = Inst.getOperand(0);
291       addAssignEdge(Src, &Inst);
292     }
293 
visitBinaryOperator(BinaryOperator & Inst)294     void visitBinaryOperator(BinaryOperator &Inst) {
295       auto *Op1 = Inst.getOperand(0);
296       auto *Op2 = Inst.getOperand(1);
297       addAssignEdge(Op1, &Inst);
298       addAssignEdge(Op2, &Inst);
299     }
300 
visitUnaryOperator(UnaryOperator & Inst)301     void visitUnaryOperator(UnaryOperator &Inst) {
302       auto *Src = Inst.getOperand(0);
303       addAssignEdge(Src, &Inst);
304     }
305 
visitAtomicCmpXchgInst(AtomicCmpXchgInst & Inst)306     void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
307       auto *Ptr = Inst.getPointerOperand();
308       auto *Val = Inst.getNewValOperand();
309       addStoreEdge(Val, Ptr);
310     }
311 
visitAtomicRMWInst(AtomicRMWInst & Inst)312     void visitAtomicRMWInst(AtomicRMWInst &Inst) {
313       auto *Ptr = Inst.getPointerOperand();
314       auto *Val = Inst.getValOperand();
315       addStoreEdge(Val, Ptr);
316     }
317 
visitPHINode(PHINode & Inst)318     void visitPHINode(PHINode &Inst) {
319       for (Value *Val : Inst.incoming_values())
320         addAssignEdge(Val, &Inst);
321     }
322 
visitGEP(GEPOperator & GEPOp)323     void visitGEP(GEPOperator &GEPOp) {
324       uint64_t Offset = UnknownOffset;
325       APInt APOffset(DL.getPointerSizeInBits(GEPOp.getPointerAddressSpace()),
326                      0);
327       if (GEPOp.accumulateConstantOffset(DL, APOffset))
328         Offset = APOffset.getSExtValue();
329 
330       auto *Op = GEPOp.getPointerOperand();
331       addAssignEdge(Op, &GEPOp, Offset);
332     }
333 
visitGetElementPtrInst(GetElementPtrInst & Inst)334     void visitGetElementPtrInst(GetElementPtrInst &Inst) {
335       auto *GEPOp = cast<GEPOperator>(&Inst);
336       visitGEP(*GEPOp);
337     }
338 
visitSelectInst(SelectInst & Inst)339     void visitSelectInst(SelectInst &Inst) {
340       // Condition is not processed here (The actual statement producing
341       // the condition result is processed elsewhere). For select, the
342       // condition is evaluated, but not loaded, stored, or assigned
343       // simply as a result of being the condition of a select.
344 
345       auto *TrueVal = Inst.getTrueValue();
346       auto *FalseVal = Inst.getFalseValue();
347       addAssignEdge(TrueVal, &Inst);
348       addAssignEdge(FalseVal, &Inst);
349     }
350 
visitAllocaInst(AllocaInst & Inst)351     void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); }
352 
visitLoadInst(LoadInst & Inst)353     void visitLoadInst(LoadInst &Inst) {
354       auto *Ptr = Inst.getPointerOperand();
355       auto *Val = &Inst;
356       addLoadEdge(Ptr, Val);
357     }
358 
visitStoreInst(StoreInst & Inst)359     void visitStoreInst(StoreInst &Inst) {
360       auto *Ptr = Inst.getPointerOperand();
361       auto *Val = Inst.getValueOperand();
362       addStoreEdge(Val, Ptr);
363     }
364 
visitVAArgInst(VAArgInst & Inst)365     void visitVAArgInst(VAArgInst &Inst) {
366       // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it
367       // does
368       // two things:
369       //  1. Loads a value from *((T*)*Ptr).
370       //  2. Increments (stores to) *Ptr by some target-specific amount.
371       // For now, we'll handle this like a landingpad instruction (by placing
372       // the
373       // result in its own group, and having that group alias externals).
374       if (Inst.getType()->isPointerTy())
375         addNode(&Inst, getAttrUnknown());
376     }
377 
isFunctionExternal(Function * Fn)378     static bool isFunctionExternal(Function *Fn) {
379       return !Fn->hasExactDefinition();
380     }
381 
tryInterproceduralAnalysis(CallBase & Call,const SmallVectorImpl<Function * > & Fns)382     bool tryInterproceduralAnalysis(CallBase &Call,
383                                     const SmallVectorImpl<Function *> &Fns) {
384       assert(Fns.size() > 0);
385 
386       if (Call.arg_size() > MaxSupportedArgsInSummary)
387         return false;
388 
389       // Exit early if we'll fail anyway
390       for (auto *Fn : Fns) {
391         if (isFunctionExternal(Fn) || Fn->isVarArg())
392           return false;
393         // Fail if the caller does not provide enough arguments
394         assert(Fn->arg_size() <= Call.arg_size());
395         if (!AA.getAliasSummary(*Fn))
396           return false;
397       }
398 
399       for (auto *Fn : Fns) {
400         auto Summary = AA.getAliasSummary(*Fn);
401         assert(Summary != nullptr);
402 
403         auto &RetParamRelations = Summary->RetParamRelations;
404         for (auto &Relation : RetParamRelations) {
405           auto IRelation = instantiateExternalRelation(Relation, Call);
406           if (IRelation) {
407             Graph.addNode(IRelation->From);
408             Graph.addNode(IRelation->To);
409             Graph.addEdge(IRelation->From, IRelation->To);
410           }
411         }
412 
413         auto &RetParamAttributes = Summary->RetParamAttributes;
414         for (auto &Attribute : RetParamAttributes) {
415           auto IAttr = instantiateExternalAttribute(Attribute, Call);
416           if (IAttr)
417             Graph.addNode(IAttr->IValue, IAttr->Attr);
418         }
419       }
420 
421       return true;
422     }
423 
visitCallBase(CallBase & Call)424     void visitCallBase(CallBase &Call) {
425       // Make sure all arguments and return value are added to the graph first
426       for (Value *V : Call.args())
427         if (V->getType()->isPointerTy())
428           addNode(V);
429       if (Call.getType()->isPointerTy())
430         addNode(&Call);
431 
432       // Check if Inst is a call to a library function that
433       // allocates/deallocates on the heap. Those kinds of functions do not
434       // introduce any aliases.
435       // TODO: address other common library functions such as realloc(),
436       // strdup(), etc.
437       if (isMallocOrCallocLikeFn(&Call, &TLI) ||
438           getFreedOperand(&Call, &TLI) != nullptr)
439         return;
440 
441       // TODO: Add support for noalias args/all the other fun function
442       // attributes that we can tack on.
443       SmallVector<Function *, 4> Targets;
444       if (getPossibleTargets(Call, Targets))
445         if (tryInterproceduralAnalysis(Call, Targets))
446           return;
447 
448       // Because the function is opaque, we need to note that anything
449       // could have happened to the arguments (unless the function is marked
450       // readonly or readnone), and that the result could alias just about
451       // anything, too (unless the result is marked noalias).
452       if (!Call.onlyReadsMemory())
453         for (Value *V : Call.args()) {
454           if (V->getType()->isPointerTy()) {
455             // The argument itself escapes.
456             Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped());
457             // The fate of argument memory is unknown. Note that since
458             // AliasAttrs is transitive with respect to dereference, we only
459             // need to specify it for the first-level memory.
460             Graph.addNode(InstantiatedValue{V, 1}, getAttrUnknown());
461           }
462         }
463 
464       if (Call.getType()->isPointerTy()) {
465         auto *Fn = Call.getCalledFunction();
466         if (Fn == nullptr || !Fn->returnDoesNotAlias())
467           // No need to call addNode() since we've added Inst at the
468           // beginning of this function and we know it is not a global.
469           Graph.addAttr(InstantiatedValue{&Call, 0}, getAttrUnknown());
470       }
471     }
472 
473     /// Because vectors/aggregates are immutable and unaddressable, there's
474     /// nothing we can do to coax a value out of them, other than calling
475     /// Extract{Element,Value}. We can effectively treat them as pointers to
476     /// arbitrary memory locations we can store in and load from.
visitExtractElementInst(ExtractElementInst & Inst)477     void visitExtractElementInst(ExtractElementInst &Inst) {
478       auto *Ptr = Inst.getVectorOperand();
479       auto *Val = &Inst;
480       addLoadEdge(Ptr, Val);
481     }
482 
visitInsertElementInst(InsertElementInst & Inst)483     void visitInsertElementInst(InsertElementInst &Inst) {
484       auto *Vec = Inst.getOperand(0);
485       auto *Val = Inst.getOperand(1);
486       addAssignEdge(Vec, &Inst);
487       addStoreEdge(Val, &Inst);
488     }
489 
visitLandingPadInst(LandingPadInst & Inst)490     void visitLandingPadInst(LandingPadInst &Inst) {
491       // Exceptions come from "nowhere", from our analysis' perspective.
492       // So we place the instruction its own group, noting that said group may
493       // alias externals
494       if (Inst.getType()->isPointerTy())
495         addNode(&Inst, getAttrUnknown());
496     }
497 
visitInsertValueInst(InsertValueInst & Inst)498     void visitInsertValueInst(InsertValueInst &Inst) {
499       auto *Agg = Inst.getOperand(0);
500       auto *Val = Inst.getOperand(1);
501       addAssignEdge(Agg, &Inst);
502       addStoreEdge(Val, &Inst);
503     }
504 
visitExtractValueInst(ExtractValueInst & Inst)505     void visitExtractValueInst(ExtractValueInst &Inst) {
506       auto *Ptr = Inst.getAggregateOperand();
507       addLoadEdge(Ptr, &Inst);
508     }
509 
visitShuffleVectorInst(ShuffleVectorInst & Inst)510     void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
511       auto *From1 = Inst.getOperand(0);
512       auto *From2 = Inst.getOperand(1);
513       addAssignEdge(From1, &Inst);
514       addAssignEdge(From2, &Inst);
515     }
516 
visitConstantExpr(ConstantExpr * CE)517     void visitConstantExpr(ConstantExpr *CE) {
518       switch (CE->getOpcode()) {
519       case Instruction::GetElementPtr: {
520         auto GEPOp = cast<GEPOperator>(CE);
521         visitGEP(*GEPOp);
522         break;
523       }
524 
525       case Instruction::PtrToInt: {
526         addNode(CE->getOperand(0), getAttrEscaped());
527         break;
528       }
529 
530       case Instruction::IntToPtr: {
531         addNode(CE, getAttrUnknown());
532         break;
533       }
534 
535       case Instruction::BitCast:
536       case Instruction::AddrSpaceCast:
537       case Instruction::Trunc:
538       case Instruction::ZExt:
539       case Instruction::SExt:
540       case Instruction::FPExt:
541       case Instruction::FPTrunc:
542       case Instruction::UIToFP:
543       case Instruction::SIToFP:
544       case Instruction::FPToUI:
545       case Instruction::FPToSI: {
546         addAssignEdge(CE->getOperand(0), CE);
547         break;
548       }
549 
550       case Instruction::Select: {
551         addAssignEdge(CE->getOperand(1), CE);
552         addAssignEdge(CE->getOperand(2), CE);
553         break;
554       }
555 
556       case Instruction::InsertElement:
557       case Instruction::InsertValue: {
558         addAssignEdge(CE->getOperand(0), CE);
559         addStoreEdge(CE->getOperand(1), CE);
560         break;
561       }
562 
563       case Instruction::ExtractElement:
564       case Instruction::ExtractValue: {
565         addLoadEdge(CE->getOperand(0), CE);
566         break;
567       }
568 
569       case Instruction::Add:
570       case Instruction::FAdd:
571       case Instruction::Sub:
572       case Instruction::FSub:
573       case Instruction::Mul:
574       case Instruction::FMul:
575       case Instruction::UDiv:
576       case Instruction::SDiv:
577       case Instruction::FDiv:
578       case Instruction::URem:
579       case Instruction::SRem:
580       case Instruction::FRem:
581       case Instruction::And:
582       case Instruction::Or:
583       case Instruction::Xor:
584       case Instruction::Shl:
585       case Instruction::LShr:
586       case Instruction::AShr:
587       case Instruction::ICmp:
588       case Instruction::FCmp:
589       case Instruction::ShuffleVector: {
590         addAssignEdge(CE->getOperand(0), CE);
591         addAssignEdge(CE->getOperand(1), CE);
592         break;
593       }
594 
595       case Instruction::FNeg: {
596         addAssignEdge(CE->getOperand(0), CE);
597         break;
598       }
599 
600       default:
601         llvm_unreachable("Unknown instruction type encountered!");
602       }
603     }
604   };
605 
606   // Helper functions
607 
608   // Determines whether or not we an instruction is useless to us (e.g.
609   // FenceInst)
hasUsefulEdges(Instruction * Inst)610   static bool hasUsefulEdges(Instruction *Inst) {
611     bool IsNonInvokeRetTerminator = Inst->isTerminator() &&
612                                     !isa<InvokeInst>(Inst) &&
613                                     !isa<ReturnInst>(Inst);
614     return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
615            !IsNonInvokeRetTerminator;
616   }
617 
addArgumentToGraph(Argument & Arg)618   void addArgumentToGraph(Argument &Arg) {
619     if (Arg.getType()->isPointerTy()) {
620       Graph.addNode(InstantiatedValue{&Arg, 0},
621                     getGlobalOrArgAttrFromValue(Arg));
622       // Pointees of a formal parameter is known to the caller
623       Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller());
624     }
625   }
626 
627   // Given an Instruction, this will add it to the graph, along with any
628   // Instructions that are potentially only available from said Instruction
629   // For example, given the following line:
630   //   %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
631   // addInstructionToGraph would add both the `load` and `getelementptr`
632   // instructions to the graph appropriately.
addInstructionToGraph(GetEdgesVisitor & Visitor,Instruction & Inst)633   void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) {
634     if (!hasUsefulEdges(&Inst))
635       return;
636 
637     Visitor.visit(Inst);
638   }
639 
640   // Builds the graph needed for constructing the StratifiedSets for the given
641   // function
buildGraphFrom(Function & Fn)642   void buildGraphFrom(Function &Fn) {
643     GetEdgesVisitor Visitor(*this, Fn.getParent()->getDataLayout());
644 
645     for (auto &Bb : Fn.getBasicBlockList())
646       for (auto &Inst : Bb.getInstList())
647         addInstructionToGraph(Visitor, Inst);
648 
649     for (auto &Arg : Fn.args())
650       addArgumentToGraph(Arg);
651   }
652 
653 public:
CFLGraphBuilder(CFLAA & Analysis,const TargetLibraryInfo & TLI,Function & Fn)654   CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn)
655       : Analysis(Analysis), TLI(TLI) {
656     buildGraphFrom(Fn);
657   }
658 
getCFLGraph()659   const CFLGraph &getCFLGraph() const { return Graph; }
getReturnValues()660   const SmallVector<Value *, 4> &getReturnValues() const {
661     return ReturnedValues;
662   }
663 };
664 
665 } // end namespace cflaa
666 } // end namespace llvm
667 
668 #endif // LLVM_LIB_ANALYSIS_CFLGRAPH_H
669