blob: 24d6710ffcb828935f063c470e736f92c7e9587c [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;
43 };
44
45 typedef std::vector<Edge> EdgeList;
46
47 struct NodeInfo {
48 EdgeList Edges;
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
80 const NodeInfo *getNode(Node N) const {
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 NodeInfo *getNode(Node N) {
George Burgess IVde1be712016-07-11 22:59:09 +000087 auto Itr = ValueImpls.find(N.Val);
88 if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
George Burgess IV1ca8aff2016-07-06 00:36:12 +000089 return nullptr;
George Burgess IVde1be712016-07-11 22:59:09 +000090 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
George Burgess IV1ca8aff2016-07-06 00:36:12 +000091 }
92
George Burgess IV1ca8aff2016-07-06 00:36:12 +000093public:
George Burgess IVde1be712016-07-11 22:59:09 +000094 typedef ValueMap::const_iterator const_value_iterator;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000095
George Burgess IVde1be712016-07-11 22:59:09 +000096 bool addNode(Node N, AliasAttrs Attr = AliasAttrs()) {
97 assert(N.Val != nullptr);
98 auto &ValInfo = ValueImpls[N.Val];
99 auto Changed = ValInfo.addNodeToLevel(N.DerefLevel);
100 ValInfo.getNodeInfoAtLevel(N.DerefLevel).Attr |= Attr;
101 return Changed;
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000102 }
103
George Burgess IVe1919962016-07-06 00:47:21 +0000104 void addAttr(Node N, AliasAttrs Attr) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000105 auto *Info = getNode(N);
106 assert(Info != nullptr);
107 Info->Attr |= Attr;
108 }
109
George Burgess IVde1be712016-07-11 22:59:09 +0000110 void addEdge(Node From, Node To, int64_t Offset = 0) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000111 auto *FromInfo = getNode(From);
112 assert(FromInfo != nullptr);
113 auto *ToInfo = getNode(To);
114 assert(ToInfo != nullptr);
115
George Burgess IVde1be712016-07-11 22:59:09 +0000116 FromInfo->Edges.push_back(Edge{To});
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000117 }
118
George Burgess IVe1919962016-07-06 00:47:21 +0000119 AliasAttrs attrFor(Node N) const {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000120 auto *Info = getNode(N);
121 assert(Info != nullptr);
122 return Info->Attr;
123 }
124
George Burgess IVde1be712016-07-11 22:59:09 +0000125 iterator_range<const_value_iterator> value_mappings() const {
126 return make_range<const_value_iterator>(ValueImpls.begin(),
127 ValueImpls.end());
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000128 }
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000129};
George Burgess IVc294d0d2016-07-09 02:54:42 +0000130
131///\brief A builder class used to create CFLGraph instance from a given function
132/// The CFL-AA that uses this builder must provide its own type as a template
133/// argument. This is necessary for interprocedural processing: CFLGraphBuilder
134/// needs a way of obtaining the summary of other functions when callinsts are
135/// encountered.
136/// As a result, we expect the said CFL-AA to expose a getAliasSummary() public
137/// member function that takes a Function& and returns the corresponding summary
138/// as a const AliasSummary*.
139template <typename CFLAA> class CFLGraphBuilder {
140 // Input of the builder
141 CFLAA &Analysis;
142 const TargetLibraryInfo &TLI;
143
144 // Output of the builder
145 CFLGraph Graph;
146 SmallVector<Value *, 4> ReturnedValues;
147
George Burgess IVc294d0d2016-07-09 02:54:42 +0000148 // Helper class
149 /// Gets the edges our graph should have, based on an Instruction*
150 class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
151 CFLAA &AA;
152 const TargetLibraryInfo &TLI;
153
154 CFLGraph &Graph;
155 SmallVectorImpl<Value *> &ReturnValues;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000156
157 static bool hasUsefulEdges(ConstantExpr *CE) {
158 // ConstantExpr doesn't have terminators, invokes, or fences, so only
159 // needs
160 // to check for compares.
161 return CE->getOpcode() != Instruction::ICmp &&
162 CE->getOpcode() != Instruction::FCmp;
163 }
164
165 // Returns possible functions called by CS into the given SmallVectorImpl.
166 // Returns true if targets found, false otherwise.
167 static bool getPossibleTargets(CallSite CS,
168 SmallVectorImpl<Function *> &Output) {
169 if (auto *Fn = CS.getCalledFunction()) {
170 Output.push_back(Fn);
171 return true;
172 }
173
174 // TODO: If the call is indirect, we might be able to enumerate all
175 // potential
176 // targets of the call and return them, rather than just failing.
177 return false;
178 }
179
George Burgess IVde1be712016-07-11 22:59:09 +0000180 void addNode(Value *Val, AliasAttrs Attr = AliasAttrs()) {
181 assert(Val != nullptr && Val->getType()->isPointerTy());
182 if (auto GVal = dyn_cast<GlobalValue>(Val)) {
183 if (Graph.addNode(InstantiatedValue{GVal, 0},
184 getGlobalOrArgAttrFromValue(*GVal)))
185 Graph.addNode(InstantiatedValue{GVal, 1}, getAttrUnknown());
186 } else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) {
187 if (hasUsefulEdges(CExpr)) {
188 if (Graph.addNode(InstantiatedValue{CExpr, 0}))
189 visitConstantExpr(CExpr);
190 }
191 } else
192 Graph.addNode(InstantiatedValue{Val, 0}, Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000193 }
194
George Burgess IVde1be712016-07-11 22:59:09 +0000195 void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000196 assert(From != nullptr && To != nullptr);
197 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
198 return;
199 addNode(From);
George Burgess IVde1be712016-07-11 22:59:09 +0000200 if (To != From) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000201 addNode(To);
George Burgess IVde1be712016-07-11 22:59:09 +0000202 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 0},
203 Offset);
204 }
205 }
206
207 void addDerefEdge(Value *From, Value *To) {
208 assert(From != nullptr && To != nullptr);
209 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
210 return;
211 addNode(From);
212 addNode(To);
213 Graph.addNode(InstantiatedValue{From, 1});
214 Graph.addEdge(InstantiatedValue{From, 1}, InstantiatedValue{To, 0});
George Burgess IVc294d0d2016-07-09 02:54:42 +0000215 }
216
217 public:
218 GetEdgesVisitor(CFLGraphBuilder &Builder)
219 : AA(Builder.Analysis), TLI(Builder.TLI), Graph(Builder.Graph),
George Burgess IVde1be712016-07-11 22:59:09 +0000220 ReturnValues(Builder.ReturnedValues) {}
George Burgess IVc294d0d2016-07-09 02:54:42 +0000221
222 void visitInstruction(Instruction &) {
223 llvm_unreachable("Unsupported instruction encountered");
224 }
225
226 void visitReturnInst(ReturnInst &Inst) {
227 if (auto RetVal = Inst.getReturnValue()) {
228 if (RetVal->getType()->isPointerTy()) {
229 addNode(RetVal);
230 ReturnValues.push_back(RetVal);
231 }
232 }
233 }
234
235 void visitPtrToIntInst(PtrToIntInst &Inst) {
236 auto *Ptr = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000237 addNode(Ptr, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000238 }
239
240 void visitIntToPtrInst(IntToPtrInst &Inst) {
241 auto *Ptr = &Inst;
George Burgess IVde1be712016-07-11 22:59:09 +0000242 addNode(Ptr, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000243 }
244
245 void visitCastInst(CastInst &Inst) {
246 auto *Src = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000247 addAssignEdge(Src, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000248 }
249
250 void visitBinaryOperator(BinaryOperator &Inst) {
251 auto *Op1 = Inst.getOperand(0);
252 auto *Op2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000253 addAssignEdge(Op1, &Inst);
254 addAssignEdge(Op2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000255 }
256
257 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
258 auto *Ptr = Inst.getPointerOperand();
259 auto *Val = Inst.getNewValOperand();
George Burgess IVde1be712016-07-11 22:59:09 +0000260 addDerefEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000261 }
262
263 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
264 auto *Ptr = Inst.getPointerOperand();
265 auto *Val = Inst.getValOperand();
George Burgess IVde1be712016-07-11 22:59:09 +0000266 addDerefEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000267 }
268
269 void visitPHINode(PHINode &Inst) {
270 for (Value *Val : Inst.incoming_values())
George Burgess IVde1be712016-07-11 22:59:09 +0000271 addAssignEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000272 }
273
274 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
275 auto *Op = Inst.getPointerOperand();
George Burgess IVde1be712016-07-11 22:59:09 +0000276 addAssignEdge(Op, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000277 }
278
279 void visitSelectInst(SelectInst &Inst) {
280 // Condition is not processed here (The actual statement producing
281 // the condition result is processed elsewhere). For select, the
282 // condition is evaluated, but not loaded, stored, or assigned
283 // simply as a result of being the condition of a select.
284
285 auto *TrueVal = Inst.getTrueValue();
286 auto *FalseVal = Inst.getFalseValue();
George Burgess IVde1be712016-07-11 22:59:09 +0000287 addAssignEdge(TrueVal, &Inst);
288 addAssignEdge(FalseVal, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000289 }
290
George Burgess IVde1be712016-07-11 22:59:09 +0000291 void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000292
293 void visitLoadInst(LoadInst &Inst) {
294 auto *Ptr = Inst.getPointerOperand();
295 auto *Val = &Inst;
George Burgess IVde1be712016-07-11 22:59:09 +0000296 addDerefEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000297 }
298
299 void visitStoreInst(StoreInst &Inst) {
300 auto *Ptr = Inst.getPointerOperand();
301 auto *Val = Inst.getValueOperand();
George Burgess IVde1be712016-07-11 22:59:09 +0000302 addDerefEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000303 }
304
305 void visitVAArgInst(VAArgInst &Inst) {
306 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it
307 // does
308 // two things:
309 // 1. Loads a value from *((T*)*Ptr).
310 // 2. Increments (stores to) *Ptr by some target-specific amount.
311 // For now, we'll handle this like a landingpad instruction (by placing
312 // the
313 // result in its own group, and having that group alias externals).
George Burgess IVde1be712016-07-11 22:59:09 +0000314 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000315 }
316
317 static bool isFunctionExternal(Function *Fn) {
318 return !Fn->hasExactDefinition();
319 }
320
321 bool tryInterproceduralAnalysis(CallSite CS,
322 const SmallVectorImpl<Function *> &Fns) {
323 assert(Fns.size() > 0);
324
325 if (CS.arg_size() > MaxSupportedArgsInSummary)
326 return false;
327
328 // Exit early if we'll fail anyway
329 for (auto *Fn : Fns) {
330 if (isFunctionExternal(Fn) || Fn->isVarArg())
331 return false;
332 // Fail if the caller does not provide enough arguments
333 assert(Fn->arg_size() <= CS.arg_size());
334 if (!AA.getAliasSummary(*Fn))
335 return false;
336 }
337
338 for (auto *Fn : Fns) {
339 auto Summary = AA.getAliasSummary(*Fn);
340 assert(Summary != nullptr);
341
342 auto &RetParamRelations = Summary->RetParamRelations;
343 for (auto &Relation : RetParamRelations) {
344 auto IRelation = instantiateExternalRelation(Relation, CS);
George Burgess IVde1be712016-07-11 22:59:09 +0000345 if (IRelation.hasValue()) {
346 Graph.addNode(IRelation->From);
347 Graph.addNode(IRelation->To);
348 Graph.addEdge(IRelation->From, IRelation->To);
349 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000350 }
351
352 auto &RetParamAttributes = Summary->RetParamAttributes;
353 for (auto &Attribute : RetParamAttributes) {
354 auto IAttr = instantiateExternalAttribute(Attribute, CS);
355 if (IAttr.hasValue())
George Burgess IVde1be712016-07-11 22:59:09 +0000356 Graph.addNode(IAttr->IValue, IAttr->Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000357 }
358 }
359
360 return true;
361 }
362
363 void visitCallSite(CallSite CS) {
364 auto Inst = CS.getInstruction();
365
366 // Make sure all arguments and return value are added to the graph first
367 for (Value *V : CS.args())
George Burgess IVde1be712016-07-11 22:59:09 +0000368 if (V->getType()->isPointerTy())
369 addNode(V);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000370 if (Inst->getType()->isPointerTy())
371 addNode(Inst);
372
373 // Check if Inst is a call to a library function that
374 // allocates/deallocates
375 // on the heap. Those kinds of functions do not introduce any aliases.
376 // TODO: address other common library functions such as realloc(),
377 // strdup(),
378 // etc.
379 if (isMallocLikeFn(Inst, &TLI) || isCallocLikeFn(Inst, &TLI) ||
380 isFreeCall(Inst, &TLI))
381 return;
382
383 // TODO: Add support for noalias args/all the other fun function
384 // attributes
385 // that we can tack on.
386 SmallVector<Function *, 4> Targets;
387 if (getPossibleTargets(CS, Targets))
388 if (tryInterproceduralAnalysis(CS, Targets))
389 return;
390
391 // Because the function is opaque, we need to note that anything
392 // could have happened to the arguments (unless the function is marked
393 // readonly or readnone), and that the result could alias just about
394 // anything, too (unless the result is marked noalias).
395 if (!CS.onlyReadsMemory())
396 for (Value *V : CS.args()) {
397 if (V->getType()->isPointerTy()) {
398 // The argument itself escapes.
George Burgess IVde1be712016-07-11 22:59:09 +0000399 Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000400 // The fate of argument memory is unknown. Note that since
George Burgess IVde1be712016-07-11 22:59:09 +0000401 // AliasAttrs is transitive with respect to dereference, we only
402 // need to specify it for the first-level memory.
403 Graph.addNode(InstantiatedValue{V, 1}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000404 }
405 }
406
407 if (Inst->getType()->isPointerTy()) {
408 auto *Fn = CS.getCalledFunction();
409 if (Fn == nullptr || !Fn->doesNotAlias(0))
George Burgess IVde1be712016-07-11 22:59:09 +0000410 // No need to call addNode() since we've added Inst at the
George Burgess IVc294d0d2016-07-09 02:54:42 +0000411 // beginning of this function and we know it is not a global.
George Burgess IVde1be712016-07-11 22:59:09 +0000412 Graph.addAttr(InstantiatedValue{Inst, 0}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000413 }
414 }
415
416 /// Because vectors/aggregates are immutable and unaddressable, there's
417 /// nothing we can do to coax a value out of them, other than calling
418 /// Extract{Element,Value}. We can effectively treat them as pointers to
419 /// arbitrary memory locations we can store in and load from.
420 void visitExtractElementInst(ExtractElementInst &Inst) {
421 auto *Ptr = Inst.getVectorOperand();
422 auto *Val = &Inst;
George Burgess IVde1be712016-07-11 22:59:09 +0000423 addDerefEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000424 }
425
426 void visitInsertElementInst(InsertElementInst &Inst) {
427 auto *Vec = Inst.getOperand(0);
428 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000429 addAssignEdge(Vec, &Inst);
430 addDerefEdge(&Inst, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000431 }
432
433 void visitLandingPadInst(LandingPadInst &Inst) {
434 // Exceptions come from "nowhere", from our analysis' perspective.
435 // So we place the instruction its own group, noting that said group may
436 // alias externals
George Burgess IVde1be712016-07-11 22:59:09 +0000437 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000438 }
439
440 void visitInsertValueInst(InsertValueInst &Inst) {
441 auto *Agg = Inst.getOperand(0);
442 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000443 addAssignEdge(Agg, &Inst);
444 addDerefEdge(&Inst, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000445 }
446
447 void visitExtractValueInst(ExtractValueInst &Inst) {
448 auto *Ptr = Inst.getAggregateOperand();
George Burgess IVde1be712016-07-11 22:59:09 +0000449 addDerefEdge(Ptr, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000450 }
451
452 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
453 auto *From1 = Inst.getOperand(0);
454 auto *From2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000455 addAssignEdge(From1, &Inst);
456 addAssignEdge(From2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000457 }
458
459 void visitConstantExpr(ConstantExpr *CE) {
460 switch (CE->getOpcode()) {
461 default:
462 llvm_unreachable("Unknown instruction type encountered!");
463// Build the switch statement using the Instruction.def file.
464#define HANDLE_INST(NUM, OPCODE, CLASS) \
465 case Instruction::OPCODE: \
466 this->visit##OPCODE(*(CLASS *)CE); \
467 break;
468#include "llvm/IR/Instruction.def"
469 }
470 }
471 };
472
473 // Helper functions
474
475 // Determines whether or not we an instruction is useless to us (e.g.
476 // FenceInst)
477 static bool hasUsefulEdges(Instruction *Inst) {
478 bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) &&
479 !isa<InvokeInst>(Inst) &&
480 !isa<ReturnInst>(Inst);
481 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
482 !IsNonInvokeRetTerminator;
483 }
484
485 void addArgumentToGraph(Argument &Arg) {
486 if (Arg.getType()->isPointerTy()) {
George Burgess IVde1be712016-07-11 22:59:09 +0000487 Graph.addNode(InstantiatedValue{&Arg, 0},
488 getGlobalOrArgAttrFromValue(Arg));
George Burgess IVc294d0d2016-07-09 02:54:42 +0000489 // Pointees of a formal parameter is known to the caller
George Burgess IVde1be712016-07-11 22:59:09 +0000490 Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000491 }
492 }
493
494 // Given an Instruction, this will add it to the graph, along with any
495 // Instructions that are potentially only available from said Instruction
496 // For example, given the following line:
497 // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
498 // addInstructionToGraph would add both the `load` and `getelementptr`
499 // instructions to the graph appropriately.
George Burgess IVde1be712016-07-11 22:59:09 +0000500 void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000501 if (!hasUsefulEdges(&Inst))
502 return;
503
George Burgess IVde1be712016-07-11 22:59:09 +0000504 Visitor.visit(Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000505 }
506
507 // Builds the graph needed for constructing the StratifiedSets for the given
508 // function
509 void buildGraphFrom(Function &Fn) {
George Burgess IVde1be712016-07-11 22:59:09 +0000510 GetEdgesVisitor Visitor(*this);
511
George Burgess IVc294d0d2016-07-09 02:54:42 +0000512 for (auto &Bb : Fn.getBasicBlockList())
513 for (auto &Inst : Bb.getInstList())
George Burgess IVde1be712016-07-11 22:59:09 +0000514 addInstructionToGraph(Visitor, Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000515
516 for (auto &Arg : Fn.args())
517 addArgumentToGraph(Arg);
518 }
519
520public:
521 CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn)
522 : Analysis(Analysis), TLI(TLI) {
523 buildGraphFrom(Fn);
524 }
525
526 const CFLGraph &getCFLGraph() const { return Graph; }
527 const SmallVector<Value *, 4> &getReturnValues() const {
528 return ReturnedValues;
529 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000530};
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000531}
532}
533
534#endif