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