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; |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 43 | int64_t Offset; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | typedef std::vector<Edge> EdgeList; |
| 47 | |
| 48 | struct NodeInfo { |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 49 | EdgeList Edges, ReverseEdges; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 50 | AliasAttrs Attr; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 53 | class ValueInfo { |
| 54 | std::vector<NodeInfo> Levels; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 55 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 56 | public: |
| 57 | bool addNodeToLevel(unsigned Level) { |
| 58 | auto NumLevels = Levels.size(); |
| 59 | if (NumLevels > Level) |
| 60 | return false; |
| 61 | Levels.resize(Level + 1); |
| 62 | return true; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 63 | } |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 64 | |
| 65 | NodeInfo &getNodeInfoAtLevel(unsigned Level) { |
| 66 | assert(Level < Levels.size()); |
| 67 | return Levels[Level]; |
| 68 | } |
| 69 | const NodeInfo &getNodeInfoAtLevel(unsigned Level) const { |
| 70 | assert(Level < Levels.size()); |
| 71 | return Levels[Level]; |
| 72 | } |
| 73 | |
| 74 | unsigned getNumLevels() const { return Levels.size(); } |
| 75 | }; |
| 76 | |
| 77 | private: |
| 78 | typedef DenseMap<Value *, ValueInfo> ValueMap; |
| 79 | ValueMap ValueImpls; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 80 | |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 81 | NodeInfo *getNode(Node N) { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 82 | auto Itr = ValueImpls.find(N.Val); |
| 83 | if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel) |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 84 | return nullptr; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 85 | return &Itr->second.getNodeInfoAtLevel(N.DerefLevel); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 88 | public: |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 89 | typedef ValueMap::const_iterator const_value_iterator; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 90 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 91 | bool addNode(Node N, AliasAttrs Attr = AliasAttrs()) { |
| 92 | assert(N.Val != nullptr); |
| 93 | auto &ValInfo = ValueImpls[N.Val]; |
| 94 | auto Changed = ValInfo.addNodeToLevel(N.DerefLevel); |
| 95 | ValInfo.getNodeInfoAtLevel(N.DerefLevel).Attr |= Attr; |
| 96 | return Changed; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 97 | } |
| 98 | |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 99 | void addAttr(Node N, AliasAttrs Attr) { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 100 | auto *Info = getNode(N); |
| 101 | assert(Info != nullptr); |
| 102 | Info->Attr |= Attr; |
| 103 | } |
| 104 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 105 | void addEdge(Node From, Node To, int64_t Offset = 0) { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 106 | auto *FromInfo = getNode(From); |
| 107 | assert(FromInfo != nullptr); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 108 | auto *ToInfo = getNode(To); |
| 109 | assert(ToInfo != nullptr); |
| 110 | |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 111 | FromInfo->Edges.push_back(Edge{To, Offset}); |
| 112 | ToInfo->ReverseEdges.push_back(Edge{From, Offset}); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | const NodeInfo *getNode(Node N) const { |
| 116 | auto Itr = ValueImpls.find(N.Val); |
| 117 | if (Itr == ValueImpls.end() || Itr->second.getNumLevels() <= N.DerefLevel) |
| 118 | return nullptr; |
| 119 | return &Itr->second.getNodeInfoAtLevel(N.DerefLevel); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 120 | } |
| 121 | |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 122 | AliasAttrs attrFor(Node N) const { |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 123 | auto *Info = getNode(N); |
| 124 | assert(Info != nullptr); |
| 125 | return Info->Attr; |
| 126 | } |
| 127 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 128 | iterator_range<const_value_iterator> value_mappings() const { |
| 129 | return make_range<const_value_iterator>(ValueImpls.begin(), |
| 130 | ValueImpls.end()); |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 131 | } |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 132 | }; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 133 | |
| 134 | ///\brief A builder class used to create CFLGraph instance from a given function |
| 135 | /// The CFL-AA that uses this builder must provide its own type as a template |
| 136 | /// argument. This is necessary for interprocedural processing: CFLGraphBuilder |
| 137 | /// needs a way of obtaining the summary of other functions when callinsts are |
| 138 | /// encountered. |
| 139 | /// As a result, we expect the said CFL-AA to expose a getAliasSummary() public |
| 140 | /// member function that takes a Function& and returns the corresponding summary |
| 141 | /// as a const AliasSummary*. |
| 142 | template <typename CFLAA> class CFLGraphBuilder { |
| 143 | // Input of the builder |
| 144 | CFLAA &Analysis; |
| 145 | const TargetLibraryInfo &TLI; |
| 146 | |
| 147 | // Output of the builder |
| 148 | CFLGraph Graph; |
| 149 | SmallVector<Value *, 4> ReturnedValues; |
| 150 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 151 | // Helper class |
| 152 | /// Gets the edges our graph should have, based on an Instruction* |
| 153 | class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> { |
| 154 | CFLAA &AA; |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 155 | const DataLayout &DL; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 156 | const TargetLibraryInfo &TLI; |
| 157 | |
| 158 | CFLGraph &Graph; |
| 159 | SmallVectorImpl<Value *> &ReturnValues; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 160 | |
| 161 | static bool hasUsefulEdges(ConstantExpr *CE) { |
| 162 | // ConstantExpr doesn't have terminators, invokes, or fences, so only |
| 163 | // needs |
| 164 | // to check for compares. |
| 165 | return CE->getOpcode() != Instruction::ICmp && |
| 166 | CE->getOpcode() != Instruction::FCmp; |
| 167 | } |
| 168 | |
| 169 | // Returns possible functions called by CS into the given SmallVectorImpl. |
| 170 | // Returns true if targets found, false otherwise. |
| 171 | static bool getPossibleTargets(CallSite CS, |
| 172 | SmallVectorImpl<Function *> &Output) { |
| 173 | if (auto *Fn = CS.getCalledFunction()) { |
| 174 | Output.push_back(Fn); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | // TODO: If the call is indirect, we might be able to enumerate all |
| 179 | // potential |
| 180 | // targets of the call and return them, rather than just failing. |
| 181 | return false; |
| 182 | } |
| 183 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 184 | void addNode(Value *Val, AliasAttrs Attr = AliasAttrs()) { |
| 185 | assert(Val != nullptr && Val->getType()->isPointerTy()); |
| 186 | if (auto GVal = dyn_cast<GlobalValue>(Val)) { |
| 187 | if (Graph.addNode(InstantiatedValue{GVal, 0}, |
| 188 | getGlobalOrArgAttrFromValue(*GVal))) |
| 189 | Graph.addNode(InstantiatedValue{GVal, 1}, getAttrUnknown()); |
| 190 | } else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) { |
| 191 | if (hasUsefulEdges(CExpr)) { |
| 192 | if (Graph.addNode(InstantiatedValue{CExpr, 0})) |
| 193 | visitConstantExpr(CExpr); |
| 194 | } |
| 195 | } else |
| 196 | Graph.addNode(InstantiatedValue{Val, 0}, Attr); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 197 | } |
| 198 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 199 | void addAssignEdge(Value *From, Value *To, int64_t Offset = 0) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 200 | assert(From != nullptr && To != nullptr); |
| 201 | if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy()) |
| 202 | return; |
| 203 | addNode(From); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 204 | if (To != From) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 205 | addNode(To); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 206 | Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 0}, |
| 207 | Offset); |
| 208 | } |
| 209 | } |
| 210 | |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 211 | void addDerefEdge(Value *From, Value *To, bool IsRead) { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 212 | assert(From != nullptr && To != nullptr); |
George Burgess IV | 0a7b989 | 2017-05-31 02:35:26 +0000 | [diff] [blame^] | 213 | // FIXME: This is subtly broken, due to how we model some instructions |
| 214 | // (e.g. extractvalue, extractelement) as loads. Since those take |
| 215 | // non-pointer operands, we'll entirely skip adding edges for those. |
| 216 | // |
| 217 | // addAssignEdge seems to have a similar issue with insertvalue, etc. |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 218 | if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy()) |
| 219 | return; |
| 220 | addNode(From); |
| 221 | addNode(To); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 222 | if (IsRead) { |
| 223 | Graph.addNode(InstantiatedValue{From, 1}); |
| 224 | Graph.addEdge(InstantiatedValue{From, 1}, InstantiatedValue{To, 0}); |
| 225 | } else { |
| 226 | Graph.addNode(InstantiatedValue{To, 1}); |
| 227 | Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 1}); |
| 228 | } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 229 | } |
| 230 | |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 231 | void addLoadEdge(Value *From, Value *To) { addDerefEdge(From, To, true); } |
| 232 | void addStoreEdge(Value *From, Value *To) { addDerefEdge(From, To, false); } |
| 233 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 234 | public: |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 235 | GetEdgesVisitor(CFLGraphBuilder &Builder, const DataLayout &DL) |
| 236 | : AA(Builder.Analysis), DL(DL), TLI(Builder.TLI), Graph(Builder.Graph), |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 237 | ReturnValues(Builder.ReturnedValues) {} |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 238 | |
| 239 | void visitInstruction(Instruction &) { |
| 240 | llvm_unreachable("Unsupported instruction encountered"); |
| 241 | } |
| 242 | |
| 243 | void visitReturnInst(ReturnInst &Inst) { |
| 244 | if (auto RetVal = Inst.getReturnValue()) { |
| 245 | if (RetVal->getType()->isPointerTy()) { |
| 246 | addNode(RetVal); |
| 247 | ReturnValues.push_back(RetVal); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void visitPtrToIntInst(PtrToIntInst &Inst) { |
| 253 | auto *Ptr = Inst.getOperand(0); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 254 | addNode(Ptr, getAttrEscaped()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void visitIntToPtrInst(IntToPtrInst &Inst) { |
| 258 | auto *Ptr = &Inst; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 259 | addNode(Ptr, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void visitCastInst(CastInst &Inst) { |
| 263 | auto *Src = Inst.getOperand(0); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 264 | addAssignEdge(Src, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void visitBinaryOperator(BinaryOperator &Inst) { |
| 268 | auto *Op1 = Inst.getOperand(0); |
| 269 | auto *Op2 = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 270 | addAssignEdge(Op1, &Inst); |
| 271 | addAssignEdge(Op2, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 275 | auto *Ptr = Inst.getPointerOperand(); |
| 276 | auto *Val = Inst.getNewValOperand(); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 277 | addStoreEdge(Val, Ptr); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 281 | auto *Ptr = Inst.getPointerOperand(); |
| 282 | auto *Val = Inst.getValOperand(); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 283 | addStoreEdge(Val, Ptr); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void visitPHINode(PHINode &Inst) { |
| 287 | for (Value *Val : Inst.incoming_values()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 288 | addAssignEdge(Val, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 289 | } |
| 290 | |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 291 | void visitGEP(GEPOperator &GEPOp) { |
| 292 | uint64_t Offset = UnknownOffset; |
| 293 | APInt APOffset(DL.getPointerSizeInBits(GEPOp.getPointerAddressSpace()), |
| 294 | 0); |
| 295 | if (GEPOp.accumulateConstantOffset(DL, APOffset)) |
| 296 | Offset = APOffset.getSExtValue(); |
| 297 | |
| 298 | auto *Op = GEPOp.getPointerOperand(); |
| 299 | addAssignEdge(Op, &GEPOp, Offset); |
| 300 | } |
| 301 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 302 | void visitGetElementPtrInst(GetElementPtrInst &Inst) { |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 303 | auto *GEPOp = cast<GEPOperator>(&Inst); |
| 304 | visitGEP(*GEPOp); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void visitSelectInst(SelectInst &Inst) { |
| 308 | // Condition is not processed here (The actual statement producing |
| 309 | // the condition result is processed elsewhere). For select, the |
| 310 | // condition is evaluated, but not loaded, stored, or assigned |
| 311 | // simply as a result of being the condition of a select. |
| 312 | |
| 313 | auto *TrueVal = Inst.getTrueValue(); |
| 314 | auto *FalseVal = Inst.getFalseValue(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 315 | addAssignEdge(TrueVal, &Inst); |
| 316 | addAssignEdge(FalseVal, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 317 | } |
| 318 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 319 | void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 320 | |
| 321 | void visitLoadInst(LoadInst &Inst) { |
| 322 | auto *Ptr = Inst.getPointerOperand(); |
| 323 | auto *Val = &Inst; |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 324 | addLoadEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void visitStoreInst(StoreInst &Inst) { |
| 328 | auto *Ptr = Inst.getPointerOperand(); |
| 329 | auto *Val = Inst.getValueOperand(); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 330 | addStoreEdge(Val, Ptr); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void visitVAArgInst(VAArgInst &Inst) { |
| 334 | // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it |
| 335 | // does |
| 336 | // two things: |
| 337 | // 1. Loads a value from *((T*)*Ptr). |
| 338 | // 2. Increments (stores to) *Ptr by some target-specific amount. |
| 339 | // For now, we'll handle this like a landingpad instruction (by placing |
| 340 | // the |
| 341 | // result in its own group, and having that group alias externals). |
George Burgess IV | 0a9cbd4 | 2016-07-29 01:23:45 +0000 | [diff] [blame] | 342 | if (Inst.getType()->isPointerTy()) |
| 343 | addNode(&Inst, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static bool isFunctionExternal(Function *Fn) { |
| 347 | return !Fn->hasExactDefinition(); |
| 348 | } |
| 349 | |
| 350 | bool tryInterproceduralAnalysis(CallSite CS, |
| 351 | const SmallVectorImpl<Function *> &Fns) { |
| 352 | assert(Fns.size() > 0); |
| 353 | |
| 354 | if (CS.arg_size() > MaxSupportedArgsInSummary) |
| 355 | return false; |
| 356 | |
| 357 | // Exit early if we'll fail anyway |
| 358 | for (auto *Fn : Fns) { |
| 359 | if (isFunctionExternal(Fn) || Fn->isVarArg()) |
| 360 | return false; |
| 361 | // Fail if the caller does not provide enough arguments |
| 362 | assert(Fn->arg_size() <= CS.arg_size()); |
| 363 | if (!AA.getAliasSummary(*Fn)) |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | for (auto *Fn : Fns) { |
| 368 | auto Summary = AA.getAliasSummary(*Fn); |
| 369 | assert(Summary != nullptr); |
| 370 | |
| 371 | auto &RetParamRelations = Summary->RetParamRelations; |
| 372 | for (auto &Relation : RetParamRelations) { |
| 373 | auto IRelation = instantiateExternalRelation(Relation, CS); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 374 | if (IRelation.hasValue()) { |
| 375 | Graph.addNode(IRelation->From); |
| 376 | Graph.addNode(IRelation->To); |
| 377 | Graph.addEdge(IRelation->From, IRelation->To); |
| 378 | } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | auto &RetParamAttributes = Summary->RetParamAttributes; |
| 382 | for (auto &Attribute : RetParamAttributes) { |
| 383 | auto IAttr = instantiateExternalAttribute(Attribute, CS); |
| 384 | if (IAttr.hasValue()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 385 | Graph.addNode(IAttr->IValue, IAttr->Attr); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | void visitCallSite(CallSite CS) { |
| 393 | auto Inst = CS.getInstruction(); |
| 394 | |
| 395 | // Make sure all arguments and return value are added to the graph first |
| 396 | for (Value *V : CS.args()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 397 | if (V->getType()->isPointerTy()) |
| 398 | addNode(V); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 399 | if (Inst->getType()->isPointerTy()) |
| 400 | addNode(Inst); |
| 401 | |
| 402 | // Check if Inst is a call to a library function that |
| 403 | // allocates/deallocates |
| 404 | // on the heap. Those kinds of functions do not introduce any aliases. |
| 405 | // TODO: address other common library functions such as realloc(), |
| 406 | // strdup(), |
| 407 | // etc. |
Craig Topper | 09bb760 | 2017-04-18 21:43:46 +0000 | [diff] [blame] | 408 | if (isMallocOrCallocLikeFn(Inst, &TLI) || isFreeCall(Inst, &TLI)) |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 409 | return; |
| 410 | |
| 411 | // TODO: Add support for noalias args/all the other fun function |
| 412 | // attributes |
| 413 | // that we can tack on. |
| 414 | SmallVector<Function *, 4> Targets; |
| 415 | if (getPossibleTargets(CS, Targets)) |
| 416 | if (tryInterproceduralAnalysis(CS, Targets)) |
| 417 | return; |
| 418 | |
| 419 | // Because the function is opaque, we need to note that anything |
| 420 | // could have happened to the arguments (unless the function is marked |
| 421 | // readonly or readnone), and that the result could alias just about |
| 422 | // anything, too (unless the result is marked noalias). |
| 423 | if (!CS.onlyReadsMemory()) |
| 424 | for (Value *V : CS.args()) { |
| 425 | if (V->getType()->isPointerTy()) { |
| 426 | // The argument itself escapes. |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 427 | Graph.addAttr(InstantiatedValue{V, 0}, getAttrEscaped()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 428 | // The fate of argument memory is unknown. Note that since |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 429 | // AliasAttrs is transitive with respect to dereference, we only |
| 430 | // need to specify it for the first-level memory. |
| 431 | Graph.addNode(InstantiatedValue{V, 1}, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | if (Inst->getType()->isPointerTy()) { |
| 436 | auto *Fn = CS.getCalledFunction(); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 437 | if (Fn == nullptr || !Fn->returnDoesNotAlias()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 438 | // 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] | 439 | // 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] | 440 | Graph.addAttr(InstantiatedValue{Inst, 0}, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | /// Because vectors/aggregates are immutable and unaddressable, there's |
| 445 | /// nothing we can do to coax a value out of them, other than calling |
| 446 | /// Extract{Element,Value}. We can effectively treat them as pointers to |
| 447 | /// arbitrary memory locations we can store in and load from. |
| 448 | void visitExtractElementInst(ExtractElementInst &Inst) { |
| 449 | auto *Ptr = Inst.getVectorOperand(); |
| 450 | auto *Val = &Inst; |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 451 | addLoadEdge(Ptr, Val); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void visitInsertElementInst(InsertElementInst &Inst) { |
| 455 | auto *Vec = Inst.getOperand(0); |
| 456 | auto *Val = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 457 | addAssignEdge(Vec, &Inst); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 458 | addStoreEdge(Val, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void visitLandingPadInst(LandingPadInst &Inst) { |
| 462 | // Exceptions come from "nowhere", from our analysis' perspective. |
| 463 | // So we place the instruction its own group, noting that said group may |
| 464 | // alias externals |
George Burgess IV | 0a9cbd4 | 2016-07-29 01:23:45 +0000 | [diff] [blame] | 465 | if (Inst.getType()->isPointerTy()) |
| 466 | addNode(&Inst, getAttrUnknown()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void visitInsertValueInst(InsertValueInst &Inst) { |
| 470 | auto *Agg = Inst.getOperand(0); |
| 471 | auto *Val = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 472 | addAssignEdge(Agg, &Inst); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 473 | addStoreEdge(Val, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | void visitExtractValueInst(ExtractValueInst &Inst) { |
| 477 | auto *Ptr = Inst.getAggregateOperand(); |
George Burgess IV | 6d30aa0 | 2016-07-15 19:53:25 +0000 | [diff] [blame] | 478 | addLoadEdge(Ptr, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void visitShuffleVectorInst(ShuffleVectorInst &Inst) { |
| 482 | auto *From1 = Inst.getOperand(0); |
| 483 | auto *From2 = Inst.getOperand(1); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 484 | addAssignEdge(From1, &Inst); |
| 485 | addAssignEdge(From2, &Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void visitConstantExpr(ConstantExpr *CE) { |
| 489 | switch (CE->getOpcode()) { |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 490 | case Instruction::GetElementPtr: { |
| 491 | auto GEPOp = cast<GEPOperator>(CE); |
| 492 | visitGEP(*GEPOp); |
| 493 | break; |
| 494 | } |
| 495 | case Instruction::PtrToInt: { |
| 496 | auto *Ptr = CE->getOperand(0); |
| 497 | addNode(Ptr, getAttrEscaped()); |
| 498 | break; |
| 499 | } |
| 500 | case Instruction::IntToPtr: { |
| 501 | addNode(CE, getAttrUnknown()); |
| 502 | break; |
| 503 | } |
| 504 | case Instruction::BitCast: |
| 505 | case Instruction::AddrSpaceCast: |
| 506 | case Instruction::Trunc: |
| 507 | case Instruction::ZExt: |
| 508 | case Instruction::SExt: |
| 509 | case Instruction::FPExt: |
| 510 | case Instruction::FPTrunc: |
| 511 | case Instruction::UIToFP: |
| 512 | case Instruction::SIToFP: |
| 513 | case Instruction::FPToUI: |
| 514 | case Instruction::FPToSI: { |
| 515 | auto *Src = CE->getOperand(0); |
| 516 | addAssignEdge(Src, CE); |
| 517 | break; |
| 518 | } |
| 519 | case Instruction::Select: { |
| 520 | auto *TrueVal = CE->getOperand(0); |
| 521 | auto *FalseVal = CE->getOperand(1); |
| 522 | addAssignEdge(TrueVal, CE); |
| 523 | addAssignEdge(FalseVal, CE); |
| 524 | break; |
| 525 | } |
| 526 | case Instruction::InsertElement: { |
| 527 | auto *Vec = CE->getOperand(0); |
| 528 | auto *Val = CE->getOperand(1); |
| 529 | addAssignEdge(Vec, CE); |
| 530 | addStoreEdge(Val, CE); |
| 531 | break; |
| 532 | } |
| 533 | case Instruction::ExtractElement: { |
| 534 | auto *Ptr = CE->getOperand(0); |
| 535 | addLoadEdge(Ptr, CE); |
| 536 | break; |
| 537 | } |
| 538 | case Instruction::InsertValue: { |
| 539 | auto *Agg = CE->getOperand(0); |
| 540 | auto *Val = CE->getOperand(1); |
| 541 | addAssignEdge(Agg, CE); |
| 542 | addStoreEdge(Val, CE); |
| 543 | break; |
| 544 | } |
| 545 | case Instruction::ExtractValue: { |
| 546 | auto *Ptr = CE->getOperand(0); |
| 547 | addLoadEdge(Ptr, CE); |
George Burgess IV | 0a7b989 | 2017-05-31 02:35:26 +0000 | [diff] [blame^] | 548 | break; |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 549 | } |
| 550 | case Instruction::ShuffleVector: { |
| 551 | auto *From1 = CE->getOperand(0); |
| 552 | auto *From2 = CE->getOperand(1); |
| 553 | addAssignEdge(From1, CE); |
| 554 | addAssignEdge(From2, CE); |
| 555 | break; |
| 556 | } |
| 557 | case Instruction::Add: |
| 558 | case Instruction::Sub: |
| 559 | case Instruction::FSub: |
| 560 | case Instruction::Mul: |
| 561 | case Instruction::FMul: |
| 562 | case Instruction::UDiv: |
| 563 | case Instruction::SDiv: |
| 564 | case Instruction::FDiv: |
| 565 | case Instruction::URem: |
| 566 | case Instruction::SRem: |
| 567 | case Instruction::FRem: |
| 568 | case Instruction::And: |
| 569 | case Instruction::Or: |
| 570 | case Instruction::Xor: |
| 571 | case Instruction::Shl: |
| 572 | case Instruction::LShr: |
| 573 | case Instruction::AShr: |
| 574 | case Instruction::ICmp: |
| 575 | case Instruction::FCmp: { |
| 576 | addAssignEdge(CE->getOperand(0), CE); |
| 577 | addAssignEdge(CE->getOperand(1), CE); |
| 578 | break; |
| 579 | } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 580 | default: |
| 581 | llvm_unreachable("Unknown instruction type encountered!"); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | }; |
| 585 | |
| 586 | // Helper functions |
| 587 | |
| 588 | // Determines whether or not we an instruction is useless to us (e.g. |
| 589 | // FenceInst) |
| 590 | static bool hasUsefulEdges(Instruction *Inst) { |
| 591 | bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) && |
| 592 | !isa<InvokeInst>(Inst) && |
| 593 | !isa<ReturnInst>(Inst); |
| 594 | return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && |
| 595 | !IsNonInvokeRetTerminator; |
| 596 | } |
| 597 | |
| 598 | void addArgumentToGraph(Argument &Arg) { |
| 599 | if (Arg.getType()->isPointerTy()) { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 600 | Graph.addNode(InstantiatedValue{&Arg, 0}, |
| 601 | getGlobalOrArgAttrFromValue(Arg)); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 602 | // Pointees of a formal parameter is known to the caller |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 603 | Graph.addNode(InstantiatedValue{&Arg, 1}, getAttrCaller()); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | // Given an Instruction, this will add it to the graph, along with any |
| 608 | // Instructions that are potentially only available from said Instruction |
| 609 | // For example, given the following line: |
| 610 | // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2 |
| 611 | // addInstructionToGraph would add both the `load` and `getelementptr` |
| 612 | // instructions to the graph appropriately. |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 613 | void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 614 | if (!hasUsefulEdges(&Inst)) |
| 615 | return; |
| 616 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 617 | Visitor.visit(Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Builds the graph needed for constructing the StratifiedSets for the given |
| 621 | // function |
| 622 | void buildGraphFrom(Function &Fn) { |
George Burgess IV | 6febd83 | 2016-07-20 22:53:30 +0000 | [diff] [blame] | 623 | GetEdgesVisitor Visitor(*this, Fn.getParent()->getDataLayout()); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 624 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 625 | for (auto &Bb : Fn.getBasicBlockList()) |
| 626 | for (auto &Inst : Bb.getInstList()) |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 627 | addInstructionToGraph(Visitor, Inst); |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 628 | |
| 629 | for (auto &Arg : Fn.args()) |
| 630 | addArgumentToGraph(Arg); |
| 631 | } |
| 632 | |
| 633 | public: |
| 634 | CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn) |
| 635 | : Analysis(Analysis), TLI(TLI) { |
| 636 | buildGraphFrom(Fn); |
| 637 | } |
| 638 | |
| 639 | const CFLGraph &getCFLGraph() const { return Graph; } |
| 640 | const SmallVector<Value *, 4> &getReturnValues() const { |
| 641 | return ReturnedValues; |
| 642 | } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 643 | }; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | #endif |