| 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 |  | 
|  | 16 | #include "llvm/Transforms/IPO/Attributor.h" | 
|  | 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" | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/CaptureTracking.h" | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/EHPersonalities.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/GlobalsModRef.h" | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/Loads.h" | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/MemoryBuiltins.h" | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ValueTracking.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Argument.h" | 
|  | 30 | #include "llvm/IR/Attributes.h" | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CFG.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 32 | #include "llvm/IR/InstIterator.h" | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IntrinsicInst.h" | 
| Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 34 | #include "llvm/InitializePasses.h" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" | 
|  | 36 | #include "llvm/Support/Debug.h" | 
|  | 37 | #include "llvm/Support/raw_ostream.h" | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | 
|  | 39 | #include "llvm/Transforms/Utils/Local.h" | 
|  | 40 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 41 | #include <cassert> | 
|  | 42 |  | 
|  | 43 | using namespace llvm; | 
|  | 44 |  | 
|  | 45 | #define DEBUG_TYPE "attributor" | 
|  | 46 |  | 
|  | 47 | STATISTIC(NumFnWithExactDefinition, | 
|  | 48 | "Number of function with exact definitions"); | 
|  | 49 | STATISTIC(NumFnWithoutExactDefinition, | 
|  | 50 | "Number of function without exact definitions"); | 
|  | 51 | STATISTIC(NumAttributesTimedOut, | 
|  | 52 | "Number of abstract attributes timed out before fixpoint"); | 
|  | 53 | STATISTIC(NumAttributesValidFixpoint, | 
|  | 54 | "Number of abstract attributes in a valid fixpoint state"); | 
|  | 55 | STATISTIC(NumAttributesManifested, | 
|  | 56 | "Number of abstract attributes manifested in IR"); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 57 | STATISTIC(NumAttributesFixedDueToRequiredDependences, | 
|  | 58 | "Number of abstract attributes fixed due to required dependences"); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 59 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 60 | // Some helper macros to deal with statistics tracking. | 
|  | 61 | // | 
|  | 62 | // Usage: | 
|  | 63 | // For simple IR attribute tracking overload trackStatistics in the abstract | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 64 | // attribute and choose the right STATS_DECLTRACK_********* macro, | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 65 | // e.g.,: | 
|  | 66 | //  void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 67 | //    STATS_DECLTRACK_ARG_ATTR(returned) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 68 | //  } | 
|  | 69 | // If there is a single "increment" side one can use the macro | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 70 | // STATS_DECLTRACK with a custom message. If there are multiple increment | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 71 | // sides, STATS_DECL and STATS_TRACK can also be used separatly. | 
|  | 72 | // | 
|  | 73 | #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \ | 
|  | 74 | ("Number of " #TYPE " marked '" #NAME "'") | 
|  | 75 | #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME | 
| Johannes Doerfert | a7a3b3a | 2019-09-04 19:01:08 +0000 | [diff] [blame] | 76 | #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG); | 
|  | 77 | #define STATS_DECL(NAME, TYPE, MSG)                                            \ | 
|  | 78 | STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG); | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 79 | #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 80 | #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \ | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 81 | {                                                                            \ | 
|  | 82 | STATS_DECL(NAME, TYPE, MSG)                                                \ | 
|  | 83 | STATS_TRACK(NAME, TYPE)                                                    \ | 
|  | 84 | } | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 85 | #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \ | 
|  | 86 | STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) | 
|  | 87 | #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \ | 
|  | 88 | STATS_DECLTRACK(NAME, CSArguments,                                           \ | 
|  | 89 | BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) | 
|  | 90 | #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \ | 
|  | 91 | STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) | 
|  | 92 | #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \ | 
|  | 93 | STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) | 
|  | 94 | #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \ | 
|  | 95 | STATS_DECLTRACK(NAME, FunctionReturn,                                        \ | 
| Johannes Doerfert | 2db8528 | 2019-08-21 20:56:56 +0000 | [diff] [blame] | 96 | BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 97 | #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \ | 
|  | 98 | STATS_DECLTRACK(NAME, CSReturn,                                              \ | 
|  | 99 | BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) | 
|  | 100 | #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \ | 
|  | 101 | STATS_DECLTRACK(NAME, Floating,                                              \ | 
|  | 102 | ("Number of floating values known to be '" #NAME "'")) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 103 |  | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 104 | // Specialization of the operator<< for abstract attributes subclasses. This | 
|  | 105 | // disambiguates situations where multiple operators are applicable. | 
|  | 106 | namespace llvm { | 
|  | 107 | #define PIPE_OPERATOR(CLASS)                                                   \ | 
|  | 108 | raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) {                  \ | 
|  | 109 | return OS << static_cast<const AbstractAttribute &>(AA);                   \ | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | PIPE_OPERATOR(AAIsDead) | 
|  | 113 | PIPE_OPERATOR(AANoUnwind) | 
|  | 114 | PIPE_OPERATOR(AANoSync) | 
|  | 115 | PIPE_OPERATOR(AANoRecurse) | 
|  | 116 | PIPE_OPERATOR(AAWillReturn) | 
|  | 117 | PIPE_OPERATOR(AANoReturn) | 
|  | 118 | PIPE_OPERATOR(AAReturnedValues) | 
|  | 119 | PIPE_OPERATOR(AANonNull) | 
|  | 120 | PIPE_OPERATOR(AANoAlias) | 
|  | 121 | PIPE_OPERATOR(AADereferenceable) | 
|  | 122 | PIPE_OPERATOR(AAAlign) | 
|  | 123 | PIPE_OPERATOR(AANoCapture) | 
|  | 124 | PIPE_OPERATOR(AAValueSimplify) | 
|  | 125 | PIPE_OPERATOR(AANoFree) | 
|  | 126 | PIPE_OPERATOR(AAHeapToStack) | 
|  | 127 | PIPE_OPERATOR(AAReachability) | 
|  | 128 | PIPE_OPERATOR(AAMemoryBehavior) | 
|  | 129 |  | 
|  | 130 | #undef PIPE_OPERATOR | 
|  | 131 | } // namespace llvm | 
|  | 132 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 133 | // TODO: Determine a good default value. | 
|  | 134 | // | 
|  | 135 | // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads | 
|  | 136 | // (when run with the first 5 abstract attributes). The results also indicate | 
|  | 137 | // that we never reach 32 iterations but always find a fixpoint sooner. | 
|  | 138 | // | 
|  | 139 | // This will become more evolved once we perform two interleaved fixpoint | 
|  | 140 | // iterations: bottom-up and top-down. | 
|  | 141 | static cl::opt<unsigned> | 
|  | 142 | MaxFixpointIterations("attributor-max-iterations", cl::Hidden, | 
|  | 143 | cl::desc("Maximal number of fixpoint iterations."), | 
|  | 144 | cl::init(32)); | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 145 | static cl::opt<bool> VerifyMaxFixpointIterations( | 
|  | 146 | "attributor-max-iterations-verify", cl::Hidden, | 
|  | 147 | cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), | 
|  | 148 | cl::init(false)); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 149 |  | 
|  | 150 | static cl::opt<bool> DisableAttributor( | 
|  | 151 | "attributor-disable", cl::Hidden, | 
|  | 152 | cl::desc("Disable the attributor inter-procedural deduction pass."), | 
| Johannes Doerfert | 282d34e | 2019-06-14 14:53:41 +0000 | [diff] [blame] | 153 | cl::init(true)); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 154 |  | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 155 | static cl::opt<bool> AnnotateDeclarationCallSites( | 
|  | 156 | "attributor-annotate-decl-cs", cl::Hidden, | 
|  | 157 | cl::desc("Annoate call sites of function declarations."), cl::init(false)); | 
|  | 158 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 159 | static cl::opt<bool> ManifestInternal( | 
|  | 160 | "attributor-manifest-internal", cl::Hidden, | 
|  | 161 | cl::desc("Manifest Attributor internal string attributes."), | 
|  | 162 | cl::init(false)); | 
|  | 163 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 164 | static cl::opt<unsigned> DepRecInterval( | 
|  | 165 | "attributor-dependence-recompute-interval", cl::Hidden, | 
|  | 166 | cl::desc("Number of iterations until dependences are recomputed."), | 
|  | 167 | cl::init(4)); | 
|  | 168 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 169 | static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", | 
|  | 170 | cl::init(true), cl::Hidden); | 
|  | 171 |  | 
| Johannes Doerfert | 1c2afae | 2019-10-10 05:34:21 +0000 | [diff] [blame] | 172 | static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), | 
|  | 173 | cl::Hidden); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 174 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 175 | /// Logic operators for the change status enum class. | 
|  | 176 | /// | 
|  | 177 | ///{ | 
|  | 178 | ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { | 
|  | 179 | return l == ChangeStatus::CHANGED ? l : r; | 
|  | 180 | } | 
|  | 181 | ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { | 
|  | 182 | return l == ChangeStatus::UNCHANGED ? l : r; | 
|  | 183 | } | 
|  | 184 | ///} | 
|  | 185 |  | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 186 | Argument *IRPosition::getAssociatedArgument() const { | 
|  | 187 | if (getPositionKind() == IRP_ARGUMENT) | 
|  | 188 | return cast<Argument>(&getAnchorValue()); | 
|  | 189 |  | 
|  | 190 | // Not an Argument and no argument number means this is not a call site | 
|  | 191 | // argument, thus we cannot find a callback argument to return. | 
|  | 192 | int ArgNo = getArgNo(); | 
|  | 193 | if (ArgNo < 0) | 
|  | 194 | return nullptr; | 
|  | 195 |  | 
|  | 196 | // Use abstract call sites to make the connection between the call site | 
|  | 197 | // values and the ones in callbacks. If a callback was found that makes use | 
|  | 198 | // of the underlying call site operand, we want the corresponding callback | 
|  | 199 | // callee argument and not the direct callee argument. | 
|  | 200 | Optional<Argument *> CBCandidateArg; | 
|  | 201 | SmallVector<const Use *, 4> CBUses; | 
|  | 202 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 203 | AbstractCallSite::getCallbackUses(ICS, CBUses); | 
|  | 204 | for (const Use *U : CBUses) { | 
|  | 205 | AbstractCallSite ACS(U); | 
|  | 206 | assert(ACS && ACS.isCallbackCall()); | 
|  | 207 | if (!ACS.getCalledFunction()) | 
|  | 208 | continue; | 
|  | 209 |  | 
|  | 210 | for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) { | 
|  | 211 |  | 
|  | 212 | // Test if the underlying call site operand is argument number u of the | 
|  | 213 | // callback callee. | 
|  | 214 | if (ACS.getCallArgOperandNo(u) != ArgNo) | 
|  | 215 | continue; | 
|  | 216 |  | 
|  | 217 | assert(ACS.getCalledFunction()->arg_size() > u && | 
|  | 218 | "ACS mapped into var-args arguments!"); | 
|  | 219 | if (CBCandidateArg.hasValue()) { | 
|  | 220 | CBCandidateArg = nullptr; | 
|  | 221 | break; | 
|  | 222 | } | 
|  | 223 | CBCandidateArg = ACS.getCalledFunction()->getArg(u); | 
|  | 224 | } | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | // If we found a unique callback candidate argument, return it. | 
|  | 228 | if (CBCandidateArg.hasValue() && CBCandidateArg.getValue()) | 
|  | 229 | return CBCandidateArg.getValue(); | 
|  | 230 |  | 
|  | 231 | // If no callbacks were found, or none used the underlying call site operand | 
|  | 232 | // exclusively, use the direct callee argument if available. | 
|  | 233 | const Function *Callee = ICS.getCalledFunction(); | 
|  | 234 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) | 
|  | 235 | return Callee->getArg(ArgNo); | 
|  | 236 |  | 
|  | 237 | return nullptr; | 
|  | 238 | } | 
|  | 239 |  | 
| Johannes Doerfert | 5d34602 | 2019-12-13 22:11:42 -0600 | [diff] [blame] | 240 | /// For calls (and invokes) we will only replace instruction uses to not disturb | 
|  | 241 | /// the old style call graph. | 
|  | 242 | /// TODO: Remove this once we get rid of the old PM. | 
|  | 243 | static void replaceAllInstructionUsesWith(Value &Old, Value &New) { | 
|  | 244 | if (!isa<CallBase>(Old)) | 
|  | 245 | return Old.replaceAllUsesWith(&New); | 
|  | 246 | SmallVector<Use *, 8> Uses; | 
|  | 247 | for (Use &U : Old.uses()) | 
|  | 248 | if (isa<Instruction>(U.getUser())) | 
|  | 249 | Uses.push_back(&U); | 
|  | 250 | for (Use *U : Uses) | 
|  | 251 | U->set(&New); | 
|  | 252 | } | 
|  | 253 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 254 | /// 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] | 255 | /// will be done by looking through cast instructions, selects, phis, and calls | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 256 | /// with the "returned" attribute. Once we cannot look through the value any | 
|  | 257 | /// further, the callback \p VisitValueCB is invoked and passed the current | 
|  | 258 | /// value, the \p State, and a flag to indicate if we stripped anything. To | 
|  | 259 | /// limit how much effort is invested, we will never visit more values than | 
|  | 260 | /// specified by \p MaxValues. | 
|  | 261 | template <typename AAType, typename StateTy> | 
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 262 | static bool genericValueTraversal( | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 263 | Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 264 | const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 265 | int MaxValues = 8) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 266 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 267 | const AAIsDead *LivenessAA = nullptr; | 
|  | 268 | if (IRP.getAnchorScope()) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 269 | LivenessAA = &A.getAAFor<AAIsDead>( | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 270 | QueryingAA, IRPosition::function(*IRP.getAnchorScope()), | 
|  | 271 | /* TrackDependence */ false); | 
|  | 272 | bool AnyDead = false; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 273 |  | 
|  | 274 | // TODO: Use Positions here to allow context sensitivity in VisitValueCB | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 275 | SmallPtrSet<Value *, 16> Visited; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 276 | SmallVector<Value *, 16> Worklist; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 277 | Worklist.push_back(&IRP.getAssociatedValue()); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 278 |  | 
|  | 279 | int Iteration = 0; | 
|  | 280 | do { | 
|  | 281 | Value *V = Worklist.pop_back_val(); | 
|  | 282 |  | 
|  | 283 | // Check if we should process the current value. To prevent endless | 
|  | 284 | // recursion keep a record of the values we followed! | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 285 | if (!Visited.insert(V).second) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 286 | continue; | 
|  | 287 |  | 
|  | 288 | // Make sure we limit the compile time for complex expressions. | 
|  | 289 | if (Iteration++ >= MaxValues) | 
|  | 290 | return false; | 
|  | 291 |  | 
|  | 292 | // Explicitly look through calls with a "returned" attribute if we do | 
|  | 293 | // not have a pointer as stripPointerCasts only works on them. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 294 | Value *NewV = nullptr; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 295 | if (V->getType()->isPointerTy()) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 296 | NewV = V->stripPointerCasts(); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 297 | } else { | 
|  | 298 | CallSite CS(V); | 
|  | 299 | if (CS && CS.getCalledFunction()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 300 | for (Argument &Arg : CS.getCalledFunction()->args()) | 
|  | 301 | if (Arg.hasReturnedAttr()) { | 
|  | 302 | NewV = CS.getArgOperand(Arg.getArgNo()); | 
|  | 303 | break; | 
|  | 304 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 305 | } | 
|  | 306 | } | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 307 | if (NewV && NewV != V) { | 
|  | 308 | Worklist.push_back(NewV); | 
|  | 309 | continue; | 
|  | 310 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 311 |  | 
|  | 312 | // Look through select instructions, visit both potential values. | 
|  | 313 | if (auto *SI = dyn_cast<SelectInst>(V)) { | 
|  | 314 | Worklist.push_back(SI->getTrueValue()); | 
|  | 315 | Worklist.push_back(SI->getFalseValue()); | 
|  | 316 | continue; | 
|  | 317 | } | 
|  | 318 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 319 | // Look through phi nodes, visit all live operands. | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 320 | if (auto *PHI = dyn_cast<PHINode>(V)) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 321 | assert(LivenessAA && | 
|  | 322 | "Expected liveness in the presence of instructions!"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 323 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { | 
|  | 324 | const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 325 | if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 326 | AnyDead = true; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 327 | continue; | 
|  | 328 | } | 
|  | 329 | Worklist.push_back(PHI->getIncomingValue(u)); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 330 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 331 | continue; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | // Once a leaf is reached we inform the user through the callback. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 335 | if (!VisitValueCB(*V, State, Iteration > 1)) | 
|  | 336 | return false; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 337 | } while (!Worklist.empty()); | 
|  | 338 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 339 | // If we actually used liveness information so we have to record a dependence. | 
|  | 340 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 341 | A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 342 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 343 | // All values have been visited. | 
|  | 344 | return true; | 
|  | 345 | } | 
|  | 346 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 347 | /// Return true if \p New is equal or worse than \p Old. | 
|  | 348 | static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { | 
|  | 349 | if (!Old.isIntAttribute()) | 
|  | 350 | return true; | 
|  | 351 |  | 
|  | 352 | return Old.getValueAsInt() >= New.getValueAsInt(); | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | /// Return true if the information provided by \p Attr was added to the | 
|  | 356 | /// 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] | 357 | /// 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] | 358 | static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 359 | AttributeList &Attrs, int AttrIdx) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 360 |  | 
|  | 361 | if (Attr.isEnumAttribute()) { | 
|  | 362 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); | 
|  | 363 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 364 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 365 | return false; | 
|  | 366 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 367 | return true; | 
|  | 368 | } | 
|  | 369 | if (Attr.isStringAttribute()) { | 
|  | 370 | StringRef Kind = Attr.getKindAsString(); | 
|  | 371 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 372 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 373 | return false; | 
|  | 374 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 375 | return true; | 
|  | 376 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 377 | if (Attr.isIntAttribute()) { | 
|  | 378 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); | 
|  | 379 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 380 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 381 | return false; | 
|  | 382 | Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); | 
|  | 383 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 384 | return true; | 
|  | 385 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 386 |  | 
|  | 387 | llvm_unreachable("Expected enum or string attribute!"); | 
|  | 388 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 389 |  | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 390 | static const Value * | 
|  | 391 | getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset, | 
|  | 392 | const DataLayout &DL, | 
|  | 393 | bool AllowNonInbounds = false) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 394 | const Value *Ptr = | 
|  | 395 | Attributor::getPointerOperand(I, /* AllowVolatile */ false); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 396 | if (!Ptr) | 
|  | 397 | return nullptr; | 
|  | 398 |  | 
|  | 399 | return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 400 | AllowNonInbounds); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 401 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 402 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 403 | ChangeStatus AbstractAttribute::update(Attributor &A) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 404 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
|  | 405 | if (getState().isAtFixpoint()) | 
|  | 406 | return HasChanged; | 
|  | 407 |  | 
|  | 408 | LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); | 
|  | 409 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 410 | HasChanged = updateImpl(A); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 411 |  | 
|  | 412 | LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this | 
|  | 413 | << "\n"); | 
|  | 414 |  | 
|  | 415 | return HasChanged; | 
|  | 416 | } | 
|  | 417 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 418 | ChangeStatus | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 419 | IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 420 | const ArrayRef<Attribute> &DeducedAttrs) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 421 | Function *ScopeFn = IRP.getAssociatedFunction(); | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 422 | IRPosition::Kind PK = IRP.getPositionKind(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 423 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 424 | // In the following some generic code that will manifest attributes in | 
|  | 425 | // DeducedAttrs if they improve the current IR. Due to the different | 
|  | 426 | // annotation positions we use the underlying AttributeList interface. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 427 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 428 | AttributeList Attrs; | 
|  | 429 | switch (PK) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 430 | case IRPosition::IRP_INVALID: | 
|  | 431 | case IRPosition::IRP_FLOAT: | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 432 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 433 | case IRPosition::IRP_ARGUMENT: | 
|  | 434 | case IRPosition::IRP_FUNCTION: | 
|  | 435 | case IRPosition::IRP_RETURNED: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 436 | Attrs = ScopeFn->getAttributes(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 437 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 438 | case IRPosition::IRP_CALL_SITE: | 
|  | 439 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 440 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 441 | Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 442 | break; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 443 | } | 
|  | 444 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 445 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 446 | LLVMContext &Ctx = IRP.getAnchorValue().getContext(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 447 | for (const Attribute &Attr : DeducedAttrs) { | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 448 | if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 449 | continue; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 450 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 451 | HasChanged = ChangeStatus::CHANGED; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 452 | } | 
|  | 453 |  | 
|  | 454 | if (HasChanged == ChangeStatus::UNCHANGED) | 
|  | 455 | return HasChanged; | 
|  | 456 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 457 | switch (PK) { | 
|  | 458 | case IRPosition::IRP_ARGUMENT: | 
|  | 459 | case IRPosition::IRP_FUNCTION: | 
|  | 460 | case IRPosition::IRP_RETURNED: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 461 | ScopeFn->setAttributes(Attrs); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 462 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 463 | case IRPosition::IRP_CALL_SITE: | 
|  | 464 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 465 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 466 | CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); | 
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 467 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 468 | case IRPosition::IRP_INVALID: | 
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 469 | case IRPosition::IRP_FLOAT: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 470 | break; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 471 | } | 
|  | 472 |  | 
|  | 473 | return HasChanged; | 
|  | 474 | } | 
|  | 475 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 476 | const IRPosition IRPosition::EmptyKey(255); | 
|  | 477 | const IRPosition IRPosition::TombstoneKey(256); | 
|  | 478 |  | 
|  | 479 | SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { | 
|  | 480 | IRPositions.emplace_back(IRP); | 
|  | 481 |  | 
|  | 482 | ImmutableCallSite ICS(&IRP.getAnchorValue()); | 
|  | 483 | switch (IRP.getPositionKind()) { | 
|  | 484 | case IRPosition::IRP_INVALID: | 
|  | 485 | case IRPosition::IRP_FLOAT: | 
|  | 486 | case IRPosition::IRP_FUNCTION: | 
|  | 487 | return; | 
|  | 488 | case IRPosition::IRP_ARGUMENT: | 
|  | 489 | case IRPosition::IRP_RETURNED: | 
|  | 490 | IRPositions.emplace_back( | 
|  | 491 | IRPosition::function(*IRP.getAssociatedFunction())); | 
|  | 492 | return; | 
|  | 493 | case IRPosition::IRP_CALL_SITE: | 
|  | 494 | assert(ICS && "Expected call site!"); | 
|  | 495 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 496 | //       in CallBase. | 
|  | 497 | if (!ICS.hasOperandBundles()) | 
|  | 498 | if (const Function *Callee = ICS.getCalledFunction()) | 
|  | 499 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 500 | return; | 
|  | 501 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
|  | 502 | assert(ICS && "Expected call site!"); | 
|  | 503 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 504 | //       in CallBase. | 
|  | 505 | if (!ICS.hasOperandBundles()) { | 
|  | 506 | if (const Function *Callee = ICS.getCalledFunction()) { | 
|  | 507 | IRPositions.emplace_back(IRPosition::returned(*Callee)); | 
|  | 508 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 509 | } | 
|  | 510 | } | 
|  | 511 | IRPositions.emplace_back( | 
|  | 512 | IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); | 
|  | 513 | return; | 
|  | 514 | case IRPosition::IRP_CALL_SITE_ARGUMENT: { | 
|  | 515 | int ArgNo = IRP.getArgNo(); | 
|  | 516 | assert(ICS && ArgNo >= 0 && "Expected call site!"); | 
|  | 517 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 518 | //       in CallBase. | 
|  | 519 | if (!ICS.hasOperandBundles()) { | 
|  | 520 | const Function *Callee = ICS.getCalledFunction(); | 
|  | 521 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) | 
|  | 522 | IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); | 
|  | 523 | if (Callee) | 
|  | 524 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 525 | } | 
|  | 526 | IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); | 
|  | 527 | return; | 
|  | 528 | } | 
|  | 529 | } | 
|  | 530 | } | 
|  | 531 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 532 | bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, | 
|  | 533 | bool IgnoreSubsumingPositions) const { | 
|  | 534 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 535 | for (Attribute::AttrKind AK : AKs) | 
|  | 536 | if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) | 
|  | 537 | return true; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 538 | // The first position returned by the SubsumingPositionIterator is | 
|  | 539 | // always the position itself. If we ignore subsuming positions we | 
|  | 540 | // are done after the first iteration. | 
|  | 541 | if (IgnoreSubsumingPositions) | 
|  | 542 | break; | 
|  | 543 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 544 | return false; | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 548 | SmallVectorImpl<Attribute> &Attrs, | 
|  | 549 | bool IgnoreSubsumingPositions) const { | 
|  | 550 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 551 | for (Attribute::AttrKind AK : AKs) { | 
|  | 552 | const Attribute &Attr = EquivIRP.getAttr(AK); | 
|  | 553 | if (Attr.getKindAsEnum() == AK) | 
|  | 554 | Attrs.push_back(Attr); | 
|  | 555 | } | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 556 | // The first position returned by the SubsumingPositionIterator is | 
|  | 557 | // always the position itself. If we ignore subsuming positions we | 
|  | 558 | // are done after the first iteration. | 
|  | 559 | if (IgnoreSubsumingPositions) | 
|  | 560 | break; | 
|  | 561 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 562 | } | 
|  | 563 |  | 
|  | 564 | void IRPosition::verify() { | 
|  | 565 | switch (KindOrArgNo) { | 
|  | 566 | default: | 
|  | 567 | assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); | 
|  | 568 | assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && | 
|  | 569 | "Expected call base or argument for positive attribute index!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 570 | if (isa<Argument>(AnchorVal)) { | 
|  | 571 | assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 572 | "Argument number mismatch!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 573 | assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && | 
|  | 574 | "Associated value mismatch!"); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 575 | } else { | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 576 | assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 577 | "Call site argument number mismatch!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 578 | assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == | 
|  | 579 | &getAssociatedValue() && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 580 | "Associated value mismatch!"); | 
|  | 581 | } | 
|  | 582 | break; | 
|  | 583 | case IRP_INVALID: | 
|  | 584 | assert(!AnchorVal && "Expected no value for an invalid position!"); | 
|  | 585 | break; | 
|  | 586 | case IRP_FLOAT: | 
|  | 587 | assert((!isa<CallBase>(&getAssociatedValue()) && | 
|  | 588 | !isa<Argument>(&getAssociatedValue())) && | 
|  | 589 | "Expected specialized kind for call base and argument values!"); | 
|  | 590 | break; | 
|  | 591 | case IRP_RETURNED: | 
|  | 592 | assert(isa<Function>(AnchorVal) && | 
|  | 593 | "Expected function for a 'returned' position!"); | 
|  | 594 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 595 | break; | 
|  | 596 | case IRP_CALL_SITE_RETURNED: | 
|  | 597 | assert((isa<CallBase>(AnchorVal)) && | 
|  | 598 | "Expected call base for 'call site returned' position!"); | 
|  | 599 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 600 | break; | 
|  | 601 | case IRP_CALL_SITE: | 
|  | 602 | assert((isa<CallBase>(AnchorVal)) && | 
|  | 603 | "Expected call base for 'call site function' position!"); | 
|  | 604 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 605 | break; | 
|  | 606 | case IRP_FUNCTION: | 
|  | 607 | assert(isa<Function>(AnchorVal) && | 
|  | 608 | "Expected function for a 'function' position!"); | 
|  | 609 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 610 | break; | 
|  | 611 | } | 
|  | 612 | } | 
|  | 613 |  | 
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 614 | namespace { | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 615 | /// 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] | 616 | /// information in \p R and indicate/return if \p S did change (as-in update is | 
|  | 617 | /// required to be run again). | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 618 | template <typename StateType> | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 619 | ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 620 | auto Assumed = S.getAssumed(); | 
|  | 621 | S ^= R; | 
|  | 622 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | 
|  | 623 | : ChangeStatus::CHANGED; | 
|  | 624 | } | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 625 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 626 | /// Clamp the information known for all returned values of a function | 
|  | 627 | /// (identified by \p QueryingAA) into \p S. | 
|  | 628 | template <typename AAType, typename StateType = typename AAType::StateType> | 
|  | 629 | static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, | 
|  | 630 | StateType &S) { | 
|  | 631 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 632 | << QueryingAA << " into " << S << "\n"); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 633 |  | 
|  | 634 | assert((QueryingAA.getIRPosition().getPositionKind() == | 
|  | 635 | IRPosition::IRP_RETURNED || | 
|  | 636 | QueryingAA.getIRPosition().getPositionKind() == | 
|  | 637 | IRPosition::IRP_CALL_SITE_RETURNED) && | 
|  | 638 | "Can only clamp returned value states for a function returned or call " | 
|  | 639 | "site returned position!"); | 
|  | 640 |  | 
|  | 641 | // Use an optional state as there might not be any return values and we want | 
|  | 642 | // to join (IntegerState::operator&) the state of all there are. | 
|  | 643 | Optional<StateType> T; | 
|  | 644 |  | 
|  | 645 | // Callback for each possibly returned value. | 
|  | 646 | auto CheckReturnValue = [&](Value &RV) -> bool { | 
|  | 647 | const IRPosition &RVPos = IRPosition::value(RV); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 648 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); | 
|  | 649 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() | 
|  | 650 | << " @ " << RVPos << "\n"); | 
|  | 651 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 652 | if (T.hasValue()) | 
|  | 653 | *T &= AAS; | 
|  | 654 | else | 
|  | 655 | T = AAS; | 
|  | 656 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T | 
|  | 657 | << "\n"); | 
|  | 658 | return T->isValidState(); | 
|  | 659 | }; | 
|  | 660 |  | 
|  | 661 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) | 
|  | 662 | S.indicatePessimisticFixpoint(); | 
|  | 663 | else if (T.hasValue()) | 
|  | 664 | S ^= *T; | 
|  | 665 | } | 
|  | 666 |  | 
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 667 | /// Helper class to compose two generic deduction | 
|  | 668 | template <typename AAType, typename Base, typename StateType, | 
|  | 669 | template <typename...> class F, template <typename...> class G> | 
|  | 670 | struct AAComposeTwoGenericDeduction | 
|  | 671 | : public F<AAType, G<AAType, Base, StateType>, StateType> { | 
|  | 672 | AAComposeTwoGenericDeduction(const IRPosition &IRP) | 
|  | 673 | : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} | 
|  | 674 |  | 
|  | 675 | /// See AbstractAttribute::updateImpl(...). | 
|  | 676 | ChangeStatus updateImpl(Attributor &A) override { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 677 | ChangeStatus ChangedF = | 
|  | 678 | F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 679 | ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); | 
|  | 680 | return ChangedF | ChangedG; | 
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 681 | } | 
|  | 682 | }; | 
|  | 683 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 684 | /// Helper class for generic deduction: return value -> returned position. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 685 | template <typename AAType, typename Base, | 
|  | 686 | typename StateType = typename AAType::StateType> | 
|  | 687 | struct AAReturnedFromReturnedValues : public Base { | 
|  | 688 | AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 689 |  | 
|  | 690 | /// See AbstractAttribute::updateImpl(...). | 
|  | 691 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 692 | StateType S; | 
|  | 693 | clampReturnedValueStates<AAType, StateType>(A, *this, S); | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 694 | // TODO: If we know we visited all returned values, thus no are assumed | 
|  | 695 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 696 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | 
|  | 697 | } | 
|  | 698 | }; | 
|  | 699 |  | 
|  | 700 | /// Clamp the information known at all call sites for a given argument | 
|  | 701 | /// (identified by \p QueryingAA) into \p S. | 
|  | 702 | template <typename AAType, typename StateType = typename AAType::StateType> | 
|  | 703 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, | 
|  | 704 | StateType &S) { | 
|  | 705 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 706 | << QueryingAA << " into " << S << "\n"); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 707 |  | 
|  | 708 | assert(QueryingAA.getIRPosition().getPositionKind() == | 
|  | 709 | IRPosition::IRP_ARGUMENT && | 
|  | 710 | "Can only clamp call site argument states for an argument position!"); | 
|  | 711 |  | 
|  | 712 | // Use an optional state as there might not be any return values and we want | 
|  | 713 | // to join (IntegerState::operator&) the state of all there are. | 
|  | 714 | Optional<StateType> T; | 
|  | 715 |  | 
|  | 716 | // The argument number which is also the call site argument number. | 
|  | 717 | unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); | 
|  | 718 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 719 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | 
|  | 720 | const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); | 
|  | 721 | // Check if a coresponding argument was found or if it is on not associated | 
|  | 722 | // (which can happen for callback calls). | 
|  | 723 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | 
|  | 724 | return false; | 
|  | 725 |  | 
|  | 726 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); | 
|  | 727 | LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() | 
|  | 728 | << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 729 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 730 | if (T.hasValue()) | 
|  | 731 | *T &= AAS; | 
|  | 732 | else | 
|  | 733 | T = AAS; | 
|  | 734 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T | 
|  | 735 | << "\n"); | 
|  | 736 | return T->isValidState(); | 
|  | 737 | }; | 
|  | 738 |  | 
|  | 739 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true)) | 
|  | 740 | S.indicatePessimisticFixpoint(); | 
|  | 741 | else if (T.hasValue()) | 
|  | 742 | S ^= *T; | 
|  | 743 | } | 
|  | 744 |  | 
|  | 745 | /// Helper class for generic deduction: call site argument -> argument position. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 746 | template <typename AAType, typename Base, | 
|  | 747 | typename StateType = typename AAType::StateType> | 
|  | 748 | struct AAArgumentFromCallSiteArguments : public Base { | 
|  | 749 | AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 750 |  | 
|  | 751 | /// See AbstractAttribute::updateImpl(...). | 
|  | 752 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 753 | StateType S; | 
|  | 754 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 755 | // TODO: If we know we visited all incoming values, thus no are assumed | 
|  | 756 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 757 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | 
|  | 758 | } | 
|  | 759 | }; | 
|  | 760 |  | 
|  | 761 | /// Helper class for generic replication: function returned -> cs returned. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 762 | template <typename AAType, typename Base, | 
|  | 763 | typename StateType = typename AAType::StateType> | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 764 | struct AACallSiteReturnedFromReturned : public Base { | 
|  | 765 | AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 766 |  | 
|  | 767 | /// See AbstractAttribute::updateImpl(...). | 
|  | 768 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 769 | assert(this->getIRPosition().getPositionKind() == | 
|  | 770 | IRPosition::IRP_CALL_SITE_RETURNED && | 
|  | 771 | "Can only wrap function returned positions for call site returned " | 
|  | 772 | "positions!"); | 
|  | 773 | auto &S = this->getState(); | 
|  | 774 |  | 
|  | 775 | const Function *AssociatedFunction = | 
|  | 776 | this->getIRPosition().getAssociatedFunction(); | 
|  | 777 | if (!AssociatedFunction) | 
|  | 778 | return S.indicatePessimisticFixpoint(); | 
|  | 779 |  | 
|  | 780 | IRPosition FnPos = IRPosition::returned(*AssociatedFunction); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 781 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 782 | return clampStateAndIndicateChange( | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 783 | S, static_cast<const typename AAType::StateType &>(AA.getState())); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 784 | } | 
|  | 785 | }; | 
|  | 786 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 787 | /// Helper class for generic deduction using must-be-executed-context | 
|  | 788 | /// Base class is required to have `followUse` method. | 
|  | 789 |  | 
|  | 790 | /// bool followUse(Attributor &A, const Use *U, const Instruction *I) | 
| Simon Pilgrim | f7aee61 | 2019-10-10 15:25:16 +0000 | [diff] [blame] | 791 | /// U - Underlying use. | 
|  | 792 | /// I - The user of the \p U. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 793 | /// `followUse` returns true if the value should be tracked transitively. | 
|  | 794 |  | 
|  | 795 | template <typename AAType, typename Base, | 
|  | 796 | typename StateType = typename AAType::StateType> | 
|  | 797 | struct AAFromMustBeExecutedContext : public Base { | 
|  | 798 | AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} | 
|  | 799 |  | 
|  | 800 | void initialize(Attributor &A) override { | 
|  | 801 | Base::initialize(A); | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 802 | const IRPosition &IRP = this->getIRPosition(); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 803 | Instruction *CtxI = IRP.getCtxI(); | 
|  | 804 |  | 
|  | 805 | if (!CtxI) | 
|  | 806 | return; | 
|  | 807 |  | 
|  | 808 | for (const Use &U : IRP.getAssociatedValue().uses()) | 
|  | 809 | Uses.insert(&U); | 
|  | 810 | } | 
|  | 811 |  | 
|  | 812 | /// See AbstractAttribute::updateImpl(...). | 
|  | 813 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 814 | auto BeforeState = this->getState(); | 
|  | 815 | auto &S = this->getState(); | 
|  | 816 | Instruction *CtxI = this->getIRPosition().getCtxI(); | 
|  | 817 | if (!CtxI) | 
|  | 818 | return ChangeStatus::UNCHANGED; | 
|  | 819 |  | 
|  | 820 | MustBeExecutedContextExplorer &Explorer = | 
|  | 821 | A.getInfoCache().getMustBeExecutedContextExplorer(); | 
|  | 822 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 823 | auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 824 | for (unsigned u = 0; u < Uses.size(); ++u) { | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 825 | const Use *U = Uses[u]; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 826 | if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 827 | bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 828 | if (Found && Base::followUse(A, U, UserI)) | 
|  | 829 | for (const Use &Us : UserI->uses()) | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 830 | Uses.insert(&Us); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 831 | } | 
|  | 832 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 833 |  | 
|  | 834 | return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; | 
|  | 835 | } | 
|  | 836 |  | 
|  | 837 | private: | 
|  | 838 | /// Container for (transitive) uses of the associated value. | 
|  | 839 | SetVector<const Use *> Uses; | 
|  | 840 | }; | 
|  | 841 |  | 
|  | 842 | template <typename AAType, typename Base, | 
|  | 843 | typename StateType = typename AAType::StateType> | 
|  | 844 | using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = | 
|  | 845 | AAComposeTwoGenericDeduction<AAType, Base, StateType, | 
|  | 846 | AAFromMustBeExecutedContext, | 
|  | 847 | AAArgumentFromCallSiteArguments>; | 
|  | 848 |  | 
|  | 849 | template <typename AAType, typename Base, | 
|  | 850 | typename StateType = typename AAType::StateType> | 
|  | 851 | using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = | 
|  | 852 | AAComposeTwoGenericDeduction<AAType, Base, StateType, | 
|  | 853 | AAFromMustBeExecutedContext, | 
|  | 854 | AACallSiteReturnedFromReturned>; | 
|  | 855 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 856 | /// -----------------------NoUnwind Function Attribute-------------------------- | 
|  | 857 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 858 | struct AANoUnwindImpl : AANoUnwind { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 859 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 860 |  | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 861 | const std::string getAsStr() const override { | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 862 | return getAssumed() ? "nounwind" : "may-unwind"; | 
|  | 863 | } | 
|  | 864 |  | 
|  | 865 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 866 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 867 | auto Opcodes = { | 
|  | 868 | (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr, | 
|  | 869 | (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet, | 
|  | 870 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; | 
|  | 871 |  | 
|  | 872 | auto CheckForNoUnwind = [&](Instruction &I) { | 
|  | 873 | if (!I.mayThrow()) | 
|  | 874 | return true; | 
|  | 875 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 876 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 877 | const auto &NoUnwindAA = | 
|  | 878 | A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); | 
|  | 879 | return NoUnwindAA.isAssumedNoUnwind(); | 
|  | 880 | } | 
|  | 881 | return false; | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 882 | }; | 
|  | 883 |  | 
|  | 884 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) | 
|  | 885 | return indicatePessimisticFixpoint(); | 
|  | 886 |  | 
|  | 887 | return ChangeStatus::UNCHANGED; | 
|  | 888 | } | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 889 | }; | 
|  | 890 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 891 | struct AANoUnwindFunction final : public AANoUnwindImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 892 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 893 |  | 
|  | 894 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 895 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 896 | }; | 
|  | 897 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 898 | /// NoUnwind attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 899 | struct AANoUnwindCallSite final : AANoUnwindImpl { | 
|  | 900 | AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} | 
|  | 901 |  | 
|  | 902 | /// See AbstractAttribute::initialize(...). | 
|  | 903 | void initialize(Attributor &A) override { | 
|  | 904 | AANoUnwindImpl::initialize(A); | 
|  | 905 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 906 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 907 | indicatePessimisticFixpoint(); | 
|  | 908 | } | 
|  | 909 |  | 
|  | 910 | /// See AbstractAttribute::updateImpl(...). | 
|  | 911 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 912 | // TODO: Once we have call site specific value information we can provide | 
|  | 913 | //       call site specific liveness information and then it makes | 
|  | 914 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 915 | //       redirecting requests to the callee argument. | 
|  | 916 | Function *F = getAssociatedFunction(); | 
|  | 917 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 918 | auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); | 
|  | 919 | return clampStateAndIndicateChange( | 
|  | 920 | getState(), | 
|  | 921 | static_cast<const AANoUnwind::StateType &>(FnAA.getState())); | 
|  | 922 | } | 
|  | 923 |  | 
|  | 924 | /// See AbstractAttribute::trackStatistics() | 
|  | 925 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } | 
|  | 926 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 927 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 928 | /// --------------------- Function Return Values ------------------------------- | 
|  | 929 |  | 
|  | 930 | /// "Attribute" that collects all potential returned values and the return | 
|  | 931 | /// instructions that they arise from. | 
|  | 932 | /// | 
|  | 933 | /// If there is a unique returned value R, the manifest method will: | 
|  | 934 | ///   - mark R with the "returned" attribute, if R is an argument. | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 935 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 936 |  | 
|  | 937 | /// Mapping of values potentially returned by the associated function to the | 
|  | 938 | /// return instructions that might return them. | 
| Johannes Doerfert | a4a308c | 2019-08-26 17:51:23 +0000 | [diff] [blame] | 939 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 940 |  | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 941 | /// Mapping to remember the number of returned values for a call site such | 
|  | 942 | /// that we can avoid updates if nothing changed. | 
|  | 943 | DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; | 
|  | 944 |  | 
|  | 945 | /// Set of unresolved calls returned by the associated function. | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 946 | SmallSetVector<CallBase *, 4> UnresolvedCalls; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 947 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 948 | /// State flags | 
|  | 949 | /// | 
|  | 950 | ///{ | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 951 | bool IsFixed = false; | 
|  | 952 | bool IsValidState = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 953 | ///} | 
|  | 954 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 955 | public: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 956 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 957 |  | 
|  | 958 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 959 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 960 | // Reset the state. | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 961 | IsFixed = false; | 
|  | 962 | IsValidState = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 963 | ReturnedValues.clear(); | 
|  | 964 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 965 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 966 | if (!F) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 967 | indicatePessimisticFixpoint(); | 
|  | 968 | return; | 
|  | 969 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 970 |  | 
|  | 971 | // The map from instruction opcodes to those instructions in the function. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 972 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 973 |  | 
|  | 974 | // 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] | 975 | for (Argument &Arg : F->args()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 976 | if (Arg.hasReturnedAttr()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 977 | auto &ReturnInstSet = ReturnedValues[&Arg]; | 
|  | 978 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) | 
|  | 979 | ReturnInstSet.insert(cast<ReturnInst>(RI)); | 
|  | 980 |  | 
|  | 981 | indicateOptimisticFixpoint(); | 
|  | 982 | return; | 
|  | 983 | } | 
|  | 984 | } | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 985 |  | 
|  | 986 | if (!F->hasExactDefinition()) | 
|  | 987 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 988 | } | 
|  | 989 |  | 
|  | 990 | /// See AbstractAttribute::manifest(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 991 | ChangeStatus manifest(Attributor &A) override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 992 |  | 
|  | 993 | /// See AbstractAttribute::getState(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 994 | AbstractState &getState() override { return *this; } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 995 |  | 
|  | 996 | /// See AbstractAttribute::getState(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 997 | const AbstractState &getState() const override { return *this; } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 998 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 999 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1000 | ChangeStatus updateImpl(Attributor &A) override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1001 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1002 | llvm::iterator_range<iterator> returned_values() override { | 
|  | 1003 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | 
|  | 1004 | } | 
|  | 1005 |  | 
|  | 1006 | llvm::iterator_range<const_iterator> returned_values() const override { | 
|  | 1007 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | 
|  | 1008 | } | 
|  | 1009 |  | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1010 | const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1011 | return UnresolvedCalls; | 
|  | 1012 | } | 
|  | 1013 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1014 | /// Return the number of potential return values, -1 if unknown. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1015 | size_t getNumReturnValues() const override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1016 | return isValidState() ? ReturnedValues.size() : -1; | 
|  | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | /// Return an assumed unique return value if a single candidate is found. If | 
|  | 1020 | /// there cannot be one, return a nullptr. If it is not clear yet, return the | 
|  | 1021 | /// Optional::NoneType. | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1022 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1023 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1024 | /// See AbstractState::checkForAllReturnedValues(...). | 
|  | 1025 | bool checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1026 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1027 | &Pred) const override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1028 |  | 
|  | 1029 | /// Pretty print the attribute similar to the IR representation. | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 1030 | const std::string getAsStr() const override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1031 |  | 
|  | 1032 | /// See AbstractState::isAtFixpoint(). | 
|  | 1033 | bool isAtFixpoint() const override { return IsFixed; } | 
|  | 1034 |  | 
|  | 1035 | /// See AbstractState::isValidState(). | 
|  | 1036 | bool isValidState() const override { return IsValidState; } | 
|  | 1037 |  | 
|  | 1038 | /// See AbstractState::indicateOptimisticFixpoint(...). | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1039 | ChangeStatus indicateOptimisticFixpoint() override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1040 | IsFixed = true; | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1041 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1042 | } | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1043 |  | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1044 | ChangeStatus indicatePessimisticFixpoint() override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1045 | IsFixed = true; | 
|  | 1046 | IsValidState = false; | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1047 | return ChangeStatus::CHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1048 | } | 
|  | 1049 | }; | 
|  | 1050 |  | 
|  | 1051 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { | 
|  | 1052 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 1053 |  | 
|  | 1054 | // Bookkeeping. | 
|  | 1055 | assert(isValidState()); | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1056 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, | 
|  | 1057 | "Number of function with known return values"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1058 |  | 
|  | 1059 | // 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] | 1060 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1061 |  | 
|  | 1062 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) | 
|  | 1063 | return Changed; | 
|  | 1064 |  | 
|  | 1065 | // Bookkeeping. | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1066 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, | 
|  | 1067 | "Number of function with unique return"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1068 |  | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1069 | // Callback to replace the uses of CB with the constant C. | 
|  | 1070 | auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) { | 
| Johannes Doerfert | 8fa56c4 | 2019-10-11 01:45:32 +0000 | [diff] [blame] | 1071 | if (CB.getNumUses() == 0 || CB.isMustTailCall()) | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1072 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 5d34602 | 2019-12-13 22:11:42 -0600 | [diff] [blame] | 1073 | replaceAllInstructionUsesWith(CB, C); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1074 | return ChangeStatus::CHANGED; | 
|  | 1075 | }; | 
|  | 1076 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1077 | // If the assumed unique return value is an argument, annotate it. | 
|  | 1078 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 1079 | // TODO: This should be handled differently! | 
|  | 1080 | this->AnchorVal = UniqueRVArg; | 
|  | 1081 | this->KindOrArgNo = UniqueRVArg->getArgNo(); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1082 | Changed = IRAttribute::manifest(A); | 
|  | 1083 | } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { | 
|  | 1084 | // We can replace the returned value with the unique returned constant. | 
|  | 1085 | Value &AnchorValue = getAnchorValue(); | 
|  | 1086 | if (Function *F = dyn_cast<Function>(&AnchorValue)) { | 
|  | 1087 | for (const Use &U : F->uses()) | 
|  | 1088 | if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1089 | if (CB->isCallee(&U)) { | 
|  | 1090 | Constant *RVCCast = | 
| Johannes Doerfert | 07d1642 | 2019-11-01 23:03:35 -0500 | [diff] [blame] | 1091 | CB->getType() == RVC->getType() | 
|  | 1092 | ? RVC | 
|  | 1093 | : ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1094 | Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; | 
|  | 1095 | } | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1096 | } else { | 
|  | 1097 | assert(isa<CallBase>(AnchorValue) && | 
|  | 1098 | "Expcected a function or call base anchor!"); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1099 | Constant *RVCCast = | 
| Johannes Doerfert | 07d1642 | 2019-11-01 23:03:35 -0500 | [diff] [blame] | 1100 | AnchorValue.getType() == RVC->getType() | 
|  | 1101 | ? RVC | 
|  | 1102 | : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1103 | Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1104 | } | 
|  | 1105 | if (Changed == ChangeStatus::CHANGED) | 
|  | 1106 | STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, | 
|  | 1107 | "Number of function returns replaced by constant return"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1108 | } | 
|  | 1109 |  | 
|  | 1110 | return Changed; | 
|  | 1111 | } | 
|  | 1112 |  | 
|  | 1113 | const std::string AAReturnedValuesImpl::getAsStr() const { | 
|  | 1114 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + | 
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 1115 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1116 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1117 | } | 
|  | 1118 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1119 | Optional<Value *> | 
|  | 1120 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { | 
|  | 1121 | // If checkForAllReturnedValues provides a unique value, ignoring potential | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1122 | // undef values that can also be present, it is assumed to be the actual | 
|  | 1123 | // return value and forwarded to the caller of this method. If there are | 
|  | 1124 | // multiple, a nullptr is returned indicating there cannot be a unique | 
|  | 1125 | // returned value. | 
|  | 1126 | Optional<Value *> UniqueRV; | 
|  | 1127 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1128 | auto Pred = [&](Value &RV) -> bool { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1129 | // If we found a second returned value and neither the current nor the saved | 
|  | 1130 | // one is an undef, there is no unique returned value. Undefs are special | 
|  | 1131 | // since we can pretend they have any value. | 
|  | 1132 | if (UniqueRV.hasValue() && UniqueRV != &RV && | 
|  | 1133 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { | 
|  | 1134 | UniqueRV = nullptr; | 
|  | 1135 | return false; | 
|  | 1136 | } | 
|  | 1137 |  | 
|  | 1138 | // Do not overwrite a value with an undef. | 
|  | 1139 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) | 
|  | 1140 | UniqueRV = &RV; | 
|  | 1141 |  | 
|  | 1142 | return true; | 
|  | 1143 | }; | 
|  | 1144 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1145 | if (!A.checkForAllReturnedValues(Pred, *this)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1146 | UniqueRV = nullptr; | 
|  | 1147 |  | 
|  | 1148 | return UniqueRV; | 
|  | 1149 | } | 
|  | 1150 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1151 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1152 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1153 | &Pred) const { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1154 | if (!isValidState()) | 
|  | 1155 | return false; | 
|  | 1156 |  | 
|  | 1157 | // Check all returned values but ignore call sites as long as we have not | 
|  | 1158 | // encountered an overdefined one during an update. | 
|  | 1159 | for (auto &It : ReturnedValues) { | 
|  | 1160 | Value *RV = It.first; | 
|  | 1161 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1162 | CallBase *CB = dyn_cast<CallBase>(RV); | 
|  | 1163 | if (CB && !UnresolvedCalls.count(CB)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1164 | continue; | 
|  | 1165 |  | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1166 | if (!Pred(*RV, It.second)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1167 | return false; | 
|  | 1168 | } | 
|  | 1169 |  | 
|  | 1170 | return true; | 
|  | 1171 | } | 
|  | 1172 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1173 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1174 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); | 
|  | 1175 | bool Changed = false; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1176 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1177 | // State used in the value traversals starting in returned values. | 
|  | 1178 | struct RVState { | 
|  | 1179 | // The map in which we collect return values -> return instrs. | 
|  | 1180 | decltype(ReturnedValues) &RetValsMap; | 
|  | 1181 | // The flag to indicate a change. | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1182 | bool &Changed; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1183 | // The return instrs we come from. | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1184 | SmallSetVector<ReturnInst *, 4> RetInsts; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1185 | }; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1186 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1187 | // Callback for a leaf value returned by the associated function. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1188 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1189 | auto Size = RVS.RetValsMap[&Val].size(); | 
|  | 1190 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); | 
|  | 1191 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; | 
|  | 1192 | RVS.Changed |= Inserted; | 
|  | 1193 | LLVM_DEBUG({ | 
|  | 1194 | if (Inserted) | 
|  | 1195 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val | 
|  | 1196 | << " => " << RVS.RetInsts.size() << "\n"; | 
|  | 1197 | }); | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1198 | return true; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1199 | }; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1200 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1201 | // Helper method to invoke the generic value traversal. | 
|  | 1202 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { | 
|  | 1203 | IRPosition RetValPos = IRPosition::value(RV); | 
|  | 1204 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, | 
|  | 1205 | RVS, VisitValueCB); | 
|  | 1206 | }; | 
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 1207 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1208 | // Callback for all "return intructions" live in the associated function. | 
|  | 1209 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { | 
|  | 1210 | ReturnInst &Ret = cast<ReturnInst>(I); | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1211 | RVState RVS({ReturnedValues, Changed, {}}); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1212 | RVS.RetInsts.insert(&Ret); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1213 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); | 
|  | 1214 | }; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1215 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1216 | // Start by discovering returned values from all live returned instructions in | 
|  | 1217 | // the associated function. | 
|  | 1218 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) | 
|  | 1219 | return indicatePessimisticFixpoint(); | 
|  | 1220 |  | 
|  | 1221 | // Once returned values "directly" present in the code are handled we try to | 
|  | 1222 | // resolve returned calls. | 
|  | 1223 | decltype(ReturnedValues) NewRVsMap; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1224 | for (auto &It : ReturnedValues) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1225 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first | 
|  | 1226 | << " by #" << It.second.size() << " RIs\n"); | 
|  | 1227 | CallBase *CB = dyn_cast<CallBase>(It.first); | 
|  | 1228 | if (!CB || UnresolvedCalls.count(CB)) | 
|  | 1229 | continue; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1230 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1231 | if (!CB->getCalledFunction()) { | 
|  | 1232 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB | 
|  | 1233 | << "\n"); | 
|  | 1234 | UnresolvedCalls.insert(CB); | 
|  | 1235 | continue; | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 1239 | const auto &RetValAA = A.getAAFor<AAReturnedValues>( | 
|  | 1240 | *this, IRPosition::function(*CB->getCalledFunction())); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1241 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 1242 | << RetValAA << "\n"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1243 |  | 
|  | 1244 | // Skip dead ends, thus if we do not know anything about the returned | 
|  | 1245 | // call we mark it as unresolved and it will stay that way. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1246 | if (!RetValAA.getState().isValidState()) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1247 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB | 
|  | 1248 | << "\n"); | 
|  | 1249 | UnresolvedCalls.insert(CB); | 
|  | 1250 | continue; | 
|  | 1251 | } | 
|  | 1252 |  | 
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1253 | // Do not try to learn partial information. If the callee has unresolved | 
|  | 1254 | // return values we will treat the call as unresolved/opaque. | 
|  | 1255 | auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); | 
|  | 1256 | if (!RetValAAUnresolvedCalls.empty()) { | 
|  | 1257 | UnresolvedCalls.insert(CB); | 
|  | 1258 | continue; | 
|  | 1259 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1260 |  | 
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1261 | // Now check if we can track transitively returned values. If possible, thus | 
|  | 1262 | // if all return value can be represented in the current scope, do so. | 
|  | 1263 | bool Unresolved = false; | 
|  | 1264 | for (auto &RetValAAIt : RetValAA.returned_values()) { | 
|  | 1265 | Value *RetVal = RetValAAIt.first; | 
|  | 1266 | if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || | 
|  | 1267 | isa<Constant>(RetVal)) | 
|  | 1268 | continue; | 
|  | 1269 | // Anything that did not fit in the above categories cannot be resolved, | 
|  | 1270 | // mark the call as unresolved. | 
|  | 1271 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " | 
|  | 1272 | "cannot be translated: " | 
|  | 1273 | << *RetVal << "\n"); | 
|  | 1274 | UnresolvedCalls.insert(CB); | 
|  | 1275 | Unresolved = true; | 
|  | 1276 | break; | 
|  | 1277 | } | 
|  | 1278 |  | 
|  | 1279 | if (Unresolved) | 
|  | 1280 | continue; | 
|  | 1281 |  | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1282 | // Now track transitively returned values. | 
|  | 1283 | unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; | 
|  | 1284 | if (NumRetAA == RetValAA.getNumReturnValues()) { | 
|  | 1285 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " | 
|  | 1286 | "changed since it was seen last\n"); | 
|  | 1287 | continue; | 
|  | 1288 | } | 
|  | 1289 | NumRetAA = RetValAA.getNumReturnValues(); | 
|  | 1290 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1291 | for (auto &RetValAAIt : RetValAA.returned_values()) { | 
|  | 1292 | Value *RetVal = RetValAAIt.first; | 
|  | 1293 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { | 
|  | 1294 | // Arguments are mapped to call site operands and we begin the traversal | 
|  | 1295 | // again. | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1296 | bool Unused = false; | 
|  | 1297 | RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1298 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); | 
|  | 1299 | continue; | 
|  | 1300 | } else if (isa<CallBase>(RetVal)) { | 
|  | 1301 | // Call sites are resolved by the callee attribute over time, no need to | 
|  | 1302 | // do anything for us. | 
|  | 1303 | continue; | 
|  | 1304 | } else if (isa<Constant>(RetVal)) { | 
|  | 1305 | // Constants are valid everywhere, we can simply take them. | 
|  | 1306 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); | 
|  | 1307 | continue; | 
|  | 1308 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1309 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1310 | } | 
|  | 1311 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1312 | // To avoid modifications to the ReturnedValues map while we iterate over it | 
|  | 1313 | // we kept record of potential new entries in a copy map, NewRVsMap. | 
|  | 1314 | for (auto &It : NewRVsMap) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1315 | assert(!It.second.empty() && "Entry does not add anything."); | 
|  | 1316 | auto &ReturnInsts = ReturnedValues[It.first]; | 
|  | 1317 | for (ReturnInst *RI : It.second) | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1318 | if (ReturnInsts.insert(RI)) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1319 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " | 
|  | 1320 | << *It.first << " => " << *RI << "\n"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1321 | Changed = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1322 | } | 
|  | 1323 | } | 
|  | 1324 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1325 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); | 
|  | 1326 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1327 | } | 
|  | 1328 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1329 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { | 
|  | 1330 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} | 
|  | 1331 |  | 
|  | 1332 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1333 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1334 | }; | 
|  | 1335 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1336 | /// Returned values information for a call sites. | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1337 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { | 
|  | 1338 | AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} | 
|  | 1339 |  | 
|  | 1340 | /// See AbstractAttribute::initialize(...). | 
|  | 1341 | void initialize(Attributor &A) override { | 
|  | 1342 | // TODO: Once we have call site specific value information we can provide | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1343 | //       call site specific liveness information and then it makes | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1344 | //       sense to specialize attributes for call sites instead of | 
|  | 1345 | //       redirecting requests to the callee. | 
|  | 1346 | llvm_unreachable("Abstract attributes for returned values are not " | 
|  | 1347 | "supported for call sites yet!"); | 
|  | 1348 | } | 
|  | 1349 |  | 
|  | 1350 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1351 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1352 | return indicatePessimisticFixpoint(); | 
|  | 1353 | } | 
|  | 1354 |  | 
|  | 1355 | /// See AbstractAttribute::trackStatistics() | 
|  | 1356 | void trackStatistics() const override {} | 
|  | 1357 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1358 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1359 | /// ------------------------ NoSync Function Attribute ------------------------- | 
|  | 1360 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1361 | struct AANoSyncImpl : AANoSync { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1362 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1363 |  | 
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 1364 | const std::string getAsStr() const override { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1365 | return getAssumed() ? "nosync" : "may-sync"; | 
|  | 1366 | } | 
|  | 1367 |  | 
|  | 1368 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1369 | ChangeStatus updateImpl(Attributor &A) override; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1370 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1371 | /// Helper function used to determine whether an instruction is non-relaxed | 
|  | 1372 | /// atomic. In other words, if an atomic instruction does not have unordered | 
|  | 1373 | /// or monotonic ordering | 
|  | 1374 | static bool isNonRelaxedAtomic(Instruction *I); | 
|  | 1375 |  | 
|  | 1376 | /// Helper function used to determine whether an instruction is volatile. | 
|  | 1377 | static bool isVolatile(Instruction *I); | 
|  | 1378 |  | 
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 1379 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, | 
|  | 1380 | /// memset). | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1381 | static bool isNoSyncIntrinsic(Instruction *I); | 
|  | 1382 | }; | 
|  | 1383 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1384 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1385 | if (!I->isAtomic()) | 
|  | 1386 | return false; | 
|  | 1387 |  | 
|  | 1388 | AtomicOrdering Ordering; | 
|  | 1389 | switch (I->getOpcode()) { | 
|  | 1390 | case Instruction::AtomicRMW: | 
|  | 1391 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); | 
|  | 1392 | break; | 
|  | 1393 | case Instruction::Store: | 
|  | 1394 | Ordering = cast<StoreInst>(I)->getOrdering(); | 
|  | 1395 | break; | 
|  | 1396 | case Instruction::Load: | 
|  | 1397 | Ordering = cast<LoadInst>(I)->getOrdering(); | 
|  | 1398 | break; | 
|  | 1399 | case Instruction::Fence: { | 
|  | 1400 | auto *FI = cast<FenceInst>(I); | 
|  | 1401 | if (FI->getSyncScopeID() == SyncScope::SingleThread) | 
|  | 1402 | return false; | 
|  | 1403 | Ordering = FI->getOrdering(); | 
|  | 1404 | break; | 
|  | 1405 | } | 
|  | 1406 | case Instruction::AtomicCmpXchg: { | 
|  | 1407 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); | 
|  | 1408 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); | 
|  | 1409 | // Only if both are relaxed, than it can be treated as relaxed. | 
|  | 1410 | // Otherwise it is non-relaxed. | 
|  | 1411 | if (Success != AtomicOrdering::Unordered && | 
|  | 1412 | Success != AtomicOrdering::Monotonic) | 
|  | 1413 | return true; | 
|  | 1414 | if (Failure != AtomicOrdering::Unordered && | 
|  | 1415 | Failure != AtomicOrdering::Monotonic) | 
|  | 1416 | return true; | 
|  | 1417 | return false; | 
|  | 1418 | } | 
|  | 1419 | default: | 
|  | 1420 | llvm_unreachable( | 
|  | 1421 | "New atomic operations need to be known in the attributor."); | 
|  | 1422 | } | 
|  | 1423 |  | 
|  | 1424 | // Relaxed. | 
|  | 1425 | if (Ordering == AtomicOrdering::Unordered || | 
|  | 1426 | Ordering == AtomicOrdering::Monotonic) | 
|  | 1427 | return false; | 
|  | 1428 | return true; | 
|  | 1429 | } | 
|  | 1430 |  | 
|  | 1431 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. | 
|  | 1432 | /// FIXME: We should ipmrove the handling of intrinsics. | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1433 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1434 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { | 
|  | 1435 | switch (II->getIntrinsicID()) { | 
|  | 1436 | /// Element wise atomic memory intrinsics are can only be unordered, | 
|  | 1437 | /// therefore nosync. | 
|  | 1438 | case Intrinsic::memset_element_unordered_atomic: | 
|  | 1439 | case Intrinsic::memmove_element_unordered_atomic: | 
|  | 1440 | case Intrinsic::memcpy_element_unordered_atomic: | 
|  | 1441 | return true; | 
|  | 1442 | case Intrinsic::memset: | 
|  | 1443 | case Intrinsic::memmove: | 
|  | 1444 | case Intrinsic::memcpy: | 
|  | 1445 | if (!cast<MemIntrinsic>(II)->isVolatile()) | 
|  | 1446 | return true; | 
|  | 1447 | return false; | 
|  | 1448 | default: | 
|  | 1449 | return false; | 
|  | 1450 | } | 
|  | 1451 | } | 
|  | 1452 | return false; | 
|  | 1453 | } | 
|  | 1454 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1455 | bool AANoSyncImpl::isVolatile(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1456 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && | 
|  | 1457 | "Calls should not be checked here"); | 
|  | 1458 |  | 
|  | 1459 | switch (I->getOpcode()) { | 
|  | 1460 | case Instruction::AtomicRMW: | 
|  | 1461 | return cast<AtomicRMWInst>(I)->isVolatile(); | 
|  | 1462 | case Instruction::Store: | 
|  | 1463 | return cast<StoreInst>(I)->isVolatile(); | 
|  | 1464 | case Instruction::Load: | 
|  | 1465 | return cast<LoadInst>(I)->isVolatile(); | 
|  | 1466 | case Instruction::AtomicCmpXchg: | 
|  | 1467 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); | 
|  | 1468 | default: | 
|  | 1469 | return false; | 
|  | 1470 | } | 
|  | 1471 | } | 
|  | 1472 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1473 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1474 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1475 | auto CheckRWInstForNoSync = [&](Instruction &I) { | 
|  | 1476 | /// We are looking for volatile instructions or Non-Relaxed atomics. | 
| Pankaj Gode | b9a26a8 | 2019-11-22 14:46:43 +0530 | [diff] [blame] | 1477 | /// FIXME: We should improve the handling of intrinsics. | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1478 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1479 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) | 
|  | 1480 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1481 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1482 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 1483 | if (ICS.hasFnAttr(Attribute::NoSync)) | 
|  | 1484 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1485 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1486 | const auto &NoSyncAA = | 
|  | 1487 | A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1488 | if (NoSyncAA.isAssumedNoSync()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1489 | return true; | 
|  | 1490 | return false; | 
|  | 1491 | } | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1492 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1493 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) | 
|  | 1494 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1495 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1496 | return false; | 
|  | 1497 | }; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1498 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1499 | auto CheckForNoSync = [&](Instruction &I) { | 
|  | 1500 | // At this point we handled all read/write effects and they are all | 
|  | 1501 | // nosync, so they can be skipped. | 
|  | 1502 | if (I.mayReadOrWriteMemory()) | 
|  | 1503 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1504 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1505 | // non-convergent and readnone imply nosync. | 
|  | 1506 | return !ImmutableCallSite(&I).isConvergent(); | 
|  | 1507 | }; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1508 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1509 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || | 
|  | 1510 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1511 | return indicatePessimisticFixpoint(); | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1512 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1513 | return ChangeStatus::UNCHANGED; | 
|  | 1514 | } | 
|  | 1515 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1516 | struct AANoSyncFunction final : public AANoSyncImpl { | 
|  | 1517 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} | 
|  | 1518 |  | 
|  | 1519 | /// See AbstractAttribute::trackStatistics() | 
|  | 1520 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } | 
|  | 1521 | }; | 
|  | 1522 |  | 
|  | 1523 | /// NoSync attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1524 | struct AANoSyncCallSite final : AANoSyncImpl { | 
|  | 1525 | AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} | 
|  | 1526 |  | 
|  | 1527 | /// See AbstractAttribute::initialize(...). | 
|  | 1528 | void initialize(Attributor &A) override { | 
|  | 1529 | AANoSyncImpl::initialize(A); | 
|  | 1530 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1531 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1532 | indicatePessimisticFixpoint(); | 
|  | 1533 | } | 
|  | 1534 |  | 
|  | 1535 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1536 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1537 | // TODO: Once we have call site specific value information we can provide | 
|  | 1538 | //       call site specific liveness information and then it makes | 
|  | 1539 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1540 | //       redirecting requests to the callee argument. | 
|  | 1541 | Function *F = getAssociatedFunction(); | 
|  | 1542 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1543 | auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); | 
|  | 1544 | return clampStateAndIndicateChange( | 
|  | 1545 | getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); | 
|  | 1546 | } | 
|  | 1547 |  | 
|  | 1548 | /// See AbstractAttribute::trackStatistics() | 
|  | 1549 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } | 
|  | 1550 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1551 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1552 | /// ------------------------ No-Free Attributes ---------------------------- | 
|  | 1553 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1554 | struct AANoFreeImpl : public AANoFree { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1555 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1556 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1557 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1558 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1559 | auto CheckForNoFree = [&](Instruction &I) { | 
|  | 1560 | ImmutableCallSite ICS(&I); | 
|  | 1561 | if (ICS.hasFnAttr(Attribute::NoFree)) | 
|  | 1562 | return true; | 
|  | 1563 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1564 | const auto &NoFreeAA = | 
|  | 1565 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1566 | return NoFreeAA.isAssumedNoFree(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1567 | }; | 
|  | 1568 |  | 
|  | 1569 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) | 
|  | 1570 | return indicatePessimisticFixpoint(); | 
|  | 1571 | return ChangeStatus::UNCHANGED; | 
|  | 1572 | } | 
|  | 1573 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1574 | /// See AbstractAttribute::getAsStr(). | 
|  | 1575 | const std::string getAsStr() const override { | 
|  | 1576 | return getAssumed() ? "nofree" : "may-free"; | 
|  | 1577 | } | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1578 | }; | 
|  | 1579 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1580 | struct AANoFreeFunction final : public AANoFreeImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1581 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1582 |  | 
|  | 1583 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1584 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1585 | }; | 
|  | 1586 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1587 | /// NoFree attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1588 | struct AANoFreeCallSite final : AANoFreeImpl { | 
|  | 1589 | AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
|  | 1590 |  | 
|  | 1591 | /// See AbstractAttribute::initialize(...). | 
|  | 1592 | void initialize(Attributor &A) override { | 
|  | 1593 | AANoFreeImpl::initialize(A); | 
|  | 1594 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1595 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1596 | indicatePessimisticFixpoint(); | 
|  | 1597 | } | 
|  | 1598 |  | 
|  | 1599 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1600 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1601 | // TODO: Once we have call site specific value information we can provide | 
|  | 1602 | //       call site specific liveness information and then it makes | 
|  | 1603 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1604 | //       redirecting requests to the callee argument. | 
|  | 1605 | Function *F = getAssociatedFunction(); | 
|  | 1606 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1607 | auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); | 
|  | 1608 | return clampStateAndIndicateChange( | 
|  | 1609 | getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); | 
|  | 1610 | } | 
|  | 1611 |  | 
|  | 1612 | /// See AbstractAttribute::trackStatistics() | 
|  | 1613 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } | 
|  | 1614 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1615 |  | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1616 | /// NoFree attribute for floating values. | 
|  | 1617 | struct AANoFreeFloating : AANoFreeImpl { | 
|  | 1618 | AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
|  | 1619 |  | 
|  | 1620 | /// See AbstractAttribute::trackStatistics() | 
|  | 1621 | void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)} | 
|  | 1622 |  | 
|  | 1623 | /// See Abstract Attribute::updateImpl(...). | 
|  | 1624 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1625 | const IRPosition &IRP = getIRPosition(); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1626 |  | 
|  | 1627 | const auto &NoFreeAA = | 
|  | 1628 | A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP)); | 
|  | 1629 | if (NoFreeAA.isAssumedNoFree()) | 
|  | 1630 | return ChangeStatus::UNCHANGED; | 
|  | 1631 |  | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1632 | Value &AssociatedValue = getIRPosition().getAssociatedValue(); | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1633 | auto Pred = [&](const Use &U, bool &Follow) -> bool { | 
|  | 1634 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1635 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1636 | if (CB->isBundleOperand(&U)) | 
|  | 1637 | return false; | 
|  | 1638 | if (!CB->isArgOperand(&U)) | 
|  | 1639 | return true; | 
|  | 1640 | unsigned ArgNo = CB->getArgOperandNo(&U); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1641 |  | 
|  | 1642 | const auto &NoFreeArg = A.getAAFor<AANoFree>( | 
|  | 1643 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1644 | return NoFreeArg.isAssumedNoFree(); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1645 | } | 
|  | 1646 |  | 
|  | 1647 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | 
|  | 1648 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1649 | Follow = true; | 
|  | 1650 | return true; | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 1651 | } | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1652 |  | 
|  | 1653 | // Unknown user. | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1654 | return false; | 
|  | 1655 | }; | 
|  | 1656 | if (!A.checkForAllUses(Pred, *this, AssociatedValue)) | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1657 | return indicatePessimisticFixpoint(); | 
| Hideto Ueno | 63599bd | 2019-12-12 12:26:09 +0000 | [diff] [blame] | 1658 |  | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1659 | return ChangeStatus::UNCHANGED; | 
|  | 1660 | } | 
|  | 1661 | }; | 
|  | 1662 |  | 
|  | 1663 | /// NoFree attribute for a call site argument. | 
|  | 1664 | struct AANoFreeArgument final : AANoFreeFloating { | 
|  | 1665 | AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} | 
|  | 1666 |  | 
|  | 1667 | /// See AbstractAttribute::trackStatistics() | 
|  | 1668 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) } | 
|  | 1669 | }; | 
|  | 1670 |  | 
|  | 1671 | /// NoFree attribute for call site arguments. | 
|  | 1672 | struct AANoFreeCallSiteArgument final : AANoFreeFloating { | 
|  | 1673 | AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} | 
|  | 1674 |  | 
|  | 1675 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1676 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1677 | // TODO: Once we have call site specific value information we can provide | 
|  | 1678 | //       call site specific liveness information and then it makes | 
|  | 1679 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1680 | //       redirecting requests to the callee argument. | 
|  | 1681 | Argument *Arg = getAssociatedArgument(); | 
|  | 1682 | if (!Arg) | 
|  | 1683 | return indicatePessimisticFixpoint(); | 
|  | 1684 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 1685 | auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos); | 
|  | 1686 | return clampStateAndIndicateChange( | 
|  | 1687 | getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState())); | 
|  | 1688 | } | 
|  | 1689 |  | 
|  | 1690 | /// See AbstractAttribute::trackStatistics() | 
|  | 1691 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)}; | 
|  | 1692 | }; | 
|  | 1693 |  | 
|  | 1694 | /// NoFree attribute for function return value. | 
|  | 1695 | struct AANoFreeReturned final : AANoFreeFloating { | 
|  | 1696 | AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) { | 
|  | 1697 | llvm_unreachable("NoFree is not applicable to function returns!"); | 
|  | 1698 | } | 
|  | 1699 |  | 
|  | 1700 | /// See AbstractAttribute::initialize(...). | 
|  | 1701 | void initialize(Attributor &A) override { | 
|  | 1702 | llvm_unreachable("NoFree is not applicable to function returns!"); | 
|  | 1703 | } | 
|  | 1704 |  | 
|  | 1705 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1706 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1707 | llvm_unreachable("NoFree is not applicable to function returns!"); | 
|  | 1708 | } | 
|  | 1709 |  | 
|  | 1710 | /// See AbstractAttribute::trackStatistics() | 
|  | 1711 | void trackStatistics() const override {} | 
|  | 1712 | }; | 
|  | 1713 |  | 
|  | 1714 | /// NoFree attribute deduction for a call site return value. | 
|  | 1715 | struct AANoFreeCallSiteReturned final : AANoFreeFloating { | 
|  | 1716 | AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {} | 
|  | 1717 |  | 
|  | 1718 | ChangeStatus manifest(Attributor &A) override { | 
|  | 1719 | return ChangeStatus::UNCHANGED; | 
|  | 1720 | } | 
|  | 1721 | /// See AbstractAttribute::trackStatistics() | 
|  | 1722 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) } | 
|  | 1723 | }; | 
|  | 1724 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1725 | /// ------------------------ NonNull Argument Attribute ------------------------ | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1726 | static int64_t getKnownNonNullAndDerefBytesForUse( | 
|  | 1727 | Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue, | 
|  | 1728 | const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1729 | TrackUse = false; | 
|  | 1730 |  | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1731 | const Value *UseV = U->get(); | 
|  | 1732 | if (!UseV->getType()->isPointerTy()) | 
|  | 1733 | return 0; | 
|  | 1734 |  | 
|  | 1735 | Type *PtrTy = UseV->getType(); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1736 | const Function *F = I->getFunction(); | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1737 | bool NullPointerIsDefined = | 
|  | 1738 | F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1739 | const DataLayout &DL = A.getInfoCache().getDL(); | 
|  | 1740 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { | 
|  | 1741 | if (ICS.isBundleOperand(U)) | 
|  | 1742 | return 0; | 
|  | 1743 |  | 
|  | 1744 | if (ICS.isCallee(U)) { | 
|  | 1745 | IsNonNull |= !NullPointerIsDefined; | 
|  | 1746 | return 0; | 
|  | 1747 | } | 
|  | 1748 |  | 
|  | 1749 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 1750 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); | 
| Johannes Doerfert | 77a6b35 | 2019-11-02 14:47:45 -0500 | [diff] [blame] | 1751 | // As long as we only use known information there is no need to track | 
|  | 1752 | // dependences here. | 
|  | 1753 | auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP, | 
|  | 1754 | /* TrackDependence */ false); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1755 | IsNonNull |= DerefAA.isKnownNonNull(); | 
|  | 1756 | return DerefAA.getKnownDereferenceableBytes(); | 
|  | 1757 | } | 
|  | 1758 |  | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1759 | // We need to follow common pointer manipulation uses to the accesses they | 
|  | 1760 | // feed into. We can try to be smart to avoid looking through things we do not | 
|  | 1761 | // like for now, e.g., non-inbounds GEPs. | 
|  | 1762 | if (isa<CastInst>(I)) { | 
|  | 1763 | TrackUse = true; | 
|  | 1764 | return 0; | 
|  | 1765 | } | 
|  | 1766 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 1767 | if (GEP->hasAllConstantIndices()) { | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1768 | TrackUse = true; | 
|  | 1769 | return 0; | 
|  | 1770 | } | 
|  | 1771 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1772 | int64_t Offset; | 
|  | 1773 | if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 1774 | if (Base == &AssociatedValue && | 
|  | 1775 | Attributor::getPointerOperand(I, /* AllowVolatile */ false) == UseV) { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1776 | int64_t DerefBytes = | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1777 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1778 |  | 
|  | 1779 | IsNonNull |= !NullPointerIsDefined; | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1780 | return std::max(int64_t(0), DerefBytes); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1781 | } | 
|  | 1782 | } | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 1783 |  | 
|  | 1784 | /// Corner case when an offset is 0. | 
|  | 1785 | if (const Value *Base = getBasePointerOfAccessPointerOperand( | 
|  | 1786 | I, Offset, DL, /*AllowNonInbounds*/ true)) { | 
|  | 1787 | if (Offset == 0 && Base == &AssociatedValue && | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 1788 | Attributor::getPointerOperand(I, /* AllowVolatile */ false) == UseV) { | 
| Hideto Ueno | 0f4383f | 2019-11-27 14:41:12 +0000 | [diff] [blame] | 1789 | int64_t DerefBytes = | 
|  | 1790 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); | 
|  | 1791 | IsNonNull |= !NullPointerIsDefined; | 
|  | 1792 | return std::max(int64_t(0), DerefBytes); | 
|  | 1793 | } | 
|  | 1794 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1795 | if (const Value *Base = | 
|  | 1796 | GetPointerBaseWithConstantOffset(UseV, Offset, DL, | 
|  | 1797 | /*AllowNonInbounds*/ false)) { | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1798 | if (Base == &AssociatedValue) { | 
| Johannes Doerfert | 77a6b35 | 2019-11-02 14:47:45 -0500 | [diff] [blame] | 1799 | // As long as we only use known information there is no need to track | 
|  | 1800 | // dependences here. | 
|  | 1801 | auto &DerefAA = A.getAAFor<AADereferenceable>( | 
|  | 1802 | QueryingAA, IRPosition::value(*Base), /* TrackDependence */ false); | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1803 | IsNonNull |= (!NullPointerIsDefined && DerefAA.isKnownNonNull()); | 
|  | 1804 | IsNonNull |= (!NullPointerIsDefined && (Offset != 0)); | 
|  | 1805 | int64_t DerefBytes = DerefAA.getKnownDereferenceableBytes(); | 
|  | 1806 | return std::max(int64_t(0), DerefBytes - std::max(int64_t(0), Offset)); | 
|  | 1807 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1808 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1809 |  | 
|  | 1810 | return 0; | 
|  | 1811 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1812 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1813 | struct AANonNullImpl : AANonNull { | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1814 | AANonNullImpl(const IRPosition &IRP) | 
|  | 1815 | : AANonNull(IRP), | 
|  | 1816 | NullIsDefined(NullPointerIsDefined( | 
|  | 1817 | getAnchorScope(), | 
|  | 1818 | getAssociatedValue().getType()->getPointerAddressSpace())) {} | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1819 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1820 | /// See AbstractAttribute::initialize(...). | 
|  | 1821 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1822 | if (!NullIsDefined && | 
|  | 1823 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1824 | indicateOptimisticFixpoint(); | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1825 | else if (isa<ConstantPointerNull>(getAssociatedValue())) | 
|  | 1826 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1827 | else | 
|  | 1828 | AANonNull::initialize(A); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1829 | } | 
|  | 1830 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1831 | /// See AAFromMustBeExecutedContext | 
|  | 1832 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 1833 | bool IsNonNull = false; | 
|  | 1834 | bool TrackUse = false; | 
|  | 1835 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, | 
|  | 1836 | IsNonNull, TrackUse); | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1837 | setKnown(IsNonNull); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1838 | return TrackUse; | 
|  | 1839 | } | 
|  | 1840 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1841 | /// See AbstractAttribute::getAsStr(). | 
|  | 1842 | const std::string getAsStr() const override { | 
|  | 1843 | return getAssumed() ? "nonnull" : "may-null"; | 
|  | 1844 | } | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1845 |  | 
|  | 1846 | /// Flag to determine if the underlying value can be null and still allow | 
|  | 1847 | /// valid accesses. | 
|  | 1848 | const bool NullIsDefined; | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1849 | }; | 
|  | 1850 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1851 | /// NonNull attribute for a floating value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1852 | struct AANonNullFloating | 
|  | 1853 | : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { | 
|  | 1854 | using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; | 
|  | 1855 | AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1856 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1857 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1858 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1859 | ChangeStatus Change = Base::updateImpl(A); | 
|  | 1860 | if (isKnownNonNull()) | 
|  | 1861 | return Change; | 
|  | 1862 |  | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1863 | if (!NullIsDefined) { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1864 | const auto &DerefAA = | 
|  | 1865 | A.getAAFor<AADereferenceable>(*this, getIRPosition()); | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1866 | if (DerefAA.getAssumedDereferenceableBytes()) | 
|  | 1867 | return Change; | 
|  | 1868 | } | 
|  | 1869 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1870 | const DataLayout &DL = A.getDataLayout(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1871 |  | 
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1872 | DominatorTree *DT = nullptr; | 
|  | 1873 | InformationCache &InfoCache = A.getInfoCache(); | 
|  | 1874 | if (const Function *Fn = getAnchorScope()) | 
|  | 1875 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); | 
|  | 1876 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1877 | auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1878 | bool Stripped) -> bool { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1879 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); | 
|  | 1880 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1881 | if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT)) | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1882 | T.indicatePessimisticFixpoint(); | 
|  | 1883 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1884 | // Use abstract attribute information. | 
|  | 1885 | const AANonNull::StateType &NS = | 
|  | 1886 | static_cast<const AANonNull::StateType &>(AA.getState()); | 
|  | 1887 | T ^= NS; | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1888 | } | 
|  | 1889 | return T.isValidState(); | 
|  | 1890 | }; | 
|  | 1891 |  | 
|  | 1892 | StateType T; | 
|  | 1893 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, | 
|  | 1894 | T, VisitValueCB)) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1895 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1896 |  | 
|  | 1897 | return clampStateAndIndicateChange(getState(), T); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1898 | } | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1899 |  | 
|  | 1900 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1901 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1902 | }; | 
|  | 1903 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1904 | /// NonNull attribute for function return value. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1905 | struct AANonNullReturned final | 
|  | 1906 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1907 | AANonNullReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1908 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1909 |  | 
|  | 1910 | /// See AbstractAttribute::trackStatistics() | 
|  | 1911 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } | 
|  | 1912 | }; | 
|  | 1913 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1914 | /// NonNull attribute for function argument. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1915 | struct AANonNullArgument final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1916 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, | 
|  | 1917 | AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1918 | AANonNullArgument(const IRPosition &IRP) | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1919 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, | 
|  | 1920 | AANonNullImpl>( | 
|  | 1921 | IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1922 |  | 
|  | 1923 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1924 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1925 | }; | 
|  | 1926 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1927 | struct AANonNullCallSiteArgument final : AANonNullFloating { | 
|  | 1928 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1929 |  | 
|  | 1930 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 1931 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1932 | }; | 
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1933 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1934 | /// NonNull attribute for a call site return position. | 
|  | 1935 | struct AANonNullCallSiteReturned final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1936 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, | 
|  | 1937 | AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1938 | AANonNullCallSiteReturned(const IRPosition &IRP) | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1939 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, | 
|  | 1940 | AANonNullImpl>( | 
|  | 1941 | IRP) {} | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1942 |  | 
|  | 1943 | /// See AbstractAttribute::trackStatistics() | 
|  | 1944 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } | 
|  | 1945 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1946 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1947 | /// ------------------------ No-Recurse Attributes ---------------------------- | 
|  | 1948 |  | 
|  | 1949 | struct AANoRecurseImpl : public AANoRecurse { | 
|  | 1950 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} | 
|  | 1951 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1952 | /// See AbstractAttribute::getAsStr() | 
|  | 1953 | const std::string getAsStr() const override { | 
|  | 1954 | return getAssumed() ? "norecurse" : "may-recurse"; | 
|  | 1955 | } | 
|  | 1956 | }; | 
|  | 1957 |  | 
|  | 1958 | struct AANoRecurseFunction final : AANoRecurseImpl { | 
|  | 1959 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} | 
|  | 1960 |  | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1961 | /// See AbstractAttribute::initialize(...). | 
|  | 1962 | void initialize(Attributor &A) override { | 
|  | 1963 | AANoRecurseImpl::initialize(A); | 
|  | 1964 | if (const Function *F = getAnchorScope()) | 
|  | 1965 | if (A.getInfoCache().getSccSize(*F) == 1) | 
|  | 1966 | return; | 
|  | 1967 | indicatePessimisticFixpoint(); | 
|  | 1968 | } | 
|  | 1969 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1970 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1971 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1972 |  | 
|  | 1973 | auto CheckForNoRecurse = [&](Instruction &I) { | 
|  | 1974 | ImmutableCallSite ICS(&I); | 
|  | 1975 | if (ICS.hasFnAttr(Attribute::NoRecurse)) | 
|  | 1976 | return true; | 
|  | 1977 |  | 
|  | 1978 | const auto &NoRecurseAA = | 
|  | 1979 | A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1980 | if (!NoRecurseAA.isAssumedNoRecurse()) | 
|  | 1981 | return false; | 
|  | 1982 |  | 
|  | 1983 | // Recursion to the same function | 
|  | 1984 | if (ICS.getCalledFunction() == getAnchorScope()) | 
|  | 1985 | return false; | 
|  | 1986 |  | 
|  | 1987 | return true; | 
|  | 1988 | }; | 
|  | 1989 |  | 
|  | 1990 | if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) | 
|  | 1991 | return indicatePessimisticFixpoint(); | 
|  | 1992 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1993 | } | 
|  | 1994 |  | 
|  | 1995 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } | 
|  | 1996 | }; | 
|  | 1997 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1998 | /// NoRecurse attribute deduction for a call sites. | 
|  | 1999 | struct AANoRecurseCallSite final : AANoRecurseImpl { | 
|  | 2000 | AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} | 
|  | 2001 |  | 
|  | 2002 | /// See AbstractAttribute::initialize(...). | 
|  | 2003 | void initialize(Attributor &A) override { | 
|  | 2004 | AANoRecurseImpl::initialize(A); | 
|  | 2005 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2006 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2007 | indicatePessimisticFixpoint(); | 
|  | 2008 | } | 
|  | 2009 |  | 
|  | 2010 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2011 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2012 | // TODO: Once we have call site specific value information we can provide | 
|  | 2013 | //       call site specific liveness information and then it makes | 
|  | 2014 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2015 | //       redirecting requests to the callee argument. | 
|  | 2016 | Function *F = getAssociatedFunction(); | 
|  | 2017 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 2018 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); | 
|  | 2019 | return clampStateAndIndicateChange( | 
|  | 2020 | getState(), | 
|  | 2021 | static_cast<const AANoRecurse::StateType &>(FnAA.getState())); | 
|  | 2022 | } | 
|  | 2023 |  | 
|  | 2024 | /// See AbstractAttribute::trackStatistics() | 
|  | 2025 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } | 
|  | 2026 | }; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2027 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2028 | /// -------------------- Undefined-Behavior Attributes ------------------------ | 
|  | 2029 |  | 
|  | 2030 | struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior { | 
|  | 2031 | AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {} | 
|  | 2032 |  | 
|  | 2033 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2034 | // through a pointer (i.e. also branches etc.) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2035 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2036 | const size_t UBPrevSize = KnownUBInsts.size(); | 
|  | 2037 | const size_t NoUBPrevSize = AssumedNoUBInsts.size(); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2038 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2039 | auto InspectMemAccessInstForUB = [&](Instruction &I) { | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2040 | // Skip instructions that are already saved. | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2041 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2042 | return true; | 
|  | 2043 |  | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2044 | // If we reach here, we know we have an instruction | 
|  | 2045 | // that accesses memory through a pointer operand, | 
|  | 2046 | // for which getPointerOperand() should give it to us. | 
|  | 2047 | const Value *PtrOp = | 
|  | 2048 | Attributor::getPointerOperand(&I, /* AllowVolatile */ true); | 
|  | 2049 | assert(PtrOp && | 
|  | 2050 | "Expected pointer operand of memory accessing instruction"); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2051 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2052 | // A memory access through a pointer is considered UB | 
|  | 2053 | // only if the pointer has constant null value. | 
|  | 2054 | // TODO: Expand it to not only check constant values. | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2055 | if (!isa<ConstantPointerNull>(PtrOp)) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2056 | AssumedNoUBInsts.insert(&I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2057 | return true; | 
|  | 2058 | } | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2059 | const Type *PtrTy = PtrOp->getType(); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2060 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2061 | // Because we only consider instructions inside functions, | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2062 | // assume that a parent function exists. | 
|  | 2063 | const Function *F = I.getFunction(); | 
|  | 2064 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2065 | // A memory access using constant null pointer is only considered UB | 
|  | 2066 | // if null pointer is _not_ defined for the target platform. | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2067 | if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace())) | 
|  | 2068 | AssumedNoUBInsts.insert(&I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2069 | else | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2070 | KnownUBInsts.insert(&I); | 
|  | 2071 | return true; | 
|  | 2072 | }; | 
|  | 2073 |  | 
|  | 2074 | auto InspectBrInstForUB = [&](Instruction &I) { | 
|  | 2075 | // A conditional branch instruction is considered UB if it has `undef` | 
|  | 2076 | // condition. | 
|  | 2077 |  | 
|  | 2078 | // Skip instructions that are already saved. | 
|  | 2079 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | 
|  | 2080 | return true; | 
|  | 2081 |  | 
|  | 2082 | // We know we have a branch instruction. | 
|  | 2083 | auto BrInst = cast<BranchInst>(&I); | 
|  | 2084 |  | 
|  | 2085 | // Unconditional branches are never considered UB. | 
|  | 2086 | if (BrInst->isUnconditional()) | 
|  | 2087 | return true; | 
|  | 2088 |  | 
|  | 2089 | // Either we stopped and the appropriate action was taken, | 
|  | 2090 | // or we got back a simplified value to continue. | 
|  | 2091 | Optional<Value *> SimplifiedCond = | 
|  | 2092 | stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst); | 
|  | 2093 | if (!SimplifiedCond.hasValue()) | 
|  | 2094 | return true; | 
|  | 2095 | AssumedNoUBInsts.insert(&I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2096 | return true; | 
|  | 2097 | }; | 
|  | 2098 |  | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2099 | A.checkForAllInstructions(InspectMemAccessInstForUB, *this, | 
|  | 2100 | {Instruction::Load, Instruction::Store, | 
|  | 2101 | Instruction::AtomicCmpXchg, | 
|  | 2102 | Instruction::AtomicRMW}); | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2103 | A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br}); | 
|  | 2104 | if (NoUBPrevSize != AssumedNoUBInsts.size() || | 
|  | 2105 | UBPrevSize != KnownUBInsts.size()) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2106 | return ChangeStatus::CHANGED; | 
|  | 2107 | return ChangeStatus::UNCHANGED; | 
|  | 2108 | } | 
|  | 2109 |  | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2110 | bool isKnownToCauseUB(Instruction *I) const override { | 
|  | 2111 | return KnownUBInsts.count(I); | 
|  | 2112 | } | 
|  | 2113 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2114 | bool isAssumedToCauseUB(Instruction *I) const override { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2115 | // In simple words, if an instruction is not in the assumed to _not_ | 
|  | 2116 | // cause UB, then it is assumed UB (that includes those | 
|  | 2117 | // in the KnownUBInsts set). The rest is boilerplate | 
|  | 2118 | // is to ensure that it is one of the instructions we test | 
|  | 2119 | // for UB. | 
|  | 2120 |  | 
|  | 2121 | switch (I->getOpcode()) { | 
|  | 2122 | case Instruction::Load: | 
|  | 2123 | case Instruction::Store: | 
|  | 2124 | case Instruction::AtomicCmpXchg: | 
|  | 2125 | case Instruction::AtomicRMW: | 
|  | 2126 | return !AssumedNoUBInsts.count(I); | 
|  | 2127 | case Instruction::Br: { | 
|  | 2128 | auto BrInst = cast<BranchInst>(I); | 
|  | 2129 | if (BrInst->isUnconditional()) | 
|  | 2130 | return false; | 
|  | 2131 | return !AssumedNoUBInsts.count(I); | 
|  | 2132 | } break; | 
|  | 2133 | default: | 
|  | 2134 | return false; | 
|  | 2135 | } | 
|  | 2136 | return false; | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2137 | } | 
|  | 2138 |  | 
|  | 2139 | ChangeStatus manifest(Attributor &A) override { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2140 | if (KnownUBInsts.empty()) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2141 | return ChangeStatus::UNCHANGED; | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2142 | for (Instruction *I : KnownUBInsts) | 
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 2143 | A.changeToUnreachableAfterManifest(I); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2144 | return ChangeStatus::CHANGED; | 
|  | 2145 | } | 
|  | 2146 |  | 
|  | 2147 | /// See AbstractAttribute::getAsStr() | 
|  | 2148 | const std::string getAsStr() const override { | 
|  | 2149 | return getAssumed() ? "undefined-behavior" : "no-ub"; | 
|  | 2150 | } | 
|  | 2151 |  | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2152 | /// Note: The correctness of this analysis depends on the fact that the | 
|  | 2153 | /// following 2 sets will stop changing after some point. | 
|  | 2154 | /// "Change" here means that their size changes. | 
|  | 2155 | /// The size of each set is monotonically increasing | 
|  | 2156 | /// (we only add items to them) and it is upper bounded by the number of | 
|  | 2157 | /// instructions in the processed function (we can never save more | 
|  | 2158 | /// elements in either set than this number). Hence, at some point, | 
|  | 2159 | /// they will stop increasing. | 
|  | 2160 | /// Consequently, at some point, both sets will have stopped | 
|  | 2161 | /// changing, effectively making the analysis reach a fixpoint. | 
|  | 2162 |  | 
|  | 2163 | /// Note: These 2 sets are disjoint and an instruction can be considered | 
|  | 2164 | /// one of 3 things: | 
|  | 2165 | /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in | 
|  | 2166 | ///    the KnownUBInsts set. | 
|  | 2167 | /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior | 
|  | 2168 | ///    has a reason to assume it). | 
|  | 2169 | /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior | 
|  | 2170 | ///    could not find a reason to assume or prove that it can cause UB, | 
|  | 2171 | ///    hence it assumes it doesn't. We have a set for these instructions | 
|  | 2172 | ///    so that we don't reprocess them in every update. | 
|  | 2173 | ///    Note however that instructions in this set may cause UB. | 
|  | 2174 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2175 | protected: | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2176 | /// A set of all live instructions _known_ to cause UB. | 
|  | 2177 | SmallPtrSet<Instruction *, 8> KnownUBInsts; | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2178 |  | 
|  | 2179 | private: | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2180 | /// A set of all the (live) instructions that are assumed to _not_ cause UB. | 
|  | 2181 | SmallPtrSet<Instruction *, 8> AssumedNoUBInsts; | 
|  | 2182 |  | 
|  | 2183 | // Should be called on updates in which if we're processing an instruction | 
|  | 2184 | // \p I that depends on a value \p V, one of the following has to happen: | 
|  | 2185 | // - If the value is assumed, then stop. | 
|  | 2186 | // - If the value is known but undef, then consider it UB. | 
|  | 2187 | // - Otherwise, do specific processing with the simplified value. | 
|  | 2188 | // We return None in the first 2 cases to signify that an appropriate | 
|  | 2189 | // action was taken and the caller should stop. | 
|  | 2190 | // Otherwise, we return the simplified value that the caller should | 
|  | 2191 | // use for specific processing. | 
|  | 2192 | Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V, | 
|  | 2193 | Instruction *I) { | 
|  | 2194 | const auto &ValueSimplifyAA = | 
|  | 2195 | A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V)); | 
|  | 2196 | Optional<Value *> SimplifiedV = | 
|  | 2197 | ValueSimplifyAA.getAssumedSimplifiedValue(A); | 
|  | 2198 | if (!ValueSimplifyAA.isKnown()) { | 
|  | 2199 | // Don't depend on assumed values. | 
|  | 2200 | return llvm::None; | 
|  | 2201 | } | 
|  | 2202 | if (!SimplifiedV.hasValue()) { | 
|  | 2203 | // If it is known (which we tested above) but it doesn't have a value, | 
|  | 2204 | // then we can assume `undef` and hence the instruction is UB. | 
|  | 2205 | KnownUBInsts.insert(I); | 
|  | 2206 | return llvm::None; | 
|  | 2207 | } | 
|  | 2208 | Value *Val = SimplifiedV.getValue(); | 
|  | 2209 | if (isa<UndefValue>(Val)) { | 
|  | 2210 | KnownUBInsts.insert(I); | 
|  | 2211 | return llvm::None; | 
|  | 2212 | } | 
|  | 2213 | return Val; | 
|  | 2214 | } | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2215 | }; | 
|  | 2216 |  | 
|  | 2217 | struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl { | 
|  | 2218 | AAUndefinedBehaviorFunction(const IRPosition &IRP) | 
|  | 2219 | : AAUndefinedBehaviorImpl(IRP) {} | 
|  | 2220 |  | 
|  | 2221 | /// See AbstractAttribute::trackStatistics() | 
|  | 2222 | void trackStatistics() const override { | 
|  | 2223 | STATS_DECL(UndefinedBehaviorInstruction, Instruction, | 
|  | 2224 | "Number of instructions known to have UB"); | 
|  | 2225 | BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) += | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2226 | KnownUBInsts.size(); | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2227 | } | 
|  | 2228 | }; | 
|  | 2229 |  | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2230 | /// ------------------------ Will-Return Attributes ---------------------------- | 
|  | 2231 |  | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2232 | // Helper function that checks whether a function has any cycle. | 
|  | 2233 | // TODO: Replace with more efficent code | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2234 | static bool containsCycle(Function &F) { | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2235 | SmallPtrSet<BasicBlock *, 32> Visited; | 
|  | 2236 |  | 
|  | 2237 | // Traverse BB by dfs and check whether successor is already visited. | 
|  | 2238 | for (BasicBlock *BB : depth_first(&F)) { | 
|  | 2239 | Visited.insert(BB); | 
|  | 2240 | for (auto *SuccBB : successors(BB)) { | 
|  | 2241 | if (Visited.count(SuccBB)) | 
|  | 2242 | return true; | 
|  | 2243 | } | 
|  | 2244 | } | 
|  | 2245 | return false; | 
|  | 2246 | } | 
|  | 2247 |  | 
|  | 2248 | // Helper function that checks the function have a loop which might become an | 
|  | 2249 | // endless loop | 
|  | 2250 | // FIXME: Any cycle is regarded as endless loop for now. | 
|  | 2251 | //        We have to allow some patterns. | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2252 | static bool containsPossiblyEndlessLoop(Function *F) { | 
|  | 2253 | return !F || !F->hasExactDefinition() || containsCycle(*F); | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2254 | } | 
|  | 2255 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2256 | struct AAWillReturnImpl : public AAWillReturn { | 
|  | 2257 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2258 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2259 | /// See AbstractAttribute::initialize(...). | 
|  | 2260 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2261 | AAWillReturn::initialize(A); | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2262 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2263 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2264 | if (containsPossiblyEndlessLoop(F)) | 
|  | 2265 | indicatePessimisticFixpoint(); | 
|  | 2266 | } | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2267 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2268 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2269 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2270 | auto CheckForWillReturn = [&](Instruction &I) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2271 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); | 
|  | 2272 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); | 
|  | 2273 | if (WillReturnAA.isKnownWillReturn()) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2274 | return true; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2275 | if (!WillReturnAA.isAssumedWillReturn()) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2276 | return false; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2277 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); | 
|  | 2278 | return NoRecurseAA.isAssumedNoRecurse(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2279 | }; | 
|  | 2280 |  | 
|  | 2281 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) | 
|  | 2282 | return indicatePessimisticFixpoint(); | 
|  | 2283 |  | 
|  | 2284 | return ChangeStatus::UNCHANGED; | 
|  | 2285 | } | 
|  | 2286 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2287 | /// See AbstractAttribute::getAsStr() | 
|  | 2288 | const std::string getAsStr() const override { | 
|  | 2289 | return getAssumed() ? "willreturn" : "may-noreturn"; | 
|  | 2290 | } | 
|  | 2291 | }; | 
|  | 2292 |  | 
|  | 2293 | struct AAWillReturnFunction final : AAWillReturnImpl { | 
|  | 2294 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} | 
|  | 2295 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2296 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2297 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2298 | }; | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2299 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2300 | /// WillReturn attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2301 | struct AAWillReturnCallSite final : AAWillReturnImpl { | 
|  | 2302 | AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} | 
|  | 2303 |  | 
|  | 2304 | /// See AbstractAttribute::initialize(...). | 
|  | 2305 | void initialize(Attributor &A) override { | 
|  | 2306 | AAWillReturnImpl::initialize(A); | 
|  | 2307 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2308 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2309 | indicatePessimisticFixpoint(); | 
|  | 2310 | } | 
|  | 2311 |  | 
|  | 2312 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2313 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2314 | // TODO: Once we have call site specific value information we can provide | 
|  | 2315 | //       call site specific liveness information and then it makes | 
|  | 2316 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2317 | //       redirecting requests to the callee argument. | 
|  | 2318 | Function *F = getAssociatedFunction(); | 
|  | 2319 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 2320 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); | 
|  | 2321 | return clampStateAndIndicateChange( | 
|  | 2322 | getState(), | 
|  | 2323 | static_cast<const AAWillReturn::StateType &>(FnAA.getState())); | 
|  | 2324 | } | 
|  | 2325 |  | 
|  | 2326 | /// See AbstractAttribute::trackStatistics() | 
|  | 2327 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } | 
|  | 2328 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2329 |  | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2330 | /// -------------------AAReachability Attribute-------------------------- | 
|  | 2331 |  | 
|  | 2332 | struct AAReachabilityImpl : AAReachability { | 
|  | 2333 | AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {} | 
|  | 2334 |  | 
|  | 2335 | const std::string getAsStr() const override { | 
|  | 2336 | // TODO: Return the number of reachable queries. | 
|  | 2337 | return "reachable"; | 
|  | 2338 | } | 
|  | 2339 |  | 
|  | 2340 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2341 | void initialize(Attributor &A) override { indicatePessimisticFixpoint(); } | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2342 |  | 
|  | 2343 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2344 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2345 | return indicatePessimisticFixpoint(); | 
|  | 2346 | } | 
|  | 2347 | }; | 
|  | 2348 |  | 
|  | 2349 | struct AAReachabilityFunction final : public AAReachabilityImpl { | 
|  | 2350 | AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {} | 
|  | 2351 |  | 
|  | 2352 | /// See AbstractAttribute::trackStatistics() | 
|  | 2353 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); } | 
|  | 2354 | }; | 
|  | 2355 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2356 | /// ------------------------ NoAlias Argument Attribute ------------------------ | 
|  | 2357 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2358 | struct AANoAliasImpl : AANoAlias { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2359 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2360 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2361 | const std::string getAsStr() const override { | 
|  | 2362 | return getAssumed() ? "noalias" : "may-alias"; | 
|  | 2363 | } | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2364 | }; | 
|  | 2365 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2366 | /// NoAlias attribute for a floating value. | 
|  | 2367 | struct AANoAliasFloating final : AANoAliasImpl { | 
|  | 2368 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2369 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2370 | /// See AbstractAttribute::initialize(...). | 
|  | 2371 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2372 | AANoAliasImpl::initialize(A); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2373 | Value &Val = getAssociatedValue(); | 
|  | 2374 | if (isa<AllocaInst>(Val)) | 
|  | 2375 | indicateOptimisticFixpoint(); | 
|  | 2376 | if (isa<ConstantPointerNull>(Val) && | 
|  | 2377 | Val.getType()->getPointerAddressSpace() == 0) | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2378 | indicateOptimisticFixpoint(); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2379 | } | 
|  | 2380 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2381 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2382 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2383 | // TODO: Implement this. | 
|  | 2384 | return indicatePessimisticFixpoint(); | 
|  | 2385 | } | 
|  | 2386 |  | 
|  | 2387 | /// See AbstractAttribute::trackStatistics() | 
|  | 2388 | void trackStatistics() const override { | 
|  | 2389 | STATS_DECLTRACK_FLOATING_ATTR(noalias) | 
|  | 2390 | } | 
|  | 2391 | }; | 
|  | 2392 |  | 
|  | 2393 | /// NoAlias attribute for an argument. | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2394 | struct AANoAliasArgument final | 
|  | 2395 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2396 | using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>; | 
|  | 2397 | AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {} | 
|  | 2398 |  | 
|  | 2399 | /// See AbstractAttribute::update(...). | 
|  | 2400 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2401 | // We have to make sure no-alias on the argument does not break | 
|  | 2402 | // synchronization when this is a callback argument, see also [1] below. | 
|  | 2403 | // If synchronization cannot be affected, we delegate to the base updateImpl | 
|  | 2404 | // function, otherwise we give up for now. | 
|  | 2405 |  | 
|  | 2406 | // If the function is no-sync, no-alias cannot break synchronization. | 
|  | 2407 | const auto &NoSyncAA = A.getAAFor<AANoSync>( | 
|  | 2408 | *this, IRPosition::function_scope(getIRPosition())); | 
|  | 2409 | if (NoSyncAA.isAssumedNoSync()) | 
|  | 2410 | return Base::updateImpl(A); | 
|  | 2411 |  | 
|  | 2412 | // If the argument is read-only, no-alias cannot break synchronization. | 
|  | 2413 | const auto &MemBehaviorAA = | 
|  | 2414 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); | 
|  | 2415 | if (MemBehaviorAA.isAssumedReadOnly()) | 
|  | 2416 | return Base::updateImpl(A); | 
|  | 2417 |  | 
|  | 2418 | // If the argument is never passed through callbacks, no-alias cannot break | 
|  | 2419 | // synchronization. | 
|  | 2420 | if (A.checkForAllCallSites( | 
|  | 2421 | [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this, | 
|  | 2422 | true)) | 
|  | 2423 | return Base::updateImpl(A); | 
|  | 2424 |  | 
|  | 2425 | // TODO: add no-alias but make sure it doesn't break synchronization by | 
|  | 2426 | // introducing fake uses. See: | 
|  | 2427 | // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel, | 
|  | 2428 | //     International Workshop on OpenMP 2018, | 
|  | 2429 | //     http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf | 
|  | 2430 |  | 
|  | 2431 | return indicatePessimisticFixpoint(); | 
|  | 2432 | } | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2433 |  | 
|  | 2434 | /// See AbstractAttribute::trackStatistics() | 
|  | 2435 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } | 
|  | 2436 | }; | 
|  | 2437 |  | 
|  | 2438 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { | 
|  | 2439 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2440 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2441 | /// See AbstractAttribute::initialize(...). | 
|  | 2442 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 2443 | // See callsite argument attribute and callee argument attribute. | 
|  | 2444 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 2445 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) | 
|  | 2446 | indicateOptimisticFixpoint(); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2447 | } | 
|  | 2448 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2449 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2450 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2451 | // We can deduce "noalias" if the following conditions hold. | 
|  | 2452 | // (i)   Associated value is assumed to be noalias in the definition. | 
|  | 2453 | // (ii)  Associated value is assumed to be no-capture in all the uses | 
|  | 2454 | //       possibly executed before this callsite. | 
|  | 2455 | // (iii) There is no other pointer argument which could alias with the | 
|  | 2456 | //       value. | 
|  | 2457 |  | 
|  | 2458 | const Value &V = getAssociatedValue(); | 
|  | 2459 | const IRPosition IRP = IRPosition::value(V); | 
|  | 2460 |  | 
|  | 2461 | // (i) Check whether noalias holds in the definition. | 
|  | 2462 |  | 
|  | 2463 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 2464 | LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] check definition: " << V | 
|  | 2465 | << " :: " << NoAliasAA << "\n"); | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2466 |  | 
|  | 2467 | if (!NoAliasAA.isAssumedNoAlias()) | 
|  | 2468 | return indicatePessimisticFixpoint(); | 
|  | 2469 |  | 
|  | 2470 | LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V | 
|  | 2471 | << " is assumed NoAlias in the definition\n"); | 
|  | 2472 |  | 
|  | 2473 | // (ii) Check whether the value is captured in the scope using AANoCapture. | 
|  | 2474 | //      FIXME: This is conservative though, it is better to look at CFG and | 
|  | 2475 | //             check only uses possibly executed before this callsite. | 
|  | 2476 |  | 
|  | 2477 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2478 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 2479 | LLVM_DEBUG( | 
|  | 2480 | dbgs() << "[Attributor][AANoAliasCSArg] " << V | 
|  | 2481 | << " cannot be noalias as it is potentially captured\n"); | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2482 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2483 | } | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2484 |  | 
|  | 2485 | // (iii) Check there is no other pointer argument which could alias with the | 
|  | 2486 | // value. | 
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2487 | // TODO: AbstractCallSite | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2488 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 2489 | for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) { | 
|  | 2490 | if (getArgNo() == (int)i) | 
|  | 2491 | continue; | 
|  | 2492 | const Value *ArgOp = ICS.getArgOperand(i); | 
|  | 2493 | if (!ArgOp->getType()->isPointerTy()) | 
|  | 2494 | continue; | 
|  | 2495 |  | 
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2496 | if (const Function *F = getAnchorScope()) { | 
|  | 2497 | if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) { | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 2498 | bool IsAliasing = !AAR->isNoAlias(&getAssociatedValue(), ArgOp); | 
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2499 | LLVM_DEBUG(dbgs() | 
|  | 2500 | << "[Attributor][NoAliasCSArg] Check alias between " | 
|  | 2501 | "callsite arguments " | 
|  | 2502 | << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " " | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2503 | << getAssociatedValue() << " " << *ArgOp << " => " | 
|  | 2504 | << (IsAliasing ? "" : "no-") << "alias \n"); | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2505 |  | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 2506 | if (!IsAliasing) | 
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2507 | continue; | 
|  | 2508 | } | 
|  | 2509 | } | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2510 | return indicatePessimisticFixpoint(); | 
|  | 2511 | } | 
|  | 2512 |  | 
|  | 2513 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2514 | } | 
|  | 2515 |  | 
|  | 2516 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2517 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2518 | }; | 
|  | 2519 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2520 | /// NoAlias attribute for function return value. | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2521 | struct AANoAliasReturned final : AANoAliasImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2522 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2523 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2524 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2525 | virtual ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2526 |  | 
|  | 2527 | auto CheckReturnValue = [&](Value &RV) -> bool { | 
|  | 2528 | if (Constant *C = dyn_cast<Constant>(&RV)) | 
|  | 2529 | if (C->isNullValue() || isa<UndefValue>(C)) | 
|  | 2530 | return true; | 
|  | 2531 |  | 
|  | 2532 | /// For now, we can only deduce noalias if we have call sites. | 
|  | 2533 | /// FIXME: add more support. | 
|  | 2534 | ImmutableCallSite ICS(&RV); | 
|  | 2535 | if (!ICS) | 
|  | 2536 | return false; | 
|  | 2537 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2538 | const IRPosition &RVPos = IRPosition::value(RV); | 
|  | 2539 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2540 | if (!NoAliasAA.isAssumedNoAlias()) | 
|  | 2541 | return false; | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2542 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2543 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); | 
|  | 2544 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2545 | }; | 
|  | 2546 |  | 
|  | 2547 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) | 
|  | 2548 | return indicatePessimisticFixpoint(); | 
|  | 2549 |  | 
|  | 2550 | return ChangeStatus::UNCHANGED; | 
|  | 2551 | } | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2552 |  | 
|  | 2553 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2554 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2555 | }; | 
|  | 2556 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2557 | /// NoAlias attribute deduction for a call site return value. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2558 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { | 
|  | 2559 | AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2560 |  | 
|  | 2561 | /// See AbstractAttribute::initialize(...). | 
|  | 2562 | void initialize(Attributor &A) override { | 
|  | 2563 | AANoAliasImpl::initialize(A); | 
|  | 2564 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2565 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2566 | indicatePessimisticFixpoint(); | 
|  | 2567 | } | 
|  | 2568 |  | 
|  | 2569 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2570 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2571 | // TODO: Once we have call site specific value information we can provide | 
|  | 2572 | //       call site specific liveness information and then it makes | 
|  | 2573 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2574 | //       redirecting requests to the callee argument. | 
|  | 2575 | Function *F = getAssociatedFunction(); | 
|  | 2576 | const IRPosition &FnPos = IRPosition::returned(*F); | 
|  | 2577 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); | 
|  | 2578 | return clampStateAndIndicateChange( | 
|  | 2579 | getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); | 
|  | 2580 | } | 
|  | 2581 |  | 
|  | 2582 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2583 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2584 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2585 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2586 | /// -------------------AAIsDead Function Attribute----------------------- | 
|  | 2587 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2588 | struct AAIsDeadValueImpl : public AAIsDead { | 
|  | 2589 | AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2590 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2591 | /// See AAIsDead::isAssumedDead(). | 
|  | 2592 | bool isAssumedDead() const override { return getAssumed(); } | 
|  | 2593 |  | 
|  | 2594 | /// See AAIsDead::isAssumedDead(BasicBlock *). | 
|  | 2595 | bool isAssumedDead(const BasicBlock *BB) const override { return false; } | 
|  | 2596 |  | 
|  | 2597 | /// See AAIsDead::isKnownDead(BasicBlock *). | 
|  | 2598 | bool isKnownDead(const BasicBlock *BB) const override { return false; } | 
|  | 2599 |  | 
|  | 2600 | /// See AAIsDead::isAssumedDead(Instruction *I). | 
|  | 2601 | bool isAssumedDead(const Instruction *I) const override { | 
|  | 2602 | return I == getCtxI() && isAssumedDead(); | 
|  | 2603 | } | 
|  | 2604 |  | 
|  | 2605 | /// See AAIsDead::isKnownDead(Instruction *I). | 
|  | 2606 | bool isKnownDead(const Instruction *I) const override { | 
|  | 2607 | return I == getCtxI() && getKnown(); | 
|  | 2608 | } | 
|  | 2609 |  | 
|  | 2610 | /// See AbstractAttribute::getAsStr(). | 
|  | 2611 | const std::string getAsStr() const override { | 
|  | 2612 | return isAssumedDead() ? "assumed-dead" : "assumed-live"; | 
|  | 2613 | } | 
|  | 2614 | }; | 
|  | 2615 |  | 
|  | 2616 | struct AAIsDeadFloating : public AAIsDeadValueImpl { | 
|  | 2617 | AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2618 |  | 
|  | 2619 | /// See AbstractAttribute::initialize(...). | 
|  | 2620 | void initialize(Attributor &A) override { | 
|  | 2621 | if (Instruction *I = dyn_cast<Instruction>(&getAssociatedValue())) | 
|  | 2622 | if (!wouldInstructionBeTriviallyDead(I)) | 
|  | 2623 | indicatePessimisticFixpoint(); | 
|  | 2624 | if (isa<UndefValue>(getAssociatedValue())) | 
|  | 2625 | indicatePessimisticFixpoint(); | 
|  | 2626 | } | 
|  | 2627 |  | 
|  | 2628 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2629 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2630 | auto UsePred = [&](const Use &U, bool &Follow) { | 
|  | 2631 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
|  | 2632 | if (CallSite CS = CallSite(UserI)) { | 
|  | 2633 | if (!CS.isArgOperand(&U)) | 
|  | 2634 | return false; | 
|  | 2635 | const IRPosition &CSArgPos = | 
|  | 2636 | IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); | 
|  | 2637 | const auto &CSArgIsDead = A.getAAFor<AAIsDead>(*this, CSArgPos); | 
|  | 2638 | return CSArgIsDead.isAssumedDead(); | 
|  | 2639 | } | 
|  | 2640 | if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { | 
|  | 2641 | const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); | 
|  | 2642 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, RetPos); | 
|  | 2643 | return RetIsDeadAA.isAssumedDead(); | 
|  | 2644 | } | 
|  | 2645 | Follow = true; | 
|  | 2646 | return wouldInstructionBeTriviallyDead(UserI); | 
|  | 2647 | }; | 
|  | 2648 |  | 
|  | 2649 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) | 
|  | 2650 | return indicatePessimisticFixpoint(); | 
|  | 2651 | return ChangeStatus::UNCHANGED; | 
|  | 2652 | } | 
|  | 2653 |  | 
|  | 2654 | /// See AbstractAttribute::manifest(...). | 
|  | 2655 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2656 | Value &V = getAssociatedValue(); | 
|  | 2657 | if (auto *I = dyn_cast<Instruction>(&V)) | 
|  | 2658 | if (wouldInstructionBeTriviallyDead(I)) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2659 | A.deleteAfterManifest(*I); | 
|  | 2660 | return ChangeStatus::CHANGED; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2661 | } | 
|  | 2662 |  | 
|  | 2663 | if (V.use_empty()) | 
|  | 2664 | return ChangeStatus::UNCHANGED; | 
|  | 2665 |  | 
|  | 2666 | UndefValue &UV = *UndefValue::get(V.getType()); | 
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 2667 | bool AnyChange = A.changeValueAfterManifest(V, UV); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2668 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
|  | 2669 | } | 
|  | 2670 |  | 
|  | 2671 | /// See AbstractAttribute::trackStatistics() | 
|  | 2672 | void trackStatistics() const override { | 
|  | 2673 | STATS_DECLTRACK_FLOATING_ATTR(IsDead) | 
|  | 2674 | } | 
|  | 2675 | }; | 
|  | 2676 |  | 
|  | 2677 | struct AAIsDeadArgument : public AAIsDeadFloating { | 
|  | 2678 | AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} | 
|  | 2679 |  | 
|  | 2680 | /// See AbstractAttribute::initialize(...). | 
|  | 2681 | void initialize(Attributor &A) override { | 
|  | 2682 | if (!getAssociatedFunction()->hasExactDefinition()) | 
|  | 2683 | indicatePessimisticFixpoint(); | 
|  | 2684 | } | 
|  | 2685 |  | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 2686 | /// See AbstractAttribute::manifest(...). | 
|  | 2687 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2688 | ChangeStatus Changed = AAIsDeadFloating::manifest(A); | 
|  | 2689 | Argument &Arg = *getAssociatedArgument(); | 
|  | 2690 | if (Arg.getParent()->hasLocalLinkage()) | 
|  | 2691 | if (A.registerFunctionSignatureRewrite( | 
|  | 2692 | Arg, /* ReplacementTypes */ {}, | 
|  | 2693 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{}, | 
|  | 2694 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) | 
|  | 2695 | return ChangeStatus::CHANGED; | 
|  | 2696 | return Changed; | 
|  | 2697 | } | 
|  | 2698 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2699 | /// See AbstractAttribute::trackStatistics() | 
|  | 2700 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } | 
|  | 2701 | }; | 
|  | 2702 |  | 
|  | 2703 | struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { | 
|  | 2704 | AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2705 |  | 
|  | 2706 | /// See AbstractAttribute::initialize(...). | 
|  | 2707 | void initialize(Attributor &A) override { | 
|  | 2708 | if (isa<UndefValue>(getAssociatedValue())) | 
|  | 2709 | indicatePessimisticFixpoint(); | 
|  | 2710 | } | 
|  | 2711 |  | 
|  | 2712 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2713 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2714 | // TODO: Once we have call site specific value information we can provide | 
|  | 2715 | //       call site specific liveness information and then it makes | 
|  | 2716 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2717 | //       redirecting requests to the callee argument. | 
|  | 2718 | Argument *Arg = getAssociatedArgument(); | 
|  | 2719 | if (!Arg) | 
|  | 2720 | return indicatePessimisticFixpoint(); | 
|  | 2721 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 2722 | auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); | 
|  | 2723 | return clampStateAndIndicateChange( | 
|  | 2724 | getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); | 
|  | 2725 | } | 
|  | 2726 |  | 
|  | 2727 | /// See AbstractAttribute::manifest(...). | 
|  | 2728 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2729 | CallBase &CB = cast<CallBase>(getAnchorValue()); | 
|  | 2730 | Use &U = CB.getArgOperandUse(getArgNo()); | 
|  | 2731 | assert(!isa<UndefValue>(U.get()) && | 
|  | 2732 | "Expected undef values to be filtered out!"); | 
|  | 2733 | UndefValue &UV = *UndefValue::get(U->getType()); | 
|  | 2734 | if (A.changeUseAfterManifest(U, UV)) | 
|  | 2735 | return ChangeStatus::CHANGED; | 
|  | 2736 | return ChangeStatus::UNCHANGED; | 
|  | 2737 | } | 
|  | 2738 |  | 
|  | 2739 | /// See AbstractAttribute::trackStatistics() | 
|  | 2740 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } | 
|  | 2741 | }; | 
|  | 2742 |  | 
|  | 2743 | struct AAIsDeadReturned : public AAIsDeadValueImpl { | 
|  | 2744 | AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2745 |  | 
|  | 2746 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2747 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2748 |  | 
|  | 2749 | auto PredForCallSite = [&](AbstractCallSite ACS) { | 
|  | 2750 | if (ACS.isCallbackCall()) | 
|  | 2751 | return false; | 
|  | 2752 | const IRPosition &CSRetPos = | 
|  | 2753 | IRPosition::callsite_returned(ACS.getCallSite()); | 
|  | 2754 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, CSRetPos); | 
|  | 2755 | return RetIsDeadAA.isAssumedDead(); | 
|  | 2756 | }; | 
|  | 2757 |  | 
|  | 2758 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) | 
|  | 2759 | return indicatePessimisticFixpoint(); | 
|  | 2760 |  | 
|  | 2761 | return ChangeStatus::UNCHANGED; | 
|  | 2762 | } | 
|  | 2763 |  | 
|  | 2764 | /// See AbstractAttribute::manifest(...). | 
|  | 2765 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2766 | // TODO: Rewrite the signature to return void? | 
|  | 2767 | bool AnyChange = false; | 
|  | 2768 | UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); | 
|  | 2769 | auto RetInstPred = [&](Instruction &I) { | 
|  | 2770 | ReturnInst &RI = cast<ReturnInst>(I); | 
|  | 2771 | if (!isa<UndefValue>(RI.getReturnValue())) | 
|  | 2772 | AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); | 
|  | 2773 | return true; | 
|  | 2774 | }; | 
|  | 2775 | A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); | 
|  | 2776 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
|  | 2777 | } | 
|  | 2778 |  | 
|  | 2779 | /// See AbstractAttribute::trackStatistics() | 
|  | 2780 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } | 
|  | 2781 | }; | 
|  | 2782 |  | 
|  | 2783 | struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { | 
|  | 2784 | AAIsDeadCallSiteReturned(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} | 
|  | 2785 |  | 
|  | 2786 | /// See AbstractAttribute::initialize(...). | 
|  | 2787 | void initialize(Attributor &A) override {} | 
|  | 2788 |  | 
|  | 2789 | /// See AbstractAttribute::trackStatistics() | 
|  | 2790 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(IsDead) } | 
|  | 2791 | }; | 
|  | 2792 |  | 
|  | 2793 | struct AAIsDeadFunction : public AAIsDead { | 
|  | 2794 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} | 
|  | 2795 |  | 
|  | 2796 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2797 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2798 | const Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2799 | if (F && !F->isDeclaration()) { | 
|  | 2800 | ToBeExploredFrom.insert(&F->getEntryBlock().front()); | 
|  | 2801 | assumeLive(A, F->getEntryBlock()); | 
|  | 2802 | } | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2803 | } | 
|  | 2804 |  | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2805 | /// See AbstractAttribute::getAsStr(). | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2806 | const std::string getAsStr() const override { | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2807 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2808 | std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2809 | std::to_string(ToBeExploredFrom.size()) + "][#KDE " + | 
|  | 2810 | std::to_string(KnownDeadEnds.size()) + "]"; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2811 | } | 
|  | 2812 |  | 
|  | 2813 | /// See AbstractAttribute::manifest(...). | 
|  | 2814 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2815 | assert(getState().isValidState() && | 
|  | 2816 | "Attempted to manifest an invalid state!"); | 
|  | 2817 |  | 
|  | 2818 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2819 | Function &F = *getAssociatedFunction(); | 
|  | 2820 |  | 
|  | 2821 | if (AssumedLiveBlocks.empty()) { | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2822 | A.deleteAfterManifest(F); | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2823 | return ChangeStatus::CHANGED; | 
|  | 2824 | } | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2825 |  | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2826 | // Flag to determine if we can change an invoke to a call assuming the | 
|  | 2827 | // callee is nounwind. This is not possible if the personality of the | 
|  | 2828 | // function allows to catch asynchronous exceptions. | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2829 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2830 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2831 | KnownDeadEnds.set_union(ToBeExploredFrom); | 
|  | 2832 | for (const Instruction *DeadEndI : KnownDeadEnds) { | 
|  | 2833 | auto *CB = dyn_cast<CallBase>(DeadEndI); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2834 | if (!CB) | 
|  | 2835 | continue; | 
|  | 2836 | const auto &NoReturnAA = | 
|  | 2837 | A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); | 
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2838 | bool MayReturn = !NoReturnAA.isAssumedNoReturn(); | 
|  | 2839 | if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2840 | continue; | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2841 | Instruction *I = const_cast<Instruction *>(DeadEndI); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2842 | BasicBlock *BB = I->getParent(); | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2843 | Instruction *SplitPos = I->getNextNode(); | 
| Johannes Doerfert | d410805 | 2019-08-21 20:56:41 +0000 | [diff] [blame] | 2844 | // TODO: mark stuff before unreachable instructions as dead. | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2845 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2846 | if (auto *II = dyn_cast<InvokeInst>(I)) { | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2847 | // If we keep the invoke the split position is at the beginning of the | 
|  | 2848 | // normal desitination block (it invokes a noreturn function after all). | 
|  | 2849 | BasicBlock *NormalDestBB = II->getNormalDest(); | 
|  | 2850 | SplitPos = &NormalDestBB->front(); | 
|  | 2851 |  | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2852 | /// Invoke is replaced with a call and unreachable is placed after it if | 
|  | 2853 | /// the callee is nounwind and noreturn. Otherwise, we keep the invoke | 
|  | 2854 | /// and only place an unreachable in the normal successor. | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2855 | if (Invoke2CallAllowed) { | 
| Michael Liao | a99086d | 2019-08-20 21:02:31 +0000 | [diff] [blame] | 2856 | if (II->getCalledFunction()) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2857 | const IRPosition &IPos = IRPosition::callsite_function(*II); | 
|  | 2858 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); | 
|  | 2859 | if (AANoUnw.isAssumedNoUnwind()) { | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2860 | LLVM_DEBUG(dbgs() | 
|  | 2861 | << "[AAIsDead] Replace invoke with call inst\n"); | 
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2862 | CallInst *CI = createCallMatchingInvoke(II); | 
|  | 2863 | CI->insertBefore(II); | 
|  | 2864 | CI->takeName(II); | 
| Johannes Doerfert | 5d34602 | 2019-12-13 22:11:42 -0600 | [diff] [blame] | 2865 | replaceAllInstructionUsesWith(*II, *CI); | 
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2866 |  | 
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2867 | // If this is a nounwind + mayreturn invoke we only remove the | 
|  | 2868 | // unwind edge. This is done by moving the invoke into a new and | 
|  | 2869 | // dead block and connecting the normal destination of the invoke | 
|  | 2870 | // with a branch that follows the call replacement we created | 
|  | 2871 | // above. | 
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2872 | if (MayReturn) { | 
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2873 | BasicBlock *NewDeadBB = | 
|  | 2874 | SplitBlock(BB, II, nullptr, nullptr, nullptr, ".i2c"); | 
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2875 | assert(isa<BranchInst>(BB->getTerminator()) && | 
|  | 2876 | BB->getTerminator()->getNumSuccessors() == 1 && | 
|  | 2877 | BB->getTerminator()->getSuccessor(0) == NewDeadBB); | 
|  | 2878 | new UnreachableInst(I->getContext(), NewDeadBB); | 
|  | 2879 | BB->getTerminator()->setOperand(0, NormalDestBB); | 
|  | 2880 | A.deleteAfterManifest(*II); | 
|  | 2881 | continue; | 
|  | 2882 | } | 
|  | 2883 |  | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2884 | // We do not need an invoke (II) but instead want a call followed | 
|  | 2885 | // by an unreachable. However, we do not remove II as other | 
|  | 2886 | // abstract attributes might have it cached as part of their | 
|  | 2887 | // results. Given that we modify the CFG anyway, we simply keep II | 
|  | 2888 | // around but in a new dead block. To avoid II being live through | 
|  | 2889 | // a different edge we have to ensure the block we place it in is | 
|  | 2890 | // only reached from the current block of II and then not reached | 
|  | 2891 | // at all when we insert the unreachable. | 
|  | 2892 | SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2893 | SplitPos = CI->getNextNode(); | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2894 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2895 | } | 
|  | 2896 | } | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2897 |  | 
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2898 | if (SplitPos == &NormalDestBB->front()) { | 
|  | 2899 | // If this is an invoke of a noreturn function the edge to the normal | 
|  | 2900 | // destination block is dead but not necessarily the block itself. | 
|  | 2901 | // TODO: We need to move to an edge based system during deduction and | 
|  | 2902 | //       also manifest. | 
|  | 2903 | assert(!NormalDestBB->isLandingPad() && | 
|  | 2904 | "Expected the normal destination not to be a landingpad!"); | 
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2905 | if (NormalDestBB->getUniquePredecessor() == BB) { | 
|  | 2906 | assumeLive(A, *NormalDestBB); | 
|  | 2907 | } else { | 
|  | 2908 | BasicBlock *SplitBB = | 
|  | 2909 | SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); | 
|  | 2910 | // The split block is live even if it contains only an unreachable | 
|  | 2911 | // instruction at the end. | 
|  | 2912 | assumeLive(A, *SplitBB); | 
|  | 2913 | SplitPos = SplitBB->getTerminator(); | 
|  | 2914 | HasChanged = ChangeStatus::CHANGED; | 
|  | 2915 | } | 
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2916 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2917 | } | 
|  | 2918 |  | 
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2919 | if (isa_and_nonnull<UnreachableInst>(SplitPos)) | 
|  | 2920 | continue; | 
|  | 2921 |  | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2922 | BB = SplitPos->getParent(); | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2923 | SplitBlock(BB, SplitPos); | 
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 2924 | A.changeToUnreachableAfterManifest(BB->getTerminator()); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2925 | HasChanged = ChangeStatus::CHANGED; | 
|  | 2926 | } | 
|  | 2927 |  | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2928 | for (BasicBlock &BB : F) | 
|  | 2929 | if (!AssumedLiveBlocks.count(&BB)) | 
|  | 2930 | A.deleteAfterManifest(BB); | 
|  | 2931 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2932 | return HasChanged; | 
|  | 2933 | } | 
|  | 2934 |  | 
|  | 2935 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2936 | ChangeStatus updateImpl(Attributor &A) override; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2937 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2938 | /// See AbstractAttribute::trackStatistics() | 
|  | 2939 | void trackStatistics() const override {} | 
|  | 2940 |  | 
|  | 2941 | /// Returns true if the function is assumed dead. | 
|  | 2942 | bool isAssumedDead() const override { return false; } | 
|  | 2943 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2944 | /// See AAIsDead::isAssumedDead(BasicBlock *). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2945 | bool isAssumedDead(const BasicBlock *BB) const override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2946 | assert(BB->getParent() == getAssociatedFunction() && | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2947 | "BB must be in the same anchor scope function."); | 
|  | 2948 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2949 | if (!getAssumed()) | 
|  | 2950 | return false; | 
|  | 2951 | return !AssumedLiveBlocks.count(BB); | 
|  | 2952 | } | 
|  | 2953 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2954 | /// See AAIsDead::isKnownDead(BasicBlock *). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2955 | bool isKnownDead(const BasicBlock *BB) const override { | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2956 | return getKnown() && isAssumedDead(BB); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2957 | } | 
|  | 2958 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2959 | /// See AAIsDead::isAssumed(Instruction *I). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2960 | bool isAssumedDead(const Instruction *I) const override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2961 | assert(I->getParent()->getParent() == getAssociatedFunction() && | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2962 | "Instruction must be in the same anchor scope function."); | 
|  | 2963 |  | 
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 2964 | if (!getAssumed()) | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2965 | return false; | 
|  | 2966 |  | 
|  | 2967 | // If it is not in AssumedLiveBlocks then it for sure dead. | 
|  | 2968 | // Otherwise, it can still be after noreturn call in a live block. | 
|  | 2969 | if (!AssumedLiveBlocks.count(I->getParent())) | 
|  | 2970 | return true; | 
|  | 2971 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2972 | // If it is not after a liveness barrier it is live. | 
|  | 2973 | const Instruction *PrevI = I->getPrevNode(); | 
|  | 2974 | while (PrevI) { | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2975 | if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2976 | return true; | 
|  | 2977 | PrevI = PrevI->getPrevNode(); | 
|  | 2978 | } | 
|  | 2979 | return false; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2980 | } | 
|  | 2981 |  | 
|  | 2982 | /// See AAIsDead::isKnownDead(Instruction *I). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2983 | bool isKnownDead(const Instruction *I) const override { | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2984 | return getKnown() && isAssumedDead(I); | 
|  | 2985 | } | 
|  | 2986 |  | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2987 | /// Determine if \p F might catch asynchronous exceptions. | 
|  | 2988 | static bool mayCatchAsynchronousExceptions(const Function &F) { | 
|  | 2989 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); | 
|  | 2990 | } | 
|  | 2991 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2992 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A | 
|  | 2993 | /// that internal function called from \p BB should now be looked at. | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2994 | bool assumeLive(Attributor &A, const BasicBlock &BB) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2995 | if (!AssumedLiveBlocks.insert(&BB).second) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2996 | return false; | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2997 |  | 
|  | 2998 | // We assume that all of BB is (probably) live now and if there are calls to | 
|  | 2999 | // internal functions we will assume that those are now live as well. This | 
|  | 3000 | // is a performance optimization for blocks with calls to a lot of internal | 
|  | 3001 | // functions. It can however cause dead functions to be treated as live. | 
|  | 3002 | for (const Instruction &I : BB) | 
|  | 3003 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) | 
|  | 3004 | if (const Function *F = ICS.getCalledFunction()) | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 3005 | if (F->hasLocalLinkage()) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3006 | A.markLiveInternalFunction(*F); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3007 | return true; | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 3008 | } | 
|  | 3009 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3010 | /// Collection of instructions that need to be explored again, e.g., we | 
|  | 3011 | /// did assume they do not transfer control to (one of their) successors. | 
|  | 3012 | SmallSetVector<const Instruction *, 8> ToBeExploredFrom; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3013 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3014 | /// Collection of instructions that are known to not transfer control. | 
|  | 3015 | SmallSetVector<const Instruction *, 8> KnownDeadEnds; | 
|  | 3016 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3017 | /// Collection of all assumed live BasicBlocks. | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3018 | DenseSet<const BasicBlock *> AssumedLiveBlocks; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3019 | }; | 
|  | 3020 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3021 | static bool | 
|  | 3022 | identifyAliveSuccessors(Attributor &A, const CallBase &CB, | 
|  | 3023 | AbstractAttribute &AA, | 
|  | 3024 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 3025 | const IRPosition &IPos = IRPosition::callsite_function(CB); | 
|  | 3026 |  | 
|  | 3027 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); | 
|  | 3028 | if (NoReturnAA.isAssumedNoReturn()) | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3029 | return !NoReturnAA.isKnownNoReturn(); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3030 | if (CB.isTerminator()) | 
|  | 3031 | AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); | 
|  | 3032 | else | 
|  | 3033 | AliveSuccessors.push_back(CB.getNextNode()); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3034 | return false; | 
|  | 3035 | } | 
|  | 3036 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3037 | static bool | 
|  | 3038 | identifyAliveSuccessors(Attributor &A, const InvokeInst &II, | 
|  | 3039 | AbstractAttribute &AA, | 
|  | 3040 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 3041 | bool UsedAssumedInformation = | 
|  | 3042 | identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3043 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3044 | // First, determine if we can change an invoke to a call assuming the | 
|  | 3045 | // callee is nounwind. This is not possible if the personality of the | 
|  | 3046 | // function allows to catch asynchronous exceptions. | 
|  | 3047 | if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { | 
|  | 3048 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | 
|  | 3049 | } else { | 
|  | 3050 | const IRPosition &IPos = IRPosition::callsite_function(II); | 
|  | 3051 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3052 | if (AANoUnw.isAssumedNoUnwind()) { | 
|  | 3053 | UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); | 
|  | 3054 | } else { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3055 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3056 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3057 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3058 | return UsedAssumedInformation; | 
|  | 3059 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3060 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3061 | static Optional<ConstantInt *> | 
|  | 3062 | getAssumedConstant(Attributor &A, const Value &V, AbstractAttribute &AA, | 
|  | 3063 | bool &UsedAssumedInformation) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3064 | const auto &ValueSimplifyAA = | 
|  | 3065 | A.getAAFor<AAValueSimplify>(AA, IRPosition::value(V)); | 
|  | 3066 | Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3067 | UsedAssumedInformation |= !ValueSimplifyAA.isKnown(); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3068 | if (!SimplifiedV.hasValue()) | 
|  | 3069 | return llvm::None; | 
|  | 3070 | if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) | 
|  | 3071 | return llvm::None; | 
|  | 3072 | return dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue()); | 
|  | 3073 | } | 
|  | 3074 |  | 
|  | 3075 | static bool | 
|  | 3076 | identifyAliveSuccessors(Attributor &A, const BranchInst &BI, | 
|  | 3077 | AbstractAttribute &AA, | 
|  | 3078 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 3079 | bool UsedAssumedInformation = false; | 
|  | 3080 | if (BI.getNumSuccessors() == 1) { | 
|  | 3081 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | 
|  | 3082 | } else { | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3083 | Optional<ConstantInt *> CI = | 
|  | 3084 | getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3085 | if (!CI.hasValue()) { | 
|  | 3086 | // No value yet, assume both edges are dead. | 
|  | 3087 | } else if (CI.getValue()) { | 
|  | 3088 | const BasicBlock *SuccBB = | 
|  | 3089 | BI.getSuccessor(1 - CI.getValue()->getZExtValue()); | 
|  | 3090 | AliveSuccessors.push_back(&SuccBB->front()); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3091 | } else { | 
|  | 3092 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | 
|  | 3093 | AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3094 | UsedAssumedInformation = false; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3095 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3096 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3097 | return UsedAssumedInformation; | 
|  | 3098 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3099 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3100 | static bool | 
|  | 3101 | identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, | 
|  | 3102 | AbstractAttribute &AA, | 
|  | 3103 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3104 | bool UsedAssumedInformation = false; | 
|  | 3105 | Optional<ConstantInt *> CI = | 
|  | 3106 | getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3107 | if (!CI.hasValue()) { | 
|  | 3108 | // No value yet, assume all edges are dead. | 
|  | 3109 | } else if (CI.getValue()) { | 
|  | 3110 | for (auto &CaseIt : SI.cases()) { | 
|  | 3111 | if (CaseIt.getCaseValue() == CI.getValue()) { | 
|  | 3112 | AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3113 | return UsedAssumedInformation; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3114 | } | 
|  | 3115 | } | 
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 3116 | AliveSuccessors.push_back(&SI.getDefaultDest()->front()); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3117 | return UsedAssumedInformation; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3118 | } else { | 
|  | 3119 | for (const BasicBlock *SuccBB : successors(SI.getParent())) | 
|  | 3120 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 3121 | } | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3122 | return UsedAssumedInformation; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3123 | } | 
|  | 3124 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3125 | ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3126 | ChangeStatus Change = ChangeStatus::UNCHANGED; | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3127 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3128 | LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" | 
|  | 3129 | << getAssociatedFunction()->size() << "] BBs and " | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3130 | << ToBeExploredFrom.size() << " exploration points and " | 
|  | 3131 | << KnownDeadEnds.size() << " known dead ends\n"); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3132 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3133 | // Copy and clear the list of instructions we need to explore from. It is | 
|  | 3134 | // refilled with instructions the next update has to look at. | 
|  | 3135 | SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), | 
|  | 3136 | ToBeExploredFrom.end()); | 
|  | 3137 | decltype(ToBeExploredFrom) NewToBeExploredFrom; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3138 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3139 | SmallVector<const Instruction *, 8> AliveSuccessors; | 
|  | 3140 | while (!Worklist.empty()) { | 
|  | 3141 | const Instruction *I = Worklist.pop_back_val(); | 
|  | 3142 | LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3143 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3144 | AliveSuccessors.clear(); | 
|  | 3145 |  | 
|  | 3146 | bool UsedAssumedInformation = false; | 
|  | 3147 | switch (I->getOpcode()) { | 
|  | 3148 | // TODO: look for (assumed) UB to backwards propagate "deadness". | 
|  | 3149 | default: | 
|  | 3150 | if (I->isTerminator()) { | 
|  | 3151 | for (const BasicBlock *SuccBB : successors(I->getParent())) | 
|  | 3152 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 3153 | } else { | 
|  | 3154 | AliveSuccessors.push_back(I->getNextNode()); | 
|  | 3155 | } | 
|  | 3156 | break; | 
|  | 3157 | case Instruction::Call: | 
|  | 3158 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), | 
|  | 3159 | *this, AliveSuccessors); | 
|  | 3160 | break; | 
|  | 3161 | case Instruction::Invoke: | 
|  | 3162 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), | 
|  | 3163 | *this, AliveSuccessors); | 
|  | 3164 | break; | 
|  | 3165 | case Instruction::Br: | 
|  | 3166 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), | 
|  | 3167 | *this, AliveSuccessors); | 
|  | 3168 | break; | 
|  | 3169 | case Instruction::Switch: | 
|  | 3170 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), | 
|  | 3171 | *this, AliveSuccessors); | 
|  | 3172 | break; | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3173 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3174 |  | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3175 | if (UsedAssumedInformation) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3176 | NewToBeExploredFrom.insert(I); | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3177 | } else { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3178 | Change = ChangeStatus::CHANGED; | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3179 | if (AliveSuccessors.empty() || | 
|  | 3180 | (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors())) | 
|  | 3181 | KnownDeadEnds.insert(I); | 
|  | 3182 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3183 |  | 
|  | 3184 | LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " | 
|  | 3185 | << AliveSuccessors.size() << " UsedAssumedInformation: " | 
|  | 3186 | << UsedAssumedInformation << "\n"); | 
|  | 3187 |  | 
|  | 3188 | for (const Instruction *AliveSuccessor : AliveSuccessors) { | 
|  | 3189 | if (!I->isTerminator()) { | 
|  | 3190 | assert(AliveSuccessors.size() == 1 && | 
|  | 3191 | "Non-terminator expected to have a single successor!"); | 
|  | 3192 | Worklist.push_back(AliveSuccessor); | 
|  | 3193 | } else { | 
|  | 3194 | if (assumeLive(A, *AliveSuccessor->getParent())) | 
|  | 3195 | Worklist.push_back(AliveSuccessor); | 
|  | 3196 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3197 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3198 | } | 
|  | 3199 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3200 | ToBeExploredFrom = std::move(NewToBeExploredFrom); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3201 |  | 
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 3202 | // 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] | 3203 | // Instead, indicating a pessimistic fixpoint will cause the state to be | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3204 | // "invalid" and all queries to be answered conservatively without lookups. | 
|  | 3205 | // To be in this state we have to (1) finished the exploration and (3) not | 
|  | 3206 | // discovered any non-trivial dead end and (2) not ruled unreachable code | 
|  | 3207 | // dead. | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3208 | if (ToBeExploredFrom.empty() && | 
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3209 | getAssociatedFunction()->size() == AssumedLiveBlocks.size() && | 
|  | 3210 | llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { | 
|  | 3211 | return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; | 
|  | 3212 | })) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3213 | return indicatePessimisticFixpoint(); | 
|  | 3214 | return Change; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3215 | } | 
|  | 3216 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3217 | /// Liveness information for a call sites. | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3218 | struct AAIsDeadCallSite final : AAIsDeadFunction { | 
|  | 3219 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3220 |  | 
|  | 3221 | /// See AbstractAttribute::initialize(...). | 
|  | 3222 | void initialize(Attributor &A) override { | 
|  | 3223 | // TODO: Once we have call site specific value information we can provide | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3224 | //       call site specific liveness information and then it makes | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3225 | //       sense to specialize attributes for call sites instead of | 
|  | 3226 | //       redirecting requests to the callee. | 
|  | 3227 | llvm_unreachable("Abstract attributes for liveness are not " | 
|  | 3228 | "supported for call sites yet!"); | 
|  | 3229 | } | 
|  | 3230 |  | 
|  | 3231 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3232 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3233 | return indicatePessimisticFixpoint(); | 
|  | 3234 | } | 
|  | 3235 |  | 
|  | 3236 | /// See AbstractAttribute::trackStatistics() | 
|  | 3237 | void trackStatistics() const override {} | 
|  | 3238 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3239 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3240 | /// -------------------- Dereferenceable Argument Attribute -------------------- | 
|  | 3241 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3242 | template <> | 
|  | 3243 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, | 
|  | 3244 | const DerefState &R) { | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3245 | ChangeStatus CS0 = | 
|  | 3246 | clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); | 
|  | 3247 | ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3248 | return CS0 | CS1; | 
|  | 3249 | } | 
|  | 3250 |  | 
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 3251 | struct AADereferenceableImpl : AADereferenceable { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3252 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3253 | using StateType = DerefState; | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3254 |  | 
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3255 | void initialize(Attributor &A) override { | 
|  | 3256 | SmallVector<Attribute, 4> Attrs; | 
|  | 3257 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, | 
|  | 3258 | Attrs); | 
|  | 3259 | for (const Attribute &Attr : Attrs) | 
|  | 3260 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); | 
|  | 3261 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3262 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition()); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3263 |  | 
|  | 3264 | const IRPosition &IRP = this->getIRPosition(); | 
|  | 3265 | bool IsFnInterface = IRP.isFnInterfaceKind(); | 
|  | 3266 | const Function *FnScope = IRP.getAnchorScope(); | 
|  | 3267 | if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) | 
|  | 3268 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3269 | } | 
|  | 3270 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3271 | /// See AbstractAttribute::getState() | 
|  | 3272 | /// { | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3273 | StateType &getState() override { return *this; } | 
|  | 3274 | const StateType &getState() const override { return *this; } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3275 | /// } | 
|  | 3276 |  | 
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3277 | /// Helper function for collecting accessed bytes in must-be-executed-context | 
|  | 3278 | void addAccessedBytesForUse(Attributor &A, const Use *U, | 
|  | 3279 | const Instruction *I) { | 
|  | 3280 | const Value *UseV = U->get(); | 
|  | 3281 | if (!UseV->getType()->isPointerTy()) | 
|  | 3282 | return; | 
|  | 3283 |  | 
|  | 3284 | Type *PtrTy = UseV->getType(); | 
|  | 3285 | const DataLayout &DL = A.getDataLayout(); | 
|  | 3286 | int64_t Offset; | 
|  | 3287 | if (const Value *Base = getBasePointerOfAccessPointerOperand( | 
|  | 3288 | I, Offset, DL, /*AllowNonInbounds*/ true)) { | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 3289 | if (Base == &getAssociatedValue() && | 
|  | 3290 | Attributor::getPointerOperand(I, /* AllowVolatile */ false) == UseV) { | 
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3291 | uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType()); | 
|  | 3292 | addAccessedBytes(Offset, Size); | 
|  | 3293 | } | 
|  | 3294 | } | 
|  | 3295 | return; | 
|  | 3296 | } | 
|  | 3297 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3298 | /// See AAFromMustBeExecutedContext | 
|  | 3299 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 3300 | bool IsNonNull = false; | 
|  | 3301 | bool TrackUse = false; | 
|  | 3302 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( | 
|  | 3303 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); | 
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3304 |  | 
|  | 3305 | addAccessedBytesForUse(A, U, I); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3306 | takeKnownDerefBytesMaximum(DerefBytes); | 
|  | 3307 | return TrackUse; | 
|  | 3308 | } | 
|  | 3309 |  | 
| Hideto Ueno | dfedae5 | 2019-11-29 06:45:07 +0000 | [diff] [blame] | 3310 | /// See AbstractAttribute::manifest(...). | 
|  | 3311 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3312 | ChangeStatus Change = AADereferenceable::manifest(A); | 
|  | 3313 | if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { | 
|  | 3314 | removeAttrs({Attribute::DereferenceableOrNull}); | 
|  | 3315 | return ChangeStatus::CHANGED; | 
|  | 3316 | } | 
|  | 3317 | return Change; | 
|  | 3318 | } | 
|  | 3319 |  | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 3320 | void getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3321 | SmallVectorImpl<Attribute> &Attrs) const override { | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3322 | // TODO: Add *_globally support | 
|  | 3323 | if (isAssumedNonNull()) | 
|  | 3324 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( | 
|  | 3325 | Ctx, getAssumedDereferenceableBytes())); | 
|  | 3326 | else | 
|  | 3327 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( | 
|  | 3328 | Ctx, getAssumedDereferenceableBytes())); | 
|  | 3329 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3330 |  | 
|  | 3331 | /// See AbstractAttribute::getAsStr(). | 
|  | 3332 | const std::string getAsStr() const override { | 
|  | 3333 | if (!getAssumedDereferenceableBytes()) | 
|  | 3334 | return "unknown-dereferenceable"; | 
|  | 3335 | return std::string("dereferenceable") + | 
|  | 3336 | (isAssumedNonNull() ? "" : "_or_null") + | 
|  | 3337 | (isAssumedGlobal() ? "_globally" : "") + "<" + | 
|  | 3338 | std::to_string(getKnownDereferenceableBytes()) + "-" + | 
|  | 3339 | std::to_string(getAssumedDereferenceableBytes()) + ">"; | 
|  | 3340 | } | 
|  | 3341 | }; | 
|  | 3342 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3343 | /// Dereferenceable attribute for a floating value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3344 | struct AADereferenceableFloating | 
|  | 3345 | : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { | 
|  | 3346 | using Base = | 
|  | 3347 | AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; | 
|  | 3348 | AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3349 |  | 
|  | 3350 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3351 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3352 | ChangeStatus Change = Base::updateImpl(A); | 
|  | 3353 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3354 | const DataLayout &DL = A.getDataLayout(); | 
|  | 3355 |  | 
|  | 3356 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { | 
|  | 3357 | unsigned IdxWidth = | 
|  | 3358 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); | 
|  | 3359 | APInt Offset(IdxWidth, 0); | 
|  | 3360 | const Value *Base = | 
|  | 3361 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); | 
|  | 3362 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3363 | const auto &AA = | 
|  | 3364 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3365 | int64_t DerefBytes = 0; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3366 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3367 | // Use IR information if we did not strip anything. | 
|  | 3368 | // TODO: track globally. | 
|  | 3369 | bool CanBeNull; | 
|  | 3370 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); | 
|  | 3371 | T.GlobalState.indicatePessimisticFixpoint(); | 
|  | 3372 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3373 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3374 | DerefBytes = DS.DerefBytesState.getAssumed(); | 
|  | 3375 | T.GlobalState &= DS.GlobalState; | 
|  | 3376 | } | 
|  | 3377 |  | 
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3378 | // For now we do not try to "increase" dereferenceability due to negative | 
|  | 3379 | // indices as we first have to come up with code to deal with loops and | 
|  | 3380 | // for overflows of the dereferenceable bytes. | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3381 | int64_t OffsetSExt = Offset.getSExtValue(); | 
|  | 3382 | if (OffsetSExt < 0) | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 3383 | OffsetSExt = 0; | 
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3384 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3385 | T.takeAssumedDerefBytesMinimum( | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3386 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3387 |  | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3388 | if (this == &AA) { | 
|  | 3389 | if (!Stripped) { | 
|  | 3390 | // If nothing was stripped IR information is all we got. | 
|  | 3391 | T.takeKnownDerefBytesMaximum( | 
|  | 3392 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | 
|  | 3393 | T.indicatePessimisticFixpoint(); | 
|  | 3394 | } else if (OffsetSExt > 0) { | 
|  | 3395 | // If something was stripped but there is circular reasoning we look | 
|  | 3396 | // for the offset. If it is positive we basically decrease the | 
|  | 3397 | // dereferenceable bytes in a circluar loop now, which will simply | 
|  | 3398 | // drive them down to the known value in a very slow way which we | 
|  | 3399 | // can accelerate. | 
|  | 3400 | T.indicatePessimisticFixpoint(); | 
|  | 3401 | } | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3402 | } | 
|  | 3403 |  | 
|  | 3404 | return T.isValidState(); | 
|  | 3405 | }; | 
|  | 3406 |  | 
|  | 3407 | DerefState T; | 
|  | 3408 | if (!genericValueTraversal<AADereferenceable, DerefState>( | 
|  | 3409 | A, getIRPosition(), *this, T, VisitValueCB)) | 
|  | 3410 | return indicatePessimisticFixpoint(); | 
|  | 3411 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3412 | return Change | clampStateAndIndicateChange(getState(), T); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3413 | } | 
|  | 3414 |  | 
|  | 3415 | /// See AbstractAttribute::trackStatistics() | 
|  | 3416 | void trackStatistics() const override { | 
|  | 3417 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) | 
|  | 3418 | } | 
|  | 3419 | }; | 
|  | 3420 |  | 
|  | 3421 | /// Dereferenceable attribute for a return value. | 
|  | 3422 | struct AADereferenceableReturned final | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3423 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, | 
|  | 3424 | DerefState> { | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3425 | AADereferenceableReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3426 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, | 
|  | 3427 | DerefState>(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3428 |  | 
|  | 3429 | /// See AbstractAttribute::trackStatistics() | 
|  | 3430 | void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3431 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3432 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3433 | }; | 
|  | 3434 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3435 | /// Dereferenceable attribute for an argument | 
|  | 3436 | struct AADereferenceableArgument final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3437 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< | 
|  | 3438 | AADereferenceable, AADereferenceableImpl, DerefState> { | 
|  | 3439 | using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< | 
|  | 3440 | AADereferenceable, AADereferenceableImpl, DerefState>; | 
|  | 3441 | AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3442 |  | 
|  | 3443 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3444 | void trackStatistics() const override { | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3445 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) | 
|  | 3446 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3447 | }; | 
|  | 3448 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3449 | /// Dereferenceable attribute for a call site argument. | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3450 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3451 | AADereferenceableCallSiteArgument(const IRPosition &IRP) | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3452 | : AADereferenceableFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3453 |  | 
|  | 3454 | /// See AbstractAttribute::trackStatistics() | 
|  | 3455 | void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3456 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3457 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3458 | }; | 
|  | 3459 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3460 | /// Dereferenceable attribute deduction for a call site return value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3461 | struct AADereferenceableCallSiteReturned final | 
|  | 3462 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< | 
|  | 3463 | AADereferenceable, AADereferenceableImpl> { | 
|  | 3464 | using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< | 
|  | 3465 | AADereferenceable, AADereferenceableImpl>; | 
|  | 3466 | AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3467 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3468 | /// See AbstractAttribute::trackStatistics() | 
|  | 3469 | void trackStatistics() const override { | 
|  | 3470 | STATS_DECLTRACK_CS_ATTR(dereferenceable); | 
|  | 3471 | } | 
|  | 3472 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3473 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3474 | // ------------------------ Align Argument Attribute ------------------------ | 
|  | 3475 |  | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3476 | static unsigned int getKnownAlignForUse(Attributor &A, | 
|  | 3477 | AbstractAttribute &QueryingAA, | 
|  | 3478 | Value &AssociatedValue, const Use *U, | 
|  | 3479 | const Instruction *I, bool &TrackUse) { | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3480 | // We need to follow common pointer manipulation uses to the accesses they | 
|  | 3481 | // feed into. | 
|  | 3482 | if (isa<CastInst>(I)) { | 
|  | 3483 | TrackUse = true; | 
|  | 3484 | return 0; | 
|  | 3485 | } | 
|  | 3486 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { | 
|  | 3487 | if (GEP->hasAllConstantIndices()) { | 
|  | 3488 | TrackUse = true; | 
|  | 3489 | return 0; | 
|  | 3490 | } | 
|  | 3491 | } | 
|  | 3492 |  | 
|  | 3493 | unsigned Alignment = 0; | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3494 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { | 
|  | 3495 | if (ICS.isBundleOperand(U) || ICS.isCallee(U)) | 
|  | 3496 | return 0; | 
|  | 3497 |  | 
|  | 3498 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 3499 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); | 
|  | 3500 | // As long as we only use known information there is no need to track | 
|  | 3501 | // dependences here. | 
|  | 3502 | auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, | 
|  | 3503 | /* TrackDependence */ false); | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3504 | Alignment = AlignAA.getKnownAlign(); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3505 | } | 
|  | 3506 |  | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3507 | const Value *UseV = U->get(); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3508 | if (auto *SI = dyn_cast<StoreInst>(I)) | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3509 | Alignment = SI->getAlignment(); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3510 | else if (auto *LI = dyn_cast<LoadInst>(I)) | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3511 | Alignment = LI->getAlignment(); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3512 |  | 
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3513 | if (Alignment <= 1) | 
|  | 3514 | return 0; | 
|  | 3515 |  | 
|  | 3516 | auto &DL = A.getDataLayout(); | 
|  | 3517 | int64_t Offset; | 
|  | 3518 |  | 
|  | 3519 | if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { | 
|  | 3520 | if (Base == &AssociatedValue) { | 
|  | 3521 | // BasePointerAddr + Offset = Alignment * Q for some integer Q. | 
|  | 3522 | // So we can say that the maximum power of two which is a divisor of | 
|  | 3523 | // gcd(Offset, Alignment) is an alignment. | 
|  | 3524 |  | 
|  | 3525 | uint32_t gcd = | 
|  | 3526 | greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); | 
|  | 3527 | Alignment = llvm::PowerOf2Floor(gcd); | 
|  | 3528 | } | 
|  | 3529 | } | 
|  | 3530 |  | 
|  | 3531 | return Alignment; | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3532 | } | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3533 | struct AAAlignImpl : AAAlign { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3534 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3535 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3536 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3537 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3538 | SmallVector<Attribute, 4> Attrs; | 
|  | 3539 | getAttrs({Attribute::Alignment}, Attrs); | 
|  | 3540 | for (const Attribute &Attr : Attrs) | 
|  | 3541 | takeKnownMaximum(Attr.getValueAsInt()); | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 3542 |  | 
|  | 3543 | if (getIRPosition().isFnInterfaceKind() && | 
|  | 3544 | (!getAssociatedFunction() || | 
|  | 3545 | !getAssociatedFunction()->hasExactDefinition())) | 
|  | 3546 | indicatePessimisticFixpoint(); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3547 | } | 
|  | 3548 |  | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3549 | /// See AbstractAttribute::manifest(...). | 
|  | 3550 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3551 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 3552 |  | 
|  | 3553 | // Check for users that allow alignment annotations. | 
|  | 3554 | Value &AnchorVal = getIRPosition().getAnchorValue(); | 
|  | 3555 | for (const Use &U : AnchorVal.uses()) { | 
|  | 3556 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { | 
|  | 3557 | if (SI->getPointerOperand() == &AnchorVal) | 
|  | 3558 | if (SI->getAlignment() < getAssumedAlign()) { | 
|  | 3559 | STATS_DECLTRACK(AAAlign, Store, | 
|  | 3560 | "Number of times alignemnt added to a store"); | 
| Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 3561 | SI->setAlignment(Align(getAssumedAlign())); | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3562 | Changed = ChangeStatus::CHANGED; | 
|  | 3563 | } | 
|  | 3564 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { | 
|  | 3565 | if (LI->getPointerOperand() == &AnchorVal) | 
|  | 3566 | if (LI->getAlignment() < getAssumedAlign()) { | 
| Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 3567 | LI->setAlignment(Align(getAssumedAlign())); | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3568 | STATS_DECLTRACK(AAAlign, Load, | 
|  | 3569 | "Number of times alignemnt added to a load"); | 
|  | 3570 | Changed = ChangeStatus::CHANGED; | 
|  | 3571 | } | 
|  | 3572 | } | 
|  | 3573 | } | 
|  | 3574 |  | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3575 | return AAAlign::manifest(A) | Changed; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3576 | } | 
|  | 3577 |  | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3578 | // TODO: Provide a helper to determine the implied ABI alignment and check in | 
|  | 3579 | //       the existing manifest method and a new one for AAAlignImpl that value | 
|  | 3580 | //       to avoid making the alignment explicit if it did not improve. | 
|  | 3581 |  | 
|  | 3582 | /// See AbstractAttribute::getDeducedAttributes | 
|  | 3583 | virtual void | 
|  | 3584 | getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3585 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 3586 | if (getAssumedAlign() > 1) | 
| Guillaume Chatelet | b65fa48 | 2019-10-15 12:56:24 +0000 | [diff] [blame] | 3587 | Attrs.emplace_back( | 
|  | 3588 | Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3589 | } | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3590 | /// See AAFromMustBeExecutedContext | 
|  | 3591 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 3592 | bool TrackUse = false; | 
|  | 3593 |  | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 3594 | unsigned int KnownAlign = | 
|  | 3595 | getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3596 | takeKnownMaximum(KnownAlign); | 
|  | 3597 |  | 
|  | 3598 | return TrackUse; | 
|  | 3599 | } | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3600 |  | 
|  | 3601 | /// See AbstractAttribute::getAsStr(). | 
|  | 3602 | const std::string getAsStr() const override { | 
|  | 3603 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + | 
|  | 3604 | "-" + std::to_string(getAssumedAlign()) + ">") | 
|  | 3605 | : "unknown-align"; | 
|  | 3606 | } | 
|  | 3607 | }; | 
|  | 3608 |  | 
|  | 3609 | /// Align attribute for a floating value. | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3610 | struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> { | 
|  | 3611 | using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>; | 
|  | 3612 | AAAlignFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3613 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3614 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3615 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3616 | Base::updateImpl(A); | 
|  | 3617 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3618 | const DataLayout &DL = A.getDataLayout(); | 
|  | 3619 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 3620 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, | 
|  | 3621 | bool Stripped) -> bool { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3622 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); | 
|  | 3623 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3624 | // Use only IR information if we did not strip anything. | 
| Guillaume Chatelet | bae629b | 2019-10-15 13:58:22 +0000 | [diff] [blame] | 3625 | const MaybeAlign PA = V.getPointerAlignment(DL); | 
|  | 3626 | T.takeKnownMaximum(PA ? PA->value() : 0); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3627 | T.indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3628 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3629 | // Use abstract attribute information. | 
|  | 3630 | const AAAlign::StateType &DS = | 
|  | 3631 | static_cast<const AAAlign::StateType &>(AA.getState()); | 
|  | 3632 | T ^= DS; | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3633 | } | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 3634 | return T.isValidState(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3635 | }; | 
|  | 3636 |  | 
|  | 3637 | StateType T; | 
|  | 3638 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, | 
|  | 3639 | VisitValueCB)) | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3640 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3641 |  | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 3642 | // TODO: If we know we visited all incoming values, thus no are assumed | 
|  | 3643 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3644 | return clampStateAndIndicateChange(getState(), T); | 
|  | 3645 | } | 
|  | 3646 |  | 
|  | 3647 | /// See AbstractAttribute::trackStatistics() | 
|  | 3648 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } | 
|  | 3649 | }; | 
|  | 3650 |  | 
|  | 3651 | /// Align attribute for function return value. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3652 | struct AAAlignReturned final | 
|  | 3653 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3654 | AAAlignReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3655 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3656 |  | 
|  | 3657 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3658 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3659 | }; | 
|  | 3660 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3661 | /// Align attribute for function argument. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3662 | struct AAAlignArgument final | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3663 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, | 
|  | 3664 | AAAlignImpl> { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3665 | AAAlignArgument(const IRPosition &IRP) | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3666 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, | 
|  | 3667 | AAAlignImpl>( | 
|  | 3668 | IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3669 |  | 
|  | 3670 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3671 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3672 | }; | 
|  | 3673 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3674 | struct AAAlignCallSiteArgument final : AAAlignFloating { | 
|  | 3675 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3676 |  | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3677 | /// See AbstractAttribute::manifest(...). | 
|  | 3678 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3679 | return AAAlignImpl::manifest(A); | 
|  | 3680 | } | 
|  | 3681 |  | 
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 3682 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
|  | 3683 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3684 | ChangeStatus Changed = AAAlignFloating::updateImpl(A); | 
|  | 3685 | if (Argument *Arg = getAssociatedArgument()) { | 
|  | 3686 | const auto &ArgAlignAA = A.getAAFor<AAAlign>( | 
|  | 3687 | *this, IRPosition::argument(*Arg), /* TrackDependence */ false, | 
|  | 3688 | DepClassTy::OPTIONAL); | 
|  | 3689 | takeKnownMaximum(ArgAlignAA.getKnownAlign()); | 
|  | 3690 | } | 
|  | 3691 | return Changed; | 
|  | 3692 | } | 
|  | 3693 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3694 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3695 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3696 | }; | 
|  | 3697 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3698 | /// Align attribute deduction for a call site return value. | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3699 | struct AAAlignCallSiteReturned final | 
|  | 3700 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, | 
|  | 3701 | AAAlignImpl> { | 
|  | 3702 | using Base = | 
|  | 3703 | AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, | 
|  | 3704 | AAAlignImpl>; | 
|  | 3705 | AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3706 |  | 
|  | 3707 | /// See AbstractAttribute::initialize(...). | 
|  | 3708 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3709 | Base::initialize(A); | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3710 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3711 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3712 | indicatePessimisticFixpoint(); | 
|  | 3713 | } | 
|  | 3714 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3715 | /// See AbstractAttribute::trackStatistics() | 
|  | 3716 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } | 
|  | 3717 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3718 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3719 | /// ------------------ Function No-Return Attribute ---------------------------- | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3720 | struct AANoReturnImpl : public AANoReturn { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3721 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3722 |  | 
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3723 | /// See AbstractAttribute::initialize(...). | 
|  | 3724 | void initialize(Attributor &A) override { | 
|  | 3725 | AANoReturn::initialize(A); | 
|  | 3726 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | 1b6041a | 2019-11-01 20:17:48 -0500 | [diff] [blame] | 3727 | if (!F) | 
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3728 | indicatePessimisticFixpoint(); | 
|  | 3729 | } | 
|  | 3730 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3731 | /// See AbstractAttribute::getAsStr(). | 
|  | 3732 | const std::string getAsStr() const override { | 
|  | 3733 | return getAssumed() ? "noreturn" : "may-return"; | 
|  | 3734 | } | 
|  | 3735 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3736 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3737 | virtual ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3738 | auto CheckForNoReturn = [](Instruction &) { return false; }; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3739 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3740 | {(unsigned)Instruction::Ret})) | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3741 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3742 | return ChangeStatus::UNCHANGED; | 
|  | 3743 | } | 
|  | 3744 | }; | 
|  | 3745 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3746 | struct AANoReturnFunction final : AANoReturnImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3747 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3748 |  | 
|  | 3749 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3750 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3751 | }; | 
|  | 3752 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3753 | /// NoReturn attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3754 | struct AANoReturnCallSite final : AANoReturnImpl { | 
|  | 3755 | AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} | 
|  | 3756 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3757 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3758 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3759 | // TODO: Once we have call site specific value information we can provide | 
|  | 3760 | //       call site specific liveness information and then it makes | 
|  | 3761 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 3762 | //       redirecting requests to the callee argument. | 
|  | 3763 | Function *F = getAssociatedFunction(); | 
|  | 3764 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 3765 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); | 
|  | 3766 | return clampStateAndIndicateChange( | 
|  | 3767 | getState(), | 
|  | 3768 | static_cast<const AANoReturn::StateType &>(FnAA.getState())); | 
|  | 3769 | } | 
|  | 3770 |  | 
|  | 3771 | /// See AbstractAttribute::trackStatistics() | 
|  | 3772 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } | 
|  | 3773 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3774 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3775 | /// ----------------------- Variable Capturing --------------------------------- | 
|  | 3776 |  | 
|  | 3777 | /// A class to hold the state of for no-capture attributes. | 
|  | 3778 | struct AANoCaptureImpl : public AANoCapture { | 
|  | 3779 | AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} | 
|  | 3780 |  | 
|  | 3781 | /// See AbstractAttribute::initialize(...). | 
|  | 3782 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3783 | if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { | 
|  | 3784 | indicateOptimisticFixpoint(); | 
|  | 3785 | return; | 
|  | 3786 | } | 
|  | 3787 | Function *AnchorScope = getAnchorScope(); | 
|  | 3788 | if (isFnInterfaceKind() && | 
|  | 3789 | (!AnchorScope || !AnchorScope->hasExactDefinition())) { | 
|  | 3790 | indicatePessimisticFixpoint(); | 
|  | 3791 | return; | 
|  | 3792 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3793 |  | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 3794 | // You cannot "capture" null in the default address space. | 
|  | 3795 | if (isa<ConstantPointerNull>(getAssociatedValue()) && | 
|  | 3796 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { | 
|  | 3797 | indicateOptimisticFixpoint(); | 
|  | 3798 | return; | 
|  | 3799 | } | 
|  | 3800 |  | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3801 | const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3802 |  | 
|  | 3803 | // Check what state the associated function can actually capture. | 
|  | 3804 | if (F) | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3805 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3806 | else | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3807 | indicatePessimisticFixpoint(); | 
|  | 3808 | } | 
|  | 3809 |  | 
|  | 3810 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3811 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 3812 |  | 
|  | 3813 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). | 
|  | 3814 | virtual void | 
|  | 3815 | getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3816 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 3817 | if (!isAssumedNoCaptureMaybeReturned()) | 
|  | 3818 | return; | 
|  | 3819 |  | 
| Hideto Ueno | 3736764 | 2019-09-11 06:52:11 +0000 | [diff] [blame] | 3820 | if (getArgNo() >= 0) { | 
|  | 3821 | if (isAssumedNoCapture()) | 
|  | 3822 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); | 
|  | 3823 | else if (ManifestInternal) | 
|  | 3824 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); | 
|  | 3825 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3826 | } | 
|  | 3827 |  | 
|  | 3828 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known | 
|  | 3829 | /// depending on the ability of the function associated with \p IRP to capture | 
|  | 3830 | /// state in memory and through "returning/throwing", respectively. | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3831 | static void determineFunctionCaptureCapabilities(const IRPosition &IRP, | 
|  | 3832 | const Function &F, | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 3833 | BitIntegerState &State) { | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3834 | // TODO: Once we have memory behavior attributes we should use them here. | 
|  | 3835 |  | 
|  | 3836 | // If we know we cannot communicate or write to memory, we do not care about | 
|  | 3837 | // ptr2int anymore. | 
|  | 3838 | if (F.onlyReadsMemory() && F.doesNotThrow() && | 
|  | 3839 | F.getReturnType()->isVoidTy()) { | 
|  | 3840 | State.addKnownBits(NO_CAPTURE); | 
|  | 3841 | return; | 
|  | 3842 | } | 
|  | 3843 |  | 
|  | 3844 | // A function cannot capture state in memory if it only reads memory, it can | 
|  | 3845 | // however return/throw state and the state might be influenced by the | 
|  | 3846 | // pointer value, e.g., loading from a returned pointer might reveal a bit. | 
|  | 3847 | if (F.onlyReadsMemory()) | 
|  | 3848 | State.addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 3849 |  | 
|  | 3850 | // A function cannot communicate state back if it does not through | 
|  | 3851 | // exceptions and doesn not return values. | 
|  | 3852 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) | 
|  | 3853 | State.addKnownBits(NOT_CAPTURED_IN_RET); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3854 |  | 
|  | 3855 | // Check existing "returned" attributes. | 
|  | 3856 | int ArgNo = IRP.getArgNo(); | 
|  | 3857 | if (F.doesNotThrow() && ArgNo >= 0) { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 3858 | for (unsigned u = 0, e = F.arg_size(); u < e; ++u) | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3859 | if (F.hasParamAttribute(u, Attribute::Returned)) { | 
| Johannes Doerfert | 9d5ad5e | 2019-10-21 01:29:10 +0000 | [diff] [blame] | 3860 | if (u == unsigned(ArgNo)) | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3861 | State.removeAssumedBits(NOT_CAPTURED_IN_RET); | 
|  | 3862 | else if (F.onlyReadsMemory()) | 
|  | 3863 | State.addKnownBits(NO_CAPTURE); | 
|  | 3864 | else | 
|  | 3865 | State.addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 3866 | break; | 
|  | 3867 | } | 
|  | 3868 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3869 | } | 
|  | 3870 |  | 
|  | 3871 | /// See AbstractState::getAsStr(). | 
|  | 3872 | const std::string getAsStr() const override { | 
|  | 3873 | if (isKnownNoCapture()) | 
|  | 3874 | return "known not-captured"; | 
|  | 3875 | if (isAssumedNoCapture()) | 
|  | 3876 | return "assumed not-captured"; | 
|  | 3877 | if (isKnownNoCaptureMaybeReturned()) | 
|  | 3878 | return "known not-captured-maybe-returned"; | 
|  | 3879 | if (isAssumedNoCaptureMaybeReturned()) | 
|  | 3880 | return "assumed not-captured-maybe-returned"; | 
|  | 3881 | return "assumed-captured"; | 
|  | 3882 | } | 
|  | 3883 | }; | 
|  | 3884 |  | 
|  | 3885 | /// Attributor-aware capture tracker. | 
|  | 3886 | struct AACaptureUseTracker final : public CaptureTracker { | 
|  | 3887 |  | 
|  | 3888 | /// Create a capture tracker that can lookup in-flight abstract attributes | 
|  | 3889 | /// through the Attributor \p A. | 
|  | 3890 | /// | 
|  | 3891 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the | 
|  | 3892 | /// search is stopped. If a use leads to a return instruction, | 
|  | 3893 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. | 
|  | 3894 | /// If a use leads to a ptr2int which may capture the value, | 
|  | 3895 | /// \p CapturedInInteger is set. If a use is found that is currently assumed | 
|  | 3896 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies | 
|  | 3897 | /// set. All values in \p PotentialCopies are later tracked as well. For every | 
|  | 3898 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, | 
|  | 3899 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger | 
|  | 3900 | /// conservatively set to true. | 
|  | 3901 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3902 | const AAIsDead &IsDeadAA, AANoCapture::StateType &State, | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3903 | SmallVectorImpl<const Value *> &PotentialCopies, | 
|  | 3904 | unsigned &RemainingUsesToExplore) | 
|  | 3905 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), | 
|  | 3906 | PotentialCopies(PotentialCopies), | 
|  | 3907 | RemainingUsesToExplore(RemainingUsesToExplore) {} | 
|  | 3908 |  | 
|  | 3909 | /// Determine if \p V maybe captured. *Also updates the state!* | 
|  | 3910 | bool valueMayBeCaptured(const Value *V) { | 
|  | 3911 | if (V->getType()->isPointerTy()) { | 
|  | 3912 | PointerMayBeCaptured(V, this); | 
|  | 3913 | } else { | 
|  | 3914 | State.indicatePessimisticFixpoint(); | 
|  | 3915 | } | 
|  | 3916 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | 
|  | 3917 | } | 
|  | 3918 |  | 
|  | 3919 | /// See CaptureTracker::tooManyUses(). | 
|  | 3920 | void tooManyUses() override { | 
|  | 3921 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); | 
|  | 3922 | } | 
|  | 3923 |  | 
|  | 3924 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { | 
|  | 3925 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) | 
|  | 3926 | return true; | 
|  | 3927 | const auto &DerefAA = | 
|  | 3928 | A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O)); | 
|  | 3929 | return DerefAA.getAssumedDereferenceableBytes(); | 
|  | 3930 | } | 
|  | 3931 |  | 
|  | 3932 | /// See CaptureTracker::captured(...). | 
|  | 3933 | bool captured(const Use *U) override { | 
|  | 3934 | Instruction *UInst = cast<Instruction>(U->getUser()); | 
|  | 3935 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst | 
|  | 3936 | << "\n"); | 
|  | 3937 |  | 
|  | 3938 | // Because we may reuse the tracker multiple times we keep track of the | 
|  | 3939 | // number of explored uses ourselves as well. | 
|  | 3940 | if (RemainingUsesToExplore-- == 0) { | 
|  | 3941 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); | 
|  | 3942 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 3943 | /* Return */ true); | 
|  | 3944 | } | 
|  | 3945 |  | 
|  | 3946 | // Deal with ptr2int by following uses. | 
|  | 3947 | if (isa<PtrToIntInst>(UInst)) { | 
|  | 3948 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); | 
|  | 3949 | return valueMayBeCaptured(UInst); | 
|  | 3950 | } | 
|  | 3951 |  | 
|  | 3952 | // Explicitly catch return instructions. | 
|  | 3953 | if (isa<ReturnInst>(UInst)) | 
|  | 3954 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 3955 | /* Return */ true); | 
|  | 3956 |  | 
|  | 3957 | // For now we only use special logic for call sites. However, the tracker | 
|  | 3958 | // itself knows about a lot of other non-capturing cases already. | 
|  | 3959 | CallSite CS(UInst); | 
|  | 3960 | if (!CS || !CS.isArgOperand(U)) | 
|  | 3961 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 3962 | /* Return */ true); | 
|  | 3963 |  | 
|  | 3964 | unsigned ArgNo = CS.getArgumentNo(U); | 
|  | 3965 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); | 
|  | 3966 | // If we have a abstract no-capture attribute for the argument we can use | 
|  | 3967 | // it to justify a non-capture attribute here. This allows recursion! | 
|  | 3968 | auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); | 
|  | 3969 | if (ArgNoCaptureAA.isAssumedNoCapture()) | 
|  | 3970 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 3971 | /* Return */ false); | 
|  | 3972 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 3973 | addPotentialCopy(CS); | 
|  | 3974 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 3975 | /* Return */ false); | 
|  | 3976 | } | 
|  | 3977 |  | 
|  | 3978 | // Lastly, we could not find a reason no-capture can be assumed so we don't. | 
|  | 3979 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 3980 | /* Return */ true); | 
|  | 3981 | } | 
|  | 3982 |  | 
|  | 3983 | /// Register \p CS as potential copy of the value we are checking. | 
|  | 3984 | void addPotentialCopy(CallSite CS) { | 
|  | 3985 | PotentialCopies.push_back(CS.getInstruction()); | 
|  | 3986 | } | 
|  | 3987 |  | 
|  | 3988 | /// See CaptureTracker::shouldExplore(...). | 
|  | 3989 | bool shouldExplore(const Use *U) override { | 
|  | 3990 | // Check liveness. | 
|  | 3991 | return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser())); | 
|  | 3992 | } | 
|  | 3993 |  | 
|  | 3994 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and | 
|  | 3995 | /// \p CapturedInRet, then return the appropriate value for use in the | 
|  | 3996 | /// CaptureTracker::captured() interface. | 
|  | 3997 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, | 
|  | 3998 | bool CapturedInRet) { | 
|  | 3999 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " | 
|  | 4000 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); | 
|  | 4001 | if (CapturedInMem) | 
|  | 4002 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); | 
|  | 4003 | if (CapturedInInt) | 
|  | 4004 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); | 
|  | 4005 | if (CapturedInRet) | 
|  | 4006 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); | 
|  | 4007 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | 
|  | 4008 | } | 
|  | 4009 |  | 
|  | 4010 | private: | 
|  | 4011 | /// The attributor providing in-flight abstract attributes. | 
|  | 4012 | Attributor &A; | 
|  | 4013 |  | 
|  | 4014 | /// The abstract attribute currently updated. | 
|  | 4015 | AANoCapture &NoCaptureAA; | 
|  | 4016 |  | 
|  | 4017 | /// The abstract liveness state. | 
|  | 4018 | const AAIsDead &IsDeadAA; | 
|  | 4019 |  | 
|  | 4020 | /// The state currently updated. | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4021 | AANoCapture::StateType &State; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4022 |  | 
|  | 4023 | /// Set of potential copies of the tracked value. | 
|  | 4024 | SmallVectorImpl<const Value *> &PotentialCopies; | 
|  | 4025 |  | 
|  | 4026 | /// Global counter to limit the number of explored uses. | 
|  | 4027 | unsigned &RemainingUsesToExplore; | 
|  | 4028 | }; | 
|  | 4029 |  | 
|  | 4030 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { | 
|  | 4031 | const IRPosition &IRP = getIRPosition(); | 
|  | 4032 | const Value *V = | 
|  | 4033 | getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); | 
|  | 4034 | if (!V) | 
|  | 4035 | return indicatePessimisticFixpoint(); | 
|  | 4036 |  | 
|  | 4037 | const Function *F = | 
|  | 4038 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); | 
|  | 4039 | assert(F && "Expected a function!"); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4040 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 4041 | const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4042 |  | 
|  | 4043 | AANoCapture::StateType T; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4044 |  | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4045 | // Readonly means we cannot capture through memory. | 
|  | 4046 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 4047 | if (FnMemAA.isAssumedReadOnly()) { | 
|  | 4048 | T.addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 4049 | if (FnMemAA.isKnownReadOnly()) | 
|  | 4050 | addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 4051 | } | 
|  | 4052 |  | 
|  | 4053 | // Make sure all returned values are different than the underlying value. | 
|  | 4054 | // TODO: we could do this in a more sophisticated way inside | 
|  | 4055 | //       AAReturnedValues, e.g., track all values that escape through returns | 
|  | 4056 | //       directly somehow. | 
|  | 4057 | auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { | 
|  | 4058 | bool SeenConstant = false; | 
|  | 4059 | for (auto &It : RVAA.returned_values()) { | 
|  | 4060 | if (isa<Constant>(It.first)) { | 
|  | 4061 | if (SeenConstant) | 
|  | 4062 | return false; | 
|  | 4063 | SeenConstant = true; | 
|  | 4064 | } else if (!isa<Argument>(It.first) || | 
|  | 4065 | It.first == getAssociatedArgument()) | 
|  | 4066 | return false; | 
|  | 4067 | } | 
|  | 4068 | return true; | 
|  | 4069 | }; | 
|  | 4070 |  | 
|  | 4071 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, FnPos); | 
|  | 4072 | if (NoUnwindAA.isAssumedNoUnwind()) { | 
|  | 4073 | bool IsVoidTy = F->getReturnType()->isVoidTy(); | 
|  | 4074 | const AAReturnedValues *RVAA = | 
|  | 4075 | IsVoidTy ? nullptr : &A.getAAFor<AAReturnedValues>(*this, FnPos); | 
|  | 4076 | if (IsVoidTy || CheckReturnedArgs(*RVAA)) { | 
|  | 4077 | T.addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 4078 | if (T.isKnown(NOT_CAPTURED_IN_MEM)) | 
|  | 4079 | return ChangeStatus::UNCHANGED; | 
|  | 4080 | if (NoUnwindAA.isKnownNoUnwind() && | 
|  | 4081 | (IsVoidTy || RVAA->getState().isAtFixpoint())) { | 
|  | 4082 | addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 4083 | if (isKnown(NOT_CAPTURED_IN_MEM)) | 
|  | 4084 | return indicateOptimisticFixpoint(); | 
|  | 4085 | } | 
|  | 4086 | } | 
|  | 4087 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4088 |  | 
|  | 4089 | // Use the CaptureTracker interface and logic with the specialized tracker, | 
|  | 4090 | // defined in AACaptureUseTracker, that can look at in-flight abstract | 
|  | 4091 | // attributes and directly updates the assumed state. | 
|  | 4092 | SmallVector<const Value *, 4> PotentialCopies; | 
|  | 4093 | unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; | 
|  | 4094 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, | 
|  | 4095 | RemainingUsesToExplore); | 
|  | 4096 |  | 
|  | 4097 | // Check all potential copies of the associated value until we can assume | 
|  | 4098 | // none will be captured or we have to assume at least one might be. | 
|  | 4099 | unsigned Idx = 0; | 
|  | 4100 | PotentialCopies.push_back(V); | 
|  | 4101 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) | 
|  | 4102 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); | 
|  | 4103 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4104 | AANoCapture::StateType &S = getState(); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4105 | auto Assumed = S.getAssumed(); | 
|  | 4106 | S.intersectAssumedBits(T.getAssumed()); | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4107 | if (!isAssumedNoCaptureMaybeReturned()) | 
|  | 4108 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4109 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | 
|  | 4110 | : ChangeStatus::CHANGED; | 
|  | 4111 | } | 
|  | 4112 |  | 
|  | 4113 | /// NoCapture attribute for function arguments. | 
|  | 4114 | struct AANoCaptureArgument final : AANoCaptureImpl { | 
|  | 4115 | AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4116 |  | 
|  | 4117 | /// See AbstractAttribute::trackStatistics() | 
|  | 4118 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } | 
|  | 4119 | }; | 
|  | 4120 |  | 
|  | 4121 | /// NoCapture attribute for call site arguments. | 
|  | 4122 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { | 
|  | 4123 | AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4124 |  | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4125 | /// See AbstractAttribute::initialize(...). | 
|  | 4126 | void initialize(Attributor &A) override { | 
|  | 4127 | if (Argument *Arg = getAssociatedArgument()) | 
|  | 4128 | if (Arg->hasByValAttr()) | 
|  | 4129 | indicateOptimisticFixpoint(); | 
|  | 4130 | AANoCaptureImpl::initialize(A); | 
|  | 4131 | } | 
|  | 4132 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4133 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4134 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4135 | // TODO: Once we have call site specific value information we can provide | 
|  | 4136 | //       call site specific liveness information and then it makes | 
|  | 4137 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 4138 | //       redirecting requests to the callee argument. | 
|  | 4139 | Argument *Arg = getAssociatedArgument(); | 
|  | 4140 | if (!Arg) | 
|  | 4141 | return indicatePessimisticFixpoint(); | 
|  | 4142 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 4143 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); | 
|  | 4144 | return clampStateAndIndicateChange( | 
|  | 4145 | getState(), | 
|  | 4146 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); | 
|  | 4147 | } | 
|  | 4148 |  | 
|  | 4149 | /// See AbstractAttribute::trackStatistics() | 
|  | 4150 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; | 
|  | 4151 | }; | 
|  | 4152 |  | 
|  | 4153 | /// NoCapture attribute for floating values. | 
|  | 4154 | struct AANoCaptureFloating final : AANoCaptureImpl { | 
|  | 4155 | AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4156 |  | 
|  | 4157 | /// See AbstractAttribute::trackStatistics() | 
|  | 4158 | void trackStatistics() const override { | 
|  | 4159 | STATS_DECLTRACK_FLOATING_ATTR(nocapture) | 
|  | 4160 | } | 
|  | 4161 | }; | 
|  | 4162 |  | 
|  | 4163 | /// NoCapture attribute for function return value. | 
|  | 4164 | struct AANoCaptureReturned final : AANoCaptureImpl { | 
|  | 4165 | AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { | 
|  | 4166 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 4167 | } | 
|  | 4168 |  | 
|  | 4169 | /// See AbstractAttribute::initialize(...). | 
|  | 4170 | void initialize(Attributor &A) override { | 
|  | 4171 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 4172 | } | 
|  | 4173 |  | 
|  | 4174 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4175 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4176 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 4177 | } | 
|  | 4178 |  | 
|  | 4179 | /// See AbstractAttribute::trackStatistics() | 
|  | 4180 | void trackStatistics() const override {} | 
|  | 4181 | }; | 
|  | 4182 |  | 
|  | 4183 | /// NoCapture attribute deduction for a call site return value. | 
|  | 4184 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { | 
|  | 4185 | AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 4186 |  | 
|  | 4187 | /// See AbstractAttribute::trackStatistics() | 
|  | 4188 | void trackStatistics() const override { | 
|  | 4189 | STATS_DECLTRACK_CSRET_ATTR(nocapture) | 
|  | 4190 | } | 
|  | 4191 | }; | 
|  | 4192 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4193 | /// ------------------ Value Simplify Attribute ---------------------------- | 
|  | 4194 | struct AAValueSimplifyImpl : AAValueSimplify { | 
|  | 4195 | AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} | 
|  | 4196 |  | 
|  | 4197 | /// See AbstractAttribute::getAsStr(). | 
|  | 4198 | const std::string getAsStr() const override { | 
|  | 4199 | return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") | 
|  | 4200 | : "not-simple"; | 
|  | 4201 | } | 
|  | 4202 |  | 
|  | 4203 | /// See AbstractAttribute::trackStatistics() | 
|  | 4204 | void trackStatistics() const override {} | 
|  | 4205 |  | 
|  | 4206 | /// See AAValueSimplify::getAssumedSimplifiedValue() | 
|  | 4207 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { | 
|  | 4208 | if (!getAssumed()) | 
|  | 4209 | return const_cast<Value *>(&getAssociatedValue()); | 
|  | 4210 | return SimplifiedAssociatedValue; | 
|  | 4211 | } | 
|  | 4212 | void initialize(Attributor &A) override {} | 
|  | 4213 |  | 
|  | 4214 | /// Helper function for querying AAValueSimplify and updating candicate. | 
|  | 4215 | /// \param QueryingValue Value trying to unify with SimplifiedValue | 
|  | 4216 | /// \param AccumulatedSimplifiedValue Current simplification result. | 
|  | 4217 | static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, | 
|  | 4218 | Value &QueryingValue, | 
|  | 4219 | Optional<Value *> &AccumulatedSimplifiedValue) { | 
|  | 4220 | // FIXME: Add a typecast support. | 
|  | 4221 |  | 
|  | 4222 | auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>( | 
|  | 4223 | QueryingAA, IRPosition::value(QueryingValue)); | 
|  | 4224 |  | 
|  | 4225 | Optional<Value *> QueryingValueSimplified = | 
|  | 4226 | ValueSimpifyAA.getAssumedSimplifiedValue(A); | 
|  | 4227 |  | 
|  | 4228 | if (!QueryingValueSimplified.hasValue()) | 
|  | 4229 | return true; | 
|  | 4230 |  | 
|  | 4231 | if (!QueryingValueSimplified.getValue()) | 
|  | 4232 | return false; | 
|  | 4233 |  | 
|  | 4234 | Value &QueryingValueSimplifiedUnwrapped = | 
|  | 4235 | *QueryingValueSimplified.getValue(); | 
|  | 4236 |  | 
|  | 4237 | if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) | 
|  | 4238 | return true; | 
|  | 4239 |  | 
|  | 4240 | if (AccumulatedSimplifiedValue.hasValue()) | 
|  | 4241 | return AccumulatedSimplifiedValue == QueryingValueSimplified; | 
|  | 4242 |  | 
|  | 4243 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue | 
|  | 4244 | << " is assumed to be " | 
|  | 4245 | << QueryingValueSimplifiedUnwrapped << "\n"); | 
|  | 4246 |  | 
|  | 4247 | AccumulatedSimplifiedValue = QueryingValueSimplified; | 
|  | 4248 | return true; | 
|  | 4249 | } | 
|  | 4250 |  | 
|  | 4251 | /// See AbstractAttribute::manifest(...). | 
|  | 4252 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4253 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 4254 |  | 
|  | 4255 | if (!SimplifiedAssociatedValue.hasValue() || | 
|  | 4256 | !SimplifiedAssociatedValue.getValue()) | 
|  | 4257 | return Changed; | 
|  | 4258 |  | 
|  | 4259 | if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) { | 
|  | 4260 | // We can replace the AssociatedValue with the constant. | 
|  | 4261 | Value &V = getAssociatedValue(); | 
|  | 4262 | if (!V.user_empty() && &V != C && V.getType() == C->getType()) { | 
|  | 4263 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C | 
|  | 4264 | << "\n"); | 
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 4265 | A.changeValueAfterManifest(V, *C); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4266 | Changed = ChangeStatus::CHANGED; | 
|  | 4267 | } | 
|  | 4268 | } | 
|  | 4269 |  | 
|  | 4270 | return Changed | AAValueSimplify::manifest(A); | 
|  | 4271 | } | 
|  | 4272 |  | 
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4273 | /// See AbstractState::indicatePessimisticFixpoint(...). | 
|  | 4274 | ChangeStatus indicatePessimisticFixpoint() override { | 
|  | 4275 | // NOTE: Associated value will be returned in a pessimistic fixpoint and is | 
|  | 4276 | // regarded as known. That's why`indicateOptimisticFixpoint` is called. | 
|  | 4277 | SimplifiedAssociatedValue = &getAssociatedValue(); | 
|  | 4278 | return indicateOptimisticFixpoint(); | 
|  | 4279 | } | 
|  | 4280 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4281 | protected: | 
|  | 4282 | // An assumed simplified value. Initially, it is set to Optional::None, which | 
|  | 4283 | // means that the value is not clear under current assumption. If in the | 
|  | 4284 | // pessimistic state, getAssumedSimplifiedValue doesn't return this value but | 
|  | 4285 | // returns orignal associated value. | 
|  | 4286 | Optional<Value *> SimplifiedAssociatedValue; | 
|  | 4287 | }; | 
|  | 4288 |  | 
|  | 4289 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { | 
|  | 4290 | AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4291 |  | 
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4292 | void initialize(Attributor &A) override { | 
|  | 4293 | AAValueSimplifyImpl::initialize(A); | 
|  | 4294 | if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) | 
|  | 4295 | indicatePessimisticFixpoint(); | 
|  | 4296 | if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, | 
|  | 4297 | /* IgnoreSubsumingPositions */ true)) | 
|  | 4298 | indicatePessimisticFixpoint(); | 
|  | 4299 | } | 
|  | 4300 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4301 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4302 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4303 | // Byval is only replacable if it is readonly otherwise we would write into | 
|  | 4304 | // the replaced value and not the copy that byval creates implicitly. | 
|  | 4305 | Argument *Arg = getAssociatedArgument(); | 
|  | 4306 | if (Arg->hasByValAttr()) { | 
|  | 4307 | const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); | 
|  | 4308 | if (!MemAA.isAssumedReadOnly()) | 
|  | 4309 | return indicatePessimisticFixpoint(); | 
|  | 4310 | } | 
|  | 4311 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4312 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 4313 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4314 | auto PredForCallSite = [&](AbstractCallSite ACS) { | 
|  | 4315 | // Check if we have an associated argument or not (which can happen for | 
|  | 4316 | // callback calls). | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4317 | Value *ArgOp = ACS.getCallArgOperand(getArgNo()); | 
|  | 4318 | if (!ArgOp) | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4319 | return false; | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4320 | // We can only propagate thread independent values through callbacks. | 
|  | 4321 | // This is different to direct/indirect call sites because for them we | 
|  | 4322 | // know the thread executing the caller and callee is the same. For | 
|  | 4323 | // callbacks this is not guaranteed, thus a thread dependent value could | 
|  | 4324 | // be different for the caller and callee, making it invalid to propagate. | 
|  | 4325 | if (ACS.isCallbackCall()) | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4326 | if (auto *C = dyn_cast<Constant>(ArgOp)) | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4327 | if (C->isThreadDependent()) | 
|  | 4328 | return false; | 
|  | 4329 | return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4330 | }; | 
|  | 4331 |  | 
|  | 4332 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) | 
|  | 4333 | return indicatePessimisticFixpoint(); | 
|  | 4334 |  | 
|  | 4335 | // If a candicate was found in this update, return CHANGED. | 
|  | 4336 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 4337 | ? ChangeStatus::UNCHANGED | 
|  | 4338 | : ChangeStatus ::CHANGED; | 
|  | 4339 | } | 
|  | 4340 |  | 
|  | 4341 | /// See AbstractAttribute::trackStatistics() | 
|  | 4342 | void trackStatistics() const override { | 
|  | 4343 | STATS_DECLTRACK_ARG_ATTR(value_simplify) | 
|  | 4344 | } | 
|  | 4345 | }; | 
|  | 4346 |  | 
|  | 4347 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { | 
|  | 4348 | AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4349 |  | 
|  | 4350 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4351 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4352 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 4353 |  | 
|  | 4354 | auto PredForReturned = [&](Value &V) { | 
|  | 4355 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); | 
|  | 4356 | }; | 
|  | 4357 |  | 
|  | 4358 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) | 
|  | 4359 | return indicatePessimisticFixpoint(); | 
|  | 4360 |  | 
|  | 4361 | // If a candicate was found in this update, return CHANGED. | 
|  | 4362 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 4363 | ? ChangeStatus::UNCHANGED | 
|  | 4364 | : ChangeStatus ::CHANGED; | 
|  | 4365 | } | 
|  | 4366 | /// See AbstractAttribute::trackStatistics() | 
|  | 4367 | void trackStatistics() const override { | 
|  | 4368 | STATS_DECLTRACK_FNRET_ATTR(value_simplify) | 
|  | 4369 | } | 
|  | 4370 | }; | 
|  | 4371 |  | 
|  | 4372 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { | 
|  | 4373 | AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4374 |  | 
|  | 4375 | /// See AbstractAttribute::initialize(...). | 
|  | 4376 | void initialize(Attributor &A) override { | 
|  | 4377 | Value &V = getAnchorValue(); | 
|  | 4378 |  | 
|  | 4379 | // TODO: add other stuffs | 
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4380 | if (isa<Constant>(V)) | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4381 | indicatePessimisticFixpoint(); | 
|  | 4382 | } | 
|  | 4383 |  | 
|  | 4384 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4385 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4386 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 4387 |  | 
|  | 4388 | auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool { | 
|  | 4389 | auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); | 
|  | 4390 | if (!Stripped && this == &AA) { | 
|  | 4391 | // TODO: Look the instruction and check recursively. | 
|  | 4392 | LLVM_DEBUG( | 
|  | 4393 | dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : " | 
|  | 4394 | << V << "\n"); | 
|  | 4395 | indicatePessimisticFixpoint(); | 
|  | 4396 | return false; | 
|  | 4397 | } | 
|  | 4398 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); | 
|  | 4399 | }; | 
|  | 4400 |  | 
|  | 4401 | if (!genericValueTraversal<AAValueSimplify, BooleanState>( | 
|  | 4402 | A, getIRPosition(), *this, static_cast<BooleanState &>(*this), | 
|  | 4403 | VisitValueCB)) | 
|  | 4404 | return indicatePessimisticFixpoint(); | 
|  | 4405 |  | 
|  | 4406 | // If a candicate was found in this update, return CHANGED. | 
|  | 4407 |  | 
|  | 4408 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 4409 | ? ChangeStatus::UNCHANGED | 
|  | 4410 | : ChangeStatus ::CHANGED; | 
|  | 4411 | } | 
|  | 4412 |  | 
|  | 4413 | /// See AbstractAttribute::trackStatistics() | 
|  | 4414 | void trackStatistics() const override { | 
|  | 4415 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify) | 
|  | 4416 | } | 
|  | 4417 | }; | 
|  | 4418 |  | 
|  | 4419 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { | 
|  | 4420 | AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 4421 |  | 
|  | 4422 | /// See AbstractAttribute::initialize(...). | 
|  | 4423 | void initialize(Attributor &A) override { | 
|  | 4424 | SimplifiedAssociatedValue = &getAnchorValue(); | 
|  | 4425 | indicateOptimisticFixpoint(); | 
|  | 4426 | } | 
|  | 4427 | /// See AbstractAttribute::initialize(...). | 
|  | 4428 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4429 | llvm_unreachable( | 
|  | 4430 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); | 
|  | 4431 | } | 
|  | 4432 | /// See AbstractAttribute::trackStatistics() | 
|  | 4433 | void trackStatistics() const override { | 
|  | 4434 | STATS_DECLTRACK_FN_ATTR(value_simplify) | 
|  | 4435 | } | 
|  | 4436 | }; | 
|  | 4437 |  | 
|  | 4438 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { | 
|  | 4439 | AAValueSimplifyCallSite(const IRPosition &IRP) | 
|  | 4440 | : AAValueSimplifyFunction(IRP) {} | 
|  | 4441 | /// See AbstractAttribute::trackStatistics() | 
|  | 4442 | void trackStatistics() const override { | 
|  | 4443 | STATS_DECLTRACK_CS_ATTR(value_simplify) | 
|  | 4444 | } | 
|  | 4445 | }; | 
|  | 4446 |  | 
|  | 4447 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { | 
|  | 4448 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP) | 
|  | 4449 | : AAValueSimplifyReturned(IRP) {} | 
|  | 4450 |  | 
|  | 4451 | void trackStatistics() const override { | 
|  | 4452 | STATS_DECLTRACK_CSRET_ATTR(value_simplify) | 
|  | 4453 | } | 
|  | 4454 | }; | 
|  | 4455 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { | 
|  | 4456 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP) | 
|  | 4457 | : AAValueSimplifyFloating(IRP) {} | 
|  | 4458 |  | 
|  | 4459 | void trackStatistics() const override { | 
|  | 4460 | STATS_DECLTRACK_CSARG_ATTR(value_simplify) | 
|  | 4461 | } | 
|  | 4462 | }; | 
|  | 4463 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4464 | /// ----------------------- Heap-To-Stack Conversion --------------------------- | 
|  | 4465 | struct AAHeapToStackImpl : public AAHeapToStack { | 
|  | 4466 | AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} | 
|  | 4467 |  | 
|  | 4468 | const std::string getAsStr() const override { | 
|  | 4469 | return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); | 
|  | 4470 | } | 
|  | 4471 |  | 
|  | 4472 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4473 | assert(getState().isValidState() && | 
|  | 4474 | "Attempted to manifest an invalid state!"); | 
|  | 4475 |  | 
|  | 4476 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
|  | 4477 | Function *F = getAssociatedFunction(); | 
|  | 4478 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | 
|  | 4479 |  | 
|  | 4480 | for (Instruction *MallocCall : MallocCalls) { | 
|  | 4481 | // This malloc cannot be replaced. | 
|  | 4482 | if (BadMallocCalls.count(MallocCall)) | 
|  | 4483 | continue; | 
|  | 4484 |  | 
|  | 4485 | for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { | 
|  | 4486 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); | 
|  | 4487 | A.deleteAfterManifest(*FreeCall); | 
|  | 4488 | HasChanged = ChangeStatus::CHANGED; | 
|  | 4489 | } | 
|  | 4490 |  | 
|  | 4491 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall | 
|  | 4492 | << "\n"); | 
|  | 4493 |  | 
|  | 4494 | Constant *Size; | 
|  | 4495 | if (isCallocLikeFn(MallocCall, TLI)) { | 
|  | 4496 | auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); | 
|  | 4497 | auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); | 
|  | 4498 | APInt TotalSize = SizeT->getValue() * Num->getValue(); | 
|  | 4499 | Size = | 
|  | 4500 | ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); | 
|  | 4501 | } else { | 
|  | 4502 | Size = cast<ConstantInt>(MallocCall->getOperand(0)); | 
|  | 4503 | } | 
|  | 4504 |  | 
|  | 4505 | unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); | 
|  | 4506 | Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, | 
|  | 4507 | Size, "", MallocCall->getNextNode()); | 
|  | 4508 |  | 
|  | 4509 | if (AI->getType() != MallocCall->getType()) | 
|  | 4510 | AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", | 
|  | 4511 | AI->getNextNode()); | 
|  | 4512 |  | 
| Johannes Doerfert | 5d34602 | 2019-12-13 22:11:42 -0600 | [diff] [blame] | 4513 | replaceAllInstructionUsesWith(*MallocCall, *AI); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4514 |  | 
|  | 4515 | if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { | 
|  | 4516 | auto *NBB = II->getNormalDest(); | 
|  | 4517 | BranchInst::Create(NBB, MallocCall->getParent()); | 
|  | 4518 | A.deleteAfterManifest(*MallocCall); | 
|  | 4519 | } else { | 
|  | 4520 | A.deleteAfterManifest(*MallocCall); | 
|  | 4521 | } | 
|  | 4522 |  | 
|  | 4523 | if (isCallocLikeFn(MallocCall, TLI)) { | 
|  | 4524 | auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", | 
|  | 4525 | AI->getNextNode()); | 
|  | 4526 | Value *Ops[] = { | 
|  | 4527 | BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, | 
|  | 4528 | ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; | 
|  | 4529 |  | 
|  | 4530 | Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; | 
|  | 4531 | Module *M = F->getParent(); | 
|  | 4532 | Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); | 
|  | 4533 | CallInst::Create(Fn, Ops, "", BI->getNextNode()); | 
|  | 4534 | } | 
|  | 4535 | HasChanged = ChangeStatus::CHANGED; | 
|  | 4536 | } | 
|  | 4537 |  | 
|  | 4538 | return HasChanged; | 
|  | 4539 | } | 
|  | 4540 |  | 
|  | 4541 | /// Collection of all malloc calls in a function. | 
|  | 4542 | SmallSetVector<Instruction *, 4> MallocCalls; | 
|  | 4543 |  | 
|  | 4544 | /// Collection of malloc calls that cannot be converted. | 
|  | 4545 | DenseSet<const Instruction *> BadMallocCalls; | 
|  | 4546 |  | 
|  | 4547 | /// A map for each malloc call to the set of associated free calls. | 
|  | 4548 | DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; | 
|  | 4549 |  | 
|  | 4550 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 4551 | }; | 
|  | 4552 |  | 
|  | 4553 | ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { | 
|  | 4554 | const Function *F = getAssociatedFunction(); | 
|  | 4555 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | 
|  | 4556 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4557 | MustBeExecutedContextExplorer &Explorer = | 
|  | 4558 | A.getInfoCache().getMustBeExecutedContextExplorer(); | 
|  | 4559 |  | 
|  | 4560 | auto FreeCheck = [&](Instruction &I) { | 
|  | 4561 | const auto &Frees = FreesForMalloc.lookup(&I); | 
|  | 4562 | if (Frees.size() != 1) | 
|  | 4563 | return false; | 
|  | 4564 | Instruction *UniqueFree = *Frees.begin(); | 
|  | 4565 | return Explorer.findInContextOf(UniqueFree, I.getNextNode()); | 
|  | 4566 | }; | 
|  | 4567 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4568 | auto UsesCheck = [&](Instruction &I) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4569 | bool ValidUsesOnly = true; | 
|  | 4570 | bool MustUse = true; | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4571 | auto Pred = [&](const Use &U, bool &Follow) -> bool { | 
|  | 4572 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4573 | if (isa<LoadInst>(UserI)) | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4574 | return true; | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4575 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4576 | if (SI->getValueOperand() == U.get()) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4577 | LLVM_DEBUG(dbgs() | 
|  | 4578 | << "[H2S] escaping store to memory: " << *UserI << "\n"); | 
|  | 4579 | ValidUsesOnly = false; | 
|  | 4580 | } else { | 
|  | 4581 | // A store into the malloc'ed memory is fine. | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4582 | } | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4583 | return true; | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4584 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4585 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4586 | if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) | 
|  | 4587 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4588 | // Record malloc. | 
|  | 4589 | if (isFreeCall(UserI, TLI)) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4590 | if (MustUse) { | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4591 | FreesForMalloc[&I].insert(UserI); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4592 | } else { | 
|  | 4593 | LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " | 
|  | 4594 | << *UserI << "\n"); | 
|  | 4595 | ValidUsesOnly = false; | 
|  | 4596 | } | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4597 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4598 | } | 
|  | 4599 |  | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4600 | unsigned ArgNo = CB->getArgOperandNo(&U); | 
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 4601 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4602 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( | 
|  | 4603 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
|  | 4604 |  | 
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 4605 | // If a callsite argument use is nofree, we are fine. | 
|  | 4606 | const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( | 
|  | 4607 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
|  | 4608 |  | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4609 | if (!NoCaptureAA.isAssumedNoCapture() || | 
|  | 4610 | !ArgNoFreeAA.isAssumedNoFree()) { | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4611 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4612 | ValidUsesOnly = false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4613 | } | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4614 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4615 | } | 
|  | 4616 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4617 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | 
|  | 4618 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | 
|  | 4619 | MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4620 | Follow = true; | 
|  | 4621 | return true; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4622 | } | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4623 | // Unknown user for which we can not track uses further (in a way that | 
|  | 4624 | // makes sense). | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4625 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4626 | ValidUsesOnly = false; | 
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4627 | return true; | 
|  | 4628 | }; | 
|  | 4629 | A.checkForAllUses(Pred, *this, I); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4630 | return ValidUsesOnly; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4631 | }; | 
|  | 4632 |  | 
|  | 4633 | auto MallocCallocCheck = [&](Instruction &I) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4634 | if (BadMallocCalls.count(&I)) | 
|  | 4635 | return true; | 
|  | 4636 |  | 
|  | 4637 | bool IsMalloc = isMallocLikeFn(&I, TLI); | 
|  | 4638 | bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); | 
|  | 4639 | if (!IsMalloc && !IsCalloc) { | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4640 | BadMallocCalls.insert(&I); | 
|  | 4641 | return true; | 
|  | 4642 | } | 
|  | 4643 |  | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4644 | if (IsMalloc) { | 
|  | 4645 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) | 
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 4646 | if (Size->getValue().ule(MaxHeapToStackSize)) | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4647 | if (UsesCheck(I) || FreeCheck(I)) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4648 | MallocCalls.insert(&I); | 
|  | 4649 | return true; | 
|  | 4650 | } | 
|  | 4651 | } else if (IsCalloc) { | 
|  | 4652 | bool Overflow = false; | 
|  | 4653 | if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) | 
|  | 4654 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) | 
|  | 4655 | if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) | 
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 4656 | .ule(MaxHeapToStackSize)) | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4657 | if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4658 | MallocCalls.insert(&I); | 
|  | 4659 | return true; | 
|  | 4660 | } | 
|  | 4661 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4662 |  | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4663 | BadMallocCalls.insert(&I); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4664 | return true; | 
|  | 4665 | }; | 
|  | 4666 |  | 
|  | 4667 | size_t NumBadMallocs = BadMallocCalls.size(); | 
|  | 4668 |  | 
|  | 4669 | A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); | 
|  | 4670 |  | 
|  | 4671 | if (NumBadMallocs != BadMallocCalls.size()) | 
|  | 4672 | return ChangeStatus::CHANGED; | 
|  | 4673 |  | 
|  | 4674 | return ChangeStatus::UNCHANGED; | 
|  | 4675 | } | 
|  | 4676 |  | 
|  | 4677 | struct AAHeapToStackFunction final : public AAHeapToStackImpl { | 
|  | 4678 | AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} | 
|  | 4679 |  | 
|  | 4680 | /// See AbstractAttribute::trackStatistics() | 
|  | 4681 | void trackStatistics() const override { | 
|  | 4682 | STATS_DECL(MallocCalls, Function, | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4683 | "Number of malloc calls converted to allocas"); | 
|  | 4684 | for (auto *C : MallocCalls) | 
|  | 4685 | if (!BadMallocCalls.count(C)) | 
|  | 4686 | ++BUILD_STAT_NAME(MallocCalls, Function); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4687 | } | 
|  | 4688 | }; | 
|  | 4689 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4690 | /// -------------------- Memory Behavior Attributes ---------------------------- | 
|  | 4691 | /// Includes read-none, read-only, and write-only. | 
|  | 4692 | /// ---------------------------------------------------------------------------- | 
|  | 4693 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { | 
|  | 4694 | AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} | 
|  | 4695 |  | 
|  | 4696 | /// See AbstractAttribute::initialize(...). | 
|  | 4697 | void initialize(Attributor &A) override { | 
|  | 4698 | intersectAssumedBits(BEST_STATE); | 
|  | 4699 | getKnownStateFromValue(getIRPosition(), getState()); | 
|  | 4700 | IRAttribute::initialize(A); | 
|  | 4701 | } | 
|  | 4702 |  | 
|  | 4703 | /// Return the memory behavior information encoded in the IR for \p IRP. | 
|  | 4704 | static void getKnownStateFromValue(const IRPosition &IRP, | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4705 | BitIntegerState &State, | 
|  | 4706 | bool IgnoreSubsumingPositions = false) { | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4707 | SmallVector<Attribute, 2> Attrs; | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4708 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4709 | for (const Attribute &Attr : Attrs) { | 
|  | 4710 | switch (Attr.getKindAsEnum()) { | 
|  | 4711 | case Attribute::ReadNone: | 
|  | 4712 | State.addKnownBits(NO_ACCESSES); | 
|  | 4713 | break; | 
|  | 4714 | case Attribute::ReadOnly: | 
|  | 4715 | State.addKnownBits(NO_WRITES); | 
|  | 4716 | break; | 
|  | 4717 | case Attribute::WriteOnly: | 
|  | 4718 | State.addKnownBits(NO_READS); | 
|  | 4719 | break; | 
|  | 4720 | default: | 
|  | 4721 | llvm_unreachable("Unexpcted attribute!"); | 
|  | 4722 | } | 
|  | 4723 | } | 
|  | 4724 |  | 
|  | 4725 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { | 
|  | 4726 | if (!I->mayReadFromMemory()) | 
|  | 4727 | State.addKnownBits(NO_READS); | 
|  | 4728 | if (!I->mayWriteToMemory()) | 
|  | 4729 | State.addKnownBits(NO_WRITES); | 
|  | 4730 | } | 
|  | 4731 | } | 
|  | 4732 |  | 
|  | 4733 | /// See AbstractAttribute::getDeducedAttributes(...). | 
|  | 4734 | void getDeducedAttributes(LLVMContext &Ctx, | 
|  | 4735 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 4736 | assert(Attrs.size() == 0); | 
|  | 4737 | if (isAssumedReadNone()) | 
|  | 4738 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); | 
|  | 4739 | else if (isAssumedReadOnly()) | 
|  | 4740 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); | 
|  | 4741 | else if (isAssumedWriteOnly()) | 
|  | 4742 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); | 
|  | 4743 | assert(Attrs.size() <= 1); | 
|  | 4744 | } | 
|  | 4745 |  | 
|  | 4746 | /// See AbstractAttribute::manifest(...). | 
|  | 4747 | ChangeStatus manifest(Attributor &A) override { | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 4748 | const IRPosition &IRP = getIRPosition(); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4749 |  | 
|  | 4750 | // Check if we would improve the existing attributes first. | 
|  | 4751 | SmallVector<Attribute, 4> DeducedAttrs; | 
|  | 4752 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); | 
|  | 4753 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { | 
|  | 4754 | return IRP.hasAttr(Attr.getKindAsEnum(), | 
|  | 4755 | /* IgnoreSubsumingPositions */ true); | 
|  | 4756 | })) | 
|  | 4757 | return ChangeStatus::UNCHANGED; | 
|  | 4758 |  | 
|  | 4759 | // Clear existing attributes. | 
|  | 4760 | IRP.removeAttrs(AttrKinds); | 
|  | 4761 |  | 
|  | 4762 | // Use the generic manifest method. | 
|  | 4763 | return IRAttribute::manifest(A); | 
|  | 4764 | } | 
|  | 4765 |  | 
|  | 4766 | /// See AbstractState::getAsStr(). | 
|  | 4767 | const std::string getAsStr() const override { | 
|  | 4768 | if (isAssumedReadNone()) | 
|  | 4769 | return "readnone"; | 
|  | 4770 | if (isAssumedReadOnly()) | 
|  | 4771 | return "readonly"; | 
|  | 4772 | if (isAssumedWriteOnly()) | 
|  | 4773 | return "writeonly"; | 
|  | 4774 | return "may-read/write"; | 
|  | 4775 | } | 
|  | 4776 |  | 
|  | 4777 | /// The set of IR attributes AAMemoryBehavior deals with. | 
|  | 4778 | static const Attribute::AttrKind AttrKinds[3]; | 
|  | 4779 | }; | 
|  | 4780 |  | 
|  | 4781 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { | 
|  | 4782 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; | 
|  | 4783 |  | 
|  | 4784 | /// Memory behavior attribute for a floating value. | 
|  | 4785 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { | 
|  | 4786 | AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 4787 |  | 
|  | 4788 | /// See AbstractAttribute::initialize(...). | 
|  | 4789 | void initialize(Attributor &A) override { | 
|  | 4790 | AAMemoryBehaviorImpl::initialize(A); | 
|  | 4791 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 4792 | for (const Use &U : getAssociatedValue().uses()) | 
|  | 4793 | Uses.insert(&U); | 
|  | 4794 | } | 
|  | 4795 |  | 
|  | 4796 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4797 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 4798 |  | 
|  | 4799 | /// See AbstractAttribute::trackStatistics() | 
|  | 4800 | void trackStatistics() const override { | 
|  | 4801 | if (isAssumedReadNone()) | 
|  | 4802 | STATS_DECLTRACK_FLOATING_ATTR(readnone) | 
|  | 4803 | else if (isAssumedReadOnly()) | 
|  | 4804 | STATS_DECLTRACK_FLOATING_ATTR(readonly) | 
|  | 4805 | else if (isAssumedWriteOnly()) | 
|  | 4806 | STATS_DECLTRACK_FLOATING_ATTR(writeonly) | 
|  | 4807 | } | 
|  | 4808 |  | 
|  | 4809 | private: | 
|  | 4810 | /// Return true if users of \p UserI might access the underlying | 
|  | 4811 | /// variable/location described by \p U and should therefore be analyzed. | 
|  | 4812 | bool followUsersOfUseIn(Attributor &A, const Use *U, | 
|  | 4813 | const Instruction *UserI); | 
|  | 4814 |  | 
|  | 4815 | /// Update the state according to the effect of use \p U in \p UserI. | 
|  | 4816 | void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); | 
|  | 4817 |  | 
|  | 4818 | protected: | 
|  | 4819 | /// Container for (transitive) uses of the associated argument. | 
|  | 4820 | SetVector<const Use *> Uses; | 
|  | 4821 | }; | 
|  | 4822 |  | 
|  | 4823 | /// Memory behavior attribute for function argument. | 
|  | 4824 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { | 
|  | 4825 | AAMemoryBehaviorArgument(const IRPosition &IRP) | 
|  | 4826 | : AAMemoryBehaviorFloating(IRP) {} | 
|  | 4827 |  | 
|  | 4828 | /// See AbstractAttribute::initialize(...). | 
|  | 4829 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4830 | intersectAssumedBits(BEST_STATE); | 
|  | 4831 | const IRPosition &IRP = getIRPosition(); | 
|  | 4832 | // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we | 
|  | 4833 | // can query it when we use has/getAttr. That would allow us to reuse the | 
|  | 4834 | // initialize of the base class here. | 
|  | 4835 | bool HasByVal = | 
|  | 4836 | IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); | 
|  | 4837 | getKnownStateFromValue(IRP, getState(), | 
|  | 4838 | /* IgnoreSubsumingPositions */ HasByVal); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4839 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4840 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 4841 | Argument *Arg = getAssociatedArgument(); | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4842 | if (!Arg || !Arg->getParent()->hasExactDefinition()) { | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4843 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4844 | } else { | 
|  | 4845 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 4846 | for (const Use &U : Arg->uses()) | 
|  | 4847 | Uses.insert(&U); | 
|  | 4848 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4849 | } | 
|  | 4850 |  | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4851 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4852 | // TODO: From readattrs.ll: "inalloca parameters are always | 
|  | 4853 | //                           considered written" | 
|  | 4854 | if (hasAttr({Attribute::InAlloca})) { | 
|  | 4855 | removeKnownBits(NO_WRITES); | 
|  | 4856 | removeAssumedBits(NO_WRITES); | 
|  | 4857 | } | 
|  | 4858 | return AAMemoryBehaviorFloating::manifest(A); | 
|  | 4859 | } | 
|  | 4860 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4861 | /// See AbstractAttribute::trackStatistics() | 
|  | 4862 | void trackStatistics() const override { | 
|  | 4863 | if (isAssumedReadNone()) | 
|  | 4864 | STATS_DECLTRACK_ARG_ATTR(readnone) | 
|  | 4865 | else if (isAssumedReadOnly()) | 
|  | 4866 | STATS_DECLTRACK_ARG_ATTR(readonly) | 
|  | 4867 | else if (isAssumedWriteOnly()) | 
|  | 4868 | STATS_DECLTRACK_ARG_ATTR(writeonly) | 
|  | 4869 | } | 
|  | 4870 | }; | 
|  | 4871 |  | 
|  | 4872 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { | 
|  | 4873 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) | 
|  | 4874 | : AAMemoryBehaviorArgument(IRP) {} | 
|  | 4875 |  | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4876 | /// See AbstractAttribute::initialize(...). | 
|  | 4877 | void initialize(Attributor &A) override { | 
|  | 4878 | if (Argument *Arg = getAssociatedArgument()) { | 
|  | 4879 | if (Arg->hasByValAttr()) { | 
|  | 4880 | addKnownBits(NO_WRITES); | 
|  | 4881 | removeKnownBits(NO_READS); | 
|  | 4882 | removeAssumedBits(NO_READS); | 
|  | 4883 | } | 
|  | 4884 | } else { | 
|  | 4885 | } | 
|  | 4886 | AAMemoryBehaviorArgument::initialize(A); | 
|  | 4887 | } | 
|  | 4888 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4889 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4890 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4891 | // TODO: Once we have call site specific value information we can provide | 
|  | 4892 | //       call site specific liveness liveness information and then it makes | 
|  | 4893 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 4894 | //       redirecting requests to the callee argument. | 
|  | 4895 | Argument *Arg = getAssociatedArgument(); | 
|  | 4896 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 4897 | auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); | 
|  | 4898 | return clampStateAndIndicateChange( | 
|  | 4899 | getState(), | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4900 | static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4901 | } | 
|  | 4902 |  | 
|  | 4903 | /// See AbstractAttribute::trackStatistics() | 
|  | 4904 | void trackStatistics() const override { | 
|  | 4905 | if (isAssumedReadNone()) | 
|  | 4906 | STATS_DECLTRACK_CSARG_ATTR(readnone) | 
|  | 4907 | else if (isAssumedReadOnly()) | 
|  | 4908 | STATS_DECLTRACK_CSARG_ATTR(readonly) | 
|  | 4909 | else if (isAssumedWriteOnly()) | 
|  | 4910 | STATS_DECLTRACK_CSARG_ATTR(writeonly) | 
|  | 4911 | } | 
|  | 4912 | }; | 
|  | 4913 |  | 
|  | 4914 | /// Memory behavior attribute for a call site return position. | 
|  | 4915 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { | 
|  | 4916 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) | 
|  | 4917 | : AAMemoryBehaviorFloating(IRP) {} | 
|  | 4918 |  | 
|  | 4919 | /// See AbstractAttribute::manifest(...). | 
|  | 4920 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4921 | // We do not annotate returned values. | 
|  | 4922 | return ChangeStatus::UNCHANGED; | 
|  | 4923 | } | 
|  | 4924 |  | 
|  | 4925 | /// See AbstractAttribute::trackStatistics() | 
|  | 4926 | void trackStatistics() const override {} | 
|  | 4927 | }; | 
|  | 4928 |  | 
|  | 4929 | /// An AA to represent the memory behavior function attributes. | 
|  | 4930 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { | 
|  | 4931 | AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 4932 |  | 
|  | 4933 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
|  | 4934 | virtual ChangeStatus updateImpl(Attributor &A) override; | 
|  | 4935 |  | 
|  | 4936 | /// See AbstractAttribute::manifest(...). | 
|  | 4937 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4938 | Function &F = cast<Function>(getAnchorValue()); | 
|  | 4939 | if (isAssumedReadNone()) { | 
|  | 4940 | F.removeFnAttr(Attribute::ArgMemOnly); | 
|  | 4941 | F.removeFnAttr(Attribute::InaccessibleMemOnly); | 
|  | 4942 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); | 
|  | 4943 | } | 
|  | 4944 | return AAMemoryBehaviorImpl::manifest(A); | 
|  | 4945 | } | 
|  | 4946 |  | 
|  | 4947 | /// See AbstractAttribute::trackStatistics() | 
|  | 4948 | void trackStatistics() const override { | 
|  | 4949 | if (isAssumedReadNone()) | 
|  | 4950 | STATS_DECLTRACK_FN_ATTR(readnone) | 
|  | 4951 | else if (isAssumedReadOnly()) | 
|  | 4952 | STATS_DECLTRACK_FN_ATTR(readonly) | 
|  | 4953 | else if (isAssumedWriteOnly()) | 
|  | 4954 | STATS_DECLTRACK_FN_ATTR(writeonly) | 
|  | 4955 | } | 
|  | 4956 | }; | 
|  | 4957 |  | 
|  | 4958 | /// AAMemoryBehavior attribute for call sites. | 
|  | 4959 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { | 
|  | 4960 | AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 4961 |  | 
|  | 4962 | /// See AbstractAttribute::initialize(...). | 
|  | 4963 | void initialize(Attributor &A) override { | 
|  | 4964 | AAMemoryBehaviorImpl::initialize(A); | 
|  | 4965 | Function *F = getAssociatedFunction(); | 
|  | 4966 | if (!F || !F->hasExactDefinition()) | 
|  | 4967 | indicatePessimisticFixpoint(); | 
|  | 4968 | } | 
|  | 4969 |  | 
|  | 4970 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4971 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4972 | // TODO: Once we have call site specific value information we can provide | 
|  | 4973 | //       call site specific liveness liveness information and then it makes | 
|  | 4974 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 4975 | //       redirecting requests to the callee argument. | 
|  | 4976 | Function *F = getAssociatedFunction(); | 
|  | 4977 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 4978 | auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 4979 | return clampStateAndIndicateChange( | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4980 | getState(), | 
|  | 4981 | static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4982 | } | 
|  | 4983 |  | 
|  | 4984 | /// See AbstractAttribute::trackStatistics() | 
|  | 4985 | void trackStatistics() const override { | 
|  | 4986 | if (isAssumedReadNone()) | 
|  | 4987 | STATS_DECLTRACK_CS_ATTR(readnone) | 
|  | 4988 | else if (isAssumedReadOnly()) | 
|  | 4989 | STATS_DECLTRACK_CS_ATTR(readonly) | 
|  | 4990 | else if (isAssumedWriteOnly()) | 
|  | 4991 | STATS_DECLTRACK_CS_ATTR(writeonly) | 
|  | 4992 | } | 
|  | 4993 | }; | 
| Benjamin Kramer | c5d1d56 | 2019-10-12 11:01:52 +0000 | [diff] [blame] | 4994 | } // namespace | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4995 |  | 
|  | 4996 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { | 
|  | 4997 |  | 
|  | 4998 | // The current assumed state used to determine a change. | 
|  | 4999 | auto AssumedState = getAssumed(); | 
|  | 5000 |  | 
|  | 5001 | auto CheckRWInst = [&](Instruction &I) { | 
|  | 5002 | // If the instruction has an own memory behavior state, use it to restrict | 
|  | 5003 | // the local state. No further analysis is required as the other memory | 
|  | 5004 | // state is as optimistic as it gets. | 
|  | 5005 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 5006 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 5007 | *this, IRPosition::callsite_function(ICS)); | 
|  | 5008 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | 
|  | 5009 | return !isAtFixpoint(); | 
|  | 5010 | } | 
|  | 5011 |  | 
|  | 5012 | // Remove access kind modifiers if necessary. | 
|  | 5013 | if (I.mayReadFromMemory()) | 
|  | 5014 | removeAssumedBits(NO_READS); | 
|  | 5015 | if (I.mayWriteToMemory()) | 
|  | 5016 | removeAssumedBits(NO_WRITES); | 
|  | 5017 | return !isAtFixpoint(); | 
|  | 5018 | }; | 
|  | 5019 |  | 
|  | 5020 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) | 
|  | 5021 | return indicatePessimisticFixpoint(); | 
|  | 5022 |  | 
|  | 5023 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | 
|  | 5024 | : ChangeStatus::UNCHANGED; | 
|  | 5025 | } | 
|  | 5026 |  | 
|  | 5027 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { | 
|  | 5028 |  | 
|  | 5029 | const IRPosition &IRP = getIRPosition(); | 
|  | 5030 | const IRPosition &FnPos = IRPosition::function_scope(IRP); | 
|  | 5031 | AAMemoryBehavior::StateType &S = getState(); | 
|  | 5032 |  | 
|  | 5033 | // First, check the function scope. We take the known information and we avoid | 
|  | 5034 | // work if the assumed information implies the current assumed information for | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5035 | // this attribute. This is a valid for all but byval arguments. | 
|  | 5036 | Argument *Arg = IRP.getAssociatedArgument(); | 
|  | 5037 | AAMemoryBehavior::base_t FnMemAssumedState = | 
|  | 5038 | AAMemoryBehavior::StateType::getWorstState(); | 
|  | 5039 | if (!Arg || !Arg->hasByValAttr()) { | 
|  | 5040 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 5041 | FnMemAssumedState = FnMemAA.getAssumed(); | 
|  | 5042 | S.addKnownBits(FnMemAA.getKnown()); | 
|  | 5043 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) | 
|  | 5044 | return ChangeStatus::UNCHANGED; | 
|  | 5045 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5046 |  | 
|  | 5047 | // Make sure the value is not captured (except through "return"), if | 
|  | 5048 | // it is, any information derived would be irrelevant anyway as we cannot | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5049 | // check the potential aliases introduced by the capture. However, no need | 
|  | 5050 | // to fall back to anythign less optimistic than the function state. | 
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 5051 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( | 
|  | 5052 | *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5053 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5054 | S.intersectAssumedBits(FnMemAssumedState); | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5055 | return ChangeStatus::CHANGED; | 
|  | 5056 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5057 |  | 
|  | 5058 | // The current assumed state used to determine a change. | 
|  | 5059 | auto AssumedState = S.getAssumed(); | 
|  | 5060 |  | 
|  | 5061 | // Liveness information to exclude dead users. | 
|  | 5062 | // TODO: Take the FnPos once we have call site specific liveness information. | 
|  | 5063 | const auto &LivenessAA = A.getAAFor<AAIsDead>( | 
|  | 5064 | *this, IRPosition::function(*IRP.getAssociatedFunction())); | 
|  | 5065 |  | 
|  | 5066 | // Visit and expand uses until all are analyzed or a fixpoint is reached. | 
|  | 5067 | for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { | 
|  | 5068 | const Use *U = Uses[i]; | 
|  | 5069 | Instruction *UserI = cast<Instruction>(U->getUser()); | 
|  | 5070 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI | 
|  | 5071 | << " [Dead: " << (LivenessAA.isAssumedDead(UserI)) | 
|  | 5072 | << "]\n"); | 
|  | 5073 | if (LivenessAA.isAssumedDead(UserI)) | 
|  | 5074 | continue; | 
|  | 5075 |  | 
|  | 5076 | // Check if the users of UserI should also be visited. | 
|  | 5077 | if (followUsersOfUseIn(A, U, UserI)) | 
|  | 5078 | for (const Use &UserIUse : UserI->uses()) | 
|  | 5079 | Uses.insert(&UserIUse); | 
|  | 5080 |  | 
|  | 5081 | // If UserI might touch memory we analyze the use in detail. | 
|  | 5082 | if (UserI->mayReadOrWriteMemory()) | 
|  | 5083 | analyzeUseIn(A, U, UserI); | 
|  | 5084 | } | 
|  | 5085 |  | 
|  | 5086 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | 
|  | 5087 | : ChangeStatus::UNCHANGED; | 
|  | 5088 | } | 
|  | 5089 |  | 
|  | 5090 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, | 
|  | 5091 | const Instruction *UserI) { | 
|  | 5092 | // The loaded value is unrelated to the pointer argument, no need to | 
|  | 5093 | // follow the users of the load. | 
|  | 5094 | if (isa<LoadInst>(UserI)) | 
|  | 5095 | return false; | 
|  | 5096 |  | 
|  | 5097 | // By default we follow all uses assuming UserI might leak information on U, | 
|  | 5098 | // we have special handling for call sites operands though. | 
|  | 5099 | ImmutableCallSite ICS(UserI); | 
|  | 5100 | if (!ICS || !ICS.isArgOperand(U)) | 
|  | 5101 | return true; | 
|  | 5102 |  | 
|  | 5103 | // If the use is a call argument known not to be captured, the users of | 
|  | 5104 | // the call do not need to be visited because they have to be unrelated to | 
|  | 5105 | // the input. Note that this check is not trivial even though we disallow | 
|  | 5106 | // general capturing of the underlying argument. The reason is that the | 
|  | 5107 | // call might the argument "through return", which we allow and for which we | 
|  | 5108 | // need to check call users. | 
|  | 5109 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 5110 | const auto &ArgNoCaptureAA = | 
|  | 5111 | A.getAAFor<AANoCapture>(*this, IRPosition::callsite_argument(ICS, ArgNo)); | 
|  | 5112 | return !ArgNoCaptureAA.isAssumedNoCapture(); | 
|  | 5113 | } | 
|  | 5114 |  | 
|  | 5115 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, | 
|  | 5116 | const Instruction *UserI) { | 
|  | 5117 | assert(UserI->mayReadOrWriteMemory()); | 
|  | 5118 |  | 
|  | 5119 | switch (UserI->getOpcode()) { | 
|  | 5120 | default: | 
|  | 5121 | // TODO: Handle all atomics and other side-effect operations we know of. | 
|  | 5122 | break; | 
|  | 5123 | case Instruction::Load: | 
|  | 5124 | // Loads cause the NO_READS property to disappear. | 
|  | 5125 | removeAssumedBits(NO_READS); | 
|  | 5126 | return; | 
|  | 5127 |  | 
|  | 5128 | case Instruction::Store: | 
|  | 5129 | // Stores cause the NO_WRITES property to disappear if the use is the | 
|  | 5130 | // pointer operand. Note that we do assume that capturing was taken care of | 
|  | 5131 | // somewhere else. | 
|  | 5132 | if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) | 
|  | 5133 | removeAssumedBits(NO_WRITES); | 
|  | 5134 | return; | 
|  | 5135 |  | 
|  | 5136 | case Instruction::Call: | 
|  | 5137 | case Instruction::CallBr: | 
|  | 5138 | case Instruction::Invoke: { | 
|  | 5139 | // For call sites we look at the argument memory behavior attribute (this | 
|  | 5140 | // could be recursive!) in order to restrict our own state. | 
|  | 5141 | ImmutableCallSite ICS(UserI); | 
|  | 5142 |  | 
|  | 5143 | // Give up on operand bundles. | 
|  | 5144 | if (ICS.isBundleOperand(U)) { | 
|  | 5145 | indicatePessimisticFixpoint(); | 
|  | 5146 | return; | 
|  | 5147 | } | 
|  | 5148 |  | 
|  | 5149 | // Calling a function does read the function pointer, maybe write it if the | 
|  | 5150 | // function is self-modifying. | 
|  | 5151 | if (ICS.isCallee(U)) { | 
|  | 5152 | removeAssumedBits(NO_READS); | 
|  | 5153 | break; | 
|  | 5154 | } | 
|  | 5155 |  | 
|  | 5156 | // Adjust the possible access behavior based on the information on the | 
|  | 5157 | // argument. | 
|  | 5158 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 5159 | const IRPosition &ArgPos = IRPosition::callsite_argument(ICS, ArgNo); | 
|  | 5160 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); | 
|  | 5161 | // "assumed" has at most the same bits as the MemBehaviorAA assumed | 
|  | 5162 | // and at least "known". | 
|  | 5163 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | 
|  | 5164 | return; | 
|  | 5165 | } | 
|  | 5166 | }; | 
|  | 5167 |  | 
|  | 5168 | // Generally, look at the "may-properties" and adjust the assumed state if we | 
|  | 5169 | // did not trigger special handling before. | 
|  | 5170 | if (UserI->mayReadFromMemory()) | 
|  | 5171 | removeAssumedBits(NO_READS); | 
|  | 5172 | if (UserI->mayWriteToMemory()) | 
|  | 5173 | removeAssumedBits(NO_WRITES); | 
|  | 5174 | } | 
|  | 5175 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5176 | /// ---------------------------------------------------------------------------- | 
|  | 5177 | ///                               Attributor | 
|  | 5178 | /// ---------------------------------------------------------------------------- | 
|  | 5179 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5180 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, | 
|  | 5181 | const AAIsDead *LivenessAA) { | 
|  | 5182 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); | 
|  | 5183 | if (!CtxI) | 
|  | 5184 | return false; | 
|  | 5185 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5186 | // TODO: Find a good way to utilize fine and coarse grained liveness | 
|  | 5187 | // information. | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5188 | if (!LivenessAA) | 
|  | 5189 | LivenessAA = | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5190 | &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), | 
|  | 5191 | /* TrackDependence */ false); | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 5192 |  | 
|  | 5193 | // Don't check liveness for AAIsDead. | 
|  | 5194 | if (&AA == LivenessAA) | 
|  | 5195 | return false; | 
|  | 5196 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5197 | if (!LivenessAA->isAssumedDead(CtxI)) | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5198 | return false; | 
|  | 5199 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5200 | // We actually used liveness information so we have to record a dependence. | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5201 | recordDependence(*LivenessAA, AA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5202 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5203 | return true; | 
|  | 5204 | } | 
|  | 5205 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5206 | bool Attributor::checkForAllUses( | 
|  | 5207 | const function_ref<bool(const Use &, bool &)> &Pred, | 
|  | 5208 | const AbstractAttribute &QueryingAA, const Value &V) { | 
|  | 5209 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 5210 | SmallVector<const Use *, 16> Worklist; | 
|  | 5211 | SmallPtrSet<const Use *, 16> Visited; | 
|  | 5212 |  | 
|  | 5213 | for (const Use &U : V.uses()) | 
|  | 5214 | Worklist.push_back(&U); | 
|  | 5215 |  | 
|  | 5216 | LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() | 
|  | 5217 | << " initial uses to check\n"); | 
|  | 5218 |  | 
|  | 5219 | if (Worklist.empty()) | 
|  | 5220 | return true; | 
|  | 5221 |  | 
|  | 5222 | bool AnyDead = false; | 
|  | 5223 | const Function *ScopeFn = IRP.getAnchorScope(); | 
|  | 5224 | const auto *LivenessAA = | 
|  | 5225 | ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), | 
|  | 5226 | /* TrackDependence */ false) | 
|  | 5227 | : nullptr; | 
|  | 5228 |  | 
|  | 5229 | while (!Worklist.empty()) { | 
|  | 5230 | const Use *U = Worklist.pop_back_val(); | 
|  | 5231 | if (!Visited.insert(U).second) | 
|  | 5232 | continue; | 
|  | 5233 | LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << "\n"); | 
|  | 5234 | if (Instruction *UserI = dyn_cast<Instruction>(U->getUser())) | 
|  | 5235 | if (LivenessAA && LivenessAA->isAssumedDead(UserI)) { | 
|  | 5236 | LLVM_DEBUG(dbgs() << "[Attributor] Dead user: " << *UserI << ": " | 
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 5237 | << *LivenessAA << "\n"); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5238 | AnyDead = true; | 
|  | 5239 | continue; | 
|  | 5240 | } | 
|  | 5241 |  | 
|  | 5242 | bool Follow = false; | 
|  | 5243 | if (!Pred(*U, Follow)) | 
|  | 5244 | return false; | 
|  | 5245 | if (!Follow) | 
|  | 5246 | continue; | 
|  | 5247 | for (const Use &UU : U->getUser()->uses()) | 
|  | 5248 | Worklist.push_back(&UU); | 
|  | 5249 | } | 
|  | 5250 |  | 
|  | 5251 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5252 | recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5253 |  | 
|  | 5254 | return true; | 
|  | 5255 | } | 
|  | 5256 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5257 | bool Attributor::checkForAllCallSites( | 
|  | 5258 | const function_ref<bool(AbstractCallSite)> &Pred, | 
|  | 5259 | const AbstractAttribute &QueryingAA, bool RequireAllCallSites) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5260 | // We can try to determine information from | 
|  | 5261 | // the call sites. However, this is only possible all call sites are known, | 
|  | 5262 | // hence the function has internal linkage. | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5263 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 5264 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 5265 | if (!AssociatedFunction) { | 
|  | 5266 | LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP | 
|  | 5267 | << "\n"); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5268 | return false; | 
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 5269 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5270 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5271 | return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, | 
|  | 5272 | &QueryingAA); | 
|  | 5273 | } | 
|  | 5274 |  | 
|  | 5275 | bool Attributor::checkForAllCallSites( | 
|  | 5276 | const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, | 
|  | 5277 | bool RequireAllCallSites, const AbstractAttribute *QueryingAA) { | 
|  | 5278 | if (RequireAllCallSites && !Fn.hasLocalLinkage()) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5279 | LLVM_DEBUG( | 
|  | 5280 | dbgs() | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5281 | << "[Attributor] Function " << Fn.getName() | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5282 | << " has no internal linkage, hence not all call sites are known\n"); | 
|  | 5283 | return false; | 
|  | 5284 | } | 
|  | 5285 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5286 | for (const Use &U : Fn.uses()) { | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5287 | AbstractCallSite ACS(&U); | 
|  | 5288 | if (!ACS) { | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5289 | LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5290 | << " has non call site use " << *U.get() << " in " | 
|  | 5291 | << *U.getUser() << "\n"); | 
| Johannes Doerfert | 2d77b0c | 2019-11-01 22:35:18 -0500 | [diff] [blame] | 5292 | // BlockAddress users are allowed. | 
|  | 5293 | if (isa<BlockAddress>(U.getUser())) | 
|  | 5294 | continue; | 
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 5295 | return false; | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5296 | } | 
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 5297 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5298 | Instruction *I = ACS.getInstruction(); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5299 | Function *Caller = I->getFunction(); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 5300 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5301 | const auto *LivenessAA = | 
|  | 5302 | lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA, | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5303 | /* TrackDependence */ false); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 5304 |  | 
|  | 5305 | // Skip dead calls. | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5306 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5307 | // We actually used liveness information so we have to record a | 
|  | 5308 | // dependence. | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5309 | if (QueryingAA) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5310 | recordDependence(*LivenessAA, *QueryingAA, DepClassTy::OPTIONAL); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 5311 | continue; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5312 | } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5313 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5314 | const Use *EffectiveUse = | 
|  | 5315 | ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; | 
|  | 5316 | if (!ACS.isCallee(EffectiveUse)) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5317 | if (!RequireAllCallSites) | 
|  | 5318 | continue; | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5319 | LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5320 | << " is an invalid use of " << Fn.getName() << "\n"); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5321 | return false; | 
|  | 5322 | } | 
|  | 5323 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5324 | if (Pred(ACS)) | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5325 | continue; | 
|  | 5326 |  | 
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 5327 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5328 | << *ACS.getInstruction() << "\n"); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5329 | return false; | 
|  | 5330 | } | 
|  | 5331 |  | 
|  | 5332 | return true; | 
|  | 5333 | } | 
|  | 5334 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5335 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 5336 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5337 | &Pred, | 
|  | 5338 | const AbstractAttribute &QueryingAA) { | 
|  | 5339 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5340 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 5341 | // Since we need to provide return instructions we have to have an exact | 
|  | 5342 | // definition. | 
|  | 5343 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5344 | if (!AssociatedFunction) | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5345 | return false; | 
|  | 5346 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5347 | // If this is a call site query we use the call site specific return values | 
|  | 5348 | // and liveness information. | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5349 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 5350 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5351 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5352 | if (!AARetVal.getState().isValidState()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5353 | return false; | 
|  | 5354 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5355 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5356 | } | 
|  | 5357 |  | 
|  | 5358 | bool Attributor::checkForAllReturnedValues( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5359 | const function_ref<bool(Value &)> &Pred, | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5360 | const AbstractAttribute &QueryingAA) { | 
|  | 5361 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5362 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 5363 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5364 | if (!AssociatedFunction) | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5365 | return false; | 
|  | 5366 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5367 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 5368 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5369 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5370 | if (!AARetVal.getState().isValidState()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5371 | return false; | 
|  | 5372 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5373 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 5374 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 5375 | return Pred(RV); | 
|  | 5376 | }); | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5377 | } | 
|  | 5378 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5379 | static bool | 
|  | 5380 | checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap, | 
|  | 5381 | const function_ref<bool(Instruction &)> &Pred, | 
|  | 5382 | const AAIsDead *LivenessAA, bool &AnyDead, | 
|  | 5383 | const ArrayRef<unsigned> &Opcodes) { | 
|  | 5384 | for (unsigned Opcode : Opcodes) { | 
|  | 5385 | for (Instruction *I : OpcodeInstMap[Opcode]) { | 
|  | 5386 | // Skip dead instructions. | 
|  | 5387 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { | 
|  | 5388 | AnyDead = true; | 
|  | 5389 | continue; | 
|  | 5390 | } | 
|  | 5391 |  | 
|  | 5392 | if (!Pred(*I)) | 
|  | 5393 | return false; | 
|  | 5394 | } | 
|  | 5395 | } | 
|  | 5396 | return true; | 
|  | 5397 | } | 
|  | 5398 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5399 | bool Attributor::checkForAllInstructions( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5400 | const llvm::function_ref<bool(Instruction &)> &Pred, | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 5401 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5402 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5403 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 5404 | // Since we need to provide instructions we have to have an exact definition. | 
|  | 5405 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5406 | if (!AssociatedFunction) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5407 | return false; | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5408 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5409 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 5410 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5411 | const auto &LivenessAA = | 
|  | 5412 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); | 
|  | 5413 | bool AnyDead = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5414 |  | 
|  | 5415 | auto &OpcodeInstMap = | 
|  | 5416 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5417 | if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, | 
|  | 5418 | Opcodes)) | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5419 | return false; | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5420 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5421 | // If we actually used liveness information so we have to record a dependence. | 
|  | 5422 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5423 | recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5424 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5425 | return true; | 
|  | 5426 | } | 
|  | 5427 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5428 | bool Attributor::checkForAllReadWriteInstructions( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5429 | const llvm::function_ref<bool(Instruction &)> &Pred, | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 5430 | AbstractAttribute &QueryingAA) { | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5431 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5432 | const Function *AssociatedFunction = | 
|  | 5433 | QueryingAA.getIRPosition().getAssociatedFunction(); | 
|  | 5434 | if (!AssociatedFunction) | 
|  | 5435 | return false; | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5436 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5437 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 5438 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
|  | 5439 | const auto &LivenessAA = | 
|  | 5440 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5441 | bool AnyDead = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5442 |  | 
|  | 5443 | for (Instruction *I : | 
|  | 5444 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5445 | // Skip dead instructions. | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5446 | if (LivenessAA.isAssumedDead(I)) { | 
|  | 5447 | AnyDead = true; | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5448 | continue; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5449 | } | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5450 |  | 
|  | 5451 | if (!Pred(*I)) | 
|  | 5452 | return false; | 
|  | 5453 | } | 
|  | 5454 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5455 | // If we actually used liveness information so we have to record a dependence. | 
|  | 5456 | if (AnyDead) | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5457 | recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5458 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5459 | return true; | 
|  | 5460 | } | 
|  | 5461 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5462 | ChangeStatus Attributor::run(Module &M) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5463 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " | 
|  | 5464 | << AllAbstractAttributes.size() | 
|  | 5465 | << " abstract attributes.\n"); | 
|  | 5466 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 5467 | // Now that all abstract attributes are collected and initialized we start | 
|  | 5468 | // the abstract analysis. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5469 |  | 
|  | 5470 | unsigned IterationCounter = 1; | 
|  | 5471 |  | 
|  | 5472 | SmallVector<AbstractAttribute *, 64> ChangedAAs; | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5473 | SetVector<AbstractAttribute *> Worklist, InvalidAAs; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5474 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); | 
|  | 5475 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5476 | bool RecomputeDependences = false; | 
|  | 5477 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5478 | do { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5479 | // Remember the size to determine new attributes. | 
|  | 5480 | size_t NumAAs = AllAbstractAttributes.size(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5481 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter | 
|  | 5482 | << ", Worklist size: " << Worklist.size() << "\n"); | 
|  | 5483 |  | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5484 | // For invalid AAs we can fix dependent AAs that have a required dependence, | 
|  | 5485 | // thereby folding long dependence chains in a single step without the need | 
|  | 5486 | // to run updates. | 
|  | 5487 | for (unsigned u = 0; u < InvalidAAs.size(); ++u) { | 
|  | 5488 | AbstractAttribute *InvalidAA = InvalidAAs[u]; | 
|  | 5489 | auto &QuerriedAAs = QueryMap[InvalidAA]; | 
|  | 5490 | LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " | 
|  | 5491 | << QuerriedAAs.RequiredAAs.size() << "/" | 
|  | 5492 | << QuerriedAAs.OptionalAAs.size() | 
|  | 5493 | << " required/optional dependences\n"); | 
|  | 5494 | for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) { | 
|  | 5495 | AbstractState &DOIAAState = DepOnInvalidAA->getState(); | 
|  | 5496 | DOIAAState.indicatePessimisticFixpoint(); | 
|  | 5497 | ++NumAttributesFixedDueToRequiredDependences; | 
|  | 5498 | assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!"); | 
|  | 5499 | if (!DOIAAState.isValidState()) | 
|  | 5500 | InvalidAAs.insert(DepOnInvalidAA); | 
|  | 5501 | } | 
|  | 5502 | if (!RecomputeDependences) | 
|  | 5503 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), | 
|  | 5504 | QuerriedAAs.OptionalAAs.end()); | 
|  | 5505 | } | 
|  | 5506 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5507 | // If dependences (=QueryMap) are recomputed we have to look at all abstract | 
|  | 5508 | // attributes again, regardless of what changed in the last iteration. | 
|  | 5509 | if (RecomputeDependences) { | 
|  | 5510 | LLVM_DEBUG( | 
|  | 5511 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); | 
|  | 5512 | QueryMap.clear(); | 
|  | 5513 | ChangedAAs.clear(); | 
|  | 5514 | Worklist.insert(AllAbstractAttributes.begin(), | 
|  | 5515 | AllAbstractAttributes.end()); | 
|  | 5516 | } | 
|  | 5517 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5518 | // Add all abstract attributes that are potentially dependent on one that | 
|  | 5519 | // changed to the work list. | 
|  | 5520 | for (AbstractAttribute *ChangedAA : ChangedAAs) { | 
|  | 5521 | auto &QuerriedAAs = QueryMap[ChangedAA]; | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5522 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), | 
|  | 5523 | QuerriedAAs.OptionalAAs.end()); | 
|  | 5524 | Worklist.insert(QuerriedAAs.RequiredAAs.begin(), | 
|  | 5525 | QuerriedAAs.RequiredAAs.end()); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5526 | } | 
|  | 5527 |  | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 5528 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter | 
|  | 5529 | << ", Worklist+Dependent size: " << Worklist.size() | 
|  | 5530 | << "\n"); | 
|  | 5531 |  | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5532 | // Reset the changed and invalid set. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5533 | ChangedAAs.clear(); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5534 | InvalidAAs.clear(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5535 |  | 
|  | 5536 | // Update all abstract attribute in the work list and record the ones that | 
|  | 5537 | // changed. | 
|  | 5538 | for (AbstractAttribute *AA : Worklist) | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 5539 | if (!AA->getState().isAtFixpoint() && !isAssumedDead(*AA, nullptr)) { | 
|  | 5540 | QueriedNonFixAA = false; | 
|  | 5541 | if (AA->update(*this) == ChangeStatus::CHANGED) { | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5542 | ChangedAAs.push_back(AA); | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5543 | if (!AA->getState().isValidState()) | 
|  | 5544 | InvalidAAs.insert(AA); | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 5545 | } else if (!QueriedNonFixAA) { | 
|  | 5546 | // If the attribute did not query any non-fix information, the state | 
|  | 5547 | // will not change and we can indicate that right away. | 
|  | 5548 | AA->getState().indicateOptimisticFixpoint(); | 
|  | 5549 | } | 
|  | 5550 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5551 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5552 | // Check if we recompute the dependences in the next iteration. | 
|  | 5553 | RecomputeDependences = (DepRecomputeInterval > 0 && | 
|  | 5554 | IterationCounter % DepRecomputeInterval == 0); | 
|  | 5555 |  | 
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 5556 | // Add attributes to the changed set if they have been created in the last | 
|  | 5557 | // iteration. | 
|  | 5558 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, | 
|  | 5559 | AllAbstractAttributes.end()); | 
|  | 5560 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5561 | // Reset the work list and repopulate with the changed abstract attributes. | 
|  | 5562 | // Note that dependent ones are added above. | 
|  | 5563 | Worklist.clear(); | 
|  | 5564 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); | 
|  | 5565 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5566 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || | 
|  | 5567 | VerifyMaxFixpointIterations)); | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5568 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5569 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " | 
|  | 5570 | << IterationCounter << "/" << MaxFixpointIterations | 
|  | 5571 | << " iterations\n"); | 
|  | 5572 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5573 | size_t NumFinalAAs = AllAbstractAttributes.size(); | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 5574 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5575 | // Reset abstract arguments not settled in a sound fixpoint by now. This | 
|  | 5576 | // happens when we stopped the fixpoint iteration early. Note that only the | 
|  | 5577 | // ones marked as "changed" *and* the ones transitively depending on them | 
|  | 5578 | // need to be reverted to a pessimistic state. Others might not be in a | 
|  | 5579 | // fixpoint state but we can use the optimistic results for them anyway. | 
|  | 5580 | SmallPtrSet<AbstractAttribute *, 32> Visited; | 
|  | 5581 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { | 
|  | 5582 | AbstractAttribute *ChangedAA = ChangedAAs[u]; | 
|  | 5583 | if (!Visited.insert(ChangedAA).second) | 
|  | 5584 | continue; | 
|  | 5585 |  | 
|  | 5586 | AbstractState &State = ChangedAA->getState(); | 
|  | 5587 | if (!State.isAtFixpoint()) { | 
|  | 5588 | State.indicatePessimisticFixpoint(); | 
|  | 5589 |  | 
|  | 5590 | NumAttributesTimedOut++; | 
|  | 5591 | } | 
|  | 5592 |  | 
|  | 5593 | auto &QuerriedAAs = QueryMap[ChangedAA]; | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5594 | ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(), | 
|  | 5595 | QuerriedAAs.OptionalAAs.end()); | 
|  | 5596 | ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(), | 
|  | 5597 | QuerriedAAs.RequiredAAs.end()); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5598 | } | 
|  | 5599 |  | 
|  | 5600 | LLVM_DEBUG({ | 
|  | 5601 | if (!Visited.empty()) | 
|  | 5602 | dbgs() << "\n[Attributor] Finalized " << Visited.size() | 
|  | 5603 | << " abstract attributes.\n"; | 
|  | 5604 | }); | 
|  | 5605 |  | 
|  | 5606 | unsigned NumManifested = 0; | 
|  | 5607 | unsigned NumAtFixpoint = 0; | 
|  | 5608 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; | 
|  | 5609 | for (AbstractAttribute *AA : AllAbstractAttributes) { | 
|  | 5610 | AbstractState &State = AA->getState(); | 
|  | 5611 |  | 
|  | 5612 | // If there is not already a fixpoint reached, we can now take the | 
|  | 5613 | // optimistic state. This is correct because we enforced a pessimistic one | 
|  | 5614 | // on abstract attributes that were transitively dependent on a changed one | 
|  | 5615 | // already above. | 
|  | 5616 | if (!State.isAtFixpoint()) | 
|  | 5617 | State.indicateOptimisticFixpoint(); | 
|  | 5618 |  | 
|  | 5619 | // If the state is invalid, we do not try to manifest it. | 
|  | 5620 | if (!State.isValidState()) | 
|  | 5621 | continue; | 
|  | 5622 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5623 | // Skip dead code. | 
|  | 5624 | if (isAssumedDead(*AA, nullptr)) | 
|  | 5625 | continue; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5626 | // Manifest the state and record if we changed the IR. | 
|  | 5627 | ChangeStatus LocalChange = AA->manifest(*this); | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 5628 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) | 
|  | 5629 | AA->trackStatistics(); | 
|  | 5630 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5631 | ManifestChange = ManifestChange | LocalChange; | 
|  | 5632 |  | 
|  | 5633 | NumAtFixpoint++; | 
|  | 5634 | NumManifested += (LocalChange == ChangeStatus::CHANGED); | 
|  | 5635 | } | 
|  | 5636 |  | 
|  | 5637 | (void)NumManifested; | 
|  | 5638 | (void)NumAtFixpoint; | 
|  | 5639 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested | 
|  | 5640 | << " arguments while " << NumAtFixpoint | 
|  | 5641 | << " were in a valid fixpoint state\n"); | 
|  | 5642 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5643 | NumAttributesManifested += NumManifested; | 
|  | 5644 | NumAttributesValidFixpoint += NumAtFixpoint; | 
|  | 5645 |  | 
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 5646 | (void)NumFinalAAs; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5647 | assert( | 
|  | 5648 | NumFinalAAs == AllAbstractAttributes.size() && | 
|  | 5649 | "Expected the final number of abstract attributes to remain unchanged!"); | 
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 5650 |  | 
|  | 5651 | // 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] | 5652 | { | 
|  | 5653 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " | 
|  | 5654 | << ToBeDeletedFunctions.size() << " functions and " | 
|  | 5655 | << ToBeDeletedBlocks.size() << " blocks and " | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5656 | << ToBeDeletedInsts.size() << " instructions and " | 
|  | 5657 | << ToBeChangedUses.size() << " uses\n"); | 
|  | 5658 |  | 
|  | 5659 | SmallVector<Instruction *, 32> DeadInsts; | 
|  | 5660 | SmallVector<Instruction *, 32> TerminatorsToFold; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5661 |  | 
|  | 5662 | for (auto &It : ToBeChangedUses) { | 
|  | 5663 | Use *U = It.first; | 
|  | 5664 | Value *NewV = It.second; | 
|  | 5665 | Value *OldV = U->get(); | 
|  | 5666 | LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() | 
|  | 5667 | << " instead of " << *OldV << "\n"); | 
|  | 5668 | U->set(NewV); | 
|  | 5669 | if (Instruction *I = dyn_cast<Instruction>(OldV)) | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5670 | if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && | 
|  | 5671 | isInstructionTriviallyDead(I)) { | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5672 | DeadInsts.push_back(I); | 
|  | 5673 | } | 
|  | 5674 | if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { | 
|  | 5675 | Instruction *UserI = cast<Instruction>(U->getUser()); | 
|  | 5676 | if (isa<UndefValue>(NewV)) { | 
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 5677 | ToBeChangedToUnreachableInsts.insert(UserI); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5678 | } else { | 
|  | 5679 | TerminatorsToFold.push_back(UserI); | 
|  | 5680 | } | 
|  | 5681 | } | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5682 | } | 
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 5683 | for (Instruction *I : ToBeChangedToUnreachableInsts) | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5684 | changeToUnreachable(I, /* UseLLVMTrap */ false); | 
|  | 5685 | for (Instruction *I : TerminatorsToFold) | 
|  | 5686 | ConstantFoldTerminator(I->getParent()); | 
|  | 5687 |  | 
|  | 5688 | for (Instruction *I : ToBeDeletedInsts) { | 
|  | 5689 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | 
|  | 5690 | if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) | 
|  | 5691 | DeadInsts.push_back(I); | 
|  | 5692 | else | 
|  | 5693 | I->eraseFromParent(); | 
|  | 5694 | } | 
|  | 5695 |  | 
|  | 5696 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 5697 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5698 | if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { | 
|  | 5699 | SmallVector<BasicBlock *, 8> ToBeDeletedBBs; | 
|  | 5700 | ToBeDeletedBBs.reserve(NumDeadBlocks); | 
|  | 5701 | ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end()); | 
| Johannes Doerfert | 5e442a5 | 2019-10-30 17:34:59 -0500 | [diff] [blame] | 5702 | // Actually we do not delete the blocks but squash them into a single | 
|  | 5703 | // unreachable but untangling branches that jump here is something we need | 
|  | 5704 | // to do in a more generic way. | 
|  | 5705 | DetatchDeadBlocks(ToBeDeletedBBs, nullptr); | 
|  | 5706 | STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); | 
|  | 5707 | BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5708 | } | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 5709 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5710 | STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); | 
|  | 5711 | for (Function *Fn : ToBeDeletedFunctions) { | 
|  | 5712 | Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); | 
|  | 5713 | Fn->eraseFromParent(); | 
|  | 5714 | STATS_TRACK(AAIsDead, Function); | 
|  | 5715 | } | 
|  | 5716 |  | 
|  | 5717 | // Identify dead internal functions and delete them. This happens outside | 
|  | 5718 | // the other fixpoint analysis as we might treat potentially dead functions | 
|  | 5719 | // as live to lower the number of iterations. If they happen to be dead, the | 
|  | 5720 | // below fixpoint loop will identify and eliminate them. | 
|  | 5721 | SmallVector<Function *, 8> InternalFns; | 
|  | 5722 | for (Function &F : M) | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 5723 | if (F.hasLocalLinkage()) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5724 | InternalFns.push_back(&F); | 
|  | 5725 |  | 
|  | 5726 | bool FoundDeadFn = true; | 
|  | 5727 | while (FoundDeadFn) { | 
|  | 5728 | FoundDeadFn = false; | 
|  | 5729 | for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { | 
|  | 5730 | Function *F = InternalFns[u]; | 
|  | 5731 | if (!F) | 
|  | 5732 | continue; | 
|  | 5733 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5734 | if (!checkForAllCallSites([](AbstractCallSite ACS) { return false; }, | 
|  | 5735 | *F, true, nullptr)) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5736 | continue; | 
|  | 5737 |  | 
|  | 5738 | STATS_TRACK(AAIsDead, Function); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5739 | ToBeDeletedFunctions.insert(F); | 
| Johannes Doerfert | 2d77b0c | 2019-11-01 22:35:18 -0500 | [diff] [blame] | 5740 | F->deleteBody(); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5741 | F->replaceAllUsesWith(UndefValue::get(F->getType())); | 
|  | 5742 | F->eraseFromParent(); | 
|  | 5743 | InternalFns[u] = nullptr; | 
|  | 5744 | FoundDeadFn = true; | 
|  | 5745 | } | 
|  | 5746 | } | 
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 5747 | } | 
|  | 5748 |  | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5749 | // Rewrite the functions as requested during manifest. | 
|  | 5750 | ManifestChange = ManifestChange | rewriteFunctionSignatures(); | 
|  | 5751 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5752 | if (VerifyMaxFixpointIterations && | 
|  | 5753 | IterationCounter != MaxFixpointIterations) { | 
|  | 5754 | errs() << "\n[Attributor] Fixpoint iteration done after: " | 
|  | 5755 | << IterationCounter << "/" << MaxFixpointIterations | 
|  | 5756 | << " iterations\n"; | 
|  | 5757 | llvm_unreachable("The fixpoint was not reached with exactly the number of " | 
|  | 5758 | "specified iterations!"); | 
|  | 5759 | } | 
|  | 5760 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5761 | return ManifestChange; | 
|  | 5762 | } | 
|  | 5763 |  | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5764 | bool Attributor::registerFunctionSignatureRewrite( | 
|  | 5765 | Argument &Arg, ArrayRef<Type *> ReplacementTypes, | 
|  | 5766 | ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, | 
|  | 5767 | ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { | 
|  | 5768 |  | 
|  | 5769 | auto CallSiteCanBeChanged = [](AbstractCallSite ACS) { | 
|  | 5770 | // Forbid must-tail calls for now. | 
|  | 5771 | return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall(); | 
|  | 5772 | }; | 
|  | 5773 |  | 
|  | 5774 | Function *Fn = Arg.getParent(); | 
|  | 5775 | // Avoid var-arg functions for now. | 
|  | 5776 | if (Fn->isVarArg()) { | 
|  | 5777 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); | 
|  | 5778 | return false; | 
|  | 5779 | } | 
|  | 5780 |  | 
|  | 5781 | // Avoid functions with complicated argument passing semantics. | 
|  | 5782 | AttributeList FnAttributeList = Fn->getAttributes(); | 
|  | 5783 | if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || | 
|  | 5784 | FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || | 
|  | 5785 | FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) { | 
|  | 5786 | LLVM_DEBUG( | 
|  | 5787 | dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); | 
|  | 5788 | return false; | 
|  | 5789 | } | 
|  | 5790 |  | 
|  | 5791 | // Avoid callbacks for now. | 
|  | 5792 | if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr)) { | 
|  | 5793 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); | 
|  | 5794 | return false; | 
|  | 5795 | } | 
|  | 5796 |  | 
|  | 5797 | auto InstPred = [](Instruction &I) { | 
|  | 5798 | if (auto *CI = dyn_cast<CallInst>(&I)) | 
|  | 5799 | return !CI->isMustTailCall(); | 
|  | 5800 | return true; | 
|  | 5801 | }; | 
|  | 5802 |  | 
|  | 5803 | // Forbid must-tail calls for now. | 
|  | 5804 | // TODO: | 
|  | 5805 | bool AnyDead; | 
|  | 5806 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); | 
|  | 5807 | if (!checkForAllInstructionsImpl(OpcodeInstMap, InstPred, nullptr, AnyDead, | 
|  | 5808 | {Instruction::Call})) { | 
|  | 5809 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); | 
|  | 5810 | return false; | 
|  | 5811 | } | 
|  | 5812 |  | 
|  | 5813 | SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn]; | 
|  | 5814 | if (ARIs.size() == 0) | 
|  | 5815 | ARIs.resize(Fn->arg_size()); | 
|  | 5816 |  | 
|  | 5817 | // If we have a replacement already with less than or equal new arguments, | 
|  | 5818 | // ignore this request. | 
|  | 5819 | ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()]; | 
|  | 5820 | if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { | 
|  | 5821 | LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); | 
|  | 5822 | return false; | 
|  | 5823 | } | 
|  | 5824 |  | 
|  | 5825 | // If we have a replacement already but we like the new one better, delete | 
|  | 5826 | // the old. | 
|  | 5827 | if (ARI) | 
|  | 5828 | delete ARI; | 
|  | 5829 |  | 
|  | 5830 | // Remember the replacement. | 
|  | 5831 | ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, | 
|  | 5832 | std::move(CalleeRepairCB), | 
|  | 5833 | std::move(ACSRepairCB)); | 
|  | 5834 |  | 
|  | 5835 | return true; | 
|  | 5836 | } | 
|  | 5837 |  | 
|  | 5838 | ChangeStatus Attributor::rewriteFunctionSignatures() { | 
|  | 5839 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 5840 |  | 
|  | 5841 | for (auto &It : ArgumentReplacementMap) { | 
|  | 5842 | Function *OldFn = It.getFirst(); | 
|  | 5843 |  | 
|  | 5844 | // Deleted functions do not require rewrites. | 
|  | 5845 | if (ToBeDeletedFunctions.count(OldFn)) | 
|  | 5846 | continue; | 
|  | 5847 |  | 
|  | 5848 | const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond(); | 
|  | 5849 | assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); | 
|  | 5850 |  | 
|  | 5851 | SmallVector<Type *, 16> NewArgumentTypes; | 
|  | 5852 | SmallVector<AttributeSet, 16> NewArgumentAttributes; | 
|  | 5853 |  | 
|  | 5854 | // Collect replacement argument types and copy over existing attributes. | 
|  | 5855 | AttributeList OldFnAttributeList = OldFn->getAttributes(); | 
|  | 5856 | for (Argument &Arg : OldFn->args()) { | 
|  | 5857 | if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) { | 
|  | 5858 | NewArgumentTypes.append(ARI->ReplacementTypes.begin(), | 
|  | 5859 | ARI->ReplacementTypes.end()); | 
|  | 5860 | NewArgumentAttributes.append(ARI->getNumReplacementArgs(), | 
|  | 5861 | AttributeSet()); | 
|  | 5862 | } else { | 
|  | 5863 | NewArgumentTypes.push_back(Arg.getType()); | 
|  | 5864 | NewArgumentAttributes.push_back( | 
|  | 5865 | OldFnAttributeList.getParamAttributes(Arg.getArgNo())); | 
|  | 5866 | } | 
|  | 5867 | } | 
|  | 5868 |  | 
|  | 5869 | FunctionType *OldFnTy = OldFn->getFunctionType(); | 
|  | 5870 | Type *RetTy = OldFnTy->getReturnType(); | 
|  | 5871 |  | 
|  | 5872 | // Construct the new function type using the new arguments types. | 
|  | 5873 | FunctionType *NewFnTy = | 
|  | 5874 | FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); | 
|  | 5875 |  | 
|  | 5876 | LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() | 
|  | 5877 | << "' from " << *OldFn->getFunctionType() << " to " | 
|  | 5878 | << *NewFnTy << "\n"); | 
|  | 5879 |  | 
|  | 5880 | // Create the new function body and insert it into the module. | 
|  | 5881 | Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), | 
|  | 5882 | OldFn->getAddressSpace(), ""); | 
|  | 5883 | OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); | 
|  | 5884 | NewFn->takeName(OldFn); | 
|  | 5885 | NewFn->copyAttributesFrom(OldFn); | 
|  | 5886 |  | 
|  | 5887 | // Patch the pointer to LLVM function in debug info descriptor. | 
|  | 5888 | NewFn->setSubprogram(OldFn->getSubprogram()); | 
|  | 5889 | OldFn->setSubprogram(nullptr); | 
|  | 5890 |  | 
|  | 5891 | // Recompute the parameter attributes list based on the new arguments for | 
|  | 5892 | // the function. | 
|  | 5893 | LLVMContext &Ctx = OldFn->getContext(); | 
|  | 5894 | NewFn->setAttributes(AttributeList::get( | 
|  | 5895 | Ctx, OldFnAttributeList.getFnAttributes(), | 
|  | 5896 | OldFnAttributeList.getRetAttributes(), NewArgumentAttributes)); | 
|  | 5897 |  | 
|  | 5898 | // Since we have now created the new function, splice the body of the old | 
|  | 5899 | // function right into the new function, leaving the old rotting hulk of the | 
|  | 5900 | // function empty. | 
|  | 5901 | NewFn->getBasicBlockList().splice(NewFn->begin(), | 
|  | 5902 | OldFn->getBasicBlockList()); | 
|  | 5903 |  | 
|  | 5904 | // Set of all "call-like" instructions that invoke the old function. | 
|  | 5905 | SmallPtrSet<Instruction *, 8> OldCallSites; | 
|  | 5906 |  | 
|  | 5907 | // Callback to create a new "call-like" instruction for a given one. | 
|  | 5908 | auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { | 
|  | 5909 | CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); | 
|  | 5910 | const AttributeList &OldCallAttributeList = OldCB->getAttributes(); | 
|  | 5911 |  | 
|  | 5912 | // Collect the new argument operands for the replacement call site. | 
|  | 5913 | SmallVector<Value *, 16> NewArgOperands; | 
|  | 5914 | SmallVector<AttributeSet, 16> NewArgOperandAttributes; | 
|  | 5915 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { | 
|  | 5916 | unsigned NewFirstArgNum = NewArgOperands.size(); | 
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 5917 | (void)NewFirstArgNum; // only used inside assert. | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5918 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { | 
|  | 5919 | if (ARI->ACSRepairCB) | 
|  | 5920 | ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); | 
|  | 5921 | assert(ARI->getNumReplacementArgs() + NewFirstArgNum == | 
|  | 5922 | NewArgOperands.size() && | 
|  | 5923 | "ACS repair callback did not provide as many operand as new " | 
|  | 5924 | "types were registered!"); | 
|  | 5925 | // TODO: Exose the attribute set to the ACS repair callback | 
|  | 5926 | NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), | 
|  | 5927 | AttributeSet()); | 
|  | 5928 | } else { | 
|  | 5929 | NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); | 
|  | 5930 | NewArgOperandAttributes.push_back( | 
|  | 5931 | OldCallAttributeList.getParamAttributes(OldArgNum)); | 
|  | 5932 | } | 
|  | 5933 | } | 
|  | 5934 |  | 
|  | 5935 | assert(NewArgOperands.size() == NewArgOperandAttributes.size() && | 
|  | 5936 | "Mismatch # argument operands vs. # argument operand attributes!"); | 
|  | 5937 | assert(NewArgOperands.size() == NewFn->arg_size() && | 
|  | 5938 | "Mismatch # argument operands vs. # function arguments!"); | 
|  | 5939 |  | 
|  | 5940 | SmallVector<OperandBundleDef, 4> OperandBundleDefs; | 
|  | 5941 | OldCB->getOperandBundlesAsDefs(OperandBundleDefs); | 
|  | 5942 |  | 
|  | 5943 | // Create a new call or invoke instruction to replace the old one. | 
|  | 5944 | CallBase *NewCB; | 
|  | 5945 | if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { | 
|  | 5946 | NewCB = | 
|  | 5947 | InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), | 
|  | 5948 | NewArgOperands, OperandBundleDefs, "", OldCB); | 
|  | 5949 | } else { | 
|  | 5950 | auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, | 
|  | 5951 | "", OldCB); | 
|  | 5952 | NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); | 
|  | 5953 | NewCB = NewCI; | 
|  | 5954 | } | 
|  | 5955 |  | 
|  | 5956 | // Copy over various properties and the new attributes. | 
|  | 5957 | OldCB->replaceAllUsesWith(NewCB); | 
|  | 5958 | uint64_t W; | 
|  | 5959 | if (OldCB->extractProfTotalWeight(W)) | 
|  | 5960 | NewCB->setProfWeight(W); | 
|  | 5961 | NewCB->setCallingConv(OldCB->getCallingConv()); | 
|  | 5962 | NewCB->setDebugLoc(OldCB->getDebugLoc()); | 
|  | 5963 | NewCB->takeName(OldCB); | 
|  | 5964 | NewCB->setAttributes(AttributeList::get( | 
|  | 5965 | Ctx, OldCallAttributeList.getFnAttributes(), | 
|  | 5966 | OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes)); | 
|  | 5967 |  | 
|  | 5968 | bool Inserted = OldCallSites.insert(OldCB).second; | 
|  | 5969 | assert(Inserted && "Call site was old twice!"); | 
|  | 5970 | (void)Inserted; | 
|  | 5971 |  | 
|  | 5972 | return true; | 
|  | 5973 | }; | 
|  | 5974 |  | 
|  | 5975 | // Use the CallSiteReplacementCreator to create replacement call sites. | 
|  | 5976 | bool Success = | 
|  | 5977 | checkForAllCallSites(CallSiteReplacementCreator, *OldFn, true, nullptr); | 
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 5978 | (void)Success; | 
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5979 | assert(Success && "Assumed call site replacement to succeed!"); | 
|  | 5980 |  | 
|  | 5981 | // Rewire the arguments. | 
|  | 5982 | auto OldFnArgIt = OldFn->arg_begin(); | 
|  | 5983 | auto NewFnArgIt = NewFn->arg_begin(); | 
|  | 5984 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); | 
|  | 5985 | ++OldArgNum, ++OldFnArgIt) { | 
|  | 5986 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { | 
|  | 5987 | if (ARI->CalleeRepairCB) | 
|  | 5988 | ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); | 
|  | 5989 | NewFnArgIt += ARI->ReplacementTypes.size(); | 
|  | 5990 | } else { | 
|  | 5991 | NewFnArgIt->takeName(&*OldFnArgIt); | 
|  | 5992 | OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); | 
|  | 5993 | ++NewFnArgIt; | 
|  | 5994 | } | 
|  | 5995 | } | 
|  | 5996 |  | 
|  | 5997 | // Eliminate the instructions *after* we visited all of them. | 
|  | 5998 | for (Instruction *OldCallSite : OldCallSites) | 
|  | 5999 | OldCallSite->eraseFromParent(); | 
|  | 6000 |  | 
|  | 6001 | assert(OldFn->getNumUses() == 0 && "Unexpected leftover uses!"); | 
|  | 6002 | OldFn->eraseFromParent(); | 
|  | 6003 | Changed = ChangeStatus::CHANGED; | 
|  | 6004 | } | 
|  | 6005 |  | 
|  | 6006 | return Changed; | 
|  | 6007 | } | 
|  | 6008 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6009 | void Attributor::initializeInformationCache(Function &F) { | 
|  | 6010 |  | 
|  | 6011 | // Walk all instructions to find interesting instructions that might be | 
|  | 6012 | // queried by abstract attributes during their initialization or update. | 
|  | 6013 | // This has to happen before we create attributes. | 
|  | 6014 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; | 
|  | 6015 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; | 
|  | 6016 |  | 
|  | 6017 | for (Instruction &I : instructions(&F)) { | 
|  | 6018 | bool IsInterestingOpcode = false; | 
|  | 6019 |  | 
|  | 6020 | // To allow easy access to all instructions in a function with a given | 
|  | 6021 | // opcode we store them in the InfoCache. As not all opcodes are interesting | 
|  | 6022 | // to concrete attributes we only cache the ones that are as identified in | 
|  | 6023 | // the following switch. | 
|  | 6024 | // Note: There are no concrete attributes now so this is initially empty. | 
|  | 6025 | switch (I.getOpcode()) { | 
|  | 6026 | default: | 
|  | 6027 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && | 
|  | 6028 | "New call site/base instruction type needs to be known int the " | 
|  | 6029 | "Attributor."); | 
|  | 6030 | break; | 
|  | 6031 | case Instruction::Load: | 
|  | 6032 | // The alignment of a pointer is interesting for loads. | 
|  | 6033 | case Instruction::Store: | 
|  | 6034 | // The alignment of a pointer is interesting for stores. | 
|  | 6035 | case Instruction::Call: | 
|  | 6036 | case Instruction::CallBr: | 
|  | 6037 | case Instruction::Invoke: | 
|  | 6038 | case Instruction::CleanupRet: | 
|  | 6039 | case Instruction::CatchSwitch: | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 6040 | case Instruction::AtomicRMW: | 
|  | 6041 | case Instruction::AtomicCmpXchg: | 
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 6042 | case Instruction::Br: | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6043 | case Instruction::Resume: | 
|  | 6044 | case Instruction::Ret: | 
|  | 6045 | IsInterestingOpcode = true; | 
|  | 6046 | } | 
|  | 6047 | if (IsInterestingOpcode) | 
|  | 6048 | InstOpcodeMap[I.getOpcode()].push_back(&I); | 
|  | 6049 | if (I.mayReadOrWriteMemory()) | 
|  | 6050 | ReadOrWriteInsts.push_back(&I); | 
|  | 6051 | } | 
|  | 6052 | } | 
|  | 6053 |  | 
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 6054 | void Attributor::recordDependence(const AbstractAttribute &FromAA, | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6055 | const AbstractAttribute &ToAA, | 
|  | 6056 | DepClassTy DepClass) { | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 6057 | if (FromAA.getState().isAtFixpoint()) | 
|  | 6058 | return; | 
|  | 6059 |  | 
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6060 | if (DepClass == DepClassTy::REQUIRED) | 
|  | 6061 | QueryMap[&FromAA].RequiredAAs.insert( | 
|  | 6062 | const_cast<AbstractAttribute *>(&ToAA)); | 
|  | 6063 | else | 
|  | 6064 | QueryMap[&FromAA].OptionalAAs.insert( | 
|  | 6065 | const_cast<AbstractAttribute *>(&ToAA)); | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 6066 | QueriedNonFixAA = true; | 
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 6067 | } | 
|  | 6068 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6069 | void Attributor::identifyDefaultAbstractAttributes(Function &F) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6070 | if (!VisitedFunctions.insert(&F).second) | 
|  | 6071 | return; | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 6072 | if (F.isDeclaration()) | 
|  | 6073 | return; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6074 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6075 | IRPosition FPos = IRPosition::function(F); | 
|  | 6076 |  | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 6077 | // Check for dead BasicBlocks in every function. | 
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 6078 | // We need dead instruction detection because we do not want to deal with | 
|  | 6079 | // broken IR in which SSA rules do not apply. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6080 | getOrCreateAAFor<AAIsDead>(FPos); | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 6081 |  | 
|  | 6082 | // Every function might be "will-return". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6083 | getOrCreateAAFor<AAWillReturn>(FPos); | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 6084 |  | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 6085 | // Every function might contain instructions that cause "undefined behavior". | 
|  | 6086 | getOrCreateAAFor<AAUndefinedBehavior>(FPos); | 
|  | 6087 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 6088 | // Every function can be nounwind. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6089 | getOrCreateAAFor<AANoUnwind>(FPos); | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 6090 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 6091 | // Every function might be marked "nosync" | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6092 | getOrCreateAAFor<AANoSync>(FPos); | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 6093 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 6094 | // Every function might be "no-free". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6095 | getOrCreateAAFor<AANoFree>(FPos); | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 6096 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 6097 | // Every function might be "no-return". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6098 | getOrCreateAAFor<AANoReturn>(FPos); | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 6099 |  | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 6100 | // Every function might be "no-recurse". | 
|  | 6101 | getOrCreateAAFor<AANoRecurse>(FPos); | 
|  | 6102 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6103 | // Every function might be "readnone/readonly/writeonly/...". | 
|  | 6104 | getOrCreateAAFor<AAMemoryBehavior>(FPos); | 
|  | 6105 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6106 | // Every function might be applicable for Heap-To-Stack conversion. | 
|  | 6107 | if (EnableHeapToStack) | 
|  | 6108 | getOrCreateAAFor<AAHeapToStack>(FPos); | 
|  | 6109 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 6110 | // Return attributes are only appropriate if the return type is non void. | 
|  | 6111 | Type *ReturnType = F.getReturnType(); | 
|  | 6112 | if (!ReturnType->isVoidTy()) { | 
|  | 6113 | // Argument attribute "returned" --- Create only one per function even | 
|  | 6114 | // though it is an argument attribute. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6115 | getOrCreateAAFor<AAReturnedValues>(FPos); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6116 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6117 | IRPosition RetPos = IRPosition::returned(F); | 
|  | 6118 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6119 | // Every returned value might be dead. | 
|  | 6120 | getOrCreateAAFor<AAIsDead>(RetPos); | 
|  | 6121 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6122 | // Every function might be simplified. | 
|  | 6123 | getOrCreateAAFor<AAValueSimplify>(RetPos); | 
|  | 6124 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6125 | if (ReturnType->isPointerTy()) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6126 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6127 | // Every function with pointer return type might be marked align. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6128 | getOrCreateAAFor<AAAlign>(RetPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6129 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6130 | // Every function with pointer return type might be marked nonnull. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6131 | getOrCreateAAFor<AANonNull>(RetPos); | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6132 |  | 
|  | 6133 | // Every function with pointer return type might be marked noalias. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6134 | getOrCreateAAFor<AANoAlias>(RetPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6135 |  | 
|  | 6136 | // Every function with pointer return type might be marked | 
|  | 6137 | // dereferenceable. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6138 | getOrCreateAAFor<AADereferenceable>(RetPos); | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6139 | } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6140 | } | 
|  | 6141 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6142 | for (Argument &Arg : F.args()) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6143 | IRPosition ArgPos = IRPosition::argument(Arg); | 
|  | 6144 |  | 
|  | 6145 | // Every argument might be simplified. | 
|  | 6146 | getOrCreateAAFor<AAValueSimplify>(ArgPos); | 
|  | 6147 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6148 | if (Arg.getType()->isPointerTy()) { | 
|  | 6149 | // Every argument with pointer type might be marked nonnull. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6150 | getOrCreateAAFor<AANonNull>(ArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6151 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6152 | // Every argument with pointer type might be marked noalias. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6153 | getOrCreateAAFor<AANoAlias>(ArgPos); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6154 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6155 | // Every argument with pointer type might be marked dereferenceable. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6156 | getOrCreateAAFor<AADereferenceable>(ArgPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6157 |  | 
|  | 6158 | // Every argument with pointer type might be marked align. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6159 | getOrCreateAAFor<AAAlign>(ArgPos); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 6160 |  | 
|  | 6161 | // Every argument with pointer type might be marked nocapture. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6162 | getOrCreateAAFor<AANoCapture>(ArgPos); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6163 |  | 
|  | 6164 | // Every argument with pointer type might be marked | 
|  | 6165 | // "readnone/readonly/writeonly/..." | 
|  | 6166 | getOrCreateAAFor<AAMemoryBehavior>(ArgPos); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6167 |  | 
|  | 6168 | // Every argument with pointer type might be marked nofree. | 
|  | 6169 | getOrCreateAAFor<AANoFree>(ArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6170 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 6171 | } | 
|  | 6172 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6173 | auto CallSitePred = [&](Instruction &I) -> bool { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6174 | CallSite CS(&I); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6175 | if (Function *Callee = CS.getCalledFunction()) { | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 6176 | // Skip declerations except if annotations on their call sites were | 
|  | 6177 | // explicitly requested. | 
| Johannes Doerfert | 139c9ef | 2019-12-13 23:41:02 -0600 | [diff] [blame] | 6178 | if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && | 
|  | 6179 | !Callee->hasMetadata(LLVMContext::MD_callback)) | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 6180 | return true; | 
|  | 6181 |  | 
|  | 6182 | if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6183 | IRPosition CSRetPos = IRPosition::callsite_returned(CS); | 
|  | 6184 |  | 
|  | 6185 | // Call site return values might be dead. | 
|  | 6186 | getOrCreateAAFor<AAIsDead>(CSRetPos); | 
|  | 6187 | } | 
|  | 6188 |  | 
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 6189 | for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6190 |  | 
|  | 6191 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); | 
|  | 6192 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6193 | // Every call site argument might be dead. | 
|  | 6194 | getOrCreateAAFor<AAIsDead>(CSArgPos); | 
|  | 6195 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6196 | // Call site argument might be simplified. | 
|  | 6197 | getOrCreateAAFor<AAValueSimplify>(CSArgPos); | 
|  | 6198 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6199 | if (!CS.getArgument(i)->getType()->isPointerTy()) | 
|  | 6200 | continue; | 
|  | 6201 |  | 
|  | 6202 | // Call site argument attribute "non-null". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6203 | getOrCreateAAFor<AANonNull>(CSArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6204 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6205 | // Call site argument attribute "no-alias". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6206 | getOrCreateAAFor<AANoAlias>(CSArgPos); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6207 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6208 | // Call site argument attribute "dereferenceable". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6209 | getOrCreateAAFor<AADereferenceable>(CSArgPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6210 |  | 
|  | 6211 | // Call site argument attribute "align". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6212 | getOrCreateAAFor<AAAlign>(CSArgPos); | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6213 |  | 
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 6214 | // Call site argument attribute | 
|  | 6215 | // "readnone/readonly/writeonly/..." | 
|  | 6216 | getOrCreateAAFor<AAMemoryBehavior>(CSArgPos); | 
|  | 6217 |  | 
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 6218 | // Call site argument attribute "nofree". | 
|  | 6219 | getOrCreateAAFor<AANoFree>(CSArgPos); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6220 | } | 
|  | 6221 | } | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6222 | return true; | 
|  | 6223 | }; | 
|  | 6224 |  | 
|  | 6225 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); | 
|  | 6226 | bool Success, AnyDead = false; | 
|  | 6227 | Success = checkForAllInstructionsImpl( | 
|  | 6228 | OpcodeInstMap, CallSitePred, nullptr, AnyDead, | 
|  | 6229 | {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, | 
|  | 6230 | (unsigned)Instruction::Call}); | 
|  | 6231 | (void)Success; | 
|  | 6232 | assert(Success && !AnyDead && "Expected the check call to be successful!"); | 
|  | 6233 |  | 
|  | 6234 | auto LoadStorePred = [&](Instruction &I) -> bool { | 
|  | 6235 | if (isa<LoadInst>(I)) | 
|  | 6236 | getOrCreateAAFor<AAAlign>( | 
|  | 6237 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); | 
|  | 6238 | else | 
|  | 6239 | getOrCreateAAFor<AAAlign>( | 
|  | 6240 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); | 
|  | 6241 | return true; | 
|  | 6242 | }; | 
|  | 6243 | Success = checkForAllInstructionsImpl( | 
|  | 6244 | OpcodeInstMap, LoadStorePred, nullptr, AnyDead, | 
|  | 6245 | {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); | 
|  | 6246 | (void)Success; | 
|  | 6247 | assert(Success && !AnyDead && "Expected the check call to be successful!"); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6248 | } | 
|  | 6249 |  | 
|  | 6250 | /// Helpers to ease debugging through output streams and print calls. | 
|  | 6251 | /// | 
|  | 6252 | ///{ | 
|  | 6253 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { | 
|  | 6254 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); | 
|  | 6255 | } | 
|  | 6256 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6257 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6258 | switch (AP) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6259 | case IRPosition::IRP_INVALID: | 
|  | 6260 | return OS << "inv"; | 
|  | 6261 | case IRPosition::IRP_FLOAT: | 
|  | 6262 | return OS << "flt"; | 
|  | 6263 | case IRPosition::IRP_RETURNED: | 
|  | 6264 | return OS << "fn_ret"; | 
|  | 6265 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
|  | 6266 | return OS << "cs_ret"; | 
|  | 6267 | case IRPosition::IRP_FUNCTION: | 
|  | 6268 | return OS << "fn"; | 
|  | 6269 | case IRPosition::IRP_CALL_SITE: | 
|  | 6270 | return OS << "cs"; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6271 | case IRPosition::IRP_ARGUMENT: | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6272 | return OS << "arg"; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6273 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6274 | return OS << "cs_arg"; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6275 | } | 
|  | 6276 | llvm_unreachable("Unknown attribute position!"); | 
|  | 6277 | } | 
|  | 6278 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6279 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6280 | const Value &AV = Pos.getAssociatedValue(); | 
|  | 6281 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6282 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; | 
|  | 6283 | } | 
|  | 6284 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 6285 | template <typename base_ty, base_ty BestState, base_ty WorstState> | 
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 6286 | raw_ostream &llvm:: | 
|  | 6287 | operator<<(raw_ostream &OS, | 
|  | 6288 | const IntegerStateBase<base_ty, BestState, WorstState> &S) { | 
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 6289 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" | 
|  | 6290 | << static_cast<const AbstractState &>(S); | 
|  | 6291 | } | 
|  | 6292 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6293 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { | 
|  | 6294 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); | 
|  | 6295 | } | 
|  | 6296 |  | 
|  | 6297 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { | 
|  | 6298 | AA.print(OS); | 
|  | 6299 | return OS; | 
|  | 6300 | } | 
|  | 6301 |  | 
|  | 6302 | void AbstractAttribute::print(raw_ostream &OS) const { | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6303 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() | 
|  | 6304 | << "]"; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6305 | } | 
|  | 6306 | ///} | 
|  | 6307 |  | 
|  | 6308 | /// ---------------------------------------------------------------------------- | 
|  | 6309 | ///                       Pass (Manager) Boilerplate | 
|  | 6310 | /// ---------------------------------------------------------------------------- | 
|  | 6311 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6312 | static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6313 | if (DisableAttributor) | 
|  | 6314 | return false; | 
|  | 6315 |  | 
|  | 6316 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() | 
|  | 6317 | << " functions.\n"); | 
|  | 6318 |  | 
|  | 6319 | // Create an Attributor and initially empty information cache that is filled | 
|  | 6320 | // while we identify default attribute opportunities. | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 6321 | InformationCache InfoCache(M, AG); | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 6322 | Attributor A(InfoCache, DepRecInterval); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6323 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6324 | for (Function &F : M) | 
|  | 6325 | A.initializeInformationCache(F); | 
|  | 6326 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6327 | for (Function &F : M) { | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 6328 | if (F.hasExactDefinition()) | 
|  | 6329 | NumFnWithExactDefinition++; | 
|  | 6330 | else | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6331 | NumFnWithoutExactDefinition++; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6332 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6333 | // We look at internal functions only on-demand but if any use is not a | 
|  | 6334 | // direct call, we have to do it eagerly. | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 6335 | if (F.hasLocalLinkage()) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6336 | if (llvm::all_of(F.uses(), [](const Use &U) { | 
|  | 6337 | return ImmutableCallSite(U.getUser()) && | 
|  | 6338 | ImmutableCallSite(U.getUser()).isCallee(&U); | 
|  | 6339 | })) | 
|  | 6340 | continue; | 
|  | 6341 | } | 
|  | 6342 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6343 | // Populate the Attributor with abstract attribute opportunities in the | 
|  | 6344 | // function and the information cache with IR information. | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6345 | A.identifyDefaultAbstractAttributes(F); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6346 | } | 
|  | 6347 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6348 | return A.run(M) == ChangeStatus::CHANGED; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6349 | } | 
|  | 6350 |  | 
|  | 6351 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 6352 | AnalysisGetter AG(AM); | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6353 | if (runAttributorOnModule(M, AG)) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6354 | // FIXME: Think about passes we will preserve and add them here. | 
|  | 6355 | return PreservedAnalyses::none(); | 
|  | 6356 | } | 
|  | 6357 | return PreservedAnalyses::all(); | 
|  | 6358 | } | 
|  | 6359 |  | 
|  | 6360 | namespace { | 
|  | 6361 |  | 
|  | 6362 | struct AttributorLegacyPass : public ModulePass { | 
|  | 6363 | static char ID; | 
|  | 6364 |  | 
|  | 6365 | AttributorLegacyPass() : ModulePass(ID) { | 
|  | 6366 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); | 
|  | 6367 | } | 
|  | 6368 |  | 
|  | 6369 | bool runOnModule(Module &M) override { | 
|  | 6370 | if (skipModule(M)) | 
|  | 6371 | return false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6372 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6373 | AnalysisGetter AG; | 
|  | 6374 | return runAttributorOnModule(M, AG); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6375 | } | 
|  | 6376 |  | 
|  | 6377 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 6378 | // FIXME: Think about passes we will preserve and add them here. | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6379 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6380 | } | 
|  | 6381 | }; | 
|  | 6382 |  | 
|  | 6383 | } // end anonymous namespace | 
|  | 6384 |  | 
|  | 6385 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } | 
|  | 6386 |  | 
|  | 6387 | char AttributorLegacyPass::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6388 |  | 
|  | 6389 | const char AAReturnedValues::ID = 0; | 
|  | 6390 | const char AANoUnwind::ID = 0; | 
|  | 6391 | const char AANoSync::ID = 0; | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 6392 | const char AANoFree::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6393 | const char AANonNull::ID = 0; | 
|  | 6394 | const char AANoRecurse::ID = 0; | 
|  | 6395 | const char AAWillReturn::ID = 0; | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 6396 | const char AAUndefinedBehavior::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6397 | const char AANoAlias::ID = 0; | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 6398 | const char AAReachability::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6399 | const char AANoReturn::ID = 0; | 
|  | 6400 | const char AAIsDead::ID = 0; | 
|  | 6401 | const char AADereferenceable::ID = 0; | 
|  | 6402 | const char AAAlign::ID = 0; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 6403 | const char AANoCapture::ID = 0; | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6404 | const char AAValueSimplify::ID = 0; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6405 | const char AAHeapToStack::ID = 0; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6406 | const char AAMemoryBehavior::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6407 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6408 | // Macro magic to create the static generator function for attributes that | 
|  | 6409 | // follow the naming scheme. | 
|  | 6410 |  | 
|  | 6411 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \ | 
|  | 6412 | case IRPosition::PK:                                                         \ | 
|  | 6413 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); | 
|  | 6414 |  | 
|  | 6415 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \ | 
|  | 6416 | case IRPosition::PK:                                                         \ | 
|  | 6417 | AA = new CLASS##SUFFIX(IRP);                                               \ | 
|  | 6418 | break; | 
|  | 6419 |  | 
|  | 6420 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \ | 
|  | 6421 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 6422 | CLASS *AA = nullptr;                                                       \ | 
|  | 6423 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 6424 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 6425 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \ | 
|  | 6426 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \ | 
|  | 6427 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 6428 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \ | 
|  | 6429 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \ | 
|  | 6430 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 6431 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 6432 | }                                                                          \ | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6433 | return *AA;                                                                \ | 
|  | 6434 | } | 
|  | 6435 |  | 
|  | 6436 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \ | 
|  | 6437 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 6438 | CLASS *AA = nullptr;                                                       \ | 
|  | 6439 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 6440 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 6441 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \ | 
|  | 6442 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \ | 
|  | 6443 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 6444 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 6445 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \ | 
|  | 6446 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 6447 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 6448 | }                                                                          \ | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6449 | return *AA;                                                                \ | 
|  | 6450 | } | 
|  | 6451 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6452 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \ | 
|  | 6453 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 6454 | CLASS *AA = nullptr;                                                       \ | 
|  | 6455 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 6456 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 6457 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 6458 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 6459 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 6460 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 6461 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \ | 
|  | 6462 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 6463 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 6464 | }                                                                          \ | 
|  | 6465 | return *AA;                                                                \ | 
|  | 6466 | } | 
|  | 6467 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6468 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \ | 
|  | 6469 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 6470 | CLASS *AA = nullptr;                                                       \ | 
|  | 6471 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 6472 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 6473 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \ | 
|  | 6474 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \ | 
|  | 6475 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 6476 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \ | 
|  | 6477 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \ | 
|  | 6478 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \ | 
|  | 6479 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 6480 | }                                                                          \ | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6481 | return *AA;                                                                \ | 
|  | 6482 | } | 
|  | 6483 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6484 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \ | 
|  | 6485 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 6486 | CLASS *AA = nullptr;                                                       \ | 
|  | 6487 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 6488 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 6489 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 6490 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 6491 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 6492 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 6493 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 6494 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 6495 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 6496 | }                                                                          \ | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6497 | return *AA;                                                                \ | 
|  | 6498 | } | 
|  | 6499 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6500 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) | 
|  | 6501 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6502 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) | 
|  | 6503 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) | 
|  | 6504 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6505 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) | 
|  | 6506 |  | 
|  | 6507 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) | 
|  | 6508 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) | 
|  | 6509 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) | 
|  | 6510 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 6511 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6512 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6513 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6514 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) | 
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6515 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6516 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6517 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) | 
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 6518 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) | 
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 6519 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6520 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6521 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) | 
|  | 6522 |  | 
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 6523 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6524 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 6525 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6526 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6527 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6528 | #undef SWITCH_PK_CREATE | 
|  | 6529 | #undef SWITCH_PK_INV | 
|  | 6530 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6531 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", | 
|  | 6532 | "Deduce and propagate attributes", false, false) | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6533 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6534 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", | 
|  | 6535 | "Deduce and propagate attributes", false, false) |