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