| 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" | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 31 | #include "llvm/Analysis/ScalarEvolution.h" | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/ValueTracking.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Argument.h" | 
|  | 34 | #include "llvm/IR/Attributes.h" | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 35 | #include "llvm/IR/CFG.h" | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 36 | #include "llvm/IR/IRBuilder.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 37 | #include "llvm/IR/InstIterator.h" | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 38 | #include "llvm/IR/IntrinsicInst.h" | 
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 39 | #include "llvm/IR/Verifier.h" | 
| Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 40 | #include "llvm/InitializePasses.h" | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 41 | #include "llvm/IR/NoFolder.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" | 
|  | 43 | #include "llvm/Support/Debug.h" | 
|  | 44 | #include "llvm/Support/raw_ostream.h" | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 45 | #include "llvm/Transforms/IPO/ArgumentPromotion.h" | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | 
|  | 47 | #include "llvm/Transforms/Utils/Local.h" | 
|  | 48 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 49 | #include <cassert> | 
|  | 50 |  | 
|  | 51 | using namespace llvm; | 
|  | 52 |  | 
|  | 53 | #define DEBUG_TYPE "attributor" | 
|  | 54 |  | 
|  | 55 | STATISTIC(NumFnWithExactDefinition, | 
|  | 56 | "Number of function with exact definitions"); | 
|  | 57 | STATISTIC(NumFnWithoutExactDefinition, | 
|  | 58 | "Number of function without exact definitions"); | 
|  | 59 | STATISTIC(NumAttributesTimedOut, | 
|  | 60 | "Number of abstract attributes timed out before fixpoint"); | 
|  | 61 | STATISTIC(NumAttributesValidFixpoint, | 
|  | 62 | "Number of abstract attributes in a valid fixpoint state"); | 
|  | 63 | STATISTIC(NumAttributesManifested, | 
|  | 64 | "Number of abstract attributes manifested in IR"); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 65 | STATISTIC(NumAttributesFixedDueToRequiredDependences, | 
|  | 66 | "Number of abstract attributes fixed due to required dependences"); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 67 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 68 | // Some helper macros to deal with statistics tracking. | 
|  | 69 | // | 
|  | 70 | // Usage: | 
|  | 71 | // For simple IR attribute tracking overload trackStatistics in the abstract | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 72 | // attribute and choose the right STATS_DECLTRACK_********* macro, | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 73 | // e.g.,: | 
|  | 74 | //  void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 75 | //    STATS_DECLTRACK_ARG_ATTR(returned) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 76 | //  } | 
|  | 77 | // If there is a single "increment" side one can use the macro | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 78 | // STATS_DECLTRACK with a custom message. If there are multiple increment | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 79 | // sides, STATS_DECL and STATS_TRACK can also be used separatly. | 
|  | 80 | // | 
|  | 81 | #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \ | 
|  | 82 | ("Number of " #TYPE " marked '" #NAME "'") | 
|  | 83 | #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME | 
| Johannes Doerfert | a7a3b3a | 2019-09-04 19:01:08 +0000 | [diff] [blame] | 84 | #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG); | 
|  | 85 | #define STATS_DECL(NAME, TYPE, MSG)                                            \ | 
|  | 86 | STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG); | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 87 | #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 88 | #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \ | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 89 | {                                                                            \ | 
|  | 90 | STATS_DECL(NAME, TYPE, MSG)                                                \ | 
|  | 91 | STATS_TRACK(NAME, TYPE)                                                    \ | 
|  | 92 | } | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 93 | #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \ | 
|  | 94 | STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) | 
|  | 95 | #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \ | 
|  | 96 | STATS_DECLTRACK(NAME, CSArguments,                                           \ | 
|  | 97 | BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) | 
|  | 98 | #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \ | 
|  | 99 | STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) | 
|  | 100 | #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \ | 
|  | 101 | STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) | 
|  | 102 | #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \ | 
|  | 103 | STATS_DECLTRACK(NAME, FunctionReturn,                                        \ | 
| Johannes Doerfert | 2db8528 | 2019-08-21 20:56:56 +0000 | [diff] [blame] | 104 | BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 105 | #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \ | 
|  | 106 | STATS_DECLTRACK(NAME, CSReturn,                                              \ | 
|  | 107 | BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) | 
|  | 108 | #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \ | 
|  | 109 | STATS_DECLTRACK(NAME, Floating,                                              \ | 
|  | 110 | ("Number of floating values known to be '" #NAME "'")) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 111 |  | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 112 | // Specialization of the operator<< for abstract attributes subclasses. This | 
|  | 113 | // disambiguates situations where multiple operators are applicable. | 
|  | 114 | namespace llvm { | 
|  | 115 | #define PIPE_OPERATOR(CLASS)                                                   \ | 
|  | 116 | raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) {                  \ | 
|  | 117 | return OS << static_cast<const AbstractAttribute &>(AA);                   \ | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | PIPE_OPERATOR(AAIsDead) | 
|  | 121 | PIPE_OPERATOR(AANoUnwind) | 
|  | 122 | PIPE_OPERATOR(AANoSync) | 
|  | 123 | PIPE_OPERATOR(AANoRecurse) | 
|  | 124 | PIPE_OPERATOR(AAWillReturn) | 
|  | 125 | PIPE_OPERATOR(AANoReturn) | 
|  | 126 | PIPE_OPERATOR(AAReturnedValues) | 
|  | 127 | PIPE_OPERATOR(AANonNull) | 
|  | 128 | PIPE_OPERATOR(AANoAlias) | 
|  | 129 | PIPE_OPERATOR(AADereferenceable) | 
|  | 130 | PIPE_OPERATOR(AAAlign) | 
|  | 131 | PIPE_OPERATOR(AANoCapture) | 
|  | 132 | PIPE_OPERATOR(AAValueSimplify) | 
|  | 133 | PIPE_OPERATOR(AANoFree) | 
|  | 134 | PIPE_OPERATOR(AAHeapToStack) | 
|  | 135 | PIPE_OPERATOR(AAReachability) | 
|  | 136 | PIPE_OPERATOR(AAMemoryBehavior) | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 137 | PIPE_OPERATOR(AAValueConstantRange) | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 138 | PIPE_OPERATOR(AAPrivatizablePtr) | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 139 |  | 
|  | 140 | #undef PIPE_OPERATOR | 
|  | 141 | } // namespace llvm | 
|  | 142 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 143 | // TODO: Determine a good default value. | 
|  | 144 | // | 
|  | 145 | // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads | 
|  | 146 | // (when run with the first 5 abstract attributes). The results also indicate | 
|  | 147 | // that we never reach 32 iterations but always find a fixpoint sooner. | 
|  | 148 | // | 
|  | 149 | // This will become more evolved once we perform two interleaved fixpoint | 
|  | 150 | // iterations: bottom-up and top-down. | 
|  | 151 | static cl::opt<unsigned> | 
|  | 152 | MaxFixpointIterations("attributor-max-iterations", cl::Hidden, | 
|  | 153 | cl::desc("Maximal number of fixpoint iterations."), | 
|  | 154 | cl::init(32)); | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 155 | static cl::opt<bool> VerifyMaxFixpointIterations( | 
|  | 156 | "attributor-max-iterations-verify", cl::Hidden, | 
|  | 157 | cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), | 
|  | 158 | cl::init(false)); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 159 |  | 
|  | 160 | static cl::opt<bool> DisableAttributor( | 
|  | 161 | "attributor-disable", cl::Hidden, | 
|  | 162 | cl::desc("Disable the attributor inter-procedural deduction pass."), | 
| Johannes Doerfert | 282d34e | 2019-06-14 14:53:41 +0000 | [diff] [blame] | 163 | cl::init(true)); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 164 |  | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 165 | static cl::opt<bool> AnnotateDeclarationCallSites( | 
|  | 166 | "attributor-annotate-decl-cs", cl::Hidden, | 
| James Henderson | d68904f | 2020-01-06 10:15:44 +0000 | [diff] [blame] | 167 | cl::desc("Annotate call sites of function declarations."), cl::init(false)); | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 168 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 169 | static cl::opt<bool> ManifestInternal( | 
|  | 170 | "attributor-manifest-internal", cl::Hidden, | 
|  | 171 | cl::desc("Manifest Attributor internal string attributes."), | 
|  | 172 | cl::init(false)); | 
|  | 173 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 174 | static cl::opt<unsigned> DepRecInterval( | 
|  | 175 | "attributor-dependence-recompute-interval", cl::Hidden, | 
|  | 176 | cl::desc("Number of iterations until dependences are recomputed."), | 
|  | 177 | cl::init(4)); | 
|  | 178 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 179 | static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", | 
|  | 180 | cl::init(true), cl::Hidden); | 
|  | 181 |  | 
| Johannes Doerfert | 1c2afae | 2019-10-10 05:34:21 +0000 | [diff] [blame] | 182 | static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), | 
|  | 183 | cl::Hidden); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 184 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 185 | /// Logic operators for the change status enum class. | 
|  | 186 | /// | 
|  | 187 | ///{ | 
|  | 188 | ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { | 
|  | 189 | return l == ChangeStatus::CHANGED ? l : r; | 
|  | 190 | } | 
|  | 191 | ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { | 
|  | 192 | return l == ChangeStatus::UNCHANGED ? l : r; | 
|  | 193 | } | 
|  | 194 | ///} | 
|  | 195 |  | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 196 | Argument *IRPosition::getAssociatedArgument() const { | 
|  | 197 | if (getPositionKind() == IRP_ARGUMENT) | 
|  | 198 | return cast<Argument>(&getAnchorValue()); | 
|  | 199 |  | 
|  | 200 | // Not an Argument and no argument number means this is not a call site | 
|  | 201 | // argument, thus we cannot find a callback argument to return. | 
|  | 202 | int ArgNo = getArgNo(); | 
|  | 203 | if (ArgNo < 0) | 
|  | 204 | return nullptr; | 
|  | 205 |  | 
|  | 206 | // Use abstract call sites to make the connection between the call site | 
|  | 207 | // values and the ones in callbacks. If a callback was found that makes use | 
|  | 208 | // of the underlying call site operand, we want the corresponding callback | 
|  | 209 | // callee argument and not the direct callee argument. | 
|  | 210 | Optional<Argument *> CBCandidateArg; | 
|  | 211 | SmallVector<const Use *, 4> CBUses; | 
|  | 212 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 213 | AbstractCallSite::getCallbackUses(ICS, CBUses); | 
|  | 214 | for (const Use *U : CBUses) { | 
|  | 215 | AbstractCallSite ACS(U); | 
|  | 216 | assert(ACS && ACS.isCallbackCall()); | 
|  | 217 | if (!ACS.getCalledFunction()) | 
|  | 218 | continue; | 
|  | 219 |  | 
|  | 220 | for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) { | 
|  | 221 |  | 
|  | 222 | // Test if the underlying call site operand is argument number u of the | 
|  | 223 | // callback callee. | 
|  | 224 | if (ACS.getCallArgOperandNo(u) != ArgNo) | 
|  | 225 | continue; | 
|  | 226 |  | 
|  | 227 | assert(ACS.getCalledFunction()->arg_size() > u && | 
|  | 228 | "ACS mapped into var-args arguments!"); | 
|  | 229 | if (CBCandidateArg.hasValue()) { | 
|  | 230 | CBCandidateArg = nullptr; | 
|  | 231 | break; | 
|  | 232 | } | 
|  | 233 | CBCandidateArg = ACS.getCalledFunction()->getArg(u); | 
|  | 234 | } | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | // If we found a unique callback candidate argument, return it. | 
|  | 238 | if (CBCandidateArg.hasValue() && CBCandidateArg.getValue()) | 
|  | 239 | return CBCandidateArg.getValue(); | 
|  | 240 |  | 
|  | 241 | // If no callbacks were found, or none used the underlying call site operand | 
|  | 242 | // exclusively, use the direct callee argument if available. | 
|  | 243 | const Function *Callee = ICS.getCalledFunction(); | 
|  | 244 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) | 
|  | 245 | return Callee->getArg(ArgNo); | 
|  | 246 |  | 
|  | 247 | return nullptr; | 
|  | 248 | } | 
|  | 249 |  | 
| Johannes Doerfert | 214ed3f | 2020-01-11 23:35:45 -0600 | [diff] [blame] | 250 | static Optional<ConstantInt *> | 
|  | 251 | getAssumedConstant(Attributor &A, const Value &V, const AbstractAttribute &AA, | 
|  | 252 | bool &UsedAssumedInformation) { | 
|  | 253 | const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>( | 
|  | 254 | AA, IRPosition::value(V), /* TrackDependence */ false); | 
|  | 255 | Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); | 
|  | 256 | bool IsKnown = ValueSimplifyAA.isKnown(); | 
|  | 257 | UsedAssumedInformation |= !IsKnown; | 
|  | 258 | if (!SimplifiedV.hasValue()) { | 
|  | 259 | A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); | 
|  | 260 | return llvm::None; | 
|  | 261 | } | 
|  | 262 | if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) { | 
|  | 263 | A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); | 
|  | 264 | return llvm::None; | 
|  | 265 | } | 
|  | 266 | ConstantInt *CI = dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue()); | 
|  | 267 | if (CI) | 
|  | 268 | A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); | 
|  | 269 | return CI; | 
|  | 270 | } | 
|  | 271 |  | 
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 272 | /// Get pointer operand of memory accessing instruction. If \p I is | 
|  | 273 | /// not a memory accessing instruction, return nullptr. If \p AllowVolatile, | 
|  | 274 | /// is set to false and the instruction is volatile, return nullptr. | 
|  | 275 | static const Value *getPointerOperand(const Instruction *I, | 
|  | 276 | bool AllowVolatile) { | 
|  | 277 | if (auto *LI = dyn_cast<LoadInst>(I)) { | 
|  | 278 | if (!AllowVolatile && LI->isVolatile()) | 
|  | 279 | return nullptr; | 
|  | 280 | return LI->getPointerOperand(); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | if (auto *SI = dyn_cast<StoreInst>(I)) { | 
|  | 284 | if (!AllowVolatile && SI->isVolatile()) | 
|  | 285 | return nullptr; | 
|  | 286 | return SI->getPointerOperand(); | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) { | 
|  | 290 | if (!AllowVolatile && CXI->isVolatile()) | 
|  | 291 | return nullptr; | 
|  | 292 | return CXI->getPointerOperand(); | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) { | 
|  | 296 | if (!AllowVolatile && RMWI->isVolatile()) | 
|  | 297 | return nullptr; | 
|  | 298 | return RMWI->getPointerOperand(); | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | return nullptr; | 
|  | 302 | } | 
|  | 303 |  | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 304 | /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and | 
|  | 305 | /// advanced by \p Offset bytes. To aid later analysis the method tries to build | 
|  | 306 | /// getelement pointer instructions that traverse the natural type of \p Ptr if | 
|  | 307 | /// possible. If that fails, the remaining offset is adjusted byte-wise, hence | 
|  | 308 | /// through a cast to i8*. | 
|  | 309 | /// | 
|  | 310 | /// TODO: This could probably live somewhere more prominantly if it doesn't | 
|  | 311 | ///       already exist. | 
|  | 312 | static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset, | 
|  | 313 | IRBuilder<NoFolder> &IRB, const DataLayout &DL) { | 
|  | 314 | assert(Offset >= 0 && "Negative offset not supported yet!"); | 
|  | 315 | LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset | 
|  | 316 | << "-bytes as " << *ResTy << "\n"); | 
|  | 317 |  | 
|  | 318 | // The initial type we are trying to traverse to get nice GEPs. | 
|  | 319 | Type *Ty = Ptr->getType(); | 
|  | 320 |  | 
|  | 321 | SmallVector<Value *, 4> Indices; | 
|  | 322 | std::string GEPName = Ptr->getName().str(); | 
|  | 323 | while (Offset) { | 
|  | 324 | uint64_t Idx, Rem; | 
|  | 325 |  | 
|  | 326 | if (auto *STy = dyn_cast<StructType>(Ty)) { | 
|  | 327 | const StructLayout *SL = DL.getStructLayout(STy); | 
|  | 328 | if (int64_t(SL->getSizeInBytes()) < Offset) | 
|  | 329 | break; | 
|  | 330 | Idx = SL->getElementContainingOffset(Offset); | 
|  | 331 | assert(Idx < STy->getNumElements() && "Offset calculation error!"); | 
|  | 332 | Rem = Offset - SL->getElementOffset(Idx); | 
|  | 333 | Ty = STy->getElementType(Idx); | 
|  | 334 | } else if (auto *PTy = dyn_cast<PointerType>(Ty)) { | 
|  | 335 | Ty = PTy->getElementType(); | 
|  | 336 | if (!Ty->isSized()) | 
|  | 337 | break; | 
|  | 338 | uint64_t ElementSize = DL.getTypeAllocSize(Ty); | 
|  | 339 | assert(ElementSize && "Expected type with size!"); | 
|  | 340 | Idx = Offset / ElementSize; | 
|  | 341 | Rem = Offset % ElementSize; | 
|  | 342 | } else { | 
|  | 343 | // Non-aggregate type, we cast and make byte-wise progress now. | 
|  | 344 | break; | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset | 
|  | 348 | << " Idx: " << Idx << " Rem: " << Rem << "\n"); | 
|  | 349 |  | 
|  | 350 | GEPName += "." + std::to_string(Idx); | 
|  | 351 | Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx)); | 
|  | 352 | Offset = Rem; | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | // Create a GEP if we collected indices above. | 
|  | 356 | if (Indices.size()) | 
|  | 357 | Ptr = IRB.CreateGEP(Ptr, Indices, GEPName); | 
|  | 358 |  | 
|  | 359 | // If an offset is left we use byte-wise adjustment. | 
|  | 360 | if (Offset) { | 
|  | 361 | Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy()); | 
|  | 362 | Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset), | 
|  | 363 | GEPName + ".b" + Twine(Offset)); | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | // Ensure the result has the requested type. | 
|  | 367 | Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast"); | 
|  | 368 |  | 
|  | 369 | LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n"); | 
|  | 370 | return Ptr; | 
|  | 371 | } | 
|  | 372 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 373 | /// 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] | 374 | /// will be done by looking through cast instructions, selects, phis, and calls | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 375 | /// with the "returned" attribute. Once we cannot look through the value any | 
|  | 376 | /// further, the callback \p VisitValueCB is invoked and passed the current | 
|  | 377 | /// value, the \p State, and a flag to indicate if we stripped anything. To | 
|  | 378 | /// limit how much effort is invested, we will never visit more values than | 
|  | 379 | /// specified by \p MaxValues. | 
|  | 380 | template <typename AAType, typename StateTy> | 
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 381 | static bool genericValueTraversal( | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 382 | Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 383 | const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 384 | int MaxValues = 8) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 385 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 386 | const AAIsDead *LivenessAA = nullptr; | 
|  | 387 | if (IRP.getAnchorScope()) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 388 | LivenessAA = &A.getAAFor<AAIsDead>( | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 389 | QueryingAA, IRPosition::function(*IRP.getAnchorScope()), | 
|  | 390 | /* TrackDependence */ false); | 
|  | 391 | bool AnyDead = false; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 392 |  | 
|  | 393 | // TODO: Use Positions here to allow context sensitivity in VisitValueCB | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 394 | SmallPtrSet<Value *, 16> Visited; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 395 | SmallVector<Value *, 16> Worklist; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 396 | Worklist.push_back(&IRP.getAssociatedValue()); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 397 |  | 
|  | 398 | int Iteration = 0; | 
|  | 399 | do { | 
|  | 400 | Value *V = Worklist.pop_back_val(); | 
|  | 401 |  | 
|  | 402 | // Check if we should process the current value. To prevent endless | 
|  | 403 | // recursion keep a record of the values we followed! | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 404 | if (!Visited.insert(V).second) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 405 | continue; | 
|  | 406 |  | 
|  | 407 | // Make sure we limit the compile time for complex expressions. | 
|  | 408 | if (Iteration++ >= MaxValues) | 
|  | 409 | return false; | 
|  | 410 |  | 
|  | 411 | // Explicitly look through calls with a "returned" attribute if we do | 
|  | 412 | // not have a pointer as stripPointerCasts only works on them. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 413 | Value *NewV = nullptr; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 414 | if (V->getType()->isPointerTy()) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 415 | NewV = V->stripPointerCasts(); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 416 | } else { | 
|  | 417 | CallSite CS(V); | 
|  | 418 | if (CS && CS.getCalledFunction()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 419 | for (Argument &Arg : CS.getCalledFunction()->args()) | 
|  | 420 | if (Arg.hasReturnedAttr()) { | 
|  | 421 | NewV = CS.getArgOperand(Arg.getArgNo()); | 
|  | 422 | break; | 
|  | 423 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 424 | } | 
|  | 425 | } | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 426 | if (NewV && NewV != V) { | 
|  | 427 | Worklist.push_back(NewV); | 
|  | 428 | continue; | 
|  | 429 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 430 |  | 
|  | 431 | // Look through select instructions, visit both potential values. | 
|  | 432 | if (auto *SI = dyn_cast<SelectInst>(V)) { | 
|  | 433 | Worklist.push_back(SI->getTrueValue()); | 
|  | 434 | Worklist.push_back(SI->getFalseValue()); | 
|  | 435 | continue; | 
|  | 436 | } | 
|  | 437 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 438 | // Look through phi nodes, visit all live operands. | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 439 | if (auto *PHI = dyn_cast<PHINode>(V)) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 440 | assert(LivenessAA && | 
|  | 441 | "Expected liveness in the presence of instructions!"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 442 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { | 
|  | 443 | const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 444 | if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 445 | AnyDead = true; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 446 | continue; | 
|  | 447 | } | 
|  | 448 | Worklist.push_back(PHI->getIncomingValue(u)); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 449 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 450 | continue; | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | // Once a leaf is reached we inform the user through the callback. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 454 | if (!VisitValueCB(*V, State, Iteration > 1)) | 
|  | 455 | return false; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 456 | } while (!Worklist.empty()); | 
|  | 457 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 458 | // If we actually used liveness information so we have to record a dependence. | 
|  | 459 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 460 | A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 461 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 462 | // All values have been visited. | 
|  | 463 | return true; | 
|  | 464 | } | 
|  | 465 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 466 | /// Return true if \p New is equal or worse than \p Old. | 
|  | 467 | static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { | 
|  | 468 | if (!Old.isIntAttribute()) | 
|  | 469 | return true; | 
|  | 470 |  | 
|  | 471 | return Old.getValueAsInt() >= New.getValueAsInt(); | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | /// Return true if the information provided by \p Attr was added to the | 
|  | 475 | /// 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] | 476 | /// 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] | 477 | static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 478 | AttributeList &Attrs, int AttrIdx) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 479 |  | 
|  | 480 | if (Attr.isEnumAttribute()) { | 
|  | 481 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); | 
|  | 482 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 483 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 484 | return false; | 
|  | 485 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 486 | return true; | 
|  | 487 | } | 
|  | 488 | if (Attr.isStringAttribute()) { | 
|  | 489 | StringRef Kind = Attr.getKindAsString(); | 
|  | 490 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 491 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 492 | return false; | 
|  | 493 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 494 | return true; | 
|  | 495 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 496 | if (Attr.isIntAttribute()) { | 
|  | 497 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); | 
|  | 498 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 499 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 500 | return false; | 
|  | 501 | Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); | 
|  | 502 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 503 | return true; | 
|  | 504 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 505 |  | 
|  | 506 | llvm_unreachable("Expected enum or string attribute!"); | 
|  | 507 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 508 |  | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 509 | static const Value * | 
|  | 510 | getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset, | 
|  | 511 | const DataLayout &DL, | 
|  | 512 | bool AllowNonInbounds = false) { | 
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 513 | const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 514 | if (!Ptr) | 
|  | 515 | return nullptr; | 
|  | 516 |  | 
|  | 517 | return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 518 | AllowNonInbounds); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 519 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 520 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 521 | ChangeStatus AbstractAttribute::update(Attributor &A) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 522 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
|  | 523 | if (getState().isAtFixpoint()) | 
|  | 524 | return HasChanged; | 
|  | 525 |  | 
|  | 526 | LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); | 
|  | 527 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 528 | HasChanged = updateImpl(A); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 529 |  | 
|  | 530 | LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this | 
|  | 531 | << "\n"); | 
|  | 532 |  | 
|  | 533 | return HasChanged; | 
|  | 534 | } | 
|  | 535 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 536 | ChangeStatus | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 537 | IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 538 | const ArrayRef<Attribute> &DeducedAttrs) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 539 | Function *ScopeFn = IRP.getAssociatedFunction(); | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 540 | IRPosition::Kind PK = IRP.getPositionKind(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 541 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 542 | // In the following some generic code that will manifest attributes in | 
|  | 543 | // DeducedAttrs if they improve the current IR. Due to the different | 
|  | 544 | // annotation positions we use the underlying AttributeList interface. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 545 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 546 | AttributeList Attrs; | 
|  | 547 | switch (PK) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 548 | case IRPosition::IRP_INVALID: | 
|  | 549 | case IRPosition::IRP_FLOAT: | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 550 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 551 | case IRPosition::IRP_ARGUMENT: | 
|  | 552 | case IRPosition::IRP_FUNCTION: | 
|  | 553 | case IRPosition::IRP_RETURNED: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 554 | Attrs = ScopeFn->getAttributes(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 555 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 556 | case IRPosition::IRP_CALL_SITE: | 
|  | 557 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 558 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 559 | Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 560 | break; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 561 | } | 
|  | 562 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 563 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 564 | LLVMContext &Ctx = IRP.getAnchorValue().getContext(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 565 | for (const Attribute &Attr : DeducedAttrs) { | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 566 | if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 567 | continue; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 568 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 569 | HasChanged = ChangeStatus::CHANGED; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 570 | } | 
|  | 571 |  | 
|  | 572 | if (HasChanged == ChangeStatus::UNCHANGED) | 
|  | 573 | return HasChanged; | 
|  | 574 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 575 | switch (PK) { | 
|  | 576 | case IRPosition::IRP_ARGUMENT: | 
|  | 577 | case IRPosition::IRP_FUNCTION: | 
|  | 578 | case IRPosition::IRP_RETURNED: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 579 | ScopeFn->setAttributes(Attrs); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 580 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 581 | case IRPosition::IRP_CALL_SITE: | 
|  | 582 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 583 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 584 | CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); | 
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 585 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 586 | case IRPosition::IRP_INVALID: | 
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 587 | case IRPosition::IRP_FLOAT: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 588 | break; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 589 | } | 
|  | 590 |  | 
|  | 591 | return HasChanged; | 
|  | 592 | } | 
|  | 593 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 594 | const IRPosition IRPosition::EmptyKey(255); | 
|  | 595 | const IRPosition IRPosition::TombstoneKey(256); | 
|  | 596 |  | 
|  | 597 | SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { | 
|  | 598 | IRPositions.emplace_back(IRP); | 
|  | 599 |  | 
|  | 600 | ImmutableCallSite ICS(&IRP.getAnchorValue()); | 
|  | 601 | switch (IRP.getPositionKind()) { | 
|  | 602 | case IRPosition::IRP_INVALID: | 
|  | 603 | case IRPosition::IRP_FLOAT: | 
|  | 604 | case IRPosition::IRP_FUNCTION: | 
|  | 605 | return; | 
|  | 606 | case IRPosition::IRP_ARGUMENT: | 
|  | 607 | case IRPosition::IRP_RETURNED: | 
|  | 608 | IRPositions.emplace_back( | 
|  | 609 | IRPosition::function(*IRP.getAssociatedFunction())); | 
|  | 610 | return; | 
|  | 611 | case IRPosition::IRP_CALL_SITE: | 
|  | 612 | assert(ICS && "Expected call site!"); | 
|  | 613 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 614 | //       in CallBase. | 
|  | 615 | if (!ICS.hasOperandBundles()) | 
|  | 616 | if (const Function *Callee = ICS.getCalledFunction()) | 
|  | 617 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 618 | return; | 
|  | 619 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
|  | 620 | assert(ICS && "Expected call site!"); | 
|  | 621 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 622 | //       in CallBase. | 
|  | 623 | if (!ICS.hasOperandBundles()) { | 
|  | 624 | if (const Function *Callee = ICS.getCalledFunction()) { | 
|  | 625 | IRPositions.emplace_back(IRPosition::returned(*Callee)); | 
|  | 626 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 627 | } | 
|  | 628 | } | 
|  | 629 | IRPositions.emplace_back( | 
|  | 630 | IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); | 
|  | 631 | return; | 
|  | 632 | case IRPosition::IRP_CALL_SITE_ARGUMENT: { | 
|  | 633 | int ArgNo = IRP.getArgNo(); | 
|  | 634 | assert(ICS && ArgNo >= 0 && "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 | const Function *Callee = ICS.getCalledFunction(); | 
|  | 639 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) | 
|  | 640 | IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); | 
|  | 641 | if (Callee) | 
|  | 642 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 643 | } | 
|  | 644 | IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); | 
|  | 645 | return; | 
|  | 646 | } | 
|  | 647 | } | 
|  | 648 | } | 
|  | 649 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 650 | bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, | 
|  | 651 | bool IgnoreSubsumingPositions) const { | 
|  | 652 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 653 | for (Attribute::AttrKind AK : AKs) | 
|  | 654 | if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) | 
|  | 655 | return true; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 656 | // The first position returned by the SubsumingPositionIterator is | 
|  | 657 | // always the position itself. If we ignore subsuming positions we | 
|  | 658 | // are done after the first iteration. | 
|  | 659 | if (IgnoreSubsumingPositions) | 
|  | 660 | break; | 
|  | 661 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 662 | return false; | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 666 | SmallVectorImpl<Attribute> &Attrs, | 
|  | 667 | bool IgnoreSubsumingPositions) const { | 
|  | 668 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 669 | for (Attribute::AttrKind AK : AKs) { | 
|  | 670 | const Attribute &Attr = EquivIRP.getAttr(AK); | 
|  | 671 | if (Attr.getKindAsEnum() == AK) | 
|  | 672 | Attrs.push_back(Attr); | 
|  | 673 | } | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 674 | // The first position returned by the SubsumingPositionIterator is | 
|  | 675 | // always the position itself. If we ignore subsuming positions we | 
|  | 676 | // are done after the first iteration. | 
|  | 677 | if (IgnoreSubsumingPositions) | 
|  | 678 | break; | 
|  | 679 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 680 | } | 
|  | 681 |  | 
|  | 682 | void IRPosition::verify() { | 
|  | 683 | switch (KindOrArgNo) { | 
|  | 684 | default: | 
|  | 685 | assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); | 
|  | 686 | assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && | 
|  | 687 | "Expected call base or argument for positive attribute index!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 688 | if (isa<Argument>(AnchorVal)) { | 
|  | 689 | assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 690 | "Argument number mismatch!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 691 | assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && | 
|  | 692 | "Associated value mismatch!"); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 693 | } else { | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 694 | assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 695 | "Call site argument number mismatch!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 696 | assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == | 
|  | 697 | &getAssociatedValue() && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 698 | "Associated value mismatch!"); | 
|  | 699 | } | 
|  | 700 | break; | 
|  | 701 | case IRP_INVALID: | 
|  | 702 | assert(!AnchorVal && "Expected no value for an invalid position!"); | 
|  | 703 | break; | 
|  | 704 | case IRP_FLOAT: | 
|  | 705 | assert((!isa<CallBase>(&getAssociatedValue()) && | 
|  | 706 | !isa<Argument>(&getAssociatedValue())) && | 
|  | 707 | "Expected specialized kind for call base and argument values!"); | 
|  | 708 | break; | 
|  | 709 | case IRP_RETURNED: | 
|  | 710 | assert(isa<Function>(AnchorVal) && | 
|  | 711 | "Expected function for a 'returned' position!"); | 
|  | 712 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 713 | break; | 
|  | 714 | case IRP_CALL_SITE_RETURNED: | 
|  | 715 | assert((isa<CallBase>(AnchorVal)) && | 
|  | 716 | "Expected call base for 'call site returned' position!"); | 
|  | 717 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 718 | break; | 
|  | 719 | case IRP_CALL_SITE: | 
|  | 720 | assert((isa<CallBase>(AnchorVal)) && | 
|  | 721 | "Expected call base for 'call site function' position!"); | 
|  | 722 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 723 | break; | 
|  | 724 | case IRP_FUNCTION: | 
|  | 725 | assert(isa<Function>(AnchorVal) && | 
|  | 726 | "Expected function for a 'function' position!"); | 
|  | 727 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 728 | break; | 
|  | 729 | } | 
|  | 730 | } | 
|  | 731 |  | 
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 732 | namespace { | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 733 | /// 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] | 734 | /// information in \p R and indicate/return if \p S did change (as-in update is | 
|  | 735 | /// required to be run again). | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 736 | template <typename StateType> | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 737 | ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 738 | auto Assumed = S.getAssumed(); | 
|  | 739 | S ^= R; | 
|  | 740 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | 
|  | 741 | : ChangeStatus::CHANGED; | 
|  | 742 | } | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 743 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 744 | /// Clamp the information known for all returned values of a function | 
|  | 745 | /// (identified by \p QueryingAA) into \p S. | 
|  | 746 | template <typename AAType, typename StateType = typename AAType::StateType> | 
|  | 747 | static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, | 
|  | 748 | StateType &S) { | 
|  | 749 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 750 | << QueryingAA << " into " << S << "\n"); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 751 |  | 
|  | 752 | assert((QueryingAA.getIRPosition().getPositionKind() == | 
|  | 753 | IRPosition::IRP_RETURNED || | 
|  | 754 | QueryingAA.getIRPosition().getPositionKind() == | 
|  | 755 | IRPosition::IRP_CALL_SITE_RETURNED) && | 
|  | 756 | "Can only clamp returned value states for a function returned or call " | 
|  | 757 | "site returned position!"); | 
|  | 758 |  | 
|  | 759 | // Use an optional state as there might not be any return values and we want | 
|  | 760 | // to join (IntegerState::operator&) the state of all there are. | 
|  | 761 | Optional<StateType> T; | 
|  | 762 |  | 
|  | 763 | // Callback for each possibly returned value. | 
|  | 764 | auto CheckReturnValue = [&](Value &RV) -> bool { | 
|  | 765 | const IRPosition &RVPos = IRPosition::value(RV); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 766 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); | 
|  | 767 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() | 
|  | 768 | << " @ " << RVPos << "\n"); | 
|  | 769 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 770 | if (T.hasValue()) | 
|  | 771 | *T &= AAS; | 
|  | 772 | else | 
|  | 773 | T = AAS; | 
|  | 774 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T | 
|  | 775 | << "\n"); | 
|  | 776 | return T->isValidState(); | 
|  | 777 | }; | 
|  | 778 |  | 
|  | 779 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) | 
|  | 780 | S.indicatePessimisticFixpoint(); | 
|  | 781 | else if (T.hasValue()) | 
|  | 782 | S ^= *T; | 
|  | 783 | } | 
|  | 784 |  | 
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 785 | /// Helper class to compose two generic deduction | 
|  | 786 | template <typename AAType, typename Base, typename StateType, | 
|  | 787 | template <typename...> class F, template <typename...> class G> | 
|  | 788 | struct AAComposeTwoGenericDeduction | 
|  | 789 | : public F<AAType, G<AAType, Base, StateType>, StateType> { | 
|  | 790 | AAComposeTwoGenericDeduction(const IRPosition &IRP) | 
|  | 791 | : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} | 
|  | 792 |  | 
|  | 793 | /// See AbstractAttribute::updateImpl(...). | 
|  | 794 | ChangeStatus updateImpl(Attributor &A) override { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 795 | ChangeStatus ChangedF = | 
|  | 796 | F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 797 | ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); | 
|  | 798 | return ChangedF | ChangedG; | 
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 799 | } | 
|  | 800 | }; | 
|  | 801 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 802 | /// Helper class for generic deduction: return value -> returned position. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 803 | template <typename AAType, typename Base, | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 804 | typename StateType = typename Base::StateType> | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 805 | struct AAReturnedFromReturnedValues : public Base { | 
|  | 806 | AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 807 |  | 
|  | 808 | /// See AbstractAttribute::updateImpl(...). | 
|  | 809 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 810 | StateType S(StateType::getBestState(this->getState())); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 811 | clampReturnedValueStates<AAType, StateType>(A, *this, S); | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 812 | // TODO: If we know we visited all returned values, thus no are assumed | 
|  | 813 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 814 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | 
|  | 815 | } | 
|  | 816 | }; | 
|  | 817 |  | 
|  | 818 | /// Clamp the information known at all call sites for a given argument | 
|  | 819 | /// (identified by \p QueryingAA) into \p S. | 
|  | 820 | template <typename AAType, typename StateType = typename AAType::StateType> | 
|  | 821 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, | 
|  | 822 | StateType &S) { | 
|  | 823 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 824 | << QueryingAA << " into " << S << "\n"); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 825 |  | 
|  | 826 | assert(QueryingAA.getIRPosition().getPositionKind() == | 
|  | 827 | IRPosition::IRP_ARGUMENT && | 
|  | 828 | "Can only clamp call site argument states for an argument position!"); | 
|  | 829 |  | 
|  | 830 | // Use an optional state as there might not be any return values and we want | 
|  | 831 | // to join (IntegerState::operator&) the state of all there are. | 
|  | 832 | Optional<StateType> T; | 
|  | 833 |  | 
|  | 834 | // The argument number which is also the call site argument number. | 
|  | 835 | unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); | 
|  | 836 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 837 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | 
|  | 838 | const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); | 
|  | 839 | // Check if a coresponding argument was found or if it is on not associated | 
|  | 840 | // (which can happen for callback calls). | 
|  | 841 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | 
|  | 842 | return false; | 
|  | 843 |  | 
|  | 844 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); | 
|  | 845 | LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() | 
|  | 846 | << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 847 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 848 | if (T.hasValue()) | 
|  | 849 | *T &= AAS; | 
|  | 850 | else | 
|  | 851 | T = AAS; | 
|  | 852 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T | 
|  | 853 | << "\n"); | 
|  | 854 | return T->isValidState(); | 
|  | 855 | }; | 
|  | 856 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 857 | bool AllCallSitesKnown; | 
|  | 858 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true, | 
|  | 859 | AllCallSitesKnown)) | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 860 | S.indicatePessimisticFixpoint(); | 
|  | 861 | else if (T.hasValue()) | 
|  | 862 | S ^= *T; | 
|  | 863 | } | 
|  | 864 |  | 
|  | 865 | /// Helper class for generic deduction: call site argument -> argument position. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 866 | template <typename AAType, typename Base, | 
|  | 867 | typename StateType = typename AAType::StateType> | 
|  | 868 | struct AAArgumentFromCallSiteArguments : public Base { | 
|  | 869 | AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 870 |  | 
|  | 871 | /// See AbstractAttribute::updateImpl(...). | 
|  | 872 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | ea5fabe | 2020-01-28 09:46:15 -0600 | [diff] [blame] | 873 | StateType S(StateType::getBestState(this->getState())); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 874 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 875 | // TODO: If we know we visited all incoming values, thus no are assumed | 
|  | 876 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 877 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | 
|  | 878 | } | 
|  | 879 | }; | 
|  | 880 |  | 
|  | 881 | /// Helper class for generic replication: function returned -> cs returned. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 882 | template <typename AAType, typename Base, | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 883 | typename StateType = typename Base::StateType> | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 884 | struct AACallSiteReturnedFromReturned : public Base { | 
|  | 885 | AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 886 |  | 
|  | 887 | /// See AbstractAttribute::updateImpl(...). | 
|  | 888 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 889 | assert(this->getIRPosition().getPositionKind() == | 
|  | 890 | IRPosition::IRP_CALL_SITE_RETURNED && | 
|  | 891 | "Can only wrap function returned positions for call site returned " | 
|  | 892 | "positions!"); | 
|  | 893 | auto &S = this->getState(); | 
|  | 894 |  | 
|  | 895 | const Function *AssociatedFunction = | 
|  | 896 | this->getIRPosition().getAssociatedFunction(); | 
|  | 897 | if (!AssociatedFunction) | 
|  | 898 | return S.indicatePessimisticFixpoint(); | 
|  | 899 |  | 
|  | 900 | IRPosition FnPos = IRPosition::returned(*AssociatedFunction); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 901 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 902 | return clampStateAndIndicateChange( | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 903 | S, static_cast<const StateType &>(AA.getState())); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 904 | } | 
|  | 905 | }; | 
|  | 906 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 907 | /// Helper class for generic deduction using must-be-executed-context | 
|  | 908 | /// Base class is required to have `followUse` method. | 
|  | 909 |  | 
|  | 910 | /// bool followUse(Attributor &A, const Use *U, const Instruction *I) | 
| Simon Pilgrim | f7aee61 | 2019-10-10 15:25:16 +0000 | [diff] [blame] | 911 | /// U - Underlying use. | 
|  | 912 | /// I - The user of the \p U. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 913 | /// `followUse` returns true if the value should be tracked transitively. | 
|  | 914 |  | 
|  | 915 | template <typename AAType, typename Base, | 
|  | 916 | typename StateType = typename AAType::StateType> | 
|  | 917 | struct AAFromMustBeExecutedContext : public Base { | 
|  | 918 | AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} | 
|  | 919 |  | 
|  | 920 | void initialize(Attributor &A) override { | 
|  | 921 | Base::initialize(A); | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 922 | const IRPosition &IRP = this->getIRPosition(); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 923 | Instruction *CtxI = IRP.getCtxI(); | 
|  | 924 |  | 
|  | 925 | if (!CtxI) | 
|  | 926 | return; | 
|  | 927 |  | 
|  | 928 | for (const Use &U : IRP.getAssociatedValue().uses()) | 
|  | 929 | Uses.insert(&U); | 
|  | 930 | } | 
|  | 931 |  | 
|  | 932 | /// See AbstractAttribute::updateImpl(...). | 
|  | 933 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 934 | auto BeforeState = this->getState(); | 
|  | 935 | auto &S = this->getState(); | 
|  | 936 | Instruction *CtxI = this->getIRPosition().getCtxI(); | 
|  | 937 | if (!CtxI) | 
|  | 938 | return ChangeStatus::UNCHANGED; | 
|  | 939 |  | 
|  | 940 | MustBeExecutedContextExplorer &Explorer = | 
|  | 941 | A.getInfoCache().getMustBeExecutedContextExplorer(); | 
|  | 942 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 943 | auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 944 | for (unsigned u = 0; u < Uses.size(); ++u) { | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 945 | const Use *U = Uses[u]; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 946 | if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 947 | bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 948 | if (Found && Base::followUse(A, U, UserI)) | 
|  | 949 | for (const Use &Us : UserI->uses()) | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 950 | Uses.insert(&Us); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 951 | } | 
|  | 952 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 953 |  | 
|  | 954 | return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; | 
|  | 955 | } | 
|  | 956 |  | 
|  | 957 | private: | 
|  | 958 | /// Container for (transitive) uses of the associated value. | 
|  | 959 | SetVector<const Use *> Uses; | 
|  | 960 | }; | 
|  | 961 |  | 
|  | 962 | template <typename AAType, typename Base, | 
|  | 963 | typename StateType = typename AAType::StateType> | 
|  | 964 | using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = | 
|  | 965 | AAComposeTwoGenericDeduction<AAType, Base, StateType, | 
|  | 966 | AAFromMustBeExecutedContext, | 
|  | 967 | AAArgumentFromCallSiteArguments>; | 
|  | 968 |  | 
|  | 969 | template <typename AAType, typename Base, | 
|  | 970 | typename StateType = typename AAType::StateType> | 
|  | 971 | using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = | 
|  | 972 | AAComposeTwoGenericDeduction<AAType, Base, StateType, | 
|  | 973 | AAFromMustBeExecutedContext, | 
|  | 974 | AACallSiteReturnedFromReturned>; | 
|  | 975 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 976 | /// -----------------------NoUnwind Function Attribute-------------------------- | 
|  | 977 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 978 | struct AANoUnwindImpl : AANoUnwind { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 979 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 980 |  | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 981 | const std::string getAsStr() const override { | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 982 | return getAssumed() ? "nounwind" : "may-unwind"; | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 986 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 987 | auto Opcodes = { | 
|  | 988 | (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr, | 
|  | 989 | (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet, | 
|  | 990 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; | 
|  | 991 |  | 
|  | 992 | auto CheckForNoUnwind = [&](Instruction &I) { | 
|  | 993 | if (!I.mayThrow()) | 
|  | 994 | return true; | 
|  | 995 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 996 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 997 | const auto &NoUnwindAA = | 
|  | 998 | A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); | 
|  | 999 | return NoUnwindAA.isAssumedNoUnwind(); | 
|  | 1000 | } | 
|  | 1001 | return false; | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1002 | }; | 
|  | 1003 |  | 
|  | 1004 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) | 
|  | 1005 | return indicatePessimisticFixpoint(); | 
|  | 1006 |  | 
|  | 1007 | return ChangeStatus::UNCHANGED; | 
|  | 1008 | } | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 1009 | }; | 
|  | 1010 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1011 | struct AANoUnwindFunction final : public AANoUnwindImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1012 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1013 |  | 
|  | 1014 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1015 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1016 | }; | 
|  | 1017 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1018 | /// NoUnwind attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1019 | struct AANoUnwindCallSite final : AANoUnwindImpl { | 
|  | 1020 | AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} | 
|  | 1021 |  | 
|  | 1022 | /// See AbstractAttribute::initialize(...). | 
|  | 1023 | void initialize(Attributor &A) override { | 
|  | 1024 | AANoUnwindImpl::initialize(A); | 
|  | 1025 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1026 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1027 | indicatePessimisticFixpoint(); | 
|  | 1028 | } | 
|  | 1029 |  | 
|  | 1030 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1031 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1032 | // TODO: Once we have call site specific value information we can provide | 
|  | 1033 | //       call site specific liveness information and then it makes | 
|  | 1034 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1035 | //       redirecting requests to the callee argument. | 
|  | 1036 | Function *F = getAssociatedFunction(); | 
|  | 1037 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1038 | auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); | 
|  | 1039 | return clampStateAndIndicateChange( | 
|  | 1040 | getState(), | 
|  | 1041 | static_cast<const AANoUnwind::StateType &>(FnAA.getState())); | 
|  | 1042 | } | 
|  | 1043 |  | 
|  | 1044 | /// See AbstractAttribute::trackStatistics() | 
|  | 1045 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } | 
|  | 1046 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1047 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1048 | /// --------------------- Function Return Values ------------------------------- | 
|  | 1049 |  | 
|  | 1050 | /// "Attribute" that collects all potential returned values and the return | 
|  | 1051 | /// instructions that they arise from. | 
|  | 1052 | /// | 
|  | 1053 | /// If there is a unique returned value R, the manifest method will: | 
|  | 1054 | ///   - mark R with the "returned" attribute, if R is an argument. | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 1055 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1056 |  | 
|  | 1057 | /// Mapping of values potentially returned by the associated function to the | 
|  | 1058 | /// return instructions that might return them. | 
| Johannes Doerfert | a4a308c | 2019-08-26 17:51:23 +0000 | [diff] [blame] | 1059 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1060 |  | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1061 | /// Mapping to remember the number of returned values for a call site such | 
|  | 1062 | /// that we can avoid updates if nothing changed. | 
|  | 1063 | DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; | 
|  | 1064 |  | 
|  | 1065 | /// Set of unresolved calls returned by the associated function. | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1066 | SmallSetVector<CallBase *, 4> UnresolvedCalls; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1067 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1068 | /// State flags | 
|  | 1069 | /// | 
|  | 1070 | ///{ | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1071 | bool IsFixed = false; | 
|  | 1072 | bool IsValidState = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1073 | ///} | 
|  | 1074 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1075 | public: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1076 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1077 |  | 
|  | 1078 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1079 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1080 | // Reset the state. | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1081 | IsFixed = false; | 
|  | 1082 | IsValidState = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1083 | ReturnedValues.clear(); | 
|  | 1084 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1085 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1086 | if (!F) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1087 | indicatePessimisticFixpoint(); | 
|  | 1088 | return; | 
|  | 1089 | } | 
| Johannes Doerfert | e273ac4 | 2020-01-12 00:13:03 -0600 | [diff] [blame] | 1090 | assert(!F->getReturnType()->isVoidTy() && | 
|  | 1091 | "Did not expect a void return type!"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1092 |  | 
|  | 1093 | // The map from instruction opcodes to those instructions in the function. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1094 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1095 |  | 
|  | 1096 | // 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] | 1097 | for (Argument &Arg : F->args()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1098 | if (Arg.hasReturnedAttr()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1099 | auto &ReturnInstSet = ReturnedValues[&Arg]; | 
|  | 1100 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) | 
|  | 1101 | ReturnInstSet.insert(cast<ReturnInst>(RI)); | 
|  | 1102 |  | 
|  | 1103 | indicateOptimisticFixpoint(); | 
|  | 1104 | return; | 
|  | 1105 | } | 
|  | 1106 | } | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1107 |  | 
|  | 1108 | if (!F->hasExactDefinition()) | 
|  | 1109 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1110 | } | 
|  | 1111 |  | 
|  | 1112 | /// See AbstractAttribute::manifest(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1113 | ChangeStatus manifest(Attributor &A) override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1114 |  | 
|  | 1115 | /// See AbstractAttribute::getState(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1116 | AbstractState &getState() override { return *this; } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1117 |  | 
|  | 1118 | /// See AbstractAttribute::getState(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1119 | const AbstractState &getState() const override { return *this; } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1120 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1121 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1122 | ChangeStatus updateImpl(Attributor &A) override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1123 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1124 | llvm::iterator_range<iterator> returned_values() override { | 
|  | 1125 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | 
|  | 1126 | } | 
|  | 1127 |  | 
|  | 1128 | llvm::iterator_range<const_iterator> returned_values() const override { | 
|  | 1129 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | 
|  | 1130 | } | 
|  | 1131 |  | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1132 | const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1133 | return UnresolvedCalls; | 
|  | 1134 | } | 
|  | 1135 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1136 | /// Return the number of potential return values, -1 if unknown. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1137 | size_t getNumReturnValues() const override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1138 | return isValidState() ? ReturnedValues.size() : -1; | 
|  | 1139 | } | 
|  | 1140 |  | 
|  | 1141 | /// Return an assumed unique return value if a single candidate is found. If | 
|  | 1142 | /// there cannot be one, return a nullptr. If it is not clear yet, return the | 
|  | 1143 | /// Optional::NoneType. | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1144 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1145 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1146 | /// See AbstractState::checkForAllReturnedValues(...). | 
|  | 1147 | bool checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1148 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1149 | &Pred) const override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1150 |  | 
|  | 1151 | /// Pretty print the attribute similar to the IR representation. | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1152 | const std::string getAsStr() const override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1153 |  | 
|  | 1154 | /// See AbstractState::isAtFixpoint(). | 
|  | 1155 | bool isAtFixpoint() const override { return IsFixed; } | 
|  | 1156 |  | 
|  | 1157 | /// See AbstractState::isValidState(). | 
|  | 1158 | bool isValidState() const override { return IsValidState; } | 
|  | 1159 |  | 
|  | 1160 | /// See AbstractState::indicateOptimisticFixpoint(...). | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1161 | ChangeStatus indicateOptimisticFixpoint() override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1162 | IsFixed = true; | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1163 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1164 | } | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1165 |  | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1166 | ChangeStatus indicatePessimisticFixpoint() override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1167 | IsFixed = true; | 
|  | 1168 | IsValidState = false; | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1169 | return ChangeStatus::CHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1170 | } | 
|  | 1171 | }; | 
|  | 1172 |  | 
|  | 1173 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { | 
|  | 1174 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 1175 |  | 
|  | 1176 | // Bookkeeping. | 
|  | 1177 | assert(isValidState()); | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1178 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, | 
|  | 1179 | "Number of function with known return values"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1180 |  | 
|  | 1181 | // 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] | 1182 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1183 |  | 
|  | 1184 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) | 
|  | 1185 | return Changed; | 
|  | 1186 |  | 
|  | 1187 | // Bookkeeping. | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1188 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, | 
|  | 1189 | "Number of function with unique return"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1190 |  | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1191 | // Callback to replace the uses of CB with the constant C. | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 1192 | auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) { | 
| Johannes Doerfert | 8fa56c4 | 2019-10-11 01:45:32 +0000 | [diff] [blame] | 1193 | if (CB.getNumUses() == 0 || CB.isMustTailCall()) | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1194 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 1195 | A.replaceAllUsesWith(CB, C); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1196 | return ChangeStatus::CHANGED; | 
|  | 1197 | }; | 
|  | 1198 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1199 | // If the assumed unique return value is an argument, annotate it. | 
|  | 1200 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 1201 | // TODO: This should be handled differently! | 
|  | 1202 | this->AnchorVal = UniqueRVArg; | 
|  | 1203 | this->KindOrArgNo = UniqueRVArg->getArgNo(); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1204 | Changed = IRAttribute::manifest(A); | 
|  | 1205 | } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { | 
|  | 1206 | // We can replace the returned value with the unique returned constant. | 
|  | 1207 | Value &AnchorValue = getAnchorValue(); | 
|  | 1208 | if (Function *F = dyn_cast<Function>(&AnchorValue)) { | 
|  | 1209 | for (const Use &U : F->uses()) | 
|  | 1210 | if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1211 | if (CB->isCallee(&U)) { | 
|  | 1212 | Constant *RVCCast = | 
| Johannes Doerfert | 07d1642 | 2019-11-01 23:03:35 -0500 | [diff] [blame] | 1213 | CB->getType() == RVC->getType() | 
|  | 1214 | ? RVC | 
|  | 1215 | : ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1216 | Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; | 
|  | 1217 | } | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1218 | } else { | 
|  | 1219 | assert(isa<CallBase>(AnchorValue) && | 
|  | 1220 | "Expcected a function or call base anchor!"); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1221 | Constant *RVCCast = | 
| Johannes Doerfert | 07d1642 | 2019-11-01 23:03:35 -0500 | [diff] [blame] | 1222 | AnchorValue.getType() == RVC->getType() | 
|  | 1223 | ? RVC | 
|  | 1224 | : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1225 | Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1226 | } | 
|  | 1227 | if (Changed == ChangeStatus::CHANGED) | 
|  | 1228 | STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, | 
|  | 1229 | "Number of function returns replaced by constant return"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | return Changed; | 
|  | 1233 | } | 
|  | 1234 |  | 
|  | 1235 | const std::string AAReturnedValuesImpl::getAsStr() const { | 
|  | 1236 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + | 
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 1237 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1238 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1239 | } | 
|  | 1240 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1241 | Optional<Value *> | 
|  | 1242 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { | 
|  | 1243 | // If checkForAllReturnedValues provides a unique value, ignoring potential | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1244 | // undef values that can also be present, it is assumed to be the actual | 
|  | 1245 | // return value and forwarded to the caller of this method. If there are | 
|  | 1246 | // multiple, a nullptr is returned indicating there cannot be a unique | 
|  | 1247 | // returned value. | 
|  | 1248 | Optional<Value *> UniqueRV; | 
|  | 1249 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1250 | auto Pred = [&](Value &RV) -> bool { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1251 | // If we found a second returned value and neither the current nor the saved | 
|  | 1252 | // one is an undef, there is no unique returned value. Undefs are special | 
|  | 1253 | // since we can pretend they have any value. | 
|  | 1254 | if (UniqueRV.hasValue() && UniqueRV != &RV && | 
|  | 1255 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { | 
|  | 1256 | UniqueRV = nullptr; | 
|  | 1257 | return false; | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | // Do not overwrite a value with an undef. | 
|  | 1261 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) | 
|  | 1262 | UniqueRV = &RV; | 
|  | 1263 |  | 
|  | 1264 | return true; | 
|  | 1265 | }; | 
|  | 1266 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1267 | if (!A.checkForAllReturnedValues(Pred, *this)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1268 | UniqueRV = nullptr; | 
|  | 1269 |  | 
|  | 1270 | return UniqueRV; | 
|  | 1271 | } | 
|  | 1272 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1273 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1274 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1275 | &Pred) const { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1276 | if (!isValidState()) | 
|  | 1277 | return false; | 
|  | 1278 |  | 
|  | 1279 | // Check all returned values but ignore call sites as long as we have not | 
|  | 1280 | // encountered an overdefined one during an update. | 
|  | 1281 | for (auto &It : ReturnedValues) { | 
|  | 1282 | Value *RV = It.first; | 
|  | 1283 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1284 | CallBase *CB = dyn_cast<CallBase>(RV); | 
|  | 1285 | if (CB && !UnresolvedCalls.count(CB)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1286 | continue; | 
|  | 1287 |  | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1288 | if (!Pred(*RV, It.second)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1289 | return false; | 
|  | 1290 | } | 
|  | 1291 |  | 
|  | 1292 | return true; | 
|  | 1293 | } | 
|  | 1294 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1295 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1296 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); | 
|  | 1297 | bool Changed = false; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1298 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1299 | // State used in the value traversals starting in returned values. | 
|  | 1300 | struct RVState { | 
|  | 1301 | // The map in which we collect return values -> return instrs. | 
|  | 1302 | decltype(ReturnedValues) &RetValsMap; | 
|  | 1303 | // The flag to indicate a change. | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1304 | bool &Changed; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1305 | // The return instrs we come from. | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1306 | SmallSetVector<ReturnInst *, 4> RetInsts; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1307 | }; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1308 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1309 | // Callback for a leaf value returned by the associated function. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1310 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1311 | auto Size = RVS.RetValsMap[&Val].size(); | 
|  | 1312 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); | 
|  | 1313 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; | 
|  | 1314 | RVS.Changed |= Inserted; | 
|  | 1315 | LLVM_DEBUG({ | 
|  | 1316 | if (Inserted) | 
|  | 1317 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val | 
|  | 1318 | << " => " << RVS.RetInsts.size() << "\n"; | 
|  | 1319 | }); | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1320 | return true; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1321 | }; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1322 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1323 | // Helper method to invoke the generic value traversal. | 
|  | 1324 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { | 
|  | 1325 | IRPosition RetValPos = IRPosition::value(RV); | 
|  | 1326 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, | 
|  | 1327 | RVS, VisitValueCB); | 
|  | 1328 | }; | 
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 1329 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1330 | // Callback for all "return intructions" live in the associated function. | 
|  | 1331 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { | 
|  | 1332 | ReturnInst &Ret = cast<ReturnInst>(I); | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1333 | RVState RVS({ReturnedValues, Changed, {}}); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1334 | RVS.RetInsts.insert(&Ret); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1335 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); | 
|  | 1336 | }; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1337 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1338 | // Start by discovering returned values from all live returned instructions in | 
|  | 1339 | // the associated function. | 
|  | 1340 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) | 
|  | 1341 | return indicatePessimisticFixpoint(); | 
|  | 1342 |  | 
|  | 1343 | // Once returned values "directly" present in the code are handled we try to | 
|  | 1344 | // resolve returned calls. | 
|  | 1345 | decltype(ReturnedValues) NewRVsMap; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1346 | for (auto &It : ReturnedValues) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1347 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first | 
|  | 1348 | << " by #" << It.second.size() << " RIs\n"); | 
|  | 1349 | CallBase *CB = dyn_cast<CallBase>(It.first); | 
|  | 1350 | if (!CB || UnresolvedCalls.count(CB)) | 
|  | 1351 | continue; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1352 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1353 | if (!CB->getCalledFunction()) { | 
|  | 1354 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB | 
|  | 1355 | << "\n"); | 
|  | 1356 | UnresolvedCalls.insert(CB); | 
|  | 1357 | continue; | 
|  | 1358 | } | 
|  | 1359 |  | 
|  | 1360 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 1361 | const auto &RetValAA = A.getAAFor<AAReturnedValues>( | 
|  | 1362 | *this, IRPosition::function(*CB->getCalledFunction())); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1363 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 1364 | << RetValAA << "\n"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1365 |  | 
|  | 1366 | // Skip dead ends, thus if we do not know anything about the returned | 
|  | 1367 | // call we mark it as unresolved and it will stay that way. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1368 | if (!RetValAA.getState().isValidState()) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1369 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB | 
|  | 1370 | << "\n"); | 
|  | 1371 | UnresolvedCalls.insert(CB); | 
|  | 1372 | continue; | 
|  | 1373 | } | 
|  | 1374 |  | 
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1375 | // Do not try to learn partial information. If the callee has unresolved | 
|  | 1376 | // return values we will treat the call as unresolved/opaque. | 
|  | 1377 | auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); | 
|  | 1378 | if (!RetValAAUnresolvedCalls.empty()) { | 
|  | 1379 | UnresolvedCalls.insert(CB); | 
|  | 1380 | continue; | 
|  | 1381 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1382 |  | 
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1383 | // Now check if we can track transitively returned values. If possible, thus | 
|  | 1384 | // if all return value can be represented in the current scope, do so. | 
|  | 1385 | bool Unresolved = false; | 
|  | 1386 | for (auto &RetValAAIt : RetValAA.returned_values()) { | 
|  | 1387 | Value *RetVal = RetValAAIt.first; | 
|  | 1388 | if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || | 
|  | 1389 | isa<Constant>(RetVal)) | 
|  | 1390 | continue; | 
|  | 1391 | // Anything that did not fit in the above categories cannot be resolved, | 
|  | 1392 | // mark the call as unresolved. | 
|  | 1393 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " | 
|  | 1394 | "cannot be translated: " | 
|  | 1395 | << *RetVal << "\n"); | 
|  | 1396 | UnresolvedCalls.insert(CB); | 
|  | 1397 | Unresolved = true; | 
|  | 1398 | break; | 
|  | 1399 | } | 
|  | 1400 |  | 
|  | 1401 | if (Unresolved) | 
|  | 1402 | continue; | 
|  | 1403 |  | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1404 | // Now track transitively returned values. | 
|  | 1405 | unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; | 
|  | 1406 | if (NumRetAA == RetValAA.getNumReturnValues()) { | 
|  | 1407 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " | 
|  | 1408 | "changed since it was seen last\n"); | 
|  | 1409 | continue; | 
|  | 1410 | } | 
|  | 1411 | NumRetAA = RetValAA.getNumReturnValues(); | 
|  | 1412 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1413 | for (auto &RetValAAIt : RetValAA.returned_values()) { | 
|  | 1414 | Value *RetVal = RetValAAIt.first; | 
|  | 1415 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { | 
|  | 1416 | // Arguments are mapped to call site operands and we begin the traversal | 
|  | 1417 | // again. | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1418 | bool Unused = false; | 
|  | 1419 | RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1420 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); | 
|  | 1421 | continue; | 
|  | 1422 | } else if (isa<CallBase>(RetVal)) { | 
|  | 1423 | // Call sites are resolved by the callee attribute over time, no need to | 
|  | 1424 | // do anything for us. | 
|  | 1425 | continue; | 
|  | 1426 | } else if (isa<Constant>(RetVal)) { | 
|  | 1427 | // Constants are valid everywhere, we can simply take them. | 
|  | 1428 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); | 
|  | 1429 | continue; | 
|  | 1430 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1431 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1432 | } | 
|  | 1433 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1434 | // To avoid modifications to the ReturnedValues map while we iterate over it | 
|  | 1435 | // we kept record of potential new entries in a copy map, NewRVsMap. | 
|  | 1436 | for (auto &It : NewRVsMap) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1437 | assert(!It.second.empty() && "Entry does not add anything."); | 
|  | 1438 | auto &ReturnInsts = ReturnedValues[It.first]; | 
|  | 1439 | for (ReturnInst *RI : It.second) | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1440 | if (ReturnInsts.insert(RI)) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1441 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " | 
|  | 1442 | << *It.first << " => " << *RI << "\n"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1443 | Changed = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1444 | } | 
|  | 1445 | } | 
|  | 1446 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1447 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); | 
|  | 1448 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1449 | } | 
|  | 1450 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1451 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { | 
|  | 1452 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} | 
|  | 1453 |  | 
|  | 1454 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1455 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1456 | }; | 
|  | 1457 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1458 | /// Returned values information for a call sites. | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1459 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { | 
|  | 1460 | AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} | 
|  | 1461 |  | 
|  | 1462 | /// See AbstractAttribute::initialize(...). | 
|  | 1463 | void initialize(Attributor &A) override { | 
|  | 1464 | // TODO: Once we have call site specific value information we can provide | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1465 | //       call site specific liveness information and then it makes | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1466 | //       sense to specialize attributes for call sites instead of | 
|  | 1467 | //       redirecting requests to the callee. | 
|  | 1468 | llvm_unreachable("Abstract attributes for returned values are not " | 
|  | 1469 | "supported for call sites yet!"); | 
|  | 1470 | } | 
|  | 1471 |  | 
|  | 1472 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1473 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1474 | return indicatePessimisticFixpoint(); | 
|  | 1475 | } | 
|  | 1476 |  | 
|  | 1477 | /// See AbstractAttribute::trackStatistics() | 
|  | 1478 | void trackStatistics() const override {} | 
|  | 1479 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1480 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1481 | /// ------------------------ NoSync Function Attribute ------------------------- | 
|  | 1482 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1483 | struct AANoSyncImpl : AANoSync { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1484 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1485 |  | 
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 1486 | const std::string getAsStr() const override { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1487 | return getAssumed() ? "nosync" : "may-sync"; | 
|  | 1488 | } | 
|  | 1489 |  | 
|  | 1490 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1491 | ChangeStatus updateImpl(Attributor &A) override; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1492 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1493 | /// Helper function used to determine whether an instruction is non-relaxed | 
|  | 1494 | /// atomic. In other words, if an atomic instruction does not have unordered | 
|  | 1495 | /// or monotonic ordering | 
|  | 1496 | static bool isNonRelaxedAtomic(Instruction *I); | 
|  | 1497 |  | 
|  | 1498 | /// Helper function used to determine whether an instruction is volatile. | 
|  | 1499 | static bool isVolatile(Instruction *I); | 
|  | 1500 |  | 
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 1501 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, | 
|  | 1502 | /// memset). | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1503 | static bool isNoSyncIntrinsic(Instruction *I); | 
|  | 1504 | }; | 
|  | 1505 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1506 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1507 | if (!I->isAtomic()) | 
|  | 1508 | return false; | 
|  | 1509 |  | 
|  | 1510 | AtomicOrdering Ordering; | 
|  | 1511 | switch (I->getOpcode()) { | 
|  | 1512 | case Instruction::AtomicRMW: | 
|  | 1513 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); | 
|  | 1514 | break; | 
|  | 1515 | case Instruction::Store: | 
|  | 1516 | Ordering = cast<StoreInst>(I)->getOrdering(); | 
|  | 1517 | break; | 
|  | 1518 | case Instruction::Load: | 
|  | 1519 | Ordering = cast<LoadInst>(I)->getOrdering(); | 
|  | 1520 | break; | 
|  | 1521 | case Instruction::Fence: { | 
|  | 1522 | auto *FI = cast<FenceInst>(I); | 
|  | 1523 | if (FI->getSyncScopeID() == SyncScope::SingleThread) | 
|  | 1524 | return false; | 
|  | 1525 | Ordering = FI->getOrdering(); | 
|  | 1526 | break; | 
|  | 1527 | } | 
|  | 1528 | case Instruction::AtomicCmpXchg: { | 
|  | 1529 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); | 
|  | 1530 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); | 
|  | 1531 | // Only if both are relaxed, than it can be treated as relaxed. | 
|  | 1532 | // Otherwise it is non-relaxed. | 
|  | 1533 | if (Success != AtomicOrdering::Unordered && | 
|  | 1534 | Success != AtomicOrdering::Monotonic) | 
|  | 1535 | return true; | 
|  | 1536 | if (Failure != AtomicOrdering::Unordered && | 
|  | 1537 | Failure != AtomicOrdering::Monotonic) | 
|  | 1538 | return true; | 
|  | 1539 | return false; | 
|  | 1540 | } | 
|  | 1541 | default: | 
|  | 1542 | llvm_unreachable( | 
|  | 1543 | "New atomic operations need to be known in the attributor."); | 
|  | 1544 | } | 
|  | 1545 |  | 
|  | 1546 | // Relaxed. | 
|  | 1547 | if (Ordering == AtomicOrdering::Unordered || | 
|  | 1548 | Ordering == AtomicOrdering::Monotonic) | 
|  | 1549 | return false; | 
|  | 1550 | return true; | 
|  | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. | 
|  | 1554 | /// FIXME: We should ipmrove the handling of intrinsics. | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1555 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1556 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { | 
|  | 1557 | switch (II->getIntrinsicID()) { | 
|  | 1558 | /// Element wise atomic memory intrinsics are can only be unordered, | 
|  | 1559 | /// therefore nosync. | 
|  | 1560 | case Intrinsic::memset_element_unordered_atomic: | 
|  | 1561 | case Intrinsic::memmove_element_unordered_atomic: | 
|  | 1562 | case Intrinsic::memcpy_element_unordered_atomic: | 
|  | 1563 | return true; | 
|  | 1564 | case Intrinsic::memset: | 
|  | 1565 | case Intrinsic::memmove: | 
|  | 1566 | case Intrinsic::memcpy: | 
|  | 1567 | if (!cast<MemIntrinsic>(II)->isVolatile()) | 
|  | 1568 | return true; | 
|  | 1569 | return false; | 
|  | 1570 | default: | 
|  | 1571 | return false; | 
|  | 1572 | } | 
|  | 1573 | } | 
|  | 1574 | return false; | 
|  | 1575 | } | 
|  | 1576 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1577 | bool AANoSyncImpl::isVolatile(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1578 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && | 
|  | 1579 | "Calls should not be checked here"); | 
|  | 1580 |  | 
|  | 1581 | switch (I->getOpcode()) { | 
|  | 1582 | case Instruction::AtomicRMW: | 
|  | 1583 | return cast<AtomicRMWInst>(I)->isVolatile(); | 
|  | 1584 | case Instruction::Store: | 
|  | 1585 | return cast<StoreInst>(I)->isVolatile(); | 
|  | 1586 | case Instruction::Load: | 
|  | 1587 | return cast<LoadInst>(I)->isVolatile(); | 
|  | 1588 | case Instruction::AtomicCmpXchg: | 
|  | 1589 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); | 
|  | 1590 | default: | 
|  | 1591 | return false; | 
|  | 1592 | } | 
|  | 1593 | } | 
|  | 1594 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1595 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1596 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1597 | auto CheckRWInstForNoSync = [&](Instruction &I) { | 
|  | 1598 | /// We are looking for volatile instructions or Non-Relaxed atomics. | 
| Pankaj Gode | b9a26a8 | 2019-11-22 14:46:43 +0530 | [diff] [blame] | 1599 | /// FIXME: We should improve the handling of intrinsics. | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1600 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1601 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) | 
|  | 1602 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1603 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1604 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 1605 | if (ICS.hasFnAttr(Attribute::NoSync)) | 
|  | 1606 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1607 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1608 | const auto &NoSyncAA = | 
|  | 1609 | A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1610 | if (NoSyncAA.isAssumedNoSync()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1611 | return true; | 
|  | 1612 | return false; | 
|  | 1613 | } | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1614 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1615 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) | 
|  | 1616 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1617 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1618 | return false; | 
|  | 1619 | }; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1620 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1621 | auto CheckForNoSync = [&](Instruction &I) { | 
|  | 1622 | // At this point we handled all read/write effects and they are all | 
|  | 1623 | // nosync, so they can be skipped. | 
|  | 1624 | if (I.mayReadOrWriteMemory()) | 
|  | 1625 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1626 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1627 | // non-convergent and readnone imply nosync. | 
|  | 1628 | return !ImmutableCallSite(&I).isConvergent(); | 
|  | 1629 | }; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1630 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1631 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || | 
|  | 1632 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1633 | return indicatePessimisticFixpoint(); | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1634 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1635 | return ChangeStatus::UNCHANGED; | 
|  | 1636 | } | 
|  | 1637 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1638 | struct AANoSyncFunction final : public AANoSyncImpl { | 
|  | 1639 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} | 
|  | 1640 |  | 
|  | 1641 | /// See AbstractAttribute::trackStatistics() | 
|  | 1642 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } | 
|  | 1643 | }; | 
|  | 1644 |  | 
|  | 1645 | /// NoSync attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1646 | struct AANoSyncCallSite final : AANoSyncImpl { | 
|  | 1647 | AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} | 
|  | 1648 |  | 
|  | 1649 | /// See AbstractAttribute::initialize(...). | 
|  | 1650 | void initialize(Attributor &A) override { | 
|  | 1651 | AANoSyncImpl::initialize(A); | 
|  | 1652 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1653 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1654 | indicatePessimisticFixpoint(); | 
|  | 1655 | } | 
|  | 1656 |  | 
|  | 1657 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1658 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1659 | // TODO: Once we have call site specific value information we can provide | 
|  | 1660 | //       call site specific liveness information and then it makes | 
|  | 1661 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1662 | //       redirecting requests to the callee argument. | 
|  | 1663 | Function *F = getAssociatedFunction(); | 
|  | 1664 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1665 | auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); | 
|  | 1666 | return clampStateAndIndicateChange( | 
|  | 1667 | getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); | 
|  | 1668 | } | 
|  | 1669 |  | 
|  | 1670 | /// See AbstractAttribute::trackStatistics() | 
|  | 1671 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } | 
|  | 1672 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1673 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1674 | /// ------------------------ No-Free Attributes ---------------------------- | 
|  | 1675 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1676 | struct AANoFreeImpl : public AANoFree { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1677 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1678 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1679 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1680 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1681 | auto CheckForNoFree = [&](Instruction &I) { | 
|  | 1682 | ImmutableCallSite ICS(&I); | 
|  | 1683 | if (ICS.hasFnAttr(Attribute::NoFree)) | 
|  | 1684 | return true; | 
|  | 1685 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1686 | const auto &NoFreeAA = | 
|  | 1687 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1688 | return NoFreeAA.isAssumedNoFree(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1689 | }; | 
|  | 1690 |  | 
|  | 1691 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) | 
|  | 1692 | return indicatePessimisticFixpoint(); | 
|  | 1693 | return ChangeStatus::UNCHANGED; | 
|  | 1694 | } | 
|  | 1695 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1696 | /// See AbstractAttribute::getAsStr(). | 
|  | 1697 | const std::string getAsStr() const override { | 
|  | 1698 | return getAssumed() ? "nofree" : "may-free"; | 
|  | 1699 | } | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1700 | }; | 
|  | 1701 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1702 | struct AANoFreeFunction final : public AANoFreeImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1703 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1704 |  | 
|  | 1705 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1706 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1707 | }; | 
|  | 1708 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1709 | /// NoFree attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1710 | struct AANoFreeCallSite final : AANoFreeImpl { | 
|  | 1711 | AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
|  | 1712 |  | 
|  | 1713 | /// See AbstractAttribute::initialize(...). | 
|  | 1714 | void initialize(Attributor &A) override { | 
|  | 1715 | AANoFreeImpl::initialize(A); | 
|  | 1716 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1717 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1718 | indicatePessimisticFixpoint(); | 
|  | 1719 | } | 
|  | 1720 |  | 
|  | 1721 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1722 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1723 | // TODO: Once we have call site specific value information we can provide | 
|  | 1724 | //       call site specific liveness information and then it makes | 
|  | 1725 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1726 | //       redirecting requests to the callee argument. | 
|  | 1727 | Function *F = getAssociatedFunction(); | 
|  | 1728 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1729 | auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); | 
|  | 1730 | return clampStateAndIndicateChange( | 
|  | 1731 | getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); | 
|  | 1732 | } | 
|  | 1733 |  | 
|  | 1734 | /// See AbstractAttribute::trackStatistics() | 
|  | 1735 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } | 
|  | 1736 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1737 |  | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1738 | /// NoFree attribute for floating values. | 
|  | 1739 | struct AANoFreeFloating : AANoFreeImpl { | 
|  | 1740 | AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
|  | 1741 |  | 
|  | 1742 | /// See AbstractAttribute::trackStatistics() | 
|  | 1743 | void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)} | 
|  | 1744 |  | 
|  | 1745 | /// See Abstract Attribute::updateImpl(...). | 
|  | 1746 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1747 | const IRPosition &IRP = getIRPosition(); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1748 |  | 
|  | 1749 | const auto &NoFreeAA = | 
|  | 1750 | A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP)); | 
|  | 1751 | if (NoFreeAA.isAssumedNoFree()) | 
|  | 1752 | return ChangeStatus::UNCHANGED; | 
|  | 1753 |  | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1754 | Value &AssociatedValue = getIRPosition().getAssociatedValue(); | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1755 | auto Pred = [&](const Use &U, bool &Follow) -> bool { | 
|  | 1756 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1757 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1758 | if (CB->isBundleOperand(&U)) | 
|  | 1759 | return false; | 
|  | 1760 | if (!CB->isArgOperand(&U)) | 
|  | 1761 | return true; | 
|  | 1762 | unsigned ArgNo = CB->getArgOperandNo(&U); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1763 |  | 
|  | 1764 | const auto &NoFreeArg = A.getAAFor<AANoFree>( | 
|  | 1765 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1766 | return NoFreeArg.isAssumedNoFree(); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1767 | } | 
|  | 1768 |  | 
|  | 1769 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | 
|  | 1770 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1771 | Follow = true; | 
|  | 1772 | return true; | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 1773 | } | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1774 |  | 
|  | 1775 | // Unknown user. | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1776 | return false; | 
|  | 1777 | }; | 
|  | 1778 | if (!A.checkForAllUses(Pred, *this, AssociatedValue)) | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1779 | return indicatePessimisticFixpoint(); | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1780 |  | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1781 | return ChangeStatus::UNCHANGED; | 
|  | 1782 | } | 
|  | 1783 | }; | 
|  | 1784 |  | 
|  | 1785 | /// NoFree attribute for a call site argument. | 
|  | 1786 | struct AANoFreeArgument final : AANoFreeFloating { | 
|  | 1787 | AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} | 
|  | 1788 |  | 
|  | 1789 | /// See AbstractAttribute::trackStatistics() | 
|  | 1790 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) } | 
|  | 1791 | }; | 
|  | 1792 |  | 
|  | 1793 | /// NoFree attribute for call site arguments. | 
|  | 1794 | struct AANoFreeCallSiteArgument final : AANoFreeFloating { | 
|  | 1795 | AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} | 
|  | 1796 |  | 
|  | 1797 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1798 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1799 | // TODO: Once we have call site specific value information we can provide | 
|  | 1800 | //       call site specific liveness information and then it makes | 
|  | 1801 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1802 | //       redirecting requests to the callee argument. | 
|  | 1803 | Argument *Arg = getAssociatedArgument(); | 
|  | 1804 | if (!Arg) | 
|  | 1805 | return indicatePessimisticFixpoint(); | 
|  | 1806 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 1807 | auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos); | 
|  | 1808 | return clampStateAndIndicateChange( | 
|  | 1809 | getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState())); | 
|  | 1810 | } | 
|  | 1811 |  | 
|  | 1812 | /// See AbstractAttribute::trackStatistics() | 
|  | 1813 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)}; | 
|  | 1814 | }; | 
|  | 1815 |  | 
|  | 1816 | /// NoFree attribute for function return value. | 
|  | 1817 | struct AANoFreeReturned final : AANoFreeFloating { | 
|  | 1818 | AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) { | 
|  | 1819 | llvm_unreachable("NoFree is not applicable to function returns!"); | 
|  | 1820 | } | 
|  | 1821 |  | 
|  | 1822 | /// See AbstractAttribute::initialize(...). | 
|  | 1823 | void initialize(Attributor &A) override { | 
|  | 1824 | llvm_unreachable("NoFree is not applicable to function returns!"); | 
|  | 1825 | } | 
|  | 1826 |  | 
|  | 1827 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1828 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1829 | llvm_unreachable("NoFree is not applicable to function returns!"); | 
|  | 1830 | } | 
|  | 1831 |  | 
|  | 1832 | /// See AbstractAttribute::trackStatistics() | 
|  | 1833 | void trackStatistics() const override {} | 
|  | 1834 | }; | 
|  | 1835 |  | 
|  | 1836 | /// NoFree attribute deduction for a call site return value. | 
|  | 1837 | struct AANoFreeCallSiteReturned final : AANoFreeFloating { | 
|  | 1838 | AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {} | 
|  | 1839 |  | 
|  | 1840 | ChangeStatus manifest(Attributor &A) override { | 
|  | 1841 | return ChangeStatus::UNCHANGED; | 
|  | 1842 | } | 
|  | 1843 | /// See AbstractAttribute::trackStatistics() | 
|  | 1844 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) } | 
|  | 1845 | }; | 
|  | 1846 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1847 | /// ------------------------ NonNull Argument Attribute ------------------------ | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1848 | static int64_t getKnownNonNullAndDerefBytesForUse( | 
|  | 1849 | Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue, | 
|  | 1850 | const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1851 | TrackUse = false; | 
|  | 1852 |  | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1853 | const Value *UseV = U->get(); | 
|  | 1854 | if (!UseV->getType()->isPointerTy()) | 
|  | 1855 | return 0; | 
|  | 1856 |  | 
|  | 1857 | Type *PtrTy = UseV->getType(); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1858 | const Function *F = I->getFunction(); | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1859 | bool NullPointerIsDefined = | 
|  | 1860 | F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1861 | const DataLayout &DL = A.getInfoCache().getDL(); | 
|  | 1862 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { | 
|  | 1863 | if (ICS.isBundleOperand(U)) | 
|  | 1864 | return 0; | 
|  | 1865 |  | 
|  | 1866 | if (ICS.isCallee(U)) { | 
|  | 1867 | IsNonNull |= !NullPointerIsDefined; | 
|  | 1868 | return 0; | 
|  | 1869 | } | 
|  | 1870 |  | 
|  | 1871 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 1872 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); | 
| Johannes Doerfert | 77a6b35 | 2019-11-02 14:47:45 -0500 | [diff] [blame] | 1873 | // As long as we only use known information there is no need to track | 
|  | 1874 | // dependences here. | 
|  | 1875 | auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP, | 
|  | 1876 | /* TrackDependence */ false); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1877 | IsNonNull |= DerefAA.isKnownNonNull(); | 
|  | 1878 | return DerefAA.getKnownDereferenceableBytes(); | 
|  | 1879 | } | 
|  | 1880 |  | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1881 | // We need to follow common pointer manipulation uses to the accesses they | 
|  | 1882 | // feed into. We can try to be smart to avoid looking through things we do not | 
|  | 1883 | // like for now, e.g., non-inbounds GEPs. | 
|  | 1884 | if (isa<CastInst>(I)) { | 
|  | 1885 | TrackUse = true; | 
|  | 1886 | return 0; | 
|  | 1887 | } | 
|  | 1888 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 1889 | if (GEP->hasAllConstantIndices()) { | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1890 | TrackUse = true; | 
|  | 1891 | return 0; | 
|  | 1892 | } | 
|  | 1893 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1894 | int64_t Offset; | 
|  | 1895 | if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 1896 | if (Base == &AssociatedValue && | 
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 1897 | getPointerOperand(I, /* AllowVolatile */ false) == UseV) { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1898 | int64_t DerefBytes = | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1899 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1900 |  | 
|  | 1901 | IsNonNull |= !NullPointerIsDefined; | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1902 | return std::max(int64_t(0), DerefBytes); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1903 | } | 
|  | 1904 | } | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 1905 |  | 
|  | 1906 | /// Corner case when an offset is 0. | 
|  | 1907 | if (const Value *Base = getBasePointerOfAccessPointerOperand( | 
|  | 1908 | I, Offset, DL, /*AllowNonInbounds*/ true)) { | 
|  | 1909 | if (Offset == 0 && Base == &AssociatedValue && | 
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 1910 | getPointerOperand(I, /* AllowVolatile */ false) == UseV) { | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 1911 | int64_t DerefBytes = | 
|  | 1912 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); | 
|  | 1913 | IsNonNull |= !NullPointerIsDefined; | 
|  | 1914 | return std::max(int64_t(0), DerefBytes); | 
|  | 1915 | } | 
|  | 1916 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1917 |  | 
|  | 1918 | return 0; | 
|  | 1919 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1920 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1921 | struct AANonNullImpl : AANonNull { | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1922 | AANonNullImpl(const IRPosition &IRP) | 
|  | 1923 | : AANonNull(IRP), | 
|  | 1924 | NullIsDefined(NullPointerIsDefined( | 
|  | 1925 | getAnchorScope(), | 
|  | 1926 | getAssociatedValue().getType()->getPointerAddressSpace())) {} | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1927 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1928 | /// See AbstractAttribute::initialize(...). | 
|  | 1929 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1930 | if (!NullIsDefined && | 
|  | 1931 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1932 | indicateOptimisticFixpoint(); | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1933 | else if (isa<ConstantPointerNull>(getAssociatedValue())) | 
|  | 1934 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1935 | else | 
|  | 1936 | AANonNull::initialize(A); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1937 | } | 
|  | 1938 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1939 | /// See AAFromMustBeExecutedContext | 
|  | 1940 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 1941 | bool IsNonNull = false; | 
|  | 1942 | bool TrackUse = false; | 
|  | 1943 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, | 
|  | 1944 | IsNonNull, TrackUse); | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1945 | setKnown(IsNonNull); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1946 | return TrackUse; | 
|  | 1947 | } | 
|  | 1948 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1949 | /// See AbstractAttribute::getAsStr(). | 
|  | 1950 | const std::string getAsStr() const override { | 
|  | 1951 | return getAssumed() ? "nonnull" : "may-null"; | 
|  | 1952 | } | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1953 |  | 
|  | 1954 | /// Flag to determine if the underlying value can be null and still allow | 
|  | 1955 | /// valid accesses. | 
|  | 1956 | const bool NullIsDefined; | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1957 | }; | 
|  | 1958 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1959 | /// NonNull attribute for a floating value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1960 | struct AANonNullFloating | 
|  | 1961 | : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { | 
|  | 1962 | using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; | 
|  | 1963 | AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1964 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1965 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1966 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1967 | ChangeStatus Change = Base::updateImpl(A); | 
|  | 1968 | if (isKnownNonNull()) | 
|  | 1969 | return Change; | 
|  | 1970 |  | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1971 | if (!NullIsDefined) { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1972 | const auto &DerefAA = | 
|  | 1973 | A.getAAFor<AADereferenceable>(*this, getIRPosition()); | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1974 | if (DerefAA.getAssumedDereferenceableBytes()) | 
|  | 1975 | return Change; | 
|  | 1976 | } | 
|  | 1977 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1978 | const DataLayout &DL = A.getDataLayout(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1979 |  | 
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1980 | DominatorTree *DT = nullptr; | 
|  | 1981 | InformationCache &InfoCache = A.getInfoCache(); | 
|  | 1982 | if (const Function *Fn = getAnchorScope()) | 
|  | 1983 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); | 
|  | 1984 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1985 | auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1986 | bool Stripped) -> bool { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1987 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); | 
|  | 1988 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1989 | if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT)) | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1990 | T.indicatePessimisticFixpoint(); | 
|  | 1991 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1992 | // Use abstract attribute information. | 
|  | 1993 | const AANonNull::StateType &NS = | 
|  | 1994 | static_cast<const AANonNull::StateType &>(AA.getState()); | 
|  | 1995 | T ^= NS; | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1996 | } | 
|  | 1997 | return T.isValidState(); | 
|  | 1998 | }; | 
|  | 1999 |  | 
|  | 2000 | StateType T; | 
|  | 2001 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, | 
|  | 2002 | T, VisitValueCB)) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2003 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2004 |  | 
|  | 2005 | return clampStateAndIndicateChange(getState(), T); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2006 | } | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2007 |  | 
|  | 2008 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2009 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2010 | }; | 
|  | 2011 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2012 | /// NonNull attribute for function return value. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2013 | struct AANonNullReturned final | 
|  | 2014 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2015 | AANonNullReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2016 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2017 |  | 
|  | 2018 | /// See AbstractAttribute::trackStatistics() | 
|  | 2019 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } | 
|  | 2020 | }; | 
|  | 2021 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2022 | /// NonNull attribute for function argument. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2023 | struct AANonNullArgument final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2024 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, | 
|  | 2025 | AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2026 | AANonNullArgument(const IRPosition &IRP) | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2027 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, | 
|  | 2028 | AANonNullImpl>( | 
|  | 2029 | IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2030 |  | 
|  | 2031 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2032 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2033 | }; | 
|  | 2034 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2035 | struct AANonNullCallSiteArgument final : AANonNullFloating { | 
|  | 2036 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2037 |  | 
|  | 2038 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 2039 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2040 | }; | 
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 2041 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2042 | /// NonNull attribute for a call site return position. | 
|  | 2043 | struct AANonNullCallSiteReturned final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2044 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, | 
|  | 2045 | AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2046 | AANonNullCallSiteReturned(const IRPosition &IRP) | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2047 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, | 
|  | 2048 | AANonNullImpl>( | 
|  | 2049 | IRP) {} | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2050 |  | 
|  | 2051 | /// See AbstractAttribute::trackStatistics() | 
|  | 2052 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } | 
|  | 2053 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2054 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2055 | /// ------------------------ No-Recurse Attributes ---------------------------- | 
|  | 2056 |  | 
|  | 2057 | struct AANoRecurseImpl : public AANoRecurse { | 
|  | 2058 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} | 
|  | 2059 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2060 | /// See AbstractAttribute::getAsStr() | 
|  | 2061 | const std::string getAsStr() const override { | 
|  | 2062 | return getAssumed() ? "norecurse" : "may-recurse"; | 
|  | 2063 | } | 
|  | 2064 | }; | 
|  | 2065 |  | 
|  | 2066 | struct AANoRecurseFunction final : AANoRecurseImpl { | 
|  | 2067 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} | 
|  | 2068 |  | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2069 | /// See AbstractAttribute::initialize(...). | 
|  | 2070 | void initialize(Attributor &A) override { | 
|  | 2071 | AANoRecurseImpl::initialize(A); | 
|  | 2072 | if (const Function *F = getAnchorScope()) | 
| Johannes Doerfert | 26d02b0 | 2019-12-30 16:15:38 -0600 | [diff] [blame] | 2073 | if (A.getInfoCache().getSccSize(*F) != 1) | 
|  | 2074 | indicatePessimisticFixpoint(); | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2075 | } | 
|  | 2076 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2077 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2078 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 2079 |  | 
| Johannes Doerfert | 26d02b0 | 2019-12-30 16:15:38 -0600 | [diff] [blame] | 2080 | // If all live call sites are known to be no-recurse, we are as well. | 
|  | 2081 | auto CallSitePred = [&](AbstractCallSite ACS) { | 
|  | 2082 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( | 
|  | 2083 | *this, IRPosition::function(*ACS.getInstruction()->getFunction()), | 
|  | 2084 | /* TrackDependence */ false, DepClassTy::OPTIONAL); | 
|  | 2085 | return NoRecurseAA.isKnownNoRecurse(); | 
|  | 2086 | }; | 
|  | 2087 | bool AllCallSitesKnown; | 
|  | 2088 | if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) { | 
|  | 2089 | // If we know all call sites and all are known no-recurse, we are done. | 
|  | 2090 | // If all known call sites, which might not be all that exist, are known | 
|  | 2091 | // to be no-recurse, we are not done but we can continue to assume | 
|  | 2092 | // no-recurse. If one of the call sites we have not visited will become | 
|  | 2093 | // live, another update is triggered. | 
|  | 2094 | if (AllCallSitesKnown) | 
|  | 2095 | indicateOptimisticFixpoint(); | 
|  | 2096 | return ChangeStatus::UNCHANGED; | 
|  | 2097 | } | 
|  | 2098 |  | 
|  | 2099 | // 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] | 2100 | auto CheckForNoRecurse = [&](Instruction &I) { | 
|  | 2101 | ImmutableCallSite ICS(&I); | 
|  | 2102 | if (ICS.hasFnAttr(Attribute::NoRecurse)) | 
|  | 2103 | return true; | 
|  | 2104 |  | 
|  | 2105 | const auto &NoRecurseAA = | 
|  | 2106 | A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); | 
|  | 2107 | if (!NoRecurseAA.isAssumedNoRecurse()) | 
|  | 2108 | return false; | 
|  | 2109 |  | 
|  | 2110 | // Recursion to the same function | 
|  | 2111 | if (ICS.getCalledFunction() == getAnchorScope()) | 
|  | 2112 | return false; | 
|  | 2113 |  | 
|  | 2114 | return true; | 
|  | 2115 | }; | 
|  | 2116 |  | 
|  | 2117 | if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) | 
|  | 2118 | return indicatePessimisticFixpoint(); | 
|  | 2119 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2120 | } | 
|  | 2121 |  | 
|  | 2122 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } | 
|  | 2123 | }; | 
|  | 2124 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2125 | /// NoRecurse attribute deduction for a call sites. | 
|  | 2126 | struct AANoRecurseCallSite final : AANoRecurseImpl { | 
|  | 2127 | AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} | 
|  | 2128 |  | 
|  | 2129 | /// See AbstractAttribute::initialize(...). | 
|  | 2130 | void initialize(Attributor &A) override { | 
|  | 2131 | AANoRecurseImpl::initialize(A); | 
|  | 2132 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2133 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2134 | indicatePessimisticFixpoint(); | 
|  | 2135 | } | 
|  | 2136 |  | 
|  | 2137 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2138 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2139 | // TODO: Once we have call site specific value information we can provide | 
|  | 2140 | //       call site specific liveness information and then it makes | 
|  | 2141 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2142 | //       redirecting requests to the callee argument. | 
|  | 2143 | Function *F = getAssociatedFunction(); | 
|  | 2144 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 2145 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); | 
|  | 2146 | return clampStateAndIndicateChange( | 
|  | 2147 | getState(), | 
|  | 2148 | static_cast<const AANoRecurse::StateType &>(FnAA.getState())); | 
|  | 2149 | } | 
|  | 2150 |  | 
|  | 2151 | /// See AbstractAttribute::trackStatistics() | 
|  | 2152 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } | 
|  | 2153 | }; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2154 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2155 | /// -------------------- Undefined-Behavior Attributes ------------------------ | 
|  | 2156 |  | 
|  | 2157 | struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior { | 
|  | 2158 | AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {} | 
|  | 2159 |  | 
|  | 2160 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2161 | // through a pointer (i.e. also branches etc.) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2162 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2163 | const size_t UBPrevSize = KnownUBInsts.size(); | 
|  | 2164 | const size_t NoUBPrevSize = AssumedNoUBInsts.size(); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2165 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2166 | auto InspectMemAccessInstForUB = [&](Instruction &I) { | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2167 | // Skip instructions that are already saved. | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2168 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2169 | return true; | 
|  | 2170 |  | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2171 | // If we reach here, we know we have an instruction | 
|  | 2172 | // that accesses memory through a pointer operand, | 
|  | 2173 | // for which getPointerOperand() should give it to us. | 
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 2174 | const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true); | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2175 | assert(PtrOp && | 
|  | 2176 | "Expected pointer operand of memory accessing instruction"); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2177 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2178 | // A memory access through a pointer is considered UB | 
|  | 2179 | // only if the pointer has constant null value. | 
|  | 2180 | // TODO: Expand it to not only check constant values. | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2181 | if (!isa<ConstantPointerNull>(PtrOp)) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2182 | AssumedNoUBInsts.insert(&I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2183 | return true; | 
|  | 2184 | } | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2185 | const Type *PtrTy = PtrOp->getType(); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2186 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2187 | // Because we only consider instructions inside functions, | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2188 | // assume that a parent function exists. | 
|  | 2189 | const Function *F = I.getFunction(); | 
|  | 2190 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2191 | // A memory access using constant null pointer is only considered UB | 
|  | 2192 | // if null pointer is _not_ defined for the target platform. | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2193 | if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace())) | 
|  | 2194 | AssumedNoUBInsts.insert(&I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2195 | else | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2196 | KnownUBInsts.insert(&I); | 
|  | 2197 | return true; | 
|  | 2198 | }; | 
|  | 2199 |  | 
|  | 2200 | auto InspectBrInstForUB = [&](Instruction &I) { | 
|  | 2201 | // A conditional branch instruction is considered UB if it has `undef` | 
|  | 2202 | // condition. | 
|  | 2203 |  | 
|  | 2204 | // Skip instructions that are already saved. | 
|  | 2205 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | 
|  | 2206 | return true; | 
|  | 2207 |  | 
|  | 2208 | // We know we have a branch instruction. | 
|  | 2209 | auto BrInst = cast<BranchInst>(&I); | 
|  | 2210 |  | 
|  | 2211 | // Unconditional branches are never considered UB. | 
|  | 2212 | if (BrInst->isUnconditional()) | 
|  | 2213 | return true; | 
|  | 2214 |  | 
|  | 2215 | // Either we stopped and the appropriate action was taken, | 
|  | 2216 | // or we got back a simplified value to continue. | 
|  | 2217 | Optional<Value *> SimplifiedCond = | 
|  | 2218 | stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst); | 
|  | 2219 | if (!SimplifiedCond.hasValue()) | 
|  | 2220 | return true; | 
|  | 2221 | AssumedNoUBInsts.insert(&I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2222 | return true; | 
|  | 2223 | }; | 
|  | 2224 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2225 | A.checkForAllInstructions(InspectMemAccessInstForUB, *this, | 
|  | 2226 | {Instruction::Load, Instruction::Store, | 
|  | 2227 | Instruction::AtomicCmpXchg, | 
|  | 2228 | Instruction::AtomicRMW}); | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2229 | A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br}); | 
|  | 2230 | if (NoUBPrevSize != AssumedNoUBInsts.size() || | 
|  | 2231 | UBPrevSize != KnownUBInsts.size()) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2232 | return ChangeStatus::CHANGED; | 
|  | 2233 | return ChangeStatus::UNCHANGED; | 
|  | 2234 | } | 
|  | 2235 |  | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2236 | bool isKnownToCauseUB(Instruction *I) const override { | 
|  | 2237 | return KnownUBInsts.count(I); | 
|  | 2238 | } | 
|  | 2239 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2240 | bool isAssumedToCauseUB(Instruction *I) const override { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2241 | // In simple words, if an instruction is not in the assumed to _not_ | 
|  | 2242 | // cause UB, then it is assumed UB (that includes those | 
|  | 2243 | // in the KnownUBInsts set). The rest is boilerplate | 
|  | 2244 | // is to ensure that it is one of the instructions we test | 
|  | 2245 | // for UB. | 
|  | 2246 |  | 
|  | 2247 | switch (I->getOpcode()) { | 
|  | 2248 | case Instruction::Load: | 
|  | 2249 | case Instruction::Store: | 
|  | 2250 | case Instruction::AtomicCmpXchg: | 
|  | 2251 | case Instruction::AtomicRMW: | 
|  | 2252 | return !AssumedNoUBInsts.count(I); | 
|  | 2253 | case Instruction::Br: { | 
|  | 2254 | auto BrInst = cast<BranchInst>(I); | 
|  | 2255 | if (BrInst->isUnconditional()) | 
|  | 2256 | return false; | 
|  | 2257 | return !AssumedNoUBInsts.count(I); | 
|  | 2258 | } break; | 
|  | 2259 | default: | 
|  | 2260 | return false; | 
|  | 2261 | } | 
|  | 2262 | return false; | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2263 | } | 
|  | 2264 |  | 
|  | 2265 | ChangeStatus manifest(Attributor &A) override { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2266 | if (KnownUBInsts.empty()) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2267 | return ChangeStatus::UNCHANGED; | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2268 | for (Instruction *I : KnownUBInsts) | 
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 2269 | A.changeToUnreachableAfterManifest(I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2270 | return ChangeStatus::CHANGED; | 
|  | 2271 | } | 
|  | 2272 |  | 
|  | 2273 | /// See AbstractAttribute::getAsStr() | 
|  | 2274 | const std::string getAsStr() const override { | 
|  | 2275 | return getAssumed() ? "undefined-behavior" : "no-ub"; | 
|  | 2276 | } | 
|  | 2277 |  | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2278 | /// Note: The correctness of this analysis depends on the fact that the | 
|  | 2279 | /// following 2 sets will stop changing after some point. | 
|  | 2280 | /// "Change" here means that their size changes. | 
|  | 2281 | /// The size of each set is monotonically increasing | 
|  | 2282 | /// (we only add items to them) and it is upper bounded by the number of | 
|  | 2283 | /// instructions in the processed function (we can never save more | 
|  | 2284 | /// elements in either set than this number). Hence, at some point, | 
|  | 2285 | /// they will stop increasing. | 
|  | 2286 | /// Consequently, at some point, both sets will have stopped | 
|  | 2287 | /// changing, effectively making the analysis reach a fixpoint. | 
|  | 2288 |  | 
|  | 2289 | /// Note: These 2 sets are disjoint and an instruction can be considered | 
|  | 2290 | /// one of 3 things: | 
|  | 2291 | /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in | 
|  | 2292 | ///    the KnownUBInsts set. | 
|  | 2293 | /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior | 
|  | 2294 | ///    has a reason to assume it). | 
|  | 2295 | /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior | 
|  | 2296 | ///    could not find a reason to assume or prove that it can cause UB, | 
|  | 2297 | ///    hence it assumes it doesn't. We have a set for these instructions | 
|  | 2298 | ///    so that we don't reprocess them in every update. | 
|  | 2299 | ///    Note however that instructions in this set may cause UB. | 
|  | 2300 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2301 | protected: | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2302 | /// A set of all live instructions _known_ to cause UB. | 
|  | 2303 | SmallPtrSet<Instruction *, 8> KnownUBInsts; | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2304 |  | 
|  | 2305 | private: | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2306 | /// A set of all the (live) instructions that are assumed to _not_ cause UB. | 
|  | 2307 | SmallPtrSet<Instruction *, 8> AssumedNoUBInsts; | 
|  | 2308 |  | 
|  | 2309 | // Should be called on updates in which if we're processing an instruction | 
|  | 2310 | // \p I that depends on a value \p V, one of the following has to happen: | 
|  | 2311 | // - If the value is assumed, then stop. | 
|  | 2312 | // - If the value is known but undef, then consider it UB. | 
|  | 2313 | // - Otherwise, do specific processing with the simplified value. | 
|  | 2314 | // We return None in the first 2 cases to signify that an appropriate | 
|  | 2315 | // action was taken and the caller should stop. | 
|  | 2316 | // Otherwise, we return the simplified value that the caller should | 
|  | 2317 | // use for specific processing. | 
|  | 2318 | Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V, | 
|  | 2319 | Instruction *I) { | 
|  | 2320 | const auto &ValueSimplifyAA = | 
|  | 2321 | A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V)); | 
|  | 2322 | Optional<Value *> SimplifiedV = | 
|  | 2323 | ValueSimplifyAA.getAssumedSimplifiedValue(A); | 
|  | 2324 | if (!ValueSimplifyAA.isKnown()) { | 
|  | 2325 | // Don't depend on assumed values. | 
|  | 2326 | return llvm::None; | 
|  | 2327 | } | 
|  | 2328 | if (!SimplifiedV.hasValue()) { | 
|  | 2329 | // If it is known (which we tested above) but it doesn't have a value, | 
|  | 2330 | // then we can assume `undef` and hence the instruction is UB. | 
|  | 2331 | KnownUBInsts.insert(I); | 
|  | 2332 | return llvm::None; | 
|  | 2333 | } | 
|  | 2334 | Value *Val = SimplifiedV.getValue(); | 
|  | 2335 | if (isa<UndefValue>(Val)) { | 
|  | 2336 | KnownUBInsts.insert(I); | 
|  | 2337 | return llvm::None; | 
|  | 2338 | } | 
|  | 2339 | return Val; | 
|  | 2340 | } | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2341 | }; | 
|  | 2342 |  | 
|  | 2343 | struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl { | 
|  | 2344 | AAUndefinedBehaviorFunction(const IRPosition &IRP) | 
|  | 2345 | : AAUndefinedBehaviorImpl(IRP) {} | 
|  | 2346 |  | 
|  | 2347 | /// See AbstractAttribute::trackStatistics() | 
|  | 2348 | void trackStatistics() const override { | 
|  | 2349 | STATS_DECL(UndefinedBehaviorInstruction, Instruction, | 
|  | 2350 | "Number of instructions known to have UB"); | 
|  | 2351 | BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) += | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2352 | KnownUBInsts.size(); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2353 | } | 
|  | 2354 | }; | 
|  | 2355 |  | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2356 | /// ------------------------ Will-Return Attributes ---------------------------- | 
|  | 2357 |  | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2358 | // Helper function that checks whether a function has any cycle. | 
|  | 2359 | // TODO: Replace with more efficent code | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2360 | static bool containsCycle(Function &F) { | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2361 | SmallPtrSet<BasicBlock *, 32> Visited; | 
|  | 2362 |  | 
|  | 2363 | // Traverse BB by dfs and check whether successor is already visited. | 
|  | 2364 | for (BasicBlock *BB : depth_first(&F)) { | 
|  | 2365 | Visited.insert(BB); | 
|  | 2366 | for (auto *SuccBB : successors(BB)) { | 
|  | 2367 | if (Visited.count(SuccBB)) | 
|  | 2368 | return true; | 
|  | 2369 | } | 
|  | 2370 | } | 
|  | 2371 | return false; | 
|  | 2372 | } | 
|  | 2373 |  | 
|  | 2374 | // Helper function that checks the function have a loop which might become an | 
|  | 2375 | // endless loop | 
|  | 2376 | // FIXME: Any cycle is regarded as endless loop for now. | 
|  | 2377 | //        We have to allow some patterns. | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2378 | static bool containsPossiblyEndlessLoop(Function *F) { | 
|  | 2379 | return !F || !F->hasExactDefinition() || containsCycle(*F); | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2380 | } | 
|  | 2381 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2382 | struct AAWillReturnImpl : public AAWillReturn { | 
|  | 2383 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2384 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2385 | /// See AbstractAttribute::initialize(...). | 
|  | 2386 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2387 | AAWillReturn::initialize(A); | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2388 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2389 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2390 | if (containsPossiblyEndlessLoop(F)) | 
|  | 2391 | indicatePessimisticFixpoint(); | 
|  | 2392 | } | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2393 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2394 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2395 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2396 | auto CheckForWillReturn = [&](Instruction &I) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2397 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); | 
|  | 2398 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); | 
|  | 2399 | if (WillReturnAA.isKnownWillReturn()) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2400 | return true; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2401 | if (!WillReturnAA.isAssumedWillReturn()) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2402 | return false; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2403 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); | 
|  | 2404 | return NoRecurseAA.isAssumedNoRecurse(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2405 | }; | 
|  | 2406 |  | 
|  | 2407 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) | 
|  | 2408 | return indicatePessimisticFixpoint(); | 
|  | 2409 |  | 
|  | 2410 | return ChangeStatus::UNCHANGED; | 
|  | 2411 | } | 
|  | 2412 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2413 | /// See AbstractAttribute::getAsStr() | 
|  | 2414 | const std::string getAsStr() const override { | 
|  | 2415 | return getAssumed() ? "willreturn" : "may-noreturn"; | 
|  | 2416 | } | 
|  | 2417 | }; | 
|  | 2418 |  | 
|  | 2419 | struct AAWillReturnFunction final : AAWillReturnImpl { | 
|  | 2420 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} | 
|  | 2421 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2422 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2423 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2424 | }; | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2425 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2426 | /// WillReturn attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2427 | struct AAWillReturnCallSite final : AAWillReturnImpl { | 
|  | 2428 | AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} | 
|  | 2429 |  | 
|  | 2430 | /// See AbstractAttribute::initialize(...). | 
|  | 2431 | void initialize(Attributor &A) override { | 
|  | 2432 | AAWillReturnImpl::initialize(A); | 
|  | 2433 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2434 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2435 | indicatePessimisticFixpoint(); | 
|  | 2436 | } | 
|  | 2437 |  | 
|  | 2438 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2439 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2440 | // TODO: Once we have call site specific value information we can provide | 
|  | 2441 | //       call site specific liveness information and then it makes | 
|  | 2442 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2443 | //       redirecting requests to the callee argument. | 
|  | 2444 | Function *F = getAssociatedFunction(); | 
|  | 2445 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 2446 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); | 
|  | 2447 | return clampStateAndIndicateChange( | 
|  | 2448 | getState(), | 
|  | 2449 | static_cast<const AAWillReturn::StateType &>(FnAA.getState())); | 
|  | 2450 | } | 
|  | 2451 |  | 
|  | 2452 | /// See AbstractAttribute::trackStatistics() | 
|  | 2453 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } | 
|  | 2454 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2455 |  | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2456 | /// -------------------AAReachability Attribute-------------------------- | 
|  | 2457 |  | 
|  | 2458 | struct AAReachabilityImpl : AAReachability { | 
|  | 2459 | AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {} | 
|  | 2460 |  | 
|  | 2461 | const std::string getAsStr() const override { | 
|  | 2462 | // TODO: Return the number of reachable queries. | 
|  | 2463 | return "reachable"; | 
|  | 2464 | } | 
|  | 2465 |  | 
|  | 2466 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2467 | void initialize(Attributor &A) override { indicatePessimisticFixpoint(); } | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2468 |  | 
|  | 2469 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2470 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2471 | return indicatePessimisticFixpoint(); | 
|  | 2472 | } | 
|  | 2473 | }; | 
|  | 2474 |  | 
|  | 2475 | struct AAReachabilityFunction final : public AAReachabilityImpl { | 
|  | 2476 | AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {} | 
|  | 2477 |  | 
|  | 2478 | /// See AbstractAttribute::trackStatistics() | 
|  | 2479 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); } | 
|  | 2480 | }; | 
|  | 2481 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2482 | /// ------------------------ NoAlias Argument Attribute ------------------------ | 
|  | 2483 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2484 | struct AANoAliasImpl : AANoAlias { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2485 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2486 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2487 | const std::string getAsStr() const override { | 
|  | 2488 | return getAssumed() ? "noalias" : "may-alias"; | 
|  | 2489 | } | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2490 | }; | 
|  | 2491 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2492 | /// NoAlias attribute for a floating value. | 
|  | 2493 | struct AANoAliasFloating final : AANoAliasImpl { | 
|  | 2494 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2495 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2496 | /// See AbstractAttribute::initialize(...). | 
|  | 2497 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2498 | AANoAliasImpl::initialize(A); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2499 | Value &Val = getAssociatedValue(); | 
|  | 2500 | if (isa<AllocaInst>(Val)) | 
|  | 2501 | indicateOptimisticFixpoint(); | 
| Johannes Doerfert | 24ae77e | 2020-01-27 22:19:11 -0600 | [diff] [blame] | 2502 | else if (isa<ConstantPointerNull>(Val) && | 
|  | 2503 | !NullPointerIsDefined(getAnchorScope(), | 
|  | 2504 | Val.getType()->getPointerAddressSpace())) | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2505 | indicateOptimisticFixpoint(); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2506 | } | 
|  | 2507 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2508 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2509 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2510 | // TODO: Implement this. | 
|  | 2511 | return indicatePessimisticFixpoint(); | 
|  | 2512 | } | 
|  | 2513 |  | 
|  | 2514 | /// See AbstractAttribute::trackStatistics() | 
|  | 2515 | void trackStatistics() const override { | 
|  | 2516 | STATS_DECLTRACK_FLOATING_ATTR(noalias) | 
|  | 2517 | } | 
|  | 2518 | }; | 
|  | 2519 |  | 
|  | 2520 | /// NoAlias attribute for an argument. | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2521 | struct AANoAliasArgument final | 
|  | 2522 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2523 | using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>; | 
|  | 2524 | AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {} | 
|  | 2525 |  | 
| Johannes Doerfert | 2baf000 | 2020-01-12 00:18:07 -0600 | [diff] [blame] | 2526 | /// See AbstractAttribute::initialize(...). | 
|  | 2527 | void initialize(Attributor &A) override { | 
|  | 2528 | Base::initialize(A); | 
|  | 2529 | // See callsite argument attribute and callee argument attribute. | 
|  | 2530 | if (hasAttr({Attribute::ByVal})) | 
|  | 2531 | indicateOptimisticFixpoint(); | 
|  | 2532 | } | 
|  | 2533 |  | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2534 | /// See AbstractAttribute::update(...). | 
|  | 2535 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2536 | // We have to make sure no-alias on the argument does not break | 
|  | 2537 | // synchronization when this is a callback argument, see also [1] below. | 
|  | 2538 | // If synchronization cannot be affected, we delegate to the base updateImpl | 
|  | 2539 | // function, otherwise we give up for now. | 
|  | 2540 |  | 
|  | 2541 | // If the function is no-sync, no-alias cannot break synchronization. | 
|  | 2542 | const auto &NoSyncAA = A.getAAFor<AANoSync>( | 
|  | 2543 | *this, IRPosition::function_scope(getIRPosition())); | 
|  | 2544 | if (NoSyncAA.isAssumedNoSync()) | 
|  | 2545 | return Base::updateImpl(A); | 
|  | 2546 |  | 
|  | 2547 | // If the argument is read-only, no-alias cannot break synchronization. | 
|  | 2548 | const auto &MemBehaviorAA = | 
|  | 2549 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); | 
|  | 2550 | if (MemBehaviorAA.isAssumedReadOnly()) | 
|  | 2551 | return Base::updateImpl(A); | 
|  | 2552 |  | 
|  | 2553 | // If the argument is never passed through callbacks, no-alias cannot break | 
|  | 2554 | // synchronization. | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2555 | bool AllCallSitesKnown; | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2556 | if (A.checkForAllCallSites( | 
|  | 2557 | [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this, | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2558 | true, AllCallSitesKnown)) | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2559 | return Base::updateImpl(A); | 
|  | 2560 |  | 
|  | 2561 | // TODO: add no-alias but make sure it doesn't break synchronization by | 
|  | 2562 | // introducing fake uses. See: | 
|  | 2563 | // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel, | 
|  | 2564 | //     International Workshop on OpenMP 2018, | 
|  | 2565 | //     http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf | 
|  | 2566 |  | 
|  | 2567 | return indicatePessimisticFixpoint(); | 
|  | 2568 | } | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2569 |  | 
|  | 2570 | /// See AbstractAttribute::trackStatistics() | 
|  | 2571 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } | 
|  | 2572 | }; | 
|  | 2573 |  | 
|  | 2574 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { | 
|  | 2575 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2576 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2577 | /// See AbstractAttribute::initialize(...). | 
|  | 2578 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 2579 | // See callsite argument attribute and callee argument attribute. | 
|  | 2580 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 2581 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) | 
|  | 2582 | indicateOptimisticFixpoint(); | 
| Johannes Doerfert | 24ae77e | 2020-01-27 22:19:11 -0600 | [diff] [blame] | 2583 | Value &Val = getAssociatedValue(); | 
|  | 2584 | if (isa<ConstantPointerNull>(Val) && | 
|  | 2585 | !NullPointerIsDefined(getAnchorScope(), | 
|  | 2586 | Val.getType()->getPointerAddressSpace())) | 
|  | 2587 | indicateOptimisticFixpoint(); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2588 | } | 
|  | 2589 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2590 | /// Determine if the underlying value may alias with the call site argument | 
|  | 2591 | /// \p OtherArgNo of \p ICS (= the underlying call site). | 
|  | 2592 | bool mayAliasWithArgument(Attributor &A, AAResults *&AAR, | 
|  | 2593 | const AAMemoryBehavior &MemBehaviorAA, | 
|  | 2594 | ImmutableCallSite ICS, unsigned OtherArgNo) { | 
|  | 2595 | // We do not need to worry about aliasing with the underlying IRP. | 
|  | 2596 | if (this->getArgNo() == (int)OtherArgNo) | 
|  | 2597 | return false; | 
|  | 2598 |  | 
|  | 2599 | // If it is not a pointer or pointer vector we do not alias. | 
|  | 2600 | const Value *ArgOp = ICS.getArgOperand(OtherArgNo); | 
|  | 2601 | if (!ArgOp->getType()->isPtrOrPtrVectorTy()) | 
|  | 2602 | return false; | 
|  | 2603 |  | 
|  | 2604 | auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 2605 | *this, IRPosition::callsite_argument(ICS, OtherArgNo), | 
|  | 2606 | /* TrackDependence */ false); | 
|  | 2607 |  | 
|  | 2608 | // If the argument is readnone, there is no read-write aliasing. | 
|  | 2609 | if (ICSArgMemBehaviorAA.isAssumedReadNone()) { | 
|  | 2610 | A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); | 
|  | 2611 | return false; | 
|  | 2612 | } | 
|  | 2613 |  | 
|  | 2614 | // If the argument is readonly and the underlying value is readonly, there | 
|  | 2615 | // is no read-write aliasing. | 
|  | 2616 | bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly(); | 
|  | 2617 | if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) { | 
|  | 2618 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); | 
|  | 2619 | A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); | 
|  | 2620 | return false; | 
|  | 2621 | } | 
|  | 2622 |  | 
|  | 2623 | // We have to utilize actual alias analysis queries so we need the object. | 
|  | 2624 | if (!AAR) | 
|  | 2625 | AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope()); | 
|  | 2626 |  | 
|  | 2627 | // Try to rule it out at the call site. | 
|  | 2628 | bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp); | 
|  | 2629 | LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between " | 
|  | 2630 | "callsite arguments: " | 
|  | 2631 | << getAssociatedValue() << " " << *ArgOp << " => " | 
|  | 2632 | << (IsAliasing ? "" : "no-") << "alias \n"); | 
|  | 2633 |  | 
|  | 2634 | return IsAliasing; | 
|  | 2635 | } | 
|  | 2636 |  | 
|  | 2637 | bool | 
|  | 2638 | isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR, | 
|  | 2639 | const AAMemoryBehavior &MemBehaviorAA, | 
|  | 2640 | const AANoAlias &NoAliasAA) { | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2641 | // We can deduce "noalias" if the following conditions hold. | 
|  | 2642 | // (i)   Associated value is assumed to be noalias in the definition. | 
|  | 2643 | // (ii)  Associated value is assumed to be no-capture in all the uses | 
|  | 2644 | //       possibly executed before this callsite. | 
|  | 2645 | // (iii) There is no other pointer argument which could alias with the | 
|  | 2646 | //       value. | 
|  | 2647 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2648 | bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias(); | 
|  | 2649 | if (!AssociatedValueIsNoAliasAtDef) { | 
|  | 2650 | LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue() | 
|  | 2651 | << " is not no-alias at the definition\n"); | 
|  | 2652 | return false; | 
|  | 2653 | } | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2654 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2655 | const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); | 
|  | 2656 | auto &NoCaptureAA = | 
|  | 2657 | A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false); | 
|  | 2658 | // Check whether the value is captured in the scope using AANoCapture. | 
|  | 2659 | // FIXME: This is conservative though, it is better to look at CFG and | 
|  | 2660 | //        check only uses possibly executed before this callsite. | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2661 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 2662 | LLVM_DEBUG( | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2663 | dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2664 | << " cannot be noalias as it is potentially captured\n"); | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2665 | return false; | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2666 | } | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2667 | A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL); | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2668 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2669 | // Check there is no other pointer argument which could alias with the | 
|  | 2670 | // value passed at this call site. | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2671 | // TODO: AbstractCallSite | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2672 | ImmutableCallSite ICS(&getAnchorValue()); | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2673 | for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands(); | 
|  | 2674 | OtherArgNo++) | 
|  | 2675 | if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo)) | 
|  | 2676 | return false; | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2677 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2678 | return true; | 
|  | 2679 | } | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2680 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2681 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2682 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2683 | // If the argument is readnone we are done as there are no accesses via the | 
|  | 2684 | // argument. | 
|  | 2685 | auto &MemBehaviorAA = | 
|  | 2686 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), | 
|  | 2687 | /* TrackDependence */ false); | 
|  | 2688 | if (MemBehaviorAA.isAssumedReadNone()) { | 
|  | 2689 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); | 
|  | 2690 | return ChangeStatus::UNCHANGED; | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2691 | } | 
|  | 2692 |  | 
| Johannes Doerfert | 53992c7 | 2020-01-27 22:24:32 -0600 | [diff] [blame] | 2693 | const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); | 
|  | 2694 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP, | 
|  | 2695 | /* TrackDependence */ false); | 
|  | 2696 |  | 
|  | 2697 | AAResults *AAR = nullptr; | 
|  | 2698 | if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA, | 
|  | 2699 | NoAliasAA)) { | 
|  | 2700 | LLVM_DEBUG( | 
|  | 2701 | dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n"); | 
|  | 2702 | return ChangeStatus::UNCHANGED; | 
|  | 2703 | } | 
|  | 2704 |  | 
|  | 2705 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2706 | } | 
|  | 2707 |  | 
|  | 2708 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2709 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2710 | }; | 
|  | 2711 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2712 | /// NoAlias attribute for function return value. | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2713 | struct AANoAliasReturned final : AANoAliasImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2714 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2715 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2716 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2717 | virtual ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2718 |  | 
|  | 2719 | auto CheckReturnValue = [&](Value &RV) -> bool { | 
|  | 2720 | if (Constant *C = dyn_cast<Constant>(&RV)) | 
|  | 2721 | if (C->isNullValue() || isa<UndefValue>(C)) | 
|  | 2722 | return true; | 
|  | 2723 |  | 
|  | 2724 | /// For now, we can only deduce noalias if we have call sites. | 
|  | 2725 | /// FIXME: add more support. | 
|  | 2726 | ImmutableCallSite ICS(&RV); | 
|  | 2727 | if (!ICS) | 
|  | 2728 | return false; | 
|  | 2729 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2730 | const IRPosition &RVPos = IRPosition::value(RV); | 
|  | 2731 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2732 | if (!NoAliasAA.isAssumedNoAlias()) | 
|  | 2733 | return false; | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2734 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2735 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); | 
|  | 2736 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2737 | }; | 
|  | 2738 |  | 
|  | 2739 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) | 
|  | 2740 | return indicatePessimisticFixpoint(); | 
|  | 2741 |  | 
|  | 2742 | return ChangeStatus::UNCHANGED; | 
|  | 2743 | } | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2744 |  | 
|  | 2745 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2746 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2747 | }; | 
|  | 2748 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2749 | /// NoAlias attribute deduction for a call site return value. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2750 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { | 
|  | 2751 | AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2752 |  | 
|  | 2753 | /// See AbstractAttribute::initialize(...). | 
|  | 2754 | void initialize(Attributor &A) override { | 
|  | 2755 | AANoAliasImpl::initialize(A); | 
|  | 2756 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2757 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2758 | indicatePessimisticFixpoint(); | 
|  | 2759 | } | 
|  | 2760 |  | 
|  | 2761 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2762 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2763 | // TODO: Once we have call site specific value information we can provide | 
|  | 2764 | //       call site specific liveness information and then it makes | 
|  | 2765 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2766 | //       redirecting requests to the callee argument. | 
|  | 2767 | Function *F = getAssociatedFunction(); | 
|  | 2768 | const IRPosition &FnPos = IRPosition::returned(*F); | 
|  | 2769 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); | 
|  | 2770 | return clampStateAndIndicateChange( | 
|  | 2771 | getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); | 
|  | 2772 | } | 
|  | 2773 |  | 
|  | 2774 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2775 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2776 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2777 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2778 | /// -------------------AAIsDead Function Attribute----------------------- | 
|  | 2779 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2780 | struct AAIsDeadValueImpl : public AAIsDead { | 
|  | 2781 | AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2782 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2783 | /// See AAIsDead::isAssumedDead(). | 
|  | 2784 | bool isAssumedDead() const override { return getAssumed(); } | 
|  | 2785 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2786 | /// See AAIsDead::isKnownDead(). | 
|  | 2787 | bool isKnownDead() const override { return getKnown(); } | 
|  | 2788 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2789 | /// See AAIsDead::isAssumedDead(BasicBlock *). | 
|  | 2790 | bool isAssumedDead(const BasicBlock *BB) const override { return false; } | 
|  | 2791 |  | 
|  | 2792 | /// See AAIsDead::isKnownDead(BasicBlock *). | 
|  | 2793 | bool isKnownDead(const BasicBlock *BB) const override { return false; } | 
|  | 2794 |  | 
|  | 2795 | /// See AAIsDead::isAssumedDead(Instruction *I). | 
|  | 2796 | bool isAssumedDead(const Instruction *I) const override { | 
|  | 2797 | return I == getCtxI() && isAssumedDead(); | 
|  | 2798 | } | 
|  | 2799 |  | 
|  | 2800 | /// See AAIsDead::isKnownDead(Instruction *I). | 
|  | 2801 | bool isKnownDead(const Instruction *I) const override { | 
|  | 2802 | return I == getCtxI() && getKnown(); | 
|  | 2803 | } | 
|  | 2804 |  | 
|  | 2805 | /// See AbstractAttribute::getAsStr(). | 
|  | 2806 | const std::string getAsStr() const override { | 
|  | 2807 | return isAssumedDead() ? "assumed-dead" : "assumed-live"; | 
|  | 2808 | } | 
|  | 2809 | }; | 
|  | 2810 |  | 
|  | 2811 | struct AAIsDeadFloating : public AAIsDeadValueImpl { | 
|  | 2812 | AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2813 |  | 
|  | 2814 | /// See AbstractAttribute::initialize(...). | 
|  | 2815 | void initialize(Attributor &A) override { | 
|  | 2816 | if (Instruction *I = dyn_cast<Instruction>(&getAssociatedValue())) | 
|  | 2817 | if (!wouldInstructionBeTriviallyDead(I)) | 
|  | 2818 | indicatePessimisticFixpoint(); | 
|  | 2819 | if (isa<UndefValue>(getAssociatedValue())) | 
|  | 2820 | indicatePessimisticFixpoint(); | 
|  | 2821 | } | 
|  | 2822 |  | 
|  | 2823 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2824 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2825 | auto UsePred = [&](const Use &U, bool &Follow) { | 
|  | 2826 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
|  | 2827 | if (CallSite CS = CallSite(UserI)) { | 
|  | 2828 | if (!CS.isArgOperand(&U)) | 
|  | 2829 | return false; | 
|  | 2830 | const IRPosition &CSArgPos = | 
|  | 2831 | IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); | 
|  | 2832 | const auto &CSArgIsDead = A.getAAFor<AAIsDead>(*this, CSArgPos); | 
|  | 2833 | return CSArgIsDead.isAssumedDead(); | 
|  | 2834 | } | 
|  | 2835 | if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { | 
|  | 2836 | const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); | 
|  | 2837 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, RetPos); | 
|  | 2838 | return RetIsDeadAA.isAssumedDead(); | 
|  | 2839 | } | 
|  | 2840 | Follow = true; | 
|  | 2841 | return wouldInstructionBeTriviallyDead(UserI); | 
|  | 2842 | }; | 
|  | 2843 |  | 
|  | 2844 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) | 
|  | 2845 | return indicatePessimisticFixpoint(); | 
|  | 2846 | return ChangeStatus::UNCHANGED; | 
|  | 2847 | } | 
|  | 2848 |  | 
|  | 2849 | /// See AbstractAttribute::manifest(...). | 
|  | 2850 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2851 | Value &V = getAssociatedValue(); | 
|  | 2852 | if (auto *I = dyn_cast<Instruction>(&V)) | 
|  | 2853 | if (wouldInstructionBeTriviallyDead(I)) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2854 | A.deleteAfterManifest(*I); | 
|  | 2855 | return ChangeStatus::CHANGED; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2856 | } | 
|  | 2857 |  | 
|  | 2858 | if (V.use_empty()) | 
|  | 2859 | return ChangeStatus::UNCHANGED; | 
|  | 2860 |  | 
|  | 2861 | UndefValue &UV = *UndefValue::get(V.getType()); | 
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 2862 | bool AnyChange = A.changeValueAfterManifest(V, UV); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2863 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
|  | 2864 | } | 
|  | 2865 |  | 
|  | 2866 | /// See AbstractAttribute::trackStatistics() | 
|  | 2867 | void trackStatistics() const override { | 
|  | 2868 | STATS_DECLTRACK_FLOATING_ATTR(IsDead) | 
|  | 2869 | } | 
|  | 2870 | }; | 
|  | 2871 |  | 
|  | 2872 | struct AAIsDeadArgument : public AAIsDeadFloating { | 
|  | 2873 | AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} | 
|  | 2874 |  | 
|  | 2875 | /// See AbstractAttribute::initialize(...). | 
|  | 2876 | void initialize(Attributor &A) override { | 
|  | 2877 | if (!getAssociatedFunction()->hasExactDefinition()) | 
|  | 2878 | indicatePessimisticFixpoint(); | 
|  | 2879 | } | 
|  | 2880 |  | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 2881 | /// See AbstractAttribute::manifest(...). | 
|  | 2882 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2883 | ChangeStatus Changed = AAIsDeadFloating::manifest(A); | 
|  | 2884 | Argument &Arg = *getAssociatedArgument(); | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 2885 | if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {})) | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 2886 | if (A.registerFunctionSignatureRewrite( | 
|  | 2887 | Arg, /* ReplacementTypes */ {}, | 
|  | 2888 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{}, | 
|  | 2889 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) | 
|  | 2890 | return ChangeStatus::CHANGED; | 
|  | 2891 | return Changed; | 
|  | 2892 | } | 
|  | 2893 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2894 | /// See AbstractAttribute::trackStatistics() | 
|  | 2895 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } | 
|  | 2896 | }; | 
|  | 2897 |  | 
|  | 2898 | struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { | 
|  | 2899 | AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2900 |  | 
|  | 2901 | /// See AbstractAttribute::initialize(...). | 
|  | 2902 | void initialize(Attributor &A) override { | 
|  | 2903 | if (isa<UndefValue>(getAssociatedValue())) | 
|  | 2904 | indicatePessimisticFixpoint(); | 
|  | 2905 | } | 
|  | 2906 |  | 
|  | 2907 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2908 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2909 | // TODO: Once we have call site specific value information we can provide | 
|  | 2910 | //       call site specific liveness information and then it makes | 
|  | 2911 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2912 | //       redirecting requests to the callee argument. | 
|  | 2913 | Argument *Arg = getAssociatedArgument(); | 
|  | 2914 | if (!Arg) | 
|  | 2915 | return indicatePessimisticFixpoint(); | 
|  | 2916 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 2917 | auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); | 
|  | 2918 | return clampStateAndIndicateChange( | 
|  | 2919 | getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); | 
|  | 2920 | } | 
|  | 2921 |  | 
|  | 2922 | /// See AbstractAttribute::manifest(...). | 
|  | 2923 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2924 | CallBase &CB = cast<CallBase>(getAnchorValue()); | 
|  | 2925 | Use &U = CB.getArgOperandUse(getArgNo()); | 
|  | 2926 | assert(!isa<UndefValue>(U.get()) && | 
|  | 2927 | "Expected undef values to be filtered out!"); | 
|  | 2928 | UndefValue &UV = *UndefValue::get(U->getType()); | 
|  | 2929 | if (A.changeUseAfterManifest(U, UV)) | 
|  | 2930 | return ChangeStatus::CHANGED; | 
|  | 2931 | return ChangeStatus::UNCHANGED; | 
|  | 2932 | } | 
|  | 2933 |  | 
|  | 2934 | /// See AbstractAttribute::trackStatistics() | 
|  | 2935 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } | 
|  | 2936 | }; | 
|  | 2937 |  | 
|  | 2938 | struct AAIsDeadReturned : public AAIsDeadValueImpl { | 
|  | 2939 | AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2940 |  | 
|  | 2941 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2942 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2943 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2944 | bool AllKnownDead = true; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2945 | auto PredForCallSite = [&](AbstractCallSite ACS) { | 
|  | 2946 | if (ACS.isCallbackCall()) | 
|  | 2947 | return false; | 
|  | 2948 | const IRPosition &CSRetPos = | 
|  | 2949 | IRPosition::callsite_returned(ACS.getCallSite()); | 
|  | 2950 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, CSRetPos); | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2951 | AllKnownDead &= RetIsDeadAA.isKnownDead(); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2952 | return RetIsDeadAA.isAssumedDead(); | 
|  | 2953 | }; | 
|  | 2954 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2955 | bool AllCallSitesKnown; | 
|  | 2956 | if (!A.checkForAllCallSites(PredForCallSite, *this, true, | 
|  | 2957 | AllCallSitesKnown)) | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2958 | return indicatePessimisticFixpoint(); | 
|  | 2959 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 2960 | if (AllCallSitesKnown && AllKnownDead) | 
|  | 2961 | indicateOptimisticFixpoint(); | 
|  | 2962 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2963 | return ChangeStatus::UNCHANGED; | 
|  | 2964 | } | 
|  | 2965 |  | 
|  | 2966 | /// See AbstractAttribute::manifest(...). | 
|  | 2967 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2968 | // TODO: Rewrite the signature to return void? | 
|  | 2969 | bool AnyChange = false; | 
|  | 2970 | UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); | 
|  | 2971 | auto RetInstPred = [&](Instruction &I) { | 
|  | 2972 | ReturnInst &RI = cast<ReturnInst>(I); | 
|  | 2973 | if (!isa<UndefValue>(RI.getReturnValue())) | 
|  | 2974 | AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); | 
|  | 2975 | return true; | 
|  | 2976 | }; | 
|  | 2977 | A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); | 
|  | 2978 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
|  | 2979 | } | 
|  | 2980 |  | 
|  | 2981 | /// See AbstractAttribute::trackStatistics() | 
|  | 2982 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } | 
|  | 2983 | }; | 
|  | 2984 |  | 
|  | 2985 | struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { | 
|  | 2986 | AAIsDeadCallSiteReturned(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} | 
|  | 2987 |  | 
|  | 2988 | /// See AbstractAttribute::initialize(...). | 
|  | 2989 | void initialize(Attributor &A) override {} | 
|  | 2990 |  | 
|  | 2991 | /// See AbstractAttribute::trackStatistics() | 
|  | 2992 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(IsDead) } | 
|  | 2993 | }; | 
|  | 2994 |  | 
|  | 2995 | struct AAIsDeadFunction : public AAIsDead { | 
|  | 2996 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} | 
|  | 2997 |  | 
|  | 2998 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2999 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 3000 | const Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3001 | if (F && !F->isDeclaration()) { | 
|  | 3002 | ToBeExploredFrom.insert(&F->getEntryBlock().front()); | 
|  | 3003 | assumeLive(A, F->getEntryBlock()); | 
|  | 3004 | } | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3005 | } | 
|  | 3006 |  | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 3007 | /// See AbstractAttribute::getAsStr(). | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3008 | const std::string getAsStr() const override { | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 3009 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3010 | std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3011 | std::to_string(ToBeExploredFrom.size()) + "][#KDE " + | 
|  | 3012 | std::to_string(KnownDeadEnds.size()) + "]"; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3013 | } | 
|  | 3014 |  | 
|  | 3015 | /// See AbstractAttribute::manifest(...). | 
|  | 3016 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3017 | assert(getState().isValidState() && | 
|  | 3018 | "Attempted to manifest an invalid state!"); | 
|  | 3019 |  | 
|  | 3020 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3021 | Function &F = *getAssociatedFunction(); | 
|  | 3022 |  | 
|  | 3023 | if (AssumedLiveBlocks.empty()) { | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 3024 | A.deleteAfterManifest(F); | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3025 | return ChangeStatus::CHANGED; | 
|  | 3026 | } | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3027 |  | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 3028 | // Flag to determine if we can change an invoke to a call assuming the | 
|  | 3029 | // callee is nounwind. This is not possible if the personality of the | 
|  | 3030 | // function allows to catch asynchronous exceptions. | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3031 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3032 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3033 | KnownDeadEnds.set_union(ToBeExploredFrom); | 
|  | 3034 | for (const Instruction *DeadEndI : KnownDeadEnds) { | 
|  | 3035 | auto *CB = dyn_cast<CallBase>(DeadEndI); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3036 | if (!CB) | 
|  | 3037 | continue; | 
|  | 3038 | const auto &NoReturnAA = | 
|  | 3039 | A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); | 
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 3040 | bool MayReturn = !NoReturnAA.isAssumedNoReturn(); | 
|  | 3041 | if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3042 | continue; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3043 |  | 
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 3044 | if (auto *II = dyn_cast<InvokeInst>(DeadEndI)) | 
|  | 3045 | A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II)); | 
|  | 3046 | else | 
|  | 3047 | A.changeToUnreachableAfterManifest( | 
|  | 3048 | const_cast<Instruction *>(DeadEndI->getNextNode())); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3049 | HasChanged = ChangeStatus::CHANGED; | 
|  | 3050 | } | 
|  | 3051 |  | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 3052 | for (BasicBlock &BB : F) | 
|  | 3053 | if (!AssumedLiveBlocks.count(&BB)) | 
|  | 3054 | A.deleteAfterManifest(BB); | 
|  | 3055 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3056 | return HasChanged; | 
|  | 3057 | } | 
|  | 3058 |  | 
|  | 3059 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3060 | ChangeStatus updateImpl(Attributor &A) override; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3061 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3062 | /// See AbstractAttribute::trackStatistics() | 
|  | 3063 | void trackStatistics() const override {} | 
|  | 3064 |  | 
|  | 3065 | /// Returns true if the function is assumed dead. | 
|  | 3066 | bool isAssumedDead() const override { return false; } | 
|  | 3067 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 3068 | /// See AAIsDead::isKnownDead(). | 
|  | 3069 | bool isKnownDead() const override { return false; } | 
|  | 3070 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3071 | /// See AAIsDead::isAssumedDead(BasicBlock *). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3072 | bool isAssumedDead(const BasicBlock *BB) const override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 3073 | assert(BB->getParent() == getAssociatedFunction() && | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3074 | "BB must be in the same anchor scope function."); | 
|  | 3075 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3076 | if (!getAssumed()) | 
|  | 3077 | return false; | 
|  | 3078 | return !AssumedLiveBlocks.count(BB); | 
|  | 3079 | } | 
|  | 3080 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3081 | /// See AAIsDead::isKnownDead(BasicBlock *). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3082 | bool isKnownDead(const BasicBlock *BB) const override { | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3083 | return getKnown() && isAssumedDead(BB); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3084 | } | 
|  | 3085 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3086 | /// See AAIsDead::isAssumed(Instruction *I). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3087 | bool isAssumedDead(const Instruction *I) const override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 3088 | assert(I->getParent()->getParent() == getAssociatedFunction() && | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3089 | "Instruction must be in the same anchor scope function."); | 
|  | 3090 |  | 
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 3091 | if (!getAssumed()) | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3092 | return false; | 
|  | 3093 |  | 
|  | 3094 | // If it is not in AssumedLiveBlocks then it for sure dead. | 
|  | 3095 | // Otherwise, it can still be after noreturn call in a live block. | 
|  | 3096 | if (!AssumedLiveBlocks.count(I->getParent())) | 
|  | 3097 | return true; | 
|  | 3098 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3099 | // If it is not after a liveness barrier it is live. | 
|  | 3100 | const Instruction *PrevI = I->getPrevNode(); | 
|  | 3101 | while (PrevI) { | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3102 | if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3103 | return true; | 
|  | 3104 | PrevI = PrevI->getPrevNode(); | 
|  | 3105 | } | 
|  | 3106 | return false; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3107 | } | 
|  | 3108 |  | 
|  | 3109 | /// See AAIsDead::isKnownDead(Instruction *I). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3110 | bool isKnownDead(const Instruction *I) const override { | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3111 | return getKnown() && isAssumedDead(I); | 
|  | 3112 | } | 
|  | 3113 |  | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3114 | /// Determine if \p F might catch asynchronous exceptions. | 
|  | 3115 | static bool mayCatchAsynchronousExceptions(const Function &F) { | 
|  | 3116 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); | 
|  | 3117 | } | 
|  | 3118 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3119 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A | 
|  | 3120 | /// that internal function called from \p BB should now be looked at. | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3121 | bool assumeLive(Attributor &A, const BasicBlock &BB) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3122 | if (!AssumedLiveBlocks.insert(&BB).second) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3123 | return false; | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3124 |  | 
|  | 3125 | // We assume that all of BB is (probably) live now and if there are calls to | 
|  | 3126 | // internal functions we will assume that those are now live as well. This | 
|  | 3127 | // is a performance optimization for blocks with calls to a lot of internal | 
|  | 3128 | // functions. It can however cause dead functions to be treated as live. | 
|  | 3129 | for (const Instruction &I : BB) | 
|  | 3130 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) | 
|  | 3131 | if (const Function *F = ICS.getCalledFunction()) | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 3132 | if (F->hasLocalLinkage()) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3133 | A.markLiveInternalFunction(*F); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3134 | return true; | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3135 | } | 
|  | 3136 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3137 | /// Collection of instructions that need to be explored again, e.g., we | 
|  | 3138 | /// did assume they do not transfer control to (one of their) successors. | 
|  | 3139 | SmallSetVector<const Instruction *, 8> ToBeExploredFrom; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3140 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3141 | /// Collection of instructions that are known to not transfer control. | 
|  | 3142 | SmallSetVector<const Instruction *, 8> KnownDeadEnds; | 
|  | 3143 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3144 | /// Collection of all assumed live BasicBlocks. | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3145 | DenseSet<const BasicBlock *> AssumedLiveBlocks; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3146 | }; | 
|  | 3147 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3148 | static bool | 
|  | 3149 | identifyAliveSuccessors(Attributor &A, const CallBase &CB, | 
|  | 3150 | AbstractAttribute &AA, | 
|  | 3151 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 3152 | const IRPosition &IPos = IRPosition::callsite_function(CB); | 
|  | 3153 |  | 
|  | 3154 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); | 
|  | 3155 | if (NoReturnAA.isAssumedNoReturn()) | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3156 | return !NoReturnAA.isKnownNoReturn(); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3157 | if (CB.isTerminator()) | 
|  | 3158 | AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); | 
|  | 3159 | else | 
|  | 3160 | AliveSuccessors.push_back(CB.getNextNode()); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3161 | return false; | 
|  | 3162 | } | 
|  | 3163 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3164 | static bool | 
|  | 3165 | identifyAliveSuccessors(Attributor &A, const InvokeInst &II, | 
|  | 3166 | AbstractAttribute &AA, | 
|  | 3167 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 3168 | bool UsedAssumedInformation = | 
|  | 3169 | identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3170 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3171 | // First, determine if we can change an invoke to a call assuming the | 
|  | 3172 | // callee is nounwind. This is not possible if the personality of the | 
|  | 3173 | // function allows to catch asynchronous exceptions. | 
|  | 3174 | if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { | 
|  | 3175 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | 
|  | 3176 | } else { | 
|  | 3177 | const IRPosition &IPos = IRPosition::callsite_function(II); | 
|  | 3178 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3179 | if (AANoUnw.isAssumedNoUnwind()) { | 
|  | 3180 | UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); | 
|  | 3181 | } else { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3182 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3183 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3184 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3185 | return UsedAssumedInformation; | 
|  | 3186 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3187 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3188 | static bool | 
|  | 3189 | identifyAliveSuccessors(Attributor &A, const BranchInst &BI, | 
|  | 3190 | AbstractAttribute &AA, | 
|  | 3191 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 3192 | bool UsedAssumedInformation = false; | 
|  | 3193 | if (BI.getNumSuccessors() == 1) { | 
|  | 3194 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | 
|  | 3195 | } else { | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3196 | Optional<ConstantInt *> CI = | 
|  | 3197 | getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3198 | if (!CI.hasValue()) { | 
|  | 3199 | // No value yet, assume both edges are dead. | 
|  | 3200 | } else if (CI.getValue()) { | 
|  | 3201 | const BasicBlock *SuccBB = | 
|  | 3202 | BI.getSuccessor(1 - CI.getValue()->getZExtValue()); | 
|  | 3203 | AliveSuccessors.push_back(&SuccBB->front()); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3204 | } else { | 
|  | 3205 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | 
|  | 3206 | AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3207 | UsedAssumedInformation = false; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3208 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3209 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3210 | return UsedAssumedInformation; | 
|  | 3211 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3212 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3213 | static bool | 
|  | 3214 | identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, | 
|  | 3215 | AbstractAttribute &AA, | 
|  | 3216 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3217 | bool UsedAssumedInformation = false; | 
|  | 3218 | Optional<ConstantInt *> CI = | 
|  | 3219 | getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3220 | if (!CI.hasValue()) { | 
|  | 3221 | // No value yet, assume all edges are dead. | 
|  | 3222 | } else if (CI.getValue()) { | 
|  | 3223 | for (auto &CaseIt : SI.cases()) { | 
|  | 3224 | if (CaseIt.getCaseValue() == CI.getValue()) { | 
|  | 3225 | AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3226 | return UsedAssumedInformation; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3227 | } | 
|  | 3228 | } | 
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 3229 | AliveSuccessors.push_back(&SI.getDefaultDest()->front()); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3230 | return UsedAssumedInformation; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3231 | } else { | 
|  | 3232 | for (const BasicBlock *SuccBB : successors(SI.getParent())) | 
|  | 3233 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 3234 | } | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3235 | return UsedAssumedInformation; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3236 | } | 
|  | 3237 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3238 | ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3239 | ChangeStatus Change = ChangeStatus::UNCHANGED; | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3240 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3241 | LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" | 
|  | 3242 | << getAssociatedFunction()->size() << "] BBs and " | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3243 | << ToBeExploredFrom.size() << " exploration points and " | 
|  | 3244 | << KnownDeadEnds.size() << " known dead ends\n"); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3245 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3246 | // Copy and clear the list of instructions we need to explore from. It is | 
|  | 3247 | // refilled with instructions the next update has to look at. | 
|  | 3248 | SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), | 
|  | 3249 | ToBeExploredFrom.end()); | 
|  | 3250 | decltype(ToBeExploredFrom) NewToBeExploredFrom; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3251 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3252 | SmallVector<const Instruction *, 8> AliveSuccessors; | 
|  | 3253 | while (!Worklist.empty()) { | 
|  | 3254 | const Instruction *I = Worklist.pop_back_val(); | 
|  | 3255 | LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3256 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3257 | AliveSuccessors.clear(); | 
|  | 3258 |  | 
|  | 3259 | bool UsedAssumedInformation = false; | 
|  | 3260 | switch (I->getOpcode()) { | 
|  | 3261 | // TODO: look for (assumed) UB to backwards propagate "deadness". | 
|  | 3262 | default: | 
|  | 3263 | if (I->isTerminator()) { | 
|  | 3264 | for (const BasicBlock *SuccBB : successors(I->getParent())) | 
|  | 3265 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 3266 | } else { | 
|  | 3267 | AliveSuccessors.push_back(I->getNextNode()); | 
|  | 3268 | } | 
|  | 3269 | break; | 
|  | 3270 | case Instruction::Call: | 
|  | 3271 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), | 
|  | 3272 | *this, AliveSuccessors); | 
|  | 3273 | break; | 
|  | 3274 | case Instruction::Invoke: | 
|  | 3275 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), | 
|  | 3276 | *this, AliveSuccessors); | 
|  | 3277 | break; | 
|  | 3278 | case Instruction::Br: | 
|  | 3279 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), | 
|  | 3280 | *this, AliveSuccessors); | 
|  | 3281 | break; | 
|  | 3282 | case Instruction::Switch: | 
|  | 3283 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), | 
|  | 3284 | *this, AliveSuccessors); | 
|  | 3285 | break; | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3286 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3287 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3288 | if (UsedAssumedInformation) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3289 | NewToBeExploredFrom.insert(I); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3290 | } else { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3291 | Change = ChangeStatus::CHANGED; | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3292 | if (AliveSuccessors.empty() || | 
|  | 3293 | (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors())) | 
|  | 3294 | KnownDeadEnds.insert(I); | 
|  | 3295 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3296 |  | 
|  | 3297 | LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " | 
|  | 3298 | << AliveSuccessors.size() << " UsedAssumedInformation: " | 
|  | 3299 | << UsedAssumedInformation << "\n"); | 
|  | 3300 |  | 
|  | 3301 | for (const Instruction *AliveSuccessor : AliveSuccessors) { | 
|  | 3302 | if (!I->isTerminator()) { | 
|  | 3303 | assert(AliveSuccessors.size() == 1 && | 
|  | 3304 | "Non-terminator expected to have a single successor!"); | 
|  | 3305 | Worklist.push_back(AliveSuccessor); | 
|  | 3306 | } else { | 
|  | 3307 | if (assumeLive(A, *AliveSuccessor->getParent())) | 
|  | 3308 | Worklist.push_back(AliveSuccessor); | 
|  | 3309 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3310 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3311 | } | 
|  | 3312 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3313 | ToBeExploredFrom = std::move(NewToBeExploredFrom); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3314 |  | 
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 3315 | // 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] | 3316 | // Instead, indicating a pessimistic fixpoint will cause the state to be | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3317 | // "invalid" and all queries to be answered conservatively without lookups. | 
|  | 3318 | // To be in this state we have to (1) finished the exploration and (3) not | 
|  | 3319 | // discovered any non-trivial dead end and (2) not ruled unreachable code | 
|  | 3320 | // dead. | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3321 | if (ToBeExploredFrom.empty() && | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3322 | getAssociatedFunction()->size() == AssumedLiveBlocks.size() && | 
|  | 3323 | llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { | 
|  | 3324 | return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; | 
|  | 3325 | })) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3326 | return indicatePessimisticFixpoint(); | 
|  | 3327 | return Change; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3328 | } | 
|  | 3329 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3330 | /// Liveness information for a call sites. | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3331 | struct AAIsDeadCallSite final : AAIsDeadFunction { | 
|  | 3332 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3333 |  | 
|  | 3334 | /// See AbstractAttribute::initialize(...). | 
|  | 3335 | void initialize(Attributor &A) override { | 
|  | 3336 | // TODO: Once we have call site specific value information we can provide | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3337 | //       call site specific liveness information and then it makes | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3338 | //       sense to specialize attributes for call sites instead of | 
|  | 3339 | //       redirecting requests to the callee. | 
|  | 3340 | llvm_unreachable("Abstract attributes for liveness are not " | 
|  | 3341 | "supported for call sites yet!"); | 
|  | 3342 | } | 
|  | 3343 |  | 
|  | 3344 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3345 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3346 | return indicatePessimisticFixpoint(); | 
|  | 3347 | } | 
|  | 3348 |  | 
|  | 3349 | /// See AbstractAttribute::trackStatistics() | 
|  | 3350 | void trackStatistics() const override {} | 
|  | 3351 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3352 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3353 | /// -------------------- Dereferenceable Argument Attribute -------------------- | 
|  | 3354 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3355 | template <> | 
|  | 3356 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, | 
|  | 3357 | const DerefState &R) { | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3358 | ChangeStatus CS0 = | 
|  | 3359 | clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); | 
|  | 3360 | ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3361 | return CS0 | CS1; | 
|  | 3362 | } | 
|  | 3363 |  | 
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 3364 | struct AADereferenceableImpl : AADereferenceable { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3365 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3366 | using StateType = DerefState; | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3367 |  | 
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3368 | void initialize(Attributor &A) override { | 
|  | 3369 | SmallVector<Attribute, 4> Attrs; | 
|  | 3370 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, | 
|  | 3371 | Attrs); | 
|  | 3372 | for (const Attribute &Attr : Attrs) | 
|  | 3373 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); | 
|  | 3374 |  | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 3375 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(), | 
|  | 3376 | /* TrackDependence */ false); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3377 |  | 
|  | 3378 | const IRPosition &IRP = this->getIRPosition(); | 
|  | 3379 | bool IsFnInterface = IRP.isFnInterfaceKind(); | 
|  | 3380 | const Function *FnScope = IRP.getAnchorScope(); | 
|  | 3381 | if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) | 
|  | 3382 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3383 | } | 
|  | 3384 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3385 | /// See AbstractAttribute::getState() | 
|  | 3386 | /// { | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3387 | StateType &getState() override { return *this; } | 
|  | 3388 | const StateType &getState() const override { return *this; } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3389 | /// } | 
|  | 3390 |  | 
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3391 | /// Helper function for collecting accessed bytes in must-be-executed-context | 
|  | 3392 | void addAccessedBytesForUse(Attributor &A, const Use *U, | 
|  | 3393 | const Instruction *I) { | 
|  | 3394 | const Value *UseV = U->get(); | 
|  | 3395 | if (!UseV->getType()->isPointerTy()) | 
|  | 3396 | return; | 
|  | 3397 |  | 
|  | 3398 | Type *PtrTy = UseV->getType(); | 
|  | 3399 | const DataLayout &DL = A.getDataLayout(); | 
|  | 3400 | int64_t Offset; | 
|  | 3401 | if (const Value *Base = getBasePointerOfAccessPointerOperand( | 
|  | 3402 | I, Offset, DL, /*AllowNonInbounds*/ true)) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 3403 | if (Base == &getAssociatedValue() && | 
| Johannes Doerfert | b6dbd0f | 2020-01-26 02:49:58 -0600 | [diff] [blame] | 3404 | getPointerOperand(I, /* AllowVolatile */ false) == UseV) { | 
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3405 | uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType()); | 
|  | 3406 | addAccessedBytes(Offset, Size); | 
|  | 3407 | } | 
|  | 3408 | } | 
|  | 3409 | return; | 
|  | 3410 | } | 
|  | 3411 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3412 | /// See AAFromMustBeExecutedContext | 
|  | 3413 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 3414 | bool IsNonNull = false; | 
|  | 3415 | bool TrackUse = false; | 
|  | 3416 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( | 
|  | 3417 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); | 
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3418 |  | 
|  | 3419 | addAccessedBytesForUse(A, U, I); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3420 | takeKnownDerefBytesMaximum(DerefBytes); | 
|  | 3421 | return TrackUse; | 
|  | 3422 | } | 
|  | 3423 |  | 
| Hideto Ueno | dfedae5 | 2019-11-29 06:45:07 +0000 | [diff] [blame] | 3424 | /// See AbstractAttribute::manifest(...). | 
|  | 3425 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3426 | ChangeStatus Change = AADereferenceable::manifest(A); | 
|  | 3427 | if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { | 
|  | 3428 | removeAttrs({Attribute::DereferenceableOrNull}); | 
|  | 3429 | return ChangeStatus::CHANGED; | 
|  | 3430 | } | 
|  | 3431 | return Change; | 
|  | 3432 | } | 
|  | 3433 |  | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 3434 | void getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3435 | SmallVectorImpl<Attribute> &Attrs) const override { | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3436 | // TODO: Add *_globally support | 
|  | 3437 | if (isAssumedNonNull()) | 
|  | 3438 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( | 
|  | 3439 | Ctx, getAssumedDereferenceableBytes())); | 
|  | 3440 | else | 
|  | 3441 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( | 
|  | 3442 | Ctx, getAssumedDereferenceableBytes())); | 
|  | 3443 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3444 |  | 
|  | 3445 | /// See AbstractAttribute::getAsStr(). | 
|  | 3446 | const std::string getAsStr() const override { | 
|  | 3447 | if (!getAssumedDereferenceableBytes()) | 
|  | 3448 | return "unknown-dereferenceable"; | 
|  | 3449 | return std::string("dereferenceable") + | 
|  | 3450 | (isAssumedNonNull() ? "" : "_or_null") + | 
|  | 3451 | (isAssumedGlobal() ? "_globally" : "") + "<" + | 
|  | 3452 | std::to_string(getKnownDereferenceableBytes()) + "-" + | 
|  | 3453 | std::to_string(getAssumedDereferenceableBytes()) + ">"; | 
|  | 3454 | } | 
|  | 3455 | }; | 
|  | 3456 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3457 | /// Dereferenceable attribute for a floating value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3458 | struct AADereferenceableFloating | 
|  | 3459 | : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { | 
|  | 3460 | using Base = | 
|  | 3461 | AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; | 
|  | 3462 | AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3463 |  | 
|  | 3464 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3465 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3466 | ChangeStatus Change = Base::updateImpl(A); | 
|  | 3467 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3468 | const DataLayout &DL = A.getDataLayout(); | 
|  | 3469 |  | 
|  | 3470 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { | 
|  | 3471 | unsigned IdxWidth = | 
|  | 3472 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); | 
|  | 3473 | APInt Offset(IdxWidth, 0); | 
|  | 3474 | const Value *Base = | 
|  | 3475 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); | 
|  | 3476 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3477 | const auto &AA = | 
|  | 3478 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3479 | int64_t DerefBytes = 0; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3480 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3481 | // Use IR information if we did not strip anything. | 
|  | 3482 | // TODO: track globally. | 
|  | 3483 | bool CanBeNull; | 
|  | 3484 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); | 
|  | 3485 | T.GlobalState.indicatePessimisticFixpoint(); | 
|  | 3486 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3487 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3488 | DerefBytes = DS.DerefBytesState.getAssumed(); | 
|  | 3489 | T.GlobalState &= DS.GlobalState; | 
|  | 3490 | } | 
|  | 3491 |  | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 3492 | // TODO: Use `AAConstantRange` to infer dereferenceable bytes. | 
|  | 3493 |  | 
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3494 | // For now we do not try to "increase" dereferenceability due to negative | 
|  | 3495 | // indices as we first have to come up with code to deal with loops and | 
|  | 3496 | // for overflows of the dereferenceable bytes. | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3497 | int64_t OffsetSExt = Offset.getSExtValue(); | 
|  | 3498 | if (OffsetSExt < 0) | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 3499 | OffsetSExt = 0; | 
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3500 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3501 | T.takeAssumedDerefBytesMinimum( | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3502 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3503 |  | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3504 | if (this == &AA) { | 
|  | 3505 | if (!Stripped) { | 
|  | 3506 | // If nothing was stripped IR information is all we got. | 
|  | 3507 | T.takeKnownDerefBytesMaximum( | 
|  | 3508 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | 
|  | 3509 | T.indicatePessimisticFixpoint(); | 
|  | 3510 | } else if (OffsetSExt > 0) { | 
|  | 3511 | // If something was stripped but there is circular reasoning we look | 
|  | 3512 | // for the offset. If it is positive we basically decrease the | 
|  | 3513 | // dereferenceable bytes in a circluar loop now, which will simply | 
|  | 3514 | // drive them down to the known value in a very slow way which we | 
|  | 3515 | // can accelerate. | 
|  | 3516 | T.indicatePessimisticFixpoint(); | 
|  | 3517 | } | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3518 | } | 
|  | 3519 |  | 
|  | 3520 | return T.isValidState(); | 
|  | 3521 | }; | 
|  | 3522 |  | 
|  | 3523 | DerefState T; | 
|  | 3524 | if (!genericValueTraversal<AADereferenceable, DerefState>( | 
|  | 3525 | A, getIRPosition(), *this, T, VisitValueCB)) | 
|  | 3526 | return indicatePessimisticFixpoint(); | 
|  | 3527 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3528 | return Change | clampStateAndIndicateChange(getState(), T); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3529 | } | 
|  | 3530 |  | 
|  | 3531 | /// See AbstractAttribute::trackStatistics() | 
|  | 3532 | void trackStatistics() const override { | 
|  | 3533 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) | 
|  | 3534 | } | 
|  | 3535 | }; | 
|  | 3536 |  | 
|  | 3537 | /// Dereferenceable attribute for a return value. | 
|  | 3538 | struct AADereferenceableReturned final | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3539 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> { | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3540 | AADereferenceableReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3541 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>( | 
|  | 3542 | IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3543 |  | 
|  | 3544 | /// See AbstractAttribute::trackStatistics() | 
|  | 3545 | void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3546 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3547 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3548 | }; | 
|  | 3549 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3550 | /// Dereferenceable attribute for an argument | 
|  | 3551 | struct AADereferenceableArgument final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3552 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3553 | AADereferenceable, AADereferenceableImpl> { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3554 | using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 3555 | AADereferenceable, AADereferenceableImpl>; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3556 | AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3557 |  | 
|  | 3558 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3559 | void trackStatistics() const override { | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3560 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) | 
|  | 3561 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3562 | }; | 
|  | 3563 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3564 | /// Dereferenceable attribute for a call site argument. | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3565 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3566 | AADereferenceableCallSiteArgument(const IRPosition &IRP) | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3567 | : AADereferenceableFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3568 |  | 
|  | 3569 | /// See AbstractAttribute::trackStatistics() | 
|  | 3570 | void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3571 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3572 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3573 | }; | 
|  | 3574 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3575 | /// Dereferenceable attribute deduction for a call site return value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3576 | struct AADereferenceableCallSiteReturned final | 
|  | 3577 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< | 
|  | 3578 | AADereferenceable, AADereferenceableImpl> { | 
|  | 3579 | using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< | 
|  | 3580 | AADereferenceable, AADereferenceableImpl>; | 
|  | 3581 | AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3582 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3583 | /// See AbstractAttribute::trackStatistics() | 
|  | 3584 | void trackStatistics() const override { | 
|  | 3585 | STATS_DECLTRACK_CS_ATTR(dereferenceable); | 
|  | 3586 | } | 
|  | 3587 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3588 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3589 | // ------------------------ Align Argument Attribute ------------------------ | 
|  | 3590 |  | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3591 | static unsigned int getKnownAlignForUse(Attributor &A, | 
|  | 3592 | AbstractAttribute &QueryingAA, | 
|  | 3593 | Value &AssociatedValue, const Use *U, | 
|  | 3594 | const Instruction *I, bool &TrackUse) { | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3595 | // We need to follow common pointer manipulation uses to the accesses they | 
|  | 3596 | // feed into. | 
|  | 3597 | if (isa<CastInst>(I)) { | 
| Johannes Doerfert | c90681b | 2020-01-02 16:41:17 -0600 | [diff] [blame] | 3598 | // Follow all but ptr2int casts. | 
|  | 3599 | TrackUse = !isa<PtrToIntInst>(I); | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3600 | return 0; | 
|  | 3601 | } | 
|  | 3602 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { | 
|  | 3603 | if (GEP->hasAllConstantIndices()) { | 
|  | 3604 | TrackUse = true; | 
|  | 3605 | return 0; | 
|  | 3606 | } | 
|  | 3607 | } | 
|  | 3608 |  | 
|  | 3609 | unsigned Alignment = 0; | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3610 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { | 
|  | 3611 | if (ICS.isBundleOperand(U) || ICS.isCallee(U)) | 
|  | 3612 | return 0; | 
|  | 3613 |  | 
|  | 3614 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 3615 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); | 
|  | 3616 | // As long as we only use known information there is no need to track | 
|  | 3617 | // dependences here. | 
|  | 3618 | auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, | 
|  | 3619 | /* TrackDependence */ false); | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3620 | Alignment = AlignAA.getKnownAlign(); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3621 | } | 
|  | 3622 |  | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3623 | const Value *UseV = U->get(); | 
| Johannes Doerfert | 30ae859 | 2020-01-10 12:13:10 -0600 | [diff] [blame] | 3624 | if (auto *SI = dyn_cast<StoreInst>(I)) { | 
|  | 3625 | if (SI->getPointerOperand() == UseV) | 
|  | 3626 | Alignment = SI->getAlignment(); | 
|  | 3627 | } else if (auto *LI = dyn_cast<LoadInst>(I)) | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3628 | Alignment = LI->getAlignment(); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3629 |  | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3630 | if (Alignment <= 1) | 
|  | 3631 | return 0; | 
|  | 3632 |  | 
|  | 3633 | auto &DL = A.getDataLayout(); | 
|  | 3634 | int64_t Offset; | 
|  | 3635 |  | 
|  | 3636 | if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { | 
|  | 3637 | if (Base == &AssociatedValue) { | 
|  | 3638 | // BasePointerAddr + Offset = Alignment * Q for some integer Q. | 
|  | 3639 | // So we can say that the maximum power of two which is a divisor of | 
|  | 3640 | // gcd(Offset, Alignment) is an alignment. | 
|  | 3641 |  | 
|  | 3642 | uint32_t gcd = | 
|  | 3643 | greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); | 
|  | 3644 | Alignment = llvm::PowerOf2Floor(gcd); | 
|  | 3645 | } | 
|  | 3646 | } | 
|  | 3647 |  | 
|  | 3648 | return Alignment; | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3649 | } | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3650 | struct AAAlignImpl : AAAlign { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3651 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3652 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3653 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3654 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3655 | SmallVector<Attribute, 4> Attrs; | 
|  | 3656 | getAttrs({Attribute::Alignment}, Attrs); | 
|  | 3657 | for (const Attribute &Attr : Attrs) | 
|  | 3658 | takeKnownMaximum(Attr.getValueAsInt()); | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 3659 |  | 
|  | 3660 | if (getIRPosition().isFnInterfaceKind() && | 
|  | 3661 | (!getAssociatedFunction() || | 
|  | 3662 | !getAssociatedFunction()->hasExactDefinition())) | 
|  | 3663 | indicatePessimisticFixpoint(); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3664 | } | 
|  | 3665 |  | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3666 | /// See AbstractAttribute::manifest(...). | 
|  | 3667 | ChangeStatus manifest(Attributor &A) override { | 
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3668 | ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3669 |  | 
|  | 3670 | // Check for users that allow alignment annotations. | 
|  | 3671 | Value &AnchorVal = getIRPosition().getAnchorValue(); | 
|  | 3672 | for (const Use &U : AnchorVal.uses()) { | 
|  | 3673 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { | 
|  | 3674 | if (SI->getPointerOperand() == &AnchorVal) | 
|  | 3675 | if (SI->getAlignment() < getAssumedAlign()) { | 
|  | 3676 | STATS_DECLTRACK(AAAlign, Store, | 
| James Henderson | d68904f | 2020-01-06 10:15:44 +0000 | [diff] [blame] | 3677 | "Number of times alignment added to a store"); | 
| Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 3678 | SI->setAlignment(Align(getAssumedAlign())); | 
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3679 | LoadStoreChanged = ChangeStatus::CHANGED; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3680 | } | 
|  | 3681 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { | 
|  | 3682 | if (LI->getPointerOperand() == &AnchorVal) | 
|  | 3683 | if (LI->getAlignment() < getAssumedAlign()) { | 
| Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 3684 | LI->setAlignment(Align(getAssumedAlign())); | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3685 | STATS_DECLTRACK(AAAlign, Load, | 
| James Henderson | d68904f | 2020-01-06 10:15:44 +0000 | [diff] [blame] | 3686 | "Number of times alignment added to a load"); | 
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3687 | LoadStoreChanged = ChangeStatus::CHANGED; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3688 | } | 
|  | 3689 | } | 
|  | 3690 | } | 
|  | 3691 |  | 
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3692 | ChangeStatus Changed = AAAlign::manifest(A); | 
|  | 3693 |  | 
|  | 3694 | MaybeAlign InheritAlign = | 
|  | 3695 | getAssociatedValue().getPointerAlignment(A.getDataLayout()); | 
|  | 3696 | if (InheritAlign.valueOrOne() >= getAssumedAlign()) | 
|  | 3697 | return LoadStoreChanged; | 
|  | 3698 | return Changed | LoadStoreChanged; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3699 | } | 
|  | 3700 |  | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3701 | // TODO: Provide a helper to determine the implied ABI alignment and check in | 
|  | 3702 | //       the existing manifest method and a new one for AAAlignImpl that value | 
|  | 3703 | //       to avoid making the alignment explicit if it did not improve. | 
|  | 3704 |  | 
|  | 3705 | /// See AbstractAttribute::getDeducedAttributes | 
|  | 3706 | virtual void | 
|  | 3707 | getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3708 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 3709 | if (getAssumedAlign() > 1) | 
| Guillaume Chatelet | b65fa48 | 2019-10-15 12:56:24 +0000 | [diff] [blame] | 3710 | Attrs.emplace_back( | 
|  | 3711 | Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3712 | } | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3713 | /// See AAFromMustBeExecutedContext | 
|  | 3714 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 3715 | bool TrackUse = false; | 
|  | 3716 |  | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 3717 | unsigned int KnownAlign = | 
|  | 3718 | getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3719 | takeKnownMaximum(KnownAlign); | 
|  | 3720 |  | 
|  | 3721 | return TrackUse; | 
|  | 3722 | } | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3723 |  | 
|  | 3724 | /// See AbstractAttribute::getAsStr(). | 
|  | 3725 | const std::string getAsStr() const override { | 
|  | 3726 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + | 
|  | 3727 | "-" + std::to_string(getAssumedAlign()) + ">") | 
|  | 3728 | : "unknown-align"; | 
|  | 3729 | } | 
|  | 3730 | }; | 
|  | 3731 |  | 
|  | 3732 | /// Align attribute for a floating value. | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3733 | struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> { | 
|  | 3734 | using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>; | 
|  | 3735 | AAAlignFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3736 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3737 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3738 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3739 | Base::updateImpl(A); | 
|  | 3740 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3741 | const DataLayout &DL = A.getDataLayout(); | 
|  | 3742 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 3743 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, | 
|  | 3744 | bool Stripped) -> bool { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3745 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); | 
|  | 3746 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3747 | // Use only IR information if we did not strip anything. | 
| Guillaume Chatelet | bae629b | 2019-10-15 13:58:22 +0000 | [diff] [blame] | 3748 | const MaybeAlign PA = V.getPointerAlignment(DL); | 
|  | 3749 | T.takeKnownMaximum(PA ? PA->value() : 0); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3750 | T.indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3751 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3752 | // Use abstract attribute information. | 
|  | 3753 | const AAAlign::StateType &DS = | 
|  | 3754 | static_cast<const AAAlign::StateType &>(AA.getState()); | 
|  | 3755 | T ^= DS; | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3756 | } | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 3757 | return T.isValidState(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3758 | }; | 
|  | 3759 |  | 
|  | 3760 | StateType T; | 
|  | 3761 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, | 
|  | 3762 | VisitValueCB)) | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3763 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3764 |  | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 3765 | // TODO: If we know we visited all incoming values, thus no are assumed | 
|  | 3766 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3767 | return clampStateAndIndicateChange(getState(), T); | 
|  | 3768 | } | 
|  | 3769 |  | 
|  | 3770 | /// See AbstractAttribute::trackStatistics() | 
|  | 3771 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } | 
|  | 3772 | }; | 
|  | 3773 |  | 
|  | 3774 | /// Align attribute for function return value. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3775 | struct AAAlignReturned final | 
|  | 3776 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3777 | AAAlignReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3778 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3779 |  | 
|  | 3780 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3781 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3782 | }; | 
|  | 3783 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3784 | /// Align attribute for function argument. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3785 | struct AAAlignArgument final | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3786 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, | 
|  | 3787 | AAAlignImpl> { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3788 | AAAlignArgument(const IRPosition &IRP) | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3789 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, | 
|  | 3790 | AAAlignImpl>( | 
|  | 3791 | IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3792 |  | 
|  | 3793 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3794 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3795 | }; | 
|  | 3796 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3797 | struct AAAlignCallSiteArgument final : AAAlignFloating { | 
|  | 3798 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3799 |  | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3800 | /// See AbstractAttribute::manifest(...). | 
|  | 3801 | ChangeStatus manifest(Attributor &A) override { | 
| Johannes Doerfert | 30179d7 | 2020-01-12 00:25:45 -0600 | [diff] [blame] | 3802 | ChangeStatus Changed = AAAlignImpl::manifest(A); | 
|  | 3803 | MaybeAlign InheritAlign = | 
|  | 3804 | getAssociatedValue().getPointerAlignment(A.getDataLayout()); | 
|  | 3805 | if (InheritAlign.valueOrOne() >= getAssumedAlign()) | 
|  | 3806 | Changed = ChangeStatus::UNCHANGED; | 
|  | 3807 | return Changed; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3808 | } | 
|  | 3809 |  | 
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 3810 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
|  | 3811 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3812 | ChangeStatus Changed = AAAlignFloating::updateImpl(A); | 
|  | 3813 | if (Argument *Arg = getAssociatedArgument()) { | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 3814 | // We only take known information from the argument | 
|  | 3815 | // so we do not need to track a dependence. | 
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 3816 | const auto &ArgAlignAA = A.getAAFor<AAAlign>( | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 3817 | *this, IRPosition::argument(*Arg), /* TrackDependence */ false); | 
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 3818 | takeKnownMaximum(ArgAlignAA.getKnownAlign()); | 
|  | 3819 | } | 
|  | 3820 | return Changed; | 
|  | 3821 | } | 
|  | 3822 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3823 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3824 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3825 | }; | 
|  | 3826 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3827 | /// Align attribute deduction for a call site return value. | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3828 | struct AAAlignCallSiteReturned final | 
|  | 3829 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, | 
|  | 3830 | AAAlignImpl> { | 
|  | 3831 | using Base = | 
|  | 3832 | AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, | 
|  | 3833 | AAAlignImpl>; | 
|  | 3834 | AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3835 |  | 
|  | 3836 | /// See AbstractAttribute::initialize(...). | 
|  | 3837 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3838 | Base::initialize(A); | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3839 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3840 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3841 | indicatePessimisticFixpoint(); | 
|  | 3842 | } | 
|  | 3843 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3844 | /// See AbstractAttribute::trackStatistics() | 
|  | 3845 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } | 
|  | 3846 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3847 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3848 | /// ------------------ Function No-Return Attribute ---------------------------- | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3849 | struct AANoReturnImpl : public AANoReturn { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3850 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3851 |  | 
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3852 | /// See AbstractAttribute::initialize(...). | 
|  | 3853 | void initialize(Attributor &A) override { | 
|  | 3854 | AANoReturn::initialize(A); | 
|  | 3855 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | 1b6041a | 2019-11-01 20:17:48 -0500 | [diff] [blame] | 3856 | if (!F) | 
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3857 | indicatePessimisticFixpoint(); | 
|  | 3858 | } | 
|  | 3859 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3860 | /// See AbstractAttribute::getAsStr(). | 
|  | 3861 | const std::string getAsStr() const override { | 
|  | 3862 | return getAssumed() ? "noreturn" : "may-return"; | 
|  | 3863 | } | 
|  | 3864 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3865 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3866 | virtual ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3867 | auto CheckForNoReturn = [](Instruction &) { return false; }; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3868 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3869 | {(unsigned)Instruction::Ret})) | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3870 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3871 | return ChangeStatus::UNCHANGED; | 
|  | 3872 | } | 
|  | 3873 | }; | 
|  | 3874 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3875 | struct AANoReturnFunction final : AANoReturnImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3876 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3877 |  | 
|  | 3878 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3879 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3880 | }; | 
|  | 3881 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3882 | /// NoReturn attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3883 | struct AANoReturnCallSite final : AANoReturnImpl { | 
|  | 3884 | AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} | 
|  | 3885 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3886 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3887 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3888 | // TODO: Once we have call site specific value information we can provide | 
|  | 3889 | //       call site specific liveness information and then it makes | 
|  | 3890 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 3891 | //       redirecting requests to the callee argument. | 
|  | 3892 | Function *F = getAssociatedFunction(); | 
|  | 3893 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 3894 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); | 
|  | 3895 | return clampStateAndIndicateChange( | 
|  | 3896 | getState(), | 
|  | 3897 | static_cast<const AANoReturn::StateType &>(FnAA.getState())); | 
|  | 3898 | } | 
|  | 3899 |  | 
|  | 3900 | /// See AbstractAttribute::trackStatistics() | 
|  | 3901 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } | 
|  | 3902 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3903 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3904 | /// ----------------------- Variable Capturing --------------------------------- | 
|  | 3905 |  | 
|  | 3906 | /// A class to hold the state of for no-capture attributes. | 
|  | 3907 | struct AANoCaptureImpl : public AANoCapture { | 
|  | 3908 | AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} | 
|  | 3909 |  | 
|  | 3910 | /// See AbstractAttribute::initialize(...). | 
|  | 3911 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3912 | if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { | 
|  | 3913 | indicateOptimisticFixpoint(); | 
|  | 3914 | return; | 
|  | 3915 | } | 
|  | 3916 | Function *AnchorScope = getAnchorScope(); | 
|  | 3917 | if (isFnInterfaceKind() && | 
|  | 3918 | (!AnchorScope || !AnchorScope->hasExactDefinition())) { | 
|  | 3919 | indicatePessimisticFixpoint(); | 
|  | 3920 | return; | 
|  | 3921 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3922 |  | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 3923 | // You cannot "capture" null in the default address space. | 
|  | 3924 | if (isa<ConstantPointerNull>(getAssociatedValue()) && | 
|  | 3925 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { | 
|  | 3926 | indicateOptimisticFixpoint(); | 
|  | 3927 | return; | 
|  | 3928 | } | 
|  | 3929 |  | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3930 | const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3931 |  | 
|  | 3932 | // Check what state the associated function can actually capture. | 
|  | 3933 | if (F) | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3934 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3935 | else | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3936 | indicatePessimisticFixpoint(); | 
|  | 3937 | } | 
|  | 3938 |  | 
|  | 3939 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3940 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 3941 |  | 
|  | 3942 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). | 
|  | 3943 | virtual void | 
|  | 3944 | getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3945 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 3946 | if (!isAssumedNoCaptureMaybeReturned()) | 
|  | 3947 | return; | 
|  | 3948 |  | 
| Hideto Ueno | 3736764 | 2019-09-11 06:52:11 +0000 | [diff] [blame] | 3949 | if (getArgNo() >= 0) { | 
|  | 3950 | if (isAssumedNoCapture()) | 
|  | 3951 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); | 
|  | 3952 | else if (ManifestInternal) | 
|  | 3953 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); | 
|  | 3954 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3955 | } | 
|  | 3956 |  | 
|  | 3957 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known | 
|  | 3958 | /// depending on the ability of the function associated with \p IRP to capture | 
|  | 3959 | /// state in memory and through "returning/throwing", respectively. | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3960 | static void determineFunctionCaptureCapabilities(const IRPosition &IRP, | 
|  | 3961 | const Function &F, | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 3962 | BitIntegerState &State) { | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3963 | // TODO: Once we have memory behavior attributes we should use them here. | 
|  | 3964 |  | 
|  | 3965 | // If we know we cannot communicate or write to memory, we do not care about | 
|  | 3966 | // ptr2int anymore. | 
|  | 3967 | if (F.onlyReadsMemory() && F.doesNotThrow() && | 
|  | 3968 | F.getReturnType()->isVoidTy()) { | 
|  | 3969 | State.addKnownBits(NO_CAPTURE); | 
|  | 3970 | return; | 
|  | 3971 | } | 
|  | 3972 |  | 
|  | 3973 | // A function cannot capture state in memory if it only reads memory, it can | 
|  | 3974 | // however return/throw state and the state might be influenced by the | 
|  | 3975 | // pointer value, e.g., loading from a returned pointer might reveal a bit. | 
|  | 3976 | if (F.onlyReadsMemory()) | 
|  | 3977 | State.addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 3978 |  | 
|  | 3979 | // A function cannot communicate state back if it does not through | 
|  | 3980 | // exceptions and doesn not return values. | 
|  | 3981 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) | 
|  | 3982 | State.addKnownBits(NOT_CAPTURED_IN_RET); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3983 |  | 
|  | 3984 | // Check existing "returned" attributes. | 
|  | 3985 | int ArgNo = IRP.getArgNo(); | 
|  | 3986 | if (F.doesNotThrow() && ArgNo >= 0) { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 3987 | for (unsigned u = 0, e = F.arg_size(); u < e; ++u) | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3988 | if (F.hasParamAttribute(u, Attribute::Returned)) { | 
| Johannes Doerfert | 9d5ad5e | 2019-10-21 01:29:10 +0000 | [diff] [blame] | 3989 | if (u == unsigned(ArgNo)) | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3990 | State.removeAssumedBits(NOT_CAPTURED_IN_RET); | 
|  | 3991 | else if (F.onlyReadsMemory()) | 
|  | 3992 | State.addKnownBits(NO_CAPTURE); | 
|  | 3993 | else | 
|  | 3994 | State.addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 3995 | break; | 
|  | 3996 | } | 
|  | 3997 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3998 | } | 
|  | 3999 |  | 
|  | 4000 | /// See AbstractState::getAsStr(). | 
|  | 4001 | const std::string getAsStr() const override { | 
|  | 4002 | if (isKnownNoCapture()) | 
|  | 4003 | return "known not-captured"; | 
|  | 4004 | if (isAssumedNoCapture()) | 
|  | 4005 | return "assumed not-captured"; | 
|  | 4006 | if (isKnownNoCaptureMaybeReturned()) | 
|  | 4007 | return "known not-captured-maybe-returned"; | 
|  | 4008 | if (isAssumedNoCaptureMaybeReturned()) | 
|  | 4009 | return "assumed not-captured-maybe-returned"; | 
|  | 4010 | return "assumed-captured"; | 
|  | 4011 | } | 
|  | 4012 | }; | 
|  | 4013 |  | 
|  | 4014 | /// Attributor-aware capture tracker. | 
|  | 4015 | struct AACaptureUseTracker final : public CaptureTracker { | 
|  | 4016 |  | 
|  | 4017 | /// Create a capture tracker that can lookup in-flight abstract attributes | 
|  | 4018 | /// through the Attributor \p A. | 
|  | 4019 | /// | 
|  | 4020 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the | 
|  | 4021 | /// search is stopped. If a use leads to a return instruction, | 
|  | 4022 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. | 
|  | 4023 | /// If a use leads to a ptr2int which may capture the value, | 
|  | 4024 | /// \p CapturedInInteger is set. If a use is found that is currently assumed | 
|  | 4025 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies | 
|  | 4026 | /// set. All values in \p PotentialCopies are later tracked as well. For every | 
|  | 4027 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, | 
|  | 4028 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger | 
|  | 4029 | /// conservatively set to true. | 
|  | 4030 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4031 | const AAIsDead &IsDeadAA, AANoCapture::StateType &State, | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4032 | SmallVectorImpl<const Value *> &PotentialCopies, | 
|  | 4033 | unsigned &RemainingUsesToExplore) | 
|  | 4034 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), | 
|  | 4035 | PotentialCopies(PotentialCopies), | 
|  | 4036 | RemainingUsesToExplore(RemainingUsesToExplore) {} | 
|  | 4037 |  | 
|  | 4038 | /// Determine if \p V maybe captured. *Also updates the state!* | 
|  | 4039 | bool valueMayBeCaptured(const Value *V) { | 
|  | 4040 | if (V->getType()->isPointerTy()) { | 
|  | 4041 | PointerMayBeCaptured(V, this); | 
|  | 4042 | } else { | 
|  | 4043 | State.indicatePessimisticFixpoint(); | 
|  | 4044 | } | 
|  | 4045 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | 
|  | 4046 | } | 
|  | 4047 |  | 
|  | 4048 | /// See CaptureTracker::tooManyUses(). | 
|  | 4049 | void tooManyUses() override { | 
|  | 4050 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); | 
|  | 4051 | } | 
|  | 4052 |  | 
|  | 4053 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { | 
|  | 4054 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) | 
|  | 4055 | return true; | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4056 | const auto &DerefAA = A.getAAFor<AADereferenceable>( | 
|  | 4057 | NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true, | 
|  | 4058 | DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4059 | return DerefAA.getAssumedDereferenceableBytes(); | 
|  | 4060 | } | 
|  | 4061 |  | 
|  | 4062 | /// See CaptureTracker::captured(...). | 
|  | 4063 | bool captured(const Use *U) override { | 
|  | 4064 | Instruction *UInst = cast<Instruction>(U->getUser()); | 
|  | 4065 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst | 
|  | 4066 | << "\n"); | 
|  | 4067 |  | 
|  | 4068 | // Because we may reuse the tracker multiple times we keep track of the | 
|  | 4069 | // number of explored uses ourselves as well. | 
|  | 4070 | if (RemainingUsesToExplore-- == 0) { | 
|  | 4071 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); | 
|  | 4072 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 4073 | /* Return */ true); | 
|  | 4074 | } | 
|  | 4075 |  | 
|  | 4076 | // Deal with ptr2int by following uses. | 
|  | 4077 | if (isa<PtrToIntInst>(UInst)) { | 
|  | 4078 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); | 
|  | 4079 | return valueMayBeCaptured(UInst); | 
|  | 4080 | } | 
|  | 4081 |  | 
|  | 4082 | // Explicitly catch return instructions. | 
|  | 4083 | if (isa<ReturnInst>(UInst)) | 
|  | 4084 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 4085 | /* Return */ true); | 
|  | 4086 |  | 
|  | 4087 | // For now we only use special logic for call sites. However, the tracker | 
|  | 4088 | // itself knows about a lot of other non-capturing cases already. | 
|  | 4089 | CallSite CS(UInst); | 
|  | 4090 | if (!CS || !CS.isArgOperand(U)) | 
|  | 4091 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 4092 | /* Return */ true); | 
|  | 4093 |  | 
|  | 4094 | unsigned ArgNo = CS.getArgumentNo(U); | 
|  | 4095 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); | 
|  | 4096 | // If we have a abstract no-capture attribute for the argument we can use | 
|  | 4097 | // it to justify a non-capture attribute here. This allows recursion! | 
|  | 4098 | auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); | 
|  | 4099 | if (ArgNoCaptureAA.isAssumedNoCapture()) | 
|  | 4100 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 4101 | /* Return */ false); | 
|  | 4102 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 4103 | addPotentialCopy(CS); | 
|  | 4104 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 4105 | /* Return */ false); | 
|  | 4106 | } | 
|  | 4107 |  | 
|  | 4108 | // Lastly, we could not find a reason no-capture can be assumed so we don't. | 
|  | 4109 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 4110 | /* Return */ true); | 
|  | 4111 | } | 
|  | 4112 |  | 
|  | 4113 | /// Register \p CS as potential copy of the value we are checking. | 
|  | 4114 | void addPotentialCopy(CallSite CS) { | 
|  | 4115 | PotentialCopies.push_back(CS.getInstruction()); | 
|  | 4116 | } | 
|  | 4117 |  | 
|  | 4118 | /// See CaptureTracker::shouldExplore(...). | 
|  | 4119 | bool shouldExplore(const Use *U) override { | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4120 | // Check liveness, if it is used to stop exploring we need a dependence. | 
|  | 4121 | if (IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser()))) { | 
|  | 4122 | A.recordDependence(IsDeadAA, NoCaptureAA, DepClassTy::OPTIONAL); | 
|  | 4123 | return false; | 
|  | 4124 | } | 
|  | 4125 | return true; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4126 | } | 
|  | 4127 |  | 
|  | 4128 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and | 
|  | 4129 | /// \p CapturedInRet, then return the appropriate value for use in the | 
|  | 4130 | /// CaptureTracker::captured() interface. | 
|  | 4131 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, | 
|  | 4132 | bool CapturedInRet) { | 
|  | 4133 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " | 
|  | 4134 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); | 
|  | 4135 | if (CapturedInMem) | 
|  | 4136 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); | 
|  | 4137 | if (CapturedInInt) | 
|  | 4138 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); | 
|  | 4139 | if (CapturedInRet) | 
|  | 4140 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); | 
|  | 4141 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | 
|  | 4142 | } | 
|  | 4143 |  | 
|  | 4144 | private: | 
|  | 4145 | /// The attributor providing in-flight abstract attributes. | 
|  | 4146 | Attributor &A; | 
|  | 4147 |  | 
|  | 4148 | /// The abstract attribute currently updated. | 
|  | 4149 | AANoCapture &NoCaptureAA; | 
|  | 4150 |  | 
|  | 4151 | /// The abstract liveness state. | 
|  | 4152 | const AAIsDead &IsDeadAA; | 
|  | 4153 |  | 
|  | 4154 | /// The state currently updated. | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4155 | AANoCapture::StateType &State; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4156 |  | 
|  | 4157 | /// Set of potential copies of the tracked value. | 
|  | 4158 | SmallVectorImpl<const Value *> &PotentialCopies; | 
|  | 4159 |  | 
|  | 4160 | /// Global counter to limit the number of explored uses. | 
|  | 4161 | unsigned &RemainingUsesToExplore; | 
|  | 4162 | }; | 
|  | 4163 |  | 
|  | 4164 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { | 
|  | 4165 | const IRPosition &IRP = getIRPosition(); | 
|  | 4166 | const Value *V = | 
|  | 4167 | getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); | 
|  | 4168 | if (!V) | 
|  | 4169 | return indicatePessimisticFixpoint(); | 
|  | 4170 |  | 
|  | 4171 | const Function *F = | 
|  | 4172 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); | 
|  | 4173 | assert(F && "Expected a function!"); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4174 | const IRPosition &FnPos = IRPosition::function(*F); | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4175 | const auto &IsDeadAA = | 
|  | 4176 | A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4177 |  | 
|  | 4178 | AANoCapture::StateType T; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4179 |  | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4180 | // Readonly means we cannot capture through memory. | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4181 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 4182 | *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4183 | if (FnMemAA.isAssumedReadOnly()) { | 
|  | 4184 | T.addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 4185 | if (FnMemAA.isKnownReadOnly()) | 
|  | 4186 | addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 4187 | } | 
|  | 4188 |  | 
|  | 4189 | // Make sure all returned values are different than the underlying value. | 
|  | 4190 | // TODO: we could do this in a more sophisticated way inside | 
|  | 4191 | //       AAReturnedValues, e.g., track all values that escape through returns | 
|  | 4192 | //       directly somehow. | 
|  | 4193 | auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { | 
|  | 4194 | bool SeenConstant = false; | 
|  | 4195 | for (auto &It : RVAA.returned_values()) { | 
|  | 4196 | if (isa<Constant>(It.first)) { | 
|  | 4197 | if (SeenConstant) | 
|  | 4198 | return false; | 
|  | 4199 | SeenConstant = true; | 
|  | 4200 | } else if (!isa<Argument>(It.first) || | 
|  | 4201 | It.first == getAssociatedArgument()) | 
|  | 4202 | return false; | 
|  | 4203 | } | 
|  | 4204 | return true; | 
|  | 4205 | }; | 
|  | 4206 |  | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4207 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>( | 
|  | 4208 | *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4209 | if (NoUnwindAA.isAssumedNoUnwind()) { | 
|  | 4210 | bool IsVoidTy = F->getReturnType()->isVoidTy(); | 
|  | 4211 | const AAReturnedValues *RVAA = | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 4212 | IsVoidTy ? nullptr | 
|  | 4213 | : &A.getAAFor<AAReturnedValues>(*this, FnPos, | 
|  | 4214 | /* TrackDependence */ true, | 
|  | 4215 | DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4216 | if (IsVoidTy || CheckReturnedArgs(*RVAA)) { | 
|  | 4217 | T.addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 4218 | if (T.isKnown(NOT_CAPTURED_IN_MEM)) | 
|  | 4219 | return ChangeStatus::UNCHANGED; | 
|  | 4220 | if (NoUnwindAA.isKnownNoUnwind() && | 
|  | 4221 | (IsVoidTy || RVAA->getState().isAtFixpoint())) { | 
|  | 4222 | addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 4223 | if (isKnown(NOT_CAPTURED_IN_MEM)) | 
|  | 4224 | return indicateOptimisticFixpoint(); | 
|  | 4225 | } | 
|  | 4226 | } | 
|  | 4227 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4228 |  | 
|  | 4229 | // Use the CaptureTracker interface and logic with the specialized tracker, | 
|  | 4230 | // defined in AACaptureUseTracker, that can look at in-flight abstract | 
|  | 4231 | // attributes and directly updates the assumed state. | 
|  | 4232 | SmallVector<const Value *, 4> PotentialCopies; | 
|  | 4233 | unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; | 
|  | 4234 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, | 
|  | 4235 | RemainingUsesToExplore); | 
|  | 4236 |  | 
|  | 4237 | // Check all potential copies of the associated value until we can assume | 
|  | 4238 | // none will be captured or we have to assume at least one might be. | 
|  | 4239 | unsigned Idx = 0; | 
|  | 4240 | PotentialCopies.push_back(V); | 
|  | 4241 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) | 
|  | 4242 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); | 
|  | 4243 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4244 | AANoCapture::StateType &S = getState(); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4245 | auto Assumed = S.getAssumed(); | 
|  | 4246 | S.intersectAssumedBits(T.getAssumed()); | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4247 | if (!isAssumedNoCaptureMaybeReturned()) | 
|  | 4248 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4249 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | 
|  | 4250 | : ChangeStatus::CHANGED; | 
|  | 4251 | } | 
|  | 4252 |  | 
|  | 4253 | /// NoCapture attribute for function arguments. | 
|  | 4254 | struct AANoCaptureArgument final : AANoCaptureImpl { | 
|  | 4255 | AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4256 |  | 
|  | 4257 | /// See AbstractAttribute::trackStatistics() | 
|  | 4258 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } | 
|  | 4259 | }; | 
|  | 4260 |  | 
|  | 4261 | /// NoCapture attribute for call site arguments. | 
|  | 4262 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { | 
|  | 4263 | AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4264 |  | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4265 | /// See AbstractAttribute::initialize(...). | 
|  | 4266 | void initialize(Attributor &A) override { | 
|  | 4267 | if (Argument *Arg = getAssociatedArgument()) | 
|  | 4268 | if (Arg->hasByValAttr()) | 
|  | 4269 | indicateOptimisticFixpoint(); | 
|  | 4270 | AANoCaptureImpl::initialize(A); | 
|  | 4271 | } | 
|  | 4272 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4273 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4274 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4275 | // TODO: Once we have call site specific value information we can provide | 
|  | 4276 | //       call site specific liveness information and then it makes | 
|  | 4277 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 4278 | //       redirecting requests to the callee argument. | 
|  | 4279 | Argument *Arg = getAssociatedArgument(); | 
|  | 4280 | if (!Arg) | 
|  | 4281 | return indicatePessimisticFixpoint(); | 
|  | 4282 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 4283 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); | 
|  | 4284 | return clampStateAndIndicateChange( | 
|  | 4285 | getState(), | 
|  | 4286 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); | 
|  | 4287 | } | 
|  | 4288 |  | 
|  | 4289 | /// See AbstractAttribute::trackStatistics() | 
|  | 4290 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; | 
|  | 4291 | }; | 
|  | 4292 |  | 
|  | 4293 | /// NoCapture attribute for floating values. | 
|  | 4294 | struct AANoCaptureFloating final : AANoCaptureImpl { | 
|  | 4295 | AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4296 |  | 
|  | 4297 | /// See AbstractAttribute::trackStatistics() | 
|  | 4298 | void trackStatistics() const override { | 
|  | 4299 | STATS_DECLTRACK_FLOATING_ATTR(nocapture) | 
|  | 4300 | } | 
|  | 4301 | }; | 
|  | 4302 |  | 
|  | 4303 | /// NoCapture attribute for function return value. | 
|  | 4304 | struct AANoCaptureReturned final : AANoCaptureImpl { | 
|  | 4305 | AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { | 
|  | 4306 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 4307 | } | 
|  | 4308 |  | 
|  | 4309 | /// See AbstractAttribute::initialize(...). | 
|  | 4310 | void initialize(Attributor &A) override { | 
|  | 4311 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 4312 | } | 
|  | 4313 |  | 
|  | 4314 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4315 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4316 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 4317 | } | 
|  | 4318 |  | 
|  | 4319 | /// See AbstractAttribute::trackStatistics() | 
|  | 4320 | void trackStatistics() const override {} | 
|  | 4321 | }; | 
|  | 4322 |  | 
|  | 4323 | /// NoCapture attribute deduction for a call site return value. | 
|  | 4324 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { | 
|  | 4325 | AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4326 |  | 
|  | 4327 | /// See AbstractAttribute::trackStatistics() | 
|  | 4328 | void trackStatistics() const override { | 
|  | 4329 | STATS_DECLTRACK_CSRET_ATTR(nocapture) | 
|  | 4330 | } | 
|  | 4331 | }; | 
|  | 4332 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4333 | /// ------------------ Value Simplify Attribute ---------------------------- | 
|  | 4334 | struct AAValueSimplifyImpl : AAValueSimplify { | 
|  | 4335 | AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} | 
|  | 4336 |  | 
| Johannes Doerfert | 9dcf889 | 2020-01-11 23:59:36 -0600 | [diff] [blame] | 4337 | /// See AbstractAttribute::initialize(...). | 
|  | 4338 | void initialize(Attributor &A) override { | 
|  | 4339 | if (getAssociatedValue().getType()->isVoidTy()) | 
|  | 4340 | indicatePessimisticFixpoint(); | 
|  | 4341 | } | 
|  | 4342 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4343 | /// See AbstractAttribute::getAsStr(). | 
|  | 4344 | const std::string getAsStr() const override { | 
|  | 4345 | return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") | 
|  | 4346 | : "not-simple"; | 
|  | 4347 | } | 
|  | 4348 |  | 
|  | 4349 | /// See AbstractAttribute::trackStatistics() | 
|  | 4350 | void trackStatistics() const override {} | 
|  | 4351 |  | 
|  | 4352 | /// See AAValueSimplify::getAssumedSimplifiedValue() | 
|  | 4353 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { | 
|  | 4354 | if (!getAssumed()) | 
|  | 4355 | return const_cast<Value *>(&getAssociatedValue()); | 
|  | 4356 | return SimplifiedAssociatedValue; | 
|  | 4357 | } | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4358 |  | 
|  | 4359 | /// Helper function for querying AAValueSimplify and updating candicate. | 
|  | 4360 | /// \param QueryingValue Value trying to unify with SimplifiedValue | 
|  | 4361 | /// \param AccumulatedSimplifiedValue Current simplification result. | 
|  | 4362 | static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, | 
|  | 4363 | Value &QueryingValue, | 
|  | 4364 | Optional<Value *> &AccumulatedSimplifiedValue) { | 
|  | 4365 | // FIXME: Add a typecast support. | 
|  | 4366 |  | 
| Johannes Doerfert | d07b5a5 | 2020-01-12 00:00:33 -0600 | [diff] [blame] | 4367 | auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>( | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4368 | QueryingAA, IRPosition::value(QueryingValue)); | 
|  | 4369 |  | 
|  | 4370 | Optional<Value *> QueryingValueSimplified = | 
| Johannes Doerfert | d07b5a5 | 2020-01-12 00:00:33 -0600 | [diff] [blame] | 4371 | ValueSimplifyAA.getAssumedSimplifiedValue(A); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4372 |  | 
|  | 4373 | if (!QueryingValueSimplified.hasValue()) | 
|  | 4374 | return true; | 
|  | 4375 |  | 
|  | 4376 | if (!QueryingValueSimplified.getValue()) | 
|  | 4377 | return false; | 
|  | 4378 |  | 
|  | 4379 | Value &QueryingValueSimplifiedUnwrapped = | 
|  | 4380 | *QueryingValueSimplified.getValue(); | 
|  | 4381 |  | 
|  | 4382 | if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) | 
|  | 4383 | return true; | 
|  | 4384 |  | 
|  | 4385 | if (AccumulatedSimplifiedValue.hasValue()) | 
|  | 4386 | return AccumulatedSimplifiedValue == QueryingValueSimplified; | 
|  | 4387 |  | 
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 4388 | LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4389 | << " is assumed to be " | 
|  | 4390 | << QueryingValueSimplifiedUnwrapped << "\n"); | 
|  | 4391 |  | 
|  | 4392 | AccumulatedSimplifiedValue = QueryingValueSimplified; | 
|  | 4393 | return true; | 
|  | 4394 | } | 
|  | 4395 |  | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4396 | bool askSimplifiedValueForAAValueConstantRange(Attributor &A) { | 
|  | 4397 | if (!getAssociatedValue().getType()->isIntegerTy()) | 
|  | 4398 | return false; | 
|  | 4399 |  | 
|  | 4400 | const auto &ValueConstantRangeAA = | 
|  | 4401 | A.getAAFor<AAValueConstantRange>(*this, getIRPosition()); | 
|  | 4402 |  | 
|  | 4403 | Optional<ConstantInt *> COpt = | 
|  | 4404 | ValueConstantRangeAA.getAssumedConstantInt(A); | 
|  | 4405 | if (COpt.hasValue()) { | 
|  | 4406 | if (auto *C = COpt.getValue()) | 
|  | 4407 | SimplifiedAssociatedValue = C; | 
|  | 4408 | else | 
|  | 4409 | return false; | 
|  | 4410 | } else { | 
| Johannes Doerfert | 63adbb9 | 2020-02-09 20:21:56 -0600 | [diff] [blame^] | 4411 | SimplifiedAssociatedValue = llvm::None; | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4412 | } | 
|  | 4413 | return true; | 
|  | 4414 | } | 
|  | 4415 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4416 | /// See AbstractAttribute::manifest(...). | 
|  | 4417 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4418 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 4419 |  | 
|  | 4420 | if (!SimplifiedAssociatedValue.hasValue() || | 
|  | 4421 | !SimplifiedAssociatedValue.getValue()) | 
|  | 4422 | return Changed; | 
|  | 4423 |  | 
|  | 4424 | if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) { | 
|  | 4425 | // We can replace the AssociatedValue with the constant. | 
|  | 4426 | Value &V = getAssociatedValue(); | 
|  | 4427 | if (!V.user_empty() && &V != C && V.getType() == C->getType()) { | 
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 4428 | LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C | 
|  | 4429 | << " :: " << *this << "\n"); | 
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 4430 | A.changeValueAfterManifest(V, *C); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4431 | Changed = ChangeStatus::CHANGED; | 
|  | 4432 | } | 
|  | 4433 | } | 
|  | 4434 |  | 
|  | 4435 | return Changed | AAValueSimplify::manifest(A); | 
|  | 4436 | } | 
|  | 4437 |  | 
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4438 | /// See AbstractState::indicatePessimisticFixpoint(...). | 
|  | 4439 | ChangeStatus indicatePessimisticFixpoint() override { | 
|  | 4440 | // NOTE: Associated value will be returned in a pessimistic fixpoint and is | 
|  | 4441 | // regarded as known. That's why`indicateOptimisticFixpoint` is called. | 
|  | 4442 | SimplifiedAssociatedValue = &getAssociatedValue(); | 
| Johannes Doerfert | a4b3588 | 2019-12-31 13:25:47 -0600 | [diff] [blame] | 4443 | indicateOptimisticFixpoint(); | 
|  | 4444 | return ChangeStatus::CHANGED; | 
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4445 | } | 
|  | 4446 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4447 | protected: | 
|  | 4448 | // An assumed simplified value. Initially, it is set to Optional::None, which | 
|  | 4449 | // means that the value is not clear under current assumption. If in the | 
|  | 4450 | // pessimistic state, getAssumedSimplifiedValue doesn't return this value but | 
|  | 4451 | // returns orignal associated value. | 
|  | 4452 | Optional<Value *> SimplifiedAssociatedValue; | 
|  | 4453 | }; | 
|  | 4454 |  | 
|  | 4455 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { | 
|  | 4456 | AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4457 |  | 
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4458 | void initialize(Attributor &A) override { | 
|  | 4459 | AAValueSimplifyImpl::initialize(A); | 
|  | 4460 | if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) | 
|  | 4461 | indicatePessimisticFixpoint(); | 
|  | 4462 | if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, | 
|  | 4463 | /* IgnoreSubsumingPositions */ true)) | 
|  | 4464 | indicatePessimisticFixpoint(); | 
|  | 4465 | } | 
|  | 4466 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4467 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4468 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4469 | // Byval is only replacable if it is readonly otherwise we would write into | 
|  | 4470 | // the replaced value and not the copy that byval creates implicitly. | 
|  | 4471 | Argument *Arg = getAssociatedArgument(); | 
|  | 4472 | if (Arg->hasByValAttr()) { | 
|  | 4473 | const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); | 
|  | 4474 | if (!MemAA.isAssumedReadOnly()) | 
|  | 4475 | return indicatePessimisticFixpoint(); | 
|  | 4476 | } | 
|  | 4477 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4478 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 4479 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4480 | auto PredForCallSite = [&](AbstractCallSite ACS) { | 
|  | 4481 | // Check if we have an associated argument or not (which can happen for | 
|  | 4482 | // callback calls). | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4483 | Value *ArgOp = ACS.getCallArgOperand(getArgNo()); | 
|  | 4484 | if (!ArgOp) | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4485 | return false; | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4486 | // We can only propagate thread independent values through callbacks. | 
|  | 4487 | // This is different to direct/indirect call sites because for them we | 
|  | 4488 | // know the thread executing the caller and callee is the same. For | 
|  | 4489 | // callbacks this is not guaranteed, thus a thread dependent value could | 
|  | 4490 | // be different for the caller and callee, making it invalid to propagate. | 
|  | 4491 | if (ACS.isCallbackCall()) | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4492 | if (auto *C = dyn_cast<Constant>(ArgOp)) | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4493 | if (C->isThreadDependent()) | 
|  | 4494 | return false; | 
|  | 4495 | return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4496 | }; | 
|  | 4497 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 4498 | bool AllCallSitesKnown; | 
|  | 4499 | if (!A.checkForAllCallSites(PredForCallSite, *this, true, | 
|  | 4500 | AllCallSitesKnown)) | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4501 | if (!askSimplifiedValueForAAValueConstantRange(A)) | 
|  | 4502 | return indicatePessimisticFixpoint(); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4503 |  | 
|  | 4504 | // If a candicate was found in this update, return CHANGED. | 
|  | 4505 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 4506 | ? ChangeStatus::UNCHANGED | 
|  | 4507 | : ChangeStatus ::CHANGED; | 
|  | 4508 | } | 
|  | 4509 |  | 
|  | 4510 | /// See AbstractAttribute::trackStatistics() | 
|  | 4511 | void trackStatistics() const override { | 
|  | 4512 | STATS_DECLTRACK_ARG_ATTR(value_simplify) | 
|  | 4513 | } | 
|  | 4514 | }; | 
|  | 4515 |  | 
|  | 4516 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { | 
|  | 4517 | AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4518 |  | 
|  | 4519 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4520 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4521 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 4522 |  | 
|  | 4523 | auto PredForReturned = [&](Value &V) { | 
|  | 4524 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); | 
|  | 4525 | }; | 
|  | 4526 |  | 
|  | 4527 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4528 | if (!askSimplifiedValueForAAValueConstantRange(A)) | 
|  | 4529 | return indicatePessimisticFixpoint(); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4530 |  | 
|  | 4531 | // If a candicate was found in this update, return CHANGED. | 
|  | 4532 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 4533 | ? ChangeStatus::UNCHANGED | 
|  | 4534 | : ChangeStatus ::CHANGED; | 
|  | 4535 | } | 
|  | 4536 | /// See AbstractAttribute::trackStatistics() | 
|  | 4537 | void trackStatistics() const override { | 
|  | 4538 | STATS_DECLTRACK_FNRET_ATTR(value_simplify) | 
|  | 4539 | } | 
|  | 4540 | }; | 
|  | 4541 |  | 
|  | 4542 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { | 
|  | 4543 | AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4544 |  | 
|  | 4545 | /// See AbstractAttribute::initialize(...). | 
|  | 4546 | void initialize(Attributor &A) override { | 
|  | 4547 | Value &V = getAnchorValue(); | 
|  | 4548 |  | 
|  | 4549 | // TODO: add other stuffs | 
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4550 | if (isa<Constant>(V)) | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4551 | indicatePessimisticFixpoint(); | 
|  | 4552 | } | 
|  | 4553 |  | 
|  | 4554 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4555 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4556 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 4557 |  | 
| Johannes Doerfert | 76843ba | 2020-01-28 23:51:25 -0600 | [diff] [blame] | 4558 | auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4559 | auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); | 
|  | 4560 | if (!Stripped && this == &AA) { | 
|  | 4561 | // TODO: Look the instruction and check recursively. | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4562 |  | 
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 4563 | LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V | 
|  | 4564 | << "\n"); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4565 | return false; | 
|  | 4566 | } | 
|  | 4567 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); | 
|  | 4568 | }; | 
|  | 4569 |  | 
| Johannes Doerfert | 76843ba | 2020-01-28 23:51:25 -0600 | [diff] [blame] | 4570 | bool Dummy = false; | 
| Johannes Doerfert | 6626d1b | 2020-01-28 11:49:59 -0600 | [diff] [blame] | 4571 | if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this, | 
|  | 4572 | Dummy, VisitValueCB)) | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 4573 | if (!askSimplifiedValueForAAValueConstantRange(A)) | 
|  | 4574 | return indicatePessimisticFixpoint(); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4575 |  | 
|  | 4576 | // If a candicate was found in this update, return CHANGED. | 
|  | 4577 |  | 
|  | 4578 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 4579 | ? ChangeStatus::UNCHANGED | 
|  | 4580 | : ChangeStatus ::CHANGED; | 
|  | 4581 | } | 
|  | 4582 |  | 
|  | 4583 | /// See AbstractAttribute::trackStatistics() | 
|  | 4584 | void trackStatistics() const override { | 
|  | 4585 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify) | 
|  | 4586 | } | 
|  | 4587 | }; | 
|  | 4588 |  | 
|  | 4589 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { | 
|  | 4590 | AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4591 |  | 
|  | 4592 | /// See AbstractAttribute::initialize(...). | 
|  | 4593 | void initialize(Attributor &A) override { | 
|  | 4594 | SimplifiedAssociatedValue = &getAnchorValue(); | 
|  | 4595 | indicateOptimisticFixpoint(); | 
|  | 4596 | } | 
|  | 4597 | /// See AbstractAttribute::initialize(...). | 
|  | 4598 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4599 | llvm_unreachable( | 
|  | 4600 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); | 
|  | 4601 | } | 
|  | 4602 | /// See AbstractAttribute::trackStatistics() | 
|  | 4603 | void trackStatistics() const override { | 
|  | 4604 | STATS_DECLTRACK_FN_ATTR(value_simplify) | 
|  | 4605 | } | 
|  | 4606 | }; | 
|  | 4607 |  | 
|  | 4608 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { | 
|  | 4609 | AAValueSimplifyCallSite(const IRPosition &IRP) | 
|  | 4610 | : AAValueSimplifyFunction(IRP) {} | 
|  | 4611 | /// See AbstractAttribute::trackStatistics() | 
|  | 4612 | void trackStatistics() const override { | 
|  | 4613 | STATS_DECLTRACK_CS_ATTR(value_simplify) | 
|  | 4614 | } | 
|  | 4615 | }; | 
|  | 4616 |  | 
|  | 4617 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { | 
|  | 4618 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP) | 
|  | 4619 | : AAValueSimplifyReturned(IRP) {} | 
|  | 4620 |  | 
|  | 4621 | void trackStatistics() const override { | 
|  | 4622 | STATS_DECLTRACK_CSRET_ATTR(value_simplify) | 
|  | 4623 | } | 
|  | 4624 | }; | 
|  | 4625 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { | 
|  | 4626 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP) | 
|  | 4627 | : AAValueSimplifyFloating(IRP) {} | 
|  | 4628 |  | 
|  | 4629 | void trackStatistics() const override { | 
|  | 4630 | STATS_DECLTRACK_CSARG_ATTR(value_simplify) | 
|  | 4631 | } | 
|  | 4632 | }; | 
|  | 4633 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4634 | /// ----------------------- Heap-To-Stack Conversion --------------------------- | 
|  | 4635 | struct AAHeapToStackImpl : public AAHeapToStack { | 
|  | 4636 | AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} | 
|  | 4637 |  | 
|  | 4638 | const std::string getAsStr() const override { | 
|  | 4639 | return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); | 
|  | 4640 | } | 
|  | 4641 |  | 
|  | 4642 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4643 | assert(getState().isValidState() && | 
|  | 4644 | "Attempted to manifest an invalid state!"); | 
|  | 4645 |  | 
|  | 4646 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
|  | 4647 | Function *F = getAssociatedFunction(); | 
|  | 4648 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | 
|  | 4649 |  | 
|  | 4650 | for (Instruction *MallocCall : MallocCalls) { | 
|  | 4651 | // This malloc cannot be replaced. | 
|  | 4652 | if (BadMallocCalls.count(MallocCall)) | 
|  | 4653 | continue; | 
|  | 4654 |  | 
|  | 4655 | for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { | 
|  | 4656 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); | 
|  | 4657 | A.deleteAfterManifest(*FreeCall); | 
|  | 4658 | HasChanged = ChangeStatus::CHANGED; | 
|  | 4659 | } | 
|  | 4660 |  | 
|  | 4661 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall | 
|  | 4662 | << "\n"); | 
|  | 4663 |  | 
|  | 4664 | Constant *Size; | 
|  | 4665 | if (isCallocLikeFn(MallocCall, TLI)) { | 
|  | 4666 | auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); | 
|  | 4667 | auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); | 
|  | 4668 | APInt TotalSize = SizeT->getValue() * Num->getValue(); | 
|  | 4669 | Size = | 
|  | 4670 | ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); | 
|  | 4671 | } else { | 
|  | 4672 | Size = cast<ConstantInt>(MallocCall->getOperand(0)); | 
|  | 4673 | } | 
|  | 4674 |  | 
|  | 4675 | unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); | 
|  | 4676 | Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, | 
|  | 4677 | Size, "", MallocCall->getNextNode()); | 
|  | 4678 |  | 
|  | 4679 | if (AI->getType() != MallocCall->getType()) | 
|  | 4680 | AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", | 
|  | 4681 | AI->getNextNode()); | 
|  | 4682 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 4683 | A.replaceAllUsesWith(*MallocCall, *AI); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4684 |  | 
|  | 4685 | if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { | 
|  | 4686 | auto *NBB = II->getNormalDest(); | 
|  | 4687 | BranchInst::Create(NBB, MallocCall->getParent()); | 
|  | 4688 | A.deleteAfterManifest(*MallocCall); | 
|  | 4689 | } else { | 
|  | 4690 | A.deleteAfterManifest(*MallocCall); | 
|  | 4691 | } | 
|  | 4692 |  | 
|  | 4693 | if (isCallocLikeFn(MallocCall, TLI)) { | 
|  | 4694 | auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", | 
|  | 4695 | AI->getNextNode()); | 
|  | 4696 | Value *Ops[] = { | 
|  | 4697 | BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, | 
|  | 4698 | ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; | 
|  | 4699 |  | 
|  | 4700 | Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; | 
|  | 4701 | Module *M = F->getParent(); | 
|  | 4702 | Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); | 
|  | 4703 | CallInst::Create(Fn, Ops, "", BI->getNextNode()); | 
|  | 4704 | } | 
|  | 4705 | HasChanged = ChangeStatus::CHANGED; | 
|  | 4706 | } | 
|  | 4707 |  | 
|  | 4708 | return HasChanged; | 
|  | 4709 | } | 
|  | 4710 |  | 
|  | 4711 | /// Collection of all malloc calls in a function. | 
|  | 4712 | SmallSetVector<Instruction *, 4> MallocCalls; | 
|  | 4713 |  | 
|  | 4714 | /// Collection of malloc calls that cannot be converted. | 
|  | 4715 | DenseSet<const Instruction *> BadMallocCalls; | 
|  | 4716 |  | 
|  | 4717 | /// A map for each malloc call to the set of associated free calls. | 
|  | 4718 | DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; | 
|  | 4719 |  | 
|  | 4720 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 4721 | }; | 
|  | 4722 |  | 
|  | 4723 | ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { | 
|  | 4724 | const Function *F = getAssociatedFunction(); | 
|  | 4725 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | 
|  | 4726 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4727 | MustBeExecutedContextExplorer &Explorer = | 
|  | 4728 | A.getInfoCache().getMustBeExecutedContextExplorer(); | 
|  | 4729 |  | 
|  | 4730 | auto FreeCheck = [&](Instruction &I) { | 
|  | 4731 | const auto &Frees = FreesForMalloc.lookup(&I); | 
|  | 4732 | if (Frees.size() != 1) | 
|  | 4733 | return false; | 
|  | 4734 | Instruction *UniqueFree = *Frees.begin(); | 
|  | 4735 | return Explorer.findInContextOf(UniqueFree, I.getNextNode()); | 
|  | 4736 | }; | 
|  | 4737 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4738 | auto UsesCheck = [&](Instruction &I) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4739 | bool ValidUsesOnly = true; | 
|  | 4740 | bool MustUse = true; | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4741 | auto Pred = [&](const Use &U, bool &Follow) -> bool { | 
|  | 4742 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4743 | if (isa<LoadInst>(UserI)) | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4744 | return true; | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4745 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4746 | if (SI->getValueOperand() == U.get()) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4747 | LLVM_DEBUG(dbgs() | 
|  | 4748 | << "[H2S] escaping store to memory: " << *UserI << "\n"); | 
|  | 4749 | ValidUsesOnly = false; | 
|  | 4750 | } else { | 
|  | 4751 | // A store into the malloc'ed memory is fine. | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4752 | } | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4753 | return true; | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4754 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4755 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4756 | if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) | 
|  | 4757 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4758 | // Record malloc. | 
|  | 4759 | if (isFreeCall(UserI, TLI)) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4760 | if (MustUse) { | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4761 | FreesForMalloc[&I].insert(UserI); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4762 | } else { | 
|  | 4763 | LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " | 
|  | 4764 | << *UserI << "\n"); | 
|  | 4765 | ValidUsesOnly = false; | 
|  | 4766 | } | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4767 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4768 | } | 
|  | 4769 |  | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4770 | unsigned ArgNo = CB->getArgOperandNo(&U); | 
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 4771 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4772 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( | 
|  | 4773 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
|  | 4774 |  | 
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 4775 | // If a callsite argument use is nofree, we are fine. | 
|  | 4776 | const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( | 
|  | 4777 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
|  | 4778 |  | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4779 | if (!NoCaptureAA.isAssumedNoCapture() || | 
|  | 4780 | !ArgNoFreeAA.isAssumedNoFree()) { | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4781 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4782 | ValidUsesOnly = false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4783 | } | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4784 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4785 | } | 
|  | 4786 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4787 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | 
|  | 4788 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | 
|  | 4789 | MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4790 | Follow = true; | 
|  | 4791 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4792 | } | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4793 | // Unknown user for which we can not track uses further (in a way that | 
|  | 4794 | // makes sense). | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4795 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4796 | ValidUsesOnly = false; | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4797 | return true; | 
|  | 4798 | }; | 
|  | 4799 | A.checkForAllUses(Pred, *this, I); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4800 | return ValidUsesOnly; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4801 | }; | 
|  | 4802 |  | 
|  | 4803 | auto MallocCallocCheck = [&](Instruction &I) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4804 | if (BadMallocCalls.count(&I)) | 
|  | 4805 | return true; | 
|  | 4806 |  | 
|  | 4807 | bool IsMalloc = isMallocLikeFn(&I, TLI); | 
|  | 4808 | bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); | 
|  | 4809 | if (!IsMalloc && !IsCalloc) { | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4810 | BadMallocCalls.insert(&I); | 
|  | 4811 | return true; | 
|  | 4812 | } | 
|  | 4813 |  | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4814 | if (IsMalloc) { | 
|  | 4815 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) | 
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 4816 | if (Size->getValue().ule(MaxHeapToStackSize)) | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4817 | if (UsesCheck(I) || FreeCheck(I)) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4818 | MallocCalls.insert(&I); | 
|  | 4819 | return true; | 
|  | 4820 | } | 
|  | 4821 | } else if (IsCalloc) { | 
|  | 4822 | bool Overflow = false; | 
|  | 4823 | if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) | 
|  | 4824 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) | 
|  | 4825 | if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) | 
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 4826 | .ule(MaxHeapToStackSize)) | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4827 | if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4828 | MallocCalls.insert(&I); | 
|  | 4829 | return true; | 
|  | 4830 | } | 
|  | 4831 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4832 |  | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4833 | BadMallocCalls.insert(&I); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4834 | return true; | 
|  | 4835 | }; | 
|  | 4836 |  | 
|  | 4837 | size_t NumBadMallocs = BadMallocCalls.size(); | 
|  | 4838 |  | 
|  | 4839 | A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); | 
|  | 4840 |  | 
|  | 4841 | if (NumBadMallocs != BadMallocCalls.size()) | 
|  | 4842 | return ChangeStatus::CHANGED; | 
|  | 4843 |  | 
|  | 4844 | return ChangeStatus::UNCHANGED; | 
|  | 4845 | } | 
|  | 4846 |  | 
|  | 4847 | struct AAHeapToStackFunction final : public AAHeapToStackImpl { | 
|  | 4848 | AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} | 
|  | 4849 |  | 
|  | 4850 | /// See AbstractAttribute::trackStatistics() | 
|  | 4851 | void trackStatistics() const override { | 
|  | 4852 | STATS_DECL(MallocCalls, Function, | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4853 | "Number of malloc calls converted to allocas"); | 
|  | 4854 | for (auto *C : MallocCalls) | 
|  | 4855 | if (!BadMallocCalls.count(C)) | 
|  | 4856 | ++BUILD_STAT_NAME(MallocCalls, Function); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4857 | } | 
|  | 4858 | }; | 
|  | 4859 |  | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 4860 | /// ----------------------- Privatizable Pointers ------------------------------ | 
|  | 4861 | struct AAPrivatizablePtrImpl : public AAPrivatizablePtr { | 
|  | 4862 | AAPrivatizablePtrImpl(const IRPosition &IRP) | 
|  | 4863 | : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {} | 
|  | 4864 |  | 
|  | 4865 | ChangeStatus indicatePessimisticFixpoint() override { | 
|  | 4866 | AAPrivatizablePtr::indicatePessimisticFixpoint(); | 
|  | 4867 | PrivatizableType = nullptr; | 
|  | 4868 | return ChangeStatus::CHANGED; | 
|  | 4869 | } | 
|  | 4870 |  | 
|  | 4871 | /// Identify the type we can chose for a private copy of the underlying | 
|  | 4872 | /// argument. None means it is not clear yet, nullptr means there is none. | 
|  | 4873 | virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0; | 
|  | 4874 |  | 
|  | 4875 | /// Return a privatizable type that encloses both T0 and T1. | 
|  | 4876 | /// TODO: This is merely a stub for now as we should manage a mapping as well. | 
|  | 4877 | Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) { | 
|  | 4878 | if (!T0.hasValue()) | 
|  | 4879 | return T1; | 
|  | 4880 | if (!T1.hasValue()) | 
|  | 4881 | return T0; | 
|  | 4882 | if (T0 == T1) | 
|  | 4883 | return T0; | 
|  | 4884 | return nullptr; | 
|  | 4885 | } | 
|  | 4886 |  | 
|  | 4887 | Optional<Type *> getPrivatizableType() const override { | 
|  | 4888 | return PrivatizableType; | 
|  | 4889 | } | 
|  | 4890 |  | 
|  | 4891 | const std::string getAsStr() const override { | 
|  | 4892 | return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]"; | 
|  | 4893 | } | 
|  | 4894 |  | 
|  | 4895 | protected: | 
|  | 4896 | Optional<Type *> PrivatizableType; | 
|  | 4897 | }; | 
|  | 4898 |  | 
|  | 4899 | // TODO: Do this for call site arguments (probably also other values) as well. | 
|  | 4900 |  | 
|  | 4901 | struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl { | 
|  | 4902 | AAPrivatizablePtrArgument(const IRPosition &IRP) | 
|  | 4903 | : AAPrivatizablePtrImpl(IRP) {} | 
|  | 4904 |  | 
|  | 4905 | /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) | 
|  | 4906 | Optional<Type *> identifyPrivatizableType(Attributor &A) override { | 
|  | 4907 | // If this is a byval argument and we know all the call sites (so we can | 
|  | 4908 | // rewrite them), there is no need to check them explicitly. | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 4909 | bool AllCallSitesKnown; | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 4910 | if (getIRPosition().hasAttr(Attribute::ByVal) && | 
|  | 4911 | A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this, | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 4912 | true, AllCallSitesKnown)) | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 4913 | return getAssociatedValue().getType()->getPointerElementType(); | 
|  | 4914 |  | 
|  | 4915 | Optional<Type *> Ty; | 
|  | 4916 | unsigned ArgNo = getIRPosition().getArgNo(); | 
|  | 4917 |  | 
|  | 4918 | // Make sure the associated call site argument has the same type at all call | 
|  | 4919 | // sites and it is an allocation we know is safe to privatize, for now that | 
|  | 4920 | // means we only allow alloca instructions. | 
|  | 4921 | // TODO: We can additionally analyze the accesses in the callee to  create | 
|  | 4922 | //       the type from that information instead. That is a little more | 
|  | 4923 | //       involved and will be done in a follow up patch. | 
|  | 4924 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | 
|  | 4925 | IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); | 
|  | 4926 | // Check if a coresponding argument was found or if it is one not | 
|  | 4927 | // associated (which can happen for callback calls). | 
|  | 4928 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | 
|  | 4929 | return false; | 
|  | 4930 |  | 
|  | 4931 | // Check that all call sites agree on a type. | 
|  | 4932 | auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos); | 
|  | 4933 | Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType(); | 
|  | 4934 |  | 
|  | 4935 | LLVM_DEBUG({ | 
|  | 4936 | dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; | 
|  | 4937 | if (CSTy.hasValue() && CSTy.getValue()) | 
|  | 4938 | CSTy.getValue()->print(dbgs()); | 
|  | 4939 | else if (CSTy.hasValue()) | 
|  | 4940 | dbgs() << "<nullptr>"; | 
|  | 4941 | else | 
|  | 4942 | dbgs() << "<none>"; | 
|  | 4943 | }); | 
|  | 4944 |  | 
|  | 4945 | Ty = combineTypes(Ty, CSTy); | 
|  | 4946 |  | 
|  | 4947 | LLVM_DEBUG({ | 
|  | 4948 | dbgs() << " : New Type: "; | 
|  | 4949 | if (Ty.hasValue() && Ty.getValue()) | 
|  | 4950 | Ty.getValue()->print(dbgs()); | 
|  | 4951 | else if (Ty.hasValue()) | 
|  | 4952 | dbgs() << "<nullptr>"; | 
|  | 4953 | else | 
|  | 4954 | dbgs() << "<none>"; | 
|  | 4955 | dbgs() << "\n"; | 
|  | 4956 | }); | 
|  | 4957 |  | 
|  | 4958 | return !Ty.hasValue() || Ty.getValue(); | 
|  | 4959 | }; | 
|  | 4960 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 4961 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown)) | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 4962 | return nullptr; | 
|  | 4963 | return Ty; | 
|  | 4964 | } | 
|  | 4965 |  | 
|  | 4966 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4967 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4968 | PrivatizableType = identifyPrivatizableType(A); | 
|  | 4969 | if (!PrivatizableType.hasValue()) | 
|  | 4970 | return ChangeStatus::UNCHANGED; | 
|  | 4971 | if (!PrivatizableType.getValue()) | 
|  | 4972 | return indicatePessimisticFixpoint(); | 
|  | 4973 |  | 
|  | 4974 | // Avoid arguments with padding for now. | 
|  | 4975 | if (!getIRPosition().hasAttr(Attribute::ByVal) && | 
|  | 4976 | !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(), | 
|  | 4977 | A.getInfoCache().getDL())) { | 
|  | 4978 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n"); | 
|  | 4979 | return indicatePessimisticFixpoint(); | 
|  | 4980 | } | 
|  | 4981 |  | 
|  | 4982 | // Verify callee and caller agree on how the promoted argument would be | 
|  | 4983 | // passed. | 
|  | 4984 | // TODO: The use of the ArgumentPromotion interface here is ugly, we need a | 
|  | 4985 | // specialized form of TargetTransformInfo::areFunctionArgsABICompatible | 
|  | 4986 | // which doesn't require the arguments ArgumentPromotion wanted to pass. | 
|  | 4987 | Function &Fn = *getIRPosition().getAnchorScope(); | 
|  | 4988 | SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy; | 
|  | 4989 | ArgsToPromote.insert(getAssociatedArgument()); | 
|  | 4990 | const auto *TTI = | 
|  | 4991 | A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn); | 
|  | 4992 | if (!TTI || | 
|  | 4993 | !ArgumentPromotionPass::areFunctionArgsABICompatible( | 
|  | 4994 | Fn, *TTI, ArgsToPromote, Dummy) || | 
|  | 4995 | ArgsToPromote.empty()) { | 
|  | 4996 | LLVM_DEBUG( | 
|  | 4997 | dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " | 
|  | 4998 | << Fn.getName() << "\n"); | 
|  | 4999 | return indicatePessimisticFixpoint(); | 
|  | 5000 | } | 
|  | 5001 |  | 
|  | 5002 | // Collect the types that will replace the privatizable type in the function | 
|  | 5003 | // signature. | 
|  | 5004 | SmallVector<Type *, 16> ReplacementTypes; | 
|  | 5005 | identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); | 
|  | 5006 |  | 
|  | 5007 | // Register a rewrite of the argument. | 
|  | 5008 | Argument *Arg = getAssociatedArgument(); | 
|  | 5009 | if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) { | 
|  | 5010 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n"); | 
|  | 5011 | return indicatePessimisticFixpoint(); | 
|  | 5012 | } | 
|  | 5013 |  | 
|  | 5014 | unsigned ArgNo = Arg->getArgNo(); | 
|  | 5015 |  | 
|  | 5016 | // Helper to check if for the given call site the associated argument is | 
|  | 5017 | // passed to a callback where the privatization would be different. | 
|  | 5018 | auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) { | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5019 | SmallVector<const Use *, 4> CBUses; | 
|  | 5020 | AbstractCallSite::getCallbackUses(CS, CBUses); | 
|  | 5021 | for (const Use *U : CBUses) { | 
|  | 5022 | AbstractCallSite CBACS(U); | 
|  | 5023 | assert(CBACS && CBACS.isCallbackCall()); | 
|  | 5024 | for (Argument &CBArg : CBACS.getCalledFunction()->args()) { | 
|  | 5025 | int CBArgNo = CBACS.getCallArgOperandNo(CBArg); | 
|  | 5026 |  | 
|  | 5027 | LLVM_DEBUG({ | 
|  | 5028 | dbgs() | 
|  | 5029 | << "[AAPrivatizablePtr] Argument " << *Arg | 
|  | 5030 | << "check if can be privatized in the context of its parent (" | 
|  | 5031 | << Arg->getParent()->getName() | 
|  | 5032 | << ")\n[AAPrivatizablePtr] because it is an argument in a " | 
|  | 5033 | "callback (" | 
|  | 5034 | << CBArgNo << "@" << CBACS.getCalledFunction()->getName() | 
|  | 5035 | << ")\n[AAPrivatizablePtr] " << CBArg << " : " | 
| Michael Forster | 676c296 | 2020-01-30 10:20:49 +0100 | [diff] [blame] | 5036 | << CBACS.getCallArgOperand(CBArg) << " vs " | 
|  | 5037 | << CS.getArgOperand(ArgNo) << "\n" | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5038 | << "[AAPrivatizablePtr] " << CBArg << " : " | 
|  | 5039 | << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; | 
|  | 5040 | }); | 
|  | 5041 |  | 
|  | 5042 | if (CBArgNo != int(ArgNo)) | 
|  | 5043 | continue; | 
|  | 5044 | const auto &CBArgPrivAA = | 
|  | 5045 | A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg)); | 
|  | 5046 | if (CBArgPrivAA.isValidState()) { | 
|  | 5047 | auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType(); | 
|  | 5048 | if (!CBArgPrivTy.hasValue()) | 
|  | 5049 | continue; | 
|  | 5050 | if (CBArgPrivTy.getValue() == PrivatizableType) | 
|  | 5051 | continue; | 
|  | 5052 | } | 
|  | 5053 |  | 
|  | 5054 | LLVM_DEBUG({ | 
|  | 5055 | dbgs() << "[AAPrivatizablePtr] Argument " << *Arg | 
|  | 5056 | << " cannot be privatized in the context of its parent (" | 
|  | 5057 | << Arg->getParent()->getName() | 
|  | 5058 | << ")\n[AAPrivatizablePtr] because it is an argument in a " | 
|  | 5059 | "callback (" | 
|  | 5060 | << CBArgNo << "@" << CBACS.getCalledFunction()->getName() | 
|  | 5061 | << ").\n[AAPrivatizablePtr] for which the argument " | 
|  | 5062 | "privatization is not compatible.\n"; | 
|  | 5063 | }); | 
|  | 5064 | return false; | 
|  | 5065 | } | 
|  | 5066 | } | 
|  | 5067 | return true; | 
|  | 5068 | }; | 
|  | 5069 |  | 
|  | 5070 | // Helper to check if for the given call site the associated argument is | 
|  | 5071 | // passed to a direct call where the privatization would be different. | 
|  | 5072 | auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) { | 
|  | 5073 | CallBase *DC = cast<CallBase>(ACS.getInstruction()); | 
|  | 5074 | int DCArgNo = ACS.getCallArgOperandNo(ArgNo); | 
|  | 5075 | assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() && | 
|  | 5076 | "Expected a direct call operand for callback call operand"); | 
|  | 5077 |  | 
|  | 5078 | LLVM_DEBUG({ | 
|  | 5079 | dbgs() << "[AAPrivatizablePtr] Argument " << *Arg | 
|  | 5080 | << " check if be privatized in the context of its parent (" | 
|  | 5081 | << Arg->getParent()->getName() | 
|  | 5082 | << ")\n[AAPrivatizablePtr] because it is an argument in a " | 
|  | 5083 | "direct call of (" | 
|  | 5084 | << DCArgNo << "@" << DC->getCalledFunction()->getName() | 
|  | 5085 | << ").\n"; | 
|  | 5086 | }); | 
|  | 5087 |  | 
|  | 5088 | Function *DCCallee = DC->getCalledFunction(); | 
|  | 5089 | if (unsigned(DCArgNo) < DCCallee->arg_size()) { | 
|  | 5090 | const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>( | 
|  | 5091 | *this, IRPosition::argument(*DCCallee->getArg(DCArgNo))); | 
|  | 5092 | if (DCArgPrivAA.isValidState()) { | 
|  | 5093 | auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType(); | 
|  | 5094 | if (!DCArgPrivTy.hasValue()) | 
|  | 5095 | return true; | 
|  | 5096 | if (DCArgPrivTy.getValue() == PrivatizableType) | 
|  | 5097 | return true; | 
|  | 5098 | } | 
|  | 5099 | } | 
|  | 5100 |  | 
|  | 5101 | LLVM_DEBUG({ | 
|  | 5102 | dbgs() << "[AAPrivatizablePtr] Argument " << *Arg | 
|  | 5103 | << " cannot be privatized in the context of its parent (" | 
|  | 5104 | << Arg->getParent()->getName() | 
|  | 5105 | << ")\n[AAPrivatizablePtr] because it is an argument in a " | 
|  | 5106 | "direct call of (" | 
|  | 5107 | << ACS.getCallSite().getCalledFunction()->getName() | 
|  | 5108 | << ").\n[AAPrivatizablePtr] for which the argument " | 
|  | 5109 | "privatization is not compatible.\n"; | 
|  | 5110 | }); | 
|  | 5111 | return false; | 
|  | 5112 | }; | 
|  | 5113 |  | 
|  | 5114 | // Helper to check if the associated argument is used at the given abstract | 
|  | 5115 | // call site in a way that is incompatible with the privatization assumed | 
|  | 5116 | // here. | 
|  | 5117 | auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) { | 
|  | 5118 | if (ACS.isDirectCall()) | 
|  | 5119 | return IsCompatiblePrivArgOfCallback(ACS.getCallSite()); | 
|  | 5120 | if (ACS.isCallbackCall()) | 
|  | 5121 | return IsCompatiblePrivArgOfDirectCS(ACS); | 
|  | 5122 | return false; | 
|  | 5123 | }; | 
|  | 5124 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 5125 | bool AllCallSitesKnown; | 
|  | 5126 | if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true, | 
|  | 5127 | AllCallSitesKnown)) | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 5128 | return indicatePessimisticFixpoint(); | 
|  | 5129 |  | 
|  | 5130 | return ChangeStatus::UNCHANGED; | 
|  | 5131 | } | 
|  | 5132 |  | 
|  | 5133 | /// Given a type to private \p PrivType, collect the constituates (which are | 
|  | 5134 | /// used) in \p ReplacementTypes. | 
|  | 5135 | static void | 
|  | 5136 | identifyReplacementTypes(Type *PrivType, | 
|  | 5137 | SmallVectorImpl<Type *> &ReplacementTypes) { | 
|  | 5138 | // TODO: For now we expand the privatization type to the fullest which can | 
|  | 5139 | //       lead to dead arguments that need to be removed later. | 
|  | 5140 | assert(PrivType && "Expected privatizable type!"); | 
|  | 5141 |  | 
|  | 5142 | // Traverse the type, extract constituate types on the outermost level. | 
|  | 5143 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { | 
|  | 5144 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) | 
|  | 5145 | ReplacementTypes.push_back(PrivStructType->getElementType(u)); | 
|  | 5146 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { | 
|  | 5147 | ReplacementTypes.append(PrivArrayType->getNumElements(), | 
|  | 5148 | PrivArrayType->getElementType()); | 
|  | 5149 | } else { | 
|  | 5150 | ReplacementTypes.push_back(PrivType); | 
|  | 5151 | } | 
|  | 5152 | } | 
|  | 5153 |  | 
|  | 5154 | /// Initialize \p Base according to the type \p PrivType at position \p IP. | 
|  | 5155 | /// The values needed are taken from the arguments of \p F starting at | 
|  | 5156 | /// position \p ArgNo. | 
|  | 5157 | static void createInitialization(Type *PrivType, Value &Base, Function &F, | 
|  | 5158 | unsigned ArgNo, Instruction &IP) { | 
|  | 5159 | assert(PrivType && "Expected privatizable type!"); | 
|  | 5160 |  | 
|  | 5161 | IRBuilder<NoFolder> IRB(&IP); | 
|  | 5162 | const DataLayout &DL = F.getParent()->getDataLayout(); | 
|  | 5163 |  | 
|  | 5164 | // Traverse the type, build GEPs and stores. | 
|  | 5165 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { | 
|  | 5166 | const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); | 
|  | 5167 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { | 
|  | 5168 | Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo(); | 
|  | 5169 | Value *Ptr = constructPointer( | 
|  | 5170 | PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL); | 
|  | 5171 | new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); | 
|  | 5172 | } | 
|  | 5173 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { | 
|  | 5174 | Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo(); | 
|  | 5175 | uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy); | 
|  | 5176 | for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { | 
|  | 5177 | Value *Ptr = | 
|  | 5178 | constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL); | 
|  | 5179 | new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); | 
|  | 5180 | } | 
|  | 5181 | } else { | 
|  | 5182 | new StoreInst(F.getArg(ArgNo), &Base, &IP); | 
|  | 5183 | } | 
|  | 5184 | } | 
|  | 5185 |  | 
|  | 5186 | /// Extract values from \p Base according to the type \p PrivType at the | 
|  | 5187 | /// call position \p ACS. The values are appended to \p ReplacementValues. | 
|  | 5188 | void createReplacementValues(Type *PrivType, AbstractCallSite ACS, | 
|  | 5189 | Value *Base, | 
|  | 5190 | SmallVectorImpl<Value *> &ReplacementValues) { | 
|  | 5191 | assert(Base && "Expected base value!"); | 
|  | 5192 | assert(PrivType && "Expected privatizable type!"); | 
|  | 5193 | Instruction *IP = ACS.getInstruction(); | 
|  | 5194 |  | 
|  | 5195 | IRBuilder<NoFolder> IRB(IP); | 
|  | 5196 | const DataLayout &DL = IP->getModule()->getDataLayout(); | 
|  | 5197 |  | 
|  | 5198 | if (Base->getType()->getPointerElementType() != PrivType) | 
|  | 5199 | Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(), | 
|  | 5200 | "", ACS.getInstruction()); | 
|  | 5201 |  | 
|  | 5202 | // TODO: Improve the alignment of the loads. | 
|  | 5203 | // Traverse the type, build GEPs and loads. | 
|  | 5204 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { | 
|  | 5205 | const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); | 
|  | 5206 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { | 
|  | 5207 | Type *PointeeTy = PrivStructType->getElementType(u); | 
|  | 5208 | Value *Ptr = | 
|  | 5209 | constructPointer(PointeeTy->getPointerTo(), Base, | 
|  | 5210 | PrivStructLayout->getElementOffset(u), IRB, DL); | 
|  | 5211 | LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); | 
|  | 5212 | L->setAlignment(MaybeAlign(1)); | 
|  | 5213 | ReplacementValues.push_back(L); | 
|  | 5214 | } | 
|  | 5215 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { | 
|  | 5216 | Type *PointeeTy = PrivArrayType->getElementType(); | 
|  | 5217 | uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); | 
|  | 5218 | Type *PointeePtrTy = PointeeTy->getPointerTo(); | 
|  | 5219 | for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { | 
|  | 5220 | Value *Ptr = | 
|  | 5221 | constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL); | 
|  | 5222 | LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP); | 
|  | 5223 | L->setAlignment(MaybeAlign(1)); | 
|  | 5224 | ReplacementValues.push_back(L); | 
|  | 5225 | } | 
|  | 5226 | } else { | 
|  | 5227 | LoadInst *L = new LoadInst(PrivType, Base, "", IP); | 
|  | 5228 | L->setAlignment(MaybeAlign(1)); | 
|  | 5229 | ReplacementValues.push_back(L); | 
|  | 5230 | } | 
|  | 5231 | } | 
|  | 5232 |  | 
|  | 5233 | /// See AbstractAttribute::manifest(...) | 
|  | 5234 | ChangeStatus manifest(Attributor &A) override { | 
|  | 5235 | if (!PrivatizableType.hasValue()) | 
|  | 5236 | return ChangeStatus::UNCHANGED; | 
|  | 5237 | assert(PrivatizableType.getValue() && "Expected privatizable type!"); | 
|  | 5238 |  | 
|  | 5239 | // Collect all tail calls in the function as we cannot allow new allocas to | 
|  | 5240 | // escape into tail recursion. | 
|  | 5241 | // TODO: Be smarter about new allocas escaping into tail calls. | 
|  | 5242 | SmallVector<CallInst *, 16> TailCalls; | 
|  | 5243 | if (!A.checkForAllInstructions( | 
|  | 5244 | [&](Instruction &I) { | 
|  | 5245 | CallInst &CI = cast<CallInst>(I); | 
|  | 5246 | if (CI.isTailCall()) | 
|  | 5247 | TailCalls.push_back(&CI); | 
|  | 5248 | return true; | 
|  | 5249 | }, | 
|  | 5250 | *this, {Instruction::Call})) | 
|  | 5251 | return ChangeStatus::UNCHANGED; | 
|  | 5252 |  | 
|  | 5253 | Argument *Arg = getAssociatedArgument(); | 
|  | 5254 |  | 
|  | 5255 | // Callback to repair the associated function. A new alloca is placed at the | 
|  | 5256 | // beginning and initialized with the values passed through arguments. The | 
|  | 5257 | // new alloca replaces the use of the old pointer argument. | 
|  | 5258 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB = | 
|  | 5259 | [=](const Attributor::ArgumentReplacementInfo &ARI, | 
|  | 5260 | Function &ReplacementFn, Function::arg_iterator ArgIt) { | 
|  | 5261 | BasicBlock &EntryBB = ReplacementFn.getEntryBlock(); | 
|  | 5262 | Instruction *IP = &*EntryBB.getFirstInsertionPt(); | 
|  | 5263 | auto *AI = new AllocaInst(PrivatizableType.getValue(), 0, | 
|  | 5264 | Arg->getName() + ".priv", IP); | 
|  | 5265 | createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn, | 
|  | 5266 | ArgIt->getArgNo(), *IP); | 
|  | 5267 | Arg->replaceAllUsesWith(AI); | 
|  | 5268 |  | 
|  | 5269 | for (CallInst *CI : TailCalls) | 
|  | 5270 | CI->setTailCall(false); | 
|  | 5271 | }; | 
|  | 5272 |  | 
|  | 5273 | // Callback to repair a call site of the associated function. The elements | 
|  | 5274 | // of the privatizable type are loaded prior to the call and passed to the | 
|  | 5275 | // new function version. | 
|  | 5276 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB = | 
|  | 5277 | [=](const Attributor::ArgumentReplacementInfo &ARI, | 
|  | 5278 | AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) { | 
|  | 5279 | createReplacementValues( | 
|  | 5280 | PrivatizableType.getValue(), ACS, | 
|  | 5281 | ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()), | 
|  | 5282 | NewArgOperands); | 
|  | 5283 | }; | 
|  | 5284 |  | 
|  | 5285 | // Collect the types that will replace the privatizable type in the function | 
|  | 5286 | // signature. | 
|  | 5287 | SmallVector<Type *, 16> ReplacementTypes; | 
|  | 5288 | identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); | 
|  | 5289 |  | 
|  | 5290 | // Register a rewrite of the argument. | 
|  | 5291 | if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes, | 
|  | 5292 | std::move(FnRepairCB), | 
|  | 5293 | std::move(ACSRepairCB))) | 
|  | 5294 | return ChangeStatus::CHANGED; | 
|  | 5295 | return ChangeStatus::UNCHANGED; | 
|  | 5296 | } | 
|  | 5297 |  | 
|  | 5298 | /// See AbstractAttribute::trackStatistics() | 
|  | 5299 | void trackStatistics() const override { | 
|  | 5300 | STATS_DECLTRACK_ARG_ATTR(privatizable_ptr); | 
|  | 5301 | } | 
|  | 5302 | }; | 
|  | 5303 |  | 
|  | 5304 | struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl { | 
|  | 5305 | AAPrivatizablePtrFloating(const IRPosition &IRP) | 
|  | 5306 | : AAPrivatizablePtrImpl(IRP) {} | 
|  | 5307 |  | 
|  | 5308 | /// See AbstractAttribute::initialize(...). | 
|  | 5309 | virtual void initialize(Attributor &A) override { | 
|  | 5310 | // TODO: We can privatize more than arguments. | 
|  | 5311 | indicatePessimisticFixpoint(); | 
|  | 5312 | } | 
|  | 5313 |  | 
|  | 5314 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 5315 | llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::" | 
|  | 5316 | "updateImpl will not be called"); | 
|  | 5317 | } | 
|  | 5318 |  | 
|  | 5319 | /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) | 
|  | 5320 | Optional<Type *> identifyPrivatizableType(Attributor &A) override { | 
|  | 5321 | Value *Obj = | 
|  | 5322 | GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL()); | 
|  | 5323 | if (!Obj) { | 
|  | 5324 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n"); | 
|  | 5325 | return nullptr; | 
|  | 5326 | } | 
|  | 5327 |  | 
|  | 5328 | if (auto *AI = dyn_cast<AllocaInst>(Obj)) | 
|  | 5329 | if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) | 
|  | 5330 | if (CI->isOne()) | 
|  | 5331 | return Obj->getType()->getPointerElementType(); | 
|  | 5332 | if (auto *Arg = dyn_cast<Argument>(Obj)) { | 
|  | 5333 | auto &PrivArgAA = | 
|  | 5334 | A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg)); | 
|  | 5335 | if (PrivArgAA.isAssumedPrivatizablePtr()) | 
|  | 5336 | return Obj->getType()->getPointerElementType(); | 
|  | 5337 | } | 
|  | 5338 |  | 
|  | 5339 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " | 
|  | 5340 | "alloca nor privatizable argument: " | 
|  | 5341 | << *Obj << "!\n"); | 
|  | 5342 | return nullptr; | 
|  | 5343 | } | 
|  | 5344 |  | 
|  | 5345 | /// See AbstractAttribute::trackStatistics() | 
|  | 5346 | void trackStatistics() const override { | 
|  | 5347 | STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr); | 
|  | 5348 | } | 
|  | 5349 | }; | 
|  | 5350 |  | 
|  | 5351 | struct AAPrivatizablePtrCallSiteArgument final | 
|  | 5352 | : public AAPrivatizablePtrFloating { | 
|  | 5353 | AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP) | 
|  | 5354 | : AAPrivatizablePtrFloating(IRP) {} | 
|  | 5355 |  | 
|  | 5356 | /// See AbstractAttribute::initialize(...). | 
|  | 5357 | void initialize(Attributor &A) override { | 
|  | 5358 | if (getIRPosition().hasAttr(Attribute::ByVal)) | 
|  | 5359 | indicateOptimisticFixpoint(); | 
|  | 5360 | } | 
|  | 5361 |  | 
|  | 5362 | /// See AbstractAttribute::updateImpl(...). | 
|  | 5363 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 5364 | PrivatizableType = identifyPrivatizableType(A); | 
|  | 5365 | if (!PrivatizableType.hasValue()) | 
|  | 5366 | return ChangeStatus::UNCHANGED; | 
|  | 5367 | if (!PrivatizableType.getValue()) | 
|  | 5368 | return indicatePessimisticFixpoint(); | 
|  | 5369 |  | 
|  | 5370 | const IRPosition &IRP = getIRPosition(); | 
|  | 5371 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); | 
|  | 5372 | if (!NoCaptureAA.isAssumedNoCapture()) { | 
|  | 5373 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n"); | 
|  | 5374 | return indicatePessimisticFixpoint(); | 
|  | 5375 | } | 
|  | 5376 |  | 
|  | 5377 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); | 
|  | 5378 | if (!NoAliasAA.isAssumedNoAlias()) { | 
|  | 5379 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n"); | 
|  | 5380 | return indicatePessimisticFixpoint(); | 
|  | 5381 | } | 
|  | 5382 |  | 
|  | 5383 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP); | 
|  | 5384 | if (!MemBehaviorAA.isAssumedReadOnly()) { | 
|  | 5385 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n"); | 
|  | 5386 | return indicatePessimisticFixpoint(); | 
|  | 5387 | } | 
|  | 5388 |  | 
|  | 5389 | return ChangeStatus::UNCHANGED; | 
|  | 5390 | } | 
|  | 5391 |  | 
|  | 5392 | /// See AbstractAttribute::trackStatistics() | 
|  | 5393 | void trackStatistics() const override { | 
|  | 5394 | STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr); | 
|  | 5395 | } | 
|  | 5396 | }; | 
|  | 5397 |  | 
|  | 5398 | struct AAPrivatizablePtrCallSiteReturned final | 
|  | 5399 | : public AAPrivatizablePtrFloating { | 
|  | 5400 | AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP) | 
|  | 5401 | : AAPrivatizablePtrFloating(IRP) {} | 
|  | 5402 |  | 
|  | 5403 | /// See AbstractAttribute::initialize(...). | 
|  | 5404 | void initialize(Attributor &A) override { | 
|  | 5405 | // TODO: We can privatize more than arguments. | 
|  | 5406 | indicatePessimisticFixpoint(); | 
|  | 5407 | } | 
|  | 5408 |  | 
|  | 5409 | /// See AbstractAttribute::trackStatistics() | 
|  | 5410 | void trackStatistics() const override { | 
|  | 5411 | STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr); | 
|  | 5412 | } | 
|  | 5413 | }; | 
|  | 5414 |  | 
|  | 5415 | struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating { | 
|  | 5416 | AAPrivatizablePtrReturned(const IRPosition &IRP) | 
|  | 5417 | : AAPrivatizablePtrFloating(IRP) {} | 
|  | 5418 |  | 
|  | 5419 | /// See AbstractAttribute::initialize(...). | 
|  | 5420 | void initialize(Attributor &A) override { | 
|  | 5421 | // TODO: We can privatize more than arguments. | 
|  | 5422 | indicatePessimisticFixpoint(); | 
|  | 5423 | } | 
|  | 5424 |  | 
|  | 5425 | /// See AbstractAttribute::trackStatistics() | 
|  | 5426 | void trackStatistics() const override { | 
|  | 5427 | STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr); | 
|  | 5428 | } | 
|  | 5429 | }; | 
|  | 5430 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5431 | /// -------------------- Memory Behavior Attributes ---------------------------- | 
|  | 5432 | /// Includes read-none, read-only, and write-only. | 
|  | 5433 | /// ---------------------------------------------------------------------------- | 
|  | 5434 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { | 
|  | 5435 | AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} | 
|  | 5436 |  | 
|  | 5437 | /// See AbstractAttribute::initialize(...). | 
|  | 5438 | void initialize(Attributor &A) override { | 
|  | 5439 | intersectAssumedBits(BEST_STATE); | 
|  | 5440 | getKnownStateFromValue(getIRPosition(), getState()); | 
|  | 5441 | IRAttribute::initialize(A); | 
|  | 5442 | } | 
|  | 5443 |  | 
|  | 5444 | /// Return the memory behavior information encoded in the IR for \p IRP. | 
|  | 5445 | static void getKnownStateFromValue(const IRPosition &IRP, | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5446 | BitIntegerState &State, | 
|  | 5447 | bool IgnoreSubsumingPositions = false) { | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5448 | SmallVector<Attribute, 2> Attrs; | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5449 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5450 | for (const Attribute &Attr : Attrs) { | 
|  | 5451 | switch (Attr.getKindAsEnum()) { | 
|  | 5452 | case Attribute::ReadNone: | 
|  | 5453 | State.addKnownBits(NO_ACCESSES); | 
|  | 5454 | break; | 
|  | 5455 | case Attribute::ReadOnly: | 
|  | 5456 | State.addKnownBits(NO_WRITES); | 
|  | 5457 | break; | 
|  | 5458 | case Attribute::WriteOnly: | 
|  | 5459 | State.addKnownBits(NO_READS); | 
|  | 5460 | break; | 
|  | 5461 | default: | 
|  | 5462 | llvm_unreachable("Unexpcted attribute!"); | 
|  | 5463 | } | 
|  | 5464 | } | 
|  | 5465 |  | 
|  | 5466 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { | 
|  | 5467 | if (!I->mayReadFromMemory()) | 
|  | 5468 | State.addKnownBits(NO_READS); | 
|  | 5469 | if (!I->mayWriteToMemory()) | 
|  | 5470 | State.addKnownBits(NO_WRITES); | 
|  | 5471 | } | 
|  | 5472 | } | 
|  | 5473 |  | 
|  | 5474 | /// See AbstractAttribute::getDeducedAttributes(...). | 
|  | 5475 | void getDeducedAttributes(LLVMContext &Ctx, | 
|  | 5476 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 5477 | assert(Attrs.size() == 0); | 
|  | 5478 | if (isAssumedReadNone()) | 
|  | 5479 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); | 
|  | 5480 | else if (isAssumedReadOnly()) | 
|  | 5481 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); | 
|  | 5482 | else if (isAssumedWriteOnly()) | 
|  | 5483 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); | 
|  | 5484 | assert(Attrs.size() <= 1); | 
|  | 5485 | } | 
|  | 5486 |  | 
|  | 5487 | /// See AbstractAttribute::manifest(...). | 
|  | 5488 | ChangeStatus manifest(Attributor &A) override { | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 5489 | const IRPosition &IRP = getIRPosition(); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5490 |  | 
|  | 5491 | // Check if we would improve the existing attributes first. | 
|  | 5492 | SmallVector<Attribute, 4> DeducedAttrs; | 
|  | 5493 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); | 
|  | 5494 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { | 
|  | 5495 | return IRP.hasAttr(Attr.getKindAsEnum(), | 
|  | 5496 | /* IgnoreSubsumingPositions */ true); | 
|  | 5497 | })) | 
|  | 5498 | return ChangeStatus::UNCHANGED; | 
|  | 5499 |  | 
|  | 5500 | // Clear existing attributes. | 
|  | 5501 | IRP.removeAttrs(AttrKinds); | 
|  | 5502 |  | 
|  | 5503 | // Use the generic manifest method. | 
|  | 5504 | return IRAttribute::manifest(A); | 
|  | 5505 | } | 
|  | 5506 |  | 
|  | 5507 | /// See AbstractState::getAsStr(). | 
|  | 5508 | const std::string getAsStr() const override { | 
|  | 5509 | if (isAssumedReadNone()) | 
|  | 5510 | return "readnone"; | 
|  | 5511 | if (isAssumedReadOnly()) | 
|  | 5512 | return "readonly"; | 
|  | 5513 | if (isAssumedWriteOnly()) | 
|  | 5514 | return "writeonly"; | 
|  | 5515 | return "may-read/write"; | 
|  | 5516 | } | 
|  | 5517 |  | 
|  | 5518 | /// The set of IR attributes AAMemoryBehavior deals with. | 
|  | 5519 | static const Attribute::AttrKind AttrKinds[3]; | 
|  | 5520 | }; | 
|  | 5521 |  | 
|  | 5522 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { | 
|  | 5523 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; | 
|  | 5524 |  | 
|  | 5525 | /// Memory behavior attribute for a floating value. | 
|  | 5526 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { | 
|  | 5527 | AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 5528 |  | 
|  | 5529 | /// See AbstractAttribute::initialize(...). | 
|  | 5530 | void initialize(Attributor &A) override { | 
|  | 5531 | AAMemoryBehaviorImpl::initialize(A); | 
|  | 5532 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 5533 | for (const Use &U : getAssociatedValue().uses()) | 
|  | 5534 | Uses.insert(&U); | 
|  | 5535 | } | 
|  | 5536 |  | 
|  | 5537 | /// See AbstractAttribute::updateImpl(...). | 
|  | 5538 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 5539 |  | 
|  | 5540 | /// See AbstractAttribute::trackStatistics() | 
|  | 5541 | void trackStatistics() const override { | 
|  | 5542 | if (isAssumedReadNone()) | 
|  | 5543 | STATS_DECLTRACK_FLOATING_ATTR(readnone) | 
|  | 5544 | else if (isAssumedReadOnly()) | 
|  | 5545 | STATS_DECLTRACK_FLOATING_ATTR(readonly) | 
|  | 5546 | else if (isAssumedWriteOnly()) | 
|  | 5547 | STATS_DECLTRACK_FLOATING_ATTR(writeonly) | 
|  | 5548 | } | 
|  | 5549 |  | 
|  | 5550 | private: | 
|  | 5551 | /// Return true if users of \p UserI might access the underlying | 
|  | 5552 | /// variable/location described by \p U and should therefore be analyzed. | 
|  | 5553 | bool followUsersOfUseIn(Attributor &A, const Use *U, | 
|  | 5554 | const Instruction *UserI); | 
|  | 5555 |  | 
|  | 5556 | /// Update the state according to the effect of use \p U in \p UserI. | 
|  | 5557 | void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); | 
|  | 5558 |  | 
|  | 5559 | protected: | 
|  | 5560 | /// Container for (transitive) uses of the associated argument. | 
|  | 5561 | SetVector<const Use *> Uses; | 
|  | 5562 | }; | 
|  | 5563 |  | 
|  | 5564 | /// Memory behavior attribute for function argument. | 
|  | 5565 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { | 
|  | 5566 | AAMemoryBehaviorArgument(const IRPosition &IRP) | 
|  | 5567 | : AAMemoryBehaviorFloating(IRP) {} | 
|  | 5568 |  | 
|  | 5569 | /// See AbstractAttribute::initialize(...). | 
|  | 5570 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5571 | intersectAssumedBits(BEST_STATE); | 
|  | 5572 | const IRPosition &IRP = getIRPosition(); | 
|  | 5573 | // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we | 
|  | 5574 | // can query it when we use has/getAttr. That would allow us to reuse the | 
|  | 5575 | // initialize of the base class here. | 
|  | 5576 | bool HasByVal = | 
|  | 5577 | IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); | 
|  | 5578 | getKnownStateFromValue(IRP, getState(), | 
|  | 5579 | /* IgnoreSubsumingPositions */ HasByVal); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5580 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5581 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 5582 | Argument *Arg = getAssociatedArgument(); | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5583 | if (!Arg || !Arg->getParent()->hasExactDefinition()) { | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5584 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5585 | } else { | 
|  | 5586 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 5587 | for (const Use &U : Arg->uses()) | 
|  | 5588 | Uses.insert(&U); | 
|  | 5589 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5590 | } | 
|  | 5591 |  | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5592 | ChangeStatus manifest(Attributor &A) override { | 
|  | 5593 | // TODO: From readattrs.ll: "inalloca parameters are always | 
|  | 5594 | //                           considered written" | 
|  | 5595 | if (hasAttr({Attribute::InAlloca})) { | 
|  | 5596 | removeKnownBits(NO_WRITES); | 
|  | 5597 | removeAssumedBits(NO_WRITES); | 
|  | 5598 | } | 
|  | 5599 | return AAMemoryBehaviorFloating::manifest(A); | 
|  | 5600 | } | 
|  | 5601 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5602 | /// See AbstractAttribute::trackStatistics() | 
|  | 5603 | void trackStatistics() const override { | 
|  | 5604 | if (isAssumedReadNone()) | 
|  | 5605 | STATS_DECLTRACK_ARG_ATTR(readnone) | 
|  | 5606 | else if (isAssumedReadOnly()) | 
|  | 5607 | STATS_DECLTRACK_ARG_ATTR(readonly) | 
|  | 5608 | else if (isAssumedWriteOnly()) | 
|  | 5609 | STATS_DECLTRACK_ARG_ATTR(writeonly) | 
|  | 5610 | } | 
|  | 5611 | }; | 
|  | 5612 |  | 
|  | 5613 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { | 
|  | 5614 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) | 
|  | 5615 | : AAMemoryBehaviorArgument(IRP) {} | 
|  | 5616 |  | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5617 | /// See AbstractAttribute::initialize(...). | 
|  | 5618 | void initialize(Attributor &A) override { | 
|  | 5619 | if (Argument *Arg = getAssociatedArgument()) { | 
|  | 5620 | if (Arg->hasByValAttr()) { | 
|  | 5621 | addKnownBits(NO_WRITES); | 
|  | 5622 | removeKnownBits(NO_READS); | 
|  | 5623 | removeAssumedBits(NO_READS); | 
|  | 5624 | } | 
|  | 5625 | } else { | 
|  | 5626 | } | 
|  | 5627 | AAMemoryBehaviorArgument::initialize(A); | 
|  | 5628 | } | 
|  | 5629 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5630 | /// See AbstractAttribute::updateImpl(...). | 
|  | 5631 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 5632 | // TODO: Once we have call site specific value information we can provide | 
|  | 5633 | //       call site specific liveness liveness information and then it makes | 
|  | 5634 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 5635 | //       redirecting requests to the callee argument. | 
|  | 5636 | Argument *Arg = getAssociatedArgument(); | 
|  | 5637 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 5638 | auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); | 
|  | 5639 | return clampStateAndIndicateChange( | 
|  | 5640 | getState(), | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 5641 | static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5642 | } | 
|  | 5643 |  | 
|  | 5644 | /// See AbstractAttribute::trackStatistics() | 
|  | 5645 | void trackStatistics() const override { | 
|  | 5646 | if (isAssumedReadNone()) | 
|  | 5647 | STATS_DECLTRACK_CSARG_ATTR(readnone) | 
|  | 5648 | else if (isAssumedReadOnly()) | 
|  | 5649 | STATS_DECLTRACK_CSARG_ATTR(readonly) | 
|  | 5650 | else if (isAssumedWriteOnly()) | 
|  | 5651 | STATS_DECLTRACK_CSARG_ATTR(writeonly) | 
|  | 5652 | } | 
|  | 5653 | }; | 
|  | 5654 |  | 
|  | 5655 | /// Memory behavior attribute for a call site return position. | 
|  | 5656 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { | 
|  | 5657 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) | 
|  | 5658 | : AAMemoryBehaviorFloating(IRP) {} | 
|  | 5659 |  | 
|  | 5660 | /// See AbstractAttribute::manifest(...). | 
|  | 5661 | ChangeStatus manifest(Attributor &A) override { | 
|  | 5662 | // We do not annotate returned values. | 
|  | 5663 | return ChangeStatus::UNCHANGED; | 
|  | 5664 | } | 
|  | 5665 |  | 
|  | 5666 | /// See AbstractAttribute::trackStatistics() | 
|  | 5667 | void trackStatistics() const override {} | 
|  | 5668 | }; | 
|  | 5669 |  | 
|  | 5670 | /// An AA to represent the memory behavior function attributes. | 
|  | 5671 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { | 
|  | 5672 | AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 5673 |  | 
|  | 5674 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
|  | 5675 | virtual ChangeStatus updateImpl(Attributor &A) override; | 
|  | 5676 |  | 
|  | 5677 | /// See AbstractAttribute::manifest(...). | 
|  | 5678 | ChangeStatus manifest(Attributor &A) override { | 
|  | 5679 | Function &F = cast<Function>(getAnchorValue()); | 
|  | 5680 | if (isAssumedReadNone()) { | 
|  | 5681 | F.removeFnAttr(Attribute::ArgMemOnly); | 
|  | 5682 | F.removeFnAttr(Attribute::InaccessibleMemOnly); | 
|  | 5683 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); | 
|  | 5684 | } | 
|  | 5685 | return AAMemoryBehaviorImpl::manifest(A); | 
|  | 5686 | } | 
|  | 5687 |  | 
|  | 5688 | /// See AbstractAttribute::trackStatistics() | 
|  | 5689 | void trackStatistics() const override { | 
|  | 5690 | if (isAssumedReadNone()) | 
|  | 5691 | STATS_DECLTRACK_FN_ATTR(readnone) | 
|  | 5692 | else if (isAssumedReadOnly()) | 
|  | 5693 | STATS_DECLTRACK_FN_ATTR(readonly) | 
|  | 5694 | else if (isAssumedWriteOnly()) | 
|  | 5695 | STATS_DECLTRACK_FN_ATTR(writeonly) | 
|  | 5696 | } | 
|  | 5697 | }; | 
|  | 5698 |  | 
|  | 5699 | /// AAMemoryBehavior attribute for call sites. | 
|  | 5700 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { | 
|  | 5701 | AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 5702 |  | 
|  | 5703 | /// See AbstractAttribute::initialize(...). | 
|  | 5704 | void initialize(Attributor &A) override { | 
|  | 5705 | AAMemoryBehaviorImpl::initialize(A); | 
|  | 5706 | Function *F = getAssociatedFunction(); | 
|  | 5707 | if (!F || !F->hasExactDefinition()) | 
|  | 5708 | indicatePessimisticFixpoint(); | 
|  | 5709 | } | 
|  | 5710 |  | 
|  | 5711 | /// See AbstractAttribute::updateImpl(...). | 
|  | 5712 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 5713 | // TODO: Once we have call site specific value information we can provide | 
|  | 5714 | //       call site specific liveness liveness information and then it makes | 
|  | 5715 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 5716 | //       redirecting requests to the callee argument. | 
|  | 5717 | Function *F = getAssociatedFunction(); | 
|  | 5718 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 5719 | auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 5720 | return clampStateAndIndicateChange( | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 5721 | getState(), | 
|  | 5722 | static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5723 | } | 
|  | 5724 |  | 
|  | 5725 | /// See AbstractAttribute::trackStatistics() | 
|  | 5726 | void trackStatistics() const override { | 
|  | 5727 | if (isAssumedReadNone()) | 
|  | 5728 | STATS_DECLTRACK_CS_ATTR(readnone) | 
|  | 5729 | else if (isAssumedReadOnly()) | 
|  | 5730 | STATS_DECLTRACK_CS_ATTR(readonly) | 
|  | 5731 | else if (isAssumedWriteOnly()) | 
|  | 5732 | STATS_DECLTRACK_CS_ATTR(writeonly) | 
|  | 5733 | } | 
|  | 5734 | }; | 
| Benjamin Kramer | c5d1d56 | 2019-10-12 11:01:52 +0000 | [diff] [blame] | 5735 | } // namespace | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5736 |  | 
|  | 5737 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { | 
|  | 5738 |  | 
|  | 5739 | // The current assumed state used to determine a change. | 
|  | 5740 | auto AssumedState = getAssumed(); | 
|  | 5741 |  | 
|  | 5742 | auto CheckRWInst = [&](Instruction &I) { | 
|  | 5743 | // If the instruction has an own memory behavior state, use it to restrict | 
|  | 5744 | // the local state. No further analysis is required as the other memory | 
|  | 5745 | // state is as optimistic as it gets. | 
|  | 5746 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 5747 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 5748 | *this, IRPosition::callsite_function(ICS)); | 
|  | 5749 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | 
|  | 5750 | return !isAtFixpoint(); | 
|  | 5751 | } | 
|  | 5752 |  | 
|  | 5753 | // Remove access kind modifiers if necessary. | 
|  | 5754 | if (I.mayReadFromMemory()) | 
|  | 5755 | removeAssumedBits(NO_READS); | 
|  | 5756 | if (I.mayWriteToMemory()) | 
|  | 5757 | removeAssumedBits(NO_WRITES); | 
|  | 5758 | return !isAtFixpoint(); | 
|  | 5759 | }; | 
|  | 5760 |  | 
|  | 5761 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) | 
|  | 5762 | return indicatePessimisticFixpoint(); | 
|  | 5763 |  | 
|  | 5764 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | 
|  | 5765 | : ChangeStatus::UNCHANGED; | 
|  | 5766 | } | 
|  | 5767 |  | 
|  | 5768 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { | 
|  | 5769 |  | 
|  | 5770 | const IRPosition &IRP = getIRPosition(); | 
|  | 5771 | const IRPosition &FnPos = IRPosition::function_scope(IRP); | 
|  | 5772 | AAMemoryBehavior::StateType &S = getState(); | 
|  | 5773 |  | 
|  | 5774 | // First, check the function scope. We take the known information and we avoid | 
|  | 5775 | // work if the assumed information implies the current assumed information for | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5776 | // this attribute. This is a valid for all but byval arguments. | 
|  | 5777 | Argument *Arg = IRP.getAssociatedArgument(); | 
|  | 5778 | AAMemoryBehavior::base_t FnMemAssumedState = | 
|  | 5779 | AAMemoryBehavior::StateType::getWorstState(); | 
|  | 5780 | if (!Arg || !Arg->hasByValAttr()) { | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 5781 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 5782 | *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5783 | FnMemAssumedState = FnMemAA.getAssumed(); | 
|  | 5784 | S.addKnownBits(FnMemAA.getKnown()); | 
|  | 5785 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) | 
|  | 5786 | return ChangeStatus::UNCHANGED; | 
|  | 5787 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5788 |  | 
|  | 5789 | // Make sure the value is not captured (except through "return"), if | 
|  | 5790 | // it is, any information derived would be irrelevant anyway as we cannot | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5791 | // check the potential aliases introduced by the capture. However, no need | 
|  | 5792 | // to fall back to anythign less optimistic than the function state. | 
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 5793 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( | 
|  | 5794 | *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5795 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5796 | S.intersectAssumedBits(FnMemAssumedState); | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5797 | return ChangeStatus::CHANGED; | 
|  | 5798 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5799 |  | 
|  | 5800 | // The current assumed state used to determine a change. | 
|  | 5801 | auto AssumedState = S.getAssumed(); | 
|  | 5802 |  | 
|  | 5803 | // Liveness information to exclude dead users. | 
|  | 5804 | // TODO: Take the FnPos once we have call site specific liveness information. | 
|  | 5805 | const auto &LivenessAA = A.getAAFor<AAIsDead>( | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 5806 | *this, IRPosition::function(*IRP.getAssociatedFunction()), | 
|  | 5807 | /* TrackDependence */ false); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5808 |  | 
|  | 5809 | // Visit and expand uses until all are analyzed or a fixpoint is reached. | 
|  | 5810 | for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { | 
|  | 5811 | const Use *U = Uses[i]; | 
|  | 5812 | Instruction *UserI = cast<Instruction>(U->getUser()); | 
|  | 5813 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI | 
|  | 5814 | << " [Dead: " << (LivenessAA.isAssumedDead(UserI)) | 
|  | 5815 | << "]\n"); | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 5816 | if (LivenessAA.isAssumedDead(UserI)) { | 
|  | 5817 | A.recordDependence(LivenessAA, *this, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5818 | continue; | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 5819 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5820 |  | 
|  | 5821 | // Check if the users of UserI should also be visited. | 
|  | 5822 | if (followUsersOfUseIn(A, U, UserI)) | 
|  | 5823 | for (const Use &UserIUse : UserI->uses()) | 
|  | 5824 | Uses.insert(&UserIUse); | 
|  | 5825 |  | 
|  | 5826 | // If UserI might touch memory we analyze the use in detail. | 
|  | 5827 | if (UserI->mayReadOrWriteMemory()) | 
|  | 5828 | analyzeUseIn(A, U, UserI); | 
|  | 5829 | } | 
|  | 5830 |  | 
|  | 5831 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | 
|  | 5832 | : ChangeStatus::UNCHANGED; | 
|  | 5833 | } | 
|  | 5834 |  | 
|  | 5835 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, | 
|  | 5836 | const Instruction *UserI) { | 
|  | 5837 | // The loaded value is unrelated to the pointer argument, no need to | 
|  | 5838 | // follow the users of the load. | 
|  | 5839 | if (isa<LoadInst>(UserI)) | 
|  | 5840 | return false; | 
|  | 5841 |  | 
|  | 5842 | // By default we follow all uses assuming UserI might leak information on U, | 
|  | 5843 | // we have special handling for call sites operands though. | 
|  | 5844 | ImmutableCallSite ICS(UserI); | 
|  | 5845 | if (!ICS || !ICS.isArgOperand(U)) | 
|  | 5846 | return true; | 
|  | 5847 |  | 
|  | 5848 | // If the use is a call argument known not to be captured, the users of | 
|  | 5849 | // the call do not need to be visited because they have to be unrelated to | 
|  | 5850 | // the input. Note that this check is not trivial even though we disallow | 
|  | 5851 | // general capturing of the underlying argument. The reason is that the | 
|  | 5852 | // call might the argument "through return", which we allow and for which we | 
|  | 5853 | // need to check call users. | 
| Johannes Doerfert | ff6254d | 2020-01-10 12:32:24 -0600 | [diff] [blame] | 5854 | if (U->get()->getType()->isPointerTy()) { | 
|  | 5855 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 5856 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 5857 | *this, IRPosition::callsite_argument(ICS, ArgNo), | 
|  | 5858 | /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | ff6254d | 2020-01-10 12:32:24 -0600 | [diff] [blame] | 5859 | return !ArgNoCaptureAA.isAssumedNoCapture(); | 
|  | 5860 | } | 
|  | 5861 |  | 
|  | 5862 | return true; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5863 | } | 
|  | 5864 |  | 
|  | 5865 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, | 
|  | 5866 | const Instruction *UserI) { | 
|  | 5867 | assert(UserI->mayReadOrWriteMemory()); | 
|  | 5868 |  | 
|  | 5869 | switch (UserI->getOpcode()) { | 
|  | 5870 | default: | 
|  | 5871 | // TODO: Handle all atomics and other side-effect operations we know of. | 
|  | 5872 | break; | 
|  | 5873 | case Instruction::Load: | 
|  | 5874 | // Loads cause the NO_READS property to disappear. | 
|  | 5875 | removeAssumedBits(NO_READS); | 
|  | 5876 | return; | 
|  | 5877 |  | 
|  | 5878 | case Instruction::Store: | 
|  | 5879 | // Stores cause the NO_WRITES property to disappear if the use is the | 
|  | 5880 | // pointer operand. Note that we do assume that capturing was taken care of | 
|  | 5881 | // somewhere else. | 
|  | 5882 | if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) | 
|  | 5883 | removeAssumedBits(NO_WRITES); | 
|  | 5884 | return; | 
|  | 5885 |  | 
|  | 5886 | case Instruction::Call: | 
|  | 5887 | case Instruction::CallBr: | 
|  | 5888 | case Instruction::Invoke: { | 
|  | 5889 | // For call sites we look at the argument memory behavior attribute (this | 
|  | 5890 | // could be recursive!) in order to restrict our own state. | 
|  | 5891 | ImmutableCallSite ICS(UserI); | 
|  | 5892 |  | 
|  | 5893 | // Give up on operand bundles. | 
|  | 5894 | if (ICS.isBundleOperand(U)) { | 
|  | 5895 | indicatePessimisticFixpoint(); | 
|  | 5896 | return; | 
|  | 5897 | } | 
|  | 5898 |  | 
|  | 5899 | // Calling a function does read the function pointer, maybe write it if the | 
|  | 5900 | // function is self-modifying. | 
|  | 5901 | if (ICS.isCallee(U)) { | 
|  | 5902 | removeAssumedBits(NO_READS); | 
|  | 5903 | break; | 
|  | 5904 | } | 
|  | 5905 |  | 
|  | 5906 | // Adjust the possible access behavior based on the information on the | 
|  | 5907 | // argument. | 
| Johannes Doerfert | ff6254d | 2020-01-10 12:32:24 -0600 | [diff] [blame] | 5908 | IRPosition Pos; | 
|  | 5909 | if (U->get()->getType()->isPointerTy()) | 
|  | 5910 | Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U)); | 
|  | 5911 | else | 
|  | 5912 | Pos = IRPosition::callsite_function(ICS); | 
| Johannes Doerfert | 7ad17e0 | 2020-01-12 00:11:56 -0600 | [diff] [blame] | 5913 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 5914 | *this, Pos, | 
|  | 5915 | /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5916 | // "assumed" has at most the same bits as the MemBehaviorAA assumed | 
|  | 5917 | // and at least "known". | 
|  | 5918 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | 
|  | 5919 | return; | 
|  | 5920 | } | 
|  | 5921 | }; | 
|  | 5922 |  | 
|  | 5923 | // Generally, look at the "may-properties" and adjust the assumed state if we | 
|  | 5924 | // did not trigger special handling before. | 
|  | 5925 | if (UserI->mayReadFromMemory()) | 
|  | 5926 | removeAssumedBits(NO_READS); | 
|  | 5927 | if (UserI->mayWriteToMemory()) | 
|  | 5928 | removeAssumedBits(NO_WRITES); | 
|  | 5929 | } | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 5930 | /// ------------------ Value Constant Range Attribute ------------------------- | 
| Hideto Ueno | e996303 | 2020-01-01 15:25:19 +0900 | [diff] [blame] | 5931 |  | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 5932 | struct AAValueConstantRangeImpl : AAValueConstantRange { | 
|  | 5933 | using StateType = IntegerRangeState; | 
|  | 5934 | AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {} | 
|  | 5935 |  | 
|  | 5936 | /// See AbstractAttribute::getAsStr(). | 
|  | 5937 | const std::string getAsStr() const override { | 
|  | 5938 | std::string Str; | 
|  | 5939 | llvm::raw_string_ostream OS(Str); | 
|  | 5940 | OS << "range(" << getBitWidth() << ")<"; | 
|  | 5941 | getKnown().print(OS); | 
|  | 5942 | OS << " / "; | 
|  | 5943 | getAssumed().print(OS); | 
|  | 5944 | OS << ">"; | 
|  | 5945 | return OS.str(); | 
|  | 5946 | } | 
|  | 5947 |  | 
|  | 5948 | /// Helper function to get a SCEV expr for the associated value at program | 
|  | 5949 | /// point \p I. | 
|  | 5950 | const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const { | 
|  | 5951 | if (!getAnchorScope()) | 
|  | 5952 | return nullptr; | 
|  | 5953 |  | 
|  | 5954 | ScalarEvolution *SE = | 
|  | 5955 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( | 
|  | 5956 | *getAnchorScope()); | 
|  | 5957 |  | 
|  | 5958 | LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>( | 
|  | 5959 | *getAnchorScope()); | 
|  | 5960 |  | 
|  | 5961 | if (!SE || !LI) | 
|  | 5962 | return nullptr; | 
|  | 5963 |  | 
|  | 5964 | const SCEV *S = SE->getSCEV(&getAssociatedValue()); | 
|  | 5965 | if (!I) | 
|  | 5966 | return S; | 
|  | 5967 |  | 
|  | 5968 | return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent())); | 
|  | 5969 | } | 
|  | 5970 |  | 
|  | 5971 | /// Helper function to get a range from SCEV for the associated value at | 
|  | 5972 | /// program point \p I. | 
|  | 5973 | ConstantRange getConstantRangeFromSCEV(Attributor &A, | 
|  | 5974 | const Instruction *I = nullptr) const { | 
|  | 5975 | if (!getAnchorScope()) | 
|  | 5976 | return getWorstState(getBitWidth()); | 
|  | 5977 |  | 
|  | 5978 | ScalarEvolution *SE = | 
|  | 5979 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( | 
|  | 5980 | *getAnchorScope()); | 
|  | 5981 |  | 
|  | 5982 | const SCEV *S = getSCEV(A, I); | 
|  | 5983 | if (!SE || !S) | 
|  | 5984 | return getWorstState(getBitWidth()); | 
|  | 5985 |  | 
|  | 5986 | return SE->getUnsignedRange(S); | 
|  | 5987 | } | 
|  | 5988 |  | 
|  | 5989 | /// Helper function to get a range from LVI for the associated value at | 
|  | 5990 | /// program point \p I. | 
|  | 5991 | ConstantRange | 
|  | 5992 | getConstantRangeFromLVI(Attributor &A, | 
|  | 5993 | const Instruction *CtxI = nullptr) const { | 
|  | 5994 | if (!getAnchorScope()) | 
|  | 5995 | return getWorstState(getBitWidth()); | 
|  | 5996 |  | 
|  | 5997 | LazyValueInfo *LVI = | 
|  | 5998 | A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>( | 
|  | 5999 | *getAnchorScope()); | 
|  | 6000 |  | 
|  | 6001 | if (!LVI || !CtxI) | 
|  | 6002 | return getWorstState(getBitWidth()); | 
|  | 6003 | return LVI->getConstantRange(&getAssociatedValue(), | 
|  | 6004 | const_cast<BasicBlock *>(CtxI->getParent()), | 
|  | 6005 | const_cast<Instruction *>(CtxI)); | 
|  | 6006 | } | 
|  | 6007 |  | 
|  | 6008 | /// See AAValueConstantRange::getKnownConstantRange(..). | 
|  | 6009 | ConstantRange | 
|  | 6010 | getKnownConstantRange(Attributor &A, | 
|  | 6011 | const Instruction *CtxI = nullptr) const override { | 
|  | 6012 | if (!CtxI || CtxI == getCtxI()) | 
|  | 6013 | return getKnown(); | 
|  | 6014 |  | 
|  | 6015 | ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); | 
|  | 6016 | ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); | 
|  | 6017 | return getKnown().intersectWith(SCEVR).intersectWith(LVIR); | 
|  | 6018 | } | 
|  | 6019 |  | 
|  | 6020 | /// See AAValueConstantRange::getAssumedConstantRange(..). | 
|  | 6021 | ConstantRange | 
|  | 6022 | getAssumedConstantRange(Attributor &A, | 
|  | 6023 | const Instruction *CtxI = nullptr) const override { | 
|  | 6024 | // TODO: Make SCEV use Attributor assumption. | 
|  | 6025 | //       We may be able to bound a variable range via assumptions in | 
|  | 6026 | //       Attributor. ex.) If x is assumed to be in [1, 3] and y is known to | 
|  | 6027 | //       evolve to x^2 + x, then we can say that y is in [2, 12]. | 
|  | 6028 |  | 
|  | 6029 | if (!CtxI || CtxI == getCtxI()) | 
|  | 6030 | return getAssumed(); | 
|  | 6031 |  | 
|  | 6032 | ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); | 
|  | 6033 | ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); | 
|  | 6034 | return getAssumed().intersectWith(SCEVR).intersectWith(LVIR); | 
|  | 6035 | } | 
|  | 6036 |  | 
|  | 6037 | /// See AbstractAttribute::initialize(..). | 
|  | 6038 | void initialize(Attributor &A) override { | 
|  | 6039 | // Intersect a range given by SCEV. | 
|  | 6040 | intersectKnown(getConstantRangeFromSCEV(A, getCtxI())); | 
|  | 6041 |  | 
|  | 6042 | // Intersect a range given by LVI. | 
|  | 6043 | intersectKnown(getConstantRangeFromLVI(A, getCtxI())); | 
|  | 6044 | } | 
|  | 6045 |  | 
|  | 6046 | /// Helper function to create MDNode for range metadata. | 
|  | 6047 | static MDNode * | 
|  | 6048 | getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx, | 
|  | 6049 | const ConstantRange &AssumedConstantRange) { | 
|  | 6050 | Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get( | 
|  | 6051 | Ty, AssumedConstantRange.getLower())), | 
|  | 6052 | ConstantAsMetadata::get(ConstantInt::get( | 
|  | 6053 | Ty, AssumedConstantRange.getUpper()))}; | 
|  | 6054 | return MDNode::get(Ctx, LowAndHigh); | 
|  | 6055 | } | 
|  | 6056 |  | 
|  | 6057 | /// Return true if \p Assumed is included in \p KnownRanges. | 
|  | 6058 | static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) { | 
|  | 6059 |  | 
|  | 6060 | if (Assumed.isFullSet()) | 
|  | 6061 | return false; | 
|  | 6062 |  | 
|  | 6063 | if (!KnownRanges) | 
|  | 6064 | return true; | 
|  | 6065 |  | 
|  | 6066 | // If multiple ranges are annotated in IR, we give up to annotate assumed | 
|  | 6067 | // range for now. | 
|  | 6068 |  | 
|  | 6069 | // TODO:  If there exists a known range which containts assumed range, we | 
|  | 6070 | // can say assumed range is better. | 
|  | 6071 | if (KnownRanges->getNumOperands() > 2) | 
|  | 6072 | return false; | 
|  | 6073 |  | 
|  | 6074 | ConstantInt *Lower = | 
|  | 6075 | mdconst::extract<ConstantInt>(KnownRanges->getOperand(0)); | 
|  | 6076 | ConstantInt *Upper = | 
|  | 6077 | mdconst::extract<ConstantInt>(KnownRanges->getOperand(1)); | 
|  | 6078 |  | 
|  | 6079 | ConstantRange Known(Lower->getValue(), Upper->getValue()); | 
|  | 6080 | return Known.contains(Assumed) && Known != Assumed; | 
|  | 6081 | } | 
|  | 6082 |  | 
|  | 6083 | /// Helper function to set range metadata. | 
|  | 6084 | static bool | 
|  | 6085 | setRangeMetadataIfisBetterRange(Instruction *I, | 
|  | 6086 | const ConstantRange &AssumedConstantRange) { | 
|  | 6087 | auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range); | 
|  | 6088 | if (isBetterRange(AssumedConstantRange, OldRangeMD)) { | 
|  | 6089 | if (!AssumedConstantRange.isEmptySet()) { | 
|  | 6090 | I->setMetadata(LLVMContext::MD_range, | 
|  | 6091 | getMDNodeForConstantRange(I->getType(), I->getContext(), | 
|  | 6092 | AssumedConstantRange)); | 
|  | 6093 | return true; | 
|  | 6094 | } | 
|  | 6095 | } | 
|  | 6096 | return false; | 
|  | 6097 | } | 
|  | 6098 |  | 
|  | 6099 | /// See AbstractAttribute::manifest() | 
|  | 6100 | ChangeStatus manifest(Attributor &A) override { | 
|  | 6101 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 6102 | ConstantRange AssumedConstantRange = getAssumedConstantRange(A); | 
|  | 6103 | assert(!AssumedConstantRange.isFullSet() && "Invalid state"); | 
|  | 6104 |  | 
|  | 6105 | auto &V = getAssociatedValue(); | 
|  | 6106 | if (!AssumedConstantRange.isEmptySet() && | 
|  | 6107 | !AssumedConstantRange.isSingleElement()) { | 
|  | 6108 | if (Instruction *I = dyn_cast<Instruction>(&V)) | 
|  | 6109 | if (isa<CallInst>(I) || isa<LoadInst>(I)) | 
|  | 6110 | if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange)) | 
|  | 6111 | Changed = ChangeStatus::CHANGED; | 
|  | 6112 | } | 
|  | 6113 |  | 
|  | 6114 | return Changed; | 
|  | 6115 | } | 
|  | 6116 | }; | 
|  | 6117 |  | 
| Johannes Doerfert | ea5fabe | 2020-01-28 09:46:15 -0600 | [diff] [blame] | 6118 | struct AAValueConstantRangeArgument final | 
|  | 6119 | : AAArgumentFromCallSiteArguments< | 
|  | 6120 | AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> { | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6121 | AAValueConstantRangeArgument(const IRPosition &IRP) | 
| Johannes Doerfert | ea5fabe | 2020-01-28 09:46:15 -0600 | [diff] [blame] | 6122 | : AAArgumentFromCallSiteArguments< | 
|  | 6123 | AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>( | 
|  | 6124 | IRP) {} | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6125 |  | 
|  | 6126 | /// See AbstractAttribute::trackStatistics() | 
|  | 6127 | void trackStatistics() const override { | 
|  | 6128 | STATS_DECLTRACK_ARG_ATTR(value_range) | 
|  | 6129 | } | 
|  | 6130 | }; | 
|  | 6131 |  | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6132 | struct AAValueConstantRangeReturned | 
|  | 6133 | : AAReturnedFromReturnedValues<AAValueConstantRange, | 
|  | 6134 | AAValueConstantRangeImpl> { | 
|  | 6135 | using Base = AAReturnedFromReturnedValues<AAValueConstantRange, | 
|  | 6136 | AAValueConstantRangeImpl>; | 
|  | 6137 | AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6138 |  | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6139 | /// See AbstractAttribute::initialize(...). | 
|  | 6140 | void initialize(Attributor &A) override {} | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6141 |  | 
|  | 6142 | /// See AbstractAttribute::trackStatistics() | 
|  | 6143 | void trackStatistics() const override { | 
|  | 6144 | STATS_DECLTRACK_FNRET_ATTR(value_range) | 
|  | 6145 | } | 
|  | 6146 | }; | 
|  | 6147 |  | 
|  | 6148 | struct AAValueConstantRangeFloating : AAValueConstantRangeImpl { | 
|  | 6149 | AAValueConstantRangeFloating(const IRPosition &IRP) | 
|  | 6150 | : AAValueConstantRangeImpl(IRP) {} | 
|  | 6151 |  | 
|  | 6152 | /// See AbstractAttribute::initialize(...). | 
|  | 6153 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 028db8c4 | 2020-02-09 19:05:15 -0600 | [diff] [blame] | 6154 | AAValueConstantRangeImpl::initialize(A); | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6155 | Value &V = getAssociatedValue(); | 
|  | 6156 |  | 
|  | 6157 | if (auto *C = dyn_cast<ConstantInt>(&V)) { | 
|  | 6158 | unionAssumed(ConstantRange(C->getValue())); | 
|  | 6159 | indicateOptimisticFixpoint(); | 
|  | 6160 | return; | 
|  | 6161 | } | 
|  | 6162 |  | 
|  | 6163 | if (isa<UndefValue>(&V)) { | 
|  | 6164 | indicateOptimisticFixpoint(); | 
|  | 6165 | return; | 
|  | 6166 | } | 
|  | 6167 |  | 
|  | 6168 | if (auto *I = dyn_cast<Instruction>(&V)) | 
|  | 6169 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I)) { | 
|  | 6170 | Value *LHS = I->getOperand(0); | 
|  | 6171 | Value *RHS = I->getOperand(1); | 
|  | 6172 |  | 
|  | 6173 | if (LHS->getType()->isIntegerTy() && RHS->getType()->isIntegerTy()) | 
|  | 6174 | return; | 
|  | 6175 | } | 
|  | 6176 |  | 
|  | 6177 | // If it is a load instruction with range metadata, use it. | 
|  | 6178 | if (LoadInst *LI = dyn_cast<LoadInst>(&V)) | 
|  | 6179 | if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) { | 
|  | 6180 | intersectKnown(getConstantRangeFromMetadata(*RangeMD)); | 
|  | 6181 | return; | 
|  | 6182 | } | 
|  | 6183 |  | 
| Johannes Doerfert | ffdbd2a | 2020-02-09 19:07:30 -0600 | [diff] [blame] | 6184 | // We handle casts in the updateImpl. | 
|  | 6185 | // TODO: Allow non integers as well. | 
|  | 6186 | if (CastInst *CI = dyn_cast<CastInst>(&V)) | 
|  | 6187 | if (CI->getOperand(0)->getType()->isIntegerTy()) | 
|  | 6188 | return; | 
|  | 6189 |  | 
| Johannes Doerfert | 7e7e659 | 2020-02-09 19:08:04 -0600 | [diff] [blame] | 6190 | // We can work with select instruction as we traverse their operands | 
|  | 6191 | // during update. | 
|  | 6192 | if (isa<SelectInst>(V)) | 
|  | 6193 | return; | 
|  | 6194 |  | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6195 | // Otherwise we give up. | 
|  | 6196 | indicatePessimisticFixpoint(); | 
|  | 6197 |  | 
| Johannes Doerfert | 02bd818 | 2020-01-28 11:49:35 -0600 | [diff] [blame] | 6198 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: " | 
|  | 6199 | << getAssociatedValue() << "\n"); | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6200 | } | 
|  | 6201 |  | 
|  | 6202 | bool calculateBinaryOperator(Attributor &A, BinaryOperator *BinOp, | 
|  | 6203 | IntegerRangeState &T, Instruction *CtxI) { | 
|  | 6204 | Value *LHS = BinOp->getOperand(0); | 
|  | 6205 | Value *RHS = BinOp->getOperand(1); | 
|  | 6206 |  | 
|  | 6207 | auto &LHSAA = | 
|  | 6208 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS)); | 
|  | 6209 | auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); | 
|  | 6210 |  | 
|  | 6211 | auto &RHSAA = | 
|  | 6212 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS)); | 
|  | 6213 | auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); | 
|  | 6214 |  | 
|  | 6215 | auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange); | 
|  | 6216 |  | 
|  | 6217 | T.unionAssumed(AssumedRange); | 
|  | 6218 |  | 
|  | 6219 | // TODO: Track a known state too. | 
|  | 6220 |  | 
|  | 6221 | return T.isValidState(); | 
|  | 6222 | } | 
|  | 6223 |  | 
| Johannes Doerfert | ffdbd2a | 2020-02-09 19:07:30 -0600 | [diff] [blame] | 6224 | bool calculateCastInst(Attributor &A, CastInst *CastI, IntegerRangeState &T, | 
|  | 6225 | Instruction *CtxI) { | 
|  | 6226 | assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!"); | 
|  | 6227 | // TODO: Allow non integers as well. | 
|  | 6228 | Value &OpV = *CastI->getOperand(0); | 
|  | 6229 | assert(OpV.getType()->isIntegerTy() && "Expected integer cast"); | 
|  | 6230 |  | 
|  | 6231 | auto &OpAA = | 
|  | 6232 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV)); | 
|  | 6233 | T.unionAssumed( | 
|  | 6234 | OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth())); | 
|  | 6235 | return T.isValidState(); | 
|  | 6236 | } | 
|  | 6237 |  | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6238 | bool calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T, | 
|  | 6239 | Instruction *CtxI) { | 
|  | 6240 | Value *LHS = CmpI->getOperand(0); | 
|  | 6241 | Value *RHS = CmpI->getOperand(1); | 
|  | 6242 |  | 
|  | 6243 | auto &LHSAA = | 
|  | 6244 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS)); | 
|  | 6245 | auto &RHSAA = | 
|  | 6246 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS)); | 
|  | 6247 |  | 
|  | 6248 | auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); | 
|  | 6249 | auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); | 
|  | 6250 |  | 
|  | 6251 | // If one of them is empty set, we can't decide. | 
|  | 6252 | if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet()) | 
|  | 6253 | return true; | 
|  | 6254 |  | 
|  | 6255 | bool MustTrue = false, MustFalse = false; | 
|  | 6256 |  | 
|  | 6257 | auto AllowedRegion = | 
|  | 6258 | ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange); | 
|  | 6259 |  | 
|  | 6260 | auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion( | 
|  | 6261 | CmpI->getPredicate(), RHSAARange); | 
|  | 6262 |  | 
|  | 6263 | if (AllowedRegion.intersectWith(LHSAARange).isEmptySet()) | 
|  | 6264 | MustFalse = true; | 
|  | 6265 |  | 
|  | 6266 | if (SatisfyingRegion.contains(LHSAARange)) | 
|  | 6267 | MustTrue = true; | 
|  | 6268 |  | 
|  | 6269 | assert((!MustTrue || !MustFalse) && | 
|  | 6270 | "Either MustTrue or MustFalse should be false!"); | 
|  | 6271 |  | 
|  | 6272 | if (MustTrue) | 
|  | 6273 | T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1))); | 
|  | 6274 | else if (MustFalse) | 
|  | 6275 | T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0))); | 
|  | 6276 | else | 
|  | 6277 | T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true)); | 
|  | 6278 |  | 
|  | 6279 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA | 
|  | 6280 | << " " << RHSAA << "\n"); | 
|  | 6281 |  | 
|  | 6282 | // TODO: Track a known state too. | 
|  | 6283 | return T.isValidState(); | 
|  | 6284 | } | 
|  | 6285 |  | 
|  | 6286 | /// See AbstractAttribute::updateImpl(...). | 
|  | 6287 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 6288 | Instruction *CtxI = getCtxI(); | 
|  | 6289 | auto VisitValueCB = [&](Value &V, IntegerRangeState &T, | 
|  | 6290 | bool Stripped) -> bool { | 
|  | 6291 | Instruction *I = dyn_cast<Instruction>(&V); | 
|  | 6292 | if (!I) { | 
|  | 6293 |  | 
|  | 6294 | // If the value is not instruction, we query AA to Attributor. | 
|  | 6295 | const auto &AA = | 
|  | 6296 | A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V)); | 
|  | 6297 |  | 
|  | 6298 | // Clamp operator is not used to utilize a program point CtxI. | 
|  | 6299 | T.unionAssumed(AA.getAssumedConstantRange(A, CtxI)); | 
|  | 6300 |  | 
|  | 6301 | return T.isValidState(); | 
|  | 6302 | } | 
|  | 6303 |  | 
|  | 6304 | if (auto *BinOp = dyn_cast<BinaryOperator>(I)) | 
|  | 6305 | return calculateBinaryOperator(A, BinOp, T, CtxI); | 
|  | 6306 | else if (auto *CmpI = dyn_cast<CmpInst>(I)) | 
|  | 6307 | return calculateCmpInst(A, CmpI, T, CtxI); | 
| Johannes Doerfert | ffdbd2a | 2020-02-09 19:07:30 -0600 | [diff] [blame] | 6308 | else if (auto *CastI = dyn_cast<CastInst>(I)) | 
|  | 6309 | return calculateCastInst(A, CastI, T, CtxI); | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6310 | else { | 
|  | 6311 | // Give up with other instructions. | 
|  | 6312 | // TODO: Add other instructions | 
|  | 6313 |  | 
|  | 6314 | T.indicatePessimisticFixpoint(); | 
|  | 6315 | return false; | 
|  | 6316 | } | 
|  | 6317 | }; | 
|  | 6318 |  | 
|  | 6319 | IntegerRangeState T(getBitWidth()); | 
|  | 6320 |  | 
|  | 6321 | if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>( | 
|  | 6322 | A, getIRPosition(), *this, T, VisitValueCB)) | 
|  | 6323 | return indicatePessimisticFixpoint(); | 
|  | 6324 |  | 
|  | 6325 | return clampStateAndIndicateChange(getState(), T); | 
|  | 6326 | } | 
|  | 6327 |  | 
|  | 6328 | /// See AbstractAttribute::trackStatistics() | 
|  | 6329 | void trackStatistics() const override { | 
|  | 6330 | STATS_DECLTRACK_FLOATING_ATTR(value_range) | 
|  | 6331 | } | 
|  | 6332 | }; | 
|  | 6333 |  | 
|  | 6334 | struct AAValueConstantRangeFunction : AAValueConstantRangeImpl { | 
|  | 6335 | AAValueConstantRangeFunction(const IRPosition &IRP) | 
|  | 6336 | : AAValueConstantRangeImpl(IRP) {} | 
|  | 6337 |  | 
|  | 6338 | /// See AbstractAttribute::initialize(...). | 
|  | 6339 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 6340 | llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will " | 
|  | 6341 | "not be called"); | 
|  | 6342 | } | 
|  | 6343 |  | 
|  | 6344 | /// See AbstractAttribute::trackStatistics() | 
|  | 6345 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) } | 
|  | 6346 | }; | 
|  | 6347 |  | 
|  | 6348 | struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction { | 
|  | 6349 | AAValueConstantRangeCallSite(const IRPosition &IRP) | 
|  | 6350 | : AAValueConstantRangeFunction(IRP) {} | 
|  | 6351 |  | 
|  | 6352 | /// See AbstractAttribute::trackStatistics() | 
|  | 6353 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) } | 
|  | 6354 | }; | 
|  | 6355 |  | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6356 | struct AAValueConstantRangeCallSiteReturned | 
|  | 6357 | : AACallSiteReturnedFromReturned<AAValueConstantRange, | 
|  | 6358 | AAValueConstantRangeImpl> { | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6359 | AAValueConstantRangeCallSiteReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6360 | : AACallSiteReturnedFromReturned<AAValueConstantRange, | 
|  | 6361 | AAValueConstantRangeImpl>(IRP) {} | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6362 |  | 
|  | 6363 | /// See AbstractAttribute::initialize(...). | 
|  | 6364 | void initialize(Attributor &A) override { | 
|  | 6365 | // If it is a load instruction with range metadata, use the metadata. | 
|  | 6366 | if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue())) | 
|  | 6367 | if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range)) | 
|  | 6368 | intersectKnown(getConstantRangeFromMetadata(*RangeMD)); | 
|  | 6369 |  | 
| Johannes Doerfert | 791c9f1 | 2020-01-29 18:02:42 -0600 | [diff] [blame] | 6370 | AAValueConstantRangeImpl::initialize(A); | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 6371 | } | 
|  | 6372 |  | 
|  | 6373 | /// See AbstractAttribute::trackStatistics() | 
|  | 6374 | void trackStatistics() const override { | 
|  | 6375 | STATS_DECLTRACK_CSRET_ATTR(value_range) | 
|  | 6376 | } | 
|  | 6377 | }; | 
|  | 6378 | struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating { | 
|  | 6379 | AAValueConstantRangeCallSiteArgument(const IRPosition &IRP) | 
|  | 6380 | : AAValueConstantRangeFloating(IRP) {} | 
|  | 6381 |  | 
|  | 6382 | /// See AbstractAttribute::trackStatistics() | 
|  | 6383 | void trackStatistics() const override { | 
|  | 6384 | STATS_DECLTRACK_CSARG_ATTR(value_range) | 
|  | 6385 | } | 
|  | 6386 | }; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6387 | /// ---------------------------------------------------------------------------- | 
|  | 6388 | ///                               Attributor | 
|  | 6389 | /// ---------------------------------------------------------------------------- | 
|  | 6390 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6391 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, | 
|  | 6392 | const AAIsDead *LivenessAA) { | 
|  | 6393 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6394 | if (!CtxI || !Functions.count(const_cast<Function *>(CtxI->getFunction()))) | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6395 | return false; | 
|  | 6396 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6397 | // TODO: Find a good way to utilize fine and coarse grained liveness | 
|  | 6398 | // information. | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6399 | if (!LivenessAA) | 
|  | 6400 | LivenessAA = | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6401 | &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), | 
|  | 6402 | /* TrackDependence */ false); | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 6403 |  | 
|  | 6404 | // Don't check liveness for AAIsDead. | 
|  | 6405 | if (&AA == LivenessAA) | 
|  | 6406 | return false; | 
|  | 6407 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6408 | if (!LivenessAA->isAssumedDead(CtxI)) | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6409 | return false; | 
|  | 6410 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6411 | // We actually used liveness information so we have to record a dependence. | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6412 | recordDependence(*LivenessAA, AA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6413 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6414 | return true; | 
|  | 6415 | } | 
|  | 6416 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6417 | bool Attributor::checkForAllUses( | 
|  | 6418 | const function_ref<bool(const Use &, bool &)> &Pred, | 
|  | 6419 | const AbstractAttribute &QueryingAA, const Value &V) { | 
|  | 6420 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 6421 | SmallVector<const Use *, 16> Worklist; | 
|  | 6422 | SmallPtrSet<const Use *, 16> Visited; | 
|  | 6423 |  | 
|  | 6424 | for (const Use &U : V.uses()) | 
|  | 6425 | Worklist.push_back(&U); | 
|  | 6426 |  | 
|  | 6427 | LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() | 
|  | 6428 | << " initial uses to check\n"); | 
|  | 6429 |  | 
|  | 6430 | if (Worklist.empty()) | 
|  | 6431 | return true; | 
|  | 6432 |  | 
|  | 6433 | bool AnyDead = false; | 
|  | 6434 | const Function *ScopeFn = IRP.getAnchorScope(); | 
|  | 6435 | const auto *LivenessAA = | 
|  | 6436 | ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), | 
|  | 6437 | /* TrackDependence */ false) | 
|  | 6438 | : nullptr; | 
|  | 6439 |  | 
|  | 6440 | while (!Worklist.empty()) { | 
|  | 6441 | const Use *U = Worklist.pop_back_val(); | 
|  | 6442 | if (!Visited.insert(U).second) | 
|  | 6443 | continue; | 
|  | 6444 | LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << "\n"); | 
|  | 6445 | if (Instruction *UserI = dyn_cast<Instruction>(U->getUser())) | 
|  | 6446 | if (LivenessAA && LivenessAA->isAssumedDead(UserI)) { | 
|  | 6447 | LLVM_DEBUG(dbgs() << "[Attributor] Dead user: " << *UserI << ": " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 6448 | << *LivenessAA << "\n"); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6449 | AnyDead = true; | 
|  | 6450 | continue; | 
|  | 6451 | } | 
|  | 6452 |  | 
|  | 6453 | bool Follow = false; | 
|  | 6454 | if (!Pred(*U, Follow)) | 
|  | 6455 | return false; | 
|  | 6456 | if (!Follow) | 
|  | 6457 | continue; | 
|  | 6458 | for (const Use &UU : U->getUser()->uses()) | 
|  | 6459 | Worklist.push_back(&UU); | 
|  | 6460 | } | 
|  | 6461 |  | 
|  | 6462 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6463 | recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6464 |  | 
|  | 6465 | return true; | 
|  | 6466 | } | 
|  | 6467 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6468 | bool Attributor::checkForAllCallSites( | 
|  | 6469 | const function_ref<bool(AbstractCallSite)> &Pred, | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6470 | const AbstractAttribute &QueryingAA, bool RequireAllCallSites, | 
|  | 6471 | bool &AllCallSitesKnown) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6472 | // We can try to determine information from | 
|  | 6473 | // the call sites. However, this is only possible all call sites are known, | 
|  | 6474 | // hence the function has internal linkage. | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6475 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 6476 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 6477 | if (!AssociatedFunction) { | 
|  | 6478 | LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP | 
|  | 6479 | << "\n"); | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6480 | AllCallSitesKnown = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6481 | return false; | 
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 6482 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6483 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6484 | return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6485 | &QueryingAA, AllCallSitesKnown); | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6486 | } | 
|  | 6487 |  | 
|  | 6488 | bool Attributor::checkForAllCallSites( | 
|  | 6489 | const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6490 | bool RequireAllCallSites, const AbstractAttribute *QueryingAA, | 
|  | 6491 | bool &AllCallSitesKnown) { | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6492 | if (RequireAllCallSites && !Fn.hasLocalLinkage()) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6493 | LLVM_DEBUG( | 
|  | 6494 | dbgs() | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6495 | << "[Attributor] Function " << Fn.getName() | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6496 | << " has no internal linkage, hence not all call sites are known\n"); | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6497 | AllCallSitesKnown = false; | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6498 | return false; | 
|  | 6499 | } | 
|  | 6500 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6501 | // If we do not require all call sites we might not see all. | 
|  | 6502 | AllCallSitesKnown = RequireAllCallSites; | 
|  | 6503 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6504 | for (const Use &U : Fn.uses()) { | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6505 | AbstractCallSite ACS(&U); | 
|  | 6506 | if (!ACS) { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6507 | LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6508 | << " has non call site use " << *U.get() << " in " | 
|  | 6509 | << *U.getUser() << "\n"); | 
| Johannes Doerfert | 2d77b0c | 2019-11-01 22:35:18 -0500 | [diff] [blame] | 6510 | // BlockAddress users are allowed. | 
|  | 6511 | if (isa<BlockAddress>(U.getUser())) | 
|  | 6512 | continue; | 
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 6513 | return false; | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6514 | } | 
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 6515 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6516 | Instruction *I = ACS.getInstruction(); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6517 | Function *Caller = I->getFunction(); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 6518 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6519 | const auto *LivenessAA = | 
|  | 6520 | lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA, | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6521 | /* TrackDependence */ false); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 6522 |  | 
|  | 6523 | // Skip dead calls. | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6524 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6525 | // We actually used liveness information so we have to record a | 
|  | 6526 | // dependence. | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 6527 | if (QueryingAA) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6528 | recordDependence(*LivenessAA, *QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6529 | AllCallSitesKnown = false; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 6530 | continue; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6531 | } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6532 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6533 | const Use *EffectiveUse = | 
|  | 6534 | ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; | 
|  | 6535 | if (!ACS.isCallee(EffectiveUse)) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6536 | if (!RequireAllCallSites) | 
|  | 6537 | continue; | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6538 | LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6539 | << " is an invalid use of " << Fn.getName() << "\n"); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6540 | return false; | 
|  | 6541 | } | 
|  | 6542 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6543 | if (Pred(ACS)) | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6544 | continue; | 
|  | 6545 |  | 
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 6546 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 6547 | << *ACS.getInstruction() << "\n"); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6548 | return false; | 
|  | 6549 | } | 
|  | 6550 |  | 
|  | 6551 | return true; | 
|  | 6552 | } | 
|  | 6553 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6554 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 6555 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6556 | &Pred, | 
|  | 6557 | const AbstractAttribute &QueryingAA) { | 
|  | 6558 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6559 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 6560 | // Since we need to provide return instructions we have to have an exact | 
|  | 6561 | // definition. | 
|  | 6562 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 6563 | if (!AssociatedFunction) | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6564 | return false; | 
|  | 6565 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6566 | // If this is a call site query we use the call site specific return values | 
|  | 6567 | // and liveness information. | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 6568 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 6569 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6570 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6571 | if (!AARetVal.getState().isValidState()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6572 | return false; | 
|  | 6573 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6574 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6575 | } | 
|  | 6576 |  | 
|  | 6577 | bool Attributor::checkForAllReturnedValues( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6578 | const function_ref<bool(Value &)> &Pred, | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6579 | const AbstractAttribute &QueryingAA) { | 
|  | 6580 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6581 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 6582 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 6583 | if (!AssociatedFunction) | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6584 | return false; | 
|  | 6585 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 6586 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 6587 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6588 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6589 | if (!AARetVal.getState().isValidState()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6590 | return false; | 
|  | 6591 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6592 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 6593 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 6594 | return Pred(RV); | 
|  | 6595 | }); | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 6596 | } | 
|  | 6597 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6598 | static bool | 
|  | 6599 | checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap, | 
|  | 6600 | const function_ref<bool(Instruction &)> &Pred, | 
|  | 6601 | const AAIsDead *LivenessAA, bool &AnyDead, | 
|  | 6602 | const ArrayRef<unsigned> &Opcodes) { | 
|  | 6603 | for (unsigned Opcode : Opcodes) { | 
|  | 6604 | for (Instruction *I : OpcodeInstMap[Opcode]) { | 
|  | 6605 | // Skip dead instructions. | 
|  | 6606 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { | 
|  | 6607 | AnyDead = true; | 
|  | 6608 | continue; | 
|  | 6609 | } | 
|  | 6610 |  | 
|  | 6611 | if (!Pred(*I)) | 
|  | 6612 | return false; | 
|  | 6613 | } | 
|  | 6614 | } | 
|  | 6615 | return true; | 
|  | 6616 | } | 
|  | 6617 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 6618 | bool Attributor::checkForAllInstructions( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6619 | const llvm::function_ref<bool(Instruction &)> &Pred, | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 6620 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 6621 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6622 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 6623 | // Since we need to provide instructions we have to have an exact definition. | 
|  | 6624 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 6625 | if (!AssociatedFunction) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6626 | return false; | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 6627 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 6628 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 6629 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6630 | const auto &LivenessAA = | 
|  | 6631 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); | 
|  | 6632 | bool AnyDead = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6633 |  | 
|  | 6634 | auto &OpcodeInstMap = | 
|  | 6635 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6636 | if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, | 
|  | 6637 | Opcodes)) | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6638 | return false; | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 6639 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6640 | // If we actually used liveness information so we have to record a dependence. | 
|  | 6641 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6642 | recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6643 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 6644 | return true; | 
|  | 6645 | } | 
|  | 6646 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6647 | bool Attributor::checkForAllReadWriteInstructions( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6648 | const llvm::function_ref<bool(Instruction &)> &Pred, | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 6649 | AbstractAttribute &QueryingAA) { | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6650 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6651 | const Function *AssociatedFunction = | 
|  | 6652 | QueryingAA.getIRPosition().getAssociatedFunction(); | 
|  | 6653 | if (!AssociatedFunction) | 
|  | 6654 | return false; | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6655 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 6656 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 6657 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
|  | 6658 | const auto &LivenessAA = | 
|  | 6659 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6660 | bool AnyDead = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6661 |  | 
|  | 6662 | for (Instruction *I : | 
|  | 6663 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6664 | // Skip dead instructions. | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6665 | if (LivenessAA.isAssumedDead(I)) { | 
|  | 6666 | AnyDead = true; | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6667 | continue; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6668 | } | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6669 |  | 
|  | 6670 | if (!Pred(*I)) | 
|  | 6671 | return false; | 
|  | 6672 | } | 
|  | 6673 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6674 | // If we actually used liveness information so we have to record a dependence. | 
|  | 6675 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6676 | recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 6677 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 6678 | return true; | 
|  | 6679 | } | 
|  | 6680 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6681 | ChangeStatus Attributor::run() { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6682 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " | 
|  | 6683 | << AllAbstractAttributes.size() | 
|  | 6684 | << " abstract attributes.\n"); | 
|  | 6685 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 6686 | // Now that all abstract attributes are collected and initialized we start | 
|  | 6687 | // the abstract analysis. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6688 |  | 
|  | 6689 | unsigned IterationCounter = 1; | 
|  | 6690 |  | 
|  | 6691 | SmallVector<AbstractAttribute *, 64> ChangedAAs; | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6692 | SetVector<AbstractAttribute *> Worklist, InvalidAAs; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6693 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); | 
|  | 6694 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 6695 | bool RecomputeDependences = false; | 
|  | 6696 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6697 | do { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6698 | // Remember the size to determine new attributes. | 
|  | 6699 | size_t NumAAs = AllAbstractAttributes.size(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6700 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter | 
|  | 6701 | << ", Worklist size: " << Worklist.size() << "\n"); | 
|  | 6702 |  | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6703 | // For invalid AAs we can fix dependent AAs that have a required dependence, | 
|  | 6704 | // thereby folding long dependence chains in a single step without the need | 
|  | 6705 | // to run updates. | 
|  | 6706 | for (unsigned u = 0; u < InvalidAAs.size(); ++u) { | 
|  | 6707 | AbstractAttribute *InvalidAA = InvalidAAs[u]; | 
|  | 6708 | auto &QuerriedAAs = QueryMap[InvalidAA]; | 
|  | 6709 | LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " | 
|  | 6710 | << QuerriedAAs.RequiredAAs.size() << "/" | 
|  | 6711 | << QuerriedAAs.OptionalAAs.size() | 
|  | 6712 | << " required/optional dependences\n"); | 
|  | 6713 | for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) { | 
|  | 6714 | AbstractState &DOIAAState = DepOnInvalidAA->getState(); | 
|  | 6715 | DOIAAState.indicatePessimisticFixpoint(); | 
|  | 6716 | ++NumAttributesFixedDueToRequiredDependences; | 
|  | 6717 | assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!"); | 
|  | 6718 | if (!DOIAAState.isValidState()) | 
|  | 6719 | InvalidAAs.insert(DepOnInvalidAA); | 
| Johannes Doerfert | 2240854 | 2020-01-28 09:38:07 -0600 | [diff] [blame] | 6720 | else | 
|  | 6721 | ChangedAAs.push_back(DepOnInvalidAA); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6722 | } | 
|  | 6723 | if (!RecomputeDependences) | 
|  | 6724 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), | 
|  | 6725 | QuerriedAAs.OptionalAAs.end()); | 
|  | 6726 | } | 
|  | 6727 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 6728 | // If dependences (=QueryMap) are recomputed we have to look at all abstract | 
|  | 6729 | // attributes again, regardless of what changed in the last iteration. | 
|  | 6730 | if (RecomputeDependences) { | 
|  | 6731 | LLVM_DEBUG( | 
|  | 6732 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); | 
|  | 6733 | QueryMap.clear(); | 
|  | 6734 | ChangedAAs.clear(); | 
|  | 6735 | Worklist.insert(AllAbstractAttributes.begin(), | 
|  | 6736 | AllAbstractAttributes.end()); | 
|  | 6737 | } | 
|  | 6738 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6739 | // Add all abstract attributes that are potentially dependent on one that | 
|  | 6740 | // changed to the work list. | 
|  | 6741 | for (AbstractAttribute *ChangedAA : ChangedAAs) { | 
|  | 6742 | auto &QuerriedAAs = QueryMap[ChangedAA]; | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6743 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), | 
|  | 6744 | QuerriedAAs.OptionalAAs.end()); | 
|  | 6745 | Worklist.insert(QuerriedAAs.RequiredAAs.begin(), | 
|  | 6746 | QuerriedAAs.RequiredAAs.end()); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6747 | } | 
|  | 6748 |  | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 6749 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter | 
|  | 6750 | << ", Worklist+Dependent size: " << Worklist.size() | 
|  | 6751 | << "\n"); | 
|  | 6752 |  | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6753 | // Reset the changed and invalid set. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6754 | ChangedAAs.clear(); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6755 | InvalidAAs.clear(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6756 |  | 
|  | 6757 | // Update all abstract attribute in the work list and record the ones that | 
|  | 6758 | // changed. | 
|  | 6759 | for (AbstractAttribute *AA : Worklist) | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 6760 | if (!AA->getState().isAtFixpoint() && !isAssumedDead(*AA, nullptr)) { | 
|  | 6761 | QueriedNonFixAA = false; | 
|  | 6762 | if (AA->update(*this) == ChangeStatus::CHANGED) { | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6763 | ChangedAAs.push_back(AA); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6764 | if (!AA->getState().isValidState()) | 
|  | 6765 | InvalidAAs.insert(AA); | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 6766 | } else if (!QueriedNonFixAA) { | 
|  | 6767 | // If the attribute did not query any non-fix information, the state | 
|  | 6768 | // will not change and we can indicate that right away. | 
|  | 6769 | AA->getState().indicateOptimisticFixpoint(); | 
|  | 6770 | } | 
|  | 6771 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6772 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 6773 | // Check if we recompute the dependences in the next iteration. | 
|  | 6774 | RecomputeDependences = (DepRecomputeInterval > 0 && | 
|  | 6775 | IterationCounter % DepRecomputeInterval == 0); | 
|  | 6776 |  | 
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 6777 | // Add attributes to the changed set if they have been created in the last | 
|  | 6778 | // iteration. | 
|  | 6779 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, | 
|  | 6780 | AllAbstractAttributes.end()); | 
|  | 6781 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6782 | // Reset the work list and repopulate with the changed abstract attributes. | 
|  | 6783 | // Note that dependent ones are added above. | 
|  | 6784 | Worklist.clear(); | 
|  | 6785 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); | 
|  | 6786 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 6787 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || | 
|  | 6788 | VerifyMaxFixpointIterations)); | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 6789 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6790 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " | 
|  | 6791 | << IterationCounter << "/" << MaxFixpointIterations | 
|  | 6792 | << " iterations\n"); | 
|  | 6793 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 6794 | size_t NumFinalAAs = AllAbstractAttributes.size(); | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 6795 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6796 | // Reset abstract arguments not settled in a sound fixpoint by now. This | 
|  | 6797 | // happens when we stopped the fixpoint iteration early. Note that only the | 
|  | 6798 | // ones marked as "changed" *and* the ones transitively depending on them | 
|  | 6799 | // need to be reverted to a pessimistic state. Others might not be in a | 
|  | 6800 | // fixpoint state but we can use the optimistic results for them anyway. | 
|  | 6801 | SmallPtrSet<AbstractAttribute *, 32> Visited; | 
|  | 6802 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { | 
|  | 6803 | AbstractAttribute *ChangedAA = ChangedAAs[u]; | 
|  | 6804 | if (!Visited.insert(ChangedAA).second) | 
|  | 6805 | continue; | 
|  | 6806 |  | 
|  | 6807 | AbstractState &State = ChangedAA->getState(); | 
|  | 6808 | if (!State.isAtFixpoint()) { | 
|  | 6809 | State.indicatePessimisticFixpoint(); | 
|  | 6810 |  | 
|  | 6811 | NumAttributesTimedOut++; | 
|  | 6812 | } | 
|  | 6813 |  | 
|  | 6814 | auto &QuerriedAAs = QueryMap[ChangedAA]; | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6815 | ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(), | 
|  | 6816 | QuerriedAAs.OptionalAAs.end()); | 
|  | 6817 | ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(), | 
|  | 6818 | QuerriedAAs.RequiredAAs.end()); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6819 | } | 
|  | 6820 |  | 
|  | 6821 | LLVM_DEBUG({ | 
|  | 6822 | if (!Visited.empty()) | 
|  | 6823 | dbgs() << "\n[Attributor] Finalized " << Visited.size() | 
|  | 6824 | << " abstract attributes.\n"; | 
|  | 6825 | }); | 
|  | 6826 |  | 
|  | 6827 | unsigned NumManifested = 0; | 
|  | 6828 | unsigned NumAtFixpoint = 0; | 
|  | 6829 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; | 
|  | 6830 | for (AbstractAttribute *AA : AllAbstractAttributes) { | 
|  | 6831 | AbstractState &State = AA->getState(); | 
|  | 6832 |  | 
|  | 6833 | // If there is not already a fixpoint reached, we can now take the | 
|  | 6834 | // optimistic state. This is correct because we enforced a pessimistic one | 
|  | 6835 | // on abstract attributes that were transitively dependent on a changed one | 
|  | 6836 | // already above. | 
|  | 6837 | if (!State.isAtFixpoint()) | 
|  | 6838 | State.indicateOptimisticFixpoint(); | 
|  | 6839 |  | 
|  | 6840 | // If the state is invalid, we do not try to manifest it. | 
|  | 6841 | if (!State.isValidState()) | 
|  | 6842 | continue; | 
|  | 6843 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 6844 | // Skip dead code. | 
|  | 6845 | if (isAssumedDead(*AA, nullptr)) | 
|  | 6846 | continue; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6847 | // Manifest the state and record if we changed the IR. | 
|  | 6848 | ChangeStatus LocalChange = AA->manifest(*this); | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 6849 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) | 
|  | 6850 | AA->trackStatistics(); | 
|  | 6851 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6852 | ManifestChange = ManifestChange | LocalChange; | 
|  | 6853 |  | 
|  | 6854 | NumAtFixpoint++; | 
|  | 6855 | NumManifested += (LocalChange == ChangeStatus::CHANGED); | 
|  | 6856 | } | 
|  | 6857 |  | 
|  | 6858 | (void)NumManifested; | 
|  | 6859 | (void)NumAtFixpoint; | 
|  | 6860 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested | 
|  | 6861 | << " arguments while " << NumAtFixpoint | 
|  | 6862 | << " were in a valid fixpoint state\n"); | 
|  | 6863 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6864 | NumAttributesManifested += NumManifested; | 
|  | 6865 | NumAttributesValidFixpoint += NumAtFixpoint; | 
|  | 6866 |  | 
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 6867 | (void)NumFinalAAs; | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6868 | assert(NumFinalAAs == AllAbstractAttributes.size() && | 
|  | 6869 | "Expected the final number of abstract attributes to remain " | 
|  | 6870 | "unchanged!"); | 
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 6871 |  | 
|  | 6872 | // 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] | 6873 | { | 
|  | 6874 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " | 
|  | 6875 | << ToBeDeletedFunctions.size() << " functions and " | 
|  | 6876 | << ToBeDeletedBlocks.size() << " blocks and " | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6877 | << ToBeDeletedInsts.size() << " instructions and " | 
|  | 6878 | << ToBeChangedUses.size() << " uses\n"); | 
|  | 6879 |  | 
| Alina Sbirlea | 9e66c4e | 2020-01-23 14:21:08 -0800 | [diff] [blame] | 6880 | SmallVector<WeakTrackingVH, 32> DeadInsts; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6881 | SmallVector<Instruction *, 32> TerminatorsToFold; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6882 |  | 
|  | 6883 | for (auto &It : ToBeChangedUses) { | 
|  | 6884 | Use *U = It.first; | 
|  | 6885 | Value *NewV = It.second; | 
|  | 6886 | Value *OldV = U->get(); | 
|  | 6887 | LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() | 
|  | 6888 | << " instead of " << *OldV << "\n"); | 
|  | 6889 | U->set(NewV); | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6890 | if (Instruction *I = dyn_cast<Instruction>(OldV)) { | 
|  | 6891 | CGModifiedFunctions.insert(I->getFunction()); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6892 | if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6893 | isInstructionTriviallyDead(I)) | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6894 | DeadInsts.push_back(I); | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6895 | } | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6896 | if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { | 
|  | 6897 | Instruction *UserI = cast<Instruction>(U->getUser()); | 
|  | 6898 | if (isa<UndefValue>(NewV)) { | 
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 6899 | ToBeChangedToUnreachableInsts.insert(UserI); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6900 | } else { | 
|  | 6901 | TerminatorsToFold.push_back(UserI); | 
|  | 6902 | } | 
|  | 6903 | } | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6904 | } | 
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 6905 | for (auto &V : InvokeWithDeadSuccessor) | 
|  | 6906 | if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) { | 
|  | 6907 | bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); | 
|  | 6908 | bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); | 
|  | 6909 | bool Invoke2CallAllowed = | 
|  | 6910 | !AAIsDeadFunction::mayCatchAsynchronousExceptions( | 
|  | 6911 | *II->getFunction()); | 
|  | 6912 | assert((UnwindBBIsDead || NormalBBIsDead) && | 
|  | 6913 | "Invoke does not have dead successors!"); | 
|  | 6914 | BasicBlock *BB = II->getParent(); | 
|  | 6915 | BasicBlock *NormalDestBB = II->getNormalDest(); | 
|  | 6916 | if (UnwindBBIsDead) { | 
|  | 6917 | Instruction *NormalNextIP = &NormalDestBB->front(); | 
|  | 6918 | if (Invoke2CallAllowed) { | 
|  | 6919 | changeToCall(II); | 
|  | 6920 | NormalNextIP = BB->getTerminator(); | 
|  | 6921 | } | 
|  | 6922 | if (NormalBBIsDead) | 
|  | 6923 | ToBeChangedToUnreachableInsts.insert(NormalNextIP); | 
|  | 6924 | } else { | 
|  | 6925 | assert(NormalBBIsDead && "Broken invariant!"); | 
|  | 6926 | if (!NormalDestBB->getUniquePredecessor()) | 
|  | 6927 | NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); | 
|  | 6928 | ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); | 
|  | 6929 | } | 
|  | 6930 | } | 
| Johannes Doerfert | 1e46eb7 | 2020-01-07 15:10:30 -0600 | [diff] [blame] | 6931 | for (auto &V : ToBeChangedToUnreachableInsts) | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6932 | if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { | 
|  | 6933 | CGModifiedFunctions.insert(I->getFunction()); | 
| Johannes Doerfert | 1e46eb7 | 2020-01-07 15:10:30 -0600 | [diff] [blame] | 6934 | changeToUnreachable(I, /* UseLLVMTrap */ false); | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6935 | } | 
|  | 6936 | for (Instruction *I : TerminatorsToFold) { | 
|  | 6937 | CGModifiedFunctions.insert(I->getFunction()); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6938 | ConstantFoldTerminator(I->getParent()); | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6939 | } | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6940 |  | 
| Johannes Doerfert | 5429c82 | 2020-01-11 23:30:36 -0600 | [diff] [blame] | 6941 | for (auto &V : ToBeDeletedInsts) { | 
|  | 6942 | if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6943 | CGModifiedFunctions.insert(I->getFunction()); | 
| Johannes Doerfert | 5429c82 | 2020-01-11 23:30:36 -0600 | [diff] [blame] | 6944 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | 
|  | 6945 | if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) | 
|  | 6946 | DeadInsts.push_back(I); | 
|  | 6947 | else | 
|  | 6948 | I->eraseFromParent(); | 
|  | 6949 | } | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6950 | } | 
|  | 6951 |  | 
|  | 6952 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 6953 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6954 | if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { | 
|  | 6955 | SmallVector<BasicBlock *, 8> ToBeDeletedBBs; | 
|  | 6956 | ToBeDeletedBBs.reserve(NumDeadBlocks); | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6957 | for (BasicBlock *BB : ToBeDeletedBlocks) { | 
|  | 6958 | CGModifiedFunctions.insert(BB->getParent()); | 
|  | 6959 | ToBeDeletedBBs.push_back(BB); | 
|  | 6960 | } | 
| Johannes Doerfert | 5e442a5 | 2019-10-30 17:34:59 -0500 | [diff] [blame] | 6961 | // Actually we do not delete the blocks but squash them into a single | 
|  | 6962 | // unreachable but untangling branches that jump here is something we need | 
|  | 6963 | // to do in a more generic way. | 
|  | 6964 | DetatchDeadBlocks(ToBeDeletedBBs, nullptr); | 
|  | 6965 | STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); | 
|  | 6966 | BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6967 | } | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 6968 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6969 | // Identify dead internal functions and delete them. This happens outside | 
|  | 6970 | // the other fixpoint analysis as we might treat potentially dead functions | 
|  | 6971 | // as live to lower the number of iterations. If they happen to be dead, the | 
|  | 6972 | // below fixpoint loop will identify and eliminate them. | 
|  | 6973 | SmallVector<Function *, 8> InternalFns; | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 6974 | for (Function *F : Functions) | 
|  | 6975 | if (F->hasLocalLinkage()) | 
|  | 6976 | InternalFns.push_back(F); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6977 |  | 
|  | 6978 | bool FoundDeadFn = true; | 
|  | 6979 | while (FoundDeadFn) { | 
|  | 6980 | FoundDeadFn = false; | 
|  | 6981 | for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { | 
|  | 6982 | Function *F = InternalFns[u]; | 
|  | 6983 | if (!F) | 
|  | 6984 | continue; | 
|  | 6985 |  | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6986 | bool AllCallSitesKnown; | 
| Johannes Doerfert | 6b9ee2d | 2020-01-02 16:53:37 -0600 | [diff] [blame] | 6987 | if (!checkForAllCallSites( | 
|  | 6988 | [this](AbstractCallSite ACS) { | 
|  | 6989 | return ToBeDeletedFunctions.count( | 
|  | 6990 | ACS.getInstruction()->getFunction()); | 
|  | 6991 | }, | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 6992 | *F, true, nullptr, AllCallSitesKnown)) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6993 | continue; | 
|  | 6994 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6995 | ToBeDeletedFunctions.insert(F); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6996 | InternalFns[u] = nullptr; | 
|  | 6997 | FoundDeadFn = true; | 
|  | 6998 | } | 
|  | 6999 | } | 
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 7000 | } | 
|  | 7001 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7002 | // Rewrite the functions as requested during manifest. | 
|  | 7003 | ManifestChange = | 
|  | 7004 | ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions); | 
|  | 7005 |  | 
|  | 7006 | for (Function *Fn : CGModifiedFunctions) | 
|  | 7007 | CGUpdater.reanalyzeFunction(*Fn); | 
|  | 7008 |  | 
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 7009 | STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); | 
|  | 7010 | BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size(); | 
|  | 7011 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7012 | for (Function *Fn : ToBeDeletedFunctions) | 
|  | 7013 | CGUpdater.removeFunction(*Fn); | 
| Johannes Doerfert | 6b9ee2d | 2020-01-02 16:53:37 -0600 | [diff] [blame] | 7014 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 7015 | if (VerifyMaxFixpointIterations && | 
|  | 7016 | IterationCounter != MaxFixpointIterations) { | 
|  | 7017 | errs() << "\n[Attributor] Fixpoint iteration done after: " | 
|  | 7018 | << IterationCounter << "/" << MaxFixpointIterations | 
|  | 7019 | << " iterations\n"; | 
|  | 7020 | llvm_unreachable("The fixpoint was not reached with exactly the number of " | 
|  | 7021 | "specified iterations!"); | 
|  | 7022 | } | 
|  | 7023 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7024 | return ManifestChange; | 
|  | 7025 | } | 
|  | 7026 |  | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7027 | bool Attributor::isValidFunctionSignatureRewrite( | 
|  | 7028 | Argument &Arg, ArrayRef<Type *> ReplacementTypes) { | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7029 |  | 
|  | 7030 | auto CallSiteCanBeChanged = [](AbstractCallSite ACS) { | 
|  | 7031 | // Forbid must-tail calls for now. | 
|  | 7032 | return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall(); | 
|  | 7033 | }; | 
|  | 7034 |  | 
|  | 7035 | Function *Fn = Arg.getParent(); | 
|  | 7036 | // Avoid var-arg functions for now. | 
|  | 7037 | if (Fn->isVarArg()) { | 
|  | 7038 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); | 
|  | 7039 | return false; | 
|  | 7040 | } | 
|  | 7041 |  | 
|  | 7042 | // Avoid functions with complicated argument passing semantics. | 
|  | 7043 | AttributeList FnAttributeList = Fn->getAttributes(); | 
|  | 7044 | if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || | 
|  | 7045 | FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || | 
|  | 7046 | FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) { | 
|  | 7047 | LLVM_DEBUG( | 
|  | 7048 | dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); | 
|  | 7049 | return false; | 
|  | 7050 | } | 
|  | 7051 |  | 
|  | 7052 | // Avoid callbacks for now. | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7053 | bool AllCallSitesKnown; | 
|  | 7054 | if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr, | 
|  | 7055 | AllCallSitesKnown)) { | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7056 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); | 
|  | 7057 | return false; | 
|  | 7058 | } | 
|  | 7059 |  | 
|  | 7060 | auto InstPred = [](Instruction &I) { | 
|  | 7061 | if (auto *CI = dyn_cast<CallInst>(&I)) | 
|  | 7062 | return !CI->isMustTailCall(); | 
|  | 7063 | return true; | 
|  | 7064 | }; | 
|  | 7065 |  | 
|  | 7066 | // Forbid must-tail calls for now. | 
|  | 7067 | // TODO: | 
|  | 7068 | bool AnyDead; | 
|  | 7069 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); | 
|  | 7070 | if (!checkForAllInstructionsImpl(OpcodeInstMap, InstPred, nullptr, AnyDead, | 
|  | 7071 | {Instruction::Call})) { | 
|  | 7072 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); | 
|  | 7073 | return false; | 
|  | 7074 | } | 
|  | 7075 |  | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7076 | return true; | 
|  | 7077 | } | 
|  | 7078 |  | 
|  | 7079 | bool Attributor::registerFunctionSignatureRewrite( | 
|  | 7080 | Argument &Arg, ArrayRef<Type *> ReplacementTypes, | 
|  | 7081 | ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, | 
|  | 7082 | ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { | 
|  | 7083 | LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " | 
|  | 7084 | << Arg.getParent()->getName() << " with " | 
|  | 7085 | << ReplacementTypes.size() << " replacements\n"); | 
|  | 7086 | assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) && | 
|  | 7087 | "Cannot register an invalid rewrite"); | 
|  | 7088 |  | 
|  | 7089 | Function *Fn = Arg.getParent(); | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7090 | SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn]; | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7091 | if (ARIs.empty()) | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7092 | ARIs.resize(Fn->arg_size()); | 
|  | 7093 |  | 
|  | 7094 | // If we have a replacement already with less than or equal new arguments, | 
|  | 7095 | // ignore this request. | 
|  | 7096 | ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()]; | 
|  | 7097 | if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { | 
|  | 7098 | LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); | 
|  | 7099 | return false; | 
|  | 7100 | } | 
|  | 7101 |  | 
|  | 7102 | // If we have a replacement already but we like the new one better, delete | 
|  | 7103 | // the old. | 
|  | 7104 | if (ARI) | 
|  | 7105 | delete ARI; | 
|  | 7106 |  | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7107 | LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " | 
|  | 7108 | << Arg.getParent()->getName() << " with " | 
|  | 7109 | << ReplacementTypes.size() << " replacements\n"); | 
|  | 7110 |  | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7111 | // Remember the replacement. | 
|  | 7112 | ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, | 
|  | 7113 | std::move(CalleeRepairCB), | 
|  | 7114 | std::move(ACSRepairCB)); | 
|  | 7115 |  | 
|  | 7116 | return true; | 
|  | 7117 | } | 
|  | 7118 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7119 | ChangeStatus Attributor::rewriteFunctionSignatures( | 
|  | 7120 | SmallPtrSetImpl<Function *> &ModifiedFns) { | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7121 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 7122 |  | 
|  | 7123 | for (auto &It : ArgumentReplacementMap) { | 
|  | 7124 | Function *OldFn = It.getFirst(); | 
|  | 7125 |  | 
|  | 7126 | // Deleted functions do not require rewrites. | 
|  | 7127 | if (ToBeDeletedFunctions.count(OldFn)) | 
|  | 7128 | continue; | 
|  | 7129 |  | 
|  | 7130 | const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond(); | 
|  | 7131 | assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); | 
|  | 7132 |  | 
|  | 7133 | SmallVector<Type *, 16> NewArgumentTypes; | 
|  | 7134 | SmallVector<AttributeSet, 16> NewArgumentAttributes; | 
|  | 7135 |  | 
|  | 7136 | // Collect replacement argument types and copy over existing attributes. | 
|  | 7137 | AttributeList OldFnAttributeList = OldFn->getAttributes(); | 
|  | 7138 | for (Argument &Arg : OldFn->args()) { | 
|  | 7139 | if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) { | 
|  | 7140 | NewArgumentTypes.append(ARI->ReplacementTypes.begin(), | 
|  | 7141 | ARI->ReplacementTypes.end()); | 
|  | 7142 | NewArgumentAttributes.append(ARI->getNumReplacementArgs(), | 
|  | 7143 | AttributeSet()); | 
|  | 7144 | } else { | 
|  | 7145 | NewArgumentTypes.push_back(Arg.getType()); | 
|  | 7146 | NewArgumentAttributes.push_back( | 
|  | 7147 | OldFnAttributeList.getParamAttributes(Arg.getArgNo())); | 
|  | 7148 | } | 
|  | 7149 | } | 
|  | 7150 |  | 
|  | 7151 | FunctionType *OldFnTy = OldFn->getFunctionType(); | 
|  | 7152 | Type *RetTy = OldFnTy->getReturnType(); | 
|  | 7153 |  | 
|  | 7154 | // Construct the new function type using the new arguments types. | 
|  | 7155 | FunctionType *NewFnTy = | 
|  | 7156 | FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); | 
|  | 7157 |  | 
|  | 7158 | LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() | 
|  | 7159 | << "' from " << *OldFn->getFunctionType() << " to " | 
|  | 7160 | << *NewFnTy << "\n"); | 
|  | 7161 |  | 
|  | 7162 | // Create the new function body and insert it into the module. | 
|  | 7163 | Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), | 
|  | 7164 | OldFn->getAddressSpace(), ""); | 
|  | 7165 | OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); | 
|  | 7166 | NewFn->takeName(OldFn); | 
|  | 7167 | NewFn->copyAttributesFrom(OldFn); | 
|  | 7168 |  | 
|  | 7169 | // Patch the pointer to LLVM function in debug info descriptor. | 
|  | 7170 | NewFn->setSubprogram(OldFn->getSubprogram()); | 
|  | 7171 | OldFn->setSubprogram(nullptr); | 
|  | 7172 |  | 
|  | 7173 | // Recompute the parameter attributes list based on the new arguments for | 
|  | 7174 | // the function. | 
|  | 7175 | LLVMContext &Ctx = OldFn->getContext(); | 
|  | 7176 | NewFn->setAttributes(AttributeList::get( | 
|  | 7177 | Ctx, OldFnAttributeList.getFnAttributes(), | 
|  | 7178 | OldFnAttributeList.getRetAttributes(), NewArgumentAttributes)); | 
|  | 7179 |  | 
|  | 7180 | // Since we have now created the new function, splice the body of the old | 
|  | 7181 | // function right into the new function, leaving the old rotting hulk of the | 
|  | 7182 | // function empty. | 
|  | 7183 | NewFn->getBasicBlockList().splice(NewFn->begin(), | 
|  | 7184 | OldFn->getBasicBlockList()); | 
|  | 7185 |  | 
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 7186 | // Set of all "call-like" instructions that invoke the old function mapped | 
|  | 7187 | // to their new replacements. | 
|  | 7188 | SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs; | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7189 |  | 
|  | 7190 | // Callback to create a new "call-like" instruction for a given one. | 
|  | 7191 | auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { | 
|  | 7192 | CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); | 
|  | 7193 | const AttributeList &OldCallAttributeList = OldCB->getAttributes(); | 
|  | 7194 |  | 
|  | 7195 | // Collect the new argument operands for the replacement call site. | 
|  | 7196 | SmallVector<Value *, 16> NewArgOperands; | 
|  | 7197 | SmallVector<AttributeSet, 16> NewArgOperandAttributes; | 
|  | 7198 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { | 
|  | 7199 | unsigned NewFirstArgNum = NewArgOperands.size(); | 
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 7200 | (void)NewFirstArgNum; // only used inside assert. | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7201 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { | 
|  | 7202 | if (ARI->ACSRepairCB) | 
|  | 7203 | ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); | 
|  | 7204 | assert(ARI->getNumReplacementArgs() + NewFirstArgNum == | 
|  | 7205 | NewArgOperands.size() && | 
|  | 7206 | "ACS repair callback did not provide as many operand as new " | 
|  | 7207 | "types were registered!"); | 
|  | 7208 | // TODO: Exose the attribute set to the ACS repair callback | 
|  | 7209 | NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), | 
|  | 7210 | AttributeSet()); | 
|  | 7211 | } else { | 
|  | 7212 | NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); | 
|  | 7213 | NewArgOperandAttributes.push_back( | 
|  | 7214 | OldCallAttributeList.getParamAttributes(OldArgNum)); | 
|  | 7215 | } | 
|  | 7216 | } | 
|  | 7217 |  | 
|  | 7218 | assert(NewArgOperands.size() == NewArgOperandAttributes.size() && | 
|  | 7219 | "Mismatch # argument operands vs. # argument operand attributes!"); | 
|  | 7220 | assert(NewArgOperands.size() == NewFn->arg_size() && | 
|  | 7221 | "Mismatch # argument operands vs. # function arguments!"); | 
|  | 7222 |  | 
|  | 7223 | SmallVector<OperandBundleDef, 4> OperandBundleDefs; | 
|  | 7224 | OldCB->getOperandBundlesAsDefs(OperandBundleDefs); | 
|  | 7225 |  | 
|  | 7226 | // Create a new call or invoke instruction to replace the old one. | 
|  | 7227 | CallBase *NewCB; | 
|  | 7228 | if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { | 
|  | 7229 | NewCB = | 
|  | 7230 | InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), | 
|  | 7231 | NewArgOperands, OperandBundleDefs, "", OldCB); | 
|  | 7232 | } else { | 
|  | 7233 | auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, | 
|  | 7234 | "", OldCB); | 
|  | 7235 | NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); | 
|  | 7236 | NewCB = NewCI; | 
|  | 7237 | } | 
|  | 7238 |  | 
|  | 7239 | // Copy over various properties and the new attributes. | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7240 | uint64_t W; | 
|  | 7241 | if (OldCB->extractProfTotalWeight(W)) | 
|  | 7242 | NewCB->setProfWeight(W); | 
|  | 7243 | NewCB->setCallingConv(OldCB->getCallingConv()); | 
|  | 7244 | NewCB->setDebugLoc(OldCB->getDebugLoc()); | 
|  | 7245 | NewCB->takeName(OldCB); | 
|  | 7246 | NewCB->setAttributes(AttributeList::get( | 
|  | 7247 | Ctx, OldCallAttributeList.getFnAttributes(), | 
|  | 7248 | OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes)); | 
|  | 7249 |  | 
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 7250 | CallSitePairs.push_back({OldCB, NewCB}); | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7251 | return true; | 
|  | 7252 | }; | 
|  | 7253 |  | 
|  | 7254 | // Use the CallSiteReplacementCreator to create replacement call sites. | 
| Johannes Doerfert | 368f7ee | 2019-12-30 16:12:36 -0600 | [diff] [blame] | 7255 | bool AllCallSitesKnown; | 
|  | 7256 | bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn, | 
|  | 7257 | true, nullptr, AllCallSitesKnown); | 
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 7258 | (void)Success; | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7259 | assert(Success && "Assumed call site replacement to succeed!"); | 
|  | 7260 |  | 
|  | 7261 | // Rewire the arguments. | 
|  | 7262 | auto OldFnArgIt = OldFn->arg_begin(); | 
|  | 7263 | auto NewFnArgIt = NewFn->arg_begin(); | 
|  | 7264 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); | 
|  | 7265 | ++OldArgNum, ++OldFnArgIt) { | 
|  | 7266 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { | 
|  | 7267 | if (ARI->CalleeRepairCB) | 
|  | 7268 | ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); | 
|  | 7269 | NewFnArgIt += ARI->ReplacementTypes.size(); | 
|  | 7270 | } else { | 
|  | 7271 | NewFnArgIt->takeName(&*OldFnArgIt); | 
|  | 7272 | OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); | 
|  | 7273 | ++NewFnArgIt; | 
|  | 7274 | } | 
|  | 7275 | } | 
|  | 7276 |  | 
|  | 7277 | // Eliminate the instructions *after* we visited all of them. | 
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 7278 | for (auto &CallSitePair : CallSitePairs) { | 
|  | 7279 | CallBase &OldCB = *CallSitePair.first; | 
|  | 7280 | CallBase &NewCB = *CallSitePair.second; | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7281 | // We do not modify the call graph here but simply reanalyze the old | 
|  | 7282 | // function. This should be revisited once the old PM is gone. | 
|  | 7283 | ModifiedFns.insert(OldCB.getFunction()); | 
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 7284 | OldCB.replaceAllUsesWith(&NewCB); | 
|  | 7285 | OldCB.eraseFromParent(); | 
|  | 7286 | } | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7287 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7288 | // Replace the function in the call graph (if any). | 
|  | 7289 | CGUpdater.replaceFunctionWith(*OldFn, *NewFn); | 
|  | 7290 |  | 
|  | 7291 | // If the old function was modified and needed to be reanalyzed, the new one | 
|  | 7292 | // does now. | 
|  | 7293 | if (ModifiedFns.erase(OldFn)) | 
|  | 7294 | ModifiedFns.insert(NewFn); | 
| Johannes Doerfert | d2d2fb1 | 2020-01-02 17:20:47 -0600 | [diff] [blame] | 7295 |  | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 7296 | Changed = ChangeStatus::CHANGED; | 
|  | 7297 | } | 
|  | 7298 |  | 
|  | 7299 | return Changed; | 
|  | 7300 | } | 
|  | 7301 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7302 | void Attributor::initializeInformationCache(Function &F) { | 
|  | 7303 |  | 
|  | 7304 | // Walk all instructions to find interesting instructions that might be | 
|  | 7305 | // queried by abstract attributes during their initialization or update. | 
|  | 7306 | // This has to happen before we create attributes. | 
|  | 7307 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; | 
|  | 7308 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; | 
|  | 7309 |  | 
|  | 7310 | for (Instruction &I : instructions(&F)) { | 
|  | 7311 | bool IsInterestingOpcode = false; | 
|  | 7312 |  | 
|  | 7313 | // To allow easy access to all instructions in a function with a given | 
|  | 7314 | // opcode we store them in the InfoCache. As not all opcodes are interesting | 
|  | 7315 | // to concrete attributes we only cache the ones that are as identified in | 
|  | 7316 | // the following switch. | 
|  | 7317 | // Note: There are no concrete attributes now so this is initially empty. | 
|  | 7318 | switch (I.getOpcode()) { | 
|  | 7319 | default: | 
|  | 7320 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && | 
|  | 7321 | "New call site/base instruction type needs to be known int the " | 
|  | 7322 | "Attributor."); | 
|  | 7323 | break; | 
|  | 7324 | case Instruction::Load: | 
|  | 7325 | // The alignment of a pointer is interesting for loads. | 
|  | 7326 | case Instruction::Store: | 
|  | 7327 | // The alignment of a pointer is interesting for stores. | 
|  | 7328 | case Instruction::Call: | 
|  | 7329 | case Instruction::CallBr: | 
|  | 7330 | case Instruction::Invoke: | 
|  | 7331 | case Instruction::CleanupRet: | 
|  | 7332 | case Instruction::CatchSwitch: | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 7333 | case Instruction::AtomicRMW: | 
|  | 7334 | case Instruction::AtomicCmpXchg: | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 7335 | case Instruction::Br: | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7336 | case Instruction::Resume: | 
|  | 7337 | case Instruction::Ret: | 
|  | 7338 | IsInterestingOpcode = true; | 
|  | 7339 | } | 
|  | 7340 | if (IsInterestingOpcode) | 
|  | 7341 | InstOpcodeMap[I.getOpcode()].push_back(&I); | 
|  | 7342 | if (I.mayReadOrWriteMemory()) | 
|  | 7343 | ReadOrWriteInsts.push_back(&I); | 
|  | 7344 | } | 
|  | 7345 | } | 
|  | 7346 |  | 
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 7347 | void Attributor::recordDependence(const AbstractAttribute &FromAA, | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7348 | const AbstractAttribute &ToAA, | 
|  | 7349 | DepClassTy DepClass) { | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 7350 | if (FromAA.getState().isAtFixpoint()) | 
|  | 7351 | return; | 
|  | 7352 |  | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 7353 | if (DepClass == DepClassTy::REQUIRED) | 
|  | 7354 | QueryMap[&FromAA].RequiredAAs.insert( | 
|  | 7355 | const_cast<AbstractAttribute *>(&ToAA)); | 
|  | 7356 | else | 
|  | 7357 | QueryMap[&FromAA].OptionalAAs.insert( | 
|  | 7358 | const_cast<AbstractAttribute *>(&ToAA)); | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 7359 | QueriedNonFixAA = true; | 
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 7360 | } | 
|  | 7361 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 7362 | void Attributor::identifyDefaultAbstractAttributes(Function &F) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7363 | if (!VisitedFunctions.insert(&F).second) | 
|  | 7364 | return; | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 7365 | if (F.isDeclaration()) | 
|  | 7366 | return; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7367 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7368 | IRPosition FPos = IRPosition::function(F); | 
|  | 7369 |  | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 7370 | // Check for dead BasicBlocks in every function. | 
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 7371 | // We need dead instruction detection because we do not want to deal with | 
|  | 7372 | // broken IR in which SSA rules do not apply. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7373 | getOrCreateAAFor<AAIsDead>(FPos); | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 7374 |  | 
|  | 7375 | // Every function might be "will-return". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7376 | getOrCreateAAFor<AAWillReturn>(FPos); | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 7377 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 7378 | // Every function might contain instructions that cause "undefined behavior". | 
|  | 7379 | getOrCreateAAFor<AAUndefinedBehavior>(FPos); | 
|  | 7380 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 7381 | // Every function can be nounwind. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7382 | getOrCreateAAFor<AANoUnwind>(FPos); | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 7383 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 7384 | // Every function might be marked "nosync" | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7385 | getOrCreateAAFor<AANoSync>(FPos); | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 7386 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 7387 | // Every function might be "no-free". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7388 | getOrCreateAAFor<AANoFree>(FPos); | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 7389 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 7390 | // Every function might be "no-return". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7391 | getOrCreateAAFor<AANoReturn>(FPos); | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 7392 |  | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 7393 | // Every function might be "no-recurse". | 
|  | 7394 | getOrCreateAAFor<AANoRecurse>(FPos); | 
|  | 7395 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 7396 | // Every function might be "readnone/readonly/writeonly/...". | 
|  | 7397 | getOrCreateAAFor<AAMemoryBehavior>(FPos); | 
|  | 7398 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7399 | // Every function might be applicable for Heap-To-Stack conversion. | 
|  | 7400 | if (EnableHeapToStack) | 
|  | 7401 | getOrCreateAAFor<AAHeapToStack>(FPos); | 
|  | 7402 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 7403 | // Return attributes are only appropriate if the return type is non void. | 
|  | 7404 | Type *ReturnType = F.getReturnType(); | 
|  | 7405 | if (!ReturnType->isVoidTy()) { | 
|  | 7406 | // Argument attribute "returned" --- Create only one per function even | 
|  | 7407 | // though it is an argument attribute. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7408 | getOrCreateAAFor<AAReturnedValues>(FPos); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7409 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7410 | IRPosition RetPos = IRPosition::returned(F); | 
|  | 7411 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7412 | // Every returned value might be dead. | 
|  | 7413 | getOrCreateAAFor<AAIsDead>(RetPos); | 
|  | 7414 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7415 | // Every function might be simplified. | 
|  | 7416 | getOrCreateAAFor<AAValueSimplify>(RetPos); | 
|  | 7417 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 7418 | if (ReturnType->isPointerTy()) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7419 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 7420 | // Every function with pointer return type might be marked align. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7421 | getOrCreateAAFor<AAAlign>(RetPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 7422 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 7423 | // Every function with pointer return type might be marked nonnull. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7424 | getOrCreateAAFor<AANonNull>(RetPos); | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 7425 |  | 
|  | 7426 | // Every function with pointer return type might be marked noalias. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7427 | getOrCreateAAFor<AANoAlias>(RetPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7428 |  | 
|  | 7429 | // Every function with pointer return type might be marked | 
|  | 7430 | // dereferenceable. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7431 | getOrCreateAAFor<AADereferenceable>(RetPos); | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 7432 | } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7433 | } | 
|  | 7434 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7435 | for (Argument &Arg : F.args()) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7436 | IRPosition ArgPos = IRPosition::argument(Arg); | 
|  | 7437 |  | 
|  | 7438 | // Every argument might be simplified. | 
|  | 7439 | getOrCreateAAFor<AAValueSimplify>(ArgPos); | 
|  | 7440 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7441 | if (Arg.getType()->isPointerTy()) { | 
|  | 7442 | // Every argument with pointer type might be marked nonnull. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7443 | getOrCreateAAFor<AANonNull>(ArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7444 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 7445 | // Every argument with pointer type might be marked noalias. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7446 | getOrCreateAAFor<AANoAlias>(ArgPos); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 7447 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7448 | // Every argument with pointer type might be marked dereferenceable. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7449 | getOrCreateAAFor<AADereferenceable>(ArgPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 7450 |  | 
|  | 7451 | // Every argument with pointer type might be marked align. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7452 | getOrCreateAAFor<AAAlign>(ArgPos); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 7453 |  | 
|  | 7454 | // Every argument with pointer type might be marked nocapture. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7455 | getOrCreateAAFor<AANoCapture>(ArgPos); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 7456 |  | 
|  | 7457 | // Every argument with pointer type might be marked | 
|  | 7458 | // "readnone/readonly/writeonly/..." | 
|  | 7459 | getOrCreateAAFor<AAMemoryBehavior>(ArgPos); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 7460 |  | 
|  | 7461 | // Every argument with pointer type might be marked nofree. | 
|  | 7462 | getOrCreateAAFor<AANoFree>(ArgPos); | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7463 |  | 
|  | 7464 | // Every argument with pointer type might be privatizable (or promotable) | 
|  | 7465 | getOrCreateAAFor<AAPrivatizablePtr>(ArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7466 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 7467 | } | 
|  | 7468 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7469 | auto CallSitePred = [&](Instruction &I) -> bool { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7470 | CallSite CS(&I); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7471 | if (Function *Callee = CS.getCalledFunction()) { | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 7472 | // Skip declerations except if annotations on their call sites were | 
|  | 7473 | // explicitly requested. | 
| Johannes Doerfert | 139c9ef | 2019-12-13 23:41:02 -0600 | [diff] [blame] | 7474 | if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && | 
|  | 7475 | !Callee->hasMetadata(LLVMContext::MD_callback)) | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 7476 | return true; | 
|  | 7477 |  | 
|  | 7478 | if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7479 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7480 | IRPosition CSRetPos = IRPosition::callsite_returned(CS); | 
|  | 7481 |  | 
|  | 7482 | // Call site return values might be dead. | 
|  | 7483 | getOrCreateAAFor<AAIsDead>(CSRetPos); | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7484 |  | 
|  | 7485 | // Call site return integer values might be limited by a constant range. | 
|  | 7486 | if (Callee->getReturnType()->isIntegerTy()) { | 
|  | 7487 | getOrCreateAAFor<AAValueConstantRange>(CSRetPos); | 
|  | 7488 | } | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7489 | } | 
|  | 7490 |  | 
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 7491 | for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7492 |  | 
|  | 7493 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); | 
|  | 7494 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7495 | // Every call site argument might be dead. | 
|  | 7496 | getOrCreateAAFor<AAIsDead>(CSArgPos); | 
|  | 7497 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7498 | // Call site argument might be simplified. | 
|  | 7499 | getOrCreateAAFor<AAValueSimplify>(CSArgPos); | 
|  | 7500 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7501 | if (!CS.getArgument(i)->getType()->isPointerTy()) | 
|  | 7502 | continue; | 
|  | 7503 |  | 
|  | 7504 | // Call site argument attribute "non-null". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7505 | getOrCreateAAFor<AANonNull>(CSArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7506 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 7507 | // Call site argument attribute "no-alias". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7508 | getOrCreateAAFor<AANoAlias>(CSArgPos); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 7509 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 7510 | // Call site argument attribute "dereferenceable". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7511 | getOrCreateAAFor<AADereferenceable>(CSArgPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 7512 |  | 
|  | 7513 | // Call site argument attribute "align". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 7514 | getOrCreateAAFor<AAAlign>(CSArgPos); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 7515 |  | 
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 7516 | // Call site argument attribute | 
|  | 7517 | // "readnone/readonly/writeonly/..." | 
|  | 7518 | getOrCreateAAFor<AAMemoryBehavior>(CSArgPos); | 
|  | 7519 |  | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 7520 | // Call site argument attribute "nofree". | 
|  | 7521 | getOrCreateAAFor<AANoFree>(CSArgPos); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 7522 | } | 
|  | 7523 | } | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7524 | return true; | 
|  | 7525 | }; | 
|  | 7526 |  | 
|  | 7527 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); | 
|  | 7528 | bool Success, AnyDead = false; | 
|  | 7529 | Success = checkForAllInstructionsImpl( | 
|  | 7530 | OpcodeInstMap, CallSitePred, nullptr, AnyDead, | 
|  | 7531 | {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, | 
|  | 7532 | (unsigned)Instruction::Call}); | 
|  | 7533 | (void)Success; | 
|  | 7534 | assert(Success && !AnyDead && "Expected the check call to be successful!"); | 
|  | 7535 |  | 
|  | 7536 | auto LoadStorePred = [&](Instruction &I) -> bool { | 
|  | 7537 | if (isa<LoadInst>(I)) | 
|  | 7538 | getOrCreateAAFor<AAAlign>( | 
|  | 7539 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); | 
|  | 7540 | else | 
|  | 7541 | getOrCreateAAFor<AAAlign>( | 
|  | 7542 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); | 
|  | 7543 | return true; | 
|  | 7544 | }; | 
|  | 7545 | Success = checkForAllInstructionsImpl( | 
|  | 7546 | OpcodeInstMap, LoadStorePred, nullptr, AnyDead, | 
|  | 7547 | {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); | 
|  | 7548 | (void)Success; | 
|  | 7549 | assert(Success && !AnyDead && "Expected the check call to be successful!"); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7550 | } | 
|  | 7551 |  | 
|  | 7552 | /// Helpers to ease debugging through output streams and print calls. | 
|  | 7553 | /// | 
|  | 7554 | ///{ | 
|  | 7555 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { | 
|  | 7556 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); | 
|  | 7557 | } | 
|  | 7558 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 7559 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7560 | switch (AP) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7561 | case IRPosition::IRP_INVALID: | 
|  | 7562 | return OS << "inv"; | 
|  | 7563 | case IRPosition::IRP_FLOAT: | 
|  | 7564 | return OS << "flt"; | 
|  | 7565 | case IRPosition::IRP_RETURNED: | 
|  | 7566 | return OS << "fn_ret"; | 
|  | 7567 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
|  | 7568 | return OS << "cs_ret"; | 
|  | 7569 | case IRPosition::IRP_FUNCTION: | 
|  | 7570 | return OS << "fn"; | 
|  | 7571 | case IRPosition::IRP_CALL_SITE: | 
|  | 7572 | return OS << "cs"; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 7573 | case IRPosition::IRP_ARGUMENT: | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7574 | return OS << "arg"; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 7575 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7576 | return OS << "cs_arg"; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7577 | } | 
|  | 7578 | llvm_unreachable("Unknown attribute position!"); | 
|  | 7579 | } | 
|  | 7580 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 7581 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 7582 | const Value &AV = Pos.getAssociatedValue(); | 
|  | 7583 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 7584 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; | 
|  | 7585 | } | 
|  | 7586 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 7587 | template <typename base_ty, base_ty BestState, base_ty WorstState> | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7588 | raw_ostream & | 
|  | 7589 | llvm::operator<<(raw_ostream &OS, | 
|  | 7590 | const IntegerStateBase<base_ty, BestState, WorstState> &S) { | 
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 7591 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" | 
|  | 7592 | << static_cast<const AbstractState &>(S); | 
|  | 7593 | } | 
|  | 7594 |  | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7595 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) { | 
|  | 7596 | OS << "range-state(" << S.getBitWidth() << ")<"; | 
|  | 7597 | S.getKnown().print(OS); | 
|  | 7598 | OS << " / "; | 
|  | 7599 | S.getAssumed().print(OS); | 
|  | 7600 | OS << ">"; | 
|  | 7601 |  | 
|  | 7602 | return OS << static_cast<const AbstractState &>(S); | 
|  | 7603 | } | 
|  | 7604 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7605 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { | 
|  | 7606 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); | 
|  | 7607 | } | 
|  | 7608 |  | 
|  | 7609 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { | 
|  | 7610 | AA.print(OS); | 
|  | 7611 | return OS; | 
|  | 7612 | } | 
|  | 7613 |  | 
|  | 7614 | void AbstractAttribute::print(raw_ostream &OS) const { | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 7615 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() | 
|  | 7616 | << "]"; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7617 | } | 
|  | 7618 | ///} | 
|  | 7619 |  | 
|  | 7620 | /// ---------------------------------------------------------------------------- | 
|  | 7621 | ///                       Pass (Manager) Boilerplate | 
|  | 7622 | /// ---------------------------------------------------------------------------- | 
|  | 7623 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7624 | static bool runAttributorOnFunctions(InformationCache &InfoCache, | 
|  | 7625 | SetVector<Function *> &Functions, | 
|  | 7626 | AnalysisGetter &AG, | 
|  | 7627 | CallGraphUpdater &CGUpdater) { | 
|  | 7628 | if (DisableAttributor || Functions.empty()) | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7629 | return false; | 
|  | 7630 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7631 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size() | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7632 | << " functions.\n"); | 
|  | 7633 |  | 
|  | 7634 | // Create an Attributor and initially empty information cache that is filled | 
|  | 7635 | // while we identify default attribute opportunities. | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7636 | Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7637 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7638 | for (Function *F : Functions) | 
|  | 7639 | A.initializeInformationCache(*F); | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 7640 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7641 | for (Function *F : Functions) { | 
|  | 7642 | if (F->hasExactDefinition()) | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 7643 | NumFnWithExactDefinition++; | 
|  | 7644 | else | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7645 | NumFnWithoutExactDefinition++; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7646 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7647 | // 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] | 7648 | // direct call or outside the current set of analyzed functions, we have to | 
|  | 7649 | // do it eagerly. | 
|  | 7650 | if (F->hasLocalLinkage()) { | 
|  | 7651 | if (llvm::all_of(F->uses(), [&Functions](const Use &U) { | 
|  | 7652 | ImmutableCallSite ICS(U.getUser()); | 
|  | 7653 | return ICS && ICS.isCallee(&U) && | 
|  | 7654 | Functions.count(const_cast<Function *>(ICS.getCaller())); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 7655 | })) | 
|  | 7656 | continue; | 
|  | 7657 | } | 
|  | 7658 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7659 | // Populate the Attributor with abstract attribute opportunities in the | 
|  | 7660 | // function and the information cache with IR information. | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7661 | A.identifyDefaultAbstractAttributes(*F); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7662 | } | 
|  | 7663 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7664 | bool Changed = A.run() == ChangeStatus::CHANGED; | 
|  | 7665 | assert(!verifyModule(*Functions.front()->getParent(), &errs()) && | 
|  | 7666 | "Module verification failed!"); | 
| Johannes Doerfert | a4088c7 | 2020-01-07 16:01:57 -0600 | [diff] [blame] | 7667 | return Changed; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7668 | } | 
|  | 7669 |  | 
|  | 7670 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7671 | FunctionAnalysisManager &FAM = | 
|  | 7672 | AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); | 
|  | 7673 | AnalysisGetter AG(FAM); | 
|  | 7674 |  | 
|  | 7675 | SetVector<Function *> Functions; | 
|  | 7676 | for (Function &F : M) | 
|  | 7677 | Functions.insert(&F); | 
|  | 7678 |  | 
|  | 7679 | CallGraphUpdater CGUpdater; | 
|  | 7680 | InformationCache InfoCache(M, AG, /* CGSCC */ nullptr); | 
|  | 7681 | if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) { | 
|  | 7682 | // FIXME: Think about passes we will preserve and add them here. | 
|  | 7683 | return PreservedAnalyses::none(); | 
|  | 7684 | } | 
|  | 7685 | return PreservedAnalyses::all(); | 
|  | 7686 | } | 
|  | 7687 |  | 
|  | 7688 | PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C, | 
|  | 7689 | CGSCCAnalysisManager &AM, | 
|  | 7690 | LazyCallGraph &CG, | 
|  | 7691 | CGSCCUpdateResult &UR) { | 
|  | 7692 | FunctionAnalysisManager &FAM = | 
|  | 7693 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); | 
|  | 7694 | AnalysisGetter AG(FAM); | 
|  | 7695 |  | 
|  | 7696 | SetVector<Function *> Functions; | 
|  | 7697 | for (LazyCallGraph::Node &N : C) | 
|  | 7698 | Functions.insert(&N.getFunction()); | 
|  | 7699 |  | 
|  | 7700 | if (Functions.empty()) | 
|  | 7701 | return PreservedAnalyses::all(); | 
|  | 7702 |  | 
|  | 7703 | Module &M = *Functions.back()->getParent(); | 
|  | 7704 | CallGraphUpdater CGUpdater; | 
|  | 7705 | CGUpdater.initialize(CG, C, AM, UR); | 
|  | 7706 | InformationCache InfoCache(M, AG, /* CGSCC */ &Functions); | 
|  | 7707 | if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7708 | // FIXME: Think about passes we will preserve and add them here. | 
|  | 7709 | return PreservedAnalyses::none(); | 
|  | 7710 | } | 
|  | 7711 | return PreservedAnalyses::all(); | 
|  | 7712 | } | 
|  | 7713 |  | 
|  | 7714 | namespace { | 
|  | 7715 |  | 
|  | 7716 | struct AttributorLegacyPass : public ModulePass { | 
|  | 7717 | static char ID; | 
|  | 7718 |  | 
|  | 7719 | AttributorLegacyPass() : ModulePass(ID) { | 
|  | 7720 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); | 
|  | 7721 | } | 
|  | 7722 |  | 
|  | 7723 | bool runOnModule(Module &M) override { | 
|  | 7724 | if (skipModule(M)) | 
|  | 7725 | return false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7726 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 7727 | AnalysisGetter AG; | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7728 | SetVector<Function *> Functions; | 
|  | 7729 | for (Function &F : M) | 
|  | 7730 | Functions.insert(&F); | 
|  | 7731 |  | 
|  | 7732 | CallGraphUpdater CGUpdater; | 
|  | 7733 | InformationCache InfoCache(M, AG, /* CGSCC */ nullptr); | 
|  | 7734 | return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7735 | } | 
|  | 7736 |  | 
|  | 7737 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 7738 | // FIXME: Think about passes we will preserve and add them here. | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7739 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7740 | } | 
|  | 7741 | }; | 
|  | 7742 |  | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7743 | struct AttributorCGSCCLegacyPass : public CallGraphSCCPass { | 
|  | 7744 | CallGraphUpdater CGUpdater; | 
|  | 7745 | static char ID; | 
|  | 7746 |  | 
|  | 7747 | AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) { | 
|  | 7748 | initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry()); | 
|  | 7749 | } | 
|  | 7750 |  | 
|  | 7751 | bool runOnSCC(CallGraphSCC &SCC) override { | 
|  | 7752 | if (skipSCC(SCC)) | 
|  | 7753 | return false; | 
|  | 7754 |  | 
|  | 7755 | SetVector<Function *> Functions; | 
|  | 7756 | for (CallGraphNode *CGN : SCC) | 
|  | 7757 | if (Function *Fn = CGN->getFunction()) | 
|  | 7758 | if (!Fn->isDeclaration()) | 
|  | 7759 | Functions.insert(Fn); | 
|  | 7760 |  | 
|  | 7761 | if (Functions.empty()) | 
|  | 7762 | return false; | 
|  | 7763 |  | 
|  | 7764 | AnalysisGetter AG; | 
|  | 7765 | CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph()); | 
|  | 7766 | CGUpdater.initialize(CG, SCC); | 
|  | 7767 | Module &M = *Functions.back()->getParent(); | 
|  | 7768 | InformationCache InfoCache(M, AG, /* CGSCC */ &Functions); | 
|  | 7769 | return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater); | 
|  | 7770 | } | 
|  | 7771 |  | 
|  | 7772 | bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); } | 
|  | 7773 |  | 
|  | 7774 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 7775 | // FIXME: Think about passes we will preserve and add them here. | 
|  | 7776 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | 
|  | 7777 | CallGraphSCCPass::getAnalysisUsage(AU); | 
|  | 7778 | } | 
|  | 7779 | }; | 
|  | 7780 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7781 | } // end anonymous namespace | 
|  | 7782 |  | 
|  | 7783 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7784 | Pass *llvm::createAttributorCGSCCLegacyPass() { | 
|  | 7785 | return new AttributorCGSCCLegacyPass(); | 
|  | 7786 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7787 |  | 
|  | 7788 | char AttributorLegacyPass::ID = 0; | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7789 | char AttributorCGSCCLegacyPass::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 7790 |  | 
|  | 7791 | const char AAReturnedValues::ID = 0; | 
|  | 7792 | const char AANoUnwind::ID = 0; | 
|  | 7793 | const char AANoSync::ID = 0; | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 7794 | const char AANoFree::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 7795 | const char AANonNull::ID = 0; | 
|  | 7796 | const char AANoRecurse::ID = 0; | 
|  | 7797 | const char AAWillReturn::ID = 0; | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 7798 | const char AAUndefinedBehavior::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 7799 | const char AANoAlias::ID = 0; | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 7800 | const char AAReachability::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 7801 | const char AANoReturn::ID = 0; | 
|  | 7802 | const char AAIsDead::ID = 0; | 
|  | 7803 | const char AADereferenceable::ID = 0; | 
|  | 7804 | const char AAAlign::ID = 0; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 7805 | const char AANoCapture::ID = 0; | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7806 | const char AAValueSimplify::ID = 0; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7807 | const char AAHeapToStack::ID = 0; | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7808 | const char AAPrivatizablePtr::ID = 0; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 7809 | const char AAMemoryBehavior::ID = 0; | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7810 | const char AAValueConstantRange::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 7811 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7812 | // Macro magic to create the static generator function for attributes that | 
|  | 7813 | // follow the naming scheme. | 
|  | 7814 |  | 
|  | 7815 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \ | 
|  | 7816 | case IRPosition::PK:                                                         \ | 
|  | 7817 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); | 
|  | 7818 |  | 
|  | 7819 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \ | 
|  | 7820 | case IRPosition::PK:                                                         \ | 
|  | 7821 | AA = new CLASS##SUFFIX(IRP);                                               \ | 
|  | 7822 | break; | 
|  | 7823 |  | 
|  | 7824 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \ | 
|  | 7825 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 7826 | CLASS *AA = nullptr;                                                       \ | 
|  | 7827 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 7828 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 7829 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \ | 
|  | 7830 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \ | 
|  | 7831 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 7832 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \ | 
|  | 7833 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \ | 
|  | 7834 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 7835 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 7836 | }                                                                          \ | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7837 | return *AA;                                                                \ | 
|  | 7838 | } | 
|  | 7839 |  | 
|  | 7840 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \ | 
|  | 7841 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 7842 | CLASS *AA = nullptr;                                                       \ | 
|  | 7843 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 7844 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 7845 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \ | 
|  | 7846 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \ | 
|  | 7847 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 7848 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 7849 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \ | 
|  | 7850 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 7851 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 7852 | }                                                                          \ | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7853 | return *AA;                                                                \ | 
|  | 7854 | } | 
|  | 7855 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7856 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \ | 
|  | 7857 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 7858 | CLASS *AA = nullptr;                                                       \ | 
|  | 7859 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 7860 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 7861 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 7862 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 7863 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 7864 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 7865 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \ | 
|  | 7866 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 7867 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 7868 | }                                                                          \ | 
|  | 7869 | return *AA;                                                                \ | 
|  | 7870 | } | 
|  | 7871 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7872 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \ | 
|  | 7873 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 7874 | CLASS *AA = nullptr;                                                       \ | 
|  | 7875 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 7876 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 7877 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \ | 
|  | 7878 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \ | 
|  | 7879 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 7880 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \ | 
|  | 7881 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \ | 
|  | 7882 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \ | 
|  | 7883 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 7884 | }                                                                          \ | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7885 | return *AA;                                                                \ | 
|  | 7886 | } | 
|  | 7887 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 7888 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \ | 
|  | 7889 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 7890 | CLASS *AA = nullptr;                                                       \ | 
|  | 7891 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 7892 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 7893 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 7894 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 7895 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 7896 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 7897 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 7898 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 7899 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 7900 | }                                                                          \ | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 7901 | return *AA;                                                                \ | 
|  | 7902 | } | 
|  | 7903 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7904 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) | 
|  | 7905 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7906 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) | 
|  | 7907 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) | 
|  | 7908 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7909 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) | 
|  | 7910 |  | 
|  | 7911 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) | 
|  | 7912 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) | 
| Johannes Doerfert | 89c2e73 | 2019-10-30 17:20:20 -0500 | [diff] [blame] | 7913 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7914 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) | 
|  | 7915 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 7916 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) | 
| Hideto Ueno | 188f9a3 | 2020-01-15 15:25:52 +0900 | [diff] [blame] | 7917 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7918 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7919 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 7920 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 7921 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7922 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7923 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 7924 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 7925 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7926 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 7927 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) | 
|  | 7928 |  | 
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 7929 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7930 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 7931 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7932 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 7933 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 7934 | #undef SWITCH_PK_CREATE | 
|  | 7935 | #undef SWITCH_PK_INV | 
|  | 7936 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7937 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", | 
|  | 7938 | "Deduce and propagate attributes", false, false) | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 7939 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 7940 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", | 
|  | 7941 | "Deduce and propagate attributes", false, false) | 
| Johannes Doerfert | b0c77c3 | 2019-11-27 00:30:12 -0600 | [diff] [blame] | 7942 | INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc", | 
|  | 7943 | "Deduce and propagate attributes (CGSCC pass)", false, | 
|  | 7944 | false) | 
|  | 7945 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) | 
|  | 7946 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) | 
|  | 7947 | INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc", | 
|  | 7948 | "Deduce and propagate attributes (CGSCC pass)", false, | 
|  | 7949 | false) |