George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 1 | //======- 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 IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 18 | #include "AliasAnalysisSummary.h" |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 21 | #include "llvm/IR/InstVisitor.h" |
| 22 | #include "llvm/IR/Instructions.h" |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 25 | namespace cflaa { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 26 | |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 32 | /// 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 IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 37 | class CFLGraph { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 38 | public: |
| 39 | typedef InstantiatedValue Node; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 40 | |
| 41 | struct Edge { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 42 | Node Other; |
| 43 | }; |
| 44 | |
| 45 | typedef std::vector<Edge> EdgeList; |
| 46 | |
| 47 | struct NodeInfo { |
| 48 | EdgeList Edges; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 49 | AliasAttrs Attr; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 52 | class ValueInfo { |
| 53 | std::vector<NodeInfo> Levels; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 54 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 55 | 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 IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 62 | } |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 63 | |
| 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 | |
| 76 | private: |
| 77 | typedef DenseMap<Value *, ValueInfo> ValueMap; |
| 78 | ValueMap ValueImpls; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 79 | |
| 80 | const NodeInfo *getNode(Node N) const { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 81 | auto Itr = ValueImpls.find(N.Val); |
| 82 | if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel) |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 83 | return nullptr; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 84 | return &Itr->second.getNodeInfoAtLevel(N.DerefLevel); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 85 | } |
| 86 | NodeInfo *getNode(Node N) { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 87 | auto Itr = ValueImpls.find(N.Val); |
| 88 | if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel) |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 89 | return nullptr; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 90 | return &Itr->second.getNodeInfoAtLevel(N.DerefLevel); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 91 | } |
| 92 | |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 93 | public: |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 94 | typedef ValueMap::const_iterator const_value_iterator; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 95 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 96 | 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 IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 104 | void addAttr(Node N, AliasAttrs Attr) { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 105 | auto *Info = getNode(N); |
| 106 | assert(Info != nullptr); |
| 107 | Info->Attr |= Attr; |
| 108 | } |
| 109 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 110 | void addEdge(Node From, Node To, int64_t Offset = 0) { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 111 | auto *FromInfo = getNode(From); |
| 112 | assert(FromInfo != nullptr); |
| 113 | auto *ToInfo = getNode(To); |
| 114 | assert(ToInfo != nullptr); |
| 115 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 116 | FromInfo->Edges.push_back(Edge{To}); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 119 | AliasAttrs attrFor(Node N) const { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 120 | auto *Info = getNode(N); |
| 121 | assert(Info != nullptr); |
| 122 | return Info->Attr; |
| 123 | } |
| 124 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 125 | iterator_range<const_value_iterator> value_mappings() const { |
| 126 | return make_range<const_value_iterator>(ValueImpls.begin(), |
| 127 | ValueImpls.end()); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 128 | } |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 129 | }; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 130 | |
| 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*. |
| 139 | template <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 IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 148 | // 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 IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 156 | |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 180 | 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 IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 193 | } |
| 194 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 195 | void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 196 | assert(From != nullptr && To != nullptr); |
| 197 | if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy()) |
| 198 | return; |
| 199 | addNode(From); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 200 | if (To != From) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 201 | addNode(To); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 202 | 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 IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | public: |
| 218 | GetEdgesVisitor(CFLGraphBuilder &Builder) |
| 219 | : AA(Builder.Analysis), TLI(Builder.TLI), Graph(Builder.Graph), |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 220 | ReturnValues(Builder.ReturnedValues) {} |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 221 | |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 237 | addNode(Ptr, getAttrEscaped()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void visitIntToPtrInst(IntToPtrInst &Inst) { |
| 241 | auto *Ptr = &Inst; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 242 | addNode(Ptr, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void visitCastInst(CastInst &Inst) { |
| 246 | auto *Src = Inst.getOperand(0); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 247 | addAssignEdge(Src, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void visitBinaryOperator(BinaryOperator &Inst) { |
| 251 | auto *Op1 = Inst.getOperand(0); |
| 252 | auto *Op2 = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 253 | addAssignEdge(Op1, &Inst); |
| 254 | addAssignEdge(Op2, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 258 | auto *Ptr = Inst.getPointerOperand(); |
| 259 | auto *Val = Inst.getNewValOperand(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 260 | addDerefEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 264 | auto *Ptr = Inst.getPointerOperand(); |
| 265 | auto *Val = Inst.getValOperand(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 266 | addDerefEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void visitPHINode(PHINode &Inst) { |
| 270 | for (Value *Val : Inst.incoming_values()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 271 | addAssignEdge(Val, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void visitGetElementPtrInst(GetElementPtrInst &Inst) { |
| 275 | auto *Op = Inst.getPointerOperand(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 276 | addAssignEdge(Op, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 277 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 287 | addAssignEdge(TrueVal, &Inst); |
| 288 | addAssignEdge(FalseVal, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 289 | } |
| 290 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 291 | void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 292 | |
| 293 | void visitLoadInst(LoadInst &Inst) { |
| 294 | auto *Ptr = Inst.getPointerOperand(); |
| 295 | auto *Val = &Inst; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 296 | addDerefEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void visitStoreInst(StoreInst &Inst) { |
| 300 | auto *Ptr = Inst.getPointerOperand(); |
| 301 | auto *Val = Inst.getValueOperand(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 302 | addDerefEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 303 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 314 | addNode(&Inst, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 315 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 345 | if (IRelation.hasValue()) { |
| 346 | Graph.addNode(IRelation->From); |
| 347 | Graph.addNode(IRelation->To); |
| 348 | Graph.addEdge(IRelation->From, IRelation->To); |
| 349 | } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | auto &RetParamAttributes = Summary->RetParamAttributes; |
| 353 | for (auto &Attribute : RetParamAttributes) { |
| 354 | auto IAttr = instantiateExternalAttribute(Attribute, CS); |
| 355 | if (IAttr.hasValue()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 356 | Graph.addNode(IAttr->IValue, IAttr->Attr); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 357 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 368 | if (V->getType()->isPointerTy()) |
| 369 | addNode(V); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 370 | 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 399 | Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 400 | // The fate of argument memory is unknown. Note that since |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 401 | // 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 IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | if (Inst->getType()->isPointerTy()) { |
| 408 | auto *Fn = CS.getCalledFunction(); |
| 409 | if (Fn == nullptr || !Fn->doesNotAlias(0)) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 410 | // No need to call addNode() since we've added Inst at the |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 411 | // beginning of this function and we know it is not a global. |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 412 | Graph.addAttr(InstantiatedValue{Inst, 0}, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 413 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 423 | addDerefEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void visitInsertElementInst(InsertElementInst &Inst) { |
| 427 | auto *Vec = Inst.getOperand(0); |
| 428 | auto *Val = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 429 | addAssignEdge(Vec, &Inst); |
| 430 | addDerefEdge(&Inst, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 431 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 437 | addNode(&Inst, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void visitInsertValueInst(InsertValueInst &Inst) { |
| 441 | auto *Agg = Inst.getOperand(0); |
| 442 | auto *Val = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 443 | addAssignEdge(Agg, &Inst); |
| 444 | addDerefEdge(&Inst, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void visitExtractValueInst(ExtractValueInst &Inst) { |
| 448 | auto *Ptr = Inst.getAggregateOperand(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 449 | addDerefEdge(Ptr, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void visitShuffleVectorInst(ShuffleVectorInst &Inst) { |
| 453 | auto *From1 = Inst.getOperand(0); |
| 454 | auto *From2 = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 455 | addAssignEdge(From1, &Inst); |
| 456 | addAssignEdge(From2, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 457 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 487 | Graph.addNode(InstantiatedValue{&Arg, 0}, |
| 488 | getGlobalOrArgAttrFromValue(Arg)); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 489 | // Pointees of a formal parameter is known to the caller |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 490 | Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 491 | } |
| 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 IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 500 | void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 501 | if (!hasUsefulEdges(&Inst)) |
| 502 | return; |
| 503 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 504 | Visitor.visit(Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Builds the graph needed for constructing the StratifiedSets for the given |
| 508 | // function |
| 509 | void buildGraphFrom(Function &Fn) { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 510 | GetEdgesVisitor Visitor(*this); |
| 511 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 512 | for (auto &Bb : Fn.getBasicBlockList()) |
| 513 | for (auto &Inst : Bb.getInstList()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame^] | 514 | addInstructionToGraph(Visitor, Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 515 | |
| 516 | for (auto &Arg : Fn.args()) |
| 517 | addArgumentToGraph(Arg); |
| 518 | } |
| 519 | |
| 520 | public: |
| 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 IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 530 | }; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | #endif |