Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1 | //===- CFLAliasAnalysis.cpp - CFL-Based Alias Analysis Implementation ------==// |
| 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 | // |
| 10 | // This file implements a CFL-based context-insensitive alias analysis |
| 11 | // algorithm. It does not depend on types. The algorithm is a mixture of the one |
| 12 | // described in "Demand-driven alias analysis for C" by Xin Zheng and Radu |
| 13 | // Rugina, and "Fast algorithms for Dyck-CFL-reachability with applications to |
| 14 | // Alias Analysis" by Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the |
| 15 | // papers, we build a graph of the uses of a variable, where each node is a |
| 16 | // memory location, and each edge is an action that happened on that memory |
| 17 | // location. The "actions" can be one of Dereference, Reference, Assign, or |
| 18 | // Assign. |
| 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 |
| 27 | // ~nlogn time (n = number of variables.), which makes queries take constant |
| 28 | // time. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | #include "StratifiedSets.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/BitVector.h" |
| 33 | #include "llvm/ADT/DenseMap.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/None.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Optional.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/Passes.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Constants.h" |
| 39 | #include "llvm/IR/Function.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 40 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 42 | #include "llvm/IR/ValueHandle.h" |
| 43 | #include "llvm/Pass.h" |
| 44 | #include "llvm/Support/Allocator.h" |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Compiler.h" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 48 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 49 | #include <algorithm> |
| 50 | #include <cassert> |
| 51 | #include <forward_list> |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 52 | #include <memory> |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 53 | #include <tuple> |
| 54 | |
| 55 | using namespace llvm; |
| 56 | |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 57 | #define DEBUG_TYPE "cfl-aa" |
| 58 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 59 | // Try to go from a Value* to a Function*. Never returns nullptr. |
| 60 | static Optional<Function *> parentFunctionOfValue(Value *); |
| 61 | |
| 62 | // Returns possible functions called by the Inst* into the given |
| 63 | // SmallVectorImpl. Returns true if targets found, false otherwise. |
| 64 | // This is templated because InvokeInst/CallInst give us the same |
| 65 | // set of functions that we care about, and I don't like repeating |
| 66 | // myself. |
| 67 | template <typename Inst> |
| 68 | static bool getPossibleTargets(Inst *, SmallVectorImpl<Function *> &); |
| 69 | |
| 70 | // Some instructions need to have their users tracked. Instructions like |
| 71 | // `add` require you to get the users of the Instruction* itself, other |
| 72 | // instructions like `store` require you to get the users of the first |
| 73 | // operand. This function gets the "proper" value to track for each |
| 74 | // type of instruction we support. |
| 75 | static Optional<Value *> getTargetValue(Instruction *); |
| 76 | |
| 77 | // There are certain instructions (i.e. FenceInst, etc.) that we ignore. |
| 78 | // This notes that we should ignore those. |
| 79 | static bool hasUsefulEdges(Instruction *); |
| 80 | |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 81 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 82 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 83 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 84 | namespace { |
| 85 | // StratifiedInfo Attribute things. |
| 86 | typedef unsigned StratifiedAttr; |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 87 | LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs; |
| 88 | LLVM_CONSTEXPR unsigned AttrAllIndex = 0; |
| 89 | LLVM_CONSTEXPR unsigned AttrGlobalIndex = 1; |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 90 | LLVM_CONSTEXPR unsigned AttrUnknownIndex = 2; |
| 91 | LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3; |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 92 | LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex; |
| 93 | LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 94 | |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 95 | LLVM_CONSTEXPR StratifiedAttr AttrNone = 0; |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 96 | LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex; |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 97 | LLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 98 | |
| 99 | // \brief StratifiedSets call for knowledge of "direction", so this is how we |
| 100 | // represent that locally. |
| 101 | enum class Level { Same, Above, Below }; |
| 102 | |
| 103 | // \brief Edges can be one of four "weights" -- each weight must have an inverse |
| 104 | // weight (Assign has Assign; Reference has Dereference). |
| 105 | enum class EdgeType { |
| 106 | // The weight assigned when assigning from or to a value. For example, in: |
| 107 | // %b = getelementptr %a, 0 |
| 108 | // ...The relationships are %b assign %a, and %a assign %b. This used to be |
| 109 | // two edges, but having a distinction bought us nothing. |
| 110 | Assign, |
| 111 | |
| 112 | // The edge used when we have an edge going from some handle to a Value. |
| 113 | // Examples of this include: |
| 114 | // %b = load %a (%b Dereference %a) |
| 115 | // %b = extractelement %a, 0 (%a Dereference %b) |
| 116 | Dereference, |
| 117 | |
| 118 | // The edge used when our edge goes from a value to a handle that may have |
| 119 | // contained it at some point. Examples: |
| 120 | // %b = load %a (%a Reference %b) |
| 121 | // %b = extractelement %a, 0 (%b Reference %a) |
| 122 | Reference |
| 123 | }; |
| 124 | |
| 125 | // \brief Encodes the notion of a "use" |
| 126 | struct Edge { |
| 127 | // \brief Which value the edge is coming from |
| 128 | Value *From; |
| 129 | |
| 130 | // \brief Which value the edge is pointing to |
| 131 | Value *To; |
| 132 | |
| 133 | // \brief Edge weight |
| 134 | EdgeType Weight; |
| 135 | |
| 136 | // \brief Whether we aliased any external values along the way that may be |
| 137 | // invisible to the analysis (i.e. landingpad for exceptions, calls for |
| 138 | // interprocedural analysis, etc.) |
| 139 | StratifiedAttrs AdditionalAttrs; |
| 140 | |
| 141 | Edge(Value *From, Value *To, EdgeType W, StratifiedAttrs A) |
| 142 | : From(From), To(To), Weight(W), AdditionalAttrs(A) {} |
| 143 | }; |
| 144 | |
| 145 | // \brief Information we have about a function and would like to keep around |
| 146 | struct FunctionInfo { |
| 147 | StratifiedSets<Value *> Sets; |
| 148 | // Lots of functions have < 4 returns. Adjust as necessary. |
| 149 | SmallVector<Value *, 4> ReturnedValues; |
Hal Finkel | 85f2692 | 2014-09-03 00:06:47 +0000 | [diff] [blame] | 150 | |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 151 | FunctionInfo(StratifiedSets<Value *> &&S, SmallVector<Value *, 4> &&RV) |
| 152 | : Sets(std::move(S)), ReturnedValues(std::move(RV)) {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | struct CFLAliasAnalysis; |
| 156 | |
David Blaikie | 7f1e056 | 2015-03-03 21:18:16 +0000 | [diff] [blame] | 157 | struct FunctionHandle : public CallbackVH { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 158 | FunctionHandle(Function *Fn, CFLAliasAnalysis *CFLAA) |
| 159 | : CallbackVH(Fn), CFLAA(CFLAA) { |
| 160 | assert(Fn != nullptr); |
| 161 | assert(CFLAA != nullptr); |
| 162 | } |
| 163 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 164 | ~FunctionHandle() override {} |
David Blaikie | 7f1e056 | 2015-03-03 21:18:16 +0000 | [diff] [blame] | 165 | |
David Blaikie | 711cd9c | 2014-11-14 19:06:36 +0000 | [diff] [blame] | 166 | void deleted() override { removeSelfFromCache(); } |
| 167 | void allUsesReplacedWith(Value *) override { removeSelfFromCache(); } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | CFLAliasAnalysis *CFLAA; |
| 171 | |
| 172 | void removeSelfFromCache(); |
| 173 | }; |
| 174 | |
| 175 | struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis { |
| 176 | private: |
| 177 | /// \brief Cached mapping of Functions to their StratifiedSets. |
| 178 | /// If a function's sets are currently being built, it is marked |
| 179 | /// in the cache as an Optional without a value. This way, if we |
| 180 | /// have any kind of recursion, it is discernable from a function |
| 181 | /// that simply has empty sets. |
| 182 | DenseMap<Function *, Optional<FunctionInfo>> Cache; |
| 183 | std::forward_list<FunctionHandle> Handles; |
| 184 | |
| 185 | public: |
| 186 | static char ID; |
| 187 | |
| 188 | CFLAliasAnalysis() : ImmutablePass(ID) { |
| 189 | initializeCFLAliasAnalysisPass(*PassRegistry::getPassRegistry()); |
| 190 | } |
| 191 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 192 | ~CFLAliasAnalysis() override {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 193 | |
Argyrios Kyrtzidis | 0b9f550 | 2014-10-01 21:00:44 +0000 | [diff] [blame] | 194 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 195 | AliasAnalysis::getAnalysisUsage(AU); |
| 196 | } |
| 197 | |
| 198 | void *getAdjustedAnalysisPointer(const void *ID) override { |
| 199 | if (ID == &AliasAnalysis::ID) |
| 200 | return (AliasAnalysis *)this; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | /// \brief Inserts the given Function into the cache. |
| 205 | void scan(Function *Fn); |
| 206 | |
| 207 | void evict(Function *Fn) { Cache.erase(Fn); } |
| 208 | |
| 209 | /// \brief Ensures that the given function is available in the cache. |
| 210 | /// Returns the appropriate entry from the cache. |
| 211 | const Optional<FunctionInfo> &ensureCached(Function *Fn) { |
| 212 | auto Iter = Cache.find(Fn); |
| 213 | if (Iter == Cache.end()) { |
| 214 | scan(Fn); |
| 215 | Iter = Cache.find(Fn); |
| 216 | assert(Iter != Cache.end()); |
| 217 | assert(Iter->second.hasValue()); |
| 218 | } |
| 219 | return Iter->second; |
| 220 | } |
| 221 | |
| 222 | AliasResult query(const Location &LocA, const Location &LocB); |
| 223 | |
| 224 | AliasResult alias(const Location &LocA, const Location &LocB) override { |
| 225 | if (LocA.Ptr == LocB.Ptr) { |
| 226 | if (LocA.Size == LocB.Size) { |
| 227 | return MustAlias; |
| 228 | } else { |
| 229 | return PartialAlias; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Comparisons between global variables and other constants should be |
| 234 | // handled by BasicAA. |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 235 | // TODO: ConstantExpr handling -- CFLAA may report NoAlias when comparing |
| 236 | // a GlobalValue and ConstantExpr, but every query needs to have at least |
| 237 | // one Value tied to a Function, and neither GlobalValues nor ConstantExprs |
| 238 | // are. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 239 | if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr)) { |
Daniel Berlin | 8f10e38 | 2015-01-26 17:30:39 +0000 | [diff] [blame] | 240 | return AliasAnalysis::alias(LocA, LocB); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 241 | } |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 242 | |
Daniel Berlin | 8f10e38 | 2015-01-26 17:30:39 +0000 | [diff] [blame] | 243 | AliasResult QueryResult = query(LocA, LocB); |
| 244 | if (QueryResult == MayAlias) |
| 245 | return AliasAnalysis::alias(LocA, LocB); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 246 | |
Daniel Berlin | 8f10e38 | 2015-01-26 17:30:39 +0000 | [diff] [blame] | 247 | return QueryResult; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 250 | bool doInitialization(Module &M) override; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | void FunctionHandle::removeSelfFromCache() { |
| 254 | assert(CFLAA != nullptr); |
| 255 | auto *Val = getValPtr(); |
| 256 | CFLAA->evict(cast<Function>(Val)); |
| 257 | setValPtr(nullptr); |
| 258 | } |
| 259 | |
| 260 | // \brief Gets the edges our graph should have, based on an Instruction* |
| 261 | class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> { |
| 262 | CFLAliasAnalysis &AA; |
| 263 | SmallVectorImpl<Edge> &Output; |
| 264 | |
| 265 | public: |
| 266 | GetEdgesVisitor(CFLAliasAnalysis &AA, SmallVectorImpl<Edge> &Output) |
| 267 | : AA(AA), Output(Output) {} |
| 268 | |
| 269 | void visitInstruction(Instruction &) { |
| 270 | llvm_unreachable("Unsupported instruction encountered"); |
| 271 | } |
| 272 | |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 273 | void visitPtrToIntInst(PtrToIntInst &Inst) { |
| 274 | auto *Ptr = Inst.getOperand(0); |
| 275 | Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown)); |
| 276 | } |
| 277 | |
| 278 | void visitIntToPtrInst(IntToPtrInst &Inst) { |
| 279 | auto *Ptr = &Inst; |
| 280 | Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown)); |
| 281 | } |
| 282 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 283 | void visitCastInst(CastInst &Inst) { |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 284 | Output.push_back( |
| 285 | Edge(&Inst, Inst.getOperand(0), EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void visitBinaryOperator(BinaryOperator &Inst) { |
| 289 | auto *Op1 = Inst.getOperand(0); |
| 290 | auto *Op2 = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 291 | Output.push_back(Edge(&Inst, Op1, EdgeType::Assign, AttrNone)); |
| 292 | Output.push_back(Edge(&Inst, Op2, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 296 | auto *Ptr = Inst.getPointerOperand(); |
| 297 | auto *Val = Inst.getNewValOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 298 | Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 302 | auto *Ptr = Inst.getPointerOperand(); |
| 303 | auto *Val = Inst.getValOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 304 | Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void visitPHINode(PHINode &Inst) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame^] | 308 | for (Value *Val : Inst.incoming_values()) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 309 | Output.push_back(Edge(&Inst, Val, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | void visitGetElementPtrInst(GetElementPtrInst &Inst) { |
| 314 | auto *Op = Inst.getPointerOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 315 | Output.push_back(Edge(&Inst, Op, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 316 | for (auto I = Inst.idx_begin(), E = Inst.idx_end(); I != E; ++I) |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 317 | Output.push_back(Edge(&Inst, *I, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void visitSelectInst(SelectInst &Inst) { |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 321 | // Condition is not processed here (The actual statement producing |
| 322 | // the condition result is processed elsewhere). For select, the |
| 323 | // condition is evaluated, but not loaded, stored, or assigned |
| 324 | // simply as a result of being the condition of a select. |
| 325 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 326 | auto *TrueVal = Inst.getTrueValue(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 327 | Output.push_back(Edge(&Inst, TrueVal, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 328 | auto *FalseVal = Inst.getFalseValue(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 329 | Output.push_back(Edge(&Inst, FalseVal, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void visitAllocaInst(AllocaInst &) {} |
| 333 | |
| 334 | void visitLoadInst(LoadInst &Inst) { |
| 335 | auto *Ptr = Inst.getPointerOperand(); |
| 336 | auto *Val = &Inst; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 337 | Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void visitStoreInst(StoreInst &Inst) { |
| 341 | auto *Ptr = Inst.getPointerOperand(); |
| 342 | auto *Val = Inst.getValueOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 343 | Output.push_back(Edge(Ptr, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 346 | void visitVAArgInst(VAArgInst &Inst) { |
| 347 | // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does |
| 348 | // two things: |
| 349 | // 1. Loads a value from *((T*)*Ptr). |
| 350 | // 2. Increments (stores to) *Ptr by some target-specific amount. |
| 351 | // For now, we'll handle this like a landingpad instruction (by placing the |
| 352 | // result in its own group, and having that group alias externals). |
| 353 | auto *Val = &Inst; |
| 354 | Output.push_back(Edge(Val, Val, EdgeType::Assign, AttrAll)); |
| 355 | } |
| 356 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 357 | static bool isFunctionExternal(Function *Fn) { |
| 358 | return Fn->isDeclaration() || !Fn->hasLocalLinkage(); |
| 359 | } |
| 360 | |
| 361 | // Gets whether the sets at Index1 above, below, or equal to the sets at |
| 362 | // Index2. Returns None if they are not in the same set chain. |
| 363 | static Optional<Level> getIndexRelation(const StratifiedSets<Value *> &Sets, |
| 364 | StratifiedIndex Index1, |
| 365 | StratifiedIndex Index2) { |
| 366 | if (Index1 == Index2) |
| 367 | return Level::Same; |
| 368 | |
| 369 | const auto *Current = &Sets.getLink(Index1); |
| 370 | while (Current->hasBelow()) { |
| 371 | if (Current->Below == Index2) |
| 372 | return Level::Below; |
| 373 | Current = &Sets.getLink(Current->Below); |
| 374 | } |
| 375 | |
| 376 | Current = &Sets.getLink(Index1); |
| 377 | while (Current->hasAbove()) { |
| 378 | if (Current->Above == Index2) |
| 379 | return Level::Above; |
| 380 | Current = &Sets.getLink(Current->Above); |
| 381 | } |
| 382 | |
| 383 | return NoneType(); |
| 384 | } |
| 385 | |
| 386 | bool |
| 387 | tryInterproceduralAnalysis(const SmallVectorImpl<Function *> &Fns, |
| 388 | Value *FuncValue, |
| 389 | const iterator_range<User::op_iterator> &Args) { |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 390 | const unsigned ExpectedMaxArgs = 8; |
| 391 | const unsigned MaxSupportedArgs = 50; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 392 | assert(Fns.size() > 0); |
| 393 | |
| 394 | // I put this here to give us an upper bound on time taken by IPA. Is it |
| 395 | // really (realistically) needed? Keep in mind that we do have an n^2 algo. |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 396 | if (std::distance(Args.begin(), Args.end()) > (int)MaxSupportedArgs) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 397 | return false; |
| 398 | |
| 399 | // Exit early if we'll fail anyway |
| 400 | for (auto *Fn : Fns) { |
| 401 | if (isFunctionExternal(Fn) || Fn->isVarArg()) |
| 402 | return false; |
| 403 | auto &MaybeInfo = AA.ensureCached(Fn); |
| 404 | if (!MaybeInfo.hasValue()) |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | SmallVector<Value *, ExpectedMaxArgs> Arguments(Args.begin(), Args.end()); |
| 409 | SmallVector<StratifiedInfo, ExpectedMaxArgs> Parameters; |
| 410 | for (auto *Fn : Fns) { |
| 411 | auto &Info = *AA.ensureCached(Fn); |
| 412 | auto &Sets = Info.Sets; |
| 413 | auto &RetVals = Info.ReturnedValues; |
| 414 | |
| 415 | Parameters.clear(); |
| 416 | for (auto &Param : Fn->args()) { |
| 417 | auto MaybeInfo = Sets.find(&Param); |
| 418 | // Did a new parameter somehow get added to the function/slip by? |
| 419 | if (!MaybeInfo.hasValue()) |
| 420 | return false; |
| 421 | Parameters.push_back(*MaybeInfo); |
| 422 | } |
| 423 | |
| 424 | // Adding an edge from argument -> return value for each parameter that |
| 425 | // may alias the return value |
| 426 | for (unsigned I = 0, E = Parameters.size(); I != E; ++I) { |
| 427 | auto &ParamInfo = Parameters[I]; |
| 428 | auto &ArgVal = Arguments[I]; |
| 429 | bool AddEdge = false; |
| 430 | StratifiedAttrs Externals; |
| 431 | for (unsigned X = 0, XE = RetVals.size(); X != XE; ++X) { |
| 432 | auto MaybeInfo = Sets.find(RetVals[X]); |
| 433 | if (!MaybeInfo.hasValue()) |
| 434 | return false; |
| 435 | |
| 436 | auto &RetInfo = *MaybeInfo; |
| 437 | auto RetAttrs = Sets.getLink(RetInfo.Index).Attrs; |
| 438 | auto ParamAttrs = Sets.getLink(ParamInfo.Index).Attrs; |
| 439 | auto MaybeRelation = |
| 440 | getIndexRelation(Sets, ParamInfo.Index, RetInfo.Index); |
| 441 | if (MaybeRelation.hasValue()) { |
| 442 | AddEdge = true; |
| 443 | Externals |= RetAttrs | ParamAttrs; |
| 444 | } |
| 445 | } |
| 446 | if (AddEdge) |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 447 | Output.push_back(Edge(FuncValue, ArgVal, EdgeType::Assign, |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 448 | StratifiedAttrs().flip())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | if (Parameters.size() != Arguments.size()) |
| 452 | return false; |
| 453 | |
| 454 | // Adding edges between arguments for arguments that may end up aliasing |
| 455 | // each other. This is necessary for functions such as |
| 456 | // void foo(int** a, int** b) { *a = *b; } |
| 457 | // (Technically, the proper sets for this would be those below |
| 458 | // Arguments[I] and Arguments[X], but our algorithm will produce |
| 459 | // extremely similar, and equally correct, results either way) |
| 460 | for (unsigned I = 0, E = Arguments.size(); I != E; ++I) { |
| 461 | auto &MainVal = Arguments[I]; |
| 462 | auto &MainInfo = Parameters[I]; |
| 463 | auto &MainAttrs = Sets.getLink(MainInfo.Index).Attrs; |
| 464 | for (unsigned X = I + 1; X != E; ++X) { |
| 465 | auto &SubInfo = Parameters[X]; |
| 466 | auto &SubVal = Arguments[X]; |
| 467 | auto &SubAttrs = Sets.getLink(SubInfo.Index).Attrs; |
| 468 | auto MaybeRelation = |
| 469 | getIndexRelation(Sets, MainInfo.Index, SubInfo.Index); |
| 470 | |
| 471 | if (!MaybeRelation.hasValue()) |
| 472 | continue; |
| 473 | |
| 474 | auto NewAttrs = SubAttrs | MainAttrs; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 475 | Output.push_back(Edge(MainVal, SubVal, EdgeType::Assign, NewAttrs)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | template <typename InstT> void visitCallLikeInst(InstT &Inst) { |
| 483 | SmallVector<Function *, 4> Targets; |
| 484 | if (getPossibleTargets(&Inst, Targets)) { |
| 485 | if (tryInterproceduralAnalysis(Targets, &Inst, Inst.arg_operands())) |
| 486 | return; |
| 487 | // Cleanup from interprocedural analysis |
| 488 | Output.clear(); |
| 489 | } |
| 490 | |
| 491 | for (Value *V : Inst.arg_operands()) |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 492 | Output.push_back(Edge(&Inst, V, EdgeType::Assign, AttrAll)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void visitCallInst(CallInst &Inst) { visitCallLikeInst(Inst); } |
| 496 | |
| 497 | void visitInvokeInst(InvokeInst &Inst) { visitCallLikeInst(Inst); } |
| 498 | |
| 499 | // Because vectors/aggregates are immutable and unaddressable, |
| 500 | // there's nothing we can do to coax a value out of them, other |
| 501 | // than calling Extract{Element,Value}. We can effectively treat |
| 502 | // them as pointers to arbitrary memory locations we can store in |
| 503 | // and load from. |
| 504 | void visitExtractElementInst(ExtractElementInst &Inst) { |
| 505 | auto *Ptr = Inst.getVectorOperand(); |
| 506 | auto *Val = &Inst; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 507 | Output.push_back(Edge(Val, Ptr, EdgeType::Reference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void visitInsertElementInst(InsertElementInst &Inst) { |
| 511 | auto *Vec = Inst.getOperand(0); |
| 512 | auto *Val = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 513 | Output.push_back(Edge(&Inst, Vec, EdgeType::Assign, AttrNone)); |
| 514 | Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void visitLandingPadInst(LandingPadInst &Inst) { |
| 518 | // Exceptions come from "nowhere", from our analysis' perspective. |
| 519 | // So we place the instruction its own group, noting that said group may |
| 520 | // alias externals |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 521 | Output.push_back(Edge(&Inst, &Inst, EdgeType::Assign, AttrAll)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void visitInsertValueInst(InsertValueInst &Inst) { |
| 525 | auto *Agg = Inst.getOperand(0); |
| 526 | auto *Val = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 527 | Output.push_back(Edge(&Inst, Agg, EdgeType::Assign, AttrNone)); |
| 528 | Output.push_back(Edge(&Inst, Val, EdgeType::Dereference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | void visitExtractValueInst(ExtractValueInst &Inst) { |
| 532 | auto *Ptr = Inst.getAggregateOperand(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 533 | Output.push_back(Edge(&Inst, Ptr, EdgeType::Reference, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void visitShuffleVectorInst(ShuffleVectorInst &Inst) { |
| 537 | auto *From1 = Inst.getOperand(0); |
| 538 | auto *From2 = Inst.getOperand(1); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 539 | Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone)); |
| 540 | Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 541 | } |
| 542 | }; |
| 543 | |
| 544 | // For a given instruction, we need to know which Value* to get the |
| 545 | // users of in order to build our graph. In some cases (i.e. add), |
| 546 | // we simply need the Instruction*. In other cases (i.e. store), |
| 547 | // finding the users of the Instruction* is useless; we need to find |
| 548 | // the users of the first operand. This handles determining which |
| 549 | // value to follow for us. |
| 550 | // |
| 551 | // Note: we *need* to keep this in sync with GetEdgesVisitor. Add |
| 552 | // something to GetEdgesVisitor, add it here -- remove something from |
| 553 | // GetEdgesVisitor, remove it here. |
| 554 | class GetTargetValueVisitor |
| 555 | : public InstVisitor<GetTargetValueVisitor, Value *> { |
| 556 | public: |
| 557 | Value *visitInstruction(Instruction &Inst) { return &Inst; } |
| 558 | |
| 559 | Value *visitStoreInst(StoreInst &Inst) { return Inst.getPointerOperand(); } |
| 560 | |
| 561 | Value *visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 562 | return Inst.getPointerOperand(); |
| 563 | } |
| 564 | |
| 565 | Value *visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 566 | return Inst.getPointerOperand(); |
| 567 | } |
| 568 | |
| 569 | Value *visitInsertElementInst(InsertElementInst &Inst) { |
| 570 | return Inst.getOperand(0); |
| 571 | } |
| 572 | |
| 573 | Value *visitInsertValueInst(InsertValueInst &Inst) { |
| 574 | return Inst.getAggregateOperand(); |
| 575 | } |
| 576 | }; |
| 577 | |
| 578 | // Set building requires a weighted bidirectional graph. |
| 579 | template <typename EdgeTypeT> class WeightedBidirectionalGraph { |
| 580 | public: |
| 581 | typedef std::size_t Node; |
| 582 | |
| 583 | private: |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 584 | const static Node StartNode = Node(0); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 585 | |
| 586 | struct Edge { |
| 587 | EdgeTypeT Weight; |
| 588 | Node Other; |
| 589 | |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 590 | Edge(const EdgeTypeT &W, const Node &N) : Weight(W), Other(N) {} |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 591 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 592 | bool operator==(const Edge &E) const { |
| 593 | return Weight == E.Weight && Other == E.Other; |
| 594 | } |
| 595 | |
| 596 | bool operator!=(const Edge &E) const { return !operator==(E); } |
| 597 | }; |
| 598 | |
| 599 | struct NodeImpl { |
| 600 | std::vector<Edge> Edges; |
| 601 | }; |
| 602 | |
| 603 | std::vector<NodeImpl> NodeImpls; |
| 604 | |
| 605 | bool inbounds(Node NodeIndex) const { return NodeIndex < NodeImpls.size(); } |
| 606 | |
| 607 | const NodeImpl &getNode(Node N) const { return NodeImpls[N]; } |
| 608 | NodeImpl &getNode(Node N) { return NodeImpls[N]; } |
| 609 | |
| 610 | public: |
| 611 | // ----- Various Edge iterators for the graph ----- // |
| 612 | |
| 613 | // \brief Iterator for edges. Because this graph is bidirected, we don't |
| 614 | // allow modificaiton of the edges using this iterator. Additionally, the |
| 615 | // iterator becomes invalid if you add edges to or from the node you're |
| 616 | // getting the edges of. |
| 617 | struct EdgeIterator : public std::iterator<std::forward_iterator_tag, |
| 618 | std::tuple<EdgeTypeT, Node *>> { |
| 619 | EdgeIterator(const typename std::vector<Edge>::const_iterator &Iter) |
| 620 | : Current(Iter) {} |
| 621 | |
| 622 | EdgeIterator(NodeImpl &Impl) : Current(Impl.begin()) {} |
| 623 | |
| 624 | EdgeIterator &operator++() { |
| 625 | ++Current; |
| 626 | return *this; |
| 627 | } |
| 628 | |
| 629 | EdgeIterator operator++(int) { |
| 630 | EdgeIterator Copy(Current); |
| 631 | operator++(); |
| 632 | return Copy; |
| 633 | } |
| 634 | |
| 635 | std::tuple<EdgeTypeT, Node> &operator*() { |
| 636 | Store = std::make_tuple(Current->Weight, Current->Other); |
| 637 | return Store; |
| 638 | } |
| 639 | |
| 640 | bool operator==(const EdgeIterator &Other) const { |
| 641 | return Current == Other.Current; |
| 642 | } |
| 643 | |
| 644 | bool operator!=(const EdgeIterator &Other) const { |
| 645 | return !operator==(Other); |
| 646 | } |
| 647 | |
| 648 | private: |
| 649 | typename std::vector<Edge>::const_iterator Current; |
| 650 | std::tuple<EdgeTypeT, Node> Store; |
| 651 | }; |
| 652 | |
| 653 | // Wrapper for EdgeIterator with begin()/end() calls. |
| 654 | struct EdgeIterable { |
| 655 | EdgeIterable(const std::vector<Edge> &Edges) |
| 656 | : BeginIter(Edges.begin()), EndIter(Edges.end()) {} |
| 657 | |
| 658 | EdgeIterator begin() { return EdgeIterator(BeginIter); } |
| 659 | |
| 660 | EdgeIterator end() { return EdgeIterator(EndIter); } |
| 661 | |
| 662 | private: |
| 663 | typename std::vector<Edge>::const_iterator BeginIter; |
| 664 | typename std::vector<Edge>::const_iterator EndIter; |
| 665 | }; |
| 666 | |
| 667 | // ----- Actual graph-related things ----- // |
| 668 | |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 669 | WeightedBidirectionalGraph() {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 670 | |
| 671 | WeightedBidirectionalGraph(WeightedBidirectionalGraph<EdgeTypeT> &&Other) |
| 672 | : NodeImpls(std::move(Other.NodeImpls)) {} |
| 673 | |
| 674 | WeightedBidirectionalGraph<EdgeTypeT> & |
| 675 | operator=(WeightedBidirectionalGraph<EdgeTypeT> &&Other) { |
| 676 | NodeImpls = std::move(Other.NodeImpls); |
| 677 | return *this; |
| 678 | } |
| 679 | |
| 680 | Node addNode() { |
| 681 | auto Index = NodeImpls.size(); |
| 682 | auto NewNode = Node(Index); |
| 683 | NodeImpls.push_back(NodeImpl()); |
| 684 | return NewNode; |
| 685 | } |
| 686 | |
| 687 | void addEdge(Node From, Node To, const EdgeTypeT &Weight, |
| 688 | const EdgeTypeT &ReverseWeight) { |
| 689 | assert(inbounds(From)); |
| 690 | assert(inbounds(To)); |
| 691 | auto &FromNode = getNode(From); |
| 692 | auto &ToNode = getNode(To); |
Hal Finkel | ca616ac | 2014-09-02 23:29:48 +0000 | [diff] [blame] | 693 | FromNode.Edges.push_back(Edge(Weight, To)); |
| 694 | ToNode.Edges.push_back(Edge(ReverseWeight, From)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | EdgeIterable edgesFor(const Node &N) const { |
| 698 | const auto &Node = getNode(N); |
| 699 | return EdgeIterable(Node.Edges); |
| 700 | } |
| 701 | |
| 702 | bool empty() const { return NodeImpls.empty(); } |
| 703 | std::size_t size() const { return NodeImpls.size(); } |
| 704 | |
| 705 | // \brief Gets an arbitrary node in the graph as a starting point for |
| 706 | // traversal. |
| 707 | Node getEntryNode() { |
| 708 | assert(inbounds(StartNode)); |
| 709 | return StartNode; |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | typedef WeightedBidirectionalGraph<std::pair<EdgeType, StratifiedAttrs>> GraphT; |
| 714 | typedef DenseMap<Value *, GraphT::Node> NodeMapT; |
| 715 | } |
| 716 | |
| 717 | // -- Setting up/registering CFLAA pass -- // |
| 718 | char CFLAliasAnalysis::ID = 0; |
| 719 | |
| 720 | INITIALIZE_AG_PASS(CFLAliasAnalysis, AliasAnalysis, "cfl-aa", |
| 721 | "CFL-Based AA implementation", false, true, false) |
| 722 | |
| 723 | ImmutablePass *llvm::createCFLAliasAnalysisPass() { |
| 724 | return new CFLAliasAnalysis(); |
| 725 | } |
| 726 | |
| 727 | //===----------------------------------------------------------------------===// |
| 728 | // Function declarations that require types defined in the namespace above |
| 729 | //===----------------------------------------------------------------------===// |
| 730 | |
| 731 | // Given an argument number, returns the appropriate Attr index to set. |
| 732 | static StratifiedAttr argNumberToAttrIndex(StratifiedAttr); |
| 733 | |
| 734 | // Given a Value, potentially return which AttrIndex it maps to. |
| 735 | static Optional<StratifiedAttr> valueToAttrIndex(Value *Val); |
| 736 | |
| 737 | // Gets the inverse of a given EdgeType. |
| 738 | static EdgeType flipWeight(EdgeType); |
| 739 | |
| 740 | // Gets edges of the given Instruction*, writing them to the SmallVector*. |
| 741 | static void argsToEdges(CFLAliasAnalysis &, Instruction *, |
| 742 | SmallVectorImpl<Edge> &); |
| 743 | |
| 744 | // Gets the "Level" that one should travel in StratifiedSets |
| 745 | // given an EdgeType. |
| 746 | static Level directionOfEdgeType(EdgeType); |
| 747 | |
| 748 | // Builds the graph needed for constructing the StratifiedSets for the |
| 749 | // given function |
| 750 | static void buildGraphFrom(CFLAliasAnalysis &, Function *, |
| 751 | SmallVectorImpl<Value *> &, NodeMapT &, GraphT &); |
| 752 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 753 | // Gets the edges of a ConstantExpr as if it was an Instruction. This |
| 754 | // function also acts on any nested ConstantExprs, adding the edges |
| 755 | // of those to the given SmallVector as well. |
| 756 | static void constexprToEdges(CFLAliasAnalysis &, ConstantExpr &, |
| 757 | SmallVectorImpl<Edge> &); |
| 758 | |
| 759 | // Given an Instruction, this will add it to the graph, along with any |
| 760 | // Instructions that are potentially only available from said Instruction |
| 761 | // For example, given the following line: |
| 762 | // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2 |
| 763 | // addInstructionToGraph would add both the `load` and `getelementptr` |
| 764 | // instructions to the graph appropriately. |
| 765 | static void addInstructionToGraph(CFLAliasAnalysis &, Instruction &, |
| 766 | SmallVectorImpl<Value *> &, NodeMapT &, |
| 767 | GraphT &); |
| 768 | |
| 769 | // Notes whether it would be pointless to add the given Value to our sets. |
| 770 | static bool canSkipAddingToSets(Value *Val); |
| 771 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 772 | // Builds the graph + StratifiedSets for a function. |
| 773 | static FunctionInfo buildSetsFrom(CFLAliasAnalysis &, Function *); |
| 774 | |
| 775 | static Optional<Function *> parentFunctionOfValue(Value *Val) { |
| 776 | if (auto *Inst = dyn_cast<Instruction>(Val)) { |
| 777 | auto *Bb = Inst->getParent(); |
| 778 | return Bb->getParent(); |
| 779 | } |
| 780 | |
| 781 | if (auto *Arg = dyn_cast<Argument>(Val)) |
| 782 | return Arg->getParent(); |
| 783 | return NoneType(); |
| 784 | } |
| 785 | |
| 786 | template <typename Inst> |
| 787 | static bool getPossibleTargets(Inst *Call, |
| 788 | SmallVectorImpl<Function *> &Output) { |
| 789 | if (auto *Fn = Call->getCalledFunction()) { |
| 790 | Output.push_back(Fn); |
| 791 | return true; |
| 792 | } |
| 793 | |
| 794 | // TODO: If the call is indirect, we might be able to enumerate all potential |
| 795 | // targets of the call and return them, rather than just failing. |
| 796 | return false; |
| 797 | } |
| 798 | |
| 799 | static Optional<Value *> getTargetValue(Instruction *Inst) { |
| 800 | GetTargetValueVisitor V; |
| 801 | return V.visit(Inst); |
| 802 | } |
| 803 | |
| 804 | static bool hasUsefulEdges(Instruction *Inst) { |
| 805 | bool IsNonInvokeTerminator = |
| 806 | isa<TerminatorInst>(Inst) && !isa<InvokeInst>(Inst); |
| 807 | return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator; |
| 808 | } |
| 809 | |
| 810 | static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) { |
| 811 | if (isa<GlobalValue>(Val)) |
| 812 | return AttrGlobalIndex; |
| 813 | |
| 814 | if (auto *Arg = dyn_cast<Argument>(Val)) |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 815 | // Only pointer arguments should have the argument attribute, |
| 816 | // because things can't escape through scalars without us seeing a |
| 817 | // cast, and thus, interaction with them doesn't matter. |
| 818 | if (!Arg->hasNoAliasAttr() && Arg->getType()->isPointerTy()) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 819 | return argNumberToAttrIndex(Arg->getArgNo()); |
| 820 | return NoneType(); |
| 821 | } |
| 822 | |
| 823 | static StratifiedAttr argNumberToAttrIndex(unsigned ArgNum) { |
George Burgess IV | 3c898c2 | 2015-01-21 16:37:21 +0000 | [diff] [blame] | 824 | if (ArgNum >= AttrMaxNumArgs) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 825 | return AttrAllIndex; |
| 826 | return ArgNum + AttrFirstArgIndex; |
| 827 | } |
| 828 | |
| 829 | static EdgeType flipWeight(EdgeType Initial) { |
| 830 | switch (Initial) { |
| 831 | case EdgeType::Assign: |
| 832 | return EdgeType::Assign; |
| 833 | case EdgeType::Dereference: |
| 834 | return EdgeType::Reference; |
| 835 | case EdgeType::Reference: |
| 836 | return EdgeType::Dereference; |
| 837 | } |
| 838 | llvm_unreachable("Incomplete coverage of EdgeType enum"); |
| 839 | } |
| 840 | |
| 841 | static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst, |
| 842 | SmallVectorImpl<Edge> &Output) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 843 | assert(hasUsefulEdges(Inst) && |
| 844 | "Expected instructions to have 'useful' edges"); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 845 | GetEdgesVisitor v(Analysis, Output); |
| 846 | v.visit(Inst); |
| 847 | } |
| 848 | |
| 849 | static Level directionOfEdgeType(EdgeType Weight) { |
| 850 | switch (Weight) { |
| 851 | case EdgeType::Reference: |
| 852 | return Level::Above; |
| 853 | case EdgeType::Dereference: |
| 854 | return Level::Below; |
| 855 | case EdgeType::Assign: |
| 856 | return Level::Same; |
| 857 | } |
| 858 | llvm_unreachable("Incomplete switch coverage"); |
| 859 | } |
| 860 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 861 | static void constexprToEdges(CFLAliasAnalysis &Analysis, |
| 862 | ConstantExpr &CExprToCollapse, |
| 863 | SmallVectorImpl<Edge> &Results) { |
| 864 | SmallVector<ConstantExpr *, 4> Worklist; |
| 865 | Worklist.push_back(&CExprToCollapse); |
| 866 | |
| 867 | SmallVector<Edge, 8> ConstexprEdges; |
| 868 | while (!Worklist.empty()) { |
| 869 | auto *CExpr = Worklist.pop_back_val(); |
| 870 | std::unique_ptr<Instruction> Inst(CExpr->getAsInstruction()); |
| 871 | |
| 872 | if (!hasUsefulEdges(Inst.get())) |
| 873 | continue; |
| 874 | |
| 875 | ConstexprEdges.clear(); |
| 876 | argsToEdges(Analysis, Inst.get(), ConstexprEdges); |
| 877 | for (auto &Edge : ConstexprEdges) { |
| 878 | if (Edge.From == Inst.get()) |
| 879 | Edge.From = CExpr; |
| 880 | else if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From)) |
| 881 | Worklist.push_back(Nested); |
| 882 | |
| 883 | if (Edge.To == Inst.get()) |
| 884 | Edge.To = CExpr; |
| 885 | else if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To)) |
| 886 | Worklist.push_back(Nested); |
| 887 | } |
| 888 | |
| 889 | Results.append(ConstexprEdges.begin(), ConstexprEdges.end()); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | static void addInstructionToGraph(CFLAliasAnalysis &Analysis, Instruction &Inst, |
| 894 | SmallVectorImpl<Value *> &ReturnedValues, |
| 895 | NodeMapT &Map, GraphT &Graph) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 896 | const auto findOrInsertNode = [&Map, &Graph](Value *Val) { |
| 897 | auto Pair = Map.insert(std::make_pair(Val, GraphT::Node())); |
| 898 | auto &Iter = Pair.first; |
| 899 | if (Pair.second) { |
| 900 | auto NewNode = Graph.addNode(); |
| 901 | Iter->second = NewNode; |
| 902 | } |
| 903 | return Iter->second; |
| 904 | }; |
| 905 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 906 | // We don't want the edges of most "return" instructions, but we *do* want |
| 907 | // to know what can be returned. |
| 908 | if (isa<ReturnInst>(&Inst)) |
| 909 | ReturnedValues.push_back(&Inst); |
| 910 | |
| 911 | if (!hasUsefulEdges(&Inst)) |
| 912 | return; |
| 913 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 914 | SmallVector<Edge, 8> Edges; |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 915 | argsToEdges(Analysis, &Inst, Edges); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 916 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 917 | // In the case of an unused alloca (or similar), edges may be empty. Note |
| 918 | // that it exists so we can potentially answer NoAlias. |
| 919 | if (Edges.empty()) { |
| 920 | auto MaybeVal = getTargetValue(&Inst); |
| 921 | assert(MaybeVal.hasValue()); |
| 922 | auto *Target = *MaybeVal; |
| 923 | findOrInsertNode(Target); |
| 924 | return; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 925 | } |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 926 | |
| 927 | const auto addEdgeToGraph = [&Graph, &findOrInsertNode](const Edge &E) { |
| 928 | auto To = findOrInsertNode(E.To); |
| 929 | auto From = findOrInsertNode(E.From); |
| 930 | auto FlippedWeight = flipWeight(E.Weight); |
| 931 | auto Attrs = E.AdditionalAttrs; |
| 932 | Graph.addEdge(From, To, std::make_pair(E.Weight, Attrs), |
| 933 | std::make_pair(FlippedWeight, Attrs)); |
| 934 | }; |
| 935 | |
| 936 | SmallVector<ConstantExpr *, 4> ConstantExprs; |
| 937 | for (const Edge &E : Edges) { |
| 938 | addEdgeToGraph(E); |
| 939 | if (auto *Constexpr = dyn_cast<ConstantExpr>(E.To)) |
| 940 | ConstantExprs.push_back(Constexpr); |
| 941 | if (auto *Constexpr = dyn_cast<ConstantExpr>(E.From)) |
| 942 | ConstantExprs.push_back(Constexpr); |
| 943 | } |
| 944 | |
| 945 | for (ConstantExpr *CE : ConstantExprs) { |
| 946 | Edges.clear(); |
| 947 | constexprToEdges(Analysis, *CE, Edges); |
| 948 | std::for_each(Edges.begin(), Edges.end(), addEdgeToGraph); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | // Aside: We may remove graph construction entirely, because it doesn't really |
| 953 | // buy us much that we don't already have. I'd like to add interprocedural |
| 954 | // analysis prior to this however, in case that somehow requires the graph |
| 955 | // produced by this for efficient execution |
| 956 | static void buildGraphFrom(CFLAliasAnalysis &Analysis, Function *Fn, |
| 957 | SmallVectorImpl<Value *> &ReturnedValues, |
| 958 | NodeMapT &Map, GraphT &Graph) { |
| 959 | for (auto &Bb : Fn->getBasicBlockList()) |
| 960 | for (auto &Inst : Bb.getInstList()) |
| 961 | addInstructionToGraph(Analysis, Inst, ReturnedValues, Map, Graph); |
| 962 | } |
| 963 | |
| 964 | static bool canSkipAddingToSets(Value *Val) { |
| 965 | // Constants can share instances, which may falsely unify multiple |
| 966 | // sets, e.g. in |
| 967 | // store i32* null, i32** %ptr1 |
| 968 | // store i32* null, i32** %ptr2 |
| 969 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 970 | // we should filter out the (potentially shared) instance to |
| 971 | // i32* null. |
| 972 | if (isa<Constant>(Val)) { |
| 973 | bool Container = isa<ConstantVector>(Val) || isa<ConstantArray>(Val) || |
| 974 | isa<ConstantStruct>(Val); |
| 975 | // TODO: Because all of these things are constant, we can determine whether |
| 976 | // the data is *actually* mutable at graph building time. This will probably |
| 977 | // come for free/cheap with offset awareness. |
| 978 | bool CanStoreMutableData = |
| 979 | isa<GlobalValue>(Val) || isa<ConstantExpr>(Val) || Container; |
| 980 | return !CanStoreMutableData; |
| 981 | } |
| 982 | |
| 983 | return false; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static FunctionInfo buildSetsFrom(CFLAliasAnalysis &Analysis, Function *Fn) { |
| 987 | NodeMapT Map; |
| 988 | GraphT Graph; |
| 989 | SmallVector<Value *, 4> ReturnedValues; |
| 990 | |
| 991 | buildGraphFrom(Analysis, Fn, ReturnedValues, Map, Graph); |
| 992 | |
| 993 | DenseMap<GraphT::Node, Value *> NodeValueMap; |
| 994 | NodeValueMap.resize(Map.size()); |
| 995 | for (const auto &Pair : Map) |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 996 | NodeValueMap.insert(std::make_pair(Pair.second, Pair.first)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 997 | |
| 998 | const auto findValueOrDie = [&NodeValueMap](GraphT::Node Node) { |
| 999 | auto ValIter = NodeValueMap.find(Node); |
| 1000 | assert(ValIter != NodeValueMap.end()); |
| 1001 | return ValIter->second; |
| 1002 | }; |
| 1003 | |
| 1004 | StratifiedSetsBuilder<Value *> Builder; |
| 1005 | |
| 1006 | SmallVector<GraphT::Node, 16> Worklist; |
| 1007 | for (auto &Pair : Map) { |
| 1008 | Worklist.clear(); |
| 1009 | |
| 1010 | auto *Value = Pair.first; |
| 1011 | Builder.add(Value); |
| 1012 | auto InitialNode = Pair.second; |
| 1013 | Worklist.push_back(InitialNode); |
| 1014 | while (!Worklist.empty()) { |
| 1015 | auto Node = Worklist.pop_back_val(); |
| 1016 | auto *CurValue = findValueOrDie(Node); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 1017 | if (canSkipAddingToSets(CurValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1018 | continue; |
| 1019 | |
| 1020 | for (const auto &EdgeTuple : Graph.edgesFor(Node)) { |
| 1021 | auto Weight = std::get<0>(EdgeTuple); |
| 1022 | auto Label = Weight.first; |
| 1023 | auto &OtherNode = std::get<1>(EdgeTuple); |
| 1024 | auto *OtherValue = findValueOrDie(OtherNode); |
| 1025 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 1026 | if (canSkipAddingToSets(OtherValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1027 | continue; |
| 1028 | |
| 1029 | bool Added; |
| 1030 | switch (directionOfEdgeType(Label)) { |
| 1031 | case Level::Above: |
| 1032 | Added = Builder.addAbove(CurValue, OtherValue); |
| 1033 | break; |
| 1034 | case Level::Below: |
| 1035 | Added = Builder.addBelow(CurValue, OtherValue); |
| 1036 | break; |
| 1037 | case Level::Same: |
| 1038 | Added = Builder.addWith(CurValue, OtherValue); |
| 1039 | break; |
| 1040 | } |
| 1041 | |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 1042 | auto Aliasing = Weight.second; |
| 1043 | if (auto MaybeCurIndex = valueToAttrIndex(CurValue)) |
| 1044 | Aliasing.set(*MaybeCurIndex); |
| 1045 | if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue)) |
| 1046 | Aliasing.set(*MaybeOtherIndex); |
| 1047 | Builder.noteAttributes(CurValue, Aliasing); |
| 1048 | Builder.noteAttributes(OtherValue, Aliasing); |
| 1049 | |
| 1050 | if (Added) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1051 | Worklist.push_back(OtherNode); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | // There are times when we end up with parameters not in our graph (i.e. if |
| 1057 | // it's only used as the condition of a branch). Other bits of code depend on |
| 1058 | // things that were present during construction being present in the graph. |
| 1059 | // So, we add all present arguments here. |
| 1060 | for (auto &Arg : Fn->args()) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 1061 | if (!Builder.add(&Arg)) |
| 1062 | continue; |
| 1063 | |
| 1064 | auto Attrs = valueToAttrIndex(&Arg); |
| 1065 | if (Attrs.hasValue()) |
| 1066 | Builder.noteAttributes(&Arg, *Attrs); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Hal Finkel | 85f2692 | 2014-09-03 00:06:47 +0000 | [diff] [blame] | 1069 | return FunctionInfo(Builder.build(), std::move(ReturnedValues)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | void CFLAliasAnalysis::scan(Function *Fn) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 1073 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1074 | (void)InsertPair; |
| 1075 | assert(InsertPair.second && |
| 1076 | "Trying to scan a function that has already been cached"); |
| 1077 | |
| 1078 | FunctionInfo Info(buildSetsFrom(*this, Fn)); |
| 1079 | Cache[Fn] = std::move(Info); |
| 1080 | Handles.push_front(FunctionHandle(Fn, this)); |
| 1081 | } |
| 1082 | |
| 1083 | AliasAnalysis::AliasResult |
| 1084 | CFLAliasAnalysis::query(const AliasAnalysis::Location &LocA, |
| 1085 | const AliasAnalysis::Location &LocB) { |
| 1086 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 1087 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 1088 | |
| 1089 | Function *Fn = nullptr; |
| 1090 | auto MaybeFnA = parentFunctionOfValue(ValA); |
| 1091 | auto MaybeFnB = parentFunctionOfValue(ValB); |
| 1092 | if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) { |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1093 | // The only times this is known to happen are when globals + InlineAsm |
| 1094 | // are involved |
| 1095 | DEBUG(dbgs() << "CFLAA: could not extract parent function information.\n"); |
| 1096 | return AliasAnalysis::MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | if (MaybeFnA.hasValue()) { |
| 1100 | Fn = *MaybeFnA; |
| 1101 | assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) && |
| 1102 | "Interprocedural queries not supported"); |
| 1103 | } else { |
| 1104 | Fn = *MaybeFnB; |
| 1105 | } |
| 1106 | |
| 1107 | assert(Fn != nullptr); |
| 1108 | auto &MaybeInfo = ensureCached(Fn); |
| 1109 | assert(MaybeInfo.hasValue()); |
| 1110 | |
| 1111 | auto &Sets = MaybeInfo->Sets; |
| 1112 | auto MaybeA = Sets.find(ValA); |
| 1113 | if (!MaybeA.hasValue()) |
| 1114 | return AliasAnalysis::MayAlias; |
| 1115 | |
| 1116 | auto MaybeB = Sets.find(ValB); |
| 1117 | if (!MaybeB.hasValue()) |
| 1118 | return AliasAnalysis::MayAlias; |
| 1119 | |
| 1120 | auto SetA = *MaybeA; |
| 1121 | auto SetB = *MaybeB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1122 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 1123 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1124 | |
Hal Finkel | 8eae3ad | 2014-10-06 14:42:56 +0000 | [diff] [blame] | 1125 | // Stratified set attributes are used as markets to signify whether a member |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1126 | // of a StratifiedSet (or a member of a set above the current set) has |
Hal Finkel | 8eae3ad | 2014-10-06 14:42:56 +0000 | [diff] [blame] | 1127 | // interacted with either arguments or globals. "Interacted with" meaning |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1128 | // its value may be different depending on the value of an argument or |
Hal Finkel | 8eae3ad | 2014-10-06 14:42:56 +0000 | [diff] [blame] | 1129 | // global. The thought behind this is that, because arguments and globals |
| 1130 | // may alias each other, if AttrsA and AttrsB have touched args/globals, |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 1131 | // we must conservatively say that they alias. However, if at least one of |
| 1132 | // the sets has no values that could legally be altered by changing the value |
Hal Finkel | 8eae3ad | 2014-10-06 14:42:56 +0000 | [diff] [blame] | 1133 | // of an argument or global, then we don't have to be as conservative. |
| 1134 | if (AttrsA.any() && AttrsB.any()) |
| 1135 | return AliasAnalysis::MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1136 | |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 1137 | // We currently unify things even if the accesses to them may not be in |
| 1138 | // bounds, so we can't return partial alias here because we don't |
| 1139 | // know whether the pointer is really within the object or not. |
| 1140 | // IE Given an out of bounds GEP and an alloca'd pointer, we may |
| 1141 | // unify the two. We can't return partial alias for this case. |
| 1142 | // Since we do not currently track enough information to |
| 1143 | // differentiate |
| 1144 | |
| 1145 | if (SetA.Index == SetB.Index) |
| 1146 | return AliasAnalysis::MayAlias; |
| 1147 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1148 | return AliasAnalysis::NoAlias; |
| 1149 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 1150 | |
| 1151 | bool CFLAliasAnalysis::doInitialization(Module &M) { |
| 1152 | InitializeAliasAnalysis(this, &M.getDataLayout()); |
| 1153 | return true; |
| 1154 | } |