| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 1 | //===- Attributor.cpp - Module-wide attribute deduction -------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements an inter procedural pass that deduces and/or propagating |
| 10 | // attributes. This is done in an abstract interpretation style fixpoint |
| 11 | // iteration. See the Attributor.h file comment and the class descriptions in |
| 12 | // that file for more information. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO/Attributor.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 17 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DepthFirstIterator.h" |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 23 | #include "llvm/Analysis/CallGraph.h" |
| 24 | #include "llvm/Analysis/CallGraphSCCPass.h" |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/CaptureTracking.h" |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/EHPersonalities.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/GlobalsModRef.h" |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 28 | #include "llvm/Analysis/LazyValueInfo.h" |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/Loads.h" |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/MemoryBuiltins.h" |
| omarahmed1111 | b285b33 | 2020-03-13 10:30:36 -0500 | [diff] [blame] | 31 | #include "llvm/Analysis/MustExecute.h" |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 32 | #include "llvm/Analysis/ScalarEvolution.h" |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/ValueTracking.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Argument.h" |
| 35 | #include "llvm/IR/Attributes.h" |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 36 | #include "llvm/IR/CFG.h" |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 37 | #include "llvm/IR/IRBuilder.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 38 | #include "llvm/IR/InstIterator.h" |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 39 | #include "llvm/IR/IntrinsicInst.h" |
| omarahmed1111 | b285b33 | 2020-03-13 10:30:36 -0500 | [diff] [blame] | 40 | #include "llvm/IR/NoFolder.h" |
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 41 | #include "llvm/IR/Verifier.h" |
| Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 42 | #include "llvm/InitializePasses.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
| 44 | #include "llvm/Support/Debug.h" |
| 45 | #include "llvm/Support/raw_ostream.h" |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 46 | #include "llvm/Transforms/IPO/ArgumentPromotion.h" |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 48 | #include "llvm/Transforms/Utils/Local.h" |
| 49 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 50 | #include <cassert> |
| 51 | |
| 52 | using namespace llvm; |
| 53 | |
| 54 | #define DEBUG_TYPE "attributor" |
| 55 | |
| 56 | STATISTIC(NumFnWithExactDefinition, |
| 57 | "Number of function with exact definitions"); |
| 58 | STATISTIC(NumFnWithoutExactDefinition, |
| 59 | "Number of function without exact definitions"); |
| 60 | STATISTIC(NumAttributesTimedOut, |
| 61 | "Number of abstract attributes timed out before fixpoint"); |
| 62 | STATISTIC(NumAttributesValidFixpoint, |
| 63 | "Number of abstract attributes in a valid fixpoint state"); |
| 64 | STATISTIC(NumAttributesManifested, |
| 65 | "Number of abstract attributes manifested in IR"); |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 66 | STATISTIC(NumAttributesFixedDueToRequiredDependences, |
| 67 | "Number of abstract attributes fixed due to required dependences"); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 68 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 69 | // Some helper macros to deal with statistics tracking. |
| 70 | // |
| 71 | // Usage: |
| 72 | // For simple IR attribute tracking overload trackStatistics in the abstract |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 73 | // attribute and choose the right STATS_DECLTRACK_********* macro, |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 74 | // e.g.,: |
| 75 | // void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 76 | // STATS_DECLTRACK_ARG_ATTR(returned) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 77 | // } |
| 78 | // If there is a single "increment" side one can use the macro |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 79 | // STATS_DECLTRACK with a custom message. If there are multiple increment |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 80 | // sides, STATS_DECL and STATS_TRACK can also be used separatly. |
| 81 | // |
| 82 | #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \ |
| 83 | ("Number of " #TYPE " marked '" #NAME "'") |
| 84 | #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME |
| Johannes Doerfert | a7a3b3a | 2019-09-04 19:01:08 +0000 | [diff] [blame] | 85 | #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG); |
| 86 | #define STATS_DECL(NAME, TYPE, MSG) \ |
| 87 | STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 88 | #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 89 | #define STATS_DECLTRACK(NAME, TYPE, MSG) \ |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 90 | { \ |
| 91 | STATS_DECL(NAME, TYPE, MSG) \ |
| 92 | STATS_TRACK(NAME, TYPE) \ |
| 93 | } |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 94 | #define STATS_DECLTRACK_ARG_ATTR(NAME) \ |
| 95 | STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) |
| 96 | #define STATS_DECLTRACK_CSARG_ATTR(NAME) \ |
| 97 | STATS_DECLTRACK(NAME, CSArguments, \ |
| 98 | BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) |
| 99 | #define STATS_DECLTRACK_FN_ATTR(NAME) \ |
| 100 | STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) |
| 101 | #define STATS_DECLTRACK_CS_ATTR(NAME) \ |
| 102 | STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) |
| 103 | #define STATS_DECLTRACK_FNRET_ATTR(NAME) \ |
| 104 | STATS_DECLTRACK(NAME, FunctionReturn, \ |
| Johannes Doerfert | 2db8528 | 2019-08-21 20:56:56 +0000 | [diff] [blame] | 105 | BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 106 | #define STATS_DECLTRACK_CSRET_ATTR(NAME) \ |
| 107 | STATS_DECLTRACK(NAME, CSReturn, \ |
| 108 | BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) |
| 109 | #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \ |
| 110 | STATS_DECLTRACK(NAME, Floating, \ |
| 111 | ("Number of floating values known to be '" #NAME "'")) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 112 | |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 113 | // Specialization of the operator<< for abstract attributes subclasses. This |
| 114 | // disambiguates situations where multiple operators are applicable. |
| 115 | namespace llvm { |
| 116 | #define PIPE_OPERATOR(CLASS) \ |
| 117 | raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \ |
| 118 | return OS << static_cast<const AbstractAttribute &>(AA); \ |
| 119 | } |
| 120 | |
| 121 | PIPE_OPERATOR(AAIsDead) |
| 122 | PIPE_OPERATOR(AANoUnwind) |
| 123 | PIPE_OPERATOR(AANoSync) |
| 124 | PIPE_OPERATOR(AANoRecurse) |
| 125 | PIPE_OPERATOR(AAWillReturn) |
| 126 | PIPE_OPERATOR(AANoReturn) |
| 127 | PIPE_OPERATOR(AAReturnedValues) |
| 128 | PIPE_OPERATOR(AANonNull) |
| 129 | PIPE_OPERATOR(AANoAlias) |
| 130 | PIPE_OPERATOR(AADereferenceable) |
| 131 | PIPE_OPERATOR(AAAlign) |
| 132 | PIPE_OPERATOR(AANoCapture) |
| 133 | PIPE_OPERATOR(AAValueSimplify) |
| 134 | PIPE_OPERATOR(AANoFree) |
| 135 | PIPE_OPERATOR(AAHeapToStack) |
| 136 | PIPE_OPERATOR(AAReachability) |
| 137 | PIPE_OPERATOR(AAMemoryBehavior) |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 138 | PIPE_OPERATOR(AAMemoryLocation) |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 139 | PIPE_OPERATOR(AAValueConstantRange) |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 140 | PIPE_OPERATOR(AAPrivatizablePtr) |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 141 | |
| 142 | #undef PIPE_OPERATOR |
| 143 | } // namespace llvm |
| 144 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 145 | // TODO: Determine a good default value. |
| 146 | // |
| 147 | // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads |
| 148 | // (when run with the first 5 abstract attributes). The results also indicate |
| 149 | // that we never reach 32 iterations but always find a fixpoint sooner. |
| 150 | // |
| 151 | // This will become more evolved once we perform two interleaved fixpoint |
| 152 | // iterations: bottom-up and top-down. |
| 153 | static cl::opt<unsigned> |
| 154 | MaxFixpointIterations("attributor-max-iterations", cl::Hidden, |
| 155 | cl::desc("Maximal number of fixpoint iterations."), |
| 156 | cl::init(32)); |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 157 | static cl::opt<bool> VerifyMaxFixpointIterations( |
| 158 | "attributor-max-iterations-verify", cl::Hidden, |
| 159 | cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), |
| 160 | cl::init(false)); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 161 | |
| 162 | static cl::opt<bool> DisableAttributor( |
| 163 | "attributor-disable", cl::Hidden, |
| 164 | cl::desc("Disable the attributor inter-procedural deduction pass."), |
| Johannes Doerfert | 282d34e | 2019-06-14 14:53:41 +0000 | [diff] [blame] | 165 | cl::init(true)); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 166 | |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 167 | static cl::opt<bool> AnnotateDeclarationCallSites( |
| 168 | "attributor-annotate-decl-cs", cl::Hidden, |
| James Henderson | d68904f | 2020-01-06 10:15:44 +0000 | [diff] [blame] | 169 | cl::desc("Annotate call sites of function declarations."), cl::init(false)); |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 170 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 171 | static cl::opt<bool> ManifestInternal( |
| 172 | "attributor-manifest-internal", cl::Hidden, |
| 173 | cl::desc("Manifest Attributor internal string attributes."), |
| 174 | cl::init(false)); |
| 175 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 176 | static cl::opt<unsigned> DepRecInterval( |
| 177 | "attributor-dependence-recompute-interval", cl::Hidden, |
| 178 | cl::desc("Number of iterations until dependences are recomputed."), |
| 179 | cl::init(4)); |
| 180 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 181 | static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", |
| 182 | cl::init(true), cl::Hidden); |
| 183 | |
| Johannes Doerfert | 1c2afae | 2019-10-10 05:34:21 +0000 | [diff] [blame] | 184 | static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), |
| 185 | cl::Hidden); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 186 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 187 | /// Logic operators for the change status enum class. |
| 188 | /// |
| 189 | ///{ |
| 190 | ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { |
| 191 | return l == ChangeStatus::CHANGED ? l : r; |
| 192 | } |
| 193 | ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { |
| 194 | return l == ChangeStatus::UNCHANGED ? l : r; |
| 195 | } |
| 196 | ///} |
| 197 | |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 198 | Argument *IRPosition::getAssociatedArgument() const { |
| 199 | if (getPositionKind() == IRP_ARGUMENT) |
| 200 | return cast<Argument>(&getAnchorValue()); |
| 201 | |
| 202 | // Not an Argument and no argument number means this is not a call site |
| 203 | // argument, thus we cannot find a callback argument to return. |
| 204 | int ArgNo = getArgNo(); |
| 205 | if (ArgNo < 0) |
| 206 | return nullptr; |
| 207 | |
| 208 | // Use abstract call sites to make the connection between the call site |
| 209 | // values and the ones in callbacks. If a callback was found that makes use |
| 210 | // of the underlying call site operand, we want the corresponding callback |
| 211 | // callee argument and not the direct callee argument. |
| 212 | Optional<Argument *> CBCandidateArg; |
| 213 | SmallVector<const Use *, 4> CBUses; |
| 214 | ImmutableCallSite ICS(&getAnchorValue()); |
| 215 | AbstractCallSite::getCallbackUses(ICS, CBUses); |
| 216 | for (const Use *U : CBUses) { |
| 217 | AbstractCallSite ACS(U); |
| 218 | assert(ACS && ACS.isCallbackCall()); |
| 219 | if (!ACS.getCalledFunction()) |
| 220 | continue; |
| 221 | |
| 222 | for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) { |
| 223 | |
| 224 | // Test if the underlying call site operand is argument number u of the |
| 225 | // callback callee. |
| 226 | if (ACS.getCallArgOperandNo(u) != ArgNo) |
| 227 | continue; |
| 228 | |
| 229 | assert(ACS.getCalledFunction()->arg_size() > u && |
| 230 | "ACS mapped into var-args arguments!"); |
| 231 | if (CBCandidateArg.hasValue()) { |
| 232 | CBCandidateArg = nullptr; |
| 233 | break; |
| 234 | } |
| 235 | CBCandidateArg = ACS.getCalledFunction()->getArg(u); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // If we found a unique callback candidate argument, return it. |
| 240 | if (CBCandidateArg.hasValue() && CBCandidateArg.getValue()) |
| 241 | return CBCandidateArg.getValue(); |
| 242 | |
| 243 | // If no callbacks were found, or none used the underlying call site operand |
| 244 | // exclusively, use the direct callee argument if available. |
| 245 | const Function *Callee = ICS.getCalledFunction(); |
| 246 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) |
| 247 | return Callee->getArg(ArgNo); |
| 248 | |
| 249 | return nullptr; |
| 250 | } |
| 251 | |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 252 | static Optional<Constant *> getAssumedConstant(Attributor &A, const Value &V, |
| 253 | const AbstractAttribute &AA, |
| 254 | bool &UsedAssumedInformation) { |
| Johannes Doerfert | 214ed3f | 2020-01-11 23:35:45 -0600 | [diff] [blame] | 255 | const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>( |
| 256 | AA, IRPosition::value(V), /* TrackDependence */ false); |
| 257 | Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); |
| 258 | bool IsKnown = ValueSimplifyAA.isKnown(); |
| 259 | UsedAssumedInformation |= !IsKnown; |
| 260 | if (!SimplifiedV.hasValue()) { |
| 261 | A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); |
| 262 | return llvm::None; |
| 263 | } |
| 264 | if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) { |
| 265 | A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); |
| 266 | return llvm::None; |
| 267 | } |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 268 | Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue()); |
| 269 | if (CI && CI->getType() != V.getType()) { |
| 270 | // TODO: Check for a save conversion. |
| 271 | return nullptr; |
| 272 | } |
| Johannes Doerfert | 214ed3f | 2020-01-11 23:35:45 -0600 | [diff] [blame] | 273 | if (CI) |
| 274 | A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); |
| 275 | return CI; |
| 276 | } |
| 277 | |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 278 | static Optional<ConstantInt *> |
| 279 | getAssumedConstantInt(Attributor &A, const Value &V, |
| 280 | const AbstractAttribute &AA, |
| 281 | bool &UsedAssumedInformation) { |
| 282 | Optional<Constant *> C = getAssumedConstant(A, V, AA, UsedAssumedInformation); |
| 283 | if (C.hasValue()) |
| 284 | return dyn_cast_or_null<ConstantInt>(C.getValue()); |
| 285 | return llvm::None; |
| 286 | } |
| 287 | |
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 288 | /// Get pointer operand of memory accessing instruction. If \p I is |
| 289 | /// not a memory accessing instruction, return nullptr. If \p AllowVolatile, |
| 290 | /// is set to false and the instruction is volatile, return nullptr. |
| 291 | static const Value *getPointerOperand(const Instruction *I, |
| 292 | bool AllowVolatile) { |
| 293 | if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 294 | if (!AllowVolatile && LI->isVolatile()) |
| 295 | return nullptr; |
| 296 | return LI->getPointerOperand(); |
| 297 | } |
| 298 | |
| 299 | if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 300 | if (!AllowVolatile && SI->isVolatile()) |
| 301 | return nullptr; |
| 302 | return SI->getPointerOperand(); |
| 303 | } |
| 304 | |
| 305 | if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) { |
| 306 | if (!AllowVolatile && CXI->isVolatile()) |
| 307 | return nullptr; |
| 308 | return CXI->getPointerOperand(); |
| 309 | } |
| 310 | |
| 311 | if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) { |
| 312 | if (!AllowVolatile && RMWI->isVolatile()) |
| 313 | return nullptr; |
| 314 | return RMWI->getPointerOperand(); |
| 315 | } |
| 316 | |
| 317 | return nullptr; |
| 318 | } |
| 319 | |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 320 | /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and |
| 321 | /// advanced by \p Offset bytes. To aid later analysis the method tries to build |
| 322 | /// getelement pointer instructions that traverse the natural type of \p Ptr if |
| 323 | /// possible. If that fails, the remaining offset is adjusted byte-wise, hence |
| 324 | /// through a cast to i8*. |
| 325 | /// |
| 326 | /// TODO: This could probably live somewhere more prominantly if it doesn't |
| 327 | /// already exist. |
| 328 | static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset, |
| 329 | IRBuilder<NoFolder> &IRB, const DataLayout &DL) { |
| 330 | assert(Offset >= 0 && "Negative offset not supported yet!"); |
| 331 | LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset |
| 332 | << "-bytes as " << *ResTy << "\n"); |
| 333 | |
| 334 | // The initial type we are trying to traverse to get nice GEPs. |
| 335 | Type *Ty = Ptr->getType(); |
| 336 | |
| 337 | SmallVector<Value *, 4> Indices; |
| 338 | std::string GEPName = Ptr->getName().str(); |
| 339 | while (Offset) { |
| 340 | uint64_t Idx, Rem; |
| 341 | |
| 342 | if (auto *STy = dyn_cast<StructType>(Ty)) { |
| 343 | const StructLayout *SL = DL.getStructLayout(STy); |
| 344 | if (int64_t(SL->getSizeInBytes()) < Offset) |
| 345 | break; |
| 346 | Idx = SL->getElementContainingOffset(Offset); |
| 347 | assert(Idx < STy->getNumElements() && "Offset calculation error!"); |
| 348 | Rem = Offset - SL->getElementOffset(Idx); |
| 349 | Ty = STy->getElementType(Idx); |
| 350 | } else if (auto *PTy = dyn_cast<PointerType>(Ty)) { |
| 351 | Ty = PTy->getElementType(); |
| 352 | if (!Ty->isSized()) |
| 353 | break; |
| 354 | uint64_t ElementSize = DL.getTypeAllocSize(Ty); |
| 355 | assert(ElementSize && "Expected type with size!"); |
| 356 | Idx = Offset / ElementSize; |
| 357 | Rem = Offset % ElementSize; |
| 358 | } else { |
| 359 | // Non-aggregate type, we cast and make byte-wise progress now. |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset |
| 364 | << " Idx: " << Idx << " Rem: " << Rem << "\n"); |
| 365 | |
| 366 | GEPName += "." + std::to_string(Idx); |
| 367 | Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx)); |
| 368 | Offset = Rem; |
| 369 | } |
| 370 | |
| 371 | // Create a GEP if we collected indices above. |
| 372 | if (Indices.size()) |
| 373 | Ptr = IRB.CreateGEP(Ptr, Indices, GEPName); |
| 374 | |
| 375 | // If an offset is left we use byte-wise adjustment. |
| 376 | if (Offset) { |
| 377 | Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy()); |
| 378 | Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset), |
| 379 | GEPName + ".b" + Twine(Offset)); |
| 380 | } |
| 381 | |
| 382 | // Ensure the result has the requested type. |
| 383 | Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast"); |
| 384 | |
| 385 | LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n"); |
| 386 | return Ptr; |
| 387 | } |
| 388 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 389 | /// Recursively visit all values that might become \p IRP at some point. This |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 390 | /// will be done by looking through cast instructions, selects, phis, and calls |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 391 | /// with the "returned" attribute. Once we cannot look through the value any |
| 392 | /// further, the callback \p VisitValueCB is invoked and passed the current |
| Johannes Doerfert | 52aec32 | 2020-02-11 15:11:32 -0600 | [diff] [blame] | 393 | /// value, the \p State, and a flag to indicate if we stripped anything. |
| 394 | /// Stripped means that we unpacked the value associated with \p IRP at least |
| 395 | /// once. Note that the value used for the callback may still be the value |
| 396 | /// associated with \p IRP (due to PHIs). To limit how much effort is invested, |
| 397 | /// we will never visit more values than specified by \p MaxValues. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 398 | template <typename AAType, typename StateTy> |
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 399 | static bool genericValueTraversal( |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 400 | Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 401 | const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 402 | int MaxValues = 8, const function_ref<Value *(Value *)> StripCB = nullptr) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 403 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 404 | const AAIsDead *LivenessAA = nullptr; |
| 405 | if (IRP.getAnchorScope()) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 406 | LivenessAA = &A.getAAFor<AAIsDead>( |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 407 | QueryingAA, IRPosition::function(*IRP.getAnchorScope()), |
| 408 | /* TrackDependence */ false); |
| 409 | bool AnyDead = false; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 410 | |
| 411 | // TODO: Use Positions here to allow context sensitivity in VisitValueCB |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 412 | SmallPtrSet<Value *, 16> Visited; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 413 | SmallVector<Value *, 16> Worklist; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 414 | Worklist.push_back(&IRP.getAssociatedValue()); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 415 | |
| 416 | int Iteration = 0; |
| 417 | do { |
| 418 | Value *V = Worklist.pop_back_val(); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 419 | if (StripCB) |
| 420 | V = StripCB(V); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 421 | |
| 422 | // Check if we should process the current value. To prevent endless |
| 423 | // recursion keep a record of the values we followed! |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 424 | if (!Visited.insert(V).second) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 425 | continue; |
| 426 | |
| 427 | // Make sure we limit the compile time for complex expressions. |
| 428 | if (Iteration++ >= MaxValues) |
| 429 | return false; |
| 430 | |
| 431 | // Explicitly look through calls with a "returned" attribute if we do |
| 432 | // not have a pointer as stripPointerCasts only works on them. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 433 | Value *NewV = nullptr; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 434 | if (V->getType()->isPointerTy()) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 435 | NewV = V->stripPointerCasts(); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 436 | } else { |
| 437 | CallSite CS(V); |
| 438 | if (CS && CS.getCalledFunction()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 439 | for (Argument &Arg : CS.getCalledFunction()->args()) |
| 440 | if (Arg.hasReturnedAttr()) { |
| 441 | NewV = CS.getArgOperand(Arg.getArgNo()); |
| 442 | break; |
| 443 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 446 | if (NewV && NewV != V) { |
| 447 | Worklist.push_back(NewV); |
| 448 | continue; |
| 449 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 450 | |
| 451 | // Look through select instructions, visit both potential values. |
| 452 | if (auto *SI = dyn_cast<SelectInst>(V)) { |
| 453 | Worklist.push_back(SI->getTrueValue()); |
| 454 | Worklist.push_back(SI->getFalseValue()); |
| 455 | continue; |
| 456 | } |
| 457 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 458 | // Look through phi nodes, visit all live operands. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 459 | if (auto *PHI = dyn_cast<PHINode>(V)) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 460 | assert(LivenessAA && |
| 461 | "Expected liveness in the presence of instructions!"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 462 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { |
| 463 | const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 464 | if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA, |
| 465 | LivenessAA, |
| 466 | /* CheckBBLivenessOnly */ true)) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 467 | AnyDead = true; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 468 | continue; |
| 469 | } |
| 470 | Worklist.push_back(PHI->getIncomingValue(u)); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 471 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 472 | continue; |
| 473 | } |
| 474 | |
| 475 | // Once a leaf is reached we inform the user through the callback. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 476 | if (!VisitValueCB(*V, State, Iteration > 1)) |
| 477 | return false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 478 | } while (!Worklist.empty()); |
| 479 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 480 | // If we actually used liveness information so we have to record a dependence. |
| 481 | if (AnyDead) |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 482 | A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 483 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 484 | // All values have been visited. |
| 485 | return true; |
| 486 | } |
| 487 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 488 | /// Return true if \p New is equal or worse than \p Old. |
| 489 | static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { |
| 490 | if (!Old.isIntAttribute()) |
| 491 | return true; |
| 492 | |
| 493 | return Old.getValueAsInt() >= New.getValueAsInt(); |
| 494 | } |
| 495 | |
| 496 | /// Return true if the information provided by \p Attr was added to the |
| 497 | /// attribute list \p Attrs. This is only the case if it was not already present |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 498 | /// in \p Attrs at the position describe by \p PK and \p AttrIdx. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 499 | static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 500 | AttributeList &Attrs, int AttrIdx) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 501 | |
| 502 | if (Attr.isEnumAttribute()) { |
| 503 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
| 504 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 505 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 506 | return false; |
| 507 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 508 | return true; |
| 509 | } |
| 510 | if (Attr.isStringAttribute()) { |
| 511 | StringRef Kind = Attr.getKindAsString(); |
| 512 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 513 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 514 | return false; |
| 515 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 516 | return true; |
| 517 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 518 | if (Attr.isIntAttribute()) { |
| 519 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
| 520 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 521 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 522 | return false; |
| 523 | Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); |
| 524 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 525 | return true; |
| 526 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 527 | |
| 528 | llvm_unreachable("Expected enum or string attribute!"); |
| 529 | } |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 530 | |
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 531 | static const Value * |
| 532 | getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset, |
| 533 | const DataLayout &DL, |
| 534 | bool AllowNonInbounds = false) { |
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 535 | const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 536 | if (!Ptr) |
| 537 | return nullptr; |
| 538 | |
| 539 | return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, |
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 540 | AllowNonInbounds); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 541 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 542 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 543 | ChangeStatus AbstractAttribute::update(Attributor &A) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 544 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| 545 | if (getState().isAtFixpoint()) |
| 546 | return HasChanged; |
| 547 | |
| 548 | LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); |
| 549 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 550 | HasChanged = updateImpl(A); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 551 | |
| 552 | LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this |
| 553 | << "\n"); |
| 554 | |
| 555 | return HasChanged; |
| 556 | } |
| 557 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 558 | ChangeStatus |
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 559 | IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 560 | const ArrayRef<Attribute> &DeducedAttrs) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 561 | Function *ScopeFn = IRP.getAssociatedFunction(); |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 562 | IRPosition::Kind PK = IRP.getPositionKind(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 563 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 564 | // In the following some generic code that will manifest attributes in |
| 565 | // DeducedAttrs if they improve the current IR. Due to the different |
| 566 | // annotation positions we use the underlying AttributeList interface. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 567 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 568 | AttributeList Attrs; |
| 569 | switch (PK) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 570 | case IRPosition::IRP_INVALID: |
| 571 | case IRPosition::IRP_FLOAT: |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 572 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 573 | case IRPosition::IRP_ARGUMENT: |
| 574 | case IRPosition::IRP_FUNCTION: |
| 575 | case IRPosition::IRP_RETURNED: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 576 | Attrs = ScopeFn->getAttributes(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 577 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 578 | case IRPosition::IRP_CALL_SITE: |
| 579 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 580 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 581 | Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 582 | break; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 585 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 586 | LLVMContext &Ctx = IRP.getAnchorValue().getContext(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 587 | for (const Attribute &Attr : DeducedAttrs) { |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 588 | if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 589 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 590 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 591 | HasChanged = ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if (HasChanged == ChangeStatus::UNCHANGED) |
| 595 | return HasChanged; |
| 596 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 597 | switch (PK) { |
| 598 | case IRPosition::IRP_ARGUMENT: |
| 599 | case IRPosition::IRP_FUNCTION: |
| 600 | case IRPosition::IRP_RETURNED: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 601 | ScopeFn->setAttributes(Attrs); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 602 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 603 | case IRPosition::IRP_CALL_SITE: |
| 604 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 605 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 606 | CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); |
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 607 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 608 | case IRPosition::IRP_INVALID: |
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 609 | case IRPosition::IRP_FLOAT: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 610 | break; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | return HasChanged; |
| 614 | } |
| 615 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 616 | const IRPosition IRPosition::EmptyKey(255); |
| 617 | const IRPosition IRPosition::TombstoneKey(256); |
| 618 | |
| 619 | SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { |
| 620 | IRPositions.emplace_back(IRP); |
| 621 | |
| 622 | ImmutableCallSite ICS(&IRP.getAnchorValue()); |
| 623 | switch (IRP.getPositionKind()) { |
| 624 | case IRPosition::IRP_INVALID: |
| 625 | case IRPosition::IRP_FLOAT: |
| 626 | case IRPosition::IRP_FUNCTION: |
| 627 | return; |
| 628 | case IRPosition::IRP_ARGUMENT: |
| 629 | case IRPosition::IRP_RETURNED: |
| 630 | IRPositions.emplace_back( |
| 631 | IRPosition::function(*IRP.getAssociatedFunction())); |
| 632 | return; |
| 633 | case IRPosition::IRP_CALL_SITE: |
| 634 | assert(ICS && "Expected call site!"); |
| 635 | // TODO: We need to look at the operand bundles similar to the redirection |
| 636 | // in CallBase. |
| 637 | if (!ICS.hasOperandBundles()) |
| 638 | if (const Function *Callee = ICS.getCalledFunction()) |
| 639 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 640 | return; |
| 641 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 642 | assert(ICS && "Expected call site!"); |
| 643 | // TODO: We need to look at the operand bundles similar to the redirection |
| 644 | // in CallBase. |
| 645 | if (!ICS.hasOperandBundles()) { |
| 646 | if (const Function *Callee = ICS.getCalledFunction()) { |
| 647 | IRPositions.emplace_back(IRPosition::returned(*Callee)); |
| 648 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| Johannes Doerfert | f8ad735 | 2020-02-19 23:39:57 -0600 | [diff] [blame] | 649 | for (const Argument &Arg : Callee->args()) |
| 650 | if (Arg.hasReturnedAttr()) { |
| 651 | IRPositions.emplace_back( |
| 652 | IRPosition::callsite_argument(ICS, Arg.getArgNo())); |
| 653 | IRPositions.emplace_back( |
| 654 | IRPosition::value(*ICS.getArgOperand(Arg.getArgNo()))); |
| 655 | IRPositions.emplace_back(IRPosition::argument(Arg)); |
| 656 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | IRPositions.emplace_back( |
| 660 | IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); |
| 661 | return; |
| 662 | case IRPosition::IRP_CALL_SITE_ARGUMENT: { |
| 663 | int ArgNo = IRP.getArgNo(); |
| 664 | assert(ICS && ArgNo >= 0 && "Expected call site!"); |
| 665 | // TODO: We need to look at the operand bundles similar to the redirection |
| 666 | // in CallBase. |
| 667 | if (!ICS.hasOperandBundles()) { |
| 668 | const Function *Callee = ICS.getCalledFunction(); |
| 669 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) |
| 670 | IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); |
| 671 | if (Callee) |
| 672 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 673 | } |
| 674 | IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); |
| 675 | return; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 680 | bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, |
| 681 | bool IgnoreSubsumingPositions) const { |
| Johannes Doerfert | 6185fb1 | 2020-02-20 02:02:57 -0600 | [diff] [blame] | 682 | SmallVector<Attribute, 4> Attrs; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 683 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 684 | for (Attribute::AttrKind AK : AKs) |
| Johannes Doerfert | 6185fb1 | 2020-02-20 02:02:57 -0600 | [diff] [blame] | 685 | if (EquivIRP.getAttrsFromIRAttr(AK, Attrs)) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 686 | return true; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 687 | // The first position returned by the SubsumingPositionIterator is |
| 688 | // always the position itself. If we ignore subsuming positions we |
| 689 | // are done after the first iteration. |
| 690 | if (IgnoreSubsumingPositions) |
| 691 | break; |
| 692 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 693 | return false; |
| 694 | } |
| 695 | |
| 696 | void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 697 | SmallVectorImpl<Attribute> &Attrs, |
| 698 | bool IgnoreSubsumingPositions) const { |
| 699 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { |
| Johannes Doerfert | 6185fb1 | 2020-02-20 02:02:57 -0600 | [diff] [blame] | 700 | for (Attribute::AttrKind AK : AKs) |
| 701 | EquivIRP.getAttrsFromIRAttr(AK, Attrs); |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 702 | // The first position returned by the SubsumingPositionIterator is |
| 703 | // always the position itself. If we ignore subsuming positions we |
| 704 | // are done after the first iteration. |
| 705 | if (IgnoreSubsumingPositions) |
| 706 | break; |
| 707 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| Johannes Doerfert | 6185fb1 | 2020-02-20 02:02:57 -0600 | [diff] [blame] | 710 | bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK, |
| 711 | SmallVectorImpl<Attribute> &Attrs) const { |
| 712 | if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT) |
| 713 | return false; |
| 714 | |
| 715 | AttributeList AttrList; |
| 716 | if (ImmutableCallSite ICS = ImmutableCallSite(&getAnchorValue())) |
| 717 | AttrList = ICS.getAttributes(); |
| 718 | else |
| 719 | AttrList = getAssociatedFunction()->getAttributes(); |
| 720 | |
| 721 | bool HasAttr = AttrList.hasAttribute(getAttrIdx(), AK); |
| 722 | if (HasAttr) |
| 723 | Attrs.push_back(AttrList.getAttribute(getAttrIdx(), AK)); |
| 724 | return HasAttr; |
| 725 | } |
| 726 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 727 | void IRPosition::verify() { |
| 728 | switch (KindOrArgNo) { |
| 729 | default: |
| 730 | assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); |
| 731 | assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && |
| 732 | "Expected call base or argument for positive attribute index!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 733 | if (isa<Argument>(AnchorVal)) { |
| 734 | assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 735 | "Argument number mismatch!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 736 | assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && |
| 737 | "Associated value mismatch!"); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 738 | } else { |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 739 | assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 740 | "Call site argument number mismatch!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 741 | assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == |
| 742 | &getAssociatedValue() && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 743 | "Associated value mismatch!"); |
| 744 | } |
| 745 | break; |
| 746 | case IRP_INVALID: |
| 747 | assert(!AnchorVal && "Expected no value for an invalid position!"); |
| 748 | break; |
| 749 | case IRP_FLOAT: |
| 750 | assert((!isa<CallBase>(&getAssociatedValue()) && |
| 751 | !isa<Argument>(&getAssociatedValue())) && |
| 752 | "Expected specialized kind for call base and argument values!"); |
| 753 | break; |
| 754 | case IRP_RETURNED: |
| 755 | assert(isa<Function>(AnchorVal) && |
| 756 | "Expected function for a 'returned' position!"); |
| 757 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 758 | break; |
| 759 | case IRP_CALL_SITE_RETURNED: |
| 760 | assert((isa<CallBase>(AnchorVal)) && |
| 761 | "Expected call base for 'call site returned' position!"); |
| 762 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 763 | break; |
| 764 | case IRP_CALL_SITE: |
| 765 | assert((isa<CallBase>(AnchorVal)) && |
| 766 | "Expected call base for 'call site function' position!"); |
| 767 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 768 | break; |
| 769 | case IRP_FUNCTION: |
| 770 | assert(isa<Function>(AnchorVal) && |
| 771 | "Expected function for a 'function' position!"); |
| 772 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 773 | break; |
| 774 | } |
| 775 | } |
| 776 | |
| Benjamin Kramer | 564a9de | 2020-02-17 17:55:03 +0100 | [diff] [blame] | 777 | namespace { |
| 778 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 779 | /// Helper function to clamp a state \p S of type \p StateType with the |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 780 | /// information in \p R and indicate/return if \p S did change (as-in update is |
| 781 | /// required to be run again). |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 782 | template <typename StateType> |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 783 | ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 784 | auto Assumed = S.getAssumed(); |
| 785 | S ^= R; |
| 786 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED |
| 787 | : ChangeStatus::CHANGED; |
| 788 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 789 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 790 | /// Clamp the information known for all returned values of a function |
| 791 | /// (identified by \p QueryingAA) into \p S. |
| 792 | template <typename AAType, typename StateType = typename AAType::StateType> |
| 793 | static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, |
| 794 | StateType &S) { |
| 795 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 796 | << QueryingAA << " into " << S << "\n"); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 797 | |
| 798 | assert((QueryingAA.getIRPosition().getPositionKind() == |
| 799 | IRPosition::IRP_RETURNED || |
| 800 | QueryingAA.getIRPosition().getPositionKind() == |
| 801 | IRPosition::IRP_CALL_SITE_RETURNED) && |
| 802 | "Can only clamp returned value states for a function returned or call " |
| 803 | "site returned position!"); |
| 804 | |
| 805 | // Use an optional state as there might not be any return values and we want |
| 806 | // to join (IntegerState::operator&) the state of all there are. |
| 807 | Optional<StateType> T; |
| 808 | |
| 809 | // Callback for each possibly returned value. |
| 810 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 811 | const IRPosition &RVPos = IRPosition::value(RV); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 812 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); |
| 813 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() |
| 814 | << " @ " << RVPos << "\n"); |
| 815 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 816 | if (T.hasValue()) |
| 817 | *T &= AAS; |
| 818 | else |
| 819 | T = AAS; |
| 820 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T |
| 821 | << "\n"); |
| 822 | return T->isValidState(); |
| 823 | }; |
| 824 | |
| 825 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) |
| 826 | S.indicatePessimisticFixpoint(); |
| 827 | else if (T.hasValue()) |
| 828 | S ^= *T; |
| 829 | } |
| 830 | |
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 831 | /// Helper class to compose two generic deduction |
| 832 | template <typename AAType, typename Base, typename StateType, |
| 833 | template <typename...> class F, template <typename...> class G> |
| 834 | struct AAComposeTwoGenericDeduction |
| 835 | : public F<AAType, G<AAType, Base, StateType>, StateType> { |
| 836 | AAComposeTwoGenericDeduction(const IRPosition &IRP) |
| 837 | : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} |
| 838 | |
| Johannes Doerfert | d95cb56 | 2020-02-20 02:03:32 -0600 | [diff] [blame] | 839 | void initialize(Attributor &A) override { |
| 840 | F<AAType, G<AAType, Base, StateType>, StateType>::initialize(A); |
| 841 | G<AAType, Base, StateType>::initialize(A); |
| 842 | } |
| 843 | |
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 844 | /// See AbstractAttribute::updateImpl(...). |
| 845 | ChangeStatus updateImpl(Attributor &A) override { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 846 | ChangeStatus ChangedF = |
| 847 | F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 848 | ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); |
| 849 | return ChangedF | ChangedG; |
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 850 | } |
| 851 | }; |
| 852 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 853 | /// Helper class for generic deduction: return value -> returned position. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 854 | template <typename AAType, typename Base, |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 855 | typename StateType = typename Base::StateType> |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 856 | struct AAReturnedFromReturnedValues : public Base { |
| 857 | AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 858 | |
| 859 | /// See AbstractAttribute::updateImpl(...). |
| 860 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 861 | StateType S(StateType::getBestState(this->getState())); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 862 | clampReturnedValueStates<AAType, StateType>(A, *this, S); |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 863 | // TODO: If we know we visited all returned values, thus no are assumed |
| 864 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 865 | return clampStateAndIndicateChange<StateType>(this->getState(), S); |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | /// Clamp the information known at all call sites for a given argument |
| 870 | /// (identified by \p QueryingAA) into \p S. |
| 871 | template <typename AAType, typename StateType = typename AAType::StateType> |
| 872 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, |
| 873 | StateType &S) { |
| 874 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 875 | << QueryingAA << " into " << S << "\n"); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 876 | |
| 877 | assert(QueryingAA.getIRPosition().getPositionKind() == |
| 878 | IRPosition::IRP_ARGUMENT && |
| 879 | "Can only clamp call site argument states for an argument position!"); |
| 880 | |
| 881 | // Use an optional state as there might not be any return values and we want |
| 882 | // to join (IntegerState::operator&) the state of all there are. |
| 883 | Optional<StateType> T; |
| 884 | |
| 885 | // The argument number which is also the call site argument number. |
| 886 | unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); |
| 887 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 888 | auto CallSiteCheck = [&](AbstractCallSite ACS) { |
| 889 | const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); |
| 890 | // Check if a coresponding argument was found or if it is on not associated |
| 891 | // (which can happen for callback calls). |
| 892 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) |
| 893 | return false; |
| 894 | |
| 895 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); |
| 896 | LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() |
| 897 | << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 898 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 899 | if (T.hasValue()) |
| 900 | *T &= AAS; |
| 901 | else |
| 902 | T = AAS; |
| 903 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T |
| 904 | << "\n"); |
| 905 | return T->isValidState(); |
| 906 | }; |
| 907 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 908 | bool AllCallSitesKnown; |
| 909 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true, |
| 910 | AllCallSitesKnown)) |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 911 | S.indicatePessimisticFixpoint(); |
| 912 | else if (T.hasValue()) |
| 913 | S ^= *T; |
| 914 | } |
| 915 | |
| 916 | /// Helper class for generic deduction: call site argument -> argument position. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 917 | template <typename AAType, typename Base, |
| 918 | typename StateType = typename AAType::StateType> |
| 919 | struct AAArgumentFromCallSiteArguments : public Base { |
| 920 | AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 921 | |
| 922 | /// See AbstractAttribute::updateImpl(...). |
| 923 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | ea5fabe | 2020-01-28 09:46:15 -0600 | [diff] [blame] | 924 | StateType S(StateType::getBestState(this->getState())); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 925 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 926 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 927 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 928 | return clampStateAndIndicateChange<StateType>(this->getState(), S); |
| 929 | } |
| 930 | }; |
| 931 | |
| 932 | /// Helper class for generic replication: function returned -> cs returned. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 933 | template <typename AAType, typename Base, |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 934 | typename StateType = typename Base::StateType> |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 935 | struct AACallSiteReturnedFromReturned : public Base { |
| 936 | AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 937 | |
| 938 | /// See AbstractAttribute::updateImpl(...). |
| 939 | ChangeStatus updateImpl(Attributor &A) override { |
| 940 | assert(this->getIRPosition().getPositionKind() == |
| 941 | IRPosition::IRP_CALL_SITE_RETURNED && |
| 942 | "Can only wrap function returned positions for call site returned " |
| 943 | "positions!"); |
| 944 | auto &S = this->getState(); |
| 945 | |
| 946 | const Function *AssociatedFunction = |
| 947 | this->getIRPosition().getAssociatedFunction(); |
| 948 | if (!AssociatedFunction) |
| 949 | return S.indicatePessimisticFixpoint(); |
| 950 | |
| 951 | IRPosition FnPos = IRPosition::returned(*AssociatedFunction); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 952 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 953 | return clampStateAndIndicateChange( |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 954 | S, static_cast<const StateType &>(AA.getState())); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 955 | } |
| 956 | }; |
| 957 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 958 | /// Helper class for generic deduction using must-be-executed-context |
| 959 | /// Base class is required to have `followUse` method. |
| 960 | |
| 961 | /// bool followUse(Attributor &A, const Use *U, const Instruction *I) |
| Simon Pilgrim | f7aee61 | 2019-10-10 15:25:16 +0000 | [diff] [blame] | 962 | /// U - Underlying use. |
| 963 | /// I - The user of the \p U. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 964 | /// `followUse` returns true if the value should be tracked transitively. |
| 965 | |
| 966 | template <typename AAType, typename Base, |
| 967 | typename StateType = typename AAType::StateType> |
| 968 | struct AAFromMustBeExecutedContext : public Base { |
| 969 | AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} |
| 970 | |
| 971 | void initialize(Attributor &A) override { |
| 972 | Base::initialize(A); |
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 973 | const IRPosition &IRP = this->getIRPosition(); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 974 | Instruction *CtxI = IRP.getCtxI(); |
| 975 | |
| 976 | if (!CtxI) |
| 977 | return; |
| 978 | |
| 979 | for (const Use &U : IRP.getAssociatedValue().uses()) |
| 980 | Uses.insert(&U); |
| 981 | } |
| 982 | |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 983 | /// Helper function to accumulate uses. |
| 984 | void followUsesInContext(Attributor &A, |
| 985 | MustBeExecutedContextExplorer &Explorer, |
| 986 | const Instruction *CtxI, |
| 987 | SetVector<const Use *> &Uses, StateType &State) { |
| 988 | auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); |
| 989 | for (unsigned u = 0; u < Uses.size(); ++u) { |
| 990 | const Use *U = Uses[u]; |
| 991 | if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { |
| 992 | bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); |
| 993 | if (Found && Base::followUse(A, U, UserI, State)) |
| 994 | for (const Use &Us : UserI->uses()) |
| 995 | Uses.insert(&Us); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1000 | /// See AbstractAttribute::updateImpl(...). |
| 1001 | ChangeStatus updateImpl(Attributor &A) override { |
| 1002 | auto BeforeState = this->getState(); |
| 1003 | auto &S = this->getState(); |
| 1004 | Instruction *CtxI = this->getIRPosition().getCtxI(); |
| 1005 | if (!CtxI) |
| 1006 | return ChangeStatus::UNCHANGED; |
| 1007 | |
| 1008 | MustBeExecutedContextExplorer &Explorer = |
| 1009 | A.getInfoCache().getMustBeExecutedContextExplorer(); |
| 1010 | |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 1011 | followUsesInContext(A, Explorer, CtxI, Uses, S); |
| 1012 | |
| 1013 | if (this->isAtFixpoint()) |
| 1014 | return ChangeStatus::CHANGED; |
| 1015 | |
| 1016 | SmallVector<const BranchInst *, 4> BrInsts; |
| 1017 | auto Pred = [&](const Instruction *I) { |
| 1018 | if (const BranchInst *Br = dyn_cast<BranchInst>(I)) |
| 1019 | if (Br->isConditional()) |
| 1020 | BrInsts.push_back(Br); |
| 1021 | return true; |
| 1022 | }; |
| 1023 | |
| 1024 | // Here, accumulate conditional branch instructions in the context. We |
| 1025 | // explore the child paths and collect the known states. The disjunction of |
| 1026 | // those states can be merged to its own state. Let ParentState_i be a state |
| 1027 | // to indicate the known information for an i-th branch instruction in the |
| 1028 | // context. ChildStates are created for its successors respectively. |
| 1029 | // |
| 1030 | // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1} |
| 1031 | // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2} |
| 1032 | // ... |
| 1033 | // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m} |
| 1034 | // |
| 1035 | // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m |
| 1036 | // |
| 1037 | // FIXME: Currently, recursive branches are not handled. For example, we |
| 1038 | // can't deduce that ptr must be dereferenced in below function. |
| 1039 | // |
| 1040 | // void f(int a, int c, int *ptr) { |
| 1041 | // if(a) |
| 1042 | // if (b) { |
| 1043 | // *ptr = 0; |
| 1044 | // } else { |
| 1045 | // *ptr = 1; |
| 1046 | // } |
| 1047 | // else { |
| 1048 | // if (b) { |
| 1049 | // *ptr = 0; |
| 1050 | // } else { |
| 1051 | // *ptr = 1; |
| 1052 | // } |
| 1053 | // } |
| 1054 | // } |
| 1055 | |
| 1056 | Explorer.checkForAllContext(CtxI, Pred); |
| 1057 | for (const BranchInst *Br : BrInsts) { |
| 1058 | StateType ParentState; |
| 1059 | |
| 1060 | // The known state of the parent state is a conjunction of children's |
| 1061 | // known states so it is initialized with a best state. |
| 1062 | ParentState.indicateOptimisticFixpoint(); |
| 1063 | |
| 1064 | for (const BasicBlock *BB : Br->successors()) { |
| 1065 | StateType ChildState; |
| 1066 | |
| 1067 | size_t BeforeSize = Uses.size(); |
| 1068 | followUsesInContext(A, Explorer, &BB->front(), Uses, ChildState); |
| 1069 | |
| 1070 | // Erase uses which only appear in the child. |
| 1071 | for (auto It = Uses.begin() + BeforeSize; It != Uses.end();) |
| 1072 | It = Uses.erase(It); |
| 1073 | |
| 1074 | ParentState &= ChildState; |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1075 | } |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 1076 | |
| 1077 | // Use only known state. |
| 1078 | S += ParentState; |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1079 | } |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1080 | |
| 1081 | return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; |
| 1082 | } |
| 1083 | |
| 1084 | private: |
| 1085 | /// Container for (transitive) uses of the associated value. |
| 1086 | SetVector<const Use *> Uses; |
| 1087 | }; |
| 1088 | |
| 1089 | template <typename AAType, typename Base, |
| 1090 | typename StateType = typename AAType::StateType> |
| 1091 | using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = |
| 1092 | AAComposeTwoGenericDeduction<AAType, Base, StateType, |
| 1093 | AAFromMustBeExecutedContext, |
| 1094 | AAArgumentFromCallSiteArguments>; |
| 1095 | |
| 1096 | template <typename AAType, typename Base, |
| 1097 | typename StateType = typename AAType::StateType> |
| 1098 | using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = |
| 1099 | AAComposeTwoGenericDeduction<AAType, Base, StateType, |
| 1100 | AAFromMustBeExecutedContext, |
| 1101 | AACallSiteReturnedFromReturned>; |
| 1102 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 1103 | /// -----------------------NoUnwind Function Attribute-------------------------- |
| 1104 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1105 | struct AANoUnwindImpl : AANoUnwind { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1106 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 1107 | |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1108 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 1109 | return getAssumed() ? "nounwind" : "may-unwind"; |
| 1110 | } |
| 1111 | |
| 1112 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1113 | ChangeStatus updateImpl(Attributor &A) override { |
| 1114 | auto Opcodes = { |
| 1115 | (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 1116 | (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, |
| 1117 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; |
| 1118 | |
| 1119 | auto CheckForNoUnwind = [&](Instruction &I) { |
| 1120 | if (!I.mayThrow()) |
| 1121 | return true; |
| 1122 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1123 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 1124 | const auto &NoUnwindAA = |
| 1125 | A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); |
| 1126 | return NoUnwindAA.isAssumedNoUnwind(); |
| 1127 | } |
| 1128 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1129 | }; |
| 1130 | |
| 1131 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) |
| 1132 | return indicatePessimisticFixpoint(); |
| 1133 | |
| 1134 | return ChangeStatus::UNCHANGED; |
| 1135 | } |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 1136 | }; |
| 1137 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1138 | struct AANoUnwindFunction final : public AANoUnwindImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1139 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1140 | |
| 1141 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1142 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1143 | }; |
| 1144 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1145 | /// NoUnwind attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1146 | struct AANoUnwindCallSite final : AANoUnwindImpl { |
| 1147 | AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} |
| 1148 | |
| 1149 | /// See AbstractAttribute::initialize(...). |
| 1150 | void initialize(Attributor &A) override { |
| 1151 | AANoUnwindImpl::initialize(A); |
| 1152 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1153 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1154 | indicatePessimisticFixpoint(); |
| 1155 | } |
| 1156 | |
| 1157 | /// See AbstractAttribute::updateImpl(...). |
| 1158 | ChangeStatus updateImpl(Attributor &A) override { |
| 1159 | // TODO: Once we have call site specific value information we can provide |
| 1160 | // call site specific liveness information and then it makes |
| 1161 | // sense to specialize attributes for call sites arguments instead of |
| 1162 | // redirecting requests to the callee argument. |
| 1163 | Function *F = getAssociatedFunction(); |
| 1164 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1165 | auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); |
| 1166 | return clampStateAndIndicateChange( |
| 1167 | getState(), |
| 1168 | static_cast<const AANoUnwind::StateType &>(FnAA.getState())); |
| 1169 | } |
| 1170 | |
| 1171 | /// See AbstractAttribute::trackStatistics() |
| 1172 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } |
| 1173 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1174 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1175 | /// --------------------- Function Return Values ------------------------------- |
| 1176 | |
| 1177 | /// "Attribute" that collects all potential returned values and the return |
| 1178 | /// instructions that they arise from. |
| 1179 | /// |
| 1180 | /// If there is a unique returned value R, the manifest method will: |
| 1181 | /// - mark R with the "returned" attribute, if R is an argument. |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 1182 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1183 | |
| 1184 | /// Mapping of values potentially returned by the associated function to the |
| 1185 | /// return instructions that might return them. |
| Johannes Doerfert | a4a308c | 2019-08-26 17:51:23 +0000 | [diff] [blame] | 1186 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1187 | |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1188 | /// Mapping to remember the number of returned values for a call site such |
| 1189 | /// that we can avoid updates if nothing changed. |
| 1190 | DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; |
| 1191 | |
| 1192 | /// Set of unresolved calls returned by the associated function. |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1193 | SmallSetVector<CallBase *, 4> UnresolvedCalls; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1194 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1195 | /// State flags |
| 1196 | /// |
| 1197 | ///{ |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1198 | bool IsFixed = false; |
| 1199 | bool IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1200 | ///} |
| 1201 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1202 | public: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1203 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1204 | |
| 1205 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1206 | void initialize(Attributor &A) override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1207 | // Reset the state. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1208 | IsFixed = false; |
| 1209 | IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1210 | ReturnedValues.clear(); |
| 1211 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1212 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1213 | if (!F) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1214 | indicatePessimisticFixpoint(); |
| 1215 | return; |
| 1216 | } |
| Johannes Doerfert | e273ac4 | 2020-01-12 00:13:03 -0600 | [diff] [blame] | 1217 | assert(!F->getReturnType()->isVoidTy() && |
| 1218 | "Did not expect a void return type!"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1219 | |
| 1220 | // The map from instruction opcodes to those instructions in the function. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1221 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Look through all arguments, if one is marked as returned we are done. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1224 | for (Argument &Arg : F->args()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1225 | if (Arg.hasReturnedAttr()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1226 | auto &ReturnInstSet = ReturnedValues[&Arg]; |
| 1227 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) |
| 1228 | ReturnInstSet.insert(cast<ReturnInst>(RI)); |
| 1229 | |
| 1230 | indicateOptimisticFixpoint(); |
| 1231 | return; |
| 1232 | } |
| 1233 | } |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1234 | |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 1235 | if (!A.isFunctionIPOAmendable(*F)) |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1236 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | /// See AbstractAttribute::manifest(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1240 | ChangeStatus manifest(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1241 | |
| 1242 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1243 | AbstractState &getState() override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1244 | |
| 1245 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1246 | const AbstractState &getState() const override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1247 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1248 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1249 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1250 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1251 | llvm::iterator_range<iterator> returned_values() override { |
| 1252 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 1253 | } |
| 1254 | |
| 1255 | llvm::iterator_range<const_iterator> returned_values() const override { |
| 1256 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 1257 | } |
| 1258 | |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1259 | const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1260 | return UnresolvedCalls; |
| 1261 | } |
| 1262 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1263 | /// Return the number of potential return values, -1 if unknown. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1264 | size_t getNumReturnValues() const override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1265 | return isValidState() ? ReturnedValues.size() : -1; |
| 1266 | } |
| 1267 | |
| 1268 | /// Return an assumed unique return value if a single candidate is found. If |
| 1269 | /// there cannot be one, return a nullptr. If it is not clear yet, return the |
| 1270 | /// Optional::NoneType. |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1271 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1272 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1273 | /// See AbstractState::checkForAllReturnedValues(...). |
| 1274 | bool checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1275 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1276 | &Pred) const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1277 | |
| 1278 | /// Pretty print the attribute similar to the IR representation. |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1279 | const std::string getAsStr() const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1280 | |
| 1281 | /// See AbstractState::isAtFixpoint(). |
| 1282 | bool isAtFixpoint() const override { return IsFixed; } |
| 1283 | |
| 1284 | /// See AbstractState::isValidState(). |
| 1285 | bool isValidState() const override { return IsValidState; } |
| 1286 | |
| 1287 | /// See AbstractState::indicateOptimisticFixpoint(...). |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1288 | ChangeStatus indicateOptimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1289 | IsFixed = true; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1290 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1291 | } |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1292 | |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1293 | ChangeStatus indicatePessimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1294 | IsFixed = true; |
| 1295 | IsValidState = false; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1296 | return ChangeStatus::CHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1297 | } |
| 1298 | }; |
| 1299 | |
| 1300 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { |
| 1301 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 1302 | |
| 1303 | // Bookkeeping. |
| 1304 | assert(isValidState()); |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1305 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, |
| 1306 | "Number of function with known return values"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1307 | |
| 1308 | // Check if we have an assumed unique return value that we could manifest. |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1309 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1310 | |
| 1311 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) |
| 1312 | return Changed; |
| 1313 | |
| 1314 | // Bookkeeping. |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1315 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, |
| 1316 | "Number of function with unique return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1317 | |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1318 | // Callback to replace the uses of CB with the constant C. |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 1319 | auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) { |
| Johannes Doerfert | 54ec9b5 | 2020-03-16 20:23:27 -0500 | [diff] [blame^] | 1320 | if (CB.getNumUses() == 0) |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1321 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 4c62a35 | 2020-01-12 00:17:08 -0600 | [diff] [blame] | 1322 | if (A.changeValueAfterManifest(CB, C)) |
| 1323 | return ChangeStatus::CHANGED; |
| 1324 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1325 | }; |
| 1326 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1327 | // If the assumed unique return value is an argument, annotate it. |
| 1328 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { |
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 1329 | // TODO: This should be handled differently! |
| 1330 | this->AnchorVal = UniqueRVArg; |
| 1331 | this->KindOrArgNo = UniqueRVArg->getArgNo(); |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1332 | Changed = IRAttribute::manifest(A); |
| 1333 | } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { |
| 1334 | // We can replace the returned value with the unique returned constant. |
| 1335 | Value &AnchorValue = getAnchorValue(); |
| 1336 | if (Function *F = dyn_cast<Function>(&AnchorValue)) { |
| 1337 | for (const Use &U : F->uses()) |
| 1338 | if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) |
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1339 | if (CB->isCallee(&U)) { |
| 1340 | Constant *RVCCast = |
| Johannes Doerfert | 07d1642 | 2019-11-01 23:03:35 -0500 | [diff] [blame] | 1341 | CB->getType() == RVC->getType() |
| 1342 | ? RVC |
| 1343 | : ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); |
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1344 | Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; |
| 1345 | } |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1346 | } else { |
| 1347 | assert(isa<CallBase>(AnchorValue) && |
| 1348 | "Expcected a function or call base anchor!"); |
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1349 | Constant *RVCCast = |
| Johannes Doerfert | 07d1642 | 2019-11-01 23:03:35 -0500 | [diff] [blame] | 1350 | AnchorValue.getType() == RVC->getType() |
| 1351 | ? RVC |
| 1352 | : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); |
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1353 | Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1354 | } |
| 1355 | if (Changed == ChangeStatus::CHANGED) |
| 1356 | STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, |
| 1357 | "Number of function returns replaced by constant return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return Changed; |
| 1361 | } |
| 1362 | |
| 1363 | const std::string AAReturnedValuesImpl::getAsStr() const { |
| 1364 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + |
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 1365 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1366 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1369 | Optional<Value *> |
| 1370 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { |
| 1371 | // If checkForAllReturnedValues provides a unique value, ignoring potential |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1372 | // undef values that can also be present, it is assumed to be the actual |
| 1373 | // return value and forwarded to the caller of this method. If there are |
| 1374 | // multiple, a nullptr is returned indicating there cannot be a unique |
| 1375 | // returned value. |
| 1376 | Optional<Value *> UniqueRV; |
| 1377 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1378 | auto Pred = [&](Value &RV) -> bool { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1379 | // If we found a second returned value and neither the current nor the saved |
| 1380 | // one is an undef, there is no unique returned value. Undefs are special |
| 1381 | // since we can pretend they have any value. |
| 1382 | if (UniqueRV.hasValue() && UniqueRV != &RV && |
| 1383 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { |
| 1384 | UniqueRV = nullptr; |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
| 1388 | // Do not overwrite a value with an undef. |
| 1389 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) |
| 1390 | UniqueRV = &RV; |
| 1391 | |
| 1392 | return true; |
| 1393 | }; |
| 1394 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1395 | if (!A.checkForAllReturnedValues(Pred, *this)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1396 | UniqueRV = nullptr; |
| 1397 | |
| 1398 | return UniqueRV; |
| 1399 | } |
| 1400 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1401 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1402 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1403 | &Pred) const { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1404 | if (!isValidState()) |
| 1405 | return false; |
| 1406 | |
| 1407 | // Check all returned values but ignore call sites as long as we have not |
| 1408 | // encountered an overdefined one during an update. |
| 1409 | for (auto &It : ReturnedValues) { |
| 1410 | Value *RV = It.first; |
| 1411 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1412 | CallBase *CB = dyn_cast<CallBase>(RV); |
| 1413 | if (CB && !UnresolvedCalls.count(CB)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1414 | continue; |
| 1415 | |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1416 | if (!Pred(*RV, It.second)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | return true; |
| 1421 | } |
| 1422 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1423 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1424 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); |
| 1425 | bool Changed = false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1426 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1427 | // State used in the value traversals starting in returned values. |
| 1428 | struct RVState { |
| 1429 | // The map in which we collect return values -> return instrs. |
| 1430 | decltype(ReturnedValues) &RetValsMap; |
| 1431 | // The flag to indicate a change. |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1432 | bool &Changed; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1433 | // The return instrs we come from. |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1434 | SmallSetVector<ReturnInst *, 4> RetInsts; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1435 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1436 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1437 | // Callback for a leaf value returned by the associated function. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1438 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1439 | auto Size = RVS.RetValsMap[&Val].size(); |
| 1440 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); |
| 1441 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; |
| 1442 | RVS.Changed |= Inserted; |
| 1443 | LLVM_DEBUG({ |
| 1444 | if (Inserted) |
| 1445 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val |
| 1446 | << " => " << RVS.RetInsts.size() << "\n"; |
| 1447 | }); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1448 | return true; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1449 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1450 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1451 | // Helper method to invoke the generic value traversal. |
| 1452 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { |
| 1453 | IRPosition RetValPos = IRPosition::value(RV); |
| 1454 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, |
| 1455 | RVS, VisitValueCB); |
| 1456 | }; |
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 1457 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1458 | // Callback for all "return intructions" live in the associated function. |
| 1459 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { |
| 1460 | ReturnInst &Ret = cast<ReturnInst>(I); |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1461 | RVState RVS({ReturnedValues, Changed, {}}); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1462 | RVS.RetInsts.insert(&Ret); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1463 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); |
| 1464 | }; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1465 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1466 | // Start by discovering returned values from all live returned instructions in |
| 1467 | // the associated function. |
| 1468 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) |
| 1469 | return indicatePessimisticFixpoint(); |
| 1470 | |
| 1471 | // Once returned values "directly" present in the code are handled we try to |
| 1472 | // resolve returned calls. |
| 1473 | decltype(ReturnedValues) NewRVsMap; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1474 | for (auto &It : ReturnedValues) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1475 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first |
| 1476 | << " by #" << It.second.size() << " RIs\n"); |
| 1477 | CallBase *CB = dyn_cast<CallBase>(It.first); |
| 1478 | if (!CB || UnresolvedCalls.count(CB)) |
| 1479 | continue; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1480 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1481 | if (!CB->getCalledFunction()) { |
| 1482 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 1483 | << "\n"); |
| 1484 | UnresolvedCalls.insert(CB); |
| 1485 | continue; |
| 1486 | } |
| 1487 | |
| 1488 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 1489 | const auto &RetValAA = A.getAAFor<AAReturnedValues>( |
| 1490 | *this, IRPosition::function(*CB->getCalledFunction())); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1491 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 1492 | << RetValAA << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1493 | |
| 1494 | // Skip dead ends, thus if we do not know anything about the returned |
| 1495 | // call we mark it as unresolved and it will stay that way. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1496 | if (!RetValAA.getState().isValidState()) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1497 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 1498 | << "\n"); |
| 1499 | UnresolvedCalls.insert(CB); |
| 1500 | continue; |
| 1501 | } |
| 1502 | |
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1503 | // Do not try to learn partial information. If the callee has unresolved |
| 1504 | // return values we will treat the call as unresolved/opaque. |
| 1505 | auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); |
| 1506 | if (!RetValAAUnresolvedCalls.empty()) { |
| 1507 | UnresolvedCalls.insert(CB); |
| 1508 | continue; |
| 1509 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1510 | |
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1511 | // Now check if we can track transitively returned values. If possible, thus |
| 1512 | // if all return value can be represented in the current scope, do so. |
| 1513 | bool Unresolved = false; |
| 1514 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 1515 | Value *RetVal = RetValAAIt.first; |
| 1516 | if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || |
| 1517 | isa<Constant>(RetVal)) |
| 1518 | continue; |
| 1519 | // Anything that did not fit in the above categories cannot be resolved, |
| 1520 | // mark the call as unresolved. |
| 1521 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " |
| 1522 | "cannot be translated: " |
| 1523 | << *RetVal << "\n"); |
| 1524 | UnresolvedCalls.insert(CB); |
| 1525 | Unresolved = true; |
| 1526 | break; |
| 1527 | } |
| 1528 | |
| 1529 | if (Unresolved) |
| 1530 | continue; |
| 1531 | |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1532 | // Now track transitively returned values. |
| 1533 | unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; |
| 1534 | if (NumRetAA == RetValAA.getNumReturnValues()) { |
| 1535 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " |
| 1536 | "changed since it was seen last\n"); |
| 1537 | continue; |
| 1538 | } |
| 1539 | NumRetAA = RetValAA.getNumReturnValues(); |
| 1540 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1541 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 1542 | Value *RetVal = RetValAAIt.first; |
| 1543 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { |
| 1544 | // Arguments are mapped to call site operands and we begin the traversal |
| 1545 | // again. |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1546 | bool Unused = false; |
| 1547 | RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1548 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); |
| 1549 | continue; |
| 1550 | } else if (isa<CallBase>(RetVal)) { |
| 1551 | // Call sites are resolved by the callee attribute over time, no need to |
| 1552 | // do anything for us. |
| 1553 | continue; |
| 1554 | } else if (isa<Constant>(RetVal)) { |
| 1555 | // Constants are valid everywhere, we can simply take them. |
| 1556 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); |
| 1557 | continue; |
| 1558 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1559 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1562 | // To avoid modifications to the ReturnedValues map while we iterate over it |
| 1563 | // we kept record of potential new entries in a copy map, NewRVsMap. |
| 1564 | for (auto &It : NewRVsMap) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1565 | assert(!It.second.empty() && "Entry does not add anything."); |
| 1566 | auto &ReturnInsts = ReturnedValues[It.first]; |
| 1567 | for (ReturnInst *RI : It.second) |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1568 | if (ReturnInsts.insert(RI)) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1569 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " |
| 1570 | << *It.first << " => " << *RI << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1571 | Changed = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1575 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); |
| 1576 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1579 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { |
| 1580 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 1581 | |
| 1582 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1583 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1584 | }; |
| 1585 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1586 | /// Returned values information for a call sites. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1587 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { |
| 1588 | AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 1589 | |
| 1590 | /// See AbstractAttribute::initialize(...). |
| 1591 | void initialize(Attributor &A) override { |
| 1592 | // TODO: Once we have call site specific value information we can provide |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1593 | // call site specific liveness information and then it makes |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1594 | // sense to specialize attributes for call sites instead of |
| 1595 | // redirecting requests to the callee. |
| 1596 | llvm_unreachable("Abstract attributes for returned values are not " |
| 1597 | "supported for call sites yet!"); |
| 1598 | } |
| 1599 | |
| 1600 | /// See AbstractAttribute::updateImpl(...). |
| 1601 | ChangeStatus updateImpl(Attributor &A) override { |
| 1602 | return indicatePessimisticFixpoint(); |
| 1603 | } |
| 1604 | |
| 1605 | /// See AbstractAttribute::trackStatistics() |
| 1606 | void trackStatistics() const override {} |
| 1607 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1608 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1609 | /// ------------------------ NoSync Function Attribute ------------------------- |
| 1610 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1611 | struct AANoSyncImpl : AANoSync { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1612 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1613 | |
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 1614 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1615 | return getAssumed() ? "nosync" : "may-sync"; |
| 1616 | } |
| 1617 | |
| 1618 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1619 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1620 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1621 | /// Helper function used to determine whether an instruction is non-relaxed |
| 1622 | /// atomic. In other words, if an atomic instruction does not have unordered |
| 1623 | /// or monotonic ordering |
| 1624 | static bool isNonRelaxedAtomic(Instruction *I); |
| 1625 | |
| 1626 | /// Helper function used to determine whether an instruction is volatile. |
| 1627 | static bool isVolatile(Instruction *I); |
| 1628 | |
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 1629 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, |
| 1630 | /// memset). |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1631 | static bool isNoSyncIntrinsic(Instruction *I); |
| 1632 | }; |
| 1633 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1634 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1635 | if (!I->isAtomic()) |
| 1636 | return false; |
| 1637 | |
| 1638 | AtomicOrdering Ordering; |
| 1639 | switch (I->getOpcode()) { |
| 1640 | case Instruction::AtomicRMW: |
| 1641 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); |
| 1642 | break; |
| 1643 | case Instruction::Store: |
| 1644 | Ordering = cast<StoreInst>(I)->getOrdering(); |
| 1645 | break; |
| 1646 | case Instruction::Load: |
| 1647 | Ordering = cast<LoadInst>(I)->getOrdering(); |
| 1648 | break; |
| 1649 | case Instruction::Fence: { |
| 1650 | auto *FI = cast<FenceInst>(I); |
| 1651 | if (FI->getSyncScopeID() == SyncScope::SingleThread) |
| 1652 | return false; |
| 1653 | Ordering = FI->getOrdering(); |
| 1654 | break; |
| 1655 | } |
| 1656 | case Instruction::AtomicCmpXchg: { |
| 1657 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); |
| 1658 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); |
| 1659 | // Only if both are relaxed, than it can be treated as relaxed. |
| 1660 | // Otherwise it is non-relaxed. |
| 1661 | if (Success != AtomicOrdering::Unordered && |
| 1662 | Success != AtomicOrdering::Monotonic) |
| 1663 | return true; |
| 1664 | if (Failure != AtomicOrdering::Unordered && |
| 1665 | Failure != AtomicOrdering::Monotonic) |
| 1666 | return true; |
| 1667 | return false; |
| 1668 | } |
| 1669 | default: |
| 1670 | llvm_unreachable( |
| 1671 | "New atomic operations need to be known in the attributor."); |
| 1672 | } |
| 1673 | |
| 1674 | // Relaxed. |
| 1675 | if (Ordering == AtomicOrdering::Unordered || |
| 1676 | Ordering == AtomicOrdering::Monotonic) |
| 1677 | return false; |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
| 1681 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. |
| 1682 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1683 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1684 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 1685 | switch (II->getIntrinsicID()) { |
| 1686 | /// Element wise atomic memory intrinsics are can only be unordered, |
| 1687 | /// therefore nosync. |
| 1688 | case Intrinsic::memset_element_unordered_atomic: |
| 1689 | case Intrinsic::memmove_element_unordered_atomic: |
| 1690 | case Intrinsic::memcpy_element_unordered_atomic: |
| 1691 | return true; |
| 1692 | case Intrinsic::memset: |
| 1693 | case Intrinsic::memmove: |
| 1694 | case Intrinsic::memcpy: |
| 1695 | if (!cast<MemIntrinsic>(II)->isVolatile()) |
| 1696 | return true; |
| 1697 | return false; |
| 1698 | default: |
| 1699 | return false; |
| 1700 | } |
| 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1705 | bool AANoSyncImpl::isVolatile(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1706 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && |
| 1707 | "Calls should not be checked here"); |
| 1708 | |
| 1709 | switch (I->getOpcode()) { |
| 1710 | case Instruction::AtomicRMW: |
| 1711 | return cast<AtomicRMWInst>(I)->isVolatile(); |
| 1712 | case Instruction::Store: |
| 1713 | return cast<StoreInst>(I)->isVolatile(); |
| 1714 | case Instruction::Load: |
| 1715 | return cast<LoadInst>(I)->isVolatile(); |
| 1716 | case Instruction::AtomicCmpXchg: |
| 1717 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); |
| 1718 | default: |
| 1719 | return false; |
| 1720 | } |
| 1721 | } |
| 1722 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1723 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1724 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1725 | auto CheckRWInstForNoSync = [&](Instruction &I) { |
| 1726 | /// We are looking for volatile instructions or Non-Relaxed atomics. |
| Pankaj Gode | b9a26a8 | 2019-11-22 14:46:43 +0530 | [diff] [blame] | 1727 | /// FIXME: We should improve the handling of intrinsics. |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1728 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1729 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) |
| 1730 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1731 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1732 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 1733 | if (ICS.hasFnAttr(Attribute::NoSync)) |
| 1734 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1735 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1736 | const auto &NoSyncAA = |
| 1737 | A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); |
| 1738 | if (NoSyncAA.isAssumedNoSync()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1739 | return true; |
| 1740 | return false; |
| 1741 | } |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1742 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1743 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) |
| 1744 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1745 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1746 | return false; |
| 1747 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1748 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1749 | auto CheckForNoSync = [&](Instruction &I) { |
| 1750 | // At this point we handled all read/write effects and they are all |
| 1751 | // nosync, so they can be skipped. |
| 1752 | if (I.mayReadOrWriteMemory()) |
| 1753 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1754 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1755 | // non-convergent and readnone imply nosync. |
| 1756 | return !ImmutableCallSite(&I).isConvergent(); |
| 1757 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1758 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1759 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || |
| 1760 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1761 | return indicatePessimisticFixpoint(); |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1762 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1763 | return ChangeStatus::UNCHANGED; |
| 1764 | } |
| 1765 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1766 | struct AANoSyncFunction final : public AANoSyncImpl { |
| 1767 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} |
| 1768 | |
| 1769 | /// See AbstractAttribute::trackStatistics() |
| 1770 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } |
| 1771 | }; |
| 1772 | |
| 1773 | /// NoSync attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1774 | struct AANoSyncCallSite final : AANoSyncImpl { |
| 1775 | AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} |
| 1776 | |
| 1777 | /// See AbstractAttribute::initialize(...). |
| 1778 | void initialize(Attributor &A) override { |
| 1779 | AANoSyncImpl::initialize(A); |
| 1780 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1781 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1782 | indicatePessimisticFixpoint(); |
| 1783 | } |
| 1784 | |
| 1785 | /// See AbstractAttribute::updateImpl(...). |
| 1786 | ChangeStatus updateImpl(Attributor &A) override { |
| 1787 | // TODO: Once we have call site specific value information we can provide |
| 1788 | // call site specific liveness information and then it makes |
| 1789 | // sense to specialize attributes for call sites arguments instead of |
| 1790 | // redirecting requests to the callee argument. |
| 1791 | Function *F = getAssociatedFunction(); |
| 1792 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1793 | auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); |
| 1794 | return clampStateAndIndicateChange( |
| 1795 | getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); |
| 1796 | } |
| 1797 | |
| 1798 | /// See AbstractAttribute::trackStatistics() |
| 1799 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } |
| 1800 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1801 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1802 | /// ------------------------ No-Free Attributes ---------------------------- |
| 1803 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1804 | struct AANoFreeImpl : public AANoFree { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1805 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1806 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1807 | /// See AbstractAttribute::updateImpl(...). |
| 1808 | ChangeStatus updateImpl(Attributor &A) override { |
| 1809 | auto CheckForNoFree = [&](Instruction &I) { |
| 1810 | ImmutableCallSite ICS(&I); |
| 1811 | if (ICS.hasFnAttr(Attribute::NoFree)) |
| 1812 | return true; |
| 1813 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1814 | const auto &NoFreeAA = |
| 1815 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); |
| 1816 | return NoFreeAA.isAssumedNoFree(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1817 | }; |
| 1818 | |
| 1819 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) |
| 1820 | return indicatePessimisticFixpoint(); |
| 1821 | return ChangeStatus::UNCHANGED; |
| 1822 | } |
| 1823 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1824 | /// See AbstractAttribute::getAsStr(). |
| 1825 | const std::string getAsStr() const override { |
| 1826 | return getAssumed() ? "nofree" : "may-free"; |
| 1827 | } |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1828 | }; |
| 1829 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1830 | struct AANoFreeFunction final : public AANoFreeImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1831 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1832 | |
| 1833 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1834 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1835 | }; |
| 1836 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1837 | /// NoFree attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1838 | struct AANoFreeCallSite final : AANoFreeImpl { |
| 1839 | AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| 1840 | |
| 1841 | /// See AbstractAttribute::initialize(...). |
| 1842 | void initialize(Attributor &A) override { |
| 1843 | AANoFreeImpl::initialize(A); |
| 1844 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1845 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1846 | indicatePessimisticFixpoint(); |
| 1847 | } |
| 1848 | |
| 1849 | /// See AbstractAttribute::updateImpl(...). |
| 1850 | ChangeStatus updateImpl(Attributor &A) override { |
| 1851 | // TODO: Once we have call site specific value information we can provide |
| 1852 | // call site specific liveness information and then it makes |
| 1853 | // sense to specialize attributes for call sites arguments instead of |
| 1854 | // redirecting requests to the callee argument. |
| 1855 | Function *F = getAssociatedFunction(); |
| 1856 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1857 | auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); |
| 1858 | return clampStateAndIndicateChange( |
| 1859 | getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); |
| 1860 | } |
| 1861 | |
| 1862 | /// See AbstractAttribute::trackStatistics() |
| 1863 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } |
| 1864 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1865 | |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1866 | /// NoFree attribute for floating values. |
| 1867 | struct AANoFreeFloating : AANoFreeImpl { |
| 1868 | AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| 1869 | |
| 1870 | /// See AbstractAttribute::trackStatistics() |
| 1871 | void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)} |
| 1872 | |
| 1873 | /// See Abstract Attribute::updateImpl(...). |
| 1874 | ChangeStatus updateImpl(Attributor &A) override { |
| 1875 | const IRPosition &IRP = getIRPosition(); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1876 | |
| 1877 | const auto &NoFreeAA = |
| 1878 | A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP)); |
| 1879 | if (NoFreeAA.isAssumedNoFree()) |
| 1880 | return ChangeStatus::UNCHANGED; |
| 1881 | |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1882 | Value &AssociatedValue = getIRPosition().getAssociatedValue(); |
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1883 | auto Pred = [&](const Use &U, bool &Follow) -> bool { |
| 1884 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1885 | if (auto *CB = dyn_cast<CallBase>(UserI)) { |
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1886 | if (CB->isBundleOperand(&U)) |
| 1887 | return false; |
| 1888 | if (!CB->isArgOperand(&U)) |
| 1889 | return true; |
| 1890 | unsigned ArgNo = CB->getArgOperandNo(&U); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1891 | |
| 1892 | const auto &NoFreeArg = A.getAAFor<AANoFree>( |
| 1893 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1894 | return NoFreeArg.isAssumedNoFree(); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || |
| 1898 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { |
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1899 | Follow = true; |
| 1900 | return true; |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 1901 | } |
| Johannes Doerfert | f955539 | 2020-01-10 14:49:45 -0600 | [diff] [blame] | 1902 | if (isa<ReturnInst>(UserI)) |
| 1903 | return true; |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1904 | |
| 1905 | // Unknown user. |
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1906 | return false; |
| 1907 | }; |
| 1908 | if (!A.checkForAllUses(Pred, *this, AssociatedValue)) |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1909 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1910 | |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1911 | return ChangeStatus::UNCHANGED; |
| 1912 | } |
| 1913 | }; |
| 1914 | |
| 1915 | /// NoFree attribute for a call site argument. |
| 1916 | struct AANoFreeArgument final : AANoFreeFloating { |
| 1917 | AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} |
| 1918 | |
| 1919 | /// See AbstractAttribute::trackStatistics() |
| 1920 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) } |
| 1921 | }; |
| 1922 | |
| 1923 | /// NoFree attribute for call site arguments. |
| 1924 | struct AANoFreeCallSiteArgument final : AANoFreeFloating { |
| 1925 | AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} |
| 1926 | |
| 1927 | /// See AbstractAttribute::updateImpl(...). |
| 1928 | ChangeStatus updateImpl(Attributor &A) override { |
| 1929 | // TODO: Once we have call site specific value information we can provide |
| 1930 | // call site specific liveness information and then it makes |
| 1931 | // sense to specialize attributes for call sites arguments instead of |
| 1932 | // redirecting requests to the callee argument. |
| 1933 | Argument *Arg = getAssociatedArgument(); |
| 1934 | if (!Arg) |
| 1935 | return indicatePessimisticFixpoint(); |
| 1936 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 1937 | auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos); |
| 1938 | return clampStateAndIndicateChange( |
| 1939 | getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState())); |
| 1940 | } |
| 1941 | |
| 1942 | /// See AbstractAttribute::trackStatistics() |
| 1943 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)}; |
| 1944 | }; |
| 1945 | |
| 1946 | /// NoFree attribute for function return value. |
| 1947 | struct AANoFreeReturned final : AANoFreeFloating { |
| 1948 | AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) { |
| 1949 | llvm_unreachable("NoFree is not applicable to function returns!"); |
| 1950 | } |
| 1951 | |
| 1952 | /// See AbstractAttribute::initialize(...). |
| 1953 | void initialize(Attributor &A) override { |
| 1954 | llvm_unreachable("NoFree is not applicable to function returns!"); |
| 1955 | } |
| 1956 | |
| 1957 | /// See AbstractAttribute::updateImpl(...). |
| 1958 | ChangeStatus updateImpl(Attributor &A) override { |
| 1959 | llvm_unreachable("NoFree is not applicable to function returns!"); |
| 1960 | } |
| 1961 | |
| 1962 | /// See AbstractAttribute::trackStatistics() |
| 1963 | void trackStatistics() const override {} |
| 1964 | }; |
| 1965 | |
| 1966 | /// NoFree attribute deduction for a call site return value. |
| 1967 | struct AANoFreeCallSiteReturned final : AANoFreeFloating { |
| 1968 | AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {} |
| 1969 | |
| 1970 | ChangeStatus manifest(Attributor &A) override { |
| 1971 | return ChangeStatus::UNCHANGED; |
| 1972 | } |
| 1973 | /// See AbstractAttribute::trackStatistics() |
| 1974 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) } |
| 1975 | }; |
| 1976 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1977 | /// ------------------------ NonNull Argument Attribute ------------------------ |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1978 | static int64_t getKnownNonNullAndDerefBytesForUse( |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 1979 | Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue, |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1980 | const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1981 | TrackUse = false; |
| 1982 | |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1983 | const Value *UseV = U->get(); |
| 1984 | if (!UseV->getType()->isPointerTy()) |
| 1985 | return 0; |
| 1986 | |
| 1987 | Type *PtrTy = UseV->getType(); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1988 | const Function *F = I->getFunction(); |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1989 | bool NullPointerIsDefined = |
| 1990 | F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1991 | const DataLayout &DL = A.getInfoCache().getDL(); |
| 1992 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { |
| 1993 | if (ICS.isBundleOperand(U)) |
| 1994 | return 0; |
| 1995 | |
| 1996 | if (ICS.isCallee(U)) { |
| 1997 | IsNonNull |= !NullPointerIsDefined; |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
| 2001 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 2002 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); |
| Johannes Doerfert | 77a6b35 | 2019-11-02 14:47:45 -0500 | [diff] [blame] | 2003 | // As long as we only use known information there is no need to track |
| 2004 | // dependences here. |
| 2005 | auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP, |
| 2006 | /* TrackDependence */ false); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2007 | IsNonNull |= DerefAA.isKnownNonNull(); |
| 2008 | return DerefAA.getKnownDereferenceableBytes(); |
| 2009 | } |
| 2010 | |
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 2011 | // We need to follow common pointer manipulation uses to the accesses they |
| 2012 | // feed into. We can try to be smart to avoid looking through things we do not |
| 2013 | // like for now, e.g., non-inbounds GEPs. |
| 2014 | if (isa<CastInst>(I)) { |
| 2015 | TrackUse = true; |
| 2016 | return 0; |
| 2017 | } |
| 2018 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) |
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 2019 | if (GEP->hasAllConstantIndices()) { |
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 2020 | TrackUse = true; |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2024 | int64_t Offset; |
| 2025 | if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2026 | if (Base == &AssociatedValue && |
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 2027 | getPointerOperand(I, /* AllowVolatile */ false) == UseV) { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2028 | int64_t DerefBytes = |
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 2029 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset; |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2030 | |
| 2031 | IsNonNull |= !NullPointerIsDefined; |
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 2032 | return std::max(int64_t(0), DerefBytes); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 2035 | |
| 2036 | /// Corner case when an offset is 0. |
| 2037 | if (const Value *Base = getBasePointerOfAccessPointerOperand( |
| 2038 | I, Offset, DL, /*AllowNonInbounds*/ true)) { |
| 2039 | if (Offset == 0 && Base == &AssociatedValue && |
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 2040 | getPointerOperand(I, /* AllowVolatile */ false) == UseV) { |
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 2041 | int64_t DerefBytes = |
| 2042 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); |
| 2043 | IsNonNull |= !NullPointerIsDefined; |
| 2044 | return std::max(int64_t(0), DerefBytes); |
| 2045 | } |
| 2046 | } |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2047 | |
| 2048 | return 0; |
| 2049 | } |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 2050 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2051 | struct AANonNullImpl : AANonNull { |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 2052 | AANonNullImpl(const IRPosition &IRP) |
| 2053 | : AANonNull(IRP), |
| 2054 | NullIsDefined(NullPointerIsDefined( |
| 2055 | getAnchorScope(), |
| 2056 | getAssociatedValue().getType()->getPointerAddressSpace())) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2057 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2058 | /// See AbstractAttribute::initialize(...). |
| 2059 | void initialize(Attributor &A) override { |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 2060 | if (!NullIsDefined && |
| 2061 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2062 | indicateOptimisticFixpoint(); |
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 2063 | else if (isa<ConstantPointerNull>(getAssociatedValue())) |
| 2064 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2065 | else |
| 2066 | AANonNull::initialize(A); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2069 | /// See AAFromMustBeExecutedContext |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 2070 | bool followUse(Attributor &A, const Use *U, const Instruction *I, |
| 2071 | AANonNull::StateType &State) { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2072 | bool IsNonNull = false; |
| 2073 | bool TrackUse = false; |
| 2074 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, |
| 2075 | IsNonNull, TrackUse); |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 2076 | State.setKnown(IsNonNull); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2077 | return TrackUse; |
| 2078 | } |
| 2079 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2080 | /// See AbstractAttribute::getAsStr(). |
| 2081 | const std::string getAsStr() const override { |
| 2082 | return getAssumed() ? "nonnull" : "may-null"; |
| 2083 | } |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 2084 | |
| 2085 | /// Flag to determine if the underlying value can be null and still allow |
| 2086 | /// valid accesses. |
| 2087 | const bool NullIsDefined; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2088 | }; |
| 2089 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2090 | /// NonNull attribute for a floating value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2091 | struct AANonNullFloating |
| 2092 | : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { |
| 2093 | using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; |
| 2094 | AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2095 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2096 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2097 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2098 | ChangeStatus Change = Base::updateImpl(A); |
| 2099 | if (isKnownNonNull()) |
| 2100 | return Change; |
| 2101 | |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 2102 | if (!NullIsDefined) { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 2103 | const auto &DerefAA = |
| 2104 | A.getAAFor<AADereferenceable>(*this, getIRPosition()); |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 2105 | if (DerefAA.getAssumedDereferenceableBytes()) |
| 2106 | return Change; |
| 2107 | } |
| 2108 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2109 | const DataLayout &DL = A.getDataLayout(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2110 | |
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 2111 | DominatorTree *DT = nullptr; |
| Hideto Ueno | 2c0edbf | 2020-02-25 12:32:50 +0900 | [diff] [blame] | 2112 | AssumptionCache *AC = nullptr; |
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 2113 | InformationCache &InfoCache = A.getInfoCache(); |
| Hideto Ueno | 2c0edbf | 2020-02-25 12:32:50 +0900 | [diff] [blame] | 2114 | if (const Function *Fn = getAnchorScope()) { |
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 2115 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); |
| Hideto Ueno | 2c0edbf | 2020-02-25 12:32:50 +0900 | [diff] [blame] | 2116 | AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn); |
| 2117 | } |
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 2118 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 2119 | auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2120 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2121 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); |
| 2122 | if (!Stripped && this == &AA) { |
| Hideto Ueno | 2c0edbf | 2020-02-25 12:32:50 +0900 | [diff] [blame] | 2123 | if (!isKnownNonZero(&V, DL, 0, AC, getCtxI(), DT)) |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2124 | T.indicatePessimisticFixpoint(); |
| 2125 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2126 | // Use abstract attribute information. |
| 2127 | const AANonNull::StateType &NS = |
| 2128 | static_cast<const AANonNull::StateType &>(AA.getState()); |
| 2129 | T ^= NS; |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2130 | } |
| 2131 | return T.isValidState(); |
| 2132 | }; |
| 2133 | |
| 2134 | StateType T; |
| 2135 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, |
| 2136 | T, VisitValueCB)) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2137 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2138 | |
| 2139 | return clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2140 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2141 | |
| 2142 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2143 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2144 | }; |
| 2145 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2146 | /// NonNull attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2147 | struct AANonNullReturned final |
| 2148 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2149 | AANonNullReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2150 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2151 | |
| 2152 | /// See AbstractAttribute::trackStatistics() |
| 2153 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| 2154 | }; |
| 2155 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2156 | /// NonNull attribute for function argument. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2157 | struct AANonNullArgument final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2158 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, |
| 2159 | AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2160 | AANonNullArgument(const IRPosition &IRP) |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2161 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, |
| 2162 | AANonNullImpl>( |
| 2163 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2164 | |
| 2165 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2166 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2167 | }; |
| 2168 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2169 | struct AANonNullCallSiteArgument final : AANonNullFloating { |
| 2170 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2171 | |
| 2172 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 2173 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2174 | }; |
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 2175 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2176 | /// NonNull attribute for a call site return position. |
| 2177 | struct AANonNullCallSiteReturned final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2178 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, |
| 2179 | AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2180 | AANonNullCallSiteReturned(const IRPosition &IRP) |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2181 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, |
| 2182 | AANonNullImpl>( |
| 2183 | IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2184 | |
| 2185 | /// See AbstractAttribute::trackStatistics() |
| 2186 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } |
| 2187 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2188 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2189 | /// ------------------------ No-Recurse Attributes ---------------------------- |
| 2190 | |
| 2191 | struct AANoRecurseImpl : public AANoRecurse { |
| 2192 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} |
| 2193 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2194 | /// See AbstractAttribute::getAsStr() |
| 2195 | const std::string getAsStr() const override { |
| 2196 | return getAssumed() ? "norecurse" : "may-recurse"; |
| 2197 | } |
| 2198 | }; |
| 2199 | |
| 2200 | struct AANoRecurseFunction final : AANoRecurseImpl { |
| 2201 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 2202 | |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2203 | /// See AbstractAttribute::initialize(...). |
| 2204 | void initialize(Attributor &A) override { |
| 2205 | AANoRecurseImpl::initialize(A); |
| 2206 | if (const Function *F = getAnchorScope()) |
| Johannes Doerfert | 26d02b0 | 2019-12-30 16:15:38 -0600 | [diff] [blame] | 2207 | if (A.getInfoCache().getSccSize(*F) != 1) |
| 2208 | indicatePessimisticFixpoint(); |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2211 | /// See AbstractAttribute::updateImpl(...). |
| 2212 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2213 | |
| Johannes Doerfert | 26d02b0 | 2019-12-30 16:15:38 -0600 | [diff] [blame] | 2214 | // If all live call sites are known to be no-recurse, we are as well. |
| 2215 | auto CallSitePred = [&](AbstractCallSite ACS) { |
| 2216 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( |
| 2217 | *this, IRPosition::function(*ACS.getInstruction()->getFunction()), |
| 2218 | /* TrackDependence */ false, DepClassTy::OPTIONAL); |
| 2219 | return NoRecurseAA.isKnownNoRecurse(); |
| 2220 | }; |
| 2221 | bool AllCallSitesKnown; |
| 2222 | if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) { |
| 2223 | // If we know all call sites and all are known no-recurse, we are done. |
| 2224 | // If all known call sites, which might not be all that exist, are known |
| 2225 | // to be no-recurse, we are not done but we can continue to assume |
| 2226 | // no-recurse. If one of the call sites we have not visited will become |
| 2227 | // live, another update is triggered. |
| 2228 | if (AllCallSitesKnown) |
| 2229 | indicateOptimisticFixpoint(); |
| 2230 | return ChangeStatus::UNCHANGED; |
| 2231 | } |
| 2232 | |
| 2233 | // If the above check does not hold anymore we look at the calls. |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2234 | auto CheckForNoRecurse = [&](Instruction &I) { |
| 2235 | ImmutableCallSite ICS(&I); |
| 2236 | if (ICS.hasFnAttr(Attribute::NoRecurse)) |
| 2237 | return true; |
| 2238 | |
| 2239 | const auto &NoRecurseAA = |
| 2240 | A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); |
| 2241 | if (!NoRecurseAA.isAssumedNoRecurse()) |
| 2242 | return false; |
| 2243 | |
| 2244 | // Recursion to the same function |
| 2245 | if (ICS.getCalledFunction() == getAnchorScope()) |
| 2246 | return false; |
| 2247 | |
| 2248 | return true; |
| 2249 | }; |
| 2250 | |
| 2251 | if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) |
| 2252 | return indicatePessimisticFixpoint(); |
| 2253 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } |
| 2257 | }; |
| 2258 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2259 | /// NoRecurse attribute deduction for a call sites. |
| 2260 | struct AANoRecurseCallSite final : AANoRecurseImpl { |
| 2261 | AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 2262 | |
| 2263 | /// See AbstractAttribute::initialize(...). |
| 2264 | void initialize(Attributor &A) override { |
| 2265 | AANoRecurseImpl::initialize(A); |
| 2266 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2267 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2268 | indicatePessimisticFixpoint(); |
| 2269 | } |
| 2270 | |
| 2271 | /// See AbstractAttribute::updateImpl(...). |
| 2272 | ChangeStatus updateImpl(Attributor &A) override { |
| 2273 | // TODO: Once we have call site specific value information we can provide |
| 2274 | // call site specific liveness information and then it makes |
| 2275 | // sense to specialize attributes for call sites arguments instead of |
| 2276 | // redirecting requests to the callee argument. |
| 2277 | Function *F = getAssociatedFunction(); |
| 2278 | const IRPosition &FnPos = IRPosition::function(*F); |
| 2279 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); |
| 2280 | return clampStateAndIndicateChange( |
| 2281 | getState(), |
| 2282 | static_cast<const AANoRecurse::StateType &>(FnAA.getState())); |
| 2283 | } |
| 2284 | |
| 2285 | /// See AbstractAttribute::trackStatistics() |
| 2286 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } |
| 2287 | }; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2288 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2289 | /// -------------------- Undefined-Behavior Attributes ------------------------ |
| 2290 | |
| 2291 | struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior { |
| 2292 | AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {} |
| 2293 | |
| 2294 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2295 | // through a pointer (i.e. also branches etc.) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2296 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2297 | const size_t UBPrevSize = KnownUBInsts.size(); |
| 2298 | const size_t NoUBPrevSize = AssumedNoUBInsts.size(); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2299 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2300 | auto InspectMemAccessInstForUB = [&](Instruction &I) { |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2301 | // Skip instructions that are already saved. |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2302 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2303 | return true; |
| 2304 | |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2305 | // If we reach here, we know we have an instruction |
| 2306 | // that accesses memory through a pointer operand, |
| 2307 | // for which getPointerOperand() should give it to us. |
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 2308 | const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true); |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2309 | assert(PtrOp && |
| 2310 | "Expected pointer operand of memory accessing instruction"); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2311 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2312 | // A memory access through a pointer is considered UB |
| 2313 | // only if the pointer has constant null value. |
| 2314 | // TODO: Expand it to not only check constant values. |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2315 | if (!isa<ConstantPointerNull>(PtrOp)) { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2316 | AssumedNoUBInsts.insert(&I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2317 | return true; |
| 2318 | } |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2319 | const Type *PtrTy = PtrOp->getType(); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2320 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2321 | // Because we only consider instructions inside functions, |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2322 | // assume that a parent function exists. |
| 2323 | const Function *F = I.getFunction(); |
| 2324 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2325 | // A memory access using constant null pointer is only considered UB |
| 2326 | // if null pointer is _not_ defined for the target platform. |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2327 | if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace())) |
| 2328 | AssumedNoUBInsts.insert(&I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2329 | else |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2330 | KnownUBInsts.insert(&I); |
| 2331 | return true; |
| 2332 | }; |
| 2333 | |
| 2334 | auto InspectBrInstForUB = [&](Instruction &I) { |
| 2335 | // A conditional branch instruction is considered UB if it has `undef` |
| 2336 | // condition. |
| 2337 | |
| 2338 | // Skip instructions that are already saved. |
| 2339 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) |
| 2340 | return true; |
| 2341 | |
| 2342 | // We know we have a branch instruction. |
| 2343 | auto BrInst = cast<BranchInst>(&I); |
| 2344 | |
| 2345 | // Unconditional branches are never considered UB. |
| 2346 | if (BrInst->isUnconditional()) |
| 2347 | return true; |
| 2348 | |
| 2349 | // Either we stopped and the appropriate action was taken, |
| 2350 | // or we got back a simplified value to continue. |
| 2351 | Optional<Value *> SimplifiedCond = |
| 2352 | stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst); |
| 2353 | if (!SimplifiedCond.hasValue()) |
| 2354 | return true; |
| 2355 | AssumedNoUBInsts.insert(&I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2356 | return true; |
| 2357 | }; |
| 2358 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2359 | A.checkForAllInstructions(InspectMemAccessInstForUB, *this, |
| 2360 | {Instruction::Load, Instruction::Store, |
| 2361 | Instruction::AtomicCmpXchg, |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 2362 | Instruction::AtomicRMW}, |
| 2363 | /* CheckBBLivenessOnly */ true); |
| 2364 | A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br}, |
| 2365 | /* CheckBBLivenessOnly */ true); |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2366 | if (NoUBPrevSize != AssumedNoUBInsts.size() || |
| 2367 | UBPrevSize != KnownUBInsts.size()) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2368 | return ChangeStatus::CHANGED; |
| 2369 | return ChangeStatus::UNCHANGED; |
| 2370 | } |
| 2371 | |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2372 | bool isKnownToCauseUB(Instruction *I) const override { |
| 2373 | return KnownUBInsts.count(I); |
| 2374 | } |
| 2375 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2376 | bool isAssumedToCauseUB(Instruction *I) const override { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2377 | // In simple words, if an instruction is not in the assumed to _not_ |
| 2378 | // cause UB, then it is assumed UB (that includes those |
| 2379 | // in the KnownUBInsts set). The rest is boilerplate |
| 2380 | // is to ensure that it is one of the instructions we test |
| 2381 | // for UB. |
| 2382 | |
| 2383 | switch (I->getOpcode()) { |
| 2384 | case Instruction::Load: |
| 2385 | case Instruction::Store: |
| 2386 | case Instruction::AtomicCmpXchg: |
| 2387 | case Instruction::AtomicRMW: |
| 2388 | return !AssumedNoUBInsts.count(I); |
| 2389 | case Instruction::Br: { |
| 2390 | auto BrInst = cast<BranchInst>(I); |
| 2391 | if (BrInst->isUnconditional()) |
| 2392 | return false; |
| 2393 | return !AssumedNoUBInsts.count(I); |
| 2394 | } break; |
| 2395 | default: |
| 2396 | return false; |
| 2397 | } |
| 2398 | return false; |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | ChangeStatus manifest(Attributor &A) override { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2402 | if (KnownUBInsts.empty()) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2403 | return ChangeStatus::UNCHANGED; |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2404 | for (Instruction *I : KnownUBInsts) |
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 2405 | A.changeToUnreachableAfterManifest(I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2406 | return ChangeStatus::CHANGED; |
| 2407 | } |
| 2408 | |
| 2409 | /// See AbstractAttribute::getAsStr() |
| 2410 | const std::string getAsStr() const override { |
| 2411 | return getAssumed() ? "undefined-behavior" : "no-ub"; |
| 2412 | } |
| 2413 | |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2414 | /// Note: The correctness of this analysis depends on the fact that the |
| 2415 | /// following 2 sets will stop changing after some point. |
| 2416 | /// "Change" here means that their size changes. |
| 2417 | /// The size of each set is monotonically increasing |
| 2418 | /// (we only add items to them) and it is upper bounded by the number of |
| 2419 | /// instructions in the processed function (we can never save more |
| 2420 | /// elements in either set than this number). Hence, at some point, |
| 2421 | /// they will stop increasing. |
| 2422 | /// Consequently, at some point, both sets will have stopped |
| 2423 | /// changing, effectively making the analysis reach a fixpoint. |
| 2424 | |
| 2425 | /// Note: These 2 sets are disjoint and an instruction can be considered |
| 2426 | /// one of 3 things: |
| 2427 | /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in |
| 2428 | /// the KnownUBInsts set. |
| 2429 | /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior |
| 2430 | /// has a reason to assume it). |
| 2431 | /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior |
| 2432 | /// could not find a reason to assume or prove that it can cause UB, |
| 2433 | /// hence it assumes it doesn't. We have a set for these instructions |
| 2434 | /// so that we don't reprocess them in every update. |
| 2435 | /// Note however that instructions in this set may cause UB. |
| 2436 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2437 | protected: |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2438 | /// A set of all live instructions _known_ to cause UB. |
| 2439 | SmallPtrSet<Instruction *, 8> KnownUBInsts; |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2440 | |
| 2441 | private: |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2442 | /// A set of all the (live) instructions that are assumed to _not_ cause UB. |
| 2443 | SmallPtrSet<Instruction *, 8> AssumedNoUBInsts; |
| 2444 | |
| 2445 | // Should be called on updates in which if we're processing an instruction |
| 2446 | // \p I that depends on a value \p V, one of the following has to happen: |
| 2447 | // - If the value is assumed, then stop. |
| 2448 | // - If the value is known but undef, then consider it UB. |
| 2449 | // - Otherwise, do specific processing with the simplified value. |
| 2450 | // We return None in the first 2 cases to signify that an appropriate |
| 2451 | // action was taken and the caller should stop. |
| 2452 | // Otherwise, we return the simplified value that the caller should |
| 2453 | // use for specific processing. |
| 2454 | Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V, |
| 2455 | Instruction *I) { |
| 2456 | const auto &ValueSimplifyAA = |
| 2457 | A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V)); |
| 2458 | Optional<Value *> SimplifiedV = |
| 2459 | ValueSimplifyAA.getAssumedSimplifiedValue(A); |
| 2460 | if (!ValueSimplifyAA.isKnown()) { |
| 2461 | // Don't depend on assumed values. |
| 2462 | return llvm::None; |
| 2463 | } |
| 2464 | if (!SimplifiedV.hasValue()) { |
| 2465 | // If it is known (which we tested above) but it doesn't have a value, |
| 2466 | // then we can assume `undef` and hence the instruction is UB. |
| 2467 | KnownUBInsts.insert(I); |
| 2468 | return llvm::None; |
| 2469 | } |
| 2470 | Value *Val = SimplifiedV.getValue(); |
| 2471 | if (isa<UndefValue>(Val)) { |
| 2472 | KnownUBInsts.insert(I); |
| 2473 | return llvm::None; |
| 2474 | } |
| 2475 | return Val; |
| 2476 | } |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2477 | }; |
| 2478 | |
| 2479 | struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl { |
| 2480 | AAUndefinedBehaviorFunction(const IRPosition &IRP) |
| 2481 | : AAUndefinedBehaviorImpl(IRP) {} |
| 2482 | |
| 2483 | /// See AbstractAttribute::trackStatistics() |
| 2484 | void trackStatistics() const override { |
| 2485 | STATS_DECL(UndefinedBehaviorInstruction, Instruction, |
| 2486 | "Number of instructions known to have UB"); |
| 2487 | BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) += |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2488 | KnownUBInsts.size(); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2489 | } |
| 2490 | }; |
| 2491 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2492 | /// ------------------------ Will-Return Attributes ---------------------------- |
| 2493 | |
| omarahmed1111 | b285b33 | 2020-03-13 10:30:36 -0500 | [diff] [blame] | 2494 | // Helper function that checks whether a function has any cycle which we don't |
| 2495 | // know if it is bounded or not. |
| 2496 | // Loops with maximum trip count are considered bounded, any other cycle not. |
| 2497 | static bool mayContainUnboundedCycle(Function &F, Attributor &A) { |
| 2498 | ScalarEvolution *SE = |
| 2499 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F); |
| 2500 | LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F); |
| 2501 | // If either SCEV or LoopInfo is not available for the function then we assume |
| 2502 | // any cycle to be unbounded cycle. |
| 2503 | // We use scc_iterator which uses Tarjan algorithm to find all the maximal |
| 2504 | // SCCs.To detect if there's a cycle, we only need to find the maximal ones. |
| 2505 | if (!SE || !LI) { |
| 2506 | for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) |
| 2507 | if (SCCI.hasCycle()) |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2508 | return true; |
| omarahmed1111 | b285b33 | 2020-03-13 10:30:36 -0500 | [diff] [blame] | 2509 | return false; |
| 2510 | } |
| 2511 | |
| 2512 | // If there's irreducible control, the function may contain non-loop cycles. |
| 2513 | if (mayContainIrreducibleControl(F, LI)) |
| 2514 | return true; |
| 2515 | |
| 2516 | // Any loop that does not have a max trip count is considered unbounded cycle. |
| 2517 | for (auto *L : LI->getLoopsInPreorder()) { |
| 2518 | if (!SE->getSmallConstantMaxTripCount(L)) |
| 2519 | return true; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2520 | } |
| 2521 | return false; |
| 2522 | } |
| 2523 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2524 | struct AAWillReturnImpl : public AAWillReturn { |
| 2525 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2526 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2527 | /// See AbstractAttribute::initialize(...). |
| 2528 | void initialize(Attributor &A) override { |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2529 | AAWillReturn::initialize(A); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2530 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2531 | Function *F = getAssociatedFunction(); |
| omarahmed1111 | b285b33 | 2020-03-13 10:30:36 -0500 | [diff] [blame] | 2532 | if (!F || !A.isFunctionIPOAmendable(*F) || mayContainUnboundedCycle(*F, A)) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2533 | indicatePessimisticFixpoint(); |
| 2534 | } |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2535 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2536 | /// See AbstractAttribute::updateImpl(...). |
| 2537 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2538 | auto CheckForWillReturn = [&](Instruction &I) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2539 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); |
| 2540 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); |
| 2541 | if (WillReturnAA.isKnownWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2542 | return true; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2543 | if (!WillReturnAA.isAssumedWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2544 | return false; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2545 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); |
| 2546 | return NoRecurseAA.isAssumedNoRecurse(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2547 | }; |
| 2548 | |
| 2549 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) |
| 2550 | return indicatePessimisticFixpoint(); |
| 2551 | |
| 2552 | return ChangeStatus::UNCHANGED; |
| 2553 | } |
| 2554 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2555 | /// See AbstractAttribute::getAsStr() |
| 2556 | const std::string getAsStr() const override { |
| 2557 | return getAssumed() ? "willreturn" : "may-noreturn"; |
| 2558 | } |
| 2559 | }; |
| 2560 | |
| 2561 | struct AAWillReturnFunction final : AAWillReturnImpl { |
| 2562 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 2563 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2564 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2565 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2566 | }; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2567 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2568 | /// WillReturn attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2569 | struct AAWillReturnCallSite final : AAWillReturnImpl { |
| 2570 | AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 2571 | |
| 2572 | /// See AbstractAttribute::initialize(...). |
| 2573 | void initialize(Attributor &A) override { |
| 2574 | AAWillReturnImpl::initialize(A); |
| 2575 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2576 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2577 | indicatePessimisticFixpoint(); |
| 2578 | } |
| 2579 | |
| 2580 | /// See AbstractAttribute::updateImpl(...). |
| 2581 | ChangeStatus updateImpl(Attributor &A) override { |
| 2582 | // TODO: Once we have call site specific value information we can provide |
| 2583 | // call site specific liveness information and then it makes |
| 2584 | // sense to specialize attributes for call sites arguments instead of |
| 2585 | // redirecting requests to the callee argument. |
| 2586 | Function *F = getAssociatedFunction(); |
| 2587 | const IRPosition &FnPos = IRPosition::function(*F); |
| 2588 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); |
| 2589 | return clampStateAndIndicateChange( |
| 2590 | getState(), |
| 2591 | static_cast<const AAWillReturn::StateType &>(FnAA.getState())); |
| 2592 | } |
| 2593 | |
| 2594 | /// See AbstractAttribute::trackStatistics() |
| 2595 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } |
| 2596 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2597 | |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2598 | /// -------------------AAReachability Attribute-------------------------- |
| 2599 | |
| 2600 | struct AAReachabilityImpl : AAReachability { |
| 2601 | AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {} |
| 2602 | |
| 2603 | const std::string getAsStr() const override { |
| 2604 | // TODO: Return the number of reachable queries. |
| 2605 | return "reachable"; |
| 2606 | } |
| 2607 | |
| 2608 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2609 | void initialize(Attributor &A) override { indicatePessimisticFixpoint(); } |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2610 | |
| 2611 | /// See AbstractAttribute::updateImpl(...). |
| 2612 | ChangeStatus updateImpl(Attributor &A) override { |
| 2613 | return indicatePessimisticFixpoint(); |
| 2614 | } |
| 2615 | }; |
| 2616 | |
| 2617 | struct AAReachabilityFunction final : public AAReachabilityImpl { |
| 2618 | AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {} |
| 2619 | |
| 2620 | /// See AbstractAttribute::trackStatistics() |
| 2621 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); } |
| 2622 | }; |
| 2623 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2624 | /// ------------------------ NoAlias Argument Attribute ------------------------ |
| 2625 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2626 | struct AANoAliasImpl : AANoAlias { |
| Johannes Doerfert | 32e98a7 | 2020-02-14 19:23:21 -0600 | [diff] [blame] | 2627 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) { |
| 2628 | assert(getAssociatedType()->isPointerTy() && |
| 2629 | "Noalias is a pointer attribute"); |
| 2630 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2631 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2632 | const std::string getAsStr() const override { |
| 2633 | return getAssumed() ? "noalias" : "may-alias"; |
| 2634 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2635 | }; |
| 2636 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2637 | /// NoAlias attribute for a floating value. |
| 2638 | struct AANoAliasFloating final : AANoAliasImpl { |
| 2639 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2640 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2641 | /// See AbstractAttribute::initialize(...). |
| 2642 | void initialize(Attributor &A) override { |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2643 | AANoAliasImpl::initialize(A); |
| Johannes Doerfert | 87ddf1f | 2020-01-25 20:21:41 -0600 | [diff] [blame] | 2644 | Value *Val = &getAssociatedValue(); |
| 2645 | do { |
| 2646 | CastInst *CI = dyn_cast<CastInst>(Val); |
| 2647 | if (!CI) |
| 2648 | break; |
| 2649 | Value *Base = CI->getOperand(0); |
| 2650 | if (Base->getNumUses() != 1) |
| 2651 | break; |
| 2652 | Val = Base; |
| 2653 | } while (true); |
| 2654 | |
| Johannes Doerfert | 32e98a7 | 2020-02-14 19:23:21 -0600 | [diff] [blame] | 2655 | if (!Val->getType()->isPointerTy()) { |
| 2656 | indicatePessimisticFixpoint(); |
| 2657 | return; |
| 2658 | } |
| 2659 | |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2660 | if (isa<AllocaInst>(Val)) |
| 2661 | indicateOptimisticFixpoint(); |
| Johannes Doerfert | 24ae77e | 2020-01-27 22:19:11 -0600 | [diff] [blame] | 2662 | else if (isa<ConstantPointerNull>(Val) && |
| 2663 | !NullPointerIsDefined(getAnchorScope(), |
| Johannes Doerfert | 87ddf1f | 2020-01-25 20:21:41 -0600 | [diff] [blame] | 2664 | Val->getType()->getPointerAddressSpace())) |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2665 | indicateOptimisticFixpoint(); |
| Johannes Doerfert | 87ddf1f | 2020-01-25 20:21:41 -0600 | [diff] [blame] | 2666 | else if (Val != &getAssociatedValue()) { |
| 2667 | const auto &ValNoAliasAA = |
| 2668 | A.getAAFor<AANoAlias>(*this, IRPosition::value(*Val)); |
| 2669 | if (ValNoAliasAA.isKnownNoAlias()) |
| 2670 | indicateOptimisticFixpoint(); |
| 2671 | } |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2674 | /// See AbstractAttribute::updateImpl(...). |
| 2675 | ChangeStatus updateImpl(Attributor &A) override { |
| 2676 | // TODO: Implement this. |
| 2677 | return indicatePessimisticFixpoint(); |
| 2678 | } |
| 2679 | |
| 2680 | /// See AbstractAttribute::trackStatistics() |
| 2681 | void trackStatistics() const override { |
| 2682 | STATS_DECLTRACK_FLOATING_ATTR(noalias) |
| 2683 | } |
| 2684 | }; |
| 2685 | |
| 2686 | /// NoAlias attribute for an argument. |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2687 | struct AANoAliasArgument final |
| 2688 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2689 | using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>; |
| 2690 | AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {} |
| 2691 | |
| Johannes Doerfert | 2baf000 | 2020-01-12 00:18:07 -0600 | [diff] [blame] | 2692 | /// See AbstractAttribute::initialize(...). |
| 2693 | void initialize(Attributor &A) override { |
| 2694 | Base::initialize(A); |
| 2695 | // See callsite argument attribute and callee argument attribute. |
| 2696 | if (hasAttr({Attribute::ByVal})) |
| 2697 | indicateOptimisticFixpoint(); |
| 2698 | } |
| 2699 | |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2700 | /// See AbstractAttribute::update(...). |
| 2701 | ChangeStatus updateImpl(Attributor &A) override { |
| 2702 | // We have to make sure no-alias on the argument does not break |
| 2703 | // synchronization when this is a callback argument, see also [1] below. |
| 2704 | // If synchronization cannot be affected, we delegate to the base updateImpl |
| 2705 | // function, otherwise we give up for now. |
| 2706 | |
| 2707 | // If the function is no-sync, no-alias cannot break synchronization. |
| 2708 | const auto &NoSyncAA = A.getAAFor<AANoSync>( |
| 2709 | *this, IRPosition::function_scope(getIRPosition())); |
| 2710 | if (NoSyncAA.isAssumedNoSync()) |
| 2711 | return Base::updateImpl(A); |
| 2712 | |
| 2713 | // If the argument is read-only, no-alias cannot break synchronization. |
| 2714 | const auto &MemBehaviorAA = |
| 2715 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); |
| 2716 | if (MemBehaviorAA.isAssumedReadOnly()) |
| 2717 | return Base::updateImpl(A); |
| 2718 | |
| 2719 | // If the argument is never passed through callbacks, no-alias cannot break |
| 2720 | // synchronization. |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2721 | bool AllCallSitesKnown; |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2722 | if (A.checkForAllCallSites( |
| 2723 | [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this, |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2724 | true, AllCallSitesKnown)) |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2725 | return Base::updateImpl(A); |
| 2726 | |
| 2727 | // TODO: add no-alias but make sure it doesn't break synchronization by |
| 2728 | // introducing fake uses. See: |
| 2729 | // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel, |
| 2730 | // International Workshop on OpenMP 2018, |
| 2731 | // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf |
| 2732 | |
| 2733 | return indicatePessimisticFixpoint(); |
| 2734 | } |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2735 | |
| 2736 | /// See AbstractAttribute::trackStatistics() |
| 2737 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } |
| 2738 | }; |
| 2739 | |
| 2740 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { |
| 2741 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2742 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2743 | /// See AbstractAttribute::initialize(...). |
| 2744 | void initialize(Attributor &A) override { |
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 2745 | // See callsite argument attribute and callee argument attribute. |
| 2746 | ImmutableCallSite ICS(&getAnchorValue()); |
| 2747 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) |
| 2748 | indicateOptimisticFixpoint(); |
| Johannes Doerfert | 24ae77e | 2020-01-27 22:19:11 -0600 | [diff] [blame] | 2749 | Value &Val = getAssociatedValue(); |
| 2750 | if (isa<ConstantPointerNull>(Val) && |
| 2751 | !NullPointerIsDefined(getAnchorScope(), |
| 2752 | Val.getType()->getPointerAddressSpace())) |
| 2753 | indicateOptimisticFixpoint(); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2756 | /// Determine if the underlying value may alias with the call site argument |
| 2757 | /// \p OtherArgNo of \p ICS (= the underlying call site). |
| 2758 | bool mayAliasWithArgument(Attributor &A, AAResults *&AAR, |
| 2759 | const AAMemoryBehavior &MemBehaviorAA, |
| 2760 | ImmutableCallSite ICS, unsigned OtherArgNo) { |
| 2761 | // We do not need to worry about aliasing with the underlying IRP. |
| 2762 | if (this->getArgNo() == (int)OtherArgNo) |
| 2763 | return false; |
| 2764 | |
| 2765 | // If it is not a pointer or pointer vector we do not alias. |
| 2766 | const Value *ArgOp = ICS.getArgOperand(OtherArgNo); |
| 2767 | if (!ArgOp->getType()->isPtrOrPtrVectorTy()) |
| 2768 | return false; |
| 2769 | |
| 2770 | auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>( |
| 2771 | *this, IRPosition::callsite_argument(ICS, OtherArgNo), |
| 2772 | /* TrackDependence */ false); |
| 2773 | |
| 2774 | // If the argument is readnone, there is no read-write aliasing. |
| 2775 | if (ICSArgMemBehaviorAA.isAssumedReadNone()) { |
| 2776 | A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); |
| 2777 | return false; |
| 2778 | } |
| 2779 | |
| 2780 | // If the argument is readonly and the underlying value is readonly, there |
| 2781 | // is no read-write aliasing. |
| 2782 | bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly(); |
| 2783 | if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) { |
| 2784 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); |
| 2785 | A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); |
| 2786 | return false; |
| 2787 | } |
| 2788 | |
| 2789 | // We have to utilize actual alias analysis queries so we need the object. |
| 2790 | if (!AAR) |
| 2791 | AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope()); |
| 2792 | |
| 2793 | // Try to rule it out at the call site. |
| 2794 | bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp); |
| 2795 | LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between " |
| 2796 | "callsite arguments: " |
| 2797 | << getAssociatedValue() << " " << *ArgOp << " => " |
| 2798 | << (IsAliasing ? "" : "no-") << "alias \n"); |
| 2799 | |
| 2800 | return IsAliasing; |
| 2801 | } |
| 2802 | |
| 2803 | bool |
| 2804 | isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR, |
| 2805 | const AAMemoryBehavior &MemBehaviorAA, |
| 2806 | const AANoAlias &NoAliasAA) { |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2807 | // We can deduce "noalias" if the following conditions hold. |
| 2808 | // (i) Associated value is assumed to be noalias in the definition. |
| 2809 | // (ii) Associated value is assumed to be no-capture in all the uses |
| 2810 | // possibly executed before this callsite. |
| 2811 | // (iii) There is no other pointer argument which could alias with the |
| 2812 | // value. |
| 2813 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2814 | bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias(); |
| 2815 | if (!AssociatedValueIsNoAliasAtDef) { |
| 2816 | LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue() |
| 2817 | << " is not no-alias at the definition\n"); |
| 2818 | return false; |
| 2819 | } |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2820 | |
| Hideto Ueno | d9bf79f | 2020-03-12 15:25:24 +0000 | [diff] [blame] | 2821 | A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL); |
| 2822 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2823 | const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); |
| 2824 | auto &NoCaptureAA = |
| 2825 | A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false); |
| 2826 | // Check whether the value is captured in the scope using AANoCapture. |
| Pankaj Gode | bf99053 | 2020-03-13 21:09:08 +0530 | [diff] [blame] | 2827 | // Look at CFG and check only uses possibly executed before this |
| 2828 | // callsite. |
| 2829 | auto UsePred = [&](const Use &U, bool &Follow) -> bool { |
| 2830 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 2831 | |
| 2832 | // If user if curr instr and only use. |
| 2833 | if ((UserI == getCtxI()) && (UserI->getNumUses() == 1)) |
| 2834 | return true; |
| 2835 | |
| 2836 | const Function *ScopeFn = VIRP.getAnchorScope(); |
| 2837 | if (ScopeFn) { |
| 2838 | const auto &ReachabilityAA = |
| 2839 | A.getAAFor<AAReachability>(*this, IRPosition::function(*ScopeFn)); |
| 2840 | |
| 2841 | if (!ReachabilityAA.isAssumedReachable(UserI, getCtxI())) |
| 2842 | return true; |
| 2843 | |
| 2844 | if (auto *CB = dyn_cast<CallBase>(UserI)) { |
| 2845 | if (CB->isArgOperand(&U)) { |
| 2846 | |
| 2847 | unsigned ArgNo = CB->getArgOperandNo(&U); |
| 2848 | |
| 2849 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( |
| 2850 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| 2851 | |
| 2852 | if (NoCaptureAA.isAssumedNoCapture()) |
| 2853 | return true; |
| 2854 | } |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | // For cases which can potentially have more users |
| 2859 | if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) || |
| 2860 | isa<SelectInst>(U)) { |
| 2861 | Follow = true; |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
| 2865 | LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n"); |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2866 | return false; |
| Pankaj Gode | bf99053 | 2020-03-13 21:09:08 +0530 | [diff] [blame] | 2867 | }; |
| 2868 | |
| 2869 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 2870 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) { |
| 2871 | LLVM_DEBUG( |
| 2872 | dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() |
| 2873 | << " cannot be noalias as it is potentially captured\n"); |
| 2874 | return false; |
| 2875 | } |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2876 | } |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2877 | A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL); |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2878 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2879 | // Check there is no other pointer argument which could alias with the |
| 2880 | // value passed at this call site. |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2881 | // TODO: AbstractCallSite |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2882 | ImmutableCallSite ICS(&getAnchorValue()); |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2883 | for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands(); |
| 2884 | OtherArgNo++) |
| 2885 | if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo)) |
| 2886 | return false; |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2887 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2888 | return true; |
| 2889 | } |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2890 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2891 | /// See AbstractAttribute::updateImpl(...). |
| 2892 | ChangeStatus updateImpl(Attributor &A) override { |
| 2893 | // If the argument is readnone we are done as there are no accesses via the |
| 2894 | // argument. |
| 2895 | auto &MemBehaviorAA = |
| 2896 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), |
| 2897 | /* TrackDependence */ false); |
| 2898 | if (MemBehaviorAA.isAssumedReadNone()) { |
| 2899 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); |
| 2900 | return ChangeStatus::UNCHANGED; |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2903 | const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); |
| 2904 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP, |
| 2905 | /* TrackDependence */ false); |
| 2906 | |
| 2907 | AAResults *AAR = nullptr; |
| 2908 | if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA, |
| 2909 | NoAliasAA)) { |
| 2910 | LLVM_DEBUG( |
| 2911 | dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n"); |
| 2912 | return ChangeStatus::UNCHANGED; |
| 2913 | } |
| 2914 | |
| 2915 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2919 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2920 | }; |
| 2921 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2922 | /// NoAlias attribute for function return value. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2923 | struct AANoAliasReturned final : AANoAliasImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2924 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2925 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2926 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2927 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| 2928 | |
| 2929 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 2930 | if (Constant *C = dyn_cast<Constant>(&RV)) |
| 2931 | if (C->isNullValue() || isa<UndefValue>(C)) |
| 2932 | return true; |
| 2933 | |
| 2934 | /// For now, we can only deduce noalias if we have call sites. |
| 2935 | /// FIXME: add more support. |
| 2936 | ImmutableCallSite ICS(&RV); |
| 2937 | if (!ICS) |
| 2938 | return false; |
| 2939 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2940 | const IRPosition &RVPos = IRPosition::value(RV); |
| 2941 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2942 | if (!NoAliasAA.isAssumedNoAlias()) |
| 2943 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2944 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2945 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); |
| 2946 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2947 | }; |
| 2948 | |
| 2949 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) |
| 2950 | return indicatePessimisticFixpoint(); |
| 2951 | |
| 2952 | return ChangeStatus::UNCHANGED; |
| 2953 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2954 | |
| 2955 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2956 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2957 | }; |
| 2958 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2959 | /// NoAlias attribute deduction for a call site return value. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2960 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { |
| 2961 | AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2962 | |
| 2963 | /// See AbstractAttribute::initialize(...). |
| 2964 | void initialize(Attributor &A) override { |
| 2965 | AANoAliasImpl::initialize(A); |
| 2966 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2967 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2968 | indicatePessimisticFixpoint(); |
| 2969 | } |
| 2970 | |
| 2971 | /// See AbstractAttribute::updateImpl(...). |
| 2972 | ChangeStatus updateImpl(Attributor &A) override { |
| 2973 | // TODO: Once we have call site specific value information we can provide |
| 2974 | // call site specific liveness information and then it makes |
| 2975 | // sense to specialize attributes for call sites arguments instead of |
| 2976 | // redirecting requests to the callee argument. |
| 2977 | Function *F = getAssociatedFunction(); |
| 2978 | const IRPosition &FnPos = IRPosition::returned(*F); |
| 2979 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); |
| 2980 | return clampStateAndIndicateChange( |
| 2981 | getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); |
| 2982 | } |
| 2983 | |
| 2984 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2985 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2986 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2987 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2988 | /// -------------------AAIsDead Function Attribute----------------------- |
| 2989 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2990 | struct AAIsDeadValueImpl : public AAIsDead { |
| 2991 | AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2992 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2993 | /// See AAIsDead::isAssumedDead(). |
| 2994 | bool isAssumedDead() const override { return getAssumed(); } |
| 2995 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2996 | /// See AAIsDead::isKnownDead(). |
| 2997 | bool isKnownDead() const override { return getKnown(); } |
| 2998 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2999 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| 3000 | bool isAssumedDead(const BasicBlock *BB) const override { return false; } |
| 3001 | |
| 3002 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| 3003 | bool isKnownDead(const BasicBlock *BB) const override { return false; } |
| 3004 | |
| 3005 | /// See AAIsDead::isAssumedDead(Instruction *I). |
| 3006 | bool isAssumedDead(const Instruction *I) const override { |
| 3007 | return I == getCtxI() && isAssumedDead(); |
| 3008 | } |
| 3009 | |
| 3010 | /// See AAIsDead::isKnownDead(Instruction *I). |
| 3011 | bool isKnownDead(const Instruction *I) const override { |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3012 | return isAssumedDead(I) && getKnown(); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | /// See AbstractAttribute::getAsStr(). |
| 3016 | const std::string getAsStr() const override { |
| 3017 | return isAssumedDead() ? "assumed-dead" : "assumed-live"; |
| 3018 | } |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3019 | |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3020 | /// Check if all uses are assumed dead. |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 3021 | bool areAllUsesAssumedDead(Attributor &A, Value &V) { |
| 3022 | auto UsePred = [&](const Use &U, bool &Follow) { return false; }; |
| 3023 | // Explicitly set the dependence class to required because we want a long |
| 3024 | // chain of N dependent instructions to be considered live as soon as one is |
| 3025 | // without going through N update cycles. This is not required for |
| 3026 | // correctness. |
| 3027 | return A.checkForAllUses(UsePred, *this, V, DepClassTy::REQUIRED); |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | /// Determine if \p I is assumed to be side-effect free. |
| 3031 | bool isAssumedSideEffectFree(Attributor &A, Instruction *I) { |
| 3032 | if (!I || wouldInstructionBeTriviallyDead(I)) |
| 3033 | return true; |
| 3034 | |
| 3035 | auto *CB = dyn_cast<CallBase>(I); |
| 3036 | if (!CB || isa<IntrinsicInst>(CB)) |
| 3037 | return false; |
| 3038 | |
| 3039 | const IRPosition &CallIRP = IRPosition::callsite_function(*CB); |
| 3040 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, CallIRP); |
| 3041 | if (!NoUnwindAA.isAssumedNoUnwind()) |
| 3042 | return false; |
| 3043 | |
| 3044 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, CallIRP); |
| 3045 | if (!MemBehaviorAA.isAssumedReadOnly()) |
| 3046 | return false; |
| 3047 | |
| 3048 | return true; |
| 3049 | } |
| 3050 | }; |
| 3051 | |
| 3052 | struct AAIsDeadFloating : public AAIsDeadValueImpl { |
| 3053 | AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} |
| 3054 | |
| 3055 | /// See AbstractAttribute::initialize(...). |
| 3056 | void initialize(Attributor &A) override { |
| 3057 | if (isa<UndefValue>(getAssociatedValue())) { |
| 3058 | indicatePessimisticFixpoint(); |
| 3059 | return; |
| 3060 | } |
| 3061 | |
| 3062 | Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); |
| 3063 | if (!isAssumedSideEffectFree(A, I)) |
| 3064 | indicatePessimisticFixpoint(); |
| 3065 | } |
| 3066 | |
| 3067 | /// See AbstractAttribute::updateImpl(...). |
| 3068 | ChangeStatus updateImpl(Attributor &A) override { |
| 3069 | Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); |
| 3070 | if (!isAssumedSideEffectFree(A, I)) |
| 3071 | return indicatePessimisticFixpoint(); |
| 3072 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 3073 | if (!areAllUsesAssumedDead(A, getAssociatedValue())) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3074 | return indicatePessimisticFixpoint(); |
| 3075 | return ChangeStatus::UNCHANGED; |
| 3076 | } |
| 3077 | |
| 3078 | /// See AbstractAttribute::manifest(...). |
| 3079 | ChangeStatus manifest(Attributor &A) override { |
| 3080 | Value &V = getAssociatedValue(); |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3081 | if (auto *I = dyn_cast<Instruction>(&V)) { |
| 3082 | // If we get here we basically know the users are all dead. We check if |
| 3083 | // isAssumedSideEffectFree returns true here again because it might not be |
| 3084 | // the case and only the users are dead but the instruction (=call) is |
| 3085 | // still needed. |
| 3086 | if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3087 | A.deleteAfterManifest(*I); |
| 3088 | return ChangeStatus::CHANGED; |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3089 | } |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3090 | } |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3091 | if (V.use_empty()) |
| 3092 | return ChangeStatus::UNCHANGED; |
| 3093 | |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 3094 | bool UsedAssumedInformation = false; |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 3095 | Optional<Constant *> C = |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 3096 | getAssumedConstant(A, V, *this, UsedAssumedInformation); |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 3097 | if (C.hasValue() && C.getValue()) |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 3098 | return ChangeStatus::UNCHANGED; |
| 3099 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3100 | UndefValue &UV = *UndefValue::get(V.getType()); |
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 3101 | bool AnyChange = A.changeValueAfterManifest(V, UV); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3102 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| 3103 | } |
| 3104 | |
| 3105 | /// See AbstractAttribute::trackStatistics() |
| 3106 | void trackStatistics() const override { |
| 3107 | STATS_DECLTRACK_FLOATING_ATTR(IsDead) |
| 3108 | } |
| 3109 | }; |
| 3110 | |
| 3111 | struct AAIsDeadArgument : public AAIsDeadFloating { |
| 3112 | AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} |
| 3113 | |
| 3114 | /// See AbstractAttribute::initialize(...). |
| 3115 | void initialize(Attributor &A) override { |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 3116 | if (!A.isFunctionIPOAmendable(*getAssociatedFunction())) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3117 | indicatePessimisticFixpoint(); |
| 3118 | } |
| 3119 | |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 3120 | /// See AbstractAttribute::manifest(...). |
| 3121 | ChangeStatus manifest(Attributor &A) override { |
| 3122 | ChangeStatus Changed = AAIsDeadFloating::manifest(A); |
| 3123 | Argument &Arg = *getAssociatedArgument(); |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 3124 | if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {})) |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 3125 | if (A.registerFunctionSignatureRewrite( |
| 3126 | Arg, /* ReplacementTypes */ {}, |
| 3127 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{}, |
| 3128 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) |
| 3129 | return ChangeStatus::CHANGED; |
| 3130 | return Changed; |
| 3131 | } |
| 3132 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3133 | /// See AbstractAttribute::trackStatistics() |
| 3134 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } |
| 3135 | }; |
| 3136 | |
| 3137 | struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { |
| 3138 | AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} |
| 3139 | |
| 3140 | /// See AbstractAttribute::initialize(...). |
| 3141 | void initialize(Attributor &A) override { |
| 3142 | if (isa<UndefValue>(getAssociatedValue())) |
| 3143 | indicatePessimisticFixpoint(); |
| 3144 | } |
| 3145 | |
| 3146 | /// See AbstractAttribute::updateImpl(...). |
| 3147 | ChangeStatus updateImpl(Attributor &A) override { |
| 3148 | // TODO: Once we have call site specific value information we can provide |
| 3149 | // call site specific liveness information and then it makes |
| 3150 | // sense to specialize attributes for call sites arguments instead of |
| 3151 | // redirecting requests to the callee argument. |
| 3152 | Argument *Arg = getAssociatedArgument(); |
| 3153 | if (!Arg) |
| 3154 | return indicatePessimisticFixpoint(); |
| 3155 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 3156 | auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); |
| 3157 | return clampStateAndIndicateChange( |
| 3158 | getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); |
| 3159 | } |
| 3160 | |
| 3161 | /// See AbstractAttribute::manifest(...). |
| 3162 | ChangeStatus manifest(Attributor &A) override { |
| 3163 | CallBase &CB = cast<CallBase>(getAnchorValue()); |
| 3164 | Use &U = CB.getArgOperandUse(getArgNo()); |
| 3165 | assert(!isa<UndefValue>(U.get()) && |
| 3166 | "Expected undef values to be filtered out!"); |
| 3167 | UndefValue &UV = *UndefValue::get(U->getType()); |
| 3168 | if (A.changeUseAfterManifest(U, UV)) |
| 3169 | return ChangeStatus::CHANGED; |
| 3170 | return ChangeStatus::UNCHANGED; |
| 3171 | } |
| 3172 | |
| 3173 | /// See AbstractAttribute::trackStatistics() |
| 3174 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } |
| 3175 | }; |
| 3176 | |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3177 | struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { |
| 3178 | AAIsDeadCallSiteReturned(const IRPosition &IRP) |
| 3179 | : AAIsDeadFloating(IRP), IsAssumedSideEffectFree(true) {} |
| 3180 | |
| 3181 | /// See AAIsDead::isAssumedDead(). |
| 3182 | bool isAssumedDead() const override { |
| 3183 | return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree; |
| 3184 | } |
| 3185 | |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3186 | /// See AbstractAttribute::initialize(...). |
| 3187 | void initialize(Attributor &A) override { |
| 3188 | if (isa<UndefValue>(getAssociatedValue())) { |
| 3189 | indicatePessimisticFixpoint(); |
| 3190 | return; |
| 3191 | } |
| 3192 | |
| 3193 | // We track this separately as a secondary state. |
| 3194 | IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI()); |
| 3195 | } |
| 3196 | |
| 3197 | /// See AbstractAttribute::updateImpl(...). |
| 3198 | ChangeStatus updateImpl(Attributor &A) override { |
| 3199 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 3200 | if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) { |
| 3201 | IsAssumedSideEffectFree = false; |
| 3202 | Changed = ChangeStatus::CHANGED; |
| 3203 | } |
| 3204 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 3205 | if (!areAllUsesAssumedDead(A, getAssociatedValue())) |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3206 | return indicatePessimisticFixpoint(); |
| 3207 | return Changed; |
| 3208 | } |
| 3209 | |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 3210 | /// See AbstractAttribute::trackStatistics() |
| 3211 | void trackStatistics() const override { |
| 3212 | if (IsAssumedSideEffectFree) |
| 3213 | STATS_DECLTRACK_CSRET_ATTR(IsDead) |
| 3214 | else |
| 3215 | STATS_DECLTRACK_CSRET_ATTR(UnusedResult) |
| 3216 | } |
| 3217 | |
| 3218 | /// See AbstractAttribute::getAsStr(). |
| 3219 | const std::string getAsStr() const override { |
| 3220 | return isAssumedDead() |
| 3221 | ? "assumed-dead" |
| 3222 | : (getAssumed() ? "assumed-dead-users" : "assumed-live"); |
| 3223 | } |
| 3224 | |
| 3225 | private: |
| 3226 | bool IsAssumedSideEffectFree; |
| 3227 | }; |
| 3228 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3229 | struct AAIsDeadReturned : public AAIsDeadValueImpl { |
| 3230 | AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} |
| 3231 | |
| 3232 | /// See AbstractAttribute::updateImpl(...). |
| 3233 | ChangeStatus updateImpl(Attributor &A) override { |
| 3234 | |
| Johannes Doerfert | b70297a | 2020-02-14 10:34:31 -0600 | [diff] [blame] | 3235 | A.checkForAllInstructions([](Instruction &) { return true; }, *this, |
| 3236 | {Instruction::Ret}); |
| 3237 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3238 | auto PredForCallSite = [&](AbstractCallSite ACS) { |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 3239 | if (ACS.isCallbackCall() || !ACS.getInstruction()) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3240 | return false; |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 3241 | return areAllUsesAssumedDead(A, *ACS.getInstruction()); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3242 | }; |
| 3243 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 3244 | bool AllCallSitesKnown; |
| 3245 | if (!A.checkForAllCallSites(PredForCallSite, *this, true, |
| 3246 | AllCallSitesKnown)) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3247 | return indicatePessimisticFixpoint(); |
| 3248 | |
| 3249 | return ChangeStatus::UNCHANGED; |
| 3250 | } |
| 3251 | |
| 3252 | /// See AbstractAttribute::manifest(...). |
| 3253 | ChangeStatus manifest(Attributor &A) override { |
| 3254 | // TODO: Rewrite the signature to return void? |
| 3255 | bool AnyChange = false; |
| 3256 | UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); |
| 3257 | auto RetInstPred = [&](Instruction &I) { |
| 3258 | ReturnInst &RI = cast<ReturnInst>(I); |
| 3259 | if (!isa<UndefValue>(RI.getReturnValue())) |
| 3260 | AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); |
| 3261 | return true; |
| 3262 | }; |
| 3263 | A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); |
| 3264 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| 3265 | } |
| 3266 | |
| 3267 | /// See AbstractAttribute::trackStatistics() |
| 3268 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } |
| 3269 | }; |
| 3270 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3271 | struct AAIsDeadFunction : public AAIsDead { |
| 3272 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} |
| 3273 | |
| 3274 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3275 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 3276 | const Function *F = getAssociatedFunction(); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3277 | if (F && !F->isDeclaration()) { |
| 3278 | ToBeExploredFrom.insert(&F->getEntryBlock().front()); |
| 3279 | assumeLive(A, F->getEntryBlock()); |
| 3280 | } |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 3283 | /// See AbstractAttribute::getAsStr(). |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3284 | const std::string getAsStr() const override { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 3285 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3286 | std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3287 | std::to_string(ToBeExploredFrom.size()) + "][#KDE " + |
| 3288 | std::to_string(KnownDeadEnds.size()) + "]"; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /// See AbstractAttribute::manifest(...). |
| 3292 | ChangeStatus manifest(Attributor &A) override { |
| 3293 | assert(getState().isValidState() && |
| 3294 | "Attempted to manifest an invalid state!"); |
| 3295 | |
| 3296 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3297 | Function &F = *getAssociatedFunction(); |
| 3298 | |
| 3299 | if (AssumedLiveBlocks.empty()) { |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 3300 | A.deleteAfterManifest(F); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3301 | return ChangeStatus::CHANGED; |
| 3302 | } |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3303 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 3304 | // Flag to determine if we can change an invoke to a call assuming the |
| 3305 | // callee is nounwind. This is not possible if the personality of the |
| 3306 | // function allows to catch asynchronous exceptions. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3307 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3308 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3309 | KnownDeadEnds.set_union(ToBeExploredFrom); |
| 3310 | for (const Instruction *DeadEndI : KnownDeadEnds) { |
| 3311 | auto *CB = dyn_cast<CallBase>(DeadEndI); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3312 | if (!CB) |
| 3313 | continue; |
| 3314 | const auto &NoReturnAA = |
| 3315 | A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); |
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 3316 | bool MayReturn = !NoReturnAA.isAssumedNoReturn(); |
| 3317 | if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3318 | continue; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3319 | |
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 3320 | if (auto *II = dyn_cast<InvokeInst>(DeadEndI)) |
| 3321 | A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II)); |
| 3322 | else |
| 3323 | A.changeToUnreachableAfterManifest( |
| 3324 | const_cast<Instruction *>(DeadEndI->getNextNode())); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3325 | HasChanged = ChangeStatus::CHANGED; |
| 3326 | } |
| 3327 | |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 3328 | for (BasicBlock &BB : F) |
| 3329 | if (!AssumedLiveBlocks.count(&BB)) |
| 3330 | A.deleteAfterManifest(BB); |
| 3331 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3332 | return HasChanged; |
| 3333 | } |
| 3334 | |
| 3335 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3336 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3337 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3338 | /// See AbstractAttribute::trackStatistics() |
| 3339 | void trackStatistics() const override {} |
| 3340 | |
| 3341 | /// Returns true if the function is assumed dead. |
| 3342 | bool isAssumedDead() const override { return false; } |
| 3343 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 3344 | /// See AAIsDead::isKnownDead(). |
| 3345 | bool isKnownDead() const override { return false; } |
| 3346 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3347 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3348 | bool isAssumedDead(const BasicBlock *BB) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 3349 | assert(BB->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3350 | "BB must be in the same anchor scope function."); |
| 3351 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3352 | if (!getAssumed()) |
| 3353 | return false; |
| 3354 | return !AssumedLiveBlocks.count(BB); |
| 3355 | } |
| 3356 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3357 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3358 | bool isKnownDead(const BasicBlock *BB) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3359 | return getKnown() && isAssumedDead(BB); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3362 | /// See AAIsDead::isAssumed(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3363 | bool isAssumedDead(const Instruction *I) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 3364 | assert(I->getParent()->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3365 | "Instruction must be in the same anchor scope function."); |
| 3366 | |
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 3367 | if (!getAssumed()) |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3368 | return false; |
| 3369 | |
| 3370 | // If it is not in AssumedLiveBlocks then it for sure dead. |
| 3371 | // Otherwise, it can still be after noreturn call in a live block. |
| 3372 | if (!AssumedLiveBlocks.count(I->getParent())) |
| 3373 | return true; |
| 3374 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3375 | // If it is not after a liveness barrier it is live. |
| 3376 | const Instruction *PrevI = I->getPrevNode(); |
| 3377 | while (PrevI) { |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3378 | if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3379 | return true; |
| 3380 | PrevI = PrevI->getPrevNode(); |
| 3381 | } |
| 3382 | return false; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | /// See AAIsDead::isKnownDead(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3386 | bool isKnownDead(const Instruction *I) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3387 | return getKnown() && isAssumedDead(I); |
| 3388 | } |
| 3389 | |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3390 | /// Determine if \p F might catch asynchronous exceptions. |
| 3391 | static bool mayCatchAsynchronousExceptions(const Function &F) { |
| 3392 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); |
| 3393 | } |
| 3394 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3395 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A |
| 3396 | /// that internal function called from \p BB should now be looked at. |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3397 | bool assumeLive(Attributor &A, const BasicBlock &BB) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3398 | if (!AssumedLiveBlocks.insert(&BB).second) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3399 | return false; |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3400 | |
| 3401 | // We assume that all of BB is (probably) live now and if there are calls to |
| 3402 | // internal functions we will assume that those are now live as well. This |
| 3403 | // is a performance optimization for blocks with calls to a lot of internal |
| 3404 | // functions. It can however cause dead functions to be treated as live. |
| 3405 | for (const Instruction &I : BB) |
| 3406 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) |
| 3407 | if (const Function *F = ICS.getCalledFunction()) |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 3408 | if (F->hasLocalLinkage()) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3409 | A.markLiveInternalFunction(*F); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3410 | return true; |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3413 | /// Collection of instructions that need to be explored again, e.g., we |
| 3414 | /// did assume they do not transfer control to (one of their) successors. |
| 3415 | SmallSetVector<const Instruction *, 8> ToBeExploredFrom; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3416 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3417 | /// Collection of instructions that are known to not transfer control. |
| 3418 | SmallSetVector<const Instruction *, 8> KnownDeadEnds; |
| 3419 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3420 | /// Collection of all assumed live BasicBlocks. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3421 | DenseSet<const BasicBlock *> AssumedLiveBlocks; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3422 | }; |
| 3423 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3424 | static bool |
| 3425 | identifyAliveSuccessors(Attributor &A, const CallBase &CB, |
| 3426 | AbstractAttribute &AA, |
| 3427 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| 3428 | const IRPosition &IPos = IRPosition::callsite_function(CB); |
| 3429 | |
| 3430 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); |
| 3431 | if (NoReturnAA.isAssumedNoReturn()) |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3432 | return !NoReturnAA.isKnownNoReturn(); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3433 | if (CB.isTerminator()) |
| 3434 | AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); |
| 3435 | else |
| 3436 | AliveSuccessors.push_back(CB.getNextNode()); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3437 | return false; |
| 3438 | } |
| 3439 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3440 | static bool |
| 3441 | identifyAliveSuccessors(Attributor &A, const InvokeInst &II, |
| 3442 | AbstractAttribute &AA, |
| 3443 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| 3444 | bool UsedAssumedInformation = |
| 3445 | identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3446 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3447 | // First, determine if we can change an invoke to a call assuming the |
| 3448 | // callee is nounwind. This is not possible if the personality of the |
| 3449 | // function allows to catch asynchronous exceptions. |
| 3450 | if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { |
| 3451 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); |
| 3452 | } else { |
| 3453 | const IRPosition &IPos = IRPosition::callsite_function(II); |
| 3454 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3455 | if (AANoUnw.isAssumedNoUnwind()) { |
| 3456 | UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); |
| 3457 | } else { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3458 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3459 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3460 | } |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3461 | return UsedAssumedInformation; |
| 3462 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3463 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3464 | static bool |
| 3465 | identifyAliveSuccessors(Attributor &A, const BranchInst &BI, |
| 3466 | AbstractAttribute &AA, |
| 3467 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| 3468 | bool UsedAssumedInformation = false; |
| 3469 | if (BI.getNumSuccessors() == 1) { |
| 3470 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); |
| 3471 | } else { |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 3472 | Optional<ConstantInt *> CI = getAssumedConstantInt( |
| 3473 | A, *BI.getCondition(), AA, UsedAssumedInformation); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3474 | if (!CI.hasValue()) { |
| 3475 | // No value yet, assume both edges are dead. |
| 3476 | } else if (CI.getValue()) { |
| 3477 | const BasicBlock *SuccBB = |
| 3478 | BI.getSuccessor(1 - CI.getValue()->getZExtValue()); |
| 3479 | AliveSuccessors.push_back(&SuccBB->front()); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3480 | } else { |
| 3481 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); |
| 3482 | AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3483 | UsedAssumedInformation = false; |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3484 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3485 | } |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3486 | return UsedAssumedInformation; |
| 3487 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3488 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3489 | static bool |
| 3490 | identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, |
| 3491 | AbstractAttribute &AA, |
| 3492 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3493 | bool UsedAssumedInformation = false; |
| 3494 | Optional<ConstantInt *> CI = |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 3495 | getAssumedConstantInt(A, *SI.getCondition(), AA, UsedAssumedInformation); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3496 | if (!CI.hasValue()) { |
| 3497 | // No value yet, assume all edges are dead. |
| 3498 | } else if (CI.getValue()) { |
| 3499 | for (auto &CaseIt : SI.cases()) { |
| 3500 | if (CaseIt.getCaseValue() == CI.getValue()) { |
| 3501 | AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3502 | return UsedAssumedInformation; |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3503 | } |
| 3504 | } |
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 3505 | AliveSuccessors.push_back(&SI.getDefaultDest()->front()); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3506 | return UsedAssumedInformation; |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3507 | } else { |
| 3508 | for (const BasicBlock *SuccBB : successors(SI.getParent())) |
| 3509 | AliveSuccessors.push_back(&SuccBB->front()); |
| 3510 | } |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3511 | return UsedAssumedInformation; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3514 | ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3515 | ChangeStatus Change = ChangeStatus::UNCHANGED; |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3516 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3517 | LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" |
| 3518 | << getAssociatedFunction()->size() << "] BBs and " |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3519 | << ToBeExploredFrom.size() << " exploration points and " |
| 3520 | << KnownDeadEnds.size() << " known dead ends\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3521 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3522 | // Copy and clear the list of instructions we need to explore from. It is |
| 3523 | // refilled with instructions the next update has to look at. |
| 3524 | SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), |
| 3525 | ToBeExploredFrom.end()); |
| 3526 | decltype(ToBeExploredFrom) NewToBeExploredFrom; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3527 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3528 | SmallVector<const Instruction *, 8> AliveSuccessors; |
| 3529 | while (!Worklist.empty()) { |
| 3530 | const Instruction *I = Worklist.pop_back_val(); |
| 3531 | LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3532 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3533 | AliveSuccessors.clear(); |
| 3534 | |
| 3535 | bool UsedAssumedInformation = false; |
| 3536 | switch (I->getOpcode()) { |
| 3537 | // TODO: look for (assumed) UB to backwards propagate "deadness". |
| 3538 | default: |
| 3539 | if (I->isTerminator()) { |
| 3540 | for (const BasicBlock *SuccBB : successors(I->getParent())) |
| 3541 | AliveSuccessors.push_back(&SuccBB->front()); |
| 3542 | } else { |
| 3543 | AliveSuccessors.push_back(I->getNextNode()); |
| 3544 | } |
| 3545 | break; |
| 3546 | case Instruction::Call: |
| 3547 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), |
| 3548 | *this, AliveSuccessors); |
| 3549 | break; |
| 3550 | case Instruction::Invoke: |
| 3551 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), |
| 3552 | *this, AliveSuccessors); |
| 3553 | break; |
| 3554 | case Instruction::Br: |
| 3555 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), |
| 3556 | *this, AliveSuccessors); |
| 3557 | break; |
| 3558 | case Instruction::Switch: |
| 3559 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), |
| 3560 | *this, AliveSuccessors); |
| 3561 | break; |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3562 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3563 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3564 | if (UsedAssumedInformation) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3565 | NewToBeExploredFrom.insert(I); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3566 | } else { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3567 | Change = ChangeStatus::CHANGED; |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3568 | if (AliveSuccessors.empty() || |
| 3569 | (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors())) |
| 3570 | KnownDeadEnds.insert(I); |
| 3571 | } |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3572 | |
| 3573 | LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " |
| 3574 | << AliveSuccessors.size() << " UsedAssumedInformation: " |
| 3575 | << UsedAssumedInformation << "\n"); |
| 3576 | |
| 3577 | for (const Instruction *AliveSuccessor : AliveSuccessors) { |
| 3578 | if (!I->isTerminator()) { |
| 3579 | assert(AliveSuccessors.size() == 1 && |
| 3580 | "Non-terminator expected to have a single successor!"); |
| 3581 | Worklist.push_back(AliveSuccessor); |
| 3582 | } else { |
| 3583 | if (assumeLive(A, *AliveSuccessor->getParent())) |
| 3584 | Worklist.push_back(AliveSuccessor); |
| 3585 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3586 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3589 | ToBeExploredFrom = std::move(NewToBeExploredFrom); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3590 | |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 3591 | // If we know everything is live there is no need to query for liveness. |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3592 | // Instead, indicating a pessimistic fixpoint will cause the state to be |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3593 | // "invalid" and all queries to be answered conservatively without lookups. |
| 3594 | // To be in this state we have to (1) finished the exploration and (3) not |
| 3595 | // discovered any non-trivial dead end and (2) not ruled unreachable code |
| 3596 | // dead. |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3597 | if (ToBeExploredFrom.empty() && |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3598 | getAssociatedFunction()->size() == AssumedLiveBlocks.size() && |
| 3599 | llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { |
| 3600 | return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; |
| 3601 | })) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3602 | return indicatePessimisticFixpoint(); |
| 3603 | return Change; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3606 | /// Liveness information for a call sites. |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3607 | struct AAIsDeadCallSite final : AAIsDeadFunction { |
| 3608 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3609 | |
| 3610 | /// See AbstractAttribute::initialize(...). |
| 3611 | void initialize(Attributor &A) override { |
| 3612 | // TODO: Once we have call site specific value information we can provide |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3613 | // call site specific liveness information and then it makes |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3614 | // sense to specialize attributes for call sites instead of |
| 3615 | // redirecting requests to the callee. |
| 3616 | llvm_unreachable("Abstract attributes for liveness are not " |
| 3617 | "supported for call sites yet!"); |
| 3618 | } |
| 3619 | |
| 3620 | /// See AbstractAttribute::updateImpl(...). |
| 3621 | ChangeStatus updateImpl(Attributor &A) override { |
| 3622 | return indicatePessimisticFixpoint(); |
| 3623 | } |
| 3624 | |
| 3625 | /// See AbstractAttribute::trackStatistics() |
| 3626 | void trackStatistics() const override {} |
| 3627 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3628 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3629 | /// -------------------- Dereferenceable Argument Attribute -------------------- |
| 3630 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3631 | template <> |
| 3632 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, |
| 3633 | const DerefState &R) { |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3634 | ChangeStatus CS0 = |
| 3635 | clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); |
| 3636 | ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3637 | return CS0 | CS1; |
| 3638 | } |
| 3639 | |
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 3640 | struct AADereferenceableImpl : AADereferenceable { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3641 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3642 | using StateType = DerefState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3643 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3644 | void initialize(Attributor &A) override { |
| 3645 | SmallVector<Attribute, 4> Attrs; |
| 3646 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, |
| 3647 | Attrs); |
| 3648 | for (const Attribute &Attr : Attrs) |
| 3649 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); |
| 3650 | |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 3651 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(), |
| 3652 | /* TrackDependence */ false); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3653 | |
| 3654 | const IRPosition &IRP = this->getIRPosition(); |
| 3655 | bool IsFnInterface = IRP.isFnInterfaceKind(); |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 3656 | Function *FnScope = IRP.getAnchorScope(); |
| 3657 | if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3658 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3661 | /// See AbstractAttribute::getState() |
| 3662 | /// { |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3663 | StateType &getState() override { return *this; } |
| 3664 | const StateType &getState() const override { return *this; } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3665 | /// } |
| 3666 | |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3667 | /// Helper function for collecting accessed bytes in must-be-executed-context |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 3668 | void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I, |
| 3669 | DerefState &State) { |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3670 | const Value *UseV = U->get(); |
| 3671 | if (!UseV->getType()->isPointerTy()) |
| 3672 | return; |
| 3673 | |
| 3674 | Type *PtrTy = UseV->getType(); |
| 3675 | const DataLayout &DL = A.getDataLayout(); |
| 3676 | int64_t Offset; |
| 3677 | if (const Value *Base = getBasePointerOfAccessPointerOperand( |
| 3678 | I, Offset, DL, /*AllowNonInbounds*/ true)) { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 3679 | if (Base == &getAssociatedValue() && |
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 3680 | getPointerOperand(I, /* AllowVolatile */ false) == UseV) { |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3681 | uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType()); |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 3682 | State.addAccessedBytes(Offset, Size); |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | return; |
| 3686 | } |
| 3687 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3688 | /// See AAFromMustBeExecutedContext |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 3689 | bool followUse(Attributor &A, const Use *U, const Instruction *I, |
| 3690 | AADereferenceable::StateType &State) { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3691 | bool IsNonNull = false; |
| 3692 | bool TrackUse = false; |
| 3693 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( |
| 3694 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3695 | |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 3696 | addAccessedBytesForUse(A, U, I, State); |
| 3697 | State.takeKnownDerefBytesMaximum(DerefBytes); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3698 | return TrackUse; |
| 3699 | } |
| 3700 | |
| Hideto Ueno | dfedae5 | 2019-11-29 06:45:07 +0000 | [diff] [blame] | 3701 | /// See AbstractAttribute::manifest(...). |
| 3702 | ChangeStatus manifest(Attributor &A) override { |
| 3703 | ChangeStatus Change = AADereferenceable::manifest(A); |
| 3704 | if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { |
| 3705 | removeAttrs({Attribute::DereferenceableOrNull}); |
| 3706 | return ChangeStatus::CHANGED; |
| 3707 | } |
| 3708 | return Change; |
| 3709 | } |
| 3710 | |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 3711 | void getDeducedAttributes(LLVMContext &Ctx, |
| 3712 | SmallVectorImpl<Attribute> &Attrs) const override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3713 | // TODO: Add *_globally support |
| 3714 | if (isAssumedNonNull()) |
| 3715 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( |
| 3716 | Ctx, getAssumedDereferenceableBytes())); |
| 3717 | else |
| 3718 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( |
| 3719 | Ctx, getAssumedDereferenceableBytes())); |
| 3720 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3721 | |
| 3722 | /// See AbstractAttribute::getAsStr(). |
| 3723 | const std::string getAsStr() const override { |
| 3724 | if (!getAssumedDereferenceableBytes()) |
| 3725 | return "unknown-dereferenceable"; |
| 3726 | return std::string("dereferenceable") + |
| 3727 | (isAssumedNonNull() ? "" : "_or_null") + |
| 3728 | (isAssumedGlobal() ? "_globally" : "") + "<" + |
| 3729 | std::to_string(getKnownDereferenceableBytes()) + "-" + |
| 3730 | std::to_string(getAssumedDereferenceableBytes()) + ">"; |
| 3731 | } |
| 3732 | }; |
| 3733 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3734 | /// Dereferenceable attribute for a floating value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3735 | struct AADereferenceableFloating |
| 3736 | : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { |
| 3737 | using Base = |
| 3738 | AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; |
| 3739 | AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3740 | |
| 3741 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3742 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3743 | ChangeStatus Change = Base::updateImpl(A); |
| 3744 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3745 | const DataLayout &DL = A.getDataLayout(); |
| 3746 | |
| 3747 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { |
| 3748 | unsigned IdxWidth = |
| 3749 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); |
| 3750 | APInt Offset(IdxWidth, 0); |
| 3751 | const Value *Base = |
| 3752 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 3753 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3754 | const auto &AA = |
| 3755 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3756 | int64_t DerefBytes = 0; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3757 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3758 | // Use IR information if we did not strip anything. |
| 3759 | // TODO: track globally. |
| 3760 | bool CanBeNull; |
| 3761 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); |
| 3762 | T.GlobalState.indicatePessimisticFixpoint(); |
| 3763 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3764 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3765 | DerefBytes = DS.DerefBytesState.getAssumed(); |
| 3766 | T.GlobalState &= DS.GlobalState; |
| 3767 | } |
| 3768 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 3769 | // TODO: Use `AAConstantRange` to infer dereferenceable bytes. |
| 3770 | |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3771 | // For now we do not try to "increase" dereferenceability due to negative |
| 3772 | // indices as we first have to come up with code to deal with loops and |
| 3773 | // for overflows of the dereferenceable bytes. |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3774 | int64_t OffsetSExt = Offset.getSExtValue(); |
| 3775 | if (OffsetSExt < 0) |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 3776 | OffsetSExt = 0; |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3777 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3778 | T.takeAssumedDerefBytesMinimum( |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3779 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3780 | |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3781 | if (this == &AA) { |
| 3782 | if (!Stripped) { |
| 3783 | // If nothing was stripped IR information is all we got. |
| 3784 | T.takeKnownDerefBytesMaximum( |
| 3785 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| 3786 | T.indicatePessimisticFixpoint(); |
| 3787 | } else if (OffsetSExt > 0) { |
| 3788 | // If something was stripped but there is circular reasoning we look |
| 3789 | // for the offset. If it is positive we basically decrease the |
| 3790 | // dereferenceable bytes in a circluar loop now, which will simply |
| 3791 | // drive them down to the known value in a very slow way which we |
| 3792 | // can accelerate. |
| 3793 | T.indicatePessimisticFixpoint(); |
| 3794 | } |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | return T.isValidState(); |
| 3798 | }; |
| 3799 | |
| 3800 | DerefState T; |
| 3801 | if (!genericValueTraversal<AADereferenceable, DerefState>( |
| 3802 | A, getIRPosition(), *this, T, VisitValueCB)) |
| 3803 | return indicatePessimisticFixpoint(); |
| 3804 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3805 | return Change | clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | /// See AbstractAttribute::trackStatistics() |
| 3809 | void trackStatistics() const override { |
| 3810 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) |
| 3811 | } |
| 3812 | }; |
| 3813 | |
| 3814 | /// Dereferenceable attribute for a return value. |
| 3815 | struct AADereferenceableReturned final |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3816 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3817 | AADereferenceableReturned(const IRPosition &IRP) |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3818 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>( |
| 3819 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3820 | |
| 3821 | /// See AbstractAttribute::trackStatistics() |
| 3822 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3823 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3824 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3825 | }; |
| 3826 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3827 | /// Dereferenceable attribute for an argument |
| 3828 | struct AADereferenceableArgument final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3829 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3830 | AADereferenceable, AADereferenceableImpl> { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3831 | using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3832 | AADereferenceable, AADereferenceableImpl>; |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3833 | AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3834 | |
| 3835 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3836 | void trackStatistics() const override { |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3837 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) |
| 3838 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3839 | }; |
| 3840 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3841 | /// Dereferenceable attribute for a call site argument. |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3842 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3843 | AADereferenceableCallSiteArgument(const IRPosition &IRP) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3844 | : AADereferenceableFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3845 | |
| 3846 | /// See AbstractAttribute::trackStatistics() |
| 3847 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3848 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3849 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3850 | }; |
| 3851 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3852 | /// Dereferenceable attribute deduction for a call site return value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3853 | struct AADereferenceableCallSiteReturned final |
| 3854 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< |
| 3855 | AADereferenceable, AADereferenceableImpl> { |
| 3856 | using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< |
| 3857 | AADereferenceable, AADereferenceableImpl>; |
| 3858 | AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3859 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3860 | /// See AbstractAttribute::trackStatistics() |
| 3861 | void trackStatistics() const override { |
| 3862 | STATS_DECLTRACK_CS_ATTR(dereferenceable); |
| 3863 | } |
| 3864 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3865 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3866 | // ------------------------ Align Argument Attribute ------------------------ |
| 3867 | |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3868 | static unsigned int getKnownAlignForUse(Attributor &A, |
| 3869 | AbstractAttribute &QueryingAA, |
| 3870 | Value &AssociatedValue, const Use *U, |
| 3871 | const Instruction *I, bool &TrackUse) { |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3872 | // We need to follow common pointer manipulation uses to the accesses they |
| 3873 | // feed into. |
| 3874 | if (isa<CastInst>(I)) { |
| Johannes Doerfert | c90681b | 2020-01-02 16:41:17 -0600 | [diff] [blame] | 3875 | // Follow all but ptr2int casts. |
| 3876 | TrackUse = !isa<PtrToIntInst>(I); |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3877 | return 0; |
| 3878 | } |
| 3879 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 3880 | if (GEP->hasAllConstantIndices()) { |
| 3881 | TrackUse = true; |
| 3882 | return 0; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | unsigned Alignment = 0; |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3887 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { |
| 3888 | if (ICS.isBundleOperand(U) || ICS.isCallee(U)) |
| 3889 | return 0; |
| 3890 | |
| 3891 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 3892 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); |
| 3893 | // As long as we only use known information there is no need to track |
| 3894 | // dependences here. |
| 3895 | auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, |
| 3896 | /* TrackDependence */ false); |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3897 | Alignment = AlignAA.getKnownAlign(); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3900 | const Value *UseV = U->get(); |
| Johannes Doerfert | 30ae859 | 2020-01-10 12:13:10 -0600 | [diff] [blame] | 3901 | if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 3902 | if (SI->getPointerOperand() == UseV) |
| 3903 | Alignment = SI->getAlignment(); |
| 3904 | } else if (auto *LI = dyn_cast<LoadInst>(I)) |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3905 | Alignment = LI->getAlignment(); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3906 | |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3907 | if (Alignment <= 1) |
| 3908 | return 0; |
| 3909 | |
| 3910 | auto &DL = A.getDataLayout(); |
| 3911 | int64_t Offset; |
| 3912 | |
| 3913 | if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { |
| 3914 | if (Base == &AssociatedValue) { |
| 3915 | // BasePointerAddr + Offset = Alignment * Q for some integer Q. |
| 3916 | // So we can say that the maximum power of two which is a divisor of |
| 3917 | // gcd(Offset, Alignment) is an alignment. |
| 3918 | |
| 3919 | uint32_t gcd = |
| 3920 | greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); |
| 3921 | Alignment = llvm::PowerOf2Floor(gcd); |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | return Alignment; |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3926 | } |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3927 | struct AAAlignImpl : AAAlign { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3928 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3929 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3930 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3931 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3932 | SmallVector<Attribute, 4> Attrs; |
| 3933 | getAttrs({Attribute::Alignment}, Attrs); |
| 3934 | for (const Attribute &Attr : Attrs) |
| 3935 | takeKnownMaximum(Attr.getValueAsInt()); |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 3936 | |
| 3937 | if (getIRPosition().isFnInterfaceKind() && |
| 3938 | (!getAssociatedFunction() || |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 3939 | !A.isFunctionIPOAmendable(*getAssociatedFunction()))) |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 3940 | indicatePessimisticFixpoint(); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3943 | /// See AbstractAttribute::manifest(...). |
| 3944 | ChangeStatus manifest(Attributor &A) override { |
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3945 | ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3946 | |
| 3947 | // Check for users that allow alignment annotations. |
| Johannes Doerfert | a801ee8 | 2020-02-19 16:52:16 -0600 | [diff] [blame] | 3948 | Value &AssociatedValue = getAssociatedValue(); |
| 3949 | for (const Use &U : AssociatedValue.uses()) { |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3950 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { |
| Johannes Doerfert | a801ee8 | 2020-02-19 16:52:16 -0600 | [diff] [blame] | 3951 | if (SI->getPointerOperand() == &AssociatedValue) |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3952 | if (SI->getAlignment() < getAssumedAlign()) { |
| 3953 | STATS_DECLTRACK(AAAlign, Store, |
| James Henderson | d68904f | 2020-01-06 10:15:44 +0000 | [diff] [blame] | 3954 | "Number of times alignment added to a store"); |
| Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 3955 | SI->setAlignment(Align(getAssumedAlign())); |
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3956 | LoadStoreChanged = ChangeStatus::CHANGED; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3957 | } |
| 3958 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { |
| Johannes Doerfert | a801ee8 | 2020-02-19 16:52:16 -0600 | [diff] [blame] | 3959 | if (LI->getPointerOperand() == &AssociatedValue) |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3960 | if (LI->getAlignment() < getAssumedAlign()) { |
| Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 3961 | LI->setAlignment(Align(getAssumedAlign())); |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3962 | STATS_DECLTRACK(AAAlign, Load, |
| James Henderson | d68904f | 2020-01-06 10:15:44 +0000 | [diff] [blame] | 3963 | "Number of times alignment added to a load"); |
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3964 | LoadStoreChanged = ChangeStatus::CHANGED; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | |
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3969 | ChangeStatus Changed = AAAlign::manifest(A); |
| 3970 | |
| 3971 | MaybeAlign InheritAlign = |
| 3972 | getAssociatedValue().getPointerAlignment(A.getDataLayout()); |
| 3973 | if (InheritAlign.valueOrOne() >= getAssumedAlign()) |
| 3974 | return LoadStoreChanged; |
| 3975 | return Changed | LoadStoreChanged; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3978 | // TODO: Provide a helper to determine the implied ABI alignment and check in |
| 3979 | // the existing manifest method and a new one for AAAlignImpl that value |
| 3980 | // to avoid making the alignment explicit if it did not improve. |
| 3981 | |
| 3982 | /// See AbstractAttribute::getDeducedAttributes |
| 3983 | virtual void |
| 3984 | getDeducedAttributes(LLVMContext &Ctx, |
| 3985 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 3986 | if (getAssumedAlign() > 1) |
| Guillaume Chatelet | b65fa48 | 2019-10-15 12:56:24 +0000 | [diff] [blame] | 3987 | Attrs.emplace_back( |
| 3988 | Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3989 | } |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3990 | /// See AAFromMustBeExecutedContext |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 3991 | bool followUse(Attributor &A, const Use *U, const Instruction *I, |
| 3992 | AAAlign::StateType &State) { |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3993 | bool TrackUse = false; |
| 3994 | |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 3995 | unsigned int KnownAlign = |
| 3996 | getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); |
| Hideto Ueno | bdcbdb4 | 2020-03-09 14:19:32 +0900 | [diff] [blame] | 3997 | State.takeKnownMaximum(KnownAlign); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3998 | |
| 3999 | return TrackUse; |
| 4000 | } |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 4001 | |
| 4002 | /// See AbstractAttribute::getAsStr(). |
| 4003 | const std::string getAsStr() const override { |
| 4004 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + |
| 4005 | "-" + std::to_string(getAssumedAlign()) + ">") |
| 4006 | : "unknown-align"; |
| 4007 | } |
| 4008 | }; |
| 4009 | |
| 4010 | /// Align attribute for a floating value. |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 4011 | struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> { |
| 4012 | using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>; |
| 4013 | AAAlignFloating(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 4014 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4015 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4016 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 4017 | Base::updateImpl(A); |
| 4018 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4019 | const DataLayout &DL = A.getDataLayout(); |
| 4020 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 4021 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, |
| 4022 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4023 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); |
| 4024 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4025 | // Use only IR information if we did not strip anything. |
| Guillaume Chatelet | bae629b | 2019-10-15 13:58:22 +0000 | [diff] [blame] | 4026 | const MaybeAlign PA = V.getPointerAlignment(DL); |
| 4027 | T.takeKnownMaximum(PA ? PA->value() : 0); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4028 | T.indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4029 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4030 | // Use abstract attribute information. |
| 4031 | const AAAlign::StateType &DS = |
| 4032 | static_cast<const AAAlign::StateType &>(AA.getState()); |
| 4033 | T ^= DS; |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4034 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 4035 | return T.isValidState(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4036 | }; |
| 4037 | |
| 4038 | StateType T; |
| 4039 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, |
| 4040 | VisitValueCB)) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 4041 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4042 | |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 4043 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 4044 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4045 | return clampStateAndIndicateChange(getState(), T); |
| 4046 | } |
| 4047 | |
| 4048 | /// See AbstractAttribute::trackStatistics() |
| 4049 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } |
| 4050 | }; |
| 4051 | |
| 4052 | /// Align attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4053 | struct AAAlignReturned final |
| 4054 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4055 | AAAlignReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4056 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4057 | |
| 4058 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 4059 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4060 | }; |
| 4061 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4062 | /// Align attribute for function argument. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4063 | struct AAAlignArgument final |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 4064 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, |
| 4065 | AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4066 | AAAlignArgument(const IRPosition &IRP) |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 4067 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, |
| 4068 | AAAlignImpl>( |
| 4069 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4070 | |
| 4071 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 4072 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4073 | }; |
| 4074 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 4075 | struct AAAlignCallSiteArgument final : AAAlignFloating { |
| 4076 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4077 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 4078 | /// See AbstractAttribute::manifest(...). |
| 4079 | ChangeStatus manifest(Attributor &A) override { |
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 4080 | ChangeStatus Changed = AAAlignImpl::manifest(A); |
| 4081 | MaybeAlign InheritAlign = |
| 4082 | getAssociatedValue().getPointerAlignment(A.getDataLayout()); |
| 4083 | if (InheritAlign.valueOrOne() >= getAssumedAlign()) |
| 4084 | Changed = ChangeStatus::UNCHANGED; |
| 4085 | return Changed; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 4088 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| 4089 | ChangeStatus updateImpl(Attributor &A) override { |
| 4090 | ChangeStatus Changed = AAAlignFloating::updateImpl(A); |
| 4091 | if (Argument *Arg = getAssociatedArgument()) { |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4092 | // We only take known information from the argument |
| 4093 | // so we do not need to track a dependence. |
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 4094 | const auto &ArgAlignAA = A.getAAFor<AAAlign>( |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4095 | *this, IRPosition::argument(*Arg), /* TrackDependence */ false); |
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 4096 | takeKnownMaximum(ArgAlignAA.getKnownAlign()); |
| 4097 | } |
| 4098 | return Changed; |
| 4099 | } |
| 4100 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4101 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 4102 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4103 | }; |
| 4104 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 4105 | /// Align attribute deduction for a call site return value. |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 4106 | struct AAAlignCallSiteReturned final |
| 4107 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, |
| 4108 | AAAlignImpl> { |
| 4109 | using Base = |
| 4110 | AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, |
| 4111 | AAAlignImpl>; |
| 4112 | AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 4113 | |
| 4114 | /// See AbstractAttribute::initialize(...). |
| 4115 | void initialize(Attributor &A) override { |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 4116 | Base::initialize(A); |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 4117 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4118 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 4119 | indicatePessimisticFixpoint(); |
| 4120 | } |
| 4121 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 4122 | /// See AbstractAttribute::trackStatistics() |
| 4123 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } |
| 4124 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 4125 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4126 | /// ------------------ Function No-Return Attribute ---------------------------- |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 4127 | struct AANoReturnImpl : public AANoReturn { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4128 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4129 | |
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 4130 | /// See AbstractAttribute::initialize(...). |
| 4131 | void initialize(Attributor &A) override { |
| 4132 | AANoReturn::initialize(A); |
| 4133 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | 1b6041a | 2019-11-01 20:17:48 -0500 | [diff] [blame] | 4134 | if (!F) |
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 4135 | indicatePessimisticFixpoint(); |
| 4136 | } |
| 4137 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4138 | /// See AbstractAttribute::getAsStr(). |
| 4139 | const std::string getAsStr() const override { |
| 4140 | return getAssumed() ? "noreturn" : "may-return"; |
| 4141 | } |
| 4142 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4143 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 4144 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4145 | auto CheckForNoReturn = [](Instruction &) { return false; }; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4146 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4147 | {(unsigned)Instruction::Ret})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4148 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4149 | return ChangeStatus::UNCHANGED; |
| 4150 | } |
| 4151 | }; |
| 4152 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4153 | struct AANoReturnFunction final : AANoReturnImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4154 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4155 | |
| 4156 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 4157 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4158 | }; |
| 4159 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 4160 | /// NoReturn attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 4161 | struct AANoReturnCallSite final : AANoReturnImpl { |
| 4162 | AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| 4163 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 4164 | /// See AbstractAttribute::updateImpl(...). |
| 4165 | ChangeStatus updateImpl(Attributor &A) override { |
| 4166 | // TODO: Once we have call site specific value information we can provide |
| 4167 | // call site specific liveness information and then it makes |
| 4168 | // sense to specialize attributes for call sites arguments instead of |
| 4169 | // redirecting requests to the callee argument. |
| 4170 | Function *F = getAssociatedFunction(); |
| 4171 | const IRPosition &FnPos = IRPosition::function(*F); |
| 4172 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); |
| 4173 | return clampStateAndIndicateChange( |
| 4174 | getState(), |
| 4175 | static_cast<const AANoReturn::StateType &>(FnAA.getState())); |
| 4176 | } |
| 4177 | |
| 4178 | /// See AbstractAttribute::trackStatistics() |
| 4179 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } |
| 4180 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 4181 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4182 | /// ----------------------- Variable Capturing --------------------------------- |
| 4183 | |
| 4184 | /// A class to hold the state of for no-capture attributes. |
| 4185 | struct AANoCaptureImpl : public AANoCapture { |
| 4186 | AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} |
| 4187 | |
| 4188 | /// See AbstractAttribute::initialize(...). |
| 4189 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 4190 | if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { |
| 4191 | indicateOptimisticFixpoint(); |
| 4192 | return; |
| 4193 | } |
| 4194 | Function *AnchorScope = getAnchorScope(); |
| 4195 | if (isFnInterfaceKind() && |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 4196 | (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) { |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 4197 | indicatePessimisticFixpoint(); |
| 4198 | return; |
| 4199 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4200 | |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 4201 | // You cannot "capture" null in the default address space. |
| 4202 | if (isa<ConstantPointerNull>(getAssociatedValue()) && |
| 4203 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { |
| 4204 | indicateOptimisticFixpoint(); |
| 4205 | return; |
| 4206 | } |
| 4207 | |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 4208 | const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4209 | |
| 4210 | // Check what state the associated function can actually capture. |
| 4211 | if (F) |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 4212 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4213 | else |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4214 | indicatePessimisticFixpoint(); |
| 4215 | } |
| 4216 | |
| 4217 | /// See AbstractAttribute::updateImpl(...). |
| 4218 | ChangeStatus updateImpl(Attributor &A) override; |
| 4219 | |
| 4220 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). |
| 4221 | virtual void |
| 4222 | getDeducedAttributes(LLVMContext &Ctx, |
| 4223 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 4224 | if (!isAssumedNoCaptureMaybeReturned()) |
| 4225 | return; |
| 4226 | |
| Hideto Ueno | 3736764 | 2019-09-11 06:52:11 +0000 | [diff] [blame] | 4227 | if (getArgNo() >= 0) { |
| 4228 | if (isAssumedNoCapture()) |
| 4229 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); |
| 4230 | else if (ManifestInternal) |
| 4231 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); |
| 4232 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known |
| 4236 | /// depending on the ability of the function associated with \p IRP to capture |
| 4237 | /// state in memory and through "returning/throwing", respectively. |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4238 | static void determineFunctionCaptureCapabilities(const IRPosition &IRP, |
| 4239 | const Function &F, |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4240 | BitIntegerState &State) { |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4241 | // TODO: Once we have memory behavior attributes we should use them here. |
| 4242 | |
| 4243 | // If we know we cannot communicate or write to memory, we do not care about |
| 4244 | // ptr2int anymore. |
| 4245 | if (F.onlyReadsMemory() && F.doesNotThrow() && |
| 4246 | F.getReturnType()->isVoidTy()) { |
| 4247 | State.addKnownBits(NO_CAPTURE); |
| 4248 | return; |
| 4249 | } |
| 4250 | |
| 4251 | // A function cannot capture state in memory if it only reads memory, it can |
| 4252 | // however return/throw state and the state might be influenced by the |
| 4253 | // pointer value, e.g., loading from a returned pointer might reveal a bit. |
| 4254 | if (F.onlyReadsMemory()) |
| 4255 | State.addKnownBits(NOT_CAPTURED_IN_MEM); |
| 4256 | |
| 4257 | // A function cannot communicate state back if it does not through |
| 4258 | // exceptions and doesn not return values. |
| 4259 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) |
| 4260 | State.addKnownBits(NOT_CAPTURED_IN_RET); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4261 | |
| 4262 | // Check existing "returned" attributes. |
| 4263 | int ArgNo = IRP.getArgNo(); |
| 4264 | if (F.doesNotThrow() && ArgNo >= 0) { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 4265 | for (unsigned u = 0, e = F.arg_size(); u < e; ++u) |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4266 | if (F.hasParamAttribute(u, Attribute::Returned)) { |
| Johannes Doerfert | 9d5ad5e | 2019-10-21 01:29:10 +0000 | [diff] [blame] | 4267 | if (u == unsigned(ArgNo)) |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4268 | State.removeAssumedBits(NOT_CAPTURED_IN_RET); |
| 4269 | else if (F.onlyReadsMemory()) |
| 4270 | State.addKnownBits(NO_CAPTURE); |
| 4271 | else |
| 4272 | State.addKnownBits(NOT_CAPTURED_IN_RET); |
| 4273 | break; |
| 4274 | } |
| 4275 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | /// See AbstractState::getAsStr(). |
| 4279 | const std::string getAsStr() const override { |
| 4280 | if (isKnownNoCapture()) |
| 4281 | return "known not-captured"; |
| 4282 | if (isAssumedNoCapture()) |
| 4283 | return "assumed not-captured"; |
| 4284 | if (isKnownNoCaptureMaybeReturned()) |
| 4285 | return "known not-captured-maybe-returned"; |
| 4286 | if (isAssumedNoCaptureMaybeReturned()) |
| 4287 | return "assumed not-captured-maybe-returned"; |
| 4288 | return "assumed-captured"; |
| 4289 | } |
| 4290 | }; |
| 4291 | |
| 4292 | /// Attributor-aware capture tracker. |
| 4293 | struct AACaptureUseTracker final : public CaptureTracker { |
| 4294 | |
| 4295 | /// Create a capture tracker that can lookup in-flight abstract attributes |
| 4296 | /// through the Attributor \p A. |
| 4297 | /// |
| 4298 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the |
| 4299 | /// search is stopped. If a use leads to a return instruction, |
| 4300 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. |
| 4301 | /// If a use leads to a ptr2int which may capture the value, |
| 4302 | /// \p CapturedInInteger is set. If a use is found that is currently assumed |
| 4303 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies |
| 4304 | /// set. All values in \p PotentialCopies are later tracked as well. For every |
| 4305 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, |
| 4306 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger |
| 4307 | /// conservatively set to true. |
| 4308 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4309 | const AAIsDead &IsDeadAA, AANoCapture::StateType &State, |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4310 | SmallVectorImpl<const Value *> &PotentialCopies, |
| 4311 | unsigned &RemainingUsesToExplore) |
| 4312 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), |
| 4313 | PotentialCopies(PotentialCopies), |
| 4314 | RemainingUsesToExplore(RemainingUsesToExplore) {} |
| 4315 | |
| 4316 | /// Determine if \p V maybe captured. *Also updates the state!* |
| 4317 | bool valueMayBeCaptured(const Value *V) { |
| 4318 | if (V->getType()->isPointerTy()) { |
| 4319 | PointerMayBeCaptured(V, this); |
| 4320 | } else { |
| 4321 | State.indicatePessimisticFixpoint(); |
| 4322 | } |
| 4323 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); |
| 4324 | } |
| 4325 | |
| 4326 | /// See CaptureTracker::tooManyUses(). |
| 4327 | void tooManyUses() override { |
| 4328 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); |
| 4329 | } |
| 4330 | |
| 4331 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { |
| 4332 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) |
| 4333 | return true; |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4334 | const auto &DerefAA = A.getAAFor<AADereferenceable>( |
| 4335 | NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true, |
| 4336 | DepClassTy::OPTIONAL); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4337 | return DerefAA.getAssumedDereferenceableBytes(); |
| 4338 | } |
| 4339 | |
| 4340 | /// See CaptureTracker::captured(...). |
| 4341 | bool captured(const Use *U) override { |
| 4342 | Instruction *UInst = cast<Instruction>(U->getUser()); |
| 4343 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst |
| 4344 | << "\n"); |
| 4345 | |
| 4346 | // Because we may reuse the tracker multiple times we keep track of the |
| 4347 | // number of explored uses ourselves as well. |
| 4348 | if (RemainingUsesToExplore-- == 0) { |
| 4349 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); |
| 4350 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 4351 | /* Return */ true); |
| 4352 | } |
| 4353 | |
| 4354 | // Deal with ptr2int by following uses. |
| 4355 | if (isa<PtrToIntInst>(UInst)) { |
| 4356 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); |
| 4357 | return valueMayBeCaptured(UInst); |
| 4358 | } |
| 4359 | |
| 4360 | // Explicitly catch return instructions. |
| 4361 | if (isa<ReturnInst>(UInst)) |
| 4362 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 4363 | /* Return */ true); |
| 4364 | |
| 4365 | // For now we only use special logic for call sites. However, the tracker |
| 4366 | // itself knows about a lot of other non-capturing cases already. |
| 4367 | CallSite CS(UInst); |
| 4368 | if (!CS || !CS.isArgOperand(U)) |
| 4369 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 4370 | /* Return */ true); |
| 4371 | |
| 4372 | unsigned ArgNo = CS.getArgumentNo(U); |
| 4373 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); |
| 4374 | // If we have a abstract no-capture attribute for the argument we can use |
| 4375 | // it to justify a non-capture attribute here. This allows recursion! |
| 4376 | auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); |
| 4377 | if (ArgNoCaptureAA.isAssumedNoCapture()) |
| 4378 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 4379 | /* Return */ false); |
| 4380 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 4381 | addPotentialCopy(CS); |
| 4382 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 4383 | /* Return */ false); |
| 4384 | } |
| 4385 | |
| 4386 | // Lastly, we could not find a reason no-capture can be assumed so we don't. |
| 4387 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 4388 | /* Return */ true); |
| 4389 | } |
| 4390 | |
| 4391 | /// Register \p CS as potential copy of the value we are checking. |
| 4392 | void addPotentialCopy(CallSite CS) { |
| 4393 | PotentialCopies.push_back(CS.getInstruction()); |
| 4394 | } |
| 4395 | |
| 4396 | /// See CaptureTracker::shouldExplore(...). |
| 4397 | bool shouldExplore(const Use *U) override { |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 4398 | // Check liveness. |
| 4399 | return !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and |
| 4403 | /// \p CapturedInRet, then return the appropriate value for use in the |
| 4404 | /// CaptureTracker::captured() interface. |
| 4405 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, |
| 4406 | bool CapturedInRet) { |
| 4407 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " |
| 4408 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); |
| 4409 | if (CapturedInMem) |
| 4410 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); |
| 4411 | if (CapturedInInt) |
| 4412 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); |
| 4413 | if (CapturedInRet) |
| 4414 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); |
| 4415 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); |
| 4416 | } |
| 4417 | |
| 4418 | private: |
| 4419 | /// The attributor providing in-flight abstract attributes. |
| 4420 | Attributor &A; |
| 4421 | |
| 4422 | /// The abstract attribute currently updated. |
| 4423 | AANoCapture &NoCaptureAA; |
| 4424 | |
| 4425 | /// The abstract liveness state. |
| 4426 | const AAIsDead &IsDeadAA; |
| 4427 | |
| 4428 | /// The state currently updated. |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4429 | AANoCapture::StateType &State; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4430 | |
| 4431 | /// Set of potential copies of the tracked value. |
| 4432 | SmallVectorImpl<const Value *> &PotentialCopies; |
| 4433 | |
| 4434 | /// Global counter to limit the number of explored uses. |
| 4435 | unsigned &RemainingUsesToExplore; |
| 4436 | }; |
| 4437 | |
| 4438 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { |
| 4439 | const IRPosition &IRP = getIRPosition(); |
| 4440 | const Value *V = |
| 4441 | getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); |
| 4442 | if (!V) |
| 4443 | return indicatePessimisticFixpoint(); |
| 4444 | |
| 4445 | const Function *F = |
| 4446 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); |
| 4447 | assert(F && "Expected a function!"); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4448 | const IRPosition &FnPos = IRPosition::function(*F); |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4449 | const auto &IsDeadAA = |
| 4450 | A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4451 | |
| 4452 | AANoCapture::StateType T; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4453 | |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4454 | // Readonly means we cannot capture through memory. |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4455 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>( |
| 4456 | *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4457 | if (FnMemAA.isAssumedReadOnly()) { |
| 4458 | T.addKnownBits(NOT_CAPTURED_IN_MEM); |
| 4459 | if (FnMemAA.isKnownReadOnly()) |
| 4460 | addKnownBits(NOT_CAPTURED_IN_MEM); |
| 4461 | } |
| 4462 | |
| 4463 | // Make sure all returned values are different than the underlying value. |
| 4464 | // TODO: we could do this in a more sophisticated way inside |
| 4465 | // AAReturnedValues, e.g., track all values that escape through returns |
| 4466 | // directly somehow. |
| 4467 | auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { |
| 4468 | bool SeenConstant = false; |
| 4469 | for (auto &It : RVAA.returned_values()) { |
| 4470 | if (isa<Constant>(It.first)) { |
| 4471 | if (SeenConstant) |
| 4472 | return false; |
| 4473 | SeenConstant = true; |
| 4474 | } else if (!isa<Argument>(It.first) || |
| 4475 | It.first == getAssociatedArgument()) |
| 4476 | return false; |
| 4477 | } |
| 4478 | return true; |
| 4479 | }; |
| 4480 | |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4481 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>( |
| 4482 | *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4483 | if (NoUnwindAA.isAssumedNoUnwind()) { |
| 4484 | bool IsVoidTy = F->getReturnType()->isVoidTy(); |
| 4485 | const AAReturnedValues *RVAA = |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4486 | IsVoidTy ? nullptr |
| 4487 | : &A.getAAFor<AAReturnedValues>(*this, FnPos, |
| 4488 | /* TrackDependence */ true, |
| 4489 | DepClassTy::OPTIONAL); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4490 | if (IsVoidTy || CheckReturnedArgs(*RVAA)) { |
| 4491 | T.addKnownBits(NOT_CAPTURED_IN_RET); |
| 4492 | if (T.isKnown(NOT_CAPTURED_IN_MEM)) |
| 4493 | return ChangeStatus::UNCHANGED; |
| 4494 | if (NoUnwindAA.isKnownNoUnwind() && |
| 4495 | (IsVoidTy || RVAA->getState().isAtFixpoint())) { |
| 4496 | addKnownBits(NOT_CAPTURED_IN_RET); |
| 4497 | if (isKnown(NOT_CAPTURED_IN_MEM)) |
| 4498 | return indicateOptimisticFixpoint(); |
| 4499 | } |
| 4500 | } |
| 4501 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4502 | |
| 4503 | // Use the CaptureTracker interface and logic with the specialized tracker, |
| 4504 | // defined in AACaptureUseTracker, that can look at in-flight abstract |
| 4505 | // attributes and directly updates the assumed state. |
| 4506 | SmallVector<const Value *, 4> PotentialCopies; |
| 4507 | unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; |
| 4508 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, |
| 4509 | RemainingUsesToExplore); |
| 4510 | |
| 4511 | // Check all potential copies of the associated value until we can assume |
| 4512 | // none will be captured or we have to assume at least one might be. |
| 4513 | unsigned Idx = 0; |
| 4514 | PotentialCopies.push_back(V); |
| 4515 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) |
| 4516 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); |
| 4517 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4518 | AANoCapture::StateType &S = getState(); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4519 | auto Assumed = S.getAssumed(); |
| 4520 | S.intersectAssumedBits(T.getAssumed()); |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4521 | if (!isAssumedNoCaptureMaybeReturned()) |
| 4522 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4523 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED |
| 4524 | : ChangeStatus::CHANGED; |
| 4525 | } |
| 4526 | |
| 4527 | /// NoCapture attribute for function arguments. |
| 4528 | struct AANoCaptureArgument final : AANoCaptureImpl { |
| 4529 | AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4530 | |
| 4531 | /// See AbstractAttribute::trackStatistics() |
| 4532 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } |
| 4533 | }; |
| 4534 | |
| 4535 | /// NoCapture attribute for call site arguments. |
| 4536 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { |
| 4537 | AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4538 | |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4539 | /// See AbstractAttribute::initialize(...). |
| 4540 | void initialize(Attributor &A) override { |
| 4541 | if (Argument *Arg = getAssociatedArgument()) |
| 4542 | if (Arg->hasByValAttr()) |
| 4543 | indicateOptimisticFixpoint(); |
| 4544 | AANoCaptureImpl::initialize(A); |
| 4545 | } |
| 4546 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4547 | /// See AbstractAttribute::updateImpl(...). |
| 4548 | ChangeStatus updateImpl(Attributor &A) override { |
| 4549 | // TODO: Once we have call site specific value information we can provide |
| 4550 | // call site specific liveness information and then it makes |
| 4551 | // sense to specialize attributes for call sites arguments instead of |
| 4552 | // redirecting requests to the callee argument. |
| 4553 | Argument *Arg = getAssociatedArgument(); |
| 4554 | if (!Arg) |
| 4555 | return indicatePessimisticFixpoint(); |
| 4556 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 4557 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); |
| 4558 | return clampStateAndIndicateChange( |
| 4559 | getState(), |
| 4560 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); |
| 4561 | } |
| 4562 | |
| 4563 | /// See AbstractAttribute::trackStatistics() |
| 4564 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; |
| 4565 | }; |
| 4566 | |
| 4567 | /// NoCapture attribute for floating values. |
| 4568 | struct AANoCaptureFloating final : AANoCaptureImpl { |
| 4569 | AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4570 | |
| 4571 | /// See AbstractAttribute::trackStatistics() |
| 4572 | void trackStatistics() const override { |
| 4573 | STATS_DECLTRACK_FLOATING_ATTR(nocapture) |
| 4574 | } |
| 4575 | }; |
| 4576 | |
| 4577 | /// NoCapture attribute for function return value. |
| 4578 | struct AANoCaptureReturned final : AANoCaptureImpl { |
| 4579 | AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { |
| 4580 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 4581 | } |
| 4582 | |
| 4583 | /// See AbstractAttribute::initialize(...). |
| 4584 | void initialize(Attributor &A) override { |
| 4585 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 4586 | } |
| 4587 | |
| 4588 | /// See AbstractAttribute::updateImpl(...). |
| 4589 | ChangeStatus updateImpl(Attributor &A) override { |
| 4590 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 4591 | } |
| 4592 | |
| 4593 | /// See AbstractAttribute::trackStatistics() |
| 4594 | void trackStatistics() const override {} |
| 4595 | }; |
| 4596 | |
| 4597 | /// NoCapture attribute deduction for a call site return value. |
| 4598 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { |
| 4599 | AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4600 | |
| 4601 | /// See AbstractAttribute::trackStatistics() |
| 4602 | void trackStatistics() const override { |
| 4603 | STATS_DECLTRACK_CSRET_ATTR(nocapture) |
| 4604 | } |
| 4605 | }; |
| 4606 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4607 | /// ------------------ Value Simplify Attribute ---------------------------- |
| 4608 | struct AAValueSimplifyImpl : AAValueSimplify { |
| 4609 | AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} |
| 4610 | |
| Johannes Doerfert | 9dcf889 | 2020-01-11 23:59:36 -0600 | [diff] [blame] | 4611 | /// See AbstractAttribute::initialize(...). |
| 4612 | void initialize(Attributor &A) override { |
| 4613 | if (getAssociatedValue().getType()->isVoidTy()) |
| 4614 | indicatePessimisticFixpoint(); |
| 4615 | } |
| 4616 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4617 | /// See AbstractAttribute::getAsStr(). |
| 4618 | const std::string getAsStr() const override { |
| 4619 | return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") |
| 4620 | : "not-simple"; |
| 4621 | } |
| 4622 | |
| 4623 | /// See AbstractAttribute::trackStatistics() |
| 4624 | void trackStatistics() const override {} |
| 4625 | |
| 4626 | /// See AAValueSimplify::getAssumedSimplifiedValue() |
| 4627 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { |
| 4628 | if (!getAssumed()) |
| 4629 | return const_cast<Value *>(&getAssociatedValue()); |
| 4630 | return SimplifiedAssociatedValue; |
| 4631 | } |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4632 | |
| 4633 | /// Helper function for querying AAValueSimplify and updating candicate. |
| 4634 | /// \param QueryingValue Value trying to unify with SimplifiedValue |
| 4635 | /// \param AccumulatedSimplifiedValue Current simplification result. |
| 4636 | static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, |
| 4637 | Value &QueryingValue, |
| 4638 | Optional<Value *> &AccumulatedSimplifiedValue) { |
| 4639 | // FIXME: Add a typecast support. |
| 4640 | |
| Johannes Doerfert | d07b5a5 | 2020-01-12 00:00:33 -0600 | [diff] [blame] | 4641 | auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>( |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4642 | QueryingAA, IRPosition::value(QueryingValue)); |
| 4643 | |
| 4644 | Optional<Value *> QueryingValueSimplified = |
| Johannes Doerfert | d07b5a5 | 2020-01-12 00:00:33 -0600 | [diff] [blame] | 4645 | ValueSimplifyAA.getAssumedSimplifiedValue(A); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4646 | |
| 4647 | if (!QueryingValueSimplified.hasValue()) |
| 4648 | return true; |
| 4649 | |
| 4650 | if (!QueryingValueSimplified.getValue()) |
| 4651 | return false; |
| 4652 | |
| 4653 | Value &QueryingValueSimplifiedUnwrapped = |
| 4654 | *QueryingValueSimplified.getValue(); |
| 4655 | |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4656 | if (AccumulatedSimplifiedValue.hasValue() && |
| 4657 | !isa<UndefValue>(AccumulatedSimplifiedValue.getValue()) && |
| 4658 | !isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4659 | return AccumulatedSimplifiedValue == QueryingValueSimplified; |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4660 | if (AccumulatedSimplifiedValue.hasValue() && |
| 4661 | isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) |
| 4662 | return true; |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4663 | |
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 4664 | LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4665 | << " is assumed to be " |
| 4666 | << QueryingValueSimplifiedUnwrapped << "\n"); |
| 4667 | |
| 4668 | AccumulatedSimplifiedValue = QueryingValueSimplified; |
| 4669 | return true; |
| 4670 | } |
| 4671 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4672 | bool askSimplifiedValueForAAValueConstantRange(Attributor &A) { |
| 4673 | if (!getAssociatedValue().getType()->isIntegerTy()) |
| 4674 | return false; |
| 4675 | |
| 4676 | const auto &ValueConstantRangeAA = |
| 4677 | A.getAAFor<AAValueConstantRange>(*this, getIRPosition()); |
| 4678 | |
| 4679 | Optional<ConstantInt *> COpt = |
| 4680 | ValueConstantRangeAA.getAssumedConstantInt(A); |
| 4681 | if (COpt.hasValue()) { |
| 4682 | if (auto *C = COpt.getValue()) |
| 4683 | SimplifiedAssociatedValue = C; |
| 4684 | else |
| 4685 | return false; |
| 4686 | } else { |
| Johannes Doerfert | 63adbb9 | 2020-02-09 20:21:56 -0600 | [diff] [blame] | 4687 | SimplifiedAssociatedValue = llvm::None; |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4688 | } |
| 4689 | return true; |
| 4690 | } |
| 4691 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4692 | /// See AbstractAttribute::manifest(...). |
| 4693 | ChangeStatus manifest(Attributor &A) override { |
| 4694 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 4695 | |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4696 | if (SimplifiedAssociatedValue.hasValue() && |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4697 | !SimplifiedAssociatedValue.getValue()) |
| 4698 | return Changed; |
| 4699 | |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4700 | Value &V = getAssociatedValue(); |
| 4701 | auto *C = SimplifiedAssociatedValue.hasValue() |
| 4702 | ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue()) |
| 4703 | : UndefValue::get(V.getType()); |
| 4704 | if (C) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4705 | // We can replace the AssociatedValue with the constant. |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4706 | if (!V.user_empty() && &V != C && V.getType() == C->getType()) { |
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 4707 | LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C |
| 4708 | << " :: " << *this << "\n"); |
| Johannes Doerfert | 4c62a35 | 2020-01-12 00:17:08 -0600 | [diff] [blame] | 4709 | if (A.changeValueAfterManifest(V, *C)) |
| 4710 | Changed = ChangeStatus::CHANGED; |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4711 | } |
| 4712 | } |
| 4713 | |
| 4714 | return Changed | AAValueSimplify::manifest(A); |
| 4715 | } |
| 4716 | |
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4717 | /// See AbstractState::indicatePessimisticFixpoint(...). |
| 4718 | ChangeStatus indicatePessimisticFixpoint() override { |
| 4719 | // NOTE: Associated value will be returned in a pessimistic fixpoint and is |
| 4720 | // regarded as known. That's why`indicateOptimisticFixpoint` is called. |
| 4721 | SimplifiedAssociatedValue = &getAssociatedValue(); |
| Johannes Doerfert | a4b3588 | 2019-12-31 13:25:47 -0600 | [diff] [blame] | 4722 | indicateOptimisticFixpoint(); |
| 4723 | return ChangeStatus::CHANGED; |
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4724 | } |
| 4725 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4726 | protected: |
| 4727 | // An assumed simplified value. Initially, it is set to Optional::None, which |
| 4728 | // means that the value is not clear under current assumption. If in the |
| 4729 | // pessimistic state, getAssumedSimplifiedValue doesn't return this value but |
| 4730 | // returns orignal associated value. |
| 4731 | Optional<Value *> SimplifiedAssociatedValue; |
| 4732 | }; |
| 4733 | |
| 4734 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { |
| 4735 | AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4736 | |
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4737 | void initialize(Attributor &A) override { |
| 4738 | AAValueSimplifyImpl::initialize(A); |
| 4739 | if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) |
| 4740 | indicatePessimisticFixpoint(); |
| 4741 | if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, |
| 4742 | /* IgnoreSubsumingPositions */ true)) |
| 4743 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 16188f9 | 2020-02-16 23:04:25 -0600 | [diff] [blame] | 4744 | |
| 4745 | // FIXME: This is a hack to prevent us from propagating function poiner in |
| 4746 | // the new pass manager CGSCC pass as it creates call edges the |
| 4747 | // CallGraphUpdater cannot handle yet. |
| 4748 | Value &V = getAssociatedValue(); |
| 4749 | if (V.getType()->isPointerTy() && |
| 4750 | V.getType()->getPointerElementType()->isFunctionTy() && |
| Johannes Doerfert | 9708279 | 2020-02-21 14:18:30 -0800 | [diff] [blame] | 4751 | !A.isModulePass()) |
| Johannes Doerfert | 16188f9 | 2020-02-16 23:04:25 -0600 | [diff] [blame] | 4752 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4753 | } |
| 4754 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4755 | /// See AbstractAttribute::updateImpl(...). |
| 4756 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4757 | // Byval is only replacable if it is readonly otherwise we would write into |
| 4758 | // the replaced value and not the copy that byval creates implicitly. |
| 4759 | Argument *Arg = getAssociatedArgument(); |
| 4760 | if (Arg->hasByValAttr()) { |
| Johannes Doerfert | 8e76fec | 2020-02-16 17:37:50 -0600 | [diff] [blame] | 4761 | // TODO: We probably need to verify synchronization is not an issue, e.g., |
| 4762 | // there is no race by not copying a constant byval. |
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4763 | const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); |
| 4764 | if (!MemAA.isAssumedReadOnly()) |
| 4765 | return indicatePessimisticFixpoint(); |
| 4766 | } |
| 4767 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4768 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 4769 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4770 | auto PredForCallSite = [&](AbstractCallSite ACS) { |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 4771 | const IRPosition &ACSArgPos = |
| 4772 | IRPosition::callsite_argument(ACS, getArgNo()); |
| 4773 | // Check if a coresponding argument was found or if it is on not |
| 4774 | // associated (which can happen for callback calls). |
| 4775 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4776 | return false; |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 4777 | |
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4778 | // We can only propagate thread independent values through callbacks. |
| 4779 | // This is different to direct/indirect call sites because for them we |
| 4780 | // know the thread executing the caller and callee is the same. For |
| 4781 | // callbacks this is not guaranteed, thus a thread dependent value could |
| 4782 | // be different for the caller and callee, making it invalid to propagate. |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 4783 | Value &ArgOp = ACSArgPos.getAssociatedValue(); |
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4784 | if (ACS.isCallbackCall()) |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 4785 | if (auto *C = dyn_cast<Constant>(&ArgOp)) |
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4786 | if (C->isThreadDependent()) |
| 4787 | return false; |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 4788 | return checkAndUpdate(A, *this, ArgOp, SimplifiedAssociatedValue); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4789 | }; |
| 4790 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 4791 | bool AllCallSitesKnown; |
| 4792 | if (!A.checkForAllCallSites(PredForCallSite, *this, true, |
| 4793 | AllCallSitesKnown)) |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4794 | if (!askSimplifiedValueForAAValueConstantRange(A)) |
| 4795 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4796 | |
| 4797 | // If a candicate was found in this update, return CHANGED. |
| 4798 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 4799 | ? ChangeStatus::UNCHANGED |
| 4800 | : ChangeStatus ::CHANGED; |
| 4801 | } |
| 4802 | |
| 4803 | /// See AbstractAttribute::trackStatistics() |
| 4804 | void trackStatistics() const override { |
| 4805 | STATS_DECLTRACK_ARG_ATTR(value_simplify) |
| 4806 | } |
| 4807 | }; |
| 4808 | |
| 4809 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { |
| 4810 | AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4811 | |
| 4812 | /// See AbstractAttribute::updateImpl(...). |
| 4813 | ChangeStatus updateImpl(Attributor &A) override { |
| 4814 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 4815 | |
| 4816 | auto PredForReturned = [&](Value &V) { |
| 4817 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); |
| 4818 | }; |
| 4819 | |
| 4820 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4821 | if (!askSimplifiedValueForAAValueConstantRange(A)) |
| 4822 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4823 | |
| 4824 | // If a candicate was found in this update, return CHANGED. |
| 4825 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 4826 | ? ChangeStatus::UNCHANGED |
| 4827 | : ChangeStatus ::CHANGED; |
| 4828 | } |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4829 | |
| 4830 | ChangeStatus manifest(Attributor &A) override { |
| 4831 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 4832 | |
| 4833 | if (SimplifiedAssociatedValue.hasValue() && |
| 4834 | !SimplifiedAssociatedValue.getValue()) |
| 4835 | return Changed; |
| 4836 | |
| 4837 | Value &V = getAssociatedValue(); |
| 4838 | auto *C = SimplifiedAssociatedValue.hasValue() |
| 4839 | ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue()) |
| 4840 | : UndefValue::get(V.getType()); |
| 4841 | if (C) { |
| 4842 | auto PredForReturned = |
| 4843 | [&](Value &V, const SmallSetVector<ReturnInst *, 4> &RetInsts) { |
| 4844 | // We can replace the AssociatedValue with the constant. |
| 4845 | if (&V == C || V.getType() != C->getType() || isa<UndefValue>(V)) |
| 4846 | return true; |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4847 | |
| 4848 | for (ReturnInst *RI : RetInsts) { |
| 4849 | if (RI->getFunction() != getAnchorScope()) |
| 4850 | continue; |
| 4851 | LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C |
| 4852 | << " in " << *RI << " :: " << *this << "\n"); |
| 4853 | if (A.changeUseAfterManifest(RI->getOperandUse(0), *C)) |
| 4854 | Changed = ChangeStatus::CHANGED; |
| 4855 | } |
| 4856 | return true; |
| 4857 | }; |
| 4858 | A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this); |
| 4859 | } |
| 4860 | |
| 4861 | return Changed | AAValueSimplify::manifest(A); |
| 4862 | } |
| 4863 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4864 | /// See AbstractAttribute::trackStatistics() |
| 4865 | void trackStatistics() const override { |
| 4866 | STATS_DECLTRACK_FNRET_ATTR(value_simplify) |
| 4867 | } |
| 4868 | }; |
| 4869 | |
| 4870 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { |
| 4871 | AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4872 | |
| 4873 | /// See AbstractAttribute::initialize(...). |
| 4874 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 9708279 | 2020-02-21 14:18:30 -0800 | [diff] [blame] | 4875 | // FIXME: This might have exposed a SCC iterator update bug in the old PM. |
| 4876 | // Needs investigation. |
| 4877 | // AAValueSimplifyImpl::initialize(A); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4878 | Value &V = getAnchorValue(); |
| 4879 | |
| 4880 | // TODO: add other stuffs |
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4881 | if (isa<Constant>(V)) |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4882 | indicatePessimisticFixpoint(); |
| 4883 | } |
| 4884 | |
| 4885 | /// See AbstractAttribute::updateImpl(...). |
| 4886 | ChangeStatus updateImpl(Attributor &A) override { |
| 4887 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 4888 | |
| Johannes Doerfert | 76843ba | 2020-01-28 23:51:25 -0600 | [diff] [blame] | 4889 | auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4890 | auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); |
| 4891 | if (!Stripped && this == &AA) { |
| 4892 | // TODO: Look the instruction and check recursively. |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4893 | |
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 4894 | LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V |
| 4895 | << "\n"); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4896 | return false; |
| 4897 | } |
| 4898 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); |
| 4899 | }; |
| 4900 | |
| Johannes Doerfert | 76843ba | 2020-01-28 23:51:25 -0600 | [diff] [blame] | 4901 | bool Dummy = false; |
| Johannes Doerfert | 6626d1b | 2020-01-28 11:49:59 -0600 | [diff] [blame] | 4902 | if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this, |
| 4903 | Dummy, VisitValueCB)) |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4904 | if (!askSimplifiedValueForAAValueConstantRange(A)) |
| 4905 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4906 | |
| 4907 | // If a candicate was found in this update, return CHANGED. |
| 4908 | |
| 4909 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 4910 | ? ChangeStatus::UNCHANGED |
| 4911 | : ChangeStatus ::CHANGED; |
| 4912 | } |
| 4913 | |
| 4914 | /// See AbstractAttribute::trackStatistics() |
| 4915 | void trackStatistics() const override { |
| 4916 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify) |
| 4917 | } |
| 4918 | }; |
| 4919 | |
| 4920 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { |
| 4921 | AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4922 | |
| 4923 | /// See AbstractAttribute::initialize(...). |
| 4924 | void initialize(Attributor &A) override { |
| 4925 | SimplifiedAssociatedValue = &getAnchorValue(); |
| 4926 | indicateOptimisticFixpoint(); |
| 4927 | } |
| 4928 | /// See AbstractAttribute::initialize(...). |
| 4929 | ChangeStatus updateImpl(Attributor &A) override { |
| 4930 | llvm_unreachable( |
| 4931 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); |
| 4932 | } |
| 4933 | /// See AbstractAttribute::trackStatistics() |
| 4934 | void trackStatistics() const override { |
| 4935 | STATS_DECLTRACK_FN_ATTR(value_simplify) |
| 4936 | } |
| 4937 | }; |
| 4938 | |
| 4939 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { |
| 4940 | AAValueSimplifyCallSite(const IRPosition &IRP) |
| 4941 | : AAValueSimplifyFunction(IRP) {} |
| 4942 | /// See AbstractAttribute::trackStatistics() |
| 4943 | void trackStatistics() const override { |
| 4944 | STATS_DECLTRACK_CS_ATTR(value_simplify) |
| 4945 | } |
| 4946 | }; |
| 4947 | |
| 4948 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { |
| 4949 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP) |
| 4950 | : AAValueSimplifyReturned(IRP) {} |
| 4951 | |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 4952 | /// See AbstractAttribute::manifest(...). |
| 4953 | ChangeStatus manifest(Attributor &A) override { |
| Johannes Doerfert | ad121ea | 2020-02-14 20:11:22 -0600 | [diff] [blame] | 4954 | return AAValueSimplifyImpl::manifest(A); |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 4955 | } |
| 4956 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4957 | void trackStatistics() const override { |
| 4958 | STATS_DECLTRACK_CSRET_ATTR(value_simplify) |
| 4959 | } |
| 4960 | }; |
| 4961 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { |
| 4962 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP) |
| 4963 | : AAValueSimplifyFloating(IRP) {} |
| 4964 | |
| 4965 | void trackStatistics() const override { |
| 4966 | STATS_DECLTRACK_CSARG_ATTR(value_simplify) |
| 4967 | } |
| 4968 | }; |
| 4969 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4970 | /// ----------------------- Heap-To-Stack Conversion --------------------------- |
| 4971 | struct AAHeapToStackImpl : public AAHeapToStack { |
| 4972 | AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} |
| 4973 | |
| 4974 | const std::string getAsStr() const override { |
| 4975 | return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); |
| 4976 | } |
| 4977 | |
| 4978 | ChangeStatus manifest(Attributor &A) override { |
| 4979 | assert(getState().isValidState() && |
| 4980 | "Attempted to manifest an invalid state!"); |
| 4981 | |
| 4982 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| 4983 | Function *F = getAssociatedFunction(); |
| 4984 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); |
| 4985 | |
| 4986 | for (Instruction *MallocCall : MallocCalls) { |
| 4987 | // This malloc cannot be replaced. |
| 4988 | if (BadMallocCalls.count(MallocCall)) |
| 4989 | continue; |
| 4990 | |
| 4991 | for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { |
| 4992 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); |
| 4993 | A.deleteAfterManifest(*FreeCall); |
| 4994 | HasChanged = ChangeStatus::CHANGED; |
| 4995 | } |
| 4996 | |
| 4997 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall |
| 4998 | << "\n"); |
| 4999 | |
| 5000 | Constant *Size; |
| 5001 | if (isCallocLikeFn(MallocCall, TLI)) { |
| 5002 | auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); |
| 5003 | auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); |
| 5004 | APInt TotalSize = SizeT->getValue() * Num->getValue(); |
| 5005 | Size = |
| 5006 | ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); |
| 5007 | } else { |
| 5008 | Size = cast<ConstantInt>(MallocCall->getOperand(0)); |
| 5009 | } |
| 5010 | |
| 5011 | unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); |
| 5012 | Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, |
| 5013 | Size, "", MallocCall->getNextNode()); |
| 5014 | |
| 5015 | if (AI->getType() != MallocCall->getType()) |
| 5016 | AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", |
| 5017 | AI->getNextNode()); |
| 5018 | |
| Johannes Doerfert | 4c62a35 | 2020-01-12 00:17:08 -0600 | [diff] [blame] | 5019 | A.changeValueAfterManifest(*MallocCall, *AI); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5020 | |
| 5021 | if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { |
| 5022 | auto *NBB = II->getNormalDest(); |
| 5023 | BranchInst::Create(NBB, MallocCall->getParent()); |
| 5024 | A.deleteAfterManifest(*MallocCall); |
| 5025 | } else { |
| 5026 | A.deleteAfterManifest(*MallocCall); |
| 5027 | } |
| 5028 | |
| 5029 | if (isCallocLikeFn(MallocCall, TLI)) { |
| 5030 | auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", |
| 5031 | AI->getNextNode()); |
| 5032 | Value *Ops[] = { |
| 5033 | BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, |
| 5034 | ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; |
| 5035 | |
| 5036 | Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; |
| 5037 | Module *M = F->getParent(); |
| 5038 | Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
| 5039 | CallInst::Create(Fn, Ops, "", BI->getNextNode()); |
| 5040 | } |
| 5041 | HasChanged = ChangeStatus::CHANGED; |
| 5042 | } |
| 5043 | |
| 5044 | return HasChanged; |
| 5045 | } |
| 5046 | |
| 5047 | /// Collection of all malloc calls in a function. |
| 5048 | SmallSetVector<Instruction *, 4> MallocCalls; |
| 5049 | |
| 5050 | /// Collection of malloc calls that cannot be converted. |
| 5051 | DenseSet<const Instruction *> BadMallocCalls; |
| 5052 | |
| 5053 | /// A map for each malloc call to the set of associated free calls. |
| 5054 | DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; |
| 5055 | |
| 5056 | ChangeStatus updateImpl(Attributor &A) override; |
| 5057 | }; |
| 5058 | |
| 5059 | ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { |
| 5060 | const Function *F = getAssociatedFunction(); |
| 5061 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); |
| 5062 | |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5063 | MustBeExecutedContextExplorer &Explorer = |
| 5064 | A.getInfoCache().getMustBeExecutedContextExplorer(); |
| 5065 | |
| 5066 | auto FreeCheck = [&](Instruction &I) { |
| 5067 | const auto &Frees = FreesForMalloc.lookup(&I); |
| 5068 | if (Frees.size() != 1) |
| 5069 | return false; |
| 5070 | Instruction *UniqueFree = *Frees.begin(); |
| 5071 | return Explorer.findInContextOf(UniqueFree, I.getNextNode()); |
| 5072 | }; |
| 5073 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5074 | auto UsesCheck = [&](Instruction &I) { |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5075 | bool ValidUsesOnly = true; |
| 5076 | bool MustUse = true; |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5077 | auto Pred = [&](const Use &U, bool &Follow) -> bool { |
| 5078 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 5079 | if (isa<LoadInst>(UserI)) |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5080 | return true; |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 5081 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5082 | if (SI->getValueOperand() == U.get()) { |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5083 | LLVM_DEBUG(dbgs() |
| 5084 | << "[H2S] escaping store to memory: " << *UserI << "\n"); |
| 5085 | ValidUsesOnly = false; |
| 5086 | } else { |
| 5087 | // A store into the malloc'ed memory is fine. |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 5088 | } |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5089 | return true; |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 5090 | } |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5091 | if (auto *CB = dyn_cast<CallBase>(UserI)) { |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5092 | if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) |
| 5093 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5094 | // Record malloc. |
| 5095 | if (isFreeCall(UserI, TLI)) { |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5096 | if (MustUse) { |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5097 | FreesForMalloc[&I].insert(UserI); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5098 | } else { |
| 5099 | LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " |
| 5100 | << *UserI << "\n"); |
| 5101 | ValidUsesOnly = false; |
| 5102 | } |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5103 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5106 | unsigned ArgNo = CB->getArgOperandNo(&U); |
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 5107 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5108 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( |
| 5109 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| 5110 | |
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 5111 | // If a callsite argument use is nofree, we are fine. |
| 5112 | const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( |
| 5113 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| 5114 | |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5115 | if (!NoCaptureAA.isAssumedNoCapture() || |
| 5116 | !ArgNoFreeAA.isAssumedNoFree()) { |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5117 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5118 | ValidUsesOnly = false; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5119 | } |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5120 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5123 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || |
| 5124 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { |
| 5125 | MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5126 | Follow = true; |
| 5127 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5128 | } |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5129 | // Unknown user for which we can not track uses further (in a way that |
| 5130 | // makes sense). |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5131 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5132 | ValidUsesOnly = false; |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 5133 | return true; |
| 5134 | }; |
| 5135 | A.checkForAllUses(Pred, *this, I); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5136 | return ValidUsesOnly; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5137 | }; |
| 5138 | |
| 5139 | auto MallocCallocCheck = [&](Instruction &I) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 5140 | if (BadMallocCalls.count(&I)) |
| 5141 | return true; |
| 5142 | |
| 5143 | bool IsMalloc = isMallocLikeFn(&I, TLI); |
| 5144 | bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); |
| 5145 | if (!IsMalloc && !IsCalloc) { |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5146 | BadMallocCalls.insert(&I); |
| 5147 | return true; |
| 5148 | } |
| 5149 | |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 5150 | if (IsMalloc) { |
| 5151 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) |
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 5152 | if (Size->getValue().ule(MaxHeapToStackSize)) |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5153 | if (UsesCheck(I) || FreeCheck(I)) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 5154 | MallocCalls.insert(&I); |
| 5155 | return true; |
| 5156 | } |
| 5157 | } else if (IsCalloc) { |
| 5158 | bool Overflow = false; |
| 5159 | if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 5160 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) |
| 5161 | if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) |
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 5162 | .ule(MaxHeapToStackSize)) |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5163 | if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 5164 | MallocCalls.insert(&I); |
| 5165 | return true; |
| 5166 | } |
| 5167 | } |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5168 | |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 5169 | BadMallocCalls.insert(&I); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5170 | return true; |
| 5171 | }; |
| 5172 | |
| 5173 | size_t NumBadMallocs = BadMallocCalls.size(); |
| 5174 | |
| 5175 | A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); |
| 5176 | |
| 5177 | if (NumBadMallocs != BadMallocCalls.size()) |
| 5178 | return ChangeStatus::CHANGED; |
| 5179 | |
| 5180 | return ChangeStatus::UNCHANGED; |
| 5181 | } |
| 5182 | |
| 5183 | struct AAHeapToStackFunction final : public AAHeapToStackImpl { |
| 5184 | AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} |
| 5185 | |
| 5186 | /// See AbstractAttribute::trackStatistics() |
| 5187 | void trackStatistics() const override { |
| 5188 | STATS_DECL(MallocCalls, Function, |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 5189 | "Number of malloc calls converted to allocas"); |
| 5190 | for (auto *C : MallocCalls) |
| 5191 | if (!BadMallocCalls.count(C)) |
| 5192 | ++BUILD_STAT_NAME(MallocCalls, Function); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5193 | } |
| 5194 | }; |
| 5195 | |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5196 | /// ----------------------- Privatizable Pointers ------------------------------ |
| 5197 | struct AAPrivatizablePtrImpl : public AAPrivatizablePtr { |
| 5198 | AAPrivatizablePtrImpl(const IRPosition &IRP) |
| 5199 | : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {} |
| 5200 | |
| 5201 | ChangeStatus indicatePessimisticFixpoint() override { |
| 5202 | AAPrivatizablePtr::indicatePessimisticFixpoint(); |
| 5203 | PrivatizableType = nullptr; |
| 5204 | return ChangeStatus::CHANGED; |
| 5205 | } |
| 5206 | |
| 5207 | /// Identify the type we can chose for a private copy of the underlying |
| 5208 | /// argument. None means it is not clear yet, nullptr means there is none. |
| 5209 | virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0; |
| 5210 | |
| 5211 | /// Return a privatizable type that encloses both T0 and T1. |
| 5212 | /// TODO: This is merely a stub for now as we should manage a mapping as well. |
| 5213 | Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) { |
| 5214 | if (!T0.hasValue()) |
| 5215 | return T1; |
| 5216 | if (!T1.hasValue()) |
| 5217 | return T0; |
| 5218 | if (T0 == T1) |
| 5219 | return T0; |
| 5220 | return nullptr; |
| 5221 | } |
| 5222 | |
| 5223 | Optional<Type *> getPrivatizableType() const override { |
| 5224 | return PrivatizableType; |
| 5225 | } |
| 5226 | |
| 5227 | const std::string getAsStr() const override { |
| 5228 | return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]"; |
| 5229 | } |
| 5230 | |
| 5231 | protected: |
| 5232 | Optional<Type *> PrivatizableType; |
| 5233 | }; |
| 5234 | |
| 5235 | // TODO: Do this for call site arguments (probably also other values) as well. |
| 5236 | |
| 5237 | struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl { |
| 5238 | AAPrivatizablePtrArgument(const IRPosition &IRP) |
| 5239 | : AAPrivatizablePtrImpl(IRP) {} |
| 5240 | |
| 5241 | /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) |
| 5242 | Optional<Type *> identifyPrivatizableType(Attributor &A) override { |
| 5243 | // If this is a byval argument and we know all the call sites (so we can |
| 5244 | // rewrite them), there is no need to check them explicitly. |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 5245 | bool AllCallSitesKnown; |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5246 | if (getIRPosition().hasAttr(Attribute::ByVal) && |
| 5247 | A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this, |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 5248 | true, AllCallSitesKnown)) |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5249 | return getAssociatedValue().getType()->getPointerElementType(); |
| 5250 | |
| 5251 | Optional<Type *> Ty; |
| 5252 | unsigned ArgNo = getIRPosition().getArgNo(); |
| 5253 | |
| 5254 | // Make sure the associated call site argument has the same type at all call |
| 5255 | // sites and it is an allocation we know is safe to privatize, for now that |
| 5256 | // means we only allow alloca instructions. |
| 5257 | // TODO: We can additionally analyze the accesses in the callee to create |
| 5258 | // the type from that information instead. That is a little more |
| 5259 | // involved and will be done in a follow up patch. |
| 5260 | auto CallSiteCheck = [&](AbstractCallSite ACS) { |
| 5261 | IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); |
| 5262 | // Check if a coresponding argument was found or if it is one not |
| 5263 | // associated (which can happen for callback calls). |
| 5264 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) |
| 5265 | return false; |
| 5266 | |
| 5267 | // Check that all call sites agree on a type. |
| 5268 | auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos); |
| 5269 | Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType(); |
| 5270 | |
| 5271 | LLVM_DEBUG({ |
| 5272 | dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; |
| 5273 | if (CSTy.hasValue() && CSTy.getValue()) |
| 5274 | CSTy.getValue()->print(dbgs()); |
| 5275 | else if (CSTy.hasValue()) |
| 5276 | dbgs() << "<nullptr>"; |
| 5277 | else |
| 5278 | dbgs() << "<none>"; |
| 5279 | }); |
| 5280 | |
| 5281 | Ty = combineTypes(Ty, CSTy); |
| 5282 | |
| 5283 | LLVM_DEBUG({ |
| 5284 | dbgs() << " : New Type: "; |
| 5285 | if (Ty.hasValue() && Ty.getValue()) |
| 5286 | Ty.getValue()->print(dbgs()); |
| 5287 | else if (Ty.hasValue()) |
| 5288 | dbgs() << "<nullptr>"; |
| 5289 | else |
| 5290 | dbgs() << "<none>"; |
| 5291 | dbgs() << "\n"; |
| 5292 | }); |
| 5293 | |
| 5294 | return !Ty.hasValue() || Ty.getValue(); |
| 5295 | }; |
| 5296 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 5297 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown)) |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5298 | return nullptr; |
| 5299 | return Ty; |
| 5300 | } |
| 5301 | |
| 5302 | /// See AbstractAttribute::updateImpl(...). |
| 5303 | ChangeStatus updateImpl(Attributor &A) override { |
| 5304 | PrivatizableType = identifyPrivatizableType(A); |
| 5305 | if (!PrivatizableType.hasValue()) |
| 5306 | return ChangeStatus::UNCHANGED; |
| 5307 | if (!PrivatizableType.getValue()) |
| 5308 | return indicatePessimisticFixpoint(); |
| 5309 | |
| 5310 | // Avoid arguments with padding for now. |
| 5311 | if (!getIRPosition().hasAttr(Attribute::ByVal) && |
| 5312 | !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(), |
| 5313 | A.getInfoCache().getDL())) { |
| 5314 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n"); |
| 5315 | return indicatePessimisticFixpoint(); |
| 5316 | } |
| 5317 | |
| 5318 | // Verify callee and caller agree on how the promoted argument would be |
| 5319 | // passed. |
| 5320 | // TODO: The use of the ArgumentPromotion interface here is ugly, we need a |
| 5321 | // specialized form of TargetTransformInfo::areFunctionArgsABICompatible |
| 5322 | // which doesn't require the arguments ArgumentPromotion wanted to pass. |
| 5323 | Function &Fn = *getIRPosition().getAnchorScope(); |
| 5324 | SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy; |
| 5325 | ArgsToPromote.insert(getAssociatedArgument()); |
| 5326 | const auto *TTI = |
| 5327 | A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn); |
| 5328 | if (!TTI || |
| 5329 | !ArgumentPromotionPass::areFunctionArgsABICompatible( |
| 5330 | Fn, *TTI, ArgsToPromote, Dummy) || |
| 5331 | ArgsToPromote.empty()) { |
| 5332 | LLVM_DEBUG( |
| 5333 | dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " |
| 5334 | << Fn.getName() << "\n"); |
| 5335 | return indicatePessimisticFixpoint(); |
| 5336 | } |
| 5337 | |
| 5338 | // Collect the types that will replace the privatizable type in the function |
| 5339 | // signature. |
| 5340 | SmallVector<Type *, 16> ReplacementTypes; |
| 5341 | identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); |
| 5342 | |
| 5343 | // Register a rewrite of the argument. |
| 5344 | Argument *Arg = getAssociatedArgument(); |
| 5345 | if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) { |
| 5346 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n"); |
| 5347 | return indicatePessimisticFixpoint(); |
| 5348 | } |
| 5349 | |
| 5350 | unsigned ArgNo = Arg->getArgNo(); |
| 5351 | |
| 5352 | // Helper to check if for the given call site the associated argument is |
| 5353 | // passed to a callback where the privatization would be different. |
| 5354 | auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) { |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5355 | SmallVector<const Use *, 4> CBUses; |
| 5356 | AbstractCallSite::getCallbackUses(CS, CBUses); |
| 5357 | for (const Use *U : CBUses) { |
| 5358 | AbstractCallSite CBACS(U); |
| 5359 | assert(CBACS && CBACS.isCallbackCall()); |
| 5360 | for (Argument &CBArg : CBACS.getCalledFunction()->args()) { |
| 5361 | int CBArgNo = CBACS.getCallArgOperandNo(CBArg); |
| 5362 | |
| 5363 | LLVM_DEBUG({ |
| 5364 | dbgs() |
| 5365 | << "[AAPrivatizablePtr] Argument " << *Arg |
| 5366 | << "check if can be privatized in the context of its parent (" |
| 5367 | << Arg->getParent()->getName() |
| 5368 | << ")\n[AAPrivatizablePtr] because it is an argument in a " |
| 5369 | "callback (" |
| 5370 | << CBArgNo << "@" << CBACS.getCalledFunction()->getName() |
| 5371 | << ")\n[AAPrivatizablePtr] " << CBArg << " : " |
| Michael Forster | 676c296 | 2020-01-30 10:20:49 +0100 | [diff] [blame] | 5372 | << CBACS.getCallArgOperand(CBArg) << " vs " |
| 5373 | << CS.getArgOperand(ArgNo) << "\n" |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5374 | << "[AAPrivatizablePtr] " << CBArg << " : " |
| 5375 | << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; |
| 5376 | }); |
| 5377 | |
| 5378 | if (CBArgNo != int(ArgNo)) |
| 5379 | continue; |
| 5380 | const auto &CBArgPrivAA = |
| 5381 | A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg)); |
| 5382 | if (CBArgPrivAA.isValidState()) { |
| 5383 | auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType(); |
| 5384 | if (!CBArgPrivTy.hasValue()) |
| 5385 | continue; |
| 5386 | if (CBArgPrivTy.getValue() == PrivatizableType) |
| 5387 | continue; |
| 5388 | } |
| 5389 | |
| 5390 | LLVM_DEBUG({ |
| 5391 | dbgs() << "[AAPrivatizablePtr] Argument " << *Arg |
| 5392 | << " cannot be privatized in the context of its parent (" |
| 5393 | << Arg->getParent()->getName() |
| 5394 | << ")\n[AAPrivatizablePtr] because it is an argument in a " |
| 5395 | "callback (" |
| 5396 | << CBArgNo << "@" << CBACS.getCalledFunction()->getName() |
| 5397 | << ").\n[AAPrivatizablePtr] for which the argument " |
| 5398 | "privatization is not compatible.\n"; |
| 5399 | }); |
| 5400 | return false; |
| 5401 | } |
| 5402 | } |
| 5403 | return true; |
| 5404 | }; |
| 5405 | |
| 5406 | // Helper to check if for the given call site the associated argument is |
| 5407 | // passed to a direct call where the privatization would be different. |
| 5408 | auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) { |
| 5409 | CallBase *DC = cast<CallBase>(ACS.getInstruction()); |
| 5410 | int DCArgNo = ACS.getCallArgOperandNo(ArgNo); |
| 5411 | assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() && |
| 5412 | "Expected a direct call operand for callback call operand"); |
| 5413 | |
| 5414 | LLVM_DEBUG({ |
| 5415 | dbgs() << "[AAPrivatizablePtr] Argument " << *Arg |
| 5416 | << " check if be privatized in the context of its parent (" |
| 5417 | << Arg->getParent()->getName() |
| 5418 | << ")\n[AAPrivatizablePtr] because it is an argument in a " |
| 5419 | "direct call of (" |
| 5420 | << DCArgNo << "@" << DC->getCalledFunction()->getName() |
| 5421 | << ").\n"; |
| 5422 | }); |
| 5423 | |
| 5424 | Function *DCCallee = DC->getCalledFunction(); |
| 5425 | if (unsigned(DCArgNo) < DCCallee->arg_size()) { |
| 5426 | const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>( |
| 5427 | *this, IRPosition::argument(*DCCallee->getArg(DCArgNo))); |
| 5428 | if (DCArgPrivAA.isValidState()) { |
| 5429 | auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType(); |
| 5430 | if (!DCArgPrivTy.hasValue()) |
| 5431 | return true; |
| 5432 | if (DCArgPrivTy.getValue() == PrivatizableType) |
| 5433 | return true; |
| 5434 | } |
| 5435 | } |
| 5436 | |
| 5437 | LLVM_DEBUG({ |
| 5438 | dbgs() << "[AAPrivatizablePtr] Argument " << *Arg |
| 5439 | << " cannot be privatized in the context of its parent (" |
| 5440 | << Arg->getParent()->getName() |
| 5441 | << ")\n[AAPrivatizablePtr] because it is an argument in a " |
| 5442 | "direct call of (" |
| 5443 | << ACS.getCallSite().getCalledFunction()->getName() |
| 5444 | << ").\n[AAPrivatizablePtr] for which the argument " |
| 5445 | "privatization is not compatible.\n"; |
| 5446 | }); |
| 5447 | return false; |
| 5448 | }; |
| 5449 | |
| 5450 | // Helper to check if the associated argument is used at the given abstract |
| 5451 | // call site in a way that is incompatible with the privatization assumed |
| 5452 | // here. |
| 5453 | auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) { |
| 5454 | if (ACS.isDirectCall()) |
| 5455 | return IsCompatiblePrivArgOfCallback(ACS.getCallSite()); |
| 5456 | if (ACS.isCallbackCall()) |
| 5457 | return IsCompatiblePrivArgOfDirectCS(ACS); |
| 5458 | return false; |
| 5459 | }; |
| 5460 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 5461 | bool AllCallSitesKnown; |
| 5462 | if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true, |
| 5463 | AllCallSitesKnown)) |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5464 | return indicatePessimisticFixpoint(); |
| 5465 | |
| 5466 | return ChangeStatus::UNCHANGED; |
| 5467 | } |
| 5468 | |
| 5469 | /// Given a type to private \p PrivType, collect the constituates (which are |
| 5470 | /// used) in \p ReplacementTypes. |
| 5471 | static void |
| 5472 | identifyReplacementTypes(Type *PrivType, |
| 5473 | SmallVectorImpl<Type *> &ReplacementTypes) { |
| 5474 | // TODO: For now we expand the privatization type to the fullest which can |
| 5475 | // lead to dead arguments that need to be removed later. |
| 5476 | assert(PrivType && "Expected privatizable type!"); |
| 5477 | |
| 5478 | // Traverse the type, extract constituate types on the outermost level. |
| 5479 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { |
| 5480 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) |
| 5481 | ReplacementTypes.push_back(PrivStructType->getElementType(u)); |
| 5482 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { |
| 5483 | ReplacementTypes.append(PrivArrayType->getNumElements(), |
| 5484 | PrivArrayType->getElementType()); |
| 5485 | } else { |
| 5486 | ReplacementTypes.push_back(PrivType); |
| 5487 | } |
| 5488 | } |
| 5489 | |
| 5490 | /// Initialize \p Base according to the type \p PrivType at position \p IP. |
| 5491 | /// The values needed are taken from the arguments of \p F starting at |
| 5492 | /// position \p ArgNo. |
| 5493 | static void createInitialization(Type *PrivType, Value &Base, Function &F, |
| 5494 | unsigned ArgNo, Instruction &IP) { |
| 5495 | assert(PrivType && "Expected privatizable type!"); |
| 5496 | |
| 5497 | IRBuilder<NoFolder> IRB(&IP); |
| 5498 | const DataLayout &DL = F.getParent()->getDataLayout(); |
| 5499 | |
| 5500 | // Traverse the type, build GEPs and stores. |
| 5501 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { |
| 5502 | const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); |
| 5503 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { |
| 5504 | Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo(); |
| 5505 | Value *Ptr = constructPointer( |
| 5506 | PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL); |
| 5507 | new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); |
| 5508 | } |
| 5509 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { |
| 5510 | Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo(); |
| 5511 | uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy); |
| 5512 | for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { |
| 5513 | Value *Ptr = |
| 5514 | constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL); |
| 5515 | new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); |
| 5516 | } |
| 5517 | } else { |
| 5518 | new StoreInst(F.getArg(ArgNo), &Base, &IP); |
| 5519 | } |
| 5520 | } |
| 5521 | |
| 5522 | /// Extract values from \p Base according to the type \p PrivType at the |
| 5523 | /// call position \p ACS. The values are appended to \p ReplacementValues. |
| 5524 | void createReplacementValues(Type *PrivType, AbstractCallSite ACS, |
| 5525 | Value *Base, |
| 5526 | SmallVectorImpl<Value *> &ReplacementValues) { |
| 5527 | assert(Base && "Expected base value!"); |
| 5528 | assert(PrivType && "Expected privatizable type!"); |
| 5529 | Instruction *IP = ACS.getInstruction(); |
| 5530 | |
| 5531 | IRBuilder<NoFolder> IRB(IP); |
| 5532 | const DataLayout &DL = IP->getModule()->getDataLayout(); |
| 5533 | |
| 5534 | if (Base->getType()->getPointerElementType() != PrivType) |
| 5535 | Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(), |
| 5536 | "", ACS.getInstruction()); |
| 5537 | |
| 5538 | // TODO: Improve the alignment of the loads. |
| 5539 | // Traverse the type, build GEPs and loads. |
| 5540 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { |
| 5541 | const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); |
| 5542 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { |
| 5543 | Type *PointeeTy = PrivStructType->getElementType(u); |
| 5544 | Value *Ptr = |
| 5545 | constructPointer(PointeeTy->getPointerTo(), Base, |
| 5546 | PrivStructLayout->getElementOffset(u), IRB, DL); |
| 5547 | LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); |
| 5548 | L->setAlignment(MaybeAlign(1)); |
| 5549 | ReplacementValues.push_back(L); |
| 5550 | } |
| 5551 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { |
| 5552 | Type *PointeeTy = PrivArrayType->getElementType(); |
| 5553 | uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); |
| 5554 | Type *PointeePtrTy = PointeeTy->getPointerTo(); |
| 5555 | for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { |
| 5556 | Value *Ptr = |
| 5557 | constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL); |
| 5558 | LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP); |
| 5559 | L->setAlignment(MaybeAlign(1)); |
| 5560 | ReplacementValues.push_back(L); |
| 5561 | } |
| 5562 | } else { |
| 5563 | LoadInst *L = new LoadInst(PrivType, Base, "", IP); |
| 5564 | L->setAlignment(MaybeAlign(1)); |
| 5565 | ReplacementValues.push_back(L); |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | /// See AbstractAttribute::manifest(...) |
| 5570 | ChangeStatus manifest(Attributor &A) override { |
| 5571 | if (!PrivatizableType.hasValue()) |
| 5572 | return ChangeStatus::UNCHANGED; |
| 5573 | assert(PrivatizableType.getValue() && "Expected privatizable type!"); |
| 5574 | |
| 5575 | // Collect all tail calls in the function as we cannot allow new allocas to |
| 5576 | // escape into tail recursion. |
| 5577 | // TODO: Be smarter about new allocas escaping into tail calls. |
| 5578 | SmallVector<CallInst *, 16> TailCalls; |
| 5579 | if (!A.checkForAllInstructions( |
| 5580 | [&](Instruction &I) { |
| 5581 | CallInst &CI = cast<CallInst>(I); |
| 5582 | if (CI.isTailCall()) |
| 5583 | TailCalls.push_back(&CI); |
| 5584 | return true; |
| 5585 | }, |
| 5586 | *this, {Instruction::Call})) |
| 5587 | return ChangeStatus::UNCHANGED; |
| 5588 | |
| 5589 | Argument *Arg = getAssociatedArgument(); |
| 5590 | |
| 5591 | // Callback to repair the associated function. A new alloca is placed at the |
| 5592 | // beginning and initialized with the values passed through arguments. The |
| 5593 | // new alloca replaces the use of the old pointer argument. |
| 5594 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB = |
| 5595 | [=](const Attributor::ArgumentReplacementInfo &ARI, |
| 5596 | Function &ReplacementFn, Function::arg_iterator ArgIt) { |
| 5597 | BasicBlock &EntryBB = ReplacementFn.getEntryBlock(); |
| 5598 | Instruction *IP = &*EntryBB.getFirstInsertionPt(); |
| 5599 | auto *AI = new AllocaInst(PrivatizableType.getValue(), 0, |
| 5600 | Arg->getName() + ".priv", IP); |
| 5601 | createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn, |
| 5602 | ArgIt->getArgNo(), *IP); |
| 5603 | Arg->replaceAllUsesWith(AI); |
| 5604 | |
| 5605 | for (CallInst *CI : TailCalls) |
| 5606 | CI->setTailCall(false); |
| 5607 | }; |
| 5608 | |
| 5609 | // Callback to repair a call site of the associated function. The elements |
| 5610 | // of the privatizable type are loaded prior to the call and passed to the |
| 5611 | // new function version. |
| 5612 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB = |
| 5613 | [=](const Attributor::ArgumentReplacementInfo &ARI, |
| 5614 | AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) { |
| 5615 | createReplacementValues( |
| 5616 | PrivatizableType.getValue(), ACS, |
| 5617 | ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()), |
| Johannes Doerfert | f3bf4b0 | 2020-03-23 02:03:23 -0500 | [diff] [blame] | 5618 | NewArgOperands); |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5619 | }; |
| 5620 | |
| 5621 | // Collect the types that will replace the privatizable type in the function |
| 5622 | // signature. |
| 5623 | SmallVector<Type *, 16> ReplacementTypes; |
| 5624 | identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); |
| 5625 | |
| 5626 | // Register a rewrite of the argument. |
| 5627 | if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes, |
| 5628 | std::move(FnRepairCB), |
| 5629 | std::move(ACSRepairCB))) |
| 5630 | return ChangeStatus::CHANGED; |
| 5631 | return ChangeStatus::UNCHANGED; |
| 5632 | } |
| 5633 | |
| 5634 | /// See AbstractAttribute::trackStatistics() |
| 5635 | void trackStatistics() const override { |
| 5636 | STATS_DECLTRACK_ARG_ATTR(privatizable_ptr); |
| 5637 | } |
| 5638 | }; |
| 5639 | |
| 5640 | struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl { |
| 5641 | AAPrivatizablePtrFloating(const IRPosition &IRP) |
| 5642 | : AAPrivatizablePtrImpl(IRP) {} |
| 5643 | |
| 5644 | /// See AbstractAttribute::initialize(...). |
| 5645 | virtual void initialize(Attributor &A) override { |
| 5646 | // TODO: We can privatize more than arguments. |
| 5647 | indicatePessimisticFixpoint(); |
| 5648 | } |
| 5649 | |
| 5650 | ChangeStatus updateImpl(Attributor &A) override { |
| 5651 | llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::" |
| 5652 | "updateImpl will not be called"); |
| 5653 | } |
| 5654 | |
| 5655 | /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) |
| 5656 | Optional<Type *> identifyPrivatizableType(Attributor &A) override { |
| 5657 | Value *Obj = |
| 5658 | GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL()); |
| 5659 | if (!Obj) { |
| 5660 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n"); |
| 5661 | return nullptr; |
| 5662 | } |
| 5663 | |
| 5664 | if (auto *AI = dyn_cast<AllocaInst>(Obj)) |
| 5665 | if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) |
| 5666 | if (CI->isOne()) |
| 5667 | return Obj->getType()->getPointerElementType(); |
| 5668 | if (auto *Arg = dyn_cast<Argument>(Obj)) { |
| 5669 | auto &PrivArgAA = |
| 5670 | A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg)); |
| 5671 | if (PrivArgAA.isAssumedPrivatizablePtr()) |
| 5672 | return Obj->getType()->getPointerElementType(); |
| 5673 | } |
| 5674 | |
| 5675 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " |
| 5676 | "alloca nor privatizable argument: " |
| 5677 | << *Obj << "!\n"); |
| 5678 | return nullptr; |
| 5679 | } |
| 5680 | |
| 5681 | /// See AbstractAttribute::trackStatistics() |
| 5682 | void trackStatistics() const override { |
| 5683 | STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr); |
| 5684 | } |
| 5685 | }; |
| 5686 | |
| 5687 | struct AAPrivatizablePtrCallSiteArgument final |
| 5688 | : public AAPrivatizablePtrFloating { |
| 5689 | AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP) |
| 5690 | : AAPrivatizablePtrFloating(IRP) {} |
| 5691 | |
| 5692 | /// See AbstractAttribute::initialize(...). |
| 5693 | void initialize(Attributor &A) override { |
| 5694 | if (getIRPosition().hasAttr(Attribute::ByVal)) |
| 5695 | indicateOptimisticFixpoint(); |
| 5696 | } |
| 5697 | |
| 5698 | /// See AbstractAttribute::updateImpl(...). |
| 5699 | ChangeStatus updateImpl(Attributor &A) override { |
| 5700 | PrivatizableType = identifyPrivatizableType(A); |
| 5701 | if (!PrivatizableType.hasValue()) |
| 5702 | return ChangeStatus::UNCHANGED; |
| 5703 | if (!PrivatizableType.getValue()) |
| 5704 | return indicatePessimisticFixpoint(); |
| 5705 | |
| 5706 | const IRPosition &IRP = getIRPosition(); |
| 5707 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); |
| 5708 | if (!NoCaptureAA.isAssumedNoCapture()) { |
| 5709 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n"); |
| 5710 | return indicatePessimisticFixpoint(); |
| 5711 | } |
| 5712 | |
| 5713 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); |
| 5714 | if (!NoAliasAA.isAssumedNoAlias()) { |
| 5715 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n"); |
| 5716 | return indicatePessimisticFixpoint(); |
| 5717 | } |
| 5718 | |
| 5719 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP); |
| 5720 | if (!MemBehaviorAA.isAssumedReadOnly()) { |
| 5721 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n"); |
| 5722 | return indicatePessimisticFixpoint(); |
| 5723 | } |
| 5724 | |
| 5725 | return ChangeStatus::UNCHANGED; |
| 5726 | } |
| 5727 | |
| 5728 | /// See AbstractAttribute::trackStatistics() |
| 5729 | void trackStatistics() const override { |
| 5730 | STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr); |
| 5731 | } |
| 5732 | }; |
| 5733 | |
| 5734 | struct AAPrivatizablePtrCallSiteReturned final |
| 5735 | : public AAPrivatizablePtrFloating { |
| 5736 | AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP) |
| 5737 | : AAPrivatizablePtrFloating(IRP) {} |
| 5738 | |
| 5739 | /// See AbstractAttribute::initialize(...). |
| 5740 | void initialize(Attributor &A) override { |
| 5741 | // TODO: We can privatize more than arguments. |
| 5742 | indicatePessimisticFixpoint(); |
| 5743 | } |
| 5744 | |
| 5745 | /// See AbstractAttribute::trackStatistics() |
| 5746 | void trackStatistics() const override { |
| 5747 | STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr); |
| 5748 | } |
| 5749 | }; |
| 5750 | |
| 5751 | struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating { |
| 5752 | AAPrivatizablePtrReturned(const IRPosition &IRP) |
| 5753 | : AAPrivatizablePtrFloating(IRP) {} |
| 5754 | |
| 5755 | /// See AbstractAttribute::initialize(...). |
| 5756 | void initialize(Attributor &A) override { |
| 5757 | // TODO: We can privatize more than arguments. |
| 5758 | indicatePessimisticFixpoint(); |
| 5759 | } |
| 5760 | |
| 5761 | /// See AbstractAttribute::trackStatistics() |
| 5762 | void trackStatistics() const override { |
| 5763 | STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr); |
| 5764 | } |
| 5765 | }; |
| 5766 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5767 | /// -------------------- Memory Behavior Attributes ---------------------------- |
| 5768 | /// Includes read-none, read-only, and write-only. |
| 5769 | /// ---------------------------------------------------------------------------- |
| 5770 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { |
| 5771 | AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} |
| 5772 | |
| 5773 | /// See AbstractAttribute::initialize(...). |
| 5774 | void initialize(Attributor &A) override { |
| 5775 | intersectAssumedBits(BEST_STATE); |
| 5776 | getKnownStateFromValue(getIRPosition(), getState()); |
| 5777 | IRAttribute::initialize(A); |
| 5778 | } |
| 5779 | |
| 5780 | /// Return the memory behavior information encoded in the IR for \p IRP. |
| 5781 | static void getKnownStateFromValue(const IRPosition &IRP, |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5782 | BitIntegerState &State, |
| 5783 | bool IgnoreSubsumingPositions = false) { |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5784 | SmallVector<Attribute, 2> Attrs; |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5785 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5786 | for (const Attribute &Attr : Attrs) { |
| 5787 | switch (Attr.getKindAsEnum()) { |
| 5788 | case Attribute::ReadNone: |
| 5789 | State.addKnownBits(NO_ACCESSES); |
| 5790 | break; |
| 5791 | case Attribute::ReadOnly: |
| 5792 | State.addKnownBits(NO_WRITES); |
| 5793 | break; |
| 5794 | case Attribute::WriteOnly: |
| 5795 | State.addKnownBits(NO_READS); |
| 5796 | break; |
| 5797 | default: |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 5798 | llvm_unreachable("Unexpected attribute!"); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { |
| 5803 | if (!I->mayReadFromMemory()) |
| 5804 | State.addKnownBits(NO_READS); |
| 5805 | if (!I->mayWriteToMemory()) |
| 5806 | State.addKnownBits(NO_WRITES); |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | /// See AbstractAttribute::getDeducedAttributes(...). |
| 5811 | void getDeducedAttributes(LLVMContext &Ctx, |
| 5812 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 5813 | assert(Attrs.size() == 0); |
| 5814 | if (isAssumedReadNone()) |
| 5815 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); |
| 5816 | else if (isAssumedReadOnly()) |
| 5817 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); |
| 5818 | else if (isAssumedWriteOnly()) |
| 5819 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); |
| 5820 | assert(Attrs.size() <= 1); |
| 5821 | } |
| 5822 | |
| 5823 | /// See AbstractAttribute::manifest(...). |
| 5824 | ChangeStatus manifest(Attributor &A) override { |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 5825 | if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true)) |
| 5826 | return ChangeStatus::UNCHANGED; |
| 5827 | |
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 5828 | const IRPosition &IRP = getIRPosition(); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5829 | |
| 5830 | // Check if we would improve the existing attributes first. |
| 5831 | SmallVector<Attribute, 4> DeducedAttrs; |
| 5832 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); |
| 5833 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { |
| 5834 | return IRP.hasAttr(Attr.getKindAsEnum(), |
| 5835 | /* IgnoreSubsumingPositions */ true); |
| 5836 | })) |
| 5837 | return ChangeStatus::UNCHANGED; |
| 5838 | |
| 5839 | // Clear existing attributes. |
| 5840 | IRP.removeAttrs(AttrKinds); |
| 5841 | |
| 5842 | // Use the generic manifest method. |
| 5843 | return IRAttribute::manifest(A); |
| 5844 | } |
| 5845 | |
| 5846 | /// See AbstractState::getAsStr(). |
| 5847 | const std::string getAsStr() const override { |
| 5848 | if (isAssumedReadNone()) |
| 5849 | return "readnone"; |
| 5850 | if (isAssumedReadOnly()) |
| 5851 | return "readonly"; |
| 5852 | if (isAssumedWriteOnly()) |
| 5853 | return "writeonly"; |
| 5854 | return "may-read/write"; |
| 5855 | } |
| 5856 | |
| 5857 | /// The set of IR attributes AAMemoryBehavior deals with. |
| 5858 | static const Attribute::AttrKind AttrKinds[3]; |
| 5859 | }; |
| 5860 | |
| 5861 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { |
| 5862 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; |
| 5863 | |
| 5864 | /// Memory behavior attribute for a floating value. |
| 5865 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { |
| 5866 | AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 5867 | |
| 5868 | /// See AbstractAttribute::initialize(...). |
| 5869 | void initialize(Attributor &A) override { |
| 5870 | AAMemoryBehaviorImpl::initialize(A); |
| 5871 | // Initialize the use vector with all direct uses of the associated value. |
| 5872 | for (const Use &U : getAssociatedValue().uses()) |
| 5873 | Uses.insert(&U); |
| 5874 | } |
| 5875 | |
| 5876 | /// See AbstractAttribute::updateImpl(...). |
| 5877 | ChangeStatus updateImpl(Attributor &A) override; |
| 5878 | |
| 5879 | /// See AbstractAttribute::trackStatistics() |
| 5880 | void trackStatistics() const override { |
| 5881 | if (isAssumedReadNone()) |
| 5882 | STATS_DECLTRACK_FLOATING_ATTR(readnone) |
| 5883 | else if (isAssumedReadOnly()) |
| 5884 | STATS_DECLTRACK_FLOATING_ATTR(readonly) |
| 5885 | else if (isAssumedWriteOnly()) |
| 5886 | STATS_DECLTRACK_FLOATING_ATTR(writeonly) |
| 5887 | } |
| 5888 | |
| 5889 | private: |
| 5890 | /// Return true if users of \p UserI might access the underlying |
| 5891 | /// variable/location described by \p U and should therefore be analyzed. |
| 5892 | bool followUsersOfUseIn(Attributor &A, const Use *U, |
| 5893 | const Instruction *UserI); |
| 5894 | |
| 5895 | /// Update the state according to the effect of use \p U in \p UserI. |
| 5896 | void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); |
| 5897 | |
| 5898 | protected: |
| 5899 | /// Container for (transitive) uses of the associated argument. |
| 5900 | SetVector<const Use *> Uses; |
| 5901 | }; |
| 5902 | |
| 5903 | /// Memory behavior attribute for function argument. |
| 5904 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { |
| 5905 | AAMemoryBehaviorArgument(const IRPosition &IRP) |
| 5906 | : AAMemoryBehaviorFloating(IRP) {} |
| 5907 | |
| 5908 | /// See AbstractAttribute::initialize(...). |
| 5909 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5910 | intersectAssumedBits(BEST_STATE); |
| 5911 | const IRPosition &IRP = getIRPosition(); |
| 5912 | // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we |
| 5913 | // can query it when we use has/getAttr. That would allow us to reuse the |
| 5914 | // initialize of the base class here. |
| 5915 | bool HasByVal = |
| 5916 | IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); |
| 5917 | getKnownStateFromValue(IRP, getState(), |
| 5918 | /* IgnoreSubsumingPositions */ HasByVal); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5919 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5920 | // Initialize the use vector with all direct uses of the associated value. |
| 5921 | Argument *Arg = getAssociatedArgument(); |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 5922 | if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent()))) { |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5923 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5924 | } else { |
| 5925 | // Initialize the use vector with all direct uses of the associated value. |
| 5926 | for (const Use &U : Arg->uses()) |
| 5927 | Uses.insert(&U); |
| 5928 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5931 | ChangeStatus manifest(Attributor &A) override { |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 5932 | // TODO: Pointer arguments are not supported on vectors of pointers yet. |
| 5933 | if (!getAssociatedValue().getType()->isPointerTy()) |
| 5934 | return ChangeStatus::UNCHANGED; |
| 5935 | |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5936 | // TODO: From readattrs.ll: "inalloca parameters are always |
| 5937 | // considered written" |
| 5938 | if (hasAttr({Attribute::InAlloca})) { |
| 5939 | removeKnownBits(NO_WRITES); |
| 5940 | removeAssumedBits(NO_WRITES); |
| 5941 | } |
| 5942 | return AAMemoryBehaviorFloating::manifest(A); |
| 5943 | } |
| 5944 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5945 | /// See AbstractAttribute::trackStatistics() |
| 5946 | void trackStatistics() const override { |
| 5947 | if (isAssumedReadNone()) |
| 5948 | STATS_DECLTRACK_ARG_ATTR(readnone) |
| 5949 | else if (isAssumedReadOnly()) |
| 5950 | STATS_DECLTRACK_ARG_ATTR(readonly) |
| 5951 | else if (isAssumedWriteOnly()) |
| 5952 | STATS_DECLTRACK_ARG_ATTR(writeonly) |
| 5953 | } |
| 5954 | }; |
| 5955 | |
| 5956 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { |
| 5957 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) |
| 5958 | : AAMemoryBehaviorArgument(IRP) {} |
| 5959 | |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5960 | /// See AbstractAttribute::initialize(...). |
| 5961 | void initialize(Attributor &A) override { |
| 5962 | if (Argument *Arg = getAssociatedArgument()) { |
| 5963 | if (Arg->hasByValAttr()) { |
| 5964 | addKnownBits(NO_WRITES); |
| 5965 | removeKnownBits(NO_READS); |
| 5966 | removeAssumedBits(NO_READS); |
| 5967 | } |
| 5968 | } else { |
| 5969 | } |
| 5970 | AAMemoryBehaviorArgument::initialize(A); |
| 5971 | } |
| 5972 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5973 | /// See AbstractAttribute::updateImpl(...). |
| 5974 | ChangeStatus updateImpl(Attributor &A) override { |
| 5975 | // TODO: Once we have call site specific value information we can provide |
| 5976 | // call site specific liveness liveness information and then it makes |
| 5977 | // sense to specialize attributes for call sites arguments instead of |
| 5978 | // redirecting requests to the callee argument. |
| 5979 | Argument *Arg = getAssociatedArgument(); |
| 5980 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 5981 | auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); |
| 5982 | return clampStateAndIndicateChange( |
| 5983 | getState(), |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 5984 | static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | /// See AbstractAttribute::trackStatistics() |
| 5988 | void trackStatistics() const override { |
| 5989 | if (isAssumedReadNone()) |
| 5990 | STATS_DECLTRACK_CSARG_ATTR(readnone) |
| 5991 | else if (isAssumedReadOnly()) |
| 5992 | STATS_DECLTRACK_CSARG_ATTR(readonly) |
| 5993 | else if (isAssumedWriteOnly()) |
| 5994 | STATS_DECLTRACK_CSARG_ATTR(writeonly) |
| 5995 | } |
| 5996 | }; |
| 5997 | |
| 5998 | /// Memory behavior attribute for a call site return position. |
| 5999 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { |
| 6000 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) |
| 6001 | : AAMemoryBehaviorFloating(IRP) {} |
| 6002 | |
| 6003 | /// See AbstractAttribute::manifest(...). |
| 6004 | ChangeStatus manifest(Attributor &A) override { |
| 6005 | // We do not annotate returned values. |
| 6006 | return ChangeStatus::UNCHANGED; |
| 6007 | } |
| 6008 | |
| 6009 | /// See AbstractAttribute::trackStatistics() |
| 6010 | void trackStatistics() const override {} |
| 6011 | }; |
| 6012 | |
| 6013 | /// An AA to represent the memory behavior function attributes. |
| 6014 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { |
| 6015 | AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 6016 | |
| 6017 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| 6018 | virtual ChangeStatus updateImpl(Attributor &A) override; |
| 6019 | |
| 6020 | /// See AbstractAttribute::manifest(...). |
| 6021 | ChangeStatus manifest(Attributor &A) override { |
| 6022 | Function &F = cast<Function>(getAnchorValue()); |
| 6023 | if (isAssumedReadNone()) { |
| 6024 | F.removeFnAttr(Attribute::ArgMemOnly); |
| 6025 | F.removeFnAttr(Attribute::InaccessibleMemOnly); |
| 6026 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
| 6027 | } |
| 6028 | return AAMemoryBehaviorImpl::manifest(A); |
| 6029 | } |
| 6030 | |
| 6031 | /// See AbstractAttribute::trackStatistics() |
| 6032 | void trackStatistics() const override { |
| 6033 | if (isAssumedReadNone()) |
| 6034 | STATS_DECLTRACK_FN_ATTR(readnone) |
| 6035 | else if (isAssumedReadOnly()) |
| 6036 | STATS_DECLTRACK_FN_ATTR(readonly) |
| 6037 | else if (isAssumedWriteOnly()) |
| 6038 | STATS_DECLTRACK_FN_ATTR(writeonly) |
| 6039 | } |
| 6040 | }; |
| 6041 | |
| 6042 | /// AAMemoryBehavior attribute for call sites. |
| 6043 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { |
| 6044 | AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 6045 | |
| 6046 | /// See AbstractAttribute::initialize(...). |
| 6047 | void initialize(Attributor &A) override { |
| 6048 | AAMemoryBehaviorImpl::initialize(A); |
| 6049 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 6050 | if (!F || !A.isFunctionIPOAmendable(*F)) |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6051 | indicatePessimisticFixpoint(); |
| 6052 | } |
| 6053 | |
| 6054 | /// See AbstractAttribute::updateImpl(...). |
| 6055 | ChangeStatus updateImpl(Attributor &A) override { |
| 6056 | // TODO: Once we have call site specific value information we can provide |
| 6057 | // call site specific liveness liveness information and then it makes |
| 6058 | // sense to specialize attributes for call sites arguments instead of |
| 6059 | // redirecting requests to the callee argument. |
| 6060 | Function *F = getAssociatedFunction(); |
| 6061 | const IRPosition &FnPos = IRPosition::function(*F); |
| 6062 | auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); |
| 6063 | return clampStateAndIndicateChange( |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 6064 | getState(), |
| 6065 | static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
| 6068 | /// See AbstractAttribute::trackStatistics() |
| 6069 | void trackStatistics() const override { |
| 6070 | if (isAssumedReadNone()) |
| 6071 | STATS_DECLTRACK_CS_ATTR(readnone) |
| 6072 | else if (isAssumedReadOnly()) |
| 6073 | STATS_DECLTRACK_CS_ATTR(readonly) |
| 6074 | else if (isAssumedWriteOnly()) |
| 6075 | STATS_DECLTRACK_CS_ATTR(writeonly) |
| 6076 | } |
| 6077 | }; |
| 6078 | |
| 6079 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { |
| 6080 | |
| 6081 | // The current assumed state used to determine a change. |
| 6082 | auto AssumedState = getAssumed(); |
| 6083 | |
| 6084 | auto CheckRWInst = [&](Instruction &I) { |
| 6085 | // If the instruction has an own memory behavior state, use it to restrict |
| 6086 | // the local state. No further analysis is required as the other memory |
| 6087 | // state is as optimistic as it gets. |
| 6088 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 6089 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( |
| 6090 | *this, IRPosition::callsite_function(ICS)); |
| 6091 | intersectAssumedBits(MemBehaviorAA.getAssumed()); |
| 6092 | return !isAtFixpoint(); |
| 6093 | } |
| 6094 | |
| 6095 | // Remove access kind modifiers if necessary. |
| 6096 | if (I.mayReadFromMemory()) |
| 6097 | removeAssumedBits(NO_READS); |
| 6098 | if (I.mayWriteToMemory()) |
| 6099 | removeAssumedBits(NO_WRITES); |
| 6100 | return !isAtFixpoint(); |
| 6101 | }; |
| 6102 | |
| 6103 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) |
| 6104 | return indicatePessimisticFixpoint(); |
| 6105 | |
| 6106 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED |
| 6107 | : ChangeStatus::UNCHANGED; |
| 6108 | } |
| 6109 | |
| 6110 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { |
| 6111 | |
| 6112 | const IRPosition &IRP = getIRPosition(); |
| 6113 | const IRPosition &FnPos = IRPosition::function_scope(IRP); |
| 6114 | AAMemoryBehavior::StateType &S = getState(); |
| 6115 | |
| 6116 | // First, check the function scope. We take the known information and we avoid |
| 6117 | // work if the assumed information implies the current assumed information for |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 6118 | // this attribute. This is a valid for all but byval arguments. |
| 6119 | Argument *Arg = IRP.getAssociatedArgument(); |
| 6120 | AAMemoryBehavior::base_t FnMemAssumedState = |
| 6121 | AAMemoryBehavior::StateType::getWorstState(); |
| 6122 | if (!Arg || !Arg->hasByValAttr()) { |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 6123 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>( |
| 6124 | *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 6125 | FnMemAssumedState = FnMemAA.getAssumed(); |
| 6126 | S.addKnownBits(FnMemAA.getKnown()); |
| 6127 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) |
| 6128 | return ChangeStatus::UNCHANGED; |
| 6129 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6130 | |
| 6131 | // Make sure the value is not captured (except through "return"), if |
| 6132 | // it is, any information derived would be irrelevant anyway as we cannot |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 6133 | // check the potential aliases introduced by the capture. However, no need |
| 6134 | // to fall back to anythign less optimistic than the function state. |
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 6135 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( |
| 6136 | *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 6137 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 6138 | S.intersectAssumedBits(FnMemAssumedState); |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 6139 | return ChangeStatus::CHANGED; |
| 6140 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6141 | |
| 6142 | // The current assumed state used to determine a change. |
| 6143 | auto AssumedState = S.getAssumed(); |
| 6144 | |
| 6145 | // Liveness information to exclude dead users. |
| 6146 | // TODO: Take the FnPos once we have call site specific liveness information. |
| 6147 | const auto &LivenessAA = A.getAAFor<AAIsDead>( |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 6148 | *this, IRPosition::function(*IRP.getAssociatedFunction()), |
| 6149 | /* TrackDependence */ false); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6150 | |
| 6151 | // Visit and expand uses until all are analyzed or a fixpoint is reached. |
| 6152 | for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { |
| 6153 | const Use *U = Uses[i]; |
| 6154 | Instruction *UserI = cast<Instruction>(U->getUser()); |
| 6155 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 6156 | << " [Dead: " << (A.isAssumedDead(*U, this, &LivenessAA)) |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6157 | << "]\n"); |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 6158 | if (A.isAssumedDead(*U, this, &LivenessAA)) |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6159 | continue; |
| 6160 | |
| 6161 | // Check if the users of UserI should also be visited. |
| 6162 | if (followUsersOfUseIn(A, U, UserI)) |
| 6163 | for (const Use &UserIUse : UserI->uses()) |
| 6164 | Uses.insert(&UserIUse); |
| 6165 | |
| 6166 | // If UserI might touch memory we analyze the use in detail. |
| 6167 | if (UserI->mayReadOrWriteMemory()) |
| 6168 | analyzeUseIn(A, U, UserI); |
| 6169 | } |
| 6170 | |
| 6171 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED |
| 6172 | : ChangeStatus::UNCHANGED; |
| 6173 | } |
| 6174 | |
| 6175 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, |
| 6176 | const Instruction *UserI) { |
| 6177 | // The loaded value is unrelated to the pointer argument, no need to |
| 6178 | // follow the users of the load. |
| 6179 | if (isa<LoadInst>(UserI)) |
| 6180 | return false; |
| 6181 | |
| 6182 | // By default we follow all uses assuming UserI might leak information on U, |
| 6183 | // we have special handling for call sites operands though. |
| 6184 | ImmutableCallSite ICS(UserI); |
| 6185 | if (!ICS || !ICS.isArgOperand(U)) |
| 6186 | return true; |
| 6187 | |
| 6188 | // If the use is a call argument known not to be captured, the users of |
| 6189 | // the call do not need to be visited because they have to be unrelated to |
| 6190 | // the input. Note that this check is not trivial even though we disallow |
| 6191 | // general capturing of the underlying argument. The reason is that the |
| 6192 | // call might the argument "through return", which we allow and for which we |
| 6193 | // need to check call users. |
| Johannes Doerfert | ff6254d | 2020-01-10 12:32:24 -0600 | [diff] [blame] | 6194 | if (U->get()->getType()->isPointerTy()) { |
| 6195 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 6196 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 6197 | *this, IRPosition::callsite_argument(ICS, ArgNo), |
| 6198 | /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | ff6254d | 2020-01-10 12:32:24 -0600 | [diff] [blame] | 6199 | return !ArgNoCaptureAA.isAssumedNoCapture(); |
| 6200 | } |
| 6201 | |
| 6202 | return true; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, |
| 6206 | const Instruction *UserI) { |
| 6207 | assert(UserI->mayReadOrWriteMemory()); |
| 6208 | |
| 6209 | switch (UserI->getOpcode()) { |
| 6210 | default: |
| 6211 | // TODO: Handle all atomics and other side-effect operations we know of. |
| 6212 | break; |
| 6213 | case Instruction::Load: |
| 6214 | // Loads cause the NO_READS property to disappear. |
| 6215 | removeAssumedBits(NO_READS); |
| 6216 | return; |
| 6217 | |
| 6218 | case Instruction::Store: |
| 6219 | // Stores cause the NO_WRITES property to disappear if the use is the |
| 6220 | // pointer operand. Note that we do assume that capturing was taken care of |
| 6221 | // somewhere else. |
| 6222 | if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) |
| 6223 | removeAssumedBits(NO_WRITES); |
| 6224 | return; |
| 6225 | |
| 6226 | case Instruction::Call: |
| 6227 | case Instruction::CallBr: |
| 6228 | case Instruction::Invoke: { |
| 6229 | // For call sites we look at the argument memory behavior attribute (this |
| 6230 | // could be recursive!) in order to restrict our own state. |
| 6231 | ImmutableCallSite ICS(UserI); |
| 6232 | |
| 6233 | // Give up on operand bundles. |
| 6234 | if (ICS.isBundleOperand(U)) { |
| 6235 | indicatePessimisticFixpoint(); |
| 6236 | return; |
| 6237 | } |
| 6238 | |
| 6239 | // Calling a function does read the function pointer, maybe write it if the |
| 6240 | // function is self-modifying. |
| 6241 | if (ICS.isCallee(U)) { |
| 6242 | removeAssumedBits(NO_READS); |
| 6243 | break; |
| 6244 | } |
| 6245 | |
| 6246 | // Adjust the possible access behavior based on the information on the |
| 6247 | // argument. |
| Johannes Doerfert | ff6254d | 2020-01-10 12:32:24 -0600 | [diff] [blame] | 6248 | IRPosition Pos; |
| 6249 | if (U->get()->getType()->isPointerTy()) |
| 6250 | Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U)); |
| 6251 | else |
| 6252 | Pos = IRPosition::callsite_function(ICS); |
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 6253 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( |
| 6254 | *this, Pos, |
| 6255 | /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6256 | // "assumed" has at most the same bits as the MemBehaviorAA assumed |
| 6257 | // and at least "known". |
| 6258 | intersectAssumedBits(MemBehaviorAA.getAssumed()); |
| 6259 | return; |
| 6260 | } |
| 6261 | }; |
| 6262 | |
| 6263 | // Generally, look at the "may-properties" and adjust the assumed state if we |
| 6264 | // did not trigger special handling before. |
| 6265 | if (UserI->mayReadFromMemory()) |
| 6266 | removeAssumedBits(NO_READS); |
| 6267 | if (UserI->mayWriteToMemory()) |
| 6268 | removeAssumedBits(NO_WRITES); |
| 6269 | } |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6270 | |
| Benjamin Kramer | 564a9de | 2020-02-17 17:55:03 +0100 | [diff] [blame] | 6271 | } // namespace |
| 6272 | |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6273 | /// -------------------- Memory Locations Attributes --------------------------- |
| 6274 | /// Includes read-none, argmemonly, inaccessiblememonly, |
| 6275 | /// inaccessiblememorargmemonly |
| 6276 | /// ---------------------------------------------------------------------------- |
| 6277 | |
| 6278 | std::string AAMemoryLocation::getMemoryLocationsAsStr( |
| 6279 | AAMemoryLocation::MemoryLocationsKind MLK) { |
| 6280 | if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS)) |
| 6281 | return "all memory"; |
| 6282 | if (MLK == AAMemoryLocation::NO_LOCATIONS) |
| 6283 | return "no memory"; |
| 6284 | std::string S = "memory:"; |
| 6285 | if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM)) |
| 6286 | S += "stack,"; |
| 6287 | if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM)) |
| 6288 | S += "constant,"; |
| 6289 | if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM)) |
| 6290 | S += "internal global,"; |
| 6291 | if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM)) |
| 6292 | S += "external global,"; |
| 6293 | if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM)) |
| 6294 | S += "argument,"; |
| 6295 | if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM)) |
| 6296 | S += "inaccessible,"; |
| 6297 | if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM)) |
| 6298 | S += "malloced,"; |
| 6299 | if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM)) |
| 6300 | S += "unknown,"; |
| 6301 | S.pop_back(); |
| 6302 | return S; |
| 6303 | } |
| 6304 | |
| Benjamin Kramer | 564a9de | 2020-02-17 17:55:03 +0100 | [diff] [blame] | 6305 | namespace { |
| 6306 | |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6307 | struct AAMemoryLocationImpl : public AAMemoryLocation { |
| 6308 | |
| 6309 | AAMemoryLocationImpl(const IRPosition &IRP) : AAMemoryLocation(IRP) {} |
| 6310 | |
| 6311 | /// See AbstractAttribute::initialize(...). |
| 6312 | void initialize(Attributor &A) override { |
| 6313 | intersectAssumedBits(BEST_STATE); |
| 6314 | getKnownStateFromValue(getIRPosition(), getState()); |
| 6315 | IRAttribute::initialize(A); |
| 6316 | } |
| 6317 | |
| 6318 | /// Return the memory behavior information encoded in the IR for \p IRP. |
| 6319 | static void getKnownStateFromValue(const IRPosition &IRP, |
| 6320 | BitIntegerState &State, |
| 6321 | bool IgnoreSubsumingPositions = false) { |
| 6322 | SmallVector<Attribute, 2> Attrs; |
| 6323 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); |
| 6324 | for (const Attribute &Attr : Attrs) { |
| 6325 | switch (Attr.getKindAsEnum()) { |
| 6326 | case Attribute::ReadNone: |
| 6327 | State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM); |
| 6328 | break; |
| 6329 | case Attribute::InaccessibleMemOnly: |
| 6330 | State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true)); |
| 6331 | break; |
| 6332 | case Attribute::ArgMemOnly: |
| 6333 | State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true)); |
| 6334 | break; |
| 6335 | case Attribute::InaccessibleMemOrArgMemOnly: |
| 6336 | State.addKnownBits( |
| 6337 | inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true)); |
| 6338 | break; |
| 6339 | default: |
| 6340 | llvm_unreachable("Unexpected attribute!"); |
| 6341 | } |
| 6342 | } |
| 6343 | } |
| 6344 | |
| 6345 | /// See AbstractAttribute::getDeducedAttributes(...). |
| 6346 | void getDeducedAttributes(LLVMContext &Ctx, |
| 6347 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 6348 | assert(Attrs.size() == 0); |
| 6349 | if (isAssumedReadNone()) { |
| 6350 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); |
| 6351 | } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) { |
| 6352 | if (isAssumedInaccessibleMemOnly()) |
| 6353 | Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly)); |
| 6354 | else if (isAssumedArgMemOnly()) |
| 6355 | Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly)); |
| 6356 | else if (isAssumedInaccessibleOrArgMemOnly()) |
| 6357 | Attrs.push_back( |
| 6358 | Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly)); |
| 6359 | } |
| 6360 | assert(Attrs.size() <= 1); |
| 6361 | } |
| 6362 | |
| 6363 | /// See AbstractAttribute::manifest(...). |
| 6364 | ChangeStatus manifest(Attributor &A) override { |
| 6365 | const IRPosition &IRP = getIRPosition(); |
| 6366 | |
| 6367 | // Check if we would improve the existing attributes first. |
| 6368 | SmallVector<Attribute, 4> DeducedAttrs; |
| 6369 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); |
| 6370 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { |
| 6371 | return IRP.hasAttr(Attr.getKindAsEnum(), |
| 6372 | /* IgnoreSubsumingPositions */ true); |
| 6373 | })) |
| 6374 | return ChangeStatus::UNCHANGED; |
| 6375 | |
| 6376 | // Clear existing attributes. |
| 6377 | IRP.removeAttrs(AttrKinds); |
| 6378 | if (isAssumedReadNone()) |
| 6379 | IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds); |
| 6380 | |
| 6381 | // Use the generic manifest method. |
| 6382 | return IRAttribute::manifest(A); |
| 6383 | } |
| 6384 | |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6385 | /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...). |
| 6386 | bool checkForAllAccessesToMemoryKind( |
| Johannes Doerfert | 1d5da8c | 2020-02-15 20:38:49 -0600 | [diff] [blame] | 6387 | const function_ref<bool(const Instruction *, const Value *, AccessKind, |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6388 | MemoryLocationsKind)> &Pred, |
| 6389 | MemoryLocationsKind RequestedMLK) const override { |
| 6390 | if (!isValidState()) |
| 6391 | return false; |
| 6392 | |
| 6393 | MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation(); |
| 6394 | if (AssumedMLK == NO_LOCATIONS) |
| 6395 | return true; |
| 6396 | |
| 6397 | for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) { |
| 6398 | if (CurMLK & RequestedMLK) |
| 6399 | continue; |
| 6400 | |
| 6401 | const auto &Accesses = AccessKindAccessesMap.lookup(CurMLK); |
| Johannes Doerfert | 1d5da8c | 2020-02-15 20:38:49 -0600 | [diff] [blame] | 6402 | for (const AccessInfo &AI : Accesses) { |
| 6403 | if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK)) |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6404 | return false; |
| Johannes Doerfert | 1d5da8c | 2020-02-15 20:38:49 -0600 | [diff] [blame] | 6405 | } |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6406 | } |
| 6407 | |
| 6408 | return true; |
| 6409 | } |
| 6410 | |
| 6411 | ChangeStatus indicatePessimisticFixpoint() override { |
| 6412 | // If we give up and indicate a pessimistic fixpoint this instruction will |
| 6413 | // become an access for all potential access kinds: |
| 6414 | // TODO: Add pointers for argmemonly and globals to improve the results of |
| 6415 | // checkForAllAccessesToMemoryKind. |
| 6416 | bool Changed = false; |
| 6417 | MemoryLocationsKind KnownMLK = getKnown(); |
| 6418 | Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); |
| 6419 | for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) |
| 6420 | if (!(CurMLK & KnownMLK)) |
| 6421 | updateStateAndAccessesMap(getState(), AccessKindAccessesMap, CurMLK, I, |
| 6422 | nullptr, Changed); |
| 6423 | return AAMemoryLocation::indicatePessimisticFixpoint(); |
| 6424 | } |
| 6425 | |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6426 | protected: |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6427 | /// Helper struct to tie together an instruction that has a read or write |
| 6428 | /// effect with the pointer it accesses (if any). |
| 6429 | struct AccessInfo { |
| 6430 | |
| 6431 | /// The instruction that caused the access. |
| 6432 | const Instruction *I; |
| 6433 | |
| 6434 | /// The base pointer that is accessed, or null if unknown. |
| 6435 | const Value *Ptr; |
| 6436 | |
| 6437 | /// The kind of access (read/write/read+write). |
| 6438 | AccessKind Kind; |
| 6439 | |
| 6440 | bool operator==(const AccessInfo &RHS) const { |
| Simon Pilgrim | 8a48c4a | 2020-02-15 13:53:18 +0000 | [diff] [blame] | 6441 | return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind; |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6442 | } |
| 6443 | bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const { |
| 6444 | if (LHS.I != RHS.I) |
| 6445 | return LHS.I < RHS.I; |
| 6446 | if (LHS.Ptr != RHS.Ptr) |
| 6447 | return LHS.Ptr < RHS.Ptr; |
| 6448 | if (LHS.Kind != RHS.Kind) |
| 6449 | return LHS.Kind < RHS.Kind; |
| 6450 | return false; |
| 6451 | } |
| 6452 | }; |
| 6453 | |
| 6454 | /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the |
| 6455 | /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind. |
| 6456 | using AccessKindAccessesMapTy = |
| 6457 | DenseMap<unsigned, SmallSet<AccessInfo, 8, AccessInfo>>; |
| 6458 | AccessKindAccessesMapTy AccessKindAccessesMap; |
| 6459 | |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6460 | /// Return the kind(s) of location that may be accessed by \p V. |
| 6461 | AAMemoryLocation::MemoryLocationsKind |
| 6462 | categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed); |
| 6463 | |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6464 | /// Update the state \p State and the AccessKindAccessesMap given that \p I is |
| 6465 | /// an access to a \p MLK memory location with the access pointer \p Ptr. |
| 6466 | static void updateStateAndAccessesMap(AAMemoryLocation::StateType &State, |
| 6467 | AccessKindAccessesMapTy &AccessMap, |
| 6468 | MemoryLocationsKind MLK, |
| 6469 | const Instruction *I, const Value *Ptr, |
| 6470 | bool &Changed) { |
| 6471 | // TODO: The kind should be determined at the call sites based on the |
| 6472 | // information we have there. |
| 6473 | AccessKind Kind = READ_WRITE; |
| 6474 | if (I) { |
| 6475 | Kind = I->mayReadFromMemory() ? READ : NONE; |
| 6476 | Kind = AccessKind(Kind | (I->mayWriteToMemory() ? WRITE : NONE)); |
| 6477 | } |
| 6478 | |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6479 | assert(isPowerOf2_32(MLK) && "Expected a single location set!"); |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6480 | Changed |= AccessMap[MLK].insert(AccessInfo{I, Ptr, Kind}).second; |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6481 | State.removeAssumedBits(MLK); |
| 6482 | } |
| 6483 | |
| 6484 | /// Determine the underlying locations kinds for \p Ptr, e.g., globals or |
| 6485 | /// arguments, and update the state and access map accordingly. |
| 6486 | void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr, |
| 6487 | AAMemoryLocation::StateType &State, bool &Changed); |
| 6488 | |
| 6489 | /// The set of IR attributes AAMemoryLocation deals with. |
| 6490 | static const Attribute::AttrKind AttrKinds[4]; |
| 6491 | }; |
| 6492 | |
| 6493 | const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = { |
| 6494 | Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly, |
| 6495 | Attribute::InaccessibleMemOrArgMemOnly}; |
| 6496 | |
| 6497 | void AAMemoryLocationImpl::categorizePtrValue( |
| 6498 | Attributor &A, const Instruction &I, const Value &Ptr, |
| 6499 | AAMemoryLocation::StateType &State, bool &Changed) { |
| 6500 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for " |
| 6501 | << Ptr << " [" |
| 6502 | << getMemoryLocationsAsStr(State.getAssumed()) << "]\n"); |
| 6503 | |
| 6504 | auto StripGEPCB = [](Value *V) -> Value * { |
| 6505 | auto *GEP = dyn_cast<GEPOperator>(V); |
| 6506 | while (GEP) { |
| 6507 | V = GEP->getPointerOperand(); |
| 6508 | GEP = dyn_cast<GEPOperator>(V); |
| 6509 | } |
| 6510 | return V; |
| 6511 | }; |
| 6512 | |
| 6513 | auto VisitValueCB = [&](Value &V, AAMemoryLocation::StateType &T, |
| 6514 | bool Stripped) -> bool { |
| 6515 | assert(!isa<GEPOperator>(V) && "GEPs should have been stripped."); |
| 6516 | if (isa<UndefValue>(V)) |
| 6517 | return true; |
| 6518 | if (auto *Arg = dyn_cast<Argument>(&V)) { |
| 6519 | if (Arg->hasByValAttr()) |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6520 | updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I, |
| 6521 | &V, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6522 | else |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6523 | updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_ARGUMENT_MEM, &I, |
| 6524 | &V, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6525 | return true; |
| 6526 | } |
| 6527 | if (auto *GV = dyn_cast<GlobalValue>(&V)) { |
| 6528 | if (GV->hasLocalLinkage()) |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6529 | updateStateAndAccessesMap(T, AccessKindAccessesMap, |
| 6530 | NO_GLOBAL_INTERNAL_MEM, &I, &V, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6531 | else |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6532 | updateStateAndAccessesMap(T, AccessKindAccessesMap, |
| 6533 | NO_GLOBAL_EXTERNAL_MEM, &I, &V, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6534 | return true; |
| 6535 | } |
| 6536 | if (isa<AllocaInst>(V)) { |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6537 | updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I, &V, |
| 6538 | Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6539 | return true; |
| 6540 | } |
| 6541 | if (ImmutableCallSite ICS = ImmutableCallSite(&V)) { |
| 6542 | const auto &NoAliasAA = |
| 6543 | A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS)); |
| 6544 | if (NoAliasAA.isAssumedNoAlias()) { |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6545 | updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_MALLOCED_MEM, &I, |
| 6546 | &V, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6547 | return true; |
| 6548 | } |
| 6549 | } |
| 6550 | |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6551 | updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_UNKOWN_MEM, &I, &V, |
| 6552 | Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6553 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value cannot be categorized: " |
| 6554 | << V << " -> " << getMemoryLocationsAsStr(T.getAssumed()) |
| 6555 | << "\n"); |
| 6556 | return true; |
| 6557 | }; |
| 6558 | |
| 6559 | if (!genericValueTraversal<AAMemoryLocation, AAMemoryLocation::StateType>( |
| 6560 | A, IRPosition::value(Ptr), *this, State, VisitValueCB, |
| 6561 | /* MaxValues */ 32, StripGEPCB)) { |
| 6562 | LLVM_DEBUG( |
| 6563 | dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n"); |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6564 | updateStateAndAccessesMap(State, AccessKindAccessesMap, NO_UNKOWN_MEM, &I, |
| 6565 | nullptr, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6566 | } else { |
| 6567 | LLVM_DEBUG( |
| 6568 | dbgs() |
| 6569 | << "[AAMemoryLocation] Accessed locations with pointer locations: " |
| 6570 | << getMemoryLocationsAsStr(State.getAssumed()) << "\n"); |
| 6571 | } |
| 6572 | } |
| 6573 | |
| 6574 | AAMemoryLocation::MemoryLocationsKind |
| 6575 | AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I, |
| 6576 | bool &Changed) { |
| 6577 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for " |
| 6578 | << I << "\n"); |
| 6579 | |
| 6580 | AAMemoryLocation::StateType AccessedLocs; |
| 6581 | AccessedLocs.intersectAssumedBits(NO_LOCATIONS); |
| 6582 | |
| 6583 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 6584 | |
| 6585 | // First check if we assume any memory is access is visible. |
| 6586 | const auto &ICSMemLocationAA = |
| 6587 | A.getAAFor<AAMemoryLocation>(*this, IRPosition::callsite_function(ICS)); |
| 6588 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I |
| 6589 | << " [" << ICSMemLocationAA << "]\n"); |
| 6590 | |
| 6591 | if (ICSMemLocationAA.isAssumedReadNone()) |
| 6592 | return NO_LOCATIONS; |
| 6593 | |
| 6594 | if (ICSMemLocationAA.isAssumedInaccessibleMemOnly()) { |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6595 | updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, |
| 6596 | NO_INACCESSIBLE_MEM, &I, nullptr, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6597 | return AccessedLocs.getAssumed(); |
| 6598 | } |
| 6599 | |
| 6600 | uint32_t ICSAssumedNotAccessedLocs = |
| 6601 | ICSMemLocationAA.getAssumedNotAccessedLocation(); |
| 6602 | |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6603 | // Set the argmemonly and global bit as we handle them separately below. |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6604 | uint32_t ICSAssumedNotAccessedLocsNoArgMem = |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6605 | ICSAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM; |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6606 | |
| 6607 | for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) { |
| 6608 | if (ICSAssumedNotAccessedLocsNoArgMem & CurMLK) |
| 6609 | continue; |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6610 | updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, CurMLK, &I, |
| 6611 | nullptr, Changed); |
| 6612 | } |
| 6613 | |
| 6614 | // Now handle global memory if it might be accessed. |
| 6615 | bool HasGlobalAccesses = !(ICSAssumedNotAccessedLocs & NO_GLOBAL_MEM); |
| 6616 | if (HasGlobalAccesses) { |
| Johannes Doerfert | 1d5da8c | 2020-02-15 20:38:49 -0600 | [diff] [blame] | 6617 | auto AccessPred = [&](const Instruction *, const Value *Ptr, |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6618 | AccessKind Kind, MemoryLocationsKind MLK) { |
| 6619 | updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, MLK, &I, |
| 6620 | Ptr, Changed); |
| 6621 | return true; |
| 6622 | }; |
| 6623 | if (!ICSMemLocationAA.checkForAllAccessesToMemoryKind( |
| 6624 | AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false))) |
| 6625 | return AccessedLocs.getWorstState(); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | LLVM_DEBUG( |
| 6629 | dbgs() << "[AAMemoryLocation] Accessed state before argument handling: " |
| 6630 | << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"); |
| 6631 | |
| 6632 | // Now handle argument memory if it might be accessed. |
| 6633 | bool HasArgAccesses = !(ICSAssumedNotAccessedLocs & NO_ARGUMENT_MEM); |
| 6634 | if (HasArgAccesses) { |
| 6635 | for (unsigned ArgNo = 0, e = ICS.getNumArgOperands(); ArgNo < e; |
| 6636 | ++ArgNo) { |
| 6637 | |
| 6638 | // Skip non-pointer arguments. |
| 6639 | const Value *ArgOp = ICS.getArgOperand(ArgNo); |
| 6640 | if (!ArgOp->getType()->isPtrOrPtrVectorTy()) |
| 6641 | continue; |
| 6642 | |
| 6643 | // Skip readnone arguments. |
| 6644 | const IRPosition &ArgOpIRP = IRPosition::callsite_argument(ICS, ArgNo); |
| 6645 | const auto &ArgOpMemLocationAA = A.getAAFor<AAMemoryBehavior>( |
| 6646 | *this, ArgOpIRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| 6647 | |
| 6648 | if (ArgOpMemLocationAA.isAssumedReadNone()) |
| 6649 | continue; |
| 6650 | |
| 6651 | // Categorize potentially accessed pointer arguments as if there was an |
| 6652 | // access instruction with them as pointer. |
| 6653 | categorizePtrValue(A, I, *ArgOp, AccessedLocs, Changed); |
| 6654 | } |
| 6655 | } |
| 6656 | |
| 6657 | LLVM_DEBUG( |
| 6658 | dbgs() << "[AAMemoryLocation] Accessed state after argument handling: " |
| 6659 | << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"); |
| 6660 | |
| 6661 | return AccessedLocs.getAssumed(); |
| 6662 | } |
| 6663 | |
| 6664 | if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) { |
| 6665 | LLVM_DEBUG( |
| 6666 | dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: " |
| 6667 | << I << " [" << *Ptr << "]\n"); |
| 6668 | categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed); |
| 6669 | return AccessedLocs.getAssumed(); |
| 6670 | } |
| 6671 | |
| 6672 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: " |
| 6673 | << I << "\n"); |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6674 | updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, NO_UNKOWN_MEM, |
| 6675 | &I, nullptr, Changed); |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6676 | return AccessedLocs.getAssumed(); |
| 6677 | } |
| 6678 | |
| 6679 | /// An AA to represent the memory behavior function attributes. |
| 6680 | struct AAMemoryLocationFunction final : public AAMemoryLocationImpl { |
| 6681 | AAMemoryLocationFunction(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {} |
| 6682 | |
| 6683 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| 6684 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| 6685 | |
| 6686 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( |
| 6687 | *this, getIRPosition(), /* TrackDependence */ false); |
| 6688 | if (MemBehaviorAA.isAssumedReadNone()) { |
| 6689 | if (MemBehaviorAA.isKnownReadNone()) |
| 6690 | return indicateOptimisticFixpoint(); |
| 6691 | assert(isAssumedReadNone() && |
| 6692 | "AAMemoryLocation was not read-none but AAMemoryBehavior was!"); |
| 6693 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); |
| 6694 | return ChangeStatus::UNCHANGED; |
| 6695 | } |
| 6696 | |
| 6697 | // The current assumed state used to determine a change. |
| 6698 | auto AssumedState = getAssumed(); |
| 6699 | bool Changed = false; |
| 6700 | |
| 6701 | auto CheckRWInst = [&](Instruction &I) { |
| 6702 | MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed); |
| 6703 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I |
| 6704 | << ": " << getMemoryLocationsAsStr(MLK) << "\n"); |
| 6705 | removeAssumedBits(inverseLocation(MLK, false, false)); |
| 6706 | return true; |
| 6707 | }; |
| 6708 | |
| 6709 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) |
| 6710 | return indicatePessimisticFixpoint(); |
| 6711 | |
| 6712 | Changed |= AssumedState != getAssumed(); |
| 6713 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| 6714 | } |
| 6715 | |
| 6716 | /// See AbstractAttribute::trackStatistics() |
| 6717 | void trackStatistics() const override { |
| 6718 | if (isAssumedReadNone()) |
| 6719 | STATS_DECLTRACK_FN_ATTR(readnone) |
| 6720 | else if (isAssumedArgMemOnly()) |
| 6721 | STATS_DECLTRACK_FN_ATTR(argmemonly) |
| 6722 | else if (isAssumedInaccessibleMemOnly()) |
| 6723 | STATS_DECLTRACK_FN_ATTR(inaccessiblememonly) |
| 6724 | else if (isAssumedInaccessibleOrArgMemOnly()) |
| 6725 | STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly) |
| 6726 | } |
| 6727 | }; |
| 6728 | |
| 6729 | /// AAMemoryLocation attribute for call sites. |
| 6730 | struct AAMemoryLocationCallSite final : AAMemoryLocationImpl { |
| 6731 | AAMemoryLocationCallSite(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {} |
| 6732 | |
| 6733 | /// See AbstractAttribute::initialize(...). |
| 6734 | void initialize(Attributor &A) override { |
| 6735 | AAMemoryLocationImpl::initialize(A); |
| 6736 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 6737 | if (!F || !A.isFunctionIPOAmendable(*F)) |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6738 | indicatePessimisticFixpoint(); |
| 6739 | } |
| 6740 | |
| 6741 | /// See AbstractAttribute::updateImpl(...). |
| 6742 | ChangeStatus updateImpl(Attributor &A) override { |
| 6743 | // TODO: Once we have call site specific value information we can provide |
| 6744 | // call site specific liveness liveness information and then it makes |
| 6745 | // sense to specialize attributes for call sites arguments instead of |
| 6746 | // redirecting requests to the callee argument. |
| 6747 | Function *F = getAssociatedFunction(); |
| 6748 | const IRPosition &FnPos = IRPosition::function(*F); |
| 6749 | auto &FnAA = A.getAAFor<AAMemoryLocation>(*this, FnPos); |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6750 | bool Changed = false; |
| Johannes Doerfert | 1d5da8c | 2020-02-15 20:38:49 -0600 | [diff] [blame] | 6751 | auto AccessPred = [&](const Instruction *I, const Value *Ptr, |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6752 | AccessKind Kind, MemoryLocationsKind MLK) { |
| Johannes Doerfert | 1d5da8c | 2020-02-15 20:38:49 -0600 | [diff] [blame] | 6753 | updateStateAndAccessesMap(getState(), AccessKindAccessesMap, MLK, I, Ptr, |
| Johannes Doerfert | ef746aa | 2020-01-27 22:49:36 -0600 | [diff] [blame] | 6754 | Changed); |
| 6755 | return true; |
| 6756 | }; |
| 6757 | if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS)) |
| 6758 | return indicatePessimisticFixpoint(); |
| 6759 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | /// See AbstractAttribute::trackStatistics() |
| 6763 | void trackStatistics() const override { |
| 6764 | if (isAssumedReadNone()) |
| 6765 | STATS_DECLTRACK_CS_ATTR(readnone) |
| 6766 | } |
| 6767 | }; |
| 6768 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6769 | /// ------------------ Value Constant Range Attribute ------------------------- |
| Hideto Ueno | e996303 | 2020-01-01 15:25:19 +0900 | [diff] [blame] | 6770 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6771 | struct AAValueConstantRangeImpl : AAValueConstantRange { |
| 6772 | using StateType = IntegerRangeState; |
| 6773 | AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {} |
| 6774 | |
| 6775 | /// See AbstractAttribute::getAsStr(). |
| 6776 | const std::string getAsStr() const override { |
| 6777 | std::string Str; |
| 6778 | llvm::raw_string_ostream OS(Str); |
| 6779 | OS << "range(" << getBitWidth() << ")<"; |
| 6780 | getKnown().print(OS); |
| 6781 | OS << " / "; |
| 6782 | getAssumed().print(OS); |
| 6783 | OS << ">"; |
| 6784 | return OS.str(); |
| 6785 | } |
| 6786 | |
| 6787 | /// Helper function to get a SCEV expr for the associated value at program |
| 6788 | /// point \p I. |
| 6789 | const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const { |
| 6790 | if (!getAnchorScope()) |
| 6791 | return nullptr; |
| 6792 | |
| 6793 | ScalarEvolution *SE = |
| 6794 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( |
| 6795 | *getAnchorScope()); |
| 6796 | |
| 6797 | LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>( |
| 6798 | *getAnchorScope()); |
| 6799 | |
| 6800 | if (!SE || !LI) |
| 6801 | return nullptr; |
| 6802 | |
| 6803 | const SCEV *S = SE->getSCEV(&getAssociatedValue()); |
| 6804 | if (!I) |
| 6805 | return S; |
| 6806 | |
| 6807 | return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent())); |
| 6808 | } |
| 6809 | |
| 6810 | /// Helper function to get a range from SCEV for the associated value at |
| 6811 | /// program point \p I. |
| 6812 | ConstantRange getConstantRangeFromSCEV(Attributor &A, |
| 6813 | const Instruction *I = nullptr) const { |
| 6814 | if (!getAnchorScope()) |
| 6815 | return getWorstState(getBitWidth()); |
| 6816 | |
| 6817 | ScalarEvolution *SE = |
| 6818 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( |
| 6819 | *getAnchorScope()); |
| 6820 | |
| 6821 | const SCEV *S = getSCEV(A, I); |
| 6822 | if (!SE || !S) |
| 6823 | return getWorstState(getBitWidth()); |
| 6824 | |
| 6825 | return SE->getUnsignedRange(S); |
| 6826 | } |
| 6827 | |
| 6828 | /// Helper function to get a range from LVI for the associated value at |
| 6829 | /// program point \p I. |
| 6830 | ConstantRange |
| 6831 | getConstantRangeFromLVI(Attributor &A, |
| 6832 | const Instruction *CtxI = nullptr) const { |
| 6833 | if (!getAnchorScope()) |
| 6834 | return getWorstState(getBitWidth()); |
| 6835 | |
| 6836 | LazyValueInfo *LVI = |
| 6837 | A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>( |
| 6838 | *getAnchorScope()); |
| 6839 | |
| 6840 | if (!LVI || !CtxI) |
| 6841 | return getWorstState(getBitWidth()); |
| 6842 | return LVI->getConstantRange(&getAssociatedValue(), |
| 6843 | const_cast<BasicBlock *>(CtxI->getParent()), |
| 6844 | const_cast<Instruction *>(CtxI)); |
| 6845 | } |
| 6846 | |
| 6847 | /// See AAValueConstantRange::getKnownConstantRange(..). |
| 6848 | ConstantRange |
| 6849 | getKnownConstantRange(Attributor &A, |
| 6850 | const Instruction *CtxI = nullptr) const override { |
| 6851 | if (!CtxI || CtxI == getCtxI()) |
| 6852 | return getKnown(); |
| 6853 | |
| 6854 | ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); |
| 6855 | ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); |
| 6856 | return getKnown().intersectWith(SCEVR).intersectWith(LVIR); |
| 6857 | } |
| 6858 | |
| 6859 | /// See AAValueConstantRange::getAssumedConstantRange(..). |
| 6860 | ConstantRange |
| 6861 | getAssumedConstantRange(Attributor &A, |
| 6862 | const Instruction *CtxI = nullptr) const override { |
| 6863 | // TODO: Make SCEV use Attributor assumption. |
| 6864 | // We may be able to bound a variable range via assumptions in |
| 6865 | // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to |
| 6866 | // evolve to x^2 + x, then we can say that y is in [2, 12]. |
| 6867 | |
| 6868 | if (!CtxI || CtxI == getCtxI()) |
| 6869 | return getAssumed(); |
| 6870 | |
| 6871 | ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); |
| 6872 | ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); |
| 6873 | return getAssumed().intersectWith(SCEVR).intersectWith(LVIR); |
| 6874 | } |
| 6875 | |
| 6876 | /// See AbstractAttribute::initialize(..). |
| 6877 | void initialize(Attributor &A) override { |
| 6878 | // Intersect a range given by SCEV. |
| 6879 | intersectKnown(getConstantRangeFromSCEV(A, getCtxI())); |
| 6880 | |
| 6881 | // Intersect a range given by LVI. |
| 6882 | intersectKnown(getConstantRangeFromLVI(A, getCtxI())); |
| 6883 | } |
| 6884 | |
| 6885 | /// Helper function to create MDNode for range metadata. |
| 6886 | static MDNode * |
| 6887 | getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx, |
| 6888 | const ConstantRange &AssumedConstantRange) { |
| 6889 | Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get( |
| 6890 | Ty, AssumedConstantRange.getLower())), |
| 6891 | ConstantAsMetadata::get(ConstantInt::get( |
| 6892 | Ty, AssumedConstantRange.getUpper()))}; |
| 6893 | return MDNode::get(Ctx, LowAndHigh); |
| 6894 | } |
| 6895 | |
| 6896 | /// Return true if \p Assumed is included in \p KnownRanges. |
| 6897 | static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) { |
| 6898 | |
| 6899 | if (Assumed.isFullSet()) |
| 6900 | return false; |
| 6901 | |
| 6902 | if (!KnownRanges) |
| 6903 | return true; |
| 6904 | |
| 6905 | // If multiple ranges are annotated in IR, we give up to annotate assumed |
| 6906 | // range for now. |
| 6907 | |
| 6908 | // TODO: If there exists a known range which containts assumed range, we |
| 6909 | // can say assumed range is better. |
| 6910 | if (KnownRanges->getNumOperands() > 2) |
| 6911 | return false; |
| 6912 | |
| 6913 | ConstantInt *Lower = |
| 6914 | mdconst::extract<ConstantInt>(KnownRanges->getOperand(0)); |
| 6915 | ConstantInt *Upper = |
| 6916 | mdconst::extract<ConstantInt>(KnownRanges->getOperand(1)); |
| 6917 | |
| 6918 | ConstantRange Known(Lower->getValue(), Upper->getValue()); |
| 6919 | return Known.contains(Assumed) && Known != Assumed; |
| 6920 | } |
| 6921 | |
| 6922 | /// Helper function to set range metadata. |
| 6923 | static bool |
| 6924 | setRangeMetadataIfisBetterRange(Instruction *I, |
| 6925 | const ConstantRange &AssumedConstantRange) { |
| 6926 | auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range); |
| 6927 | if (isBetterRange(AssumedConstantRange, OldRangeMD)) { |
| 6928 | if (!AssumedConstantRange.isEmptySet()) { |
| 6929 | I->setMetadata(LLVMContext::MD_range, |
| 6930 | getMDNodeForConstantRange(I->getType(), I->getContext(), |
| 6931 | AssumedConstantRange)); |
| 6932 | return true; |
| 6933 | } |
| 6934 | } |
| 6935 | return false; |
| 6936 | } |
| 6937 | |
| 6938 | /// See AbstractAttribute::manifest() |
| 6939 | ChangeStatus manifest(Attributor &A) override { |
| 6940 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 6941 | ConstantRange AssumedConstantRange = getAssumedConstantRange(A); |
| 6942 | assert(!AssumedConstantRange.isFullSet() && "Invalid state"); |
| 6943 | |
| 6944 | auto &V = getAssociatedValue(); |
| 6945 | if (!AssumedConstantRange.isEmptySet() && |
| 6946 | !AssumedConstantRange.isSingleElement()) { |
| 6947 | if (Instruction *I = dyn_cast<Instruction>(&V)) |
| 6948 | if (isa<CallInst>(I) || isa<LoadInst>(I)) |
| 6949 | if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange)) |
| 6950 | Changed = ChangeStatus::CHANGED; |
| 6951 | } |
| 6952 | |
| 6953 | return Changed; |
| 6954 | } |
| 6955 | }; |
| 6956 | |
| Johannes Doerfert | ea5fabe | 2020-01-28 09:46:15 -0600 | [diff] [blame] | 6957 | struct AAValueConstantRangeArgument final |
| 6958 | : AAArgumentFromCallSiteArguments< |
| 6959 | AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> { |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6960 | AAValueConstantRangeArgument(const IRPosition &IRP) |
| Johannes Doerfert | ea5fabe | 2020-01-28 09:46:15 -0600 | [diff] [blame] | 6961 | : AAArgumentFromCallSiteArguments< |
| 6962 | AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>( |
| 6963 | IRP) {} |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6964 | |
| 6965 | /// See AbstractAttribute::trackStatistics() |
| 6966 | void trackStatistics() const override { |
| 6967 | STATS_DECLTRACK_ARG_ATTR(value_range) |
| 6968 | } |
| 6969 | }; |
| 6970 | |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6971 | struct AAValueConstantRangeReturned |
| 6972 | : AAReturnedFromReturnedValues<AAValueConstantRange, |
| 6973 | AAValueConstantRangeImpl> { |
| 6974 | using Base = AAReturnedFromReturnedValues<AAValueConstantRange, |
| 6975 | AAValueConstantRangeImpl>; |
| 6976 | AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6977 | |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6978 | /// See AbstractAttribute::initialize(...). |
| 6979 | void initialize(Attributor &A) override {} |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6980 | |
| 6981 | /// See AbstractAttribute::trackStatistics() |
| 6982 | void trackStatistics() const override { |
| 6983 | STATS_DECLTRACK_FNRET_ATTR(value_range) |
| 6984 | } |
| 6985 | }; |
| 6986 | |
| 6987 | struct AAValueConstantRangeFloating : AAValueConstantRangeImpl { |
| 6988 | AAValueConstantRangeFloating(const IRPosition &IRP) |
| 6989 | : AAValueConstantRangeImpl(IRP) {} |
| 6990 | |
| 6991 | /// See AbstractAttribute::initialize(...). |
| 6992 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 028db8c4 | 2020-02-09 19:05:15 -0600 | [diff] [blame] | 6993 | AAValueConstantRangeImpl::initialize(A); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6994 | Value &V = getAssociatedValue(); |
| 6995 | |
| 6996 | if (auto *C = dyn_cast<ConstantInt>(&V)) { |
| 6997 | unionAssumed(ConstantRange(C->getValue())); |
| 6998 | indicateOptimisticFixpoint(); |
| 6999 | return; |
| 7000 | } |
| 7001 | |
| 7002 | if (isa<UndefValue>(&V)) { |
| Johannes Doerfert | b53af0e | 2020-02-14 20:08:20 -0600 | [diff] [blame] | 7003 | // Collapse the undef state to 0. |
| 7004 | unionAssumed(ConstantRange(APInt(getBitWidth(), 0))); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7005 | indicateOptimisticFixpoint(); |
| 7006 | return; |
| 7007 | } |
| 7008 | |
| Johannes Doerfert | 7cbb107 | 2020-02-13 14:22:26 -0600 | [diff] [blame] | 7009 | if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V)) |
| 7010 | return; |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7011 | // If it is a load instruction with range metadata, use it. |
| 7012 | if (LoadInst *LI = dyn_cast<LoadInst>(&V)) |
| 7013 | if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) { |
| 7014 | intersectKnown(getConstantRangeFromMetadata(*RangeMD)); |
| 7015 | return; |
| 7016 | } |
| 7017 | |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7018 | // We can work with PHI and select instruction as we traverse their operands |
| Johannes Doerfert | 7e7e659 | 2020-02-09 19:08:04 -0600 | [diff] [blame] | 7019 | // during update. |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7020 | if (isa<SelectInst>(V) || isa<PHINode>(V)) |
| Johannes Doerfert | 7e7e659 | 2020-02-09 19:08:04 -0600 | [diff] [blame] | 7021 | return; |
| 7022 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7023 | // Otherwise we give up. |
| 7024 | indicatePessimisticFixpoint(); |
| 7025 | |
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 7026 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: " |
| 7027 | << getAssociatedValue() << "\n"); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7028 | } |
| 7029 | |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7030 | bool calculateBinaryOperator( |
| 7031 | Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T, |
| 7032 | Instruction *CtxI, |
| 7033 | SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7034 | Value *LHS = BinOp->getOperand(0); |
| 7035 | Value *RHS = BinOp->getOperand(1); |
| Johannes Doerfert | 7cbb107 | 2020-02-13 14:22:26 -0600 | [diff] [blame] | 7036 | // TODO: Allow non integers as well. |
| 7037 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) |
| 7038 | return false; |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7039 | |
| 7040 | auto &LHSAA = |
| 7041 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS)); |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7042 | QuerriedAAs.push_back(&LHSAA); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7043 | auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); |
| 7044 | |
| 7045 | auto &RHSAA = |
| 7046 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS)); |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7047 | QuerriedAAs.push_back(&RHSAA); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7048 | auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); |
| 7049 | |
| 7050 | auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange); |
| 7051 | |
| 7052 | T.unionAssumed(AssumedRange); |
| 7053 | |
| 7054 | // TODO: Track a known state too. |
| 7055 | |
| 7056 | return T.isValidState(); |
| 7057 | } |
| 7058 | |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7059 | bool calculateCastInst( |
| 7060 | Attributor &A, CastInst *CastI, IntegerRangeState &T, Instruction *CtxI, |
| 7061 | SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { |
| Johannes Doerfert | ffdbd2a | 2020-02-09 19:07:30 -0600 | [diff] [blame] | 7062 | assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!"); |
| 7063 | // TODO: Allow non integers as well. |
| 7064 | Value &OpV = *CastI->getOperand(0); |
| Johannes Doerfert | 7cbb107 | 2020-02-13 14:22:26 -0600 | [diff] [blame] | 7065 | if (!OpV.getType()->isIntegerTy()) |
| 7066 | return false; |
| Johannes Doerfert | ffdbd2a | 2020-02-09 19:07:30 -0600 | [diff] [blame] | 7067 | |
| 7068 | auto &OpAA = |
| 7069 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV)); |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7070 | QuerriedAAs.push_back(&OpAA); |
| Johannes Doerfert | ffdbd2a | 2020-02-09 19:07:30 -0600 | [diff] [blame] | 7071 | T.unionAssumed( |
| 7072 | OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth())); |
| 7073 | return T.isValidState(); |
| 7074 | } |
| 7075 | |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7076 | bool |
| 7077 | calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T, |
| 7078 | Instruction *CtxI, |
| 7079 | SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7080 | Value *LHS = CmpI->getOperand(0); |
| 7081 | Value *RHS = CmpI->getOperand(1); |
| Johannes Doerfert | 7cbb107 | 2020-02-13 14:22:26 -0600 | [diff] [blame] | 7082 | // TODO: Allow non integers as well. |
| 7083 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) |
| 7084 | return false; |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7085 | |
| 7086 | auto &LHSAA = |
| 7087 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS)); |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7088 | QuerriedAAs.push_back(&LHSAA); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7089 | auto &RHSAA = |
| 7090 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS)); |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7091 | QuerriedAAs.push_back(&RHSAA); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7092 | |
| 7093 | auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); |
| 7094 | auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); |
| 7095 | |
| 7096 | // If one of them is empty set, we can't decide. |
| 7097 | if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet()) |
| 7098 | return true; |
| 7099 | |
| 7100 | bool MustTrue = false, MustFalse = false; |
| 7101 | |
| 7102 | auto AllowedRegion = |
| 7103 | ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange); |
| 7104 | |
| 7105 | auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion( |
| 7106 | CmpI->getPredicate(), RHSAARange); |
| 7107 | |
| 7108 | if (AllowedRegion.intersectWith(LHSAARange).isEmptySet()) |
| 7109 | MustFalse = true; |
| 7110 | |
| 7111 | if (SatisfyingRegion.contains(LHSAARange)) |
| 7112 | MustTrue = true; |
| 7113 | |
| 7114 | assert((!MustTrue || !MustFalse) && |
| 7115 | "Either MustTrue or MustFalse should be false!"); |
| 7116 | |
| 7117 | if (MustTrue) |
| 7118 | T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1))); |
| 7119 | else if (MustFalse) |
| 7120 | T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0))); |
| 7121 | else |
| 7122 | T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true)); |
| 7123 | |
| 7124 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA |
| 7125 | << " " << RHSAA << "\n"); |
| 7126 | |
| 7127 | // TODO: Track a known state too. |
| 7128 | return T.isValidState(); |
| 7129 | } |
| 7130 | |
| 7131 | /// See AbstractAttribute::updateImpl(...). |
| 7132 | ChangeStatus updateImpl(Attributor &A) override { |
| 7133 | Instruction *CtxI = getCtxI(); |
| 7134 | auto VisitValueCB = [&](Value &V, IntegerRangeState &T, |
| 7135 | bool Stripped) -> bool { |
| 7136 | Instruction *I = dyn_cast<Instruction>(&V); |
| 7137 | if (!I) { |
| 7138 | |
| 7139 | // If the value is not instruction, we query AA to Attributor. |
| 7140 | const auto &AA = |
| 7141 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V)); |
| 7142 | |
| 7143 | // Clamp operator is not used to utilize a program point CtxI. |
| 7144 | T.unionAssumed(AA.getAssumedConstantRange(A, CtxI)); |
| 7145 | |
| 7146 | return T.isValidState(); |
| 7147 | } |
| 7148 | |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7149 | SmallVector<const AAValueConstantRange *, 4> QuerriedAAs; |
| 7150 | if (auto *BinOp = dyn_cast<BinaryOperator>(I)) { |
| 7151 | if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs)) |
| 7152 | return false; |
| 7153 | } else if (auto *CmpI = dyn_cast<CmpInst>(I)) { |
| 7154 | if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs)) |
| 7155 | return false; |
| 7156 | } else if (auto *CastI = dyn_cast<CastInst>(I)) { |
| 7157 | if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs)) |
| 7158 | return false; |
| 7159 | } else { |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7160 | // Give up with other instructions. |
| 7161 | // TODO: Add other instructions |
| 7162 | |
| 7163 | T.indicatePessimisticFixpoint(); |
| 7164 | return false; |
| 7165 | } |
| Johannes Doerfert | 8155439 | 2020-02-09 20:14:35 -0600 | [diff] [blame] | 7166 | |
| 7167 | // Catch circular reasoning in a pessimistic way for now. |
| 7168 | // TODO: Check how the range evolves and if we stripped anything, see also |
| 7169 | // AADereferenceable or AAAlign for similar situations. |
| 7170 | for (const AAValueConstantRange *QueriedAA : QuerriedAAs) { |
| 7171 | if (QueriedAA != this) |
| 7172 | continue; |
| 7173 | // If we are in a stady state we do not need to worry. |
| 7174 | if (T.getAssumed() == getState().getAssumed()) |
| 7175 | continue; |
| 7176 | T.indicatePessimisticFixpoint(); |
| 7177 | } |
| 7178 | |
| 7179 | return T.isValidState(); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7180 | }; |
| 7181 | |
| 7182 | IntegerRangeState T(getBitWidth()); |
| 7183 | |
| 7184 | if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>( |
| 7185 | A, getIRPosition(), *this, T, VisitValueCB)) |
| 7186 | return indicatePessimisticFixpoint(); |
| 7187 | |
| 7188 | return clampStateAndIndicateChange(getState(), T); |
| 7189 | } |
| 7190 | |
| 7191 | /// See AbstractAttribute::trackStatistics() |
| 7192 | void trackStatistics() const override { |
| 7193 | STATS_DECLTRACK_FLOATING_ATTR(value_range) |
| 7194 | } |
| 7195 | }; |
| 7196 | |
| 7197 | struct AAValueConstantRangeFunction : AAValueConstantRangeImpl { |
| 7198 | AAValueConstantRangeFunction(const IRPosition &IRP) |
| 7199 | : AAValueConstantRangeImpl(IRP) {} |
| 7200 | |
| 7201 | /// See AbstractAttribute::initialize(...). |
| 7202 | ChangeStatus updateImpl(Attributor &A) override { |
| 7203 | llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will " |
| 7204 | "not be called"); |
| 7205 | } |
| 7206 | |
| 7207 | /// See AbstractAttribute::trackStatistics() |
| 7208 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) } |
| 7209 | }; |
| 7210 | |
| 7211 | struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction { |
| 7212 | AAValueConstantRangeCallSite(const IRPosition &IRP) |
| 7213 | : AAValueConstantRangeFunction(IRP) {} |
| 7214 | |
| 7215 | /// See AbstractAttribute::trackStatistics() |
| 7216 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) } |
| 7217 | }; |
| 7218 | |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 7219 | struct AAValueConstantRangeCallSiteReturned |
| 7220 | : AACallSiteReturnedFromReturned<AAValueConstantRange, |
| 7221 | AAValueConstantRangeImpl> { |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7222 | AAValueConstantRangeCallSiteReturned(const IRPosition &IRP) |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 7223 | : AACallSiteReturnedFromReturned<AAValueConstantRange, |
| 7224 | AAValueConstantRangeImpl>(IRP) {} |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7225 | |
| 7226 | /// See AbstractAttribute::initialize(...). |
| 7227 | void initialize(Attributor &A) override { |
| 7228 | // If it is a load instruction with range metadata, use the metadata. |
| 7229 | if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue())) |
| 7230 | if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range)) |
| 7231 | intersectKnown(getConstantRangeFromMetadata(*RangeMD)); |
| 7232 | |
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 7233 | AAValueConstantRangeImpl::initialize(A); |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | /// See AbstractAttribute::trackStatistics() |
| 7237 | void trackStatistics() const override { |
| 7238 | STATS_DECLTRACK_CSRET_ATTR(value_range) |
| 7239 | } |
| 7240 | }; |
| 7241 | struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating { |
| 7242 | AAValueConstantRangeCallSiteArgument(const IRPosition &IRP) |
| 7243 | : AAValueConstantRangeFloating(IRP) {} |
| 7244 | |
| 7245 | /// See AbstractAttribute::trackStatistics() |
| 7246 | void trackStatistics() const override { |
| 7247 | STATS_DECLTRACK_CSARG_ATTR(value_range) |
| 7248 | } |
| 7249 | }; |
| Benjamin Kramer | 564a9de | 2020-02-17 17:55:03 +0100 | [diff] [blame] | 7250 | |
| 7251 | } // namespace |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7252 | /// ---------------------------------------------------------------------------- |
| 7253 | /// Attributor |
| 7254 | /// ---------------------------------------------------------------------------- |
| 7255 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7256 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7257 | const AAIsDead *FnLivenessAA, |
| 7258 | bool CheckBBLivenessOnly, DepClassTy DepClass) { |
| 7259 | const IRPosition &IRP = AA.getIRPosition(); |
| 7260 | if (!Functions.count(IRP.getAnchorScope())) |
| 7261 | return false; |
| 7262 | return isAssumedDead(IRP, &AA, FnLivenessAA, CheckBBLivenessOnly, DepClass); |
| 7263 | } |
| 7264 | |
| 7265 | bool Attributor::isAssumedDead(const Use &U, |
| 7266 | const AbstractAttribute *QueryingAA, |
| 7267 | const AAIsDead *FnLivenessAA, |
| 7268 | bool CheckBBLivenessOnly, DepClassTy DepClass) { |
| 7269 | Instruction *UserI = dyn_cast<Instruction>(U.getUser()); |
| 7270 | if (!UserI) |
| 7271 | return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA, |
| 7272 | CheckBBLivenessOnly, DepClass); |
| 7273 | |
| 7274 | if (CallSite CS = CallSite(UserI)) { |
| 7275 | // For call site argument uses we can check if the argument is |
| 7276 | // unused/dead. |
| 7277 | if (CS.isArgOperand(&U)) { |
| 7278 | const IRPosition &CSArgPos = |
| 7279 | IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); |
| 7280 | return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA, |
| 7281 | CheckBBLivenessOnly, DepClass); |
| 7282 | } |
| 7283 | } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { |
| 7284 | const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); |
| 7285 | return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, CheckBBLivenessOnly, |
| 7286 | DepClass); |
| 7287 | } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) { |
| 7288 | BasicBlock *IncomingBB = PHI->getIncomingBlock(U); |
| 7289 | return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA, |
| 7290 | CheckBBLivenessOnly, DepClass); |
| 7291 | } |
| 7292 | |
| 7293 | return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA, |
| 7294 | CheckBBLivenessOnly, DepClass); |
| 7295 | } |
| 7296 | |
| 7297 | bool Attributor::isAssumedDead(const Instruction &I, |
| 7298 | const AbstractAttribute *QueryingAA, |
| 7299 | const AAIsDead *FnLivenessAA, |
| 7300 | bool CheckBBLivenessOnly, DepClassTy DepClass) { |
| 7301 | if (!FnLivenessAA) |
| 7302 | FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction()), |
| 7303 | QueryingAA, |
| 7304 | /* TrackDependence */ false); |
| 7305 | |
| 7306 | // If we have a context instruction and a liveness AA we use it. |
| 7307 | if (FnLivenessAA && |
| 7308 | FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() && |
| 7309 | FnLivenessAA->isAssumedDead(&I)) { |
| 7310 | if (QueryingAA) |
| 7311 | recordDependence(*FnLivenessAA, *QueryingAA, DepClass); |
| 7312 | return true; |
| 7313 | } |
| 7314 | |
| 7315 | if (CheckBBLivenessOnly) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7316 | return false; |
| 7317 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7318 | const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>( |
| 7319 | IRPosition::value(I), QueryingAA, /* TrackDependence */ false); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 7320 | // Don't check liveness for AAIsDead. |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7321 | if (QueryingAA == &IsDeadAA) |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 7322 | return false; |
| 7323 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7324 | if (IsDeadAA.isAssumedDead()) { |
| 7325 | if (QueryingAA) |
| 7326 | recordDependence(IsDeadAA, *QueryingAA, DepClass); |
| 7327 | return true; |
| 7328 | } |
| 7329 | |
| 7330 | return false; |
| 7331 | } |
| 7332 | |
| 7333 | bool Attributor::isAssumedDead(const IRPosition &IRP, |
| 7334 | const AbstractAttribute *QueryingAA, |
| 7335 | const AAIsDead *FnLivenessAA, |
| 7336 | bool CheckBBLivenessOnly, DepClassTy DepClass) { |
| 7337 | Instruction *CtxI = IRP.getCtxI(); |
| 7338 | if (CtxI && |
| 7339 | isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, |
| 7340 | /* CheckBBLivenessOnly */ true, |
| 7341 | CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL)) |
| 7342 | return true; |
| 7343 | |
| 7344 | if (CheckBBLivenessOnly) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7345 | return false; |
| 7346 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7347 | // If we haven't succeeded we query the specific liveness info for the IRP. |
| 7348 | const AAIsDead *IsDeadAA; |
| 7349 | if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE) |
| 7350 | IsDeadAA = &getOrCreateAAFor<AAIsDead>( |
| 7351 | IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())), |
| 7352 | QueryingAA, /* TrackDependence */ false); |
| 7353 | else |
| 7354 | IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, |
| 7355 | /* TrackDependence */ false); |
| 7356 | // Don't check liveness for AAIsDead. |
| 7357 | if (QueryingAA == IsDeadAA) |
| 7358 | return false; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 7359 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7360 | if (IsDeadAA->isAssumedDead()) { |
| 7361 | if (QueryingAA) |
| 7362 | recordDependence(*IsDeadAA, *QueryingAA, DepClass); |
| 7363 | return true; |
| 7364 | } |
| 7365 | |
| 7366 | return false; |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7367 | } |
| 7368 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7369 | bool Attributor::checkForAllUses( |
| 7370 | const function_ref<bool(const Use &, bool &)> &Pred, |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7371 | const AbstractAttribute &QueryingAA, const Value &V, |
| 7372 | DepClassTy LivenessDepClass) { |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 7373 | |
| 7374 | // Check the trivial case first as it catches void values. |
| 7375 | if (V.use_empty()) |
| 7376 | return true; |
| 7377 | |
| 7378 | // If the value is replaced by another one, for now a constant, we do not have |
| 7379 | // uses. Note that this requires users of `checkForAllUses` to not recurse but |
| 7380 | // instead use the `follow` callback argument to look at transitive users, |
| 7381 | // however, that should be clear from the presence of the argument. |
| 7382 | bool UsedAssumedInformation = false; |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 7383 | Optional<Constant *> C = |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 7384 | getAssumedConstant(*this, V, QueryingAA, UsedAssumedInformation); |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 7385 | if (C.hasValue() && C.getValue()) { |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 7386 | LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V |
| Johannes Doerfert | e1eed6c | 2020-02-16 16:45:28 -0600 | [diff] [blame] | 7387 | << " -> " << *C.getValue() << "\n"); |
| Johannes Doerfert | b2c7600 | 2020-01-23 17:12:56 -0600 | [diff] [blame] | 7388 | return true; |
| 7389 | } |
| 7390 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7391 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 7392 | SmallVector<const Use *, 16> Worklist; |
| 7393 | SmallPtrSet<const Use *, 16> Visited; |
| 7394 | |
| 7395 | for (const Use &U : V.uses()) |
| 7396 | Worklist.push_back(&U); |
| 7397 | |
| 7398 | LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() |
| 7399 | << " initial uses to check\n"); |
| 7400 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7401 | const Function *ScopeFn = IRP.getAnchorScope(); |
| 7402 | const auto *LivenessAA = |
| 7403 | ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), |
| 7404 | /* TrackDependence */ false) |
| 7405 | : nullptr; |
| 7406 | |
| 7407 | while (!Worklist.empty()) { |
| 7408 | const Use *U = Worklist.pop_back_val(); |
| 7409 | if (!Visited.insert(U).second) |
| 7410 | continue; |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7411 | LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in " |
| 7412 | << *U->getUser() << "\n"); |
| 7413 | if (isAssumedDead(*U, &QueryingAA, LivenessAA, |
| 7414 | /* CheckBBLivenessOnly */ false, LivenessDepClass)) { |
| 7415 | LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); |
| 7416 | continue; |
| Johannes Doerfert | 8e62968 | 2020-02-11 00:10:35 -0600 | [diff] [blame] | 7417 | } |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7418 | |
| 7419 | bool Follow = false; |
| 7420 | if (!Pred(*U, Follow)) |
| 7421 | return false; |
| 7422 | if (!Follow) |
| 7423 | continue; |
| 7424 | for (const Use &UU : U->getUser()->uses()) |
| 7425 | Worklist.push_back(&UU); |
| 7426 | } |
| 7427 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7428 | return true; |
| 7429 | } |
| 7430 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7431 | bool Attributor::checkForAllCallSites( |
| 7432 | const function_ref<bool(AbstractCallSite)> &Pred, |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7433 | const AbstractAttribute &QueryingAA, bool RequireAllCallSites, |
| 7434 | bool &AllCallSitesKnown) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7435 | // We can try to determine information from |
| 7436 | // the call sites. However, this is only possible all call sites are known, |
| 7437 | // hence the function has internal linkage. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7438 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 7439 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 7440 | if (!AssociatedFunction) { |
| 7441 | LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP |
| 7442 | << "\n"); |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7443 | AllCallSitesKnown = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7444 | return false; |
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 7445 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7446 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 7447 | return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7448 | &QueryingAA, AllCallSitesKnown); |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 7449 | } |
| 7450 | |
| 7451 | bool Attributor::checkForAllCallSites( |
| 7452 | const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7453 | bool RequireAllCallSites, const AbstractAttribute *QueryingAA, |
| 7454 | bool &AllCallSitesKnown) { |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 7455 | if (RequireAllCallSites && !Fn.hasLocalLinkage()) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7456 | LLVM_DEBUG( |
| 7457 | dbgs() |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 7458 | << "[Attributor] Function " << Fn.getName() |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7459 | << " has no internal linkage, hence not all call sites are known\n"); |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7460 | AllCallSitesKnown = false; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7461 | return false; |
| 7462 | } |
| 7463 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7464 | // If we do not require all call sites we might not see all. |
| 7465 | AllCallSitesKnown = RequireAllCallSites; |
| 7466 | |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 7467 | SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses())); |
| 7468 | for (unsigned u = 0; u < Uses.size(); ++u) { |
| 7469 | const Use &U = *Uses[u]; |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7470 | LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in " |
| 7471 | << *U.getUser() << "\n"); |
| 7472 | if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) { |
| 7473 | LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); |
| 7474 | continue; |
| 7475 | } |
| Johannes Doerfert | c6ac717 | 2020-01-25 20:24:38 -0600 | [diff] [blame] | 7476 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) { |
| 7477 | if (CE->isCast() && CE->getType()->isPointerTy() && |
| 7478 | CE->getType()->getPointerElementType()->isFunctionTy()) { |
| 7479 | for (const Use &CEU : CE->uses()) |
| 7480 | Uses.push_back(&CEU); |
| 7481 | continue; |
| 7482 | } |
| 7483 | } |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7484 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7485 | AbstractCallSite ACS(&U); |
| 7486 | if (!ACS) { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 7487 | LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7488 | << " has non call site use " << *U.get() << " in " |
| 7489 | << *U.getUser() << "\n"); |
| Johannes Doerfert | 2d77b0c | 2019-11-01 22:35:18 -0500 | [diff] [blame] | 7490 | // BlockAddress users are allowed. |
| 7491 | if (isa<BlockAddress>(U.getUser())) |
| 7492 | continue; |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 7493 | return false; |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7494 | } |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 7495 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7496 | const Use *EffectiveUse = |
| 7497 | ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; |
| 7498 | if (!ACS.isCallee(EffectiveUse)) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7499 | if (!RequireAllCallSites) |
| 7500 | continue; |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7501 | LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 7502 | << " is an invalid use of " << Fn.getName() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7503 | return false; |
| 7504 | } |
| 7505 | |
| Johannes Doerfert | 1e99fc9 | 2020-02-16 16:42:47 -0600 | [diff] [blame] | 7506 | // Make sure the arguments that can be matched between the call site and the |
| 7507 | // callee argee on their type. It is unlikely they do not and it doesn't |
| 7508 | // make sense for all attributes to know/care about this. |
| 7509 | assert(&Fn == ACS.getCalledFunction() && "Expected known callee"); |
| 7510 | unsigned MinArgsParams = |
| 7511 | std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size()); |
| 7512 | for (unsigned u = 0; u < MinArgsParams; ++u) { |
| 7513 | Value *CSArgOp = ACS.getCallArgOperand(u); |
| 7514 | if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) { |
| 7515 | LLVM_DEBUG( |
| 7516 | dbgs() << "[Attributor] Call site / callee argument type mismatch [" |
| 7517 | << u << "@" << Fn.getName() << ": " |
| 7518 | << *Fn.getArg(u)->getType() << " vs. " |
| 7519 | << *ACS.getCallArgOperand(u)->getType() << "\n"); |
| 7520 | return false; |
| 7521 | } |
| 7522 | } |
| 7523 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7524 | if (Pred(ACS)) |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7525 | continue; |
| 7526 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 7527 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 7528 | << *ACS.getInstruction() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7529 | return false; |
| 7530 | } |
| 7531 | |
| 7532 | return true; |
| 7533 | } |
| 7534 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7535 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 7536 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7537 | &Pred, |
| 7538 | const AbstractAttribute &QueryingAA) { |
| 7539 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7540 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 7541 | // Since we need to provide return instructions we have to have an exact |
| 7542 | // definition. |
| 7543 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 7544 | if (!AssociatedFunction) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7545 | return false; |
| 7546 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7547 | // If this is a call site query we use the call site specific return values |
| 7548 | // and liveness information. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 7549 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 7550 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7551 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7552 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7553 | return false; |
| 7554 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7555 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7556 | } |
| 7557 | |
| 7558 | bool Attributor::checkForAllReturnedValues( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7559 | const function_ref<bool(Value &)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7560 | const AbstractAttribute &QueryingAA) { |
| 7561 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7562 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 7563 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 7564 | if (!AssociatedFunction) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7565 | return false; |
| 7566 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 7567 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 7568 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7569 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7570 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7571 | return false; |
| 7572 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7573 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 7574 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 7575 | return Pred(RV); |
| 7576 | }); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 7577 | } |
| 7578 | |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7579 | static bool checkForAllInstructionsImpl( |
| 7580 | Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap, |
| 7581 | const function_ref<bool(Instruction &)> &Pred, |
| 7582 | const AbstractAttribute *QueryingAA, const AAIsDead *LivenessAA, |
| 7583 | const ArrayRef<unsigned> &Opcodes, bool CheckBBLivenessOnly = false) { |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7584 | for (unsigned Opcode : Opcodes) { |
| 7585 | for (Instruction *I : OpcodeInstMap[Opcode]) { |
| 7586 | // Skip dead instructions. |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7587 | if (A && A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA, |
| 7588 | CheckBBLivenessOnly)) |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7589 | continue; |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7590 | |
| 7591 | if (!Pred(*I)) |
| 7592 | return false; |
| 7593 | } |
| 7594 | } |
| 7595 | return true; |
| 7596 | } |
| 7597 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 7598 | bool Attributor::checkForAllInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7599 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7600 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes, |
| 7601 | bool CheckBBLivenessOnly) { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 7602 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7603 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 7604 | // Since we need to provide instructions we have to have an exact definition. |
| 7605 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 7606 | if (!AssociatedFunction) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7607 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 7608 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 7609 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 7610 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 7611 | const auto &LivenessAA = |
| 7612 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7613 | |
| 7614 | auto &OpcodeInstMap = |
| 7615 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7616 | if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA, |
| 7617 | &LivenessAA, Opcodes, CheckBBLivenessOnly)) |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7618 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 7619 | |
| 7620 | return true; |
| 7621 | } |
| 7622 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 7623 | bool Attributor::checkForAllReadWriteInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7624 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 7625 | AbstractAttribute &QueryingAA) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 7626 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7627 | const Function *AssociatedFunction = |
| 7628 | QueryingAA.getIRPosition().getAssociatedFunction(); |
| 7629 | if (!AssociatedFunction) |
| 7630 | return false; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 7631 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 7632 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 7633 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| 7634 | const auto &LivenessAA = |
| 7635 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7636 | |
| 7637 | for (Instruction *I : |
| 7638 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 7639 | // Skip dead instructions. |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7640 | if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA)) |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 7641 | continue; |
| 7642 | |
| 7643 | if (!Pred(*I)) |
| 7644 | return false; |
| 7645 | } |
| 7646 | |
| 7647 | return true; |
| 7648 | } |
| 7649 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7650 | ChangeStatus Attributor::run() { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7651 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " |
| 7652 | << AllAbstractAttributes.size() |
| 7653 | << " abstract attributes.\n"); |
| 7654 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 7655 | // Now that all abstract attributes are collected and initialized we start |
| 7656 | // the abstract analysis. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7657 | |
| 7658 | unsigned IterationCounter = 1; |
| 7659 | |
| 7660 | SmallVector<AbstractAttribute *, 64> ChangedAAs; |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7661 | SetVector<AbstractAttribute *> Worklist, InvalidAAs; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7662 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); |
| 7663 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 7664 | bool RecomputeDependences = false; |
| 7665 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7666 | do { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7667 | // Remember the size to determine new attributes. |
| 7668 | size_t NumAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7669 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter |
| 7670 | << ", Worklist size: " << Worklist.size() << "\n"); |
| 7671 | |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7672 | // For invalid AAs we can fix dependent AAs that have a required dependence, |
| 7673 | // thereby folding long dependence chains in a single step without the need |
| 7674 | // to run updates. |
| 7675 | for (unsigned u = 0; u < InvalidAAs.size(); ++u) { |
| 7676 | AbstractAttribute *InvalidAA = InvalidAAs[u]; |
| 7677 | auto &QuerriedAAs = QueryMap[InvalidAA]; |
| 7678 | LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " |
| 7679 | << QuerriedAAs.RequiredAAs.size() << "/" |
| 7680 | << QuerriedAAs.OptionalAAs.size() |
| 7681 | << " required/optional dependences\n"); |
| 7682 | for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) { |
| 7683 | AbstractState &DOIAAState = DepOnInvalidAA->getState(); |
| 7684 | DOIAAState.indicatePessimisticFixpoint(); |
| 7685 | ++NumAttributesFixedDueToRequiredDependences; |
| 7686 | assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!"); |
| 7687 | if (!DOIAAState.isValidState()) |
| 7688 | InvalidAAs.insert(DepOnInvalidAA); |
| Johannes Doerfert | 2240854 | 2020-01-28 09:38:07 -0600 | [diff] [blame] | 7689 | else |
| 7690 | ChangedAAs.push_back(DepOnInvalidAA); |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7691 | } |
| 7692 | if (!RecomputeDependences) |
| 7693 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), |
| 7694 | QuerriedAAs.OptionalAAs.end()); |
| 7695 | } |
| 7696 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 7697 | // If dependences (=QueryMap) are recomputed we have to look at all abstract |
| 7698 | // attributes again, regardless of what changed in the last iteration. |
| 7699 | if (RecomputeDependences) { |
| 7700 | LLVM_DEBUG( |
| 7701 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); |
| 7702 | QueryMap.clear(); |
| 7703 | ChangedAAs.clear(); |
| 7704 | Worklist.insert(AllAbstractAttributes.begin(), |
| 7705 | AllAbstractAttributes.end()); |
| 7706 | } |
| 7707 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7708 | // Add all abstract attributes that are potentially dependent on one that |
| 7709 | // changed to the work list. |
| 7710 | for (AbstractAttribute *ChangedAA : ChangedAAs) { |
| 7711 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7712 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), |
| 7713 | QuerriedAAs.OptionalAAs.end()); |
| 7714 | Worklist.insert(QuerriedAAs.RequiredAAs.begin(), |
| 7715 | QuerriedAAs.RequiredAAs.end()); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 7718 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter |
| 7719 | << ", Worklist+Dependent size: " << Worklist.size() |
| 7720 | << "\n"); |
| 7721 | |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7722 | // Reset the changed and invalid set. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7723 | ChangedAAs.clear(); |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7724 | InvalidAAs.clear(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7725 | |
| 7726 | // Update all abstract attribute in the work list and record the ones that |
| 7727 | // changed. |
| 7728 | for (AbstractAttribute *AA : Worklist) |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7729 | if (!AA->getState().isAtFixpoint() && |
| 7730 | !isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) { |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 7731 | QueriedNonFixAA = false; |
| 7732 | if (AA->update(*this) == ChangeStatus::CHANGED) { |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7733 | ChangedAAs.push_back(AA); |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7734 | if (!AA->getState().isValidState()) |
| 7735 | InvalidAAs.insert(AA); |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 7736 | } else if (!QueriedNonFixAA) { |
| 7737 | // If the attribute did not query any non-fix information, the state |
| 7738 | // will not change and we can indicate that right away. |
| 7739 | AA->getState().indicateOptimisticFixpoint(); |
| 7740 | } |
| 7741 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7742 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 7743 | // Check if we recompute the dependences in the next iteration. |
| 7744 | RecomputeDependences = (DepRecomputeInterval > 0 && |
| 7745 | IterationCounter % DepRecomputeInterval == 0); |
| 7746 | |
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 7747 | // Add attributes to the changed set if they have been created in the last |
| 7748 | // iteration. |
| 7749 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, |
| 7750 | AllAbstractAttributes.end()); |
| 7751 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7752 | // Reset the work list and repopulate with the changed abstract attributes. |
| 7753 | // Note that dependent ones are added above. |
| 7754 | Worklist.clear(); |
| 7755 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); |
| 7756 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 7757 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || |
| 7758 | VerifyMaxFixpointIterations)); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 7759 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7760 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " |
| 7761 | << IterationCounter << "/" << MaxFixpointIterations |
| 7762 | << " iterations\n"); |
| 7763 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 7764 | size_t NumFinalAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 7765 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7766 | // Reset abstract arguments not settled in a sound fixpoint by now. This |
| 7767 | // happens when we stopped the fixpoint iteration early. Note that only the |
| 7768 | // ones marked as "changed" *and* the ones transitively depending on them |
| 7769 | // need to be reverted to a pessimistic state. Others might not be in a |
| 7770 | // fixpoint state but we can use the optimistic results for them anyway. |
| 7771 | SmallPtrSet<AbstractAttribute *, 32> Visited; |
| 7772 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { |
| 7773 | AbstractAttribute *ChangedAA = ChangedAAs[u]; |
| 7774 | if (!Visited.insert(ChangedAA).second) |
| 7775 | continue; |
| 7776 | |
| 7777 | AbstractState &State = ChangedAA->getState(); |
| 7778 | if (!State.isAtFixpoint()) { |
| 7779 | State.indicatePessimisticFixpoint(); |
| 7780 | |
| 7781 | NumAttributesTimedOut++; |
| 7782 | } |
| 7783 | |
| 7784 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7785 | ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(), |
| 7786 | QuerriedAAs.OptionalAAs.end()); |
| 7787 | ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(), |
| 7788 | QuerriedAAs.RequiredAAs.end()); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | LLVM_DEBUG({ |
| 7792 | if (!Visited.empty()) |
| 7793 | dbgs() << "\n[Attributor] Finalized " << Visited.size() |
| 7794 | << " abstract attributes.\n"; |
| 7795 | }); |
| 7796 | |
| 7797 | unsigned NumManifested = 0; |
| 7798 | unsigned NumAtFixpoint = 0; |
| 7799 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; |
| 7800 | for (AbstractAttribute *AA : AllAbstractAttributes) { |
| 7801 | AbstractState &State = AA->getState(); |
| 7802 | |
| 7803 | // If there is not already a fixpoint reached, we can now take the |
| 7804 | // optimistic state. This is correct because we enforced a pessimistic one |
| 7805 | // on abstract attributes that were transitively dependent on a changed one |
| 7806 | // already above. |
| 7807 | if (!State.isAtFixpoint()) |
| 7808 | State.indicateOptimisticFixpoint(); |
| 7809 | |
| 7810 | // If the state is invalid, we do not try to manifest it. |
| 7811 | if (!State.isValidState()) |
| 7812 | continue; |
| 7813 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7814 | // Skip dead code. |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 7815 | if (isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 7816 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7817 | // Manifest the state and record if we changed the IR. |
| 7818 | ChangeStatus LocalChange = AA->manifest(*this); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 7819 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) |
| 7820 | AA->trackStatistics(); |
| Johannes Doerfert | 8e76fec | 2020-02-16 17:37:50 -0600 | [diff] [blame] | 7821 | LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA |
| 7822 | << "\n"); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 7823 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7824 | ManifestChange = ManifestChange | LocalChange; |
| 7825 | |
| 7826 | NumAtFixpoint++; |
| 7827 | NumManifested += (LocalChange == ChangeStatus::CHANGED); |
| 7828 | } |
| 7829 | |
| 7830 | (void)NumManifested; |
| 7831 | (void)NumAtFixpoint; |
| 7832 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested |
| 7833 | << " arguments while " << NumAtFixpoint |
| 7834 | << " were in a valid fixpoint state\n"); |
| 7835 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7836 | NumAttributesManifested += NumManifested; |
| 7837 | NumAttributesValidFixpoint += NumAtFixpoint; |
| 7838 | |
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 7839 | (void)NumFinalAAs; |
| Johannes Doerfert | b70297a | 2020-02-14 10:34:31 -0600 | [diff] [blame] | 7840 | if (NumFinalAAs != AllAbstractAttributes.size()) { |
| 7841 | for (unsigned u = NumFinalAAs; u < AllAbstractAttributes.size(); ++u) |
| 7842 | errs() << "Unexpected abstract attribute: " << *AllAbstractAttributes[u] |
| 7843 | << " :: " |
| 7844 | << AllAbstractAttributes[u]->getIRPosition().getAssociatedValue() |
| 7845 | << "\n"; |
| 7846 | llvm_unreachable("Expected the final number of abstract attributes to " |
| 7847 | "remain unchanged!"); |
| 7848 | } |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 7849 | |
| 7850 | // Delete stuff at the end to avoid invalid references and a nice order. |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7851 | { |
| 7852 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " |
| 7853 | << ToBeDeletedFunctions.size() << " functions and " |
| 7854 | << ToBeDeletedBlocks.size() << " blocks and " |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7855 | << ToBeDeletedInsts.size() << " instructions and " |
| 7856 | << ToBeChangedUses.size() << " uses\n"); |
| 7857 | |
| Alina Sbirlea | 9e66c4e | 2020-01-23 14:21:08 -0800 | [diff] [blame] | 7858 | SmallVector<WeakTrackingVH, 32> DeadInsts; |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7859 | SmallVector<Instruction *, 32> TerminatorsToFold; |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7860 | |
| 7861 | for (auto &It : ToBeChangedUses) { |
| 7862 | Use *U = It.first; |
| 7863 | Value *NewV = It.second; |
| 7864 | Value *OldV = U->get(); |
| Johannes Doerfert | 54ec9b5 | 2020-03-16 20:23:27 -0500 | [diff] [blame^] | 7865 | |
| 7866 | // Do not replace uses in returns if the value is a must-tail call we will |
| 7867 | // not delete. |
| 7868 | if (isa<ReturnInst>(U->getUser())) |
| 7869 | if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts())) |
| 7870 | if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI)) |
| 7871 | continue; |
| 7872 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7873 | LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() |
| 7874 | << " instead of " << *OldV << "\n"); |
| 7875 | U->set(NewV); |
| Johannes Doerfert | 137c99a | 2020-02-14 20:06:34 -0600 | [diff] [blame] | 7876 | // Do not modify call instructions outside the SCC. |
| 7877 | if (auto *CB = dyn_cast<CallBase>(OldV)) |
| 7878 | if (!Functions.count(CB->getCaller())) |
| 7879 | continue; |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7880 | if (Instruction *I = dyn_cast<Instruction>(OldV)) { |
| 7881 | CGModifiedFunctions.insert(I->getFunction()); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 7882 | if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7883 | isInstructionTriviallyDead(I)) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7884 | DeadInsts.push_back(I); |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7885 | } |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7886 | if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { |
| 7887 | Instruction *UserI = cast<Instruction>(U->getUser()); |
| 7888 | if (isa<UndefValue>(NewV)) { |
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 7889 | ToBeChangedToUnreachableInsts.insert(UserI); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7890 | } else { |
| 7891 | TerminatorsToFold.push_back(UserI); |
| 7892 | } |
| 7893 | } |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7894 | } |
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 7895 | for (auto &V : InvokeWithDeadSuccessor) |
| 7896 | if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) { |
| 7897 | bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); |
| 7898 | bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); |
| 7899 | bool Invoke2CallAllowed = |
| 7900 | !AAIsDeadFunction::mayCatchAsynchronousExceptions( |
| 7901 | *II->getFunction()); |
| 7902 | assert((UnwindBBIsDead || NormalBBIsDead) && |
| 7903 | "Invoke does not have dead successors!"); |
| 7904 | BasicBlock *BB = II->getParent(); |
| 7905 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 7906 | if (UnwindBBIsDead) { |
| 7907 | Instruction *NormalNextIP = &NormalDestBB->front(); |
| 7908 | if (Invoke2CallAllowed) { |
| 7909 | changeToCall(II); |
| 7910 | NormalNextIP = BB->getTerminator(); |
| 7911 | } |
| 7912 | if (NormalBBIsDead) |
| 7913 | ToBeChangedToUnreachableInsts.insert(NormalNextIP); |
| 7914 | } else { |
| 7915 | assert(NormalBBIsDead && "Broken invariant!"); |
| 7916 | if (!NormalDestBB->getUniquePredecessor()) |
| 7917 | NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); |
| 7918 | ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); |
| 7919 | } |
| 7920 | } |
| Stefanos Baziotis | 01c48d7d | 2020-03-07 12:38:44 +0200 | [diff] [blame] | 7921 | for (Instruction *I : TerminatorsToFold) { |
| 7922 | CGModifiedFunctions.insert(I->getFunction()); |
| 7923 | ConstantFoldTerminator(I->getParent()); |
| 7924 | } |
| Johannes Doerfert | 1e46eb7 | 2020-01-07 15:10:30 -0600 | [diff] [blame] | 7925 | for (auto &V : ToBeChangedToUnreachableInsts) |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7926 | if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 7927 | CGModifiedFunctions.insert(I->getFunction()); |
| Johannes Doerfert | 1e46eb7 | 2020-01-07 15:10:30 -0600 | [diff] [blame] | 7928 | changeToUnreachable(I, /* UseLLVMTrap */ false); |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7929 | } |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7930 | |
| Johannes Doerfert | 5429c82 | 2020-01-11 23:30:36 -0600 | [diff] [blame] | 7931 | for (auto &V : ToBeDeletedInsts) { |
| 7932 | if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7933 | CGModifiedFunctions.insert(I->getFunction()); |
| Johannes Doerfert | b4352e4 | 2020-02-13 20:10:59 -0600 | [diff] [blame] | 7934 | if (!I->getType()->isVoidTy()) |
| 7935 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| Johannes Doerfert | 5429c82 | 2020-01-11 23:30:36 -0600 | [diff] [blame] | 7936 | if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) |
| 7937 | DeadInsts.push_back(I); |
| 7938 | else |
| 7939 | I->eraseFromParent(); |
| 7940 | } |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7941 | } |
| 7942 | |
| 7943 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 7944 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7945 | if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { |
| 7946 | SmallVector<BasicBlock *, 8> ToBeDeletedBBs; |
| 7947 | ToBeDeletedBBs.reserve(NumDeadBlocks); |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7948 | for (BasicBlock *BB : ToBeDeletedBlocks) { |
| 7949 | CGModifiedFunctions.insert(BB->getParent()); |
| 7950 | ToBeDeletedBBs.push_back(BB); |
| 7951 | } |
| Johannes Doerfert | 5e442a5 | 2019-10-30 17:34:59 -0500 | [diff] [blame] | 7952 | // Actually we do not delete the blocks but squash them into a single |
| 7953 | // unreachable but untangling branches that jump here is something we need |
| 7954 | // to do in a more generic way. |
| 7955 | DetatchDeadBlocks(ToBeDeletedBBs, nullptr); |
| 7956 | STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); |
| 7957 | BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7958 | } |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 7959 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7960 | // Identify dead internal functions and delete them. This happens outside |
| 7961 | // the other fixpoint analysis as we might treat potentially dead functions |
| 7962 | // as live to lower the number of iterations. If they happen to be dead, the |
| 7963 | // below fixpoint loop will identify and eliminate them. |
| 7964 | SmallVector<Function *, 8> InternalFns; |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7965 | for (Function *F : Functions) |
| 7966 | if (F->hasLocalLinkage()) |
| 7967 | InternalFns.push_back(F); |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7968 | |
| 7969 | bool FoundDeadFn = true; |
| 7970 | while (FoundDeadFn) { |
| 7971 | FoundDeadFn = false; |
| 7972 | for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { |
| 7973 | Function *F = InternalFns[u]; |
| 7974 | if (!F) |
| 7975 | continue; |
| 7976 | |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7977 | bool AllCallSitesKnown; |
| Johannes Doerfert | 6b9ee2d | 2020-01-02 16:53:37 -0600 | [diff] [blame] | 7978 | if (!checkForAllCallSites( |
| 7979 | [this](AbstractCallSite ACS) { |
| 7980 | return ToBeDeletedFunctions.count( |
| 7981 | ACS.getInstruction()->getFunction()); |
| 7982 | }, |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7983 | *F, true, nullptr, AllCallSitesKnown)) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7984 | continue; |
| 7985 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7986 | ToBeDeletedFunctions.insert(F); |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7987 | InternalFns[u] = nullptr; |
| 7988 | FoundDeadFn = true; |
| 7989 | } |
| 7990 | } |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7993 | // Rewrite the functions as requested during manifest. |
| 7994 | ManifestChange = |
| 7995 | ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions); |
| 7996 | |
| 7997 | for (Function *Fn : CGModifiedFunctions) |
| 7998 | CGUpdater.reanalyzeFunction(*Fn); |
| 7999 | |
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 8000 | STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); |
| 8001 | BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size(); |
| 8002 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8003 | for (Function *Fn : ToBeDeletedFunctions) |
| 8004 | CGUpdater.removeFunction(*Fn); |
| Johannes Doerfert | 6b9ee2d | 2020-01-02 16:53:37 -0600 | [diff] [blame] | 8005 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 8006 | if (VerifyMaxFixpointIterations && |
| 8007 | IterationCounter != MaxFixpointIterations) { |
| 8008 | errs() << "\n[Attributor] Fixpoint iteration done after: " |
| 8009 | << IterationCounter << "/" << MaxFixpointIterations |
| 8010 | << " iterations\n"; |
| 8011 | llvm_unreachable("The fixpoint was not reached with exactly the number of " |
| 8012 | "specified iterations!"); |
| 8013 | } |
| 8014 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8015 | return ManifestChange; |
| 8016 | } |
| 8017 | |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8018 | bool Attributor::isValidFunctionSignatureRewrite( |
| 8019 | Argument &Arg, ArrayRef<Type *> ReplacementTypes) { |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8020 | |
| 8021 | auto CallSiteCanBeChanged = [](AbstractCallSite ACS) { |
| 8022 | // Forbid must-tail calls for now. |
| 8023 | return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall(); |
| 8024 | }; |
| 8025 | |
| 8026 | Function *Fn = Arg.getParent(); |
| 8027 | // Avoid var-arg functions for now. |
| 8028 | if (Fn->isVarArg()) { |
| 8029 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); |
| 8030 | return false; |
| 8031 | } |
| 8032 | |
| 8033 | // Avoid functions with complicated argument passing semantics. |
| 8034 | AttributeList FnAttributeList = Fn->getAttributes(); |
| 8035 | if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || |
| 8036 | FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || |
| 8037 | FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) { |
| 8038 | LLVM_DEBUG( |
| 8039 | dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); |
| 8040 | return false; |
| 8041 | } |
| 8042 | |
| 8043 | // Avoid callbacks for now. |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 8044 | bool AllCallSitesKnown; |
| 8045 | if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr, |
| 8046 | AllCallSitesKnown)) { |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8047 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); |
| 8048 | return false; |
| 8049 | } |
| 8050 | |
| 8051 | auto InstPred = [](Instruction &I) { |
| 8052 | if (auto *CI = dyn_cast<CallInst>(&I)) |
| 8053 | return !CI->isMustTailCall(); |
| 8054 | return true; |
| 8055 | }; |
| 8056 | |
| 8057 | // Forbid must-tail calls for now. |
| 8058 | // TODO: |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8059 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 8060 | if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr, |
| 8061 | nullptr, {Instruction::Call})) { |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8062 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); |
| 8063 | return false; |
| 8064 | } |
| 8065 | |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8066 | return true; |
| 8067 | } |
| 8068 | |
| 8069 | bool Attributor::registerFunctionSignatureRewrite( |
| 8070 | Argument &Arg, ArrayRef<Type *> ReplacementTypes, |
| 8071 | ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, |
| 8072 | ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { |
| 8073 | LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " |
| 8074 | << Arg.getParent()->getName() << " with " |
| 8075 | << ReplacementTypes.size() << " replacements\n"); |
| 8076 | assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) && |
| 8077 | "Cannot register an invalid rewrite"); |
| 8078 | |
| 8079 | Function *Fn = Arg.getParent(); |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8080 | SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn]; |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8081 | if (ARIs.empty()) |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8082 | ARIs.resize(Fn->arg_size()); |
| 8083 | |
| 8084 | // If we have a replacement already with less than or equal new arguments, |
| 8085 | // ignore this request. |
| 8086 | ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()]; |
| 8087 | if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { |
| 8088 | LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); |
| 8089 | return false; |
| 8090 | } |
| 8091 | |
| 8092 | // If we have a replacement already but we like the new one better, delete |
| 8093 | // the old. |
| 8094 | if (ARI) |
| 8095 | delete ARI; |
| 8096 | |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8097 | LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " |
| 8098 | << Arg.getParent()->getName() << " with " |
| 8099 | << ReplacementTypes.size() << " replacements\n"); |
| 8100 | |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8101 | // Remember the replacement. |
| 8102 | ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, |
| 8103 | std::move(CalleeRepairCB), |
| 8104 | std::move(ACSRepairCB)); |
| 8105 | |
| 8106 | return true; |
| 8107 | } |
| 8108 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8109 | ChangeStatus Attributor::rewriteFunctionSignatures( |
| 8110 | SmallPtrSetImpl<Function *> &ModifiedFns) { |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8111 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 8112 | |
| 8113 | for (auto &It : ArgumentReplacementMap) { |
| 8114 | Function *OldFn = It.getFirst(); |
| 8115 | |
| 8116 | // Deleted functions do not require rewrites. |
| 8117 | if (ToBeDeletedFunctions.count(OldFn)) |
| 8118 | continue; |
| 8119 | |
| 8120 | const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond(); |
| 8121 | assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); |
| 8122 | |
| 8123 | SmallVector<Type *, 16> NewArgumentTypes; |
| 8124 | SmallVector<AttributeSet, 16> NewArgumentAttributes; |
| 8125 | |
| 8126 | // Collect replacement argument types and copy over existing attributes. |
| 8127 | AttributeList OldFnAttributeList = OldFn->getAttributes(); |
| 8128 | for (Argument &Arg : OldFn->args()) { |
| 8129 | if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) { |
| 8130 | NewArgumentTypes.append(ARI->ReplacementTypes.begin(), |
| 8131 | ARI->ReplacementTypes.end()); |
| 8132 | NewArgumentAttributes.append(ARI->getNumReplacementArgs(), |
| 8133 | AttributeSet()); |
| 8134 | } else { |
| 8135 | NewArgumentTypes.push_back(Arg.getType()); |
| 8136 | NewArgumentAttributes.push_back( |
| 8137 | OldFnAttributeList.getParamAttributes(Arg.getArgNo())); |
| 8138 | } |
| 8139 | } |
| 8140 | |
| 8141 | FunctionType *OldFnTy = OldFn->getFunctionType(); |
| 8142 | Type *RetTy = OldFnTy->getReturnType(); |
| 8143 | |
| 8144 | // Construct the new function type using the new arguments types. |
| 8145 | FunctionType *NewFnTy = |
| 8146 | FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); |
| 8147 | |
| 8148 | LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() |
| 8149 | << "' from " << *OldFn->getFunctionType() << " to " |
| 8150 | << *NewFnTy << "\n"); |
| 8151 | |
| 8152 | // Create the new function body and insert it into the module. |
| 8153 | Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), |
| 8154 | OldFn->getAddressSpace(), ""); |
| 8155 | OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); |
| 8156 | NewFn->takeName(OldFn); |
| 8157 | NewFn->copyAttributesFrom(OldFn); |
| 8158 | |
| 8159 | // Patch the pointer to LLVM function in debug info descriptor. |
| 8160 | NewFn->setSubprogram(OldFn->getSubprogram()); |
| 8161 | OldFn->setSubprogram(nullptr); |
| 8162 | |
| 8163 | // Recompute the parameter attributes list based on the new arguments for |
| 8164 | // the function. |
| 8165 | LLVMContext &Ctx = OldFn->getContext(); |
| 8166 | NewFn->setAttributes(AttributeList::get( |
| 8167 | Ctx, OldFnAttributeList.getFnAttributes(), |
| 8168 | OldFnAttributeList.getRetAttributes(), NewArgumentAttributes)); |
| 8169 | |
| 8170 | // Since we have now created the new function, splice the body of the old |
| 8171 | // function right into the new function, leaving the old rotting hulk of the |
| 8172 | // function empty. |
| 8173 | NewFn->getBasicBlockList().splice(NewFn->begin(), |
| 8174 | OldFn->getBasicBlockList()); |
| 8175 | |
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 8176 | // Set of all "call-like" instructions that invoke the old function mapped |
| 8177 | // to their new replacements. |
| 8178 | SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs; |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8179 | |
| 8180 | // Callback to create a new "call-like" instruction for a given one. |
| 8181 | auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { |
| 8182 | CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); |
| 8183 | const AttributeList &OldCallAttributeList = OldCB->getAttributes(); |
| 8184 | |
| 8185 | // Collect the new argument operands for the replacement call site. |
| 8186 | SmallVector<Value *, 16> NewArgOperands; |
| 8187 | SmallVector<AttributeSet, 16> NewArgOperandAttributes; |
| 8188 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { |
| 8189 | unsigned NewFirstArgNum = NewArgOperands.size(); |
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 8190 | (void)NewFirstArgNum; // only used inside assert. |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8191 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { |
| 8192 | if (ARI->ACSRepairCB) |
| 8193 | ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); |
| 8194 | assert(ARI->getNumReplacementArgs() + NewFirstArgNum == |
| 8195 | NewArgOperands.size() && |
| 8196 | "ACS repair callback did not provide as many operand as new " |
| 8197 | "types were registered!"); |
| 8198 | // TODO: Exose the attribute set to the ACS repair callback |
| 8199 | NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), |
| 8200 | AttributeSet()); |
| 8201 | } else { |
| 8202 | NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); |
| 8203 | NewArgOperandAttributes.push_back( |
| 8204 | OldCallAttributeList.getParamAttributes(OldArgNum)); |
| 8205 | } |
| 8206 | } |
| 8207 | |
| 8208 | assert(NewArgOperands.size() == NewArgOperandAttributes.size() && |
| 8209 | "Mismatch # argument operands vs. # argument operand attributes!"); |
| 8210 | assert(NewArgOperands.size() == NewFn->arg_size() && |
| 8211 | "Mismatch # argument operands vs. # function arguments!"); |
| 8212 | |
| 8213 | SmallVector<OperandBundleDef, 4> OperandBundleDefs; |
| 8214 | OldCB->getOperandBundlesAsDefs(OperandBundleDefs); |
| 8215 | |
| 8216 | // Create a new call or invoke instruction to replace the old one. |
| 8217 | CallBase *NewCB; |
| 8218 | if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { |
| 8219 | NewCB = |
| 8220 | InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), |
| 8221 | NewArgOperands, OperandBundleDefs, "", OldCB); |
| 8222 | } else { |
| 8223 | auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, |
| 8224 | "", OldCB); |
| 8225 | NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); |
| 8226 | NewCB = NewCI; |
| 8227 | } |
| 8228 | |
| 8229 | // Copy over various properties and the new attributes. |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8230 | uint64_t W; |
| 8231 | if (OldCB->extractProfTotalWeight(W)) |
| 8232 | NewCB->setProfWeight(W); |
| 8233 | NewCB->setCallingConv(OldCB->getCallingConv()); |
| 8234 | NewCB->setDebugLoc(OldCB->getDebugLoc()); |
| 8235 | NewCB->takeName(OldCB); |
| 8236 | NewCB->setAttributes(AttributeList::get( |
| 8237 | Ctx, OldCallAttributeList.getFnAttributes(), |
| 8238 | OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes)); |
| 8239 | |
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 8240 | CallSitePairs.push_back({OldCB, NewCB}); |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8241 | return true; |
| 8242 | }; |
| 8243 | |
| 8244 | // Use the CallSiteReplacementCreator to create replacement call sites. |
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 8245 | bool AllCallSitesKnown; |
| 8246 | bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn, |
| 8247 | true, nullptr, AllCallSitesKnown); |
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 8248 | (void)Success; |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8249 | assert(Success && "Assumed call site replacement to succeed!"); |
| 8250 | |
| 8251 | // Rewire the arguments. |
| 8252 | auto OldFnArgIt = OldFn->arg_begin(); |
| 8253 | auto NewFnArgIt = NewFn->arg_begin(); |
| 8254 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); |
| 8255 | ++OldArgNum, ++OldFnArgIt) { |
| 8256 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { |
| 8257 | if (ARI->CalleeRepairCB) |
| 8258 | ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); |
| 8259 | NewFnArgIt += ARI->ReplacementTypes.size(); |
| 8260 | } else { |
| 8261 | NewFnArgIt->takeName(&*OldFnArgIt); |
| 8262 | OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); |
| 8263 | ++NewFnArgIt; |
| 8264 | } |
| 8265 | } |
| 8266 | |
| 8267 | // Eliminate the instructions *after* we visited all of them. |
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 8268 | for (auto &CallSitePair : CallSitePairs) { |
| 8269 | CallBase &OldCB = *CallSitePair.first; |
| 8270 | CallBase &NewCB = *CallSitePair.second; |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8271 | // We do not modify the call graph here but simply reanalyze the old |
| 8272 | // function. This should be revisited once the old PM is gone. |
| 8273 | ModifiedFns.insert(OldCB.getFunction()); |
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 8274 | OldCB.replaceAllUsesWith(&NewCB); |
| 8275 | OldCB.eraseFromParent(); |
| 8276 | } |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8277 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8278 | // Replace the function in the call graph (if any). |
| 8279 | CGUpdater.replaceFunctionWith(*OldFn, *NewFn); |
| 8280 | |
| 8281 | // If the old function was modified and needed to be reanalyzed, the new one |
| 8282 | // does now. |
| 8283 | if (ModifiedFns.erase(OldFn)) |
| 8284 | ModifiedFns.insert(NewFn); |
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 8285 | |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 8286 | Changed = ChangeStatus::CHANGED; |
| 8287 | } |
| 8288 | |
| 8289 | return Changed; |
| 8290 | } |
| 8291 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8292 | void Attributor::initializeInformationCache(Function &F) { |
| 8293 | |
| 8294 | // Walk all instructions to find interesting instructions that might be |
| 8295 | // queried by abstract attributes during their initialization or update. |
| 8296 | // This has to happen before we create attributes. |
| 8297 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; |
| 8298 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; |
| 8299 | |
| 8300 | for (Instruction &I : instructions(&F)) { |
| 8301 | bool IsInterestingOpcode = false; |
| 8302 | |
| 8303 | // To allow easy access to all instructions in a function with a given |
| 8304 | // opcode we store them in the InfoCache. As not all opcodes are interesting |
| 8305 | // to concrete attributes we only cache the ones that are as identified in |
| 8306 | // the following switch. |
| 8307 | // Note: There are no concrete attributes now so this is initially empty. |
| 8308 | switch (I.getOpcode()) { |
| 8309 | default: |
| 8310 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && |
| 8311 | "New call site/base instruction type needs to be known int the " |
| 8312 | "Attributor."); |
| 8313 | break; |
| 8314 | case Instruction::Load: |
| 8315 | // The alignment of a pointer is interesting for loads. |
| 8316 | case Instruction::Store: |
| 8317 | // The alignment of a pointer is interesting for stores. |
| 8318 | case Instruction::Call: |
| 8319 | case Instruction::CallBr: |
| 8320 | case Instruction::Invoke: |
| 8321 | case Instruction::CleanupRet: |
| 8322 | case Instruction::CatchSwitch: |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 8323 | case Instruction::AtomicRMW: |
| 8324 | case Instruction::AtomicCmpXchg: |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 8325 | case Instruction::Br: |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8326 | case Instruction::Resume: |
| 8327 | case Instruction::Ret: |
| 8328 | IsInterestingOpcode = true; |
| 8329 | } |
| 8330 | if (IsInterestingOpcode) |
| 8331 | InstOpcodeMap[I.getOpcode()].push_back(&I); |
| 8332 | if (I.mayReadOrWriteMemory()) |
| 8333 | ReadOrWriteInsts.push_back(&I); |
| 8334 | } |
| Johannes Doerfert | a198adb | 2020-03-13 00:32:38 -0500 | [diff] [blame] | 8335 | |
| 8336 | if (F.hasFnAttribute(Attribute::AlwaysInline) && |
| 8337 | isInlineViable(F).isSuccess()) |
| 8338 | InfoCache.InlineableFunctions.insert(&F); |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 8341 | void Attributor::recordDependence(const AbstractAttribute &FromAA, |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 8342 | const AbstractAttribute &ToAA, |
| 8343 | DepClassTy DepClass) { |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 8344 | if (FromAA.getState().isAtFixpoint()) |
| 8345 | return; |
| 8346 | |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 8347 | if (DepClass == DepClassTy::REQUIRED) |
| 8348 | QueryMap[&FromAA].RequiredAAs.insert( |
| 8349 | const_cast<AbstractAttribute *>(&ToAA)); |
| 8350 | else |
| 8351 | QueryMap[&FromAA].OptionalAAs.insert( |
| 8352 | const_cast<AbstractAttribute *>(&ToAA)); |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 8353 | QueriedNonFixAA = true; |
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 8354 | } |
| 8355 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 8356 | void Attributor::identifyDefaultAbstractAttributes(Function &F) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 8357 | if (!VisitedFunctions.insert(&F).second) |
| 8358 | return; |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 8359 | if (F.isDeclaration()) |
| 8360 | return; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8361 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 8362 | IRPosition FPos = IRPosition::function(F); |
| 8363 | |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 8364 | // Check for dead BasicBlocks in every function. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 8365 | // We need dead instruction detection because we do not want to deal with |
| 8366 | // broken IR in which SSA rules do not apply. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8367 | getOrCreateAAFor<AAIsDead>(FPos); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 8368 | |
| 8369 | // Every function might be "will-return". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8370 | getOrCreateAAFor<AAWillReturn>(FPos); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 8371 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 8372 | // Every function might contain instructions that cause "undefined behavior". |
| 8373 | getOrCreateAAFor<AAUndefinedBehavior>(FPos); |
| 8374 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 8375 | // Every function can be nounwind. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8376 | getOrCreateAAFor<AANoUnwind>(FPos); |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 8377 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 8378 | // Every function might be marked "nosync" |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8379 | getOrCreateAAFor<AANoSync>(FPos); |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 8380 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 8381 | // Every function might be "no-free". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8382 | getOrCreateAAFor<AANoFree>(FPos); |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 8383 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 8384 | // Every function might be "no-return". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8385 | getOrCreateAAFor<AANoReturn>(FPos); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 8386 | |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 8387 | // Every function might be "no-recurse". |
| 8388 | getOrCreateAAFor<AANoRecurse>(FPos); |
| 8389 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 8390 | // Every function might be "readnone/readonly/writeonly/...". |
| 8391 | getOrCreateAAFor<AAMemoryBehavior>(FPos); |
| 8392 | |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 8393 | // Every function can be "readnone/argmemonly/inaccessiblememonly/...". |
| 8394 | getOrCreateAAFor<AAMemoryLocation>(FPos); |
| 8395 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8396 | // Every function might be applicable for Heap-To-Stack conversion. |
| 8397 | if (EnableHeapToStack) |
| 8398 | getOrCreateAAFor<AAHeapToStack>(FPos); |
| 8399 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 8400 | // Return attributes are only appropriate if the return type is non void. |
| 8401 | Type *ReturnType = F.getReturnType(); |
| 8402 | if (!ReturnType->isVoidTy()) { |
| 8403 | // Argument attribute "returned" --- Create only one per function even |
| 8404 | // though it is an argument attribute. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8405 | getOrCreateAAFor<AAReturnedValues>(FPos); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 8406 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8407 | IRPosition RetPos = IRPosition::returned(F); |
| 8408 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 8409 | // Every returned value might be dead. |
| 8410 | getOrCreateAAFor<AAIsDead>(RetPos); |
| 8411 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8412 | // Every function might be simplified. |
| 8413 | getOrCreateAAFor<AAValueSimplify>(RetPos); |
| 8414 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 8415 | if (ReturnType->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 8416 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 8417 | // Every function with pointer return type might be marked align. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8418 | getOrCreateAAFor<AAAlign>(RetPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 8419 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 8420 | // Every function with pointer return type might be marked nonnull. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8421 | getOrCreateAAFor<AANonNull>(RetPos); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 8422 | |
| 8423 | // Every function with pointer return type might be marked noalias. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8424 | getOrCreateAAFor<AANoAlias>(RetPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8425 | |
| 8426 | // Every function with pointer return type might be marked |
| 8427 | // dereferenceable. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8428 | getOrCreateAAFor<AADereferenceable>(RetPos); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 8429 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 8430 | } |
| 8431 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 8432 | for (Argument &Arg : F.args()) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8433 | IRPosition ArgPos = IRPosition::argument(Arg); |
| 8434 | |
| 8435 | // Every argument might be simplified. |
| 8436 | getOrCreateAAFor<AAValueSimplify>(ArgPos); |
| 8437 | |
| Johannes Doerfert | 1e99fc9 | 2020-02-16 16:42:47 -0600 | [diff] [blame] | 8438 | // Every argument might be dead. |
| 8439 | getOrCreateAAFor<AAIsDead>(ArgPos); |
| 8440 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8441 | if (Arg.getType()->isPointerTy()) { |
| 8442 | // Every argument with pointer type might be marked nonnull. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8443 | getOrCreateAAFor<AANonNull>(ArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8444 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 8445 | // Every argument with pointer type might be marked noalias. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8446 | getOrCreateAAFor<AANoAlias>(ArgPos); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 8447 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8448 | // Every argument with pointer type might be marked dereferenceable. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8449 | getOrCreateAAFor<AADereferenceable>(ArgPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 8450 | |
| 8451 | // Every argument with pointer type might be marked align. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8452 | getOrCreateAAFor<AAAlign>(ArgPos); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 8453 | |
| 8454 | // Every argument with pointer type might be marked nocapture. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8455 | getOrCreateAAFor<AANoCapture>(ArgPos); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 8456 | |
| 8457 | // Every argument with pointer type might be marked |
| 8458 | // "readnone/readonly/writeonly/..." |
| 8459 | getOrCreateAAFor<AAMemoryBehavior>(ArgPos); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 8460 | |
| 8461 | // Every argument with pointer type might be marked nofree. |
| 8462 | getOrCreateAAFor<AANoFree>(ArgPos); |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8463 | |
| 8464 | // Every argument with pointer type might be privatizable (or promotable) |
| 8465 | getOrCreateAAFor<AAPrivatizablePtr>(ArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8466 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 8467 | } |
| 8468 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8469 | auto CallSitePred = [&](Instruction &I) -> bool { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 8470 | CallSite CS(&I); |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 8471 | IRPosition CSRetPos = IRPosition::callsite_returned(CS); |
| 8472 | |
| 8473 | // Call sites might be dead if they do not have side effects and no live |
| 8474 | // users. The return value might be dead if there are no live users. |
| 8475 | getOrCreateAAFor<AAIsDead>(CSRetPos); |
| 8476 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 8477 | if (Function *Callee = CS.getCalledFunction()) { |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 8478 | // Skip declerations except if annotations on their call sites were |
| 8479 | // explicitly requested. |
| Johannes Doerfert | 139c9ef | 2019-12-13 23:41:02 -0600 | [diff] [blame] | 8480 | if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && |
| 8481 | !Callee->hasMetadata(LLVMContext::MD_callback)) |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 8482 | return true; |
| 8483 | |
| 8484 | if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8485 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 8486 | IRPosition CSRetPos = IRPosition::callsite_returned(CS); |
| 8487 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8488 | // Call site return integer values might be limited by a constant range. |
| Johannes Doerfert | 86509e8 | 2020-01-12 00:34:38 -0600 | [diff] [blame] | 8489 | if (Callee->getReturnType()->isIntegerTy()) |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8490 | getOrCreateAAFor<AAValueConstantRange>(CSRetPos); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 8491 | } |
| 8492 | |
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 8493 | for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8494 | |
| 8495 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); |
| 8496 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 8497 | // Every call site argument might be dead. |
| 8498 | getOrCreateAAFor<AAIsDead>(CSArgPos); |
| 8499 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8500 | // Call site argument might be simplified. |
| 8501 | getOrCreateAAFor<AAValueSimplify>(CSArgPos); |
| 8502 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 8503 | if (!CS.getArgument(i)->getType()->isPointerTy()) |
| 8504 | continue; |
| 8505 | |
| 8506 | // Call site argument attribute "non-null". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8507 | getOrCreateAAFor<AANonNull>(CSArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8508 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 8509 | // Call site argument attribute "no-alias". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8510 | getOrCreateAAFor<AANoAlias>(CSArgPos); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 8511 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 8512 | // Call site argument attribute "dereferenceable". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8513 | getOrCreateAAFor<AADereferenceable>(CSArgPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 8514 | |
| 8515 | // Call site argument attribute "align". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 8516 | getOrCreateAAFor<AAAlign>(CSArgPos); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 8517 | |
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 8518 | // Call site argument attribute |
| 8519 | // "readnone/readonly/writeonly/..." |
| 8520 | getOrCreateAAFor<AAMemoryBehavior>(CSArgPos); |
| 8521 | |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 8522 | // Call site argument attribute "nofree". |
| 8523 | getOrCreateAAFor<AANoFree>(CSArgPos); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 8524 | } |
| 8525 | } |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8526 | return true; |
| 8527 | }; |
| 8528 | |
| 8529 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 8530 | bool Success; |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8531 | Success = checkForAllInstructionsImpl( |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 8532 | nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr, |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8533 | {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 8534 | (unsigned)Instruction::Call}); |
| 8535 | (void)Success; |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 8536 | assert(Success && "Expected the check call to be successful!"); |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8537 | |
| 8538 | auto LoadStorePred = [&](Instruction &I) -> bool { |
| 8539 | if (isa<LoadInst>(I)) |
| 8540 | getOrCreateAAFor<AAAlign>( |
| 8541 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); |
| 8542 | else |
| 8543 | getOrCreateAAFor<AAAlign>( |
| 8544 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); |
| 8545 | return true; |
| 8546 | }; |
| 8547 | Success = checkForAllInstructionsImpl( |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 8548 | nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr, |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8549 | {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); |
| 8550 | (void)Success; |
| Johannes Doerfert | 23f41f1 | 2020-01-12 01:09:22 -0600 | [diff] [blame] | 8551 | assert(Success && "Expected the check call to be successful!"); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8552 | } |
| 8553 | |
| 8554 | /// Helpers to ease debugging through output streams and print calls. |
| 8555 | /// |
| 8556 | ///{ |
| 8557 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { |
| 8558 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); |
| 8559 | } |
| 8560 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 8561 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8562 | switch (AP) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 8563 | case IRPosition::IRP_INVALID: |
| 8564 | return OS << "inv"; |
| 8565 | case IRPosition::IRP_FLOAT: |
| 8566 | return OS << "flt"; |
| 8567 | case IRPosition::IRP_RETURNED: |
| 8568 | return OS << "fn_ret"; |
| 8569 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 8570 | return OS << "cs_ret"; |
| 8571 | case IRPosition::IRP_FUNCTION: |
| 8572 | return OS << "fn"; |
| 8573 | case IRPosition::IRP_CALL_SITE: |
| 8574 | return OS << "cs"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 8575 | case IRPosition::IRP_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8576 | return OS << "arg"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 8577 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8578 | return OS << "cs_arg"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8579 | } |
| 8580 | llvm_unreachable("Unknown attribute position!"); |
| 8581 | } |
| 8582 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 8583 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 8584 | const Value &AV = Pos.getAssociatedValue(); |
| 8585 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 8586 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; |
| 8587 | } |
| 8588 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 8589 | template <typename base_ty, base_ty BestState, base_ty WorstState> |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8590 | raw_ostream & |
| 8591 | llvm::operator<<(raw_ostream &OS, |
| 8592 | const IntegerStateBase<base_ty, BestState, WorstState> &S) { |
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 8593 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" |
| 8594 | << static_cast<const AbstractState &>(S); |
| 8595 | } |
| 8596 | |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8597 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) { |
| 8598 | OS << "range-state(" << S.getBitWidth() << ")<"; |
| 8599 | S.getKnown().print(OS); |
| 8600 | OS << " / "; |
| 8601 | S.getAssumed().print(OS); |
| 8602 | OS << ">"; |
| 8603 | |
| 8604 | return OS << static_cast<const AbstractState &>(S); |
| 8605 | } |
| 8606 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8607 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { |
| 8608 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); |
| 8609 | } |
| 8610 | |
| 8611 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { |
| 8612 | AA.print(OS); |
| 8613 | return OS; |
| 8614 | } |
| 8615 | |
| 8616 | void AbstractAttribute::print(raw_ostream &OS) const { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 8617 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() |
| 8618 | << "]"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8619 | } |
| 8620 | ///} |
| 8621 | |
| 8622 | /// ---------------------------------------------------------------------------- |
| 8623 | /// Pass (Manager) Boilerplate |
| 8624 | /// ---------------------------------------------------------------------------- |
| 8625 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8626 | static bool runAttributorOnFunctions(InformationCache &InfoCache, |
| 8627 | SetVector<Function *> &Functions, |
| 8628 | AnalysisGetter &AG, |
| 8629 | CallGraphUpdater &CGUpdater) { |
| 8630 | if (DisableAttributor || Functions.empty()) |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8631 | return false; |
| 8632 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8633 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size() |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8634 | << " functions.\n"); |
| 8635 | |
| 8636 | // Create an Attributor and initially empty information cache that is filled |
| 8637 | // while we identify default attribute opportunities. |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8638 | Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8639 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8640 | for (Function *F : Functions) |
| 8641 | A.initializeInformationCache(*F); |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 8642 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8643 | for (Function *F : Functions) { |
| 8644 | if (F->hasExactDefinition()) |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 8645 | NumFnWithExactDefinition++; |
| 8646 | else |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8647 | NumFnWithoutExactDefinition++; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8648 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 8649 | // We look at internal functions only on-demand but if any use is not a |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8650 | // direct call or outside the current set of analyzed functions, we have to |
| 8651 | // do it eagerly. |
| 8652 | if (F->hasLocalLinkage()) { |
| 8653 | if (llvm::all_of(F->uses(), [&Functions](const Use &U) { |
| 8654 | ImmutableCallSite ICS(U.getUser()); |
| 8655 | return ICS && ICS.isCallee(&U) && |
| 8656 | Functions.count(const_cast<Function *>(ICS.getCaller())); |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 8657 | })) |
| 8658 | continue; |
| 8659 | } |
| 8660 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8661 | // Populate the Attributor with abstract attribute opportunities in the |
| 8662 | // function and the information cache with IR information. |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8663 | A.identifyDefaultAbstractAttributes(*F); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8664 | } |
| 8665 | |
| Johannes Doerfert | 0995001 | 2020-03-16 20:18:07 -0500 | [diff] [blame] | 8666 | Module &M = *Functions.front()->getParent(); |
| Johannes Doerfert | 77a9e61 | 2020-01-11 23:36:17 -0600 | [diff] [blame] | 8667 | ChangeStatus Changed = A.run(); |
| Johannes Doerfert | 0995001 | 2020-03-16 20:18:07 -0500 | [diff] [blame] | 8668 | assert(!verifyModule(M, &errs()) && "Module verification failed!"); |
| Johannes Doerfert | 77a9e61 | 2020-01-11 23:36:17 -0600 | [diff] [blame] | 8669 | LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size() |
| 8670 | << " functions, result: " << Changed << ".\n"); |
| 8671 | return Changed == ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8672 | } |
| 8673 | |
| 8674 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8675 | FunctionAnalysisManager &FAM = |
| 8676 | AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 8677 | AnalysisGetter AG(FAM); |
| 8678 | |
| 8679 | SetVector<Function *> Functions; |
| 8680 | for (Function &F : M) |
| 8681 | Functions.insert(&F); |
| 8682 | |
| 8683 | CallGraphUpdater CGUpdater; |
| 8684 | InformationCache InfoCache(M, AG, /* CGSCC */ nullptr); |
| 8685 | if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) { |
| 8686 | // FIXME: Think about passes we will preserve and add them here. |
| 8687 | return PreservedAnalyses::none(); |
| 8688 | } |
| 8689 | return PreservedAnalyses::all(); |
| 8690 | } |
| 8691 | |
| 8692 | PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C, |
| 8693 | CGSCCAnalysisManager &AM, |
| 8694 | LazyCallGraph &CG, |
| 8695 | CGSCCUpdateResult &UR) { |
| 8696 | FunctionAnalysisManager &FAM = |
| 8697 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); |
| 8698 | AnalysisGetter AG(FAM); |
| 8699 | |
| 8700 | SetVector<Function *> Functions; |
| 8701 | for (LazyCallGraph::Node &N : C) |
| 8702 | Functions.insert(&N.getFunction()); |
| 8703 | |
| 8704 | if (Functions.empty()) |
| 8705 | return PreservedAnalyses::all(); |
| 8706 | |
| 8707 | Module &M = *Functions.back()->getParent(); |
| 8708 | CallGraphUpdater CGUpdater; |
| 8709 | CGUpdater.initialize(CG, C, AM, UR); |
| 8710 | InformationCache InfoCache(M, AG, /* CGSCC */ &Functions); |
| 8711 | if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8712 | // FIXME: Think about passes we will preserve and add them here. |
| 8713 | return PreservedAnalyses::none(); |
| 8714 | } |
| 8715 | return PreservedAnalyses::all(); |
| 8716 | } |
| 8717 | |
| 8718 | namespace { |
| 8719 | |
| 8720 | struct AttributorLegacyPass : public ModulePass { |
| 8721 | static char ID; |
| 8722 | |
| 8723 | AttributorLegacyPass() : ModulePass(ID) { |
| 8724 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 8725 | } |
| 8726 | |
| 8727 | bool runOnModule(Module &M) override { |
| 8728 | if (skipModule(M)) |
| 8729 | return false; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8730 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 8731 | AnalysisGetter AG; |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8732 | SetVector<Function *> Functions; |
| 8733 | for (Function &F : M) |
| 8734 | Functions.insert(&F); |
| 8735 | |
| 8736 | CallGraphUpdater CGUpdater; |
| 8737 | InformationCache InfoCache(M, AG, /* CGSCC */ nullptr); |
| 8738 | return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8739 | } |
| 8740 | |
| 8741 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 8742 | // FIXME: Think about passes we will preserve and add them here. |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8743 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8744 | } |
| 8745 | }; |
| 8746 | |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8747 | struct AttributorCGSCCLegacyPass : public CallGraphSCCPass { |
| 8748 | CallGraphUpdater CGUpdater; |
| 8749 | static char ID; |
| 8750 | |
| 8751 | AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) { |
| 8752 | initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 8753 | } |
| 8754 | |
| 8755 | bool runOnSCC(CallGraphSCC &SCC) override { |
| 8756 | if (skipSCC(SCC)) |
| 8757 | return false; |
| 8758 | |
| 8759 | SetVector<Function *> Functions; |
| 8760 | for (CallGraphNode *CGN : SCC) |
| 8761 | if (Function *Fn = CGN->getFunction()) |
| 8762 | if (!Fn->isDeclaration()) |
| 8763 | Functions.insert(Fn); |
| 8764 | |
| 8765 | if (Functions.empty()) |
| 8766 | return false; |
| 8767 | |
| 8768 | AnalysisGetter AG; |
| 8769 | CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph()); |
| 8770 | CGUpdater.initialize(CG, SCC); |
| 8771 | Module &M = *Functions.back()->getParent(); |
| 8772 | InformationCache InfoCache(M, AG, /* CGSCC */ &Functions); |
| 8773 | return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater); |
| 8774 | } |
| 8775 | |
| 8776 | bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); } |
| 8777 | |
| 8778 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 8779 | // FIXME: Think about passes we will preserve and add them here. |
| 8780 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 8781 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 8782 | } |
| 8783 | }; |
| 8784 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8785 | } // end anonymous namespace |
| 8786 | |
| 8787 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8788 | Pass *llvm::createAttributorCGSCCLegacyPass() { |
| 8789 | return new AttributorCGSCCLegacyPass(); |
| 8790 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8791 | |
| 8792 | char AttributorLegacyPass::ID = 0; |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8793 | char AttributorCGSCCLegacyPass::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 8794 | |
| 8795 | const char AAReturnedValues::ID = 0; |
| 8796 | const char AANoUnwind::ID = 0; |
| 8797 | const char AANoSync::ID = 0; |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 8798 | const char AANoFree::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 8799 | const char AANonNull::ID = 0; |
| 8800 | const char AANoRecurse::ID = 0; |
| 8801 | const char AAWillReturn::ID = 0; |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 8802 | const char AAUndefinedBehavior::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 8803 | const char AANoAlias::ID = 0; |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 8804 | const char AAReachability::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 8805 | const char AANoReturn::ID = 0; |
| 8806 | const char AAIsDead::ID = 0; |
| 8807 | const char AADereferenceable::ID = 0; |
| 8808 | const char AAAlign::ID = 0; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 8809 | const char AANoCapture::ID = 0; |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8810 | const char AAValueSimplify::ID = 0; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8811 | const char AAHeapToStack::ID = 0; |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8812 | const char AAPrivatizablePtr::ID = 0; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 8813 | const char AAMemoryBehavior::ID = 0; |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 8814 | const char AAMemoryLocation::ID = 0; |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8815 | const char AAValueConstantRange::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 8816 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8817 | // Macro magic to create the static generator function for attributes that |
| 8818 | // follow the naming scheme. |
| 8819 | |
| 8820 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ |
| 8821 | case IRPosition::PK: \ |
| 8822 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); |
| 8823 | |
| 8824 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ |
| 8825 | case IRPosition::PK: \ |
| 8826 | AA = new CLASS##SUFFIX(IRP); \ |
| 8827 | break; |
| 8828 | |
| 8829 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 8830 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 8831 | CLASS *AA = nullptr; \ |
| 8832 | switch (IRP.getPositionKind()) { \ |
| 8833 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 8834 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 8835 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 8836 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 8837 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 8838 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 8839 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 8840 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 8841 | } \ |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8842 | return *AA; \ |
| 8843 | } |
| 8844 | |
| 8845 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 8846 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 8847 | CLASS *AA = nullptr; \ |
| 8848 | switch (IRP.getPositionKind()) { \ |
| 8849 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 8850 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ |
| 8851 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 8852 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 8853 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 8854 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 8855 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 8856 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 8857 | } \ |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8858 | return *AA; \ |
| 8859 | } |
| 8860 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8861 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 8862 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 8863 | CLASS *AA = nullptr; \ |
| 8864 | switch (IRP.getPositionKind()) { \ |
| 8865 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 8866 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 8867 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 8868 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 8869 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 8870 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 8871 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 8872 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 8873 | } \ |
| 8874 | return *AA; \ |
| 8875 | } |
| 8876 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8877 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 8878 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 8879 | CLASS *AA = nullptr; \ |
| 8880 | switch (IRP.getPositionKind()) { \ |
| 8881 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 8882 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 8883 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 8884 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 8885 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 8886 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 8887 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 8888 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 8889 | } \ |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8890 | return *AA; \ |
| 8891 | } |
| 8892 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 8893 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 8894 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 8895 | CLASS *AA = nullptr; \ |
| 8896 | switch (IRP.getPositionKind()) { \ |
| 8897 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 8898 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 8899 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 8900 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 8901 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 8902 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 8903 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 8904 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 8905 | } \ |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 8906 | return *AA; \ |
| 8907 | } |
| 8908 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8909 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) |
| 8910 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8911 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) |
| 8912 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) |
| 8913 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8914 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) |
| Johannes Doerfert | 282f5d7 | 2020-01-26 02:51:57 -0600 | [diff] [blame] | 8915 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8916 | |
| 8917 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) |
| 8918 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) |
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 8919 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8920 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) |
| 8921 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 8922 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) |
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 8923 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8924 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8925 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 8926 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 8927 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8928 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8929 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 8930 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 8931 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8932 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 8933 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) |
| 8934 | |
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 8935 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8936 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 8937 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8938 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 8939 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 8940 | #undef SWITCH_PK_CREATE |
| 8941 | #undef SWITCH_PK_INV |
| 8942 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8943 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", |
| 8944 | "Deduce and propagate attributes", false, false) |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 8945 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 8946 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", |
| 8947 | "Deduce and propagate attributes", false, false) |
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 8948 | INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc", |
| 8949 | "Deduce and propagate attributes (CGSCC pass)", false, |
| 8950 | false) |
| 8951 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 8952 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
| 8953 | INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc", |
| 8954 | "Deduce and propagate attributes (CGSCC pass)", false, |
| 8955 | false) |