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