George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 1 | //- CFLSteensAliasAnalysis.cpp - Unification-based Alias Analysis ---*- C++-*-// |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 10 | // This file implements a CFL-base, summary-based alias analysis algorithm. It |
| 11 | // does not depend on types. The algorithm is a mixture of the one described in |
| 12 | // "Demand-driven alias analysis for C" by Xin Zheng and Radu Rugina, and "Fast |
| 13 | // algorithms for Dyck-CFL-reachability with applications to Alias Analysis" by |
| 14 | // Zhang Q, Lyu M R, Yuan H, and Su Z. -- to summarize the papers, we build a |
| 15 | // graph of the uses of a variable, where each node is a memory location, and |
| 16 | // each edge is an action that happened on that memory location. The "actions" |
| 17 | // can be one of Dereference, Reference, or Assign. The precision of this |
| 18 | // analysis is roughly the same as that of an one level context-sensitive |
| 19 | // Steensgaard's algorithm. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 20 | // |
| 21 | // Two variables are considered as aliasing iff you can reach one value's node |
| 22 | // from the other value's node and the language formed by concatenating all of |
| 23 | // the edge labels (actions) conforms to a context-free grammar. |
| 24 | // |
| 25 | // Because this algorithm requires a graph search on each query, we execute the |
| 26 | // algorithm outlined in "Fast algorithms..." (mentioned above) |
| 27 | // in order to transform the graph into sets of variables that may alias in |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 28 | // ~nlogn time (n = number of variables), which makes queries take constant |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 29 | // time. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 32 | // N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 33 | // CFLSteensAA is interprocedural. This is *technically* A Bad Thing, because |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 34 | // FunctionPasses are only allowed to inspect the Function that they're being |
| 35 | // run on. Realistically, this likely isn't a problem until we allow |
| 36 | // FunctionPasses to run concurrently. |
| 37 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 39 | #include "CFLGraph.h" |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 40 | #include "StratifiedSets.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/DenseMap.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/None.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/Optional.h" |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 45 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Constants.h" |
| 47 | #include "llvm/IR/Function.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 48 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 50 | #include "llvm/Pass.h" |
Hal Finkel | 7d7087c | 2014-09-02 22:13:00 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Compiler.h" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 53 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 54 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 55 | #include <algorithm> |
| 56 | #include <cassert> |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 57 | #include <memory> |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 58 | #include <tuple> |
| 59 | |
| 60 | using namespace llvm; |
George Burgess IV | 1ca8aff | 2016-07-06 00:36:12 +0000 | [diff] [blame] | 61 | using namespace llvm::cflaa; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 62 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 63 | #define DEBUG_TYPE "cfl-steens-aa" |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 64 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 65 | CFLSteensAAResult::CFLSteensAAResult(const TargetLibraryInfo &TLI) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 66 | : AAResultBase(), TLI(TLI) {} |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 67 | CFLSteensAAResult::CFLSteensAAResult(CFLSteensAAResult &&Arg) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 68 | : AAResultBase(std::move(Arg)), TLI(Arg.TLI) {} |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 69 | CFLSteensAAResult::~CFLSteensAAResult() {} |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 70 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 71 | /// 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] | 72 | class CFLSteensAAResult::FunctionInfo { |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 73 | StratifiedSets<Value *> Sets; |
| 74 | |
| 75 | // RetParamRelations is a collection of ExternalRelations. |
| 76 | SmallVector<ExternalRelation, 8> RetParamRelations; |
| 77 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 78 | // RetParamAttributes is a collection of ExternalAttributes. |
| 79 | SmallVector<ExternalAttribute, 8> RetParamAttributes; |
| 80 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 81 | public: |
| 82 | FunctionInfo(Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
| 83 | StratifiedSets<Value *> S); |
| 84 | |
| 85 | const StratifiedSets<Value *> &getStratifiedSets() const { return Sets; } |
| 86 | const SmallVectorImpl<ExternalRelation> &getRetParamRelations() const { |
| 87 | return RetParamRelations; |
| 88 | } |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 89 | const SmallVectorImpl<ExternalAttribute> &getRetParamAttributes() const { |
| 90 | return RetParamAttributes; |
| 91 | } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 94 | /// Try to go from a Value* to a Function*. Never returns nullptr. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 95 | static Optional<Function *> parentFunctionOfValue(Value *); |
| 96 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 97 | /// Returns possible functions called by the Inst* into the given |
| 98 | /// SmallVectorImpl. Returns true if targets found, false otherwise. This is |
| 99 | /// templated so we can use it with CallInsts and InvokeInsts. |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 100 | static bool getPossibleTargets(CallSite, SmallVectorImpl<Function *> &); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 101 | |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 102 | const StratifiedIndex StratifiedLink::SetSentinel = |
George Burgess IV | 11d509d | 2015-03-15 00:52:21 +0000 | [diff] [blame] | 103 | std::numeric_limits<StratifiedIndex>::max(); |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 104 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 105 | namespace { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 106 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 107 | /// The maximum number of arguments we can put into a summary. |
| 108 | LLVM_CONSTEXPR unsigned MaxSupportedArgsInSummary = 50; |
| 109 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 110 | /// StratifiedSets call for knowledge of "direction", so this is how we |
| 111 | /// represent that locally. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 112 | enum class Level { Same, Above, Below }; |
| 113 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 114 | /// Gets the edges our graph should have, based on an Instruction* |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 115 | class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> { |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 116 | CFLSteensAAResult &AA; |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 117 | const TargetLibraryInfo &TLI; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 118 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 119 | CFLGraph &Graph; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 120 | SmallVectorImpl<Value *> &ReturnValues; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 121 | SmallPtrSetImpl<Value *> &Externals; |
| 122 | SmallPtrSetImpl<Value *> &Escapes; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 123 | SmallVectorImpl<InstantiatedRelation> &InstantiatedRelations; |
| 124 | SmallVectorImpl<InstantiatedAttr> &InstantiatedAttrs; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 125 | |
| 126 | static bool hasUsefulEdges(ConstantExpr *CE) { |
| 127 | // ConstantExpr doesn't have terminators, invokes, or fences, so only needs |
| 128 | // to check for compares. |
| 129 | return CE->getOpcode() != Instruction::ICmp && |
| 130 | CE->getOpcode() != Instruction::FCmp; |
| 131 | } |
| 132 | |
| 133 | void addNode(Value *Val) { |
| 134 | if (!Graph.addNode(Val)) |
| 135 | return; |
| 136 | |
| 137 | if (isa<GlobalValue>(Val)) |
| 138 | Externals.insert(Val); |
| 139 | else if (auto CExpr = dyn_cast<ConstantExpr>(Val)) |
| 140 | if (hasUsefulEdges(CExpr)) |
| 141 | visitConstantExpr(CExpr); |
| 142 | } |
| 143 | |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 144 | void addNodeWithAttr(Value *Val, AliasAttrs Attr) { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 145 | addNode(Val); |
| 146 | Graph.addAttr(Val, Attr); |
| 147 | } |
| 148 | |
| 149 | void addEdge(Value *From, Value *To, EdgeType Type) { |
| 150 | if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy()) |
| 151 | return; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 152 | addNode(From); |
| 153 | if (To != From) |
| 154 | addNode(To); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 155 | Graph.addEdge(From, To, Type); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 158 | public: |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 159 | GetEdgesVisitor(CFLSteensAAResult &AA, const TargetLibraryInfo &TLI, |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 160 | CFLGraph &Graph, SmallVectorImpl<Value *> &ReturnValues, |
| 161 | SmallPtrSetImpl<Value *> &Externals, |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 162 | SmallPtrSetImpl<Value *> &Escapes, |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 163 | SmallVectorImpl<InstantiatedRelation> &InstantiatedRelations, |
| 164 | SmallVectorImpl<InstantiatedAttr> &InstantiatedAttrs) |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 165 | : AA(AA), TLI(TLI), Graph(Graph), ReturnValues(ReturnValues), |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 166 | Externals(Externals), Escapes(Escapes), |
| 167 | InstantiatedRelations(InstantiatedRelations), |
| 168 | InstantiatedAttrs(InstantiatedAttrs) {} |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 169 | |
| 170 | void visitInstruction(Instruction &) { |
| 171 | llvm_unreachable("Unsupported instruction encountered"); |
| 172 | } |
| 173 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 174 | void visitReturnInst(ReturnInst &Inst) { |
| 175 | if (auto RetVal = Inst.getReturnValue()) { |
| 176 | if (RetVal->getType()->isPointerTy()) { |
| 177 | addNode(RetVal); |
| 178 | ReturnValues.push_back(RetVal); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 183 | void visitPtrToIntInst(PtrToIntInst &Inst) { |
| 184 | auto *Ptr = Inst.getOperand(0); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 185 | addNodeWithAttr(Ptr, getAttrEscaped()); |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void visitIntToPtrInst(IntToPtrInst &Inst) { |
| 189 | auto *Ptr = &Inst; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 190 | addNodeWithAttr(Ptr, getAttrUnknown()); |
George Burgess IV | b54a8d62 | 2015-03-10 02:40:06 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 193 | void visitCastInst(CastInst &Inst) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 194 | auto *Src = Inst.getOperand(0); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 195 | addEdge(Src, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void visitBinaryOperator(BinaryOperator &Inst) { |
| 199 | auto *Op1 = Inst.getOperand(0); |
| 200 | auto *Op2 = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 201 | addEdge(Op1, &Inst, EdgeType::Assign); |
| 202 | addEdge(Op2, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) { |
| 206 | auto *Ptr = Inst.getPointerOperand(); |
| 207 | auto *Val = Inst.getNewValOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 208 | addEdge(Ptr, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void visitAtomicRMWInst(AtomicRMWInst &Inst) { |
| 212 | auto *Ptr = Inst.getPointerOperand(); |
| 213 | auto *Val = Inst.getValOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 214 | addEdge(Ptr, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void visitPHINode(PHINode &Inst) { |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 218 | for (Value *Val : Inst.incoming_values()) |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 219 | addEdge(Val, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void visitGetElementPtrInst(GetElementPtrInst &Inst) { |
| 223 | auto *Op = Inst.getPointerOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 224 | addEdge(Op, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void visitSelectInst(SelectInst &Inst) { |
Daniel Berlin | 16f7a52 | 2015-01-26 17:31:17 +0000 | [diff] [blame] | 228 | // Condition is not processed here (The actual statement producing |
| 229 | // the condition result is processed elsewhere). For select, the |
| 230 | // condition is evaluated, but not loaded, stored, or assigned |
| 231 | // simply as a result of being the condition of a select. |
| 232 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 233 | auto *TrueVal = Inst.getTrueValue(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 234 | auto *FalseVal = Inst.getFalseValue(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 235 | addEdge(TrueVal, &Inst, EdgeType::Assign); |
| 236 | addEdge(FalseVal, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 237 | } |
| 238 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 239 | void visitAllocaInst(AllocaInst &Inst) { Graph.addNode(&Inst); } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 240 | |
| 241 | void visitLoadInst(LoadInst &Inst) { |
| 242 | auto *Ptr = Inst.getPointerOperand(); |
| 243 | auto *Val = &Inst; |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 244 | addEdge(Val, Ptr, EdgeType::Reference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void visitStoreInst(StoreInst &Inst) { |
| 248 | auto *Ptr = Inst.getPointerOperand(); |
| 249 | auto *Val = Inst.getValueOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 250 | addEdge(Ptr, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 253 | void visitVAArgInst(VAArgInst &Inst) { |
| 254 | // We can't fully model va_arg here. For *Ptr = Inst.getOperand(0), it does |
| 255 | // two things: |
| 256 | // 1. Loads a value from *((T*)*Ptr). |
| 257 | // 2. Increments (stores to) *Ptr by some target-specific amount. |
| 258 | // For now, we'll handle this like a landingpad instruction (by placing the |
| 259 | // result in its own group, and having that group alias externals). |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 260 | addNodeWithAttr(&Inst, getAttrUnknown()); |
Hal Finkel | db5f86a | 2014-10-14 20:51:26 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 263 | static bool isFunctionExternal(Function *Fn) { |
George Burgess IV | 9fdbfe1 | 2016-06-21 01:42:47 +0000 | [diff] [blame] | 264 | return !Fn->hasExactDefinition(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 265 | } |
| 266 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 267 | bool tryInterproceduralAnalysis(CallSite CS, |
| 268 | const SmallVectorImpl<Function *> &Fns) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 269 | assert(Fns.size() > 0); |
| 270 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 271 | if (CS.arg_size() > MaxSupportedArgsInSummary) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 272 | return false; |
| 273 | |
| 274 | // Exit early if we'll fail anyway |
| 275 | for (auto *Fn : Fns) { |
| 276 | if (isFunctionExternal(Fn) || Fn->isVarArg()) |
| 277 | return false; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 278 | // Fail if the caller does not provide enough arguments |
| 279 | assert(Fn->arg_size() <= CS.arg_size()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 280 | auto &MaybeInfo = AA.ensureCached(Fn); |
| 281 | if (!MaybeInfo.hasValue()) |
| 282 | return false; |
| 283 | } |
| 284 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 285 | for (auto *Fn : Fns) { |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 286 | auto &FnInfo = AA.ensureCached(Fn); |
| 287 | assert(FnInfo.hasValue()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 288 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 289 | auto &RetParamRelations = FnInfo->getRetParamRelations(); |
| 290 | for (auto &Relation : RetParamRelations) { |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 291 | auto IRelation = instantiateExternalRelation(Relation, CS); |
| 292 | if (IRelation.hasValue()) |
| 293 | InstantiatedRelations.push_back(*IRelation); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | auto &RetParamAttributes = FnInfo->getRetParamAttributes(); |
| 297 | for (auto &Attribute : RetParamAttributes) { |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 298 | auto IAttr = instantiateExternalAttribute(Attribute, CS); |
| 299 | if (IAttr.hasValue()) |
| 300 | InstantiatedAttrs.push_back(*IAttr); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 301 | } |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 302 | } |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 303 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 304 | return true; |
| 305 | } |
| 306 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 307 | void visitCallSite(CallSite CS) { |
| 308 | auto Inst = CS.getInstruction(); |
| 309 | |
| 310 | // Make sure all arguments and return value are added to the graph first |
| 311 | for (Value *V : CS.args()) |
| 312 | addNode(V); |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 313 | if (Inst->getType()->isPointerTy()) |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 314 | addNode(Inst); |
| 315 | |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 316 | // Check if Inst is a call to a library function that allocates/deallocates |
| 317 | // on the heap. Those kinds of functions do not introduce any aliases. |
| 318 | // TODO: address other common library functions such as realloc(), strdup(), |
| 319 | // etc. |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 320 | if (isMallocLikeFn(Inst, &TLI) || isCallocLikeFn(Inst, &TLI) || |
| 321 | isFreeCall(Inst, &TLI)) |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 322 | return; |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 323 | |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 324 | // TODO: Add support for noalias args/all the other fun function attributes |
| 325 | // that we can tack on. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 326 | SmallVector<Function *, 4> Targets; |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 327 | if (getPossibleTargets(CS, Targets)) |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 328 | if (tryInterproceduralAnalysis(CS, Targets)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 329 | return; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 330 | |
George Burgess IV | 68b36e0 | 2015-08-28 00:16:18 +0000 | [diff] [blame] | 331 | // Because the function is opaque, we need to note that anything |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 332 | // could have happened to the arguments (unless the function is marked |
| 333 | // readonly or readnone), and that the result could alias just about |
| 334 | // anything, too (unless the result is marked noalias). |
| 335 | if (!CS.onlyReadsMemory()) |
| 336 | for (Value *V : CS.args()) { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 337 | if (V->getType()->isPointerTy()) |
| 338 | Escapes.insert(V); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 339 | } |
| 340 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 341 | if (Inst->getType()->isPointerTy()) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 342 | auto *Fn = CS.getCalledFunction(); |
| 343 | if (Fn == nullptr || !Fn->doesNotAlias(0)) |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 344 | Graph.addAttr(Inst, getAttrUnknown()); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 345 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 346 | } |
| 347 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 348 | /// Because vectors/aggregates are immutable and unaddressable, there's |
| 349 | /// nothing we can do to coax a value out of them, other than calling |
| 350 | /// Extract{Element,Value}. We can effectively treat them as pointers to |
| 351 | /// arbitrary memory locations we can store in and load from. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 352 | void visitExtractElementInst(ExtractElementInst &Inst) { |
| 353 | auto *Ptr = Inst.getVectorOperand(); |
| 354 | auto *Val = &Inst; |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 355 | addEdge(Val, Ptr, EdgeType::Reference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void visitInsertElementInst(InsertElementInst &Inst) { |
| 359 | auto *Vec = Inst.getOperand(0); |
| 360 | auto *Val = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 361 | addEdge(Vec, &Inst, EdgeType::Assign); |
| 362 | addEdge(&Inst, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void visitLandingPadInst(LandingPadInst &Inst) { |
| 366 | // Exceptions come from "nowhere", from our analysis' perspective. |
| 367 | // So we place the instruction its own group, noting that said group may |
| 368 | // alias externals |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 369 | addNodeWithAttr(&Inst, getAttrUnknown()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void visitInsertValueInst(InsertValueInst &Inst) { |
| 373 | auto *Agg = Inst.getOperand(0); |
| 374 | auto *Val = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 375 | addEdge(Agg, &Inst, EdgeType::Assign); |
| 376 | addEdge(&Inst, Val, EdgeType::Dereference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void visitExtractValueInst(ExtractValueInst &Inst) { |
| 380 | auto *Ptr = Inst.getAggregateOperand(); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 381 | addEdge(&Inst, Ptr, EdgeType::Reference); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void visitShuffleVectorInst(ShuffleVectorInst &Inst) { |
| 385 | auto *From1 = Inst.getOperand(0); |
| 386 | auto *From2 = Inst.getOperand(1); |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 387 | addEdge(From1, &Inst, EdgeType::Assign); |
| 388 | addEdge(From2, &Inst, EdgeType::Assign); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 389 | } |
Pete Cooper | 3664253 | 2015-06-12 16:13:54 +0000 | [diff] [blame] | 390 | |
| 391 | void visitConstantExpr(ConstantExpr *CE) { |
| 392 | switch (CE->getOpcode()) { |
| 393 | default: |
| 394 | llvm_unreachable("Unknown instruction type encountered!"); |
| 395 | // Build the switch statement using the Instruction.def file. |
| 396 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 397 | case Instruction::OPCODE: \ |
| 398 | visit##OPCODE(*(CLASS *)CE); \ |
| 399 | break; |
| 400 | #include "llvm/IR/Instruction.def" |
| 401 | } |
| 402 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 403 | }; |
| 404 | |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 405 | class CFLGraphBuilder { |
| 406 | // Input of the builder |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 407 | CFLSteensAAResult &Analysis; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 408 | const TargetLibraryInfo &TLI; |
| 409 | |
| 410 | // Output of the builder |
| 411 | CFLGraph Graph; |
| 412 | SmallVector<Value *, 4> ReturnedValues; |
| 413 | |
| 414 | // Auxiliary structures used by the builder |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 415 | SmallPtrSet<Value *, 8> ExternalValues; |
| 416 | SmallPtrSet<Value *, 8> EscapedValues; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 417 | SmallVector<InstantiatedRelation, 8> InstantiatedRelations; |
| 418 | SmallVector<InstantiatedAttr, 8> InstantiatedAttrs; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 419 | |
| 420 | // Helper functions |
| 421 | |
| 422 | // Determines whether or not we an instruction is useless to us (e.g. |
| 423 | // FenceInst) |
| 424 | static bool hasUsefulEdges(Instruction *Inst) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 425 | bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) && |
| 426 | !isa<InvokeInst>(Inst) && |
| 427 | !isa<ReturnInst>(Inst); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 428 | return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 429 | !IsNonInvokeRetTerminator; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 430 | } |
| 431 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 432 | void addArgumentToGraph(Argument &Arg) { |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 433 | if (Arg.getType()->isPointerTy()) { |
| 434 | Graph.addNode(&Arg); |
| 435 | ExternalValues.insert(&Arg); |
| 436 | } |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // Given an Instruction, this will add it to the graph, along with any |
| 440 | // Instructions that are potentially only available from said Instruction |
| 441 | // For example, given the following line: |
| 442 | // %0 = load i16* getelementptr ([1 x i16]* @a, 0, 0), align 2 |
| 443 | // addInstructionToGraph would add both the `load` and `getelementptr` |
| 444 | // instructions to the graph appropriately. |
| 445 | void addInstructionToGraph(Instruction &Inst) { |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 446 | if (!hasUsefulEdges(&Inst)) |
| 447 | return; |
| 448 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 449 | GetEdgesVisitor(Analysis, TLI, Graph, ReturnedValues, ExternalValues, |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 450 | EscapedValues, InstantiatedRelations, InstantiatedAttrs) |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 451 | .visit(Inst); |
| 452 | } |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 453 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 454 | // Builds the graph needed for constructing the StratifiedSets for the given |
| 455 | // function |
| 456 | void buildGraphFrom(Function &Fn) { |
| 457 | for (auto &Bb : Fn.getBasicBlockList()) |
| 458 | for (auto &Inst : Bb.getInstList()) |
| 459 | addInstructionToGraph(Inst); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 460 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 461 | for (auto &Arg : Fn.args()) |
| 462 | addArgumentToGraph(Arg); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | public: |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 466 | CFLGraphBuilder(CFLSteensAAResult &Analysis, const TargetLibraryInfo &TLI, |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 467 | Function &Fn) |
| 468 | : Analysis(Analysis), TLI(TLI) { |
| 469 | buildGraphFrom(Fn); |
| 470 | } |
| 471 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 472 | const CFLGraph &getCFLGraph() const { return Graph; } |
| 473 | const SmallVector<Value *, 4> &getReturnValues() const { |
| 474 | return ReturnedValues; |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 475 | } |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 476 | const SmallPtrSet<Value *, 8> &getExternalValues() const { |
| 477 | return ExternalValues; |
| 478 | } |
| 479 | const SmallPtrSet<Value *, 8> &getEscapedValues() const { |
| 480 | return EscapedValues; |
| 481 | } |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 482 | const SmallVector<InstantiatedRelation, 8> &getInstantiatedRelations() const { |
| 483 | return InstantiatedRelations; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 484 | } |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 485 | const SmallVector<InstantiatedAttr, 8> &getInstantiatedAttrs() const { |
| 486 | return InstantiatedAttrs; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 487 | } |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 488 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 489 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 490 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 491 | //===----------------------------------------------------------------------===// |
| 492 | // Function declarations that require types defined in the namespace above |
| 493 | //===----------------------------------------------------------------------===// |
| 494 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 495 | /// Gets the "Level" that one should travel in StratifiedSets |
| 496 | /// given an EdgeType. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 497 | static Level directionOfEdgeType(EdgeType); |
| 498 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 499 | /// 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] | 500 | static bool canSkipAddingToSets(Value *Val); |
| 501 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 502 | static Optional<Function *> parentFunctionOfValue(Value *Val) { |
| 503 | if (auto *Inst = dyn_cast<Instruction>(Val)) { |
| 504 | auto *Bb = Inst->getParent(); |
| 505 | return Bb->getParent(); |
| 506 | } |
| 507 | |
| 508 | if (auto *Arg = dyn_cast<Argument>(Val)) |
| 509 | return Arg->getParent(); |
George Burgess IV | 77351ba3 | 2016-01-28 00:54:01 +0000 | [diff] [blame] | 510 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 511 | } |
| 512 | |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 513 | static bool getPossibleTargets(CallSite CS, |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 514 | SmallVectorImpl<Function *> &Output) { |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 515 | if (auto *Fn = CS.getCalledFunction()) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 516 | Output.push_back(Fn); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | // TODO: If the call is indirect, we might be able to enumerate all potential |
| 521 | // targets of the call and return them, rather than just failing. |
| 522 | return false; |
| 523 | } |
| 524 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 525 | static Level directionOfEdgeType(EdgeType Weight) { |
| 526 | switch (Weight) { |
| 527 | case EdgeType::Reference: |
| 528 | return Level::Above; |
| 529 | case EdgeType::Dereference: |
| 530 | return Level::Below; |
| 531 | case EdgeType::Assign: |
| 532 | return Level::Same; |
| 533 | } |
| 534 | llvm_unreachable("Incomplete switch coverage"); |
| 535 | } |
| 536 | |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 537 | static bool canSkipAddingToSets(Value *Val) { |
| 538 | // Constants can share instances, which may falsely unify multiple |
| 539 | // sets, e.g. in |
| 540 | // store i32* null, i32** %ptr1 |
| 541 | // store i32* null, i32** %ptr2 |
| 542 | // clearly ptr1 and ptr2 should not be unified into the same set, so |
| 543 | // we should filter out the (potentially shared) instance to |
| 544 | // i32* null. |
| 545 | if (isa<Constant>(Val)) { |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 546 | // TODO: Because all of these things are constant, we can determine whether |
| 547 | // the data is *actually* mutable at graph building time. This will probably |
| 548 | // come for free/cheap with offset awareness. |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 549 | bool CanStoreMutableData = isa<GlobalValue>(Val) || |
| 550 | isa<ConstantExpr>(Val) || |
| 551 | isa<ConstantAggregate>(Val); |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 552 | return !CanStoreMutableData; |
| 553 | } |
| 554 | |
| 555 | return false; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 556 | } |
| 557 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 558 | CFLSteensAAResult::FunctionInfo::FunctionInfo( |
| 559 | Function &Fn, const SmallVectorImpl<Value *> &RetVals, |
| 560 | StratifiedSets<Value *> S) |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 561 | : Sets(std::move(S)) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 562 | // Historically, an arbitrary upper-bound of 50 args was selected. We may want |
| 563 | // to remove this if it doesn't really matter in practice. |
| 564 | if (Fn.arg_size() > MaxSupportedArgsInSummary) |
| 565 | return; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 566 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 567 | DenseMap<StratifiedIndex, InterfaceValue> InterfaceMap; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 568 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 569 | // Our intention here is to record all InterfaceValues that share the same |
| 570 | // StratifiedIndex in RetParamRelations. For each valid InterfaceValue, we |
| 571 | // have its StratifiedIndex scanned here and check if the index is presented |
| 572 | // in InterfaceMap: if it is not, we add the correspondence to the map; |
| 573 | // otherwise, an aliasing relation is found and we add it to |
| 574 | // RetParamRelations. |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 575 | |
George Burgess IV | d14d05a | 2016-06-23 20:59:13 +0000 | [diff] [blame] | 576 | auto AddToRetParamRelations = [&](unsigned InterfaceIndex, |
| 577 | StratifiedIndex SetIndex) { |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 578 | unsigned Level = 0; |
| 579 | while (true) { |
| 580 | InterfaceValue CurrValue{InterfaceIndex, Level}; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 581 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 582 | auto Itr = InterfaceMap.find(SetIndex); |
| 583 | if (Itr != InterfaceMap.end()) { |
| 584 | if (CurrValue != Itr->second) |
| 585 | RetParamRelations.push_back(ExternalRelation{CurrValue, Itr->second}); |
| 586 | break; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 587 | } |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 588 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 589 | auto &Link = Sets.getLink(SetIndex); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 590 | InterfaceMap.insert(std::make_pair(SetIndex, CurrValue)); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 591 | auto ExternalAttrs = getExternallyVisibleAttrs(Link.Attrs); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 592 | if (ExternalAttrs.any()) |
| 593 | RetParamAttributes.push_back( |
| 594 | ExternalAttribute{CurrValue, ExternalAttrs}); |
| 595 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 596 | if (!Link.hasBelow()) |
| 597 | break; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 598 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 599 | ++Level; |
| 600 | SetIndex = Link.Below; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 601 | } |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | // Populate RetParamRelations for return values |
| 605 | for (auto *RetVal : RetVals) { |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 606 | assert(RetVal != nullptr); |
| 607 | assert(RetVal->getType()->isPointerTy()); |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 608 | auto RetInfo = Sets.find(RetVal); |
| 609 | if (RetInfo.hasValue()) |
| 610 | AddToRetParamRelations(0, RetInfo->Index); |
| 611 | } |
| 612 | |
| 613 | // Populate RetParamRelations for parameters |
| 614 | unsigned I = 0; |
| 615 | for (auto &Param : Fn.args()) { |
| 616 | if (Param.getType()->isPointerTy()) { |
| 617 | auto ParamInfo = Sets.find(&Param); |
| 618 | if (ParamInfo.hasValue()) |
| 619 | AddToRetParamRelations(I + 1, ParamInfo->Index); |
| 620 | } |
| 621 | ++I; |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 625 | // Builds the graph + StratifiedSets for a function. |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 626 | CFLSteensAAResult::FunctionInfo CFLSteensAAResult::buildSetsFrom(Function *Fn) { |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 627 | CFLGraphBuilder GraphBuilder(*this, TLI, *Fn); |
| 628 | StratifiedSetsBuilder<Value *> SetBuilder; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 629 | |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 630 | auto &Graph = GraphBuilder.getCFLGraph(); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 631 | SmallVector<Value *, 16> Worklist; |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 632 | for (auto Node : Graph.nodes()) |
| 633 | Worklist.push_back(Node); |
| 634 | |
| 635 | while (!Worklist.empty()) { |
| 636 | auto *CurValue = Worklist.pop_back_val(); |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 637 | SetBuilder.add(CurValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 638 | if (canSkipAddingToSets(CurValue)) |
| 639 | continue; |
| 640 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 641 | auto Attr = Graph.attrFor(CurValue); |
| 642 | SetBuilder.noteAttributes(CurValue, Attr); |
| 643 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 644 | for (const auto &Edge : Graph.edgesFor(CurValue)) { |
| 645 | auto Label = Edge.Type; |
| 646 | auto *OtherValue = Edge.Other; |
| 647 | |
| 648 | if (canSkipAddingToSets(OtherValue)) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 649 | continue; |
| 650 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 651 | bool Added; |
| 652 | switch (directionOfEdgeType(Label)) { |
| 653 | case Level::Above: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 654 | Added = SetBuilder.addAbove(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 655 | break; |
| 656 | case Level::Below: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 657 | Added = SetBuilder.addBelow(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 658 | break; |
| 659 | case Level::Same: |
George Burgess IV | e17756e | 2016-06-14 18:02:27 +0000 | [diff] [blame] | 660 | Added = SetBuilder.addWith(CurValue, OtherValue); |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 661 | break; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 662 | } |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 663 | |
George Burgess IV | dc96feb | 2016-06-13 19:21:18 +0000 | [diff] [blame] | 664 | if (Added) |
| 665 | Worklist.push_back(OtherValue); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 669 | // Special handling for globals and arguments |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 670 | for (auto *External : GraphBuilder.getExternalValues()) { |
| 671 | SetBuilder.add(External); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 672 | auto Attr = getGlobalOrArgAttrFromValue(*External); |
| 673 | if (Attr.any()) { |
| 674 | SetBuilder.noteAttributes(External, Attr); |
| 675 | if (isa<GlobalValue>(External)) |
| 676 | SetBuilder.addAttributesBelow(External, 1, getAttrUnknown()); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 677 | else |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 678 | SetBuilder.addAttributesBelow(External, 1, getAttrCaller()); |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 679 | } |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 680 | } |
George Burgess IV | ab03af2 | 2015-03-10 02:58:15 +0000 | [diff] [blame] | 681 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 682 | // Special handling for interprocedural aliases |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 683 | for (auto &Edge : GraphBuilder.getInstantiatedRelations()) { |
George Burgess IV | fe1397b | 2016-06-23 19:16:04 +0000 | [diff] [blame] | 684 | auto FromVal = Edge.From.Val; |
| 685 | auto ToVal = Edge.To.Val; |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 686 | SetBuilder.add(FromVal); |
| 687 | SetBuilder.add(ToVal); |
| 688 | SetBuilder.addBelowWith(FromVal, Edge.From.DerefLevel, ToVal, |
| 689 | Edge.To.DerefLevel); |
| 690 | } |
| 691 | |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 692 | // Special handling for interprocedural attributes |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 693 | for (auto &IPAttr : GraphBuilder.getInstantiatedAttrs()) { |
| 694 | auto Val = IPAttr.IValue.Val; |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 695 | SetBuilder.add(Val); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 696 | SetBuilder.addAttributesBelow(Val, IPAttr.IValue.DerefLevel, IPAttr.Attr); |
George Burgess IV | a3d62be | 2016-06-24 01:00:03 +0000 | [diff] [blame] | 697 | } |
| 698 | |
George Burgess IV | 1f99da5 | 2016-06-23 18:55:23 +0000 | [diff] [blame] | 699 | // Special handling for opaque external functions |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 700 | for (auto *Escape : GraphBuilder.getEscapedValues()) { |
| 701 | SetBuilder.add(Escape); |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 702 | SetBuilder.noteAttributes(Escape, getAttrEscaped()); |
| 703 | SetBuilder.addAttributesBelow(Escape, 1, getAttrUnknown()); |
George Burgess IV | 24eb0da | 2016-06-14 18:12:28 +0000 | [diff] [blame] | 704 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 705 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 706 | return FunctionInfo(*Fn, GraphBuilder.getReturnValues(), SetBuilder.build()); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 707 | } |
| 708 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 709 | void CFLSteensAAResult::scan(Function *Fn) { |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 710 | auto InsertPair = Cache.insert(std::make_pair(Fn, Optional<FunctionInfo>())); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 711 | (void)InsertPair; |
| 712 | assert(InsertPair.second && |
| 713 | "Trying to scan a function that has already been cached"); |
| 714 | |
George Burgess IV | 6edb891 | 2016-05-02 18:09:19 +0000 | [diff] [blame] | 715 | // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call |
| 716 | // may get evaluated after operator[], potentially triggering a DenseMap |
| 717 | // resize and invalidating the reference returned by operator[] |
| 718 | auto FunInfo = buildSetsFrom(Fn); |
| 719 | Cache[Fn] = std::move(FunInfo); |
| 720 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 721 | Handles.push_front(FunctionHandle(Fn, this)); |
| 722 | } |
| 723 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 724 | void CFLSteensAAResult::evict(Function *Fn) { Cache.erase(Fn); } |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 725 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 726 | /// Ensures that the given function is available in the cache, and returns the |
| 727 | /// entry. |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 728 | const Optional<CFLSteensAAResult::FunctionInfo> & |
| 729 | CFLSteensAAResult::ensureCached(Function *Fn) { |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 730 | auto Iter = Cache.find(Fn); |
| 731 | if (Iter == Cache.end()) { |
| 732 | scan(Fn); |
| 733 | Iter = Cache.find(Fn); |
| 734 | assert(Iter != Cache.end()); |
| 735 | assert(Iter->second.hasValue()); |
| 736 | } |
| 737 | return Iter->second; |
| 738 | } |
| 739 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 740 | AliasResult CFLSteensAAResult::query(const MemoryLocation &LocA, |
| 741 | const MemoryLocation &LocB) { |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 742 | auto *ValA = const_cast<Value *>(LocA.Ptr); |
| 743 | auto *ValB = const_cast<Value *>(LocB.Ptr); |
| 744 | |
George Burgess IV | 259d901 | 2016-06-15 20:43:41 +0000 | [diff] [blame] | 745 | if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy()) |
| 746 | return NoAlias; |
| 747 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 748 | Function *Fn = nullptr; |
| 749 | auto MaybeFnA = parentFunctionOfValue(ValA); |
| 750 | auto MaybeFnB = parentFunctionOfValue(ValB); |
| 751 | if (!MaybeFnA.hasValue() && !MaybeFnB.hasValue()) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 752 | // The only times this is known to happen are when globals + InlineAsm are |
| 753 | // involved |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 754 | DEBUG(dbgs() |
| 755 | << "CFLSteensAA: could not extract parent function information.\n"); |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 756 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | if (MaybeFnA.hasValue()) { |
| 760 | Fn = *MaybeFnA; |
| 761 | assert((!MaybeFnB.hasValue() || *MaybeFnB == *MaybeFnA) && |
| 762 | "Interprocedural queries not supported"); |
| 763 | } else { |
| 764 | Fn = *MaybeFnB; |
| 765 | } |
| 766 | |
| 767 | assert(Fn != nullptr); |
| 768 | auto &MaybeInfo = ensureCached(Fn); |
| 769 | assert(MaybeInfo.hasValue()); |
| 770 | |
George Burgess IV | 87b2e41 | 2016-06-20 23:10:56 +0000 | [diff] [blame] | 771 | auto &Sets = MaybeInfo->getStratifiedSets(); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 772 | auto MaybeA = Sets.find(ValA); |
| 773 | if (!MaybeA.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 774 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 775 | |
| 776 | auto MaybeB = Sets.find(ValB); |
| 777 | if (!MaybeB.hasValue()) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 778 | return MayAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 779 | |
| 780 | auto SetA = *MaybeA; |
| 781 | auto SetB = *MaybeB; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 782 | auto AttrsA = Sets.getLink(SetA.Index).Attrs; |
| 783 | auto AttrsB = Sets.getLink(SetB.Index).Attrs; |
George Burgess IV | 33305e7 | 2015-02-12 03:07:07 +0000 | [diff] [blame] | 784 | |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 785 | // If both values are local (meaning the corresponding set has attribute |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 786 | // AttrNone or AttrEscaped), then we know that CFLSteensAA fully models them: |
| 787 | // 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] | 788 | // If at least one value is non-local (meaning it either is global/argument or |
| 789 | // it comes from unknown sources like integer cast), the situation becomes a |
| 790 | // bit more interesting. We follow three general rules described below: |
| 791 | // - Non-local values may alias each other |
| 792 | // - AttrNone values do not alias any non-local values |
George Burgess IV | 652ec4f | 2016-06-09 23:15:04 +0000 | [diff] [blame] | 793 | // - AttrEscaped do not alias globals/arguments, but they may alias |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 794 | // AttrUnknown values |
| 795 | if (SetA.Index == SetB.Index) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 796 | return MayAlias; |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 797 | if (AttrsA.none() || AttrsB.none()) |
| 798 | return NoAlias; |
George Burgess IV | e191996 | 2016-07-06 00:47:21 +0000 | [diff] [blame^] | 799 | if (hasUnknownOrCallerAttr(AttrsA) || hasUnknownOrCallerAttr(AttrsB)) |
George Burgess IV | a1f9a2d | 2016-06-07 18:35:37 +0000 | [diff] [blame] | 800 | return MayAlias; |
| 801 | if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB)) |
| 802 | return MayAlias; |
| 803 | return NoAlias; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 804 | } |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 805 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 806 | ModRefInfo CFLSteensAAResult::getArgModRefInfo(ImmutableCallSite CS, |
| 807 | unsigned ArgIdx) { |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 808 | if (auto CalledFunc = CS.getCalledFunction()) { |
| 809 | auto &MaybeInfo = ensureCached(const_cast<Function *>(CalledFunc)); |
| 810 | if (!MaybeInfo.hasValue()) |
| 811 | return MRI_ModRef; |
| 812 | auto &RetParamAttributes = MaybeInfo->getRetParamAttributes(); |
| 813 | auto &RetParamRelations = MaybeInfo->getRetParamRelations(); |
| 814 | |
| 815 | bool ArgAttributeIsWritten = |
| 816 | std::any_of(RetParamAttributes.begin(), RetParamAttributes.end(), |
| 817 | [ArgIdx](const ExternalAttribute &ExtAttr) { |
| 818 | return ExtAttr.IValue.Index == ArgIdx + 1; |
| 819 | }); |
| 820 | bool ArgIsAccessed = |
| 821 | std::any_of(RetParamRelations.begin(), RetParamRelations.end(), |
| 822 | [ArgIdx](const ExternalRelation &ExtRelation) { |
| 823 | return ExtRelation.To.Index == ArgIdx + 1 || |
| 824 | ExtRelation.From.Index == ArgIdx + 1; |
| 825 | }); |
| 826 | |
| 827 | return (!ArgIsAccessed && !ArgAttributeIsWritten) ? MRI_NoModRef |
| 828 | : MRI_ModRef; |
| 829 | } |
| 830 | |
| 831 | return MRI_ModRef; |
| 832 | } |
| 833 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 834 | FunctionModRefBehavior |
| 835 | CFLSteensAAResult::getModRefBehavior(ImmutableCallSite CS) { |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 836 | // If we know the callee, try analyzing it |
| 837 | if (auto CalledFunc = CS.getCalledFunction()) |
| 838 | return getModRefBehavior(CalledFunc); |
| 839 | |
| 840 | // Otherwise, be conservative |
| 841 | return FMRB_UnknownModRefBehavior; |
| 842 | } |
| 843 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 844 | FunctionModRefBehavior CFLSteensAAResult::getModRefBehavior(const Function *F) { |
George Burgess IV | d86e38e | 2016-06-30 02:11:26 +0000 | [diff] [blame] | 845 | assert(F != nullptr); |
| 846 | |
| 847 | // TODO: Remove the const_cast |
| 848 | auto &MaybeInfo = ensureCached(const_cast<Function *>(F)); |
| 849 | if (!MaybeInfo.hasValue()) |
| 850 | return FMRB_UnknownModRefBehavior; |
| 851 | auto &RetParamAttributes = MaybeInfo->getRetParamAttributes(); |
| 852 | auto &RetParamRelations = MaybeInfo->getRetParamRelations(); |
| 853 | |
| 854 | // First, if any argument is marked Escpaed, Unknown or Global, anything may |
| 855 | // happen to them and thus we can't draw any conclusion. |
| 856 | if (!RetParamAttributes.empty()) |
| 857 | return FMRB_UnknownModRefBehavior; |
| 858 | |
| 859 | // Currently we don't (and can't) distinguish reads from writes in |
| 860 | // RetParamRelations. All we can say is whether there may be memory access or |
| 861 | // not. |
| 862 | if (RetParamRelations.empty()) |
| 863 | return FMRB_DoesNotAccessMemory; |
| 864 | |
| 865 | // Check if something beyond argmem gets touched. |
| 866 | bool AccessArgMemoryOnly = |
| 867 | std::all_of(RetParamRelations.begin(), RetParamRelations.end(), |
| 868 | [](const ExternalRelation &ExtRelation) { |
| 869 | // Both DerefLevels has to be 0, since we don't know which |
| 870 | // one is a read and which is a write. |
| 871 | return ExtRelation.From.DerefLevel == 0 && |
| 872 | ExtRelation.To.DerefLevel == 0; |
| 873 | }); |
| 874 | return AccessArgMemoryOnly ? FMRB_OnlyAccessesArgumentPointees |
| 875 | : FMRB_UnknownModRefBehavior; |
| 876 | } |
| 877 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 878 | char CFLSteensAA::PassID; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 879 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 880 | CFLSteensAAResult CFLSteensAA::run(Function &F, AnalysisManager<Function> &AM) { |
| 881 | return CFLSteensAAResult(AM.getResult<TargetLibraryAnalysis>(F)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 882 | } |
| 883 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 884 | char CFLSteensAAWrapperPass::ID = 0; |
| 885 | INITIALIZE_PASS(CFLSteensAAWrapperPass, "cfl-steens-aa", |
| 886 | "Unification-Based CFL Alias Analysis", false, true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 887 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 888 | ImmutablePass *llvm::createCFLSteensAAWrapperPass() { |
| 889 | return new CFLSteensAAWrapperPass(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 890 | } |
| 891 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 892 | CFLSteensAAWrapperPass::CFLSteensAAWrapperPass() : ImmutablePass(ID) { |
| 893 | initializeCFLSteensAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 894 | } |
| 895 | |
| 896 | void CFLSteensAAWrapperPass::initializePass() { |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 897 | auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 898 | Result.reset(new CFLSteensAAResult(TLIWP.getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 899 | } |
| 900 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 901 | void CFLSteensAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 902 | AU.setPreservesAll(); |
George Burgess IV | 18b83fe | 2016-06-01 18:39:54 +0000 | [diff] [blame] | 903 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 904 | } |