Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 1 | //===- CFLSteensAliasAnalysis.cpp - Unification-based Alias Analysis ------===// |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 9 | // This file implements a CFL-base, summary-based alias analysis algorithm. It |
| 10 | // does not depend on types. The algorithm is a mixture of the one described in |
| 11 | // "Demand-driven alias analysis for C" by Xin Zheng and Radu Rugina, and "Fast |
| 12 | // algorithms for Dyck-CFL-reachability with applications to Alias Analysis" by |
| 13 | // Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the papers, we build a |
| 14 | // graph of the uses of a variable, where each node is a memory location, and |
| 15 | // each edge is an action that happened on that memory location. The "actions" |
| 16 | // can be one of Dereference, Reference, or Assign. The precision of this |
| 17 | // analysis is roughly the same as that of an one level context-sensitive |
| 18 | // Steensgaard's algorithm. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 19 | // |
| 20 | // Two variables are considered as aliasing iff you can reach one value's node |
| 21 | // from the other value's node and the language formed by concatenating all of |
| 22 | // the edge labels (actions) conforms to a context-free grammar. |
| 23 | // |
| 24 | // Because this algorithm requires a graph search on each query, we execute the |
| 25 | // algorithm outlined in "Fast algorithms..." (mentioned above) |
| 26 | // 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] | 27 | // ~nlogn time (n = number of variables), which makes queries take constant |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 28 | // time. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 31 | // 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] | 32 | // CFLSteensAA is interprocedural. This is *technically* A Bad Thing, because |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 33 | // FunctionPasses are only allowed to inspect the Function that they're being |
| 34 | // run on. Realistically, this likely isn't a problem until we allow |
| 35 | // FunctionPasses to run concurrently. |
| 36 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 38 | #include "AliasAnalysisSummary.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" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/Optional.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/SmallVector.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" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 47 | #include "llvm/IR/Type.h" |
| 48 | #include "llvm/IR/Value.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 49 | #include "llvm/Pass.h" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.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> |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 54 | #include <limits> |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 55 | #include <memory> |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 56 | #include <utility> |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 57 | |
| 58 | using namespace llvm; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 59 | using namespace llvm::cflaa; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 60 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 61 | #define DEBUG_TYPE "cfl-steens-aa" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 62 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 63 | CFLSteensAAResult::CFLSteensAAResult(const TargetLibraryInfo &TLI) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 64 | : AAResultBase(), TLI(TLI) {} |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 65 | CFLSteensAAResult::CFLSteensAAResult(CFLSteensAAResult &&Arg) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 66 | : AAResultBase(std::move(Arg)), TLI(Arg.TLI) {} |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 67 | CFLSteensAAResult::~CFLSteensAAResult() = default; |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 68 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 69 | /// 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] | 70 | class CFLSteensAAResult::FunctionInfo { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 71 | StratifiedSets<InstantiatedValue> Sets; |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 72 | AliasSummary Summary; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 73 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 74 | public: |
| 75 | FunctionInfo(Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 76 | StratifiedSets<InstantiatedValue> S); |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 77 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 78 | const StratifiedSets<InstantiatedValue> &getStratifiedSets() const { |
| 79 | return Sets; |
| 80 | } |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 81 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 82 | const AliasSummary &getAliasSummary() const { return Summary; } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 85 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 86 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 87 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 88 | //===----------------------------------------------------------------------===// |
| 89 | // Function declarations that require types defined in the namespace above |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 92 | /// 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] | 93 | static bool canSkipAddingToSets(Value *Val) { |
| 94 | // Constants can share instances, which may falsely unify multiple |
| 95 | // sets, e.g. in |
| 96 | // store i32* null, i32** %ptr1 |
| 97 | // store i32* null, i32** %ptr2 |
| 98 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 99 | // we should filter out the (potentially shared) instance to |
| 100 | // i32* null. |
| 101 | if (isa<Constant>(Val)) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 102 | // TODO: Because all of these things are constant, we can determine whether |
| 103 | // the data is *actually* mutable at graph building time. This will probably |
| 104 | // come for free/cheap with offset awareness. |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 105 | bool CanStoreMutableData = isa<GlobalValue>(Val) || |
| 106 | isa<ConstantExpr>(Val) || |
| 107 | isa<ConstantAggregate>(Val); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 108 | return !CanStoreMutableData; |
| 109 | } |
| 110 | |
| 111 | return false; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 112 | } |
| 113 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 114 | CFLSteensAAResult::FunctionInfo::FunctionInfo( |
| 115 | Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 116 | StratifiedSets<InstantiatedValue> S) |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 117 | : Sets(std::move(S)) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 118 | // Historically, an arbitrary upper-bound of 50 args was selected. We may want |
| 119 | // to remove this if it doesn't really matter in practice. |
| 120 | if (Fn.arg_size() > MaxSupportedArgsInSummary) |
| 121 | return; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 122 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 123 | DenseMap<StratifiedIndex, InterfaceValue> InterfaceMap; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 124 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 125 | // Our intention here is to record all InterfaceValues that share the same |
| 126 | // StratifiedIndex in RetParamRelations. For each valid InterfaceValue, we |
| 127 | // have its StratifiedIndex scanned here and check if the index is presented |
| 128 | // in InterfaceMap: if it is not, we add the correspondence to the map; |
| 129 | // otherwise, an aliasing relation is found and we add it to |
| 130 | // RetParamRelations. |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 131 | |
George Burgess IV | d14d05a | 2016-06-23 20:59:13 +0000 | [diff] [blame] | 132 | auto AddToRetParamRelations = [&](unsigned InterfaceIndex, |
| 133 | StratifiedIndex SetIndex) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 134 | unsigned Level = 0; |
| 135 | while (true) { |
| 136 | InterfaceValue CurrValue{InterfaceIndex, Level}; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 137 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 138 | auto Itr = InterfaceMap.find(SetIndex); |
| 139 | if (Itr != InterfaceMap.end()) { |
| 140 | if (CurrValue != Itr->second) |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 141 | Summary.RetParamRelations.push_back( |
George Burgess IV | 4ec1753 | 2016-07-22 22:30:48 +0000 | [diff] [blame] | 142 | ExternalRelation{CurrValue, Itr->second, UnknownOffset}); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 143 | break; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 144 | } |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 145 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 146 | auto &Link = Sets.getLink(SetIndex); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 147 | InterfaceMap.insert(std::make_pair(SetIndex, CurrValue)); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 148 | auto ExternalAttrs = getExternallyVisibleAttrs(Link.Attrs); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 149 | if (ExternalAttrs.any()) |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 150 | Summary.RetParamAttributes.push_back( |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 151 | ExternalAttribute{CurrValue, ExternalAttrs}); |
| 152 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 153 | if (!Link.hasBelow()) |
| 154 | break; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 155 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 156 | ++Level; |
| 157 | SetIndex = Link.Below; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 158 | } |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | // Populate RetParamRelations for return values |
| 162 | for (auto *RetVal : RetVals) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 163 | assert(RetVal != nullptr); |
| 164 | assert(RetVal->getType()->isPointerTy()); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 165 | auto RetInfo = Sets.find(InstantiatedValue{RetVal, 0}); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 166 | if (RetInfo.hasValue()) |
| 167 | AddToRetParamRelations(0, RetInfo->Index); |
| 168 | } |
| 169 | |
| 170 | // Populate RetParamRelations for parameters |
| 171 | unsigned I = 0; |
| 172 | for (auto &Param : Fn.args()) { |
| 173 | if (Param.getType()->isPointerTy()) { |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 174 | auto ParamInfo = Sets.find(InstantiatedValue{&Param, 0}); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 175 | if (ParamInfo.hasValue()) |
| 176 | AddToRetParamRelations(I + 1, ParamInfo->Index); |
| 177 | } |
| 178 | ++I; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 182 | // Builds the graph + StratifiedSets for a function. |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 183 | CFLSteensAAResult::FunctionInfo CFLSteensAAResult::buildSetsFrom(Function *Fn) { |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 184 | CFLGraphBuilder<CFLSteensAAResult> GraphBuilder(*this, TLI, *Fn); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 185 | StratifiedSetsBuilder<InstantiatedValue> SetBuilder; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 186 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 187 | // Add all CFLGraph nodes and all Dereference edges to StratifiedSets |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 188 | auto &Graph = GraphBuilder.getCFLGraph(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 189 | for (const auto &Mapping : Graph.value_mappings()) { |
| 190 | auto Val = Mapping.first; |
| 191 | if (canSkipAddingToSets(Val)) |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 192 | continue; |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 193 | auto &ValueInfo = Mapping.second; |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 194 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 195 | assert(ValueInfo.getNumLevels() > 0); |
| 196 | SetBuilder.add(InstantiatedValue{Val, 0}); |
| 197 | SetBuilder.noteAttributes(InstantiatedValue{Val, 0}, |
| 198 | ValueInfo.getNodeInfoAtLevel(0).Attr); |
| 199 | for (unsigned I = 0, E = ValueInfo.getNumLevels() - 1; I < E; ++I) { |
| 200 | SetBuilder.add(InstantiatedValue{Val, I + 1}); |
| 201 | SetBuilder.noteAttributes(InstantiatedValue{Val, I + 1}, |
| 202 | ValueInfo.getNodeInfoAtLevel(I + 1).Attr); |
| 203 | SetBuilder.addBelow(InstantiatedValue{Val, I}, |
| 204 | InstantiatedValue{Val, I + 1}); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 208 | // Add all assign edges to StratifiedSets |
| 209 | for (const auto &Mapping : Graph.value_mappings()) { |
| 210 | auto Val = Mapping.first; |
| 211 | if (canSkipAddingToSets(Val)) |
| 212 | continue; |
| 213 | auto &ValueInfo = Mapping.second; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 214 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 215 | for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) { |
| 216 | auto Src = InstantiatedValue{Val, I}; |
| 217 | for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) |
| 218 | SetBuilder.addWith(Src, Edge.Other); |
| 219 | } |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 220 | } |
| 221 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 222 | return FunctionInfo(*Fn, GraphBuilder.getReturnValues(), SetBuilder.build()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 223 | } |
| 224 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 225 | void CFLSteensAAResult::scan(Function *Fn) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 226 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 227 | (void)InsertPair; |
| 228 | assert(InsertPair.second && |
| 229 | "Trying to scan a function that has already been cached"); |
| 230 | |
George Burgess IV | 6edb891 | 2016-05-02 18:09:19 +0000 | [diff] [blame] | 231 | // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call |
| 232 | // may get evaluated after operator[], potentially triggering a DenseMap |
| 233 | // resize and invalidating the reference returned by operator[] |
| 234 | auto FunInfo = buildSetsFrom(Fn); |
| 235 | Cache[Fn] = std::move(FunInfo); |
| 236 | |
Davide Italiano | e34a806 | 2017-06-26 23:59:14 +0000 | [diff] [blame] | 237 | Handles.emplace_front(Fn, this); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 240 | void CFLSteensAAResult::evict(Function *Fn) { Cache.erase(Fn); } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 241 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 242 | /// Ensures that the given function is available in the cache, and returns the |
| 243 | /// entry. |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 244 | const Optional<CFLSteensAAResult::FunctionInfo> & |
| 245 | CFLSteensAAResult::ensureCached(Function *Fn) { |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 246 | auto Iter = Cache.find(Fn); |
| 247 | if (Iter == Cache.end()) { |
| 248 | scan(Fn); |
| 249 | Iter = Cache.find(Fn); |
| 250 | assert(Iter != Cache.end()); |
| 251 | assert(Iter->second.hasValue()); |
| 252 | } |
| 253 | return Iter->second; |
| 254 | } |
| 255 | |
George Burgess IV | c294d0d | 2016-07-09 02:54:42 +0000 | [diff] [blame] | 256 | const AliasSummary *CFLSteensAAResult::getAliasSummary(Function &Fn) { |
| 257 | auto &FunInfo = ensureCached(&Fn); |
| 258 | if (FunInfo.hasValue()) |
| 259 | return &FunInfo->getAliasSummary(); |
| 260 | else |
| 261 | return nullptr; |
| 262 | } |
| 263 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 264 | AliasResult CFLSteensAAResult::query(const MemoryLocation &LocA, |
| 265 | const MemoryLocation &LocB) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 266 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 267 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 268 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 269 | if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy()) |
| 270 | return NoAlias; |
| 271 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 272 | Function *Fn = nullptr; |
Davide Italiano | 31d4c1b | 2017-06-27 02:25:06 +0000 | [diff] [blame] | 273 | Function *MaybeFnA = const_cast<Function *>(parentFunctionOfValue(ValA)); |
| 274 | Function *MaybeFnB = const_cast<Function *>(parentFunctionOfValue(ValB)); |
Davide Italiano | 604c003 | 2017-06-27 00:33:37 +0000 | [diff] [blame] | 275 | if (!MaybeFnA && !MaybeFnB) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 276 | // The only times this is known to happen are when globals + InlineAsm are |
| 277 | // involved |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 278 | LLVM_DEBUG( |
| 279 | dbgs() |
| 280 | << "CFLSteensAA: could not extract parent function information.\n"); |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 281 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Davide Italiano | 604c003 | 2017-06-27 00:33:37 +0000 | [diff] [blame] | 284 | if (MaybeFnA) { |
| 285 | Fn = MaybeFnA; |
| 286 | assert((!MaybeFnB || MaybeFnB == MaybeFnA) && |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 287 | "Interprocedural queries not supported"); |
| 288 | } else { |
Davide Italiano | 604c003 | 2017-06-27 00:33:37 +0000 | [diff] [blame] | 289 | Fn = MaybeFnB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | assert(Fn != nullptr); |
| 293 | auto &MaybeInfo = ensureCached(Fn); |
| 294 | assert(MaybeInfo.hasValue()); |
| 295 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 296 | auto &Sets = MaybeInfo->getStratifiedSets(); |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 297 | auto MaybeA = Sets.find(InstantiatedValue{ValA, 0}); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 298 | if (!MaybeA.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 299 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 300 | |
George Burgess IV | de1be71 | 2016-07-11 22:59:09 +0000 | [diff] [blame] | 301 | auto MaybeB = Sets.find(InstantiatedValue{ValB, 0}); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 302 | if (!MaybeB.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 303 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 304 | |
| 305 | auto SetA = *MaybeA; |
| 306 | auto SetB = *MaybeB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 307 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 308 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 309 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 310 | // If both values are local (meaning the corresponding set has attribute |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 311 | // AttrNone or AttrEscaped), then we know that CFLSteensAA fully models them: |
| 312 | // 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] | 313 | // If at least one value is non-local (meaning it either is global/argument or |
| 314 | // it comes from unknown sources like integer cast), the situation becomes a |
| 315 | // bit more interesting. We follow three general rules described below: |
| 316 | // - Non-local values may alias each other |
| 317 | // - AttrNone values do not alias any non-local values |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 318 | // - AttrEscaped do not alias globals/arguments, but they may alias |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 319 | // AttrUnknown values |
| 320 | if (SetA.Index == SetB.Index) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 321 | return MayAlias; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 322 | if (AttrsA.none() || AttrsB.none()) |
| 323 | return NoAlias; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame] | 324 | if (hasUnknownOrCallerAttr(AttrsA) || hasUnknownOrCallerAttr(AttrsB)) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 325 | return MayAlias; |
| 326 | if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB)) |
| 327 | return MayAlias; |
| 328 | return NoAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 329 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 330 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 331 | AnalysisKey CFLSteensAA::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 332 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 333 | CFLSteensAAResult CFLSteensAA::run(Function &F, FunctionAnalysisManager &AM) { |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 334 | return CFLSteensAAResult(AM.getResult<TargetLibraryAnalysis>(F)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 335 | } |
| 336 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 337 | char CFLSteensAAWrapperPass::ID = 0; |
| 338 | INITIALIZE_PASS(CFLSteensAAWrapperPass, "cfl-steens-aa", |
| 339 | "Unification-Based CFL Alias Analysis", false, true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 340 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 341 | ImmutablePass *llvm::createCFLSteensAAWrapperPass() { |
| 342 | return new CFLSteensAAWrapperPass(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 343 | } |
| 344 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 345 | CFLSteensAAWrapperPass::CFLSteensAAWrapperPass() : ImmutablePass(ID) { |
| 346 | initializeCFLSteensAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 347 | } |
| 348 | |
| 349 | void CFLSteensAAWrapperPass::initializePass() { |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 350 | auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 351 | Result.reset(new CFLSteensAAResult(TLIWP.getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 352 | } |
| 353 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 354 | void CFLSteensAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 355 | AU.setPreservesAll(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 356 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 357 | } |