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