| 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" | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" | 
|  | 35 | #include "llvm/Support/Debug.h" | 
|  | 36 | #include "llvm/Support/raw_ostream.h" | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | 
|  | 38 | #include "llvm/Transforms/Utils/Local.h" | 
|  | 39 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 40 | #include <cassert> | 
|  | 41 |  | 
|  | 42 | using namespace llvm; | 
|  | 43 |  | 
|  | 44 | #define DEBUG_TYPE "attributor" | 
|  | 45 |  | 
|  | 46 | STATISTIC(NumFnWithExactDefinition, | 
|  | 47 | "Number of function with exact definitions"); | 
|  | 48 | STATISTIC(NumFnWithoutExactDefinition, | 
|  | 49 | "Number of function without exact definitions"); | 
|  | 50 | STATISTIC(NumAttributesTimedOut, | 
|  | 51 | "Number of abstract attributes timed out before fixpoint"); | 
|  | 52 | STATISTIC(NumAttributesValidFixpoint, | 
|  | 53 | "Number of abstract attributes in a valid fixpoint state"); | 
|  | 54 | STATISTIC(NumAttributesManifested, | 
|  | 55 | "Number of abstract attributes manifested in IR"); | 
|  | 56 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 57 | // Some helper macros to deal with statistics tracking. | 
|  | 58 | // | 
|  | 59 | // Usage: | 
|  | 60 | // For simple IR attribute tracking overload trackStatistics in the abstract | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 61 | // attribute and choose the right STATS_DECLTRACK_********* macro, | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 62 | // e.g.,: | 
|  | 63 | //  void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 64 | //    STATS_DECLTRACK_ARG_ATTR(returned) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 65 | //  } | 
|  | 66 | // If there is a single "increment" side one can use the macro | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 67 | // STATS_DECLTRACK with a custom message. If there are multiple increment | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 68 | // sides, STATS_DECL and STATS_TRACK can also be used separatly. | 
|  | 69 | // | 
|  | 70 | #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \ | 
|  | 71 | ("Number of " #TYPE " marked '" #NAME "'") | 
|  | 72 | #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME | 
| Johannes Doerfert | a7a3b3a | 2019-09-04 19:01:08 +0000 | [diff] [blame] | 73 | #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG); | 
|  | 74 | #define STATS_DECL(NAME, TYPE, MSG)                                            \ | 
|  | 75 | STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG); | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 76 | #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 77 | #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \ | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 78 | {                                                                            \ | 
|  | 79 | STATS_DECL(NAME, TYPE, MSG)                                                \ | 
|  | 80 | STATS_TRACK(NAME, TYPE)                                                    \ | 
|  | 81 | } | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 82 | #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \ | 
|  | 83 | STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) | 
|  | 84 | #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \ | 
|  | 85 | STATS_DECLTRACK(NAME, CSArguments,                                           \ | 
|  | 86 | BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) | 
|  | 87 | #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \ | 
|  | 88 | STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) | 
|  | 89 | #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \ | 
|  | 90 | STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) | 
|  | 91 | #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \ | 
|  | 92 | STATS_DECLTRACK(NAME, FunctionReturn,                                        \ | 
| Johannes Doerfert | 2db8528 | 2019-08-21 20:56:56 +0000 | [diff] [blame] | 93 | BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 94 | #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \ | 
|  | 95 | STATS_DECLTRACK(NAME, CSReturn,                                              \ | 
|  | 96 | BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) | 
|  | 97 | #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \ | 
|  | 98 | STATS_DECLTRACK(NAME, Floating,                                              \ | 
|  | 99 | ("Number of floating values known to be '" #NAME "'")) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 100 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 101 | // TODO: Determine a good default value. | 
|  | 102 | // | 
|  | 103 | // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads | 
|  | 104 | // (when run with the first 5 abstract attributes). The results also indicate | 
|  | 105 | // that we never reach 32 iterations but always find a fixpoint sooner. | 
|  | 106 | // | 
|  | 107 | // This will become more evolved once we perform two interleaved fixpoint | 
|  | 108 | // iterations: bottom-up and top-down. | 
|  | 109 | static cl::opt<unsigned> | 
|  | 110 | MaxFixpointIterations("attributor-max-iterations", cl::Hidden, | 
|  | 111 | cl::desc("Maximal number of fixpoint iterations."), | 
|  | 112 | cl::init(32)); | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 113 | static cl::opt<bool> VerifyMaxFixpointIterations( | 
|  | 114 | "attributor-max-iterations-verify", cl::Hidden, | 
|  | 115 | cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), | 
|  | 116 | cl::init(false)); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 117 |  | 
|  | 118 | static cl::opt<bool> DisableAttributor( | 
|  | 119 | "attributor-disable", cl::Hidden, | 
|  | 120 | cl::desc("Disable the attributor inter-procedural deduction pass."), | 
| Johannes Doerfert | 282d34e | 2019-06-14 14:53:41 +0000 | [diff] [blame] | 121 | cl::init(true)); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 122 |  | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 123 | static cl::opt<bool> AnnotateDeclarationCallSites( | 
|  | 124 | "attributor-annotate-decl-cs", cl::Hidden, | 
|  | 125 | cl::desc("Annoate call sites of function declarations."), cl::init(false)); | 
|  | 126 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 127 | static cl::opt<bool> ManifestInternal( | 
|  | 128 | "attributor-manifest-internal", cl::Hidden, | 
|  | 129 | cl::desc("Manifest Attributor internal string attributes."), | 
|  | 130 | cl::init(false)); | 
|  | 131 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 132 | static cl::opt<unsigned> DepRecInterval( | 
|  | 133 | "attributor-dependence-recompute-interval", cl::Hidden, | 
|  | 134 | cl::desc("Number of iterations until dependences are recomputed."), | 
|  | 135 | cl::init(4)); | 
|  | 136 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 137 | static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", | 
|  | 138 | cl::init(true), cl::Hidden); | 
|  | 139 |  | 
| Johannes Doerfert | 1c2afae | 2019-10-10 05:34:21 +0000 | [diff] [blame] | 140 | static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), | 
|  | 141 | cl::Hidden); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 142 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 143 | /// Logic operators for the change status enum class. | 
|  | 144 | /// | 
|  | 145 | ///{ | 
|  | 146 | ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { | 
|  | 147 | return l == ChangeStatus::CHANGED ? l : r; | 
|  | 148 | } | 
|  | 149 | ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { | 
|  | 150 | return l == ChangeStatus::UNCHANGED ? l : r; | 
|  | 151 | } | 
|  | 152 | ///} | 
|  | 153 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 154 | /// 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] | 155 | /// will be done by looking through cast instructions, selects, phis, and calls | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 156 | /// with the "returned" attribute. Once we cannot look through the value any | 
|  | 157 | /// further, the callback \p VisitValueCB is invoked and passed the current | 
|  | 158 | /// value, the \p State, and a flag to indicate if we stripped anything. To | 
|  | 159 | /// limit how much effort is invested, we will never visit more values than | 
|  | 160 | /// specified by \p MaxValues. | 
|  | 161 | template <typename AAType, typename StateTy> | 
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 162 | static bool genericValueTraversal( | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 163 | Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 164 | const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 165 | int MaxValues = 8) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 166 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 167 | const AAIsDead *LivenessAA = nullptr; | 
|  | 168 | if (IRP.getAnchorScope()) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 169 | LivenessAA = &A.getAAFor<AAIsDead>( | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 170 | QueryingAA, IRPosition::function(*IRP.getAnchorScope()), | 
|  | 171 | /* TrackDependence */ false); | 
|  | 172 | bool AnyDead = false; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 173 |  | 
|  | 174 | // TODO: Use Positions here to allow context sensitivity in VisitValueCB | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 175 | SmallPtrSet<Value *, 16> Visited; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 176 | SmallVector<Value *, 16> Worklist; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 177 | Worklist.push_back(&IRP.getAssociatedValue()); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 178 |  | 
|  | 179 | int Iteration = 0; | 
|  | 180 | do { | 
|  | 181 | Value *V = Worklist.pop_back_val(); | 
|  | 182 |  | 
|  | 183 | // Check if we should process the current value. To prevent endless | 
|  | 184 | // recursion keep a record of the values we followed! | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 185 | if (!Visited.insert(V).second) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 186 | continue; | 
|  | 187 |  | 
|  | 188 | // Make sure we limit the compile time for complex expressions. | 
|  | 189 | if (Iteration++ >= MaxValues) | 
|  | 190 | return false; | 
|  | 191 |  | 
|  | 192 | // Explicitly look through calls with a "returned" attribute if we do | 
|  | 193 | // not have a pointer as stripPointerCasts only works on them. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 194 | Value *NewV = nullptr; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 195 | if (V->getType()->isPointerTy()) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 196 | NewV = V->stripPointerCasts(); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 197 | } else { | 
|  | 198 | CallSite CS(V); | 
|  | 199 | if (CS && CS.getCalledFunction()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 200 | for (Argument &Arg : CS.getCalledFunction()->args()) | 
|  | 201 | if (Arg.hasReturnedAttr()) { | 
|  | 202 | NewV = CS.getArgOperand(Arg.getArgNo()); | 
|  | 203 | break; | 
|  | 204 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 205 | } | 
|  | 206 | } | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 207 | if (NewV && NewV != V) { | 
|  | 208 | Worklist.push_back(NewV); | 
|  | 209 | continue; | 
|  | 210 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 211 |  | 
|  | 212 | // Look through select instructions, visit both potential values. | 
|  | 213 | if (auto *SI = dyn_cast<SelectInst>(V)) { | 
|  | 214 | Worklist.push_back(SI->getTrueValue()); | 
|  | 215 | Worklist.push_back(SI->getFalseValue()); | 
|  | 216 | continue; | 
|  | 217 | } | 
|  | 218 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 219 | // Look through phi nodes, visit all live operands. | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 220 | if (auto *PHI = dyn_cast<PHINode>(V)) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 221 | assert(LivenessAA && | 
|  | 222 | "Expected liveness in the presence of instructions!"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 223 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { | 
|  | 224 | const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 225 | if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 226 | AnyDead = true; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 227 | continue; | 
|  | 228 | } | 
|  | 229 | Worklist.push_back(PHI->getIncomingValue(u)); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 230 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 231 | continue; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | // Once a leaf is reached we inform the user through the callback. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 235 | if (!VisitValueCB(*V, State, Iteration > 1)) | 
|  | 236 | return false; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 237 | } while (!Worklist.empty()); | 
|  | 238 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 239 | // If we actually used liveness information so we have to record a dependence. | 
|  | 240 | if (AnyDead) | 
|  | 241 | A.recordDependence(*LivenessAA, QueryingAA); | 
|  | 242 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 243 | // All values have been visited. | 
|  | 244 | return true; | 
|  | 245 | } | 
|  | 246 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 247 | /// Return true if \p New is equal or worse than \p Old. | 
|  | 248 | static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { | 
|  | 249 | if (!Old.isIntAttribute()) | 
|  | 250 | return true; | 
|  | 251 |  | 
|  | 252 | return Old.getValueAsInt() >= New.getValueAsInt(); | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | /// Return true if the information provided by \p Attr was added to the | 
|  | 256 | /// 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] | 257 | /// 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] | 258 | static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 259 | AttributeList &Attrs, int AttrIdx) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 260 |  | 
|  | 261 | if (Attr.isEnumAttribute()) { | 
|  | 262 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); | 
|  | 263 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 264 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 265 | return false; | 
|  | 266 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 267 | return true; | 
|  | 268 | } | 
|  | 269 | if (Attr.isStringAttribute()) { | 
|  | 270 | StringRef Kind = Attr.getKindAsString(); | 
|  | 271 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 272 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 273 | return false; | 
|  | 274 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 275 | return true; | 
|  | 276 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 277 | if (Attr.isIntAttribute()) { | 
|  | 278 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); | 
|  | 279 | if (Attrs.hasAttribute(AttrIdx, Kind)) | 
|  | 280 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) | 
|  | 281 | return false; | 
|  | 282 | Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); | 
|  | 283 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); | 
|  | 284 | return true; | 
|  | 285 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 286 |  | 
|  | 287 | llvm_unreachable("Expected enum or string attribute!"); | 
|  | 288 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 289 | static const Value *getPointerOperand(const Instruction *I) { | 
|  | 290 | if (auto *LI = dyn_cast<LoadInst>(I)) | 
|  | 291 | if (!LI->isVolatile()) | 
|  | 292 | return LI->getPointerOperand(); | 
|  | 293 |  | 
|  | 294 | if (auto *SI = dyn_cast<StoreInst>(I)) | 
|  | 295 | if (!SI->isVolatile()) | 
|  | 296 | return SI->getPointerOperand(); | 
|  | 297 |  | 
|  | 298 | if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) | 
|  | 299 | if (!CXI->isVolatile()) | 
|  | 300 | return CXI->getPointerOperand(); | 
|  | 301 |  | 
|  | 302 | if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) | 
|  | 303 | if (!RMWI->isVolatile()) | 
|  | 304 | return RMWI->getPointerOperand(); | 
|  | 305 |  | 
|  | 306 | return nullptr; | 
|  | 307 | } | 
|  | 308 | static const Value *getBasePointerOfAccessPointerOperand(const Instruction *I, | 
|  | 309 | int64_t &BytesOffset, | 
|  | 310 | const DataLayout &DL) { | 
|  | 311 | const Value *Ptr = getPointerOperand(I); | 
|  | 312 | if (!Ptr) | 
|  | 313 | return nullptr; | 
|  | 314 |  | 
|  | 315 | return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, | 
|  | 316 | /*AllowNonInbounds*/ false); | 
|  | 317 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 318 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 319 | ChangeStatus AbstractAttribute::update(Attributor &A) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 320 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
|  | 321 | if (getState().isAtFixpoint()) | 
|  | 322 | return HasChanged; | 
|  | 323 |  | 
|  | 324 | LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); | 
|  | 325 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 326 | HasChanged = updateImpl(A); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 327 |  | 
|  | 328 | LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this | 
|  | 329 | << "\n"); | 
|  | 330 |  | 
|  | 331 | return HasChanged; | 
|  | 332 | } | 
|  | 333 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 334 | ChangeStatus | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 335 | IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 336 | const ArrayRef<Attribute> &DeducedAttrs) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 337 | Function *ScopeFn = IRP.getAssociatedFunction(); | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 338 | IRPosition::Kind PK = IRP.getPositionKind(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 339 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 340 | // In the following some generic code that will manifest attributes in | 
|  | 341 | // DeducedAttrs if they improve the current IR. Due to the different | 
|  | 342 | // annotation positions we use the underlying AttributeList interface. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 343 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 344 | AttributeList Attrs; | 
|  | 345 | switch (PK) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 346 | case IRPosition::IRP_INVALID: | 
|  | 347 | case IRPosition::IRP_FLOAT: | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 348 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 349 | case IRPosition::IRP_ARGUMENT: | 
|  | 350 | case IRPosition::IRP_FUNCTION: | 
|  | 351 | case IRPosition::IRP_RETURNED: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 352 | Attrs = ScopeFn->getAttributes(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 353 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 354 | case IRPosition::IRP_CALL_SITE: | 
|  | 355 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 356 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 357 | Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 358 | break; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 361 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 362 | LLVMContext &Ctx = IRP.getAnchorValue().getContext(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 363 | for (const Attribute &Attr : DeducedAttrs) { | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 364 | if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 365 | continue; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 366 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 367 | HasChanged = ChangeStatus::CHANGED; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 368 | } | 
|  | 369 |  | 
|  | 370 | if (HasChanged == ChangeStatus::UNCHANGED) | 
|  | 371 | return HasChanged; | 
|  | 372 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 373 | switch (PK) { | 
|  | 374 | case IRPosition::IRP_ARGUMENT: | 
|  | 375 | case IRPosition::IRP_FUNCTION: | 
|  | 376 | case IRPosition::IRP_RETURNED: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 377 | ScopeFn->setAttributes(Attrs); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 378 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 379 | case IRPosition::IRP_CALL_SITE: | 
|  | 380 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 381 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 382 | CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); | 
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 383 | break; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 384 | case IRPosition::IRP_INVALID: | 
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 385 | case IRPosition::IRP_FLOAT: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 386 | break; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 387 | } | 
|  | 388 |  | 
|  | 389 | return HasChanged; | 
|  | 390 | } | 
|  | 391 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 392 | const IRPosition IRPosition::EmptyKey(255); | 
|  | 393 | const IRPosition IRPosition::TombstoneKey(256); | 
|  | 394 |  | 
|  | 395 | SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { | 
|  | 396 | IRPositions.emplace_back(IRP); | 
|  | 397 |  | 
|  | 398 | ImmutableCallSite ICS(&IRP.getAnchorValue()); | 
|  | 399 | switch (IRP.getPositionKind()) { | 
|  | 400 | case IRPosition::IRP_INVALID: | 
|  | 401 | case IRPosition::IRP_FLOAT: | 
|  | 402 | case IRPosition::IRP_FUNCTION: | 
|  | 403 | return; | 
|  | 404 | case IRPosition::IRP_ARGUMENT: | 
|  | 405 | case IRPosition::IRP_RETURNED: | 
|  | 406 | IRPositions.emplace_back( | 
|  | 407 | IRPosition::function(*IRP.getAssociatedFunction())); | 
|  | 408 | return; | 
|  | 409 | case IRPosition::IRP_CALL_SITE: | 
|  | 410 | assert(ICS && "Expected call site!"); | 
|  | 411 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 412 | //       in CallBase. | 
|  | 413 | if (!ICS.hasOperandBundles()) | 
|  | 414 | if (const Function *Callee = ICS.getCalledFunction()) | 
|  | 415 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 416 | return; | 
|  | 417 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
|  | 418 | assert(ICS && "Expected call site!"); | 
|  | 419 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 420 | //       in CallBase. | 
|  | 421 | if (!ICS.hasOperandBundles()) { | 
|  | 422 | if (const Function *Callee = ICS.getCalledFunction()) { | 
|  | 423 | IRPositions.emplace_back(IRPosition::returned(*Callee)); | 
|  | 424 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 425 | } | 
|  | 426 | } | 
|  | 427 | IRPositions.emplace_back( | 
|  | 428 | IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); | 
|  | 429 | return; | 
|  | 430 | case IRPosition::IRP_CALL_SITE_ARGUMENT: { | 
|  | 431 | int ArgNo = IRP.getArgNo(); | 
|  | 432 | assert(ICS && ArgNo >= 0 && "Expected call site!"); | 
|  | 433 | // TODO: We need to look at the operand bundles similar to the redirection | 
|  | 434 | //       in CallBase. | 
|  | 435 | if (!ICS.hasOperandBundles()) { | 
|  | 436 | const Function *Callee = ICS.getCalledFunction(); | 
|  | 437 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) | 
|  | 438 | IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); | 
|  | 439 | if (Callee) | 
|  | 440 | IRPositions.emplace_back(IRPosition::function(*Callee)); | 
|  | 441 | } | 
|  | 442 | IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); | 
|  | 443 | return; | 
|  | 444 | } | 
|  | 445 | } | 
|  | 446 | } | 
|  | 447 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 448 | bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, | 
|  | 449 | bool IgnoreSubsumingPositions) const { | 
|  | 450 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 451 | for (Attribute::AttrKind AK : AKs) | 
|  | 452 | if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) | 
|  | 453 | return true; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 454 | // The first position returned by the SubsumingPositionIterator is | 
|  | 455 | // always the position itself. If we ignore subsuming positions we | 
|  | 456 | // are done after the first iteration. | 
|  | 457 | if (IgnoreSubsumingPositions) | 
|  | 458 | break; | 
|  | 459 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 460 | return false; | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, | 
|  | 464 | SmallVectorImpl<Attribute> &Attrs) const { | 
|  | 465 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) | 
|  | 466 | for (Attribute::AttrKind AK : AKs) { | 
|  | 467 | const Attribute &Attr = EquivIRP.getAttr(AK); | 
|  | 468 | if (Attr.getKindAsEnum() == AK) | 
|  | 469 | Attrs.push_back(Attr); | 
|  | 470 | } | 
|  | 471 | } | 
|  | 472 |  | 
|  | 473 | void IRPosition::verify() { | 
|  | 474 | switch (KindOrArgNo) { | 
|  | 475 | default: | 
|  | 476 | assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); | 
|  | 477 | assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && | 
|  | 478 | "Expected call base or argument for positive attribute index!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 479 | if (isa<Argument>(AnchorVal)) { | 
|  | 480 | assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 481 | "Argument number mismatch!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 482 | assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && | 
|  | 483 | "Associated value mismatch!"); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 484 | } else { | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 485 | assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 486 | "Call site argument number mismatch!"); | 
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 487 | assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == | 
|  | 488 | &getAssociatedValue() && | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 489 | "Associated value mismatch!"); | 
|  | 490 | } | 
|  | 491 | break; | 
|  | 492 | case IRP_INVALID: | 
|  | 493 | assert(!AnchorVal && "Expected no value for an invalid position!"); | 
|  | 494 | break; | 
|  | 495 | case IRP_FLOAT: | 
|  | 496 | assert((!isa<CallBase>(&getAssociatedValue()) && | 
|  | 497 | !isa<Argument>(&getAssociatedValue())) && | 
|  | 498 | "Expected specialized kind for call base and argument values!"); | 
|  | 499 | break; | 
|  | 500 | case IRP_RETURNED: | 
|  | 501 | assert(isa<Function>(AnchorVal) && | 
|  | 502 | "Expected function for a 'returned' position!"); | 
|  | 503 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 504 | break; | 
|  | 505 | case IRP_CALL_SITE_RETURNED: | 
|  | 506 | assert((isa<CallBase>(AnchorVal)) && | 
|  | 507 | "Expected call base for 'call site returned' position!"); | 
|  | 508 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 509 | break; | 
|  | 510 | case IRP_CALL_SITE: | 
|  | 511 | assert((isa<CallBase>(AnchorVal)) && | 
|  | 512 | "Expected call base for 'call site function' position!"); | 
|  | 513 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 514 | break; | 
|  | 515 | case IRP_FUNCTION: | 
|  | 516 | assert(isa<Function>(AnchorVal) && | 
|  | 517 | "Expected function for a 'function' position!"); | 
|  | 518 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); | 
|  | 519 | break; | 
|  | 520 | } | 
|  | 521 | } | 
|  | 522 |  | 
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 523 | namespace { | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 524 | /// 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] | 525 | /// information in \p R and indicate/return if \p S did change (as-in update is | 
|  | 526 | /// required to be run again). | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 527 | template <typename StateType> | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 528 | ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 529 | auto Assumed = S.getAssumed(); | 
|  | 530 | S ^= R; | 
|  | 531 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | 
|  | 532 | : ChangeStatus::CHANGED; | 
|  | 533 | } | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 534 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 535 | /// Clamp the information known for all returned values of a function | 
|  | 536 | /// (identified by \p QueryingAA) into \p S. | 
|  | 537 | template <typename AAType, typename StateType = typename AAType::StateType> | 
|  | 538 | static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, | 
|  | 539 | StateType &S) { | 
|  | 540 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " | 
|  | 541 | << static_cast<const AbstractAttribute &>(QueryingAA) | 
|  | 542 | << " into " << S << "\n"); | 
|  | 543 |  | 
|  | 544 | assert((QueryingAA.getIRPosition().getPositionKind() == | 
|  | 545 | IRPosition::IRP_RETURNED || | 
|  | 546 | QueryingAA.getIRPosition().getPositionKind() == | 
|  | 547 | IRPosition::IRP_CALL_SITE_RETURNED) && | 
|  | 548 | "Can only clamp returned value states for a function returned or call " | 
|  | 549 | "site returned position!"); | 
|  | 550 |  | 
|  | 551 | // Use an optional state as there might not be any return values and we want | 
|  | 552 | // to join (IntegerState::operator&) the state of all there are. | 
|  | 553 | Optional<StateType> T; | 
|  | 554 |  | 
|  | 555 | // Callback for each possibly returned value. | 
|  | 556 | auto CheckReturnValue = [&](Value &RV) -> bool { | 
|  | 557 | const IRPosition &RVPos = IRPosition::value(RV); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 558 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); | 
|  | 559 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() | 
|  | 560 | << " @ " << RVPos << "\n"); | 
|  | 561 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 562 | if (T.hasValue()) | 
|  | 563 | *T &= AAS; | 
|  | 564 | else | 
|  | 565 | T = AAS; | 
|  | 566 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T | 
|  | 567 | << "\n"); | 
|  | 568 | return T->isValidState(); | 
|  | 569 | }; | 
|  | 570 |  | 
|  | 571 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) | 
|  | 572 | S.indicatePessimisticFixpoint(); | 
|  | 573 | else if (T.hasValue()) | 
|  | 574 | S ^= *T; | 
|  | 575 | } | 
|  | 576 |  | 
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 577 | /// Helper class to compose two generic deduction | 
|  | 578 | template <typename AAType, typename Base, typename StateType, | 
|  | 579 | template <typename...> class F, template <typename...> class G> | 
|  | 580 | struct AAComposeTwoGenericDeduction | 
|  | 581 | : public F<AAType, G<AAType, Base, StateType>, StateType> { | 
|  | 582 | AAComposeTwoGenericDeduction(const IRPosition &IRP) | 
|  | 583 | : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} | 
|  | 584 |  | 
|  | 585 | /// See AbstractAttribute::updateImpl(...). | 
|  | 586 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 587 | ChangeStatus ChangedF = F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); | 
|  | 588 | ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); | 
|  | 589 | return ChangedF | ChangedG; | 
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 590 | } | 
|  | 591 | }; | 
|  | 592 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 593 | /// Helper class for generic deduction: return value -> returned position. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 594 | template <typename AAType, typename Base, | 
|  | 595 | typename StateType = typename AAType::StateType> | 
|  | 596 | struct AAReturnedFromReturnedValues : public Base { | 
|  | 597 | AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 598 |  | 
|  | 599 | /// See AbstractAttribute::updateImpl(...). | 
|  | 600 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 601 | StateType S; | 
|  | 602 | clampReturnedValueStates<AAType, StateType>(A, *this, S); | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 603 | // TODO: If we know we visited all returned values, thus no are assumed | 
|  | 604 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 605 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | 
|  | 606 | } | 
|  | 607 | }; | 
|  | 608 |  | 
|  | 609 | /// Clamp the information known at all call sites for a given argument | 
|  | 610 | /// (identified by \p QueryingAA) into \p S. | 
|  | 611 | template <typename AAType, typename StateType = typename AAType::StateType> | 
|  | 612 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, | 
|  | 613 | StateType &S) { | 
|  | 614 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " | 
|  | 615 | << static_cast<const AbstractAttribute &>(QueryingAA) | 
|  | 616 | << " into " << S << "\n"); | 
|  | 617 |  | 
|  | 618 | assert(QueryingAA.getIRPosition().getPositionKind() == | 
|  | 619 | IRPosition::IRP_ARGUMENT && | 
|  | 620 | "Can only clamp call site argument states for an argument position!"); | 
|  | 621 |  | 
|  | 622 | // Use an optional state as there might not be any return values and we want | 
|  | 623 | // to join (IntegerState::operator&) the state of all there are. | 
|  | 624 | Optional<StateType> T; | 
|  | 625 |  | 
|  | 626 | // The argument number which is also the call site argument number. | 
|  | 627 | unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); | 
|  | 628 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 629 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | 
|  | 630 | const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); | 
|  | 631 | // Check if a coresponding argument was found or if it is on not associated | 
|  | 632 | // (which can happen for callback calls). | 
|  | 633 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | 
|  | 634 | return false; | 
|  | 635 |  | 
|  | 636 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); | 
|  | 637 | LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() | 
|  | 638 | << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 639 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 640 | if (T.hasValue()) | 
|  | 641 | *T &= AAS; | 
|  | 642 | else | 
|  | 643 | T = AAS; | 
|  | 644 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T | 
|  | 645 | << "\n"); | 
|  | 646 | return T->isValidState(); | 
|  | 647 | }; | 
|  | 648 |  | 
|  | 649 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true)) | 
|  | 650 | S.indicatePessimisticFixpoint(); | 
|  | 651 | else if (T.hasValue()) | 
|  | 652 | S ^= *T; | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | /// Helper class for generic deduction: call site argument -> argument position. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 656 | template <typename AAType, typename Base, | 
|  | 657 | typename StateType = typename AAType::StateType> | 
|  | 658 | struct AAArgumentFromCallSiteArguments : public Base { | 
|  | 659 | AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 660 |  | 
|  | 661 | /// See AbstractAttribute::updateImpl(...). | 
|  | 662 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 663 | StateType S; | 
|  | 664 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 665 | // TODO: If we know we visited all incoming values, thus no are assumed | 
|  | 666 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 667 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | 
|  | 668 | } | 
|  | 669 | }; | 
|  | 670 |  | 
|  | 671 | /// Helper class for generic replication: function returned -> cs returned. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 672 | template <typename AAType, typename Base, | 
|  | 673 | typename StateType = typename AAType::StateType> | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 674 | struct AACallSiteReturnedFromReturned : public Base { | 
|  | 675 | AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 676 |  | 
|  | 677 | /// See AbstractAttribute::updateImpl(...). | 
|  | 678 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 679 | assert(this->getIRPosition().getPositionKind() == | 
|  | 680 | IRPosition::IRP_CALL_SITE_RETURNED && | 
|  | 681 | "Can only wrap function returned positions for call site returned " | 
|  | 682 | "positions!"); | 
|  | 683 | auto &S = this->getState(); | 
|  | 684 |  | 
|  | 685 | const Function *AssociatedFunction = | 
|  | 686 | this->getIRPosition().getAssociatedFunction(); | 
|  | 687 | if (!AssociatedFunction) | 
|  | 688 | return S.indicatePessimisticFixpoint(); | 
|  | 689 |  | 
|  | 690 | IRPosition FnPos = IRPosition::returned(*AssociatedFunction); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 691 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 692 | return clampStateAndIndicateChange( | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 693 | S, static_cast<const typename AAType::StateType &>(AA.getState())); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 694 | } | 
|  | 695 | }; | 
|  | 696 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 697 | /// Helper class for generic deduction using must-be-executed-context | 
|  | 698 | /// Base class is required to have `followUse` method. | 
|  | 699 |  | 
|  | 700 | /// bool followUse(Attributor &A, const Use *U, const Instruction *I) | 
| Simon Pilgrim | f7aee61 | 2019-10-10 15:25:16 +0000 | [diff] [blame] | 701 | /// U - Underlying use. | 
|  | 702 | /// I - The user of the \p U. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 703 | /// `followUse` returns true if the value should be tracked transitively. | 
|  | 704 |  | 
|  | 705 | template <typename AAType, typename Base, | 
|  | 706 | typename StateType = typename AAType::StateType> | 
|  | 707 | struct AAFromMustBeExecutedContext : public Base { | 
|  | 708 | AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} | 
|  | 709 |  | 
|  | 710 | void initialize(Attributor &A) override { | 
|  | 711 | Base::initialize(A); | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 712 | const IRPosition &IRP = this->getIRPosition(); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 713 | Instruction *CtxI = IRP.getCtxI(); | 
|  | 714 |  | 
|  | 715 | if (!CtxI) | 
|  | 716 | return; | 
|  | 717 |  | 
|  | 718 | for (const Use &U : IRP.getAssociatedValue().uses()) | 
|  | 719 | Uses.insert(&U); | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | /// See AbstractAttribute::updateImpl(...). | 
|  | 723 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 724 | auto BeforeState = this->getState(); | 
|  | 725 | auto &S = this->getState(); | 
|  | 726 | Instruction *CtxI = this->getIRPosition().getCtxI(); | 
|  | 727 | if (!CtxI) | 
|  | 728 | return ChangeStatus::UNCHANGED; | 
|  | 729 |  | 
|  | 730 | MustBeExecutedContextExplorer &Explorer = | 
|  | 731 | A.getInfoCache().getMustBeExecutedContextExplorer(); | 
|  | 732 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 733 | auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 734 | for (unsigned u = 0 ; u < Uses.size(); ++u) { | 
|  | 735 | const Use *U = Uses[u]; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 736 | if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 737 | bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 738 | if (Found && Base::followUse(A, U, UserI)) | 
|  | 739 | for (const Use &Us : UserI->uses()) | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 740 | Uses.insert(&Us); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 741 | } | 
|  | 742 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 743 |  | 
|  | 744 | return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; | 
|  | 745 | } | 
|  | 746 |  | 
|  | 747 | private: | 
|  | 748 | /// Container for (transitive) uses of the associated value. | 
|  | 749 | SetVector<const Use *> Uses; | 
|  | 750 | }; | 
|  | 751 |  | 
|  | 752 | template <typename AAType, typename Base, | 
|  | 753 | typename StateType = typename AAType::StateType> | 
|  | 754 | using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = | 
|  | 755 | AAComposeTwoGenericDeduction<AAType, Base, StateType, | 
|  | 756 | AAFromMustBeExecutedContext, | 
|  | 757 | AAArgumentFromCallSiteArguments>; | 
|  | 758 |  | 
|  | 759 | template <typename AAType, typename Base, | 
|  | 760 | typename StateType = typename AAType::StateType> | 
|  | 761 | using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = | 
|  | 762 | AAComposeTwoGenericDeduction<AAType, Base, StateType, | 
|  | 763 | AAFromMustBeExecutedContext, | 
|  | 764 | AACallSiteReturnedFromReturned>; | 
|  | 765 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 766 | /// -----------------------NoUnwind Function Attribute-------------------------- | 
|  | 767 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 768 | struct AANoUnwindImpl : AANoUnwind { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 769 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 770 |  | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 771 | const std::string getAsStr() const override { | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 772 | return getAssumed() ? "nounwind" : "may-unwind"; | 
|  | 773 | } | 
|  | 774 |  | 
|  | 775 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 776 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 777 | auto Opcodes = { | 
|  | 778 | (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr, | 
|  | 779 | (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet, | 
|  | 780 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; | 
|  | 781 |  | 
|  | 782 | auto CheckForNoUnwind = [&](Instruction &I) { | 
|  | 783 | if (!I.mayThrow()) | 
|  | 784 | return true; | 
|  | 785 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 786 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 787 | const auto &NoUnwindAA = | 
|  | 788 | A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); | 
|  | 789 | return NoUnwindAA.isAssumedNoUnwind(); | 
|  | 790 | } | 
|  | 791 | return false; | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 792 | }; | 
|  | 793 |  | 
|  | 794 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) | 
|  | 795 | return indicatePessimisticFixpoint(); | 
|  | 796 |  | 
|  | 797 | return ChangeStatus::UNCHANGED; | 
|  | 798 | } | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 799 | }; | 
|  | 800 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 801 | struct AANoUnwindFunction final : public AANoUnwindImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 802 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 803 |  | 
|  | 804 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 805 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 806 | }; | 
|  | 807 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 808 | /// NoUnwind attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 809 | struct AANoUnwindCallSite final : AANoUnwindImpl { | 
|  | 810 | AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} | 
|  | 811 |  | 
|  | 812 | /// See AbstractAttribute::initialize(...). | 
|  | 813 | void initialize(Attributor &A) override { | 
|  | 814 | AANoUnwindImpl::initialize(A); | 
|  | 815 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 816 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 817 | indicatePessimisticFixpoint(); | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | /// See AbstractAttribute::updateImpl(...). | 
|  | 821 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 822 | // TODO: Once we have call site specific value information we can provide | 
|  | 823 | //       call site specific liveness information and then it makes | 
|  | 824 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 825 | //       redirecting requests to the callee argument. | 
|  | 826 | Function *F = getAssociatedFunction(); | 
|  | 827 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 828 | auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); | 
|  | 829 | return clampStateAndIndicateChange( | 
|  | 830 | getState(), | 
|  | 831 | static_cast<const AANoUnwind::StateType &>(FnAA.getState())); | 
|  | 832 | } | 
|  | 833 |  | 
|  | 834 | /// See AbstractAttribute::trackStatistics() | 
|  | 835 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } | 
|  | 836 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 837 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 838 | /// --------------------- Function Return Values ------------------------------- | 
|  | 839 |  | 
|  | 840 | /// "Attribute" that collects all potential returned values and the return | 
|  | 841 | /// instructions that they arise from. | 
|  | 842 | /// | 
|  | 843 | /// If there is a unique returned value R, the manifest method will: | 
|  | 844 | ///   - mark R with the "returned" attribute, if R is an argument. | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 845 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 846 |  | 
|  | 847 | /// Mapping of values potentially returned by the associated function to the | 
|  | 848 | /// return instructions that might return them. | 
| Johannes Doerfert | a4a308c | 2019-08-26 17:51:23 +0000 | [diff] [blame] | 849 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 850 |  | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 851 | /// Mapping to remember the number of returned values for a call site such | 
|  | 852 | /// that we can avoid updates if nothing changed. | 
|  | 853 | DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; | 
|  | 854 |  | 
|  | 855 | /// Set of unresolved calls returned by the associated function. | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 856 | SmallSetVector<CallBase *, 4> UnresolvedCalls; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 857 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 858 | /// State flags | 
|  | 859 | /// | 
|  | 860 | ///{ | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 861 | bool IsFixed = false; | 
|  | 862 | bool IsValidState = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 863 | ///} | 
|  | 864 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 865 | public: | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 866 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 867 |  | 
|  | 868 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 869 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 870 | // Reset the state. | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 871 | IsFixed = false; | 
|  | 872 | IsValidState = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 873 | ReturnedValues.clear(); | 
|  | 874 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 875 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 876 | if (!F) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 877 | indicatePessimisticFixpoint(); | 
|  | 878 | return; | 
|  | 879 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 880 |  | 
|  | 881 | // The map from instruction opcodes to those instructions in the function. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 882 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 883 |  | 
|  | 884 | // 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] | 885 | for (Argument &Arg : F->args()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 886 | if (Arg.hasReturnedAttr()) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 887 | auto &ReturnInstSet = ReturnedValues[&Arg]; | 
|  | 888 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) | 
|  | 889 | ReturnInstSet.insert(cast<ReturnInst>(RI)); | 
|  | 890 |  | 
|  | 891 | indicateOptimisticFixpoint(); | 
|  | 892 | return; | 
|  | 893 | } | 
|  | 894 | } | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 895 |  | 
|  | 896 | if (!F->hasExactDefinition()) | 
|  | 897 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 898 | } | 
|  | 899 |  | 
|  | 900 | /// See AbstractAttribute::manifest(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 901 | ChangeStatus manifest(Attributor &A) override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 902 |  | 
|  | 903 | /// See AbstractAttribute::getState(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 904 | AbstractState &getState() override { return *this; } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 905 |  | 
|  | 906 | /// See AbstractAttribute::getState(...). | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 907 | const AbstractState &getState() const override { return *this; } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 908 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 909 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 910 | ChangeStatus updateImpl(Attributor &A) override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 911 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 912 | llvm::iterator_range<iterator> returned_values() override { | 
|  | 913 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | 
|  | 914 | } | 
|  | 915 |  | 
|  | 916 | llvm::iterator_range<const_iterator> returned_values() const override { | 
|  | 917 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | 
|  | 918 | } | 
|  | 919 |  | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 920 | const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 921 | return UnresolvedCalls; | 
|  | 922 | } | 
|  | 923 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 924 | /// Return the number of potential return values, -1 if unknown. | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 925 | size_t getNumReturnValues() const override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 926 | return isValidState() ? ReturnedValues.size() : -1; | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | /// Return an assumed unique return value if a single candidate is found. If | 
|  | 930 | /// there cannot be one, return a nullptr. If it is not clear yet, return the | 
|  | 931 | /// Optional::NoneType. | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 932 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 933 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 934 | /// See AbstractState::checkForAllReturnedValues(...). | 
|  | 935 | bool checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 936 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 937 | &Pred) const override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 938 |  | 
|  | 939 | /// Pretty print the attribute similar to the IR representation. | 
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 940 | const std::string getAsStr() const override; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 941 |  | 
|  | 942 | /// See AbstractState::isAtFixpoint(). | 
|  | 943 | bool isAtFixpoint() const override { return IsFixed; } | 
|  | 944 |  | 
|  | 945 | /// See AbstractState::isValidState(). | 
|  | 946 | bool isValidState() const override { return IsValidState; } | 
|  | 947 |  | 
|  | 948 | /// See AbstractState::indicateOptimisticFixpoint(...). | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 949 | ChangeStatus indicateOptimisticFixpoint() override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 950 | IsFixed = true; | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 951 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 952 | } | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 953 |  | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 954 | ChangeStatus indicatePessimisticFixpoint() override { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 955 | IsFixed = true; | 
|  | 956 | IsValidState = false; | 
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 957 | return ChangeStatus::CHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 958 | } | 
|  | 959 | }; | 
|  | 960 |  | 
|  | 961 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { | 
|  | 962 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 963 |  | 
|  | 964 | // Bookkeeping. | 
|  | 965 | assert(isValidState()); | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 966 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, | 
|  | 967 | "Number of function with known return values"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 968 |  | 
|  | 969 | // 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] | 970 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 971 |  | 
|  | 972 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) | 
|  | 973 | return Changed; | 
|  | 974 |  | 
|  | 975 | // Bookkeeping. | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 976 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, | 
|  | 977 | "Number of function with unique return"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 978 |  | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 979 | // Callback to replace the uses of CB with the constant C. | 
|  | 980 | auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) { | 
| Johannes Doerfert | 8fa56c4 | 2019-10-11 01:45:32 +0000 | [diff] [blame] | 981 | if (CB.getNumUses() == 0 || CB.isMustTailCall()) | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 982 | return ChangeStatus::UNCHANGED; | 
|  | 983 | CB.replaceAllUsesWith(&C); | 
|  | 984 | return ChangeStatus::CHANGED; | 
|  | 985 | }; | 
|  | 986 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 987 | // If the assumed unique return value is an argument, annotate it. | 
|  | 988 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 989 | // TODO: This should be handled differently! | 
|  | 990 | this->AnchorVal = UniqueRVArg; | 
|  | 991 | this->KindOrArgNo = UniqueRVArg->getArgNo(); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 992 | Changed = IRAttribute::manifest(A); | 
|  | 993 | } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { | 
|  | 994 | // We can replace the returned value with the unique returned constant. | 
|  | 995 | Value &AnchorValue = getAnchorValue(); | 
|  | 996 | if (Function *F = dyn_cast<Function>(&AnchorValue)) { | 
|  | 997 | for (const Use &U : F->uses()) | 
|  | 998 | if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 999 | if (CB->isCallee(&U)) { | 
|  | 1000 | Constant *RVCCast = | 
|  | 1001 | ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); | 
|  | 1002 | Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; | 
|  | 1003 | } | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1004 | } else { | 
|  | 1005 | assert(isa<CallBase>(AnchorValue) && | 
|  | 1006 | "Expcected a function or call base anchor!"); | 
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1007 | Constant *RVCCast = | 
|  | 1008 | ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); | 
|  | 1009 | Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); | 
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1010 | } | 
|  | 1011 | if (Changed == ChangeStatus::CHANGED) | 
|  | 1012 | STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, | 
|  | 1013 | "Number of function returns replaced by constant return"); | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1014 | } | 
|  | 1015 |  | 
|  | 1016 | return Changed; | 
|  | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | const std::string AAReturnedValuesImpl::getAsStr() const { | 
|  | 1020 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + | 
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 1021 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1022 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1023 | } | 
|  | 1024 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1025 | Optional<Value *> | 
|  | 1026 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { | 
|  | 1027 | // If checkForAllReturnedValues provides a unique value, ignoring potential | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1028 | // undef values that can also be present, it is assumed to be the actual | 
|  | 1029 | // return value and forwarded to the caller of this method. If there are | 
|  | 1030 | // multiple, a nullptr is returned indicating there cannot be a unique | 
|  | 1031 | // returned value. | 
|  | 1032 | Optional<Value *> UniqueRV; | 
|  | 1033 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1034 | auto Pred = [&](Value &RV) -> bool { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1035 | // If we found a second returned value and neither the current nor the saved | 
|  | 1036 | // one is an undef, there is no unique returned value. Undefs are special | 
|  | 1037 | // since we can pretend they have any value. | 
|  | 1038 | if (UniqueRV.hasValue() && UniqueRV != &RV && | 
|  | 1039 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { | 
|  | 1040 | UniqueRV = nullptr; | 
|  | 1041 | return false; | 
|  | 1042 | } | 
|  | 1043 |  | 
|  | 1044 | // Do not overwrite a value with an undef. | 
|  | 1045 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) | 
|  | 1046 | UniqueRV = &RV; | 
|  | 1047 |  | 
|  | 1048 | return true; | 
|  | 1049 | }; | 
|  | 1050 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1051 | if (!A.checkForAllReturnedValues(Pred, *this)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1052 | UniqueRV = nullptr; | 
|  | 1053 |  | 
|  | 1054 | return UniqueRV; | 
|  | 1055 | } | 
|  | 1056 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1057 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1058 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1059 | &Pred) const { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1060 | if (!isValidState()) | 
|  | 1061 | return false; | 
|  | 1062 |  | 
|  | 1063 | // Check all returned values but ignore call sites as long as we have not | 
|  | 1064 | // encountered an overdefined one during an update. | 
|  | 1065 | for (auto &It : ReturnedValues) { | 
|  | 1066 | Value *RV = It.first; | 
|  | 1067 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1068 | CallBase *CB = dyn_cast<CallBase>(RV); | 
|  | 1069 | if (CB && !UnresolvedCalls.count(CB)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1070 | continue; | 
|  | 1071 |  | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1072 | if (!Pred(*RV, It.second)) | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1073 | return false; | 
|  | 1074 | } | 
|  | 1075 |  | 
|  | 1076 | return true; | 
|  | 1077 | } | 
|  | 1078 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1079 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1080 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); | 
|  | 1081 | bool Changed = false; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1082 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1083 | // State used in the value traversals starting in returned values. | 
|  | 1084 | struct RVState { | 
|  | 1085 | // The map in which we collect return values -> return instrs. | 
|  | 1086 | decltype(ReturnedValues) &RetValsMap; | 
|  | 1087 | // The flag to indicate a change. | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1088 | bool &Changed; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1089 | // The return instrs we come from. | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1090 | SmallSetVector<ReturnInst *, 4> RetInsts; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1091 | }; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1092 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1093 | // Callback for a leaf value returned by the associated function. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1094 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1095 | auto Size = RVS.RetValsMap[&Val].size(); | 
|  | 1096 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); | 
|  | 1097 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; | 
|  | 1098 | RVS.Changed |= Inserted; | 
|  | 1099 | LLVM_DEBUG({ | 
|  | 1100 | if (Inserted) | 
|  | 1101 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val | 
|  | 1102 | << " => " << RVS.RetInsts.size() << "\n"; | 
|  | 1103 | }); | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1104 | return true; | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1105 | }; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1106 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1107 | // Helper method to invoke the generic value traversal. | 
|  | 1108 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { | 
|  | 1109 | IRPosition RetValPos = IRPosition::value(RV); | 
|  | 1110 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, | 
|  | 1111 | RVS, VisitValueCB); | 
|  | 1112 | }; | 
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 1113 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1114 | // Callback for all "return intructions" live in the associated function. | 
|  | 1115 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { | 
|  | 1116 | ReturnInst &Ret = cast<ReturnInst>(I); | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1117 | RVState RVS({ReturnedValues, Changed, {}}); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1118 | RVS.RetInsts.insert(&Ret); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1119 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); | 
|  | 1120 | }; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1121 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1122 | // Start by discovering returned values from all live returned instructions in | 
|  | 1123 | // the associated function. | 
|  | 1124 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) | 
|  | 1125 | return indicatePessimisticFixpoint(); | 
|  | 1126 |  | 
|  | 1127 | // Once returned values "directly" present in the code are handled we try to | 
|  | 1128 | // resolve returned calls. | 
|  | 1129 | decltype(ReturnedValues) NewRVsMap; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1130 | for (auto &It : ReturnedValues) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1131 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first | 
|  | 1132 | << " by #" << It.second.size() << " RIs\n"); | 
|  | 1133 | CallBase *CB = dyn_cast<CallBase>(It.first); | 
|  | 1134 | if (!CB || UnresolvedCalls.count(CB)) | 
|  | 1135 | continue; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1136 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1137 | if (!CB->getCalledFunction()) { | 
|  | 1138 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB | 
|  | 1139 | << "\n"); | 
|  | 1140 | UnresolvedCalls.insert(CB); | 
|  | 1141 | continue; | 
|  | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 1145 | const auto &RetValAA = A.getAAFor<AAReturnedValues>( | 
|  | 1146 | *this, IRPosition::function(*CB->getCalledFunction())); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1147 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " | 
|  | 1148 | << static_cast<const AbstractAttribute &>(RetValAA) | 
|  | 1149 | << "\n"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1150 |  | 
|  | 1151 | // Skip dead ends, thus if we do not know anything about the returned | 
|  | 1152 | // call we mark it as unresolved and it will stay that way. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1153 | if (!RetValAA.getState().isValidState()) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1154 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB | 
|  | 1155 | << "\n"); | 
|  | 1156 | UnresolvedCalls.insert(CB); | 
|  | 1157 | continue; | 
|  | 1158 | } | 
|  | 1159 |  | 
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1160 | // Do not try to learn partial information. If the callee has unresolved | 
|  | 1161 | // return values we will treat the call as unresolved/opaque. | 
|  | 1162 | auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); | 
|  | 1163 | if (!RetValAAUnresolvedCalls.empty()) { | 
|  | 1164 | UnresolvedCalls.insert(CB); | 
|  | 1165 | continue; | 
|  | 1166 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1167 |  | 
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1168 | // Now check if we can track transitively returned values. If possible, thus | 
|  | 1169 | // if all return value can be represented in the current scope, do so. | 
|  | 1170 | bool Unresolved = false; | 
|  | 1171 | for (auto &RetValAAIt : RetValAA.returned_values()) { | 
|  | 1172 | Value *RetVal = RetValAAIt.first; | 
|  | 1173 | if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || | 
|  | 1174 | isa<Constant>(RetVal)) | 
|  | 1175 | continue; | 
|  | 1176 | // Anything that did not fit in the above categories cannot be resolved, | 
|  | 1177 | // mark the call as unresolved. | 
|  | 1178 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " | 
|  | 1179 | "cannot be translated: " | 
|  | 1180 | << *RetVal << "\n"); | 
|  | 1181 | UnresolvedCalls.insert(CB); | 
|  | 1182 | Unresolved = true; | 
|  | 1183 | break; | 
|  | 1184 | } | 
|  | 1185 |  | 
|  | 1186 | if (Unresolved) | 
|  | 1187 | continue; | 
|  | 1188 |  | 
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1189 | // Now track transitively returned values. | 
|  | 1190 | unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; | 
|  | 1191 | if (NumRetAA == RetValAA.getNumReturnValues()) { | 
|  | 1192 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " | 
|  | 1193 | "changed since it was seen last\n"); | 
|  | 1194 | continue; | 
|  | 1195 | } | 
|  | 1196 | NumRetAA = RetValAA.getNumReturnValues(); | 
|  | 1197 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1198 | for (auto &RetValAAIt : RetValAA.returned_values()) { | 
|  | 1199 | Value *RetVal = RetValAAIt.first; | 
|  | 1200 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { | 
|  | 1201 | // Arguments are mapped to call site operands and we begin the traversal | 
|  | 1202 | // again. | 
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1203 | bool Unused = false; | 
|  | 1204 | RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1205 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); | 
|  | 1206 | continue; | 
|  | 1207 | } else if (isa<CallBase>(RetVal)) { | 
|  | 1208 | // Call sites are resolved by the callee attribute over time, no need to | 
|  | 1209 | // do anything for us. | 
|  | 1210 | continue; | 
|  | 1211 | } else if (isa<Constant>(RetVal)) { | 
|  | 1212 | // Constants are valid everywhere, we can simply take them. | 
|  | 1213 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); | 
|  | 1214 | continue; | 
|  | 1215 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1216 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1217 | } | 
|  | 1218 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1219 | // To avoid modifications to the ReturnedValues map while we iterate over it | 
|  | 1220 | // we kept record of potential new entries in a copy map, NewRVsMap. | 
|  | 1221 | for (auto &It : NewRVsMap) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1222 | assert(!It.second.empty() && "Entry does not add anything."); | 
|  | 1223 | auto &ReturnInsts = ReturnedValues[It.first]; | 
|  | 1224 | for (ReturnInst *RI : It.second) | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1225 | if (ReturnInsts.insert(RI)) { | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1226 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " | 
|  | 1227 | << *It.first << " => " << *RI << "\n"); | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1228 | Changed = true; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1229 | } | 
|  | 1230 | } | 
|  | 1231 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1232 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); | 
|  | 1233 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1234 | } | 
|  | 1235 |  | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1236 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { | 
|  | 1237 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} | 
|  | 1238 |  | 
|  | 1239 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1240 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1241 | }; | 
|  | 1242 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1243 | /// Returned values information for a call sites. | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1244 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { | 
|  | 1245 | AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} | 
|  | 1246 |  | 
|  | 1247 | /// See AbstractAttribute::initialize(...). | 
|  | 1248 | void initialize(Attributor &A) override { | 
|  | 1249 | // TODO: Once we have call site specific value information we can provide | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1250 | //       call site specific liveness information and then it makes | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1251 | //       sense to specialize attributes for call sites instead of | 
|  | 1252 | //       redirecting requests to the callee. | 
|  | 1253 | llvm_unreachable("Abstract attributes for returned values are not " | 
|  | 1254 | "supported for call sites yet!"); | 
|  | 1255 | } | 
|  | 1256 |  | 
|  | 1257 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1258 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1259 | return indicatePessimisticFixpoint(); | 
|  | 1260 | } | 
|  | 1261 |  | 
|  | 1262 | /// See AbstractAttribute::trackStatistics() | 
|  | 1263 | void trackStatistics() const override {} | 
|  | 1264 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1265 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1266 | /// ------------------------ NoSync Function Attribute ------------------------- | 
|  | 1267 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1268 | struct AANoSyncImpl : AANoSync { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1269 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1270 |  | 
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 1271 | const std::string getAsStr() const override { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1272 | return getAssumed() ? "nosync" : "may-sync"; | 
|  | 1273 | } | 
|  | 1274 |  | 
|  | 1275 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1276 | ChangeStatus updateImpl(Attributor &A) override; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1277 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1278 | /// Helper function used to determine whether an instruction is non-relaxed | 
|  | 1279 | /// atomic. In other words, if an atomic instruction does not have unordered | 
|  | 1280 | /// or monotonic ordering | 
|  | 1281 | static bool isNonRelaxedAtomic(Instruction *I); | 
|  | 1282 |  | 
|  | 1283 | /// Helper function used to determine whether an instruction is volatile. | 
|  | 1284 | static bool isVolatile(Instruction *I); | 
|  | 1285 |  | 
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 1286 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, | 
|  | 1287 | /// memset). | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1288 | static bool isNoSyncIntrinsic(Instruction *I); | 
|  | 1289 | }; | 
|  | 1290 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1291 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1292 | if (!I->isAtomic()) | 
|  | 1293 | return false; | 
|  | 1294 |  | 
|  | 1295 | AtomicOrdering Ordering; | 
|  | 1296 | switch (I->getOpcode()) { | 
|  | 1297 | case Instruction::AtomicRMW: | 
|  | 1298 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); | 
|  | 1299 | break; | 
|  | 1300 | case Instruction::Store: | 
|  | 1301 | Ordering = cast<StoreInst>(I)->getOrdering(); | 
|  | 1302 | break; | 
|  | 1303 | case Instruction::Load: | 
|  | 1304 | Ordering = cast<LoadInst>(I)->getOrdering(); | 
|  | 1305 | break; | 
|  | 1306 | case Instruction::Fence: { | 
|  | 1307 | auto *FI = cast<FenceInst>(I); | 
|  | 1308 | if (FI->getSyncScopeID() == SyncScope::SingleThread) | 
|  | 1309 | return false; | 
|  | 1310 | Ordering = FI->getOrdering(); | 
|  | 1311 | break; | 
|  | 1312 | } | 
|  | 1313 | case Instruction::AtomicCmpXchg: { | 
|  | 1314 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); | 
|  | 1315 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); | 
|  | 1316 | // Only if both are relaxed, than it can be treated as relaxed. | 
|  | 1317 | // Otherwise it is non-relaxed. | 
|  | 1318 | if (Success != AtomicOrdering::Unordered && | 
|  | 1319 | Success != AtomicOrdering::Monotonic) | 
|  | 1320 | return true; | 
|  | 1321 | if (Failure != AtomicOrdering::Unordered && | 
|  | 1322 | Failure != AtomicOrdering::Monotonic) | 
|  | 1323 | return true; | 
|  | 1324 | return false; | 
|  | 1325 | } | 
|  | 1326 | default: | 
|  | 1327 | llvm_unreachable( | 
|  | 1328 | "New atomic operations need to be known in the attributor."); | 
|  | 1329 | } | 
|  | 1330 |  | 
|  | 1331 | // Relaxed. | 
|  | 1332 | if (Ordering == AtomicOrdering::Unordered || | 
|  | 1333 | Ordering == AtomicOrdering::Monotonic) | 
|  | 1334 | return false; | 
|  | 1335 | return true; | 
|  | 1336 | } | 
|  | 1337 |  | 
|  | 1338 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. | 
|  | 1339 | /// FIXME: We should ipmrove the handling of intrinsics. | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1340 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1341 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { | 
|  | 1342 | switch (II->getIntrinsicID()) { | 
|  | 1343 | /// Element wise atomic memory intrinsics are can only be unordered, | 
|  | 1344 | /// therefore nosync. | 
|  | 1345 | case Intrinsic::memset_element_unordered_atomic: | 
|  | 1346 | case Intrinsic::memmove_element_unordered_atomic: | 
|  | 1347 | case Intrinsic::memcpy_element_unordered_atomic: | 
|  | 1348 | return true; | 
|  | 1349 | case Intrinsic::memset: | 
|  | 1350 | case Intrinsic::memmove: | 
|  | 1351 | case Intrinsic::memcpy: | 
|  | 1352 | if (!cast<MemIntrinsic>(II)->isVolatile()) | 
|  | 1353 | return true; | 
|  | 1354 | return false; | 
|  | 1355 | default: | 
|  | 1356 | return false; | 
|  | 1357 | } | 
|  | 1358 | } | 
|  | 1359 | return false; | 
|  | 1360 | } | 
|  | 1361 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1362 | bool AANoSyncImpl::isVolatile(Instruction *I) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1363 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && | 
|  | 1364 | "Calls should not be checked here"); | 
|  | 1365 |  | 
|  | 1366 | switch (I->getOpcode()) { | 
|  | 1367 | case Instruction::AtomicRMW: | 
|  | 1368 | return cast<AtomicRMWInst>(I)->isVolatile(); | 
|  | 1369 | case Instruction::Store: | 
|  | 1370 | return cast<StoreInst>(I)->isVolatile(); | 
|  | 1371 | case Instruction::Load: | 
|  | 1372 | return cast<LoadInst>(I)->isVolatile(); | 
|  | 1373 | case Instruction::AtomicCmpXchg: | 
|  | 1374 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); | 
|  | 1375 | default: | 
|  | 1376 | return false; | 
|  | 1377 | } | 
|  | 1378 | } | 
|  | 1379 |  | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1380 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1381 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1382 | auto CheckRWInstForNoSync = [&](Instruction &I) { | 
|  | 1383 | /// We are looking for volatile instructions or Non-Relaxed atomics. | 
|  | 1384 | /// FIXME: We should ipmrove the handling of intrinsics. | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1385 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1386 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) | 
|  | 1387 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1388 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1389 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 1390 | if (ICS.hasFnAttr(Attribute::NoSync)) | 
|  | 1391 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1392 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1393 | const auto &NoSyncAA = | 
|  | 1394 | A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1395 | if (NoSyncAA.isAssumedNoSync()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1396 | return true; | 
|  | 1397 | return false; | 
|  | 1398 | } | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1399 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1400 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) | 
|  | 1401 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1402 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1403 | return false; | 
|  | 1404 | }; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1405 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1406 | auto CheckForNoSync = [&](Instruction &I) { | 
|  | 1407 | // At this point we handled all read/write effects and they are all | 
|  | 1408 | // nosync, so they can be skipped. | 
|  | 1409 | if (I.mayReadOrWriteMemory()) | 
|  | 1410 | return true; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1411 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1412 | // non-convergent and readnone imply nosync. | 
|  | 1413 | return !ImmutableCallSite(&I).isConvergent(); | 
|  | 1414 | }; | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1415 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1416 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || | 
|  | 1417 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1418 | return indicatePessimisticFixpoint(); | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1419 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1420 | return ChangeStatus::UNCHANGED; | 
|  | 1421 | } | 
|  | 1422 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1423 | struct AANoSyncFunction final : public AANoSyncImpl { | 
|  | 1424 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} | 
|  | 1425 |  | 
|  | 1426 | /// See AbstractAttribute::trackStatistics() | 
|  | 1427 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } | 
|  | 1428 | }; | 
|  | 1429 |  | 
|  | 1430 | /// NoSync attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1431 | struct AANoSyncCallSite final : AANoSyncImpl { | 
|  | 1432 | AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} | 
|  | 1433 |  | 
|  | 1434 | /// See AbstractAttribute::initialize(...). | 
|  | 1435 | void initialize(Attributor &A) override { | 
|  | 1436 | AANoSyncImpl::initialize(A); | 
|  | 1437 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1438 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1439 | indicatePessimisticFixpoint(); | 
|  | 1440 | } | 
|  | 1441 |  | 
|  | 1442 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1443 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1444 | // TODO: Once we have call site specific value information we can provide | 
|  | 1445 | //       call site specific liveness information and then it makes | 
|  | 1446 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1447 | //       redirecting requests to the callee argument. | 
|  | 1448 | Function *F = getAssociatedFunction(); | 
|  | 1449 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1450 | auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); | 
|  | 1451 | return clampStateAndIndicateChange( | 
|  | 1452 | getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); | 
|  | 1453 | } | 
|  | 1454 |  | 
|  | 1455 | /// See AbstractAttribute::trackStatistics() | 
|  | 1456 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } | 
|  | 1457 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1458 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1459 | /// ------------------------ No-Free Attributes ---------------------------- | 
|  | 1460 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1461 | struct AANoFreeImpl : public AANoFree { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1462 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1463 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1464 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1465 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1466 | auto CheckForNoFree = [&](Instruction &I) { | 
|  | 1467 | ImmutableCallSite ICS(&I); | 
|  | 1468 | if (ICS.hasFnAttr(Attribute::NoFree)) | 
|  | 1469 | return true; | 
|  | 1470 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1471 | const auto &NoFreeAA = | 
|  | 1472 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1473 | return NoFreeAA.isAssumedNoFree(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1474 | }; | 
|  | 1475 |  | 
|  | 1476 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) | 
|  | 1477 | return indicatePessimisticFixpoint(); | 
|  | 1478 | return ChangeStatus::UNCHANGED; | 
|  | 1479 | } | 
|  | 1480 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1481 | /// See AbstractAttribute::getAsStr(). | 
|  | 1482 | const std::string getAsStr() const override { | 
|  | 1483 | return getAssumed() ? "nofree" : "may-free"; | 
|  | 1484 | } | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1485 | }; | 
|  | 1486 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1487 | struct AANoFreeFunction final : public AANoFreeImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1488 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1489 |  | 
|  | 1490 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1491 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1492 | }; | 
|  | 1493 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1494 | /// NoFree attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1495 | struct AANoFreeCallSite final : AANoFreeImpl { | 
|  | 1496 | AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} | 
|  | 1497 |  | 
|  | 1498 | /// See AbstractAttribute::initialize(...). | 
|  | 1499 | void initialize(Attributor &A) override { | 
|  | 1500 | AANoFreeImpl::initialize(A); | 
|  | 1501 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1502 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1503 | indicatePessimisticFixpoint(); | 
|  | 1504 | } | 
|  | 1505 |  | 
|  | 1506 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1507 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1508 | // TODO: Once we have call site specific value information we can provide | 
|  | 1509 | //       call site specific liveness information and then it makes | 
|  | 1510 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1511 | //       redirecting requests to the callee argument. | 
|  | 1512 | Function *F = getAssociatedFunction(); | 
|  | 1513 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1514 | auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); | 
|  | 1515 | return clampStateAndIndicateChange( | 
|  | 1516 | getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); | 
|  | 1517 | } | 
|  | 1518 |  | 
|  | 1519 | /// See AbstractAttribute::trackStatistics() | 
|  | 1520 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } | 
|  | 1521 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1522 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1523 | /// ------------------------ NonNull Argument Attribute ------------------------ | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1524 | static int64_t getKnownNonNullAndDerefBytesForUse( | 
|  | 1525 | Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue, | 
|  | 1526 | const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1527 | TrackUse = false; | 
|  | 1528 |  | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1529 | const Value *UseV = U->get(); | 
|  | 1530 | if (!UseV->getType()->isPointerTy()) | 
|  | 1531 | return 0; | 
|  | 1532 |  | 
|  | 1533 | Type *PtrTy = UseV->getType(); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1534 | const Function *F = I->getFunction(); | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1535 | bool NullPointerIsDefined = | 
|  | 1536 | F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1537 | const DataLayout &DL = A.getInfoCache().getDL(); | 
|  | 1538 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { | 
|  | 1539 | if (ICS.isBundleOperand(U)) | 
|  | 1540 | return 0; | 
|  | 1541 |  | 
|  | 1542 | if (ICS.isCallee(U)) { | 
|  | 1543 | IsNonNull |= !NullPointerIsDefined; | 
|  | 1544 | return 0; | 
|  | 1545 | } | 
|  | 1546 |  | 
|  | 1547 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 1548 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); | 
|  | 1549 | auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP); | 
|  | 1550 | IsNonNull |= DerefAA.isKnownNonNull(); | 
|  | 1551 | return DerefAA.getKnownDereferenceableBytes(); | 
|  | 1552 | } | 
|  | 1553 |  | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1554 | // We need to follow common pointer manipulation uses to the accesses they | 
|  | 1555 | // feed into. We can try to be smart to avoid looking through things we do not | 
|  | 1556 | // like for now, e.g., non-inbounds GEPs. | 
|  | 1557 | if (isa<CastInst>(I)) { | 
|  | 1558 | TrackUse = true; | 
|  | 1559 | return 0; | 
|  | 1560 | } | 
|  | 1561 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) | 
|  | 1562 | if (GEP->hasAllZeroIndices() || | 
|  | 1563 | (GEP->isInBounds() && GEP->hasAllConstantIndices())) { | 
|  | 1564 | TrackUse = true; | 
|  | 1565 | return 0; | 
|  | 1566 | } | 
|  | 1567 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1568 | int64_t Offset; | 
|  | 1569 | if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1570 | if (Base == &AssociatedValue && getPointerOperand(I) == UseV) { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1571 | int64_t DerefBytes = | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1572 | (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset; | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1573 |  | 
|  | 1574 | IsNonNull |= !NullPointerIsDefined; | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1575 | return std::max(int64_t(0), DerefBytes); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1576 | } | 
|  | 1577 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1578 | if (const Value *Base = | 
|  | 1579 | GetPointerBaseWithConstantOffset(UseV, Offset, DL, | 
|  | 1580 | /*AllowNonInbounds*/ false)) { | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1581 | if (Base == &AssociatedValue) { | 
|  | 1582 | auto &DerefAA = | 
|  | 1583 | A.getAAFor<AADereferenceable>(QueryingAA, IRPosition::value(*Base)); | 
|  | 1584 | IsNonNull |= (!NullPointerIsDefined && DerefAA.isKnownNonNull()); | 
|  | 1585 | IsNonNull |= (!NullPointerIsDefined && (Offset != 0)); | 
|  | 1586 | int64_t DerefBytes = DerefAA.getKnownDereferenceableBytes(); | 
|  | 1587 | return std::max(int64_t(0), DerefBytes - std::max(int64_t(0), Offset)); | 
|  | 1588 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1589 | } | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1590 |  | 
|  | 1591 | return 0; | 
|  | 1592 | } | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1593 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1594 | struct AANonNullImpl : AANonNull { | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1595 | AANonNullImpl(const IRPosition &IRP) | 
|  | 1596 | : AANonNull(IRP), | 
|  | 1597 | NullIsDefined(NullPointerIsDefined( | 
|  | 1598 | getAnchorScope(), | 
|  | 1599 | getAssociatedValue().getType()->getPointerAddressSpace())) {} | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1600 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1601 | /// See AbstractAttribute::initialize(...). | 
|  | 1602 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1603 | if (!NullIsDefined && | 
|  | 1604 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1605 | indicateOptimisticFixpoint(); | 
| Johannes Doerfert | eb4f41d | 2019-10-30 17:21:53 -0500 | [diff] [blame] | 1606 | else if (isa<ConstantPointerNull>(getAssociatedValue())) | 
|  | 1607 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1608 | else | 
|  | 1609 | AANonNull::initialize(A); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1610 | } | 
|  | 1611 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1612 | /// See AAFromMustBeExecutedContext | 
|  | 1613 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 1614 | bool IsNonNull = false; | 
|  | 1615 | bool TrackUse = false; | 
|  | 1616 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, | 
|  | 1617 | IsNonNull, TrackUse); | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1618 | setKnown(IsNonNull); | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1619 | return TrackUse; | 
|  | 1620 | } | 
|  | 1621 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1622 | /// See AbstractAttribute::getAsStr(). | 
|  | 1623 | const std::string getAsStr() const override { | 
|  | 1624 | return getAssumed() ? "nonnull" : "may-null"; | 
|  | 1625 | } | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1626 |  | 
|  | 1627 | /// Flag to determine if the underlying value can be null and still allow | 
|  | 1628 | /// valid accesses. | 
|  | 1629 | const bool NullIsDefined; | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1630 | }; | 
|  | 1631 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1632 | /// NonNull attribute for a floating value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1633 | struct AANonNullFloating | 
|  | 1634 | : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { | 
|  | 1635 | using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; | 
|  | 1636 | AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1637 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1638 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1639 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1640 | ChangeStatus Change = Base::updateImpl(A); | 
|  | 1641 | if (isKnownNonNull()) | 
|  | 1642 | return Change; | 
|  | 1643 |  | 
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame] | 1644 | if (!NullIsDefined) { | 
|  | 1645 | const auto &DerefAA = A.getAAFor<AADereferenceable>(*this, getIRPosition()); | 
|  | 1646 | if (DerefAA.getAssumedDereferenceableBytes()) | 
|  | 1647 | return Change; | 
|  | 1648 | } | 
|  | 1649 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1650 | const DataLayout &DL = A.getDataLayout(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1651 |  | 
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1652 | DominatorTree *DT = nullptr; | 
|  | 1653 | InformationCache &InfoCache = A.getInfoCache(); | 
|  | 1654 | if (const Function *Fn = getAnchorScope()) | 
|  | 1655 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); | 
|  | 1656 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 1657 | auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1658 | bool Stripped) -> bool { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1659 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); | 
|  | 1660 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | 2d6d651 | 2019-10-29 11:46:00 -0500 | [diff] [blame] | 1661 | if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT)) | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1662 | T.indicatePessimisticFixpoint(); | 
|  | 1663 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1664 | // Use abstract attribute information. | 
|  | 1665 | const AANonNull::StateType &NS = | 
|  | 1666 | static_cast<const AANonNull::StateType &>(AA.getState()); | 
|  | 1667 | T ^= NS; | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1668 | } | 
|  | 1669 | return T.isValidState(); | 
|  | 1670 | }; | 
|  | 1671 |  | 
|  | 1672 | StateType T; | 
|  | 1673 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, | 
|  | 1674 | T, VisitValueCB)) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1675 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1676 |  | 
|  | 1677 | return clampStateAndIndicateChange(getState(), T); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1678 | } | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1679 |  | 
|  | 1680 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1681 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1682 | }; | 
|  | 1683 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1684 | /// NonNull attribute for function return value. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1685 | struct AANonNullReturned final | 
|  | 1686 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1687 | AANonNullReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1688 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1689 |  | 
|  | 1690 | /// See AbstractAttribute::trackStatistics() | 
|  | 1691 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } | 
|  | 1692 | }; | 
|  | 1693 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1694 | /// NonNull attribute for function argument. | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1695 | struct AANonNullArgument final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1696 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, | 
|  | 1697 | AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1698 | AANonNullArgument(const IRPosition &IRP) | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1699 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, | 
|  | 1700 | AANonNullImpl>( | 
|  | 1701 | IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1702 |  | 
|  | 1703 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1704 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1705 | }; | 
|  | 1706 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1707 | struct AANonNullCallSiteArgument final : AANonNullFloating { | 
|  | 1708 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1709 |  | 
|  | 1710 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 1711 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1712 | }; | 
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1713 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1714 | /// NonNull attribute for a call site return position. | 
|  | 1715 | struct AANonNullCallSiteReturned final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1716 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, | 
|  | 1717 | AANonNullImpl> { | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1718 | AANonNullCallSiteReturned(const IRPosition &IRP) | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1719 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, | 
|  | 1720 | AANonNullImpl>( | 
|  | 1721 | IRP) {} | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1722 |  | 
|  | 1723 | /// See AbstractAttribute::trackStatistics() | 
|  | 1724 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } | 
|  | 1725 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1726 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1727 | /// ------------------------ No-Recurse Attributes ---------------------------- | 
|  | 1728 |  | 
|  | 1729 | struct AANoRecurseImpl : public AANoRecurse { | 
|  | 1730 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} | 
|  | 1731 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1732 | /// See AbstractAttribute::getAsStr() | 
|  | 1733 | const std::string getAsStr() const override { | 
|  | 1734 | return getAssumed() ? "norecurse" : "may-recurse"; | 
|  | 1735 | } | 
|  | 1736 | }; | 
|  | 1737 |  | 
|  | 1738 | struct AANoRecurseFunction final : AANoRecurseImpl { | 
|  | 1739 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} | 
|  | 1740 |  | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1741 | /// See AbstractAttribute::initialize(...). | 
|  | 1742 | void initialize(Attributor &A) override { | 
|  | 1743 | AANoRecurseImpl::initialize(A); | 
|  | 1744 | if (const Function *F = getAnchorScope()) | 
|  | 1745 | if (A.getInfoCache().getSccSize(*F) == 1) | 
|  | 1746 | return; | 
|  | 1747 | indicatePessimisticFixpoint(); | 
|  | 1748 | } | 
|  | 1749 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1750 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1751 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1752 |  | 
|  | 1753 | auto CheckForNoRecurse = [&](Instruction &I) { | 
|  | 1754 | ImmutableCallSite ICS(&I); | 
|  | 1755 | if (ICS.hasFnAttr(Attribute::NoRecurse)) | 
|  | 1756 | return true; | 
|  | 1757 |  | 
|  | 1758 | const auto &NoRecurseAA = | 
|  | 1759 | A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); | 
|  | 1760 | if (!NoRecurseAA.isAssumedNoRecurse()) | 
|  | 1761 | return false; | 
|  | 1762 |  | 
|  | 1763 | // Recursion to the same function | 
|  | 1764 | if (ICS.getCalledFunction() == getAnchorScope()) | 
|  | 1765 | return false; | 
|  | 1766 |  | 
|  | 1767 | return true; | 
|  | 1768 | }; | 
|  | 1769 |  | 
|  | 1770 | if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) | 
|  | 1771 | return indicatePessimisticFixpoint(); | 
|  | 1772 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1773 | } | 
|  | 1774 |  | 
|  | 1775 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } | 
|  | 1776 | }; | 
|  | 1777 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1778 | /// NoRecurse attribute deduction for a call sites. | 
|  | 1779 | struct AANoRecurseCallSite final : AANoRecurseImpl { | 
|  | 1780 | AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} | 
|  | 1781 |  | 
|  | 1782 | /// See AbstractAttribute::initialize(...). | 
|  | 1783 | void initialize(Attributor &A) override { | 
|  | 1784 | AANoRecurseImpl::initialize(A); | 
|  | 1785 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1786 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1787 | indicatePessimisticFixpoint(); | 
|  | 1788 | } | 
|  | 1789 |  | 
|  | 1790 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1791 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1792 | // TODO: Once we have call site specific value information we can provide | 
|  | 1793 | //       call site specific liveness information and then it makes | 
|  | 1794 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1795 | //       redirecting requests to the callee argument. | 
|  | 1796 | Function *F = getAssociatedFunction(); | 
|  | 1797 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1798 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); | 
|  | 1799 | return clampStateAndIndicateChange( | 
|  | 1800 | getState(), | 
|  | 1801 | static_cast<const AANoRecurse::StateType &>(FnAA.getState())); | 
|  | 1802 | } | 
|  | 1803 |  | 
|  | 1804 | /// See AbstractAttribute::trackStatistics() | 
|  | 1805 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } | 
|  | 1806 | }; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1807 |  | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1808 | /// ------------------------ Will-Return Attributes ---------------------------- | 
|  | 1809 |  | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1810 | // Helper function that checks whether a function has any cycle. | 
|  | 1811 | // TODO: Replace with more efficent code | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1812 | static bool containsCycle(Function &F) { | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1813 | SmallPtrSet<BasicBlock *, 32> Visited; | 
|  | 1814 |  | 
|  | 1815 | // Traverse BB by dfs and check whether successor is already visited. | 
|  | 1816 | for (BasicBlock *BB : depth_first(&F)) { | 
|  | 1817 | Visited.insert(BB); | 
|  | 1818 | for (auto *SuccBB : successors(BB)) { | 
|  | 1819 | if (Visited.count(SuccBB)) | 
|  | 1820 | return true; | 
|  | 1821 | } | 
|  | 1822 | } | 
|  | 1823 | return false; | 
|  | 1824 | } | 
|  | 1825 |  | 
|  | 1826 | // Helper function that checks the function have a loop which might become an | 
|  | 1827 | // endless loop | 
|  | 1828 | // FIXME: Any cycle is regarded as endless loop for now. | 
|  | 1829 | //        We have to allow some patterns. | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1830 | static bool containsPossiblyEndlessLoop(Function *F) { | 
|  | 1831 | return !F || !F->hasExactDefinition() || containsCycle(*F); | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1832 | } | 
|  | 1833 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1834 | struct AAWillReturnImpl : public AAWillReturn { | 
|  | 1835 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1836 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1837 | /// See AbstractAttribute::initialize(...). | 
|  | 1838 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1839 | AAWillReturn::initialize(A); | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1840 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1841 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1842 | if (containsPossiblyEndlessLoop(F)) | 
|  | 1843 | indicatePessimisticFixpoint(); | 
|  | 1844 | } | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1845 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1846 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1847 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1848 | auto CheckForWillReturn = [&](Instruction &I) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1849 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); | 
|  | 1850 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); | 
|  | 1851 | if (WillReturnAA.isKnownWillReturn()) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1852 | return true; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1853 | if (!WillReturnAA.isAssumedWillReturn()) | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1854 | return false; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1855 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); | 
|  | 1856 | return NoRecurseAA.isAssumedNoRecurse(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1857 | }; | 
|  | 1858 |  | 
|  | 1859 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) | 
|  | 1860 | return indicatePessimisticFixpoint(); | 
|  | 1861 |  | 
|  | 1862 | return ChangeStatus::UNCHANGED; | 
|  | 1863 | } | 
|  | 1864 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1865 | /// See AbstractAttribute::getAsStr() | 
|  | 1866 | const std::string getAsStr() const override { | 
|  | 1867 | return getAssumed() ? "willreturn" : "may-noreturn"; | 
|  | 1868 | } | 
|  | 1869 | }; | 
|  | 1870 |  | 
|  | 1871 | struct AAWillReturnFunction final : AAWillReturnImpl { | 
|  | 1872 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} | 
|  | 1873 |  | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1874 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1875 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1876 | }; | 
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1877 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1878 | /// WillReturn attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1879 | struct AAWillReturnCallSite final : AAWillReturnImpl { | 
|  | 1880 | AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} | 
|  | 1881 |  | 
|  | 1882 | /// See AbstractAttribute::initialize(...). | 
|  | 1883 | void initialize(Attributor &A) override { | 
|  | 1884 | AAWillReturnImpl::initialize(A); | 
|  | 1885 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1886 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1887 | indicatePessimisticFixpoint(); | 
|  | 1888 | } | 
|  | 1889 |  | 
|  | 1890 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1891 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1892 | // TODO: Once we have call site specific value information we can provide | 
|  | 1893 | //       call site specific liveness information and then it makes | 
|  | 1894 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 1895 | //       redirecting requests to the callee argument. | 
|  | 1896 | Function *F = getAssociatedFunction(); | 
|  | 1897 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 1898 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); | 
|  | 1899 | return clampStateAndIndicateChange( | 
|  | 1900 | getState(), | 
|  | 1901 | static_cast<const AAWillReturn::StateType &>(FnAA.getState())); | 
|  | 1902 | } | 
|  | 1903 |  | 
|  | 1904 | /// See AbstractAttribute::trackStatistics() | 
|  | 1905 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } | 
|  | 1906 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1907 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1908 | /// ------------------------ NoAlias Argument Attribute ------------------------ | 
|  | 1909 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1910 | struct AANoAliasImpl : AANoAlias { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1911 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1912 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1913 | const std::string getAsStr() const override { | 
|  | 1914 | return getAssumed() ? "noalias" : "may-alias"; | 
|  | 1915 | } | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1916 | }; | 
|  | 1917 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1918 | /// NoAlias attribute for a floating value. | 
|  | 1919 | struct AANoAliasFloating final : AANoAliasImpl { | 
|  | 1920 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 1921 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1922 | /// See AbstractAttribute::initialize(...). | 
|  | 1923 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1924 | AANoAliasImpl::initialize(A); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 1925 | Value &Val = getAssociatedValue(); | 
|  | 1926 | if (isa<AllocaInst>(Val)) | 
|  | 1927 | indicateOptimisticFixpoint(); | 
|  | 1928 | if (isa<ConstantPointerNull>(Val) && | 
|  | 1929 | Val.getType()->getPointerAddressSpace() == 0) | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1930 | indicateOptimisticFixpoint(); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1931 | } | 
|  | 1932 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1933 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1934 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 1935 | // TODO: Implement this. | 
|  | 1936 | return indicatePessimisticFixpoint(); | 
|  | 1937 | } | 
|  | 1938 |  | 
|  | 1939 | /// See AbstractAttribute::trackStatistics() | 
|  | 1940 | void trackStatistics() const override { | 
|  | 1941 | STATS_DECLTRACK_FLOATING_ATTR(noalias) | 
|  | 1942 | } | 
|  | 1943 | }; | 
|  | 1944 |  | 
|  | 1945 | /// NoAlias attribute for an argument. | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1946 | struct AANoAliasArgument final | 
|  | 1947 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { | 
|  | 1948 | AANoAliasArgument(const IRPosition &IRP) | 
|  | 1949 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {} | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1950 |  | 
|  | 1951 | /// See AbstractAttribute::trackStatistics() | 
|  | 1952 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } | 
|  | 1953 | }; | 
|  | 1954 |  | 
|  | 1955 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { | 
|  | 1956 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 1957 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1958 | /// See AbstractAttribute::initialize(...). | 
|  | 1959 | void initialize(Attributor &A) override { | 
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 1960 | // See callsite argument attribute and callee argument attribute. | 
|  | 1961 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 1962 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) | 
|  | 1963 | indicateOptimisticFixpoint(); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1964 | } | 
|  | 1965 |  | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1966 | /// See AbstractAttribute::updateImpl(...). | 
|  | 1967 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1968 | // We can deduce "noalias" if the following conditions hold. | 
|  | 1969 | // (i)   Associated value is assumed to be noalias in the definition. | 
|  | 1970 | // (ii)  Associated value is assumed to be no-capture in all the uses | 
|  | 1971 | //       possibly executed before this callsite. | 
|  | 1972 | // (iii) There is no other pointer argument which could alias with the | 
|  | 1973 | //       value. | 
|  | 1974 |  | 
|  | 1975 | const Value &V = getAssociatedValue(); | 
|  | 1976 | const IRPosition IRP = IRPosition::value(V); | 
|  | 1977 |  | 
|  | 1978 | // (i) Check whether noalias holds in the definition. | 
|  | 1979 |  | 
|  | 1980 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); | 
|  | 1981 |  | 
|  | 1982 | if (!NoAliasAA.isAssumedNoAlias()) | 
|  | 1983 | return indicatePessimisticFixpoint(); | 
|  | 1984 |  | 
|  | 1985 | LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V | 
|  | 1986 | << " is assumed NoAlias in the definition\n"); | 
|  | 1987 |  | 
|  | 1988 | // (ii) Check whether the value is captured in the scope using AANoCapture. | 
|  | 1989 | //      FIXME: This is conservative though, it is better to look at CFG and | 
|  | 1990 | //             check only uses possibly executed before this callsite. | 
|  | 1991 |  | 
|  | 1992 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 1993 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 1994 | LLVM_DEBUG( | 
|  | 1995 | dbgs() << "[Attributor][AANoAliasCSArg] " << V | 
|  | 1996 | << " cannot be noalias as it is potentially captured\n"); | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1997 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 1998 | } | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1999 |  | 
|  | 2000 | // (iii) Check there is no other pointer argument which could alias with the | 
|  | 2001 | // value. | 
|  | 2002 | ImmutableCallSite ICS(&getAnchorValue()); | 
|  | 2003 | for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) { | 
|  | 2004 | if (getArgNo() == (int)i) | 
|  | 2005 | continue; | 
|  | 2006 | const Value *ArgOp = ICS.getArgOperand(i); | 
|  | 2007 | if (!ArgOp->getType()->isPointerTy()) | 
|  | 2008 | continue; | 
|  | 2009 |  | 
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2010 | if (const Function *F = getAnchorScope()) { | 
|  | 2011 | if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) { | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2012 | bool IsAliasing = AAR->isNoAlias(&getAssociatedValue(), ArgOp); | 
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2013 | LLVM_DEBUG(dbgs() | 
|  | 2014 | << "[Attributor][NoAliasCSArg] Check alias between " | 
|  | 2015 | "callsite arguments " | 
|  | 2016 | << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " " | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2017 | << getAssociatedValue() << " " << *ArgOp << " => " | 
|  | 2018 | << (IsAliasing ? "" : "no-") << "alias \n"); | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2019 |  | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2020 | if (IsAliasing) | 
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2021 | continue; | 
|  | 2022 | } | 
|  | 2023 | } | 
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2024 | return indicatePessimisticFixpoint(); | 
|  | 2025 | } | 
|  | 2026 |  | 
|  | 2027 | return ChangeStatus::UNCHANGED; | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2028 | } | 
|  | 2029 |  | 
|  | 2030 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2031 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2032 | }; | 
|  | 2033 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2034 | /// NoAlias attribute for function return value. | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2035 | struct AANoAliasReturned final : AANoAliasImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2036 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2037 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2038 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2039 | virtual ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2040 |  | 
|  | 2041 | auto CheckReturnValue = [&](Value &RV) -> bool { | 
|  | 2042 | if (Constant *C = dyn_cast<Constant>(&RV)) | 
|  | 2043 | if (C->isNullValue() || isa<UndefValue>(C)) | 
|  | 2044 | return true; | 
|  | 2045 |  | 
|  | 2046 | /// For now, we can only deduce noalias if we have call sites. | 
|  | 2047 | /// FIXME: add more support. | 
|  | 2048 | ImmutableCallSite ICS(&RV); | 
|  | 2049 | if (!ICS) | 
|  | 2050 | return false; | 
|  | 2051 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2052 | const IRPosition &RVPos = IRPosition::value(RV); | 
|  | 2053 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2054 | if (!NoAliasAA.isAssumedNoAlias()) | 
|  | 2055 | return false; | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2056 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2057 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); | 
|  | 2058 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); | 
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2059 | }; | 
|  | 2060 |  | 
|  | 2061 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) | 
|  | 2062 | return indicatePessimisticFixpoint(); | 
|  | 2063 |  | 
|  | 2064 | return ChangeStatus::UNCHANGED; | 
|  | 2065 | } | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2066 |  | 
|  | 2067 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2068 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2069 | }; | 
|  | 2070 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2071 | /// NoAlias attribute deduction for a call site return value. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2072 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { | 
|  | 2073 | AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} | 
|  | 2074 |  | 
|  | 2075 | /// See AbstractAttribute::initialize(...). | 
|  | 2076 | void initialize(Attributor &A) override { | 
|  | 2077 | AANoAliasImpl::initialize(A); | 
|  | 2078 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2079 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2080 | indicatePessimisticFixpoint(); | 
|  | 2081 | } | 
|  | 2082 |  | 
|  | 2083 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2084 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2085 | // TODO: Once we have call site specific value information we can provide | 
|  | 2086 | //       call site specific liveness information and then it makes | 
|  | 2087 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2088 | //       redirecting requests to the callee argument. | 
|  | 2089 | Function *F = getAssociatedFunction(); | 
|  | 2090 | const IRPosition &FnPos = IRPosition::returned(*F); | 
|  | 2091 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); | 
|  | 2092 | return clampStateAndIndicateChange( | 
|  | 2093 | getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); | 
|  | 2094 | } | 
|  | 2095 |  | 
|  | 2096 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2097 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2098 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2099 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2100 | /// -------------------AAIsDead Function Attribute----------------------- | 
|  | 2101 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2102 | struct AAIsDeadValueImpl : public AAIsDead { | 
|  | 2103 | AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2104 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2105 | /// See AAIsDead::isAssumedDead(). | 
|  | 2106 | bool isAssumedDead() const override { return getAssumed(); } | 
|  | 2107 |  | 
|  | 2108 | /// See AAIsDead::isAssumedDead(BasicBlock *). | 
|  | 2109 | bool isAssumedDead(const BasicBlock *BB) const override { return false; } | 
|  | 2110 |  | 
|  | 2111 | /// See AAIsDead::isKnownDead(BasicBlock *). | 
|  | 2112 | bool isKnownDead(const BasicBlock *BB) const override { return false; } | 
|  | 2113 |  | 
|  | 2114 | /// See AAIsDead::isAssumedDead(Instruction *I). | 
|  | 2115 | bool isAssumedDead(const Instruction *I) const override { | 
|  | 2116 | return I == getCtxI() && isAssumedDead(); | 
|  | 2117 | } | 
|  | 2118 |  | 
|  | 2119 | /// See AAIsDead::isKnownDead(Instruction *I). | 
|  | 2120 | bool isKnownDead(const Instruction *I) const override { | 
|  | 2121 | return I == getCtxI() && getKnown(); | 
|  | 2122 | } | 
|  | 2123 |  | 
|  | 2124 | /// See AbstractAttribute::getAsStr(). | 
|  | 2125 | const std::string getAsStr() const override { | 
|  | 2126 | return isAssumedDead() ? "assumed-dead" : "assumed-live"; | 
|  | 2127 | } | 
|  | 2128 | }; | 
|  | 2129 |  | 
|  | 2130 | struct AAIsDeadFloating : public AAIsDeadValueImpl { | 
|  | 2131 | AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2132 |  | 
|  | 2133 | /// See AbstractAttribute::initialize(...). | 
|  | 2134 | void initialize(Attributor &A) override { | 
|  | 2135 | if (Instruction *I = dyn_cast<Instruction>(&getAssociatedValue())) | 
|  | 2136 | if (!wouldInstructionBeTriviallyDead(I)) | 
|  | 2137 | indicatePessimisticFixpoint(); | 
|  | 2138 | if (isa<UndefValue>(getAssociatedValue())) | 
|  | 2139 | indicatePessimisticFixpoint(); | 
|  | 2140 | } | 
|  | 2141 |  | 
|  | 2142 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2143 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2144 | auto UsePred = [&](const Use &U, bool &Follow) { | 
|  | 2145 | Instruction *UserI = cast<Instruction>(U.getUser()); | 
|  | 2146 | if (CallSite CS = CallSite(UserI)) { | 
|  | 2147 | if (!CS.isArgOperand(&U)) | 
|  | 2148 | return false; | 
|  | 2149 | const IRPosition &CSArgPos = | 
|  | 2150 | IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); | 
|  | 2151 | const auto &CSArgIsDead = A.getAAFor<AAIsDead>(*this, CSArgPos); | 
|  | 2152 | return CSArgIsDead.isAssumedDead(); | 
|  | 2153 | } | 
|  | 2154 | if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { | 
|  | 2155 | const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); | 
|  | 2156 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, RetPos); | 
|  | 2157 | return RetIsDeadAA.isAssumedDead(); | 
|  | 2158 | } | 
|  | 2159 | Follow = true; | 
|  | 2160 | return wouldInstructionBeTriviallyDead(UserI); | 
|  | 2161 | }; | 
|  | 2162 |  | 
|  | 2163 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) | 
|  | 2164 | return indicatePessimisticFixpoint(); | 
|  | 2165 | return ChangeStatus::UNCHANGED; | 
|  | 2166 | } | 
|  | 2167 |  | 
|  | 2168 | /// See AbstractAttribute::manifest(...). | 
|  | 2169 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2170 | Value &V = getAssociatedValue(); | 
|  | 2171 | if (auto *I = dyn_cast<Instruction>(&V)) | 
|  | 2172 | if (wouldInstructionBeTriviallyDead(I)) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2173 | A.deleteAfterManifest(*I); | 
|  | 2174 | return ChangeStatus::CHANGED; | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2175 | } | 
|  | 2176 |  | 
|  | 2177 | if (V.use_empty()) | 
|  | 2178 | return ChangeStatus::UNCHANGED; | 
|  | 2179 |  | 
|  | 2180 | UndefValue &UV = *UndefValue::get(V.getType()); | 
|  | 2181 | bool AnyChange = false; | 
|  | 2182 | for (Use &U : V.uses()) | 
|  | 2183 | AnyChange |= A.changeUseAfterManifest(U, UV); | 
|  | 2184 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
|  | 2185 | } | 
|  | 2186 |  | 
|  | 2187 | /// See AbstractAttribute::trackStatistics() | 
|  | 2188 | void trackStatistics() const override { | 
|  | 2189 | STATS_DECLTRACK_FLOATING_ATTR(IsDead) | 
|  | 2190 | } | 
|  | 2191 | }; | 
|  | 2192 |  | 
|  | 2193 | struct AAIsDeadArgument : public AAIsDeadFloating { | 
|  | 2194 | AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} | 
|  | 2195 |  | 
|  | 2196 | /// See AbstractAttribute::initialize(...). | 
|  | 2197 | void initialize(Attributor &A) override { | 
|  | 2198 | if (!getAssociatedFunction()->hasExactDefinition()) | 
|  | 2199 | indicatePessimisticFixpoint(); | 
|  | 2200 | } | 
|  | 2201 |  | 
|  | 2202 | /// See AbstractAttribute::trackStatistics() | 
|  | 2203 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } | 
|  | 2204 | }; | 
|  | 2205 |  | 
|  | 2206 | struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { | 
|  | 2207 | AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2208 |  | 
|  | 2209 | /// See AbstractAttribute::initialize(...). | 
|  | 2210 | void initialize(Attributor &A) override { | 
|  | 2211 | if (isa<UndefValue>(getAssociatedValue())) | 
|  | 2212 | indicatePessimisticFixpoint(); | 
|  | 2213 | } | 
|  | 2214 |  | 
|  | 2215 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2216 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2217 | // TODO: Once we have call site specific value information we can provide | 
|  | 2218 | //       call site specific liveness information and then it makes | 
|  | 2219 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 2220 | //       redirecting requests to the callee argument. | 
|  | 2221 | Argument *Arg = getAssociatedArgument(); | 
|  | 2222 | if (!Arg) | 
|  | 2223 | return indicatePessimisticFixpoint(); | 
|  | 2224 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 2225 | auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); | 
|  | 2226 | return clampStateAndIndicateChange( | 
|  | 2227 | getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); | 
|  | 2228 | } | 
|  | 2229 |  | 
|  | 2230 | /// See AbstractAttribute::manifest(...). | 
|  | 2231 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2232 | CallBase &CB = cast<CallBase>(getAnchorValue()); | 
|  | 2233 | Use &U = CB.getArgOperandUse(getArgNo()); | 
|  | 2234 | assert(!isa<UndefValue>(U.get()) && | 
|  | 2235 | "Expected undef values to be filtered out!"); | 
|  | 2236 | UndefValue &UV = *UndefValue::get(U->getType()); | 
|  | 2237 | if (A.changeUseAfterManifest(U, UV)) | 
|  | 2238 | return ChangeStatus::CHANGED; | 
|  | 2239 | return ChangeStatus::UNCHANGED; | 
|  | 2240 | } | 
|  | 2241 |  | 
|  | 2242 | /// See AbstractAttribute::trackStatistics() | 
|  | 2243 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } | 
|  | 2244 | }; | 
|  | 2245 |  | 
|  | 2246 | struct AAIsDeadReturned : public AAIsDeadValueImpl { | 
|  | 2247 | AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} | 
|  | 2248 |  | 
|  | 2249 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2250 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2251 |  | 
|  | 2252 | auto PredForCallSite = [&](AbstractCallSite ACS) { | 
|  | 2253 | if (ACS.isCallbackCall()) | 
|  | 2254 | return false; | 
|  | 2255 | const IRPosition &CSRetPos = | 
|  | 2256 | IRPosition::callsite_returned(ACS.getCallSite()); | 
|  | 2257 | const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, CSRetPos); | 
|  | 2258 | return RetIsDeadAA.isAssumedDead(); | 
|  | 2259 | }; | 
|  | 2260 |  | 
|  | 2261 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) | 
|  | 2262 | return indicatePessimisticFixpoint(); | 
|  | 2263 |  | 
|  | 2264 | return ChangeStatus::UNCHANGED; | 
|  | 2265 | } | 
|  | 2266 |  | 
|  | 2267 | /// See AbstractAttribute::manifest(...). | 
|  | 2268 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2269 | // TODO: Rewrite the signature to return void? | 
|  | 2270 | bool AnyChange = false; | 
|  | 2271 | UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); | 
|  | 2272 | auto RetInstPred = [&](Instruction &I) { | 
|  | 2273 | ReturnInst &RI = cast<ReturnInst>(I); | 
|  | 2274 | if (!isa<UndefValue>(RI.getReturnValue())) | 
|  | 2275 | AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); | 
|  | 2276 | return true; | 
|  | 2277 | }; | 
|  | 2278 | A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); | 
|  | 2279 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | 
|  | 2280 | } | 
|  | 2281 |  | 
|  | 2282 | /// See AbstractAttribute::trackStatistics() | 
|  | 2283 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } | 
|  | 2284 | }; | 
|  | 2285 |  | 
|  | 2286 | struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { | 
|  | 2287 | AAIsDeadCallSiteReturned(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} | 
|  | 2288 |  | 
|  | 2289 | /// See AbstractAttribute::initialize(...). | 
|  | 2290 | void initialize(Attributor &A) override {} | 
|  | 2291 |  | 
|  | 2292 | /// See AbstractAttribute::trackStatistics() | 
|  | 2293 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(IsDead) } | 
|  | 2294 | }; | 
|  | 2295 |  | 
|  | 2296 | struct AAIsDeadFunction : public AAIsDead { | 
|  | 2297 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} | 
|  | 2298 |  | 
|  | 2299 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2300 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2301 | const Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2302 | if (F && !F->isDeclaration()) { | 
|  | 2303 | ToBeExploredFrom.insert(&F->getEntryBlock().front()); | 
|  | 2304 | assumeLive(A, F->getEntryBlock()); | 
|  | 2305 | } | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2306 | } | 
|  | 2307 |  | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2308 | /// See AbstractAttribute::getAsStr(). | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2309 | const std::string getAsStr() const override { | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2310 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2311 | std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + | 
|  | 2312 | std::to_string(ToBeExploredFrom.size()) + "]"; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2313 | } | 
|  | 2314 |  | 
|  | 2315 | /// See AbstractAttribute::manifest(...). | 
|  | 2316 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2317 | assert(getState().isValidState() && | 
|  | 2318 | "Attempted to manifest an invalid state!"); | 
|  | 2319 |  | 
|  | 2320 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2321 | Function &F = *getAssociatedFunction(); | 
|  | 2322 |  | 
|  | 2323 | if (AssumedLiveBlocks.empty()) { | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2324 | A.deleteAfterManifest(F); | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2325 | return ChangeStatus::CHANGED; | 
|  | 2326 | } | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2327 |  | 
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2328 | // Flag to determine if we can change an invoke to a call assuming the | 
|  | 2329 | // callee is nounwind. This is not possible if the personality of the | 
|  | 2330 | // function allows to catch asynchronous exceptions. | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2331 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2332 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2333 | for (const Instruction *ExploreEndI : ToBeExploredFrom) { | 
|  | 2334 | auto *CB = dyn_cast<CallBase>(ExploreEndI); | 
|  | 2335 | if (!CB) | 
|  | 2336 | continue; | 
|  | 2337 | const auto &NoReturnAA = | 
|  | 2338 | A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); | 
|  | 2339 | if (!NoReturnAA.isAssumedNoReturn()) | 
|  | 2340 | continue; | 
|  | 2341 | Instruction *I = const_cast<Instruction *>(ExploreEndI); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2342 | BasicBlock *BB = I->getParent(); | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2343 | Instruction *SplitPos = I->getNextNode(); | 
| Johannes Doerfert | d410805 | 2019-08-21 20:56:41 +0000 | [diff] [blame] | 2344 | // TODO: mark stuff before unreachable instructions as dead. | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2345 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2346 | if (auto *II = dyn_cast<InvokeInst>(I)) { | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2347 | // If we keep the invoke the split position is at the beginning of the | 
|  | 2348 | // normal desitination block (it invokes a noreturn function after all). | 
|  | 2349 | BasicBlock *NormalDestBB = II->getNormalDest(); | 
|  | 2350 | SplitPos = &NormalDestBB->front(); | 
|  | 2351 |  | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2352 | /// Invoke is replaced with a call and unreachable is placed after it if | 
|  | 2353 | /// the callee is nounwind and noreturn. Otherwise, we keep the invoke | 
|  | 2354 | /// and only place an unreachable in the normal successor. | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2355 | if (Invoke2CallAllowed) { | 
| Michael Liao | a99086d | 2019-08-20 21:02:31 +0000 | [diff] [blame] | 2356 | if (II->getCalledFunction()) { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2357 | const IRPosition &IPos = IRPosition::callsite_function(*II); | 
|  | 2358 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); | 
|  | 2359 | if (AANoUnw.isAssumedNoUnwind()) { | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2360 | LLVM_DEBUG(dbgs() | 
|  | 2361 | << "[AAIsDead] Replace invoke with call inst\n"); | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2362 | // We do not need an invoke (II) but instead want a call followed | 
|  | 2363 | // by an unreachable. However, we do not remove II as other | 
|  | 2364 | // abstract attributes might have it cached as part of their | 
|  | 2365 | // results. Given that we modify the CFG anyway, we simply keep II | 
|  | 2366 | // around but in a new dead block. To avoid II being live through | 
|  | 2367 | // a different edge we have to ensure the block we place it in is | 
|  | 2368 | // only reached from the current block of II and then not reached | 
|  | 2369 | // at all when we insert the unreachable. | 
|  | 2370 | SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); | 
|  | 2371 | CallInst *CI = createCallMatchingInvoke(II); | 
|  | 2372 | CI->insertBefore(II); | 
|  | 2373 | CI->takeName(II); | 
|  | 2374 | II->replaceAllUsesWith(CI); | 
|  | 2375 | SplitPos = CI->getNextNode(); | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2376 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2377 | } | 
|  | 2378 | } | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2379 |  | 
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2380 | if (SplitPos == &NormalDestBB->front()) { | 
|  | 2381 | // If this is an invoke of a noreturn function the edge to the normal | 
|  | 2382 | // destination block is dead but not necessarily the block itself. | 
|  | 2383 | // TODO: We need to move to an edge based system during deduction and | 
|  | 2384 | //       also manifest. | 
|  | 2385 | assert(!NormalDestBB->isLandingPad() && | 
|  | 2386 | "Expected the normal destination not to be a landingpad!"); | 
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2387 | if (NormalDestBB->getUniquePredecessor() == BB) { | 
|  | 2388 | assumeLive(A, *NormalDestBB); | 
|  | 2389 | } else { | 
|  | 2390 | BasicBlock *SplitBB = | 
|  | 2391 | SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); | 
|  | 2392 | // The split block is live even if it contains only an unreachable | 
|  | 2393 | // instruction at the end. | 
|  | 2394 | assumeLive(A, *SplitBB); | 
|  | 2395 | SplitPos = SplitBB->getTerminator(); | 
|  | 2396 | HasChanged = ChangeStatus::CHANGED; | 
|  | 2397 | } | 
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2398 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2399 | } | 
|  | 2400 |  | 
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2401 | if (isa_and_nonnull<UnreachableInst>(SplitPos)) | 
|  | 2402 | continue; | 
|  | 2403 |  | 
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2404 | BB = SplitPos->getParent(); | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2405 | SplitBlock(BB, SplitPos); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2406 | changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false); | 
|  | 2407 | HasChanged = ChangeStatus::CHANGED; | 
|  | 2408 | } | 
|  | 2409 |  | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2410 | for (BasicBlock &BB : F) | 
|  | 2411 | if (!AssumedLiveBlocks.count(&BB)) | 
|  | 2412 | A.deleteAfterManifest(BB); | 
|  | 2413 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2414 | return HasChanged; | 
|  | 2415 | } | 
|  | 2416 |  | 
|  | 2417 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2418 | ChangeStatus updateImpl(Attributor &A) override; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2419 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2420 | /// See AbstractAttribute::trackStatistics() | 
|  | 2421 | void trackStatistics() const override {} | 
|  | 2422 |  | 
|  | 2423 | /// Returns true if the function is assumed dead. | 
|  | 2424 | bool isAssumedDead() const override { return false; } | 
|  | 2425 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2426 | /// See AAIsDead::isAssumedDead(BasicBlock *). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2427 | bool isAssumedDead(const BasicBlock *BB) const override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2428 | assert(BB->getParent() == getAssociatedFunction() && | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2429 | "BB must be in the same anchor scope function."); | 
|  | 2430 |  | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2431 | if (!getAssumed()) | 
|  | 2432 | return false; | 
|  | 2433 | return !AssumedLiveBlocks.count(BB); | 
|  | 2434 | } | 
|  | 2435 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2436 | /// See AAIsDead::isKnownDead(BasicBlock *). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2437 | bool isKnownDead(const BasicBlock *BB) const override { | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2438 | return getKnown() && isAssumedDead(BB); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2439 | } | 
|  | 2440 |  | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2441 | /// See AAIsDead::isAssumed(Instruction *I). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2442 | bool isAssumedDead(const Instruction *I) const override { | 
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2443 | assert(I->getParent()->getParent() == getAssociatedFunction() && | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2444 | "Instruction must be in the same anchor scope function."); | 
|  | 2445 |  | 
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 2446 | if (!getAssumed()) | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2447 | return false; | 
|  | 2448 |  | 
|  | 2449 | // If it is not in AssumedLiveBlocks then it for sure dead. | 
|  | 2450 | // Otherwise, it can still be after noreturn call in a live block. | 
|  | 2451 | if (!AssumedLiveBlocks.count(I->getParent())) | 
|  | 2452 | return true; | 
|  | 2453 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2454 | // If it is not after a liveness barrier it is live. | 
|  | 2455 | const Instruction *PrevI = I->getPrevNode(); | 
|  | 2456 | while (PrevI) { | 
|  | 2457 | if (ToBeExploredFrom.count(PrevI)) | 
|  | 2458 | return true; | 
|  | 2459 | PrevI = PrevI->getPrevNode(); | 
|  | 2460 | } | 
|  | 2461 | return false; | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2462 | } | 
|  | 2463 |  | 
|  | 2464 | /// See AAIsDead::isKnownDead(Instruction *I). | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2465 | bool isKnownDead(const Instruction *I) const override { | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2466 | return getKnown() && isAssumedDead(I); | 
|  | 2467 | } | 
|  | 2468 |  | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2469 | /// Determine if \p F might catch asynchronous exceptions. | 
|  | 2470 | static bool mayCatchAsynchronousExceptions(const Function &F) { | 
|  | 2471 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); | 
|  | 2472 | } | 
|  | 2473 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2474 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A | 
|  | 2475 | /// that internal function called from \p BB should now be looked at. | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2476 | bool assumeLive(Attributor &A, const BasicBlock &BB) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2477 | if (!AssumedLiveBlocks.insert(&BB).second) | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2478 | return false; | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2479 |  | 
|  | 2480 | // We assume that all of BB is (probably) live now and if there are calls to | 
|  | 2481 | // internal functions we will assume that those are now live as well. This | 
|  | 2482 | // is a performance optimization for blocks with calls to a lot of internal | 
|  | 2483 | // functions. It can however cause dead functions to be treated as live. | 
|  | 2484 | for (const Instruction &I : BB) | 
|  | 2485 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) | 
|  | 2486 | if (const Function *F = ICS.getCalledFunction()) | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 2487 | if (F->hasLocalLinkage()) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2488 | A.markLiveInternalFunction(*F); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2489 | return true; | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2490 | } | 
|  | 2491 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2492 | /// Collection of instructions that need to be explored again, e.g., we | 
|  | 2493 | /// did assume they do not transfer control to (one of their) successors. | 
|  | 2494 | SmallSetVector<const Instruction *, 8> ToBeExploredFrom; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2495 |  | 
|  | 2496 | /// Collection of all assumed live BasicBlocks. | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2497 | DenseSet<const BasicBlock *> AssumedLiveBlocks; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2498 | }; | 
|  | 2499 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2500 | static bool | 
|  | 2501 | identifyAliveSuccessors(Attributor &A, const CallBase &CB, | 
|  | 2502 | AbstractAttribute &AA, | 
|  | 2503 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 2504 | const IRPosition &IPos = IRPosition::callsite_function(CB); | 
|  | 2505 |  | 
|  | 2506 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); | 
|  | 2507 | if (NoReturnAA.isAssumedNoReturn()) | 
|  | 2508 | return true; | 
|  | 2509 | if (CB.isTerminator()) | 
|  | 2510 | AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); | 
|  | 2511 | else | 
|  | 2512 | AliveSuccessors.push_back(CB.getNextNode()); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2513 | return false; | 
|  | 2514 | } | 
|  | 2515 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2516 | static bool | 
|  | 2517 | identifyAliveSuccessors(Attributor &A, const InvokeInst &II, | 
|  | 2518 | AbstractAttribute &AA, | 
|  | 2519 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 2520 | bool UsedAssumedInformation = | 
|  | 2521 | identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); | 
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2522 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2523 | // First, determine if we can change an invoke to a call assuming the | 
|  | 2524 | // callee is nounwind. This is not possible if the personality of the | 
|  | 2525 | // function allows to catch asynchronous exceptions. | 
|  | 2526 | if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { | 
|  | 2527 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | 
|  | 2528 | } else { | 
|  | 2529 | const IRPosition &IPos = IRPosition::callsite_function(II); | 
|  | 2530 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); | 
|  | 2531 | if (!AANoUnw.isAssumedNoUnwind()) { | 
|  | 2532 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | 
|  | 2533 | UsedAssumedInformation = true; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2534 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2535 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2536 | return UsedAssumedInformation; | 
|  | 2537 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2538 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2539 | static Optional<ConstantInt *> getAssumedConstant(Attributor &A, const Value &V, | 
|  | 2540 | AbstractAttribute &AA) { | 
|  | 2541 | const auto &ValueSimplifyAA = | 
|  | 2542 | A.getAAFor<AAValueSimplify>(AA, IRPosition::value(V)); | 
|  | 2543 | Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); | 
|  | 2544 | if (!SimplifiedV.hasValue()) | 
|  | 2545 | return llvm::None; | 
|  | 2546 | if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) | 
|  | 2547 | return llvm::None; | 
|  | 2548 | return dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue()); | 
|  | 2549 | } | 
|  | 2550 |  | 
|  | 2551 | static bool | 
|  | 2552 | identifyAliveSuccessors(Attributor &A, const BranchInst &BI, | 
|  | 2553 | AbstractAttribute &AA, | 
|  | 2554 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
|  | 2555 | bool UsedAssumedInformation = false; | 
|  | 2556 | if (BI.getNumSuccessors() == 1) { | 
|  | 2557 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | 
|  | 2558 | } else { | 
|  | 2559 | Optional<ConstantInt *> CI = getAssumedConstant(A, *BI.getCondition(), AA); | 
|  | 2560 | if (!CI.hasValue()) { | 
|  | 2561 | // No value yet, assume both edges are dead. | 
|  | 2562 | } else if (CI.getValue()) { | 
|  | 2563 | const BasicBlock *SuccBB = | 
|  | 2564 | BI.getSuccessor(1 - CI.getValue()->getZExtValue()); | 
|  | 2565 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 2566 | UsedAssumedInformation = true; | 
|  | 2567 | } else { | 
|  | 2568 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | 
|  | 2569 | AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); | 
|  | 2570 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2571 | } | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2572 | return UsedAssumedInformation; | 
|  | 2573 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2574 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2575 | static bool | 
|  | 2576 | identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, | 
|  | 2577 | AbstractAttribute &AA, | 
|  | 2578 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | 
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 2579 | Optional<ConstantInt *> CI = getAssumedConstant(A, *SI.getCondition(), AA); | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2580 | if (!CI.hasValue()) { | 
|  | 2581 | // No value yet, assume all edges are dead. | 
|  | 2582 | } else if (CI.getValue()) { | 
|  | 2583 | for (auto &CaseIt : SI.cases()) { | 
|  | 2584 | if (CaseIt.getCaseValue() == CI.getValue()) { | 
|  | 2585 | AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); | 
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 2586 | return true; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2587 | } | 
|  | 2588 | } | 
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 2589 | AliveSuccessors.push_back(&SI.getDefaultDest()->front()); | 
|  | 2590 | return true; | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2591 | } else { | 
|  | 2592 | for (const BasicBlock *SuccBB : successors(SI.getParent())) | 
|  | 2593 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 2594 | } | 
| Johannes Doerfert | ed47a9c | 2019-11-01 13:57:49 -0500 | [diff] [blame] | 2595 | return false; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2596 | } | 
|  | 2597 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2598 | ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2599 | ChangeStatus Change = ChangeStatus::UNCHANGED; | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2600 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2601 | LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" | 
|  | 2602 | << getAssociatedFunction()->size() << "] BBs and " | 
|  | 2603 | << ToBeExploredFrom.size() << " exploration points\n"); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2604 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2605 | // Copy and clear the list of instructions we need to explore from. It is | 
|  | 2606 | // refilled with instructions the next update has to look at. | 
|  | 2607 | SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), | 
|  | 2608 | ToBeExploredFrom.end()); | 
|  | 2609 | decltype(ToBeExploredFrom) NewToBeExploredFrom; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2610 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2611 | SmallVector<const Instruction *, 8> AliveSuccessors; | 
|  | 2612 | while (!Worklist.empty()) { | 
|  | 2613 | const Instruction *I = Worklist.pop_back_val(); | 
|  | 2614 | LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2615 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2616 | AliveSuccessors.clear(); | 
|  | 2617 |  | 
|  | 2618 | bool UsedAssumedInformation = false; | 
|  | 2619 | switch (I->getOpcode()) { | 
|  | 2620 | // TODO: look for (assumed) UB to backwards propagate "deadness". | 
|  | 2621 | default: | 
|  | 2622 | if (I->isTerminator()) { | 
|  | 2623 | for (const BasicBlock *SuccBB : successors(I->getParent())) | 
|  | 2624 | AliveSuccessors.push_back(&SuccBB->front()); | 
|  | 2625 | } else { | 
|  | 2626 | AliveSuccessors.push_back(I->getNextNode()); | 
|  | 2627 | } | 
|  | 2628 | break; | 
|  | 2629 | case Instruction::Call: | 
|  | 2630 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), | 
|  | 2631 | *this, AliveSuccessors); | 
|  | 2632 | break; | 
|  | 2633 | case Instruction::Invoke: | 
|  | 2634 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), | 
|  | 2635 | *this, AliveSuccessors); | 
|  | 2636 | break; | 
|  | 2637 | case Instruction::Br: | 
|  | 2638 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), | 
|  | 2639 | *this, AliveSuccessors); | 
|  | 2640 | break; | 
|  | 2641 | case Instruction::Switch: | 
|  | 2642 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), | 
|  | 2643 | *this, AliveSuccessors); | 
|  | 2644 | break; | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2645 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2646 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2647 | if (UsedAssumedInformation) | 
|  | 2648 | NewToBeExploredFrom.insert(I); | 
|  | 2649 | else | 
|  | 2650 | Change = ChangeStatus::CHANGED; | 
|  | 2651 |  | 
|  | 2652 | LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " | 
|  | 2653 | << AliveSuccessors.size() << " UsedAssumedInformation: " | 
|  | 2654 | << UsedAssumedInformation << "\n"); | 
|  | 2655 |  | 
|  | 2656 | for (const Instruction *AliveSuccessor : AliveSuccessors) { | 
|  | 2657 | if (!I->isTerminator()) { | 
|  | 2658 | assert(AliveSuccessors.size() == 1 && | 
|  | 2659 | "Non-terminator expected to have a single successor!"); | 
|  | 2660 | Worklist.push_back(AliveSuccessor); | 
|  | 2661 | } else { | 
|  | 2662 | if (assumeLive(A, *AliveSuccessor->getParent())) | 
|  | 2663 | Worklist.push_back(AliveSuccessor); | 
|  | 2664 | } | 
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2665 | } | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2666 | } | 
|  | 2667 |  | 
| Johannes Doerfert | dac2d40 | 2019-10-29 11:47:47 -0500 | [diff] [blame] | 2668 | ToBeExploredFrom = std::move(NewToBeExploredFrom); | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2669 |  | 
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 2670 | // 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] | 2671 | // Instead, indicating a pessimistic fixpoint will cause the state to be | 
|  | 2672 | // "invalid" and all queries to be answered conservatively. | 
|  | 2673 | if (ToBeExploredFrom.empty() && | 
|  | 2674 | getAssociatedFunction()->size() == AssumedLiveBlocks.size()) | 
|  | 2675 | return indicatePessimisticFixpoint(); | 
|  | 2676 | return Change; | 
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2677 | } | 
|  | 2678 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2679 | /// Liveness information for a call sites. | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 2680 | struct AAIsDeadCallSite final : AAIsDeadFunction { | 
|  | 2681 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2682 |  | 
|  | 2683 | /// See AbstractAttribute::initialize(...). | 
|  | 2684 | void initialize(Attributor &A) override { | 
|  | 2685 | // TODO: Once we have call site specific value information we can provide | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2686 | //       call site specific liveness information and then it makes | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2687 | //       sense to specialize attributes for call sites instead of | 
|  | 2688 | //       redirecting requests to the callee. | 
|  | 2689 | llvm_unreachable("Abstract attributes for liveness are not " | 
|  | 2690 | "supported for call sites yet!"); | 
|  | 2691 | } | 
|  | 2692 |  | 
|  | 2693 | /// See AbstractAttribute::updateImpl(...). | 
|  | 2694 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2695 | return indicatePessimisticFixpoint(); | 
|  | 2696 | } | 
|  | 2697 |  | 
|  | 2698 | /// See AbstractAttribute::trackStatistics() | 
|  | 2699 | void trackStatistics() const override {} | 
|  | 2700 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2701 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2702 | /// -------------------- Dereferenceable Argument Attribute -------------------- | 
|  | 2703 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2704 | template <> | 
|  | 2705 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, | 
|  | 2706 | const DerefState &R) { | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 2707 | ChangeStatus CS0 = | 
|  | 2708 | clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); | 
|  | 2709 | ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2710 | return CS0 | CS1; | 
|  | 2711 | } | 
|  | 2712 |  | 
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 2713 | struct AADereferenceableImpl : AADereferenceable { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2714 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2715 | using StateType = DerefState; | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2716 |  | 
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 2717 | void initialize(Attributor &A) override { | 
|  | 2718 | SmallVector<Attribute, 4> Attrs; | 
|  | 2719 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, | 
|  | 2720 | Attrs); | 
|  | 2721 | for (const Attribute &Attr : Attrs) | 
|  | 2722 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); | 
|  | 2723 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2724 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition()); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2725 |  | 
|  | 2726 | const IRPosition &IRP = this->getIRPosition(); | 
|  | 2727 | bool IsFnInterface = IRP.isFnInterfaceKind(); | 
|  | 2728 | const Function *FnScope = IRP.getAnchorScope(); | 
|  | 2729 | if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) | 
|  | 2730 | indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 2731 | } | 
|  | 2732 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2733 | /// See AbstractAttribute::getState() | 
|  | 2734 | /// { | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2735 | StateType &getState() override { return *this; } | 
|  | 2736 | const StateType &getState() const override { return *this; } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2737 | /// } | 
|  | 2738 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2739 | /// See AAFromMustBeExecutedContext | 
|  | 2740 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { | 
|  | 2741 | bool IsNonNull = false; | 
|  | 2742 | bool TrackUse = false; | 
|  | 2743 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( | 
|  | 2744 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); | 
|  | 2745 | takeKnownDerefBytesMaximum(DerefBytes); | 
|  | 2746 | return TrackUse; | 
|  | 2747 | } | 
|  | 2748 |  | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 2749 | void getDeducedAttributes(LLVMContext &Ctx, | 
|  | 2750 | SmallVectorImpl<Attribute> &Attrs) const override { | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2751 | // TODO: Add *_globally support | 
|  | 2752 | if (isAssumedNonNull()) | 
|  | 2753 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( | 
|  | 2754 | Ctx, getAssumedDereferenceableBytes())); | 
|  | 2755 | else | 
|  | 2756 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( | 
|  | 2757 | Ctx, getAssumedDereferenceableBytes())); | 
|  | 2758 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2759 |  | 
|  | 2760 | /// See AbstractAttribute::getAsStr(). | 
|  | 2761 | const std::string getAsStr() const override { | 
|  | 2762 | if (!getAssumedDereferenceableBytes()) | 
|  | 2763 | return "unknown-dereferenceable"; | 
|  | 2764 | return std::string("dereferenceable") + | 
|  | 2765 | (isAssumedNonNull() ? "" : "_or_null") + | 
|  | 2766 | (isAssumedGlobal() ? "_globally" : "") + "<" + | 
|  | 2767 | std::to_string(getKnownDereferenceableBytes()) + "-" + | 
|  | 2768 | std::to_string(getAssumedDereferenceableBytes()) + ">"; | 
|  | 2769 | } | 
|  | 2770 | }; | 
|  | 2771 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2772 | /// Dereferenceable attribute for a floating value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2773 | struct AADereferenceableFloating | 
|  | 2774 | : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { | 
|  | 2775 | using Base = | 
|  | 2776 | AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; | 
|  | 2777 | AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2778 |  | 
|  | 2779 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2780 | ChangeStatus updateImpl(Attributor &A) override { | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2781 | ChangeStatus Change = Base::updateImpl(A); | 
|  | 2782 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2783 | const DataLayout &DL = A.getDataLayout(); | 
|  | 2784 |  | 
|  | 2785 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { | 
|  | 2786 | unsigned IdxWidth = | 
|  | 2787 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); | 
|  | 2788 | APInt Offset(IdxWidth, 0); | 
|  | 2789 | const Value *Base = | 
|  | 2790 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); | 
|  | 2791 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2792 | const auto &AA = | 
|  | 2793 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2794 | int64_t DerefBytes = 0; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2795 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2796 | // Use IR information if we did not strip anything. | 
|  | 2797 | // TODO: track globally. | 
|  | 2798 | bool CanBeNull; | 
|  | 2799 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); | 
|  | 2800 | T.GlobalState.indicatePessimisticFixpoint(); | 
|  | 2801 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2802 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2803 | DerefBytes = DS.DerefBytesState.getAssumed(); | 
|  | 2804 | T.GlobalState &= DS.GlobalState; | 
|  | 2805 | } | 
|  | 2806 |  | 
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 2807 | // For now we do not try to "increase" dereferenceability due to negative | 
|  | 2808 | // indices as we first have to come up with code to deal with loops and | 
|  | 2809 | // for overflows of the dereferenceable bytes. | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2810 | int64_t OffsetSExt = Offset.getSExtValue(); | 
|  | 2811 | if (OffsetSExt < 0) | 
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 2812 | OffsetSExt = 0; | 
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 2813 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2814 | T.takeAssumedDerefBytesMinimum( | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2815 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2816 |  | 
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2817 | if (this == &AA) { | 
|  | 2818 | if (!Stripped) { | 
|  | 2819 | // If nothing was stripped IR information is all we got. | 
|  | 2820 | T.takeKnownDerefBytesMaximum( | 
|  | 2821 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | 
|  | 2822 | T.indicatePessimisticFixpoint(); | 
|  | 2823 | } else if (OffsetSExt > 0) { | 
|  | 2824 | // If something was stripped but there is circular reasoning we look | 
|  | 2825 | // for the offset. If it is positive we basically decrease the | 
|  | 2826 | // dereferenceable bytes in a circluar loop now, which will simply | 
|  | 2827 | // drive them down to the known value in a very slow way which we | 
|  | 2828 | // can accelerate. | 
|  | 2829 | T.indicatePessimisticFixpoint(); | 
|  | 2830 | } | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2831 | } | 
|  | 2832 |  | 
|  | 2833 | return T.isValidState(); | 
|  | 2834 | }; | 
|  | 2835 |  | 
|  | 2836 | DerefState T; | 
|  | 2837 | if (!genericValueTraversal<AADereferenceable, DerefState>( | 
|  | 2838 | A, getIRPosition(), *this, T, VisitValueCB)) | 
|  | 2839 | return indicatePessimisticFixpoint(); | 
|  | 2840 |  | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2841 | return Change | clampStateAndIndicateChange(getState(), T); | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2842 | } | 
|  | 2843 |  | 
|  | 2844 | /// See AbstractAttribute::trackStatistics() | 
|  | 2845 | void trackStatistics() const override { | 
|  | 2846 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) | 
|  | 2847 | } | 
|  | 2848 | }; | 
|  | 2849 |  | 
|  | 2850 | /// Dereferenceable attribute for a return value. | 
|  | 2851 | struct AADereferenceableReturned final | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2852 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, | 
|  | 2853 | DerefState> { | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2854 | AADereferenceableReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2855 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, | 
|  | 2856 | DerefState>(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2857 |  | 
|  | 2858 | /// See AbstractAttribute::trackStatistics() | 
|  | 2859 | void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2860 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2861 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2862 | }; | 
|  | 2863 |  | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2864 | /// Dereferenceable attribute for an argument | 
|  | 2865 | struct AADereferenceableArgument final | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2866 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< | 
|  | 2867 | AADereferenceable, AADereferenceableImpl, DerefState> { | 
|  | 2868 | using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< | 
|  | 2869 | AADereferenceable, AADereferenceableImpl, DerefState>; | 
|  | 2870 | AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2871 |  | 
|  | 2872 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2873 | void trackStatistics() const override { | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 2874 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) | 
|  | 2875 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2876 | }; | 
|  | 2877 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2878 | /// Dereferenceable attribute for a call site argument. | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2879 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2880 | AADereferenceableCallSiteArgument(const IRPosition &IRP) | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2881 | : AADereferenceableFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2882 |  | 
|  | 2883 | /// See AbstractAttribute::trackStatistics() | 
|  | 2884 | void trackStatistics() const override { | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2885 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2886 | } | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2887 | }; | 
|  | 2888 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2889 | /// Dereferenceable attribute deduction for a call site return value. | 
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2890 | struct AADereferenceableCallSiteReturned final | 
|  | 2891 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< | 
|  | 2892 | AADereferenceable, AADereferenceableImpl> { | 
|  | 2893 | using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< | 
|  | 2894 | AADereferenceable, AADereferenceableImpl>; | 
|  | 2895 | AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2896 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2897 | /// See AbstractAttribute::trackStatistics() | 
|  | 2898 | void trackStatistics() const override { | 
|  | 2899 | STATS_DECLTRACK_CS_ATTR(dereferenceable); | 
|  | 2900 | } | 
|  | 2901 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2902 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2903 | // ------------------------ Align Argument Attribute ------------------------ | 
|  | 2904 |  | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2905 | struct AAAlignImpl : AAAlign { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2906 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2907 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2908 | /// See AbstractAttribute::initialize(...). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2909 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2910 | SmallVector<Attribute, 4> Attrs; | 
|  | 2911 | getAttrs({Attribute::Alignment}, Attrs); | 
|  | 2912 | for (const Attribute &Attr : Attrs) | 
|  | 2913 | takeKnownMaximum(Attr.getValueAsInt()); | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 2914 |  | 
|  | 2915 | if (getIRPosition().isFnInterfaceKind() && | 
|  | 2916 | (!getAssociatedFunction() || | 
|  | 2917 | !getAssociatedFunction()->hasExactDefinition())) | 
|  | 2918 | indicatePessimisticFixpoint(); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2919 | } | 
|  | 2920 |  | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2921 | /// See AbstractAttribute::manifest(...). | 
|  | 2922 | ChangeStatus manifest(Attributor &A) override { | 
|  | 2923 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 2924 |  | 
|  | 2925 | // Check for users that allow alignment annotations. | 
|  | 2926 | Value &AnchorVal = getIRPosition().getAnchorValue(); | 
|  | 2927 | for (const Use &U : AnchorVal.uses()) { | 
|  | 2928 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { | 
|  | 2929 | if (SI->getPointerOperand() == &AnchorVal) | 
|  | 2930 | if (SI->getAlignment() < getAssumedAlign()) { | 
|  | 2931 | STATS_DECLTRACK(AAAlign, Store, | 
|  | 2932 | "Number of times alignemnt added to a store"); | 
| Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 2933 | SI->setAlignment(Align(getAssumedAlign())); | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2934 | Changed = ChangeStatus::CHANGED; | 
|  | 2935 | } | 
|  | 2936 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { | 
|  | 2937 | if (LI->getPointerOperand() == &AnchorVal) | 
|  | 2938 | if (LI->getAlignment() < getAssumedAlign()) { | 
| Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 2939 | LI->setAlignment(Align(getAssumedAlign())); | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2940 | STATS_DECLTRACK(AAAlign, Load, | 
|  | 2941 | "Number of times alignemnt added to a load"); | 
|  | 2942 | Changed = ChangeStatus::CHANGED; | 
|  | 2943 | } | 
|  | 2944 | } | 
|  | 2945 | } | 
|  | 2946 |  | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 2947 | return AAAlign::manifest(A) | Changed; | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2948 | } | 
|  | 2949 |  | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 2950 | // TODO: Provide a helper to determine the implied ABI alignment and check in | 
|  | 2951 | //       the existing manifest method and a new one for AAAlignImpl that value | 
|  | 2952 | //       to avoid making the alignment explicit if it did not improve. | 
|  | 2953 |  | 
|  | 2954 | /// See AbstractAttribute::getDeducedAttributes | 
|  | 2955 | virtual void | 
|  | 2956 | getDeducedAttributes(LLVMContext &Ctx, | 
|  | 2957 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 2958 | if (getAssumedAlign() > 1) | 
| Guillaume Chatelet | b65fa48 | 2019-10-15 12:56:24 +0000 | [diff] [blame] | 2959 | Attrs.emplace_back( | 
|  | 2960 | Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); | 
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 2961 | } | 
|  | 2962 |  | 
|  | 2963 | /// See AbstractAttribute::getAsStr(). | 
|  | 2964 | const std::string getAsStr() const override { | 
|  | 2965 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + | 
|  | 2966 | "-" + std::to_string(getAssumedAlign()) + ">") | 
|  | 2967 | : "unknown-align"; | 
|  | 2968 | } | 
|  | 2969 | }; | 
|  | 2970 |  | 
|  | 2971 | /// Align attribute for a floating value. | 
|  | 2972 | struct AAAlignFloating : AAAlignImpl { | 
|  | 2973 | AAAlignFloating(const IRPosition &IRP) : AAAlignImpl(IRP) {} | 
|  | 2974 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2975 | /// See AbstractAttribute::updateImpl(...). | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2976 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 2977 | const DataLayout &DL = A.getDataLayout(); | 
|  | 2978 |  | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2979 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, | 
|  | 2980 | bool Stripped) -> bool { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2981 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); | 
|  | 2982 | if (!Stripped && this == &AA) { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2983 | // Use only IR information if we did not strip anything. | 
| Guillaume Chatelet | bae629b | 2019-10-15 13:58:22 +0000 | [diff] [blame] | 2984 | const MaybeAlign PA = V.getPointerAlignment(DL); | 
|  | 2985 | T.takeKnownMaximum(PA ? PA->value() : 0); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2986 | T.indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2987 | } else { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2988 | // Use abstract attribute information. | 
|  | 2989 | const AAAlign::StateType &DS = | 
|  | 2990 | static_cast<const AAAlign::StateType &>(AA.getState()); | 
|  | 2991 | T ^= DS; | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2992 | } | 
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2993 | return T.isValidState(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2994 | }; | 
|  | 2995 |  | 
|  | 2996 | StateType T; | 
|  | 2997 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, | 
|  | 2998 | VisitValueCB)) | 
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2999 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3000 |  | 
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 3001 | // TODO: If we know we visited all incoming values, thus no are assumed | 
|  | 3002 | // dead, we can take the known information from the state T. | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3003 | return clampStateAndIndicateChange(getState(), T); | 
|  | 3004 | } | 
|  | 3005 |  | 
|  | 3006 | /// See AbstractAttribute::trackStatistics() | 
|  | 3007 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } | 
|  | 3008 | }; | 
|  | 3009 |  | 
|  | 3010 | /// Align attribute for function return value. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3011 | struct AAAlignReturned final | 
|  | 3012 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3013 | AAAlignReturned(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3014 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3015 |  | 
|  | 3016 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3017 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3018 | }; | 
|  | 3019 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3020 | /// Align attribute for function argument. | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3021 | struct AAAlignArgument final | 
|  | 3022 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> { | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3023 | AAAlignArgument(const IRPosition &IRP) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3024 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3025 |  | 
|  | 3026 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 3027 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3028 | }; | 
|  | 3029 |  | 
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 3030 | struct AAAlignCallSiteArgument final : AAAlignFloating { | 
|  | 3031 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3032 |  | 
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 3033 | /// See AbstractAttribute::manifest(...). | 
|  | 3034 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3035 | return AAAlignImpl::manifest(A); | 
|  | 3036 | } | 
|  | 3037 |  | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3038 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3039 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 3040 | }; | 
|  | 3041 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3042 | /// Align attribute deduction for a call site return value. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3043 | struct AAAlignCallSiteReturned final : AAAlignImpl { | 
|  | 3044 | AAAlignCallSiteReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {} | 
|  | 3045 |  | 
|  | 3046 | /// See AbstractAttribute::initialize(...). | 
|  | 3047 | void initialize(Attributor &A) override { | 
|  | 3048 | AAAlignImpl::initialize(A); | 
|  | 3049 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3050 | if (!F) | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3051 | indicatePessimisticFixpoint(); | 
|  | 3052 | } | 
|  | 3053 |  | 
|  | 3054 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3055 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3056 | // TODO: Once we have call site specific value information we can provide | 
|  | 3057 | //       call site specific liveness information and then it makes | 
|  | 3058 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 3059 | //       redirecting requests to the callee argument. | 
|  | 3060 | Function *F = getAssociatedFunction(); | 
|  | 3061 | const IRPosition &FnPos = IRPosition::returned(*F); | 
|  | 3062 | auto &FnAA = A.getAAFor<AAAlign>(*this, FnPos); | 
|  | 3063 | return clampStateAndIndicateChange( | 
|  | 3064 | getState(), static_cast<const AAAlign::StateType &>(FnAA.getState())); | 
|  | 3065 | } | 
|  | 3066 |  | 
|  | 3067 | /// See AbstractAttribute::trackStatistics() | 
|  | 3068 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } | 
|  | 3069 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3070 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3071 | /// ------------------ Function No-Return Attribute ---------------------------- | 
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 3072 | struct AANoReturnImpl : public AANoReturn { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3073 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3074 |  | 
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3075 | /// See AbstractAttribute::initialize(...). | 
|  | 3076 | void initialize(Attributor &A) override { | 
|  | 3077 | AANoReturn::initialize(A); | 
|  | 3078 | Function *F = getAssociatedFunction(); | 
| Johannes Doerfert | 1b6041a | 2019-11-01 20:17:48 -0500 | [diff] [blame^] | 3079 | if (!F) | 
| Johannes Doerfert | 0cc2b61 | 2019-10-13 21:25:53 +0000 | [diff] [blame] | 3080 | indicatePessimisticFixpoint(); | 
|  | 3081 | } | 
|  | 3082 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3083 | /// See AbstractAttribute::getAsStr(). | 
|  | 3084 | const std::string getAsStr() const override { | 
|  | 3085 | return getAssumed() ? "noreturn" : "may-return"; | 
|  | 3086 | } | 
|  | 3087 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3088 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3089 | virtual ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3090 | auto CheckForNoReturn = [](Instruction &) { return false; }; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3091 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 3092 | {(unsigned)Instruction::Ret})) | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3093 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 3094 | return ChangeStatus::UNCHANGED; | 
|  | 3095 | } | 
|  | 3096 | }; | 
|  | 3097 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3098 | struct AANoReturnFunction final : AANoReturnImpl { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 3099 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 3100 |  | 
|  | 3101 | /// See AbstractAttribute::trackStatistics() | 
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 3102 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 3103 | }; | 
|  | 3104 |  | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3105 | /// NoReturn attribute deduction for a call sites. | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3106 | struct AANoReturnCallSite final : AANoReturnImpl { | 
|  | 3107 | AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} | 
|  | 3108 |  | 
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 3109 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3110 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3111 | // TODO: Once we have call site specific value information we can provide | 
|  | 3112 | //       call site specific liveness information and then it makes | 
|  | 3113 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 3114 | //       redirecting requests to the callee argument. | 
|  | 3115 | Function *F = getAssociatedFunction(); | 
|  | 3116 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 3117 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); | 
|  | 3118 | return clampStateAndIndicateChange( | 
|  | 3119 | getState(), | 
|  | 3120 | static_cast<const AANoReturn::StateType &>(FnAA.getState())); | 
|  | 3121 | } | 
|  | 3122 |  | 
|  | 3123 | /// See AbstractAttribute::trackStatistics() | 
|  | 3124 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } | 
|  | 3125 | }; | 
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 3126 |  | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3127 | /// ----------------------- Variable Capturing --------------------------------- | 
|  | 3128 |  | 
|  | 3129 | /// A class to hold the state of for no-capture attributes. | 
|  | 3130 | struct AANoCaptureImpl : public AANoCapture { | 
|  | 3131 | AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} | 
|  | 3132 |  | 
|  | 3133 | /// See AbstractAttribute::initialize(...). | 
|  | 3134 | void initialize(Attributor &A) override { | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3135 | if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { | 
|  | 3136 | indicateOptimisticFixpoint(); | 
|  | 3137 | return; | 
|  | 3138 | } | 
|  | 3139 | Function *AnchorScope = getAnchorScope(); | 
|  | 3140 | if (isFnInterfaceKind() && | 
|  | 3141 | (!AnchorScope || !AnchorScope->hasExactDefinition())) { | 
|  | 3142 | indicatePessimisticFixpoint(); | 
|  | 3143 | return; | 
|  | 3144 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3145 |  | 
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 3146 | // You cannot "capture" null in the default address space. | 
|  | 3147 | if (isa<ConstantPointerNull>(getAssociatedValue()) && | 
|  | 3148 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { | 
|  | 3149 | indicateOptimisticFixpoint(); | 
|  | 3150 | return; | 
|  | 3151 | } | 
|  | 3152 |  | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3153 | const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3154 |  | 
|  | 3155 | // Check what state the associated function can actually capture. | 
|  | 3156 | if (F) | 
| Johannes Doerfert | 0437bfc | 2019-10-31 20:03:13 -0500 | [diff] [blame] | 3157 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 3158 | else | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3159 | indicatePessimisticFixpoint(); | 
|  | 3160 | } | 
|  | 3161 |  | 
|  | 3162 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3163 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 3164 |  | 
|  | 3165 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). | 
|  | 3166 | virtual void | 
|  | 3167 | getDeducedAttributes(LLVMContext &Ctx, | 
|  | 3168 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 3169 | if (!isAssumedNoCaptureMaybeReturned()) | 
|  | 3170 | return; | 
|  | 3171 |  | 
| Hideto Ueno | 3736764 | 2019-09-11 06:52:11 +0000 | [diff] [blame] | 3172 | if (getArgNo() >= 0) { | 
|  | 3173 | if (isAssumedNoCapture()) | 
|  | 3174 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); | 
|  | 3175 | else if (ManifestInternal) | 
|  | 3176 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); | 
|  | 3177 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3178 | } | 
|  | 3179 |  | 
|  | 3180 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known | 
|  | 3181 | /// depending on the ability of the function associated with \p IRP to capture | 
|  | 3182 | /// state in memory and through "returning/throwing", respectively. | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3183 | static void determineFunctionCaptureCapabilities(const IRPosition &IRP, | 
|  | 3184 | const Function &F, | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 3185 | BitIntegerState &State) { | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3186 | // TODO: Once we have memory behavior attributes we should use them here. | 
|  | 3187 |  | 
|  | 3188 | // If we know we cannot communicate or write to memory, we do not care about | 
|  | 3189 | // ptr2int anymore. | 
|  | 3190 | if (F.onlyReadsMemory() && F.doesNotThrow() && | 
|  | 3191 | F.getReturnType()->isVoidTy()) { | 
|  | 3192 | State.addKnownBits(NO_CAPTURE); | 
|  | 3193 | return; | 
|  | 3194 | } | 
|  | 3195 |  | 
|  | 3196 | // A function cannot capture state in memory if it only reads memory, it can | 
|  | 3197 | // however return/throw state and the state might be influenced by the | 
|  | 3198 | // pointer value, e.g., loading from a returned pointer might reveal a bit. | 
|  | 3199 | if (F.onlyReadsMemory()) | 
|  | 3200 | State.addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 3201 |  | 
|  | 3202 | // A function cannot communicate state back if it does not through | 
|  | 3203 | // exceptions and doesn not return values. | 
|  | 3204 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) | 
|  | 3205 | State.addKnownBits(NOT_CAPTURED_IN_RET); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3206 |  | 
|  | 3207 | // Check existing "returned" attributes. | 
|  | 3208 | int ArgNo = IRP.getArgNo(); | 
|  | 3209 | if (F.doesNotThrow() && ArgNo >= 0) { | 
|  | 3210 | for (unsigned u = 0, e = F.arg_size(); u< e; ++u) | 
|  | 3211 | if (F.hasParamAttribute(u, Attribute::Returned)) { | 
| Johannes Doerfert | 9d5ad5e | 2019-10-21 01:29:10 +0000 | [diff] [blame] | 3212 | if (u == unsigned(ArgNo)) | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3213 | State.removeAssumedBits(NOT_CAPTURED_IN_RET); | 
|  | 3214 | else if (F.onlyReadsMemory()) | 
|  | 3215 | State.addKnownBits(NO_CAPTURE); | 
|  | 3216 | else | 
|  | 3217 | State.addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 3218 | break; | 
|  | 3219 | } | 
|  | 3220 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3221 | } | 
|  | 3222 |  | 
|  | 3223 | /// See AbstractState::getAsStr(). | 
|  | 3224 | const std::string getAsStr() const override { | 
|  | 3225 | if (isKnownNoCapture()) | 
|  | 3226 | return "known not-captured"; | 
|  | 3227 | if (isAssumedNoCapture()) | 
|  | 3228 | return "assumed not-captured"; | 
|  | 3229 | if (isKnownNoCaptureMaybeReturned()) | 
|  | 3230 | return "known not-captured-maybe-returned"; | 
|  | 3231 | if (isAssumedNoCaptureMaybeReturned()) | 
|  | 3232 | return "assumed not-captured-maybe-returned"; | 
|  | 3233 | return "assumed-captured"; | 
|  | 3234 | } | 
|  | 3235 | }; | 
|  | 3236 |  | 
|  | 3237 | /// Attributor-aware capture tracker. | 
|  | 3238 | struct AACaptureUseTracker final : public CaptureTracker { | 
|  | 3239 |  | 
|  | 3240 | /// Create a capture tracker that can lookup in-flight abstract attributes | 
|  | 3241 | /// through the Attributor \p A. | 
|  | 3242 | /// | 
|  | 3243 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the | 
|  | 3244 | /// search is stopped. If a use leads to a return instruction, | 
|  | 3245 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. | 
|  | 3246 | /// If a use leads to a ptr2int which may capture the value, | 
|  | 3247 | /// \p CapturedInInteger is set. If a use is found that is currently assumed | 
|  | 3248 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies | 
|  | 3249 | /// set. All values in \p PotentialCopies are later tracked as well. For every | 
|  | 3250 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, | 
|  | 3251 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger | 
|  | 3252 | /// conservatively set to true. | 
|  | 3253 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3254 | const AAIsDead &IsDeadAA, AANoCapture::StateType &State, | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3255 | SmallVectorImpl<const Value *> &PotentialCopies, | 
|  | 3256 | unsigned &RemainingUsesToExplore) | 
|  | 3257 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), | 
|  | 3258 | PotentialCopies(PotentialCopies), | 
|  | 3259 | RemainingUsesToExplore(RemainingUsesToExplore) {} | 
|  | 3260 |  | 
|  | 3261 | /// Determine if \p V maybe captured. *Also updates the state!* | 
|  | 3262 | bool valueMayBeCaptured(const Value *V) { | 
|  | 3263 | if (V->getType()->isPointerTy()) { | 
|  | 3264 | PointerMayBeCaptured(V, this); | 
|  | 3265 | } else { | 
|  | 3266 | State.indicatePessimisticFixpoint(); | 
|  | 3267 | } | 
|  | 3268 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | 
|  | 3269 | } | 
|  | 3270 |  | 
|  | 3271 | /// See CaptureTracker::tooManyUses(). | 
|  | 3272 | void tooManyUses() override { | 
|  | 3273 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); | 
|  | 3274 | } | 
|  | 3275 |  | 
|  | 3276 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { | 
|  | 3277 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) | 
|  | 3278 | return true; | 
|  | 3279 | const auto &DerefAA = | 
|  | 3280 | A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O)); | 
|  | 3281 | return DerefAA.getAssumedDereferenceableBytes(); | 
|  | 3282 | } | 
|  | 3283 |  | 
|  | 3284 | /// See CaptureTracker::captured(...). | 
|  | 3285 | bool captured(const Use *U) override { | 
|  | 3286 | Instruction *UInst = cast<Instruction>(U->getUser()); | 
|  | 3287 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst | 
|  | 3288 | << "\n"); | 
|  | 3289 |  | 
|  | 3290 | // Because we may reuse the tracker multiple times we keep track of the | 
|  | 3291 | // number of explored uses ourselves as well. | 
|  | 3292 | if (RemainingUsesToExplore-- == 0) { | 
|  | 3293 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); | 
|  | 3294 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 3295 | /* Return */ true); | 
|  | 3296 | } | 
|  | 3297 |  | 
|  | 3298 | // Deal with ptr2int by following uses. | 
|  | 3299 | if (isa<PtrToIntInst>(UInst)) { | 
|  | 3300 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); | 
|  | 3301 | return valueMayBeCaptured(UInst); | 
|  | 3302 | } | 
|  | 3303 |  | 
|  | 3304 | // Explicitly catch return instructions. | 
|  | 3305 | if (isa<ReturnInst>(UInst)) | 
|  | 3306 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 3307 | /* Return */ true); | 
|  | 3308 |  | 
|  | 3309 | // For now we only use special logic for call sites. However, the tracker | 
|  | 3310 | // itself knows about a lot of other non-capturing cases already. | 
|  | 3311 | CallSite CS(UInst); | 
|  | 3312 | if (!CS || !CS.isArgOperand(U)) | 
|  | 3313 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 3314 | /* Return */ true); | 
|  | 3315 |  | 
|  | 3316 | unsigned ArgNo = CS.getArgumentNo(U); | 
|  | 3317 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); | 
|  | 3318 | // If we have a abstract no-capture attribute for the argument we can use | 
|  | 3319 | // it to justify a non-capture attribute here. This allows recursion! | 
|  | 3320 | auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); | 
|  | 3321 | if (ArgNoCaptureAA.isAssumedNoCapture()) | 
|  | 3322 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 3323 | /* Return */ false); | 
|  | 3324 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 3325 | addPotentialCopy(CS); | 
|  | 3326 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | 
|  | 3327 | /* Return */ false); | 
|  | 3328 | } | 
|  | 3329 |  | 
|  | 3330 | // Lastly, we could not find a reason no-capture can be assumed so we don't. | 
|  | 3331 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | 
|  | 3332 | /* Return */ true); | 
|  | 3333 | } | 
|  | 3334 |  | 
|  | 3335 | /// Register \p CS as potential copy of the value we are checking. | 
|  | 3336 | void addPotentialCopy(CallSite CS) { | 
|  | 3337 | PotentialCopies.push_back(CS.getInstruction()); | 
|  | 3338 | } | 
|  | 3339 |  | 
|  | 3340 | /// See CaptureTracker::shouldExplore(...). | 
|  | 3341 | bool shouldExplore(const Use *U) override { | 
|  | 3342 | // Check liveness. | 
|  | 3343 | return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser())); | 
|  | 3344 | } | 
|  | 3345 |  | 
|  | 3346 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and | 
|  | 3347 | /// \p CapturedInRet, then return the appropriate value for use in the | 
|  | 3348 | /// CaptureTracker::captured() interface. | 
|  | 3349 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, | 
|  | 3350 | bool CapturedInRet) { | 
|  | 3351 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " | 
|  | 3352 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); | 
|  | 3353 | if (CapturedInMem) | 
|  | 3354 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); | 
|  | 3355 | if (CapturedInInt) | 
|  | 3356 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); | 
|  | 3357 | if (CapturedInRet) | 
|  | 3358 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); | 
|  | 3359 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | 
|  | 3360 | } | 
|  | 3361 |  | 
|  | 3362 | private: | 
|  | 3363 | /// The attributor providing in-flight abstract attributes. | 
|  | 3364 | Attributor &A; | 
|  | 3365 |  | 
|  | 3366 | /// The abstract attribute currently updated. | 
|  | 3367 | AANoCapture &NoCaptureAA; | 
|  | 3368 |  | 
|  | 3369 | /// The abstract liveness state. | 
|  | 3370 | const AAIsDead &IsDeadAA; | 
|  | 3371 |  | 
|  | 3372 | /// The state currently updated. | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3373 | AANoCapture::StateType &State; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3374 |  | 
|  | 3375 | /// Set of potential copies of the tracked value. | 
|  | 3376 | SmallVectorImpl<const Value *> &PotentialCopies; | 
|  | 3377 |  | 
|  | 3378 | /// Global counter to limit the number of explored uses. | 
|  | 3379 | unsigned &RemainingUsesToExplore; | 
|  | 3380 | }; | 
|  | 3381 |  | 
|  | 3382 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { | 
|  | 3383 | const IRPosition &IRP = getIRPosition(); | 
|  | 3384 | const Value *V = | 
|  | 3385 | getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); | 
|  | 3386 | if (!V) | 
|  | 3387 | return indicatePessimisticFixpoint(); | 
|  | 3388 |  | 
|  | 3389 | const Function *F = | 
|  | 3390 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); | 
|  | 3391 | assert(F && "Expected a function!"); | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3392 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 3393 | const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3394 |  | 
|  | 3395 | AANoCapture::StateType T; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3396 |  | 
| Johannes Doerfert | 3839b57 | 2019-10-21 00:48:42 +0000 | [diff] [blame] | 3397 | // Readonly means we cannot capture through memory. | 
|  | 3398 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 3399 | if (FnMemAA.isAssumedReadOnly()) { | 
|  | 3400 | T.addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 3401 | if (FnMemAA.isKnownReadOnly()) | 
|  | 3402 | addKnownBits(NOT_CAPTURED_IN_MEM); | 
|  | 3403 | } | 
|  | 3404 |  | 
|  | 3405 | // Make sure all returned values are different than the underlying value. | 
|  | 3406 | // TODO: we could do this in a more sophisticated way inside | 
|  | 3407 | //       AAReturnedValues, e.g., track all values that escape through returns | 
|  | 3408 | //       directly somehow. | 
|  | 3409 | auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { | 
|  | 3410 | bool SeenConstant = false; | 
|  | 3411 | for (auto &It : RVAA.returned_values()) { | 
|  | 3412 | if (isa<Constant>(It.first)) { | 
|  | 3413 | if (SeenConstant) | 
|  | 3414 | return false; | 
|  | 3415 | SeenConstant = true; | 
|  | 3416 | } else if (!isa<Argument>(It.first) || | 
|  | 3417 | It.first == getAssociatedArgument()) | 
|  | 3418 | return false; | 
|  | 3419 | } | 
|  | 3420 | return true; | 
|  | 3421 | }; | 
|  | 3422 |  | 
|  | 3423 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, FnPos); | 
|  | 3424 | if (NoUnwindAA.isAssumedNoUnwind()) { | 
|  | 3425 | bool IsVoidTy = F->getReturnType()->isVoidTy(); | 
|  | 3426 | const AAReturnedValues *RVAA = | 
|  | 3427 | IsVoidTy ? nullptr : &A.getAAFor<AAReturnedValues>(*this, FnPos); | 
|  | 3428 | if (IsVoidTy || CheckReturnedArgs(*RVAA)) { | 
|  | 3429 | T.addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 3430 | if (T.isKnown(NOT_CAPTURED_IN_MEM)) | 
|  | 3431 | return ChangeStatus::UNCHANGED; | 
|  | 3432 | if (NoUnwindAA.isKnownNoUnwind() && | 
|  | 3433 | (IsVoidTy || RVAA->getState().isAtFixpoint())) { | 
|  | 3434 | addKnownBits(NOT_CAPTURED_IN_RET); | 
|  | 3435 | if (isKnown(NOT_CAPTURED_IN_MEM)) | 
|  | 3436 | return indicateOptimisticFixpoint(); | 
|  | 3437 | } | 
|  | 3438 | } | 
|  | 3439 | } | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3440 |  | 
|  | 3441 | // Use the CaptureTracker interface and logic with the specialized tracker, | 
|  | 3442 | // defined in AACaptureUseTracker, that can look at in-flight abstract | 
|  | 3443 | // attributes and directly updates the assumed state. | 
|  | 3444 | SmallVector<const Value *, 4> PotentialCopies; | 
|  | 3445 | unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; | 
|  | 3446 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, | 
|  | 3447 | RemainingUsesToExplore); | 
|  | 3448 |  | 
|  | 3449 | // Check all potential copies of the associated value until we can assume | 
|  | 3450 | // none will be captured or we have to assume at least one might be. | 
|  | 3451 | unsigned Idx = 0; | 
|  | 3452 | PotentialCopies.push_back(V); | 
|  | 3453 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) | 
|  | 3454 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); | 
|  | 3455 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 3456 | AANoCapture::StateType &S = getState(); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3457 | auto Assumed = S.getAssumed(); | 
|  | 3458 | S.intersectAssumedBits(T.getAssumed()); | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 3459 | if (!isAssumedNoCaptureMaybeReturned()) | 
|  | 3460 | return indicatePessimisticFixpoint(); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 3461 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | 
|  | 3462 | : ChangeStatus::CHANGED; | 
|  | 3463 | } | 
|  | 3464 |  | 
|  | 3465 | /// NoCapture attribute for function arguments. | 
|  | 3466 | struct AANoCaptureArgument final : AANoCaptureImpl { | 
|  | 3467 | AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 3468 |  | 
|  | 3469 | /// See AbstractAttribute::trackStatistics() | 
|  | 3470 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } | 
|  | 3471 | }; | 
|  | 3472 |  | 
|  | 3473 | /// NoCapture attribute for call site arguments. | 
|  | 3474 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { | 
|  | 3475 | AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 3476 |  | 
|  | 3477 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3478 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3479 | // TODO: Once we have call site specific value information we can provide | 
|  | 3480 | //       call site specific liveness information and then it makes | 
|  | 3481 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 3482 | //       redirecting requests to the callee argument. | 
|  | 3483 | Argument *Arg = getAssociatedArgument(); | 
|  | 3484 | if (!Arg) | 
|  | 3485 | return indicatePessimisticFixpoint(); | 
|  | 3486 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 3487 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); | 
|  | 3488 | return clampStateAndIndicateChange( | 
|  | 3489 | getState(), | 
|  | 3490 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); | 
|  | 3491 | } | 
|  | 3492 |  | 
|  | 3493 | /// See AbstractAttribute::trackStatistics() | 
|  | 3494 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; | 
|  | 3495 | }; | 
|  | 3496 |  | 
|  | 3497 | /// NoCapture attribute for floating values. | 
|  | 3498 | struct AANoCaptureFloating final : AANoCaptureImpl { | 
|  | 3499 | AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 3500 |  | 
|  | 3501 | /// See AbstractAttribute::trackStatistics() | 
|  | 3502 | void trackStatistics() const override { | 
|  | 3503 | STATS_DECLTRACK_FLOATING_ATTR(nocapture) | 
|  | 3504 | } | 
|  | 3505 | }; | 
|  | 3506 |  | 
|  | 3507 | /// NoCapture attribute for function return value. | 
|  | 3508 | struct AANoCaptureReturned final : AANoCaptureImpl { | 
|  | 3509 | AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { | 
|  | 3510 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 3511 | } | 
|  | 3512 |  | 
|  | 3513 | /// See AbstractAttribute::initialize(...). | 
|  | 3514 | void initialize(Attributor &A) override { | 
|  | 3515 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 3516 | } | 
|  | 3517 |  | 
|  | 3518 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3519 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3520 | llvm_unreachable("NoCapture is not applicable to function returns!"); | 
|  | 3521 | } | 
|  | 3522 |  | 
|  | 3523 | /// See AbstractAttribute::trackStatistics() | 
|  | 3524 | void trackStatistics() const override {} | 
|  | 3525 | }; | 
|  | 3526 |  | 
|  | 3527 | /// NoCapture attribute deduction for a call site return value. | 
|  | 3528 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { | 
|  | 3529 | AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} | 
|  | 3530 |  | 
|  | 3531 | /// See AbstractAttribute::trackStatistics() | 
|  | 3532 | void trackStatistics() const override { | 
|  | 3533 | STATS_DECLTRACK_CSRET_ATTR(nocapture) | 
|  | 3534 | } | 
|  | 3535 | }; | 
|  | 3536 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 3537 | /// ------------------ Value Simplify Attribute ---------------------------- | 
|  | 3538 | struct AAValueSimplifyImpl : AAValueSimplify { | 
|  | 3539 | AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} | 
|  | 3540 |  | 
|  | 3541 | /// See AbstractAttribute::getAsStr(). | 
|  | 3542 | const std::string getAsStr() const override { | 
|  | 3543 | return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") | 
|  | 3544 | : "not-simple"; | 
|  | 3545 | } | 
|  | 3546 |  | 
|  | 3547 | /// See AbstractAttribute::trackStatistics() | 
|  | 3548 | void trackStatistics() const override {} | 
|  | 3549 |  | 
|  | 3550 | /// See AAValueSimplify::getAssumedSimplifiedValue() | 
|  | 3551 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { | 
|  | 3552 | if (!getAssumed()) | 
|  | 3553 | return const_cast<Value *>(&getAssociatedValue()); | 
|  | 3554 | return SimplifiedAssociatedValue; | 
|  | 3555 | } | 
|  | 3556 | void initialize(Attributor &A) override {} | 
|  | 3557 |  | 
|  | 3558 | /// Helper function for querying AAValueSimplify and updating candicate. | 
|  | 3559 | /// \param QueryingValue Value trying to unify with SimplifiedValue | 
|  | 3560 | /// \param AccumulatedSimplifiedValue Current simplification result. | 
|  | 3561 | static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, | 
|  | 3562 | Value &QueryingValue, | 
|  | 3563 | Optional<Value *> &AccumulatedSimplifiedValue) { | 
|  | 3564 | // FIXME: Add a typecast support. | 
|  | 3565 |  | 
|  | 3566 | auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>( | 
|  | 3567 | QueryingAA, IRPosition::value(QueryingValue)); | 
|  | 3568 |  | 
|  | 3569 | Optional<Value *> QueryingValueSimplified = | 
|  | 3570 | ValueSimpifyAA.getAssumedSimplifiedValue(A); | 
|  | 3571 |  | 
|  | 3572 | if (!QueryingValueSimplified.hasValue()) | 
|  | 3573 | return true; | 
|  | 3574 |  | 
|  | 3575 | if (!QueryingValueSimplified.getValue()) | 
|  | 3576 | return false; | 
|  | 3577 |  | 
|  | 3578 | Value &QueryingValueSimplifiedUnwrapped = | 
|  | 3579 | *QueryingValueSimplified.getValue(); | 
|  | 3580 |  | 
|  | 3581 | if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) | 
|  | 3582 | return true; | 
|  | 3583 |  | 
|  | 3584 | if (AccumulatedSimplifiedValue.hasValue()) | 
|  | 3585 | return AccumulatedSimplifiedValue == QueryingValueSimplified; | 
|  | 3586 |  | 
|  | 3587 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue | 
|  | 3588 | << " is assumed to be " | 
|  | 3589 | << QueryingValueSimplifiedUnwrapped << "\n"); | 
|  | 3590 |  | 
|  | 3591 | AccumulatedSimplifiedValue = QueryingValueSimplified; | 
|  | 3592 | return true; | 
|  | 3593 | } | 
|  | 3594 |  | 
|  | 3595 | /// See AbstractAttribute::manifest(...). | 
|  | 3596 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3597 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | 
|  | 3598 |  | 
|  | 3599 | if (!SimplifiedAssociatedValue.hasValue() || | 
|  | 3600 | !SimplifiedAssociatedValue.getValue()) | 
|  | 3601 | return Changed; | 
|  | 3602 |  | 
|  | 3603 | if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) { | 
|  | 3604 | // We can replace the AssociatedValue with the constant. | 
|  | 3605 | Value &V = getAssociatedValue(); | 
|  | 3606 | if (!V.user_empty() && &V != C && V.getType() == C->getType()) { | 
|  | 3607 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C | 
|  | 3608 | << "\n"); | 
|  | 3609 | V.replaceAllUsesWith(C); | 
|  | 3610 | Changed = ChangeStatus::CHANGED; | 
|  | 3611 | } | 
|  | 3612 | } | 
|  | 3613 |  | 
|  | 3614 | return Changed | AAValueSimplify::manifest(A); | 
|  | 3615 | } | 
|  | 3616 |  | 
|  | 3617 | protected: | 
|  | 3618 | // An assumed simplified value. Initially, it is set to Optional::None, which | 
|  | 3619 | // means that the value is not clear under current assumption. If in the | 
|  | 3620 | // pessimistic state, getAssumedSimplifiedValue doesn't return this value but | 
|  | 3621 | // returns orignal associated value. | 
|  | 3622 | Optional<Value *> SimplifiedAssociatedValue; | 
|  | 3623 | }; | 
|  | 3624 |  | 
|  | 3625 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { | 
|  | 3626 | AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 3627 |  | 
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 3628 | void initialize(Attributor &A) override { | 
|  | 3629 | AAValueSimplifyImpl::initialize(A); | 
|  | 3630 | if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) | 
|  | 3631 | indicatePessimisticFixpoint(); | 
|  | 3632 | if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, | 
|  | 3633 | /* IgnoreSubsumingPositions */ true)) | 
|  | 3634 | indicatePessimisticFixpoint(); | 
|  | 3635 | } | 
|  | 3636 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 3637 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3638 | ChangeStatus updateImpl(Attributor &A) override { | 
| Johannes Doerfert | 15cd90a | 2019-11-01 13:42:54 -0500 | [diff] [blame] | 3639 | // Byval is only replacable if it is readonly otherwise we would write into | 
|  | 3640 | // the replaced value and not the copy that byval creates implicitly. | 
|  | 3641 | Argument *Arg = getAssociatedArgument(); | 
|  | 3642 | if (Arg->hasByValAttr()) { | 
|  | 3643 | const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); | 
|  | 3644 | if (!MemAA.isAssumedReadOnly()) | 
|  | 3645 | return indicatePessimisticFixpoint(); | 
|  | 3646 | } | 
|  | 3647 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 3648 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 3649 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 3650 | auto PredForCallSite = [&](AbstractCallSite ACS) { | 
|  | 3651 | // Check if we have an associated argument or not (which can happen for | 
|  | 3652 | // callback calls). | 
| Johannes Doerfert | e360ee6 | 2019-11-01 18:45:25 -0500 | [diff] [blame] | 3653 | Value *ArgOp = ACS.getCallArgOperand(getArgNo()); | 
|  | 3654 | if (!ArgOp) | 
|  | 3655 | return false; | 
|  | 3656 | // We can only propagate thread independent values through callbacks. | 
|  | 3657 | // This is different to direct/indirect call sites because for them we | 
|  | 3658 | // know the thread executing the caller and callee is the same. For | 
|  | 3659 | // callbacks this is not guaranteed, thus a thread dependent value could | 
|  | 3660 | // be different for the caller and callee, making it invalid to propagate. | 
|  | 3661 | if (ACS.isCallbackCall()) | 
|  | 3662 | if (auto *C =dyn_cast<Constant>(ArgOp)) | 
|  | 3663 | if (C->isThreadDependent()) | 
|  | 3664 | return false; | 
|  | 3665 | return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue); | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 3666 | }; | 
|  | 3667 |  | 
|  | 3668 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) | 
|  | 3669 | return indicatePessimisticFixpoint(); | 
|  | 3670 |  | 
|  | 3671 | // If a candicate was found in this update, return CHANGED. | 
|  | 3672 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 3673 | ? ChangeStatus::UNCHANGED | 
|  | 3674 | : ChangeStatus ::CHANGED; | 
|  | 3675 | } | 
|  | 3676 |  | 
|  | 3677 | /// See AbstractAttribute::trackStatistics() | 
|  | 3678 | void trackStatistics() const override { | 
|  | 3679 | STATS_DECLTRACK_ARG_ATTR(value_simplify) | 
|  | 3680 | } | 
|  | 3681 | }; | 
|  | 3682 |  | 
|  | 3683 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { | 
|  | 3684 | AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 3685 |  | 
|  | 3686 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3687 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3688 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 3689 |  | 
|  | 3690 | auto PredForReturned = [&](Value &V) { | 
|  | 3691 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); | 
|  | 3692 | }; | 
|  | 3693 |  | 
|  | 3694 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) | 
|  | 3695 | return indicatePessimisticFixpoint(); | 
|  | 3696 |  | 
|  | 3697 | // If a candicate was found in this update, return CHANGED. | 
|  | 3698 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 3699 | ? ChangeStatus::UNCHANGED | 
|  | 3700 | : ChangeStatus ::CHANGED; | 
|  | 3701 | } | 
|  | 3702 | /// See AbstractAttribute::trackStatistics() | 
|  | 3703 | void trackStatistics() const override { | 
|  | 3704 | STATS_DECLTRACK_FNRET_ATTR(value_simplify) | 
|  | 3705 | } | 
|  | 3706 | }; | 
|  | 3707 |  | 
|  | 3708 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { | 
|  | 3709 | AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 3710 |  | 
|  | 3711 | /// See AbstractAttribute::initialize(...). | 
|  | 3712 | void initialize(Attributor &A) override { | 
|  | 3713 | Value &V = getAnchorValue(); | 
|  | 3714 |  | 
|  | 3715 | // TODO: add other stuffs | 
|  | 3716 | if (isa<Constant>(V) || isa<UndefValue>(V)) | 
|  | 3717 | indicatePessimisticFixpoint(); | 
|  | 3718 | } | 
|  | 3719 |  | 
|  | 3720 | /// See AbstractAttribute::updateImpl(...). | 
|  | 3721 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3722 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); | 
|  | 3723 |  | 
|  | 3724 | auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool { | 
|  | 3725 | auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); | 
|  | 3726 | if (!Stripped && this == &AA) { | 
|  | 3727 | // TODO: Look the instruction and check recursively. | 
|  | 3728 | LLVM_DEBUG( | 
|  | 3729 | dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : " | 
|  | 3730 | << V << "\n"); | 
|  | 3731 | indicatePessimisticFixpoint(); | 
|  | 3732 | return false; | 
|  | 3733 | } | 
|  | 3734 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); | 
|  | 3735 | }; | 
|  | 3736 |  | 
|  | 3737 | if (!genericValueTraversal<AAValueSimplify, BooleanState>( | 
|  | 3738 | A, getIRPosition(), *this, static_cast<BooleanState &>(*this), | 
|  | 3739 | VisitValueCB)) | 
|  | 3740 | return indicatePessimisticFixpoint(); | 
|  | 3741 |  | 
|  | 3742 | // If a candicate was found in this update, return CHANGED. | 
|  | 3743 |  | 
|  | 3744 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() | 
|  | 3745 | ? ChangeStatus::UNCHANGED | 
|  | 3746 | : ChangeStatus ::CHANGED; | 
|  | 3747 | } | 
|  | 3748 |  | 
|  | 3749 | /// See AbstractAttribute::trackStatistics() | 
|  | 3750 | void trackStatistics() const override { | 
|  | 3751 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify) | 
|  | 3752 | } | 
|  | 3753 | }; | 
|  | 3754 |  | 
|  | 3755 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { | 
|  | 3756 | AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} | 
|  | 3757 |  | 
|  | 3758 | /// See AbstractAttribute::initialize(...). | 
|  | 3759 | void initialize(Attributor &A) override { | 
|  | 3760 | SimplifiedAssociatedValue = &getAnchorValue(); | 
|  | 3761 | indicateOptimisticFixpoint(); | 
|  | 3762 | } | 
|  | 3763 | /// See AbstractAttribute::initialize(...). | 
|  | 3764 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 3765 | llvm_unreachable( | 
|  | 3766 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); | 
|  | 3767 | } | 
|  | 3768 | /// See AbstractAttribute::trackStatistics() | 
|  | 3769 | void trackStatistics() const override { | 
|  | 3770 | STATS_DECLTRACK_FN_ATTR(value_simplify) | 
|  | 3771 | } | 
|  | 3772 | }; | 
|  | 3773 |  | 
|  | 3774 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { | 
|  | 3775 | AAValueSimplifyCallSite(const IRPosition &IRP) | 
|  | 3776 | : AAValueSimplifyFunction(IRP) {} | 
|  | 3777 | /// See AbstractAttribute::trackStatistics() | 
|  | 3778 | void trackStatistics() const override { | 
|  | 3779 | STATS_DECLTRACK_CS_ATTR(value_simplify) | 
|  | 3780 | } | 
|  | 3781 | }; | 
|  | 3782 |  | 
|  | 3783 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { | 
|  | 3784 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP) | 
|  | 3785 | : AAValueSimplifyReturned(IRP) {} | 
|  | 3786 |  | 
|  | 3787 | void trackStatistics() const override { | 
|  | 3788 | STATS_DECLTRACK_CSRET_ATTR(value_simplify) | 
|  | 3789 | } | 
|  | 3790 | }; | 
|  | 3791 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { | 
|  | 3792 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP) | 
|  | 3793 | : AAValueSimplifyFloating(IRP) {} | 
|  | 3794 |  | 
|  | 3795 | void trackStatistics() const override { | 
|  | 3796 | STATS_DECLTRACK_CSARG_ATTR(value_simplify) | 
|  | 3797 | } | 
|  | 3798 | }; | 
|  | 3799 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3800 | /// ----------------------- Heap-To-Stack Conversion --------------------------- | 
|  | 3801 | struct AAHeapToStackImpl : public AAHeapToStack { | 
|  | 3802 | AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} | 
|  | 3803 |  | 
|  | 3804 | const std::string getAsStr() const override { | 
|  | 3805 | return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); | 
|  | 3806 | } | 
|  | 3807 |  | 
|  | 3808 | ChangeStatus manifest(Attributor &A) override { | 
|  | 3809 | assert(getState().isValidState() && | 
|  | 3810 | "Attempted to manifest an invalid state!"); | 
|  | 3811 |  | 
|  | 3812 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | 
|  | 3813 | Function *F = getAssociatedFunction(); | 
|  | 3814 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | 
|  | 3815 |  | 
|  | 3816 | for (Instruction *MallocCall : MallocCalls) { | 
|  | 3817 | // This malloc cannot be replaced. | 
|  | 3818 | if (BadMallocCalls.count(MallocCall)) | 
|  | 3819 | continue; | 
|  | 3820 |  | 
|  | 3821 | for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { | 
|  | 3822 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); | 
|  | 3823 | A.deleteAfterManifest(*FreeCall); | 
|  | 3824 | HasChanged = ChangeStatus::CHANGED; | 
|  | 3825 | } | 
|  | 3826 |  | 
|  | 3827 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall | 
|  | 3828 | << "\n"); | 
|  | 3829 |  | 
|  | 3830 | Constant *Size; | 
|  | 3831 | if (isCallocLikeFn(MallocCall, TLI)) { | 
|  | 3832 | auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); | 
|  | 3833 | auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); | 
|  | 3834 | APInt TotalSize = SizeT->getValue() * Num->getValue(); | 
|  | 3835 | Size = | 
|  | 3836 | ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); | 
|  | 3837 | } else { | 
|  | 3838 | Size = cast<ConstantInt>(MallocCall->getOperand(0)); | 
|  | 3839 | } | 
|  | 3840 |  | 
|  | 3841 | unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); | 
|  | 3842 | Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, | 
|  | 3843 | Size, "", MallocCall->getNextNode()); | 
|  | 3844 |  | 
|  | 3845 | if (AI->getType() != MallocCall->getType()) | 
|  | 3846 | AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", | 
|  | 3847 | AI->getNextNode()); | 
|  | 3848 |  | 
|  | 3849 | MallocCall->replaceAllUsesWith(AI); | 
|  | 3850 |  | 
|  | 3851 | if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { | 
|  | 3852 | auto *NBB = II->getNormalDest(); | 
|  | 3853 | BranchInst::Create(NBB, MallocCall->getParent()); | 
|  | 3854 | A.deleteAfterManifest(*MallocCall); | 
|  | 3855 | } else { | 
|  | 3856 | A.deleteAfterManifest(*MallocCall); | 
|  | 3857 | } | 
|  | 3858 |  | 
|  | 3859 | if (isCallocLikeFn(MallocCall, TLI)) { | 
|  | 3860 | auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", | 
|  | 3861 | AI->getNextNode()); | 
|  | 3862 | Value *Ops[] = { | 
|  | 3863 | BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, | 
|  | 3864 | ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; | 
|  | 3865 |  | 
|  | 3866 | Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; | 
|  | 3867 | Module *M = F->getParent(); | 
|  | 3868 | Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); | 
|  | 3869 | CallInst::Create(Fn, Ops, "", BI->getNextNode()); | 
|  | 3870 | } | 
|  | 3871 | HasChanged = ChangeStatus::CHANGED; | 
|  | 3872 | } | 
|  | 3873 |  | 
|  | 3874 | return HasChanged; | 
|  | 3875 | } | 
|  | 3876 |  | 
|  | 3877 | /// Collection of all malloc calls in a function. | 
|  | 3878 | SmallSetVector<Instruction *, 4> MallocCalls; | 
|  | 3879 |  | 
|  | 3880 | /// Collection of malloc calls that cannot be converted. | 
|  | 3881 | DenseSet<const Instruction *> BadMallocCalls; | 
|  | 3882 |  | 
|  | 3883 | /// A map for each malloc call to the set of associated free calls. | 
|  | 3884 | DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; | 
|  | 3885 |  | 
|  | 3886 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 3887 | }; | 
|  | 3888 |  | 
|  | 3889 | ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { | 
|  | 3890 | const Function *F = getAssociatedFunction(); | 
|  | 3891 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | 
|  | 3892 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3893 | MustBeExecutedContextExplorer &Explorer = | 
|  | 3894 | A.getInfoCache().getMustBeExecutedContextExplorer(); | 
|  | 3895 |  | 
|  | 3896 | auto FreeCheck = [&](Instruction &I) { | 
|  | 3897 | const auto &Frees = FreesForMalloc.lookup(&I); | 
|  | 3898 | if (Frees.size() != 1) | 
|  | 3899 | return false; | 
|  | 3900 | Instruction *UniqueFree = *Frees.begin(); | 
|  | 3901 | return Explorer.findInContextOf(UniqueFree, I.getNextNode()); | 
|  | 3902 | }; | 
|  | 3903 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3904 | auto UsesCheck = [&](Instruction &I) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3905 | bool ValidUsesOnly = true; | 
|  | 3906 | bool MustUse = true; | 
|  | 3907 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3908 | SmallPtrSet<const Use *, 8> Visited; | 
|  | 3909 | SmallVector<const Use *, 8> Worklist; | 
|  | 3910 |  | 
|  | 3911 | for (Use &U : I.uses()) | 
|  | 3912 | Worklist.push_back(&U); | 
|  | 3913 |  | 
|  | 3914 | while (!Worklist.empty()) { | 
|  | 3915 | const Use *U = Worklist.pop_back_val(); | 
|  | 3916 | if (!Visited.insert(U).second) | 
|  | 3917 | continue; | 
|  | 3918 |  | 
|  | 3919 | auto *UserI = U->getUser(); | 
|  | 3920 |  | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 3921 | if (isa<LoadInst>(UserI)) | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3922 | continue; | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 3923 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { | 
|  | 3924 | if (SI->getValueOperand() == U->get()) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3925 | LLVM_DEBUG(dbgs() | 
|  | 3926 | << "[H2S] escaping store to memory: " << *UserI << "\n"); | 
|  | 3927 | ValidUsesOnly = false; | 
|  | 3928 | } else { | 
|  | 3929 | // A store into the malloc'ed memory is fine. | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 3930 | } | 
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 3931 | continue; | 
|  | 3932 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3933 |  | 
|  | 3934 | // NOTE: Right now, if a function that has malloc pointer as an argument | 
|  | 3935 | // frees memory, we assume that the malloc pointer is freed. | 
|  | 3936 |  | 
|  | 3937 | // TODO: Add nofree callsite argument attribute to indicate that pointer | 
|  | 3938 | // argument is not freed. | 
|  | 3939 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | 
|  | 3940 | if (!CB->isArgOperand(U)) | 
|  | 3941 | continue; | 
|  | 3942 |  | 
|  | 3943 | if (CB->isLifetimeStartOrEnd()) | 
|  | 3944 | continue; | 
|  | 3945 |  | 
|  | 3946 | // Record malloc. | 
|  | 3947 | if (isFreeCall(UserI, TLI)) { | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3948 | if (MustUse) { | 
|  | 3949 | FreesForMalloc[&I].insert( | 
|  | 3950 | cast<Instruction>(const_cast<User *>(UserI))); | 
|  | 3951 | } else { | 
|  | 3952 | LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " | 
|  | 3953 | << *UserI << "\n"); | 
|  | 3954 | ValidUsesOnly = false; | 
|  | 3955 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3956 | continue; | 
|  | 3957 | } | 
|  | 3958 |  | 
|  | 3959 | // If a function does not free memory we are fine | 
|  | 3960 | const auto &NoFreeAA = | 
|  | 3961 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(*CB)); | 
|  | 3962 |  | 
|  | 3963 | unsigned ArgNo = U - CB->arg_begin(); | 
|  | 3964 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( | 
|  | 3965 | *this, IRPosition::callsite_argument(*CB, ArgNo)); | 
|  | 3966 |  | 
|  | 3967 | if (!NoCaptureAA.isAssumedNoCapture() || !NoFreeAA.isAssumedNoFree()) { | 
|  | 3968 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3969 | ValidUsesOnly = false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3970 | } | 
|  | 3971 | continue; | 
|  | 3972 | } | 
|  | 3973 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3974 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | 
|  | 3975 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | 
|  | 3976 | MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3977 | for (Use &U : UserI->uses()) | 
|  | 3978 | Worklist.push_back(&U); | 
|  | 3979 | continue; | 
|  | 3980 | } | 
|  | 3981 |  | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3982 | // Unknown user for which we can not track uses further (in a way that | 
|  | 3983 | // makes sense). | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3984 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3985 | ValidUsesOnly = false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3986 | } | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 3987 | return ValidUsesOnly; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3988 | }; | 
|  | 3989 |  | 
|  | 3990 | auto MallocCallocCheck = [&](Instruction &I) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 3991 | if (BadMallocCalls.count(&I)) | 
|  | 3992 | return true; | 
|  | 3993 |  | 
|  | 3994 | bool IsMalloc = isMallocLikeFn(&I, TLI); | 
|  | 3995 | bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); | 
|  | 3996 | if (!IsMalloc && !IsCalloc) { | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3997 | BadMallocCalls.insert(&I); | 
|  | 3998 | return true; | 
|  | 3999 | } | 
|  | 4000 |  | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4001 | if (IsMalloc) { | 
|  | 4002 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) | 
|  | 4003 | if (Size->getValue().sle(MaxHeapToStackSize)) | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4004 | if (UsesCheck(I) || FreeCheck(I)) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4005 | MallocCalls.insert(&I); | 
|  | 4006 | return true; | 
|  | 4007 | } | 
|  | 4008 | } else if (IsCalloc) { | 
|  | 4009 | bool Overflow = false; | 
|  | 4010 | if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) | 
|  | 4011 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) | 
|  | 4012 | if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) | 
|  | 4013 | .sle(MaxHeapToStackSize)) | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4014 | if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4015 | MallocCalls.insert(&I); | 
|  | 4016 | return true; | 
|  | 4017 | } | 
|  | 4018 | } | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4019 |  | 
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 4020 | BadMallocCalls.insert(&I); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4021 | return true; | 
|  | 4022 | }; | 
|  | 4023 |  | 
|  | 4024 | size_t NumBadMallocs = BadMallocCalls.size(); | 
|  | 4025 |  | 
|  | 4026 | A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); | 
|  | 4027 |  | 
|  | 4028 | if (NumBadMallocs != BadMallocCalls.size()) | 
|  | 4029 | return ChangeStatus::CHANGED; | 
|  | 4030 |  | 
|  | 4031 | return ChangeStatus::UNCHANGED; | 
|  | 4032 | } | 
|  | 4033 |  | 
|  | 4034 | struct AAHeapToStackFunction final : public AAHeapToStackImpl { | 
|  | 4035 | AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} | 
|  | 4036 |  | 
|  | 4037 | /// See AbstractAttribute::trackStatistics() | 
|  | 4038 | void trackStatistics() const override { | 
|  | 4039 | STATS_DECL(MallocCalls, Function, | 
| Johannes Doerfert | 0be9cf2 | 2019-10-14 17:29:05 -0500 | [diff] [blame] | 4040 | "Number of malloc calls converted to allocas"); | 
|  | 4041 | for (auto *C : MallocCalls) | 
|  | 4042 | if (!BadMallocCalls.count(C)) | 
|  | 4043 | ++BUILD_STAT_NAME(MallocCalls, Function); | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4044 | } | 
|  | 4045 | }; | 
|  | 4046 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4047 | /// -------------------- Memory Behavior Attributes ---------------------------- | 
|  | 4048 | /// Includes read-none, read-only, and write-only. | 
|  | 4049 | /// ---------------------------------------------------------------------------- | 
|  | 4050 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { | 
|  | 4051 | AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} | 
|  | 4052 |  | 
|  | 4053 | /// See AbstractAttribute::initialize(...). | 
|  | 4054 | void initialize(Attributor &A) override { | 
|  | 4055 | intersectAssumedBits(BEST_STATE); | 
|  | 4056 | getKnownStateFromValue(getIRPosition(), getState()); | 
|  | 4057 | IRAttribute::initialize(A); | 
|  | 4058 | } | 
|  | 4059 |  | 
|  | 4060 | /// Return the memory behavior information encoded in the IR for \p IRP. | 
|  | 4061 | static void getKnownStateFromValue(const IRPosition &IRP, | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4062 | BitIntegerState &State) { | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4063 | SmallVector<Attribute, 2> Attrs; | 
|  | 4064 | IRP.getAttrs(AttrKinds, Attrs); | 
|  | 4065 | for (const Attribute &Attr : Attrs) { | 
|  | 4066 | switch (Attr.getKindAsEnum()) { | 
|  | 4067 | case Attribute::ReadNone: | 
|  | 4068 | State.addKnownBits(NO_ACCESSES); | 
|  | 4069 | break; | 
|  | 4070 | case Attribute::ReadOnly: | 
|  | 4071 | State.addKnownBits(NO_WRITES); | 
|  | 4072 | break; | 
|  | 4073 | case Attribute::WriteOnly: | 
|  | 4074 | State.addKnownBits(NO_READS); | 
|  | 4075 | break; | 
|  | 4076 | default: | 
|  | 4077 | llvm_unreachable("Unexpcted attribute!"); | 
|  | 4078 | } | 
|  | 4079 | } | 
|  | 4080 |  | 
|  | 4081 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { | 
|  | 4082 | if (!I->mayReadFromMemory()) | 
|  | 4083 | State.addKnownBits(NO_READS); | 
|  | 4084 | if (!I->mayWriteToMemory()) | 
|  | 4085 | State.addKnownBits(NO_WRITES); | 
|  | 4086 | } | 
|  | 4087 | } | 
|  | 4088 |  | 
|  | 4089 | /// See AbstractAttribute::getDeducedAttributes(...). | 
|  | 4090 | void getDeducedAttributes(LLVMContext &Ctx, | 
|  | 4091 | SmallVectorImpl<Attribute> &Attrs) const override { | 
|  | 4092 | assert(Attrs.size() == 0); | 
|  | 4093 | if (isAssumedReadNone()) | 
|  | 4094 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); | 
|  | 4095 | else if (isAssumedReadOnly()) | 
|  | 4096 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); | 
|  | 4097 | else if (isAssumedWriteOnly()) | 
|  | 4098 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); | 
|  | 4099 | assert(Attrs.size() <= 1); | 
|  | 4100 | } | 
|  | 4101 |  | 
|  | 4102 | /// See AbstractAttribute::manifest(...). | 
|  | 4103 | ChangeStatus manifest(Attributor &A) override { | 
| Johannes Doerfert | b2083c5 | 2019-10-20 22:46:48 -0500 | [diff] [blame] | 4104 | const IRPosition &IRP = getIRPosition(); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4105 |  | 
|  | 4106 | // Check if we would improve the existing attributes first. | 
|  | 4107 | SmallVector<Attribute, 4> DeducedAttrs; | 
|  | 4108 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); | 
|  | 4109 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { | 
|  | 4110 | return IRP.hasAttr(Attr.getKindAsEnum(), | 
|  | 4111 | /* IgnoreSubsumingPositions */ true); | 
|  | 4112 | })) | 
|  | 4113 | return ChangeStatus::UNCHANGED; | 
|  | 4114 |  | 
|  | 4115 | // Clear existing attributes. | 
|  | 4116 | IRP.removeAttrs(AttrKinds); | 
|  | 4117 |  | 
|  | 4118 | // Use the generic manifest method. | 
|  | 4119 | return IRAttribute::manifest(A); | 
|  | 4120 | } | 
|  | 4121 |  | 
|  | 4122 | /// See AbstractState::getAsStr(). | 
|  | 4123 | const std::string getAsStr() const override { | 
|  | 4124 | if (isAssumedReadNone()) | 
|  | 4125 | return "readnone"; | 
|  | 4126 | if (isAssumedReadOnly()) | 
|  | 4127 | return "readonly"; | 
|  | 4128 | if (isAssumedWriteOnly()) | 
|  | 4129 | return "writeonly"; | 
|  | 4130 | return "may-read/write"; | 
|  | 4131 | } | 
|  | 4132 |  | 
|  | 4133 | /// The set of IR attributes AAMemoryBehavior deals with. | 
|  | 4134 | static const Attribute::AttrKind AttrKinds[3]; | 
|  | 4135 | }; | 
|  | 4136 |  | 
|  | 4137 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { | 
|  | 4138 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; | 
|  | 4139 |  | 
|  | 4140 | /// Memory behavior attribute for a floating value. | 
|  | 4141 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { | 
|  | 4142 | AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 4143 |  | 
|  | 4144 | /// See AbstractAttribute::initialize(...). | 
|  | 4145 | void initialize(Attributor &A) override { | 
|  | 4146 | AAMemoryBehaviorImpl::initialize(A); | 
|  | 4147 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 4148 | for (const Use &U : getAssociatedValue().uses()) | 
|  | 4149 | Uses.insert(&U); | 
|  | 4150 | } | 
|  | 4151 |  | 
|  | 4152 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4153 | ChangeStatus updateImpl(Attributor &A) override; | 
|  | 4154 |  | 
|  | 4155 | /// See AbstractAttribute::trackStatistics() | 
|  | 4156 | void trackStatistics() const override { | 
|  | 4157 | if (isAssumedReadNone()) | 
|  | 4158 | STATS_DECLTRACK_FLOATING_ATTR(readnone) | 
|  | 4159 | else if (isAssumedReadOnly()) | 
|  | 4160 | STATS_DECLTRACK_FLOATING_ATTR(readonly) | 
|  | 4161 | else if (isAssumedWriteOnly()) | 
|  | 4162 | STATS_DECLTRACK_FLOATING_ATTR(writeonly) | 
|  | 4163 | } | 
|  | 4164 |  | 
|  | 4165 | private: | 
|  | 4166 | /// Return true if users of \p UserI might access the underlying | 
|  | 4167 | /// variable/location described by \p U and should therefore be analyzed. | 
|  | 4168 | bool followUsersOfUseIn(Attributor &A, const Use *U, | 
|  | 4169 | const Instruction *UserI); | 
|  | 4170 |  | 
|  | 4171 | /// Update the state according to the effect of use \p U in \p UserI. | 
|  | 4172 | void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); | 
|  | 4173 |  | 
|  | 4174 | protected: | 
|  | 4175 | /// Container for (transitive) uses of the associated argument. | 
|  | 4176 | SetVector<const Use *> Uses; | 
|  | 4177 | }; | 
|  | 4178 |  | 
|  | 4179 | /// Memory behavior attribute for function argument. | 
|  | 4180 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { | 
|  | 4181 | AAMemoryBehaviorArgument(const IRPosition &IRP) | 
|  | 4182 | : AAMemoryBehaviorFloating(IRP) {} | 
|  | 4183 |  | 
|  | 4184 | /// See AbstractAttribute::initialize(...). | 
|  | 4185 | void initialize(Attributor &A) override { | 
|  | 4186 | AAMemoryBehaviorFloating::initialize(A); | 
|  | 4187 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4188 | // Initialize the use vector with all direct uses of the associated value. | 
|  | 4189 | Argument *Arg = getAssociatedArgument(); | 
|  | 4190 | if (!Arg || !Arg->getParent()->hasExactDefinition()) | 
|  | 4191 | indicatePessimisticFixpoint(); | 
|  | 4192 | } | 
|  | 4193 |  | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4194 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4195 | // TODO: From readattrs.ll: "inalloca parameters are always | 
|  | 4196 | //                           considered written" | 
|  | 4197 | if (hasAttr({Attribute::InAlloca})) { | 
|  | 4198 | removeKnownBits(NO_WRITES); | 
|  | 4199 | removeAssumedBits(NO_WRITES); | 
|  | 4200 | } | 
|  | 4201 | return AAMemoryBehaviorFloating::manifest(A); | 
|  | 4202 | } | 
|  | 4203 |  | 
|  | 4204 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4205 | /// See AbstractAttribute::trackStatistics() | 
|  | 4206 | void trackStatistics() const override { | 
|  | 4207 | if (isAssumedReadNone()) | 
|  | 4208 | STATS_DECLTRACK_ARG_ATTR(readnone) | 
|  | 4209 | else if (isAssumedReadOnly()) | 
|  | 4210 | STATS_DECLTRACK_ARG_ATTR(readonly) | 
|  | 4211 | else if (isAssumedWriteOnly()) | 
|  | 4212 | STATS_DECLTRACK_ARG_ATTR(writeonly) | 
|  | 4213 | } | 
|  | 4214 | }; | 
|  | 4215 |  | 
|  | 4216 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { | 
|  | 4217 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) | 
|  | 4218 | : AAMemoryBehaviorArgument(IRP) {} | 
|  | 4219 |  | 
|  | 4220 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4221 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4222 | // TODO: Once we have call site specific value information we can provide | 
|  | 4223 | //       call site specific liveness liveness information and then it makes | 
|  | 4224 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 4225 | //       redirecting requests to the callee argument. | 
|  | 4226 | Argument *Arg = getAssociatedArgument(); | 
|  | 4227 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | 
|  | 4228 | auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); | 
|  | 4229 | return clampStateAndIndicateChange( | 
|  | 4230 | getState(), | 
| Johannes Doerfert | 3178424 | 2019-10-29 23:18:49 -0500 | [diff] [blame] | 4231 | static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4232 | } | 
|  | 4233 |  | 
|  | 4234 | /// See AbstractAttribute::trackStatistics() | 
|  | 4235 | void trackStatistics() const override { | 
|  | 4236 | if (isAssumedReadNone()) | 
|  | 4237 | STATS_DECLTRACK_CSARG_ATTR(readnone) | 
|  | 4238 | else if (isAssumedReadOnly()) | 
|  | 4239 | STATS_DECLTRACK_CSARG_ATTR(readonly) | 
|  | 4240 | else if (isAssumedWriteOnly()) | 
|  | 4241 | STATS_DECLTRACK_CSARG_ATTR(writeonly) | 
|  | 4242 | } | 
|  | 4243 | }; | 
|  | 4244 |  | 
|  | 4245 | /// Memory behavior attribute for a call site return position. | 
|  | 4246 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { | 
|  | 4247 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) | 
|  | 4248 | : AAMemoryBehaviorFloating(IRP) {} | 
|  | 4249 |  | 
|  | 4250 | /// See AbstractAttribute::manifest(...). | 
|  | 4251 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4252 | // We do not annotate returned values. | 
|  | 4253 | return ChangeStatus::UNCHANGED; | 
|  | 4254 | } | 
|  | 4255 |  | 
|  | 4256 | /// See AbstractAttribute::trackStatistics() | 
|  | 4257 | void trackStatistics() const override {} | 
|  | 4258 | }; | 
|  | 4259 |  | 
|  | 4260 | /// An AA to represent the memory behavior function attributes. | 
|  | 4261 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { | 
|  | 4262 | AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 4263 |  | 
|  | 4264 | /// See AbstractAttribute::updateImpl(Attributor &A). | 
|  | 4265 | virtual ChangeStatus updateImpl(Attributor &A) override; | 
|  | 4266 |  | 
|  | 4267 | /// See AbstractAttribute::manifest(...). | 
|  | 4268 | ChangeStatus manifest(Attributor &A) override { | 
|  | 4269 | Function &F = cast<Function>(getAnchorValue()); | 
|  | 4270 | if (isAssumedReadNone()) { | 
|  | 4271 | F.removeFnAttr(Attribute::ArgMemOnly); | 
|  | 4272 | F.removeFnAttr(Attribute::InaccessibleMemOnly); | 
|  | 4273 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); | 
|  | 4274 | } | 
|  | 4275 | return AAMemoryBehaviorImpl::manifest(A); | 
|  | 4276 | } | 
|  | 4277 |  | 
|  | 4278 | /// See AbstractAttribute::trackStatistics() | 
|  | 4279 | void trackStatistics() const override { | 
|  | 4280 | if (isAssumedReadNone()) | 
|  | 4281 | STATS_DECLTRACK_FN_ATTR(readnone) | 
|  | 4282 | else if (isAssumedReadOnly()) | 
|  | 4283 | STATS_DECLTRACK_FN_ATTR(readonly) | 
|  | 4284 | else if (isAssumedWriteOnly()) | 
|  | 4285 | STATS_DECLTRACK_FN_ATTR(writeonly) | 
|  | 4286 | } | 
|  | 4287 | }; | 
|  | 4288 |  | 
|  | 4289 | /// AAMemoryBehavior attribute for call sites. | 
|  | 4290 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { | 
|  | 4291 | AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} | 
|  | 4292 |  | 
|  | 4293 | /// See AbstractAttribute::initialize(...). | 
|  | 4294 | void initialize(Attributor &A) override { | 
|  | 4295 | AAMemoryBehaviorImpl::initialize(A); | 
|  | 4296 | Function *F = getAssociatedFunction(); | 
|  | 4297 | if (!F || !F->hasExactDefinition()) | 
|  | 4298 | indicatePessimisticFixpoint(); | 
|  | 4299 | } | 
|  | 4300 |  | 
|  | 4301 | /// See AbstractAttribute::updateImpl(...). | 
|  | 4302 | ChangeStatus updateImpl(Attributor &A) override { | 
|  | 4303 | // TODO: Once we have call site specific value information we can provide | 
|  | 4304 | //       call site specific liveness liveness information and then it makes | 
|  | 4305 | //       sense to specialize attributes for call sites arguments instead of | 
|  | 4306 | //       redirecting requests to the callee argument. | 
|  | 4307 | Function *F = getAssociatedFunction(); | 
|  | 4308 | const IRPosition &FnPos = IRPosition::function(*F); | 
|  | 4309 | auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 4310 | return clampStateAndIndicateChange( | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 4311 | getState(), | 
|  | 4312 | static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4313 | } | 
|  | 4314 |  | 
|  | 4315 | /// See AbstractAttribute::trackStatistics() | 
|  | 4316 | void trackStatistics() const override { | 
|  | 4317 | if (isAssumedReadNone()) | 
|  | 4318 | STATS_DECLTRACK_CS_ATTR(readnone) | 
|  | 4319 | else if (isAssumedReadOnly()) | 
|  | 4320 | STATS_DECLTRACK_CS_ATTR(readonly) | 
|  | 4321 | else if (isAssumedWriteOnly()) | 
|  | 4322 | STATS_DECLTRACK_CS_ATTR(writeonly) | 
|  | 4323 | } | 
|  | 4324 | }; | 
| Benjamin Kramer | c5d1d56 | 2019-10-12 11:01:52 +0000 | [diff] [blame] | 4325 | } // namespace | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4326 |  | 
|  | 4327 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { | 
|  | 4328 |  | 
|  | 4329 | // The current assumed state used to determine a change. | 
|  | 4330 | auto AssumedState = getAssumed(); | 
|  | 4331 |  | 
|  | 4332 | auto CheckRWInst = [&](Instruction &I) { | 
|  | 4333 | // If the instruction has an own memory behavior state, use it to restrict | 
|  | 4334 | // the local state. No further analysis is required as the other memory | 
|  | 4335 | // state is as optimistic as it gets. | 
|  | 4336 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { | 
|  | 4337 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | 
|  | 4338 | *this, IRPosition::callsite_function(ICS)); | 
|  | 4339 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | 
|  | 4340 | return !isAtFixpoint(); | 
|  | 4341 | } | 
|  | 4342 |  | 
|  | 4343 | // Remove access kind modifiers if necessary. | 
|  | 4344 | if (I.mayReadFromMemory()) | 
|  | 4345 | removeAssumedBits(NO_READS); | 
|  | 4346 | if (I.mayWriteToMemory()) | 
|  | 4347 | removeAssumedBits(NO_WRITES); | 
|  | 4348 | return !isAtFixpoint(); | 
|  | 4349 | }; | 
|  | 4350 |  | 
|  | 4351 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) | 
|  | 4352 | return indicatePessimisticFixpoint(); | 
|  | 4353 |  | 
|  | 4354 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | 
|  | 4355 | : ChangeStatus::UNCHANGED; | 
|  | 4356 | } | 
|  | 4357 |  | 
|  | 4358 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { | 
|  | 4359 |  | 
|  | 4360 | const IRPosition &IRP = getIRPosition(); | 
|  | 4361 | const IRPosition &FnPos = IRPosition::function_scope(IRP); | 
|  | 4362 | AAMemoryBehavior::StateType &S = getState(); | 
|  | 4363 |  | 
|  | 4364 | // First, check the function scope. We take the known information and we avoid | 
|  | 4365 | // work if the assumed information implies the current assumed information for | 
|  | 4366 | // this attribute. | 
|  | 4367 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); | 
|  | 4368 | S.addKnownBits(FnMemAA.getKnown()); | 
|  | 4369 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) | 
|  | 4370 | return ChangeStatus::UNCHANGED; | 
|  | 4371 |  | 
|  | 4372 | // Make sure the value is not captured (except through "return"), if | 
|  | 4373 | // it is, any information derived would be irrelevant anyway as we cannot | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4374 | // check the potential aliases introduced by the capture. However, no need | 
|  | 4375 | // to fall back to anythign less optimistic than the function state. | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4376 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); | 
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4377 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | 
|  | 4378 | S.intersectAssumedBits(FnMemAA.getAssumed()); | 
|  | 4379 | return ChangeStatus::CHANGED; | 
|  | 4380 | } | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4381 |  | 
|  | 4382 | // The current assumed state used to determine a change. | 
|  | 4383 | auto AssumedState = S.getAssumed(); | 
|  | 4384 |  | 
|  | 4385 | // Liveness information to exclude dead users. | 
|  | 4386 | // TODO: Take the FnPos once we have call site specific liveness information. | 
|  | 4387 | const auto &LivenessAA = A.getAAFor<AAIsDead>( | 
|  | 4388 | *this, IRPosition::function(*IRP.getAssociatedFunction())); | 
|  | 4389 |  | 
|  | 4390 | // Visit and expand uses until all are analyzed or a fixpoint is reached. | 
|  | 4391 | for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { | 
|  | 4392 | const Use *U = Uses[i]; | 
|  | 4393 | Instruction *UserI = cast<Instruction>(U->getUser()); | 
|  | 4394 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI | 
|  | 4395 | << " [Dead: " << (LivenessAA.isAssumedDead(UserI)) | 
|  | 4396 | << "]\n"); | 
|  | 4397 | if (LivenessAA.isAssumedDead(UserI)) | 
|  | 4398 | continue; | 
|  | 4399 |  | 
|  | 4400 | // Check if the users of UserI should also be visited. | 
|  | 4401 | if (followUsersOfUseIn(A, U, UserI)) | 
|  | 4402 | for (const Use &UserIUse : UserI->uses()) | 
|  | 4403 | Uses.insert(&UserIUse); | 
|  | 4404 |  | 
|  | 4405 | // If UserI might touch memory we analyze the use in detail. | 
|  | 4406 | if (UserI->mayReadOrWriteMemory()) | 
|  | 4407 | analyzeUseIn(A, U, UserI); | 
|  | 4408 | } | 
|  | 4409 |  | 
|  | 4410 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | 
|  | 4411 | : ChangeStatus::UNCHANGED; | 
|  | 4412 | } | 
|  | 4413 |  | 
|  | 4414 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, | 
|  | 4415 | const Instruction *UserI) { | 
|  | 4416 | // The loaded value is unrelated to the pointer argument, no need to | 
|  | 4417 | // follow the users of the load. | 
|  | 4418 | if (isa<LoadInst>(UserI)) | 
|  | 4419 | return false; | 
|  | 4420 |  | 
|  | 4421 | // By default we follow all uses assuming UserI might leak information on U, | 
|  | 4422 | // we have special handling for call sites operands though. | 
|  | 4423 | ImmutableCallSite ICS(UserI); | 
|  | 4424 | if (!ICS || !ICS.isArgOperand(U)) | 
|  | 4425 | return true; | 
|  | 4426 |  | 
|  | 4427 | // If the use is a call argument known not to be captured, the users of | 
|  | 4428 | // the call do not need to be visited because they have to be unrelated to | 
|  | 4429 | // the input. Note that this check is not trivial even though we disallow | 
|  | 4430 | // general capturing of the underlying argument. The reason is that the | 
|  | 4431 | // call might the argument "through return", which we allow and for which we | 
|  | 4432 | // need to check call users. | 
|  | 4433 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 4434 | const auto &ArgNoCaptureAA = | 
|  | 4435 | A.getAAFor<AANoCapture>(*this, IRPosition::callsite_argument(ICS, ArgNo)); | 
|  | 4436 | return !ArgNoCaptureAA.isAssumedNoCapture(); | 
|  | 4437 | } | 
|  | 4438 |  | 
|  | 4439 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, | 
|  | 4440 | const Instruction *UserI) { | 
|  | 4441 | assert(UserI->mayReadOrWriteMemory()); | 
|  | 4442 |  | 
|  | 4443 | switch (UserI->getOpcode()) { | 
|  | 4444 | default: | 
|  | 4445 | // TODO: Handle all atomics and other side-effect operations we know of. | 
|  | 4446 | break; | 
|  | 4447 | case Instruction::Load: | 
|  | 4448 | // Loads cause the NO_READS property to disappear. | 
|  | 4449 | removeAssumedBits(NO_READS); | 
|  | 4450 | return; | 
|  | 4451 |  | 
|  | 4452 | case Instruction::Store: | 
|  | 4453 | // Stores cause the NO_WRITES property to disappear if the use is the | 
|  | 4454 | // pointer operand. Note that we do assume that capturing was taken care of | 
|  | 4455 | // somewhere else. | 
|  | 4456 | if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) | 
|  | 4457 | removeAssumedBits(NO_WRITES); | 
|  | 4458 | return; | 
|  | 4459 |  | 
|  | 4460 | case Instruction::Call: | 
|  | 4461 | case Instruction::CallBr: | 
|  | 4462 | case Instruction::Invoke: { | 
|  | 4463 | // For call sites we look at the argument memory behavior attribute (this | 
|  | 4464 | // could be recursive!) in order to restrict our own state. | 
|  | 4465 | ImmutableCallSite ICS(UserI); | 
|  | 4466 |  | 
|  | 4467 | // Give up on operand bundles. | 
|  | 4468 | if (ICS.isBundleOperand(U)) { | 
|  | 4469 | indicatePessimisticFixpoint(); | 
|  | 4470 | return; | 
|  | 4471 | } | 
|  | 4472 |  | 
|  | 4473 | // Calling a function does read the function pointer, maybe write it if the | 
|  | 4474 | // function is self-modifying. | 
|  | 4475 | if (ICS.isCallee(U)) { | 
|  | 4476 | removeAssumedBits(NO_READS); | 
|  | 4477 | break; | 
|  | 4478 | } | 
|  | 4479 |  | 
|  | 4480 | // Adjust the possible access behavior based on the information on the | 
|  | 4481 | // argument. | 
|  | 4482 | unsigned ArgNo = ICS.getArgumentNo(U); | 
|  | 4483 | const IRPosition &ArgPos = IRPosition::callsite_argument(ICS, ArgNo); | 
|  | 4484 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); | 
|  | 4485 | // "assumed" has at most the same bits as the MemBehaviorAA assumed | 
|  | 4486 | // and at least "known". | 
|  | 4487 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | 
|  | 4488 | return; | 
|  | 4489 | } | 
|  | 4490 | }; | 
|  | 4491 |  | 
|  | 4492 | // Generally, look at the "may-properties" and adjust the assumed state if we | 
|  | 4493 | // did not trigger special handling before. | 
|  | 4494 | if (UserI->mayReadFromMemory()) | 
|  | 4495 | removeAssumedBits(NO_READS); | 
|  | 4496 | if (UserI->mayWriteToMemory()) | 
|  | 4497 | removeAssumedBits(NO_WRITES); | 
|  | 4498 | } | 
|  | 4499 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4500 | /// ---------------------------------------------------------------------------- | 
|  | 4501 | ///                               Attributor | 
|  | 4502 | /// ---------------------------------------------------------------------------- | 
|  | 4503 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4504 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, | 
|  | 4505 | const AAIsDead *LivenessAA) { | 
|  | 4506 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); | 
|  | 4507 | if (!CtxI) | 
|  | 4508 | return false; | 
|  | 4509 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 4510 | // TODO: Find a good way to utilize fine and coarse grained liveness | 
|  | 4511 | // information. | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4512 | if (!LivenessAA) | 
|  | 4513 | LivenessAA = | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4514 | &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), | 
|  | 4515 | /* TrackDependence */ false); | 
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 4516 |  | 
|  | 4517 | // Don't check liveness for AAIsDead. | 
|  | 4518 | if (&AA == LivenessAA) | 
|  | 4519 | return false; | 
|  | 4520 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4521 | if (!LivenessAA->isAssumedDead(CtxI)) | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4522 | return false; | 
|  | 4523 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4524 | // We actually used liveness information so we have to record a dependence. | 
|  | 4525 | recordDependence(*LivenessAA, AA); | 
|  | 4526 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4527 | return true; | 
|  | 4528 | } | 
|  | 4529 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 4530 | bool Attributor::checkForAllUses( | 
|  | 4531 | const function_ref<bool(const Use &, bool &)> &Pred, | 
|  | 4532 | const AbstractAttribute &QueryingAA, const Value &V) { | 
|  | 4533 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 4534 | SmallVector<const Use *, 16> Worklist; | 
|  | 4535 | SmallPtrSet<const Use *, 16> Visited; | 
|  | 4536 |  | 
|  | 4537 | for (const Use &U : V.uses()) | 
|  | 4538 | Worklist.push_back(&U); | 
|  | 4539 |  | 
|  | 4540 | LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() | 
|  | 4541 | << " initial uses to check\n"); | 
|  | 4542 |  | 
|  | 4543 | if (Worklist.empty()) | 
|  | 4544 | return true; | 
|  | 4545 |  | 
|  | 4546 | bool AnyDead = false; | 
|  | 4547 | const Function *ScopeFn = IRP.getAnchorScope(); | 
|  | 4548 | const auto *LivenessAA = | 
|  | 4549 | ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), | 
|  | 4550 | /* TrackDependence */ false) | 
|  | 4551 | : nullptr; | 
|  | 4552 |  | 
|  | 4553 | while (!Worklist.empty()) { | 
|  | 4554 | const Use *U = Worklist.pop_back_val(); | 
|  | 4555 | if (!Visited.insert(U).second) | 
|  | 4556 | continue; | 
|  | 4557 | LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << "\n"); | 
|  | 4558 | if (Instruction *UserI = dyn_cast<Instruction>(U->getUser())) | 
|  | 4559 | if (LivenessAA && LivenessAA->isAssumedDead(UserI)) { | 
|  | 4560 | LLVM_DEBUG(dbgs() << "[Attributor] Dead user: " << *UserI << ": " | 
|  | 4561 | << static_cast<const AbstractAttribute &>(*LivenessAA) | 
|  | 4562 | << "\n"); | 
|  | 4563 | AnyDead = true; | 
|  | 4564 | continue; | 
|  | 4565 | } | 
|  | 4566 |  | 
|  | 4567 | bool Follow = false; | 
|  | 4568 | if (!Pred(*U, Follow)) | 
|  | 4569 | return false; | 
|  | 4570 | if (!Follow) | 
|  | 4571 | continue; | 
|  | 4572 | for (const Use &UU : U->getUser()->uses()) | 
|  | 4573 | Worklist.push_back(&UU); | 
|  | 4574 | } | 
|  | 4575 |  | 
|  | 4576 | if (AnyDead) | 
|  | 4577 | recordDependence(*LivenessAA, QueryingAA); | 
|  | 4578 |  | 
|  | 4579 | return true; | 
|  | 4580 | } | 
|  | 4581 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4582 | bool Attributor::checkForAllCallSites( | 
|  | 4583 | const function_ref<bool(AbstractCallSite)> &Pred, | 
|  | 4584 | const AbstractAttribute &QueryingAA, bool RequireAllCallSites) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4585 | // We can try to determine information from | 
|  | 4586 | // the call sites. However, this is only possible all call sites are known, | 
|  | 4587 | // hence the function has internal linkage. | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4588 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 4589 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 4590 | if (!AssociatedFunction) { | 
|  | 4591 | LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP | 
|  | 4592 | << "\n"); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4593 | return false; | 
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 4594 | } | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4595 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4596 | return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, | 
|  | 4597 | &QueryingAA); | 
|  | 4598 | } | 
|  | 4599 |  | 
|  | 4600 | bool Attributor::checkForAllCallSites( | 
|  | 4601 | const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, | 
|  | 4602 | bool RequireAllCallSites, const AbstractAttribute *QueryingAA) { | 
|  | 4603 | if (RequireAllCallSites && !Fn.hasLocalLinkage()) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4604 | LLVM_DEBUG( | 
|  | 4605 | dbgs() | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4606 | << "[Attributor] Function " << Fn.getName() | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4607 | << " has no internal linkage, hence not all call sites are known\n"); | 
|  | 4608 | return false; | 
|  | 4609 | } | 
|  | 4610 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4611 | for (const Use &U : Fn.uses()) { | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4612 | AbstractCallSite ACS(&U); | 
|  | 4613 | if (!ACS) { | 
|  | 4614 | LLVM_DEBUG(dbgs() << "[Attributor] Function " | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4615 | << Fn.getName() | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4616 | << " has non call site use " << *U.get() << " in " | 
|  | 4617 | << *U.getUser() << "\n"); | 
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 4618 | return false; | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4619 | } | 
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 4620 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4621 | Instruction *I = ACS.getInstruction(); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4622 | Function *Caller = I->getFunction(); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 4623 |  | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4624 | const auto *LivenessAA = | 
|  | 4625 | lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA, | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4626 | /* TrackDependence */ false); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 4627 |  | 
|  | 4628 | // Skip dead calls. | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4629 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4630 | // We actually used liveness information so we have to record a | 
|  | 4631 | // dependence. | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4632 | if (QueryingAA) | 
|  | 4633 | recordDependence(*LivenessAA, *QueryingAA); | 
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 4634 | continue; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4635 | } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4636 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4637 | const Use *EffectiveUse = | 
|  | 4638 | ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; | 
|  | 4639 | if (!ACS.isCallee(EffectiveUse)) { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4640 | if (!RequireAllCallSites) | 
|  | 4641 | continue; | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4642 | LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4643 | << " is an invalid use of " | 
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4644 | << Fn.getName() << "\n"); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4645 | return false; | 
|  | 4646 | } | 
|  | 4647 |  | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4648 | if (Pred(ACS)) | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4649 | continue; | 
|  | 4650 |  | 
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 4651 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " | 
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4652 | << *ACS.getInstruction() << "\n"); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4653 | return false; | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | return true; | 
|  | 4657 | } | 
|  | 4658 |  | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4659 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 4660 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4661 | &Pred, | 
|  | 4662 | const AbstractAttribute &QueryingAA) { | 
|  | 4663 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4664 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 4665 | // Since we need to provide return instructions we have to have an exact | 
|  | 4666 | // definition. | 
|  | 4667 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4668 | if (!AssociatedFunction) | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4669 | return false; | 
|  | 4670 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4671 | // If this is a call site query we use the call site specific return values | 
|  | 4672 | // and liveness information. | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4673 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 4674 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4675 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4676 | if (!AARetVal.getState().isValidState()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4677 | return false; | 
|  | 4678 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4679 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4680 | } | 
|  | 4681 |  | 
|  | 4682 | bool Attributor::checkForAllReturnedValues( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4683 | const function_ref<bool(Value &)> &Pred, | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4684 | const AbstractAttribute &QueryingAA) { | 
|  | 4685 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4686 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 4687 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4688 | if (!AssociatedFunction) | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4689 | return false; | 
|  | 4690 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4691 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 4692 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4693 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4694 | if (!AARetVal.getState().isValidState()) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4695 | return false; | 
|  | 4696 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4697 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( | 
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 4698 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { | 
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 4699 | return Pred(RV); | 
|  | 4700 | }); | 
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4701 | } | 
|  | 4702 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4703 | static bool | 
|  | 4704 | checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap, | 
|  | 4705 | const function_ref<bool(Instruction &)> &Pred, | 
|  | 4706 | const AAIsDead *LivenessAA, bool &AnyDead, | 
|  | 4707 | const ArrayRef<unsigned> &Opcodes) { | 
|  | 4708 | for (unsigned Opcode : Opcodes) { | 
|  | 4709 | for (Instruction *I : OpcodeInstMap[Opcode]) { | 
|  | 4710 | // Skip dead instructions. | 
|  | 4711 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { | 
|  | 4712 | AnyDead = true; | 
|  | 4713 | continue; | 
|  | 4714 | } | 
|  | 4715 |  | 
|  | 4716 | if (!Pred(*I)) | 
|  | 4717 | return false; | 
|  | 4718 | } | 
|  | 4719 | } | 
|  | 4720 | return true; | 
|  | 4721 | } | 
|  | 4722 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4723 | bool Attributor::checkForAllInstructions( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4724 | const llvm::function_ref<bool(Instruction &)> &Pred, | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 4725 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4726 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4727 | const IRPosition &IRP = QueryingAA.getIRPosition(); | 
|  | 4728 | // Since we need to provide instructions we have to have an exact definition. | 
|  | 4729 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4730 | if (!AssociatedFunction) | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4731 | return false; | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4732 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4733 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 4734 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4735 | const auto &LivenessAA = | 
|  | 4736 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); | 
|  | 4737 | bool AnyDead = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4738 |  | 
|  | 4739 | auto &OpcodeInstMap = | 
|  | 4740 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4741 | if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, | 
|  | 4742 | Opcodes)) | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4743 | return false; | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4744 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4745 | // If we actually used liveness information so we have to record a dependence. | 
|  | 4746 | if (AnyDead) | 
|  | 4747 | recordDependence(LivenessAA, QueryingAA); | 
|  | 4748 |  | 
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4749 | return true; | 
|  | 4750 | } | 
|  | 4751 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4752 | bool Attributor::checkForAllReadWriteInstructions( | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4753 | const llvm::function_ref<bool(Instruction &)> &Pred, | 
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 4754 | AbstractAttribute &QueryingAA) { | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4755 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4756 | const Function *AssociatedFunction = | 
|  | 4757 | QueryingAA.getIRPosition().getAssociatedFunction(); | 
|  | 4758 | if (!AssociatedFunction) | 
|  | 4759 | return false; | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4760 |  | 
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4761 | // TODO: use the function scope once we have call site AAReturnedValues. | 
|  | 4762 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); | 
|  | 4763 | const auto &LivenessAA = | 
|  | 4764 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4765 | bool AnyDead = false; | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4766 |  | 
|  | 4767 | for (Instruction *I : | 
|  | 4768 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4769 | // Skip dead instructions. | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4770 | if (LivenessAA.isAssumedDead(I)) { | 
|  | 4771 | AnyDead = true; | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4772 | continue; | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4773 | } | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4774 |  | 
|  | 4775 | if (!Pred(*I)) | 
|  | 4776 | return false; | 
|  | 4777 | } | 
|  | 4778 |  | 
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4779 | // If we actually used liveness information so we have to record a dependence. | 
|  | 4780 | if (AnyDead) | 
|  | 4781 | recordDependence(LivenessAA, QueryingAA); | 
|  | 4782 |  | 
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4783 | return true; | 
|  | 4784 | } | 
|  | 4785 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4786 | ChangeStatus Attributor::run(Module &M) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4787 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " | 
|  | 4788 | << AllAbstractAttributes.size() | 
|  | 4789 | << " abstract attributes.\n"); | 
|  | 4790 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 4791 | // Now that all abstract attributes are collected and initialized we start | 
|  | 4792 | // the abstract analysis. | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4793 |  | 
|  | 4794 | unsigned IterationCounter = 1; | 
|  | 4795 |  | 
|  | 4796 | SmallVector<AbstractAttribute *, 64> ChangedAAs; | 
|  | 4797 | SetVector<AbstractAttribute *> Worklist; | 
|  | 4798 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); | 
|  | 4799 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4800 | bool RecomputeDependences = false; | 
|  | 4801 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4802 | do { | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4803 | // Remember the size to determine new attributes. | 
|  | 4804 | size_t NumAAs = AllAbstractAttributes.size(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4805 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter | 
|  | 4806 | << ", Worklist size: " << Worklist.size() << "\n"); | 
|  | 4807 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4808 | // If dependences (=QueryMap) are recomputed we have to look at all abstract | 
|  | 4809 | // attributes again, regardless of what changed in the last iteration. | 
|  | 4810 | if (RecomputeDependences) { | 
|  | 4811 | LLVM_DEBUG( | 
|  | 4812 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); | 
|  | 4813 | QueryMap.clear(); | 
|  | 4814 | ChangedAAs.clear(); | 
|  | 4815 | Worklist.insert(AllAbstractAttributes.begin(), | 
|  | 4816 | AllAbstractAttributes.end()); | 
|  | 4817 | } | 
|  | 4818 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4819 | // Add all abstract attributes that are potentially dependent on one that | 
|  | 4820 | // changed to the work list. | 
|  | 4821 | for (AbstractAttribute *ChangedAA : ChangedAAs) { | 
|  | 4822 | auto &QuerriedAAs = QueryMap[ChangedAA]; | 
|  | 4823 | Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end()); | 
|  | 4824 | } | 
|  | 4825 |  | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 4826 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter | 
|  | 4827 | << ", Worklist+Dependent size: " << Worklist.size() | 
|  | 4828 | << "\n"); | 
|  | 4829 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4830 | // Reset the changed set. | 
|  | 4831 | ChangedAAs.clear(); | 
|  | 4832 |  | 
|  | 4833 | // Update all abstract attribute in the work list and record the ones that | 
|  | 4834 | // changed. | 
|  | 4835 | for (AbstractAttribute *AA : Worklist) | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 4836 | if (!AA->getState().isAtFixpoint() && !isAssumedDead(*AA, nullptr)) { | 
|  | 4837 | QueriedNonFixAA = false; | 
|  | 4838 | if (AA->update(*this) == ChangeStatus::CHANGED) { | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4839 | ChangedAAs.push_back(AA); | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 4840 | } else if (!QueriedNonFixAA) { | 
|  | 4841 | // If the attribute did not query any non-fix information, the state | 
|  | 4842 | // will not change and we can indicate that right away. | 
|  | 4843 | AA->getState().indicateOptimisticFixpoint(); | 
|  | 4844 | } | 
|  | 4845 | } | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4846 |  | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4847 | // Check if we recompute the dependences in the next iteration. | 
|  | 4848 | RecomputeDependences = (DepRecomputeInterval > 0 && | 
|  | 4849 | IterationCounter % DepRecomputeInterval == 0); | 
|  | 4850 |  | 
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 4851 | // Add attributes to the changed set if they have been created in the last | 
|  | 4852 | // iteration. | 
|  | 4853 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, | 
|  | 4854 | AllAbstractAttributes.end()); | 
|  | 4855 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4856 | // Reset the work list and repopulate with the changed abstract attributes. | 
|  | 4857 | // Note that dependent ones are added above. | 
|  | 4858 | Worklist.clear(); | 
|  | 4859 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); | 
|  | 4860 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 4861 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || | 
|  | 4862 | VerifyMaxFixpointIterations)); | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4863 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4864 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " | 
|  | 4865 | << IterationCounter << "/" << MaxFixpointIterations | 
|  | 4866 | << " iterations\n"); | 
|  | 4867 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 4868 | size_t NumFinalAAs = AllAbstractAttributes.size(); | 
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 4869 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4870 | // Reset abstract arguments not settled in a sound fixpoint by now. This | 
|  | 4871 | // happens when we stopped the fixpoint iteration early. Note that only the | 
|  | 4872 | // ones marked as "changed" *and* the ones transitively depending on them | 
|  | 4873 | // need to be reverted to a pessimistic state. Others might not be in a | 
|  | 4874 | // fixpoint state but we can use the optimistic results for them anyway. | 
|  | 4875 | SmallPtrSet<AbstractAttribute *, 32> Visited; | 
|  | 4876 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { | 
|  | 4877 | AbstractAttribute *ChangedAA = ChangedAAs[u]; | 
|  | 4878 | if (!Visited.insert(ChangedAA).second) | 
|  | 4879 | continue; | 
|  | 4880 |  | 
|  | 4881 | AbstractState &State = ChangedAA->getState(); | 
|  | 4882 | if (!State.isAtFixpoint()) { | 
|  | 4883 | State.indicatePessimisticFixpoint(); | 
|  | 4884 |  | 
|  | 4885 | NumAttributesTimedOut++; | 
|  | 4886 | } | 
|  | 4887 |  | 
|  | 4888 | auto &QuerriedAAs = QueryMap[ChangedAA]; | 
|  | 4889 | ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end()); | 
|  | 4890 | } | 
|  | 4891 |  | 
|  | 4892 | LLVM_DEBUG({ | 
|  | 4893 | if (!Visited.empty()) | 
|  | 4894 | dbgs() << "\n[Attributor] Finalized " << Visited.size() | 
|  | 4895 | << " abstract attributes.\n"; | 
|  | 4896 | }); | 
|  | 4897 |  | 
|  | 4898 | unsigned NumManifested = 0; | 
|  | 4899 | unsigned NumAtFixpoint = 0; | 
|  | 4900 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; | 
|  | 4901 | for (AbstractAttribute *AA : AllAbstractAttributes) { | 
|  | 4902 | AbstractState &State = AA->getState(); | 
|  | 4903 |  | 
|  | 4904 | // If there is not already a fixpoint reached, we can now take the | 
|  | 4905 | // optimistic state. This is correct because we enforced a pessimistic one | 
|  | 4906 | // on abstract attributes that were transitively dependent on a changed one | 
|  | 4907 | // already above. | 
|  | 4908 | if (!State.isAtFixpoint()) | 
|  | 4909 | State.indicateOptimisticFixpoint(); | 
|  | 4910 |  | 
|  | 4911 | // If the state is invalid, we do not try to manifest it. | 
|  | 4912 | if (!State.isValidState()) | 
|  | 4913 | continue; | 
|  | 4914 |  | 
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4915 | // Skip dead code. | 
|  | 4916 | if (isAssumedDead(*AA, nullptr)) | 
|  | 4917 | continue; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4918 | // Manifest the state and record if we changed the IR. | 
|  | 4919 | ChangeStatus LocalChange = AA->manifest(*this); | 
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4920 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) | 
|  | 4921 | AA->trackStatistics(); | 
|  | 4922 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4923 | ManifestChange = ManifestChange | LocalChange; | 
|  | 4924 |  | 
|  | 4925 | NumAtFixpoint++; | 
|  | 4926 | NumManifested += (LocalChange == ChangeStatus::CHANGED); | 
|  | 4927 | } | 
|  | 4928 |  | 
|  | 4929 | (void)NumManifested; | 
|  | 4930 | (void)NumAtFixpoint; | 
|  | 4931 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested | 
|  | 4932 | << " arguments while " << NumAtFixpoint | 
|  | 4933 | << " were in a valid fixpoint state\n"); | 
|  | 4934 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4935 | NumAttributesManifested += NumManifested; | 
|  | 4936 | NumAttributesValidFixpoint += NumAtFixpoint; | 
|  | 4937 |  | 
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 4938 | (void)NumFinalAAs; | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4939 | assert( | 
|  | 4940 | NumFinalAAs == AllAbstractAttributes.size() && | 
|  | 4941 | "Expected the final number of abstract attributes to remain unchanged!"); | 
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 4942 |  | 
|  | 4943 | // 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] | 4944 | { | 
|  | 4945 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " | 
|  | 4946 | << ToBeDeletedFunctions.size() << " functions and " | 
|  | 4947 | << ToBeDeletedBlocks.size() << " blocks and " | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 4948 | << ToBeDeletedInsts.size() << " instructions and " | 
|  | 4949 | << ToBeChangedUses.size() << " uses\n"); | 
|  | 4950 |  | 
|  | 4951 | SmallVector<Instruction *, 32> DeadInsts; | 
|  | 4952 | SmallVector<Instruction *, 32> TerminatorsToFold; | 
|  | 4953 | SmallVector<Instruction *, 32> UnreachablesToInsert; | 
|  | 4954 |  | 
|  | 4955 | for (auto &It : ToBeChangedUses) { | 
|  | 4956 | Use *U = It.first; | 
|  | 4957 | Value *NewV = It.second; | 
|  | 4958 | Value *OldV = U->get(); | 
|  | 4959 | LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() | 
|  | 4960 | << " instead of " << *OldV << "\n"); | 
|  | 4961 | U->set(NewV); | 
|  | 4962 | if (Instruction *I = dyn_cast<Instruction>(OldV)) | 
|  | 4963 | if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && isInstructionTriviallyDead(I)) { | 
|  | 4964 | DeadInsts.push_back(I); | 
|  | 4965 | } | 
|  | 4966 | if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { | 
|  | 4967 | Instruction *UserI = cast<Instruction>(U->getUser()); | 
|  | 4968 | if (isa<UndefValue>(NewV)) { | 
|  | 4969 | UnreachablesToInsert.push_back(UserI); | 
|  | 4970 | } else { | 
|  | 4971 | TerminatorsToFold.push_back(UserI); | 
|  | 4972 | } | 
|  | 4973 | } | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4974 | } | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 4975 | for (Instruction *I : UnreachablesToInsert) | 
|  | 4976 | changeToUnreachable(I, /* UseLLVMTrap */ false); | 
|  | 4977 | for (Instruction *I : TerminatorsToFold) | 
|  | 4978 | ConstantFoldTerminator(I->getParent()); | 
|  | 4979 |  | 
|  | 4980 | for (Instruction *I : ToBeDeletedInsts) { | 
|  | 4981 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | 
|  | 4982 | if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) | 
|  | 4983 | DeadInsts.push_back(I); | 
|  | 4984 | else | 
|  | 4985 | I->eraseFromParent(); | 
|  | 4986 | } | 
|  | 4987 |  | 
|  | 4988 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 4989 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4990 | if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { | 
|  | 4991 | SmallVector<BasicBlock *, 8> ToBeDeletedBBs; | 
|  | 4992 | ToBeDeletedBBs.reserve(NumDeadBlocks); | 
|  | 4993 | ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end()); | 
| Johannes Doerfert | 5e442a5 | 2019-10-30 17:34:59 -0500 | [diff] [blame] | 4994 | // Actually we do not delete the blocks but squash them into a single | 
|  | 4995 | // unreachable but untangling branches that jump here is something we need | 
|  | 4996 | // to do in a more generic way. | 
|  | 4997 | DetatchDeadBlocks(ToBeDeletedBBs, nullptr); | 
|  | 4998 | STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); | 
|  | 4999 | BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5000 | } | 
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 5001 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5002 | STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); | 
|  | 5003 | for (Function *Fn : ToBeDeletedFunctions) { | 
|  | 5004 | Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); | 
|  | 5005 | Fn->eraseFromParent(); | 
|  | 5006 | STATS_TRACK(AAIsDead, Function); | 
|  | 5007 | } | 
|  | 5008 |  | 
|  | 5009 | // Identify dead internal functions and delete them. This happens outside | 
|  | 5010 | // the other fixpoint analysis as we might treat potentially dead functions | 
|  | 5011 | // as live to lower the number of iterations. If they happen to be dead, the | 
|  | 5012 | // below fixpoint loop will identify and eliminate them. | 
|  | 5013 | SmallVector<Function *, 8> InternalFns; | 
|  | 5014 | for (Function &F : M) | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 5015 | if (F.hasLocalLinkage()) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5016 | InternalFns.push_back(&F); | 
|  | 5017 |  | 
|  | 5018 | bool FoundDeadFn = true; | 
|  | 5019 | while (FoundDeadFn) { | 
|  | 5020 | FoundDeadFn = false; | 
|  | 5021 | for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { | 
|  | 5022 | Function *F = InternalFns[u]; | 
|  | 5023 | if (!F) | 
|  | 5024 | continue; | 
|  | 5025 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5026 | if (!checkForAllCallSites([](AbstractCallSite ACS) { return false; }, | 
|  | 5027 | *F, true, nullptr)) | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5028 | continue; | 
|  | 5029 |  | 
|  | 5030 | STATS_TRACK(AAIsDead, Function); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5031 | ToBeDeletedFunctions.insert(F); | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5032 | F->replaceAllUsesWith(UndefValue::get(F->getType())); | 
|  | 5033 | F->eraseFromParent(); | 
|  | 5034 | InternalFns[u] = nullptr; | 
|  | 5035 | FoundDeadFn = true; | 
|  | 5036 | } | 
|  | 5037 | } | 
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 5038 | } | 
|  | 5039 |  | 
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 5040 | if (VerifyMaxFixpointIterations && | 
|  | 5041 | IterationCounter != MaxFixpointIterations) { | 
|  | 5042 | errs() << "\n[Attributor] Fixpoint iteration done after: " | 
|  | 5043 | << IterationCounter << "/" << MaxFixpointIterations | 
|  | 5044 | << " iterations\n"; | 
|  | 5045 | llvm_unreachable("The fixpoint was not reached with exactly the number of " | 
|  | 5046 | "specified iterations!"); | 
|  | 5047 | } | 
|  | 5048 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5049 | return ManifestChange; | 
|  | 5050 | } | 
|  | 5051 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5052 | void Attributor::initializeInformationCache(Function &F) { | 
|  | 5053 |  | 
|  | 5054 | // Walk all instructions to find interesting instructions that might be | 
|  | 5055 | // queried by abstract attributes during their initialization or update. | 
|  | 5056 | // This has to happen before we create attributes. | 
|  | 5057 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; | 
|  | 5058 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; | 
|  | 5059 |  | 
|  | 5060 | for (Instruction &I : instructions(&F)) { | 
|  | 5061 | bool IsInterestingOpcode = false; | 
|  | 5062 |  | 
|  | 5063 | // To allow easy access to all instructions in a function with a given | 
|  | 5064 | // opcode we store them in the InfoCache. As not all opcodes are interesting | 
|  | 5065 | // to concrete attributes we only cache the ones that are as identified in | 
|  | 5066 | // the following switch. | 
|  | 5067 | // Note: There are no concrete attributes now so this is initially empty. | 
|  | 5068 | switch (I.getOpcode()) { | 
|  | 5069 | default: | 
|  | 5070 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && | 
|  | 5071 | "New call site/base instruction type needs to be known int the " | 
|  | 5072 | "Attributor."); | 
|  | 5073 | break; | 
|  | 5074 | case Instruction::Load: | 
|  | 5075 | // The alignment of a pointer is interesting for loads. | 
|  | 5076 | case Instruction::Store: | 
|  | 5077 | // The alignment of a pointer is interesting for stores. | 
|  | 5078 | case Instruction::Call: | 
|  | 5079 | case Instruction::CallBr: | 
|  | 5080 | case Instruction::Invoke: | 
|  | 5081 | case Instruction::CleanupRet: | 
|  | 5082 | case Instruction::CatchSwitch: | 
|  | 5083 | case Instruction::Resume: | 
|  | 5084 | case Instruction::Ret: | 
|  | 5085 | IsInterestingOpcode = true; | 
|  | 5086 | } | 
|  | 5087 | if (IsInterestingOpcode) | 
|  | 5088 | InstOpcodeMap[I.getOpcode()].push_back(&I); | 
|  | 5089 | if (I.mayReadOrWriteMemory()) | 
|  | 5090 | ReadOrWriteInsts.push_back(&I); | 
|  | 5091 | } | 
|  | 5092 | } | 
|  | 5093 |  | 
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 5094 | void Attributor::recordDependence(const AbstractAttribute &FromAA, | 
|  | 5095 | const AbstractAttribute &ToAA) { | 
| Johannes Doerfert | 2dad729 | 2019-10-13 21:10:31 -0500 | [diff] [blame] | 5096 | if (FromAA.getState().isAtFixpoint()) | 
|  | 5097 | return; | 
|  | 5098 |  | 
|  | 5099 | QueryMap[&FromAA].insert(const_cast<AbstractAttribute *>(&ToAA)); | 
|  | 5100 | QueriedNonFixAA = true; | 
| Johannes Doerfert | 12173e6 | 2019-10-13 20:25:25 -0500 | [diff] [blame] | 5101 | } | 
|  | 5102 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 5103 | void Attributor::identifyDefaultAbstractAttributes(Function &F) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5104 | if (!VisitedFunctions.insert(&F).second) | 
|  | 5105 | return; | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 5106 | if (F.isDeclaration()) | 
|  | 5107 | return; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5108 |  | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5109 | IRPosition FPos = IRPosition::function(F); | 
|  | 5110 |  | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 5111 | // Check for dead BasicBlocks in every function. | 
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 5112 | // We need dead instruction detection because we do not want to deal with | 
|  | 5113 | // broken IR in which SSA rules do not apply. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5114 | getOrCreateAAFor<AAIsDead>(FPos); | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 5115 |  | 
|  | 5116 | // Every function might be "will-return". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5117 | getOrCreateAAFor<AAWillReturn>(FPos); | 
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 5118 |  | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 5119 | // Every function can be nounwind. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5120 | getOrCreateAAFor<AANoUnwind>(FPos); | 
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 5121 |  | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 5122 | // Every function might be marked "nosync" | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5123 | getOrCreateAAFor<AANoSync>(FPos); | 
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 5124 |  | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 5125 | // Every function might be "no-free". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5126 | getOrCreateAAFor<AANoFree>(FPos); | 
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 5127 |  | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 5128 | // Every function might be "no-return". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5129 | getOrCreateAAFor<AANoReturn>(FPos); | 
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 5130 |  | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 5131 | // Every function might be "no-recurse". | 
|  | 5132 | getOrCreateAAFor<AANoRecurse>(FPos); | 
|  | 5133 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5134 | // Every function might be "readnone/readonly/writeonly/...". | 
|  | 5135 | getOrCreateAAFor<AAMemoryBehavior>(FPos); | 
|  | 5136 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5137 | // Every function might be applicable for Heap-To-Stack conversion. | 
|  | 5138 | if (EnableHeapToStack) | 
|  | 5139 | getOrCreateAAFor<AAHeapToStack>(FPos); | 
|  | 5140 |  | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 5141 | // Return attributes are only appropriate if the return type is non void. | 
|  | 5142 | Type *ReturnType = F.getReturnType(); | 
|  | 5143 | if (!ReturnType->isVoidTy()) { | 
|  | 5144 | // Argument attribute "returned" --- Create only one per function even | 
|  | 5145 | // though it is an argument attribute. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5146 | getOrCreateAAFor<AAReturnedValues>(FPos); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5147 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5148 | IRPosition RetPos = IRPosition::returned(F); | 
|  | 5149 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5150 | // Every returned value might be dead. | 
|  | 5151 | getOrCreateAAFor<AAIsDead>(RetPos); | 
|  | 5152 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5153 | // Every function might be simplified. | 
|  | 5154 | getOrCreateAAFor<AAValueSimplify>(RetPos); | 
|  | 5155 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 5156 | if (ReturnType->isPointerTy()) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5157 |  | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 5158 | // Every function with pointer return type might be marked align. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5159 | getOrCreateAAFor<AAAlign>(RetPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 5160 |  | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 5161 | // Every function with pointer return type might be marked nonnull. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5162 | getOrCreateAAFor<AANonNull>(RetPos); | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 5163 |  | 
|  | 5164 | // Every function with pointer return type might be marked noalias. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5165 | getOrCreateAAFor<AANoAlias>(RetPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5166 |  | 
|  | 5167 | // Every function with pointer return type might be marked | 
|  | 5168 | // dereferenceable. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5169 | getOrCreateAAFor<AADereferenceable>(RetPos); | 
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 5170 | } | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5171 | } | 
|  | 5172 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5173 | for (Argument &Arg : F.args()) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5174 | IRPosition ArgPos = IRPosition::argument(Arg); | 
|  | 5175 |  | 
|  | 5176 | // Every argument might be simplified. | 
|  | 5177 | getOrCreateAAFor<AAValueSimplify>(ArgPos); | 
|  | 5178 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5179 | if (Arg.getType()->isPointerTy()) { | 
|  | 5180 | // Every argument with pointer type might be marked nonnull. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5181 | getOrCreateAAFor<AANonNull>(ArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5182 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 5183 | // Every argument with pointer type might be marked noalias. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5184 | getOrCreateAAFor<AANoAlias>(ArgPos); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 5185 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5186 | // Every argument with pointer type might be marked dereferenceable. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5187 | getOrCreateAAFor<AADereferenceable>(ArgPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 5188 |  | 
|  | 5189 | // Every argument with pointer type might be marked align. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5190 | getOrCreateAAFor<AAAlign>(ArgPos); | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 5191 |  | 
|  | 5192 | // Every argument with pointer type might be marked nocapture. | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5193 | getOrCreateAAFor<AANoCapture>(ArgPos); | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5194 |  | 
|  | 5195 | // Every argument with pointer type might be marked | 
|  | 5196 | // "readnone/readonly/writeonly/..." | 
|  | 5197 | getOrCreateAAFor<AAMemoryBehavior>(ArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5198 | } | 
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 5199 | } | 
|  | 5200 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5201 | auto CallSitePred = [&](Instruction &I) -> bool { | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5202 | CallSite CS(&I); | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5203 | if (Function *Callee = CS.getCalledFunction()) { | 
| Johannes Doerfert | c36e2eb | 2019-10-31 20:15:02 -0500 | [diff] [blame] | 5204 | // Skip declerations except if annotations on their call sites were | 
|  | 5205 | // explicitly requested. | 
|  | 5206 | if (!AnnotateDeclarationCallSites && Callee->isDeclaration()) | 
|  | 5207 | return true; | 
|  | 5208 |  | 
|  | 5209 | if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5210 | IRPosition CSRetPos = IRPosition::callsite_returned(CS); | 
|  | 5211 |  | 
|  | 5212 | // Call site return values might be dead. | 
|  | 5213 | getOrCreateAAFor<AAIsDead>(CSRetPos); | 
|  | 5214 | } | 
|  | 5215 |  | 
|  | 5216 | for (int i = 0, e = Callee->arg_size(); i < e; i++) { | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5217 |  | 
|  | 5218 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); | 
|  | 5219 |  | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5220 | // Every call site argument might be dead. | 
|  | 5221 | getOrCreateAAFor<AAIsDead>(CSArgPos); | 
|  | 5222 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5223 | // Call site argument might be simplified. | 
|  | 5224 | getOrCreateAAFor<AAValueSimplify>(CSArgPos); | 
|  | 5225 |  | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5226 | if (!CS.getArgument(i)->getType()->isPointerTy()) | 
|  | 5227 | continue; | 
|  | 5228 |  | 
|  | 5229 | // Call site argument attribute "non-null". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5230 | getOrCreateAAFor<AANonNull>(CSArgPos); | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5231 |  | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 5232 | // Call site argument attribute "no-alias". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5233 | getOrCreateAAFor<AANoAlias>(CSArgPos); | 
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 5234 |  | 
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 5235 | // Call site argument attribute "dereferenceable". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5236 | getOrCreateAAFor<AADereferenceable>(CSArgPos); | 
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 5237 |  | 
|  | 5238 | // Call site argument attribute "align". | 
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 5239 | getOrCreateAAFor<AAAlign>(CSArgPos); | 
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 5240 | } | 
|  | 5241 | } | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5242 | return true; | 
|  | 5243 | }; | 
|  | 5244 |  | 
|  | 5245 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); | 
|  | 5246 | bool Success, AnyDead = false; | 
|  | 5247 | Success = checkForAllInstructionsImpl( | 
|  | 5248 | OpcodeInstMap, CallSitePred, nullptr, AnyDead, | 
|  | 5249 | {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, | 
|  | 5250 | (unsigned)Instruction::Call}); | 
|  | 5251 | (void)Success; | 
|  | 5252 | assert(Success && !AnyDead && "Expected the check call to be successful!"); | 
|  | 5253 |  | 
|  | 5254 | auto LoadStorePred = [&](Instruction &I) -> bool { | 
|  | 5255 | if (isa<LoadInst>(I)) | 
|  | 5256 | getOrCreateAAFor<AAAlign>( | 
|  | 5257 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); | 
|  | 5258 | else | 
|  | 5259 | getOrCreateAAFor<AAAlign>( | 
|  | 5260 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); | 
|  | 5261 | return true; | 
|  | 5262 | }; | 
|  | 5263 | Success = checkForAllInstructionsImpl( | 
|  | 5264 | OpcodeInstMap, LoadStorePred, nullptr, AnyDead, | 
|  | 5265 | {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); | 
|  | 5266 | (void)Success; | 
|  | 5267 | assert(Success && !AnyDead && "Expected the check call to be successful!"); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5268 | } | 
|  | 5269 |  | 
|  | 5270 | /// Helpers to ease debugging through output streams and print calls. | 
|  | 5271 | /// | 
|  | 5272 | ///{ | 
|  | 5273 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { | 
|  | 5274 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); | 
|  | 5275 | } | 
|  | 5276 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 5277 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5278 | switch (AP) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5279 | case IRPosition::IRP_INVALID: | 
|  | 5280 | return OS << "inv"; | 
|  | 5281 | case IRPosition::IRP_FLOAT: | 
|  | 5282 | return OS << "flt"; | 
|  | 5283 | case IRPosition::IRP_RETURNED: | 
|  | 5284 | return OS << "fn_ret"; | 
|  | 5285 | case IRPosition::IRP_CALL_SITE_RETURNED: | 
|  | 5286 | return OS << "cs_ret"; | 
|  | 5287 | case IRPosition::IRP_FUNCTION: | 
|  | 5288 | return OS << "fn"; | 
|  | 5289 | case IRPosition::IRP_CALL_SITE: | 
|  | 5290 | return OS << "cs"; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 5291 | case IRPosition::IRP_ARGUMENT: | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5292 | return OS << "arg"; | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 5293 | case IRPosition::IRP_CALL_SITE_ARGUMENT: | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5294 | return OS << "cs_arg"; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5295 | } | 
|  | 5296 | llvm_unreachable("Unknown attribute position!"); | 
|  | 5297 | } | 
|  | 5298 |  | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 5299 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { | 
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 5300 | const Value &AV = Pos.getAssociatedValue(); | 
|  | 5301 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 5302 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; | 
|  | 5303 | } | 
|  | 5304 |  | 
| Johannes Doerfert | 1a74645 | 2019-10-20 22:28:49 -0500 | [diff] [blame] | 5305 | template <typename base_ty, base_ty BestState, base_ty WorstState> | 
|  | 5306 | raw_ostream &llvm:: | 
|  | 5307 | operator<<(raw_ostream &OS, | 
|  | 5308 | const IntegerStateBase<base_ty, BestState, WorstState> &S) { | 
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 5309 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" | 
|  | 5310 | << static_cast<const AbstractState &>(S); | 
|  | 5311 | } | 
|  | 5312 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5313 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { | 
|  | 5314 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); | 
|  | 5315 | } | 
|  | 5316 |  | 
|  | 5317 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { | 
|  | 5318 | AA.print(OS); | 
|  | 5319 | return OS; | 
|  | 5320 | } | 
|  | 5321 |  | 
|  | 5322 | void AbstractAttribute::print(raw_ostream &OS) const { | 
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 5323 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() | 
|  | 5324 | << "]"; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5325 | } | 
|  | 5326 | ///} | 
|  | 5327 |  | 
|  | 5328 | /// ---------------------------------------------------------------------------- | 
|  | 5329 | ///                       Pass (Manager) Boilerplate | 
|  | 5330 | /// ---------------------------------------------------------------------------- | 
|  | 5331 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 5332 | static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5333 | if (DisableAttributor) | 
|  | 5334 | return false; | 
|  | 5335 |  | 
|  | 5336 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() | 
|  | 5337 | << " functions.\n"); | 
|  | 5338 |  | 
|  | 5339 | // Create an Attributor and initially empty information cache that is filled | 
|  | 5340 | // while we identify default attribute opportunities. | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 5341 | InformationCache InfoCache(M, AG); | 
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 5342 | Attributor A(InfoCache, DepRecInterval); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5343 |  | 
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 5344 | for (Function &F : M) | 
|  | 5345 | A.initializeInformationCache(F); | 
|  | 5346 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5347 | for (Function &F : M) { | 
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 5348 | if (F.hasExactDefinition()) | 
|  | 5349 | NumFnWithExactDefinition++; | 
|  | 5350 | else | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5351 | NumFnWithoutExactDefinition++; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5352 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5353 | // We look at internal functions only on-demand but if any use is not a | 
|  | 5354 | // direct call, we have to do it eagerly. | 
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 5355 | if (F.hasLocalLinkage()) { | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5356 | if (llvm::all_of(F.uses(), [](const Use &U) { | 
|  | 5357 | return ImmutableCallSite(U.getUser()) && | 
|  | 5358 | ImmutableCallSite(U.getUser()).isCallee(&U); | 
|  | 5359 | })) | 
|  | 5360 | continue; | 
|  | 5361 | } | 
|  | 5362 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5363 | // Populate the Attributor with abstract attribute opportunities in the | 
|  | 5364 | // function and the information cache with IR information. | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 5365 | A.identifyDefaultAbstractAttributes(F); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5366 | } | 
|  | 5367 |  | 
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 5368 | return A.run(M) == ChangeStatus::CHANGED; | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5369 | } | 
|  | 5370 |  | 
|  | 5371 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { | 
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 5372 | AnalysisGetter AG(AM); | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 5373 | if (runAttributorOnModule(M, AG)) { | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5374 | // FIXME: Think about passes we will preserve and add them here. | 
|  | 5375 | return PreservedAnalyses::none(); | 
|  | 5376 | } | 
|  | 5377 | return PreservedAnalyses::all(); | 
|  | 5378 | } | 
|  | 5379 |  | 
|  | 5380 | namespace { | 
|  | 5381 |  | 
|  | 5382 | struct AttributorLegacyPass : public ModulePass { | 
|  | 5383 | static char ID; | 
|  | 5384 |  | 
|  | 5385 | AttributorLegacyPass() : ModulePass(ID) { | 
|  | 5386 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); | 
|  | 5387 | } | 
|  | 5388 |  | 
|  | 5389 | bool runOnModule(Module &M) override { | 
|  | 5390 | if (skipModule(M)) | 
|  | 5391 | return false; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5392 |  | 
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 5393 | AnalysisGetter AG; | 
|  | 5394 | return runAttributorOnModule(M, AG); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5395 | } | 
|  | 5396 |  | 
|  | 5397 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 5398 | // FIXME: Think about passes we will preserve and add them here. | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5399 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5400 | } | 
|  | 5401 | }; | 
|  | 5402 |  | 
|  | 5403 | } // end anonymous namespace | 
|  | 5404 |  | 
|  | 5405 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } | 
|  | 5406 |  | 
|  | 5407 | char AttributorLegacyPass::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 5408 |  | 
|  | 5409 | const char AAReturnedValues::ID = 0; | 
|  | 5410 | const char AANoUnwind::ID = 0; | 
|  | 5411 | const char AANoSync::ID = 0; | 
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 5412 | const char AANoFree::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 5413 | const char AANonNull::ID = 0; | 
|  | 5414 | const char AANoRecurse::ID = 0; | 
|  | 5415 | const char AAWillReturn::ID = 0; | 
|  | 5416 | const char AANoAlias::ID = 0; | 
|  | 5417 | const char AANoReturn::ID = 0; | 
|  | 5418 | const char AAIsDead::ID = 0; | 
|  | 5419 | const char AADereferenceable::ID = 0; | 
|  | 5420 | const char AAAlign::ID = 0; | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 5421 | const char AANoCapture::ID = 0; | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5422 | const char AAValueSimplify::ID = 0; | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5423 | const char AAHeapToStack::ID = 0; | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5424 | const char AAMemoryBehavior::ID = 0; | 
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 5425 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5426 | // Macro magic to create the static generator function for attributes that | 
|  | 5427 | // follow the naming scheme. | 
|  | 5428 |  | 
|  | 5429 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \ | 
|  | 5430 | case IRPosition::PK:                                                         \ | 
|  | 5431 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); | 
|  | 5432 |  | 
|  | 5433 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \ | 
|  | 5434 | case IRPosition::PK:                                                         \ | 
|  | 5435 | AA = new CLASS##SUFFIX(IRP);                                               \ | 
|  | 5436 | break; | 
|  | 5437 |  | 
|  | 5438 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \ | 
|  | 5439 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 5440 | CLASS *AA = nullptr;                                                       \ | 
|  | 5441 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 5442 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 5443 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \ | 
|  | 5444 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \ | 
|  | 5445 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 5446 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \ | 
|  | 5447 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \ | 
|  | 5448 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 5449 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 5450 | }                                                                          \ | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5451 | return *AA;                                                                \ | 
|  | 5452 | } | 
|  | 5453 |  | 
|  | 5454 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \ | 
|  | 5455 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 5456 | CLASS *AA = nullptr;                                                       \ | 
|  | 5457 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 5458 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 5459 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \ | 
|  | 5460 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \ | 
|  | 5461 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 5462 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 5463 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \ | 
|  | 5464 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 5465 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 5466 | }                                                                          \ | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5467 | return *AA;                                                                \ | 
|  | 5468 | } | 
|  | 5469 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5470 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \ | 
|  | 5471 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 5472 | CLASS *AA = nullptr;                                                       \ | 
|  | 5473 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 5474 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 5475 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 5476 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 5477 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 5478 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 5479 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \ | 
|  | 5480 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 5481 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 5482 | }                                                                          \ | 
|  | 5483 | return *AA;                                                                \ | 
|  | 5484 | } | 
|  | 5485 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5486 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \ | 
|  | 5487 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 5488 | CLASS *AA = nullptr;                                                       \ | 
|  | 5489 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 5490 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 5491 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \ | 
|  | 5492 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \ | 
|  | 5493 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 5494 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \ | 
|  | 5495 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \ | 
|  | 5496 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \ | 
|  | 5497 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 5498 | }                                                                          \ | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5499 | return *AA;                                                                \ | 
|  | 5500 | } | 
|  | 5501 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5502 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \ | 
|  | 5503 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \ | 
|  | 5504 | CLASS *AA = nullptr;                                                       \ | 
|  | 5505 | switch (IRP.getPositionKind()) {                                           \ | 
|  | 5506 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \ | 
|  | 5507 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \ | 
|  | 5508 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \ | 
|  | 5509 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \ | 
|  | 5510 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \ | 
|  | 5511 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \ | 
|  | 5512 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \ | 
|  | 5513 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \ | 
|  | 5514 | }                                                                          \ | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5515 | return *AA;                                                                \ | 
|  | 5516 | } | 
|  | 5517 |  | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5518 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) | 
|  | 5519 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) | 
|  | 5520 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) | 
|  | 5521 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) | 
|  | 5522 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) | 
|  | 5523 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5524 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) | 
|  | 5525 |  | 
|  | 5526 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) | 
|  | 5527 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) | 
|  | 5528 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) | 
|  | 5529 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) | 
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 5530 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5531 |  | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5532 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) | 
| Johannes Doerfert | cd4aab4 | 2019-10-13 03:08:18 -0500 | [diff] [blame] | 5533 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5534 |  | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5535 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) | 
|  | 5536 |  | 
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5537 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) | 
|  | 5538 |  | 
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 5539 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5540 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 5541 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5542 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5543 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION | 
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5544 | #undef SWITCH_PK_CREATE | 
|  | 5545 | #undef SWITCH_PK_INV | 
|  | 5546 |  | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5547 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", | 
|  | 5548 | "Deduce and propagate attributes", false, false) | 
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5549 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) | 
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5550 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", | 
|  | 5551 | "Deduce and propagate attributes", false, false) |