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