| 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 | } |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1795 | |
| 1796 | return 0; |
| 1797 | } |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1798 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1799 | struct AANonNullImpl : AANonNull { |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1800 | AANonNullImpl(const IRPosition &IRP) |
| 1801 | : AANonNull(IRP), |
| 1802 | NullIsDefined(NullPointerIsDefined( |
| 1803 | getAnchorScope(), |
| 1804 | getAssociatedValue().getType()->getPointerAddressSpace())) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1805 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1806 | /// See AbstractAttribute::initialize(...). |
| 1807 | void initialize(Attributor &A) override { |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1808 | if (!NullIsDefined && |
| 1809 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1810 | indicateOptimisticFixpoint(); |
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1811 | else if (isa<ConstantPointerNull>(getAssociatedValue())) |
| 1812 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1813 | else |
| 1814 | AANonNull::initialize(A); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1817 | /// See AAFromMustBeExecutedContext |
| 1818 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { |
| 1819 | bool IsNonNull = false; |
| 1820 | bool TrackUse = false; |
| 1821 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, |
| 1822 | IsNonNull, TrackUse); |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1823 | setKnown(IsNonNull); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1824 | return TrackUse; |
| 1825 | } |
| 1826 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1827 | /// See AbstractAttribute::getAsStr(). |
| 1828 | const std::string getAsStr() const override { |
| 1829 | return getAssumed() ? "nonnull" : "may-null"; |
| 1830 | } |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1831 | |
| 1832 | /// Flag to determine if the underlying value can be null and still allow |
| 1833 | /// valid accesses. |
| 1834 | const bool NullIsDefined; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1835 | }; |
| 1836 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1837 | /// NonNull attribute for a floating value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1838 | struct AANonNullFloating |
| 1839 | : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { |
| 1840 | using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; |
| 1841 | AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1842 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1843 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1844 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1845 | ChangeStatus Change = Base::updateImpl(A); |
| 1846 | if (isKnownNonNull()) |
| 1847 | return Change; |
| 1848 | |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1849 | if (!NullIsDefined) { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 1850 | const auto &DerefAA = |
| 1851 | A.getAAFor<AADereferenceable>(*this, getIRPosition()); |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1852 | if (DerefAA.getAssumedDereferenceableBytes()) |
| 1853 | return Change; |
| 1854 | } |
| 1855 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1856 | const DataLayout &DL = A.getDataLayout(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1857 | |
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1858 | DominatorTree *DT = nullptr; |
| 1859 | InformationCache &InfoCache = A.getInfoCache(); |
| 1860 | if (const Function *Fn = getAnchorScope()) |
| 1861 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); |
| 1862 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1863 | auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1864 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1865 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); |
| 1866 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1867 | if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT)) |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1868 | T.indicatePessimisticFixpoint(); |
| 1869 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1870 | // Use abstract attribute information. |
| 1871 | const AANonNull::StateType &NS = |
| 1872 | static_cast<const AANonNull::StateType &>(AA.getState()); |
| 1873 | T ^= NS; |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1874 | } |
| 1875 | return T.isValidState(); |
| 1876 | }; |
| 1877 | |
| 1878 | StateType T; |
| 1879 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, |
| 1880 | T, VisitValueCB)) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1881 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1882 | |
| 1883 | return clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1884 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1885 | |
| 1886 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1887 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1888 | }; |
| 1889 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1890 | /// NonNull attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1891 | struct AANonNullReturned final |
| 1892 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1893 | AANonNullReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1894 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1895 | |
| 1896 | /// See AbstractAttribute::trackStatistics() |
| 1897 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| 1898 | }; |
| 1899 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1900 | /// NonNull attribute for function argument. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1901 | struct AANonNullArgument final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1902 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, |
| 1903 | AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1904 | AANonNullArgument(const IRPosition &IRP) |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1905 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, |
| 1906 | AANonNullImpl>( |
| 1907 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1908 | |
| 1909 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1910 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1911 | }; |
| 1912 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1913 | struct AANonNullCallSiteArgument final : AANonNullFloating { |
| 1914 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1915 | |
| 1916 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 1917 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1918 | }; |
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1919 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1920 | /// NonNull attribute for a call site return position. |
| 1921 | struct AANonNullCallSiteReturned final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1922 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, |
| 1923 | AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1924 | AANonNullCallSiteReturned(const IRPosition &IRP) |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1925 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, |
| 1926 | AANonNullImpl>( |
| 1927 | IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1928 | |
| 1929 | /// See AbstractAttribute::trackStatistics() |
| 1930 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } |
| 1931 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1932 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1933 | /// ------------------------ No-Recurse Attributes ---------------------------- |
| 1934 | |
| 1935 | struct AANoRecurseImpl : public AANoRecurse { |
| 1936 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} |
| 1937 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1938 | /// See AbstractAttribute::getAsStr() |
| 1939 | const std::string getAsStr() const override { |
| 1940 | return getAssumed() ? "norecurse" : "may-recurse"; |
| 1941 | } |
| 1942 | }; |
| 1943 | |
| 1944 | struct AANoRecurseFunction final : AANoRecurseImpl { |
| 1945 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 1946 | |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1947 | /// See AbstractAttribute::initialize(...). |
| 1948 | void initialize(Attributor &A) override { |
| 1949 | AANoRecurseImpl::initialize(A); |
| 1950 | if (const Function *F = getAnchorScope()) |
| 1951 | if (A.getInfoCache().getSccSize(*F) == 1) |
| 1952 | return; |
| 1953 | indicatePessimisticFixpoint(); |
| 1954 | } |
| 1955 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1956 | /// See AbstractAttribute::updateImpl(...). |
| 1957 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1958 | |
| 1959 | auto CheckForNoRecurse = [&](Instruction &I) { |
| 1960 | ImmutableCallSite ICS(&I); |
| 1961 | if (ICS.hasFnAttr(Attribute::NoRecurse)) |
| 1962 | return true; |
| 1963 | |
| 1964 | const auto &NoRecurseAA = |
| 1965 | A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); |
| 1966 | if (!NoRecurseAA.isAssumedNoRecurse()) |
| 1967 | return false; |
| 1968 | |
| 1969 | // Recursion to the same function |
| 1970 | if (ICS.getCalledFunction() == getAnchorScope()) |
| 1971 | return false; |
| 1972 | |
| 1973 | return true; |
| 1974 | }; |
| 1975 | |
| 1976 | if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) |
| 1977 | return indicatePessimisticFixpoint(); |
| 1978 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } |
| 1982 | }; |
| 1983 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1984 | /// NoRecurse attribute deduction for a call sites. |
| 1985 | struct AANoRecurseCallSite final : AANoRecurseImpl { |
| 1986 | AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 1987 | |
| 1988 | /// See AbstractAttribute::initialize(...). |
| 1989 | void initialize(Attributor &A) override { |
| 1990 | AANoRecurseImpl::initialize(A); |
| 1991 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1992 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1993 | indicatePessimisticFixpoint(); |
| 1994 | } |
| 1995 | |
| 1996 | /// See AbstractAttribute::updateImpl(...). |
| 1997 | ChangeStatus updateImpl(Attributor &A) override { |
| 1998 | // TODO: Once we have call site specific value information we can provide |
| 1999 | // call site specific liveness information and then it makes |
| 2000 | // sense to specialize attributes for call sites arguments instead of |
| 2001 | // redirecting requests to the callee argument. |
| 2002 | Function *F = getAssociatedFunction(); |
| 2003 | const IRPosition &FnPos = IRPosition::function(*F); |
| 2004 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); |
| 2005 | return clampStateAndIndicateChange( |
| 2006 | getState(), |
| 2007 | static_cast<const AANoRecurse::StateType &>(FnAA.getState())); |
| 2008 | } |
| 2009 | |
| 2010 | /// See AbstractAttribute::trackStatistics() |
| 2011 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } |
| 2012 | }; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2013 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2014 | /// -------------------- Undefined-Behavior Attributes ------------------------ |
| 2015 | |
| 2016 | struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior { |
| 2017 | AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {} |
| 2018 | |
| 2019 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2020 | // through a pointer (i.e. also branches etc.) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2021 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2022 | const size_t UBPrevSize = KnownUBInsts.size(); |
| 2023 | const size_t NoUBPrevSize = AssumedNoUBInsts.size(); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2024 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2025 | auto InspectMemAccessInstForUB = [&](Instruction &I) { |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2026 | // Skip instructions that are already saved. |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2027 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2028 | return true; |
| 2029 | |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2030 | // If we reach here, we know we have an instruction |
| 2031 | // that accesses memory through a pointer operand, |
| 2032 | // for which getPointerOperand() should give it to us. |
| 2033 | const Value *PtrOp = |
| 2034 | Attributor::getPointerOperand(&I, /* AllowVolatile */ true); |
| 2035 | assert(PtrOp && |
| 2036 | "Expected pointer operand of memory accessing instruction"); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2037 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2038 | // A memory access through a pointer is considered UB |
| 2039 | // only if the pointer has constant null value. |
| 2040 | // TODO: Expand it to not only check constant values. |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2041 | if (!isa<ConstantPointerNull>(PtrOp)) { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2042 | AssumedNoUBInsts.insert(&I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2043 | return true; |
| 2044 | } |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2045 | const Type *PtrTy = PtrOp->getType(); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2046 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2047 | // Because we only consider instructions inside functions, |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2048 | // assume that a parent function exists. |
| 2049 | const Function *F = I.getFunction(); |
| 2050 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2051 | // A memory access using constant null pointer is only considered UB |
| 2052 | // if null pointer is _not_ defined for the target platform. |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2053 | if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace())) |
| 2054 | AssumedNoUBInsts.insert(&I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2055 | else |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2056 | KnownUBInsts.insert(&I); |
| 2057 | return true; |
| 2058 | }; |
| 2059 | |
| 2060 | auto InspectBrInstForUB = [&](Instruction &I) { |
| 2061 | // A conditional branch instruction is considered UB if it has `undef` |
| 2062 | // condition. |
| 2063 | |
| 2064 | // Skip instructions that are already saved. |
| 2065 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) |
| 2066 | return true; |
| 2067 | |
| 2068 | // We know we have a branch instruction. |
| 2069 | auto BrInst = cast<BranchInst>(&I); |
| 2070 | |
| 2071 | // Unconditional branches are never considered UB. |
| 2072 | if (BrInst->isUnconditional()) |
| 2073 | return true; |
| 2074 | |
| 2075 | // Either we stopped and the appropriate action was taken, |
| 2076 | // or we got back a simplified value to continue. |
| 2077 | Optional<Value *> SimplifiedCond = |
| 2078 | stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst); |
| 2079 | if (!SimplifiedCond.hasValue()) |
| 2080 | return true; |
| 2081 | AssumedNoUBInsts.insert(&I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2082 | return true; |
| 2083 | }; |
| 2084 | |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 2085 | A.checkForAllInstructions(InspectMemAccessInstForUB, *this, |
| 2086 | {Instruction::Load, Instruction::Store, |
| 2087 | Instruction::AtomicCmpXchg, |
| 2088 | Instruction::AtomicRMW}); |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2089 | A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br}); |
| 2090 | if (NoUBPrevSize != AssumedNoUBInsts.size() || |
| 2091 | UBPrevSize != KnownUBInsts.size()) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2092 | return ChangeStatus::CHANGED; |
| 2093 | return ChangeStatus::UNCHANGED; |
| 2094 | } |
| 2095 | |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2096 | bool isKnownToCauseUB(Instruction *I) const override { |
| 2097 | return KnownUBInsts.count(I); |
| 2098 | } |
| 2099 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2100 | bool isAssumedToCauseUB(Instruction *I) const override { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2101 | // In simple words, if an instruction is not in the assumed to _not_ |
| 2102 | // cause UB, then it is assumed UB (that includes those |
| 2103 | // in the KnownUBInsts set). The rest is boilerplate |
| 2104 | // is to ensure that it is one of the instructions we test |
| 2105 | // for UB. |
| 2106 | |
| 2107 | switch (I->getOpcode()) { |
| 2108 | case Instruction::Load: |
| 2109 | case Instruction::Store: |
| 2110 | case Instruction::AtomicCmpXchg: |
| 2111 | case Instruction::AtomicRMW: |
| 2112 | return !AssumedNoUBInsts.count(I); |
| 2113 | case Instruction::Br: { |
| 2114 | auto BrInst = cast<BranchInst>(I); |
| 2115 | if (BrInst->isUnconditional()) |
| 2116 | return false; |
| 2117 | return !AssumedNoUBInsts.count(I); |
| 2118 | } break; |
| 2119 | default: |
| 2120 | return false; |
| 2121 | } |
| 2122 | return false; |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | ChangeStatus manifest(Attributor &A) override { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2126 | if (KnownUBInsts.empty()) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2127 | return ChangeStatus::UNCHANGED; |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2128 | for (Instruction *I : KnownUBInsts) |
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 2129 | A.changeToUnreachableAfterManifest(I); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2130 | return ChangeStatus::CHANGED; |
| 2131 | } |
| 2132 | |
| 2133 | /// See AbstractAttribute::getAsStr() |
| 2134 | const std::string getAsStr() const override { |
| 2135 | return getAssumed() ? "undefined-behavior" : "no-ub"; |
| 2136 | } |
| 2137 | |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2138 | /// Note: The correctness of this analysis depends on the fact that the |
| 2139 | /// following 2 sets will stop changing after some point. |
| 2140 | /// "Change" here means that their size changes. |
| 2141 | /// The size of each set is monotonically increasing |
| 2142 | /// (we only add items to them) and it is upper bounded by the number of |
| 2143 | /// instructions in the processed function (we can never save more |
| 2144 | /// elements in either set than this number). Hence, at some point, |
| 2145 | /// they will stop increasing. |
| 2146 | /// Consequently, at some point, both sets will have stopped |
| 2147 | /// changing, effectively making the analysis reach a fixpoint. |
| 2148 | |
| 2149 | /// Note: These 2 sets are disjoint and an instruction can be considered |
| 2150 | /// one of 3 things: |
| 2151 | /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in |
| 2152 | /// the KnownUBInsts set. |
| 2153 | /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior |
| 2154 | /// has a reason to assume it). |
| 2155 | /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior |
| 2156 | /// could not find a reason to assume or prove that it can cause UB, |
| 2157 | /// hence it assumes it doesn't. We have a set for these instructions |
| 2158 | /// so that we don't reprocess them in every update. |
| 2159 | /// Note however that instructions in this set may cause UB. |
| 2160 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2161 | protected: |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2162 | /// A set of all live instructions _known_ to cause UB. |
| 2163 | SmallPtrSet<Instruction *, 8> KnownUBInsts; |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2164 | |
| 2165 | private: |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2166 | /// A set of all the (live) instructions that are assumed to _not_ cause UB. |
| 2167 | SmallPtrSet<Instruction *, 8> AssumedNoUBInsts; |
| 2168 | |
| 2169 | // Should be called on updates in which if we're processing an instruction |
| 2170 | // \p I that depends on a value \p V, one of the following has to happen: |
| 2171 | // - If the value is assumed, then stop. |
| 2172 | // - If the value is known but undef, then consider it UB. |
| 2173 | // - Otherwise, do specific processing with the simplified value. |
| 2174 | // We return None in the first 2 cases to signify that an appropriate |
| 2175 | // action was taken and the caller should stop. |
| 2176 | // Otherwise, we return the simplified value that the caller should |
| 2177 | // use for specific processing. |
| 2178 | Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V, |
| 2179 | Instruction *I) { |
| 2180 | const auto &ValueSimplifyAA = |
| 2181 | A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V)); |
| 2182 | Optional<Value *> SimplifiedV = |
| 2183 | ValueSimplifyAA.getAssumedSimplifiedValue(A); |
| 2184 | if (!ValueSimplifyAA.isKnown()) { |
| 2185 | // Don't depend on assumed values. |
| 2186 | return llvm::None; |
| 2187 | } |
| 2188 | if (!SimplifiedV.hasValue()) { |
| 2189 | // If it is known (which we tested above) but it doesn't have a value, |
| 2190 | // then we can assume `undef` and hence the instruction is UB. |
| 2191 | KnownUBInsts.insert(I); |
| 2192 | return llvm::None; |
| 2193 | } |
| 2194 | Value *Val = SimplifiedV.getValue(); |
| 2195 | if (isa<UndefValue>(Val)) { |
| 2196 | KnownUBInsts.insert(I); |
| 2197 | return llvm::None; |
| 2198 | } |
| 2199 | return Val; |
| 2200 | } |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2201 | }; |
| 2202 | |
| 2203 | struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl { |
| 2204 | AAUndefinedBehaviorFunction(const IRPosition &IRP) |
| 2205 | : AAUndefinedBehaviorImpl(IRP) {} |
| 2206 | |
| 2207 | /// See AbstractAttribute::trackStatistics() |
| 2208 | void trackStatistics() const override { |
| 2209 | STATS_DECL(UndefinedBehaviorInstruction, Instruction, |
| 2210 | "Number of instructions known to have UB"); |
| 2211 | BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) += |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 2212 | KnownUBInsts.size(); |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 2213 | } |
| 2214 | }; |
| 2215 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2216 | /// ------------------------ Will-Return Attributes ---------------------------- |
| 2217 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2218 | // Helper function that checks whether a function has any cycle. |
| 2219 | // TODO: Replace with more efficent code |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2220 | static bool containsCycle(Function &F) { |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2221 | SmallPtrSet<BasicBlock *, 32> Visited; |
| 2222 | |
| 2223 | // Traverse BB by dfs and check whether successor is already visited. |
| 2224 | for (BasicBlock *BB : depth_first(&F)) { |
| 2225 | Visited.insert(BB); |
| 2226 | for (auto *SuccBB : successors(BB)) { |
| 2227 | if (Visited.count(SuccBB)) |
| 2228 | return true; |
| 2229 | } |
| 2230 | } |
| 2231 | return false; |
| 2232 | } |
| 2233 | |
| 2234 | // Helper function that checks the function have a loop which might become an |
| 2235 | // endless loop |
| 2236 | // FIXME: Any cycle is regarded as endless loop for now. |
| 2237 | // We have to allow some patterns. |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2238 | static bool containsPossiblyEndlessLoop(Function *F) { |
| 2239 | return !F || !F->hasExactDefinition() || containsCycle(*F); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2242 | struct AAWillReturnImpl : public AAWillReturn { |
| 2243 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2244 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2245 | /// See AbstractAttribute::initialize(...). |
| 2246 | void initialize(Attributor &A) override { |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2247 | AAWillReturn::initialize(A); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2248 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2249 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2250 | if (containsPossiblyEndlessLoop(F)) |
| 2251 | indicatePessimisticFixpoint(); |
| 2252 | } |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2253 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2254 | /// See AbstractAttribute::updateImpl(...). |
| 2255 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2256 | auto CheckForWillReturn = [&](Instruction &I) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2257 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); |
| 2258 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); |
| 2259 | if (WillReturnAA.isKnownWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2260 | return true; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2261 | if (!WillReturnAA.isAssumedWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2262 | return false; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2263 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); |
| 2264 | return NoRecurseAA.isAssumedNoRecurse(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2265 | }; |
| 2266 | |
| 2267 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) |
| 2268 | return indicatePessimisticFixpoint(); |
| 2269 | |
| 2270 | return ChangeStatus::UNCHANGED; |
| 2271 | } |
| 2272 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2273 | /// See AbstractAttribute::getAsStr() |
| 2274 | const std::string getAsStr() const override { |
| 2275 | return getAssumed() ? "willreturn" : "may-noreturn"; |
| 2276 | } |
| 2277 | }; |
| 2278 | |
| 2279 | struct AAWillReturnFunction final : AAWillReturnImpl { |
| 2280 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 2281 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2282 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2283 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2284 | }; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 2285 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2286 | /// WillReturn attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2287 | struct AAWillReturnCallSite final : AAWillReturnImpl { |
| 2288 | AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 2289 | |
| 2290 | /// See AbstractAttribute::initialize(...). |
| 2291 | void initialize(Attributor &A) override { |
| 2292 | AAWillReturnImpl::initialize(A); |
| 2293 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2294 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2295 | indicatePessimisticFixpoint(); |
| 2296 | } |
| 2297 | |
| 2298 | /// See AbstractAttribute::updateImpl(...). |
| 2299 | ChangeStatus updateImpl(Attributor &A) override { |
| 2300 | // TODO: Once we have call site specific value information we can provide |
| 2301 | // call site specific liveness information and then it makes |
| 2302 | // sense to specialize attributes for call sites arguments instead of |
| 2303 | // redirecting requests to the callee argument. |
| 2304 | Function *F = getAssociatedFunction(); |
| 2305 | const IRPosition &FnPos = IRPosition::function(*F); |
| 2306 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); |
| 2307 | return clampStateAndIndicateChange( |
| 2308 | getState(), |
| 2309 | static_cast<const AAWillReturn::StateType &>(FnAA.getState())); |
| 2310 | } |
| 2311 | |
| 2312 | /// See AbstractAttribute::trackStatistics() |
| 2313 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } |
| 2314 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2315 | |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2316 | /// -------------------AAReachability Attribute-------------------------- |
| 2317 | |
| 2318 | struct AAReachabilityImpl : AAReachability { |
| 2319 | AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {} |
| 2320 | |
| 2321 | const std::string getAsStr() const override { |
| 2322 | // TODO: Return the number of reachable queries. |
| 2323 | return "reachable"; |
| 2324 | } |
| 2325 | |
| 2326 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2327 | void initialize(Attributor &A) override { indicatePessimisticFixpoint(); } |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 2328 | |
| 2329 | /// See AbstractAttribute::updateImpl(...). |
| 2330 | ChangeStatus updateImpl(Attributor &A) override { |
| 2331 | return indicatePessimisticFixpoint(); |
| 2332 | } |
| 2333 | }; |
| 2334 | |
| 2335 | struct AAReachabilityFunction final : public AAReachabilityImpl { |
| 2336 | AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {} |
| 2337 | |
| 2338 | /// See AbstractAttribute::trackStatistics() |
| 2339 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); } |
| 2340 | }; |
| 2341 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2342 | /// ------------------------ NoAlias Argument Attribute ------------------------ |
| 2343 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2344 | struct AANoAliasImpl : AANoAlias { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2345 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2346 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2347 | const std::string getAsStr() const override { |
| 2348 | return getAssumed() ? "noalias" : "may-alias"; |
| 2349 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2350 | }; |
| 2351 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2352 | /// NoAlias attribute for a floating value. |
| 2353 | struct AANoAliasFloating final : AANoAliasImpl { |
| 2354 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2355 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2356 | /// See AbstractAttribute::initialize(...). |
| 2357 | void initialize(Attributor &A) override { |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2358 | AANoAliasImpl::initialize(A); |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2359 | Value &Val = getAssociatedValue(); |
| 2360 | if (isa<AllocaInst>(Val)) |
| 2361 | indicateOptimisticFixpoint(); |
| 2362 | if (isa<ConstantPointerNull>(Val) && |
| 2363 | Val.getType()->getPointerAddressSpace() == 0) |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2364 | indicateOptimisticFixpoint(); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2367 | /// See AbstractAttribute::updateImpl(...). |
| 2368 | ChangeStatus updateImpl(Attributor &A) override { |
| 2369 | // TODO: Implement this. |
| 2370 | return indicatePessimisticFixpoint(); |
| 2371 | } |
| 2372 | |
| 2373 | /// See AbstractAttribute::trackStatistics() |
| 2374 | void trackStatistics() const override { |
| 2375 | STATS_DECLTRACK_FLOATING_ATTR(noalias) |
| 2376 | } |
| 2377 | }; |
| 2378 | |
| 2379 | /// NoAlias attribute for an argument. |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2380 | struct AANoAliasArgument final |
| 2381 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2382 | using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>; |
| 2383 | AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {} |
| 2384 | |
| 2385 | /// See AbstractAttribute::update(...). |
| 2386 | ChangeStatus updateImpl(Attributor &A) override { |
| 2387 | // We have to make sure no-alias on the argument does not break |
| 2388 | // synchronization when this is a callback argument, see also [1] below. |
| 2389 | // If synchronization cannot be affected, we delegate to the base updateImpl |
| 2390 | // function, otherwise we give up for now. |
| 2391 | |
| 2392 | // If the function is no-sync, no-alias cannot break synchronization. |
| 2393 | const auto &NoSyncAA = A.getAAFor<AANoSync>( |
| 2394 | *this, IRPosition::function_scope(getIRPosition())); |
| 2395 | if (NoSyncAA.isAssumedNoSync()) |
| 2396 | return Base::updateImpl(A); |
| 2397 | |
| 2398 | // If the argument is read-only, no-alias cannot break synchronization. |
| 2399 | const auto &MemBehaviorAA = |
| 2400 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); |
| 2401 | if (MemBehaviorAA.isAssumedReadOnly()) |
| 2402 | return Base::updateImpl(A); |
| 2403 | |
| 2404 | // If the argument is never passed through callbacks, no-alias cannot break |
| 2405 | // synchronization. |
| 2406 | if (A.checkForAllCallSites( |
| 2407 | [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this, |
| 2408 | true)) |
| 2409 | return Base::updateImpl(A); |
| 2410 | |
| 2411 | // TODO: add no-alias but make sure it doesn't break synchronization by |
| 2412 | // introducing fake uses. See: |
| 2413 | // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel, |
| 2414 | // International Workshop on OpenMP 2018, |
| 2415 | // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf |
| 2416 | |
| 2417 | return indicatePessimisticFixpoint(); |
| 2418 | } |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2419 | |
| 2420 | /// See AbstractAttribute::trackStatistics() |
| 2421 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } |
| 2422 | }; |
| 2423 | |
| 2424 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { |
| 2425 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2426 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2427 | /// See AbstractAttribute::initialize(...). |
| 2428 | void initialize(Attributor &A) override { |
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 2429 | // See callsite argument attribute and callee argument attribute. |
| 2430 | ImmutableCallSite ICS(&getAnchorValue()); |
| 2431 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) |
| 2432 | indicateOptimisticFixpoint(); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2435 | /// See AbstractAttribute::updateImpl(...). |
| 2436 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2437 | // We can deduce "noalias" if the following conditions hold. |
| 2438 | // (i) Associated value is assumed to be noalias in the definition. |
| 2439 | // (ii) Associated value is assumed to be no-capture in all the uses |
| 2440 | // possibly executed before this callsite. |
| 2441 | // (iii) There is no other pointer argument which could alias with the |
| 2442 | // value. |
| 2443 | |
| 2444 | const Value &V = getAssociatedValue(); |
| 2445 | const IRPosition IRP = IRPosition::value(V); |
| 2446 | |
| 2447 | // (i) Check whether noalias holds in the definition. |
| 2448 | |
| 2449 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 2450 | LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] check definition: " << V |
| 2451 | << " :: " << NoAliasAA << "\n"); |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2452 | |
| 2453 | if (!NoAliasAA.isAssumedNoAlias()) |
| 2454 | return indicatePessimisticFixpoint(); |
| 2455 | |
| 2456 | LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V |
| 2457 | << " is assumed NoAlias in the definition\n"); |
| 2458 | |
| 2459 | // (ii) Check whether the value is captured in the scope using AANoCapture. |
| 2460 | // FIXME: This is conservative though, it is better to look at CFG and |
| 2461 | // check only uses possibly executed before this callsite. |
| 2462 | |
| 2463 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2464 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 2465 | LLVM_DEBUG( |
| 2466 | dbgs() << "[Attributor][AANoAliasCSArg] " << V |
| 2467 | << " cannot be noalias as it is potentially captured\n"); |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2468 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2469 | } |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2470 | |
| 2471 | // (iii) Check there is no other pointer argument which could alias with the |
| 2472 | // value. |
| Johannes Doerfert | b1b441d | 2019-10-10 01:19:57 -0500 | [diff] [blame] | 2473 | // TODO: AbstractCallSite |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2474 | ImmutableCallSite ICS(&getAnchorValue()); |
| 2475 | for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) { |
| 2476 | if (getArgNo() == (int)i) |
| 2477 | continue; |
| 2478 | const Value *ArgOp = ICS.getArgOperand(i); |
| 2479 | if (!ArgOp->getType()->isPointerTy()) |
| 2480 | continue; |
| 2481 | |
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2482 | if (const Function *F = getAnchorScope()) { |
| 2483 | if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) { |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 2484 | bool IsAliasing = !AAR->isNoAlias(&getAssociatedValue(), ArgOp); |
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2485 | LLVM_DEBUG(dbgs() |
| 2486 | << "[Attributor][NoAliasCSArg] Check alias between " |
| 2487 | "callsite arguments " |
| 2488 | << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " " |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2489 | << getAssociatedValue() << " " << *ArgOp << " => " |
| 2490 | << (IsAliasing ? "" : "no-") << "alias \n"); |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2491 | |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 2492 | if (!IsAliasing) |
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2493 | continue; |
| 2494 | } |
| 2495 | } |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2496 | return indicatePessimisticFixpoint(); |
| 2497 | } |
| 2498 | |
| 2499 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2503 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2504 | }; |
| 2505 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2506 | /// NoAlias attribute for function return value. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2507 | struct AANoAliasReturned final : AANoAliasImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2508 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2509 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2510 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2511 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| 2512 | |
| 2513 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 2514 | if (Constant *C = dyn_cast<Constant>(&RV)) |
| 2515 | if (C->isNullValue() || isa<UndefValue>(C)) |
| 2516 | return true; |
| 2517 | |
| 2518 | /// For now, we can only deduce noalias if we have call sites. |
| 2519 | /// FIXME: add more support. |
| 2520 | ImmutableCallSite ICS(&RV); |
| 2521 | if (!ICS) |
| 2522 | return false; |
| 2523 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2524 | const IRPosition &RVPos = IRPosition::value(RV); |
| 2525 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2526 | if (!NoAliasAA.isAssumedNoAlias()) |
| 2527 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2528 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2529 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); |
| 2530 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2531 | }; |
| 2532 | |
| 2533 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) |
| 2534 | return indicatePessimisticFixpoint(); |
| 2535 | |
| 2536 | return ChangeStatus::UNCHANGED; |
| 2537 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2538 | |
| 2539 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2540 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2541 | }; |
| 2542 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2543 | /// NoAlias attribute deduction for a call site return value. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2544 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { |
| 2545 | AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2546 | |
| 2547 | /// See AbstractAttribute::initialize(...). |
| 2548 | void initialize(Attributor &A) override { |
| 2549 | AANoAliasImpl::initialize(A); |
| 2550 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2551 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2552 | indicatePessimisticFixpoint(); |
| 2553 | } |
| 2554 | |
| 2555 | /// See AbstractAttribute::updateImpl(...). |
| 2556 | ChangeStatus updateImpl(Attributor &A) override { |
| 2557 | // TODO: Once we have call site specific value information we can provide |
| 2558 | // call site specific liveness information and then it makes |
| 2559 | // sense to specialize attributes for call sites arguments instead of |
| 2560 | // redirecting requests to the callee argument. |
| 2561 | Function *F = getAssociatedFunction(); |
| 2562 | const IRPosition &FnPos = IRPosition::returned(*F); |
| 2563 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); |
| 2564 | return clampStateAndIndicateChange( |
| 2565 | getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); |
| 2566 | } |
| 2567 | |
| 2568 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2569 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2570 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2571 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2572 | /// -------------------AAIsDead Function Attribute----------------------- |
| 2573 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2574 | struct AAIsDeadValueImpl : public AAIsDead { |
| 2575 | AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2576 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2577 | /// See AAIsDead::isAssumedDead(). |
| 2578 | bool isAssumedDead() const override { return getAssumed(); } |
| 2579 | |
| 2580 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| 2581 | bool isAssumedDead(const BasicBlock *BB) const override { return false; } |
| 2582 | |
| 2583 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| 2584 | bool isKnownDead(const BasicBlock *BB) const override { return false; } |
| 2585 | |
| 2586 | /// See AAIsDead::isAssumedDead(Instruction *I). |
| 2587 | bool isAssumedDead(const Instruction *I) const override { |
| 2588 | return I == getCtxI() && isAssumedDead(); |
| 2589 | } |
| 2590 | |
| 2591 | /// See AAIsDead::isKnownDead(Instruction *I). |
| 2592 | bool isKnownDead(const Instruction *I) const override { |
| 2593 | return I == getCtxI() && getKnown(); |
| 2594 | } |
| 2595 | |
| 2596 | /// See AbstractAttribute::getAsStr(). |
| 2597 | const std::string getAsStr() const override { |
| 2598 | return isAssumedDead() ? "assumed-dead" : "assumed-live"; |
| 2599 | } |
| 2600 | }; |
| 2601 | |
| 2602 | struct AAIsDeadFloating : public AAIsDeadValueImpl { |
| 2603 | AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} |
| 2604 | |
| 2605 | /// See AbstractAttribute::initialize(...). |
| 2606 | void initialize(Attributor &A) override { |
| 2607 | if (Instruction *I = dyn_cast<Instruction>(&getAssociatedValue())) |
| 2608 | if (!wouldInstructionBeTriviallyDead(I)) |
| 2609 | indicatePessimisticFixpoint(); |
| 2610 | if (isa<UndefValue>(getAssociatedValue())) |
| 2611 | indicatePessimisticFixpoint(); |
| 2612 | } |
| 2613 | |
| 2614 | /// See AbstractAttribute::updateImpl(...). |
| 2615 | ChangeStatus updateImpl(Attributor &A) override { |
| 2616 | auto UsePred = [&](const Use &U, bool &Follow) { |
| 2617 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 2618 | if (CallSite CS = CallSite(UserI)) { |
| 2619 | if (!CS.isArgOperand(&U)) |
| 2620 | return false; |
| 2621 | const IRPosition &CSArgPos = |
| 2622 | IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); |
| 2623 | const auto &CSArgIsDead = A.getAAFor<AAIsDead>(*this, CSArgPos); |
| 2624 | return CSArgIsDead.isAssumedDead(); |
| 2625 | } |
| 2626 | if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { |
| 2627 | const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); |
| 2628 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, RetPos); |
| 2629 | return RetIsDeadAA.isAssumedDead(); |
| 2630 | } |
| 2631 | Follow = true; |
| 2632 | return wouldInstructionBeTriviallyDead(UserI); |
| 2633 | }; |
| 2634 | |
| 2635 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) |
| 2636 | return indicatePessimisticFixpoint(); |
| 2637 | return ChangeStatus::UNCHANGED; |
| 2638 | } |
| 2639 | |
| 2640 | /// See AbstractAttribute::manifest(...). |
| 2641 | ChangeStatus manifest(Attributor &A) override { |
| 2642 | Value &V = getAssociatedValue(); |
| 2643 | if (auto *I = dyn_cast<Instruction>(&V)) |
| 2644 | if (wouldInstructionBeTriviallyDead(I)) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2645 | A.deleteAfterManifest(*I); |
| 2646 | return ChangeStatus::CHANGED; |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | if (V.use_empty()) |
| 2650 | return ChangeStatus::UNCHANGED; |
| 2651 | |
| 2652 | UndefValue &UV = *UndefValue::get(V.getType()); |
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 2653 | bool AnyChange = A.changeValueAfterManifest(V, UV); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2654 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| 2655 | } |
| 2656 | |
| 2657 | /// See AbstractAttribute::trackStatistics() |
| 2658 | void trackStatistics() const override { |
| 2659 | STATS_DECLTRACK_FLOATING_ATTR(IsDead) |
| 2660 | } |
| 2661 | }; |
| 2662 | |
| 2663 | struct AAIsDeadArgument : public AAIsDeadFloating { |
| 2664 | AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} |
| 2665 | |
| 2666 | /// See AbstractAttribute::initialize(...). |
| 2667 | void initialize(Attributor &A) override { |
| 2668 | if (!getAssociatedFunction()->hasExactDefinition()) |
| 2669 | indicatePessimisticFixpoint(); |
| 2670 | } |
| 2671 | |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 2672 | /// See AbstractAttribute::manifest(...). |
| 2673 | ChangeStatus manifest(Attributor &A) override { |
| 2674 | ChangeStatus Changed = AAIsDeadFloating::manifest(A); |
| 2675 | Argument &Arg = *getAssociatedArgument(); |
| 2676 | if (Arg.getParent()->hasLocalLinkage()) |
| 2677 | if (A.registerFunctionSignatureRewrite( |
| 2678 | Arg, /* ReplacementTypes */ {}, |
| 2679 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{}, |
| 2680 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) |
| 2681 | return ChangeStatus::CHANGED; |
| 2682 | return Changed; |
| 2683 | } |
| 2684 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2685 | /// See AbstractAttribute::trackStatistics() |
| 2686 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } |
| 2687 | }; |
| 2688 | |
| 2689 | struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { |
| 2690 | AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} |
| 2691 | |
| 2692 | /// See AbstractAttribute::initialize(...). |
| 2693 | void initialize(Attributor &A) override { |
| 2694 | if (isa<UndefValue>(getAssociatedValue())) |
| 2695 | indicatePessimisticFixpoint(); |
| 2696 | } |
| 2697 | |
| 2698 | /// See AbstractAttribute::updateImpl(...). |
| 2699 | ChangeStatus updateImpl(Attributor &A) override { |
| 2700 | // TODO: Once we have call site specific value information we can provide |
| 2701 | // call site specific liveness information and then it makes |
| 2702 | // sense to specialize attributes for call sites arguments instead of |
| 2703 | // redirecting requests to the callee argument. |
| 2704 | Argument *Arg = getAssociatedArgument(); |
| 2705 | if (!Arg) |
| 2706 | return indicatePessimisticFixpoint(); |
| 2707 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 2708 | auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); |
| 2709 | return clampStateAndIndicateChange( |
| 2710 | getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); |
| 2711 | } |
| 2712 | |
| 2713 | /// See AbstractAttribute::manifest(...). |
| 2714 | ChangeStatus manifest(Attributor &A) override { |
| 2715 | CallBase &CB = cast<CallBase>(getAnchorValue()); |
| 2716 | Use &U = CB.getArgOperandUse(getArgNo()); |
| 2717 | assert(!isa<UndefValue>(U.get()) && |
| 2718 | "Expected undef values to be filtered out!"); |
| 2719 | UndefValue &UV = *UndefValue::get(U->getType()); |
| 2720 | if (A.changeUseAfterManifest(U, UV)) |
| 2721 | return ChangeStatus::CHANGED; |
| 2722 | return ChangeStatus::UNCHANGED; |
| 2723 | } |
| 2724 | |
| 2725 | /// See AbstractAttribute::trackStatistics() |
| 2726 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } |
| 2727 | }; |
| 2728 | |
| 2729 | struct AAIsDeadReturned : public AAIsDeadValueImpl { |
| 2730 | AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} |
| 2731 | |
| 2732 | /// See AbstractAttribute::updateImpl(...). |
| 2733 | ChangeStatus updateImpl(Attributor &A) override { |
| 2734 | |
| 2735 | auto PredForCallSite = [&](AbstractCallSite ACS) { |
| 2736 | if (ACS.isCallbackCall()) |
| 2737 | return false; |
| 2738 | const IRPosition &CSRetPos = |
| 2739 | IRPosition::callsite_returned(ACS.getCallSite()); |
| 2740 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, CSRetPos); |
| 2741 | return RetIsDeadAA.isAssumedDead(); |
| 2742 | }; |
| 2743 | |
| 2744 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) |
| 2745 | return indicatePessimisticFixpoint(); |
| 2746 | |
| 2747 | return ChangeStatus::UNCHANGED; |
| 2748 | } |
| 2749 | |
| 2750 | /// See AbstractAttribute::manifest(...). |
| 2751 | ChangeStatus manifest(Attributor &A) override { |
| 2752 | // TODO: Rewrite the signature to return void? |
| 2753 | bool AnyChange = false; |
| 2754 | UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); |
| 2755 | auto RetInstPred = [&](Instruction &I) { |
| 2756 | ReturnInst &RI = cast<ReturnInst>(I); |
| 2757 | if (!isa<UndefValue>(RI.getReturnValue())) |
| 2758 | AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); |
| 2759 | return true; |
| 2760 | }; |
| 2761 | A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); |
| 2762 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| 2763 | } |
| 2764 | |
| 2765 | /// See AbstractAttribute::trackStatistics() |
| 2766 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } |
| 2767 | }; |
| 2768 | |
| 2769 | struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { |
| 2770 | AAIsDeadCallSiteReturned(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} |
| 2771 | |
| 2772 | /// See AbstractAttribute::initialize(...). |
| 2773 | void initialize(Attributor &A) override {} |
| 2774 | |
| 2775 | /// See AbstractAttribute::trackStatistics() |
| 2776 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(IsDead) } |
| 2777 | }; |
| 2778 | |
| 2779 | struct AAIsDeadFunction : public AAIsDead { |
| 2780 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} |
| 2781 | |
| 2782 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2783 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2784 | const Function *F = getAssociatedFunction(); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2785 | if (F && !F->isDeclaration()) { |
| 2786 | ToBeExploredFrom.insert(&F->getEntryBlock().front()); |
| 2787 | assumeLive(A, F->getEntryBlock()); |
| 2788 | } |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2791 | /// See AbstractAttribute::getAsStr(). |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2792 | const std::string getAsStr() const override { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2793 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2794 | std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2795 | std::to_string(ToBeExploredFrom.size()) + "][#KDE " + |
| 2796 | std::to_string(KnownDeadEnds.size()) + "]"; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | /// See AbstractAttribute::manifest(...). |
| 2800 | ChangeStatus manifest(Attributor &A) override { |
| 2801 | assert(getState().isValidState() && |
| 2802 | "Attempted to manifest an invalid state!"); |
| 2803 | |
| 2804 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2805 | Function &F = *getAssociatedFunction(); |
| 2806 | |
| 2807 | if (AssumedLiveBlocks.empty()) { |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2808 | A.deleteAfterManifest(F); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2809 | return ChangeStatus::CHANGED; |
| 2810 | } |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2811 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2812 | // Flag to determine if we can change an invoke to a call assuming the |
| 2813 | // callee is nounwind. This is not possible if the personality of the |
| 2814 | // function allows to catch asynchronous exceptions. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2815 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2816 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2817 | KnownDeadEnds.set_union(ToBeExploredFrom); |
| 2818 | for (const Instruction *DeadEndI : KnownDeadEnds) { |
| 2819 | auto *CB = dyn_cast<CallBase>(DeadEndI); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2820 | if (!CB) |
| 2821 | continue; |
| 2822 | const auto &NoReturnAA = |
| 2823 | A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); |
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2824 | bool MayReturn = !NoReturnAA.isAssumedNoReturn(); |
| 2825 | if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2826 | continue; |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2827 | Instruction *I = const_cast<Instruction *>(DeadEndI); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2828 | BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2829 | Instruction *SplitPos = I->getNextNode(); |
| Johannes Doerfert | d410805 | 2019-08-21 20:56:41 +0000 | [diff] [blame] | 2830 | // TODO: mark stuff before unreachable instructions as dead. |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2831 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2832 | if (auto *II = dyn_cast<InvokeInst>(I)) { |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2833 | // If we keep the invoke the split position is at the beginning of the |
| 2834 | // normal desitination block (it invokes a noreturn function after all). |
| 2835 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 2836 | SplitPos = &NormalDestBB->front(); |
| 2837 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2838 | /// Invoke is replaced with a call and unreachable is placed after it if |
| 2839 | /// the callee is nounwind and noreturn. Otherwise, we keep the invoke |
| 2840 | /// and only place an unreachable in the normal successor. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2841 | if (Invoke2CallAllowed) { |
| Michael Liao | a99086d | 2019-08-20 21:02:31 +0000 | [diff] [blame] | 2842 | if (II->getCalledFunction()) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2843 | const IRPosition &IPos = IRPosition::callsite_function(*II); |
| 2844 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); |
| 2845 | if (AANoUnw.isAssumedNoUnwind()) { |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2846 | LLVM_DEBUG(dbgs() |
| 2847 | << "[AAIsDead] Replace invoke with call inst\n"); |
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2848 | CallInst *CI = createCallMatchingInvoke(II); |
| 2849 | CI->insertBefore(II); |
| 2850 | CI->takeName(II); |
| Johannes Doerfert | 5d34602 | 2019-12-13 22:11:42 -0600 | [diff] [blame] | 2851 | replaceAllInstructionUsesWith(*II, *CI); |
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2852 | |
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2853 | // If this is a nounwind + mayreturn invoke we only remove the |
| 2854 | // unwind edge. This is done by moving the invoke into a new and |
| 2855 | // dead block and connecting the normal destination of the invoke |
| 2856 | // with a branch that follows the call replacement we created |
| 2857 | // above. |
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2858 | if (MayReturn) { |
| Johannes Doerfert | 0bc3336 | 2019-12-16 21:03:18 -0600 | [diff] [blame] | 2859 | BasicBlock *NewDeadBB = |
| 2860 | SplitBlock(BB, II, nullptr, nullptr, nullptr, ".i2c"); |
| Johannes Doerfert | c7ab19d | 2019-11-01 21:59:32 -0500 | [diff] [blame] | 2861 | assert(isa<BranchInst>(BB->getTerminator()) && |
| 2862 | BB->getTerminator()->getNumSuccessors() == 1 && |
| 2863 | BB->getTerminator()->getSuccessor(0) == NewDeadBB); |
| 2864 | new UnreachableInst(I->getContext(), NewDeadBB); |
| 2865 | BB->getTerminator()->setOperand(0, NormalDestBB); |
| 2866 | A.deleteAfterManifest(*II); |
| 2867 | continue; |
| 2868 | } |
| 2869 | |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2870 | // We do not need an invoke (II) but instead want a call followed |
| 2871 | // by an unreachable. However, we do not remove II as other |
| 2872 | // abstract attributes might have it cached as part of their |
| 2873 | // results. Given that we modify the CFG anyway, we simply keep II |
| 2874 | // around but in a new dead block. To avoid II being live through |
| 2875 | // a different edge we have to ensure the block we place it in is |
| 2876 | // only reached from the current block of II and then not reached |
| 2877 | // at all when we insert the unreachable. |
| 2878 | SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2879 | SplitPos = CI->getNextNode(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2880 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2881 | } |
| 2882 | } |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2883 | |
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2884 | if (SplitPos == &NormalDestBB->front()) { |
| 2885 | // If this is an invoke of a noreturn function the edge to the normal |
| 2886 | // destination block is dead but not necessarily the block itself. |
| 2887 | // TODO: We need to move to an edge based system during deduction and |
| 2888 | // also manifest. |
| 2889 | assert(!NormalDestBB->isLandingPad() && |
| 2890 | "Expected the normal destination not to be a landingpad!"); |
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2891 | if (NormalDestBB->getUniquePredecessor() == BB) { |
| 2892 | assumeLive(A, *NormalDestBB); |
| 2893 | } else { |
| 2894 | BasicBlock *SplitBB = |
| 2895 | SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); |
| 2896 | // The split block is live even if it contains only an unreachable |
| 2897 | // instruction at the end. |
| 2898 | assumeLive(A, *SplitBB); |
| 2899 | SplitPos = SplitBB->getTerminator(); |
| 2900 | HasChanged = ChangeStatus::CHANGED; |
| 2901 | } |
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2902 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2905 | if (isa_and_nonnull<UnreachableInst>(SplitPos)) |
| 2906 | continue; |
| 2907 | |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2908 | BB = SplitPos->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2909 | SplitBlock(BB, SplitPos); |
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 2910 | A.changeToUnreachableAfterManifest(BB->getTerminator()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2911 | HasChanged = ChangeStatus::CHANGED; |
| 2912 | } |
| 2913 | |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2914 | for (BasicBlock &BB : F) |
| 2915 | if (!AssumedLiveBlocks.count(&BB)) |
| 2916 | A.deleteAfterManifest(BB); |
| 2917 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2918 | return HasChanged; |
| 2919 | } |
| 2920 | |
| 2921 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2922 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2923 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2924 | /// See AbstractAttribute::trackStatistics() |
| 2925 | void trackStatistics() const override {} |
| 2926 | |
| 2927 | /// Returns true if the function is assumed dead. |
| 2928 | bool isAssumedDead() const override { return false; } |
| 2929 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2930 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2931 | bool isAssumedDead(const BasicBlock *BB) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2932 | assert(BB->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2933 | "BB must be in the same anchor scope function."); |
| 2934 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2935 | if (!getAssumed()) |
| 2936 | return false; |
| 2937 | return !AssumedLiveBlocks.count(BB); |
| 2938 | } |
| 2939 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2940 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2941 | bool isKnownDead(const BasicBlock *BB) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2942 | return getKnown() && isAssumedDead(BB); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2945 | /// See AAIsDead::isAssumed(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2946 | bool isAssumedDead(const Instruction *I) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2947 | assert(I->getParent()->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2948 | "Instruction must be in the same anchor scope function."); |
| 2949 | |
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 2950 | if (!getAssumed()) |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2951 | return false; |
| 2952 | |
| 2953 | // If it is not in AssumedLiveBlocks then it for sure dead. |
| 2954 | // Otherwise, it can still be after noreturn call in a live block. |
| 2955 | if (!AssumedLiveBlocks.count(I->getParent())) |
| 2956 | return true; |
| 2957 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2958 | // If it is not after a liveness barrier it is live. |
| 2959 | const Instruction *PrevI = I->getPrevNode(); |
| 2960 | while (PrevI) { |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 2961 | if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2962 | return true; |
| 2963 | PrevI = PrevI->getPrevNode(); |
| 2964 | } |
| 2965 | return false; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | /// See AAIsDead::isKnownDead(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2969 | bool isKnownDead(const Instruction *I) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2970 | return getKnown() && isAssumedDead(I); |
| 2971 | } |
| 2972 | |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2973 | /// Determine if \p F might catch asynchronous exceptions. |
| 2974 | static bool mayCatchAsynchronousExceptions(const Function &F) { |
| 2975 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); |
| 2976 | } |
| 2977 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2978 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A |
| 2979 | /// that internal function called from \p BB should now be looked at. |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2980 | bool assumeLive(Attributor &A, const BasicBlock &BB) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2981 | if (!AssumedLiveBlocks.insert(&BB).second) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2982 | return false; |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2983 | |
| 2984 | // We assume that all of BB is (probably) live now and if there are calls to |
| 2985 | // internal functions we will assume that those are now live as well. This |
| 2986 | // is a performance optimization for blocks with calls to a lot of internal |
| 2987 | // functions. It can however cause dead functions to be treated as live. |
| 2988 | for (const Instruction &I : BB) |
| 2989 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) |
| 2990 | if (const Function *F = ICS.getCalledFunction()) |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 2991 | if (F->hasLocalLinkage()) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2992 | A.markLiveInternalFunction(*F); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2993 | return true; |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2996 | /// Collection of instructions that need to be explored again, e.g., we |
| 2997 | /// did assume they do not transfer control to (one of their) successors. |
| 2998 | SmallSetVector<const Instruction *, 8> ToBeExploredFrom; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2999 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3000 | /// Collection of instructions that are known to not transfer control. |
| 3001 | SmallSetVector<const Instruction *, 8> KnownDeadEnds; |
| 3002 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3003 | /// Collection of all assumed live BasicBlocks. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3004 | DenseSet<const BasicBlock *> AssumedLiveBlocks; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3005 | }; |
| 3006 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3007 | static bool |
| 3008 | identifyAliveSuccessors(Attributor &A, const CallBase &CB, |
| 3009 | AbstractAttribute &AA, |
| 3010 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| 3011 | const IRPosition &IPos = IRPosition::callsite_function(CB); |
| 3012 | |
| 3013 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); |
| 3014 | if (NoReturnAA.isAssumedNoReturn()) |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3015 | return !NoReturnAA.isKnownNoReturn(); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3016 | if (CB.isTerminator()) |
| 3017 | AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); |
| 3018 | else |
| 3019 | AliveSuccessors.push_back(CB.getNextNode()); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 3020 | return false; |
| 3021 | } |
| 3022 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3023 | static bool |
| 3024 | identifyAliveSuccessors(Attributor &A, const InvokeInst &II, |
| 3025 | AbstractAttribute &AA, |
| 3026 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| 3027 | bool UsedAssumedInformation = |
| 3028 | identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 3029 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3030 | // First, determine if we can change an invoke to a call assuming the |
| 3031 | // callee is nounwind. This is not possible if the personality of the |
| 3032 | // function allows to catch asynchronous exceptions. |
| 3033 | if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { |
| 3034 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); |
| 3035 | } else { |
| 3036 | const IRPosition &IPos = IRPosition::callsite_function(II); |
| 3037 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3038 | if (AANoUnw.isAssumedNoUnwind()) { |
| 3039 | UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); |
| 3040 | } else { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3041 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3042 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3043 | } |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3044 | return UsedAssumedInformation; |
| 3045 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3046 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3047 | static Optional<ConstantInt *> |
| 3048 | getAssumedConstant(Attributor &A, const Value &V, AbstractAttribute &AA, |
| 3049 | bool &UsedAssumedInformation) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3050 | const auto &ValueSimplifyAA = |
| 3051 | A.getAAFor<AAValueSimplify>(AA, IRPosition::value(V)); |
| 3052 | Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3053 | UsedAssumedInformation |= !ValueSimplifyAA.isKnown(); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3054 | if (!SimplifiedV.hasValue()) |
| 3055 | return llvm::None; |
| 3056 | if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) |
| 3057 | return llvm::None; |
| 3058 | return dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue()); |
| 3059 | } |
| 3060 | |
| 3061 | static bool |
| 3062 | identifyAliveSuccessors(Attributor &A, const BranchInst &BI, |
| 3063 | AbstractAttribute &AA, |
| 3064 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| 3065 | bool UsedAssumedInformation = false; |
| 3066 | if (BI.getNumSuccessors() == 1) { |
| 3067 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); |
| 3068 | } else { |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3069 | Optional<ConstantInt *> CI = |
| 3070 | getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3071 | if (!CI.hasValue()) { |
| 3072 | // No value yet, assume both edges are dead. |
| 3073 | } else if (CI.getValue()) { |
| 3074 | const BasicBlock *SuccBB = |
| 3075 | BI.getSuccessor(1 - CI.getValue()->getZExtValue()); |
| 3076 | AliveSuccessors.push_back(&SuccBB->front()); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3077 | } else { |
| 3078 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); |
| 3079 | AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3080 | UsedAssumedInformation = false; |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3081 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3082 | } |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3083 | return UsedAssumedInformation; |
| 3084 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3085 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3086 | static bool |
| 3087 | identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, |
| 3088 | AbstractAttribute &AA, |
| 3089 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3090 | bool UsedAssumedInformation = false; |
| 3091 | Optional<ConstantInt *> CI = |
| 3092 | getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation); |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3093 | if (!CI.hasValue()) { |
| 3094 | // No value yet, assume all edges are dead. |
| 3095 | } else if (CI.getValue()) { |
| 3096 | for (auto &CaseIt : SI.cases()) { |
| 3097 | if (CaseIt.getCaseValue() == CI.getValue()) { |
| 3098 | AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3099 | return UsedAssumedInformation; |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3100 | } |
| 3101 | } |
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 3102 | AliveSuccessors.push_back(&SI.getDefaultDest()->front()); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3103 | return UsedAssumedInformation; |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3104 | } else { |
| 3105 | for (const BasicBlock *SuccBB : successors(SI.getParent())) |
| 3106 | AliveSuccessors.push_back(&SuccBB->front()); |
| 3107 | } |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3108 | return UsedAssumedInformation; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3111 | ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3112 | ChangeStatus Change = ChangeStatus::UNCHANGED; |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 3113 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3114 | LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" |
| 3115 | << getAssociatedFunction()->size() << "] BBs and " |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3116 | << ToBeExploredFrom.size() << " exploration points and " |
| 3117 | << KnownDeadEnds.size() << " known dead ends\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3118 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3119 | // Copy and clear the list of instructions we need to explore from. It is |
| 3120 | // refilled with instructions the next update has to look at. |
| 3121 | SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), |
| 3122 | ToBeExploredFrom.end()); |
| 3123 | decltype(ToBeExploredFrom) NewToBeExploredFrom; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3124 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3125 | SmallVector<const Instruction *, 8> AliveSuccessors; |
| 3126 | while (!Worklist.empty()) { |
| 3127 | const Instruction *I = Worklist.pop_back_val(); |
| 3128 | LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3129 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3130 | AliveSuccessors.clear(); |
| 3131 | |
| 3132 | bool UsedAssumedInformation = false; |
| 3133 | switch (I->getOpcode()) { |
| 3134 | // TODO: look for (assumed) UB to backwards propagate "deadness". |
| 3135 | default: |
| 3136 | if (I->isTerminator()) { |
| 3137 | for (const BasicBlock *SuccBB : successors(I->getParent())) |
| 3138 | AliveSuccessors.push_back(&SuccBB->front()); |
| 3139 | } else { |
| 3140 | AliveSuccessors.push_back(I->getNextNode()); |
| 3141 | } |
| 3142 | break; |
| 3143 | case Instruction::Call: |
| 3144 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), |
| 3145 | *this, AliveSuccessors); |
| 3146 | break; |
| 3147 | case Instruction::Invoke: |
| 3148 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), |
| 3149 | *this, AliveSuccessors); |
| 3150 | break; |
| 3151 | case Instruction::Br: |
| 3152 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), |
| 3153 | *this, AliveSuccessors); |
| 3154 | break; |
| 3155 | case Instruction::Switch: |
| 3156 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), |
| 3157 | *this, AliveSuccessors); |
| 3158 | break; |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3159 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3160 | |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3161 | if (UsedAssumedInformation) { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3162 | NewToBeExploredFrom.insert(I); |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3163 | } else { |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3164 | Change = ChangeStatus::CHANGED; |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3165 | if (AliveSuccessors.empty() || |
| 3166 | (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors())) |
| 3167 | KnownDeadEnds.insert(I); |
| 3168 | } |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3169 | |
| 3170 | LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " |
| 3171 | << AliveSuccessors.size() << " UsedAssumedInformation: " |
| 3172 | << UsedAssumedInformation << "\n"); |
| 3173 | |
| 3174 | for (const Instruction *AliveSuccessor : AliveSuccessors) { |
| 3175 | if (!I->isTerminator()) { |
| 3176 | assert(AliveSuccessors.size() == 1 && |
| 3177 | "Non-terminator expected to have a single successor!"); |
| 3178 | Worklist.push_back(AliveSuccessor); |
| 3179 | } else { |
| 3180 | if (assumeLive(A, *AliveSuccessor->getParent())) |
| 3181 | Worklist.push_back(AliveSuccessor); |
| 3182 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 3183 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3186 | ToBeExploredFrom = std::move(NewToBeExploredFrom); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3187 | |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 3188 | // 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] | 3189 | // Instead, indicating a pessimistic fixpoint will cause the state to be |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3190 | // "invalid" and all queries to be answered conservatively without lookups. |
| 3191 | // To be in this state we have to (1) finished the exploration and (3) not |
| 3192 | // discovered any non-trivial dead end and (2) not ruled unreachable code |
| 3193 | // dead. |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3194 | if (ToBeExploredFrom.empty() && |
| Johannes Doerfert | 3cbe331 | 2019-11-01 21:04:54 -0500 | [diff] [blame] | 3195 | getAssociatedFunction()->size() == AssumedLiveBlocks.size() && |
| 3196 | llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { |
| 3197 | return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; |
| 3198 | })) |
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 3199 | return indicatePessimisticFixpoint(); |
| 3200 | return Change; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3203 | /// Liveness information for a call sites. |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 3204 | struct AAIsDeadCallSite final : AAIsDeadFunction { |
| 3205 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3206 | |
| 3207 | /// See AbstractAttribute::initialize(...). |
| 3208 | void initialize(Attributor &A) override { |
| 3209 | // TODO: Once we have call site specific value information we can provide |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3210 | // call site specific liveness information and then it makes |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 3211 | // sense to specialize attributes for call sites instead of |
| 3212 | // redirecting requests to the callee. |
| 3213 | llvm_unreachable("Abstract attributes for liveness are not " |
| 3214 | "supported for call sites yet!"); |
| 3215 | } |
| 3216 | |
| 3217 | /// See AbstractAttribute::updateImpl(...). |
| 3218 | ChangeStatus updateImpl(Attributor &A) override { |
| 3219 | return indicatePessimisticFixpoint(); |
| 3220 | } |
| 3221 | |
| 3222 | /// See AbstractAttribute::trackStatistics() |
| 3223 | void trackStatistics() const override {} |
| 3224 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3225 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3226 | /// -------------------- Dereferenceable Argument Attribute -------------------- |
| 3227 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3228 | template <> |
| 3229 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, |
| 3230 | const DerefState &R) { |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3231 | ChangeStatus CS0 = |
| 3232 | clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); |
| 3233 | ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3234 | return CS0 | CS1; |
| 3235 | } |
| 3236 | |
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 3237 | struct AADereferenceableImpl : AADereferenceable { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3238 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3239 | using StateType = DerefState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3240 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3241 | void initialize(Attributor &A) override { |
| 3242 | SmallVector<Attribute, 4> Attrs; |
| 3243 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, |
| 3244 | Attrs); |
| 3245 | for (const Attribute &Attr : Attrs) |
| 3246 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); |
| 3247 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3248 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition()); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3249 | |
| 3250 | const IRPosition &IRP = this->getIRPosition(); |
| 3251 | bool IsFnInterface = IRP.isFnInterfaceKind(); |
| 3252 | const Function *FnScope = IRP.getAnchorScope(); |
| 3253 | if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) |
| 3254 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3257 | /// See AbstractAttribute::getState() |
| 3258 | /// { |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3259 | StateType &getState() override { return *this; } |
| 3260 | const StateType &getState() const override { return *this; } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3261 | /// } |
| 3262 | |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3263 | /// Helper function for collecting accessed bytes in must-be-executed-context |
| 3264 | void addAccessedBytesForUse(Attributor &A, const Use *U, |
| 3265 | const Instruction *I) { |
| 3266 | const Value *UseV = U->get(); |
| 3267 | if (!UseV->getType()->isPointerTy()) |
| 3268 | return; |
| 3269 | |
| 3270 | Type *PtrTy = UseV->getType(); |
| 3271 | const DataLayout &DL = A.getDataLayout(); |
| 3272 | int64_t Offset; |
| 3273 | if (const Value *Base = getBasePointerOfAccessPointerOperand( |
| 3274 | I, Offset, DL, /*AllowNonInbounds*/ true)) { |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 3275 | if (Base == &getAssociatedValue() && |
| 3276 | Attributor::getPointerOperand(I, /* AllowVolatile */ false) == UseV) { |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3277 | uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType()); |
| 3278 | addAccessedBytes(Offset, Size); |
| 3279 | } |
| 3280 | } |
| 3281 | return; |
| 3282 | } |
| 3283 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3284 | /// See AAFromMustBeExecutedContext |
| 3285 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { |
| 3286 | bool IsNonNull = false; |
| 3287 | bool TrackUse = false; |
| 3288 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( |
| 3289 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); |
| Hideto Ueno | 6c742fd | 2019-11-29 06:55:58 +0000 | [diff] [blame] | 3290 | |
| 3291 | addAccessedBytesForUse(A, U, I); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3292 | takeKnownDerefBytesMaximum(DerefBytes); |
| 3293 | return TrackUse; |
| 3294 | } |
| 3295 | |
| Hideto Ueno | dfedae5 | 2019-11-29 06:45:07 +0000 | [diff] [blame] | 3296 | /// See AbstractAttribute::manifest(...). |
| 3297 | ChangeStatus manifest(Attributor &A) override { |
| 3298 | ChangeStatus Change = AADereferenceable::manifest(A); |
| 3299 | if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { |
| 3300 | removeAttrs({Attribute::DereferenceableOrNull}); |
| 3301 | return ChangeStatus::CHANGED; |
| 3302 | } |
| 3303 | return Change; |
| 3304 | } |
| 3305 | |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 3306 | void getDeducedAttributes(LLVMContext &Ctx, |
| 3307 | SmallVectorImpl<Attribute> &Attrs) const override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3308 | // TODO: Add *_globally support |
| 3309 | if (isAssumedNonNull()) |
| 3310 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( |
| 3311 | Ctx, getAssumedDereferenceableBytes())); |
| 3312 | else |
| 3313 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( |
| 3314 | Ctx, getAssumedDereferenceableBytes())); |
| 3315 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3316 | |
| 3317 | /// See AbstractAttribute::getAsStr(). |
| 3318 | const std::string getAsStr() const override { |
| 3319 | if (!getAssumedDereferenceableBytes()) |
| 3320 | return "unknown-dereferenceable"; |
| 3321 | return std::string("dereferenceable") + |
| 3322 | (isAssumedNonNull() ? "" : "_or_null") + |
| 3323 | (isAssumedGlobal() ? "_globally" : "") + "<" + |
| 3324 | std::to_string(getKnownDereferenceableBytes()) + "-" + |
| 3325 | std::to_string(getAssumedDereferenceableBytes()) + ">"; |
| 3326 | } |
| 3327 | }; |
| 3328 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3329 | /// Dereferenceable attribute for a floating value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3330 | struct AADereferenceableFloating |
| 3331 | : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { |
| 3332 | using Base = |
| 3333 | AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; |
| 3334 | AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3335 | |
| 3336 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3337 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3338 | ChangeStatus Change = Base::updateImpl(A); |
| 3339 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3340 | const DataLayout &DL = A.getDataLayout(); |
| 3341 | |
| 3342 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { |
| 3343 | unsigned IdxWidth = |
| 3344 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); |
| 3345 | APInt Offset(IdxWidth, 0); |
| 3346 | const Value *Base = |
| 3347 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 3348 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3349 | const auto &AA = |
| 3350 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3351 | int64_t DerefBytes = 0; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3352 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3353 | // Use IR information if we did not strip anything. |
| 3354 | // TODO: track globally. |
| 3355 | bool CanBeNull; |
| 3356 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); |
| 3357 | T.GlobalState.indicatePessimisticFixpoint(); |
| 3358 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3359 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3360 | DerefBytes = DS.DerefBytesState.getAssumed(); |
| 3361 | T.GlobalState &= DS.GlobalState; |
| 3362 | } |
| 3363 | |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3364 | // For now we do not try to "increase" dereferenceability due to negative |
| 3365 | // indices as we first have to come up with code to deal with loops and |
| 3366 | // for overflows of the dereferenceable bytes. |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3367 | int64_t OffsetSExt = Offset.getSExtValue(); |
| 3368 | if (OffsetSExt < 0) |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 3369 | OffsetSExt = 0; |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 3370 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3371 | T.takeAssumedDerefBytesMinimum( |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3372 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3373 | |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 3374 | if (this == &AA) { |
| 3375 | if (!Stripped) { |
| 3376 | // If nothing was stripped IR information is all we got. |
| 3377 | T.takeKnownDerefBytesMaximum( |
| 3378 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| 3379 | T.indicatePessimisticFixpoint(); |
| 3380 | } else if (OffsetSExt > 0) { |
| 3381 | // If something was stripped but there is circular reasoning we look |
| 3382 | // for the offset. If it is positive we basically decrease the |
| 3383 | // dereferenceable bytes in a circluar loop now, which will simply |
| 3384 | // drive them down to the known value in a very slow way which we |
| 3385 | // can accelerate. |
| 3386 | T.indicatePessimisticFixpoint(); |
| 3387 | } |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | return T.isValidState(); |
| 3391 | }; |
| 3392 | |
| 3393 | DerefState T; |
| 3394 | if (!genericValueTraversal<AADereferenceable, DerefState>( |
| 3395 | A, getIRPosition(), *this, T, VisitValueCB)) |
| 3396 | return indicatePessimisticFixpoint(); |
| 3397 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3398 | return Change | clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | /// See AbstractAttribute::trackStatistics() |
| 3402 | void trackStatistics() const override { |
| 3403 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) |
| 3404 | } |
| 3405 | }; |
| 3406 | |
| 3407 | /// Dereferenceable attribute for a return value. |
| 3408 | struct AADereferenceableReturned final |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3409 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, |
| 3410 | DerefState> { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3411 | AADereferenceableReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3412 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, |
| 3413 | DerefState>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3414 | |
| 3415 | /// See AbstractAttribute::trackStatistics() |
| 3416 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3417 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3418 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3419 | }; |
| 3420 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3421 | /// Dereferenceable attribute for an argument |
| 3422 | struct AADereferenceableArgument final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3423 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< |
| 3424 | AADereferenceable, AADereferenceableImpl, DerefState> { |
| 3425 | using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< |
| 3426 | AADereferenceable, AADereferenceableImpl, DerefState>; |
| 3427 | AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3428 | |
| 3429 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3430 | void trackStatistics() const override { |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3431 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) |
| 3432 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3433 | }; |
| 3434 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3435 | /// Dereferenceable attribute for a call site argument. |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3436 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3437 | AADereferenceableCallSiteArgument(const IRPosition &IRP) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3438 | : AADereferenceableFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3439 | |
| 3440 | /// See AbstractAttribute::trackStatistics() |
| 3441 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3442 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3443 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 3444 | }; |
| 3445 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3446 | /// Dereferenceable attribute deduction for a call site return value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 3447 | struct AADereferenceableCallSiteReturned final |
| 3448 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< |
| 3449 | AADereferenceable, AADereferenceableImpl> { |
| 3450 | using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< |
| 3451 | AADereferenceable, AADereferenceableImpl>; |
| 3452 | AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3453 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3454 | /// See AbstractAttribute::trackStatistics() |
| 3455 | void trackStatistics() const override { |
| 3456 | STATS_DECLTRACK_CS_ATTR(dereferenceable); |
| 3457 | } |
| 3458 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3459 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3460 | // ------------------------ Align Argument Attribute ------------------------ |
| 3461 | |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3462 | static unsigned int getKnownAlignForUse(Attributor &A, |
| 3463 | AbstractAttribute &QueryingAA, |
| 3464 | Value &AssociatedValue, const Use *U, |
| 3465 | const Instruction *I, bool &TrackUse) { |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3466 | // We need to follow common pointer manipulation uses to the accesses they |
| 3467 | // feed into. |
| 3468 | if (isa<CastInst>(I)) { |
| Johannes Doerfert | c90681b | 2020-01-02 16:41:17 -0600 | [diff] [blame] | 3469 | // Follow all but ptr2int casts. |
| 3470 | TrackUse = !isa<PtrToIntInst>(I); |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3471 | return 0; |
| 3472 | } |
| 3473 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 3474 | if (GEP->hasAllConstantIndices()) { |
| 3475 | TrackUse = true; |
| 3476 | return 0; |
| 3477 | } |
| 3478 | } |
| 3479 | |
| 3480 | unsigned Alignment = 0; |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3481 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { |
| 3482 | if (ICS.isBundleOperand(U) || ICS.isCallee(U)) |
| 3483 | return 0; |
| 3484 | |
| 3485 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 3486 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); |
| 3487 | // As long as we only use known information there is no need to track |
| 3488 | // dependences here. |
| 3489 | auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, |
| 3490 | /* TrackDependence */ false); |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3491 | Alignment = AlignAA.getKnownAlign(); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3494 | const Value *UseV = U->get(); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3495 | if (auto *SI = dyn_cast<StoreInst>(I)) |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3496 | Alignment = SI->getAlignment(); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3497 | else if (auto *LI = dyn_cast<LoadInst>(I)) |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3498 | Alignment = LI->getAlignment(); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3499 | |
| Hideto Ueno | 78a7502 | 2019-11-26 07:51:59 +0000 | [diff] [blame] | 3500 | if (Alignment <= 1) |
| 3501 | return 0; |
| 3502 | |
| 3503 | auto &DL = A.getDataLayout(); |
| 3504 | int64_t Offset; |
| 3505 | |
| 3506 | if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { |
| 3507 | if (Base == &AssociatedValue) { |
| 3508 | // BasePointerAddr + Offset = Alignment * Q for some integer Q. |
| 3509 | // So we can say that the maximum power of two which is a divisor of |
| 3510 | // gcd(Offset, Alignment) is an alignment. |
| 3511 | |
| 3512 | uint32_t gcd = |
| 3513 | greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); |
| 3514 | Alignment = llvm::PowerOf2Floor(gcd); |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | return Alignment; |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3519 | } |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3520 | struct AAAlignImpl : AAAlign { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3521 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3522 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3523 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3524 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3525 | SmallVector<Attribute, 4> Attrs; |
| 3526 | getAttrs({Attribute::Alignment}, Attrs); |
| 3527 | for (const Attribute &Attr : Attrs) |
| 3528 | takeKnownMaximum(Attr.getValueAsInt()); |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 3529 | |
| 3530 | if (getIRPosition().isFnInterfaceKind() && |
| 3531 | (!getAssociatedFunction() || |
| 3532 | !getAssociatedFunction()->hasExactDefinition())) |
| 3533 | indicatePessimisticFixpoint(); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3536 | /// See AbstractAttribute::manifest(...). |
| 3537 | ChangeStatus manifest(Attributor &A) override { |
| 3538 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 3539 | |
| 3540 | // Check for users that allow alignment annotations. |
| 3541 | Value &AnchorVal = getIRPosition().getAnchorValue(); |
| 3542 | for (const Use &U : AnchorVal.uses()) { |
| 3543 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { |
| 3544 | if (SI->getPointerOperand() == &AnchorVal) |
| 3545 | if (SI->getAlignment() < getAssumedAlign()) { |
| 3546 | STATS_DECLTRACK(AAAlign, Store, |
| 3547 | "Number of times alignemnt added to a store"); |
| Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 3548 | SI->setAlignment(Align(getAssumedAlign())); |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3549 | Changed = ChangeStatus::CHANGED; |
| 3550 | } |
| 3551 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { |
| 3552 | if (LI->getPointerOperand() == &AnchorVal) |
| 3553 | if (LI->getAlignment() < getAssumedAlign()) { |
| Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 3554 | LI->setAlignment(Align(getAssumedAlign())); |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3555 | STATS_DECLTRACK(AAAlign, Load, |
| 3556 | "Number of times alignemnt added to a load"); |
| 3557 | Changed = ChangeStatus::CHANGED; |
| 3558 | } |
| 3559 | } |
| 3560 | } |
| 3561 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3562 | return AAAlign::manifest(A) | Changed; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3565 | // TODO: Provide a helper to determine the implied ABI alignment and check in |
| 3566 | // the existing manifest method and a new one for AAAlignImpl that value |
| 3567 | // to avoid making the alignment explicit if it did not improve. |
| 3568 | |
| 3569 | /// See AbstractAttribute::getDeducedAttributes |
| 3570 | virtual void |
| 3571 | getDeducedAttributes(LLVMContext &Ctx, |
| 3572 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 3573 | if (getAssumedAlign() > 1) |
| Guillaume Chatelet | b65fa48 | 2019-10-15 12:56:24 +0000 | [diff] [blame] | 3574 | Attrs.emplace_back( |
| 3575 | Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3576 | } |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3577 | /// See AAFromMustBeExecutedContext |
| 3578 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { |
| 3579 | bool TrackUse = false; |
| 3580 | |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 3581 | unsigned int KnownAlign = |
| 3582 | getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3583 | takeKnownMaximum(KnownAlign); |
| 3584 | |
| 3585 | return TrackUse; |
| 3586 | } |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3587 | |
| 3588 | /// See AbstractAttribute::getAsStr(). |
| 3589 | const std::string getAsStr() const override { |
| 3590 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + |
| 3591 | "-" + std::to_string(getAssumedAlign()) + ">") |
| 3592 | : "unknown-align"; |
| 3593 | } |
| 3594 | }; |
| 3595 | |
| 3596 | /// Align attribute for a floating value. |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3597 | struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> { |
| 3598 | using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>; |
| 3599 | AAAlignFloating(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 3600 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3601 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3602 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3603 | Base::updateImpl(A); |
| 3604 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3605 | const DataLayout &DL = A.getDataLayout(); |
| 3606 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 3607 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, |
| 3608 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3609 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); |
| 3610 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3611 | // Use only IR information if we did not strip anything. |
| Guillaume Chatelet | bae629b | 2019-10-15 13:58:22 +0000 | [diff] [blame] | 3612 | const MaybeAlign PA = V.getPointerAlignment(DL); |
| 3613 | T.takeKnownMaximum(PA ? PA->value() : 0); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3614 | T.indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3615 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3616 | // Use abstract attribute information. |
| 3617 | const AAAlign::StateType &DS = |
| 3618 | static_cast<const AAAlign::StateType &>(AA.getState()); |
| 3619 | T ^= DS; |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3620 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 3621 | return T.isValidState(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3622 | }; |
| 3623 | |
| 3624 | StateType T; |
| 3625 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, |
| 3626 | VisitValueCB)) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 3627 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3628 | |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 3629 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 3630 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3631 | return clampStateAndIndicateChange(getState(), T); |
| 3632 | } |
| 3633 | |
| 3634 | /// See AbstractAttribute::trackStatistics() |
| 3635 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } |
| 3636 | }; |
| 3637 | |
| 3638 | /// Align attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3639 | struct AAAlignReturned final |
| 3640 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3641 | AAAlignReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3642 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3643 | |
| 3644 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3645 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3646 | }; |
| 3647 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3648 | /// Align attribute for function argument. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3649 | struct AAAlignArgument final |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3650 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, |
| 3651 | AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3652 | AAAlignArgument(const IRPosition &IRP) |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3653 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, |
| 3654 | AAAlignImpl>( |
| 3655 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3656 | |
| 3657 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3658 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3659 | }; |
| 3660 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3661 | struct AAAlignCallSiteArgument final : AAAlignFloating { |
| 3662 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3663 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3664 | /// See AbstractAttribute::manifest(...). |
| 3665 | ChangeStatus manifest(Attributor &A) override { |
| 3666 | return AAAlignImpl::manifest(A); |
| 3667 | } |
| 3668 | |
| Johannes Doerfert | dada813 | 2019-12-31 01:27:50 -0600 | [diff] [blame] | 3669 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| 3670 | ChangeStatus updateImpl(Attributor &A) override { |
| 3671 | ChangeStatus Changed = AAAlignFloating::updateImpl(A); |
| 3672 | if (Argument *Arg = getAssociatedArgument()) { |
| 3673 | const auto &ArgAlignAA = A.getAAFor<AAAlign>( |
| 3674 | *this, IRPosition::argument(*Arg), /* TrackDependence */ false, |
| 3675 | DepClassTy::OPTIONAL); |
| 3676 | takeKnownMaximum(ArgAlignAA.getKnownAlign()); |
| 3677 | } |
| 3678 | return Changed; |
| 3679 | } |
| 3680 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3681 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3682 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3683 | }; |
| 3684 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3685 | /// Align attribute deduction for a call site return value. |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3686 | struct AAAlignCallSiteReturned final |
| 3687 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, |
| 3688 | AAAlignImpl> { |
| 3689 | using Base = |
| 3690 | AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, |
| 3691 | AAAlignImpl>; |
| 3692 | AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3693 | |
| 3694 | /// See AbstractAttribute::initialize(...). |
| 3695 | void initialize(Attributor &A) override { |
| Hideto Ueno | 88b04ef | 2019-11-12 06:36:49 +0000 | [diff] [blame] | 3696 | Base::initialize(A); |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3697 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3698 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3699 | indicatePessimisticFixpoint(); |
| 3700 | } |
| 3701 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3702 | /// See AbstractAttribute::trackStatistics() |
| 3703 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } |
| 3704 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3705 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3706 | /// ------------------ Function No-Return Attribute ---------------------------- |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3707 | struct AANoReturnImpl : public AANoReturn { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3708 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3709 | |
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3710 | /// See AbstractAttribute::initialize(...). |
| 3711 | void initialize(Attributor &A) override { |
| 3712 | AANoReturn::initialize(A); |
| 3713 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | 1b6041a | 2019-11-01 20:17:48 -0500 | [diff] [blame] | 3714 | if (!F) |
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3715 | indicatePessimisticFixpoint(); |
| 3716 | } |
| 3717 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3718 | /// See AbstractAttribute::getAsStr(). |
| 3719 | const std::string getAsStr() const override { |
| 3720 | return getAssumed() ? "noreturn" : "may-return"; |
| 3721 | } |
| 3722 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3723 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3724 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3725 | auto CheckForNoReturn = [](Instruction &) { return false; }; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3726 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3727 | {(unsigned)Instruction::Ret})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3728 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3729 | return ChangeStatus::UNCHANGED; |
| 3730 | } |
| 3731 | }; |
| 3732 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3733 | struct AANoReturnFunction final : AANoReturnImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3734 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3735 | |
| 3736 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3737 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3738 | }; |
| 3739 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3740 | /// NoReturn attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3741 | struct AANoReturnCallSite final : AANoReturnImpl { |
| 3742 | AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| 3743 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3744 | /// See AbstractAttribute::updateImpl(...). |
| 3745 | ChangeStatus updateImpl(Attributor &A) override { |
| 3746 | // TODO: Once we have call site specific value information we can provide |
| 3747 | // call site specific liveness information and then it makes |
| 3748 | // sense to specialize attributes for call sites arguments instead of |
| 3749 | // redirecting requests to the callee argument. |
| 3750 | Function *F = getAssociatedFunction(); |
| 3751 | const IRPosition &FnPos = IRPosition::function(*F); |
| 3752 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); |
| 3753 | return clampStateAndIndicateChange( |
| 3754 | getState(), |
| 3755 | static_cast<const AANoReturn::StateType &>(FnAA.getState())); |
| 3756 | } |
| 3757 | |
| 3758 | /// See AbstractAttribute::trackStatistics() |
| 3759 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } |
| 3760 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3761 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3762 | /// ----------------------- Variable Capturing --------------------------------- |
| 3763 | |
| 3764 | /// A class to hold the state of for no-capture attributes. |
| 3765 | struct AANoCaptureImpl : public AANoCapture { |
| 3766 | AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} |
| 3767 | |
| 3768 | /// See AbstractAttribute::initialize(...). |
| 3769 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3770 | if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { |
| 3771 | indicateOptimisticFixpoint(); |
| 3772 | return; |
| 3773 | } |
| 3774 | Function *AnchorScope = getAnchorScope(); |
| 3775 | if (isFnInterfaceKind() && |
| 3776 | (!AnchorScope || !AnchorScope->hasExactDefinition())) { |
| 3777 | indicatePessimisticFixpoint(); |
| 3778 | return; |
| 3779 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3780 | |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 3781 | // You cannot "capture" null in the default address space. |
| 3782 | if (isa<ConstantPointerNull>(getAssociatedValue()) && |
| 3783 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { |
| 3784 | indicateOptimisticFixpoint(); |
| 3785 | return; |
| 3786 | } |
| 3787 | |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3788 | const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3789 | |
| 3790 | // Check what state the associated function can actually capture. |
| 3791 | if (F) |
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3792 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3793 | else |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3794 | indicatePessimisticFixpoint(); |
| 3795 | } |
| 3796 | |
| 3797 | /// See AbstractAttribute::updateImpl(...). |
| 3798 | ChangeStatus updateImpl(Attributor &A) override; |
| 3799 | |
| 3800 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). |
| 3801 | virtual void |
| 3802 | getDeducedAttributes(LLVMContext &Ctx, |
| 3803 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 3804 | if (!isAssumedNoCaptureMaybeReturned()) |
| 3805 | return; |
| 3806 | |
| Hideto Ueno | 3736764 | 2019-09-11 06:52:11 +0000 | [diff] [blame] | 3807 | if (getArgNo() >= 0) { |
| 3808 | if (isAssumedNoCapture()) |
| 3809 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); |
| 3810 | else if (ManifestInternal) |
| 3811 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); |
| 3812 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known |
| 3816 | /// depending on the ability of the function associated with \p IRP to capture |
| 3817 | /// state in memory and through "returning/throwing", respectively. |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3818 | static void determineFunctionCaptureCapabilities(const IRPosition &IRP, |
| 3819 | const Function &F, |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 3820 | BitIntegerState &State) { |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3821 | // TODO: Once we have memory behavior attributes we should use them here. |
| 3822 | |
| 3823 | // If we know we cannot communicate or write to memory, we do not care about |
| 3824 | // ptr2int anymore. |
| 3825 | if (F.onlyReadsMemory() && F.doesNotThrow() && |
| 3826 | F.getReturnType()->isVoidTy()) { |
| 3827 | State.addKnownBits(NO_CAPTURE); |
| 3828 | return; |
| 3829 | } |
| 3830 | |
| 3831 | // A function cannot capture state in memory if it only reads memory, it can |
| 3832 | // however return/throw state and the state might be influenced by the |
| 3833 | // pointer value, e.g., loading from a returned pointer might reveal a bit. |
| 3834 | if (F.onlyReadsMemory()) |
| 3835 | State.addKnownBits(NOT_CAPTURED_IN_MEM); |
| 3836 | |
| 3837 | // A function cannot communicate state back if it does not through |
| 3838 | // exceptions and doesn not return values. |
| 3839 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) |
| 3840 | State.addKnownBits(NOT_CAPTURED_IN_RET); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3841 | |
| 3842 | // Check existing "returned" attributes. |
| 3843 | int ArgNo = IRP.getArgNo(); |
| 3844 | if (F.doesNotThrow() && ArgNo >= 0) { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 3845 | for (unsigned u = 0, e = F.arg_size(); u < e; ++u) |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3846 | if (F.hasParamAttribute(u, Attribute::Returned)) { |
| Johannes Doerfert | 9d5ad5e | 2019-10-21 01:29:10 +0000 | [diff] [blame] | 3847 | if (u == unsigned(ArgNo)) |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3848 | State.removeAssumedBits(NOT_CAPTURED_IN_RET); |
| 3849 | else if (F.onlyReadsMemory()) |
| 3850 | State.addKnownBits(NO_CAPTURE); |
| 3851 | else |
| 3852 | State.addKnownBits(NOT_CAPTURED_IN_RET); |
| 3853 | break; |
| 3854 | } |
| 3855 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | /// See AbstractState::getAsStr(). |
| 3859 | const std::string getAsStr() const override { |
| 3860 | if (isKnownNoCapture()) |
| 3861 | return "known not-captured"; |
| 3862 | if (isAssumedNoCapture()) |
| 3863 | return "assumed not-captured"; |
| 3864 | if (isKnownNoCaptureMaybeReturned()) |
| 3865 | return "known not-captured-maybe-returned"; |
| 3866 | if (isAssumedNoCaptureMaybeReturned()) |
| 3867 | return "assumed not-captured-maybe-returned"; |
| 3868 | return "assumed-captured"; |
| 3869 | } |
| 3870 | }; |
| 3871 | |
| 3872 | /// Attributor-aware capture tracker. |
| 3873 | struct AACaptureUseTracker final : public CaptureTracker { |
| 3874 | |
| 3875 | /// Create a capture tracker that can lookup in-flight abstract attributes |
| 3876 | /// through the Attributor \p A. |
| 3877 | /// |
| 3878 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the |
| 3879 | /// search is stopped. If a use leads to a return instruction, |
| 3880 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. |
| 3881 | /// If a use leads to a ptr2int which may capture the value, |
| 3882 | /// \p CapturedInInteger is set. If a use is found that is currently assumed |
| 3883 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies |
| 3884 | /// set. All values in \p PotentialCopies are later tracked as well. For every |
| 3885 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, |
| 3886 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger |
| 3887 | /// conservatively set to true. |
| 3888 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3889 | const AAIsDead &IsDeadAA, AANoCapture::StateType &State, |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3890 | SmallVectorImpl<const Value *> &PotentialCopies, |
| 3891 | unsigned &RemainingUsesToExplore) |
| 3892 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), |
| 3893 | PotentialCopies(PotentialCopies), |
| 3894 | RemainingUsesToExplore(RemainingUsesToExplore) {} |
| 3895 | |
| 3896 | /// Determine if \p V maybe captured. *Also updates the state!* |
| 3897 | bool valueMayBeCaptured(const Value *V) { |
| 3898 | if (V->getType()->isPointerTy()) { |
| 3899 | PointerMayBeCaptured(V, this); |
| 3900 | } else { |
| 3901 | State.indicatePessimisticFixpoint(); |
| 3902 | } |
| 3903 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); |
| 3904 | } |
| 3905 | |
| 3906 | /// See CaptureTracker::tooManyUses(). |
| 3907 | void tooManyUses() override { |
| 3908 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); |
| 3909 | } |
| 3910 | |
| 3911 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { |
| 3912 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) |
| 3913 | return true; |
| 3914 | const auto &DerefAA = |
| 3915 | A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O)); |
| 3916 | return DerefAA.getAssumedDereferenceableBytes(); |
| 3917 | } |
| 3918 | |
| 3919 | /// See CaptureTracker::captured(...). |
| 3920 | bool captured(const Use *U) override { |
| 3921 | Instruction *UInst = cast<Instruction>(U->getUser()); |
| 3922 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst |
| 3923 | << "\n"); |
| 3924 | |
| 3925 | // Because we may reuse the tracker multiple times we keep track of the |
| 3926 | // number of explored uses ourselves as well. |
| 3927 | if (RemainingUsesToExplore-- == 0) { |
| 3928 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); |
| 3929 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 3930 | /* Return */ true); |
| 3931 | } |
| 3932 | |
| 3933 | // Deal with ptr2int by following uses. |
| 3934 | if (isa<PtrToIntInst>(UInst)) { |
| 3935 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); |
| 3936 | return valueMayBeCaptured(UInst); |
| 3937 | } |
| 3938 | |
| 3939 | // Explicitly catch return instructions. |
| 3940 | if (isa<ReturnInst>(UInst)) |
| 3941 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 3942 | /* Return */ true); |
| 3943 | |
| 3944 | // For now we only use special logic for call sites. However, the tracker |
| 3945 | // itself knows about a lot of other non-capturing cases already. |
| 3946 | CallSite CS(UInst); |
| 3947 | if (!CS || !CS.isArgOperand(U)) |
| 3948 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 3949 | /* Return */ true); |
| 3950 | |
| 3951 | unsigned ArgNo = CS.getArgumentNo(U); |
| 3952 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); |
| 3953 | // If we have a abstract no-capture attribute for the argument we can use |
| 3954 | // it to justify a non-capture attribute here. This allows recursion! |
| 3955 | auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); |
| 3956 | if (ArgNoCaptureAA.isAssumedNoCapture()) |
| 3957 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 3958 | /* Return */ false); |
| 3959 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 3960 | addPotentialCopy(CS); |
| 3961 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 3962 | /* Return */ false); |
| 3963 | } |
| 3964 | |
| 3965 | // Lastly, we could not find a reason no-capture can be assumed so we don't. |
| 3966 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 3967 | /* Return */ true); |
| 3968 | } |
| 3969 | |
| 3970 | /// Register \p CS as potential copy of the value we are checking. |
| 3971 | void addPotentialCopy(CallSite CS) { |
| 3972 | PotentialCopies.push_back(CS.getInstruction()); |
| 3973 | } |
| 3974 | |
| 3975 | /// See CaptureTracker::shouldExplore(...). |
| 3976 | bool shouldExplore(const Use *U) override { |
| 3977 | // Check liveness. |
| 3978 | return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser())); |
| 3979 | } |
| 3980 | |
| 3981 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and |
| 3982 | /// \p CapturedInRet, then return the appropriate value for use in the |
| 3983 | /// CaptureTracker::captured() interface. |
| 3984 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, |
| 3985 | bool CapturedInRet) { |
| 3986 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " |
| 3987 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); |
| 3988 | if (CapturedInMem) |
| 3989 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); |
| 3990 | if (CapturedInInt) |
| 3991 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); |
| 3992 | if (CapturedInRet) |
| 3993 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); |
| 3994 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); |
| 3995 | } |
| 3996 | |
| 3997 | private: |
| 3998 | /// The attributor providing in-flight abstract attributes. |
| 3999 | Attributor &A; |
| 4000 | |
| 4001 | /// The abstract attribute currently updated. |
| 4002 | AANoCapture &NoCaptureAA; |
| 4003 | |
| 4004 | /// The abstract liveness state. |
| 4005 | const AAIsDead &IsDeadAA; |
| 4006 | |
| 4007 | /// The state currently updated. |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4008 | AANoCapture::StateType &State; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4009 | |
| 4010 | /// Set of potential copies of the tracked value. |
| 4011 | SmallVectorImpl<const Value *> &PotentialCopies; |
| 4012 | |
| 4013 | /// Global counter to limit the number of explored uses. |
| 4014 | unsigned &RemainingUsesToExplore; |
| 4015 | }; |
| 4016 | |
| 4017 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { |
| 4018 | const IRPosition &IRP = getIRPosition(); |
| 4019 | const Value *V = |
| 4020 | getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); |
| 4021 | if (!V) |
| 4022 | return indicatePessimisticFixpoint(); |
| 4023 | |
| 4024 | const Function *F = |
| 4025 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); |
| 4026 | assert(F && "Expected a function!"); |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4027 | const IRPosition &FnPos = IRPosition::function(*F); |
| 4028 | const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4029 | |
| 4030 | AANoCapture::StateType T; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4031 | |
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 4032 | // Readonly means we cannot capture through memory. |
| 4033 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); |
| 4034 | if (FnMemAA.isAssumedReadOnly()) { |
| 4035 | T.addKnownBits(NOT_CAPTURED_IN_MEM); |
| 4036 | if (FnMemAA.isKnownReadOnly()) |
| 4037 | addKnownBits(NOT_CAPTURED_IN_MEM); |
| 4038 | } |
| 4039 | |
| 4040 | // Make sure all returned values are different than the underlying value. |
| 4041 | // TODO: we could do this in a more sophisticated way inside |
| 4042 | // AAReturnedValues, e.g., track all values that escape through returns |
| 4043 | // directly somehow. |
| 4044 | auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { |
| 4045 | bool SeenConstant = false; |
| 4046 | for (auto &It : RVAA.returned_values()) { |
| 4047 | if (isa<Constant>(It.first)) { |
| 4048 | if (SeenConstant) |
| 4049 | return false; |
| 4050 | SeenConstant = true; |
| 4051 | } else if (!isa<Argument>(It.first) || |
| 4052 | It.first == getAssociatedArgument()) |
| 4053 | return false; |
| 4054 | } |
| 4055 | return true; |
| 4056 | }; |
| 4057 | |
| 4058 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, FnPos); |
| 4059 | if (NoUnwindAA.isAssumedNoUnwind()) { |
| 4060 | bool IsVoidTy = F->getReturnType()->isVoidTy(); |
| 4061 | const AAReturnedValues *RVAA = |
| 4062 | IsVoidTy ? nullptr : &A.getAAFor<AAReturnedValues>(*this, FnPos); |
| 4063 | if (IsVoidTy || CheckReturnedArgs(*RVAA)) { |
| 4064 | T.addKnownBits(NOT_CAPTURED_IN_RET); |
| 4065 | if (T.isKnown(NOT_CAPTURED_IN_MEM)) |
| 4066 | return ChangeStatus::UNCHANGED; |
| 4067 | if (NoUnwindAA.isKnownNoUnwind() && |
| 4068 | (IsVoidTy || RVAA->getState().isAtFixpoint())) { |
| 4069 | addKnownBits(NOT_CAPTURED_IN_RET); |
| 4070 | if (isKnown(NOT_CAPTURED_IN_MEM)) |
| 4071 | return indicateOptimisticFixpoint(); |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4075 | |
| 4076 | // Use the CaptureTracker interface and logic with the specialized tracker, |
| 4077 | // defined in AACaptureUseTracker, that can look at in-flight abstract |
| 4078 | // attributes and directly updates the assumed state. |
| 4079 | SmallVector<const Value *, 4> PotentialCopies; |
| 4080 | unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; |
| 4081 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, |
| 4082 | RemainingUsesToExplore); |
| 4083 | |
| 4084 | // Check all potential copies of the associated value until we can assume |
| 4085 | // none will be captured or we have to assume at least one might be. |
| 4086 | unsigned Idx = 0; |
| 4087 | PotentialCopies.push_back(V); |
| 4088 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) |
| 4089 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); |
| 4090 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4091 | AANoCapture::StateType &S = getState(); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4092 | auto Assumed = S.getAssumed(); |
| 4093 | S.intersectAssumedBits(T.getAssumed()); |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4094 | if (!isAssumedNoCaptureMaybeReturned()) |
| 4095 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4096 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED |
| 4097 | : ChangeStatus::CHANGED; |
| 4098 | } |
| 4099 | |
| 4100 | /// NoCapture attribute for function arguments. |
| 4101 | struct AANoCaptureArgument final : AANoCaptureImpl { |
| 4102 | AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4103 | |
| 4104 | /// See AbstractAttribute::trackStatistics() |
| 4105 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } |
| 4106 | }; |
| 4107 | |
| 4108 | /// NoCapture attribute for call site arguments. |
| 4109 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { |
| 4110 | AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4111 | |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4112 | /// See AbstractAttribute::initialize(...). |
| 4113 | void initialize(Attributor &A) override { |
| 4114 | if (Argument *Arg = getAssociatedArgument()) |
| 4115 | if (Arg->hasByValAttr()) |
| 4116 | indicateOptimisticFixpoint(); |
| 4117 | AANoCaptureImpl::initialize(A); |
| 4118 | } |
| 4119 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4120 | /// See AbstractAttribute::updateImpl(...). |
| 4121 | ChangeStatus updateImpl(Attributor &A) override { |
| 4122 | // TODO: Once we have call site specific value information we can provide |
| 4123 | // call site specific liveness information and then it makes |
| 4124 | // sense to specialize attributes for call sites arguments instead of |
| 4125 | // redirecting requests to the callee argument. |
| 4126 | Argument *Arg = getAssociatedArgument(); |
| 4127 | if (!Arg) |
| 4128 | return indicatePessimisticFixpoint(); |
| 4129 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 4130 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); |
| 4131 | return clampStateAndIndicateChange( |
| 4132 | getState(), |
| 4133 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); |
| 4134 | } |
| 4135 | |
| 4136 | /// See AbstractAttribute::trackStatistics() |
| 4137 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; |
| 4138 | }; |
| 4139 | |
| 4140 | /// NoCapture attribute for floating values. |
| 4141 | struct AANoCaptureFloating final : AANoCaptureImpl { |
| 4142 | AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4143 | |
| 4144 | /// See AbstractAttribute::trackStatistics() |
| 4145 | void trackStatistics() const override { |
| 4146 | STATS_DECLTRACK_FLOATING_ATTR(nocapture) |
| 4147 | } |
| 4148 | }; |
| 4149 | |
| 4150 | /// NoCapture attribute for function return value. |
| 4151 | struct AANoCaptureReturned final : AANoCaptureImpl { |
| 4152 | AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { |
| 4153 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 4154 | } |
| 4155 | |
| 4156 | /// See AbstractAttribute::initialize(...). |
| 4157 | void initialize(Attributor &A) override { |
| 4158 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 4159 | } |
| 4160 | |
| 4161 | /// See AbstractAttribute::updateImpl(...). |
| 4162 | ChangeStatus updateImpl(Attributor &A) override { |
| 4163 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 4164 | } |
| 4165 | |
| 4166 | /// See AbstractAttribute::trackStatistics() |
| 4167 | void trackStatistics() const override {} |
| 4168 | }; |
| 4169 | |
| 4170 | /// NoCapture attribute deduction for a call site return value. |
| 4171 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { |
| 4172 | AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 4173 | |
| 4174 | /// See AbstractAttribute::trackStatistics() |
| 4175 | void trackStatistics() const override { |
| 4176 | STATS_DECLTRACK_CSRET_ATTR(nocapture) |
| 4177 | } |
| 4178 | }; |
| 4179 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4180 | /// ------------------ Value Simplify Attribute ---------------------------- |
| 4181 | struct AAValueSimplifyImpl : AAValueSimplify { |
| 4182 | AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} |
| 4183 | |
| 4184 | /// See AbstractAttribute::getAsStr(). |
| 4185 | const std::string getAsStr() const override { |
| 4186 | return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") |
| 4187 | : "not-simple"; |
| 4188 | } |
| 4189 | |
| 4190 | /// See AbstractAttribute::trackStatistics() |
| 4191 | void trackStatistics() const override {} |
| 4192 | |
| 4193 | /// See AAValueSimplify::getAssumedSimplifiedValue() |
| 4194 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { |
| 4195 | if (!getAssumed()) |
| 4196 | return const_cast<Value *>(&getAssociatedValue()); |
| 4197 | return SimplifiedAssociatedValue; |
| 4198 | } |
| 4199 | void initialize(Attributor &A) override {} |
| 4200 | |
| 4201 | /// Helper function for querying AAValueSimplify and updating candicate. |
| 4202 | /// \param QueryingValue Value trying to unify with SimplifiedValue |
| 4203 | /// \param AccumulatedSimplifiedValue Current simplification result. |
| 4204 | static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, |
| 4205 | Value &QueryingValue, |
| 4206 | Optional<Value *> &AccumulatedSimplifiedValue) { |
| 4207 | // FIXME: Add a typecast support. |
| 4208 | |
| 4209 | auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>( |
| 4210 | QueryingAA, IRPosition::value(QueryingValue)); |
| 4211 | |
| 4212 | Optional<Value *> QueryingValueSimplified = |
| 4213 | ValueSimpifyAA.getAssumedSimplifiedValue(A); |
| 4214 | |
| 4215 | if (!QueryingValueSimplified.hasValue()) |
| 4216 | return true; |
| 4217 | |
| 4218 | if (!QueryingValueSimplified.getValue()) |
| 4219 | return false; |
| 4220 | |
| 4221 | Value &QueryingValueSimplifiedUnwrapped = |
| 4222 | *QueryingValueSimplified.getValue(); |
| 4223 | |
| 4224 | if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) |
| 4225 | return true; |
| 4226 | |
| 4227 | if (AccumulatedSimplifiedValue.hasValue()) |
| 4228 | return AccumulatedSimplifiedValue == QueryingValueSimplified; |
| 4229 | |
| 4230 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue |
| 4231 | << " is assumed to be " |
| 4232 | << QueryingValueSimplifiedUnwrapped << "\n"); |
| 4233 | |
| 4234 | AccumulatedSimplifiedValue = QueryingValueSimplified; |
| 4235 | return true; |
| 4236 | } |
| 4237 | |
| 4238 | /// See AbstractAttribute::manifest(...). |
| 4239 | ChangeStatus manifest(Attributor &A) override { |
| 4240 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 4241 | |
| 4242 | if (!SimplifiedAssociatedValue.hasValue() || |
| 4243 | !SimplifiedAssociatedValue.getValue()) |
| 4244 | return Changed; |
| 4245 | |
| 4246 | if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) { |
| 4247 | // We can replace the AssociatedValue with the constant. |
| 4248 | Value &V = getAssociatedValue(); |
| 4249 | if (!V.user_empty() && &V != C && V.getType() == C->getType()) { |
| 4250 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C |
| 4251 | << "\n"); |
| Hideto Ueno | 34fe8d0 | 2019-12-30 17:08:48 +0900 | [diff] [blame] | 4252 | A.changeValueAfterManifest(V, *C); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4253 | Changed = ChangeStatus::CHANGED; |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | return Changed | AAValueSimplify::manifest(A); |
| 4258 | } |
| 4259 | |
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4260 | /// See AbstractState::indicatePessimisticFixpoint(...). |
| 4261 | ChangeStatus indicatePessimisticFixpoint() override { |
| 4262 | // NOTE: Associated value will be returned in a pessimistic fixpoint and is |
| 4263 | // regarded as known. That's why`indicateOptimisticFixpoint` is called. |
| 4264 | SimplifiedAssociatedValue = &getAssociatedValue(); |
| Johannes Doerfert | a4b3588 | 2019-12-31 13:25:47 -0600 | [diff] [blame] | 4265 | indicateOptimisticFixpoint(); |
| 4266 | return ChangeStatus::CHANGED; |
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4267 | } |
| 4268 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4269 | protected: |
| 4270 | // An assumed simplified value. Initially, it is set to Optional::None, which |
| 4271 | // means that the value is not clear under current assumption. If in the |
| 4272 | // pessimistic state, getAssumedSimplifiedValue doesn't return this value but |
| 4273 | // returns orignal associated value. |
| 4274 | Optional<Value *> SimplifiedAssociatedValue; |
| 4275 | }; |
| 4276 | |
| 4277 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { |
| 4278 | AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4279 | |
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4280 | void initialize(Attributor &A) override { |
| 4281 | AAValueSimplifyImpl::initialize(A); |
| 4282 | if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) |
| 4283 | indicatePessimisticFixpoint(); |
| 4284 | if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, |
| 4285 | /* IgnoreSubsumingPositions */ true)) |
| 4286 | indicatePessimisticFixpoint(); |
| 4287 | } |
| 4288 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4289 | /// See AbstractAttribute::updateImpl(...). |
| 4290 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 4291 | // Byval is only replacable if it is readonly otherwise we would write into |
| 4292 | // the replaced value and not the copy that byval creates implicitly. |
| 4293 | Argument *Arg = getAssociatedArgument(); |
| 4294 | if (Arg->hasByValAttr()) { |
| 4295 | const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); |
| 4296 | if (!MemAA.isAssumedReadOnly()) |
| 4297 | return indicatePessimisticFixpoint(); |
| 4298 | } |
| 4299 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4300 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 4301 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4302 | auto PredForCallSite = [&](AbstractCallSite ACS) { |
| 4303 | // Check if we have an associated argument or not (which can happen for |
| 4304 | // callback calls). |
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4305 | Value *ArgOp = ACS.getCallArgOperand(getArgNo()); |
| 4306 | if (!ArgOp) |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4307 | return false; |
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4308 | // We can only propagate thread independent values through callbacks. |
| 4309 | // This is different to direct/indirect call sites because for them we |
| 4310 | // know the thread executing the caller and callee is the same. For |
| 4311 | // callbacks this is not guaranteed, thus a thread dependent value could |
| 4312 | // be different for the caller and callee, making it invalid to propagate. |
| 4313 | if (ACS.isCallbackCall()) |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 4314 | if (auto *C = dyn_cast<Constant>(ArgOp)) |
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 4315 | if (C->isThreadDependent()) |
| 4316 | return false; |
| 4317 | return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4318 | }; |
| 4319 | |
| 4320 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) |
| Hideto Ueno | 5fc02dc | 2020-01-03 11:03:56 +0900 | [diff] [blame] | 4321 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4322 | |
| 4323 | // If a candicate was found in this update, return CHANGED. |
| 4324 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 4325 | ? ChangeStatus::UNCHANGED |
| 4326 | : ChangeStatus ::CHANGED; |
| 4327 | } |
| 4328 | |
| 4329 | /// See AbstractAttribute::trackStatistics() |
| 4330 | void trackStatistics() const override { |
| 4331 | STATS_DECLTRACK_ARG_ATTR(value_simplify) |
| 4332 | } |
| 4333 | }; |
| 4334 | |
| 4335 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { |
| 4336 | AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4337 | |
| 4338 | /// See AbstractAttribute::updateImpl(...). |
| 4339 | ChangeStatus updateImpl(Attributor &A) override { |
| 4340 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 4341 | |
| 4342 | auto PredForReturned = [&](Value &V) { |
| 4343 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); |
| 4344 | }; |
| 4345 | |
| 4346 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) |
| Hideto Ueno | 5fc02dc | 2020-01-03 11:03:56 +0900 | [diff] [blame] | 4347 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4348 | |
| 4349 | // If a candicate was found in this update, return CHANGED. |
| 4350 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 4351 | ? ChangeStatus::UNCHANGED |
| 4352 | : ChangeStatus ::CHANGED; |
| 4353 | } |
| 4354 | /// See AbstractAttribute::trackStatistics() |
| 4355 | void trackStatistics() const override { |
| 4356 | STATS_DECLTRACK_FNRET_ATTR(value_simplify) |
| 4357 | } |
| 4358 | }; |
| 4359 | |
| 4360 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { |
| 4361 | AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4362 | |
| 4363 | /// See AbstractAttribute::initialize(...). |
| 4364 | void initialize(Attributor &A) override { |
| 4365 | Value &V = getAnchorValue(); |
| 4366 | |
| 4367 | // TODO: add other stuffs |
| Hideto Ueno | 1d5d074 | 2019-12-25 14:14:32 +0900 | [diff] [blame] | 4368 | if (isa<Constant>(V)) |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4369 | indicatePessimisticFixpoint(); |
| 4370 | } |
| 4371 | |
| 4372 | /// See AbstractAttribute::updateImpl(...). |
| 4373 | ChangeStatus updateImpl(Attributor &A) override { |
| 4374 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 4375 | |
| 4376 | auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool { |
| 4377 | auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); |
| 4378 | if (!Stripped && this == &AA) { |
| 4379 | // TODO: Look the instruction and check recursively. |
| 4380 | LLVM_DEBUG( |
| 4381 | dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : " |
| 4382 | << V << "\n"); |
| Hideto Ueno | 5fc02dc | 2020-01-03 11:03:56 +0900 | [diff] [blame] | 4383 | indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4384 | return false; |
| 4385 | } |
| 4386 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); |
| 4387 | }; |
| 4388 | |
| 4389 | if (!genericValueTraversal<AAValueSimplify, BooleanState>( |
| 4390 | A, getIRPosition(), *this, static_cast<BooleanState &>(*this), |
| 4391 | VisitValueCB)) |
| Hideto Ueno | 5fc02dc | 2020-01-03 11:03:56 +0900 | [diff] [blame] | 4392 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4393 | |
| 4394 | // If a candicate was found in this update, return CHANGED. |
| 4395 | |
| 4396 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 4397 | ? ChangeStatus::UNCHANGED |
| 4398 | : ChangeStatus ::CHANGED; |
| 4399 | } |
| 4400 | |
| 4401 | /// See AbstractAttribute::trackStatistics() |
| 4402 | void trackStatistics() const override { |
| 4403 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify) |
| 4404 | } |
| 4405 | }; |
| 4406 | |
| 4407 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { |
| 4408 | AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 4409 | |
| 4410 | /// See AbstractAttribute::initialize(...). |
| 4411 | void initialize(Attributor &A) override { |
| 4412 | SimplifiedAssociatedValue = &getAnchorValue(); |
| 4413 | indicateOptimisticFixpoint(); |
| 4414 | } |
| 4415 | /// See AbstractAttribute::initialize(...). |
| 4416 | ChangeStatus updateImpl(Attributor &A) override { |
| 4417 | llvm_unreachable( |
| 4418 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); |
| 4419 | } |
| 4420 | /// See AbstractAttribute::trackStatistics() |
| 4421 | void trackStatistics() const override { |
| 4422 | STATS_DECLTRACK_FN_ATTR(value_simplify) |
| 4423 | } |
| 4424 | }; |
| 4425 | |
| 4426 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { |
| 4427 | AAValueSimplifyCallSite(const IRPosition &IRP) |
| 4428 | : AAValueSimplifyFunction(IRP) {} |
| 4429 | /// See AbstractAttribute::trackStatistics() |
| 4430 | void trackStatistics() const override { |
| 4431 | STATS_DECLTRACK_CS_ATTR(value_simplify) |
| 4432 | } |
| 4433 | }; |
| 4434 | |
| 4435 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { |
| 4436 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP) |
| 4437 | : AAValueSimplifyReturned(IRP) {} |
| 4438 | |
| 4439 | void trackStatistics() const override { |
| 4440 | STATS_DECLTRACK_CSRET_ATTR(value_simplify) |
| 4441 | } |
| 4442 | }; |
| 4443 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { |
| 4444 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP) |
| 4445 | : AAValueSimplifyFloating(IRP) {} |
| 4446 | |
| 4447 | void trackStatistics() const override { |
| 4448 | STATS_DECLTRACK_CSARG_ATTR(value_simplify) |
| 4449 | } |
| 4450 | }; |
| 4451 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4452 | /// ----------------------- Heap-To-Stack Conversion --------------------------- |
| 4453 | struct AAHeapToStackImpl : public AAHeapToStack { |
| 4454 | AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} |
| 4455 | |
| 4456 | const std::string getAsStr() const override { |
| 4457 | return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); |
| 4458 | } |
| 4459 | |
| 4460 | ChangeStatus manifest(Attributor &A) override { |
| 4461 | assert(getState().isValidState() && |
| 4462 | "Attempted to manifest an invalid state!"); |
| 4463 | |
| 4464 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| 4465 | Function *F = getAssociatedFunction(); |
| 4466 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); |
| 4467 | |
| 4468 | for (Instruction *MallocCall : MallocCalls) { |
| 4469 | // This malloc cannot be replaced. |
| 4470 | if (BadMallocCalls.count(MallocCall)) |
| 4471 | continue; |
| 4472 | |
| 4473 | for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { |
| 4474 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); |
| 4475 | A.deleteAfterManifest(*FreeCall); |
| 4476 | HasChanged = ChangeStatus::CHANGED; |
| 4477 | } |
| 4478 | |
| 4479 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall |
| 4480 | << "\n"); |
| 4481 | |
| 4482 | Constant *Size; |
| 4483 | if (isCallocLikeFn(MallocCall, TLI)) { |
| 4484 | auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); |
| 4485 | auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); |
| 4486 | APInt TotalSize = SizeT->getValue() * Num->getValue(); |
| 4487 | Size = |
| 4488 | ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); |
| 4489 | } else { |
| 4490 | Size = cast<ConstantInt>(MallocCall->getOperand(0)); |
| 4491 | } |
| 4492 | |
| 4493 | unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); |
| 4494 | Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, |
| 4495 | Size, "", MallocCall->getNextNode()); |
| 4496 | |
| 4497 | if (AI->getType() != MallocCall->getType()) |
| 4498 | AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", |
| 4499 | AI->getNextNode()); |
| 4500 | |
| Johannes Doerfert | 5d34602 | 2019-12-13 22:11:42 -0600 | [diff] [blame] | 4501 | replaceAllInstructionUsesWith(*MallocCall, *AI); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4502 | |
| 4503 | if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { |
| 4504 | auto *NBB = II->getNormalDest(); |
| 4505 | BranchInst::Create(NBB, MallocCall->getParent()); |
| 4506 | A.deleteAfterManifest(*MallocCall); |
| 4507 | } else { |
| 4508 | A.deleteAfterManifest(*MallocCall); |
| 4509 | } |
| 4510 | |
| 4511 | if (isCallocLikeFn(MallocCall, TLI)) { |
| 4512 | auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", |
| 4513 | AI->getNextNode()); |
| 4514 | Value *Ops[] = { |
| 4515 | BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, |
| 4516 | ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; |
| 4517 | |
| 4518 | Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; |
| 4519 | Module *M = F->getParent(); |
| 4520 | Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
| 4521 | CallInst::Create(Fn, Ops, "", BI->getNextNode()); |
| 4522 | } |
| 4523 | HasChanged = ChangeStatus::CHANGED; |
| 4524 | } |
| 4525 | |
| 4526 | return HasChanged; |
| 4527 | } |
| 4528 | |
| 4529 | /// Collection of all malloc calls in a function. |
| 4530 | SmallSetVector<Instruction *, 4> MallocCalls; |
| 4531 | |
| 4532 | /// Collection of malloc calls that cannot be converted. |
| 4533 | DenseSet<const Instruction *> BadMallocCalls; |
| 4534 | |
| 4535 | /// A map for each malloc call to the set of associated free calls. |
| 4536 | DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; |
| 4537 | |
| 4538 | ChangeStatus updateImpl(Attributor &A) override; |
| 4539 | }; |
| 4540 | |
| 4541 | ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { |
| 4542 | const Function *F = getAssociatedFunction(); |
| 4543 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); |
| 4544 | |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4545 | MustBeExecutedContextExplorer &Explorer = |
| 4546 | A.getInfoCache().getMustBeExecutedContextExplorer(); |
| 4547 | |
| 4548 | auto FreeCheck = [&](Instruction &I) { |
| 4549 | const auto &Frees = FreesForMalloc.lookup(&I); |
| 4550 | if (Frees.size() != 1) |
| 4551 | return false; |
| 4552 | Instruction *UniqueFree = *Frees.begin(); |
| 4553 | return Explorer.findInContextOf(UniqueFree, I.getNextNode()); |
| 4554 | }; |
| 4555 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4556 | auto UsesCheck = [&](Instruction &I) { |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4557 | bool ValidUsesOnly = true; |
| 4558 | bool MustUse = true; |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4559 | auto Pred = [&](const Use &U, bool &Follow) -> bool { |
| 4560 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4561 | if (isa<LoadInst>(UserI)) |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4562 | return true; |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4563 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4564 | if (SI->getValueOperand() == U.get()) { |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4565 | LLVM_DEBUG(dbgs() |
| 4566 | << "[H2S] escaping store to memory: " << *UserI << "\n"); |
| 4567 | ValidUsesOnly = false; |
| 4568 | } else { |
| 4569 | // A store into the malloc'ed memory is fine. |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4570 | } |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4571 | return true; |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 4572 | } |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4573 | if (auto *CB = dyn_cast<CallBase>(UserI)) { |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4574 | if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) |
| 4575 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4576 | // Record malloc. |
| 4577 | if (isFreeCall(UserI, TLI)) { |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4578 | if (MustUse) { |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4579 | FreesForMalloc[&I].insert(UserI); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4580 | } else { |
| 4581 | LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " |
| 4582 | << *UserI << "\n"); |
| 4583 | ValidUsesOnly = false; |
| 4584 | } |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4585 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4588 | unsigned ArgNo = CB->getArgOperandNo(&U); |
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 4589 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4590 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( |
| 4591 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| 4592 | |
| Stefan Stipanovic | a516fba | 2019-11-17 21:35:04 +0100 | [diff] [blame] | 4593 | // If a callsite argument use is nofree, we are fine. |
| 4594 | const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( |
| 4595 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| 4596 | |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4597 | if (!NoCaptureAA.isAssumedNoCapture() || |
| 4598 | !ArgNoFreeAA.isAssumedNoFree()) { |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4599 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4600 | ValidUsesOnly = false; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4601 | } |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4602 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4605 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || |
| 4606 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { |
| 4607 | MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4608 | Follow = true; |
| 4609 | return true; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4610 | } |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4611 | // Unknown user for which we can not track uses further (in a way that |
| 4612 | // makes sense). |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4613 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4614 | ValidUsesOnly = false; |
| Hideto Ueno | 827bade | 2019-12-12 12:26:30 +0000 | [diff] [blame] | 4615 | return true; |
| 4616 | }; |
| 4617 | A.checkForAllUses(Pred, *this, I); |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4618 | return ValidUsesOnly; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4619 | }; |
| 4620 | |
| 4621 | auto MallocCallocCheck = [&](Instruction &I) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4622 | if (BadMallocCalls.count(&I)) |
| 4623 | return true; |
| 4624 | |
| 4625 | bool IsMalloc = isMallocLikeFn(&I, TLI); |
| 4626 | bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); |
| 4627 | if (!IsMalloc && !IsCalloc) { |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4628 | BadMallocCalls.insert(&I); |
| 4629 | return true; |
| 4630 | } |
| 4631 | |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4632 | if (IsMalloc) { |
| 4633 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) |
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 4634 | if (Size->getValue().ule(MaxHeapToStackSize)) |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4635 | if (UsesCheck(I) || FreeCheck(I)) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4636 | MallocCalls.insert(&I); |
| 4637 | return true; |
| 4638 | } |
| 4639 | } else if (IsCalloc) { |
| 4640 | bool Overflow = false; |
| 4641 | if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 4642 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) |
| 4643 | if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) |
| Stefan Stipanovic | fff8ec9 | 2019-12-17 20:41:09 +0100 | [diff] [blame] | 4644 | .ule(MaxHeapToStackSize)) |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4645 | if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4646 | MallocCalls.insert(&I); |
| 4647 | return true; |
| 4648 | } |
| 4649 | } |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4650 | |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4651 | BadMallocCalls.insert(&I); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4652 | return true; |
| 4653 | }; |
| 4654 | |
| 4655 | size_t NumBadMallocs = BadMallocCalls.size(); |
| 4656 | |
| 4657 | A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); |
| 4658 | |
| 4659 | if (NumBadMallocs != BadMallocCalls.size()) |
| 4660 | return ChangeStatus::CHANGED; |
| 4661 | |
| 4662 | return ChangeStatus::UNCHANGED; |
| 4663 | } |
| 4664 | |
| 4665 | struct AAHeapToStackFunction final : public AAHeapToStackImpl { |
| 4666 | AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} |
| 4667 | |
| 4668 | /// See AbstractAttribute::trackStatistics() |
| 4669 | void trackStatistics() const override { |
| 4670 | STATS_DECL(MallocCalls, Function, |
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4671 | "Number of malloc calls converted to allocas"); |
| 4672 | for (auto *C : MallocCalls) |
| 4673 | if (!BadMallocCalls.count(C)) |
| 4674 | ++BUILD_STAT_NAME(MallocCalls, Function); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4675 | } |
| 4676 | }; |
| 4677 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4678 | /// -------------------- Memory Behavior Attributes ---------------------------- |
| 4679 | /// Includes read-none, read-only, and write-only. |
| 4680 | /// ---------------------------------------------------------------------------- |
| 4681 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { |
| 4682 | AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} |
| 4683 | |
| 4684 | /// See AbstractAttribute::initialize(...). |
| 4685 | void initialize(Attributor &A) override { |
| 4686 | intersectAssumedBits(BEST_STATE); |
| 4687 | getKnownStateFromValue(getIRPosition(), getState()); |
| 4688 | IRAttribute::initialize(A); |
| 4689 | } |
| 4690 | |
| 4691 | /// Return the memory behavior information encoded in the IR for \p IRP. |
| 4692 | static void getKnownStateFromValue(const IRPosition &IRP, |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4693 | BitIntegerState &State, |
| 4694 | bool IgnoreSubsumingPositions = false) { |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4695 | SmallVector<Attribute, 2> Attrs; |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4696 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4697 | for (const Attribute &Attr : Attrs) { |
| 4698 | switch (Attr.getKindAsEnum()) { |
| 4699 | case Attribute::ReadNone: |
| 4700 | State.addKnownBits(NO_ACCESSES); |
| 4701 | break; |
| 4702 | case Attribute::ReadOnly: |
| 4703 | State.addKnownBits(NO_WRITES); |
| 4704 | break; |
| 4705 | case Attribute::WriteOnly: |
| 4706 | State.addKnownBits(NO_READS); |
| 4707 | break; |
| 4708 | default: |
| 4709 | llvm_unreachable("Unexpcted attribute!"); |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { |
| 4714 | if (!I->mayReadFromMemory()) |
| 4715 | State.addKnownBits(NO_READS); |
| 4716 | if (!I->mayWriteToMemory()) |
| 4717 | State.addKnownBits(NO_WRITES); |
| 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | /// See AbstractAttribute::getDeducedAttributes(...). |
| 4722 | void getDeducedAttributes(LLVMContext &Ctx, |
| 4723 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 4724 | assert(Attrs.size() == 0); |
| 4725 | if (isAssumedReadNone()) |
| 4726 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); |
| 4727 | else if (isAssumedReadOnly()) |
| 4728 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); |
| 4729 | else if (isAssumedWriteOnly()) |
| 4730 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); |
| 4731 | assert(Attrs.size() <= 1); |
| 4732 | } |
| 4733 | |
| 4734 | /// See AbstractAttribute::manifest(...). |
| 4735 | ChangeStatus manifest(Attributor &A) override { |
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 4736 | const IRPosition &IRP = getIRPosition(); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4737 | |
| 4738 | // Check if we would improve the existing attributes first. |
| 4739 | SmallVector<Attribute, 4> DeducedAttrs; |
| 4740 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); |
| 4741 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { |
| 4742 | return IRP.hasAttr(Attr.getKindAsEnum(), |
| 4743 | /* IgnoreSubsumingPositions */ true); |
| 4744 | })) |
| 4745 | return ChangeStatus::UNCHANGED; |
| 4746 | |
| 4747 | // Clear existing attributes. |
| 4748 | IRP.removeAttrs(AttrKinds); |
| 4749 | |
| 4750 | // Use the generic manifest method. |
| 4751 | return IRAttribute::manifest(A); |
| 4752 | } |
| 4753 | |
| 4754 | /// See AbstractState::getAsStr(). |
| 4755 | const std::string getAsStr() const override { |
| 4756 | if (isAssumedReadNone()) |
| 4757 | return "readnone"; |
| 4758 | if (isAssumedReadOnly()) |
| 4759 | return "readonly"; |
| 4760 | if (isAssumedWriteOnly()) |
| 4761 | return "writeonly"; |
| 4762 | return "may-read/write"; |
| 4763 | } |
| 4764 | |
| 4765 | /// The set of IR attributes AAMemoryBehavior deals with. |
| 4766 | static const Attribute::AttrKind AttrKinds[3]; |
| 4767 | }; |
| 4768 | |
| 4769 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { |
| 4770 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; |
| 4771 | |
| 4772 | /// Memory behavior attribute for a floating value. |
| 4773 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { |
| 4774 | AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 4775 | |
| 4776 | /// See AbstractAttribute::initialize(...). |
| 4777 | void initialize(Attributor &A) override { |
| 4778 | AAMemoryBehaviorImpl::initialize(A); |
| 4779 | // Initialize the use vector with all direct uses of the associated value. |
| 4780 | for (const Use &U : getAssociatedValue().uses()) |
| 4781 | Uses.insert(&U); |
| 4782 | } |
| 4783 | |
| 4784 | /// See AbstractAttribute::updateImpl(...). |
| 4785 | ChangeStatus updateImpl(Attributor &A) override; |
| 4786 | |
| 4787 | /// See AbstractAttribute::trackStatistics() |
| 4788 | void trackStatistics() const override { |
| 4789 | if (isAssumedReadNone()) |
| 4790 | STATS_DECLTRACK_FLOATING_ATTR(readnone) |
| 4791 | else if (isAssumedReadOnly()) |
| 4792 | STATS_DECLTRACK_FLOATING_ATTR(readonly) |
| 4793 | else if (isAssumedWriteOnly()) |
| 4794 | STATS_DECLTRACK_FLOATING_ATTR(writeonly) |
| 4795 | } |
| 4796 | |
| 4797 | private: |
| 4798 | /// Return true if users of \p UserI might access the underlying |
| 4799 | /// variable/location described by \p U and should therefore be analyzed. |
| 4800 | bool followUsersOfUseIn(Attributor &A, const Use *U, |
| 4801 | const Instruction *UserI); |
| 4802 | |
| 4803 | /// Update the state according to the effect of use \p U in \p UserI. |
| 4804 | void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); |
| 4805 | |
| 4806 | protected: |
| 4807 | /// Container for (transitive) uses of the associated argument. |
| 4808 | SetVector<const Use *> Uses; |
| 4809 | }; |
| 4810 | |
| 4811 | /// Memory behavior attribute for function argument. |
| 4812 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { |
| 4813 | AAMemoryBehaviorArgument(const IRPosition &IRP) |
| 4814 | : AAMemoryBehaviorFloating(IRP) {} |
| 4815 | |
| 4816 | /// See AbstractAttribute::initialize(...). |
| 4817 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4818 | intersectAssumedBits(BEST_STATE); |
| 4819 | const IRPosition &IRP = getIRPosition(); |
| 4820 | // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we |
| 4821 | // can query it when we use has/getAttr. That would allow us to reuse the |
| 4822 | // initialize of the base class here. |
| 4823 | bool HasByVal = |
| 4824 | IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); |
| 4825 | getKnownStateFromValue(IRP, getState(), |
| 4826 | /* IgnoreSubsumingPositions */ HasByVal); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4827 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4828 | // Initialize the use vector with all direct uses of the associated value. |
| 4829 | Argument *Arg = getAssociatedArgument(); |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4830 | if (!Arg || !Arg->getParent()->hasExactDefinition()) { |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4831 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4832 | } else { |
| 4833 | // Initialize the use vector with all direct uses of the associated value. |
| 4834 | for (const Use &U : Arg->uses()) |
| 4835 | Uses.insert(&U); |
| 4836 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4839 | ChangeStatus manifest(Attributor &A) override { |
| 4840 | // TODO: From readattrs.ll: "inalloca parameters are always |
| 4841 | // considered written" |
| 4842 | if (hasAttr({Attribute::InAlloca})) { |
| 4843 | removeKnownBits(NO_WRITES); |
| 4844 | removeAssumedBits(NO_WRITES); |
| 4845 | } |
| 4846 | return AAMemoryBehaviorFloating::manifest(A); |
| 4847 | } |
| 4848 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4849 | /// See AbstractAttribute::trackStatistics() |
| 4850 | void trackStatistics() const override { |
| 4851 | if (isAssumedReadNone()) |
| 4852 | STATS_DECLTRACK_ARG_ATTR(readnone) |
| 4853 | else if (isAssumedReadOnly()) |
| 4854 | STATS_DECLTRACK_ARG_ATTR(readonly) |
| 4855 | else if (isAssumedWriteOnly()) |
| 4856 | STATS_DECLTRACK_ARG_ATTR(writeonly) |
| 4857 | } |
| 4858 | }; |
| 4859 | |
| 4860 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { |
| 4861 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) |
| 4862 | : AAMemoryBehaviorArgument(IRP) {} |
| 4863 | |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 4864 | /// See AbstractAttribute::initialize(...). |
| 4865 | void initialize(Attributor &A) override { |
| 4866 | if (Argument *Arg = getAssociatedArgument()) { |
| 4867 | if (Arg->hasByValAttr()) { |
| 4868 | addKnownBits(NO_WRITES); |
| 4869 | removeKnownBits(NO_READS); |
| 4870 | removeAssumedBits(NO_READS); |
| 4871 | } |
| 4872 | } else { |
| 4873 | } |
| 4874 | AAMemoryBehaviorArgument::initialize(A); |
| 4875 | } |
| 4876 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4877 | /// See AbstractAttribute::updateImpl(...). |
| 4878 | ChangeStatus updateImpl(Attributor &A) override { |
| 4879 | // TODO: Once we have call site specific value information we can provide |
| 4880 | // call site specific liveness liveness information and then it makes |
| 4881 | // sense to specialize attributes for call sites arguments instead of |
| 4882 | // redirecting requests to the callee argument. |
| 4883 | Argument *Arg = getAssociatedArgument(); |
| 4884 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 4885 | auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); |
| 4886 | return clampStateAndIndicateChange( |
| 4887 | getState(), |
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4888 | static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | /// See AbstractAttribute::trackStatistics() |
| 4892 | void trackStatistics() const override { |
| 4893 | if (isAssumedReadNone()) |
| 4894 | STATS_DECLTRACK_CSARG_ATTR(readnone) |
| 4895 | else if (isAssumedReadOnly()) |
| 4896 | STATS_DECLTRACK_CSARG_ATTR(readonly) |
| 4897 | else if (isAssumedWriteOnly()) |
| 4898 | STATS_DECLTRACK_CSARG_ATTR(writeonly) |
| 4899 | } |
| 4900 | }; |
| 4901 | |
| 4902 | /// Memory behavior attribute for a call site return position. |
| 4903 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { |
| 4904 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) |
| 4905 | : AAMemoryBehaviorFloating(IRP) {} |
| 4906 | |
| 4907 | /// See AbstractAttribute::manifest(...). |
| 4908 | ChangeStatus manifest(Attributor &A) override { |
| 4909 | // We do not annotate returned values. |
| 4910 | return ChangeStatus::UNCHANGED; |
| 4911 | } |
| 4912 | |
| 4913 | /// See AbstractAttribute::trackStatistics() |
| 4914 | void trackStatistics() const override {} |
| 4915 | }; |
| 4916 | |
| 4917 | /// An AA to represent the memory behavior function attributes. |
| 4918 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { |
| 4919 | AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 4920 | |
| 4921 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| 4922 | virtual ChangeStatus updateImpl(Attributor &A) override; |
| 4923 | |
| 4924 | /// See AbstractAttribute::manifest(...). |
| 4925 | ChangeStatus manifest(Attributor &A) override { |
| 4926 | Function &F = cast<Function>(getAnchorValue()); |
| 4927 | if (isAssumedReadNone()) { |
| 4928 | F.removeFnAttr(Attribute::ArgMemOnly); |
| 4929 | F.removeFnAttr(Attribute::InaccessibleMemOnly); |
| 4930 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
| 4931 | } |
| 4932 | return AAMemoryBehaviorImpl::manifest(A); |
| 4933 | } |
| 4934 | |
| 4935 | /// See AbstractAttribute::trackStatistics() |
| 4936 | void trackStatistics() const override { |
| 4937 | if (isAssumedReadNone()) |
| 4938 | STATS_DECLTRACK_FN_ATTR(readnone) |
| 4939 | else if (isAssumedReadOnly()) |
| 4940 | STATS_DECLTRACK_FN_ATTR(readonly) |
| 4941 | else if (isAssumedWriteOnly()) |
| 4942 | STATS_DECLTRACK_FN_ATTR(writeonly) |
| 4943 | } |
| 4944 | }; |
| 4945 | |
| 4946 | /// AAMemoryBehavior attribute for call sites. |
| 4947 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { |
| 4948 | AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 4949 | |
| 4950 | /// See AbstractAttribute::initialize(...). |
| 4951 | void initialize(Attributor &A) override { |
| 4952 | AAMemoryBehaviorImpl::initialize(A); |
| 4953 | Function *F = getAssociatedFunction(); |
| 4954 | if (!F || !F->hasExactDefinition()) |
| 4955 | indicatePessimisticFixpoint(); |
| 4956 | } |
| 4957 | |
| 4958 | /// See AbstractAttribute::updateImpl(...). |
| 4959 | ChangeStatus updateImpl(Attributor &A) override { |
| 4960 | // TODO: Once we have call site specific value information we can provide |
| 4961 | // call site specific liveness liveness information and then it makes |
| 4962 | // sense to specialize attributes for call sites arguments instead of |
| 4963 | // redirecting requests to the callee argument. |
| 4964 | Function *F = getAssociatedFunction(); |
| 4965 | const IRPosition &FnPos = IRPosition::function(*F); |
| 4966 | auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); |
| 4967 | return clampStateAndIndicateChange( |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4968 | getState(), |
| 4969 | static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | /// See AbstractAttribute::trackStatistics() |
| 4973 | void trackStatistics() const override { |
| 4974 | if (isAssumedReadNone()) |
| 4975 | STATS_DECLTRACK_CS_ATTR(readnone) |
| 4976 | else if (isAssumedReadOnly()) |
| 4977 | STATS_DECLTRACK_CS_ATTR(readonly) |
| 4978 | else if (isAssumedWriteOnly()) |
| 4979 | STATS_DECLTRACK_CS_ATTR(writeonly) |
| 4980 | } |
| 4981 | }; |
| Benjamin Kramer | c5d1d56 | 2019-10-12 11:01:52 +0000 | [diff] [blame] | 4982 | } // namespace |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4983 | |
| 4984 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { |
| 4985 | |
| 4986 | // The current assumed state used to determine a change. |
| 4987 | auto AssumedState = getAssumed(); |
| 4988 | |
| 4989 | auto CheckRWInst = [&](Instruction &I) { |
| 4990 | // If the instruction has an own memory behavior state, use it to restrict |
| 4991 | // the local state. No further analysis is required as the other memory |
| 4992 | // state is as optimistic as it gets. |
| 4993 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 4994 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( |
| 4995 | *this, IRPosition::callsite_function(ICS)); |
| 4996 | intersectAssumedBits(MemBehaviorAA.getAssumed()); |
| 4997 | return !isAtFixpoint(); |
| 4998 | } |
| 4999 | |
| 5000 | // Remove access kind modifiers if necessary. |
| 5001 | if (I.mayReadFromMemory()) |
| 5002 | removeAssumedBits(NO_READS); |
| 5003 | if (I.mayWriteToMemory()) |
| 5004 | removeAssumedBits(NO_WRITES); |
| 5005 | return !isAtFixpoint(); |
| 5006 | }; |
| 5007 | |
| 5008 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) |
| 5009 | return indicatePessimisticFixpoint(); |
| 5010 | |
| 5011 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED |
| 5012 | : ChangeStatus::UNCHANGED; |
| 5013 | } |
| 5014 | |
| 5015 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { |
| 5016 | |
| 5017 | const IRPosition &IRP = getIRPosition(); |
| 5018 | const IRPosition &FnPos = IRPosition::function_scope(IRP); |
| 5019 | AAMemoryBehavior::StateType &S = getState(); |
| 5020 | |
| 5021 | // First, check the function scope. We take the known information and we avoid |
| 5022 | // work if the assumed information implies the current assumed information for |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5023 | // this attribute. This is a valid for all but byval arguments. |
| 5024 | Argument *Arg = IRP.getAssociatedArgument(); |
| 5025 | AAMemoryBehavior::base_t FnMemAssumedState = |
| 5026 | AAMemoryBehavior::StateType::getWorstState(); |
| 5027 | if (!Arg || !Arg->hasByValAttr()) { |
| 5028 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); |
| 5029 | FnMemAssumedState = FnMemAA.getAssumed(); |
| 5030 | S.addKnownBits(FnMemAA.getKnown()); |
| 5031 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) |
| 5032 | return ChangeStatus::UNCHANGED; |
| 5033 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5034 | |
| 5035 | // Make sure the value is not captured (except through "return"), if |
| 5036 | // it is, any information derived would be irrelevant anyway as we cannot |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5037 | // check the potential aliases introduced by the capture. However, no need |
| 5038 | // to fall back to anythign less optimistic than the function state. |
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 5039 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( |
| 5040 | *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5041 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| Johannes Doerfert | 6abd01e | 2019-12-12 15:02:36 -0600 | [diff] [blame] | 5042 | S.intersectAssumedBits(FnMemAssumedState); |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 5043 | return ChangeStatus::CHANGED; |
| 5044 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5045 | |
| 5046 | // The current assumed state used to determine a change. |
| 5047 | auto AssumedState = S.getAssumed(); |
| 5048 | |
| 5049 | // Liveness information to exclude dead users. |
| 5050 | // TODO: Take the FnPos once we have call site specific liveness information. |
| 5051 | const auto &LivenessAA = A.getAAFor<AAIsDead>( |
| 5052 | *this, IRPosition::function(*IRP.getAssociatedFunction())); |
| 5053 | |
| 5054 | // Visit and expand uses until all are analyzed or a fixpoint is reached. |
| 5055 | for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { |
| 5056 | const Use *U = Uses[i]; |
| 5057 | Instruction *UserI = cast<Instruction>(U->getUser()); |
| 5058 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI |
| 5059 | << " [Dead: " << (LivenessAA.isAssumedDead(UserI)) |
| 5060 | << "]\n"); |
| 5061 | if (LivenessAA.isAssumedDead(UserI)) |
| 5062 | continue; |
| 5063 | |
| 5064 | // Check if the users of UserI should also be visited. |
| 5065 | if (followUsersOfUseIn(A, U, UserI)) |
| 5066 | for (const Use &UserIUse : UserI->uses()) |
| 5067 | Uses.insert(&UserIUse); |
| 5068 | |
| 5069 | // If UserI might touch memory we analyze the use in detail. |
| 5070 | if (UserI->mayReadOrWriteMemory()) |
| 5071 | analyzeUseIn(A, U, UserI); |
| 5072 | } |
| 5073 | |
| 5074 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED |
| 5075 | : ChangeStatus::UNCHANGED; |
| 5076 | } |
| 5077 | |
| 5078 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, |
| 5079 | const Instruction *UserI) { |
| 5080 | // The loaded value is unrelated to the pointer argument, no need to |
| 5081 | // follow the users of the load. |
| 5082 | if (isa<LoadInst>(UserI)) |
| 5083 | return false; |
| 5084 | |
| 5085 | // By default we follow all uses assuming UserI might leak information on U, |
| 5086 | // we have special handling for call sites operands though. |
| 5087 | ImmutableCallSite ICS(UserI); |
| 5088 | if (!ICS || !ICS.isArgOperand(U)) |
| 5089 | return true; |
| 5090 | |
| 5091 | // If the use is a call argument known not to be captured, the users of |
| 5092 | // the call do not need to be visited because they have to be unrelated to |
| 5093 | // the input. Note that this check is not trivial even though we disallow |
| 5094 | // general capturing of the underlying argument. The reason is that the |
| 5095 | // call might the argument "through return", which we allow and for which we |
| 5096 | // need to check call users. |
| 5097 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 5098 | const auto &ArgNoCaptureAA = |
| 5099 | A.getAAFor<AANoCapture>(*this, IRPosition::callsite_argument(ICS, ArgNo)); |
| 5100 | return !ArgNoCaptureAA.isAssumedNoCapture(); |
| 5101 | } |
| 5102 | |
| 5103 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, |
| 5104 | const Instruction *UserI) { |
| 5105 | assert(UserI->mayReadOrWriteMemory()); |
| 5106 | |
| 5107 | switch (UserI->getOpcode()) { |
| 5108 | default: |
| 5109 | // TODO: Handle all atomics and other side-effect operations we know of. |
| 5110 | break; |
| 5111 | case Instruction::Load: |
| 5112 | // Loads cause the NO_READS property to disappear. |
| 5113 | removeAssumedBits(NO_READS); |
| 5114 | return; |
| 5115 | |
| 5116 | case Instruction::Store: |
| 5117 | // Stores cause the NO_WRITES property to disappear if the use is the |
| 5118 | // pointer operand. Note that we do assume that capturing was taken care of |
| 5119 | // somewhere else. |
| 5120 | if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) |
| 5121 | removeAssumedBits(NO_WRITES); |
| 5122 | return; |
| 5123 | |
| 5124 | case Instruction::Call: |
| 5125 | case Instruction::CallBr: |
| 5126 | case Instruction::Invoke: { |
| 5127 | // For call sites we look at the argument memory behavior attribute (this |
| 5128 | // could be recursive!) in order to restrict our own state. |
| 5129 | ImmutableCallSite ICS(UserI); |
| 5130 | |
| 5131 | // Give up on operand bundles. |
| 5132 | if (ICS.isBundleOperand(U)) { |
| 5133 | indicatePessimisticFixpoint(); |
| 5134 | return; |
| 5135 | } |
| 5136 | |
| 5137 | // Calling a function does read the function pointer, maybe write it if the |
| 5138 | // function is self-modifying. |
| 5139 | if (ICS.isCallee(U)) { |
| 5140 | removeAssumedBits(NO_READS); |
| 5141 | break; |
| 5142 | } |
| 5143 | |
| 5144 | // Adjust the possible access behavior based on the information on the |
| 5145 | // argument. |
| 5146 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 5147 | const IRPosition &ArgPos = IRPosition::callsite_argument(ICS, ArgNo); |
| 5148 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); |
| 5149 | // "assumed" has at most the same bits as the MemBehaviorAA assumed |
| 5150 | // and at least "known". |
| 5151 | intersectAssumedBits(MemBehaviorAA.getAssumed()); |
| 5152 | return; |
| 5153 | } |
| 5154 | }; |
| 5155 | |
| 5156 | // Generally, look at the "may-properties" and adjust the assumed state if we |
| 5157 | // did not trigger special handling before. |
| 5158 | if (UserI->mayReadFromMemory()) |
| 5159 | removeAssumedBits(NO_READS); |
| 5160 | if (UserI->mayWriteToMemory()) |
| 5161 | removeAssumedBits(NO_WRITES); |
| 5162 | } |
| Hideto Ueno | e996303 | 2020-01-01 15:25:19 +0900 | [diff] [blame] | 5163 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5164 | /// ---------------------------------------------------------------------------- |
| 5165 | /// Attributor |
| 5166 | /// ---------------------------------------------------------------------------- |
| 5167 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5168 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, |
| 5169 | const AAIsDead *LivenessAA) { |
| 5170 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); |
| 5171 | if (!CtxI) |
| 5172 | return false; |
| 5173 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5174 | // TODO: Find a good way to utilize fine and coarse grained liveness |
| 5175 | // information. |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5176 | if (!LivenessAA) |
| 5177 | LivenessAA = |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5178 | &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), |
| 5179 | /* TrackDependence */ false); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 5180 | |
| 5181 | // Don't check liveness for AAIsDead. |
| 5182 | if (&AA == LivenessAA) |
| 5183 | return false; |
| 5184 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5185 | if (!LivenessAA->isAssumedDead(CtxI)) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5186 | return false; |
| 5187 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5188 | // We actually used liveness information so we have to record a dependence. |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5189 | recordDependence(*LivenessAA, AA, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5190 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5191 | return true; |
| 5192 | } |
| 5193 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5194 | bool Attributor::checkForAllUses( |
| 5195 | const function_ref<bool(const Use &, bool &)> &Pred, |
| 5196 | const AbstractAttribute &QueryingAA, const Value &V) { |
| 5197 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 5198 | SmallVector<const Use *, 16> Worklist; |
| 5199 | SmallPtrSet<const Use *, 16> Visited; |
| 5200 | |
| 5201 | for (const Use &U : V.uses()) |
| 5202 | Worklist.push_back(&U); |
| 5203 | |
| 5204 | LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() |
| 5205 | << " initial uses to check\n"); |
| 5206 | |
| 5207 | if (Worklist.empty()) |
| 5208 | return true; |
| 5209 | |
| 5210 | bool AnyDead = false; |
| 5211 | const Function *ScopeFn = IRP.getAnchorScope(); |
| 5212 | const auto *LivenessAA = |
| 5213 | ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), |
| 5214 | /* TrackDependence */ false) |
| 5215 | : nullptr; |
| 5216 | |
| 5217 | while (!Worklist.empty()) { |
| 5218 | const Use *U = Worklist.pop_back_val(); |
| 5219 | if (!Visited.insert(U).second) |
| 5220 | continue; |
| 5221 | LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << "\n"); |
| 5222 | if (Instruction *UserI = dyn_cast<Instruction>(U->getUser())) |
| 5223 | if (LivenessAA && LivenessAA->isAssumedDead(UserI)) { |
| 5224 | LLVM_DEBUG(dbgs() << "[Attributor] Dead user: " << *UserI << ": " |
| Johannes Doerfert | 3d347e2 | 2019-12-13 23:35:45 -0600 | [diff] [blame] | 5225 | << *LivenessAA << "\n"); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5226 | AnyDead = true; |
| 5227 | continue; |
| 5228 | } |
| 5229 | |
| 5230 | bool Follow = false; |
| 5231 | if (!Pred(*U, Follow)) |
| 5232 | return false; |
| 5233 | if (!Follow) |
| 5234 | continue; |
| 5235 | for (const Use &UU : U->getUser()->uses()) |
| 5236 | Worklist.push_back(&UU); |
| 5237 | } |
| 5238 | |
| 5239 | if (AnyDead) |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5240 | recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5241 | |
| 5242 | return true; |
| 5243 | } |
| 5244 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5245 | bool Attributor::checkForAllCallSites( |
| 5246 | const function_ref<bool(AbstractCallSite)> &Pred, |
| 5247 | const AbstractAttribute &QueryingAA, bool RequireAllCallSites) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5248 | // We can try to determine information from |
| 5249 | // the call sites. However, this is only possible all call sites are known, |
| 5250 | // hence the function has internal linkage. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5251 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 5252 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 5253 | if (!AssociatedFunction) { |
| 5254 | LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP |
| 5255 | << "\n"); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5256 | return false; |
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 5257 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5258 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5259 | return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, |
| 5260 | &QueryingAA); |
| 5261 | } |
| 5262 | |
| 5263 | bool Attributor::checkForAllCallSites( |
| 5264 | const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, |
| 5265 | bool RequireAllCallSites, const AbstractAttribute *QueryingAA) { |
| 5266 | if (RequireAllCallSites && !Fn.hasLocalLinkage()) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5267 | LLVM_DEBUG( |
| 5268 | dbgs() |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5269 | << "[Attributor] Function " << Fn.getName() |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5270 | << " has no internal linkage, hence not all call sites are known\n"); |
| 5271 | return false; |
| 5272 | } |
| 5273 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5274 | for (const Use &U : Fn.uses()) { |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5275 | AbstractCallSite ACS(&U); |
| 5276 | if (!ACS) { |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5277 | LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5278 | << " has non call site use " << *U.get() << " in " |
| 5279 | << *U.getUser() << "\n"); |
| Johannes Doerfert | 2d77b0c | 2019-11-01 22:35:18 -0500 | [diff] [blame] | 5280 | // BlockAddress users are allowed. |
| 5281 | if (isa<BlockAddress>(U.getUser())) |
| 5282 | continue; |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 5283 | return false; |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5284 | } |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 5285 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5286 | Instruction *I = ACS.getInstruction(); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5287 | Function *Caller = I->getFunction(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 5288 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5289 | const auto *LivenessAA = |
| 5290 | lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA, |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5291 | /* TrackDependence */ false); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 5292 | |
| 5293 | // Skip dead calls. |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5294 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5295 | // We actually used liveness information so we have to record a |
| 5296 | // dependence. |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 5297 | if (QueryingAA) |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5298 | recordDependence(*LivenessAA, *QueryingAA, DepClassTy::OPTIONAL); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 5299 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5300 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5301 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5302 | const Use *EffectiveUse = |
| 5303 | ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; |
| 5304 | if (!ACS.isCallee(EffectiveUse)) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5305 | if (!RequireAllCallSites) |
| 5306 | continue; |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5307 | LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5308 | << " is an invalid use of " << Fn.getName() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5309 | return false; |
| 5310 | } |
| 5311 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5312 | if (Pred(ACS)) |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5313 | continue; |
| 5314 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 5315 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 5316 | << *ACS.getInstruction() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5317 | return false; |
| 5318 | } |
| 5319 | |
| 5320 | return true; |
| 5321 | } |
| 5322 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5323 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 5324 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5325 | &Pred, |
| 5326 | const AbstractAttribute &QueryingAA) { |
| 5327 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5328 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 5329 | // Since we need to provide return instructions we have to have an exact |
| 5330 | // definition. |
| 5331 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5332 | if (!AssociatedFunction) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5333 | return false; |
| 5334 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5335 | // If this is a call site query we use the call site specific return values |
| 5336 | // and liveness information. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5337 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 5338 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5339 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5340 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5341 | return false; |
| 5342 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5343 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | bool Attributor::checkForAllReturnedValues( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5347 | const function_ref<bool(Value &)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5348 | const AbstractAttribute &QueryingAA) { |
| 5349 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5350 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 5351 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5352 | if (!AssociatedFunction) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5353 | return false; |
| 5354 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5355 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 5356 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5357 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5358 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5359 | return false; |
| 5360 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5361 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 5362 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 5363 | return Pred(RV); |
| 5364 | }); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5367 | static bool |
| 5368 | checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap, |
| 5369 | const function_ref<bool(Instruction &)> &Pred, |
| 5370 | const AAIsDead *LivenessAA, bool &AnyDead, |
| 5371 | const ArrayRef<unsigned> &Opcodes) { |
| 5372 | for (unsigned Opcode : Opcodes) { |
| 5373 | for (Instruction *I : OpcodeInstMap[Opcode]) { |
| 5374 | // Skip dead instructions. |
| 5375 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { |
| 5376 | AnyDead = true; |
| 5377 | continue; |
| 5378 | } |
| 5379 | |
| 5380 | if (!Pred(*I)) |
| 5381 | return false; |
| 5382 | } |
| 5383 | } |
| 5384 | return true; |
| 5385 | } |
| 5386 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5387 | bool Attributor::checkForAllInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5388 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 5389 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5390 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5391 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 5392 | // Since we need to provide instructions we have to have an exact definition. |
| 5393 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5394 | if (!AssociatedFunction) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5395 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5396 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5397 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 5398 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5399 | const auto &LivenessAA = |
| 5400 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| 5401 | bool AnyDead = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5402 | |
| 5403 | auto &OpcodeInstMap = |
| 5404 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5405 | if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, |
| 5406 | Opcodes)) |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5407 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5408 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5409 | // If we actually used liveness information so we have to record a dependence. |
| 5410 | if (AnyDead) |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5411 | recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5412 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 5413 | return true; |
| 5414 | } |
| 5415 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5416 | bool Attributor::checkForAllReadWriteInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5417 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 5418 | AbstractAttribute &QueryingAA) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5419 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5420 | const Function *AssociatedFunction = |
| 5421 | QueryingAA.getIRPosition().getAssociatedFunction(); |
| 5422 | if (!AssociatedFunction) |
| 5423 | return false; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5424 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 5425 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 5426 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| 5427 | const auto &LivenessAA = |
| 5428 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5429 | bool AnyDead = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5430 | |
| 5431 | for (Instruction *I : |
| 5432 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5433 | // Skip dead instructions. |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5434 | if (LivenessAA.isAssumedDead(I)) { |
| 5435 | AnyDead = true; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5436 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5437 | } |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5438 | |
| 5439 | if (!Pred(*I)) |
| 5440 | return false; |
| 5441 | } |
| 5442 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5443 | // If we actually used liveness information so we have to record a dependence. |
| 5444 | if (AnyDead) |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5445 | recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 5446 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 5447 | return true; |
| 5448 | } |
| 5449 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5450 | ChangeStatus Attributor::run(Module &M) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5451 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " |
| 5452 | << AllAbstractAttributes.size() |
| 5453 | << " abstract attributes.\n"); |
| 5454 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 5455 | // Now that all abstract attributes are collected and initialized we start |
| 5456 | // the abstract analysis. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5457 | |
| 5458 | unsigned IterationCounter = 1; |
| 5459 | |
| 5460 | SmallVector<AbstractAttribute *, 64> ChangedAAs; |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5461 | SetVector<AbstractAttribute *> Worklist, InvalidAAs; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5462 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); |
| 5463 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5464 | bool RecomputeDependences = false; |
| 5465 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5466 | do { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5467 | // Remember the size to determine new attributes. |
| 5468 | size_t NumAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5469 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter |
| 5470 | << ", Worklist size: " << Worklist.size() << "\n"); |
| 5471 | |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5472 | // For invalid AAs we can fix dependent AAs that have a required dependence, |
| 5473 | // thereby folding long dependence chains in a single step without the need |
| 5474 | // to run updates. |
| 5475 | for (unsigned u = 0; u < InvalidAAs.size(); ++u) { |
| 5476 | AbstractAttribute *InvalidAA = InvalidAAs[u]; |
| 5477 | auto &QuerriedAAs = QueryMap[InvalidAA]; |
| 5478 | LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " |
| 5479 | << QuerriedAAs.RequiredAAs.size() << "/" |
| 5480 | << QuerriedAAs.OptionalAAs.size() |
| 5481 | << " required/optional dependences\n"); |
| 5482 | for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) { |
| 5483 | AbstractState &DOIAAState = DepOnInvalidAA->getState(); |
| 5484 | DOIAAState.indicatePessimisticFixpoint(); |
| 5485 | ++NumAttributesFixedDueToRequiredDependences; |
| 5486 | assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!"); |
| 5487 | if (!DOIAAState.isValidState()) |
| 5488 | InvalidAAs.insert(DepOnInvalidAA); |
| 5489 | } |
| 5490 | if (!RecomputeDependences) |
| 5491 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), |
| 5492 | QuerriedAAs.OptionalAAs.end()); |
| 5493 | } |
| 5494 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5495 | // If dependences (=QueryMap) are recomputed we have to look at all abstract |
| 5496 | // attributes again, regardless of what changed in the last iteration. |
| 5497 | if (RecomputeDependences) { |
| 5498 | LLVM_DEBUG( |
| 5499 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); |
| 5500 | QueryMap.clear(); |
| 5501 | ChangedAAs.clear(); |
| 5502 | Worklist.insert(AllAbstractAttributes.begin(), |
| 5503 | AllAbstractAttributes.end()); |
| 5504 | } |
| 5505 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5506 | // Add all abstract attributes that are potentially dependent on one that |
| 5507 | // changed to the work list. |
| 5508 | for (AbstractAttribute *ChangedAA : ChangedAAs) { |
| 5509 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5510 | Worklist.insert(QuerriedAAs.OptionalAAs.begin(), |
| 5511 | QuerriedAAs.OptionalAAs.end()); |
| 5512 | Worklist.insert(QuerriedAAs.RequiredAAs.begin(), |
| 5513 | QuerriedAAs.RequiredAAs.end()); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 5516 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter |
| 5517 | << ", Worklist+Dependent size: " << Worklist.size() |
| 5518 | << "\n"); |
| 5519 | |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5520 | // Reset the changed and invalid set. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5521 | ChangedAAs.clear(); |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5522 | InvalidAAs.clear(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5523 | |
| 5524 | // Update all abstract attribute in the work list and record the ones that |
| 5525 | // changed. |
| 5526 | for (AbstractAttribute *AA : Worklist) |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 5527 | if (!AA->getState().isAtFixpoint() && !isAssumedDead(*AA, nullptr)) { |
| 5528 | QueriedNonFixAA = false; |
| 5529 | if (AA->update(*this) == ChangeStatus::CHANGED) { |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5530 | ChangedAAs.push_back(AA); |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5531 | if (!AA->getState().isValidState()) |
| 5532 | InvalidAAs.insert(AA); |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 5533 | } else if (!QueriedNonFixAA) { |
| 5534 | // If the attribute did not query any non-fix information, the state |
| 5535 | // will not change and we can indicate that right away. |
| 5536 | AA->getState().indicateOptimisticFixpoint(); |
| 5537 | } |
| 5538 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5539 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5540 | // Check if we recompute the dependences in the next iteration. |
| 5541 | RecomputeDependences = (DepRecomputeInterval > 0 && |
| 5542 | IterationCounter % DepRecomputeInterval == 0); |
| 5543 | |
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 5544 | // Add attributes to the changed set if they have been created in the last |
| 5545 | // iteration. |
| 5546 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, |
| 5547 | AllAbstractAttributes.end()); |
| 5548 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5549 | // Reset the work list and repopulate with the changed abstract attributes. |
| 5550 | // Note that dependent ones are added above. |
| 5551 | Worklist.clear(); |
| 5552 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); |
| 5553 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5554 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || |
| 5555 | VerifyMaxFixpointIterations)); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5556 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5557 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " |
| 5558 | << IterationCounter << "/" << MaxFixpointIterations |
| 5559 | << " iterations\n"); |
| 5560 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5561 | size_t NumFinalAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 5562 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5563 | // Reset abstract arguments not settled in a sound fixpoint by now. This |
| 5564 | // happens when we stopped the fixpoint iteration early. Note that only the |
| 5565 | // ones marked as "changed" *and* the ones transitively depending on them |
| 5566 | // need to be reverted to a pessimistic state. Others might not be in a |
| 5567 | // fixpoint state but we can use the optimistic results for them anyway. |
| 5568 | SmallPtrSet<AbstractAttribute *, 32> Visited; |
| 5569 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { |
| 5570 | AbstractAttribute *ChangedAA = ChangedAAs[u]; |
| 5571 | if (!Visited.insert(ChangedAA).second) |
| 5572 | continue; |
| 5573 | |
| 5574 | AbstractState &State = ChangedAA->getState(); |
| 5575 | if (!State.isAtFixpoint()) { |
| 5576 | State.indicatePessimisticFixpoint(); |
| 5577 | |
| 5578 | NumAttributesTimedOut++; |
| 5579 | } |
| 5580 | |
| 5581 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 5582 | ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(), |
| 5583 | QuerriedAAs.OptionalAAs.end()); |
| 5584 | ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(), |
| 5585 | QuerriedAAs.RequiredAAs.end()); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
| 5588 | LLVM_DEBUG({ |
| 5589 | if (!Visited.empty()) |
| 5590 | dbgs() << "\n[Attributor] Finalized " << Visited.size() |
| 5591 | << " abstract attributes.\n"; |
| 5592 | }); |
| 5593 | |
| 5594 | unsigned NumManifested = 0; |
| 5595 | unsigned NumAtFixpoint = 0; |
| 5596 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; |
| 5597 | for (AbstractAttribute *AA : AllAbstractAttributes) { |
| 5598 | AbstractState &State = AA->getState(); |
| 5599 | |
| 5600 | // If there is not already a fixpoint reached, we can now take the |
| 5601 | // optimistic state. This is correct because we enforced a pessimistic one |
| 5602 | // on abstract attributes that were transitively dependent on a changed one |
| 5603 | // already above. |
| 5604 | if (!State.isAtFixpoint()) |
| 5605 | State.indicateOptimisticFixpoint(); |
| 5606 | |
| 5607 | // If the state is invalid, we do not try to manifest it. |
| 5608 | if (!State.isValidState()) |
| 5609 | continue; |
| 5610 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 5611 | // Skip dead code. |
| 5612 | if (isAssumedDead(*AA, nullptr)) |
| 5613 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5614 | // Manifest the state and record if we changed the IR. |
| 5615 | ChangeStatus LocalChange = AA->manifest(*this); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 5616 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) |
| 5617 | AA->trackStatistics(); |
| 5618 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5619 | ManifestChange = ManifestChange | LocalChange; |
| 5620 | |
| 5621 | NumAtFixpoint++; |
| 5622 | NumManifested += (LocalChange == ChangeStatus::CHANGED); |
| 5623 | } |
| 5624 | |
| 5625 | (void)NumManifested; |
| 5626 | (void)NumAtFixpoint; |
| 5627 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested |
| 5628 | << " arguments while " << NumAtFixpoint |
| 5629 | << " were in a valid fixpoint state\n"); |
| 5630 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5631 | NumAttributesManifested += NumManifested; |
| 5632 | NumAttributesValidFixpoint += NumAtFixpoint; |
| 5633 | |
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 5634 | (void)NumFinalAAs; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5635 | assert( |
| 5636 | NumFinalAAs == AllAbstractAttributes.size() && |
| 5637 | "Expected the final number of abstract attributes to remain unchanged!"); |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 5638 | |
| 5639 | // 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] | 5640 | { |
| 5641 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " |
| 5642 | << ToBeDeletedFunctions.size() << " functions and " |
| 5643 | << ToBeDeletedBlocks.size() << " blocks and " |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5644 | << ToBeDeletedInsts.size() << " instructions and " |
| 5645 | << ToBeChangedUses.size() << " uses\n"); |
| 5646 | |
| 5647 | SmallVector<Instruction *, 32> DeadInsts; |
| 5648 | SmallVector<Instruction *, 32> TerminatorsToFold; |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5649 | |
| 5650 | for (auto &It : ToBeChangedUses) { |
| 5651 | Use *U = It.first; |
| 5652 | Value *NewV = It.second; |
| 5653 | Value *OldV = U->get(); |
| 5654 | LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() |
| 5655 | << " instead of " << *OldV << "\n"); |
| 5656 | U->set(NewV); |
| 5657 | if (Instruction *I = dyn_cast<Instruction>(OldV)) |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 5658 | if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && |
| 5659 | isInstructionTriviallyDead(I)) { |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5660 | DeadInsts.push_back(I); |
| 5661 | } |
| 5662 | if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { |
| 5663 | Instruction *UserI = cast<Instruction>(U->getUser()); |
| 5664 | if (isa<UndefValue>(NewV)) { |
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 5665 | ToBeChangedToUnreachableInsts.insert(UserI); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5666 | } else { |
| 5667 | TerminatorsToFold.push_back(UserI); |
| 5668 | } |
| 5669 | } |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5670 | } |
| Hideto Ueno | cb5eb13 | 2019-12-27 02:39:37 +0900 | [diff] [blame] | 5671 | for (Instruction *I : ToBeChangedToUnreachableInsts) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5672 | changeToUnreachable(I, /* UseLLVMTrap */ false); |
| 5673 | for (Instruction *I : TerminatorsToFold) |
| 5674 | ConstantFoldTerminator(I->getParent()); |
| 5675 | |
| 5676 | for (Instruction *I : ToBeDeletedInsts) { |
| 5677 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 5678 | if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) |
| 5679 | DeadInsts.push_back(I); |
| 5680 | else |
| 5681 | I->eraseFromParent(); |
| 5682 | } |
| 5683 | |
| 5684 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 5685 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5686 | if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { |
| 5687 | SmallVector<BasicBlock *, 8> ToBeDeletedBBs; |
| 5688 | ToBeDeletedBBs.reserve(NumDeadBlocks); |
| 5689 | ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end()); |
| Johannes Doerfert | 5e442a5 | 2019-10-30 17:34:59 -0500 | [diff] [blame] | 5690 | // Actually we do not delete the blocks but squash them into a single |
| 5691 | // unreachable but untangling branches that jump here is something we need |
| 5692 | // to do in a more generic way. |
| 5693 | DetatchDeadBlocks(ToBeDeletedBBs, nullptr); |
| 5694 | STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); |
| 5695 | BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5696 | } |
| 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 | // Identify dead internal functions and delete them. This happens outside |
| 5699 | // the other fixpoint analysis as we might treat potentially dead functions |
| 5700 | // as live to lower the number of iterations. If they happen to be dead, the |
| 5701 | // below fixpoint loop will identify and eliminate them. |
| 5702 | SmallVector<Function *, 8> InternalFns; |
| 5703 | for (Function &F : M) |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 5704 | if (F.hasLocalLinkage()) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5705 | InternalFns.push_back(&F); |
| 5706 | |
| 5707 | bool FoundDeadFn = true; |
| 5708 | while (FoundDeadFn) { |
| 5709 | FoundDeadFn = false; |
| 5710 | for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { |
| 5711 | Function *F = InternalFns[u]; |
| 5712 | if (!F) |
| 5713 | continue; |
| 5714 | |
| Johannes Doerfert | 6b9ee2d | 2020-01-02 16:53:37 -0600 | [diff] [blame^] | 5715 | if (!checkForAllCallSites( |
| 5716 | [this](AbstractCallSite ACS) { |
| 5717 | return ToBeDeletedFunctions.count( |
| 5718 | ACS.getInstruction()->getFunction()); |
| 5719 | }, |
| 5720 | *F, true, nullptr)) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5721 | continue; |
| 5722 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5723 | ToBeDeletedFunctions.insert(F); |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5724 | InternalFns[u] = nullptr; |
| 5725 | FoundDeadFn = true; |
| 5726 | } |
| 5727 | } |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5730 | // Rewrite the functions as requested during manifest. |
| 5731 | ManifestChange = ManifestChange | rewriteFunctionSignatures(); |
| 5732 | |
| Johannes Doerfert | 6b9ee2d | 2020-01-02 16:53:37 -0600 | [diff] [blame^] | 5733 | STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); |
| 5734 | BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size(); |
| 5735 | for (Function *Fn : ToBeDeletedFunctions) { |
| 5736 | Fn->deleteBody(); |
| 5737 | Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); |
| 5738 | Fn->eraseFromParent(); |
| 5739 | } |
| 5740 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5741 | if (VerifyMaxFixpointIterations && |
| 5742 | IterationCounter != MaxFixpointIterations) { |
| 5743 | errs() << "\n[Attributor] Fixpoint iteration done after: " |
| 5744 | << IterationCounter << "/" << MaxFixpointIterations |
| 5745 | << " iterations\n"; |
| 5746 | llvm_unreachable("The fixpoint was not reached with exactly the number of " |
| 5747 | "specified iterations!"); |
| 5748 | } |
| 5749 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5750 | return ManifestChange; |
| 5751 | } |
| 5752 | |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5753 | bool Attributor::registerFunctionSignatureRewrite( |
| 5754 | Argument &Arg, ArrayRef<Type *> ReplacementTypes, |
| 5755 | ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, |
| 5756 | ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { |
| 5757 | |
| 5758 | auto CallSiteCanBeChanged = [](AbstractCallSite ACS) { |
| 5759 | // Forbid must-tail calls for now. |
| 5760 | return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall(); |
| 5761 | }; |
| 5762 | |
| 5763 | Function *Fn = Arg.getParent(); |
| 5764 | // Avoid var-arg functions for now. |
| 5765 | if (Fn->isVarArg()) { |
| 5766 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); |
| 5767 | return false; |
| 5768 | } |
| 5769 | |
| 5770 | // Avoid functions with complicated argument passing semantics. |
| 5771 | AttributeList FnAttributeList = Fn->getAttributes(); |
| 5772 | if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || |
| 5773 | FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || |
| 5774 | FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) { |
| 5775 | LLVM_DEBUG( |
| 5776 | dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); |
| 5777 | return false; |
| 5778 | } |
| 5779 | |
| 5780 | // Avoid callbacks for now. |
| 5781 | if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr)) { |
| 5782 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); |
| 5783 | return false; |
| 5784 | } |
| 5785 | |
| 5786 | auto InstPred = [](Instruction &I) { |
| 5787 | if (auto *CI = dyn_cast<CallInst>(&I)) |
| 5788 | return !CI->isMustTailCall(); |
| 5789 | return true; |
| 5790 | }; |
| 5791 | |
| 5792 | // Forbid must-tail calls for now. |
| 5793 | // TODO: |
| 5794 | bool AnyDead; |
| 5795 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); |
| 5796 | if (!checkForAllInstructionsImpl(OpcodeInstMap, InstPred, nullptr, AnyDead, |
| 5797 | {Instruction::Call})) { |
| 5798 | LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); |
| 5799 | return false; |
| 5800 | } |
| 5801 | |
| 5802 | SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn]; |
| 5803 | if (ARIs.size() == 0) |
| 5804 | ARIs.resize(Fn->arg_size()); |
| 5805 | |
| 5806 | // If we have a replacement already with less than or equal new arguments, |
| 5807 | // ignore this request. |
| 5808 | ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()]; |
| 5809 | if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { |
| 5810 | LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); |
| 5811 | return false; |
| 5812 | } |
| 5813 | |
| 5814 | // If we have a replacement already but we like the new one better, delete |
| 5815 | // the old. |
| 5816 | if (ARI) |
| 5817 | delete ARI; |
| 5818 | |
| 5819 | // Remember the replacement. |
| 5820 | ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, |
| 5821 | std::move(CalleeRepairCB), |
| 5822 | std::move(ACSRepairCB)); |
| 5823 | |
| 5824 | return true; |
| 5825 | } |
| 5826 | |
| 5827 | ChangeStatus Attributor::rewriteFunctionSignatures() { |
| 5828 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 5829 | |
| 5830 | for (auto &It : ArgumentReplacementMap) { |
| 5831 | Function *OldFn = It.getFirst(); |
| 5832 | |
| 5833 | // Deleted functions do not require rewrites. |
| 5834 | if (ToBeDeletedFunctions.count(OldFn)) |
| 5835 | continue; |
| 5836 | |
| 5837 | const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond(); |
| 5838 | assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); |
| 5839 | |
| 5840 | SmallVector<Type *, 16> NewArgumentTypes; |
| 5841 | SmallVector<AttributeSet, 16> NewArgumentAttributes; |
| 5842 | |
| 5843 | // Collect replacement argument types and copy over existing attributes. |
| 5844 | AttributeList OldFnAttributeList = OldFn->getAttributes(); |
| 5845 | for (Argument &Arg : OldFn->args()) { |
| 5846 | if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) { |
| 5847 | NewArgumentTypes.append(ARI->ReplacementTypes.begin(), |
| 5848 | ARI->ReplacementTypes.end()); |
| 5849 | NewArgumentAttributes.append(ARI->getNumReplacementArgs(), |
| 5850 | AttributeSet()); |
| 5851 | } else { |
| 5852 | NewArgumentTypes.push_back(Arg.getType()); |
| 5853 | NewArgumentAttributes.push_back( |
| 5854 | OldFnAttributeList.getParamAttributes(Arg.getArgNo())); |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | FunctionType *OldFnTy = OldFn->getFunctionType(); |
| 5859 | Type *RetTy = OldFnTy->getReturnType(); |
| 5860 | |
| 5861 | // Construct the new function type using the new arguments types. |
| 5862 | FunctionType *NewFnTy = |
| 5863 | FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); |
| 5864 | |
| 5865 | LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() |
| 5866 | << "' from " << *OldFn->getFunctionType() << " to " |
| 5867 | << *NewFnTy << "\n"); |
| 5868 | |
| 5869 | // Create the new function body and insert it into the module. |
| 5870 | Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), |
| 5871 | OldFn->getAddressSpace(), ""); |
| 5872 | OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); |
| 5873 | NewFn->takeName(OldFn); |
| 5874 | NewFn->copyAttributesFrom(OldFn); |
| 5875 | |
| 5876 | // Patch the pointer to LLVM function in debug info descriptor. |
| 5877 | NewFn->setSubprogram(OldFn->getSubprogram()); |
| 5878 | OldFn->setSubprogram(nullptr); |
| 5879 | |
| 5880 | // Recompute the parameter attributes list based on the new arguments for |
| 5881 | // the function. |
| 5882 | LLVMContext &Ctx = OldFn->getContext(); |
| 5883 | NewFn->setAttributes(AttributeList::get( |
| 5884 | Ctx, OldFnAttributeList.getFnAttributes(), |
| 5885 | OldFnAttributeList.getRetAttributes(), NewArgumentAttributes)); |
| 5886 | |
| 5887 | // Since we have now created the new function, splice the body of the old |
| 5888 | // function right into the new function, leaving the old rotting hulk of the |
| 5889 | // function empty. |
| 5890 | NewFn->getBasicBlockList().splice(NewFn->begin(), |
| 5891 | OldFn->getBasicBlockList()); |
| 5892 | |
| 5893 | // Set of all "call-like" instructions that invoke the old function. |
| 5894 | SmallPtrSet<Instruction *, 8> OldCallSites; |
| 5895 | |
| 5896 | // Callback to create a new "call-like" instruction for a given one. |
| 5897 | auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { |
| 5898 | CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); |
| 5899 | const AttributeList &OldCallAttributeList = OldCB->getAttributes(); |
| 5900 | |
| 5901 | // Collect the new argument operands for the replacement call site. |
| 5902 | SmallVector<Value *, 16> NewArgOperands; |
| 5903 | SmallVector<AttributeSet, 16> NewArgOperandAttributes; |
| 5904 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { |
| 5905 | unsigned NewFirstArgNum = NewArgOperands.size(); |
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 5906 | (void)NewFirstArgNum; // only used inside assert. |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5907 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { |
| 5908 | if (ARI->ACSRepairCB) |
| 5909 | ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); |
| 5910 | assert(ARI->getNumReplacementArgs() + NewFirstArgNum == |
| 5911 | NewArgOperands.size() && |
| 5912 | "ACS repair callback did not provide as many operand as new " |
| 5913 | "types were registered!"); |
| 5914 | // TODO: Exose the attribute set to the ACS repair callback |
| 5915 | NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), |
| 5916 | AttributeSet()); |
| 5917 | } else { |
| 5918 | NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); |
| 5919 | NewArgOperandAttributes.push_back( |
| 5920 | OldCallAttributeList.getParamAttributes(OldArgNum)); |
| 5921 | } |
| 5922 | } |
| 5923 | |
| 5924 | assert(NewArgOperands.size() == NewArgOperandAttributes.size() && |
| 5925 | "Mismatch # argument operands vs. # argument operand attributes!"); |
| 5926 | assert(NewArgOperands.size() == NewFn->arg_size() && |
| 5927 | "Mismatch # argument operands vs. # function arguments!"); |
| 5928 | |
| 5929 | SmallVector<OperandBundleDef, 4> OperandBundleDefs; |
| 5930 | OldCB->getOperandBundlesAsDefs(OperandBundleDefs); |
| 5931 | |
| 5932 | // Create a new call or invoke instruction to replace the old one. |
| 5933 | CallBase *NewCB; |
| 5934 | if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { |
| 5935 | NewCB = |
| 5936 | InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), |
| 5937 | NewArgOperands, OperandBundleDefs, "", OldCB); |
| 5938 | } else { |
| 5939 | auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, |
| 5940 | "", OldCB); |
| 5941 | NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); |
| 5942 | NewCB = NewCI; |
| 5943 | } |
| 5944 | |
| 5945 | // Copy over various properties and the new attributes. |
| 5946 | OldCB->replaceAllUsesWith(NewCB); |
| 5947 | uint64_t W; |
| 5948 | if (OldCB->extractProfTotalWeight(W)) |
| 5949 | NewCB->setProfWeight(W); |
| 5950 | NewCB->setCallingConv(OldCB->getCallingConv()); |
| 5951 | NewCB->setDebugLoc(OldCB->getDebugLoc()); |
| 5952 | NewCB->takeName(OldCB); |
| 5953 | NewCB->setAttributes(AttributeList::get( |
| 5954 | Ctx, OldCallAttributeList.getFnAttributes(), |
| 5955 | OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes)); |
| 5956 | |
| 5957 | bool Inserted = OldCallSites.insert(OldCB).second; |
| 5958 | assert(Inserted && "Call site was old twice!"); |
| 5959 | (void)Inserted; |
| 5960 | |
| 5961 | return true; |
| 5962 | }; |
| 5963 | |
| 5964 | // Use the CallSiteReplacementCreator to create replacement call sites. |
| 5965 | bool Success = |
| 5966 | checkForAllCallSites(CallSiteReplacementCreator, *OldFn, true, nullptr); |
| Ilya Biryukov | 4f82af8 | 2019-12-31 10:21:52 +0100 | [diff] [blame] | 5967 | (void)Success; |
| Johannes Doerfert | 7513363 | 2019-10-10 01:39:16 -0500 | [diff] [blame] | 5968 | assert(Success && "Assumed call site replacement to succeed!"); |
| 5969 | |
| 5970 | // Rewire the arguments. |
| 5971 | auto OldFnArgIt = OldFn->arg_begin(); |
| 5972 | auto NewFnArgIt = NewFn->arg_begin(); |
| 5973 | for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); |
| 5974 | ++OldArgNum, ++OldFnArgIt) { |
| 5975 | if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { |
| 5976 | if (ARI->CalleeRepairCB) |
| 5977 | ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); |
| 5978 | NewFnArgIt += ARI->ReplacementTypes.size(); |
| 5979 | } else { |
| 5980 | NewFnArgIt->takeName(&*OldFnArgIt); |
| 5981 | OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); |
| 5982 | ++NewFnArgIt; |
| 5983 | } |
| 5984 | } |
| 5985 | |
| 5986 | // Eliminate the instructions *after* we visited all of them. |
| 5987 | for (Instruction *OldCallSite : OldCallSites) |
| 5988 | OldCallSite->eraseFromParent(); |
| 5989 | |
| 5990 | assert(OldFn->getNumUses() == 0 && "Unexpected leftover uses!"); |
| 5991 | OldFn->eraseFromParent(); |
| 5992 | Changed = ChangeStatus::CHANGED; |
| 5993 | } |
| 5994 | |
| 5995 | return Changed; |
| 5996 | } |
| 5997 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5998 | void Attributor::initializeInformationCache(Function &F) { |
| 5999 | |
| 6000 | // Walk all instructions to find interesting instructions that might be |
| 6001 | // queried by abstract attributes during their initialization or update. |
| 6002 | // This has to happen before we create attributes. |
| 6003 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; |
| 6004 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; |
| 6005 | |
| 6006 | for (Instruction &I : instructions(&F)) { |
| 6007 | bool IsInterestingOpcode = false; |
| 6008 | |
| 6009 | // To allow easy access to all instructions in a function with a given |
| 6010 | // opcode we store them in the InfoCache. As not all opcodes are interesting |
| 6011 | // to concrete attributes we only cache the ones that are as identified in |
| 6012 | // the following switch. |
| 6013 | // Note: There are no concrete attributes now so this is initially empty. |
| 6014 | switch (I.getOpcode()) { |
| 6015 | default: |
| 6016 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && |
| 6017 | "New call site/base instruction type needs to be known int the " |
| 6018 | "Attributor."); |
| 6019 | break; |
| 6020 | case Instruction::Load: |
| 6021 | // The alignment of a pointer is interesting for loads. |
| 6022 | case Instruction::Store: |
| 6023 | // The alignment of a pointer is interesting for stores. |
| 6024 | case Instruction::Call: |
| 6025 | case Instruction::CallBr: |
| 6026 | case Instruction::Invoke: |
| 6027 | case Instruction::CleanupRet: |
| 6028 | case Instruction::CatchSwitch: |
| Johannes Doerfert | 5732f56 | 2019-12-24 19:25:08 -0600 | [diff] [blame] | 6029 | case Instruction::AtomicRMW: |
| 6030 | case Instruction::AtomicCmpXchg: |
| Hideto Ueno | ef4febd | 2019-12-29 17:34:08 +0900 | [diff] [blame] | 6031 | case Instruction::Br: |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6032 | case Instruction::Resume: |
| 6033 | case Instruction::Ret: |
| 6034 | IsInterestingOpcode = true; |
| 6035 | } |
| 6036 | if (IsInterestingOpcode) |
| 6037 | InstOpcodeMap[I.getOpcode()].push_back(&I); |
| 6038 | if (I.mayReadOrWriteMemory()) |
| 6039 | ReadOrWriteInsts.push_back(&I); |
| 6040 | } |
| 6041 | } |
| 6042 | |
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 6043 | void Attributor::recordDependence(const AbstractAttribute &FromAA, |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6044 | const AbstractAttribute &ToAA, |
| 6045 | DepClassTy DepClass) { |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 6046 | if (FromAA.getState().isAtFixpoint()) |
| 6047 | return; |
| 6048 | |
| Johannes Doerfert | 680f638 | 2019-11-02 02:48:05 -0500 | [diff] [blame] | 6049 | if (DepClass == DepClassTy::REQUIRED) |
| 6050 | QueryMap[&FromAA].RequiredAAs.insert( |
| 6051 | const_cast<AbstractAttribute *>(&ToAA)); |
| 6052 | else |
| 6053 | QueryMap[&FromAA].OptionalAAs.insert( |
| 6054 | const_cast<AbstractAttribute *>(&ToAA)); |
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 6055 | QueriedNonFixAA = true; |
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 6056 | } |
| 6057 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6058 | void Attributor::identifyDefaultAbstractAttributes(Function &F) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6059 | if (!VisitedFunctions.insert(&F).second) |
| 6060 | return; |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 6061 | if (F.isDeclaration()) |
| 6062 | return; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6063 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6064 | IRPosition FPos = IRPosition::function(F); |
| 6065 | |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 6066 | // Check for dead BasicBlocks in every function. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 6067 | // We need dead instruction detection because we do not want to deal with |
| 6068 | // broken IR in which SSA rules do not apply. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6069 | getOrCreateAAFor<AAIsDead>(FPos); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 6070 | |
| 6071 | // Every function might be "will-return". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6072 | getOrCreateAAFor<AAWillReturn>(FPos); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 6073 | |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 6074 | // Every function might contain instructions that cause "undefined behavior". |
| 6075 | getOrCreateAAFor<AAUndefinedBehavior>(FPos); |
| 6076 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 6077 | // Every function can be nounwind. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6078 | getOrCreateAAFor<AANoUnwind>(FPos); |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 6079 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 6080 | // Every function might be marked "nosync" |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6081 | getOrCreateAAFor<AANoSync>(FPos); |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 6082 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 6083 | // Every function might be "no-free". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6084 | getOrCreateAAFor<AANoFree>(FPos); |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 6085 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 6086 | // Every function might be "no-return". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6087 | getOrCreateAAFor<AANoReturn>(FPos); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 6088 | |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 6089 | // Every function might be "no-recurse". |
| 6090 | getOrCreateAAFor<AANoRecurse>(FPos); |
| 6091 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6092 | // Every function might be "readnone/readonly/writeonly/...". |
| 6093 | getOrCreateAAFor<AAMemoryBehavior>(FPos); |
| 6094 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6095 | // Every function might be applicable for Heap-To-Stack conversion. |
| 6096 | if (EnableHeapToStack) |
| 6097 | getOrCreateAAFor<AAHeapToStack>(FPos); |
| 6098 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 6099 | // Return attributes are only appropriate if the return type is non void. |
| 6100 | Type *ReturnType = F.getReturnType(); |
| 6101 | if (!ReturnType->isVoidTy()) { |
| 6102 | // Argument attribute "returned" --- Create only one per function even |
| 6103 | // though it is an argument attribute. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6104 | getOrCreateAAFor<AAReturnedValues>(FPos); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6105 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6106 | IRPosition RetPos = IRPosition::returned(F); |
| 6107 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6108 | // Every returned value might be dead. |
| 6109 | getOrCreateAAFor<AAIsDead>(RetPos); |
| 6110 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6111 | // Every function might be simplified. |
| 6112 | getOrCreateAAFor<AAValueSimplify>(RetPos); |
| 6113 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6114 | if (ReturnType->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6115 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6116 | // Every function with pointer return type might be marked align. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6117 | getOrCreateAAFor<AAAlign>(RetPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6118 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6119 | // Every function with pointer return type might be marked nonnull. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6120 | getOrCreateAAFor<AANonNull>(RetPos); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6121 | |
| 6122 | // Every function with pointer return type might be marked noalias. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6123 | getOrCreateAAFor<AANoAlias>(RetPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6124 | |
| 6125 | // Every function with pointer return type might be marked |
| 6126 | // dereferenceable. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6127 | getOrCreateAAFor<AADereferenceable>(RetPos); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 6128 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6131 | for (Argument &Arg : F.args()) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6132 | IRPosition ArgPos = IRPosition::argument(Arg); |
| 6133 | |
| 6134 | // Every argument might be simplified. |
| 6135 | getOrCreateAAFor<AAValueSimplify>(ArgPos); |
| 6136 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6137 | if (Arg.getType()->isPointerTy()) { |
| 6138 | // Every argument with pointer type might be marked nonnull. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6139 | getOrCreateAAFor<AANonNull>(ArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6140 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6141 | // Every argument with pointer type might be marked noalias. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6142 | getOrCreateAAFor<AANoAlias>(ArgPos); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6143 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6144 | // Every argument with pointer type might be marked dereferenceable. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6145 | getOrCreateAAFor<AADereferenceable>(ArgPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6146 | |
| 6147 | // Every argument with pointer type might be marked align. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6148 | getOrCreateAAFor<AAAlign>(ArgPos); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 6149 | |
| 6150 | // Every argument with pointer type might be marked nocapture. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6151 | getOrCreateAAFor<AANoCapture>(ArgPos); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6152 | |
| 6153 | // Every argument with pointer type might be marked |
| 6154 | // "readnone/readonly/writeonly/..." |
| 6155 | getOrCreateAAFor<AAMemoryBehavior>(ArgPos); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6156 | |
| 6157 | // Every argument with pointer type might be marked nofree. |
| 6158 | getOrCreateAAFor<AANoFree>(ArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6159 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 6160 | } |
| 6161 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6162 | auto CallSitePred = [&](Instruction &I) -> bool { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6163 | CallSite CS(&I); |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6164 | if (Function *Callee = CS.getCalledFunction()) { |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 6165 | // Skip declerations except if annotations on their call sites were |
| 6166 | // explicitly requested. |
| Johannes Doerfert | 139c9ef | 2019-12-13 23:41:02 -0600 | [diff] [blame] | 6167 | if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && |
| 6168 | !Callee->hasMetadata(LLVMContext::MD_callback)) |
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 6169 | return true; |
| 6170 | |
| 6171 | if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6172 | IRPosition CSRetPos = IRPosition::callsite_returned(CS); |
| 6173 | |
| 6174 | // Call site return values might be dead. |
| 6175 | getOrCreateAAFor<AAIsDead>(CSRetPos); |
| 6176 | } |
| 6177 | |
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 6178 | for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6179 | |
| 6180 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); |
| 6181 | |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6182 | // Every call site argument might be dead. |
| 6183 | getOrCreateAAFor<AAIsDead>(CSArgPos); |
| 6184 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6185 | // Call site argument might be simplified. |
| 6186 | getOrCreateAAFor<AAValueSimplify>(CSArgPos); |
| 6187 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6188 | if (!CS.getArgument(i)->getType()->isPointerTy()) |
| 6189 | continue; |
| 6190 | |
| 6191 | // Call site argument attribute "non-null". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6192 | getOrCreateAAFor<AANonNull>(CSArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6193 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6194 | // Call site argument attribute "no-alias". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6195 | getOrCreateAAFor<AANoAlias>(CSArgPos); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 6196 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 6197 | // Call site argument attribute "dereferenceable". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6198 | getOrCreateAAFor<AADereferenceable>(CSArgPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 6199 | |
| 6200 | // Call site argument attribute "align". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 6201 | getOrCreateAAFor<AAAlign>(CSArgPos); |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6202 | |
| Johannes Doerfert | 2888019 | 2019-12-31 00:57:00 -0600 | [diff] [blame] | 6203 | // Call site argument attribute |
| 6204 | // "readnone/readonly/writeonly/..." |
| 6205 | getOrCreateAAFor<AAMemoryBehavior>(CSArgPos); |
| 6206 | |
| Hideto Ueno | 4ecf255 | 2019-12-12 13:42:40 +0000 | [diff] [blame] | 6207 | // Call site argument attribute "nofree". |
| 6208 | getOrCreateAAFor<AANoFree>(CSArgPos); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 6209 | } |
| 6210 | } |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6211 | return true; |
| 6212 | }; |
| 6213 | |
| 6214 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); |
| 6215 | bool Success, AnyDead = false; |
| 6216 | Success = checkForAllInstructionsImpl( |
| 6217 | OpcodeInstMap, CallSitePred, nullptr, AnyDead, |
| 6218 | {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 6219 | (unsigned)Instruction::Call}); |
| 6220 | (void)Success; |
| 6221 | assert(Success && !AnyDead && "Expected the check call to be successful!"); |
| 6222 | |
| 6223 | auto LoadStorePred = [&](Instruction &I) -> bool { |
| 6224 | if (isa<LoadInst>(I)) |
| 6225 | getOrCreateAAFor<AAAlign>( |
| 6226 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); |
| 6227 | else |
| 6228 | getOrCreateAAFor<AAAlign>( |
| 6229 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); |
| 6230 | return true; |
| 6231 | }; |
| 6232 | Success = checkForAllInstructionsImpl( |
| 6233 | OpcodeInstMap, LoadStorePred, nullptr, AnyDead, |
| 6234 | {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); |
| 6235 | (void)Success; |
| 6236 | assert(Success && !AnyDead && "Expected the check call to be successful!"); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | /// Helpers to ease debugging through output streams and print calls. |
| 6240 | /// |
| 6241 | ///{ |
| 6242 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { |
| 6243 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); |
| 6244 | } |
| 6245 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6246 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6247 | switch (AP) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6248 | case IRPosition::IRP_INVALID: |
| 6249 | return OS << "inv"; |
| 6250 | case IRPosition::IRP_FLOAT: |
| 6251 | return OS << "flt"; |
| 6252 | case IRPosition::IRP_RETURNED: |
| 6253 | return OS << "fn_ret"; |
| 6254 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 6255 | return OS << "cs_ret"; |
| 6256 | case IRPosition::IRP_FUNCTION: |
| 6257 | return OS << "fn"; |
| 6258 | case IRPosition::IRP_CALL_SITE: |
| 6259 | return OS << "cs"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6260 | case IRPosition::IRP_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6261 | return OS << "arg"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6262 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6263 | return OS << "cs_arg"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6264 | } |
| 6265 | llvm_unreachable("Unknown attribute position!"); |
| 6266 | } |
| 6267 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6268 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 6269 | const Value &AV = Pos.getAssociatedValue(); |
| 6270 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6271 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; |
| 6272 | } |
| 6273 | |
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 6274 | template <typename base_ty, base_ty BestState, base_ty WorstState> |
| Hideto Ueno | 5fc02dc | 2020-01-03 11:03:56 +0900 | [diff] [blame] | 6275 | raw_ostream &llvm:: |
| 6276 | operator<<(raw_ostream &OS, |
| 6277 | const IntegerStateBase<base_ty, BestState, WorstState> &S) { |
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 6278 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" |
| 6279 | << static_cast<const AbstractState &>(S); |
| 6280 | } |
| 6281 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6282 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { |
| 6283 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); |
| 6284 | } |
| 6285 | |
| 6286 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { |
| 6287 | AA.print(OS); |
| 6288 | return OS; |
| 6289 | } |
| 6290 | |
| 6291 | void AbstractAttribute::print(raw_ostream &OS) const { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 6292 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() |
| 6293 | << "]"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6294 | } |
| 6295 | ///} |
| 6296 | |
| 6297 | /// ---------------------------------------------------------------------------- |
| 6298 | /// Pass (Manager) Boilerplate |
| 6299 | /// ---------------------------------------------------------------------------- |
| 6300 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6301 | static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6302 | if (DisableAttributor) |
| 6303 | return false; |
| 6304 | |
| 6305 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() |
| 6306 | << " functions.\n"); |
| 6307 | |
| 6308 | // Create an Attributor and initially empty information cache that is filled |
| 6309 | // while we identify default attribute opportunities. |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 6310 | InformationCache InfoCache(M, AG); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 6311 | Attributor A(InfoCache, DepRecInterval); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6312 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 6313 | for (Function &F : M) |
| 6314 | A.initializeInformationCache(F); |
| 6315 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6316 | for (Function &F : M) { |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 6317 | if (F.hasExactDefinition()) |
| 6318 | NumFnWithExactDefinition++; |
| 6319 | else |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6320 | NumFnWithoutExactDefinition++; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6321 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6322 | // We look at internal functions only on-demand but if any use is not a |
| 6323 | // direct call, we have to do it eagerly. |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 6324 | if (F.hasLocalLinkage()) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6325 | if (llvm::all_of(F.uses(), [](const Use &U) { |
| 6326 | return ImmutableCallSite(U.getUser()) && |
| 6327 | ImmutableCallSite(U.getUser()).isCallee(&U); |
| 6328 | })) |
| 6329 | continue; |
| 6330 | } |
| 6331 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6332 | // Populate the Attributor with abstract attribute opportunities in the |
| 6333 | // function and the information cache with IR information. |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6334 | A.identifyDefaultAbstractAttributes(F); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 6337 | return A.run(M) == ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6338 | } |
| 6339 | |
| 6340 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 6341 | AnalysisGetter AG(AM); |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6342 | if (runAttributorOnModule(M, AG)) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6343 | // FIXME: Think about passes we will preserve and add them here. |
| 6344 | return PreservedAnalyses::none(); |
| 6345 | } |
| 6346 | return PreservedAnalyses::all(); |
| 6347 | } |
| 6348 | |
| 6349 | namespace { |
| 6350 | |
| 6351 | struct AttributorLegacyPass : public ModulePass { |
| 6352 | static char ID; |
| 6353 | |
| 6354 | AttributorLegacyPass() : ModulePass(ID) { |
| 6355 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 6356 | } |
| 6357 | |
| 6358 | bool runOnModule(Module &M) override { |
| 6359 | if (skipModule(M)) |
| 6360 | return false; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6361 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 6362 | AnalysisGetter AG; |
| 6363 | return runAttributorOnModule(M, AG); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
| 6366 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 6367 | // FIXME: Think about passes we will preserve and add them here. |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6368 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6369 | } |
| 6370 | }; |
| 6371 | |
| 6372 | } // end anonymous namespace |
| 6373 | |
| 6374 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } |
| 6375 | |
| 6376 | char AttributorLegacyPass::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6377 | |
| 6378 | const char AAReturnedValues::ID = 0; |
| 6379 | const char AANoUnwind::ID = 0; |
| 6380 | const char AANoSync::ID = 0; |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 6381 | const char AANoFree::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6382 | const char AANonNull::ID = 0; |
| 6383 | const char AANoRecurse::ID = 0; |
| 6384 | const char AAWillReturn::ID = 0; |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 6385 | const char AAUndefinedBehavior::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6386 | const char AANoAlias::ID = 0; |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 6387 | const char AAReachability::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6388 | const char AANoReturn::ID = 0; |
| 6389 | const char AAIsDead::ID = 0; |
| 6390 | const char AADereferenceable::ID = 0; |
| 6391 | const char AAAlign::ID = 0; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 6392 | const char AANoCapture::ID = 0; |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6393 | const char AAValueSimplify::ID = 0; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6394 | const char AAHeapToStack::ID = 0; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6395 | const char AAMemoryBehavior::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 6396 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6397 | // Macro magic to create the static generator function for attributes that |
| 6398 | // follow the naming scheme. |
| 6399 | |
| 6400 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ |
| 6401 | case IRPosition::PK: \ |
| 6402 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); |
| 6403 | |
| 6404 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ |
| 6405 | case IRPosition::PK: \ |
| 6406 | AA = new CLASS##SUFFIX(IRP); \ |
| 6407 | break; |
| 6408 | |
| 6409 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 6410 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 6411 | CLASS *AA = nullptr; \ |
| 6412 | switch (IRP.getPositionKind()) { \ |
| 6413 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 6414 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 6415 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 6416 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 6417 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 6418 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 6419 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 6420 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 6421 | } \ |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6422 | return *AA; \ |
| 6423 | } |
| 6424 | |
| 6425 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 6426 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 6427 | CLASS *AA = nullptr; \ |
| 6428 | switch (IRP.getPositionKind()) { \ |
| 6429 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 6430 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ |
| 6431 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 6432 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 6433 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 6434 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 6435 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 6436 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 6437 | } \ |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6438 | return *AA; \ |
| 6439 | } |
| 6440 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6441 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 6442 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 6443 | CLASS *AA = nullptr; \ |
| 6444 | switch (IRP.getPositionKind()) { \ |
| 6445 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 6446 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 6447 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 6448 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 6449 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 6450 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 6451 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 6452 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 6453 | } \ |
| 6454 | return *AA; \ |
| 6455 | } |
| 6456 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6457 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 6458 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 6459 | CLASS *AA = nullptr; \ |
| 6460 | switch (IRP.getPositionKind()) { \ |
| 6461 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 6462 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 6463 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 6464 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 6465 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 6466 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 6467 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 6468 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 6469 | } \ |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6470 | return *AA; \ |
| 6471 | } |
| 6472 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6473 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 6474 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 6475 | CLASS *AA = nullptr; \ |
| 6476 | switch (IRP.getPositionKind()) { \ |
| 6477 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 6478 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 6479 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 6480 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 6481 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 6482 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 6483 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 6484 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 6485 | } \ |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6486 | return *AA; \ |
| 6487 | } |
| 6488 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6489 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) |
| 6490 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6491 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) |
| 6492 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) |
| 6493 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6494 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) |
| 6495 | |
| 6496 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) |
| 6497 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) |
| 6498 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) |
| 6499 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 6500 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6501 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6502 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) |
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 6503 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) |
| Stefan Stipanovic | f35740d | 2019-11-02 16:35:38 +0100 | [diff] [blame] | 6504 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6505 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6506 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) |
| Pankaj Gode | 04945c9 | 2019-11-22 18:40:47 +0530 | [diff] [blame] | 6507 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) |
| Johannes Doerfert | 58f324a | 2019-12-24 18:48:50 -0600 | [diff] [blame] | 6508 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6509 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 6510 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) |
| 6511 | |
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 6512 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6513 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 6514 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6515 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 6516 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 6517 | #undef SWITCH_PK_CREATE |
| 6518 | #undef SWITCH_PK_INV |
| 6519 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6520 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", |
| 6521 | "Deduce and propagate attributes", false, false) |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 6522 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 6523 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", |
| 6524 | "Deduce and propagate attributes", false, false) |