blob: 43e622016ca54baa5fd3e96e5285cafe9192b826 [file] [log] [blame]
George Burgess IV1ca8aff2016-07-06 00:36:12 +00001//======- CFLGraph.h - Abstract stratified sets 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/// \file
10/// This file defines CFLGraph, an auxiliary data structure used by CFL-based
11/// alias analysis.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_CFLGRAPH_H
16#define LLVM_ANALYSIS_CFLGRAPH_H
17
George Burgess IVe1919962016-07-06 00:47:21 +000018#include "AliasAnalysisSummary.h"
George Burgess IVde1be712016-07-11 22:59:09 +000019#include "llvm/ADT/SmallPtrSet.h"
George Burgess IVc294d0d2016-07-09 02:54:42 +000020#include "llvm/Analysis/MemoryBuiltins.h"
21#include "llvm/IR/InstVisitor.h"
22#include "llvm/IR/Instructions.h"
George Burgess IV1ca8aff2016-07-06 00:36:12 +000023
24namespace llvm {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000025namespace cflaa {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000026
27/// \brief The Program Expression Graph (PEG) of CFL analysis
28/// CFLGraph is auxiliary data structure used by CFL-based alias analysis to
29/// describe flow-insensitive pointer-related behaviors. Given an LLVM function,
30/// the main purpose of this graph is to abstract away unrelated facts and
31/// translate the rest into a form that can be easily digested by CFL analyses.
George Burgess IVde1be712016-07-11 22:59:09 +000032/// Each Node in the graph is an InstantiatedValue, and each edge represent a
33/// pointer assignment between InstantiatedValue. Pointer
34/// references/dereferences are not explicitly stored in the graph: we
35/// implicitly assume that for each node (X, I) it has a dereference edge to (X,
36/// I+1) and a reference edge to (X, I-1).
George Burgess IV1ca8aff2016-07-06 00:36:12 +000037class CFLGraph {
George Burgess IVde1be712016-07-11 22:59:09 +000038public:
39 typedef InstantiatedValue Node;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000040
41 struct Edge {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000042 Node Other;
George Burgess IV6febd832016-07-20 22:53:30 +000043 int64_t Offset;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000044 };
45
46 typedef std::vector<Edge> EdgeList;
47
48 struct NodeInfo {
George Burgess IV6d30aa02016-07-15 19:53:25 +000049 EdgeList Edges, ReverseEdges;
George Burgess IVe1919962016-07-06 00:47:21 +000050 AliasAttrs Attr;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000051 };
52
George Burgess IVde1be712016-07-11 22:59:09 +000053 class ValueInfo {
54 std::vector<NodeInfo> Levels;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000055
George Burgess IVde1be712016-07-11 22:59:09 +000056 public:
57 bool addNodeToLevel(unsigned Level) {
58 auto NumLevels = Levels.size();
59 if (NumLevels > Level)
60 return false;
61 Levels.resize(Level + 1);
62 return true;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000063 }
George Burgess IVde1be712016-07-11 22:59:09 +000064
65 NodeInfo &getNodeInfoAtLevel(unsigned Level) {
66 assert(Level < Levels.size());
67 return Levels[Level];
68 }
69 const NodeInfo &getNodeInfoAtLevel(unsigned Level) const {
70 assert(Level < Levels.size());
71 return Levels[Level];
72 }
73
74 unsigned getNumLevels() const { return Levels.size(); }
75 };
76
77private:
78 typedef DenseMap<Value *, ValueInfo> ValueMap;
79 ValueMap ValueImpls;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000080
George Burgess IV1ca8aff2016-07-06 00:36:12 +000081 NodeInfo *getNode(Node N) {
George Burgess IVde1be712016-07-11 22:59:09 +000082 auto Itr = ValueImpls.find(N.Val);
83 if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
George Burgess IV1ca8aff2016-07-06 00:36:12 +000084 return nullptr;
George Burgess IVde1be712016-07-11 22:59:09 +000085 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
George Burgess IV1ca8aff2016-07-06 00:36:12 +000086 }
87
George Burgess IV1ca8aff2016-07-06 00:36:12 +000088public:
George Burgess IVde1be712016-07-11 22:59:09 +000089 typedef ValueMap::const_iterator const_value_iterator;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000090
George Burgess IVde1be712016-07-11 22:59:09 +000091 bool addNode(Node N, AliasAttrs Attr = AliasAttrs()) {
92 assert(N.Val != nullptr);
93 auto &ValInfo = ValueImpls[N.Val];
94 auto Changed = ValInfo.addNodeToLevel(N.DerefLevel);
95 ValInfo.getNodeInfoAtLevel(N.DerefLevel).Attr |= Attr;
96 return Changed;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000097 }
98
George Burgess IVe1919962016-07-06 00:47:21 +000099 void addAttr(Node N, AliasAttrs Attr) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000100 auto *Info = getNode(N);
101 assert(Info != nullptr);
102 Info->Attr |= Attr;
103 }
104
George Burgess IVde1be712016-07-11 22:59:09 +0000105 void addEdge(Node From, Node To, int64_t Offset = 0) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000106 auto *FromInfo = getNode(From);
107 assert(FromInfo != nullptr);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000108 auto *ToInfo = getNode(To);
109 assert(ToInfo != nullptr);
110
George Burgess IV6febd832016-07-20 22:53:30 +0000111 FromInfo->Edges.push_back(Edge{To, Offset});
112 ToInfo->ReverseEdges.push_back(Edge{From, Offset});
George Burgess IV6d30aa02016-07-15 19:53:25 +0000113 }
114
115 const NodeInfo *getNode(Node N) const {
116 auto Itr = ValueImpls.find(N.Val);
117 if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
118 return nullptr;
119 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000120 }
121
George Burgess IVe1919962016-07-06 00:47:21 +0000122 AliasAttrs attrFor(Node N) const {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000123 auto *Info = getNode(N);
124 assert(Info != nullptr);
125 return Info->Attr;
126 }
127
George Burgess IVde1be712016-07-11 22:59:09 +0000128 iterator_range<const_value_iterator> value_mappings() const {
129 return make_range<const_value_iterator>(ValueImpls.begin(),
130 ValueImpls.end());
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000131 }
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000132};
George Burgess IVc294d0d2016-07-09 02:54:42 +0000133
134///\brief A builder class used to create CFLGraph instance from a given function
135/// The CFL-AA that uses this builder must provide its own type as a template
136/// argument. This is necessary for interprocedural processing: CFLGraphBuilder
137/// needs a way of obtaining the summary of other functions when callinsts are
138/// encountered.
139/// As a result, we expect the said CFL-AA to expose a getAliasSummary() public
140/// member function that takes a Function& and returns the corresponding summary
141/// as a const AliasSummary*.
142template <typename CFLAA> class CFLGraphBuilder {
143 // Input of the builder
144 CFLAA &Analysis;
145 const TargetLibraryInfo &TLI;
146
147 // Output of the builder
148 CFLGraph Graph;
149 SmallVector<Value *, 4> ReturnedValues;
150
George Burgess IVc294d0d2016-07-09 02:54:42 +0000151 // Helper class
152 /// Gets the edges our graph should have, based on an Instruction*
153 class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
154 CFLAA &AA;
George Burgess IV6febd832016-07-20 22:53:30 +0000155 const DataLayout &DL;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000156 const TargetLibraryInfo &TLI;
157
158 CFLGraph &Graph;
159 SmallVectorImpl<Value *> &ReturnValues;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000160
161 static bool hasUsefulEdges(ConstantExpr *CE) {
162 // ConstantExpr doesn't have terminators, invokes, or fences, so only
163 // needs
164 // to check for compares.
165 return CE->getOpcode() != Instruction::ICmp &&
166 CE->getOpcode() != Instruction::FCmp;
167 }
168
169 // Returns possible functions called by CS into the given SmallVectorImpl.
170 // Returns true if targets found, false otherwise.
171 static bool getPossibleTargets(CallSite CS,
172 SmallVectorImpl<Function *> &Output) {
173 if (auto *Fn = CS.getCalledFunction()) {
174 Output.push_back(Fn);
175 return true;
176 }
177
178 // TODO: If the call is indirect, we might be able to enumerate all
179 // potential
180 // targets of the call and return them, rather than just failing.
181 return false;
182 }
183
George Burgess IVde1be712016-07-11 22:59:09 +0000184 void addNode(Value *Val, AliasAttrs Attr = AliasAttrs()) {
185 assert(Val != nullptr && Val->getType()->isPointerTy());
186 if (auto GVal = dyn_cast<GlobalValue>(Val)) {
187 if (Graph.addNode(InstantiatedValue{GVal, 0},
188 getGlobalOrArgAttrFromValue(*GVal)))
189 Graph.addNode(InstantiatedValue{GVal, 1}, getAttrUnknown());
190 } else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) {
191 if (hasUsefulEdges(CExpr)) {
192 if (Graph.addNode(InstantiatedValue{CExpr, 0}))
193 visitConstantExpr(CExpr);
194 }
195 } else
196 Graph.addNode(InstantiatedValue{Val, 0}, Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000197 }
198
George Burgess IVde1be712016-07-11 22:59:09 +0000199 void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000200 assert(From != nullptr && To != nullptr);
201 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
202 return;
203 addNode(From);
George Burgess IVde1be712016-07-11 22:59:09 +0000204 if (To != From) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000205 addNode(To);
George Burgess IVde1be712016-07-11 22:59:09 +0000206 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 0},
207 Offset);
208 }
209 }
210
George Burgess IV6d30aa02016-07-15 19:53:25 +0000211 void addDerefEdge(Value *From, Value *To, bool IsRead) {
George Burgess IVde1be712016-07-11 22:59:09 +0000212 assert(From != nullptr && To != nullptr);
213 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
214 return;
215 addNode(From);
216 addNode(To);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000217 if (IsRead) {
218 Graph.addNode(InstantiatedValue{From, 1});
219 Graph.addEdge(InstantiatedValue{From, 1}, InstantiatedValue{To, 0});
220 } else {
221 Graph.addNode(InstantiatedValue{To, 1});
222 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 1});
223 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000224 }
225
George Burgess IV6d30aa02016-07-15 19:53:25 +0000226 void addLoadEdge(Value *From, Value *To) { addDerefEdge(From, To, true); }
227 void addStoreEdge(Value *From, Value *To) { addDerefEdge(From, To, false); }
228
George Burgess IVc294d0d2016-07-09 02:54:42 +0000229 public:
George Burgess IV6febd832016-07-20 22:53:30 +0000230 GetEdgesVisitor(CFLGraphBuilder &Builder, const DataLayout &DL)
231 : AA(Builder.Analysis), DL(DL), TLI(Builder.TLI), Graph(Builder.Graph),
George Burgess IVde1be712016-07-11 22:59:09 +0000232 ReturnValues(Builder.ReturnedValues) {}
George Burgess IVc294d0d2016-07-09 02:54:42 +0000233
234 void visitInstruction(Instruction &) {
235 llvm_unreachable("Unsupported instruction encountered");
236 }
237
238 void visitReturnInst(ReturnInst &Inst) {
239 if (auto RetVal = Inst.getReturnValue()) {
240 if (RetVal->getType()->isPointerTy()) {
241 addNode(RetVal);
242 ReturnValues.push_back(RetVal);
243 }
244 }
245 }
246
247 void visitPtrToIntInst(PtrToIntInst &Inst) {
248 auto *Ptr = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000249 addNode(Ptr, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000250 }
251
252 void visitIntToPtrInst(IntToPtrInst &Inst) {
253 auto *Ptr = &Inst;
George Burgess IVde1be712016-07-11 22:59:09 +0000254 addNode(Ptr, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000255 }
256
257 void visitCastInst(CastInst &Inst) {
258 auto *Src = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000259 addAssignEdge(Src, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000260 }
261
262 void visitBinaryOperator(BinaryOperator &Inst) {
263 auto *Op1 = Inst.getOperand(0);
264 auto *Op2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000265 addAssignEdge(Op1, &Inst);
266 addAssignEdge(Op2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000267 }
268
269 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
270 auto *Ptr = Inst.getPointerOperand();
271 auto *Val = Inst.getNewValOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000272 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000273 }
274
275 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
276 auto *Ptr = Inst.getPointerOperand();
277 auto *Val = Inst.getValOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000278 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000279 }
280
281 void visitPHINode(PHINode &Inst) {
282 for (Value *Val : Inst.incoming_values())
George Burgess IVde1be712016-07-11 22:59:09 +0000283 addAssignEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000284 }
285
George Burgess IV6febd832016-07-20 22:53:30 +0000286 void visitGEP(GEPOperator &GEPOp) {
287 uint64_t Offset = UnknownOffset;
288 APInt APOffset(DL.getPointerSizeInBits(GEPOp.getPointerAddressSpace()),
289 0);
290 if (GEPOp.accumulateConstantOffset(DL, APOffset))
291 Offset = APOffset.getSExtValue();
292
293 auto *Op = GEPOp.getPointerOperand();
294 addAssignEdge(Op, &GEPOp, Offset);
295 }
296
George Burgess IVc294d0d2016-07-09 02:54:42 +0000297 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
George Burgess IV6febd832016-07-20 22:53:30 +0000298 auto *GEPOp = cast<GEPOperator>(&Inst);
299 visitGEP(*GEPOp);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000300 }
301
302 void visitSelectInst(SelectInst &Inst) {
303 // Condition is not processed here (The actual statement producing
304 // the condition result is processed elsewhere). For select, the
305 // condition is evaluated, but not loaded, stored, or assigned
306 // simply as a result of being the condition of a select.
307
308 auto *TrueVal = Inst.getTrueValue();
309 auto *FalseVal = Inst.getFalseValue();
George Burgess IVde1be712016-07-11 22:59:09 +0000310 addAssignEdge(TrueVal, &Inst);
311 addAssignEdge(FalseVal, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000312 }
313
George Burgess IVde1be712016-07-11 22:59:09 +0000314 void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000315
316 void visitLoadInst(LoadInst &Inst) {
317 auto *Ptr = Inst.getPointerOperand();
318 auto *Val = &Inst;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000319 addLoadEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000320 }
321
322 void visitStoreInst(StoreInst &Inst) {
323 auto *Ptr = Inst.getPointerOperand();
324 auto *Val = Inst.getValueOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000325 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000326 }
327
328 void visitVAArgInst(VAArgInst &Inst) {
329 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it
330 // does
331 // two things:
332 // 1. Loads a value from *((T*)*Ptr).
333 // 2. Increments (stores to) *Ptr by some target-specific amount.
334 // For now, we'll handle this like a landingpad instruction (by placing
335 // the
336 // result in its own group, and having that group alias externals).
George Burgess IVde1be712016-07-11 22:59:09 +0000337 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000338 }
339
340 static bool isFunctionExternal(Function *Fn) {
341 return !Fn->hasExactDefinition();
342 }
343
344 bool tryInterproceduralAnalysis(CallSite CS,
345 const SmallVectorImpl<Function *> &Fns) {
346 assert(Fns.size() > 0);
347
348 if (CS.arg_size() > MaxSupportedArgsInSummary)
349 return false;
350
351 // Exit early if we'll fail anyway
352 for (auto *Fn : Fns) {
353 if (isFunctionExternal(Fn) || Fn->isVarArg())
354 return false;
355 // Fail if the caller does not provide enough arguments
356 assert(Fn->arg_size() <= CS.arg_size());
357 if (!AA.getAliasSummary(*Fn))
358 return false;
359 }
360
361 for (auto *Fn : Fns) {
362 auto Summary = AA.getAliasSummary(*Fn);
363 assert(Summary != nullptr);
364
365 auto &RetParamRelations = Summary->RetParamRelations;
366 for (auto &Relation : RetParamRelations) {
367 auto IRelation = instantiateExternalRelation(Relation, CS);
George Burgess IVde1be712016-07-11 22:59:09 +0000368 if (IRelation.hasValue()) {
369 Graph.addNode(IRelation->From);
370 Graph.addNode(IRelation->To);
371 Graph.addEdge(IRelation->From, IRelation->To);
372 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000373 }
374
375 auto &RetParamAttributes = Summary->RetParamAttributes;
376 for (auto &Attribute : RetParamAttributes) {
377 auto IAttr = instantiateExternalAttribute(Attribute, CS);
378 if (IAttr.hasValue())
George Burgess IVde1be712016-07-11 22:59:09 +0000379 Graph.addNode(IAttr->IValue, IAttr->Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000380 }
381 }
382
383 return true;
384 }
385
386 void visitCallSite(CallSite CS) {
387 auto Inst = CS.getInstruction();
388
389 // Make sure all arguments and return value are added to the graph first
390 for (Value *V : CS.args())
George Burgess IVde1be712016-07-11 22:59:09 +0000391 if (V->getType()->isPointerTy())
392 addNode(V);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000393 if (Inst->getType()->isPointerTy())
394 addNode(Inst);
395
396 // Check if Inst is a call to a library function that
397 // allocates/deallocates
398 // on the heap. Those kinds of functions do not introduce any aliases.
399 // TODO: address other common library functions such as realloc(),
400 // strdup(),
401 // etc.
402 if (isMallocLikeFn(Inst, &TLI) || isCallocLikeFn(Inst, &TLI) ||
403 isFreeCall(Inst, &TLI))
404 return;
405
406 // TODO: Add support for noalias args/all the other fun function
407 // attributes
408 // that we can tack on.
409 SmallVector<Function *, 4> Targets;
410 if (getPossibleTargets(CS, Targets))
411 if (tryInterproceduralAnalysis(CS, Targets))
412 return;
413
414 // Because the function is opaque, we need to note that anything
415 // could have happened to the arguments (unless the function is marked
416 // readonly or readnone), and that the result could alias just about
417 // anything, too (unless the result is marked noalias).
418 if (!CS.onlyReadsMemory())
419 for (Value *V : CS.args()) {
420 if (V->getType()->isPointerTy()) {
421 // The argument itself escapes.
George Burgess IVde1be712016-07-11 22:59:09 +0000422 Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000423 // The fate of argument memory is unknown. Note that since
George Burgess IVde1be712016-07-11 22:59:09 +0000424 // AliasAttrs is transitive with respect to dereference, we only
425 // need to specify it for the first-level memory.
426 Graph.addNode(InstantiatedValue{V, 1}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000427 }
428 }
429
430 if (Inst->getType()->isPointerTy()) {
431 auto *Fn = CS.getCalledFunction();
432 if (Fn == nullptr || !Fn->doesNotAlias(0))
George Burgess IVde1be712016-07-11 22:59:09 +0000433 // No need to call addNode() since we've added Inst at the
George Burgess IVc294d0d2016-07-09 02:54:42 +0000434 // beginning of this function and we know it is not a global.
George Burgess IVde1be712016-07-11 22:59:09 +0000435 Graph.addAttr(InstantiatedValue{Inst, 0}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000436 }
437 }
438
439 /// Because vectors/aggregates are immutable and unaddressable, there's
440 /// nothing we can do to coax a value out of them, other than calling
441 /// Extract{Element,Value}. We can effectively treat them as pointers to
442 /// arbitrary memory locations we can store in and load from.
443 void visitExtractElementInst(ExtractElementInst &Inst) {
444 auto *Ptr = Inst.getVectorOperand();
445 auto *Val = &Inst;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000446 addLoadEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000447 }
448
449 void visitInsertElementInst(InsertElementInst &Inst) {
450 auto *Vec = Inst.getOperand(0);
451 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000452 addAssignEdge(Vec, &Inst);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000453 addStoreEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000454 }
455
456 void visitLandingPadInst(LandingPadInst &Inst) {
457 // Exceptions come from "nowhere", from our analysis' perspective.
458 // So we place the instruction its own group, noting that said group may
459 // alias externals
George Burgess IVde1be712016-07-11 22:59:09 +0000460 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000461 }
462
463 void visitInsertValueInst(InsertValueInst &Inst) {
464 auto *Agg = Inst.getOperand(0);
465 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000466 addAssignEdge(Agg, &Inst);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000467 addStoreEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000468 }
469
470 void visitExtractValueInst(ExtractValueInst &Inst) {
471 auto *Ptr = Inst.getAggregateOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000472 addLoadEdge(Ptr, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000473 }
474
475 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
476 auto *From1 = Inst.getOperand(0);
477 auto *From2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000478 addAssignEdge(From1, &Inst);
479 addAssignEdge(From2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000480 }
481
482 void visitConstantExpr(ConstantExpr *CE) {
483 switch (CE->getOpcode()) {
George Burgess IV6febd832016-07-20 22:53:30 +0000484 case Instruction::GetElementPtr: {
485 auto GEPOp = cast<GEPOperator>(CE);
486 visitGEP(*GEPOp);
487 break;
488 }
489 case Instruction::PtrToInt: {
490 auto *Ptr = CE->getOperand(0);
491 addNode(Ptr, getAttrEscaped());
492 break;
493 }
494 case Instruction::IntToPtr: {
495 addNode(CE, getAttrUnknown());
496 break;
497 }
498 case Instruction::BitCast:
499 case Instruction::AddrSpaceCast:
500 case Instruction::Trunc:
501 case Instruction::ZExt:
502 case Instruction::SExt:
503 case Instruction::FPExt:
504 case Instruction::FPTrunc:
505 case Instruction::UIToFP:
506 case Instruction::SIToFP:
507 case Instruction::FPToUI:
508 case Instruction::FPToSI: {
509 auto *Src = CE->getOperand(0);
510 addAssignEdge(Src, CE);
511 break;
512 }
513 case Instruction::Select: {
514 auto *TrueVal = CE->getOperand(0);
515 auto *FalseVal = CE->getOperand(1);
516 addAssignEdge(TrueVal, CE);
517 addAssignEdge(FalseVal, CE);
518 break;
519 }
520 case Instruction::InsertElement: {
521 auto *Vec = CE->getOperand(0);
522 auto *Val = CE->getOperand(1);
523 addAssignEdge(Vec, CE);
524 addStoreEdge(Val, CE);
525 break;
526 }
527 case Instruction::ExtractElement: {
528 auto *Ptr = CE->getOperand(0);
529 addLoadEdge(Ptr, CE);
530 break;
531 }
532 case Instruction::InsertValue: {
533 auto *Agg = CE->getOperand(0);
534 auto *Val = CE->getOperand(1);
535 addAssignEdge(Agg, CE);
536 addStoreEdge(Val, CE);
537 break;
538 }
539 case Instruction::ExtractValue: {
540 auto *Ptr = CE->getOperand(0);
541 addLoadEdge(Ptr, CE);
542 }
543 case Instruction::ShuffleVector: {
544 auto *From1 = CE->getOperand(0);
545 auto *From2 = CE->getOperand(1);
546 addAssignEdge(From1, CE);
547 addAssignEdge(From2, CE);
548 break;
549 }
550 case Instruction::Add:
551 case Instruction::Sub:
552 case Instruction::FSub:
553 case Instruction::Mul:
554 case Instruction::FMul:
555 case Instruction::UDiv:
556 case Instruction::SDiv:
557 case Instruction::FDiv:
558 case Instruction::URem:
559 case Instruction::SRem:
560 case Instruction::FRem:
561 case Instruction::And:
562 case Instruction::Or:
563 case Instruction::Xor:
564 case Instruction::Shl:
565 case Instruction::LShr:
566 case Instruction::AShr:
567 case Instruction::ICmp:
568 case Instruction::FCmp: {
569 addAssignEdge(CE->getOperand(0), CE);
570 addAssignEdge(CE->getOperand(1), CE);
571 break;
572 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000573 default:
574 llvm_unreachable("Unknown instruction type encountered!");
George Burgess IVc294d0d2016-07-09 02:54:42 +0000575 }
576 }
577 };
578
579 // Helper functions
580
581 // Determines whether or not we an instruction is useless to us (e.g.
582 // FenceInst)
583 static bool hasUsefulEdges(Instruction *Inst) {
584 bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) &&
585 !isa<InvokeInst>(Inst) &&
586 !isa<ReturnInst>(Inst);
587 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
588 !IsNonInvokeRetTerminator;
589 }
590
591 void addArgumentToGraph(Argument &Arg) {
592 if (Arg.getType()->isPointerTy()) {
George Burgess IVde1be712016-07-11 22:59:09 +0000593 Graph.addNode(InstantiatedValue{&Arg, 0},
594 getGlobalOrArgAttrFromValue(Arg));
George Burgess IVc294d0d2016-07-09 02:54:42 +0000595 // Pointees of a formal parameter is known to the caller
George Burgess IVde1be712016-07-11 22:59:09 +0000596 Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000597 }
598 }
599
600 // Given an Instruction, this will add it to the graph, along with any
601 // Instructions that are potentially only available from said Instruction
602 // For example, given the following line:
603 // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
604 // addInstructionToGraph would add both the `load` and `getelementptr`
605 // instructions to the graph appropriately.
George Burgess IVde1be712016-07-11 22:59:09 +0000606 void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000607 if (!hasUsefulEdges(&Inst))
608 return;
609
George Burgess IVde1be712016-07-11 22:59:09 +0000610 Visitor.visit(Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000611 }
612
613 // Builds the graph needed for constructing the StratifiedSets for the given
614 // function
615 void buildGraphFrom(Function &Fn) {
George Burgess IV6febd832016-07-20 22:53:30 +0000616 GetEdgesVisitor Visitor(*this, Fn.getParent()->getDataLayout());
George Burgess IVde1be712016-07-11 22:59:09 +0000617
George Burgess IVc294d0d2016-07-09 02:54:42 +0000618 for (auto &Bb : Fn.getBasicBlockList())
619 for (auto &Inst : Bb.getInstList())
George Burgess IVde1be712016-07-11 22:59:09 +0000620 addInstructionToGraph(Visitor, Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000621
622 for (auto &Arg : Fn.args())
623 addArgumentToGraph(Arg);
624 }
625
626public:
627 CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn)
628 : Analysis(Analysis), TLI(TLI) {
629 buildGraphFrom(Fn);
630 }
631
632 const CFLGraph &getCFLGraph() const { return Graph; }
633 const SmallVector<Value *, 4> &getReturnValues() const {
634 return ReturnedValues;
635 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000636};
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000637}
638}
639
640#endif