blob: 95874b88244b179e359d0e26b5adad98810488ff [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 IVc294d0d2016-07-09 02:54:42 +000019#include "llvm/Analysis/MemoryBuiltins.h"
20#include "llvm/IR/InstVisitor.h"
21#include "llvm/IR/Instructions.h"
George Burgess IV1ca8aff2016-07-06 00:36:12 +000022
23namespace llvm {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000024namespace cflaa {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000025
26/// \brief The Program Expression Graph (PEG) of CFL analysis
27/// CFLGraph is auxiliary data structure used by CFL-based alias analysis to
28/// describe flow-insensitive pointer-related behaviors. Given an LLVM function,
29/// the main purpose of this graph is to abstract away unrelated facts and
30/// translate the rest into a form that can be easily digested by CFL analyses.
George Burgess IVde1be712016-07-11 22:59:09 +000031/// Each Node in the graph is an InstantiatedValue, and each edge represent a
32/// pointer assignment between InstantiatedValue. Pointer
33/// references/dereferences are not explicitly stored in the graph: we
34/// implicitly assume that for each node (X, I) it has a dereference edge to (X,
35/// I+1) and a reference edge to (X, I-1).
George Burgess IV1ca8aff2016-07-06 00:36:12 +000036class CFLGraph {
George Burgess IVde1be712016-07-11 22:59:09 +000037public:
38 typedef InstantiatedValue Node;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000039
40 struct Edge {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000041 Node Other;
George Burgess IV6febd832016-07-20 22:53:30 +000042 int64_t Offset;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000043 };
44
45 typedef std::vector<Edge> EdgeList;
46
47 struct NodeInfo {
George Burgess IV6d30aa02016-07-15 19:53:25 +000048 EdgeList Edges, ReverseEdges;
George Burgess IVe1919962016-07-06 00:47:21 +000049 AliasAttrs Attr;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000050 };
51
George Burgess IVde1be712016-07-11 22:59:09 +000052 class ValueInfo {
53 std::vector<NodeInfo> Levels;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000054
George Burgess IVde1be712016-07-11 22:59:09 +000055 public:
56 bool addNodeToLevel(unsigned Level) {
57 auto NumLevels = Levels.size();
58 if (NumLevels > Level)
59 return false;
60 Levels.resize(Level + 1);
61 return true;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000062 }
George Burgess IVde1be712016-07-11 22:59:09 +000063
64 NodeInfo &getNodeInfoAtLevel(unsigned Level) {
65 assert(Level < Levels.size());
66 return Levels[Level];
67 }
68 const NodeInfo &getNodeInfoAtLevel(unsigned Level) const {
69 assert(Level < Levels.size());
70 return Levels[Level];
71 }
72
73 unsigned getNumLevels() const { return Levels.size(); }
74 };
75
76private:
77 typedef DenseMap<Value *, ValueInfo> ValueMap;
78 ValueMap ValueImpls;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000079
George Burgess IV1ca8aff2016-07-06 00:36:12 +000080 NodeInfo *getNode(Node N) {
George Burgess IVde1be712016-07-11 22:59:09 +000081 auto Itr = ValueImpls.find(N.Val);
82 if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
George Burgess IV1ca8aff2016-07-06 00:36:12 +000083 return nullptr;
George Burgess IVde1be712016-07-11 22:59:09 +000084 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
George Burgess IV1ca8aff2016-07-06 00:36:12 +000085 }
86
George Burgess IV1ca8aff2016-07-06 00:36:12 +000087public:
George Burgess IVde1be712016-07-11 22:59:09 +000088 typedef ValueMap::const_iterator const_value_iterator;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000089
George Burgess IVde1be712016-07-11 22:59:09 +000090 bool addNode(Node N, AliasAttrs Attr = AliasAttrs()) {
91 assert(N.Val != nullptr);
92 auto &ValInfo = ValueImpls[N.Val];
93 auto Changed = ValInfo.addNodeToLevel(N.DerefLevel);
94 ValInfo.getNodeInfoAtLevel(N.DerefLevel).Attr |= Attr;
95 return Changed;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000096 }
97
George Burgess IVe1919962016-07-06 00:47:21 +000098 void addAttr(Node N, AliasAttrs Attr) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000099 auto *Info = getNode(N);
100 assert(Info != nullptr);
101 Info->Attr |= Attr;
102 }
103
George Burgess IVde1be712016-07-11 22:59:09 +0000104 void addEdge(Node From, Node To, int64_t Offset = 0) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000105 auto *FromInfo = getNode(From);
106 assert(FromInfo != nullptr);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000107 auto *ToInfo = getNode(To);
108 assert(ToInfo != nullptr);
109
George Burgess IV6febd832016-07-20 22:53:30 +0000110 FromInfo->Edges.push_back(Edge{To, Offset});
111 ToInfo->ReverseEdges.push_back(Edge{From, Offset});
George Burgess IV6d30aa02016-07-15 19:53:25 +0000112 }
113
114 const NodeInfo *getNode(Node N) const {
115 auto Itr = ValueImpls.find(N.Val);
116 if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
117 return nullptr;
118 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000119 }
120
George Burgess IVe1919962016-07-06 00:47:21 +0000121 AliasAttrs attrFor(Node N) const {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000122 auto *Info = getNode(N);
123 assert(Info != nullptr);
124 return Info->Attr;
125 }
126
George Burgess IVde1be712016-07-11 22:59:09 +0000127 iterator_range<const_value_iterator> value_mappings() const {
128 return make_range<const_value_iterator>(ValueImpls.begin(),
129 ValueImpls.end());
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000130 }
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000131};
George Burgess IVc294d0d2016-07-09 02:54:42 +0000132
133///\brief A builder class used to create CFLGraph instance from a given function
134/// The CFL-AA that uses this builder must provide its own type as a template
135/// argument. This is necessary for interprocedural processing: CFLGraphBuilder
136/// needs a way of obtaining the summary of other functions when callinsts are
137/// encountered.
138/// As a result, we expect the said CFL-AA to expose a getAliasSummary() public
139/// member function that takes a Function& and returns the corresponding summary
140/// as a const AliasSummary*.
141template <typename CFLAA> class CFLGraphBuilder {
142 // Input of the builder
143 CFLAA &Analysis;
144 const TargetLibraryInfo &TLI;
145
146 // Output of the builder
147 CFLGraph Graph;
148 SmallVector<Value *, 4> ReturnedValues;
149
George Burgess IVc294d0d2016-07-09 02:54:42 +0000150 // Helper class
151 /// Gets the edges our graph should have, based on an Instruction*
152 class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
153 CFLAA &AA;
George Burgess IV6febd832016-07-20 22:53:30 +0000154 const DataLayout &DL;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000155 const TargetLibraryInfo &TLI;
156
157 CFLGraph &Graph;
158 SmallVectorImpl<Value *> &ReturnValues;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000159
160 static bool hasUsefulEdges(ConstantExpr *CE) {
161 // ConstantExpr doesn't have terminators, invokes, or fences, so only
162 // needs
163 // to check for compares.
164 return CE->getOpcode() != Instruction::ICmp &&
165 CE->getOpcode() != Instruction::FCmp;
166 }
167
168 // Returns possible functions called by CS into the given SmallVectorImpl.
169 // Returns true if targets found, false otherwise.
170 static bool getPossibleTargets(CallSite CS,
171 SmallVectorImpl<Function *> &Output) {
172 if (auto *Fn = CS.getCalledFunction()) {
173 Output.push_back(Fn);
174 return true;
175 }
176
177 // TODO: If the call is indirect, we might be able to enumerate all
178 // potential
179 // targets of the call and return them, rather than just failing.
180 return false;
181 }
182
George Burgess IVde1be712016-07-11 22:59:09 +0000183 void addNode(Value *Val, AliasAttrs Attr = AliasAttrs()) {
184 assert(Val != nullptr && Val->getType()->isPointerTy());
185 if (auto GVal = dyn_cast<GlobalValue>(Val)) {
186 if (Graph.addNode(InstantiatedValue{GVal, 0},
187 getGlobalOrArgAttrFromValue(*GVal)))
188 Graph.addNode(InstantiatedValue{GVal, 1}, getAttrUnknown());
189 } else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) {
190 if (hasUsefulEdges(CExpr)) {
191 if (Graph.addNode(InstantiatedValue{CExpr, 0}))
192 visitConstantExpr(CExpr);
193 }
194 } else
195 Graph.addNode(InstantiatedValue{Val, 0}, Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000196 }
197
George Burgess IVde1be712016-07-11 22:59:09 +0000198 void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000199 assert(From != nullptr && To != nullptr);
200 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
201 return;
202 addNode(From);
George Burgess IVde1be712016-07-11 22:59:09 +0000203 if (To != From) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000204 addNode(To);
George Burgess IVde1be712016-07-11 22:59:09 +0000205 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 0},
206 Offset);
207 }
208 }
209
George Burgess IV6d30aa02016-07-15 19:53:25 +0000210 void addDerefEdge(Value *From, Value *To, bool IsRead) {
George Burgess IVde1be712016-07-11 22:59:09 +0000211 assert(From != nullptr && To != nullptr);
George Burgess IV0a7b9892017-05-31 02:35:26 +0000212 // FIXME: This is subtly broken, due to how we model some instructions
213 // (e.g. extractvalue, extractelement) as loads. Since those take
214 // non-pointer operands, we'll entirely skip adding edges for those.
215 //
216 // addAssignEdge seems to have a similar issue with insertvalue, etc.
George Burgess IVde1be712016-07-11 22:59:09 +0000217 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
218 return;
219 addNode(From);
220 addNode(To);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000221 if (IsRead) {
222 Graph.addNode(InstantiatedValue{From, 1});
223 Graph.addEdge(InstantiatedValue{From, 1}, InstantiatedValue{To, 0});
224 } else {
225 Graph.addNode(InstantiatedValue{To, 1});
226 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 1});
227 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000228 }
229
George Burgess IV6d30aa02016-07-15 19:53:25 +0000230 void addLoadEdge(Value *From, Value *To) { addDerefEdge(From, To, true); }
231 void addStoreEdge(Value *From, Value *To) { addDerefEdge(From, To, false); }
232
George Burgess IVc294d0d2016-07-09 02:54:42 +0000233 public:
George Burgess IV6febd832016-07-20 22:53:30 +0000234 GetEdgesVisitor(CFLGraphBuilder &Builder, const DataLayout &DL)
235 : AA(Builder.Analysis), DL(DL), TLI(Builder.TLI), Graph(Builder.Graph),
George Burgess IVde1be712016-07-11 22:59:09 +0000236 ReturnValues(Builder.ReturnedValues) {}
George Burgess IVc294d0d2016-07-09 02:54:42 +0000237
238 void visitInstruction(Instruction &) {
239 llvm_unreachable("Unsupported instruction encountered");
240 }
241
242 void visitReturnInst(ReturnInst &Inst) {
243 if (auto RetVal = Inst.getReturnValue()) {
244 if (RetVal->getType()->isPointerTy()) {
245 addNode(RetVal);
246 ReturnValues.push_back(RetVal);
247 }
248 }
249 }
250
251 void visitPtrToIntInst(PtrToIntInst &Inst) {
252 auto *Ptr = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000253 addNode(Ptr, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000254 }
255
256 void visitIntToPtrInst(IntToPtrInst &Inst) {
257 auto *Ptr = &Inst;
George Burgess IVde1be712016-07-11 22:59:09 +0000258 addNode(Ptr, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000259 }
260
261 void visitCastInst(CastInst &Inst) {
262 auto *Src = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000263 addAssignEdge(Src, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000264 }
265
266 void visitBinaryOperator(BinaryOperator &Inst) {
267 auto *Op1 = Inst.getOperand(0);
268 auto *Op2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000269 addAssignEdge(Op1, &Inst);
270 addAssignEdge(Op2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000271 }
272
273 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
274 auto *Ptr = Inst.getPointerOperand();
275 auto *Val = Inst.getNewValOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000276 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000277 }
278
279 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
280 auto *Ptr = Inst.getPointerOperand();
281 auto *Val = Inst.getValOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000282 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000283 }
284
285 void visitPHINode(PHINode &Inst) {
286 for (Value *Val : Inst.incoming_values())
George Burgess IVde1be712016-07-11 22:59:09 +0000287 addAssignEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000288 }
289
George Burgess IV6febd832016-07-20 22:53:30 +0000290 void visitGEP(GEPOperator &GEPOp) {
291 uint64_t Offset = UnknownOffset;
292 APInt APOffset(DL.getPointerSizeInBits(GEPOp.getPointerAddressSpace()),
293 0);
294 if (GEPOp.accumulateConstantOffset(DL, APOffset))
295 Offset = APOffset.getSExtValue();
296
297 auto *Op = GEPOp.getPointerOperand();
298 addAssignEdge(Op, &GEPOp, Offset);
299 }
300
George Burgess IVc294d0d2016-07-09 02:54:42 +0000301 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
George Burgess IV6febd832016-07-20 22:53:30 +0000302 auto *GEPOp = cast<GEPOperator>(&Inst);
303 visitGEP(*GEPOp);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000304 }
305
306 void visitSelectInst(SelectInst &Inst) {
307 // Condition is not processed here (The actual statement producing
308 // the condition result is processed elsewhere). For select, the
309 // condition is evaluated, but not loaded, stored, or assigned
310 // simply as a result of being the condition of a select.
311
312 auto *TrueVal = Inst.getTrueValue();
313 auto *FalseVal = Inst.getFalseValue();
George Burgess IVde1be712016-07-11 22:59:09 +0000314 addAssignEdge(TrueVal, &Inst);
315 addAssignEdge(FalseVal, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000316 }
317
George Burgess IVde1be712016-07-11 22:59:09 +0000318 void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000319
320 void visitLoadInst(LoadInst &Inst) {
321 auto *Ptr = Inst.getPointerOperand();
322 auto *Val = &Inst;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000323 addLoadEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000324 }
325
326 void visitStoreInst(StoreInst &Inst) {
327 auto *Ptr = Inst.getPointerOperand();
328 auto *Val = Inst.getValueOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000329 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000330 }
331
332 void visitVAArgInst(VAArgInst &Inst) {
333 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it
334 // does
335 // two things:
336 // 1. Loads a value from *((T*)*Ptr).
337 // 2. Increments (stores to) *Ptr by some target-specific amount.
338 // For now, we'll handle this like a landingpad instruction (by placing
339 // the
340 // result in its own group, and having that group alias externals).
George Burgess IV0a9cbd42016-07-29 01:23:45 +0000341 if (Inst.getType()->isPointerTy())
342 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000343 }
344
345 static bool isFunctionExternal(Function *Fn) {
346 return !Fn->hasExactDefinition();
347 }
348
349 bool tryInterproceduralAnalysis(CallSite CS,
350 const SmallVectorImpl<Function *> &Fns) {
351 assert(Fns.size() > 0);
352
353 if (CS.arg_size() > MaxSupportedArgsInSummary)
354 return false;
355
356 // Exit early if we'll fail anyway
357 for (auto *Fn : Fns) {
358 if (isFunctionExternal(Fn) || Fn->isVarArg())
359 return false;
360 // Fail if the caller does not provide enough arguments
361 assert(Fn->arg_size() <= CS.arg_size());
362 if (!AA.getAliasSummary(*Fn))
363 return false;
364 }
365
366 for (auto *Fn : Fns) {
367 auto Summary = AA.getAliasSummary(*Fn);
368 assert(Summary != nullptr);
369
370 auto &RetParamRelations = Summary->RetParamRelations;
371 for (auto &Relation : RetParamRelations) {
372 auto IRelation = instantiateExternalRelation(Relation, CS);
George Burgess IVde1be712016-07-11 22:59:09 +0000373 if (IRelation.hasValue()) {
374 Graph.addNode(IRelation->From);
375 Graph.addNode(IRelation->To);
376 Graph.addEdge(IRelation->From, IRelation->To);
377 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000378 }
379
380 auto &RetParamAttributes = Summary->RetParamAttributes;
381 for (auto &Attribute : RetParamAttributes) {
382 auto IAttr = instantiateExternalAttribute(Attribute, CS);
383 if (IAttr.hasValue())
George Burgess IVde1be712016-07-11 22:59:09 +0000384 Graph.addNode(IAttr->IValue, IAttr->Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000385 }
386 }
387
388 return true;
389 }
390
391 void visitCallSite(CallSite CS) {
392 auto Inst = CS.getInstruction();
393
394 // Make sure all arguments and return value are added to the graph first
395 for (Value *V : CS.args())
George Burgess IVde1be712016-07-11 22:59:09 +0000396 if (V->getType()->isPointerTy())
397 addNode(V);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000398 if (Inst->getType()->isPointerTy())
399 addNode(Inst);
400
401 // Check if Inst is a call to a library function that
402 // allocates/deallocates
403 // on the heap. Those kinds of functions do not introduce any aliases.
404 // TODO: address other common library functions such as realloc(),
405 // strdup(),
406 // etc.
Craig Topper09bb7602017-04-18 21:43:46 +0000407 if (isMallocOrCallocLikeFn(Inst, &TLI) || isFreeCall(Inst, &TLI))
George Burgess IVc294d0d2016-07-09 02:54:42 +0000408 return;
409
410 // TODO: Add support for noalias args/all the other fun function
411 // attributes
412 // that we can tack on.
413 SmallVector<Function *, 4> Targets;
414 if (getPossibleTargets(CS, Targets))
415 if (tryInterproceduralAnalysis(CS, Targets))
416 return;
417
418 // Because the function is opaque, we need to note that anything
419 // could have happened to the arguments (unless the function is marked
420 // readonly or readnone), and that the result could alias just about
421 // anything, too (unless the result is marked noalias).
422 if (!CS.onlyReadsMemory())
423 for (Value *V : CS.args()) {
424 if (V->getType()->isPointerTy()) {
425 // The argument itself escapes.
George Burgess IVde1be712016-07-11 22:59:09 +0000426 Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000427 // The fate of argument memory is unknown. Note that since
George Burgess IVde1be712016-07-11 22:59:09 +0000428 // AliasAttrs is transitive with respect to dereference, we only
429 // need to specify it for the first-level memory.
430 Graph.addNode(InstantiatedValue{V, 1}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000431 }
432 }
433
434 if (Inst->getType()->isPointerTy()) {
435 auto *Fn = CS.getCalledFunction();
Reid Klecknera0b45f42017-05-03 18:17:31 +0000436 if (Fn == nullptr || !Fn->returnDoesNotAlias())
George Burgess IVde1be712016-07-11 22:59:09 +0000437 // No need to call addNode() since we've added Inst at the
George Burgess IVc294d0d2016-07-09 02:54:42 +0000438 // beginning of this function and we know it is not a global.
George Burgess IVde1be712016-07-11 22:59:09 +0000439 Graph.addAttr(InstantiatedValue{Inst, 0}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000440 }
441 }
442
443 /// Because vectors/aggregates are immutable and unaddressable, there's
444 /// nothing we can do to coax a value out of them, other than calling
445 /// Extract{Element,Value}. We can effectively treat them as pointers to
446 /// arbitrary memory locations we can store in and load from.
447 void visitExtractElementInst(ExtractElementInst &Inst) {
448 auto *Ptr = Inst.getVectorOperand();
449 auto *Val = &Inst;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000450 addLoadEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000451 }
452
453 void visitInsertElementInst(InsertElementInst &Inst) {
454 auto *Vec = Inst.getOperand(0);
455 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000456 addAssignEdge(Vec, &Inst);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000457 addStoreEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000458 }
459
460 void visitLandingPadInst(LandingPadInst &Inst) {
461 // Exceptions come from "nowhere", from our analysis' perspective.
462 // So we place the instruction its own group, noting that said group may
463 // alias externals
George Burgess IV0a9cbd42016-07-29 01:23:45 +0000464 if (Inst.getType()->isPointerTy())
465 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000466 }
467
468 void visitInsertValueInst(InsertValueInst &Inst) {
469 auto *Agg = Inst.getOperand(0);
470 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000471 addAssignEdge(Agg, &Inst);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000472 addStoreEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000473 }
474
475 void visitExtractValueInst(ExtractValueInst &Inst) {
476 auto *Ptr = Inst.getAggregateOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000477 addLoadEdge(Ptr, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000478 }
479
480 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
481 auto *From1 = Inst.getOperand(0);
482 auto *From2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000483 addAssignEdge(From1, &Inst);
484 addAssignEdge(From2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000485 }
486
487 void visitConstantExpr(ConstantExpr *CE) {
488 switch (CE->getOpcode()) {
George Burgess IV6febd832016-07-20 22:53:30 +0000489 case Instruction::GetElementPtr: {
490 auto GEPOp = cast<GEPOperator>(CE);
491 visitGEP(*GEPOp);
492 break;
493 }
494 case Instruction::PtrToInt: {
495 auto *Ptr = CE->getOperand(0);
496 addNode(Ptr, getAttrEscaped());
497 break;
498 }
499 case Instruction::IntToPtr: {
500 addNode(CE, getAttrUnknown());
501 break;
502 }
503 case Instruction::BitCast:
504 case Instruction::AddrSpaceCast:
505 case Instruction::Trunc:
506 case Instruction::ZExt:
507 case Instruction::SExt:
508 case Instruction::FPExt:
509 case Instruction::FPTrunc:
510 case Instruction::UIToFP:
511 case Instruction::SIToFP:
512 case Instruction::FPToUI:
513 case Instruction::FPToSI: {
514 auto *Src = CE->getOperand(0);
515 addAssignEdge(Src, CE);
516 break;
517 }
518 case Instruction::Select: {
519 auto *TrueVal = CE->getOperand(0);
520 auto *FalseVal = CE->getOperand(1);
521 addAssignEdge(TrueVal, CE);
522 addAssignEdge(FalseVal, CE);
523 break;
524 }
525 case Instruction::InsertElement: {
526 auto *Vec = CE->getOperand(0);
527 auto *Val = CE->getOperand(1);
528 addAssignEdge(Vec, CE);
529 addStoreEdge(Val, CE);
530 break;
531 }
532 case Instruction::ExtractElement: {
533 auto *Ptr = CE->getOperand(0);
534 addLoadEdge(Ptr, CE);
535 break;
536 }
537 case Instruction::InsertValue: {
538 auto *Agg = CE->getOperand(0);
539 auto *Val = CE->getOperand(1);
540 addAssignEdge(Agg, CE);
541 addStoreEdge(Val, CE);
542 break;
543 }
544 case Instruction::ExtractValue: {
545 auto *Ptr = CE->getOperand(0);
546 addLoadEdge(Ptr, CE);
George Burgess IV0a7b9892017-05-31 02:35:26 +0000547 break;
George Burgess IV6febd832016-07-20 22:53:30 +0000548 }
549 case Instruction::ShuffleVector: {
550 auto *From1 = CE->getOperand(0);
551 auto *From2 = CE->getOperand(1);
552 addAssignEdge(From1, CE);
553 addAssignEdge(From2, CE);
554 break;
555 }
556 case Instruction::Add:
557 case Instruction::Sub:
558 case Instruction::FSub:
559 case Instruction::Mul:
560 case Instruction::FMul:
561 case Instruction::UDiv:
562 case Instruction::SDiv:
563 case Instruction::FDiv:
564 case Instruction::URem:
565 case Instruction::SRem:
566 case Instruction::FRem:
567 case Instruction::And:
568 case Instruction::Or:
569 case Instruction::Xor:
570 case Instruction::Shl:
571 case Instruction::LShr:
572 case Instruction::AShr:
573 case Instruction::ICmp:
574 case Instruction::FCmp: {
575 addAssignEdge(CE->getOperand(0), CE);
576 addAssignEdge(CE->getOperand(1), CE);
577 break;
578 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000579 default:
580 llvm_unreachable("Unknown instruction type encountered!");
George Burgess IVc294d0d2016-07-09 02:54:42 +0000581 }
582 }
583 };
584
585 // Helper functions
586
587 // Determines whether or not we an instruction is useless to us (e.g.
588 // FenceInst)
589 static bool hasUsefulEdges(Instruction *Inst) {
590 bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) &&
591 !isa<InvokeInst>(Inst) &&
592 !isa<ReturnInst>(Inst);
593 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
594 !IsNonInvokeRetTerminator;
595 }
596
597 void addArgumentToGraph(Argument &Arg) {
598 if (Arg.getType()->isPointerTy()) {
George Burgess IVde1be712016-07-11 22:59:09 +0000599 Graph.addNode(InstantiatedValue{&Arg, 0},
600 getGlobalOrArgAttrFromValue(Arg));
George Burgess IVc294d0d2016-07-09 02:54:42 +0000601 // Pointees of a formal parameter is known to the caller
George Burgess IVde1be712016-07-11 22:59:09 +0000602 Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000603 }
604 }
605
606 // Given an Instruction, this will add it to the graph, along with any
607 // Instructions that are potentially only available from said Instruction
608 // For example, given the following line:
609 // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
610 // addInstructionToGraph would add both the `load` and `getelementptr`
611 // instructions to the graph appropriately.
George Burgess IVde1be712016-07-11 22:59:09 +0000612 void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000613 if (!hasUsefulEdges(&Inst))
614 return;
615
George Burgess IVde1be712016-07-11 22:59:09 +0000616 Visitor.visit(Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000617 }
618
619 // Builds the graph needed for constructing the StratifiedSets for the given
620 // function
621 void buildGraphFrom(Function &Fn) {
George Burgess IV6febd832016-07-20 22:53:30 +0000622 GetEdgesVisitor Visitor(*this, Fn.getParent()->getDataLayout());
George Burgess IVde1be712016-07-11 22:59:09 +0000623
George Burgess IVc294d0d2016-07-09 02:54:42 +0000624 for (auto &Bb : Fn.getBasicBlockList())
625 for (auto &Inst : Bb.getInstList())
George Burgess IVde1be712016-07-11 22:59:09 +0000626 addInstructionToGraph(Visitor, Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000627
628 for (auto &Arg : Fn.args())
629 addArgumentToGraph(Arg);
630 }
631
632public:
633 CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn)
634 : Analysis(Analysis), TLI(TLI) {
635 buildGraphFrom(Fn);
636 }
637
638 const CFLGraph &getCFLGraph() const { return Graph; }
639 const SmallVector<Value *, 4> &getReturnValues() const {
640 return ReturnedValues;
641 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000642};
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000643}
644}
645
646#endif