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