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" |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 40 | #include "StratifiedSets.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/DenseMap.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/None.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/Optional.h" |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 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/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; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 58 | using namespace llvm::cflaa; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 59 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 60 | #define DEBUG_TYPE "cfl-steens-aa" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 61 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 62 | CFLSteensAAResult::CFLSteensAAResult(const TargetLibraryInfo &TLI) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 63 | : AAResultBase(), TLI(TLI) {} |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 64 | CFLSteensAAResult::CFLSteensAAResult(CFLSteensAAResult &&Arg) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 65 | : AAResultBase(std::move(Arg)), TLI(Arg.TLI) {} |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 66 | CFLSteensAAResult::~CFLSteensAAResult() {} |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 67 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 68 | /// 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] | 69 | class CFLSteensAAResult::FunctionInfo { |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 70 | StratifiedSets<Value *> Sets; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 71 | AliasSummary Summary; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 72 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 73 | public: |
| 74 | FunctionInfo(Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
| 75 | StratifiedSets<Value *> S); |
| 76 | |
| 77 | const StratifiedSets<Value *> &getStratifiedSets() const { return Sets; } |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 78 | const AliasSummary &getAliasSummary() const { return Summary; } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 81 | /// Try to go from a Value* to a Function*. Never returns nullptr. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 82 | static Optional<Function *> parentFunctionOfValue(Value *); |
| 83 | |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 84 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 85 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 86 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 87 | namespace { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 88 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 89 | /// StratifiedSets call for knowledge of "direction", so this is how we |
| 90 | /// represent that locally. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 91 | enum class Level { Same, Above, Below }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 92 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 93 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
| 95 | // Function declarations that require types defined in the namespace above |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 98 | /// Gets the "Level" that one should travel in StratifiedSets |
| 99 | /// given an EdgeType. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 100 | static Level directionOfEdgeType(EdgeType); |
| 101 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 102 | /// 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] | 103 | static bool canSkipAddingToSets(Value *Val); |
| 104 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 105 | static Optional<Function *> parentFunctionOfValue(Value *Val) { |
| 106 | if (auto *Inst = dyn_cast<Instruction>(Val)) { |
| 107 | auto *Bb = Inst->getParent(); |
| 108 | return Bb->getParent(); |
| 109 | } |
| 110 | |
| 111 | if (auto *Arg = dyn_cast<Argument>(Val)) |
| 112 | return Arg->getParent(); |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 113 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 116 | static Level directionOfEdgeType(EdgeType Weight) { |
| 117 | switch (Weight) { |
| 118 | case EdgeType::Reference: |
| 119 | return Level::Above; |
| 120 | case EdgeType::Dereference: |
| 121 | return Level::Below; |
| 122 | case EdgeType::Assign: |
| 123 | return Level::Same; |
| 124 | } |
| 125 | llvm_unreachable("Incomplete switch coverage"); |
| 126 | } |
| 127 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 128 | static bool canSkipAddingToSets(Value *Val) { |
| 129 | // Constants can share instances, which may falsely unify multiple |
| 130 | // sets, e.g. in |
| 131 | // store i32* null, i32** %ptr1 |
| 132 | // store i32* null, i32** %ptr2 |
| 133 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 134 | // we should filter out the (potentially shared) instance to |
| 135 | // i32* null. |
| 136 | if (isa<Constant>(Val)) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 137 | // TODO: Because all of these things are constant, we can determine whether |
| 138 | // the data is *actually* mutable at graph building time. This will probably |
| 139 | // come for free/cheap with offset awareness. |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 140 | bool CanStoreMutableData = isa<GlobalValue>(Val) || |
| 141 | isa<ConstantExpr>(Val) || |
| 142 | isa<ConstantAggregate>(Val); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 143 | return !CanStoreMutableData; |
| 144 | } |
| 145 | |
| 146 | return false; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 149 | CFLSteensAAResult::FunctionInfo::FunctionInfo( |
| 150 | Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
| 151 | StratifiedSets<Value *> S) |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 152 | : Sets(std::move(S)) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 153 | // Historically, an arbitrary upper-bound of 50 args was selected. We may want |
| 154 | // to remove this if it doesn't really matter in practice. |
| 155 | if (Fn.arg_size() > MaxSupportedArgsInSummary) |
| 156 | return; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 157 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 158 | DenseMap<StratifiedIndex, InterfaceValue> InterfaceMap; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 159 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 160 | // Our intention here is to record all InterfaceValues that share the same |
| 161 | // StratifiedIndex in RetParamRelations. For each valid InterfaceValue, we |
| 162 | // have its StratifiedIndex scanned here and check if the index is presented |
| 163 | // in InterfaceMap: if it is not, we add the correspondence to the map; |
| 164 | // otherwise, an aliasing relation is found and we add it to |
| 165 | // RetParamRelations. |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 166 | |
George Burgess IV | d14d05a | 2016-06-23 20:59:13 +0000 | [diff] [blame] | 167 | auto AddToRetParamRelations = [&](unsigned InterfaceIndex, |
| 168 | StratifiedIndex SetIndex) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 169 | unsigned Level = 0; |
| 170 | while (true) { |
| 171 | InterfaceValue CurrValue{InterfaceIndex, Level}; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 172 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 173 | auto Itr = InterfaceMap.find(SetIndex); |
| 174 | if (Itr != InterfaceMap.end()) { |
| 175 | if (CurrValue != Itr->second) |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 176 | Summary.RetParamRelations.push_back( |
| 177 | ExternalRelation{CurrValue, Itr->second}); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 178 | break; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 179 | } |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 180 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 181 | auto &Link = Sets.getLink(SetIndex); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 182 | InterfaceMap.insert(std::make_pair(SetIndex, CurrValue)); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 183 | auto ExternalAttrs = getExternallyVisibleAttrs(Link.Attrs); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 184 | if (ExternalAttrs.any()) |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 185 | Summary.RetParamAttributes.push_back( |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 186 | ExternalAttribute{CurrValue, ExternalAttrs}); |
| 187 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 188 | if (!Link.hasBelow()) |
| 189 | break; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 190 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 191 | ++Level; |
| 192 | SetIndex = Link.Below; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 193 | } |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | // Populate RetParamRelations for return values |
| 197 | for (auto *RetVal : RetVals) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 198 | assert(RetVal != nullptr); |
| 199 | assert(RetVal->getType()->isPointerTy()); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 200 | auto RetInfo = Sets.find(RetVal); |
| 201 | if (RetInfo.hasValue()) |
| 202 | AddToRetParamRelations(0, RetInfo->Index); |
| 203 | } |
| 204 | |
| 205 | // Populate RetParamRelations for parameters |
| 206 | unsigned I = 0; |
| 207 | for (auto &Param : Fn.args()) { |
| 208 | if (Param.getType()->isPointerTy()) { |
| 209 | auto ParamInfo = Sets.find(&Param); |
| 210 | if (ParamInfo.hasValue()) |
| 211 | AddToRetParamRelations(I + 1, ParamInfo->Index); |
| 212 | } |
| 213 | ++I; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 217 | // Builds the graph + StratifiedSets for a function. |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 218 | CFLSteensAAResult::FunctionInfo CFLSteensAAResult::buildSetsFrom(Function *Fn) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 219 | CFLGraphBuilder<CFLSteensAAResult> GraphBuilder(*this, TLI, *Fn); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 220 | StratifiedSetsBuilder<Value *> SetBuilder; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 221 | |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 222 | auto &Graph = GraphBuilder.getCFLGraph(); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 223 | SmallVector<Value *, 16> Worklist; |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 224 | for (auto Node : Graph.nodes()) |
| 225 | Worklist.push_back(Node); |
| 226 | |
| 227 | while (!Worklist.empty()) { |
| 228 | auto *CurValue = Worklist.pop_back_val(); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 229 | SetBuilder.add(CurValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 230 | if (canSkipAddingToSets(CurValue)) |
| 231 | continue; |
| 232 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 233 | auto Attr = Graph.attrFor(CurValue); |
| 234 | SetBuilder.noteAttributes(CurValue, Attr); |
| 235 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 236 | for (const auto &Edge : Graph.edgesFor(CurValue)) { |
| 237 | auto Label = Edge.Type; |
| 238 | auto *OtherValue = Edge.Other; |
| 239 | |
| 240 | if (canSkipAddingToSets(OtherValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 241 | continue; |
| 242 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 243 | bool Added; |
| 244 | switch (directionOfEdgeType(Label)) { |
| 245 | case Level::Above: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 246 | Added = SetBuilder.addAbove(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 247 | break; |
| 248 | case Level::Below: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 249 | Added = SetBuilder.addBelow(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 250 | break; |
| 251 | case Level::Same: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 252 | Added = SetBuilder.addWith(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 253 | break; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 254 | } |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 255 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 256 | if (Added) |
| 257 | Worklist.push_back(OtherValue); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 261 | // Special handling for interprocedural aliases |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 262 | for (auto &Edge : GraphBuilder.getInstantiatedRelations()) { |
George Burgess IV | fe1397b | 2016-06-23 19:16:04 +0000 | [diff] [blame] | 263 | auto FromVal = Edge.From.Val; |
| 264 | auto ToVal = Edge.To.Val; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 265 | SetBuilder.add(FromVal); |
| 266 | SetBuilder.add(ToVal); |
| 267 | SetBuilder.addBelowWith(FromVal, Edge.From.DerefLevel, ToVal, |
| 268 | Edge.To.DerefLevel); |
| 269 | } |
| 270 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 271 | // Special handling for interprocedural attributes |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 272 | for (auto &IPAttr : GraphBuilder.getInstantiatedAttrs()) { |
| 273 | auto Val = IPAttr.IValue.Val; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 274 | SetBuilder.add(Val); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 275 | SetBuilder.addAttributesBelow(Val, IPAttr.IValue.DerefLevel, IPAttr.Attr); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 276 | } |
| 277 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 278 | return FunctionInfo(*Fn, GraphBuilder.getReturnValues(), SetBuilder.build()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 279 | } |
| 280 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 281 | void CFLSteensAAResult::scan(Function *Fn) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 282 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 283 | (void)InsertPair; |
| 284 | assert(InsertPair.second && |
| 285 | "Trying to scan a function that has already been cached"); |
| 286 | |
George Burgess IV | 6edb891 | 2016-05-02 18:09:19 +0000 | [diff] [blame] | 287 | // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call |
| 288 | // may get evaluated after operator[], potentially triggering a DenseMap |
| 289 | // resize and invalidating the reference returned by operator[] |
| 290 | auto FunInfo = buildSetsFrom(Fn); |
| 291 | Cache[Fn] = std::move(FunInfo); |
| 292 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 293 | Handles.push_front(FunctionHandle(Fn, this)); |
| 294 | } |
| 295 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 296 | void CFLSteensAAResult::evict(Function *Fn) { Cache.erase(Fn); } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 297 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 298 | /// Ensures that the given function is available in the cache, and returns the |
| 299 | /// entry. |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 300 | const Optional<CFLSteensAAResult::FunctionInfo> & |
| 301 | CFLSteensAAResult::ensureCached(Function *Fn) { |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 302 | auto Iter = Cache.find(Fn); |
| 303 | if (Iter == Cache.end()) { |
| 304 | scan(Fn); |
| 305 | Iter = Cache.find(Fn); |
| 306 | assert(Iter != Cache.end()); |
| 307 | assert(Iter->second.hasValue()); |
| 308 | } |
| 309 | return Iter->second; |
| 310 | } |
| 311 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 312 | const AliasSummary *CFLSteensAAResult::getAliasSummary(Function &Fn) { |
| 313 | auto &FunInfo = ensureCached(&Fn); |
| 314 | if (FunInfo.hasValue()) |
| 315 | return &FunInfo->getAliasSummary(); |
| 316 | else |
| 317 | return nullptr; |
| 318 | } |
| 319 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 320 | AliasResult CFLSteensAAResult::query(const MemoryLocation &LocA, |
| 321 | const MemoryLocation &LocB) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 322 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 323 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 324 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 325 | if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy()) |
| 326 | return NoAlias; |
| 327 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 328 | Function *Fn = nullptr; |
| 329 | auto MaybeFnA = parentFunctionOfValue(ValA); |
| 330 | auto MaybeFnB = parentFunctionOfValue(ValB); |
| 331 | if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 332 | // The only times this is known to happen are when globals + InlineAsm are |
| 333 | // involved |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 334 | DEBUG(dbgs() |
| 335 | << "CFLSteensAA: could not extract parent function information.\n"); |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 336 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (MaybeFnA.hasValue()) { |
| 340 | Fn = *MaybeFnA; |
| 341 | assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) && |
| 342 | "Interprocedural queries not supported"); |
| 343 | } else { |
| 344 | Fn = *MaybeFnB; |
| 345 | } |
| 346 | |
| 347 | assert(Fn != nullptr); |
| 348 | auto &MaybeInfo = ensureCached(Fn); |
| 349 | assert(MaybeInfo.hasValue()); |
| 350 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 351 | auto &Sets = MaybeInfo->getStratifiedSets(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 352 | auto MaybeA = Sets.find(ValA); |
| 353 | if (!MaybeA.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 354 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 355 | |
| 356 | auto MaybeB = Sets.find(ValB); |
| 357 | if (!MaybeB.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 358 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 359 | |
| 360 | auto SetA = *MaybeA; |
| 361 | auto SetB = *MaybeB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 362 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 363 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 364 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 365 | // If both values are local (meaning the corresponding set has attribute |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 366 | // AttrNone or AttrEscaped), then we know that CFLSteensAA fully models them: |
| 367 | // 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] | 368 | // If at least one value is non-local (meaning it either is global/argument or |
| 369 | // it comes from unknown sources like integer cast), the situation becomes a |
| 370 | // bit more interesting. We follow three general rules described below: |
| 371 | // - Non-local values may alias each other |
| 372 | // - AttrNone values do not alias any non-local values |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 373 | // - AttrEscaped do not alias globals/arguments, but they may alias |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 374 | // AttrUnknown values |
| 375 | if (SetA.Index == SetB.Index) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 376 | return MayAlias; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 377 | if (AttrsA.none() || AttrsB.none()) |
| 378 | return NoAlias; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 379 | if (hasUnknownOrCallerAttr(AttrsA) || hasUnknownOrCallerAttr(AttrsB)) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 380 | return MayAlias; |
| 381 | if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB)) |
| 382 | return MayAlias; |
| 383 | return NoAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 384 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 385 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 386 | ModRefInfo CFLSteensAAResult::getArgModRefInfo(ImmutableCallSite CS, |
| 387 | unsigned ArgIdx) { |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 388 | if (auto CalledFunc = CS.getCalledFunction()) { |
| 389 | auto &MaybeInfo = ensureCached(const_cast<Function *>(CalledFunc)); |
| 390 | if (!MaybeInfo.hasValue()) |
| 391 | return MRI_ModRef; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 392 | auto &RetParamAttributes = MaybeInfo->getAliasSummary().RetParamAttributes; |
| 393 | auto &RetParamRelations = MaybeInfo->getAliasSummary().RetParamRelations; |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 394 | |
| 395 | bool ArgAttributeIsWritten = |
| 396 | std::any_of(RetParamAttributes.begin(), RetParamAttributes.end(), |
| 397 | [ArgIdx](const ExternalAttribute &ExtAttr) { |
| 398 | return ExtAttr.IValue.Index == ArgIdx + 1; |
| 399 | }); |
| 400 | bool ArgIsAccessed = |
| 401 | std::any_of(RetParamRelations.begin(), RetParamRelations.end(), |
| 402 | [ArgIdx](const ExternalRelation &ExtRelation) { |
| 403 | return ExtRelation.To.Index == ArgIdx + 1 || |
| 404 | ExtRelation.From.Index == ArgIdx + 1; |
| 405 | }); |
| 406 | |
| 407 | return (!ArgIsAccessed && !ArgAttributeIsWritten) ? MRI_NoModRef |
| 408 | : MRI_ModRef; |
| 409 | } |
| 410 | |
| 411 | return MRI_ModRef; |
| 412 | } |
| 413 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 414 | FunctionModRefBehavior |
| 415 | CFLSteensAAResult::getModRefBehavior(ImmutableCallSite CS) { |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 416 | // If we know the callee, try analyzing it |
| 417 | if (auto CalledFunc = CS.getCalledFunction()) |
| 418 | return getModRefBehavior(CalledFunc); |
| 419 | |
| 420 | // Otherwise, be conservative |
| 421 | return FMRB_UnknownModRefBehavior; |
| 422 | } |
| 423 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 424 | FunctionModRefBehavior CFLSteensAAResult::getModRefBehavior(const Function *F) { |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 425 | assert(F != nullptr); |
| 426 | |
| 427 | // TODO: Remove the const_cast |
| 428 | auto &MaybeInfo = ensureCached(const_cast<Function *>(F)); |
| 429 | if (!MaybeInfo.hasValue()) |
| 430 | return FMRB_UnknownModRefBehavior; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 431 | auto &RetParamAttributes = MaybeInfo->getAliasSummary().RetParamAttributes; |
| 432 | auto &RetParamRelations = MaybeInfo->getAliasSummary().RetParamRelations; |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 433 | |
| 434 | // First, if any argument is marked Escpaed, Unknown or Global, anything may |
| 435 | // happen to them and thus we can't draw any conclusion. |
| 436 | if (!RetParamAttributes.empty()) |
| 437 | return FMRB_UnknownModRefBehavior; |
| 438 | |
| 439 | // Currently we don't (and can't) distinguish reads from writes in |
| 440 | // RetParamRelations. All we can say is whether there may be memory access or |
| 441 | // not. |
| 442 | if (RetParamRelations.empty()) |
| 443 | return FMRB_DoesNotAccessMemory; |
| 444 | |
| 445 | // Check if something beyond argmem gets touched. |
| 446 | bool AccessArgMemoryOnly = |
| 447 | std::all_of(RetParamRelations.begin(), RetParamRelations.end(), |
| 448 | [](const ExternalRelation &ExtRelation) { |
| 449 | // Both DerefLevels has to be 0, since we don't know which |
| 450 | // one is a read and which is a write. |
| 451 | return ExtRelation.From.DerefLevel == 0 && |
| 452 | ExtRelation.To.DerefLevel == 0; |
| 453 | }); |
| 454 | return AccessArgMemoryOnly ? FMRB_OnlyAccessesArgumentPointees |
| 455 | : FMRB_UnknownModRefBehavior; |
| 456 | } |
| 457 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 458 | char CFLSteensAA::PassID; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 459 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 460 | CFLSteensAAResult CFLSteensAA::run(Function &F, AnalysisManager<Function> &AM) { |
| 461 | return CFLSteensAAResult(AM.getResult<TargetLibraryAnalysis>(F)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 462 | } |
| 463 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 464 | char CFLSteensAAWrapperPass::ID = 0; |
| 465 | INITIALIZE_PASS(CFLSteensAAWrapperPass, "cfl-steens-aa", |
| 466 | "Unification-Based CFL Alias Analysis", false, true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 467 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 468 | ImmutablePass *llvm::createCFLSteensAAWrapperPass() { |
| 469 | return new CFLSteensAAWrapperPass(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 472 | CFLSteensAAWrapperPass::CFLSteensAAWrapperPass() : ImmutablePass(ID) { |
| 473 | initializeCFLSteensAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 474 | } |
| 475 | |
| 476 | void CFLSteensAAWrapperPass::initializePass() { |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 477 | auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 478 | Result.reset(new CFLSteensAAResult(TLIWP.getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 479 | } |
| 480 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 481 | void CFLSteensAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 482 | AU.setPreservesAll(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 483 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 484 | } |