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