Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1 | //===- CFLAliasAnalysis.cpp - CFL-Based Alias Analysis 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 | // |
| 10 | // This file implements a CFL-based context-insensitive alias analysis |
| 11 | // algorithm. It does not depend on types. The algorithm is a mixture of the one |
| 12 | // described in "Demand-driven alias analysis for C" by Xin Zheng and Radu |
| 13 | // Rugina, and "Fast algorithms for Dyck-CFL-reachability with applications to |
| 14 | // Alias Analysis" by Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the |
| 15 | // papers, we build a graph of the uses of a variable, where each node is a |
| 16 | // memory location, and each edge is an action that happened on that memory |
Chad Rosier | 38c6ad2 | 2015-06-19 17:32:57 +0000 | [diff] [blame] | 17 | // location. The "actions" can be one of Dereference, Reference, or Assign. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 18 | // |
| 19 | // Two variables are considered as aliasing iff you can reach one value's node |
| 20 | // from the other value's node and the language formed by concatenating all of |
| 21 | // the edge labels (actions) conforms to a context-free grammar. |
| 22 | // |
| 23 | // Because this algorithm requires a graph search on each query, we execute the |
| 24 | // algorithm outlined in "Fast algorithms..." (mentioned above) |
| 25 | // in order to transform the graph into sets of variables that may alias in |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 26 | // ~nlogn time (n = number of variables), which makes queries take constant |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 27 | // time. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 30 | // N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and |
| 31 | // CFLAA is interprocedural. This is *technically* A Bad Thing, because |
| 32 | // FunctionPasses are only allowed to inspect the Function that they're being |
| 33 | // run on. Realistically, this likely isn't a problem until we allow |
| 34 | // FunctionPasses to run concurrently. |
| 35 | |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/CFLAliasAnalysis.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 37 | #include "StratifiedSets.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/DenseMap.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/None.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/Optional.h" |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 42 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Constants.h" |
| 44 | #include "llvm/IR/Function.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 45 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 47 | #include "llvm/Pass.h" |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Compiler.h" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 52 | #include <algorithm> |
| 53 | #include <cassert> |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 54 | #include <memory> |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 55 | #include <tuple> |
| 56 | |
| 57 | using namespace llvm; |
| 58 | |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 59 | #define DEBUG_TYPE "cfl-aa" |
| 60 | |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 61 | CFLAAResult::CFLAAResult(const TargetLibraryInfo &TLI) |
| 62 | : AAResultBase(), TLI(TLI) {} |
| 63 | CFLAAResult::CFLAAResult(CFLAAResult &&Arg) |
| 64 | : AAResultBase(std::move(Arg)), TLI(Arg.TLI) {} |
Chandler Carruth | 342c671 | 2016-02-20 03:52:02 +0000 | [diff] [blame] | 65 | CFLAAResult::~CFLAAResult() {} |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 66 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 67 | /// We use InterfaceValue to describe parameters/return value, as well as |
| 68 | /// potential memory locations that are pointed to by parameters/return value, |
| 69 | /// of a function. |
| 70 | /// Index is an integer which represents a single parameter or a return value. |
| 71 | /// When the index is 0, it refers to the return value. Non-zero index i refers |
| 72 | /// to the i-th parameter. |
| 73 | /// DerefLevel indicates the number of dereferences one must perform on the |
| 74 | /// parameter/return value to get this InterfaceValue. |
| 75 | struct InterfaceValue { |
| 76 | unsigned Index; |
| 77 | unsigned DerefLevel; |
| 78 | }; |
| 79 | |
| 80 | bool operator==(InterfaceValue lhs, InterfaceValue rhs) { |
| 81 | return lhs.Index == rhs.Index && lhs.DerefLevel == rhs.DerefLevel; |
| 82 | } |
| 83 | bool operator!=(InterfaceValue lhs, InterfaceValue rhs) { |
| 84 | return !(lhs == rhs); |
| 85 | } |
| 86 | |
| 87 | /// We use ExternalRelation to describe an externally visible aliasing relations |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 88 | /// between parameters/return value of a function. |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 89 | struct ExternalRelation { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 90 | InterfaceValue From, To; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 91 | }; |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 92 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 93 | /// We use ExternalAttribute to describe an externally visible StratifiedAttrs |
| 94 | /// for parameters/return value. |
| 95 | struct ExternalAttribute { |
| 96 | InterfaceValue IValue; |
| 97 | StratifiedAttrs Attr; |
| 98 | }; |
| 99 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 100 | /// Information we have about a function and would like to keep around. |
| 101 | class CFLAAResult::FunctionInfo { |
| 102 | StratifiedSets<Value *> Sets; |
| 103 | |
| 104 | // RetParamRelations is a collection of ExternalRelations. |
| 105 | SmallVector<ExternalRelation, 8> RetParamRelations; |
| 106 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 107 | // RetParamAttributes is a collection of ExternalAttributes. |
| 108 | SmallVector<ExternalAttribute, 8> RetParamAttributes; |
| 109 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 110 | public: |
| 111 | FunctionInfo(Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
| 112 | StratifiedSets<Value *> S); |
| 113 | |
| 114 | const StratifiedSets<Value *> &getStratifiedSets() const { return Sets; } |
| 115 | const SmallVectorImpl<ExternalRelation> &getRetParamRelations() const { |
| 116 | return RetParamRelations; |
| 117 | } |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 118 | const SmallVectorImpl<ExternalAttribute> &getRetParamAttributes() const { |
| 119 | return RetParamAttributes; |
| 120 | } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 123 | /// Try to go from a Value* to a Function*. Never returns nullptr. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 124 | static Optional<Function *> parentFunctionOfValue(Value *); |
| 125 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 126 | /// Returns possible functions called by the Inst* into the given |
| 127 | /// SmallVectorImpl. Returns true if targets found, false otherwise. This is |
| 128 | /// templated so we can use it with CallInsts and InvokeInsts. |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 129 | static bool getPossibleTargets(CallSite, SmallVectorImpl<Function *> &); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 130 | |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 131 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 132 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 133 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 134 | namespace { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 135 | /// StratifiedInfo Attribute things. |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 136 | LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 137 | LLVM_CONSTEXPR unsigned AttrEscapedIndex = 0; |
| 138 | LLVM_CONSTEXPR unsigned AttrUnknownIndex = 1; |
| 139 | LLVM_CONSTEXPR unsigned AttrGlobalIndex = 2; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 140 | LLVM_CONSTEXPR unsigned AttrCallerIndex = 3; |
| 141 | LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 4; |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 142 | LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex; |
| 143 | LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 144 | |
George Burgess IV | d9c39fc | 2016-06-24 01:41:29 +0000 | [diff] [blame] | 145 | // NOTE: These aren't StratifiedAttrs because bitsets don't have a constexpr |
| 146 | // ctor for some versions of MSVC that we support. We could maybe refactor, |
| 147 | // but... |
| 148 | using StratifiedAttr = unsigned; |
| 149 | LLVM_CONSTEXPR StratifiedAttr AttrNone = 0; |
| 150 | LLVM_CONSTEXPR StratifiedAttr AttrEscaped = 1 << AttrEscapedIndex; |
| 151 | LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex; |
| 152 | LLVM_CONSTEXPR StratifiedAttr AttrGlobal = 1 << AttrGlobalIndex; |
| 153 | LLVM_CONSTEXPR StratifiedAttr AttrCaller = 1 << AttrCallerIndex; |
| 154 | LLVM_CONSTEXPR StratifiedAttr ExternalAttrMask = |
| 155 | AttrEscaped | AttrUnknown | AttrGlobal; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 156 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 157 | /// The maximum number of arguments we can put into a summary. |
| 158 | LLVM_CONSTEXPR unsigned MaxSupportedArgsInSummary = 50; |
| 159 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 160 | /// StratifiedSets call for knowledge of "direction", so this is how we |
| 161 | /// represent that locally. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 162 | enum class Level { Same, Above, Below }; |
| 163 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 164 | /// Edges can be one of four "weights" -- each weight must have an inverse |
| 165 | /// weight (Assign has Assign; Reference has Dereference). |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 166 | enum class EdgeType { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 167 | /// The weight assigned when assigning from or to a value. For example, in: |
| 168 | /// %b = getelementptr %a, 0 |
| 169 | /// ...The relationships are %b assign %a, and %a assign %b. This used to be |
| 170 | /// two edges, but having a distinction bought us nothing. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 171 | Assign, |
| 172 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 173 | /// The edge used when we have an edge going from some handle to a Value. |
| 174 | /// Examples of this include: |
| 175 | /// %b = load %a (%b Dereference %a) |
| 176 | /// %b = extractelement %a, 0 (%a Dereference %b) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 177 | Dereference, |
| 178 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 179 | /// The edge used when our edge goes from a value to a handle that may have |
| 180 | /// contained it at some point. Examples: |
| 181 | /// %b = load %a (%a Reference %b) |
| 182 | /// %b = extractelement %a, 0 (%b Reference %a) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 183 | Reference |
| 184 | }; |
| 185 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 186 | /// The Program Expression Graph (PEG) of CFL analysis |
| 187 | class CFLGraph { |
| 188 | typedef Value *Node; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 189 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 190 | struct Edge { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 191 | EdgeType Type; |
| 192 | Node Other; |
| 193 | }; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 194 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 195 | typedef std::vector<Edge> EdgeList; |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 196 | |
| 197 | struct NodeInfo { |
| 198 | EdgeList Edges; |
| 199 | StratifiedAttrs Attr; |
| 200 | }; |
| 201 | |
| 202 | typedef DenseMap<Node, NodeInfo> NodeMap; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 203 | NodeMap NodeImpls; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 204 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 205 | // Gets the inverse of a given EdgeType. |
| 206 | static EdgeType flipWeight(EdgeType Initial) { |
| 207 | switch (Initial) { |
| 208 | case EdgeType::Assign: |
| 209 | return EdgeType::Assign; |
| 210 | case EdgeType::Dereference: |
| 211 | return EdgeType::Reference; |
| 212 | case EdgeType::Reference: |
| 213 | return EdgeType::Dereference; |
| 214 | } |
| 215 | llvm_unreachable("Incomplete coverage of EdgeType enum"); |
| 216 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 217 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 218 | const NodeInfo *getNode(Node N) const { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 219 | auto Itr = NodeImpls.find(N); |
| 220 | if (Itr == NodeImpls.end()) |
| 221 | return nullptr; |
| 222 | return &Itr->second; |
| 223 | } |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 224 | NodeInfo *getNode(Node N) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 225 | auto Itr = NodeImpls.find(N); |
| 226 | if (Itr == NodeImpls.end()) |
| 227 | return nullptr; |
| 228 | return &Itr->second; |
| 229 | } |
| 230 | |
| 231 | static Node nodeDeref(const NodeMap::value_type &P) { return P.first; } |
| 232 | typedef std::pointer_to_unary_function<const NodeMap::value_type &, Node> |
| 233 | NodeDerefFun; |
| 234 | |
| 235 | public: |
| 236 | typedef EdgeList::const_iterator const_edge_iterator; |
| 237 | typedef mapped_iterator<NodeMap::const_iterator, NodeDerefFun> |
| 238 | const_node_iterator; |
| 239 | |
| 240 | bool addNode(Node N) { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 241 | return NodeImpls.insert(std::make_pair(N, NodeInfo{EdgeList(), AttrNone})) |
| 242 | .second; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 243 | } |
| 244 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 245 | void addAttr(Node N, StratifiedAttrs Attr) { |
| 246 | auto *Info = getNode(N); |
| 247 | assert(Info != nullptr); |
| 248 | Info->Attr |= Attr; |
| 249 | } |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 250 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 251 | void addEdge(Node From, Node To, EdgeType Type) { |
| 252 | auto *FromInfo = getNode(From); |
| 253 | assert(FromInfo != nullptr); |
| 254 | auto *ToInfo = getNode(To); |
| 255 | assert(ToInfo != nullptr); |
| 256 | |
| 257 | FromInfo->Edges.push_back(Edge{Type, To}); |
| 258 | ToInfo->Edges.push_back(Edge{flipWeight(Type), From}); |
| 259 | } |
| 260 | |
| 261 | StratifiedAttrs attrFor(Node N) const { |
| 262 | auto *Info = getNode(N); |
| 263 | assert(Info != nullptr); |
| 264 | return Info->Attr; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | iterator_range<const_edge_iterator> edgesFor(Node N) const { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 268 | auto *Info = getNode(N); |
| 269 | assert(Info != nullptr); |
| 270 | auto &Edges = Info->Edges; |
| 271 | return make_range(Edges.begin(), Edges.end()); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | iterator_range<const_node_iterator> nodes() const { |
| 275 | return make_range<const_node_iterator>( |
| 276 | map_iterator(NodeImpls.begin(), NodeDerefFun(nodeDeref)), |
| 277 | map_iterator(NodeImpls.end(), NodeDerefFun(nodeDeref))); |
| 278 | } |
| 279 | |
| 280 | bool empty() const { return NodeImpls.empty(); } |
| 281 | std::size_t size() const { return NodeImpls.size(); } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 284 | // This is the result of instantiating InterfaceValue at a particular callsite |
| 285 | struct InterprocNode { |
| 286 | Value *Val; |
| 287 | unsigned DerefLevel; |
| 288 | }; |
| 289 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 290 | // Interprocedural assignment edges that CFLGraph may not easily model |
| 291 | struct InterprocEdge { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 292 | InterprocNode From, To; |
| 293 | }; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 294 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 295 | // Interprocedural attribute tagging that CFLGraph may not easily model |
| 296 | struct InterprocAttr { |
| 297 | InterprocNode Node; |
| 298 | StratifiedAttrs Attr; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 301 | /// Gets the edges our graph should have, based on an Instruction* |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 302 | class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 303 | CFLAAResult &AA; |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 304 | const TargetLibraryInfo &TLI; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 305 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 306 | CFLGraph &Graph; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 307 | SmallVectorImpl<Value *> &ReturnValues; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 308 | SmallPtrSetImpl<Value *> &Externals; |
| 309 | SmallPtrSetImpl<Value *> &Escapes; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 310 | SmallVectorImpl<InterprocEdge> &InterprocEdges; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 311 | SmallVectorImpl<InterprocAttr> &InterprocAttrs; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 312 | |
| 313 | static bool hasUsefulEdges(ConstantExpr *CE) { |
| 314 | // ConstantExpr doesn't have terminators, invokes, or fences, so only needs |
| 315 | // to check for compares. |
| 316 | return CE->getOpcode() != Instruction::ICmp && |
| 317 | CE->getOpcode() != Instruction::FCmp; |
| 318 | } |
| 319 | |
| 320 | void addNode(Value *Val) { |
| 321 | if (!Graph.addNode(Val)) |
| 322 | return; |
| 323 | |
| 324 | if (isa<GlobalValue>(Val)) |
| 325 | Externals.insert(Val); |
| 326 | else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) |
| 327 | if (hasUsefulEdges(CExpr)) |
| 328 | visitConstantExpr(CExpr); |
| 329 | } |
| 330 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 331 | void addNodeWithAttr(Value *Val, StratifiedAttrs Attr) { |
| 332 | addNode(Val); |
| 333 | Graph.addAttr(Val, Attr); |
| 334 | } |
| 335 | |
| 336 | void addEdge(Value *From, Value *To, EdgeType Type) { |
| 337 | if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy()) |
| 338 | return; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 339 | addNode(From); |
| 340 | if (To != From) |
| 341 | addNode(To); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 342 | Graph.addEdge(From, To, Type); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 345 | public: |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 346 | GetEdgesVisitor(CFLAAResult &AA, const TargetLibraryInfo &TLI, |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 347 | CFLGraph &Graph, SmallVectorImpl<Value *> &ReturnValues, |
| 348 | SmallPtrSetImpl<Value *> &Externals, |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 349 | SmallPtrSetImpl<Value *> &Escapes, |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 350 | SmallVectorImpl<InterprocEdge> &InterprocEdges, |
| 351 | SmallVectorImpl<InterprocAttr> &InterprocAttrs) |
| 352 | : AA(AA), TLI(TLI), Graph(Graph), ReturnValues(ReturnValues), |
| 353 | Externals(Externals), Escapes(Escapes), InterprocEdges(InterprocEdges), |
| 354 | InterprocAttrs(InterprocAttrs) {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 355 | |
| 356 | void visitInstruction(Instruction &) { |
| 357 | llvm_unreachable("Unsupported instruction encountered"); |
| 358 | } |
| 359 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 360 | void visitReturnInst(ReturnInst &Inst) { |
| 361 | if (auto RetVal = Inst.getReturnValue()) { |
| 362 | if (RetVal->getType()->isPointerTy()) { |
| 363 | addNode(RetVal); |
| 364 | ReturnValues.push_back(RetVal); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 369 | void visitPtrToIntInst(PtrToIntInst &Inst) { |
| 370 | auto *Ptr = Inst.getOperand(0); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 371 | addNodeWithAttr(Ptr, AttrEscaped); |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void visitIntToPtrInst(IntToPtrInst &Inst) { |
| 375 | auto *Ptr = &Inst; |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 376 | addNodeWithAttr(Ptr, AttrUnknown); |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 379 | void visitCastInst(CastInst &Inst) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 380 | auto *Src = Inst.getOperand(0); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 381 | addEdge(Src, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void visitBinaryOperator(BinaryOperator &Inst) { |
| 385 | auto *Op1 = Inst.getOperand(0); |
| 386 | auto *Op2 = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 387 | addEdge(Op1, &Inst, EdgeType::Assign); |
| 388 | addEdge(Op2, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 392 | auto *Ptr = Inst.getPointerOperand(); |
| 393 | auto *Val = Inst.getNewValOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 394 | addEdge(Ptr, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 398 | auto *Ptr = Inst.getPointerOperand(); |
| 399 | auto *Val = Inst.getValOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 400 | addEdge(Ptr, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void visitPHINode(PHINode &Inst) { |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 404 | for (Value *Val : Inst.incoming_values()) |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 405 | addEdge(Val, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void visitGetElementPtrInst(GetElementPtrInst &Inst) { |
| 409 | auto *Op = Inst.getPointerOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 410 | addEdge(Op, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void visitSelectInst(SelectInst &Inst) { |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 414 | // Condition is not processed here (The actual statement producing |
| 415 | // the condition result is processed elsewhere). For select, the |
| 416 | // condition is evaluated, but not loaded, stored, or assigned |
| 417 | // simply as a result of being the condition of a select. |
| 418 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 419 | auto *TrueVal = Inst.getTrueValue(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 420 | auto *FalseVal = Inst.getFalseValue(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 421 | addEdge(TrueVal, &Inst, EdgeType::Assign); |
| 422 | addEdge(FalseVal, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 423 | } |
| 424 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 425 | void visitAllocaInst(AllocaInst &Inst) { Graph.addNode(&Inst); } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 426 | |
| 427 | void visitLoadInst(LoadInst &Inst) { |
| 428 | auto *Ptr = Inst.getPointerOperand(); |
| 429 | auto *Val = &Inst; |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 430 | addEdge(Val, Ptr, EdgeType::Reference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void visitStoreInst(StoreInst &Inst) { |
| 434 | auto *Ptr = Inst.getPointerOperand(); |
| 435 | auto *Val = Inst.getValueOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 436 | addEdge(Ptr, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 439 | void visitVAArgInst(VAArgInst &Inst) { |
| 440 | // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does |
| 441 | // two things: |
| 442 | // 1. Loads a value from *((T*)*Ptr). |
| 443 | // 2. Increments (stores to) *Ptr by some target-specific amount. |
| 444 | // For now, we'll handle this like a landingpad instruction (by placing the |
| 445 | // result in its own group, and having that group alias externals). |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 446 | addNodeWithAttr(&Inst, AttrUnknown); |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 449 | static bool isFunctionExternal(Function *Fn) { |
George Burgess IV | 9fdbfe1 | 2016-06-21 01:42:47 +0000 | [diff] [blame] | 450 | return !Fn->hasExactDefinition(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 451 | } |
| 452 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 453 | bool tryInterproceduralAnalysis(CallSite CS, |
| 454 | const SmallVectorImpl<Function *> &Fns) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 455 | assert(Fns.size() > 0); |
| 456 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 457 | if (CS.arg_size() > MaxSupportedArgsInSummary) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 458 | return false; |
| 459 | |
| 460 | // Exit early if we'll fail anyway |
| 461 | for (auto *Fn : Fns) { |
| 462 | if (isFunctionExternal(Fn) || Fn->isVarArg()) |
| 463 | return false; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 464 | // Fail if the caller does not provide enough arguments |
| 465 | assert(Fn->arg_size() <= CS.arg_size()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 466 | auto &MaybeInfo = AA.ensureCached(Fn); |
| 467 | if (!MaybeInfo.hasValue()) |
| 468 | return false; |
| 469 | } |
| 470 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 471 | auto InstantiateInterfaceIndex = [&CS](unsigned Index) { |
| 472 | auto Value = |
| 473 | (Index == 0) ? CS.getInstruction() : CS.getArgument(Index - 1); |
| 474 | return Value->getType()->isPointerTy() ? Value : nullptr; |
| 475 | }; |
| 476 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 477 | for (auto *Fn : Fns) { |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 478 | auto &FnInfo = AA.ensureCached(Fn); |
| 479 | assert(FnInfo.hasValue()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 480 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 481 | auto &RetParamRelations = FnInfo->getRetParamRelations(); |
| 482 | for (auto &Relation : RetParamRelations) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 483 | auto FromVal = InstantiateInterfaceIndex(Relation.From.Index); |
| 484 | auto ToVal = InstantiateInterfaceIndex(Relation.To.Index); |
| 485 | if (FromVal && ToVal) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 486 | auto FromLevel = Relation.From.DerefLevel; |
| 487 | auto ToLevel = Relation.To.DerefLevel; |
| 488 | InterprocEdges.push_back( |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 489 | InterprocEdge{InterprocNode{FromVal, FromLevel}, |
| 490 | InterprocNode{ToVal, ToLevel}}); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | auto &RetParamAttributes = FnInfo->getRetParamAttributes(); |
| 495 | for (auto &Attribute : RetParamAttributes) { |
| 496 | if (auto Val = InstantiateInterfaceIndex(Attribute.IValue.Index)) { |
| 497 | InterprocAttrs.push_back(InterprocAttr{ |
| 498 | InterprocNode{Val, Attribute.IValue.DerefLevel}, Attribute.Attr}); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 499 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 500 | } |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 501 | } |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 502 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 506 | void visitCallSite(CallSite CS) { |
| 507 | auto Inst = CS.getInstruction(); |
| 508 | |
| 509 | // Make sure all arguments and return value are added to the graph first |
| 510 | for (Value *V : CS.args()) |
| 511 | addNode(V); |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 512 | if (Inst->getType()->isPointerTy()) |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 513 | addNode(Inst); |
| 514 | |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 515 | // Check if Inst is a call to a library function that allocates/deallocates |
| 516 | // on the heap. Those kinds of functions do not introduce any aliases. |
| 517 | // TODO: address other common library functions such as realloc(), strdup(), |
| 518 | // etc. |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 519 | if (isMallocLikeFn(Inst, &TLI) || isCallocLikeFn(Inst, &TLI) || |
| 520 | isFreeCall(Inst, &TLI)) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 521 | return; |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 522 | |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 523 | // TODO: Add support for noalias args/all the other fun function attributes |
| 524 | // that we can tack on. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 525 | SmallVector<Function *, 4> Targets; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 526 | if (getPossibleTargets(CS, Targets)) |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 527 | if (tryInterproceduralAnalysis(CS, Targets)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 528 | return; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 529 | |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 530 | // Because the function is opaque, we need to note that anything |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 531 | // could have happened to the arguments (unless the function is marked |
| 532 | // readonly or readnone), and that the result could alias just about |
| 533 | // anything, too (unless the result is marked noalias). |
| 534 | if (!CS.onlyReadsMemory()) |
| 535 | for (Value *V : CS.args()) { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 536 | if (V->getType()->isPointerTy()) |
| 537 | Escapes.insert(V); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 538 | } |
| 539 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 540 | if (Inst->getType()->isPointerTy()) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 541 | auto *Fn = CS.getCalledFunction(); |
| 542 | if (Fn == nullptr || !Fn->doesNotAlias(0)) |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 543 | Graph.addAttr(Inst, AttrUnknown); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 544 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 545 | } |
| 546 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 547 | /// Because vectors/aggregates are immutable and unaddressable, there's |
| 548 | /// nothing we can do to coax a value out of them, other than calling |
| 549 | /// Extract{Element,Value}. We can effectively treat them as pointers to |
| 550 | /// arbitrary memory locations we can store in and load from. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 551 | void visitExtractElementInst(ExtractElementInst &Inst) { |
| 552 | auto *Ptr = Inst.getVectorOperand(); |
| 553 | auto *Val = &Inst; |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 554 | addEdge(Val, Ptr, EdgeType::Reference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void visitInsertElementInst(InsertElementInst &Inst) { |
| 558 | auto *Vec = Inst.getOperand(0); |
| 559 | auto *Val = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 560 | addEdge(Vec, &Inst, EdgeType::Assign); |
| 561 | addEdge(&Inst, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void visitLandingPadInst(LandingPadInst &Inst) { |
| 565 | // Exceptions come from "nowhere", from our analysis' perspective. |
| 566 | // So we place the instruction its own group, noting that said group may |
| 567 | // alias externals |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 568 | addNodeWithAttr(&Inst, AttrUnknown); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | void visitInsertValueInst(InsertValueInst &Inst) { |
| 572 | auto *Agg = Inst.getOperand(0); |
| 573 | auto *Val = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 574 | addEdge(Agg, &Inst, EdgeType::Assign); |
| 575 | addEdge(&Inst, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | void visitExtractValueInst(ExtractValueInst &Inst) { |
| 579 | auto *Ptr = Inst.getAggregateOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 580 | addEdge(&Inst, Ptr, EdgeType::Reference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | void visitShuffleVectorInst(ShuffleVectorInst &Inst) { |
| 584 | auto *From1 = Inst.getOperand(0); |
| 585 | auto *From2 = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 586 | addEdge(From1, &Inst, EdgeType::Assign); |
| 587 | addEdge(From2, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 588 | } |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 589 | |
| 590 | void visitConstantExpr(ConstantExpr *CE) { |
| 591 | switch (CE->getOpcode()) { |
| 592 | default: |
| 593 | llvm_unreachable("Unknown instruction type encountered!"); |
| 594 | // Build the switch statement using the Instruction.def file. |
| 595 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 596 | case Instruction::OPCODE: \ |
| 597 | visit##OPCODE(*(CLASS *)CE); \ |
| 598 | break; |
| 599 | #include "llvm/IR/Instruction.def" |
| 600 | } |
| 601 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 604 | class CFLGraphBuilder { |
| 605 | // Input of the builder |
| 606 | CFLAAResult &Analysis; |
| 607 | const TargetLibraryInfo &TLI; |
| 608 | |
| 609 | // Output of the builder |
| 610 | CFLGraph Graph; |
| 611 | SmallVector<Value *, 4> ReturnedValues; |
| 612 | |
| 613 | // Auxiliary structures used by the builder |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 614 | SmallPtrSet<Value *, 8> ExternalValues; |
| 615 | SmallPtrSet<Value *, 8> EscapedValues; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 616 | SmallVector<InterprocEdge, 8> InterprocEdges; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 617 | SmallVector<InterprocAttr, 8> InterprocAttrs; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 618 | |
| 619 | // Helper functions |
| 620 | |
| 621 | // Determines whether or not we an instruction is useless to us (e.g. |
| 622 | // FenceInst) |
| 623 | static bool hasUsefulEdges(Instruction *Inst) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 624 | bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) && |
| 625 | !isa<InvokeInst>(Inst) && |
| 626 | !isa<ReturnInst>(Inst); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 627 | return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 628 | !IsNonInvokeRetTerminator; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 629 | } |
| 630 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 631 | void addArgumentToGraph(Argument &Arg) { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 632 | if (Arg.getType()->isPointerTy()) { |
| 633 | Graph.addNode(&Arg); |
| 634 | ExternalValues.insert(&Arg); |
| 635 | } |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | // Given an Instruction, this will add it to the graph, along with any |
| 639 | // Instructions that are potentially only available from said Instruction |
| 640 | // For example, given the following line: |
| 641 | // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2 |
| 642 | // addInstructionToGraph would add both the `load` and `getelementptr` |
| 643 | // instructions to the graph appropriately. |
| 644 | void addInstructionToGraph(Instruction &Inst) { |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 645 | if (!hasUsefulEdges(&Inst)) |
| 646 | return; |
| 647 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 648 | GetEdgesVisitor(Analysis, TLI, Graph, ReturnedValues, ExternalValues, |
| 649 | EscapedValues, InterprocEdges, InterprocAttrs) |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 650 | .visit(Inst); |
| 651 | } |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 652 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 653 | // Builds the graph needed for constructing the StratifiedSets for the given |
| 654 | // function |
| 655 | void buildGraphFrom(Function &Fn) { |
| 656 | for (auto &Bb : Fn.getBasicBlockList()) |
| 657 | for (auto &Inst : Bb.getInstList()) |
| 658 | addInstructionToGraph(Inst); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 659 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 660 | for (auto &Arg : Fn.args()) |
| 661 | addArgumentToGraph(Arg); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | public: |
| 665 | CFLGraphBuilder(CFLAAResult &Analysis, const TargetLibraryInfo &TLI, |
| 666 | Function &Fn) |
| 667 | : Analysis(Analysis), TLI(TLI) { |
| 668 | buildGraphFrom(Fn); |
| 669 | } |
| 670 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 671 | const CFLGraph &getCFLGraph() const { return Graph; } |
| 672 | const SmallVector<Value *, 4> &getReturnValues() const { |
| 673 | return ReturnedValues; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 674 | } |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 675 | const SmallPtrSet<Value *, 8> &getExternalValues() const { |
| 676 | return ExternalValues; |
| 677 | } |
| 678 | const SmallPtrSet<Value *, 8> &getEscapedValues() const { |
| 679 | return EscapedValues; |
| 680 | } |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 681 | const SmallVector<InterprocEdge, 8> &getInterprocEdges() const { |
| 682 | return InterprocEdges; |
| 683 | } |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 684 | const SmallVector<InterprocAttr, 8> &getInterprocAttrs() const { |
| 685 | return InterprocAttrs; |
| 686 | } |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 687 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 688 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 689 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 690 | //===----------------------------------------------------------------------===// |
| 691 | // Function declarations that require types defined in the namespace above |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 694 | /// Given a StratifiedAttrs, returns true if it marks the corresponding values |
| 695 | /// as globals or arguments |
| 696 | static bool isGlobalOrArgAttr(StratifiedAttrs Attr); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 697 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 698 | /// Given a StratifiedAttrs, returns true if the corresponding values come from |
| 699 | /// an unknown source (such as opaque memory or an integer cast) |
| 700 | static bool isUnknownAttr(StratifiedAttrs Attr); |
| 701 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 702 | /// Given an argument number, returns the appropriate StratifiedAttr to set. |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 703 | static StratifiedAttrs argNumberToAttr(unsigned ArgNum); |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 704 | |
| 705 | /// Given a Value, potentially return which StratifiedAttr it maps to. |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 706 | static Optional<StratifiedAttrs> valueToAttr(Value *Val); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 707 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 708 | /// Gets the "Level" that one should travel in StratifiedSets |
| 709 | /// given an EdgeType. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 710 | static Level directionOfEdgeType(EdgeType); |
| 711 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 712 | /// Determines whether it would be pointless to add the given Value to our sets. |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 713 | static bool canSkipAddingToSets(Value *Val); |
| 714 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 715 | static Optional<Function *> parentFunctionOfValue(Value *Val) { |
| 716 | if (auto *Inst = dyn_cast<Instruction>(Val)) { |
| 717 | auto *Bb = Inst->getParent(); |
| 718 | return Bb->getParent(); |
| 719 | } |
| 720 | |
| 721 | if (auto *Arg = dyn_cast<Argument>(Val)) |
| 722 | return Arg->getParent(); |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 723 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 724 | } |
| 725 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 726 | static bool getPossibleTargets(CallSite CS, |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 727 | SmallVectorImpl<Function *> &Output) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 728 | if (auto *Fn = CS.getCalledFunction()) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 729 | Output.push_back(Fn); |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | // TODO: If the call is indirect, we might be able to enumerate all potential |
| 734 | // targets of the call and return them, rather than just failing. |
| 735 | return false; |
| 736 | } |
| 737 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 738 | static bool isGlobalOrArgAttr(StratifiedAttrs Attr) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 739 | return Attr.reset(AttrEscapedIndex) |
| 740 | .reset(AttrUnknownIndex) |
| 741 | .reset(AttrCallerIndex) |
| 742 | .any(); |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 743 | } |
| 744 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 745 | static bool isUnknownAttr(StratifiedAttrs Attr) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 746 | return Attr.test(AttrUnknownIndex) || Attr.test(AttrCallerIndex); |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 747 | } |
| 748 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 749 | static Optional<StratifiedAttrs> valueToAttr(Value *Val) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 750 | if (isa<GlobalValue>(Val)) |
George Burgess IV | d9c39fc | 2016-06-24 01:41:29 +0000 | [diff] [blame] | 751 | return StratifiedAttrs(AttrGlobal); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 752 | |
| 753 | if (auto *Arg = dyn_cast<Argument>(Val)) |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 754 | // Only pointer arguments should have the argument attribute, |
| 755 | // because things can't escape through scalars without us seeing a |
| 756 | // cast, and thus, interaction with them doesn't matter. |
| 757 | if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy()) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 758 | return argNumberToAttr(Arg->getArgNo()); |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 759 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 760 | } |
| 761 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 762 | static StratifiedAttrs argNumberToAttr(unsigned ArgNum) { |
George Burgess IV | 3c898c2 | 2015-01-21 16:37:21 +0000 | [diff] [blame] | 763 | if (ArgNum >= AttrMaxNumArgs) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 764 | return AttrUnknown; |
George Burgess IV | f10c7fc | 2016-06-27 22:50:01 +0000 | [diff] [blame^] | 765 | // N.B. MSVC complains if we use `1U` here, since StratifiedAttrs' ctor takes |
| 766 | // an unsigned long long. |
| 767 | return StratifiedAttrs(1ULL << (ArgNum + AttrFirstArgIndex)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 770 | static Level directionOfEdgeType(EdgeType Weight) { |
| 771 | switch (Weight) { |
| 772 | case EdgeType::Reference: |
| 773 | return Level::Above; |
| 774 | case EdgeType::Dereference: |
| 775 | return Level::Below; |
| 776 | case EdgeType::Assign: |
| 777 | return Level::Same; |
| 778 | } |
| 779 | llvm_unreachable("Incomplete switch coverage"); |
| 780 | } |
| 781 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 782 | static bool canSkipAddingToSets(Value *Val) { |
| 783 | // Constants can share instances, which may falsely unify multiple |
| 784 | // sets, e.g. in |
| 785 | // store i32* null, i32** %ptr1 |
| 786 | // store i32* null, i32** %ptr2 |
| 787 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 788 | // we should filter out the (potentially shared) instance to |
| 789 | // i32* null. |
| 790 | if (isa<Constant>(Val)) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 791 | // TODO: Because all of these things are constant, we can determine whether |
| 792 | // the data is *actually* mutable at graph building time. This will probably |
| 793 | // come for free/cheap with offset awareness. |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 794 | bool CanStoreMutableData = isa<GlobalValue>(Val) || |
| 795 | isa<ConstantExpr>(Val) || |
| 796 | isa<ConstantAggregate>(Val); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 797 | return !CanStoreMutableData; |
| 798 | } |
| 799 | |
| 800 | return false; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 801 | } |
| 802 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 803 | CFLAAResult::FunctionInfo::FunctionInfo(Function &Fn, |
| 804 | const SmallVectorImpl<Value *> &RetVals, |
| 805 | StratifiedSets<Value *> S) |
| 806 | : Sets(std::move(S)) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 807 | // Historically, an arbitrary upper-bound of 50 args was selected. We may want |
| 808 | // to remove this if it doesn't really matter in practice. |
| 809 | if (Fn.arg_size() > MaxSupportedArgsInSummary) |
| 810 | return; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 811 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 812 | DenseMap<StratifiedIndex, InterfaceValue> InterfaceMap; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 813 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 814 | // Our intention here is to record all InterfaceValues that share the same |
| 815 | // StratifiedIndex in RetParamRelations. For each valid InterfaceValue, we |
| 816 | // have its StratifiedIndex scanned here and check if the index is presented |
| 817 | // in InterfaceMap: if it is not, we add the correspondence to the map; |
| 818 | // otherwise, an aliasing relation is found and we add it to |
| 819 | // RetParamRelations. |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 820 | |
George Burgess IV | d14d05a | 2016-06-23 20:59:13 +0000 | [diff] [blame] | 821 | auto AddToRetParamRelations = [&](unsigned InterfaceIndex, |
| 822 | StratifiedIndex SetIndex) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 823 | unsigned Level = 0; |
| 824 | while (true) { |
| 825 | InterfaceValue CurrValue{InterfaceIndex, Level}; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 826 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 827 | auto Itr = InterfaceMap.find(SetIndex); |
| 828 | if (Itr != InterfaceMap.end()) { |
| 829 | if (CurrValue != Itr->second) |
| 830 | RetParamRelations.push_back(ExternalRelation{CurrValue, Itr->second}); |
| 831 | break; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 832 | } |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 833 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 834 | auto &Link = Sets.getLink(SetIndex); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 835 | InterfaceMap.insert(std::make_pair(SetIndex, CurrValue)); |
George Burgess IV | d9c39fc | 2016-06-24 01:41:29 +0000 | [diff] [blame] | 836 | auto ExternalAttrs = Link.Attrs & StratifiedAttrs(ExternalAttrMask); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 837 | if (ExternalAttrs.any()) |
| 838 | RetParamAttributes.push_back( |
| 839 | ExternalAttribute{CurrValue, ExternalAttrs}); |
| 840 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 841 | if (!Link.hasBelow()) |
| 842 | break; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 843 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 844 | ++Level; |
| 845 | SetIndex = Link.Below; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 846 | } |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 847 | }; |
| 848 | |
| 849 | // Populate RetParamRelations for return values |
| 850 | for (auto *RetVal : RetVals) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 851 | assert(RetVal != nullptr); |
| 852 | assert(RetVal->getType()->isPointerTy()); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 853 | auto RetInfo = Sets.find(RetVal); |
| 854 | if (RetInfo.hasValue()) |
| 855 | AddToRetParamRelations(0, RetInfo->Index); |
| 856 | } |
| 857 | |
| 858 | // Populate RetParamRelations for parameters |
| 859 | unsigned I = 0; |
| 860 | for (auto &Param : Fn.args()) { |
| 861 | if (Param.getType()->isPointerTy()) { |
| 862 | auto ParamInfo = Sets.find(&Param); |
| 863 | if (ParamInfo.hasValue()) |
| 864 | AddToRetParamRelations(I + 1, ParamInfo->Index); |
| 865 | } |
| 866 | ++I; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 870 | // Builds the graph + StratifiedSets for a function. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 871 | CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) { |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 872 | CFLGraphBuilder GraphBuilder(*this, TLI, *Fn); |
| 873 | StratifiedSetsBuilder<Value *> SetBuilder; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 874 | |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 875 | auto &Graph = GraphBuilder.getCFLGraph(); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 876 | SmallVector<Value *, 16> Worklist; |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 877 | for (auto Node : Graph.nodes()) |
| 878 | Worklist.push_back(Node); |
| 879 | |
| 880 | while (!Worklist.empty()) { |
| 881 | auto *CurValue = Worklist.pop_back_val(); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 882 | SetBuilder.add(CurValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 883 | if (canSkipAddingToSets(CurValue)) |
| 884 | continue; |
| 885 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 886 | auto Attr = Graph.attrFor(CurValue); |
| 887 | SetBuilder.noteAttributes(CurValue, Attr); |
| 888 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 889 | for (const auto &Edge : Graph.edgesFor(CurValue)) { |
| 890 | auto Label = Edge.Type; |
| 891 | auto *OtherValue = Edge.Other; |
| 892 | |
| 893 | if (canSkipAddingToSets(OtherValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 894 | continue; |
| 895 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 896 | bool Added; |
| 897 | switch (directionOfEdgeType(Label)) { |
| 898 | case Level::Above: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 899 | Added = SetBuilder.addAbove(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 900 | break; |
| 901 | case Level::Below: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 902 | Added = SetBuilder.addBelow(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 903 | break; |
| 904 | case Level::Same: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 905 | Added = SetBuilder.addWith(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 906 | break; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 907 | } |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 908 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 909 | if (Added) |
| 910 | Worklist.push_back(OtherValue); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 914 | // Special handling for globals and arguments |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 915 | for (auto *External : GraphBuilder.getExternalValues()) { |
| 916 | SetBuilder.add(External); |
| 917 | auto Attr = valueToAttr(External); |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 918 | if (Attr.hasValue()) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 919 | SetBuilder.noteAttributes(External, *Attr); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 920 | if (*Attr == AttrGlobal) |
| 921 | SetBuilder.addAttributesBelow(External, 1, AttrUnknown); |
| 922 | else |
| 923 | SetBuilder.addAttributesBelow(External, 1, AttrCaller); |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 924 | } |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 925 | } |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 926 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 927 | // Special handling for interprocedural aliases |
| 928 | for (auto &Edge : GraphBuilder.getInterprocEdges()) { |
George Burgess IV | fe1397b | 2016-06-23 19:16:04 +0000 | [diff] [blame] | 929 | auto FromVal = Edge.From.Val; |
| 930 | auto ToVal = Edge.To.Val; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 931 | SetBuilder.add(FromVal); |
| 932 | SetBuilder.add(ToVal); |
| 933 | SetBuilder.addBelowWith(FromVal, Edge.From.DerefLevel, ToVal, |
| 934 | Edge.To.DerefLevel); |
| 935 | } |
| 936 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 937 | // Special handling for interprocedural attributes |
| 938 | for (auto &IPAttr : GraphBuilder.getInterprocAttrs()) { |
| 939 | auto Val = IPAttr.Node.Val; |
| 940 | SetBuilder.add(Val); |
| 941 | SetBuilder.addAttributesBelow(Val, IPAttr.Node.DerefLevel, IPAttr.Attr); |
| 942 | } |
| 943 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 944 | // Special handling for opaque external functions |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 945 | for (auto *Escape : GraphBuilder.getEscapedValues()) { |
| 946 | SetBuilder.add(Escape); |
| 947 | SetBuilder.noteAttributes(Escape, AttrEscaped); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 948 | SetBuilder.addAttributesBelow(Escape, 1, AttrUnknown); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 949 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 950 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 951 | return FunctionInfo(*Fn, GraphBuilder.getReturnValues(), SetBuilder.build()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 954 | void CFLAAResult::scan(Function *Fn) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 955 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 956 | (void)InsertPair; |
| 957 | assert(InsertPair.second && |
| 958 | "Trying to scan a function that has already been cached"); |
| 959 | |
George Burgess IV | 6edb891 | 2016-05-02 18:09:19 +0000 | [diff] [blame] | 960 | // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call |
| 961 | // may get evaluated after operator[], potentially triggering a DenseMap |
| 962 | // resize and invalidating the reference returned by operator[] |
| 963 | auto FunInfo = buildSetsFrom(Fn); |
| 964 | Cache[Fn] = std::move(FunInfo); |
| 965 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 966 | Handles.push_front(FunctionHandle(Fn, this)); |
| 967 | } |
| 968 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 969 | void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 970 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 971 | /// Ensures that the given function is available in the cache, and returns the |
| 972 | /// entry. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 973 | const Optional<CFLAAResult::FunctionInfo> & |
| 974 | CFLAAResult::ensureCached(Function *Fn) { |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 975 | auto Iter = Cache.find(Fn); |
| 976 | if (Iter == Cache.end()) { |
| 977 | scan(Fn); |
| 978 | Iter = Cache.find(Fn); |
| 979 | assert(Iter != Cache.end()); |
| 980 | assert(Iter->second.hasValue()); |
| 981 | } |
| 982 | return Iter->second; |
| 983 | } |
| 984 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 985 | AliasResult CFLAAResult::query(const MemoryLocation &LocA, |
| 986 | const MemoryLocation &LocB) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 987 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 988 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 989 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 990 | if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy()) |
| 991 | return NoAlias; |
| 992 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 993 | Function *Fn = nullptr; |
| 994 | auto MaybeFnA = parentFunctionOfValue(ValA); |
| 995 | auto MaybeFnB = parentFunctionOfValue(ValB); |
| 996 | if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 997 | // The only times this is known to happen are when globals + InlineAsm are |
| 998 | // involved |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 999 | DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n"); |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1000 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | if (MaybeFnA.hasValue()) { |
| 1004 | Fn = *MaybeFnA; |
| 1005 | assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) && |
| 1006 | "Interprocedural queries not supported"); |
| 1007 | } else { |
| 1008 | Fn = *MaybeFnB; |
| 1009 | } |
| 1010 | |
| 1011 | assert(Fn != nullptr); |
| 1012 | auto &MaybeInfo = ensureCached(Fn); |
| 1013 | assert(MaybeInfo.hasValue()); |
| 1014 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 1015 | auto &Sets = MaybeInfo->getStratifiedSets(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1016 | auto MaybeA = Sets.find(ValA); |
| 1017 | if (!MaybeA.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1018 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1019 | |
| 1020 | auto MaybeB = Sets.find(ValB); |
| 1021 | if (!MaybeB.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1022 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1023 | |
| 1024 | auto SetA = *MaybeA; |
| 1025 | auto SetB = *MaybeB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1026 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 1027 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1028 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1029 | // If both values are local (meaning the corresponding set has attribute |
| 1030 | // AttrNone or AttrEscaped), then we know that CFLAA fully models them: they |
| 1031 | // may-alias each other if and only if they are in the same set |
| 1032 | // If at least one value is non-local (meaning it either is global/argument or |
| 1033 | // it comes from unknown sources like integer cast), the situation becomes a |
| 1034 | // bit more interesting. We follow three general rules described below: |
| 1035 | // - Non-local values may alias each other |
| 1036 | // - AttrNone values do not alias any non-local values |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 1037 | // - AttrEscaped do not alias globals/arguments, but they may alias |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1038 | // AttrUnknown values |
| 1039 | if (SetA.Index == SetB.Index) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1040 | return MayAlias; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1041 | if (AttrsA.none() || AttrsB.none()) |
| 1042 | return NoAlias; |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 1043 | if (isUnknownAttr(AttrsA) || isUnknownAttr(AttrsB)) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1044 | return MayAlias; |
| 1045 | if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB)) |
| 1046 | return MayAlias; |
| 1047 | return NoAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1048 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 1049 | |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 1050 | char CFLAA::PassID; |
| 1051 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1052 | CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) { |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 1053 | return CFLAAResult(AM.getResult<TargetLibraryAnalysis>(F)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1056 | char CFLAAWrapperPass::ID = 0; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 1057 | INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false, |
| 1058 | true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1059 | |
| 1060 | ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); } |
| 1061 | |
| 1062 | CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) { |
| 1063 | initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 1064 | } |
| 1065 | |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 1066 | void CFLAAWrapperPass::initializePass() { |
| 1067 | auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>(); |
| 1068 | Result.reset(new CFLAAResult(TLIWP.getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1072 | AU.setPreservesAll(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 1073 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 1074 | } |