blob: 21842ed364876e6cb9279dd0f9b0fceec1807cb7 [file] [log] [blame]
Eugene Zelenko530851c2017-08-11 21:30:02 +00001//===- CFLGraph.h - Abstract stratified sets implementation. -----*- C++-*-===//
George Burgess IV1ca8aff2016-07-06 00:36:12 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
George Burgess IV1ca8aff2016-07-06 00:36:12 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenko530851c2017-08-11 21:30:02 +00008//
George Burgess IV1ca8aff2016-07-06 00:36:12 +00009/// \file
10/// This file defines CFLGraph, an auxiliary data structure used by CFL-based
11/// alias analysis.
Eugene Zelenko530851c2017-08-11 21:30:02 +000012//
George Burgess IV1ca8aff2016-07-06 00:36:12 +000013//===----------------------------------------------------------------------===//
14
Eugene Zelenko530851c2017-08-11 21:30:02 +000015#ifndef LLVM_LIB_ANALYSIS_CFLGRAPH_H
16#define LLVM_LIB_ANALYSIS_CFLGRAPH_H
George Burgess IV1ca8aff2016-07-06 00:36:12 +000017
George Burgess IVe1919962016-07-06 00:47:21 +000018#include "AliasAnalysisSummary.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000019#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/iterator_range.h"
George Burgess IVc294d0d2016-07-09 02:54:42 +000023#include "llvm/Analysis/MemoryBuiltins.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
25#include "llvm/IR/Argument.h"
26#include "llvm/IR/BasicBlock.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000027#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/GlobalValue.h"
George Burgess IVc294d0d2016-07-09 02:54:42 +000031#include "llvm/IR/InstVisitor.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000032#include "llvm/IR/InstrTypes.h"
33#include "llvm/IR/Instruction.h"
George Burgess IVc294d0d2016-07-09 02:54:42 +000034#include "llvm/IR/Instructions.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000035#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>
George Burgess IV1ca8aff2016-07-06 00:36:12 +000043
44namespace llvm {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000045namespace cflaa {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000046
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000047/// The Program Expression Graph (PEG) of CFL analysis
George Burgess IV1ca8aff2016-07-06 00:36:12 +000048/// 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.
George Burgess IVde1be712016-07-11 22:59:09 +000052/// 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).
George Burgess IV1ca8aff2016-07-06 00:36:12 +000057class CFLGraph {
George Burgess IVde1be712016-07-11 22:59:09 +000058public:
Eugene Zelenko530851c2017-08-11 21:30:02 +000059 using Node = InstantiatedValue;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000060
61 struct Edge {
George Burgess IV1ca8aff2016-07-06 00:36:12 +000062 Node Other;
George Burgess IVc6526172018-05-17 21:56:39 +000063 int64_t Offset;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000064 };
65
Eugene Zelenko530851c2017-08-11 21:30:02 +000066 using EdgeList = std::vector<Edge>;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000067
68 struct NodeInfo {
George Burgess IV6d30aa02016-07-15 19:53:25 +000069 EdgeList Edges, ReverseEdges;
George Burgess IVe1919962016-07-06 00:47:21 +000070 AliasAttrs Attr;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000071 };
72
George Burgess IVde1be712016-07-11 22:59:09 +000073 class ValueInfo {
74 std::vector<NodeInfo> Levels;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000075
George Burgess IVde1be712016-07-11 22:59:09 +000076 public:
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;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000083 }
George Burgess IVde1be712016-07-11 22:59:09 +000084
85 NodeInfo &getNodeInfoAtLevel(unsigned Level) {
86 assert(Level < Levels.size());
87 return Levels[Level];
88 }
89 const NodeInfo &getNodeInfoAtLevel(unsigned Level) const {
90 assert(Level < Levels.size());
91 return Levels[Level];
92 }
93
94 unsigned getNumLevels() const { return Levels.size(); }
95 };
96
97private:
Eugene Zelenko530851c2017-08-11 21:30:02 +000098 using ValueMap = DenseMap<Value *, ValueInfo>;
99
George Burgess IVde1be712016-07-11 22:59:09 +0000100 ValueMap ValueImpls;
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000101
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000102 NodeInfo *getNode(Node N) {
George Burgess IVde1be712016-07-11 22:59:09 +0000103 auto Itr = ValueImpls.find(N.Val);
104 if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel)
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000105 return nullptr;
George Burgess IVde1be712016-07-11 22:59:09 +0000106 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000107 }
108
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000109public:
Eugene Zelenko530851c2017-08-11 21:30:02 +0000110 using const_value_iterator = ValueMap::const_iterator;
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000111
George Burgess IVde1be712016-07-11 22:59:09 +0000112 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;
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000118 }
119
George Burgess IVe1919962016-07-06 00:47:21 +0000120 void addAttr(Node N, AliasAttrs Attr) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000121 auto *Info = getNode(N);
122 assert(Info != nullptr);
123 Info->Attr |= Attr;
124 }
125
George Burgess IVc6526172018-05-17 21:56:39 +0000126 void addEdge(Node From, Node To, int64_t Offset = 0) {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000127 auto *FromInfo = getNode(From);
128 assert(FromInfo != nullptr);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000129 auto *ToInfo = getNode(To);
130 assert(ToInfo != nullptr);
131
George Burgess IV6febd832016-07-20 22:53:30 +0000132 FromInfo->Edges.push_back(Edge{To, Offset});
133 ToInfo->ReverseEdges.push_back(Edge{From, Offset});
George Burgess IV6d30aa02016-07-15 19:53:25 +0000134 }
135
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);
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000141 }
142
George Burgess IVe1919962016-07-06 00:47:21 +0000143 AliasAttrs attrFor(Node N) const {
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000144 auto *Info = getNode(N);
145 assert(Info != nullptr);
146 return Info->Attr;
147 }
148
George Burgess IVde1be712016-07-11 22:59:09 +0000149 iterator_range<const_value_iterator> value_mappings() const {
150 return make_range<const_value_iterator>(ValueImpls.begin(),
151 ValueImpls.end());
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000152 }
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000153};
George Burgess IVc294d0d2016-07-09 02:54:42 +0000154
George Burgess IVc24a2f42019-06-03 19:56:22 +0000155/// A builder class used to create CFLGraph instance from a given function
George Burgess IVc294d0d2016-07-09 02:54:42 +0000156/// 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*.
163template <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
George Burgess IVc294d0d2016-07-09 02:54:42 +0000172 // Helper class
173 /// Gets the edges our graph should have, based on an Instruction*
174 class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
175 CFLAA &AA;
George Burgess IV6febd832016-07-20 22:53:30 +0000176 const DataLayout &DL;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000177 const TargetLibraryInfo &TLI;
178
179 CFLGraph &Graph;
180 SmallVectorImpl<Value *> &ReturnValues;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000181
182 static bool hasUsefulEdges(ConstantExpr *CE) {
183 // ConstantExpr doesn't have terminators, invokes, or fences, so only
George Burgess IVc24a2f42019-06-03 19:56:22 +0000184 // needs to check for compares.
George Burgess IVc294d0d2016-07-09 02:54:42 +0000185 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.
Chandler Carruth9beadff2019-02-11 09:25:41 +0000191 static bool getPossibleTargets(CallBase &Call,
George Burgess IVc294d0d2016-07-09 02:54:42 +0000192 SmallVectorImpl<Function *> &Output) {
Chandler Carruth9beadff2019-02-11 09:25:41 +0000193 if (auto *Fn = Call.getCalledFunction()) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000194 Output.push_back(Fn);
195 return true;
196 }
197
198 // TODO: If the call is indirect, we might be able to enumerate all
George Burgess IVc24a2f42019-06-03 19:56:22 +0000199 // potential targets of the call and return them, rather than just
200 // failing.
George Burgess IVc294d0d2016-07-09 02:54:42 +0000201 return false;
202 }
203
George Burgess IVde1be712016-07-11 22:59:09 +0000204 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);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000217 }
218
George Burgess IVc6526172018-05-17 21:56:39 +0000219 void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000220 assert(From != nullptr && To != nullptr);
221 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
222 return;
223 addNode(From);
George Burgess IVde1be712016-07-11 22:59:09 +0000224 if (To != From) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000225 addNode(To);
George Burgess IVde1be712016-07-11 22:59:09 +0000226 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 0},
227 Offset);
228 }
229 }
230
George Burgess IV6d30aa02016-07-15 19:53:25 +0000231 void addDerefEdge(Value *From, Value *To, bool IsRead) {
George Burgess IVde1be712016-07-11 22:59:09 +0000232 assert(From != nullptr && To != nullptr);
George Burgess IV0a7b9892017-05-31 02:35:26 +0000233 // 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.
George Burgess IVde1be712016-07-11 22:59:09 +0000238 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
239 return;
240 addNode(From);
241 addNode(To);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000242 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 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000249 }
250
George Burgess IV6d30aa02016-07-15 19:53:25 +0000251 void addLoadEdge(Value *From, Value *To) { addDerefEdge(From, To, true); }
252 void addStoreEdge(Value *From, Value *To) { addDerefEdge(From, To, false); }
253
George Burgess IVc294d0d2016-07-09 02:54:42 +0000254 public:
George Burgess IV6febd832016-07-20 22:53:30 +0000255 GetEdgesVisitor(CFLGraphBuilder &Builder, const DataLayout &DL)
256 : AA(Builder.Analysis), DL(DL), TLI(Builder.TLI), Graph(Builder.Graph),
George Burgess IVde1be712016-07-11 22:59:09 +0000257 ReturnValues(Builder.ReturnedValues) {}
George Burgess IVc294d0d2016-07-09 02:54:42 +0000258
259 void visitInstruction(Instruction &) {
260 llvm_unreachable("Unsupported instruction encountered");
261 }
262
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
272 void visitPtrToIntInst(PtrToIntInst &Inst) {
273 auto *Ptr = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000274 addNode(Ptr, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000275 }
276
277 void visitIntToPtrInst(IntToPtrInst &Inst) {
278 auto *Ptr = &Inst;
George Burgess IVde1be712016-07-11 22:59:09 +0000279 addNode(Ptr, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000280 }
281
282 void visitCastInst(CastInst &Inst) {
283 auto *Src = Inst.getOperand(0);
George Burgess IVde1be712016-07-11 22:59:09 +0000284 addAssignEdge(Src, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000285 }
286
287 void visitBinaryOperator(BinaryOperator &Inst) {
288 auto *Op1 = Inst.getOperand(0);
289 auto *Op2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000290 addAssignEdge(Op1, &Inst);
291 addAssignEdge(Op2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000292 }
293
Craig Topperca541b22019-06-06 19:21:23 +0000294 void visitUnaryOperator(UnaryOperator &Inst) {
295 auto *Src = Inst.getOperand(0);
296 addAssignEdge(Src, &Inst);
297 }
298
George Burgess IVc294d0d2016-07-09 02:54:42 +0000299 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
300 auto *Ptr = Inst.getPointerOperand();
301 auto *Val = Inst.getNewValOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000302 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000303 }
304
305 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
306 auto *Ptr = Inst.getPointerOperand();
307 auto *Val = Inst.getValOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000308 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000309 }
310
311 void visitPHINode(PHINode &Inst) {
312 for (Value *Val : Inst.incoming_values())
George Burgess IVde1be712016-07-11 22:59:09 +0000313 addAssignEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000314 }
315
George Burgess IV6febd832016-07-20 22:53:30 +0000316 void visitGEP(GEPOperator &GEPOp) {
George Burgess IVc6526172018-05-17 21:56:39 +0000317 uint64_t Offset = UnknownOffset;
George Burgess IV6febd832016-07-20 22:53:30 +0000318 APInt APOffset(DL.getPointerSizeInBits(GEPOp.getPointerAddressSpace()),
319 0);
320 if (GEPOp.accumulateConstantOffset(DL, APOffset))
321 Offset = APOffset.getSExtValue();
322
323 auto *Op = GEPOp.getPointerOperand();
324 addAssignEdge(Op, &GEPOp, Offset);
325 }
326
George Burgess IVc294d0d2016-07-09 02:54:42 +0000327 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
George Burgess IV6febd832016-07-20 22:53:30 +0000328 auto *GEPOp = cast<GEPOperator>(&Inst);
329 visitGEP(*GEPOp);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000330 }
331
332 void visitSelectInst(SelectInst &Inst) {
333 // Condition is not processed here (The actual statement producing
334 // the condition result is processed elsewhere). For select, the
335 // condition is evaluated, but not loaded, stored, or assigned
336 // simply as a result of being the condition of a select.
337
338 auto *TrueVal = Inst.getTrueValue();
339 auto *FalseVal = Inst.getFalseValue();
George Burgess IVde1be712016-07-11 22:59:09 +0000340 addAssignEdge(TrueVal, &Inst);
341 addAssignEdge(FalseVal, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000342 }
343
George Burgess IVde1be712016-07-11 22:59:09 +0000344 void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000345
346 void visitLoadInst(LoadInst &Inst) {
347 auto *Ptr = Inst.getPointerOperand();
348 auto *Val = &Inst;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000349 addLoadEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000350 }
351
352 void visitStoreInst(StoreInst &Inst) {
353 auto *Ptr = Inst.getPointerOperand();
354 auto *Val = Inst.getValueOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000355 addStoreEdge(Val, Ptr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000356 }
357
358 void visitVAArgInst(VAArgInst &Inst) {
359 // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it
360 // does
361 // two things:
362 // 1. Loads a value from *((T*)*Ptr).
363 // 2. Increments (stores to) *Ptr by some target-specific amount.
364 // For now, we'll handle this like a landingpad instruction (by placing
365 // the
366 // result in its own group, and having that group alias externals).
George Burgess IV0a9cbd42016-07-29 01:23:45 +0000367 if (Inst.getType()->isPointerTy())
368 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000369 }
370
371 static bool isFunctionExternal(Function *Fn) {
372 return !Fn->hasExactDefinition();
373 }
374
Chandler Carruth9beadff2019-02-11 09:25:41 +0000375 bool tryInterproceduralAnalysis(CallBase &Call,
George Burgess IVc294d0d2016-07-09 02:54:42 +0000376 const SmallVectorImpl<Function *> &Fns) {
377 assert(Fns.size() > 0);
378
Chandler Carruth9beadff2019-02-11 09:25:41 +0000379 if (Call.arg_size() > MaxSupportedArgsInSummary)
George Burgess IVc294d0d2016-07-09 02:54:42 +0000380 return false;
381
382 // Exit early if we'll fail anyway
383 for (auto *Fn : Fns) {
384 if (isFunctionExternal(Fn) || Fn->isVarArg())
385 return false;
386 // Fail if the caller does not provide enough arguments
Chandler Carruth9beadff2019-02-11 09:25:41 +0000387 assert(Fn->arg_size() <= Call.arg_size());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000388 if (!AA.getAliasSummary(*Fn))
389 return false;
390 }
391
392 for (auto *Fn : Fns) {
393 auto Summary = AA.getAliasSummary(*Fn);
394 assert(Summary != nullptr);
395
396 auto &RetParamRelations = Summary->RetParamRelations;
397 for (auto &Relation : RetParamRelations) {
Chandler Carruth9beadff2019-02-11 09:25:41 +0000398 auto IRelation = instantiateExternalRelation(Relation, Call);
George Burgess IVde1be712016-07-11 22:59:09 +0000399 if (IRelation.hasValue()) {
400 Graph.addNode(IRelation->From);
401 Graph.addNode(IRelation->To);
George Burgess IVc6526172018-05-17 21:56:39 +0000402 Graph.addEdge(IRelation->From, IRelation->To);
George Burgess IVde1be712016-07-11 22:59:09 +0000403 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000404 }
405
406 auto &RetParamAttributes = Summary->RetParamAttributes;
407 for (auto &Attribute : RetParamAttributes) {
Chandler Carruth9beadff2019-02-11 09:25:41 +0000408 auto IAttr = instantiateExternalAttribute(Attribute, Call);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000409 if (IAttr.hasValue())
George Burgess IVde1be712016-07-11 22:59:09 +0000410 Graph.addNode(IAttr->IValue, IAttr->Attr);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000411 }
412 }
413
414 return true;
415 }
416
Chandler Carruth9beadff2019-02-11 09:25:41 +0000417 void visitCallBase(CallBase &Call) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000418 // Make sure all arguments and return value are added to the graph first
Chandler Carruth9beadff2019-02-11 09:25:41 +0000419 for (Value *V : Call.args())
George Burgess IVde1be712016-07-11 22:59:09 +0000420 if (V->getType()->isPointerTy())
421 addNode(V);
Chandler Carruth9beadff2019-02-11 09:25:41 +0000422 if (Call.getType()->isPointerTy())
423 addNode(&Call);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000424
425 // Check if Inst is a call to a library function that
George Burgess IVe8168182018-05-26 02:17:43 +0000426 // allocates/deallocates on the heap. Those kinds of functions do not
427 // introduce any aliases.
George Burgess IVc294d0d2016-07-09 02:54:42 +0000428 // TODO: address other common library functions such as realloc(),
George Burgess IVe8168182018-05-26 02:17:43 +0000429 // strdup(), etc.
Chandler Carruth9beadff2019-02-11 09:25:41 +0000430 if (isMallocOrCallocLikeFn(&Call, &TLI) || isFreeCall(&Call, &TLI))
George Burgess IVc294d0d2016-07-09 02:54:42 +0000431 return;
432
433 // TODO: Add support for noalias args/all the other fun function
George Burgess IVe8168182018-05-26 02:17:43 +0000434 // attributes that we can tack on.
George Burgess IVc294d0d2016-07-09 02:54:42 +0000435 SmallVector<Function *, 4> Targets;
Chandler Carruth9beadff2019-02-11 09:25:41 +0000436 if (getPossibleTargets(Call, Targets))
437 if (tryInterproceduralAnalysis(Call, Targets))
George Burgess IVc294d0d2016-07-09 02:54:42 +0000438 return;
439
440 // Because the function is opaque, we need to note that anything
441 // could have happened to the arguments (unless the function is marked
442 // readonly or readnone), and that the result could alias just about
443 // anything, too (unless the result is marked noalias).
Chandler Carruth9beadff2019-02-11 09:25:41 +0000444 if (!Call.onlyReadsMemory())
445 for (Value *V : Call.args()) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000446 if (V->getType()->isPointerTy()) {
447 // The argument itself escapes.
George Burgess IVde1be712016-07-11 22:59:09 +0000448 Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000449 // The fate of argument memory is unknown. Note that since
George Burgess IVde1be712016-07-11 22:59:09 +0000450 // AliasAttrs is transitive with respect to dereference, we only
451 // need to specify it for the first-level memory.
452 Graph.addNode(InstantiatedValue{V, 1}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000453 }
454 }
455
Chandler Carruth9beadff2019-02-11 09:25:41 +0000456 if (Call.getType()->isPointerTy()) {
457 auto *Fn = Call.getCalledFunction();
Reid Klecknera0b45f42017-05-03 18:17:31 +0000458 if (Fn == nullptr || !Fn->returnDoesNotAlias())
George Burgess IVde1be712016-07-11 22:59:09 +0000459 // No need to call addNode() since we've added Inst at the
George Burgess IVc294d0d2016-07-09 02:54:42 +0000460 // beginning of this function and we know it is not a global.
Chandler Carruth9beadff2019-02-11 09:25:41 +0000461 Graph.addAttr(InstantiatedValue{&Call, 0}, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000462 }
463 }
464
465 /// Because vectors/aggregates are immutable and unaddressable, there's
466 /// nothing we can do to coax a value out of them, other than calling
467 /// Extract{Element,Value}. We can effectively treat them as pointers to
468 /// arbitrary memory locations we can store in and load from.
469 void visitExtractElementInst(ExtractElementInst &Inst) {
470 auto *Ptr = Inst.getVectorOperand();
471 auto *Val = &Inst;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000472 addLoadEdge(Ptr, Val);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000473 }
474
475 void visitInsertElementInst(InsertElementInst &Inst) {
476 auto *Vec = Inst.getOperand(0);
477 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000478 addAssignEdge(Vec, &Inst);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000479 addStoreEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000480 }
481
482 void visitLandingPadInst(LandingPadInst &Inst) {
483 // Exceptions come from "nowhere", from our analysis' perspective.
484 // So we place the instruction its own group, noting that said group may
485 // alias externals
George Burgess IV0a9cbd42016-07-29 01:23:45 +0000486 if (Inst.getType()->isPointerTy())
487 addNode(&Inst, getAttrUnknown());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000488 }
489
490 void visitInsertValueInst(InsertValueInst &Inst) {
491 auto *Agg = Inst.getOperand(0);
492 auto *Val = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000493 addAssignEdge(Agg, &Inst);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000494 addStoreEdge(Val, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000495 }
496
497 void visitExtractValueInst(ExtractValueInst &Inst) {
498 auto *Ptr = Inst.getAggregateOperand();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000499 addLoadEdge(Ptr, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000500 }
501
502 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
503 auto *From1 = Inst.getOperand(0);
504 auto *From2 = Inst.getOperand(1);
George Burgess IVde1be712016-07-11 22:59:09 +0000505 addAssignEdge(From1, &Inst);
506 addAssignEdge(From2, &Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000507 }
508
509 void visitConstantExpr(ConstantExpr *CE) {
510 switch (CE->getOpcode()) {
George Burgess IV6febd832016-07-20 22:53:30 +0000511 case Instruction::GetElementPtr: {
512 auto GEPOp = cast<GEPOperator>(CE);
513 visitGEP(*GEPOp);
514 break;
515 }
David Bolvansky8d693602018-05-01 21:35:32 +0000516
George Burgess IV6febd832016-07-20 22:53:30 +0000517 case Instruction::PtrToInt: {
David Bolvansky8d693602018-05-01 21:35:32 +0000518 addNode(CE->getOperand(0), getAttrEscaped());
George Burgess IV6febd832016-07-20 22:53:30 +0000519 break;
520 }
David Bolvansky8d693602018-05-01 21:35:32 +0000521
522 case Instruction::IntToPtr: {
George Burgess IV6febd832016-07-20 22:53:30 +0000523 addNode(CE, getAttrUnknown());
524 break;
David Bolvansky8d693602018-05-01 21:35:32 +0000525 }
Eugene Zelenko530851c2017-08-11 21:30:02 +0000526
George Burgess IV6febd832016-07-20 22:53:30 +0000527 case Instruction::BitCast:
528 case Instruction::AddrSpaceCast:
529 case Instruction::Trunc:
530 case Instruction::ZExt:
531 case Instruction::SExt:
532 case Instruction::FPExt:
533 case Instruction::FPTrunc:
534 case Instruction::UIToFP:
535 case Instruction::SIToFP:
536 case Instruction::FPToUI:
537 case Instruction::FPToSI: {
David Bolvansky8d693602018-05-01 21:35:32 +0000538 addAssignEdge(CE->getOperand(0), CE);
George Burgess IV6febd832016-07-20 22:53:30 +0000539 break;
540 }
David Bolvansky8d693602018-05-01 21:35:32 +0000541
David Bolvansky10c218d2018-05-10 11:47:36 +0000542 case Instruction::Select: {
543 addAssignEdge(CE->getOperand(1), CE);
544 addAssignEdge(CE->getOperand(2), CE);
545 break;
546 }
547
David Bolvansky8d693602018-05-01 21:35:32 +0000548 case Instruction::InsertElement:
George Burgess IV6febd832016-07-20 22:53:30 +0000549 case Instruction::InsertValue: {
David Bolvansky8d693602018-05-01 21:35:32 +0000550 addAssignEdge(CE->getOperand(0), CE);
551 addStoreEdge(CE->getOperand(1), CE);
George Burgess IV6febd832016-07-20 22:53:30 +0000552 break;
553 }
David Bolvansky8d693602018-05-01 21:35:32 +0000554
555 case Instruction::ExtractElement:
George Burgess IV6febd832016-07-20 22:53:30 +0000556 case Instruction::ExtractValue: {
David Bolvansky8d693602018-05-01 21:35:32 +0000557 addLoadEdge(CE->getOperand(0), CE);
George Burgess IV0a7b9892017-05-31 02:35:26 +0000558 break;
George Burgess IV6febd832016-07-20 22:53:30 +0000559 }
David Bolvansky8d693602018-05-01 21:35:32 +0000560
George Burgess IV6febd832016-07-20 22:53:30 +0000561 case Instruction::Add:
Craig Topper7a4eabe2019-06-03 19:35:52 +0000562 case Instruction::FAdd:
George Burgess IV6febd832016-07-20 22:53:30 +0000563 case Instruction::Sub:
564 case Instruction::FSub:
565 case Instruction::Mul:
566 case Instruction::FMul:
567 case Instruction::UDiv:
568 case Instruction::SDiv:
569 case Instruction::FDiv:
570 case Instruction::URem:
571 case Instruction::SRem:
572 case Instruction::FRem:
573 case Instruction::And:
574 case Instruction::Or:
575 case Instruction::Xor:
576 case Instruction::Shl:
577 case Instruction::LShr:
578 case Instruction::AShr:
579 case Instruction::ICmp:
Eugene Zelenko530851c2017-08-11 21:30:02 +0000580 case Instruction::FCmp:
David Bolvansky8d693602018-05-01 21:35:32 +0000581 case Instruction::ShuffleVector: {
George Burgess IV6febd832016-07-20 22:53:30 +0000582 addAssignEdge(CE->getOperand(0), CE);
583 addAssignEdge(CE->getOperand(1), CE);
584 break;
David Bolvansky8d693602018-05-01 21:35:32 +0000585 }
Eugene Zelenko530851c2017-08-11 21:30:02 +0000586
Craig Topperca541b22019-06-06 19:21:23 +0000587 case Instruction::FNeg: {
588 addAssignEdge(CE->getOperand(0), CE);
589 break;
590 }
591
George Burgess IVc294d0d2016-07-09 02:54:42 +0000592 default:
593 llvm_unreachable("Unknown instruction type encountered!");
George Burgess IVc294d0d2016-07-09 02:54:42 +0000594 }
595 }
596 };
597
598 // Helper functions
599
600 // Determines whether or not we an instruction is useless to us (e.g.
601 // FenceInst)
602 static bool hasUsefulEdges(Instruction *Inst) {
Chandler Carruth9ae926b2018-08-26 09:51:22 +0000603 bool IsNonInvokeRetTerminator = Inst->isTerminator() &&
George Burgess IVc294d0d2016-07-09 02:54:42 +0000604 !isa<InvokeInst>(Inst) &&
605 !isa<ReturnInst>(Inst);
606 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
607 !IsNonInvokeRetTerminator;
608 }
609
610 void addArgumentToGraph(Argument &Arg) {
611 if (Arg.getType()->isPointerTy()) {
George Burgess IVde1be712016-07-11 22:59:09 +0000612 Graph.addNode(InstantiatedValue{&Arg, 0},
613 getGlobalOrArgAttrFromValue(Arg));
George Burgess IVc294d0d2016-07-09 02:54:42 +0000614 // Pointees of a formal parameter is known to the caller
George Burgess IVde1be712016-07-11 22:59:09 +0000615 Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller());
George Burgess IVc294d0d2016-07-09 02:54:42 +0000616 }
617 }
618
619 // Given an Instruction, this will add it to the graph, along with any
620 // Instructions that are potentially only available from said Instruction
621 // For example, given the following line:
622 // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2
623 // addInstructionToGraph would add both the `load` and `getelementptr`
624 // instructions to the graph appropriately.
George Burgess IVde1be712016-07-11 22:59:09 +0000625 void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) {
George Burgess IVc294d0d2016-07-09 02:54:42 +0000626 if (!hasUsefulEdges(&Inst))
627 return;
628
George Burgess IVde1be712016-07-11 22:59:09 +0000629 Visitor.visit(Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000630 }
631
632 // Builds the graph needed for constructing the StratifiedSets for the given
633 // function
634 void buildGraphFrom(Function &Fn) {
George Burgess IV6febd832016-07-20 22:53:30 +0000635 GetEdgesVisitor Visitor(*this, Fn.getParent()->getDataLayout());
George Burgess IVde1be712016-07-11 22:59:09 +0000636
George Burgess IVc294d0d2016-07-09 02:54:42 +0000637 for (auto &Bb : Fn.getBasicBlockList())
638 for (auto &Inst : Bb.getInstList())
George Burgess IVde1be712016-07-11 22:59:09 +0000639 addInstructionToGraph(Visitor, Inst);
George Burgess IVc294d0d2016-07-09 02:54:42 +0000640
641 for (auto &Arg : Fn.args())
642 addArgumentToGraph(Arg);
643 }
644
645public:
646 CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn)
647 : Analysis(Analysis), TLI(TLI) {
648 buildGraphFrom(Fn);
649 }
650
651 const CFLGraph &getCFLGraph() const { return Graph; }
652 const SmallVector<Value *, 4> &getReturnValues() const {
653 return ReturnedValues;
654 }
George Burgess IVc294d0d2016-07-09 02:54:42 +0000655};
George Burgess IV1ca8aff2016-07-06 00:36:12 +0000656
Eugene Zelenko530851c2017-08-11 21:30:02 +0000657} // end namespace cflaa
658} // end namespace llvm
659
660#endif // LLVM_LIB_ANALYSIS_CFLGRAPH_H