Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 1 | //===- CFLSteensAliasAnalysis.cpp - Unification-based Alias Analysis ------===// |
Hal Finkel | bf301d5 | 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 | 3b5b98a | 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 | bf301d5 | 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 | 134d294 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 28 | // ~nlogn time (n = number of variables), which makes queries take constant |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 29 | // time. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
George Burgess IV | 134d294 | 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 | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 33 | // CFLSteensAA is interprocedural. This is *technically* A Bad Thing, because |
George Burgess IV | 134d294 | 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 | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 39 | #include "AliasAnalysisSummary.h" |
George Burgess IV | 02af278 | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 40 | #include "CFLGraph.h" |
George Burgess IV | cc48e0f | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 41 | #include "StratifiedSets.h" |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/Optional.h" |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallVector.h" |
George Burgess IV | dbfc82f | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Constants.h" |
| 47 | #include "llvm/IR/Function.h" |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 48 | #include "llvm/IR/Type.h" |
| 49 | #include "llvm/IR/Value.h" |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 50 | #include "llvm/Pass.h" |
George Burgess IV | c571d11 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 52 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 53 | #include <algorithm> |
| 54 | #include <cassert> |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 55 | #include <limits> |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 56 | #include <memory> |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 57 | #include <utility> |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 58 | |
| 59 | using namespace llvm; |
George Burgess IV | 02af278 | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 60 | using namespace llvm::cflaa; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 61 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 62 | #define DEBUG_TYPE "cfl-steens-aa" |
George Burgess IV | c571d11 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 63 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 64 | CFLSteensAAResult::CFLSteensAAResult(const TargetLibraryInfo &TLI) |
George Burgess IV | dbfc82f | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 65 | : AAResultBase(), TLI(TLI) {} |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 66 | CFLSteensAAResult::CFLSteensAAResult(CFLSteensAAResult &&Arg) |
George Burgess IV | dbfc82f | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 67 | : AAResultBase(std::move(Arg)), TLI(Arg.TLI) {} |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 68 | CFLSteensAAResult::~CFLSteensAAResult() = default; |
Chandler Carruth | 519ed7c | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 69 | |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 70 | /// Information we have about a function and would like to keep around. |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 71 | class CFLSteensAAResult::FunctionInfo { |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 72 | StratifiedSets<InstantiatedValue> Sets; |
George Burgess IV | 654ec29 | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 73 | AliasSummary Summary; |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 74 | |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 75 | public: |
| 76 | FunctionInfo(Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 77 | StratifiedSets<InstantiatedValue> S); |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 78 | |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 79 | const StratifiedSets<InstantiatedValue> &getStratifiedSets() const { |
| 80 | return Sets; |
| 81 | } |
Eugene Zelenko | c5e4ac8 | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 82 | |
George Burgess IV | 654ec29 | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 83 | const AliasSummary &getAliasSummary() const { return Summary; } |
Chandler Carruth | 519ed7c | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Hal Finkel | b38c0b8 | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 86 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 2546837 | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 87 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | b38c0b8 | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 88 | |
Hal Finkel | bf301d5 | 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 | ea89695 | 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 | d8bd7ce | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 94 | static bool canSkipAddingToSets(Value *Val) { |
| 95 | // Constants can share instances, which may falsely unify multiple |
| 96 | // sets, e.g. in |
| 97 | // store i32* null, i32** %ptr1 |
| 98 | // store i32* null, i32** %ptr2 |
| 99 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 100 | // we should filter out the (potentially shared) instance to |
| 101 | // i32* null. |
| 102 | if (isa<Constant>(Val)) { |
George Burgess IV | d8bd7ce | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 103 | // TODO: Because all of these things are constant, we can determine whether |
| 104 | // the data is *actually* mutable at graph building time. This will probably |
| 105 | // come for free/cheap with offset awareness. |
Duncan P. N. Exon Smith | d7773c0 | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 106 | bool CanStoreMutableData = isa<GlobalValue>(Val) || |
| 107 | isa<ConstantExpr>(Val) || |
| 108 | isa<ConstantAggregate>(Val); |
George Burgess IV | d8bd7ce | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 109 | return !CanStoreMutableData; |
| 110 | } |
| 111 | |
| 112 | return false; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 115 | CFLSteensAAResult::FunctionInfo::FunctionInfo( |
| 116 | Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 117 | StratifiedSets<InstantiatedValue> S) |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 118 | : Sets(std::move(S)) { |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 119 | // Historically, an arbitrary upper-bound of 50 args was selected. We may want |
| 120 | // to remove this if it doesn't really matter in practice. |
| 121 | if (Fn.arg_size() > MaxSupportedArgsInSummary) |
| 122 | return; |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 123 | |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 124 | DenseMap<StratifiedIndex, InterfaceValue> InterfaceMap; |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 125 | |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 126 | // Our intention here is to record all InterfaceValues that share the same |
| 127 | // StratifiedIndex in RetParamRelations. For each valid InterfaceValue, we |
| 128 | // have its StratifiedIndex scanned here and check if the index is presented |
| 129 | // in InterfaceMap: if it is not, we add the correspondence to the map; |
| 130 | // otherwise, an aliasing relation is found and we add it to |
| 131 | // RetParamRelations. |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 132 | |
George Burgess IV | f71f365 | 2016-06-23 20:59:13 +0000 | [diff] [blame] | 133 | auto AddToRetParamRelations = [&](unsigned InterfaceIndex, |
| 134 | StratifiedIndex SetIndex) { |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 135 | unsigned Level = 0; |
| 136 | while (true) { |
| 137 | InterfaceValue CurrValue{InterfaceIndex, Level}; |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 138 | |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 139 | auto Itr = InterfaceMap.find(SetIndex); |
| 140 | if (Itr != InterfaceMap.end()) { |
| 141 | if (CurrValue != Itr->second) |
George Burgess IV | 654ec29 | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 142 | Summary.RetParamRelations.push_back( |
George Burgess IV | 3dba903 | 2016-07-22 22:30:48 +0000 | [diff] [blame] | 143 | ExternalRelation{CurrValue, Itr->second, UnknownOffset}); |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 144 | break; |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 145 | } |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 146 | |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 147 | auto &Link = Sets.getLink(SetIndex); |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 148 | InterfaceMap.insert(std::make_pair(SetIndex, CurrValue)); |
George Burgess IV | cc48e0f | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 149 | auto ExternalAttrs = getExternallyVisibleAttrs(Link.Attrs); |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 150 | if (ExternalAttrs.any()) |
George Burgess IV | 654ec29 | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 151 | Summary.RetParamAttributes.push_back( |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 152 | ExternalAttribute{CurrValue, ExternalAttrs}); |
| 153 | |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 154 | if (!Link.hasBelow()) |
| 155 | break; |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 156 | |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 157 | ++Level; |
| 158 | SetIndex = Link.Below; |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 159 | } |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | // Populate RetParamRelations for return values |
| 163 | for (auto *RetVal : RetVals) { |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 164 | assert(RetVal != nullptr); |
| 165 | assert(RetVal->getType()->isPointerTy()); |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 166 | auto RetInfo = Sets.find(InstantiatedValue{RetVal, 0}); |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 167 | if (RetInfo.hasValue()) |
| 168 | AddToRetParamRelations(0, RetInfo->Index); |
| 169 | } |
| 170 | |
| 171 | // Populate RetParamRelations for parameters |
| 172 | unsigned I = 0; |
| 173 | for (auto &Param : Fn.args()) { |
| 174 | if (Param.getType()->isPointerTy()) { |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 175 | auto ParamInfo = Sets.find(InstantiatedValue{&Param, 0}); |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 176 | if (ParamInfo.hasValue()) |
| 177 | AddToRetParamRelations(I + 1, ParamInfo->Index); |
| 178 | } |
| 179 | ++I; |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Chandler Carruth | 519ed7c | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 183 | // Builds the graph + StratifiedSets for a function. |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 184 | CFLSteensAAResult::FunctionInfo CFLSteensAAResult::buildSetsFrom(Function *Fn) { |
George Burgess IV | 654ec29 | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 185 | CFLGraphBuilder<CFLSteensAAResult> GraphBuilder(*this, TLI, *Fn); |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 186 | StratifiedSetsBuilder<InstantiatedValue> SetBuilder; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 187 | |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 188 | // Add all CFLGraph nodes and all Dereference edges to StratifiedSets |
George Burgess IV | 59a3486 | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 189 | auto &Graph = GraphBuilder.getCFLGraph(); |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 190 | for (const auto &Mapping : Graph.value_mappings()) { |
| 191 | auto Val = Mapping.first; |
| 192 | if (canSkipAddingToSets(Val)) |
George Burgess IV | b7ebc01 | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 193 | continue; |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 194 | auto &ValueInfo = Mapping.second; |
George Burgess IV | b7ebc01 | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 195 | |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 196 | assert(ValueInfo.getNumLevels() > 0); |
| 197 | SetBuilder.add(InstantiatedValue{Val, 0}); |
| 198 | SetBuilder.noteAttributes(InstantiatedValue{Val, 0}, |
| 199 | ValueInfo.getNodeInfoAtLevel(0).Attr); |
| 200 | for (unsigned I = 0, E = ValueInfo.getNumLevels() - 1; I < E; ++I) { |
| 201 | SetBuilder.add(InstantiatedValue{Val, I + 1}); |
| 202 | SetBuilder.noteAttributes(InstantiatedValue{Val, I + 1}, |
| 203 | ValueInfo.getNodeInfoAtLevel(I + 1).Attr); |
| 204 | SetBuilder.addBelow(InstantiatedValue{Val, I}, |
| 205 | InstantiatedValue{Val, I + 1}); |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 209 | // Add all assign edges to StratifiedSets |
| 210 | for (const auto &Mapping : Graph.value_mappings()) { |
| 211 | auto Val = Mapping.first; |
| 212 | if (canSkipAddingToSets(Val)) |
| 213 | continue; |
| 214 | auto &ValueInfo = Mapping.second; |
George Burgess IV | 39639d9 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 215 | |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 216 | for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) { |
| 217 | auto Src = InstantiatedValue{Val, I}; |
| 218 | for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) |
| 219 | SetBuilder.addWith(Src, Edge.Other); |
| 220 | } |
George Burgess IV | 7161bf8 | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 221 | } |
| 222 | |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 223 | return FunctionInfo(*Fn, GraphBuilder.getReturnValues(), SetBuilder.build()); |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 224 | } |
| 225 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 226 | void CFLSteensAAResult::scan(Function *Fn) { |
Hal Finkel | 8de4282 | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 227 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 228 | (void)InsertPair; |
| 229 | assert(InsertPair.second && |
| 230 | "Trying to scan a function that has already been cached"); |
| 231 | |
George Burgess IV | c3791a9 | 2016-05-02 18:09:19 +0000 | [diff] [blame] | 232 | // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call |
| 233 | // may get evaluated after operator[], potentially triggering a DenseMap |
| 234 | // resize and invalidating the reference returned by operator[] |
| 235 | auto FunInfo = buildSetsFrom(Fn); |
| 236 | Cache[Fn] = std::move(FunInfo); |
| 237 | |
Davide Italiano | 4d73d7a | 2017-06-26 23:59:14 +0000 | [diff] [blame] | 238 | Handles.emplace_front(Fn, this); |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 239 | } |
| 240 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 241 | void CFLSteensAAResult::evict(Function *Fn) { Cache.erase(Fn); } |
Chandler Carruth | 519ed7c | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 242 | |
George Burgess IV | ea89695 | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 243 | /// Ensures that the given function is available in the cache, and returns the |
| 244 | /// entry. |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 245 | const Optional<CFLSteensAAResult::FunctionInfo> & |
| 246 | CFLSteensAAResult::ensureCached(Function *Fn) { |
Chandler Carruth | 519ed7c | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 247 | auto Iter = Cache.find(Fn); |
| 248 | if (Iter == Cache.end()) { |
| 249 | scan(Fn); |
| 250 | Iter = Cache.find(Fn); |
| 251 | assert(Iter != Cache.end()); |
| 252 | assert(Iter->second.hasValue()); |
| 253 | } |
| 254 | return Iter->second; |
| 255 | } |
| 256 | |
George Burgess IV | 654ec29 | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 257 | const AliasSummary *CFLSteensAAResult::getAliasSummary(Function &Fn) { |
| 258 | auto &FunInfo = ensureCached(&Fn); |
| 259 | if (FunInfo.hasValue()) |
| 260 | return &FunInfo->getAliasSummary(); |
| 261 | else |
| 262 | return nullptr; |
| 263 | } |
| 264 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 265 | AliasResult CFLSteensAAResult::query(const MemoryLocation &LocA, |
| 266 | const MemoryLocation &LocB) { |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 267 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 268 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 269 | |
George Burgess IV | 3cdf3eb | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 270 | if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy()) |
| 271 | return NoAlias; |
| 272 | |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 273 | Function *Fn = nullptr; |
Davide Italiano | 257136b | 2017-06-27 02:25:06 +0000 | [diff] [blame] | 274 | Function *MaybeFnA = const_cast<Function *>(parentFunctionOfValue(ValA)); |
| 275 | Function *MaybeFnB = const_cast<Function *>(parentFunctionOfValue(ValB)); |
Davide Italiano | 2ba2672 | 2017-06-27 00:33:37 +0000 | [diff] [blame] | 276 | if (!MaybeFnA && !MaybeFnB) { |
George Burgess IV | ea89695 | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 277 | // The only times this is known to happen are when globals + InlineAsm are |
| 278 | // involved |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 279 | LLVM_DEBUG( |
| 280 | dbgs() |
| 281 | << "CFLSteensAA: could not extract parent function information.\n"); |
Chandler Carruth | 1e3557d | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 282 | return MayAlias; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Davide Italiano | 2ba2672 | 2017-06-27 00:33:37 +0000 | [diff] [blame] | 285 | if (MaybeFnA) { |
| 286 | Fn = MaybeFnA; |
| 287 | assert((!MaybeFnB || MaybeFnB == MaybeFnA) && |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 288 | "Interprocedural queries not supported"); |
| 289 | } else { |
Davide Italiano | 2ba2672 | 2017-06-27 00:33:37 +0000 | [diff] [blame] | 290 | Fn = MaybeFnB; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | assert(Fn != nullptr); |
| 294 | auto &MaybeInfo = ensureCached(Fn); |
| 295 | assert(MaybeInfo.hasValue()); |
| 296 | |
George Burgess IV | 4ac8718 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 297 | auto &Sets = MaybeInfo->getStratifiedSets(); |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 298 | auto MaybeA = Sets.find(InstantiatedValue{ValA, 0}); |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 299 | if (!MaybeA.hasValue()) |
Chandler Carruth | 1e3557d | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 300 | return MayAlias; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 301 | |
George Burgess IV | 9e93706 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 302 | auto MaybeB = Sets.find(InstantiatedValue{ValB, 0}); |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 303 | if (!MaybeB.hasValue()) |
Chandler Carruth | 1e3557d | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 304 | return MayAlias; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 305 | |
| 306 | auto SetA = *MaybeA; |
| 307 | auto SetB = *MaybeB; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 308 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 309 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | c571d11 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 310 | |
George Burgess IV | 90fe2c5 | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 311 | // If both values are local (meaning the corresponding set has attribute |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 312 | // AttrNone or AttrEscaped), then we know that CFLSteensAA fully models them: |
| 313 | // they may-alias each other if and only if they are in the same set. |
George Burgess IV | 90fe2c5 | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 314 | // If at least one value is non-local (meaning it either is global/argument or |
| 315 | // it comes from unknown sources like integer cast), the situation becomes a |
| 316 | // bit more interesting. We follow three general rules described below: |
| 317 | // - Non-local values may alias each other |
| 318 | // - AttrNone values do not alias any non-local values |
George Burgess IV | bec2df6 | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 319 | // - AttrEscaped do not alias globals/arguments, but they may alias |
George Burgess IV | 90fe2c5 | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 320 | // AttrUnknown values |
| 321 | if (SetA.Index == SetB.Index) |
Chandler Carruth | 1e3557d | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 322 | return MayAlias; |
George Burgess IV | 90fe2c5 | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 323 | if (AttrsA.none() || AttrsB.none()) |
| 324 | return NoAlias; |
George Burgess IV | cc48e0f | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 325 | if (hasUnknownOrCallerAttr(AttrsA) || hasUnknownOrCallerAttr(AttrsB)) |
George Burgess IV | 90fe2c5 | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 326 | return MayAlias; |
| 327 | if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB)) |
| 328 | return MayAlias; |
| 329 | return NoAlias; |
Hal Finkel | bf301d5 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 330 | } |
Mehdi Amini | c94da20 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 331 | |
Chandler Carruth | 33d5681 | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 332 | AnalysisKey CFLSteensAA::Key; |
Chandler Carruth | e95015f | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 333 | |
Sean Silva | 20b343c | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 334 | CFLSteensAAResult CFLSteensAA::run(Function &F, FunctionAnalysisManager &AM) { |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 335 | return CFLSteensAAResult(AM.getResult<TargetLibraryAnalysis>(F)); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 336 | } |
| 337 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 338 | char CFLSteensAAWrapperPass::ID = 0; |
| 339 | INITIALIZE_PASS(CFLSteensAAWrapperPass, "cfl-steens-aa", |
| 340 | "Unification-Based CFL Alias Analysis", false, true) |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 341 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 342 | ImmutablePass *llvm::createCFLSteensAAWrapperPass() { |
| 343 | return new CFLSteensAAWrapperPass(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 344 | } |
| 345 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 346 | CFLSteensAAWrapperPass::CFLSteensAAWrapperPass() : ImmutablePass(ID) { |
| 347 | initializeCFLSteensAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 348 | } |
| 349 | |
| 350 | void CFLSteensAAWrapperPass::initializePass() { |
George Burgess IV | dbfc82f | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 351 | auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>(); |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 352 | Result.reset(new CFLSteensAAResult(TLIWP.getTLI())); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 353 | } |
| 354 | |
George Burgess IV | 3b5b98a | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 355 | void CFLSteensAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 356 | AU.setPreservesAll(); |
George Burgess IV | dbfc82f | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 357 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Mehdi Amini | c94da20 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 358 | } |