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 | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 67 | /// Information we have about a function and would like to keep around. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 68 | struct CFLAAResult::FunctionInfo { |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 69 | StratifiedSets<Value *> Sets; |
| 70 | // Lots of functions have < 4 returns. Adjust as necessary. |
| 71 | SmallVector<Value *, 4> ReturnedValues; |
| 72 | |
| 73 | FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV) |
| 74 | : Sets(std::move(S)), ReturnedValues(std::move(RV)) {} |
| 75 | }; |
| 76 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 77 | /// Try to go from a Value* to a Function*. Never returns nullptr. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 78 | static Optional<Function *> parentFunctionOfValue(Value *); |
| 79 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 80 | /// Returns possible functions called by the Inst* into the given |
| 81 | /// SmallVectorImpl. Returns true if targets found, false otherwise. This is |
| 82 | /// templated so we can use it with CallInsts and InvokeInsts. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 83 | template <typename Inst> |
| 84 | static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &); |
| 85 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 86 | /// Some instructions need to have their users tracked. Instructions like |
| 87 | /// `add` require you to get the users of the Instruction* itself, other |
| 88 | /// instructions like `store` require you to get the users of the first |
| 89 | /// operand. This function gets the "proper" value to track for each |
| 90 | /// type of instruction we support. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 91 | static Optional<Value *> getTargetValue(Instruction *); |
| 92 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 93 | /// Determines whether or not we an instruction is useless to us (e.g. |
| 94 | /// FenceInst) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 95 | static bool hasUsefulEdges(Instruction *); |
| 96 | |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 97 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 98 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 99 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 100 | namespace { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 101 | /// StratifiedInfo Attribute things. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 102 | typedef unsigned StratifiedAttr; |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 103 | LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 104 | LLVM_CONSTEXPR unsigned AttrEscapedIndex = 0; |
| 105 | LLVM_CONSTEXPR unsigned AttrUnknownIndex = 1; |
| 106 | LLVM_CONSTEXPR unsigned AttrGlobalIndex = 2; |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 107 | LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3; |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 108 | LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex; |
| 109 | LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 110 | |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 111 | LLVM_CONSTEXPR StratifiedAttr AttrNone = 0; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 112 | LLVM_CONSTEXPR StratifiedAttr AttrEscaped = 1 << AttrEscapedIndex; |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 113 | LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 114 | LLVM_CONSTEXPR StratifiedAttr AttrGlobal = 1 << AttrGlobalIndex; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 115 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 116 | /// StratifiedSets call for knowledge of "direction", so this is how we |
| 117 | /// represent that locally. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 118 | enum class Level { Same, Above, Below }; |
| 119 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 120 | /// Edges can be one of four "weights" -- each weight must have an inverse |
| 121 | /// weight (Assign has Assign; Reference has Dereference). |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 122 | enum class EdgeType { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 123 | /// The weight assigned when assigning from or to a value. For example, in: |
| 124 | /// %b = getelementptr %a, 0 |
| 125 | /// ...The relationships are %b assign %a, and %a assign %b. This used to be |
| 126 | /// two edges, but having a distinction bought us nothing. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 127 | Assign, |
| 128 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 129 | /// The edge used when we have an edge going from some handle to a Value. |
| 130 | /// Examples of this include: |
| 131 | /// %b = load %a (%b Dereference %a) |
| 132 | /// %b = extractelement %a, 0 (%a Dereference %b) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 133 | Dereference, |
| 134 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 135 | /// The edge used when our edge goes from a value to a handle that may have |
| 136 | /// contained it at some point. Examples: |
| 137 | /// %b = load %a (%a Reference %b) |
| 138 | /// %b = extractelement %a, 0 (%b Reference %a) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 139 | Reference |
| 140 | }; |
| 141 | |
| 142 | // \brief Encodes the notion of a "use" |
| 143 | struct Edge { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 144 | /// Which value the edge is coming from |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 145 | Value *From; |
| 146 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 147 | /// Which value the edge is pointing to |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 148 | Value *To; |
| 149 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 150 | /// Edge weight |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 151 | EdgeType Weight; |
| 152 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 153 | /// Whether we aliased any external values along the way that may be |
| 154 | /// invisible to the analysis (i.e. landingpad for exceptions, calls for |
| 155 | /// interprocedural analysis, etc.) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 156 | StratifiedAttrs AdditionalAttrs; |
| 157 | |
| 158 | Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A) |
| 159 | : From(From), To(To), Weight(W), AdditionalAttrs(A) {} |
| 160 | }; |
| 161 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 162 | /// Gets the edges our graph should have, based on an Instruction* |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 163 | class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 164 | CFLAAResult &AA; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 165 | SmallVectorImpl<Edge> &Output; |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 166 | const TargetLibraryInfo &TLI; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 167 | |
| 168 | public: |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 169 | GetEdgesVisitor(CFLAAResult &AA, SmallVectorImpl<Edge> &Output, |
| 170 | const TargetLibraryInfo &TLI) |
| 171 | : AA(AA), Output(Output), TLI(TLI) {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 172 | |
| 173 | void visitInstruction(Instruction &) { |
| 174 | llvm_unreachable("Unsupported instruction encountered"); |
| 175 | } |
| 176 | |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 177 | void visitPtrToIntInst(PtrToIntInst &Inst) { |
| 178 | auto *Ptr = Inst.getOperand(0); |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 179 | Output.push_back(Edge(Ptr, &Inst, EdgeType::Assign, AttrEscaped)); |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void visitIntToPtrInst(IntToPtrInst &Inst) { |
| 183 | auto *Ptr = &Inst; |
| 184 | Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown)); |
| 185 | } |
| 186 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 187 | void visitCastInst(CastInst &Inst) { |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 188 | Output.push_back( |
| 189 | Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void visitBinaryOperator(BinaryOperator &Inst) { |
| 193 | auto *Op1 = Inst.getOperand(0); |
| 194 | auto *Op2 = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 195 | Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone)); |
| 196 | Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 200 | auto *Ptr = Inst.getPointerOperand(); |
| 201 | auto *Val = Inst.getNewValOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 202 | Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 206 | auto *Ptr = Inst.getPointerOperand(); |
| 207 | auto *Val = Inst.getValOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 208 | Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void visitPHINode(PHINode &Inst) { |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 212 | for (Value *Val : Inst.incoming_values()) |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 213 | Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void visitGetElementPtrInst(GetElementPtrInst &Inst) { |
| 217 | auto *Op = Inst.getPointerOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 218 | Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void visitSelectInst(SelectInst &Inst) { |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 222 | // Condition is not processed here (The actual statement producing |
| 223 | // the condition result is processed elsewhere). For select, the |
| 224 | // condition is evaluated, but not loaded, stored, or assigned |
| 225 | // simply as a result of being the condition of a select. |
| 226 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 227 | auto *TrueVal = Inst.getTrueValue(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 228 | Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 229 | auto *FalseVal = Inst.getFalseValue(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 230 | Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void visitAllocaInst(AllocaInst &) {} |
| 234 | |
| 235 | void visitLoadInst(LoadInst &Inst) { |
| 236 | auto *Ptr = Inst.getPointerOperand(); |
| 237 | auto *Val = &Inst; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 238 | Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void visitStoreInst(StoreInst &Inst) { |
| 242 | auto *Ptr = Inst.getPointerOperand(); |
| 243 | auto *Val = Inst.getValueOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 244 | Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 247 | void visitVAArgInst(VAArgInst &Inst) { |
| 248 | // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does |
| 249 | // two things: |
| 250 | // 1. Loads a value from *((T*)*Ptr). |
| 251 | // 2. Increments (stores to) *Ptr by some target-specific amount. |
| 252 | // For now, we'll handle this like a landingpad instruction (by placing the |
| 253 | // result in its own group, and having that group alias externals). |
| 254 | auto *Val = &Inst; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 255 | Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrUnknown)); |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 258 | static bool isFunctionExternal(Function *Fn) { |
| 259 | return Fn->isDeclaration() || !Fn->hasLocalLinkage(); |
| 260 | } |
| 261 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 262 | /// Gets whether the sets at Index1 above, below, or equal to the sets at |
| 263 | /// Index2. Returns None if they are not in the same set chain. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 264 | static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets, |
| 265 | StratifiedIndex Index1, |
| 266 | StratifiedIndex Index2) { |
| 267 | if (Index1 == Index2) |
| 268 | return Level::Same; |
| 269 | |
| 270 | const auto *Current = &Sets.getLink(Index1); |
| 271 | while (Current->hasBelow()) { |
| 272 | if (Current->Below == Index2) |
| 273 | return Level::Below; |
| 274 | Current = &Sets.getLink(Current->Below); |
| 275 | } |
| 276 | |
| 277 | Current = &Sets.getLink(Index1); |
| 278 | while (Current->hasAbove()) { |
| 279 | if (Current->Above == Index2) |
| 280 | return Level::Above; |
| 281 | Current = &Sets.getLink(Current->Above); |
| 282 | } |
| 283 | |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 284 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool |
| 288 | tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns, |
| 289 | Value *FuncValue, |
| 290 | const iterator_range<User::op_iterator> &Args) { |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 291 | const unsigned ExpectedMaxArgs = 8; |
| 292 | const unsigned MaxSupportedArgs = 50; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 293 | assert(Fns.size() > 0); |
| 294 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 295 | // This algorithm is n^2, so an arbitrary upper-bound of 50 args was |
| 296 | // selected, so it doesn't take too long in insane cases. |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 297 | if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 298 | return false; |
| 299 | |
| 300 | // Exit early if we'll fail anyway |
| 301 | for (auto *Fn : Fns) { |
| 302 | if (isFunctionExternal(Fn) || Fn->isVarArg()) |
| 303 | return false; |
| 304 | auto &MaybeInfo = AA.ensureCached(Fn); |
| 305 | if (!MaybeInfo.hasValue()) |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end()); |
| 310 | SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters; |
| 311 | for (auto *Fn : Fns) { |
| 312 | auto &Info = *AA.ensureCached(Fn); |
| 313 | auto &Sets = Info.Sets; |
| 314 | auto &RetVals = Info.ReturnedValues; |
| 315 | |
| 316 | Parameters.clear(); |
| 317 | for (auto &Param : Fn->args()) { |
| 318 | auto MaybeInfo = Sets.find(&Param); |
| 319 | // Did a new parameter somehow get added to the function/slip by? |
| 320 | if (!MaybeInfo.hasValue()) |
| 321 | return false; |
| 322 | Parameters.push_back(*MaybeInfo); |
| 323 | } |
| 324 | |
| 325 | // Adding an edge from argument -> return value for each parameter that |
| 326 | // may alias the return value |
| 327 | for (unsigned I = 0, E = Parameters.size(); I != E; ++I) { |
| 328 | auto &ParamInfo = Parameters[I]; |
| 329 | auto &ArgVal = Arguments[I]; |
| 330 | bool AddEdge = false; |
| 331 | StratifiedAttrs Externals; |
| 332 | for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) { |
| 333 | auto MaybeInfo = Sets.find(RetVals[X]); |
| 334 | if (!MaybeInfo.hasValue()) |
| 335 | return false; |
| 336 | |
| 337 | auto &RetInfo = *MaybeInfo; |
| 338 | auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs; |
| 339 | auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs; |
| 340 | auto MaybeRelation = |
| 341 | getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index); |
| 342 | if (MaybeRelation.hasValue()) { |
| 343 | AddEdge = true; |
| 344 | Externals |= RetAttrs | ParamAttrs; |
| 345 | } |
| 346 | } |
| 347 | if (AddEdge) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 348 | Output.push_back( |
| 349 | Edge(FuncValue, ArgVal, EdgeType::Assign, Externals)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (Parameters.size() != Arguments.size()) |
| 353 | return false; |
| 354 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 355 | /// Adding edges between arguments for arguments that may end up aliasing |
| 356 | /// each other. This is necessary for functions such as |
| 357 | /// void foo(int** a, int** b) { *a = *b; } |
| 358 | /// (Technically, the proper sets for this would be those below |
| 359 | /// Arguments[I] and Arguments[X], but our algorithm will produce |
| 360 | /// extremely similar, and equally correct, results either way) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 361 | for (unsigned I = 0, E = Arguments.size(); I != E; ++I) { |
| 362 | auto &MainVal = Arguments[I]; |
| 363 | auto &MainInfo = Parameters[I]; |
| 364 | auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs; |
| 365 | for (unsigned X = I + 1; X != E; ++X) { |
| 366 | auto &SubInfo = Parameters[X]; |
| 367 | auto &SubVal = Arguments[X]; |
| 368 | auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs; |
| 369 | auto MaybeRelation = |
| 370 | getIndexRelation(Sets, MainInfo.Index, SubInfo.Index); |
| 371 | |
| 372 | if (!MaybeRelation.hasValue()) |
| 373 | continue; |
| 374 | |
| 375 | auto NewAttrs = SubAttrs | MainAttrs; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 376 | Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | template <typename InstT> void visitCallLikeInst(InstT &Inst) { |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 384 | // Check if Inst is a call to a library function that allocates/deallocates |
| 385 | // on the heap. Those kinds of functions do not introduce any aliases. |
| 386 | // TODO: address other common library functions such as realloc(), strdup(), |
| 387 | // etc. |
| 388 | if (isMallocLikeFn(&Inst, &TLI) || isCallocLikeFn(&Inst, &TLI)) { |
| 389 | Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrNone)); |
| 390 | return; |
| 391 | } else if (isFreeCall(&Inst, &TLI)) { |
| 392 | assert(Inst.getNumArgOperands() == 1); |
| 393 | auto argVal = Inst.arg_begin()->get(); |
| 394 | Output.push_back(Edge(argVal, argVal, EdgeType::Assign, AttrNone)); |
| 395 | return; |
| 396 | } |
| 397 | |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 398 | // TODO: Add support for noalias args/all the other fun function attributes |
| 399 | // that we can tack on. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 400 | SmallVector<Function *, 4> Targets; |
| 401 | if (getPossibleTargets(&Inst, Targets)) { |
| 402 | if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands())) |
| 403 | return; |
| 404 | // Cleanup from interprocedural analysis |
| 405 | Output.clear(); |
| 406 | } |
| 407 | |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 408 | // Because the function is opaque, we need to note that anything |
| 409 | // could have happened to the arguments, and that the result could alias |
| 410 | // just about anything, too. |
| 411 | // The goal of the loop is in part to unify many Values into one set, so we |
| 412 | // don't care if the function is void there. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 413 | for (Value *V : Inst.arg_operands()) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 414 | Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrUnknown)); |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 415 | if (Inst.getNumArgOperands() == 0 && |
| 416 | Inst.getType() != Type::getVoidTy(Inst.getContext())) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 417 | Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrUnknown)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); } |
| 421 | |
| 422 | void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); } |
| 423 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 424 | /// Because vectors/aggregates are immutable and unaddressable, there's |
| 425 | /// nothing we can do to coax a value out of them, other than calling |
| 426 | /// Extract{Element,Value}. We can effectively treat them as pointers to |
| 427 | /// arbitrary memory locations we can store in and load from. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 428 | void visitExtractElementInst(ExtractElementInst &Inst) { |
| 429 | auto *Ptr = Inst.getVectorOperand(); |
| 430 | auto *Val = &Inst; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 431 | Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void visitInsertElementInst(InsertElementInst &Inst) { |
| 435 | auto *Vec = Inst.getOperand(0); |
| 436 | auto *Val = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 437 | Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone)); |
| 438 | Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void visitLandingPadInst(LandingPadInst &Inst) { |
| 442 | // Exceptions come from "nowhere", from our analysis' perspective. |
| 443 | // So we place the instruction its own group, noting that said group may |
| 444 | // alias externals |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 445 | Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrUnknown)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | void visitInsertValueInst(InsertValueInst &Inst) { |
| 449 | auto *Agg = Inst.getOperand(0); |
| 450 | auto *Val = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 451 | Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone)); |
| 452 | Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void visitExtractValueInst(ExtractValueInst &Inst) { |
| 456 | auto *Ptr = Inst.getAggregateOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 457 | Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void visitShuffleVectorInst(ShuffleVectorInst &Inst) { |
| 461 | auto *From1 = Inst.getOperand(0); |
| 462 | auto *From2 = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 463 | Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone)); |
| 464 | Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 465 | } |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 466 | |
| 467 | void visitConstantExpr(ConstantExpr *CE) { |
| 468 | switch (CE->getOpcode()) { |
| 469 | default: |
| 470 | llvm_unreachable("Unknown instruction type encountered!"); |
| 471 | // Build the switch statement using the Instruction.def file. |
| 472 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 473 | case Instruction::OPCODE: \ |
| 474 | visit##OPCODE(*(CLASS *)CE); \ |
| 475 | break; |
| 476 | #include "llvm/IR/Instruction.def" |
| 477 | } |
| 478 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 479 | }; |
| 480 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 481 | /// For a given instruction, we need to know which Value* to get the |
| 482 | /// users of in order to build our graph. In some cases (i.e. add), |
| 483 | /// we simply need the Instruction*. In other cases (i.e. store), |
| 484 | /// finding the users of the Instruction* is useless; we need to find |
| 485 | /// the users of the first operand. This handles determining which |
| 486 | /// value to follow for us. |
| 487 | /// |
| 488 | /// Note: we *need* to keep this in sync with GetEdgesVisitor. Add |
| 489 | /// something to GetEdgesVisitor, add it here -- remove something from |
| 490 | /// GetEdgesVisitor, remove it here. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 491 | class GetTargetValueVisitor |
| 492 | : public InstVisitor<GetTargetValueVisitor, Value *> { |
| 493 | public: |
| 494 | Value *visitInstruction(Instruction &Inst) { return &Inst; } |
| 495 | |
| 496 | Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); } |
| 497 | |
| 498 | Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 499 | return Inst.getPointerOperand(); |
| 500 | } |
| 501 | |
| 502 | Value *visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 503 | return Inst.getPointerOperand(); |
| 504 | } |
| 505 | |
| 506 | Value *visitInsertElementInst(InsertElementInst &Inst) { |
| 507 | return Inst.getOperand(0); |
| 508 | } |
| 509 | |
| 510 | Value *visitInsertValueInst(InsertValueInst &Inst) { |
| 511 | return Inst.getAggregateOperand(); |
| 512 | } |
| 513 | }; |
| 514 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 515 | /// Set building requires a weighted bidirectional graph. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 516 | template <typename EdgeTypeT> class WeightedBidirectionalGraph { |
| 517 | public: |
| 518 | typedef std::size_t Node; |
| 519 | |
| 520 | private: |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 521 | const static Node StartNode = Node(0); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 522 | |
| 523 | struct Edge { |
| 524 | EdgeTypeT Weight; |
| 525 | Node Other; |
| 526 | |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 527 | Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {} |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 528 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 529 | bool operator==(const Edge &E) const { |
| 530 | return Weight == E.Weight && Other == E.Other; |
| 531 | } |
| 532 | |
| 533 | bool operator!=(const Edge &E) const { return !operator==(E); } |
| 534 | }; |
| 535 | |
| 536 | struct NodeImpl { |
| 537 | std::vector<Edge> Edges; |
| 538 | }; |
| 539 | |
| 540 | std::vector<NodeImpl> NodeImpls; |
| 541 | |
| 542 | bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); } |
| 543 | |
| 544 | const NodeImpl &getNode(Node N) const { return NodeImpls[N]; } |
| 545 | NodeImpl &getNode(Node N) { return NodeImpls[N]; } |
| 546 | |
| 547 | public: |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 548 | /// \brief Iterator for edges. Because this graph is bidirected, we don't |
| 549 | /// allow modification of the edges using this iterator. Additionally, the |
| 550 | /// iterator becomes invalid if you add edges to or from the node you're |
| 551 | /// getting the edges of. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 552 | struct EdgeIterator : public std::iterator<std::forward_iterator_tag, |
| 553 | std::tuple<EdgeTypeT, Node *>> { |
| 554 | EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter) |
| 555 | : Current(Iter) {} |
| 556 | |
| 557 | EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {} |
| 558 | |
| 559 | EdgeIterator &operator++() { |
| 560 | ++Current; |
| 561 | return *this; |
| 562 | } |
| 563 | |
| 564 | EdgeIterator operator++(int) { |
| 565 | EdgeIterator Copy(Current); |
| 566 | operator++(); |
| 567 | return Copy; |
| 568 | } |
| 569 | |
| 570 | std::tuple<EdgeTypeT, Node> &operator*() { |
| 571 | Store = std::make_tuple(Current->Weight, Current->Other); |
| 572 | return Store; |
| 573 | } |
| 574 | |
| 575 | bool operator==(const EdgeIterator &Other) const { |
| 576 | return Current == Other.Current; |
| 577 | } |
| 578 | |
| 579 | bool operator!=(const EdgeIterator &Other) const { |
| 580 | return !operator==(Other); |
| 581 | } |
| 582 | |
| 583 | private: |
| 584 | typename std::vector<Edge>::const_iterator Current; |
| 585 | std::tuple<EdgeTypeT, Node> Store; |
| 586 | }; |
| 587 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 588 | /// Wrapper for EdgeIterator with begin()/end() calls. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 589 | struct EdgeIterable { |
| 590 | EdgeIterable(const std::vector<Edge> &Edges) |
| 591 | : BeginIter(Edges.begin()), EndIter(Edges.end()) {} |
| 592 | |
| 593 | EdgeIterator begin() { return EdgeIterator(BeginIter); } |
| 594 | |
| 595 | EdgeIterator end() { return EdgeIterator(EndIter); } |
| 596 | |
| 597 | private: |
| 598 | typename std::vector<Edge>::const_iterator BeginIter; |
| 599 | typename std::vector<Edge>::const_iterator EndIter; |
| 600 | }; |
| 601 | |
| 602 | // ----- Actual graph-related things ----- // |
| 603 | |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 604 | WeightedBidirectionalGraph() {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 605 | |
| 606 | WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other) |
| 607 | : NodeImpls(std::move(Other.NodeImpls)) {} |
| 608 | |
| 609 | WeightedBidirectionalGraph<EdgeTypeT> & |
| 610 | operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) { |
| 611 | NodeImpls = std::move(Other.NodeImpls); |
| 612 | return *this; |
| 613 | } |
| 614 | |
| 615 | Node addNode() { |
| 616 | auto Index = NodeImpls.size(); |
| 617 | auto NewNode = Node(Index); |
| 618 | NodeImpls.push_back(NodeImpl()); |
| 619 | return NewNode; |
| 620 | } |
| 621 | |
| 622 | void addEdge(Node From, Node To, const EdgeTypeT &Weight, |
| 623 | const EdgeTypeT &ReverseWeight) { |
| 624 | assert(inbounds(From)); |
| 625 | assert(inbounds(To)); |
| 626 | auto &FromNode = getNode(From); |
| 627 | auto &ToNode = getNode(To); |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 628 | FromNode.Edges.push_back(Edge(Weight, To)); |
| 629 | ToNode.Edges.push_back(Edge(ReverseWeight, From)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 630 | } |
| 631 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 632 | iterator_range<EdgeIterator> edgesFor(const Node &N) const { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 633 | const auto &Node = getNode(N); |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 634 | return make_range(EdgeIterator(Node.Edges.begin()), |
| 635 | EdgeIterator(Node.Edges.end())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | bool empty() const { return NodeImpls.empty(); } |
| 639 | std::size_t size() const { return NodeImpls.size(); } |
| 640 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 641 | /// Gets an arbitrary node in the graph as a starting point for traversal. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 642 | Node getEntryNode() { |
| 643 | assert(inbounds(StartNode)); |
| 644 | return StartNode; |
| 645 | } |
| 646 | }; |
| 647 | |
| 648 | typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT; |
| 649 | typedef DenseMap<Value *, GraphT::Node> NodeMapT; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 650 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 651 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 652 | //===----------------------------------------------------------------------===// |
| 653 | // Function declarations that require types defined in the namespace above |
| 654 | //===----------------------------------------------------------------------===// |
| 655 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 656 | /// Given a StratifiedAttrs, returns true if it marks the corresponding values |
| 657 | /// as globals or arguments |
| 658 | static bool isGlobalOrArgAttr(StratifiedAttrs Attr); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 659 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 660 | /// Given a StratifiedAttrs, returns true if the corresponding values come from |
| 661 | /// an unknown source (such as opaque memory or an integer cast) |
| 662 | static bool isUnknownAttr(StratifiedAttrs Attr); |
| 663 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 664 | /// Given an argument number, returns the appropriate StratifiedAttr to set. |
| 665 | static StratifiedAttr argNumberToAttr(unsigned ArgNum); |
| 666 | |
| 667 | /// Given a Value, potentially return which StratifiedAttr it maps to. |
| 668 | static Optional<StratifiedAttr> valueToAttr(Value *Val); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 669 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 670 | /// Gets the inverse of a given EdgeType. |
| 671 | static EdgeType flipWeight(EdgeType Initial); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 672 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 673 | /// Gets edges of the given Instruction*, writing them to the SmallVector*. |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 674 | static void argsToEdges(CFLAAResult &, Instruction *, SmallVectorImpl<Edge> &, |
| 675 | const TargetLibraryInfo &); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 676 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 677 | /// Gets edges of the given ConstantExpr*, writing them to the SmallVector*. |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 678 | static void argsToEdges(CFLAAResult &, ConstantExpr *, SmallVectorImpl<Edge> &, |
| 679 | const TargetLibraryInfo &); |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 680 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 681 | /// Gets the "Level" that one should travel in StratifiedSets |
| 682 | /// given an EdgeType. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 683 | static Level directionOfEdgeType(EdgeType); |
| 684 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 685 | /// Builds the graph needed for constructing the StratifiedSets for the |
| 686 | /// given function |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 687 | static void buildGraphFrom(CFLAAResult &, Function *, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 688 | SmallVectorImpl<Value *> &, NodeMapT &, GraphT &, |
| 689 | const TargetLibraryInfo &); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 690 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 691 | /// Gets the edges of a ConstantExpr as if it was an Instruction. This function |
| 692 | /// also acts on any nested ConstantExprs, adding the edges of those to the |
| 693 | /// given SmallVector as well. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 694 | static void constexprToEdges(CFLAAResult &, ConstantExpr &, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 695 | SmallVectorImpl<Edge> &, |
| 696 | const TargetLibraryInfo &); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 697 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 698 | /// Given an Instruction, this will add it to the graph, along with any |
| 699 | /// Instructions that are potentially only available from said Instruction |
| 700 | /// For example, given the following line: |
| 701 | /// %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2 |
| 702 | /// addInstructionToGraph would add both the `load` and `getelementptr` |
| 703 | /// instructions to the graph appropriately. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 704 | static void addInstructionToGraph(CFLAAResult &, Instruction &, |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 705 | SmallVectorImpl<Value *> &, NodeMapT &, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 706 | GraphT &, const TargetLibraryInfo &); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 707 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 708 | /// Determines whether it would be pointless to add the given Value to our sets. |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 709 | static bool canSkipAddingToSets(Value *Val); |
| 710 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 711 | static Optional<Function *> parentFunctionOfValue(Value *Val) { |
| 712 | if (auto *Inst = dyn_cast<Instruction>(Val)) { |
| 713 | auto *Bb = Inst->getParent(); |
| 714 | return Bb->getParent(); |
| 715 | } |
| 716 | |
| 717 | if (auto *Arg = dyn_cast<Argument>(Val)) |
| 718 | return Arg->getParent(); |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 719 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | template <typename Inst> |
| 723 | static bool getPossibleTargets(Inst *Call, |
| 724 | SmallVectorImpl<Function *> &Output) { |
| 725 | if (auto *Fn = Call->getCalledFunction()) { |
| 726 | Output.push_back(Fn); |
| 727 | return true; |
| 728 | } |
| 729 | |
| 730 | // TODO: If the call is indirect, we might be able to enumerate all potential |
| 731 | // targets of the call and return them, rather than just failing. |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | static Optional<Value *> getTargetValue(Instruction *Inst) { |
| 736 | GetTargetValueVisitor V; |
| 737 | return V.visit(Inst); |
| 738 | } |
| 739 | |
| 740 | static bool hasUsefulEdges(Instruction *Inst) { |
| 741 | bool IsNonInvokeTerminator = |
| 742 | isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst); |
| 743 | return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator; |
| 744 | } |
| 745 | |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 746 | static bool hasUsefulEdges(ConstantExpr *CE) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 747 | // ConstantExpr doesn't have terminators, invokes, or fences, so only needs |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 748 | // to check for compares. |
| 749 | return CE->getOpcode() != Instruction::ICmp && |
| 750 | CE->getOpcode() != Instruction::FCmp; |
| 751 | } |
| 752 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 753 | static bool isGlobalOrArgAttr(StratifiedAttrs Attr) { |
| 754 | return Attr.reset(AttrEscapedIndex).reset(AttrUnknownIndex).any(); |
| 755 | } |
| 756 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 757 | static bool isUnknownAttr(StratifiedAttrs Attr) { |
| 758 | return Attr.test(AttrUnknownIndex); |
| 759 | } |
| 760 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 761 | static Optional<StratifiedAttr> valueToAttr(Value *Val) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 762 | if (isa<GlobalValue>(Val)) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 763 | return AttrGlobal; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 764 | |
| 765 | if (auto *Arg = dyn_cast<Argument>(Val)) |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 766 | // Only pointer arguments should have the argument attribute, |
| 767 | // because things can't escape through scalars without us seeing a |
| 768 | // cast, and thus, interaction with them doesn't matter. |
| 769 | if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy()) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 770 | return argNumberToAttr(Arg->getArgNo()); |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 771 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 772 | } |
| 773 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 774 | static StratifiedAttr argNumberToAttr(unsigned ArgNum) { |
George Burgess IV | 3c898c2 | 2015-01-21 16:37:21 +0000 | [diff] [blame] | 775 | if (ArgNum >= AttrMaxNumArgs) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 776 | return AttrUnknown; |
| 777 | return 1 << (ArgNum + AttrFirstArgIndex); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static EdgeType flipWeight(EdgeType Initial) { |
| 781 | switch (Initial) { |
| 782 | case EdgeType::Assign: |
| 783 | return EdgeType::Assign; |
| 784 | case EdgeType::Dereference: |
| 785 | return EdgeType::Reference; |
| 786 | case EdgeType::Reference: |
| 787 | return EdgeType::Dereference; |
| 788 | } |
| 789 | llvm_unreachable("Incomplete coverage of EdgeType enum"); |
| 790 | } |
| 791 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 792 | static void argsToEdges(CFLAAResult &Analysis, Instruction *Inst, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 793 | SmallVectorImpl<Edge> &Output, |
| 794 | const TargetLibraryInfo &TLI) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 795 | assert(hasUsefulEdges(Inst) && |
| 796 | "Expected instructions to have 'useful' edges"); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 797 | GetEdgesVisitor v(Analysis, Output, TLI); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 798 | v.visit(Inst); |
| 799 | } |
| 800 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 801 | static void argsToEdges(CFLAAResult &Analysis, ConstantExpr *CE, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 802 | SmallVectorImpl<Edge> &Output, |
| 803 | const TargetLibraryInfo &TLI) { |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 804 | assert(hasUsefulEdges(CE) && "Expected constant expr to have 'useful' edges"); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 805 | GetEdgesVisitor v(Analysis, Output, TLI); |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 806 | v.visitConstantExpr(CE); |
| 807 | } |
| 808 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 809 | static Level directionOfEdgeType(EdgeType Weight) { |
| 810 | switch (Weight) { |
| 811 | case EdgeType::Reference: |
| 812 | return Level::Above; |
| 813 | case EdgeType::Dereference: |
| 814 | return Level::Below; |
| 815 | case EdgeType::Assign: |
| 816 | return Level::Same; |
| 817 | } |
| 818 | llvm_unreachable("Incomplete switch coverage"); |
| 819 | } |
| 820 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 821 | static void constexprToEdges(CFLAAResult &Analysis, |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 822 | ConstantExpr &CExprToCollapse, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 823 | SmallVectorImpl<Edge> &Results, |
| 824 | const TargetLibraryInfo &TLI) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 825 | SmallVector<ConstantExpr *, 4> Worklist; |
| 826 | Worklist.push_back(&CExprToCollapse); |
| 827 | |
| 828 | SmallVector<Edge, 8> ConstexprEdges; |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 829 | SmallPtrSet<ConstantExpr *, 4> Visited; |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 830 | while (!Worklist.empty()) { |
| 831 | auto *CExpr = Worklist.pop_back_val(); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 832 | |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 833 | if (!hasUsefulEdges(CExpr)) |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 834 | continue; |
| 835 | |
| 836 | ConstexprEdges.clear(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 837 | argsToEdges(Analysis, CExpr, ConstexprEdges, TLI); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 838 | for (auto &Edge : ConstexprEdges) { |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 839 | if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From)) |
| 840 | if (Visited.insert(Nested).second) |
| 841 | Worklist.push_back(Nested); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 842 | |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 843 | if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To)) |
| 844 | if (Visited.insert(Nested).second) |
| 845 | Worklist.push_back(Nested); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | Results.append(ConstexprEdges.begin(), ConstexprEdges.end()); |
| 849 | } |
| 850 | } |
| 851 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 852 | static void addInstructionToGraph(CFLAAResult &Analysis, Instruction &Inst, |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 853 | SmallVectorImpl<Value *> &ReturnedValues, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 854 | NodeMapT &Map, GraphT &Graph, |
| 855 | const TargetLibraryInfo &TLI) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 856 | const auto findOrInsertNode = [&Map, &Graph](Value *Val) { |
| 857 | auto Pair = Map.insert(std::make_pair(Val, GraphT::Node())); |
| 858 | auto &Iter = Pair.first; |
| 859 | if (Pair.second) { |
| 860 | auto NewNode = Graph.addNode(); |
| 861 | Iter->second = NewNode; |
| 862 | } |
| 863 | return Iter->second; |
| 864 | }; |
| 865 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 866 | // We don't want the edges of most "return" instructions, but we *do* want |
| 867 | // to know what can be returned. |
| 868 | if (isa<ReturnInst>(&Inst)) |
| 869 | ReturnedValues.push_back(&Inst); |
| 870 | |
| 871 | if (!hasUsefulEdges(&Inst)) |
| 872 | return; |
| 873 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 874 | SmallVector<Edge, 8> Edges; |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 875 | argsToEdges(Analysis, &Inst, Edges, TLI); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 876 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 877 | // In the case of an unused alloca (or similar), edges may be empty. Note |
| 878 | // that it exists so we can potentially answer NoAlias. |
| 879 | if (Edges.empty()) { |
| 880 | auto MaybeVal = getTargetValue(&Inst); |
| 881 | assert(MaybeVal.hasValue()); |
| 882 | auto *Target = *MaybeVal; |
| 883 | findOrInsertNode(Target); |
| 884 | return; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 885 | } |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 886 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 887 | auto addEdgeToGraph = [&](const Edge &E) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 888 | auto To = findOrInsertNode(E.To); |
| 889 | auto From = findOrInsertNode(E.From); |
| 890 | auto FlippedWeight = flipWeight(E.Weight); |
| 891 | auto Attrs = E.AdditionalAttrs; |
| 892 | Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs), |
| 893 | std::make_pair(FlippedWeight, Attrs)); |
| 894 | }; |
| 895 | |
| 896 | SmallVector<ConstantExpr *, 4> ConstantExprs; |
| 897 | for (const Edge &E : Edges) { |
| 898 | addEdgeToGraph(E); |
| 899 | if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To)) |
| 900 | ConstantExprs.push_back(Constexpr); |
| 901 | if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From)) |
| 902 | ConstantExprs.push_back(Constexpr); |
| 903 | } |
| 904 | |
| 905 | for (ConstantExpr *CE : ConstantExprs) { |
| 906 | Edges.clear(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 907 | constexprToEdges(Analysis, *CE, Edges, TLI); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 908 | std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph); |
| 909 | } |
| 910 | } |
| 911 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 912 | static void buildGraphFrom(CFLAAResult &Analysis, Function *Fn, |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 913 | SmallVectorImpl<Value *> &ReturnedValues, |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 914 | NodeMapT &Map, GraphT &Graph, |
| 915 | const TargetLibraryInfo &TLI) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 916 | // (N.B. We may remove graph construction entirely, because it doesn't really |
| 917 | // buy us much.) |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 918 | for (auto &Bb : Fn->getBasicBlockList()) |
| 919 | for (auto &Inst : Bb.getInstList()) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 920 | addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph, TLI); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static bool canSkipAddingToSets(Value *Val) { |
| 924 | // Constants can share instances, which may falsely unify multiple |
| 925 | // sets, e.g. in |
| 926 | // store i32* null, i32** %ptr1 |
| 927 | // store i32* null, i32** %ptr2 |
| 928 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 929 | // we should filter out the (potentially shared) instance to |
| 930 | // i32* null. |
| 931 | if (isa<Constant>(Val)) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 932 | // TODO: Because all of these things are constant, we can determine whether |
| 933 | // the data is *actually* mutable at graph building time. This will probably |
| 934 | // come for free/cheap with offset awareness. |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 935 | bool CanStoreMutableData = isa<GlobalValue>(Val) || |
| 936 | isa<ConstantExpr>(Val) || |
| 937 | isa<ConstantAggregate>(Val); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 938 | return !CanStoreMutableData; |
| 939 | } |
| 940 | |
| 941 | return false; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 944 | // Builds the graph + StratifiedSets for a function. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 945 | CFLAAResult::FunctionInfo CFLAAResult::buildSetsFrom(Function *Fn) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 946 | NodeMapT Map; |
| 947 | GraphT Graph; |
| 948 | SmallVector<Value *, 4> ReturnedValues; |
| 949 | |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 950 | buildGraphFrom(*this, Fn, ReturnedValues, Map, Graph, TLI); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 951 | |
| 952 | DenseMap<GraphT::Node, Value *> NodeValueMap; |
Mehdi Amini | c04fc7a | 2016-03-22 07:20:00 +0000 | [diff] [blame] | 953 | NodeValueMap.reserve(Map.size()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 954 | for (const auto &Pair : Map) |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 955 | NodeValueMap.insert(std::make_pair(Pair.second, Pair.first)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 956 | |
| 957 | const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) { |
| 958 | auto ValIter = NodeValueMap.find(Node); |
| 959 | assert(ValIter != NodeValueMap.end()); |
| 960 | return ValIter->second; |
| 961 | }; |
| 962 | |
| 963 | StratifiedSetsBuilder<Value *> Builder; |
| 964 | |
| 965 | SmallVector<GraphT::Node, 16> Worklist; |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 966 | SmallPtrSet<Value *, 16> Globals; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 967 | for (auto &Pair : Map) { |
| 968 | Worklist.clear(); |
| 969 | |
| 970 | auto *Value = Pair.first; |
| 971 | Builder.add(Value); |
| 972 | auto InitialNode = Pair.second; |
| 973 | Worklist.push_back(InitialNode); |
| 974 | while (!Worklist.empty()) { |
| 975 | auto Node = Worklist.pop_back_val(); |
| 976 | auto *CurValue = findValueOrDie(Node); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 977 | if (canSkipAddingToSets(CurValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 978 | continue; |
| 979 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 980 | if (isa<GlobalValue>(CurValue)) |
| 981 | Globals.insert(CurValue); |
George Burgess IV | 7e5404c | 2016-04-05 21:40:45 +0000 | [diff] [blame] | 982 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 983 | for (const auto &EdgeTuple : Graph.edgesFor(Node)) { |
| 984 | auto Weight = std::get<0>(EdgeTuple); |
| 985 | auto Label = Weight.first; |
| 986 | auto &OtherNode = std::get<1>(EdgeTuple); |
| 987 | auto *OtherValue = findValueOrDie(OtherNode); |
| 988 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 989 | if (canSkipAddingToSets(OtherValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 990 | continue; |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 991 | if (isa<GlobalValue>(OtherValue)) |
| 992 | Globals.insert(OtherValue); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 993 | |
| 994 | bool Added; |
| 995 | switch (directionOfEdgeType(Label)) { |
| 996 | case Level::Above: |
| 997 | Added = Builder.addAbove(CurValue, OtherValue); |
| 998 | break; |
| 999 | case Level::Below: |
| 1000 | Added = Builder.addBelow(CurValue, OtherValue); |
| 1001 | break; |
| 1002 | case Level::Same: |
| 1003 | Added = Builder.addWith(CurValue, OtherValue); |
| 1004 | break; |
| 1005 | } |
| 1006 | |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 1007 | auto Aliasing = Weight.second; |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 1008 | Builder.noteAttributes(CurValue, Aliasing); |
| 1009 | Builder.noteAttributes(OtherValue, Aliasing); |
| 1010 | |
| 1011 | if (Added) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1012 | Worklist.push_back(OtherNode); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 1017 | // Special handling for globals and arguments |
| 1018 | auto ProcessGlobalOrArgValue = [&Builder](Value &Val) { |
| 1019 | Builder.add(&Val); |
| 1020 | auto Attr = valueToAttr(&Val); |
| 1021 | if (Attr.hasValue()) { |
| 1022 | Builder.noteAttributes(&Val, *Attr); |
| 1023 | // TODO: do we need to filter out non-pointer values here? |
| 1024 | Builder.addAttributesBelow(&Val, AttrUnknown); |
| 1025 | } |
| 1026 | }; |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 1027 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 1028 | for (auto &Arg : Fn->args()) |
| 1029 | ProcessGlobalOrArgValue(Arg); |
| 1030 | for (auto *Global : Globals) |
| 1031 | ProcessGlobalOrArgValue(*Global); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1032 | |
Hal Finkel | 85f2692 | 2014-09-03 00:06:47 +0000 | [diff] [blame] | 1033 | return FunctionInfo(Builder.build(), std::move(ReturnedValues)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1036 | void CFLAAResult::scan(Function *Fn) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 1037 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1038 | (void)InsertPair; |
| 1039 | assert(InsertPair.second && |
| 1040 | "Trying to scan a function that has already been cached"); |
| 1041 | |
George Burgess IV | 6edb891 | 2016-05-02 18:09:19 +0000 | [diff] [blame] | 1042 | // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call |
| 1043 | // may get evaluated after operator[], potentially triggering a DenseMap |
| 1044 | // resize and invalidating the reference returned by operator[] |
| 1045 | auto FunInfo = buildSetsFrom(Fn); |
| 1046 | Cache[Fn] = std::move(FunInfo); |
| 1047 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1048 | Handles.push_front(FunctionHandle(Fn, this)); |
| 1049 | } |
| 1050 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1051 | void CFLAAResult::evict(Function *Fn) { Cache.erase(Fn); } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 1052 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 1053 | /// Ensures that the given function is available in the cache, and returns the |
| 1054 | /// entry. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1055 | const Optional<CFLAAResult::FunctionInfo> & |
| 1056 | CFLAAResult::ensureCached(Function *Fn) { |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 1057 | auto Iter = Cache.find(Fn); |
| 1058 | if (Iter == Cache.end()) { |
| 1059 | scan(Fn); |
| 1060 | Iter = Cache.find(Fn); |
| 1061 | assert(Iter != Cache.end()); |
| 1062 | assert(Iter->second.hasValue()); |
| 1063 | } |
| 1064 | return Iter->second; |
| 1065 | } |
| 1066 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1067 | AliasResult CFLAAResult::query(const MemoryLocation &LocA, |
| 1068 | const MemoryLocation &LocB) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1069 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 1070 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 1071 | |
| 1072 | Function *Fn = nullptr; |
| 1073 | auto MaybeFnA = parentFunctionOfValue(ValA); |
| 1074 | auto MaybeFnB = parentFunctionOfValue(ValB); |
| 1075 | if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 1076 | // The only times this is known to happen are when globals + InlineAsm are |
| 1077 | // involved |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1078 | DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n"); |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1079 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | if (MaybeFnA.hasValue()) { |
| 1083 | Fn = *MaybeFnA; |
| 1084 | assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) && |
| 1085 | "Interprocedural queries not supported"); |
| 1086 | } else { |
| 1087 | Fn = *MaybeFnB; |
| 1088 | } |
| 1089 | |
| 1090 | assert(Fn != nullptr); |
| 1091 | auto &MaybeInfo = ensureCached(Fn); |
| 1092 | assert(MaybeInfo.hasValue()); |
| 1093 | |
| 1094 | auto &Sets = MaybeInfo->Sets; |
| 1095 | auto MaybeA = Sets.find(ValA); |
| 1096 | if (!MaybeA.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1097 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1098 | |
| 1099 | auto MaybeB = Sets.find(ValB); |
| 1100 | if (!MaybeB.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1101 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1102 | |
| 1103 | auto SetA = *MaybeA; |
| 1104 | auto SetB = *MaybeB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1105 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 1106 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1107 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1108 | // If both values are local (meaning the corresponding set has attribute |
| 1109 | // AttrNone or AttrEscaped), then we know that CFLAA fully models them: they |
| 1110 | // may-alias each other if and only if they are in the same set |
| 1111 | // If at least one value is non-local (meaning it either is global/argument or |
| 1112 | // it comes from unknown sources like integer cast), the situation becomes a |
| 1113 | // bit more interesting. We follow three general rules described below: |
| 1114 | // - Non-local values may alias each other |
| 1115 | // - AttrNone values do not alias any non-local values |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 1116 | // - AttrEscaped do not alias globals/arguments, but they may alias |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1117 | // AttrUnknown values |
| 1118 | if (SetA.Index == SetB.Index) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1119 | return MayAlias; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1120 | if (AttrsA.none() || AttrsB.none()) |
| 1121 | return NoAlias; |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame^] | 1122 | if (isUnknownAttr(AttrsA) || isUnknownAttr(AttrsB)) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 1123 | return MayAlias; |
| 1124 | if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB)) |
| 1125 | return MayAlias; |
| 1126 | return NoAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1127 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 1128 | |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 1129 | char CFLAA::PassID; |
| 1130 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1131 | CFLAAResult CFLAA::run(Function &F, AnalysisManager<Function> &AM) { |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 1132 | return CFLAAResult(AM.getResult<TargetLibraryAnalysis>(F)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1135 | char CFLAAWrapperPass::ID = 0; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 1136 | INITIALIZE_PASS(CFLAAWrapperPass, "cfl-aa", "CFL-Based Alias Analysis", false, |
| 1137 | true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1138 | |
| 1139 | ImmutablePass *llvm::createCFLAAWrapperPass() { return new CFLAAWrapperPass(); } |
| 1140 | |
| 1141 | CFLAAWrapperPass::CFLAAWrapperPass() : ImmutablePass(ID) { |
| 1142 | initializeCFLAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 1143 | } |
| 1144 | |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 1145 | void CFLAAWrapperPass::initializePass() { |
| 1146 | auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>(); |
| 1147 | Result.reset(new CFLAAResult(TLIWP.getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | void CFLAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1151 | AU.setPreservesAll(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 1152 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 1153 | } |