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