| 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 | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 123 | static cl::opt<bool> ManifestInternal( |
| 124 | "attributor-manifest-internal", cl::Hidden, |
| 125 | cl::desc("Manifest Attributor internal string attributes."), |
| 126 | cl::init(false)); |
| 127 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 128 | static cl::opt<unsigned> DepRecInterval( |
| 129 | "attributor-dependence-recompute-interval", cl::Hidden, |
| 130 | cl::desc("Number of iterations until dependences are recomputed."), |
| 131 | cl::init(4)); |
| 132 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 133 | static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", |
| 134 | cl::init(true), cl::Hidden); |
| 135 | |
| Johannes Doerfert | 1c2afae | 2019-10-10 05:34:21 +0000 | [diff] [blame] | 136 | static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), |
| 137 | cl::Hidden); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 138 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 139 | /// Logic operators for the change status enum class. |
| 140 | /// |
| 141 | ///{ |
| 142 | ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { |
| 143 | return l == ChangeStatus::CHANGED ? l : r; |
| 144 | } |
| 145 | ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { |
| 146 | return l == ChangeStatus::UNCHANGED ? l : r; |
| 147 | } |
| 148 | ///} |
| 149 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 150 | /// 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] | 151 | /// will be done by looking through cast instructions, selects, phis, and calls |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 152 | /// with the "returned" attribute. Once we cannot look through the value any |
| 153 | /// further, the callback \p VisitValueCB is invoked and passed the current |
| 154 | /// value, the \p State, and a flag to indicate if we stripped anything. To |
| 155 | /// limit how much effort is invested, we will never visit more values than |
| 156 | /// specified by \p MaxValues. |
| 157 | template <typename AAType, typename StateTy> |
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 158 | static bool genericValueTraversal( |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 159 | Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 160 | const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 161 | int MaxValues = 8) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 162 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 163 | const AAIsDead *LivenessAA = nullptr; |
| 164 | if (IRP.getAnchorScope()) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 165 | LivenessAA = &A.getAAFor<AAIsDead>( |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 166 | QueryingAA, IRPosition::function(*IRP.getAnchorScope()), |
| 167 | /* TrackDependence */ false); |
| 168 | bool AnyDead = false; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 169 | |
| 170 | // TODO: Use Positions here to allow context sensitivity in VisitValueCB |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 171 | SmallPtrSet<Value *, 16> Visited; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 172 | SmallVector<Value *, 16> Worklist; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 173 | Worklist.push_back(&IRP.getAssociatedValue()); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 174 | |
| 175 | int Iteration = 0; |
| 176 | do { |
| 177 | Value *V = Worklist.pop_back_val(); |
| 178 | |
| 179 | // Check if we should process the current value. To prevent endless |
| 180 | // recursion keep a record of the values we followed! |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 181 | if (!Visited.insert(V).second) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 182 | continue; |
| 183 | |
| 184 | // Make sure we limit the compile time for complex expressions. |
| 185 | if (Iteration++ >= MaxValues) |
| 186 | return false; |
| 187 | |
| 188 | // Explicitly look through calls with a "returned" attribute if we do |
| 189 | // not have a pointer as stripPointerCasts only works on them. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 190 | Value *NewV = nullptr; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 191 | if (V->getType()->isPointerTy()) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 192 | NewV = V->stripPointerCasts(); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 193 | } else { |
| 194 | CallSite CS(V); |
| 195 | if (CS && CS.getCalledFunction()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 196 | for (Argument &Arg : CS.getCalledFunction()->args()) |
| 197 | if (Arg.hasReturnedAttr()) { |
| 198 | NewV = CS.getArgOperand(Arg.getArgNo()); |
| 199 | break; |
| 200 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 203 | if (NewV && NewV != V) { |
| 204 | Worklist.push_back(NewV); |
| 205 | continue; |
| 206 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 207 | |
| 208 | // Look through select instructions, visit both potential values. |
| 209 | if (auto *SI = dyn_cast<SelectInst>(V)) { |
| 210 | Worklist.push_back(SI->getTrueValue()); |
| 211 | Worklist.push_back(SI->getFalseValue()); |
| 212 | continue; |
| 213 | } |
| 214 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 215 | // Look through phi nodes, visit all live operands. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 216 | if (auto *PHI = dyn_cast<PHINode>(V)) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 217 | assert(LivenessAA && |
| 218 | "Expected liveness in the presence of instructions!"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 219 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { |
| 220 | const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 221 | if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 222 | AnyDead = true; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 223 | continue; |
| 224 | } |
| 225 | Worklist.push_back(PHI->getIncomingValue(u)); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 226 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 227 | continue; |
| 228 | } |
| 229 | |
| 230 | // Once a leaf is reached we inform the user through the callback. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 231 | if (!VisitValueCB(*V, State, Iteration > 1)) |
| 232 | return false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 233 | } while (!Worklist.empty()); |
| 234 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 235 | // If we actually used liveness information so we have to record a dependence. |
| 236 | if (AnyDead) |
| 237 | A.recordDependence(*LivenessAA, QueryingAA); |
| 238 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 239 | // All values have been visited. |
| 240 | return true; |
| 241 | } |
| 242 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 243 | /// Return true if \p New is equal or worse than \p Old. |
| 244 | static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { |
| 245 | if (!Old.isIntAttribute()) |
| 246 | return true; |
| 247 | |
| 248 | return Old.getValueAsInt() >= New.getValueAsInt(); |
| 249 | } |
| 250 | |
| 251 | /// Return true if the information provided by \p Attr was added to the |
| 252 | /// 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] | 253 | /// 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] | 254 | static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 255 | AttributeList &Attrs, int AttrIdx) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 256 | |
| 257 | if (Attr.isEnumAttribute()) { |
| 258 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
| 259 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 260 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 261 | return false; |
| 262 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 263 | return true; |
| 264 | } |
| 265 | if (Attr.isStringAttribute()) { |
| 266 | StringRef Kind = Attr.getKindAsString(); |
| 267 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 268 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 269 | return false; |
| 270 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 271 | return true; |
| 272 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 273 | if (Attr.isIntAttribute()) { |
| 274 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
| 275 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 276 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 277 | return false; |
| 278 | Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); |
| 279 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 280 | return true; |
| 281 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 282 | |
| 283 | llvm_unreachable("Expected enum or string attribute!"); |
| 284 | } |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 285 | static const Value *getPointerOperand(const Instruction *I) { |
| 286 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 287 | if (!LI->isVolatile()) |
| 288 | return LI->getPointerOperand(); |
| 289 | |
| 290 | if (auto *SI = dyn_cast<StoreInst>(I)) |
| 291 | if (!SI->isVolatile()) |
| 292 | return SI->getPointerOperand(); |
| 293 | |
| 294 | if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) |
| 295 | if (!CXI->isVolatile()) |
| 296 | return CXI->getPointerOperand(); |
| 297 | |
| 298 | if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) |
| 299 | if (!RMWI->isVolatile()) |
| 300 | return RMWI->getPointerOperand(); |
| 301 | |
| 302 | return nullptr; |
| 303 | } |
| 304 | static const Value *getBasePointerOfAccessPointerOperand(const Instruction *I, |
| 305 | int64_t &BytesOffset, |
| 306 | const DataLayout &DL) { |
| 307 | const Value *Ptr = getPointerOperand(I); |
| 308 | if (!Ptr) |
| 309 | return nullptr; |
| 310 | |
| 311 | return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, |
| 312 | /*AllowNonInbounds*/ false); |
| 313 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 314 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 315 | ChangeStatus AbstractAttribute::update(Attributor &A) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 316 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| 317 | if (getState().isAtFixpoint()) |
| 318 | return HasChanged; |
| 319 | |
| 320 | LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); |
| 321 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 322 | HasChanged = updateImpl(A); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 323 | |
| 324 | LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this |
| 325 | << "\n"); |
| 326 | |
| 327 | return HasChanged; |
| 328 | } |
| 329 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 330 | ChangeStatus |
| 331 | IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP, |
| 332 | const ArrayRef<Attribute> &DeducedAttrs) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 333 | Function *ScopeFn = IRP.getAssociatedFunction(); |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 334 | IRPosition::Kind PK = IRP.getPositionKind(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 335 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 336 | // In the following some generic code that will manifest attributes in |
| 337 | // DeducedAttrs if they improve the current IR. Due to the different |
| 338 | // annotation positions we use the underlying AttributeList interface. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 339 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 340 | AttributeList Attrs; |
| 341 | switch (PK) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 342 | case IRPosition::IRP_INVALID: |
| 343 | case IRPosition::IRP_FLOAT: |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 344 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 345 | case IRPosition::IRP_ARGUMENT: |
| 346 | case IRPosition::IRP_FUNCTION: |
| 347 | case IRPosition::IRP_RETURNED: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 348 | Attrs = ScopeFn->getAttributes(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 349 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 350 | case IRPosition::IRP_CALL_SITE: |
| 351 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 352 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 353 | Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 354 | break; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 357 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 358 | LLVMContext &Ctx = IRP.getAnchorValue().getContext(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 359 | for (const Attribute &Attr : DeducedAttrs) { |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 360 | if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 361 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 362 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 363 | HasChanged = ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (HasChanged == ChangeStatus::UNCHANGED) |
| 367 | return HasChanged; |
| 368 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 369 | switch (PK) { |
| 370 | case IRPosition::IRP_ARGUMENT: |
| 371 | case IRPosition::IRP_FUNCTION: |
| 372 | case IRPosition::IRP_RETURNED: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 373 | ScopeFn->setAttributes(Attrs); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 374 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 375 | case IRPosition::IRP_CALL_SITE: |
| 376 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 377 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 378 | CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); |
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 379 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 380 | case IRPosition::IRP_INVALID: |
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 381 | case IRPosition::IRP_FLOAT: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 382 | break; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return HasChanged; |
| 386 | } |
| 387 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 388 | const IRPosition IRPosition::EmptyKey(255); |
| 389 | const IRPosition IRPosition::TombstoneKey(256); |
| 390 | |
| 391 | SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { |
| 392 | IRPositions.emplace_back(IRP); |
| 393 | |
| 394 | ImmutableCallSite ICS(&IRP.getAnchorValue()); |
| 395 | switch (IRP.getPositionKind()) { |
| 396 | case IRPosition::IRP_INVALID: |
| 397 | case IRPosition::IRP_FLOAT: |
| 398 | case IRPosition::IRP_FUNCTION: |
| 399 | return; |
| 400 | case IRPosition::IRP_ARGUMENT: |
| 401 | case IRPosition::IRP_RETURNED: |
| 402 | IRPositions.emplace_back( |
| 403 | IRPosition::function(*IRP.getAssociatedFunction())); |
| 404 | return; |
| 405 | case IRPosition::IRP_CALL_SITE: |
| 406 | assert(ICS && "Expected call site!"); |
| 407 | // TODO: We need to look at the operand bundles similar to the redirection |
| 408 | // in CallBase. |
| 409 | if (!ICS.hasOperandBundles()) |
| 410 | if (const Function *Callee = ICS.getCalledFunction()) |
| 411 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 412 | return; |
| 413 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 414 | assert(ICS && "Expected call site!"); |
| 415 | // TODO: We need to look at the operand bundles similar to the redirection |
| 416 | // in CallBase. |
| 417 | if (!ICS.hasOperandBundles()) { |
| 418 | if (const Function *Callee = ICS.getCalledFunction()) { |
| 419 | IRPositions.emplace_back(IRPosition::returned(*Callee)); |
| 420 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 421 | } |
| 422 | } |
| 423 | IRPositions.emplace_back( |
| 424 | IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); |
| 425 | return; |
| 426 | case IRPosition::IRP_CALL_SITE_ARGUMENT: { |
| 427 | int ArgNo = IRP.getArgNo(); |
| 428 | assert(ICS && ArgNo >= 0 && "Expected call site!"); |
| 429 | // TODO: We need to look at the operand bundles similar to the redirection |
| 430 | // in CallBase. |
| 431 | if (!ICS.hasOperandBundles()) { |
| 432 | const Function *Callee = ICS.getCalledFunction(); |
| 433 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) |
| 434 | IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); |
| 435 | if (Callee) |
| 436 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 437 | } |
| 438 | IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); |
| 439 | return; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 444 | bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, |
| 445 | bool IgnoreSubsumingPositions) const { |
| 446 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 447 | for (Attribute::AttrKind AK : AKs) |
| 448 | if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) |
| 449 | return true; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 450 | // The first position returned by the SubsumingPositionIterator is |
| 451 | // always the position itself. If we ignore subsuming positions we |
| 452 | // are done after the first iteration. |
| 453 | if (IgnoreSubsumingPositions) |
| 454 | break; |
| 455 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
| 459 | void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, |
| 460 | SmallVectorImpl<Attribute> &Attrs) const { |
| 461 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) |
| 462 | for (Attribute::AttrKind AK : AKs) { |
| 463 | const Attribute &Attr = EquivIRP.getAttr(AK); |
| 464 | if (Attr.getKindAsEnum() == AK) |
| 465 | Attrs.push_back(Attr); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void IRPosition::verify() { |
| 470 | switch (KindOrArgNo) { |
| 471 | default: |
| 472 | assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); |
| 473 | assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && |
| 474 | "Expected call base or argument for positive attribute index!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 475 | if (isa<Argument>(AnchorVal)) { |
| 476 | assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 477 | "Argument number mismatch!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 478 | assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && |
| 479 | "Associated value mismatch!"); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 480 | } else { |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 481 | assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 482 | "Call site argument number mismatch!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 483 | assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == |
| 484 | &getAssociatedValue() && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 485 | "Associated value mismatch!"); |
| 486 | } |
| 487 | break; |
| 488 | case IRP_INVALID: |
| 489 | assert(!AnchorVal && "Expected no value for an invalid position!"); |
| 490 | break; |
| 491 | case IRP_FLOAT: |
| 492 | assert((!isa<CallBase>(&getAssociatedValue()) && |
| 493 | !isa<Argument>(&getAssociatedValue())) && |
| 494 | "Expected specialized kind for call base and argument values!"); |
| 495 | break; |
| 496 | case IRP_RETURNED: |
| 497 | assert(isa<Function>(AnchorVal) && |
| 498 | "Expected function for a 'returned' position!"); |
| 499 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 500 | break; |
| 501 | case IRP_CALL_SITE_RETURNED: |
| 502 | assert((isa<CallBase>(AnchorVal)) && |
| 503 | "Expected call base for 'call site returned' position!"); |
| 504 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 505 | break; |
| 506 | case IRP_CALL_SITE: |
| 507 | assert((isa<CallBase>(AnchorVal)) && |
| 508 | "Expected call base for 'call site function' position!"); |
| 509 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 510 | break; |
| 511 | case IRP_FUNCTION: |
| 512 | assert(isa<Function>(AnchorVal) && |
| 513 | "Expected function for a 'function' position!"); |
| 514 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | |
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 519 | namespace { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 520 | /// Helper functions to clamp a state \p S of type \p StateType with the |
| 521 | /// information in \p R and indicate/return if \p S did change (as-in update is |
| 522 | /// required to be run again). |
| 523 | /// |
| 524 | ///{ |
| 525 | template <typename StateType> |
| 526 | ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R); |
| 527 | |
| 528 | template <> |
| 529 | ChangeStatus clampStateAndIndicateChange<IntegerState>(IntegerState &S, |
| 530 | const IntegerState &R) { |
| 531 | auto Assumed = S.getAssumed(); |
| 532 | S ^= R; |
| 533 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED |
| 534 | : ChangeStatus::CHANGED; |
| 535 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 536 | |
| 537 | template <> |
| 538 | ChangeStatus clampStateAndIndicateChange<BooleanState>(BooleanState &S, |
| 539 | const BooleanState &R) { |
| 540 | return clampStateAndIndicateChange<IntegerState>(S, R); |
| 541 | } |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 542 | ///} |
| 543 | |
| 544 | /// Clamp the information known for all returned values of a function |
| 545 | /// (identified by \p QueryingAA) into \p S. |
| 546 | template <typename AAType, typename StateType = typename AAType::StateType> |
| 547 | static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, |
| 548 | StateType &S) { |
| 549 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " |
| 550 | << static_cast<const AbstractAttribute &>(QueryingAA) |
| 551 | << " into " << S << "\n"); |
| 552 | |
| 553 | assert((QueryingAA.getIRPosition().getPositionKind() == |
| 554 | IRPosition::IRP_RETURNED || |
| 555 | QueryingAA.getIRPosition().getPositionKind() == |
| 556 | IRPosition::IRP_CALL_SITE_RETURNED) && |
| 557 | "Can only clamp returned value states for a function returned or call " |
| 558 | "site returned position!"); |
| 559 | |
| 560 | // Use an optional state as there might not be any return values and we want |
| 561 | // to join (IntegerState::operator&) the state of all there are. |
| 562 | Optional<StateType> T; |
| 563 | |
| 564 | // Callback for each possibly returned value. |
| 565 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 566 | const IRPosition &RVPos = IRPosition::value(RV); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 567 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); |
| 568 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() |
| 569 | << " @ " << RVPos << "\n"); |
| 570 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 571 | if (T.hasValue()) |
| 572 | *T &= AAS; |
| 573 | else |
| 574 | T = AAS; |
| 575 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T |
| 576 | << "\n"); |
| 577 | return T->isValidState(); |
| 578 | }; |
| 579 | |
| 580 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) |
| 581 | S.indicatePessimisticFixpoint(); |
| 582 | else if (T.hasValue()) |
| 583 | S ^= *T; |
| 584 | } |
| 585 | |
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 586 | /// Helper class to compose two generic deduction |
| 587 | template <typename AAType, typename Base, typename StateType, |
| 588 | template <typename...> class F, template <typename...> class G> |
| 589 | struct AAComposeTwoGenericDeduction |
| 590 | : public F<AAType, G<AAType, Base, StateType>, StateType> { |
| 591 | AAComposeTwoGenericDeduction(const IRPosition &IRP) |
| 592 | : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} |
| 593 | |
| 594 | /// See AbstractAttribute::updateImpl(...). |
| 595 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 596 | ChangeStatus ChangedF = F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); |
| 597 | ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); |
| 598 | return ChangedF | ChangedG; |
| Hideto Ueno | 08daf8c | 2019-10-08 15:20:19 +0000 | [diff] [blame] | 599 | } |
| 600 | }; |
| 601 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 602 | /// Helper class for generic deduction: return value -> returned position. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 603 | template <typename AAType, typename Base, |
| 604 | typename StateType = typename AAType::StateType> |
| 605 | struct AAReturnedFromReturnedValues : public Base { |
| 606 | AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 607 | |
| 608 | /// See AbstractAttribute::updateImpl(...). |
| 609 | ChangeStatus updateImpl(Attributor &A) override { |
| 610 | StateType S; |
| 611 | clampReturnedValueStates<AAType, StateType>(A, *this, S); |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 612 | // TODO: If we know we visited all returned values, thus no are assumed |
| 613 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 614 | return clampStateAndIndicateChange<StateType>(this->getState(), S); |
| 615 | } |
| 616 | }; |
| 617 | |
| 618 | /// Clamp the information known at all call sites for a given argument |
| 619 | /// (identified by \p QueryingAA) into \p S. |
| 620 | template <typename AAType, typename StateType = typename AAType::StateType> |
| 621 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, |
| 622 | StateType &S) { |
| 623 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " |
| 624 | << static_cast<const AbstractAttribute &>(QueryingAA) |
| 625 | << " into " << S << "\n"); |
| 626 | |
| 627 | assert(QueryingAA.getIRPosition().getPositionKind() == |
| 628 | IRPosition::IRP_ARGUMENT && |
| 629 | "Can only clamp call site argument states for an argument position!"); |
| 630 | |
| 631 | // Use an optional state as there might not be any return values and we want |
| 632 | // to join (IntegerState::operator&) the state of all there are. |
| 633 | Optional<StateType> T; |
| 634 | |
| 635 | // The argument number which is also the call site argument number. |
| 636 | unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); |
| 637 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 638 | auto CallSiteCheck = [&](AbstractCallSite ACS) { |
| 639 | const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); |
| 640 | // Check if a coresponding argument was found or if it is on not associated |
| 641 | // (which can happen for callback calls). |
| 642 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) |
| 643 | return false; |
| 644 | |
| 645 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); |
| 646 | LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() |
| 647 | << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 648 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 649 | if (T.hasValue()) |
| 650 | *T &= AAS; |
| 651 | else |
| 652 | T = AAS; |
| 653 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T |
| 654 | << "\n"); |
| 655 | return T->isValidState(); |
| 656 | }; |
| 657 | |
| 658 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true)) |
| 659 | S.indicatePessimisticFixpoint(); |
| 660 | else if (T.hasValue()) |
| 661 | S ^= *T; |
| 662 | } |
| 663 | |
| 664 | /// Helper class for generic deduction: call site argument -> argument position. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 665 | template <typename AAType, typename Base, |
| 666 | typename StateType = typename AAType::StateType> |
| 667 | struct AAArgumentFromCallSiteArguments : public Base { |
| 668 | AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 669 | |
| 670 | /// See AbstractAttribute::updateImpl(...). |
| 671 | ChangeStatus updateImpl(Attributor &A) override { |
| 672 | StateType S; |
| 673 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 674 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 675 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 676 | return clampStateAndIndicateChange<StateType>(this->getState(), S); |
| 677 | } |
| 678 | }; |
| 679 | |
| 680 | /// Helper class for generic replication: function returned -> cs returned. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 681 | template <typename AAType, typename Base, |
| 682 | typename StateType = typename AAType::StateType> |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 683 | struct AACallSiteReturnedFromReturned : public Base { |
| 684 | AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 685 | |
| 686 | /// See AbstractAttribute::updateImpl(...). |
| 687 | ChangeStatus updateImpl(Attributor &A) override { |
| 688 | assert(this->getIRPosition().getPositionKind() == |
| 689 | IRPosition::IRP_CALL_SITE_RETURNED && |
| 690 | "Can only wrap function returned positions for call site returned " |
| 691 | "positions!"); |
| 692 | auto &S = this->getState(); |
| 693 | |
| 694 | const Function *AssociatedFunction = |
| 695 | this->getIRPosition().getAssociatedFunction(); |
| 696 | if (!AssociatedFunction) |
| 697 | return S.indicatePessimisticFixpoint(); |
| 698 | |
| 699 | IRPosition FnPos = IRPosition::returned(*AssociatedFunction); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 700 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 701 | return clampStateAndIndicateChange( |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 702 | S, static_cast<const typename AAType::StateType &>(AA.getState())); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 703 | } |
| 704 | }; |
| 705 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 706 | /// Helper class for generic deduction using must-be-executed-context |
| 707 | /// Base class is required to have `followUse` method. |
| 708 | |
| 709 | /// bool followUse(Attributor &A, const Use *U, const Instruction *I) |
| Simon Pilgrim | f7aee61 | 2019-10-10 15:25:16 +0000 | [diff] [blame] | 710 | /// U - Underlying use. |
| 711 | /// I - The user of the \p U. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 712 | /// `followUse` returns true if the value should be tracked transitively. |
| 713 | |
| 714 | template <typename AAType, typename Base, |
| 715 | typename StateType = typename AAType::StateType> |
| 716 | struct AAFromMustBeExecutedContext : public Base { |
| 717 | AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} |
| 718 | |
| 719 | void initialize(Attributor &A) override { |
| 720 | Base::initialize(A); |
| 721 | IRPosition &IRP = this->getIRPosition(); |
| 722 | Instruction *CtxI = IRP.getCtxI(); |
| 723 | |
| 724 | if (!CtxI) |
| 725 | return; |
| 726 | |
| 727 | for (const Use &U : IRP.getAssociatedValue().uses()) |
| 728 | Uses.insert(&U); |
| 729 | } |
| 730 | |
| 731 | /// See AbstractAttribute::updateImpl(...). |
| 732 | ChangeStatus updateImpl(Attributor &A) override { |
| 733 | auto BeforeState = this->getState(); |
| 734 | auto &S = this->getState(); |
| 735 | Instruction *CtxI = this->getIRPosition().getCtxI(); |
| 736 | if (!CtxI) |
| 737 | return ChangeStatus::UNCHANGED; |
| 738 | |
| 739 | MustBeExecutedContextExplorer &Explorer = |
| 740 | A.getInfoCache().getMustBeExecutedContextExplorer(); |
| 741 | |
| 742 | SetVector<const Use *> NextUses; |
| 743 | |
| 744 | for (const Use *U : Uses) { |
| 745 | if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { |
| 746 | auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); |
| 747 | bool Found = EIt.count(UserI); |
| 748 | while (!Found && ++EIt != EEnd) |
| 749 | Found = EIt.getCurrentInst() == UserI; |
| 750 | if (Found && Base::followUse(A, U, UserI)) |
| 751 | for (const Use &Us : UserI->uses()) |
| 752 | NextUses.insert(&Us); |
| 753 | } |
| 754 | } |
| 755 | for (const Use *U : NextUses) |
| 756 | Uses.insert(U); |
| 757 | |
| 758 | return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; |
| 759 | } |
| 760 | |
| 761 | private: |
| 762 | /// Container for (transitive) uses of the associated value. |
| 763 | SetVector<const Use *> Uses; |
| 764 | }; |
| 765 | |
| 766 | template <typename AAType, typename Base, |
| 767 | typename StateType = typename AAType::StateType> |
| 768 | using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = |
| 769 | AAComposeTwoGenericDeduction<AAType, Base, StateType, |
| 770 | AAFromMustBeExecutedContext, |
| 771 | AAArgumentFromCallSiteArguments>; |
| 772 | |
| 773 | template <typename AAType, typename Base, |
| 774 | typename StateType = typename AAType::StateType> |
| 775 | using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = |
| 776 | AAComposeTwoGenericDeduction<AAType, Base, StateType, |
| 777 | AAFromMustBeExecutedContext, |
| 778 | AACallSiteReturnedFromReturned>; |
| 779 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 780 | /// -----------------------NoUnwind Function Attribute-------------------------- |
| 781 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 782 | struct AANoUnwindImpl : AANoUnwind { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 783 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 784 | |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 785 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 786 | return getAssumed() ? "nounwind" : "may-unwind"; |
| 787 | } |
| 788 | |
| 789 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 790 | ChangeStatus updateImpl(Attributor &A) override { |
| 791 | auto Opcodes = { |
| 792 | (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 793 | (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, |
| 794 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; |
| 795 | |
| 796 | auto CheckForNoUnwind = [&](Instruction &I) { |
| 797 | if (!I.mayThrow()) |
| 798 | return true; |
| 799 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 800 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 801 | const auto &NoUnwindAA = |
| 802 | A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); |
| 803 | return NoUnwindAA.isAssumedNoUnwind(); |
| 804 | } |
| 805 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 806 | }; |
| 807 | |
| 808 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) |
| 809 | return indicatePessimisticFixpoint(); |
| 810 | |
| 811 | return ChangeStatus::UNCHANGED; |
| 812 | } |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 813 | }; |
| 814 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 815 | struct AANoUnwindFunction final : public AANoUnwindImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 816 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 817 | |
| 818 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 819 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 820 | }; |
| 821 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 822 | /// NoUnwind attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 823 | struct AANoUnwindCallSite final : AANoUnwindImpl { |
| 824 | AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} |
| 825 | |
| 826 | /// See AbstractAttribute::initialize(...). |
| 827 | void initialize(Attributor &A) override { |
| 828 | AANoUnwindImpl::initialize(A); |
| 829 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 830 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 831 | indicatePessimisticFixpoint(); |
| 832 | } |
| 833 | |
| 834 | /// See AbstractAttribute::updateImpl(...). |
| 835 | ChangeStatus updateImpl(Attributor &A) override { |
| 836 | // TODO: Once we have call site specific value information we can provide |
| 837 | // call site specific liveness information and then it makes |
| 838 | // sense to specialize attributes for call sites arguments instead of |
| 839 | // redirecting requests to the callee argument. |
| 840 | Function *F = getAssociatedFunction(); |
| 841 | const IRPosition &FnPos = IRPosition::function(*F); |
| 842 | auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); |
| 843 | return clampStateAndIndicateChange( |
| 844 | getState(), |
| 845 | static_cast<const AANoUnwind::StateType &>(FnAA.getState())); |
| 846 | } |
| 847 | |
| 848 | /// See AbstractAttribute::trackStatistics() |
| 849 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } |
| 850 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 851 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 852 | /// --------------------- Function Return Values ------------------------------- |
| 853 | |
| 854 | /// "Attribute" that collects all potential returned values and the return |
| 855 | /// instructions that they arise from. |
| 856 | /// |
| 857 | /// If there is a unique returned value R, the manifest method will: |
| 858 | /// - mark R with the "returned" attribute, if R is an argument. |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 859 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 860 | |
| 861 | /// Mapping of values potentially returned by the associated function to the |
| 862 | /// return instructions that might return them. |
| Johannes Doerfert | a4a308c | 2019-08-26 17:51:23 +0000 | [diff] [blame] | 863 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 864 | |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 865 | /// Mapping to remember the number of returned values for a call site such |
| 866 | /// that we can avoid updates if nothing changed. |
| 867 | DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; |
| 868 | |
| 869 | /// Set of unresolved calls returned by the associated function. |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 870 | SmallSetVector<CallBase *, 4> UnresolvedCalls; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 871 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 872 | /// State flags |
| 873 | /// |
| 874 | ///{ |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 875 | bool IsFixed = false; |
| 876 | bool IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 877 | ///} |
| 878 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 879 | public: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 880 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 881 | |
| 882 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 883 | void initialize(Attributor &A) override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 884 | // Reset the state. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 885 | IsFixed = false; |
| 886 | IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 887 | ReturnedValues.clear(); |
| 888 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 889 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 890 | if (!F) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 891 | indicatePessimisticFixpoint(); |
| 892 | return; |
| 893 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 894 | |
| 895 | // The map from instruction opcodes to those instructions in the function. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 896 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 897 | |
| 898 | // 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] | 899 | for (Argument &Arg : F->args()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 900 | if (Arg.hasReturnedAttr()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 901 | auto &ReturnInstSet = ReturnedValues[&Arg]; |
| 902 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) |
| 903 | ReturnInstSet.insert(cast<ReturnInst>(RI)); |
| 904 | |
| 905 | indicateOptimisticFixpoint(); |
| 906 | return; |
| 907 | } |
| 908 | } |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 909 | |
| 910 | if (!F->hasExactDefinition()) |
| 911 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /// See AbstractAttribute::manifest(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 915 | ChangeStatus manifest(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 916 | |
| 917 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 918 | AbstractState &getState() override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 919 | |
| 920 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 921 | const AbstractState &getState() const override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 922 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 923 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 924 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 925 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 926 | llvm::iterator_range<iterator> returned_values() override { |
| 927 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 928 | } |
| 929 | |
| 930 | llvm::iterator_range<const_iterator> returned_values() const override { |
| 931 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 932 | } |
| 933 | |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 934 | const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 935 | return UnresolvedCalls; |
| 936 | } |
| 937 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 938 | /// Return the number of potential return values, -1 if unknown. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 939 | size_t getNumReturnValues() const override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 940 | return isValidState() ? ReturnedValues.size() : -1; |
| 941 | } |
| 942 | |
| 943 | /// Return an assumed unique return value if a single candidate is found. If |
| 944 | /// there cannot be one, return a nullptr. If it is not clear yet, return the |
| 945 | /// Optional::NoneType. |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 946 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 947 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 948 | /// See AbstractState::checkForAllReturnedValues(...). |
| 949 | bool checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 950 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 951 | &Pred) const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 952 | |
| 953 | /// Pretty print the attribute similar to the IR representation. |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 954 | const std::string getAsStr() const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 955 | |
| 956 | /// See AbstractState::isAtFixpoint(). |
| 957 | bool isAtFixpoint() const override { return IsFixed; } |
| 958 | |
| 959 | /// See AbstractState::isValidState(). |
| 960 | bool isValidState() const override { return IsValidState; } |
| 961 | |
| 962 | /// See AbstractState::indicateOptimisticFixpoint(...). |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 963 | ChangeStatus indicateOptimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 964 | IsFixed = true; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 965 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 966 | } |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 967 | |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 968 | ChangeStatus indicatePessimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 969 | IsFixed = true; |
| 970 | IsValidState = false; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 971 | return ChangeStatus::CHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 972 | } |
| 973 | }; |
| 974 | |
| 975 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { |
| 976 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 977 | |
| 978 | // Bookkeeping. |
| 979 | assert(isValidState()); |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 980 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, |
| 981 | "Number of function with known return values"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 982 | |
| 983 | // 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] | 984 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 985 | |
| 986 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) |
| 987 | return Changed; |
| 988 | |
| 989 | // Bookkeeping. |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 990 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, |
| 991 | "Number of function with unique return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 992 | |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 993 | // Callback to replace the uses of CB with the constant C. |
| 994 | auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) { |
| Johannes Doerfert | 8fa56c4 | 2019-10-11 01:45:32 +0000 | [diff] [blame] | 995 | if (CB.getNumUses() == 0 || CB.isMustTailCall()) |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 996 | return ChangeStatus::UNCHANGED; |
| 997 | CB.replaceAllUsesWith(&C); |
| 998 | return ChangeStatus::CHANGED; |
| 999 | }; |
| 1000 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1001 | // If the assumed unique return value is an argument, annotate it. |
| 1002 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1003 | getIRPosition() = IRPosition::argument(*UniqueRVArg); |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1004 | Changed = IRAttribute::manifest(A); |
| 1005 | } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { |
| 1006 | // We can replace the returned value with the unique returned constant. |
| 1007 | Value &AnchorValue = getAnchorValue(); |
| 1008 | if (Function *F = dyn_cast<Function>(&AnchorValue)) { |
| 1009 | for (const Use &U : F->uses()) |
| 1010 | if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) |
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1011 | if (CB->isCallee(&U)) { |
| 1012 | Constant *RVCCast = |
| 1013 | ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); |
| 1014 | Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; |
| 1015 | } |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1016 | } else { |
| 1017 | assert(isa<CallBase>(AnchorValue) && |
| 1018 | "Expcected a function or call base anchor!"); |
| Johannes Doerfert | e7c6f97 | 2019-09-14 02:57:50 +0000 | [diff] [blame] | 1019 | Constant *RVCCast = |
| 1020 | ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); |
| 1021 | Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 1022 | } |
| 1023 | if (Changed == ChangeStatus::CHANGED) |
| 1024 | STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, |
| 1025 | "Number of function returns replaced by constant return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | return Changed; |
| 1029 | } |
| 1030 | |
| 1031 | const std::string AAReturnedValuesImpl::getAsStr() const { |
| 1032 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + |
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 1033 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1034 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1037 | Optional<Value *> |
| 1038 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { |
| 1039 | // If checkForAllReturnedValues provides a unique value, ignoring potential |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1040 | // undef values that can also be present, it is assumed to be the actual |
| 1041 | // return value and forwarded to the caller of this method. If there are |
| 1042 | // multiple, a nullptr is returned indicating there cannot be a unique |
| 1043 | // returned value. |
| 1044 | Optional<Value *> UniqueRV; |
| 1045 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1046 | auto Pred = [&](Value &RV) -> bool { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1047 | // If we found a second returned value and neither the current nor the saved |
| 1048 | // one is an undef, there is no unique returned value. Undefs are special |
| 1049 | // since we can pretend they have any value. |
| 1050 | if (UniqueRV.hasValue() && UniqueRV != &RV && |
| 1051 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { |
| 1052 | UniqueRV = nullptr; |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | // Do not overwrite a value with an undef. |
| 1057 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) |
| 1058 | UniqueRV = &RV; |
| 1059 | |
| 1060 | return true; |
| 1061 | }; |
| 1062 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1063 | if (!A.checkForAllReturnedValues(Pred, *this)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1064 | UniqueRV = nullptr; |
| 1065 | |
| 1066 | return UniqueRV; |
| 1067 | } |
| 1068 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1069 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1070 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1071 | &Pred) const { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1072 | if (!isValidState()) |
| 1073 | return false; |
| 1074 | |
| 1075 | // Check all returned values but ignore call sites as long as we have not |
| 1076 | // encountered an overdefined one during an update. |
| 1077 | for (auto &It : ReturnedValues) { |
| 1078 | Value *RV = It.first; |
| 1079 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1080 | CallBase *CB = dyn_cast<CallBase>(RV); |
| 1081 | if (CB && !UnresolvedCalls.count(CB)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1082 | continue; |
| 1083 | |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1084 | if (!Pred(*RV, It.second)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | return true; |
| 1089 | } |
| 1090 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1091 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1092 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); |
| 1093 | bool Changed = false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1094 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1095 | // State used in the value traversals starting in returned values. |
| 1096 | struct RVState { |
| 1097 | // The map in which we collect return values -> return instrs. |
| 1098 | decltype(ReturnedValues) &RetValsMap; |
| 1099 | // The flag to indicate a change. |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1100 | bool &Changed; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1101 | // The return instrs we come from. |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1102 | SmallSetVector<ReturnInst *, 4> RetInsts; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1103 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1104 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1105 | // Callback for a leaf value returned by the associated function. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1106 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1107 | auto Size = RVS.RetValsMap[&Val].size(); |
| 1108 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); |
| 1109 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; |
| 1110 | RVS.Changed |= Inserted; |
| 1111 | LLVM_DEBUG({ |
| 1112 | if (Inserted) |
| 1113 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val |
| 1114 | << " => " << RVS.RetInsts.size() << "\n"; |
| 1115 | }); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1116 | return true; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1117 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1118 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1119 | // Helper method to invoke the generic value traversal. |
| 1120 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { |
| 1121 | IRPosition RetValPos = IRPosition::value(RV); |
| 1122 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, |
| 1123 | RVS, VisitValueCB); |
| 1124 | }; |
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 1125 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1126 | // Callback for all "return intructions" live in the associated function. |
| 1127 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { |
| 1128 | ReturnInst &Ret = cast<ReturnInst>(I); |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1129 | RVState RVS({ReturnedValues, Changed, {}}); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1130 | RVS.RetInsts.insert(&Ret); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1131 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); |
| 1132 | }; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1133 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1134 | // Start by discovering returned values from all live returned instructions in |
| 1135 | // the associated function. |
| 1136 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) |
| 1137 | return indicatePessimisticFixpoint(); |
| 1138 | |
| 1139 | // Once returned values "directly" present in the code are handled we try to |
| 1140 | // resolve returned calls. |
| 1141 | decltype(ReturnedValues) NewRVsMap; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1142 | for (auto &It : ReturnedValues) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1143 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first |
| 1144 | << " by #" << It.second.size() << " RIs\n"); |
| 1145 | CallBase *CB = dyn_cast<CallBase>(It.first); |
| 1146 | if (!CB || UnresolvedCalls.count(CB)) |
| 1147 | continue; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1148 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1149 | if (!CB->getCalledFunction()) { |
| 1150 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 1151 | << "\n"); |
| 1152 | UnresolvedCalls.insert(CB); |
| 1153 | continue; |
| 1154 | } |
| 1155 | |
| 1156 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 1157 | const auto &RetValAA = A.getAAFor<AAReturnedValues>( |
| 1158 | *this, IRPosition::function(*CB->getCalledFunction())); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1159 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " |
| 1160 | << static_cast<const AbstractAttribute &>(RetValAA) |
| 1161 | << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Skip dead ends, thus if we do not know anything about the returned |
| 1164 | // call we mark it as unresolved and it will stay that way. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1165 | if (!RetValAA.getState().isValidState()) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1166 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 1167 | << "\n"); |
| 1168 | UnresolvedCalls.insert(CB); |
| 1169 | continue; |
| 1170 | } |
| 1171 | |
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1172 | // Do not try to learn partial information. If the callee has unresolved |
| 1173 | // return values we will treat the call as unresolved/opaque. |
| 1174 | auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); |
| 1175 | if (!RetValAAUnresolvedCalls.empty()) { |
| 1176 | UnresolvedCalls.insert(CB); |
| 1177 | continue; |
| 1178 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1179 | |
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1180 | // Now check if we can track transitively returned values. If possible, thus |
| 1181 | // if all return value can be represented in the current scope, do so. |
| 1182 | bool Unresolved = false; |
| 1183 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 1184 | Value *RetVal = RetValAAIt.first; |
| 1185 | if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || |
| 1186 | isa<Constant>(RetVal)) |
| 1187 | continue; |
| 1188 | // Anything that did not fit in the above categories cannot be resolved, |
| 1189 | // mark the call as unresolved. |
| 1190 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " |
| 1191 | "cannot be translated: " |
| 1192 | << *RetVal << "\n"); |
| 1193 | UnresolvedCalls.insert(CB); |
| 1194 | Unresolved = true; |
| 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | if (Unresolved) |
| 1199 | continue; |
| 1200 | |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1201 | // Now track transitively returned values. |
| 1202 | unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; |
| 1203 | if (NumRetAA == RetValAA.getNumReturnValues()) { |
| 1204 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " |
| 1205 | "changed since it was seen last\n"); |
| 1206 | continue; |
| 1207 | } |
| 1208 | NumRetAA = RetValAA.getNumReturnValues(); |
| 1209 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1210 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 1211 | Value *RetVal = RetValAAIt.first; |
| 1212 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { |
| 1213 | // Arguments are mapped to call site operands and we begin the traversal |
| 1214 | // again. |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1215 | bool Unused = false; |
| 1216 | RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1217 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); |
| 1218 | continue; |
| 1219 | } else if (isa<CallBase>(RetVal)) { |
| 1220 | // Call sites are resolved by the callee attribute over time, no need to |
| 1221 | // do anything for us. |
| 1222 | continue; |
| 1223 | } else if (isa<Constant>(RetVal)) { |
| 1224 | // Constants are valid everywhere, we can simply take them. |
| 1225 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); |
| 1226 | continue; |
| 1227 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1228 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1231 | // To avoid modifications to the ReturnedValues map while we iterate over it |
| 1232 | // we kept record of potential new entries in a copy map, NewRVsMap. |
| 1233 | for (auto &It : NewRVsMap) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1234 | assert(!It.second.empty() && "Entry does not add anything."); |
| 1235 | auto &ReturnInsts = ReturnedValues[It.first]; |
| 1236 | for (ReturnInst *RI : It.second) |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1237 | if (ReturnInsts.insert(RI)) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1238 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " |
| 1239 | << *It.first << " => " << *RI << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1240 | Changed = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1244 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); |
| 1245 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1248 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { |
| 1249 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 1250 | |
| 1251 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1252 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1253 | }; |
| 1254 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1255 | /// Returned values information for a call sites. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1256 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { |
| 1257 | AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 1258 | |
| 1259 | /// See AbstractAttribute::initialize(...). |
| 1260 | void initialize(Attributor &A) override { |
| 1261 | // TODO: Once we have call site specific value information we can provide |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1262 | // call site specific liveness information and then it makes |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1263 | // sense to specialize attributes for call sites instead of |
| 1264 | // redirecting requests to the callee. |
| 1265 | llvm_unreachable("Abstract attributes for returned values are not " |
| 1266 | "supported for call sites yet!"); |
| 1267 | } |
| 1268 | |
| 1269 | /// See AbstractAttribute::updateImpl(...). |
| 1270 | ChangeStatus updateImpl(Attributor &A) override { |
| 1271 | return indicatePessimisticFixpoint(); |
| 1272 | } |
| 1273 | |
| 1274 | /// See AbstractAttribute::trackStatistics() |
| 1275 | void trackStatistics() const override {} |
| 1276 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1277 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1278 | /// ------------------------ NoSync Function Attribute ------------------------- |
| 1279 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1280 | struct AANoSyncImpl : AANoSync { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1281 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1282 | |
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 1283 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1284 | return getAssumed() ? "nosync" : "may-sync"; |
| 1285 | } |
| 1286 | |
| 1287 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1288 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1289 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1290 | /// Helper function used to determine whether an instruction is non-relaxed |
| 1291 | /// atomic. In other words, if an atomic instruction does not have unordered |
| 1292 | /// or monotonic ordering |
| 1293 | static bool isNonRelaxedAtomic(Instruction *I); |
| 1294 | |
| 1295 | /// Helper function used to determine whether an instruction is volatile. |
| 1296 | static bool isVolatile(Instruction *I); |
| 1297 | |
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 1298 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, |
| 1299 | /// memset). |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1300 | static bool isNoSyncIntrinsic(Instruction *I); |
| 1301 | }; |
| 1302 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1303 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1304 | if (!I->isAtomic()) |
| 1305 | return false; |
| 1306 | |
| 1307 | AtomicOrdering Ordering; |
| 1308 | switch (I->getOpcode()) { |
| 1309 | case Instruction::AtomicRMW: |
| 1310 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); |
| 1311 | break; |
| 1312 | case Instruction::Store: |
| 1313 | Ordering = cast<StoreInst>(I)->getOrdering(); |
| 1314 | break; |
| 1315 | case Instruction::Load: |
| 1316 | Ordering = cast<LoadInst>(I)->getOrdering(); |
| 1317 | break; |
| 1318 | case Instruction::Fence: { |
| 1319 | auto *FI = cast<FenceInst>(I); |
| 1320 | if (FI->getSyncScopeID() == SyncScope::SingleThread) |
| 1321 | return false; |
| 1322 | Ordering = FI->getOrdering(); |
| 1323 | break; |
| 1324 | } |
| 1325 | case Instruction::AtomicCmpXchg: { |
| 1326 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); |
| 1327 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); |
| 1328 | // Only if both are relaxed, than it can be treated as relaxed. |
| 1329 | // Otherwise it is non-relaxed. |
| 1330 | if (Success != AtomicOrdering::Unordered && |
| 1331 | Success != AtomicOrdering::Monotonic) |
| 1332 | return true; |
| 1333 | if (Failure != AtomicOrdering::Unordered && |
| 1334 | Failure != AtomicOrdering::Monotonic) |
| 1335 | return true; |
| 1336 | return false; |
| 1337 | } |
| 1338 | default: |
| 1339 | llvm_unreachable( |
| 1340 | "New atomic operations need to be known in the attributor."); |
| 1341 | } |
| 1342 | |
| 1343 | // Relaxed. |
| 1344 | if (Ordering == AtomicOrdering::Unordered || |
| 1345 | Ordering == AtomicOrdering::Monotonic) |
| 1346 | return false; |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
| 1350 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. |
| 1351 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1352 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1353 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 1354 | switch (II->getIntrinsicID()) { |
| 1355 | /// Element wise atomic memory intrinsics are can only be unordered, |
| 1356 | /// therefore nosync. |
| 1357 | case Intrinsic::memset_element_unordered_atomic: |
| 1358 | case Intrinsic::memmove_element_unordered_atomic: |
| 1359 | case Intrinsic::memcpy_element_unordered_atomic: |
| 1360 | return true; |
| 1361 | case Intrinsic::memset: |
| 1362 | case Intrinsic::memmove: |
| 1363 | case Intrinsic::memcpy: |
| 1364 | if (!cast<MemIntrinsic>(II)->isVolatile()) |
| 1365 | return true; |
| 1366 | return false; |
| 1367 | default: |
| 1368 | return false; |
| 1369 | } |
| 1370 | } |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1374 | bool AANoSyncImpl::isVolatile(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1375 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && |
| 1376 | "Calls should not be checked here"); |
| 1377 | |
| 1378 | switch (I->getOpcode()) { |
| 1379 | case Instruction::AtomicRMW: |
| 1380 | return cast<AtomicRMWInst>(I)->isVolatile(); |
| 1381 | case Instruction::Store: |
| 1382 | return cast<StoreInst>(I)->isVolatile(); |
| 1383 | case Instruction::Load: |
| 1384 | return cast<LoadInst>(I)->isVolatile(); |
| 1385 | case Instruction::AtomicCmpXchg: |
| 1386 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); |
| 1387 | default: |
| 1388 | return false; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1392 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1393 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1394 | auto CheckRWInstForNoSync = [&](Instruction &I) { |
| 1395 | /// We are looking for volatile instructions or Non-Relaxed atomics. |
| 1396 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1397 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1398 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) |
| 1399 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1400 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1401 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 1402 | if (ICS.hasFnAttr(Attribute::NoSync)) |
| 1403 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1404 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1405 | const auto &NoSyncAA = |
| 1406 | A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); |
| 1407 | if (NoSyncAA.isAssumedNoSync()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1408 | return true; |
| 1409 | return false; |
| 1410 | } |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1411 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1412 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) |
| 1413 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1414 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1415 | return false; |
| 1416 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1417 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1418 | auto CheckForNoSync = [&](Instruction &I) { |
| 1419 | // At this point we handled all read/write effects and they are all |
| 1420 | // nosync, so they can be skipped. |
| 1421 | if (I.mayReadOrWriteMemory()) |
| 1422 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1423 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1424 | // non-convergent and readnone imply nosync. |
| 1425 | return !ImmutableCallSite(&I).isConvergent(); |
| 1426 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1427 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1428 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || |
| 1429 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1430 | return indicatePessimisticFixpoint(); |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1431 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1432 | return ChangeStatus::UNCHANGED; |
| 1433 | } |
| 1434 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1435 | struct AANoSyncFunction final : public AANoSyncImpl { |
| 1436 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} |
| 1437 | |
| 1438 | /// See AbstractAttribute::trackStatistics() |
| 1439 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } |
| 1440 | }; |
| 1441 | |
| 1442 | /// NoSync attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1443 | struct AANoSyncCallSite final : AANoSyncImpl { |
| 1444 | AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} |
| 1445 | |
| 1446 | /// See AbstractAttribute::initialize(...). |
| 1447 | void initialize(Attributor &A) override { |
| 1448 | AANoSyncImpl::initialize(A); |
| 1449 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1450 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1451 | indicatePessimisticFixpoint(); |
| 1452 | } |
| 1453 | |
| 1454 | /// See AbstractAttribute::updateImpl(...). |
| 1455 | ChangeStatus updateImpl(Attributor &A) override { |
| 1456 | // TODO: Once we have call site specific value information we can provide |
| 1457 | // call site specific liveness information and then it makes |
| 1458 | // sense to specialize attributes for call sites arguments instead of |
| 1459 | // redirecting requests to the callee argument. |
| 1460 | Function *F = getAssociatedFunction(); |
| 1461 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1462 | auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); |
| 1463 | return clampStateAndIndicateChange( |
| 1464 | getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); |
| 1465 | } |
| 1466 | |
| 1467 | /// See AbstractAttribute::trackStatistics() |
| 1468 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } |
| 1469 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1470 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1471 | /// ------------------------ No-Free Attributes ---------------------------- |
| 1472 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1473 | struct AANoFreeImpl : public AANoFree { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1474 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1475 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1476 | /// See AbstractAttribute::updateImpl(...). |
| 1477 | ChangeStatus updateImpl(Attributor &A) override { |
| 1478 | auto CheckForNoFree = [&](Instruction &I) { |
| 1479 | ImmutableCallSite ICS(&I); |
| 1480 | if (ICS.hasFnAttr(Attribute::NoFree)) |
| 1481 | return true; |
| 1482 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1483 | const auto &NoFreeAA = |
| 1484 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); |
| 1485 | return NoFreeAA.isAssumedNoFree(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1486 | }; |
| 1487 | |
| 1488 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) |
| 1489 | return indicatePessimisticFixpoint(); |
| 1490 | return ChangeStatus::UNCHANGED; |
| 1491 | } |
| 1492 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1493 | /// See AbstractAttribute::getAsStr(). |
| 1494 | const std::string getAsStr() const override { |
| 1495 | return getAssumed() ? "nofree" : "may-free"; |
| 1496 | } |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1497 | }; |
| 1498 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1499 | struct AANoFreeFunction final : public AANoFreeImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1500 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1501 | |
| 1502 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1503 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1504 | }; |
| 1505 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1506 | /// NoFree attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1507 | struct AANoFreeCallSite final : AANoFreeImpl { |
| 1508 | AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| 1509 | |
| 1510 | /// See AbstractAttribute::initialize(...). |
| 1511 | void initialize(Attributor &A) override { |
| 1512 | AANoFreeImpl::initialize(A); |
| 1513 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1514 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1515 | indicatePessimisticFixpoint(); |
| 1516 | } |
| 1517 | |
| 1518 | /// See AbstractAttribute::updateImpl(...). |
| 1519 | ChangeStatus updateImpl(Attributor &A) override { |
| 1520 | // TODO: Once we have call site specific value information we can provide |
| 1521 | // call site specific liveness information and then it makes |
| 1522 | // sense to specialize attributes for call sites arguments instead of |
| 1523 | // redirecting requests to the callee argument. |
| 1524 | Function *F = getAssociatedFunction(); |
| 1525 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1526 | auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); |
| 1527 | return clampStateAndIndicateChange( |
| 1528 | getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); |
| 1529 | } |
| 1530 | |
| 1531 | /// See AbstractAttribute::trackStatistics() |
| 1532 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } |
| 1533 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1534 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1535 | /// ------------------------ NonNull Argument Attribute ------------------------ |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1536 | static int64_t getKnownNonNullAndDerefBytesForUse( |
| 1537 | Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue, |
| 1538 | const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1539 | TrackUse = false; |
| 1540 | |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1541 | const Value *UseV = U->get(); |
| 1542 | if (!UseV->getType()->isPointerTy()) |
| 1543 | return 0; |
| 1544 | |
| 1545 | Type *PtrTy = UseV->getType(); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1546 | const Function *F = I->getFunction(); |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1547 | bool NullPointerIsDefined = |
| 1548 | F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1549 | const DataLayout &DL = A.getInfoCache().getDL(); |
| 1550 | if (ImmutableCallSite ICS = ImmutableCallSite(I)) { |
| 1551 | if (ICS.isBundleOperand(U)) |
| 1552 | return 0; |
| 1553 | |
| 1554 | if (ICS.isCallee(U)) { |
| 1555 | IsNonNull |= !NullPointerIsDefined; |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 1560 | IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); |
| 1561 | auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP); |
| 1562 | IsNonNull |= DerefAA.isKnownNonNull(); |
| 1563 | return DerefAA.getKnownDereferenceableBytes(); |
| 1564 | } |
| 1565 | |
| 1566 | int64_t Offset; |
| 1567 | if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1568 | if (Base == &AssociatedValue && getPointerOperand(I) == UseV) { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1569 | int64_t DerefBytes = |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1570 | Offset + (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1571 | |
| 1572 | IsNonNull |= !NullPointerIsDefined; |
| 1573 | return DerefBytes; |
| 1574 | } |
| 1575 | } |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1576 | if (const Value *Base = |
| 1577 | GetPointerBaseWithConstantOffset(UseV, Offset, DL, |
| 1578 | /*AllowNonInbounds*/ false)) { |
| 1579 | auto &DerefAA = |
| 1580 | A.getAAFor<AADereferenceable>(QueryingAA, IRPosition::value(*Base)); |
| 1581 | IsNonNull |= (!NullPointerIsDefined && DerefAA.isKnownNonNull()); |
| 1582 | IsNonNull |= (!NullPointerIsDefined && (Offset != 0)); |
| 1583 | int64_t DerefBytes = DerefAA.getKnownDereferenceableBytes(); |
| 1584 | return std::max(int64_t(0), DerefBytes - Offset); |
| 1585 | } |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1586 | |
| 1587 | return 0; |
| 1588 | } |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 1589 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1590 | struct AANonNullImpl : AANonNull { |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame^] | 1591 | AANonNullImpl(const IRPosition &IRP) |
| 1592 | : AANonNull(IRP), |
| 1593 | NullIsDefined(NullPointerIsDefined( |
| 1594 | getAnchorScope(), |
| 1595 | getAssociatedValue().getType()->getPointerAddressSpace())) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1596 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1597 | /// See AbstractAttribute::initialize(...). |
| 1598 | void initialize(Attributor &A) override { |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame^] | 1599 | if (!NullIsDefined && |
| 1600 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1601 | indicateOptimisticFixpoint(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1602 | else |
| 1603 | AANonNull::initialize(A); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1606 | /// See AAFromMustBeExecutedContext |
| 1607 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { |
| 1608 | bool IsNonNull = false; |
| 1609 | bool TrackUse = false; |
| 1610 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, |
| 1611 | IsNonNull, TrackUse); |
| 1612 | takeKnownMaximum(IsNonNull); |
| 1613 | return TrackUse; |
| 1614 | } |
| 1615 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1616 | /// See AbstractAttribute::getAsStr(). |
| 1617 | const std::string getAsStr() const override { |
| 1618 | return getAssumed() ? "nonnull" : "may-null"; |
| 1619 | } |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame^] | 1620 | |
| 1621 | /// Flag to determine if the underlying value can be null and still allow |
| 1622 | /// valid accesses. |
| 1623 | const bool NullIsDefined; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1624 | }; |
| 1625 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1626 | /// NonNull attribute for a floating value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1627 | struct AANonNullFloating |
| 1628 | : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { |
| 1629 | using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; |
| 1630 | AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1631 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1632 | /// See AbstractAttribute::initialize(...). |
| 1633 | void initialize(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1634 | Base::initialize(A); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1635 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1636 | if (isAtFixpoint()) |
| 1637 | return; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1638 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1639 | const IRPosition &IRP = getIRPosition(); |
| 1640 | const Value &V = IRP.getAssociatedValue(); |
| 1641 | const DataLayout &DL = A.getDataLayout(); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1642 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1643 | // TODO: This context sensitive query should be removed once we can do |
| 1644 | // context sensitive queries in the genericValueTraversal below. |
| 1645 | if (isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, IRP.getCtxI(), |
| 1646 | /* TODO: DT */ nullptr)) |
| 1647 | indicateOptimisticFixpoint(); |
| 1648 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1649 | |
| 1650 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1651 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1652 | ChangeStatus Change = Base::updateImpl(A); |
| 1653 | if (isKnownNonNull()) |
| 1654 | return Change; |
| 1655 | |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame^] | 1656 | if (!NullIsDefined) { |
| 1657 | const auto &DerefAA = A.getAAFor<AADereferenceable>(*this, getIRPosition()); |
| 1658 | if (DerefAA.getAssumedDereferenceableBytes()) |
| 1659 | return Change; |
| 1660 | } |
| 1661 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1662 | const DataLayout &DL = A.getDataLayout(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1663 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1664 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, |
| 1665 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1666 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); |
| 1667 | if (!Stripped && this == &AA) { |
| 1668 | if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, |
| Johannes Doerfert | d82385b | 2019-10-13 20:48:26 +0000 | [diff] [blame^] | 1669 | /* CtxI */ getCtxI(), |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1670 | /* TODO: DT */ nullptr)) |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1671 | T.indicatePessimisticFixpoint(); |
| 1672 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1673 | // Use abstract attribute information. |
| 1674 | const AANonNull::StateType &NS = |
| 1675 | static_cast<const AANonNull::StateType &>(AA.getState()); |
| 1676 | T ^= NS; |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1677 | } |
| 1678 | return T.isValidState(); |
| 1679 | }; |
| 1680 | |
| 1681 | StateType T; |
| 1682 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, |
| 1683 | T, VisitValueCB)) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1684 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1685 | |
| 1686 | return clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1687 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1688 | |
| 1689 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1690 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1691 | }; |
| 1692 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1693 | /// NonNull attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1694 | struct AANonNullReturned final |
| 1695 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1696 | AANonNullReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1697 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1698 | |
| 1699 | /// See AbstractAttribute::trackStatistics() |
| 1700 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| 1701 | }; |
| 1702 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1703 | /// NonNull attribute for function argument. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1704 | struct AANonNullArgument final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1705 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, |
| 1706 | AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1707 | AANonNullArgument(const IRPosition &IRP) |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1708 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, |
| 1709 | AANonNullImpl>( |
| 1710 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1711 | |
| 1712 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1713 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1714 | }; |
| 1715 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1716 | struct AANonNullCallSiteArgument final : AANonNullFloating { |
| 1717 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1718 | |
| 1719 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 1720 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1721 | }; |
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1722 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1723 | /// NonNull attribute for a call site return position. |
| 1724 | struct AANonNullCallSiteReturned final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1725 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, |
| 1726 | AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1727 | AANonNullCallSiteReturned(const IRPosition &IRP) |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 1728 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, |
| 1729 | AANonNullImpl>( |
| 1730 | IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1731 | |
| 1732 | /// See AbstractAttribute::trackStatistics() |
| 1733 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } |
| 1734 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1735 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1736 | /// ------------------------ No-Recurse Attributes ---------------------------- |
| 1737 | |
| 1738 | struct AANoRecurseImpl : public AANoRecurse { |
| 1739 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} |
| 1740 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1741 | /// See AbstractAttribute::getAsStr() |
| 1742 | const std::string getAsStr() const override { |
| 1743 | return getAssumed() ? "norecurse" : "may-recurse"; |
| 1744 | } |
| 1745 | }; |
| 1746 | |
| 1747 | struct AANoRecurseFunction final : AANoRecurseImpl { |
| 1748 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 1749 | |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1750 | /// See AbstractAttribute::initialize(...). |
| 1751 | void initialize(Attributor &A) override { |
| 1752 | AANoRecurseImpl::initialize(A); |
| 1753 | if (const Function *F = getAnchorScope()) |
| 1754 | if (A.getInfoCache().getSccSize(*F) == 1) |
| 1755 | return; |
| 1756 | indicatePessimisticFixpoint(); |
| 1757 | } |
| 1758 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1759 | /// See AbstractAttribute::updateImpl(...). |
| 1760 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 1761 | |
| 1762 | auto CheckForNoRecurse = [&](Instruction &I) { |
| 1763 | ImmutableCallSite ICS(&I); |
| 1764 | if (ICS.hasFnAttr(Attribute::NoRecurse)) |
| 1765 | return true; |
| 1766 | |
| 1767 | const auto &NoRecurseAA = |
| 1768 | A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); |
| 1769 | if (!NoRecurseAA.isAssumedNoRecurse()) |
| 1770 | return false; |
| 1771 | |
| 1772 | // Recursion to the same function |
| 1773 | if (ICS.getCalledFunction() == getAnchorScope()) |
| 1774 | return false; |
| 1775 | |
| 1776 | return true; |
| 1777 | }; |
| 1778 | |
| 1779 | if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) |
| 1780 | return indicatePessimisticFixpoint(); |
| 1781 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } |
| 1785 | }; |
| 1786 | |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1787 | /// NoRecurse attribute deduction for a call sites. |
| 1788 | struct AANoRecurseCallSite final : AANoRecurseImpl { |
| 1789 | AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 1790 | |
| 1791 | /// See AbstractAttribute::initialize(...). |
| 1792 | void initialize(Attributor &A) override { |
| 1793 | AANoRecurseImpl::initialize(A); |
| 1794 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1795 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1796 | indicatePessimisticFixpoint(); |
| 1797 | } |
| 1798 | |
| 1799 | /// See AbstractAttribute::updateImpl(...). |
| 1800 | ChangeStatus updateImpl(Attributor &A) override { |
| 1801 | // TODO: Once we have call site specific value information we can provide |
| 1802 | // call site specific liveness information and then it makes |
| 1803 | // sense to specialize attributes for call sites arguments instead of |
| 1804 | // redirecting requests to the callee argument. |
| 1805 | Function *F = getAssociatedFunction(); |
| 1806 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1807 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); |
| 1808 | return clampStateAndIndicateChange( |
| 1809 | getState(), |
| 1810 | static_cast<const AANoRecurse::StateType &>(FnAA.getState())); |
| 1811 | } |
| 1812 | |
| 1813 | /// See AbstractAttribute::trackStatistics() |
| 1814 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } |
| 1815 | }; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1816 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1817 | /// ------------------------ Will-Return Attributes ---------------------------- |
| 1818 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1819 | // Helper function that checks whether a function has any cycle. |
| 1820 | // TODO: Replace with more efficent code |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1821 | static bool containsCycle(Function &F) { |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1822 | SmallPtrSet<BasicBlock *, 32> Visited; |
| 1823 | |
| 1824 | // Traverse BB by dfs and check whether successor is already visited. |
| 1825 | for (BasicBlock *BB : depth_first(&F)) { |
| 1826 | Visited.insert(BB); |
| 1827 | for (auto *SuccBB : successors(BB)) { |
| 1828 | if (Visited.count(SuccBB)) |
| 1829 | return true; |
| 1830 | } |
| 1831 | } |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
| 1835 | // Helper function that checks the function have a loop which might become an |
| 1836 | // endless loop |
| 1837 | // FIXME: Any cycle is regarded as endless loop for now. |
| 1838 | // We have to allow some patterns. |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1839 | static bool containsPossiblyEndlessLoop(Function *F) { |
| 1840 | return !F || !F->hasExactDefinition() || containsCycle(*F); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1843 | struct AAWillReturnImpl : public AAWillReturn { |
| 1844 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} |
| 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::initialize(...). |
| 1847 | void initialize(Attributor &A) override { |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1848 | AAWillReturn::initialize(A); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1849 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1850 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1851 | if (containsPossiblyEndlessLoop(F)) |
| 1852 | indicatePessimisticFixpoint(); |
| 1853 | } |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1854 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1855 | /// See AbstractAttribute::updateImpl(...). |
| 1856 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1857 | auto CheckForWillReturn = [&](Instruction &I) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1858 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); |
| 1859 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); |
| 1860 | if (WillReturnAA.isKnownWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1861 | return true; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1862 | if (!WillReturnAA.isAssumedWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1863 | return false; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1864 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); |
| 1865 | return NoRecurseAA.isAssumedNoRecurse(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1866 | }; |
| 1867 | |
| 1868 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) |
| 1869 | return indicatePessimisticFixpoint(); |
| 1870 | |
| 1871 | return ChangeStatus::UNCHANGED; |
| 1872 | } |
| 1873 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1874 | /// See AbstractAttribute::getAsStr() |
| 1875 | const std::string getAsStr() const override { |
| 1876 | return getAssumed() ? "willreturn" : "may-noreturn"; |
| 1877 | } |
| 1878 | }; |
| 1879 | |
| 1880 | struct AAWillReturnFunction final : AAWillReturnImpl { |
| 1881 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 1882 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1883 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1884 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1885 | }; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1886 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1887 | /// WillReturn attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1888 | struct AAWillReturnCallSite final : AAWillReturnImpl { |
| 1889 | AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 1890 | |
| 1891 | /// See AbstractAttribute::initialize(...). |
| 1892 | void initialize(Attributor &A) override { |
| 1893 | AAWillReturnImpl::initialize(A); |
| 1894 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 1895 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 1896 | indicatePessimisticFixpoint(); |
| 1897 | } |
| 1898 | |
| 1899 | /// See AbstractAttribute::updateImpl(...). |
| 1900 | ChangeStatus updateImpl(Attributor &A) override { |
| 1901 | // TODO: Once we have call site specific value information we can provide |
| 1902 | // call site specific liveness information and then it makes |
| 1903 | // sense to specialize attributes for call sites arguments instead of |
| 1904 | // redirecting requests to the callee argument. |
| 1905 | Function *F = getAssociatedFunction(); |
| 1906 | const IRPosition &FnPos = IRPosition::function(*F); |
| 1907 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); |
| 1908 | return clampStateAndIndicateChange( |
| 1909 | getState(), |
| 1910 | static_cast<const AAWillReturn::StateType &>(FnAA.getState())); |
| 1911 | } |
| 1912 | |
| 1913 | /// See AbstractAttribute::trackStatistics() |
| 1914 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } |
| 1915 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1916 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1917 | /// ------------------------ NoAlias Argument Attribute ------------------------ |
| 1918 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1919 | struct AANoAliasImpl : AANoAlias { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1920 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1921 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1922 | const std::string getAsStr() const override { |
| 1923 | return getAssumed() ? "noalias" : "may-alias"; |
| 1924 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1925 | }; |
| 1926 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1927 | /// NoAlias attribute for a floating value. |
| 1928 | struct AANoAliasFloating final : AANoAliasImpl { |
| 1929 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 1930 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1931 | /// See AbstractAttribute::initialize(...). |
| 1932 | void initialize(Attributor &A) override { |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1933 | AANoAliasImpl::initialize(A); |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 1934 | Value &Val = getAssociatedValue(); |
| 1935 | if (isa<AllocaInst>(Val)) |
| 1936 | indicateOptimisticFixpoint(); |
| 1937 | if (isa<ConstantPointerNull>(Val) && |
| 1938 | Val.getType()->getPointerAddressSpace() == 0) |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1939 | indicateOptimisticFixpoint(); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1942 | /// See AbstractAttribute::updateImpl(...). |
| 1943 | ChangeStatus updateImpl(Attributor &A) override { |
| 1944 | // TODO: Implement this. |
| 1945 | return indicatePessimisticFixpoint(); |
| 1946 | } |
| 1947 | |
| 1948 | /// See AbstractAttribute::trackStatistics() |
| 1949 | void trackStatistics() const override { |
| 1950 | STATS_DECLTRACK_FLOATING_ATTR(noalias) |
| 1951 | } |
| 1952 | }; |
| 1953 | |
| 1954 | /// NoAlias attribute for an argument. |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1955 | struct AANoAliasArgument final |
| 1956 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { |
| 1957 | AANoAliasArgument(const IRPosition &IRP) |
| 1958 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {} |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1959 | |
| 1960 | /// See AbstractAttribute::trackStatistics() |
| 1961 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } |
| 1962 | }; |
| 1963 | |
| 1964 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { |
| 1965 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 1966 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1967 | /// See AbstractAttribute::initialize(...). |
| 1968 | void initialize(Attributor &A) override { |
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 1969 | // See callsite argument attribute and callee argument attribute. |
| 1970 | ImmutableCallSite ICS(&getAnchorValue()); |
| 1971 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) |
| 1972 | indicateOptimisticFixpoint(); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1975 | /// See AbstractAttribute::updateImpl(...). |
| 1976 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 1977 | // We can deduce "noalias" if the following conditions hold. |
| 1978 | // (i) Associated value is assumed to be noalias in the definition. |
| 1979 | // (ii) Associated value is assumed to be no-capture in all the uses |
| 1980 | // possibly executed before this callsite. |
| 1981 | // (iii) There is no other pointer argument which could alias with the |
| 1982 | // value. |
| 1983 | |
| 1984 | const Value &V = getAssociatedValue(); |
| 1985 | const IRPosition IRP = IRPosition::value(V); |
| 1986 | |
| 1987 | // (i) Check whether noalias holds in the definition. |
| 1988 | |
| 1989 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); |
| 1990 | |
| 1991 | if (!NoAliasAA.isAssumedNoAlias()) |
| 1992 | return indicatePessimisticFixpoint(); |
| 1993 | |
| 1994 | LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V |
| 1995 | << " is assumed NoAlias in the definition\n"); |
| 1996 | |
| 1997 | // (ii) Check whether the value is captured in the scope using AANoCapture. |
| 1998 | // FIXME: This is conservative though, it is better to look at CFG and |
| 1999 | // check only uses possibly executed before this callsite. |
| 2000 | |
| 2001 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2002 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 2003 | LLVM_DEBUG( |
| 2004 | dbgs() << "[Attributor][AANoAliasCSArg] " << V |
| 2005 | << " cannot be noalias as it is potentially captured\n"); |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2006 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2007 | } |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2008 | |
| 2009 | // (iii) Check there is no other pointer argument which could alias with the |
| 2010 | // value. |
| 2011 | ImmutableCallSite ICS(&getAnchorValue()); |
| 2012 | for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) { |
| 2013 | if (getArgNo() == (int)i) |
| 2014 | continue; |
| 2015 | const Value *ArgOp = ICS.getArgOperand(i); |
| 2016 | if (!ArgOp->getType()->isPointerTy()) |
| 2017 | continue; |
| 2018 | |
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2019 | if (const Function *F = getAnchorScope()) { |
| 2020 | if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) { |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2021 | bool IsAliasing = AAR->isNoAlias(&getAssociatedValue(), ArgOp); |
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2022 | LLVM_DEBUG(dbgs() |
| 2023 | << "[Attributor][NoAliasCSArg] Check alias between " |
| 2024 | "callsite arguments " |
| 2025 | << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " " |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2026 | << getAssociatedValue() << " " << *ArgOp << " => " |
| 2027 | << (IsAliasing ? "" : "no-") << "alias \n"); |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2028 | |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2029 | if (IsAliasing) |
| Hideto Ueno | 30d86f1 | 2019-09-17 06:53:27 +0000 | [diff] [blame] | 2030 | continue; |
| 2031 | } |
| 2032 | } |
| Hideto Ueno | 1d68ed8 | 2019-09-11 07:00:33 +0000 | [diff] [blame] | 2033 | return indicatePessimisticFixpoint(); |
| 2034 | } |
| 2035 | |
| 2036 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2040 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2041 | }; |
| 2042 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2043 | /// NoAlias attribute for function return value. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2044 | struct AANoAliasReturned final : AANoAliasImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2045 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2046 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2047 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2048 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| 2049 | |
| 2050 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 2051 | if (Constant *C = dyn_cast<Constant>(&RV)) |
| 2052 | if (C->isNullValue() || isa<UndefValue>(C)) |
| 2053 | return true; |
| 2054 | |
| 2055 | /// For now, we can only deduce noalias if we have call sites. |
| 2056 | /// FIXME: add more support. |
| 2057 | ImmutableCallSite ICS(&RV); |
| 2058 | if (!ICS) |
| 2059 | return false; |
| 2060 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2061 | const IRPosition &RVPos = IRPosition::value(RV); |
| 2062 | const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2063 | if (!NoAliasAA.isAssumedNoAlias()) |
| 2064 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2065 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2066 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); |
| 2067 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 2068 | }; |
| 2069 | |
| 2070 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) |
| 2071 | return indicatePessimisticFixpoint(); |
| 2072 | |
| 2073 | return ChangeStatus::UNCHANGED; |
| 2074 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2075 | |
| 2076 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2077 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2078 | }; |
| 2079 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2080 | /// NoAlias attribute deduction for a call site return value. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2081 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { |
| 2082 | AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 2083 | |
| 2084 | /// See AbstractAttribute::initialize(...). |
| 2085 | void initialize(Attributor &A) override { |
| 2086 | AANoAliasImpl::initialize(A); |
| 2087 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2088 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2089 | indicatePessimisticFixpoint(); |
| 2090 | } |
| 2091 | |
| 2092 | /// See AbstractAttribute::updateImpl(...). |
| 2093 | ChangeStatus updateImpl(Attributor &A) override { |
| 2094 | // TODO: Once we have call site specific value information we can provide |
| 2095 | // call site specific liveness information and then it makes |
| 2096 | // sense to specialize attributes for call sites arguments instead of |
| 2097 | // redirecting requests to the callee argument. |
| 2098 | Function *F = getAssociatedFunction(); |
| 2099 | const IRPosition &FnPos = IRPosition::returned(*F); |
| 2100 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); |
| 2101 | return clampStateAndIndicateChange( |
| 2102 | getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); |
| 2103 | } |
| 2104 | |
| 2105 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 56e9b60 | 2019-09-04 20:34:57 +0000 | [diff] [blame] | 2106 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2107 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2108 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2109 | /// -------------------AAIsDead Function Attribute----------------------- |
| 2110 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2111 | struct AAIsDeadImpl : public AAIsDead { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2112 | AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {} |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2113 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2114 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2115 | const Function *F = getAssociatedFunction(); |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 2116 | if (F && !F->isDeclaration()) |
| 2117 | exploreFromEntry(A, F); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | void exploreFromEntry(Attributor &A, const Function *F) { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2121 | ToBeExploredPaths.insert(&(F->getEntryBlock().front())); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2122 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2123 | for (size_t i = 0; i < ToBeExploredPaths.size(); ++i) |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2124 | if (const Instruction *NextNoReturnI = |
| 2125 | findNextNoReturn(A, ToBeExploredPaths[i])) |
| 2126 | NoReturnCalls.insert(NextNoReturnI); |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 2127 | |
| 2128 | // Mark the block live after we looked for no-return instructions. |
| 2129 | assumeLive(A, F->getEntryBlock()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2132 | /// Find the next assumed noreturn instruction in the block of \p I starting |
| 2133 | /// from, thus including, \p I. |
| 2134 | /// |
| 2135 | /// The caller is responsible to monitor the ToBeExploredPaths set as new |
| 2136 | /// instructions discovered in other basic block will be placed in there. |
| 2137 | /// |
| 2138 | /// \returns The next assumed noreturn instructions in the block of \p I |
| 2139 | /// starting from, thus including, \p I. |
| 2140 | const Instruction *findNextNoReturn(Attributor &A, const Instruction *I); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2141 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2142 | /// See AbstractAttribute::getAsStr(). |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2143 | const std::string getAsStr() const override { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2144 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2145 | std::to_string(getAssociatedFunction()->size()) + "][#NRI " + |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2146 | std::to_string(NoReturnCalls.size()) + "]"; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | /// See AbstractAttribute::manifest(...). |
| 2150 | ChangeStatus manifest(Attributor &A) override { |
| 2151 | assert(getState().isValidState() && |
| 2152 | "Attempted to manifest an invalid state!"); |
| 2153 | |
| 2154 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2155 | Function &F = *getAssociatedFunction(); |
| 2156 | |
| 2157 | if (AssumedLiveBlocks.empty()) { |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2158 | A.deleteAfterManifest(F); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2159 | return ChangeStatus::CHANGED; |
| 2160 | } |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2161 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 2162 | // Flag to determine if we can change an invoke to a call assuming the |
| 2163 | // callee is nounwind. This is not possible if the personality of the |
| 2164 | // function allows to catch asynchronous exceptions. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2165 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2166 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2167 | for (const Instruction *NRC : NoReturnCalls) { |
| 2168 | Instruction *I = const_cast<Instruction *>(NRC); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2169 | BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2170 | Instruction *SplitPos = I->getNextNode(); |
| Johannes Doerfert | d410805 | 2019-08-21 20:56:41 +0000 | [diff] [blame] | 2171 | // TODO: mark stuff before unreachable instructions as dead. |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2172 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2173 | if (auto *II = dyn_cast<InvokeInst>(I)) { |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2174 | // If we keep the invoke the split position is at the beginning of the |
| 2175 | // normal desitination block (it invokes a noreturn function after all). |
| 2176 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 2177 | SplitPos = &NormalDestBB->front(); |
| 2178 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2179 | /// Invoke is replaced with a call and unreachable is placed after it if |
| 2180 | /// the callee is nounwind and noreturn. Otherwise, we keep the invoke |
| 2181 | /// and only place an unreachable in the normal successor. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2182 | if (Invoke2CallAllowed) { |
| Michael Liao | a99086d | 2019-08-20 21:02:31 +0000 | [diff] [blame] | 2183 | if (II->getCalledFunction()) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2184 | const IRPosition &IPos = IRPosition::callsite_function(*II); |
| 2185 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); |
| 2186 | if (AANoUnw.isAssumedNoUnwind()) { |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2187 | LLVM_DEBUG(dbgs() |
| 2188 | << "[AAIsDead] Replace invoke with call inst\n"); |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2189 | // We do not need an invoke (II) but instead want a call followed |
| 2190 | // by an unreachable. However, we do not remove II as other |
| 2191 | // abstract attributes might have it cached as part of their |
| 2192 | // results. Given that we modify the CFG anyway, we simply keep II |
| 2193 | // around but in a new dead block. To avoid II being live through |
| 2194 | // a different edge we have to ensure the block we place it in is |
| 2195 | // only reached from the current block of II and then not reached |
| 2196 | // at all when we insert the unreachable. |
| 2197 | SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); |
| 2198 | CallInst *CI = createCallMatchingInvoke(II); |
| 2199 | CI->insertBefore(II); |
| 2200 | CI->takeName(II); |
| 2201 | II->replaceAllUsesWith(CI); |
| 2202 | SplitPos = CI->getNextNode(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2203 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2204 | } |
| 2205 | } |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2206 | |
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2207 | if (SplitPos == &NormalDestBB->front()) { |
| 2208 | // If this is an invoke of a noreturn function the edge to the normal |
| 2209 | // destination block is dead but not necessarily the block itself. |
| 2210 | // TODO: We need to move to an edge based system during deduction and |
| 2211 | // also manifest. |
| 2212 | assert(!NormalDestBB->isLandingPad() && |
| 2213 | "Expected the normal destination not to be a landingpad!"); |
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2214 | if (NormalDestBB->getUniquePredecessor() == BB) { |
| 2215 | assumeLive(A, *NormalDestBB); |
| 2216 | } else { |
| 2217 | BasicBlock *SplitBB = |
| 2218 | SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); |
| 2219 | // The split block is live even if it contains only an unreachable |
| 2220 | // instruction at the end. |
| 2221 | assumeLive(A, *SplitBB); |
| 2222 | SplitPos = SplitBB->getTerminator(); |
| 2223 | HasChanged = ChangeStatus::CHANGED; |
| 2224 | } |
| Johannes Doerfert | 7ab5253 | 2019-09-04 20:34:52 +0000 | [diff] [blame] | 2225 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| Johannes Doerfert | 4056e7f | 2019-10-13 05:27:09 +0000 | [diff] [blame] | 2228 | if (isa_and_nonnull<UnreachableInst>(SplitPos)) |
| 2229 | continue; |
| 2230 | |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 2231 | BB = SplitPos->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2232 | SplitBlock(BB, SplitPos); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2233 | changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false); |
| 2234 | HasChanged = ChangeStatus::CHANGED; |
| 2235 | } |
| 2236 | |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 2237 | for (BasicBlock &BB : F) |
| 2238 | if (!AssumedLiveBlocks.count(&BB)) |
| 2239 | A.deleteAfterManifest(BB); |
| 2240 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2241 | return HasChanged; |
| 2242 | } |
| 2243 | |
| 2244 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2245 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2246 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2247 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2248 | bool isAssumedDead(const BasicBlock *BB) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2249 | assert(BB->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2250 | "BB must be in the same anchor scope function."); |
| 2251 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2252 | if (!getAssumed()) |
| 2253 | return false; |
| 2254 | return !AssumedLiveBlocks.count(BB); |
| 2255 | } |
| 2256 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2257 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2258 | bool isKnownDead(const BasicBlock *BB) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2259 | return getKnown() && isAssumedDead(BB); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2262 | /// See AAIsDead::isAssumed(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2263 | bool isAssumedDead(const Instruction *I) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2264 | assert(I->getParent()->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2265 | "Instruction must be in the same anchor scope function."); |
| 2266 | |
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 2267 | if (!getAssumed()) |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2268 | return false; |
| 2269 | |
| 2270 | // If it is not in AssumedLiveBlocks then it for sure dead. |
| 2271 | // Otherwise, it can still be after noreturn call in a live block. |
| 2272 | if (!AssumedLiveBlocks.count(I->getParent())) |
| 2273 | return true; |
| 2274 | |
| 2275 | // If it is not after a noreturn call, than it is live. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2276 | return isAfterNoReturn(I); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /// See AAIsDead::isKnownDead(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2280 | bool isKnownDead(const Instruction *I) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2281 | return getKnown() && isAssumedDead(I); |
| 2282 | } |
| 2283 | |
| 2284 | /// Check if instruction is after noreturn call, in other words, assumed dead. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2285 | bool isAfterNoReturn(const Instruction *I) const; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2286 | |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2287 | /// Determine if \p F might catch asynchronous exceptions. |
| 2288 | static bool mayCatchAsynchronousExceptions(const Function &F) { |
| 2289 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); |
| 2290 | } |
| 2291 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2292 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A |
| 2293 | /// that internal function called from \p BB should now be looked at. |
| 2294 | void assumeLive(Attributor &A, const BasicBlock &BB) { |
| 2295 | if (!AssumedLiveBlocks.insert(&BB).second) |
| 2296 | return; |
| 2297 | |
| 2298 | // We assume that all of BB is (probably) live now and if there are calls to |
| 2299 | // internal functions we will assume that those are now live as well. This |
| 2300 | // is a performance optimization for blocks with calls to a lot of internal |
| 2301 | // functions. It can however cause dead functions to be treated as live. |
| 2302 | for (const Instruction &I : BB) |
| 2303 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) |
| 2304 | if (const Function *F = ICS.getCalledFunction()) |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 2305 | if (F->hasLocalLinkage()) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2306 | A.markLiveInternalFunction(*F); |
| 2307 | } |
| 2308 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2309 | /// Collection of to be explored paths. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2310 | SmallSetVector<const Instruction *, 8> ToBeExploredPaths; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2311 | |
| 2312 | /// Collection of all assumed live BasicBlocks. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2313 | DenseSet<const BasicBlock *> AssumedLiveBlocks; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2314 | |
| 2315 | /// Collection of calls with noreturn attribute, assumed or knwon. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2316 | SmallSetVector<const Instruction *, 4> NoReturnCalls; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2317 | }; |
| 2318 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2319 | struct AAIsDeadFunction final : public AAIsDeadImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2320 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2321 | |
| 2322 | /// See AbstractAttribute::trackStatistics() |
| 2323 | void trackStatistics() const override { |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2324 | STATS_DECL(PartiallyDeadBlocks, Function, |
| 2325 | "Number of basic blocks classified as partially dead"); |
| 2326 | BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size(); |
| 2327 | } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2328 | }; |
| 2329 | |
| 2330 | bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2331 | const Instruction *PrevI = I->getPrevNode(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2332 | while (PrevI) { |
| 2333 | if (NoReturnCalls.count(PrevI)) |
| 2334 | return true; |
| 2335 | PrevI = PrevI->getPrevNode(); |
| 2336 | } |
| 2337 | return false; |
| 2338 | } |
| 2339 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2340 | const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A, |
| 2341 | const Instruction *I) { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2342 | const BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 2343 | const Function &F = *BB->getParent(); |
| 2344 | |
| 2345 | // Flag to determine if we can change an invoke to a call assuming the callee |
| 2346 | // is nounwind. This is not possible if the personality of the function allows |
| 2347 | // to catch asynchronous exceptions. |
| 2348 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2349 | |
| 2350 | // TODO: We should have a function that determines if an "edge" is dead. |
| 2351 | // Edges could be from an instruction to the next or from a terminator |
| 2352 | // to the successor. For now, we need to special case the unwind block |
| 2353 | // of InvokeInst below. |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2354 | |
| 2355 | while (I) { |
| 2356 | ImmutableCallSite ICS(I); |
| 2357 | |
| 2358 | if (ICS) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2359 | const IRPosition &IPos = IRPosition::callsite_function(ICS); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2360 | // Regarless of the no-return property of an invoke instruction we only |
| 2361 | // learn that the regular successor is not reachable through this |
| 2362 | // instruction but the unwind block might still be. |
| 2363 | if (auto *Invoke = dyn_cast<InvokeInst>(I)) { |
| 2364 | // Use nounwind to justify the unwind block is dead as well. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2365 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); |
| 2366 | if (!Invoke2CallAllowed || !AANoUnw.isAssumedNoUnwind()) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2367 | assumeLive(A, *Invoke->getUnwindDest()); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2368 | ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front()); |
| 2369 | } |
| 2370 | } |
| 2371 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2372 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos); |
| 2373 | if (NoReturnAA.isAssumedNoReturn()) |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2374 | return I; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | I = I->getNextNode(); |
| 2378 | } |
| 2379 | |
| 2380 | // get new paths (reachable blocks). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2381 | for (const BasicBlock *SuccBB : successors(BB)) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 2382 | assumeLive(A, *SuccBB); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2383 | ToBeExploredPaths.insert(&SuccBB->front()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2386 | // No noreturn instruction found. |
| 2387 | return nullptr; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2390 | ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2391 | ChangeStatus Status = ChangeStatus::UNCHANGED; |
| 2392 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2393 | // Temporary collection to iterate over existing noreturn instructions. This |
| 2394 | // will alow easier modification of NoReturnCalls collection |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2395 | SmallVector<const Instruction *, 8> NoReturnChanged; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2396 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2397 | for (const Instruction *I : NoReturnCalls) |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2398 | NoReturnChanged.push_back(I); |
| 2399 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2400 | for (const Instruction *I : NoReturnChanged) { |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2401 | size_t Size = ToBeExploredPaths.size(); |
| 2402 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2403 | const Instruction *NextNoReturnI = findNextNoReturn(A, I); |
| 2404 | if (NextNoReturnI != I) { |
| 2405 | Status = ChangeStatus::CHANGED; |
| 2406 | NoReturnCalls.remove(I); |
| 2407 | if (NextNoReturnI) |
| 2408 | NoReturnCalls.insert(NextNoReturnI); |
| 2409 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2410 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 2411 | // Explore new paths. |
| 2412 | while (Size != ToBeExploredPaths.size()) { |
| 2413 | Status = ChangeStatus::CHANGED; |
| 2414 | if (const Instruction *NextNoReturnI = |
| 2415 | findNextNoReturn(A, ToBeExploredPaths[Size++])) |
| 2416 | NoReturnCalls.insert(NextNoReturnI); |
| 2417 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 2420 | LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: " |
| 2421 | << AssumedLiveBlocks.size() << " Total number of blocks: " |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2422 | << getAssociatedFunction()->size() << "\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2423 | |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 2424 | // If we know everything is live there is no need to query for liveness. |
| 2425 | if (NoReturnCalls.empty() && |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 2426 | getAssociatedFunction()->size() == AssumedLiveBlocks.size()) { |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 2427 | // Indicating a pessimistic fixpoint will cause the state to be "invalid" |
| 2428 | // which will cause the Attributor to not return the AAIsDead on request, |
| 2429 | // which will prevent us from querying isAssumedDead(). |
| 2430 | indicatePessimisticFixpoint(); |
| 2431 | assert(!isValidState() && "Expected an invalid state!"); |
| Johannes Doerfert | 62a9c1d | 2019-08-29 01:26:58 +0000 | [diff] [blame] | 2432 | Status = ChangeStatus::CHANGED; |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 2435 | return Status; |
| 2436 | } |
| 2437 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2438 | /// Liveness information for a call sites. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2439 | struct AAIsDeadCallSite final : AAIsDeadImpl { |
| 2440 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadImpl(IRP) {} |
| 2441 | |
| 2442 | /// See AbstractAttribute::initialize(...). |
| 2443 | void initialize(Attributor &A) override { |
| 2444 | // TODO: Once we have call site specific value information we can provide |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2445 | // call site specific liveness information and then it makes |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2446 | // sense to specialize attributes for call sites instead of |
| 2447 | // redirecting requests to the callee. |
| 2448 | llvm_unreachable("Abstract attributes for liveness are not " |
| 2449 | "supported for call sites yet!"); |
| 2450 | } |
| 2451 | |
| 2452 | /// See AbstractAttribute::updateImpl(...). |
| 2453 | ChangeStatus updateImpl(Attributor &A) override { |
| 2454 | return indicatePessimisticFixpoint(); |
| 2455 | } |
| 2456 | |
| 2457 | /// See AbstractAttribute::trackStatistics() |
| 2458 | void trackStatistics() const override {} |
| 2459 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2460 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2461 | /// -------------------- Dereferenceable Argument Attribute -------------------- |
| 2462 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2463 | template <> |
| 2464 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, |
| 2465 | const DerefState &R) { |
| 2466 | ChangeStatus CS0 = clampStateAndIndicateChange<IntegerState>( |
| 2467 | S.DerefBytesState, R.DerefBytesState); |
| 2468 | ChangeStatus CS1 = |
| 2469 | clampStateAndIndicateChange<IntegerState>(S.GlobalState, R.GlobalState); |
| 2470 | return CS0 | CS1; |
| 2471 | } |
| 2472 | |
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 2473 | struct AADereferenceableImpl : AADereferenceable { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2474 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2475 | using StateType = DerefState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2476 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 2477 | void initialize(Attributor &A) override { |
| 2478 | SmallVector<Attribute, 4> Attrs; |
| 2479 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, |
| 2480 | Attrs); |
| 2481 | for (const Attribute &Attr : Attrs) |
| 2482 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); |
| 2483 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2484 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition()); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2485 | |
| 2486 | const IRPosition &IRP = this->getIRPosition(); |
| 2487 | bool IsFnInterface = IRP.isFnInterfaceKind(); |
| 2488 | const Function *FnScope = IRP.getAnchorScope(); |
| 2489 | if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) |
| 2490 | indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2493 | /// See AbstractAttribute::getState() |
| 2494 | /// { |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2495 | StateType &getState() override { return *this; } |
| 2496 | const StateType &getState() const override { return *this; } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2497 | /// } |
| 2498 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2499 | /// See AAFromMustBeExecutedContext |
| 2500 | bool followUse(Attributor &A, const Use *U, const Instruction *I) { |
| 2501 | bool IsNonNull = false; |
| 2502 | bool TrackUse = false; |
| 2503 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( |
| 2504 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); |
| 2505 | takeKnownDerefBytesMaximum(DerefBytes); |
| 2506 | return TrackUse; |
| 2507 | } |
| 2508 | |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 2509 | void getDeducedAttributes(LLVMContext &Ctx, |
| 2510 | SmallVectorImpl<Attribute> &Attrs) const override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2511 | // TODO: Add *_globally support |
| 2512 | if (isAssumedNonNull()) |
| 2513 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( |
| 2514 | Ctx, getAssumedDereferenceableBytes())); |
| 2515 | else |
| 2516 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( |
| 2517 | Ctx, getAssumedDereferenceableBytes())); |
| 2518 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2519 | |
| 2520 | /// See AbstractAttribute::getAsStr(). |
| 2521 | const std::string getAsStr() const override { |
| 2522 | if (!getAssumedDereferenceableBytes()) |
| 2523 | return "unknown-dereferenceable"; |
| 2524 | return std::string("dereferenceable") + |
| 2525 | (isAssumedNonNull() ? "" : "_or_null") + |
| 2526 | (isAssumedGlobal() ? "_globally" : "") + "<" + |
| 2527 | std::to_string(getKnownDereferenceableBytes()) + "-" + |
| 2528 | std::to_string(getAssumedDereferenceableBytes()) + ">"; |
| 2529 | } |
| 2530 | }; |
| 2531 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2532 | /// Dereferenceable attribute for a floating value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2533 | struct AADereferenceableFloating |
| 2534 | : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { |
| 2535 | using Base = |
| 2536 | AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; |
| 2537 | AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2538 | |
| 2539 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2540 | ChangeStatus updateImpl(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2541 | ChangeStatus Change = Base::updateImpl(A); |
| 2542 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2543 | const DataLayout &DL = A.getDataLayout(); |
| 2544 | |
| 2545 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { |
| 2546 | unsigned IdxWidth = |
| 2547 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); |
| 2548 | APInt Offset(IdxWidth, 0); |
| 2549 | const Value *Base = |
| 2550 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 2551 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2552 | const auto &AA = |
| 2553 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2554 | int64_t DerefBytes = 0; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2555 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2556 | // Use IR information if we did not strip anything. |
| 2557 | // TODO: track globally. |
| 2558 | bool CanBeNull; |
| 2559 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); |
| 2560 | T.GlobalState.indicatePessimisticFixpoint(); |
| 2561 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2562 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2563 | DerefBytes = DS.DerefBytesState.getAssumed(); |
| 2564 | T.GlobalState &= DS.GlobalState; |
| 2565 | } |
| 2566 | |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 2567 | // For now we do not try to "increase" dereferenceability due to negative |
| 2568 | // indices as we first have to come up with code to deal with loops and |
| 2569 | // for overflows of the dereferenceable bytes. |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2570 | int64_t OffsetSExt = Offset.getSExtValue(); |
| 2571 | if (OffsetSExt < 0) |
| Johannes Doerfert | db6efb0 | 2019-10-13 20:40:10 +0000 | [diff] [blame] | 2572 | OffsetSExt = 0; |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 2573 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2574 | T.takeAssumedDerefBytesMinimum( |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2575 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2576 | |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2577 | if (this == &AA) { |
| 2578 | if (!Stripped) { |
| 2579 | // If nothing was stripped IR information is all we got. |
| 2580 | T.takeKnownDerefBytesMaximum( |
| 2581 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| 2582 | T.indicatePessimisticFixpoint(); |
| 2583 | } else if (OffsetSExt > 0) { |
| 2584 | // If something was stripped but there is circular reasoning we look |
| 2585 | // for the offset. If it is positive we basically decrease the |
| 2586 | // dereferenceable bytes in a circluar loop now, which will simply |
| 2587 | // drive them down to the known value in a very slow way which we |
| 2588 | // can accelerate. |
| 2589 | T.indicatePessimisticFixpoint(); |
| 2590 | } |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | return T.isValidState(); |
| 2594 | }; |
| 2595 | |
| 2596 | DerefState T; |
| 2597 | if (!genericValueTraversal<AADereferenceable, DerefState>( |
| 2598 | A, getIRPosition(), *this, T, VisitValueCB)) |
| 2599 | return indicatePessimisticFixpoint(); |
| 2600 | |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2601 | return Change | clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | /// See AbstractAttribute::trackStatistics() |
| 2605 | void trackStatistics() const override { |
| 2606 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) |
| 2607 | } |
| 2608 | }; |
| 2609 | |
| 2610 | /// Dereferenceable attribute for a return value. |
| 2611 | struct AADereferenceableReturned final |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2612 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, |
| 2613 | DerefState> { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2614 | AADereferenceableReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2615 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, |
| 2616 | DerefState>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2617 | |
| 2618 | /// See AbstractAttribute::trackStatistics() |
| 2619 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2620 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2621 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2622 | }; |
| 2623 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2624 | /// Dereferenceable attribute for an argument |
| 2625 | struct AADereferenceableArgument final |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2626 | : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< |
| 2627 | AADereferenceable, AADereferenceableImpl, DerefState> { |
| 2628 | using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< |
| 2629 | AADereferenceable, AADereferenceableImpl, DerefState>; |
| 2630 | AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2631 | |
| 2632 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2633 | void trackStatistics() const override { |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 2634 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) |
| 2635 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2636 | }; |
| 2637 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2638 | /// Dereferenceable attribute for a call site argument. |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2639 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2640 | AADereferenceableCallSiteArgument(const IRPosition &IRP) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2641 | : AADereferenceableFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2642 | |
| 2643 | /// See AbstractAttribute::trackStatistics() |
| 2644 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2645 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2646 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2647 | }; |
| 2648 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2649 | /// Dereferenceable attribute deduction for a call site return value. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2650 | struct AADereferenceableCallSiteReturned final |
| 2651 | : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< |
| 2652 | AADereferenceable, AADereferenceableImpl> { |
| 2653 | using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< |
| 2654 | AADereferenceable, AADereferenceableImpl>; |
| 2655 | AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2656 | |
| 2657 | /// See AbstractAttribute::initialize(...). |
| 2658 | void initialize(Attributor &A) override { |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2659 | Base::initialize(A); |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2660 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2661 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2662 | indicatePessimisticFixpoint(); |
| 2663 | } |
| 2664 | |
| 2665 | /// See AbstractAttribute::updateImpl(...). |
| 2666 | ChangeStatus updateImpl(Attributor &A) override { |
| 2667 | // TODO: Once we have call site specific value information we can provide |
| 2668 | // call site specific liveness information and then it makes |
| 2669 | // sense to specialize attributes for call sites arguments instead of |
| 2670 | // redirecting requests to the callee argument. |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2671 | |
| 2672 | ChangeStatus Change = Base::updateImpl(A); |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2673 | Function *F = getAssociatedFunction(); |
| 2674 | const IRPosition &FnPos = IRPosition::returned(*F); |
| 2675 | auto &FnAA = A.getAAFor<AADereferenceable>(*this, FnPos); |
| Hideto Ueno | 96e6ce4 | 2019-10-08 15:25:56 +0000 | [diff] [blame] | 2676 | return Change | |
| 2677 | clampStateAndIndicateChange( |
| 2678 | getState(), static_cast<const DerefState &>(FnAA.getState())); |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | /// See AbstractAttribute::trackStatistics() |
| 2682 | void trackStatistics() const override { |
| 2683 | STATS_DECLTRACK_CS_ATTR(dereferenceable); |
| 2684 | } |
| 2685 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2686 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2687 | // ------------------------ Align Argument Attribute ------------------------ |
| 2688 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2689 | struct AAAlignImpl : AAAlign { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2690 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2691 | |
| 2692 | // Max alignemnt value allowed in IR |
| 2693 | static const unsigned MAX_ALIGN = 1U << 29; |
| 2694 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2695 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2696 | void initialize(Attributor &A) override { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2697 | takeAssumedMinimum(MAX_ALIGN); |
| 2698 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2699 | SmallVector<Attribute, 4> Attrs; |
| 2700 | getAttrs({Attribute::Alignment}, Attrs); |
| 2701 | for (const Attribute &Attr : Attrs) |
| 2702 | takeKnownMaximum(Attr.getValueAsInt()); |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 2703 | |
| 2704 | if (getIRPosition().isFnInterfaceKind() && |
| 2705 | (!getAssociatedFunction() || |
| 2706 | !getAssociatedFunction()->hasExactDefinition())) |
| 2707 | indicatePessimisticFixpoint(); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2710 | /// See AbstractAttribute::manifest(...). |
| 2711 | ChangeStatus manifest(Attributor &A) override { |
| 2712 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 2713 | |
| 2714 | // Check for users that allow alignment annotations. |
| 2715 | Value &AnchorVal = getIRPosition().getAnchorValue(); |
| 2716 | for (const Use &U : AnchorVal.uses()) { |
| 2717 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { |
| 2718 | if (SI->getPointerOperand() == &AnchorVal) |
| 2719 | if (SI->getAlignment() < getAssumedAlign()) { |
| 2720 | STATS_DECLTRACK(AAAlign, Store, |
| 2721 | "Number of times alignemnt added to a store"); |
| Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 2722 | SI->setAlignment(Align(getAssumedAlign())); |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2723 | Changed = ChangeStatus::CHANGED; |
| 2724 | } |
| 2725 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { |
| 2726 | if (LI->getPointerOperand() == &AnchorVal) |
| 2727 | if (LI->getAlignment() < getAssumedAlign()) { |
| Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 2728 | LI->setAlignment(Align(getAssumedAlign())); |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2729 | STATS_DECLTRACK(AAAlign, Load, |
| 2730 | "Number of times alignemnt added to a load"); |
| 2731 | Changed = ChangeStatus::CHANGED; |
| 2732 | } |
| 2733 | } |
| 2734 | } |
| 2735 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 2736 | return AAAlign::manifest(A) | Changed; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame] | 2739 | // TODO: Provide a helper to determine the implied ABI alignment and check in |
| 2740 | // the existing manifest method and a new one for AAAlignImpl that value |
| 2741 | // to avoid making the alignment explicit if it did not improve. |
| 2742 | |
| 2743 | /// See AbstractAttribute::getDeducedAttributes |
| 2744 | virtual void |
| 2745 | getDeducedAttributes(LLVMContext &Ctx, |
| 2746 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 2747 | if (getAssumedAlign() > 1) |
| 2748 | Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign())); |
| 2749 | } |
| 2750 | |
| 2751 | /// See AbstractAttribute::getAsStr(). |
| 2752 | const std::string getAsStr() const override { |
| 2753 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + |
| 2754 | "-" + std::to_string(getAssumedAlign()) + ">") |
| 2755 | : "unknown-align"; |
| 2756 | } |
| 2757 | }; |
| 2758 | |
| 2759 | /// Align attribute for a floating value. |
| 2760 | struct AAAlignFloating : AAAlignImpl { |
| 2761 | AAAlignFloating(const IRPosition &IRP) : AAAlignImpl(IRP) {} |
| 2762 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2763 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2764 | ChangeStatus updateImpl(Attributor &A) override { |
| 2765 | const DataLayout &DL = A.getDataLayout(); |
| 2766 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2767 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, |
| 2768 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2769 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); |
| 2770 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2771 | // Use only IR information if we did not strip anything. |
| 2772 | T.takeKnownMaximum(V.getPointerAlignment(DL)); |
| 2773 | T.indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2774 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2775 | // Use abstract attribute information. |
| 2776 | const AAAlign::StateType &DS = |
| 2777 | static_cast<const AAAlign::StateType &>(AA.getState()); |
| 2778 | T ^= DS; |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2779 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2780 | return T.isValidState(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2781 | }; |
| 2782 | |
| 2783 | StateType T; |
| 2784 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, |
| 2785 | VisitValueCB)) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2786 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2787 | |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 2788 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 2789 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2790 | return clampStateAndIndicateChange(getState(), T); |
| 2791 | } |
| 2792 | |
| 2793 | /// See AbstractAttribute::trackStatistics() |
| 2794 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } |
| 2795 | }; |
| 2796 | |
| 2797 | /// Align attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2798 | struct AAAlignReturned final |
| 2799 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2800 | AAAlignReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2801 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2802 | |
| 2803 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2804 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2805 | }; |
| 2806 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2807 | /// Align attribute for function argument. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2808 | struct AAAlignArgument final |
| 2809 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2810 | AAAlignArgument(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2811 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2812 | |
| 2813 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 2814 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2815 | }; |
| 2816 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2817 | struct AAAlignCallSiteArgument final : AAAlignFloating { |
| 2818 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2819 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2820 | /// See AbstractAttribute::manifest(...). |
| 2821 | ChangeStatus manifest(Attributor &A) override { |
| 2822 | return AAAlignImpl::manifest(A); |
| 2823 | } |
| 2824 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2825 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2826 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2827 | }; |
| 2828 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2829 | /// Align attribute deduction for a call site return value. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2830 | struct AAAlignCallSiteReturned final : AAAlignImpl { |
| 2831 | AAAlignCallSiteReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {} |
| 2832 | |
| 2833 | /// See AbstractAttribute::initialize(...). |
| 2834 | void initialize(Attributor &A) override { |
| 2835 | AAAlignImpl::initialize(A); |
| 2836 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2837 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2838 | indicatePessimisticFixpoint(); |
| 2839 | } |
| 2840 | |
| 2841 | /// See AbstractAttribute::updateImpl(...). |
| 2842 | ChangeStatus updateImpl(Attributor &A) override { |
| 2843 | // TODO: Once we have call site specific value information we can provide |
| 2844 | // call site specific liveness information and then it makes |
| 2845 | // sense to specialize attributes for call sites arguments instead of |
| 2846 | // redirecting requests to the callee argument. |
| 2847 | Function *F = getAssociatedFunction(); |
| 2848 | const IRPosition &FnPos = IRPosition::returned(*F); |
| 2849 | auto &FnAA = A.getAAFor<AAAlign>(*this, FnPos); |
| 2850 | return clampStateAndIndicateChange( |
| 2851 | getState(), static_cast<const AAAlign::StateType &>(FnAA.getState())); |
| 2852 | } |
| 2853 | |
| 2854 | /// See AbstractAttribute::trackStatistics() |
| 2855 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } |
| 2856 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2857 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2858 | /// ------------------ Function No-Return Attribute ---------------------------- |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2859 | struct AANoReturnImpl : public AANoReturn { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2860 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2861 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2862 | /// See AbstractAttribute::getAsStr(). |
| 2863 | const std::string getAsStr() const override { |
| 2864 | return getAssumed() ? "noreturn" : "may-return"; |
| 2865 | } |
| 2866 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2867 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2868 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2869 | auto CheckForNoReturn = [](Instruction &) { return false; }; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2870 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2871 | {(unsigned)Instruction::Ret})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2872 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2873 | return ChangeStatus::UNCHANGED; |
| 2874 | } |
| 2875 | }; |
| 2876 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2877 | struct AANoReturnFunction final : AANoReturnImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2878 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2879 | |
| 2880 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2881 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2882 | }; |
| 2883 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2884 | /// NoReturn attribute deduction for a call sites. |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2885 | struct AANoReturnCallSite final : AANoReturnImpl { |
| 2886 | AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| 2887 | |
| 2888 | /// See AbstractAttribute::initialize(...). |
| 2889 | void initialize(Attributor &A) override { |
| 2890 | AANoReturnImpl::initialize(A); |
| 2891 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2892 | if (!F) |
| Johannes Doerfert | 3fac668 | 2019-08-30 15:24:52 +0000 | [diff] [blame] | 2893 | indicatePessimisticFixpoint(); |
| 2894 | } |
| 2895 | |
| 2896 | /// See AbstractAttribute::updateImpl(...). |
| 2897 | ChangeStatus updateImpl(Attributor &A) override { |
| 2898 | // TODO: Once we have call site specific value information we can provide |
| 2899 | // call site specific liveness information and then it makes |
| 2900 | // sense to specialize attributes for call sites arguments instead of |
| 2901 | // redirecting requests to the callee argument. |
| 2902 | Function *F = getAssociatedFunction(); |
| 2903 | const IRPosition &FnPos = IRPosition::function(*F); |
| 2904 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); |
| 2905 | return clampStateAndIndicateChange( |
| 2906 | getState(), |
| 2907 | static_cast<const AANoReturn::StateType &>(FnAA.getState())); |
| 2908 | } |
| 2909 | |
| 2910 | /// See AbstractAttribute::trackStatistics() |
| 2911 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } |
| 2912 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2913 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2914 | /// ----------------------- Variable Capturing --------------------------------- |
| 2915 | |
| 2916 | /// A class to hold the state of for no-capture attributes. |
| 2917 | struct AANoCaptureImpl : public AANoCapture { |
| 2918 | AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} |
| 2919 | |
| 2920 | /// See AbstractAttribute::initialize(...). |
| 2921 | void initialize(Attributor &A) override { |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2922 | AANoCapture::initialize(A); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2923 | |
| Johannes Doerfert | 72adda1 | 2019-10-10 05:33:21 +0000 | [diff] [blame] | 2924 | // You cannot "capture" null in the default address space. |
| 2925 | if (isa<ConstantPointerNull>(getAssociatedValue()) && |
| 2926 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { |
| 2927 | indicateOptimisticFixpoint(); |
| 2928 | return; |
| 2929 | } |
| 2930 | |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2931 | const IRPosition &IRP = getIRPosition(); |
| 2932 | const Function *F = |
| 2933 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); |
| 2934 | |
| 2935 | // Check what state the associated function can actually capture. |
| 2936 | if (F) |
| 2937 | determineFunctionCaptureCapabilities(*F, *this); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 2938 | else |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2939 | indicatePessimisticFixpoint(); |
| 2940 | } |
| 2941 | |
| 2942 | /// See AbstractAttribute::updateImpl(...). |
| 2943 | ChangeStatus updateImpl(Attributor &A) override; |
| 2944 | |
| 2945 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). |
| 2946 | virtual void |
| 2947 | getDeducedAttributes(LLVMContext &Ctx, |
| 2948 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 2949 | if (!isAssumedNoCaptureMaybeReturned()) |
| 2950 | return; |
| 2951 | |
| Hideto Ueno | 3736764 | 2019-09-11 06:52:11 +0000 | [diff] [blame] | 2952 | if (getArgNo() >= 0) { |
| 2953 | if (isAssumedNoCapture()) |
| 2954 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); |
| 2955 | else if (ManifestInternal) |
| 2956 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); |
| 2957 | } |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known |
| 2961 | /// depending on the ability of the function associated with \p IRP to capture |
| 2962 | /// state in memory and through "returning/throwing", respectively. |
| 2963 | static void determineFunctionCaptureCapabilities(const Function &F, |
| 2964 | IntegerState &State) { |
| 2965 | // TODO: Once we have memory behavior attributes we should use them here. |
| 2966 | |
| 2967 | // If we know we cannot communicate or write to memory, we do not care about |
| 2968 | // ptr2int anymore. |
| 2969 | if (F.onlyReadsMemory() && F.doesNotThrow() && |
| 2970 | F.getReturnType()->isVoidTy()) { |
| 2971 | State.addKnownBits(NO_CAPTURE); |
| 2972 | return; |
| 2973 | } |
| 2974 | |
| 2975 | // A function cannot capture state in memory if it only reads memory, it can |
| 2976 | // however return/throw state and the state might be influenced by the |
| 2977 | // pointer value, e.g., loading from a returned pointer might reveal a bit. |
| 2978 | if (F.onlyReadsMemory()) |
| 2979 | State.addKnownBits(NOT_CAPTURED_IN_MEM); |
| 2980 | |
| 2981 | // A function cannot communicate state back if it does not through |
| 2982 | // exceptions and doesn not return values. |
| 2983 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) |
| 2984 | State.addKnownBits(NOT_CAPTURED_IN_RET); |
| 2985 | } |
| 2986 | |
| 2987 | /// See AbstractState::getAsStr(). |
| 2988 | const std::string getAsStr() const override { |
| 2989 | if (isKnownNoCapture()) |
| 2990 | return "known not-captured"; |
| 2991 | if (isAssumedNoCapture()) |
| 2992 | return "assumed not-captured"; |
| 2993 | if (isKnownNoCaptureMaybeReturned()) |
| 2994 | return "known not-captured-maybe-returned"; |
| 2995 | if (isAssumedNoCaptureMaybeReturned()) |
| 2996 | return "assumed not-captured-maybe-returned"; |
| 2997 | return "assumed-captured"; |
| 2998 | } |
| 2999 | }; |
| 3000 | |
| 3001 | /// Attributor-aware capture tracker. |
| 3002 | struct AACaptureUseTracker final : public CaptureTracker { |
| 3003 | |
| 3004 | /// Create a capture tracker that can lookup in-flight abstract attributes |
| 3005 | /// through the Attributor \p A. |
| 3006 | /// |
| 3007 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the |
| 3008 | /// search is stopped. If a use leads to a return instruction, |
| 3009 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. |
| 3010 | /// If a use leads to a ptr2int which may capture the value, |
| 3011 | /// \p CapturedInInteger is set. If a use is found that is currently assumed |
| 3012 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies |
| 3013 | /// set. All values in \p PotentialCopies are later tracked as well. For every |
| 3014 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, |
| 3015 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger |
| 3016 | /// conservatively set to true. |
| 3017 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, |
| 3018 | const AAIsDead &IsDeadAA, IntegerState &State, |
| 3019 | SmallVectorImpl<const Value *> &PotentialCopies, |
| 3020 | unsigned &RemainingUsesToExplore) |
| 3021 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), |
| 3022 | PotentialCopies(PotentialCopies), |
| 3023 | RemainingUsesToExplore(RemainingUsesToExplore) {} |
| 3024 | |
| 3025 | /// Determine if \p V maybe captured. *Also updates the state!* |
| 3026 | bool valueMayBeCaptured(const Value *V) { |
| 3027 | if (V->getType()->isPointerTy()) { |
| 3028 | PointerMayBeCaptured(V, this); |
| 3029 | } else { |
| 3030 | State.indicatePessimisticFixpoint(); |
| 3031 | } |
| 3032 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); |
| 3033 | } |
| 3034 | |
| 3035 | /// See CaptureTracker::tooManyUses(). |
| 3036 | void tooManyUses() override { |
| 3037 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); |
| 3038 | } |
| 3039 | |
| 3040 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { |
| 3041 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) |
| 3042 | return true; |
| 3043 | const auto &DerefAA = |
| 3044 | A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O)); |
| 3045 | return DerefAA.getAssumedDereferenceableBytes(); |
| 3046 | } |
| 3047 | |
| 3048 | /// See CaptureTracker::captured(...). |
| 3049 | bool captured(const Use *U) override { |
| 3050 | Instruction *UInst = cast<Instruction>(U->getUser()); |
| 3051 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst |
| 3052 | << "\n"); |
| 3053 | |
| 3054 | // Because we may reuse the tracker multiple times we keep track of the |
| 3055 | // number of explored uses ourselves as well. |
| 3056 | if (RemainingUsesToExplore-- == 0) { |
| 3057 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); |
| 3058 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 3059 | /* Return */ true); |
| 3060 | } |
| 3061 | |
| 3062 | // Deal with ptr2int by following uses. |
| 3063 | if (isa<PtrToIntInst>(UInst)) { |
| 3064 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); |
| 3065 | return valueMayBeCaptured(UInst); |
| 3066 | } |
| 3067 | |
| 3068 | // Explicitly catch return instructions. |
| 3069 | if (isa<ReturnInst>(UInst)) |
| 3070 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 3071 | /* Return */ true); |
| 3072 | |
| 3073 | // For now we only use special logic for call sites. However, the tracker |
| 3074 | // itself knows about a lot of other non-capturing cases already. |
| 3075 | CallSite CS(UInst); |
| 3076 | if (!CS || !CS.isArgOperand(U)) |
| 3077 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 3078 | /* Return */ true); |
| 3079 | |
| 3080 | unsigned ArgNo = CS.getArgumentNo(U); |
| 3081 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); |
| 3082 | // If we have a abstract no-capture attribute for the argument we can use |
| 3083 | // it to justify a non-capture attribute here. This allows recursion! |
| 3084 | auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); |
| 3085 | if (ArgNoCaptureAA.isAssumedNoCapture()) |
| 3086 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 3087 | /* Return */ false); |
| 3088 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 3089 | addPotentialCopy(CS); |
| 3090 | return isCapturedIn(/* Memory */ false, /* Integer */ false, |
| 3091 | /* Return */ false); |
| 3092 | } |
| 3093 | |
| 3094 | // Lastly, we could not find a reason no-capture can be assumed so we don't. |
| 3095 | return isCapturedIn(/* Memory */ true, /* Integer */ true, |
| 3096 | /* Return */ true); |
| 3097 | } |
| 3098 | |
| 3099 | /// Register \p CS as potential copy of the value we are checking. |
| 3100 | void addPotentialCopy(CallSite CS) { |
| 3101 | PotentialCopies.push_back(CS.getInstruction()); |
| 3102 | } |
| 3103 | |
| 3104 | /// See CaptureTracker::shouldExplore(...). |
| 3105 | bool shouldExplore(const Use *U) override { |
| 3106 | // Check liveness. |
| 3107 | return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser())); |
| 3108 | } |
| 3109 | |
| 3110 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and |
| 3111 | /// \p CapturedInRet, then return the appropriate value for use in the |
| 3112 | /// CaptureTracker::captured() interface. |
| 3113 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, |
| 3114 | bool CapturedInRet) { |
| 3115 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " |
| 3116 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); |
| 3117 | if (CapturedInMem) |
| 3118 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); |
| 3119 | if (CapturedInInt) |
| 3120 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); |
| 3121 | if (CapturedInRet) |
| 3122 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); |
| 3123 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); |
| 3124 | } |
| 3125 | |
| 3126 | private: |
| 3127 | /// The attributor providing in-flight abstract attributes. |
| 3128 | Attributor &A; |
| 3129 | |
| 3130 | /// The abstract attribute currently updated. |
| 3131 | AANoCapture &NoCaptureAA; |
| 3132 | |
| 3133 | /// The abstract liveness state. |
| 3134 | const AAIsDead &IsDeadAA; |
| 3135 | |
| 3136 | /// The state currently updated. |
| 3137 | IntegerState &State; |
| 3138 | |
| 3139 | /// Set of potential copies of the tracked value. |
| 3140 | SmallVectorImpl<const Value *> &PotentialCopies; |
| 3141 | |
| 3142 | /// Global counter to limit the number of explored uses. |
| 3143 | unsigned &RemainingUsesToExplore; |
| 3144 | }; |
| 3145 | |
| 3146 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { |
| 3147 | const IRPosition &IRP = getIRPosition(); |
| 3148 | const Value *V = |
| 3149 | getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); |
| 3150 | if (!V) |
| 3151 | return indicatePessimisticFixpoint(); |
| 3152 | |
| 3153 | const Function *F = |
| 3154 | getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); |
| 3155 | assert(F && "Expected a function!"); |
| 3156 | const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, IRPosition::function(*F)); |
| 3157 | |
| 3158 | AANoCapture::StateType T; |
| 3159 | // TODO: Once we have memory behavior attributes we should use them here |
| 3160 | // similar to the reasoning in |
| 3161 | // AANoCaptureImpl::determineFunctionCaptureCapabilities(...). |
| 3162 | |
| 3163 | // TODO: Use the AAReturnedValues to learn if the argument can return or |
| 3164 | // not. |
| 3165 | |
| 3166 | // Use the CaptureTracker interface and logic with the specialized tracker, |
| 3167 | // defined in AACaptureUseTracker, that can look at in-flight abstract |
| 3168 | // attributes and directly updates the assumed state. |
| 3169 | SmallVector<const Value *, 4> PotentialCopies; |
| 3170 | unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; |
| 3171 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, |
| 3172 | RemainingUsesToExplore); |
| 3173 | |
| 3174 | // Check all potential copies of the associated value until we can assume |
| 3175 | // none will be captured or we have to assume at least one might be. |
| 3176 | unsigned Idx = 0; |
| 3177 | PotentialCopies.push_back(V); |
| 3178 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) |
| 3179 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); |
| 3180 | |
| 3181 | AAAlign::StateType &S = getState(); |
| 3182 | auto Assumed = S.getAssumed(); |
| 3183 | S.intersectAssumedBits(T.getAssumed()); |
| 3184 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED |
| 3185 | : ChangeStatus::CHANGED; |
| 3186 | } |
| 3187 | |
| 3188 | /// NoCapture attribute for function arguments. |
| 3189 | struct AANoCaptureArgument final : AANoCaptureImpl { |
| 3190 | AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 3191 | |
| 3192 | /// See AbstractAttribute::trackStatistics() |
| 3193 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } |
| 3194 | }; |
| 3195 | |
| 3196 | /// NoCapture attribute for call site arguments. |
| 3197 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { |
| 3198 | AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 3199 | |
| 3200 | /// See AbstractAttribute::updateImpl(...). |
| 3201 | ChangeStatus updateImpl(Attributor &A) override { |
| 3202 | // TODO: Once we have call site specific value information we can provide |
| 3203 | // call site specific liveness information and then it makes |
| 3204 | // sense to specialize attributes for call sites arguments instead of |
| 3205 | // redirecting requests to the callee argument. |
| 3206 | Argument *Arg = getAssociatedArgument(); |
| 3207 | if (!Arg) |
| 3208 | return indicatePessimisticFixpoint(); |
| 3209 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 3210 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); |
| 3211 | return clampStateAndIndicateChange( |
| 3212 | getState(), |
| 3213 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); |
| 3214 | } |
| 3215 | |
| 3216 | /// See AbstractAttribute::trackStatistics() |
| 3217 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; |
| 3218 | }; |
| 3219 | |
| 3220 | /// NoCapture attribute for floating values. |
| 3221 | struct AANoCaptureFloating final : AANoCaptureImpl { |
| 3222 | AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 3223 | |
| 3224 | /// See AbstractAttribute::trackStatistics() |
| 3225 | void trackStatistics() const override { |
| 3226 | STATS_DECLTRACK_FLOATING_ATTR(nocapture) |
| 3227 | } |
| 3228 | }; |
| 3229 | |
| 3230 | /// NoCapture attribute for function return value. |
| 3231 | struct AANoCaptureReturned final : AANoCaptureImpl { |
| 3232 | AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { |
| 3233 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 3234 | } |
| 3235 | |
| 3236 | /// See AbstractAttribute::initialize(...). |
| 3237 | void initialize(Attributor &A) override { |
| 3238 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 3239 | } |
| 3240 | |
| 3241 | /// See AbstractAttribute::updateImpl(...). |
| 3242 | ChangeStatus updateImpl(Attributor &A) override { |
| 3243 | llvm_unreachable("NoCapture is not applicable to function returns!"); |
| 3244 | } |
| 3245 | |
| 3246 | /// See AbstractAttribute::trackStatistics() |
| 3247 | void trackStatistics() const override {} |
| 3248 | }; |
| 3249 | |
| 3250 | /// NoCapture attribute deduction for a call site return value. |
| 3251 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { |
| 3252 | AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} |
| 3253 | |
| 3254 | /// See AbstractAttribute::trackStatistics() |
| 3255 | void trackStatistics() const override { |
| 3256 | STATS_DECLTRACK_CSRET_ATTR(nocapture) |
| 3257 | } |
| 3258 | }; |
| 3259 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 3260 | /// ------------------ Value Simplify Attribute ---------------------------- |
| 3261 | struct AAValueSimplifyImpl : AAValueSimplify { |
| 3262 | AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} |
| 3263 | |
| 3264 | /// See AbstractAttribute::getAsStr(). |
| 3265 | const std::string getAsStr() const override { |
| 3266 | return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") |
| 3267 | : "not-simple"; |
| 3268 | } |
| 3269 | |
| 3270 | /// See AbstractAttribute::trackStatistics() |
| 3271 | void trackStatistics() const override {} |
| 3272 | |
| 3273 | /// See AAValueSimplify::getAssumedSimplifiedValue() |
| 3274 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { |
| 3275 | if (!getAssumed()) |
| 3276 | return const_cast<Value *>(&getAssociatedValue()); |
| 3277 | return SimplifiedAssociatedValue; |
| 3278 | } |
| 3279 | void initialize(Attributor &A) override {} |
| 3280 | |
| 3281 | /// Helper function for querying AAValueSimplify and updating candicate. |
| 3282 | /// \param QueryingValue Value trying to unify with SimplifiedValue |
| 3283 | /// \param AccumulatedSimplifiedValue Current simplification result. |
| 3284 | static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, |
| 3285 | Value &QueryingValue, |
| 3286 | Optional<Value *> &AccumulatedSimplifiedValue) { |
| 3287 | // FIXME: Add a typecast support. |
| 3288 | |
| 3289 | auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>( |
| 3290 | QueryingAA, IRPosition::value(QueryingValue)); |
| 3291 | |
| 3292 | Optional<Value *> QueryingValueSimplified = |
| 3293 | ValueSimpifyAA.getAssumedSimplifiedValue(A); |
| 3294 | |
| 3295 | if (!QueryingValueSimplified.hasValue()) |
| 3296 | return true; |
| 3297 | |
| 3298 | if (!QueryingValueSimplified.getValue()) |
| 3299 | return false; |
| 3300 | |
| 3301 | Value &QueryingValueSimplifiedUnwrapped = |
| 3302 | *QueryingValueSimplified.getValue(); |
| 3303 | |
| 3304 | if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) |
| 3305 | return true; |
| 3306 | |
| 3307 | if (AccumulatedSimplifiedValue.hasValue()) |
| 3308 | return AccumulatedSimplifiedValue == QueryingValueSimplified; |
| 3309 | |
| 3310 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue |
| 3311 | << " is assumed to be " |
| 3312 | << QueryingValueSimplifiedUnwrapped << "\n"); |
| 3313 | |
| 3314 | AccumulatedSimplifiedValue = QueryingValueSimplified; |
| 3315 | return true; |
| 3316 | } |
| 3317 | |
| 3318 | /// See AbstractAttribute::manifest(...). |
| 3319 | ChangeStatus manifest(Attributor &A) override { |
| 3320 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 3321 | |
| 3322 | if (!SimplifiedAssociatedValue.hasValue() || |
| 3323 | !SimplifiedAssociatedValue.getValue()) |
| 3324 | return Changed; |
| 3325 | |
| 3326 | if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) { |
| 3327 | // We can replace the AssociatedValue with the constant. |
| 3328 | Value &V = getAssociatedValue(); |
| 3329 | if (!V.user_empty() && &V != C && V.getType() == C->getType()) { |
| 3330 | LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C |
| 3331 | << "\n"); |
| 3332 | V.replaceAllUsesWith(C); |
| 3333 | Changed = ChangeStatus::CHANGED; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | return Changed | AAValueSimplify::manifest(A); |
| 3338 | } |
| 3339 | |
| 3340 | protected: |
| 3341 | // An assumed simplified value. Initially, it is set to Optional::None, which |
| 3342 | // means that the value is not clear under current assumption. If in the |
| 3343 | // pessimistic state, getAssumedSimplifiedValue doesn't return this value but |
| 3344 | // returns orignal associated value. |
| 3345 | Optional<Value *> SimplifiedAssociatedValue; |
| 3346 | }; |
| 3347 | |
| 3348 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { |
| 3349 | AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 3350 | |
| 3351 | /// See AbstractAttribute::updateImpl(...). |
| 3352 | ChangeStatus updateImpl(Attributor &A) override { |
| 3353 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 3354 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 3355 | auto PredForCallSite = [&](AbstractCallSite ACS) { |
| 3356 | // Check if we have an associated argument or not (which can happen for |
| 3357 | // callback calls). |
| 3358 | if (Value *ArgOp = ACS.getCallArgOperand(getArgNo())) |
| 3359 | return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue); |
| 3360 | return false; |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 3361 | }; |
| 3362 | |
| 3363 | if (!A.checkForAllCallSites(PredForCallSite, *this, true)) |
| 3364 | return indicatePessimisticFixpoint(); |
| 3365 | |
| 3366 | // If a candicate was found in this update, return CHANGED. |
| 3367 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 3368 | ? ChangeStatus::UNCHANGED |
| 3369 | : ChangeStatus ::CHANGED; |
| 3370 | } |
| 3371 | |
| 3372 | /// See AbstractAttribute::trackStatistics() |
| 3373 | void trackStatistics() const override { |
| 3374 | STATS_DECLTRACK_ARG_ATTR(value_simplify) |
| 3375 | } |
| 3376 | }; |
| 3377 | |
| 3378 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { |
| 3379 | AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 3380 | |
| 3381 | /// See AbstractAttribute::updateImpl(...). |
| 3382 | ChangeStatus updateImpl(Attributor &A) override { |
| 3383 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 3384 | |
| 3385 | auto PredForReturned = [&](Value &V) { |
| 3386 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); |
| 3387 | }; |
| 3388 | |
| 3389 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) |
| 3390 | return indicatePessimisticFixpoint(); |
| 3391 | |
| 3392 | // If a candicate was found in this update, return CHANGED. |
| 3393 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 3394 | ? ChangeStatus::UNCHANGED |
| 3395 | : ChangeStatus ::CHANGED; |
| 3396 | } |
| 3397 | /// See AbstractAttribute::trackStatistics() |
| 3398 | void trackStatistics() const override { |
| 3399 | STATS_DECLTRACK_FNRET_ATTR(value_simplify) |
| 3400 | } |
| 3401 | }; |
| 3402 | |
| 3403 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { |
| 3404 | AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 3405 | |
| 3406 | /// See AbstractAttribute::initialize(...). |
| 3407 | void initialize(Attributor &A) override { |
| 3408 | Value &V = getAnchorValue(); |
| 3409 | |
| 3410 | // TODO: add other stuffs |
| 3411 | if (isa<Constant>(V) || isa<UndefValue>(V)) |
| 3412 | indicatePessimisticFixpoint(); |
| 3413 | } |
| 3414 | |
| 3415 | /// See AbstractAttribute::updateImpl(...). |
| 3416 | ChangeStatus updateImpl(Attributor &A) override { |
| 3417 | bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); |
| 3418 | |
| 3419 | auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool { |
| 3420 | auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); |
| 3421 | if (!Stripped && this == &AA) { |
| 3422 | // TODO: Look the instruction and check recursively. |
| 3423 | LLVM_DEBUG( |
| 3424 | dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : " |
| 3425 | << V << "\n"); |
| 3426 | indicatePessimisticFixpoint(); |
| 3427 | return false; |
| 3428 | } |
| 3429 | return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); |
| 3430 | }; |
| 3431 | |
| 3432 | if (!genericValueTraversal<AAValueSimplify, BooleanState>( |
| 3433 | A, getIRPosition(), *this, static_cast<BooleanState &>(*this), |
| 3434 | VisitValueCB)) |
| 3435 | return indicatePessimisticFixpoint(); |
| 3436 | |
| 3437 | // If a candicate was found in this update, return CHANGED. |
| 3438 | |
| 3439 | return HasValueBefore == SimplifiedAssociatedValue.hasValue() |
| 3440 | ? ChangeStatus::UNCHANGED |
| 3441 | : ChangeStatus ::CHANGED; |
| 3442 | } |
| 3443 | |
| 3444 | /// See AbstractAttribute::trackStatistics() |
| 3445 | void trackStatistics() const override { |
| 3446 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify) |
| 3447 | } |
| 3448 | }; |
| 3449 | |
| 3450 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { |
| 3451 | AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} |
| 3452 | |
| 3453 | /// See AbstractAttribute::initialize(...). |
| 3454 | void initialize(Attributor &A) override { |
| 3455 | SimplifiedAssociatedValue = &getAnchorValue(); |
| 3456 | indicateOptimisticFixpoint(); |
| 3457 | } |
| 3458 | /// See AbstractAttribute::initialize(...). |
| 3459 | ChangeStatus updateImpl(Attributor &A) override { |
| 3460 | llvm_unreachable( |
| 3461 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); |
| 3462 | } |
| 3463 | /// See AbstractAttribute::trackStatistics() |
| 3464 | void trackStatistics() const override { |
| 3465 | STATS_DECLTRACK_FN_ATTR(value_simplify) |
| 3466 | } |
| 3467 | }; |
| 3468 | |
| 3469 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { |
| 3470 | AAValueSimplifyCallSite(const IRPosition &IRP) |
| 3471 | : AAValueSimplifyFunction(IRP) {} |
| 3472 | /// See AbstractAttribute::trackStatistics() |
| 3473 | void trackStatistics() const override { |
| 3474 | STATS_DECLTRACK_CS_ATTR(value_simplify) |
| 3475 | } |
| 3476 | }; |
| 3477 | |
| 3478 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { |
| 3479 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP) |
| 3480 | : AAValueSimplifyReturned(IRP) {} |
| 3481 | |
| 3482 | void trackStatistics() const override { |
| 3483 | STATS_DECLTRACK_CSRET_ATTR(value_simplify) |
| 3484 | } |
| 3485 | }; |
| 3486 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { |
| 3487 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP) |
| 3488 | : AAValueSimplifyFloating(IRP) {} |
| 3489 | |
| 3490 | void trackStatistics() const override { |
| 3491 | STATS_DECLTRACK_CSARG_ATTR(value_simplify) |
| 3492 | } |
| 3493 | }; |
| 3494 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3495 | /// ----------------------- Heap-To-Stack Conversion --------------------------- |
| 3496 | struct AAHeapToStackImpl : public AAHeapToStack { |
| 3497 | AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} |
| 3498 | |
| 3499 | const std::string getAsStr() const override { |
| 3500 | return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); |
| 3501 | } |
| 3502 | |
| 3503 | ChangeStatus manifest(Attributor &A) override { |
| 3504 | assert(getState().isValidState() && |
| 3505 | "Attempted to manifest an invalid state!"); |
| 3506 | |
| 3507 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| 3508 | Function *F = getAssociatedFunction(); |
| 3509 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); |
| 3510 | |
| 3511 | for (Instruction *MallocCall : MallocCalls) { |
| 3512 | // This malloc cannot be replaced. |
| 3513 | if (BadMallocCalls.count(MallocCall)) |
| 3514 | continue; |
| 3515 | |
| 3516 | for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { |
| 3517 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); |
| 3518 | A.deleteAfterManifest(*FreeCall); |
| 3519 | HasChanged = ChangeStatus::CHANGED; |
| 3520 | } |
| 3521 | |
| 3522 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall |
| 3523 | << "\n"); |
| 3524 | |
| 3525 | Constant *Size; |
| 3526 | if (isCallocLikeFn(MallocCall, TLI)) { |
| 3527 | auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); |
| 3528 | auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); |
| 3529 | APInt TotalSize = SizeT->getValue() * Num->getValue(); |
| 3530 | Size = |
| 3531 | ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); |
| 3532 | } else { |
| 3533 | Size = cast<ConstantInt>(MallocCall->getOperand(0)); |
| 3534 | } |
| 3535 | |
| 3536 | unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); |
| 3537 | Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, |
| 3538 | Size, "", MallocCall->getNextNode()); |
| 3539 | |
| 3540 | if (AI->getType() != MallocCall->getType()) |
| 3541 | AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", |
| 3542 | AI->getNextNode()); |
| 3543 | |
| 3544 | MallocCall->replaceAllUsesWith(AI); |
| 3545 | |
| 3546 | if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { |
| 3547 | auto *NBB = II->getNormalDest(); |
| 3548 | BranchInst::Create(NBB, MallocCall->getParent()); |
| 3549 | A.deleteAfterManifest(*MallocCall); |
| 3550 | } else { |
| 3551 | A.deleteAfterManifest(*MallocCall); |
| 3552 | } |
| 3553 | |
| 3554 | if (isCallocLikeFn(MallocCall, TLI)) { |
| 3555 | auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", |
| 3556 | AI->getNextNode()); |
| 3557 | Value *Ops[] = { |
| 3558 | BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, |
| 3559 | ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; |
| 3560 | |
| 3561 | Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; |
| 3562 | Module *M = F->getParent(); |
| 3563 | Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); |
| 3564 | CallInst::Create(Fn, Ops, "", BI->getNextNode()); |
| 3565 | } |
| 3566 | HasChanged = ChangeStatus::CHANGED; |
| 3567 | } |
| 3568 | |
| 3569 | return HasChanged; |
| 3570 | } |
| 3571 | |
| 3572 | /// Collection of all malloc calls in a function. |
| 3573 | SmallSetVector<Instruction *, 4> MallocCalls; |
| 3574 | |
| 3575 | /// Collection of malloc calls that cannot be converted. |
| 3576 | DenseSet<const Instruction *> BadMallocCalls; |
| 3577 | |
| 3578 | /// A map for each malloc call to the set of associated free calls. |
| 3579 | DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; |
| 3580 | |
| 3581 | ChangeStatus updateImpl(Attributor &A) override; |
| 3582 | }; |
| 3583 | |
| 3584 | ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { |
| 3585 | const Function *F = getAssociatedFunction(); |
| 3586 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); |
| 3587 | |
| 3588 | auto UsesCheck = [&](Instruction &I) { |
| 3589 | SmallPtrSet<const Use *, 8> Visited; |
| 3590 | SmallVector<const Use *, 8> Worklist; |
| 3591 | |
| 3592 | for (Use &U : I.uses()) |
| 3593 | Worklist.push_back(&U); |
| 3594 | |
| 3595 | while (!Worklist.empty()) { |
| 3596 | const Use *U = Worklist.pop_back_val(); |
| 3597 | if (!Visited.insert(U).second) |
| 3598 | continue; |
| 3599 | |
| 3600 | auto *UserI = U->getUser(); |
| 3601 | |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 3602 | if (isa<LoadInst>(UserI)) |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3603 | continue; |
| Johannes Doerfert | af6e479 | 2019-10-13 04:14:15 +0000 | [diff] [blame] | 3604 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { |
| 3605 | if (SI->getValueOperand() == U->get()) { |
| 3606 | LLVM_DEBUG(dbgs() << "[H2S] escaping store to memory: " << *UserI << "\n"); |
| 3607 | return false; |
| 3608 | } |
| 3609 | // A store into the malloc'ed memory is fine. |
| 3610 | continue; |
| 3611 | } |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3612 | |
| 3613 | // NOTE: Right now, if a function that has malloc pointer as an argument |
| 3614 | // frees memory, we assume that the malloc pointer is freed. |
| 3615 | |
| 3616 | // TODO: Add nofree callsite argument attribute to indicate that pointer |
| 3617 | // argument is not freed. |
| 3618 | if (auto *CB = dyn_cast<CallBase>(UserI)) { |
| 3619 | if (!CB->isArgOperand(U)) |
| 3620 | continue; |
| 3621 | |
| 3622 | if (CB->isLifetimeStartOrEnd()) |
| 3623 | continue; |
| 3624 | |
| 3625 | // Record malloc. |
| 3626 | if (isFreeCall(UserI, TLI)) { |
| 3627 | FreesForMalloc[&I].insert( |
| 3628 | cast<Instruction>(const_cast<User *>(UserI))); |
| 3629 | continue; |
| 3630 | } |
| 3631 | |
| 3632 | // If a function does not free memory we are fine |
| 3633 | const auto &NoFreeAA = |
| 3634 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(*CB)); |
| 3635 | |
| 3636 | unsigned ArgNo = U - CB->arg_begin(); |
| 3637 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( |
| 3638 | *this, IRPosition::callsite_argument(*CB, ArgNo)); |
| 3639 | |
| 3640 | if (!NoCaptureAA.isAssumedNoCapture() || !NoFreeAA.isAssumedNoFree()) { |
| 3641 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); |
| 3642 | return false; |
| 3643 | } |
| 3644 | continue; |
| 3645 | } |
| 3646 | |
| 3647 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI)) { |
| 3648 | for (Use &U : UserI->uses()) |
| 3649 | Worklist.push_back(&U); |
| 3650 | continue; |
| 3651 | } |
| 3652 | |
| 3653 | // Unknown user. |
| 3654 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); |
| 3655 | return false; |
| 3656 | } |
| 3657 | return true; |
| 3658 | }; |
| 3659 | |
| 3660 | auto MallocCallocCheck = [&](Instruction &I) { |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 3661 | if (BadMallocCalls.count(&I)) |
| 3662 | return true; |
| 3663 | |
| 3664 | bool IsMalloc = isMallocLikeFn(&I, TLI); |
| 3665 | bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); |
| 3666 | if (!IsMalloc && !IsCalloc) { |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3667 | BadMallocCalls.insert(&I); |
| 3668 | return true; |
| 3669 | } |
| 3670 | |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 3671 | if (IsMalloc) { |
| 3672 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 3673 | if (Size->getValue().sle(MaxHeapToStackSize)) |
| 3674 | if (UsesCheck(I)) { |
| 3675 | MallocCalls.insert(&I); |
| 3676 | return true; |
| 3677 | } |
| 3678 | } else if (IsCalloc) { |
| 3679 | bool Overflow = false; |
| 3680 | if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 3681 | if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) |
| 3682 | if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) |
| 3683 | .sle(MaxHeapToStackSize)) |
| 3684 | if (!Overflow && UsesCheck(I)) { |
| 3685 | MallocCalls.insert(&I); |
| 3686 | return true; |
| 3687 | } |
| 3688 | } |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3689 | |
| Johannes Doerfert | d20f807 | 2019-10-13 03:54:08 +0000 | [diff] [blame] | 3690 | BadMallocCalls.insert(&I); |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 3691 | return true; |
| 3692 | }; |
| 3693 | |
| 3694 | size_t NumBadMallocs = BadMallocCalls.size(); |
| 3695 | |
| 3696 | A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); |
| 3697 | |
| 3698 | if (NumBadMallocs != BadMallocCalls.size()) |
| 3699 | return ChangeStatus::CHANGED; |
| 3700 | |
| 3701 | return ChangeStatus::UNCHANGED; |
| 3702 | } |
| 3703 | |
| 3704 | struct AAHeapToStackFunction final : public AAHeapToStackImpl { |
| 3705 | AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} |
| 3706 | |
| 3707 | /// See AbstractAttribute::trackStatistics() |
| 3708 | void trackStatistics() const override { |
| 3709 | STATS_DECL(MallocCalls, Function, |
| 3710 | "Number of MallocCalls converted to allocas"); |
| 3711 | BUILD_STAT_NAME(MallocCalls, Function) += MallocCalls.size(); |
| 3712 | } |
| 3713 | }; |
| 3714 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 3715 | /// -------------------- Memory Behavior Attributes ---------------------------- |
| 3716 | /// Includes read-none, read-only, and write-only. |
| 3717 | /// ---------------------------------------------------------------------------- |
| 3718 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { |
| 3719 | AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} |
| 3720 | |
| 3721 | /// See AbstractAttribute::initialize(...). |
| 3722 | void initialize(Attributor &A) override { |
| 3723 | intersectAssumedBits(BEST_STATE); |
| 3724 | getKnownStateFromValue(getIRPosition(), getState()); |
| 3725 | IRAttribute::initialize(A); |
| 3726 | } |
| 3727 | |
| 3728 | /// Return the memory behavior information encoded in the IR for \p IRP. |
| 3729 | static void getKnownStateFromValue(const IRPosition &IRP, |
| 3730 | IntegerState &State) { |
| 3731 | SmallVector<Attribute, 2> Attrs; |
| 3732 | IRP.getAttrs(AttrKinds, Attrs); |
| 3733 | for (const Attribute &Attr : Attrs) { |
| 3734 | switch (Attr.getKindAsEnum()) { |
| 3735 | case Attribute::ReadNone: |
| 3736 | State.addKnownBits(NO_ACCESSES); |
| 3737 | break; |
| 3738 | case Attribute::ReadOnly: |
| 3739 | State.addKnownBits(NO_WRITES); |
| 3740 | break; |
| 3741 | case Attribute::WriteOnly: |
| 3742 | State.addKnownBits(NO_READS); |
| 3743 | break; |
| 3744 | default: |
| 3745 | llvm_unreachable("Unexpcted attribute!"); |
| 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { |
| 3750 | if (!I->mayReadFromMemory()) |
| 3751 | State.addKnownBits(NO_READS); |
| 3752 | if (!I->mayWriteToMemory()) |
| 3753 | State.addKnownBits(NO_WRITES); |
| 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | /// See AbstractAttribute::getDeducedAttributes(...). |
| 3758 | void getDeducedAttributes(LLVMContext &Ctx, |
| 3759 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 3760 | assert(Attrs.size() == 0); |
| 3761 | if (isAssumedReadNone()) |
| 3762 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); |
| 3763 | else if (isAssumedReadOnly()) |
| 3764 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); |
| 3765 | else if (isAssumedWriteOnly()) |
| 3766 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); |
| 3767 | assert(Attrs.size() <= 1); |
| 3768 | } |
| 3769 | |
| 3770 | /// See AbstractAttribute::manifest(...). |
| 3771 | ChangeStatus manifest(Attributor &A) override { |
| 3772 | IRPosition &IRP = getIRPosition(); |
| 3773 | |
| 3774 | // Check if we would improve the existing attributes first. |
| 3775 | SmallVector<Attribute, 4> DeducedAttrs; |
| 3776 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); |
| 3777 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { |
| 3778 | return IRP.hasAttr(Attr.getKindAsEnum(), |
| 3779 | /* IgnoreSubsumingPositions */ true); |
| 3780 | })) |
| 3781 | return ChangeStatus::UNCHANGED; |
| 3782 | |
| 3783 | // Clear existing attributes. |
| 3784 | IRP.removeAttrs(AttrKinds); |
| 3785 | |
| 3786 | // Use the generic manifest method. |
| 3787 | return IRAttribute::manifest(A); |
| 3788 | } |
| 3789 | |
| 3790 | /// See AbstractState::getAsStr(). |
| 3791 | const std::string getAsStr() const override { |
| 3792 | if (isAssumedReadNone()) |
| 3793 | return "readnone"; |
| 3794 | if (isAssumedReadOnly()) |
| 3795 | return "readonly"; |
| 3796 | if (isAssumedWriteOnly()) |
| 3797 | return "writeonly"; |
| 3798 | return "may-read/write"; |
| 3799 | } |
| 3800 | |
| 3801 | /// The set of IR attributes AAMemoryBehavior deals with. |
| 3802 | static const Attribute::AttrKind AttrKinds[3]; |
| 3803 | }; |
| 3804 | |
| 3805 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { |
| 3806 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; |
| 3807 | |
| 3808 | /// Memory behavior attribute for a floating value. |
| 3809 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { |
| 3810 | AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 3811 | |
| 3812 | /// See AbstractAttribute::initialize(...). |
| 3813 | void initialize(Attributor &A) override { |
| 3814 | AAMemoryBehaviorImpl::initialize(A); |
| 3815 | // Initialize the use vector with all direct uses of the associated value. |
| 3816 | for (const Use &U : getAssociatedValue().uses()) |
| 3817 | Uses.insert(&U); |
| 3818 | } |
| 3819 | |
| 3820 | /// See AbstractAttribute::updateImpl(...). |
| 3821 | ChangeStatus updateImpl(Attributor &A) override; |
| 3822 | |
| 3823 | /// See AbstractAttribute::trackStatistics() |
| 3824 | void trackStatistics() const override { |
| 3825 | if (isAssumedReadNone()) |
| 3826 | STATS_DECLTRACK_FLOATING_ATTR(readnone) |
| 3827 | else if (isAssumedReadOnly()) |
| 3828 | STATS_DECLTRACK_FLOATING_ATTR(readonly) |
| 3829 | else if (isAssumedWriteOnly()) |
| 3830 | STATS_DECLTRACK_FLOATING_ATTR(writeonly) |
| 3831 | } |
| 3832 | |
| 3833 | private: |
| 3834 | /// Return true if users of \p UserI might access the underlying |
| 3835 | /// variable/location described by \p U and should therefore be analyzed. |
| 3836 | bool followUsersOfUseIn(Attributor &A, const Use *U, |
| 3837 | const Instruction *UserI); |
| 3838 | |
| 3839 | /// Update the state according to the effect of use \p U in \p UserI. |
| 3840 | void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); |
| 3841 | |
| 3842 | protected: |
| 3843 | /// Container for (transitive) uses of the associated argument. |
| 3844 | SetVector<const Use *> Uses; |
| 3845 | }; |
| 3846 | |
| 3847 | /// Memory behavior attribute for function argument. |
| 3848 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { |
| 3849 | AAMemoryBehaviorArgument(const IRPosition &IRP) |
| 3850 | : AAMemoryBehaviorFloating(IRP) {} |
| 3851 | |
| 3852 | /// See AbstractAttribute::initialize(...). |
| 3853 | void initialize(Attributor &A) override { |
| 3854 | AAMemoryBehaviorFloating::initialize(A); |
| 3855 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 3856 | // Initialize the use vector with all direct uses of the associated value. |
| 3857 | Argument *Arg = getAssociatedArgument(); |
| 3858 | if (!Arg || !Arg->getParent()->hasExactDefinition()) |
| 3859 | indicatePessimisticFixpoint(); |
| 3860 | } |
| 3861 | |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 3862 | ChangeStatus manifest(Attributor &A) override { |
| 3863 | // TODO: From readattrs.ll: "inalloca parameters are always |
| 3864 | // considered written" |
| 3865 | if (hasAttr({Attribute::InAlloca})) { |
| 3866 | removeKnownBits(NO_WRITES); |
| 3867 | removeAssumedBits(NO_WRITES); |
| 3868 | } |
| 3869 | return AAMemoryBehaviorFloating::manifest(A); |
| 3870 | } |
| 3871 | |
| 3872 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 3873 | /// See AbstractAttribute::trackStatistics() |
| 3874 | void trackStatistics() const override { |
| 3875 | if (isAssumedReadNone()) |
| 3876 | STATS_DECLTRACK_ARG_ATTR(readnone) |
| 3877 | else if (isAssumedReadOnly()) |
| 3878 | STATS_DECLTRACK_ARG_ATTR(readonly) |
| 3879 | else if (isAssumedWriteOnly()) |
| 3880 | STATS_DECLTRACK_ARG_ATTR(writeonly) |
| 3881 | } |
| 3882 | }; |
| 3883 | |
| 3884 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { |
| 3885 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) |
| 3886 | : AAMemoryBehaviorArgument(IRP) {} |
| 3887 | |
| 3888 | /// See AbstractAttribute::updateImpl(...). |
| 3889 | ChangeStatus updateImpl(Attributor &A) override { |
| 3890 | // TODO: Once we have call site specific value information we can provide |
| 3891 | // call site specific liveness liveness information and then it makes |
| 3892 | // sense to specialize attributes for call sites arguments instead of |
| 3893 | // redirecting requests to the callee argument. |
| 3894 | Argument *Arg = getAssociatedArgument(); |
| 3895 | const IRPosition &ArgPos = IRPosition::argument(*Arg); |
| 3896 | auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); |
| 3897 | return clampStateAndIndicateChange( |
| 3898 | getState(), |
| 3899 | static_cast<const AANoCapture::StateType &>(ArgAA.getState())); |
| 3900 | } |
| 3901 | |
| 3902 | /// See AbstractAttribute::trackStatistics() |
| 3903 | void trackStatistics() const override { |
| 3904 | if (isAssumedReadNone()) |
| 3905 | STATS_DECLTRACK_CSARG_ATTR(readnone) |
| 3906 | else if (isAssumedReadOnly()) |
| 3907 | STATS_DECLTRACK_CSARG_ATTR(readonly) |
| 3908 | else if (isAssumedWriteOnly()) |
| 3909 | STATS_DECLTRACK_CSARG_ATTR(writeonly) |
| 3910 | } |
| 3911 | }; |
| 3912 | |
| 3913 | /// Memory behavior attribute for a call site return position. |
| 3914 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { |
| 3915 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) |
| 3916 | : AAMemoryBehaviorFloating(IRP) {} |
| 3917 | |
| 3918 | /// See AbstractAttribute::manifest(...). |
| 3919 | ChangeStatus manifest(Attributor &A) override { |
| 3920 | // We do not annotate returned values. |
| 3921 | return ChangeStatus::UNCHANGED; |
| 3922 | } |
| 3923 | |
| 3924 | /// See AbstractAttribute::trackStatistics() |
| 3925 | void trackStatistics() const override {} |
| 3926 | }; |
| 3927 | |
| 3928 | /// An AA to represent the memory behavior function attributes. |
| 3929 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { |
| 3930 | AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 3931 | |
| 3932 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| 3933 | virtual ChangeStatus updateImpl(Attributor &A) override; |
| 3934 | |
| 3935 | /// See AbstractAttribute::manifest(...). |
| 3936 | ChangeStatus manifest(Attributor &A) override { |
| 3937 | Function &F = cast<Function>(getAnchorValue()); |
| 3938 | if (isAssumedReadNone()) { |
| 3939 | F.removeFnAttr(Attribute::ArgMemOnly); |
| 3940 | F.removeFnAttr(Attribute::InaccessibleMemOnly); |
| 3941 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
| 3942 | } |
| 3943 | return AAMemoryBehaviorImpl::manifest(A); |
| 3944 | } |
| 3945 | |
| 3946 | /// See AbstractAttribute::trackStatistics() |
| 3947 | void trackStatistics() const override { |
| 3948 | if (isAssumedReadNone()) |
| 3949 | STATS_DECLTRACK_FN_ATTR(readnone) |
| 3950 | else if (isAssumedReadOnly()) |
| 3951 | STATS_DECLTRACK_FN_ATTR(readonly) |
| 3952 | else if (isAssumedWriteOnly()) |
| 3953 | STATS_DECLTRACK_FN_ATTR(writeonly) |
| 3954 | } |
| 3955 | }; |
| 3956 | |
| 3957 | /// AAMemoryBehavior attribute for call sites. |
| 3958 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { |
| 3959 | AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} |
| 3960 | |
| 3961 | /// See AbstractAttribute::initialize(...). |
| 3962 | void initialize(Attributor &A) override { |
| 3963 | AAMemoryBehaviorImpl::initialize(A); |
| 3964 | Function *F = getAssociatedFunction(); |
| 3965 | if (!F || !F->hasExactDefinition()) |
| 3966 | indicatePessimisticFixpoint(); |
| 3967 | } |
| 3968 | |
| 3969 | /// See AbstractAttribute::updateImpl(...). |
| 3970 | ChangeStatus updateImpl(Attributor &A) override { |
| 3971 | // TODO: Once we have call site specific value information we can provide |
| 3972 | // call site specific liveness liveness information and then it makes |
| 3973 | // sense to specialize attributes for call sites arguments instead of |
| 3974 | // redirecting requests to the callee argument. |
| 3975 | Function *F = getAssociatedFunction(); |
| 3976 | const IRPosition &FnPos = IRPosition::function(*F); |
| 3977 | auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); |
| 3978 | return clampStateAndIndicateChange( |
| 3979 | getState(), static_cast<const AAAlign::StateType &>(FnAA.getState())); |
| 3980 | } |
| 3981 | |
| 3982 | /// See AbstractAttribute::trackStatistics() |
| 3983 | void trackStatistics() const override { |
| 3984 | if (isAssumedReadNone()) |
| 3985 | STATS_DECLTRACK_CS_ATTR(readnone) |
| 3986 | else if (isAssumedReadOnly()) |
| 3987 | STATS_DECLTRACK_CS_ATTR(readonly) |
| 3988 | else if (isAssumedWriteOnly()) |
| 3989 | STATS_DECLTRACK_CS_ATTR(writeonly) |
| 3990 | } |
| 3991 | }; |
| Benjamin Kramer | c5d1d56 | 2019-10-12 11:01:52 +0000 | [diff] [blame] | 3992 | } // namespace |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 3993 | |
| 3994 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { |
| 3995 | |
| 3996 | // The current assumed state used to determine a change. |
| 3997 | auto AssumedState = getAssumed(); |
| 3998 | |
| 3999 | auto CheckRWInst = [&](Instruction &I) { |
| 4000 | // If the instruction has an own memory behavior state, use it to restrict |
| 4001 | // the local state. No further analysis is required as the other memory |
| 4002 | // state is as optimistic as it gets. |
| 4003 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 4004 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( |
| 4005 | *this, IRPosition::callsite_function(ICS)); |
| 4006 | intersectAssumedBits(MemBehaviorAA.getAssumed()); |
| 4007 | return !isAtFixpoint(); |
| 4008 | } |
| 4009 | |
| 4010 | // Remove access kind modifiers if necessary. |
| 4011 | if (I.mayReadFromMemory()) |
| 4012 | removeAssumedBits(NO_READS); |
| 4013 | if (I.mayWriteToMemory()) |
| 4014 | removeAssumedBits(NO_WRITES); |
| 4015 | return !isAtFixpoint(); |
| 4016 | }; |
| 4017 | |
| 4018 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) |
| 4019 | return indicatePessimisticFixpoint(); |
| 4020 | |
| 4021 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED |
| 4022 | : ChangeStatus::UNCHANGED; |
| 4023 | } |
| 4024 | |
| 4025 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { |
| 4026 | |
| 4027 | const IRPosition &IRP = getIRPosition(); |
| 4028 | const IRPosition &FnPos = IRPosition::function_scope(IRP); |
| 4029 | AAMemoryBehavior::StateType &S = getState(); |
| 4030 | |
| 4031 | // First, check the function scope. We take the known information and we avoid |
| 4032 | // work if the assumed information implies the current assumed information for |
| 4033 | // this attribute. |
| 4034 | const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); |
| 4035 | S.addKnownBits(FnMemAA.getKnown()); |
| 4036 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) |
| 4037 | return ChangeStatus::UNCHANGED; |
| 4038 | |
| 4039 | // Make sure the value is not captured (except through "return"), if |
| 4040 | // it is, any information derived would be irrelevant anyway as we cannot |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4041 | // check the potential aliases introduced by the capture. However, no need |
| 4042 | // to fall back to anythign less optimistic than the function state. |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4043 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); |
| Johannes Doerfert | 8ee410c | 2019-10-13 20:47:16 +0000 | [diff] [blame] | 4044 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { |
| 4045 | S.intersectAssumedBits(FnMemAA.getAssumed()); |
| 4046 | return ChangeStatus::CHANGED; |
| 4047 | } |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4048 | |
| 4049 | // The current assumed state used to determine a change. |
| 4050 | auto AssumedState = S.getAssumed(); |
| 4051 | |
| 4052 | // Liveness information to exclude dead users. |
| 4053 | // TODO: Take the FnPos once we have call site specific liveness information. |
| 4054 | const auto &LivenessAA = A.getAAFor<AAIsDead>( |
| 4055 | *this, IRPosition::function(*IRP.getAssociatedFunction())); |
| 4056 | |
| 4057 | // Visit and expand uses until all are analyzed or a fixpoint is reached. |
| 4058 | for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { |
| 4059 | const Use *U = Uses[i]; |
| 4060 | Instruction *UserI = cast<Instruction>(U->getUser()); |
| 4061 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI |
| 4062 | << " [Dead: " << (LivenessAA.isAssumedDead(UserI)) |
| 4063 | << "]\n"); |
| 4064 | if (LivenessAA.isAssumedDead(UserI)) |
| 4065 | continue; |
| 4066 | |
| 4067 | // Check if the users of UserI should also be visited. |
| 4068 | if (followUsersOfUseIn(A, U, UserI)) |
| 4069 | for (const Use &UserIUse : UserI->uses()) |
| 4070 | Uses.insert(&UserIUse); |
| 4071 | |
| 4072 | // If UserI might touch memory we analyze the use in detail. |
| 4073 | if (UserI->mayReadOrWriteMemory()) |
| 4074 | analyzeUseIn(A, U, UserI); |
| 4075 | } |
| 4076 | |
| 4077 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED |
| 4078 | : ChangeStatus::UNCHANGED; |
| 4079 | } |
| 4080 | |
| 4081 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, |
| 4082 | const Instruction *UserI) { |
| 4083 | // The loaded value is unrelated to the pointer argument, no need to |
| 4084 | // follow the users of the load. |
| 4085 | if (isa<LoadInst>(UserI)) |
| 4086 | return false; |
| 4087 | |
| 4088 | // By default we follow all uses assuming UserI might leak information on U, |
| 4089 | // we have special handling for call sites operands though. |
| 4090 | ImmutableCallSite ICS(UserI); |
| 4091 | if (!ICS || !ICS.isArgOperand(U)) |
| 4092 | return true; |
| 4093 | |
| 4094 | // If the use is a call argument known not to be captured, the users of |
| 4095 | // the call do not need to be visited because they have to be unrelated to |
| 4096 | // the input. Note that this check is not trivial even though we disallow |
| 4097 | // general capturing of the underlying argument. The reason is that the |
| 4098 | // call might the argument "through return", which we allow and for which we |
| 4099 | // need to check call users. |
| 4100 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 4101 | const auto &ArgNoCaptureAA = |
| 4102 | A.getAAFor<AANoCapture>(*this, IRPosition::callsite_argument(ICS, ArgNo)); |
| 4103 | return !ArgNoCaptureAA.isAssumedNoCapture(); |
| 4104 | } |
| 4105 | |
| 4106 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, |
| 4107 | const Instruction *UserI) { |
| 4108 | assert(UserI->mayReadOrWriteMemory()); |
| 4109 | |
| 4110 | switch (UserI->getOpcode()) { |
| 4111 | default: |
| 4112 | // TODO: Handle all atomics and other side-effect operations we know of. |
| 4113 | break; |
| 4114 | case Instruction::Load: |
| 4115 | // Loads cause the NO_READS property to disappear. |
| 4116 | removeAssumedBits(NO_READS); |
| 4117 | return; |
| 4118 | |
| 4119 | case Instruction::Store: |
| 4120 | // Stores cause the NO_WRITES property to disappear if the use is the |
| 4121 | // pointer operand. Note that we do assume that capturing was taken care of |
| 4122 | // somewhere else. |
| 4123 | if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) |
| 4124 | removeAssumedBits(NO_WRITES); |
| 4125 | return; |
| 4126 | |
| 4127 | case Instruction::Call: |
| 4128 | case Instruction::CallBr: |
| 4129 | case Instruction::Invoke: { |
| 4130 | // For call sites we look at the argument memory behavior attribute (this |
| 4131 | // could be recursive!) in order to restrict our own state. |
| 4132 | ImmutableCallSite ICS(UserI); |
| 4133 | |
| 4134 | // Give up on operand bundles. |
| 4135 | if (ICS.isBundleOperand(U)) { |
| 4136 | indicatePessimisticFixpoint(); |
| 4137 | return; |
| 4138 | } |
| 4139 | |
| 4140 | // Calling a function does read the function pointer, maybe write it if the |
| 4141 | // function is self-modifying. |
| 4142 | if (ICS.isCallee(U)) { |
| 4143 | removeAssumedBits(NO_READS); |
| 4144 | break; |
| 4145 | } |
| 4146 | |
| 4147 | // Adjust the possible access behavior based on the information on the |
| 4148 | // argument. |
| 4149 | unsigned ArgNo = ICS.getArgumentNo(U); |
| 4150 | const IRPosition &ArgPos = IRPosition::callsite_argument(ICS, ArgNo); |
| 4151 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); |
| 4152 | // "assumed" has at most the same bits as the MemBehaviorAA assumed |
| 4153 | // and at least "known". |
| 4154 | intersectAssumedBits(MemBehaviorAA.getAssumed()); |
| 4155 | return; |
| 4156 | } |
| 4157 | }; |
| 4158 | |
| 4159 | // Generally, look at the "may-properties" and adjust the assumed state if we |
| 4160 | // did not trigger special handling before. |
| 4161 | if (UserI->mayReadFromMemory()) |
| 4162 | removeAssumedBits(NO_READS); |
| 4163 | if (UserI->mayWriteToMemory()) |
| 4164 | removeAssumedBits(NO_WRITES); |
| 4165 | } |
| 4166 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4167 | /// ---------------------------------------------------------------------------- |
| 4168 | /// Attributor |
| 4169 | /// ---------------------------------------------------------------------------- |
| 4170 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4171 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, |
| 4172 | const AAIsDead *LivenessAA) { |
| 4173 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); |
| 4174 | if (!CtxI) |
| 4175 | return false; |
| 4176 | |
| 4177 | if (!LivenessAA) |
| 4178 | LivenessAA = |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4179 | &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), |
| 4180 | /* TrackDependence */ false); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 4181 | |
| 4182 | // Don't check liveness for AAIsDead. |
| 4183 | if (&AA == LivenessAA) |
| 4184 | return false; |
| 4185 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4186 | if (!LivenessAA->isAssumedDead(CtxI)) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4187 | return false; |
| 4188 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4189 | // We actually used liveness information so we have to record a dependence. |
| 4190 | recordDependence(*LivenessAA, AA); |
| 4191 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4192 | return true; |
| 4193 | } |
| 4194 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4195 | bool Attributor::checkForAllCallSites( |
| 4196 | const function_ref<bool(AbstractCallSite)> &Pred, |
| 4197 | const AbstractAttribute &QueryingAA, bool RequireAllCallSites) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4198 | // We can try to determine information from |
| 4199 | // the call sites. However, this is only possible all call sites are known, |
| 4200 | // hence the function has internal linkage. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4201 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 4202 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 4203 | if (!AssociatedFunction) { |
| 4204 | LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP |
| 4205 | << "\n"); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4206 | return false; |
| Johannes Doerfert | 748538e | 2019-10-07 23:30:04 +0000 | [diff] [blame] | 4207 | } |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4208 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4209 | return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, |
| 4210 | &QueryingAA); |
| 4211 | } |
| 4212 | |
| 4213 | bool Attributor::checkForAllCallSites( |
| 4214 | const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, |
| 4215 | bool RequireAllCallSites, const AbstractAttribute *QueryingAA) { |
| 4216 | if (RequireAllCallSites && !Fn.hasLocalLinkage()) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4217 | LLVM_DEBUG( |
| 4218 | dbgs() |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4219 | << "[Attributor] Function " << Fn.getName() |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4220 | << " has no internal linkage, hence not all call sites are known\n"); |
| 4221 | return false; |
| 4222 | } |
| 4223 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4224 | for (const Use &U : Fn.uses()) { |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4225 | AbstractCallSite ACS(&U); |
| 4226 | if (!ACS) { |
| 4227 | LLVM_DEBUG(dbgs() << "[Attributor] Function " |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4228 | << Fn.getName() |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4229 | << " has non call site use " << *U.get() << " in " |
| 4230 | << *U.getUser() << "\n"); |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 4231 | return false; |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4232 | } |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 4233 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4234 | Instruction *I = ACS.getInstruction(); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4235 | Function *Caller = I->getFunction(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 4236 | |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4237 | const auto *LivenessAA = |
| 4238 | lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA, |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4239 | /* TrackDependence */ false); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 4240 | |
| 4241 | // Skip dead calls. |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4242 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4243 | // We actually used liveness information so we have to record a |
| 4244 | // dependence. |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4245 | if (QueryingAA) |
| 4246 | recordDependence(*LivenessAA, *QueryingAA); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 4247 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4248 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4249 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4250 | const Use *EffectiveUse = |
| 4251 | ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; |
| 4252 | if (!ACS.isCallee(EffectiveUse)) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4253 | if (!RequireAllCallSites) |
| 4254 | continue; |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4255 | LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4256 | << " is an invalid use of " |
| Johannes Doerfert | 3753aa7 | 2019-10-13 04:16:02 +0000 | [diff] [blame] | 4257 | << Fn.getName() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4258 | return false; |
| 4259 | } |
| 4260 | |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4261 | if (Pred(ACS)) |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4262 | continue; |
| 4263 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 4264 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4265 | << *ACS.getInstruction() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4266 | return false; |
| 4267 | } |
| 4268 | |
| 4269 | return true; |
| 4270 | } |
| 4271 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4272 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 4273 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4274 | &Pred, |
| 4275 | const AbstractAttribute &QueryingAA) { |
| 4276 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4277 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 4278 | // Since we need to provide return instructions we have to have an exact |
| 4279 | // definition. |
| 4280 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4281 | if (!AssociatedFunction) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4282 | return false; |
| 4283 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4284 | // If this is a call site query we use the call site specific return values |
| 4285 | // and liveness information. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4286 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 4287 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4288 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4289 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4290 | return false; |
| 4291 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4292 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | bool Attributor::checkForAllReturnedValues( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4296 | const function_ref<bool(Value &)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4297 | const AbstractAttribute &QueryingAA) { |
| 4298 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4299 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 4300 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4301 | if (!AssociatedFunction) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4302 | return false; |
| 4303 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4304 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 4305 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4306 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4307 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4308 | return false; |
| 4309 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4310 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 4311 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 4312 | return Pred(RV); |
| 4313 | }); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4316 | static bool |
| 4317 | checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap, |
| 4318 | const function_ref<bool(Instruction &)> &Pred, |
| 4319 | const AAIsDead *LivenessAA, bool &AnyDead, |
| 4320 | const ArrayRef<unsigned> &Opcodes) { |
| 4321 | for (unsigned Opcode : Opcodes) { |
| 4322 | for (Instruction *I : OpcodeInstMap[Opcode]) { |
| 4323 | // Skip dead instructions. |
| 4324 | if (LivenessAA && LivenessAA->isAssumedDead(I)) { |
| 4325 | AnyDead = true; |
| 4326 | continue; |
| 4327 | } |
| 4328 | |
| 4329 | if (!Pred(*I)) |
| 4330 | return false; |
| 4331 | } |
| 4332 | } |
| 4333 | return true; |
| 4334 | } |
| 4335 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4336 | bool Attributor::checkForAllInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4337 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 4338 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4339 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4340 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 4341 | // Since we need to provide instructions we have to have an exact definition. |
| 4342 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4343 | if (!AssociatedFunction) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4344 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4345 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4346 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 4347 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4348 | const auto &LivenessAA = |
| 4349 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| 4350 | bool AnyDead = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4351 | |
| 4352 | auto &OpcodeInstMap = |
| 4353 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4354 | if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, |
| 4355 | Opcodes)) |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4356 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4357 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4358 | // If we actually used liveness information so we have to record a dependence. |
| 4359 | if (AnyDead) |
| 4360 | recordDependence(LivenessAA, QueryingAA); |
| 4361 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 4362 | return true; |
| 4363 | } |
| 4364 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4365 | bool Attributor::checkForAllReadWriteInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4366 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 4367 | AbstractAttribute &QueryingAA) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4368 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4369 | const Function *AssociatedFunction = |
| 4370 | QueryingAA.getIRPosition().getAssociatedFunction(); |
| 4371 | if (!AssociatedFunction) |
| 4372 | return false; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4373 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 4374 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 4375 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| 4376 | const auto &LivenessAA = |
| 4377 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4378 | bool AnyDead = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4379 | |
| 4380 | for (Instruction *I : |
| 4381 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4382 | // Skip dead instructions. |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4383 | if (LivenessAA.isAssumedDead(I)) { |
| 4384 | AnyDead = true; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4385 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4386 | } |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4387 | |
| 4388 | if (!Pred(*I)) |
| 4389 | return false; |
| 4390 | } |
| 4391 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 4392 | // If we actually used liveness information so we have to record a dependence. |
| 4393 | if (AnyDead) |
| 4394 | recordDependence(LivenessAA, QueryingAA); |
| 4395 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 4396 | return true; |
| 4397 | } |
| 4398 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4399 | ChangeStatus Attributor::run(Module &M) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4400 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " |
| 4401 | << AllAbstractAttributes.size() |
| 4402 | << " abstract attributes.\n"); |
| 4403 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 4404 | // Now that all abstract attributes are collected and initialized we start |
| 4405 | // the abstract analysis. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4406 | |
| 4407 | unsigned IterationCounter = 1; |
| 4408 | |
| 4409 | SmallVector<AbstractAttribute *, 64> ChangedAAs; |
| 4410 | SetVector<AbstractAttribute *> Worklist; |
| 4411 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); |
| 4412 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4413 | bool RecomputeDependences = false; |
| 4414 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4415 | do { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4416 | // Remember the size to determine new attributes. |
| 4417 | size_t NumAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4418 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter |
| 4419 | << ", Worklist size: " << Worklist.size() << "\n"); |
| 4420 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4421 | // If dependences (=QueryMap) are recomputed we have to look at all abstract |
| 4422 | // attributes again, regardless of what changed in the last iteration. |
| 4423 | if (RecomputeDependences) { |
| 4424 | LLVM_DEBUG( |
| 4425 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); |
| 4426 | QueryMap.clear(); |
| 4427 | ChangedAAs.clear(); |
| 4428 | Worklist.insert(AllAbstractAttributes.begin(), |
| 4429 | AllAbstractAttributes.end()); |
| 4430 | } |
| 4431 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4432 | // Add all abstract attributes that are potentially dependent on one that |
| 4433 | // changed to the work list. |
| 4434 | for (AbstractAttribute *ChangedAA : ChangedAAs) { |
| 4435 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| 4436 | Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end()); |
| 4437 | } |
| 4438 | |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 4439 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter |
| 4440 | << ", Worklist+Dependent size: " << Worklist.size() |
| 4441 | << "\n"); |
| 4442 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4443 | // Reset the changed set. |
| 4444 | ChangedAAs.clear(); |
| 4445 | |
| 4446 | // Update all abstract attribute in the work list and record the ones that |
| 4447 | // changed. |
| 4448 | for (AbstractAttribute *AA : Worklist) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4449 | if (!isAssumedDead(*AA, nullptr)) |
| 4450 | if (AA->update(*this) == ChangeStatus::CHANGED) |
| 4451 | ChangedAAs.push_back(AA); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4452 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4453 | // Check if we recompute the dependences in the next iteration. |
| 4454 | RecomputeDependences = (DepRecomputeInterval > 0 && |
| 4455 | IterationCounter % DepRecomputeInterval == 0); |
| 4456 | |
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 4457 | // Add attributes to the changed set if they have been created in the last |
| 4458 | // iteration. |
| 4459 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, |
| 4460 | AllAbstractAttributes.end()); |
| 4461 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4462 | // Reset the work list and repopulate with the changed abstract attributes. |
| 4463 | // Note that dependent ones are added above. |
| 4464 | Worklist.clear(); |
| 4465 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); |
| 4466 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 4467 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || |
| 4468 | VerifyMaxFixpointIterations)); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4469 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4470 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " |
| 4471 | << IterationCounter << "/" << MaxFixpointIterations |
| 4472 | << " iterations\n"); |
| 4473 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 4474 | size_t NumFinalAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 4475 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4476 | // Reset abstract arguments not settled in a sound fixpoint by now. This |
| 4477 | // happens when we stopped the fixpoint iteration early. Note that only the |
| 4478 | // ones marked as "changed" *and* the ones transitively depending on them |
| 4479 | // need to be reverted to a pessimistic state. Others might not be in a |
| 4480 | // fixpoint state but we can use the optimistic results for them anyway. |
| 4481 | SmallPtrSet<AbstractAttribute *, 32> Visited; |
| 4482 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { |
| 4483 | AbstractAttribute *ChangedAA = ChangedAAs[u]; |
| 4484 | if (!Visited.insert(ChangedAA).second) |
| 4485 | continue; |
| 4486 | |
| 4487 | AbstractState &State = ChangedAA->getState(); |
| 4488 | if (!State.isAtFixpoint()) { |
| 4489 | State.indicatePessimisticFixpoint(); |
| 4490 | |
| 4491 | NumAttributesTimedOut++; |
| 4492 | } |
| 4493 | |
| 4494 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| 4495 | ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end()); |
| 4496 | } |
| 4497 | |
| 4498 | LLVM_DEBUG({ |
| 4499 | if (!Visited.empty()) |
| 4500 | dbgs() << "\n[Attributor] Finalized " << Visited.size() |
| 4501 | << " abstract attributes.\n"; |
| 4502 | }); |
| 4503 | |
| 4504 | unsigned NumManifested = 0; |
| 4505 | unsigned NumAtFixpoint = 0; |
| 4506 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; |
| 4507 | for (AbstractAttribute *AA : AllAbstractAttributes) { |
| 4508 | AbstractState &State = AA->getState(); |
| 4509 | |
| 4510 | // If there is not already a fixpoint reached, we can now take the |
| 4511 | // optimistic state. This is correct because we enforced a pessimistic one |
| 4512 | // on abstract attributes that were transitively dependent on a changed one |
| 4513 | // already above. |
| 4514 | if (!State.isAtFixpoint()) |
| 4515 | State.indicateOptimisticFixpoint(); |
| 4516 | |
| 4517 | // If the state is invalid, we do not try to manifest it. |
| 4518 | if (!State.isValidState()) |
| 4519 | continue; |
| 4520 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 4521 | // Skip dead code. |
| 4522 | if (isAssumedDead(*AA, nullptr)) |
| 4523 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4524 | // Manifest the state and record if we changed the IR. |
| 4525 | ChangeStatus LocalChange = AA->manifest(*this); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 4526 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) |
| 4527 | AA->trackStatistics(); |
| 4528 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4529 | ManifestChange = ManifestChange | LocalChange; |
| 4530 | |
| 4531 | NumAtFixpoint++; |
| 4532 | NumManifested += (LocalChange == ChangeStatus::CHANGED); |
| 4533 | } |
| 4534 | |
| 4535 | (void)NumManifested; |
| 4536 | (void)NumAtFixpoint; |
| 4537 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested |
| 4538 | << " arguments while " << NumAtFixpoint |
| 4539 | << " were in a valid fixpoint state\n"); |
| 4540 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4541 | NumAttributesManifested += NumManifested; |
| 4542 | NumAttributesValidFixpoint += NumAtFixpoint; |
| 4543 | |
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 4544 | (void)NumFinalAAs; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4545 | assert( |
| 4546 | NumFinalAAs == AllAbstractAttributes.size() && |
| 4547 | "Expected the final number of abstract attributes to remain unchanged!"); |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 4548 | |
| 4549 | // 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] | 4550 | { |
| 4551 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " |
| 4552 | << ToBeDeletedFunctions.size() << " functions and " |
| 4553 | << ToBeDeletedBlocks.size() << " blocks and " |
| 4554 | << ToBeDeletedInsts.size() << " instructions\n"); |
| 4555 | for (Instruction *I : ToBeDeletedInsts) { |
| 4556 | if (!I->use_empty()) |
| 4557 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 4558 | I->eraseFromParent(); |
| 4559 | } |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 4560 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4561 | if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { |
| 4562 | SmallVector<BasicBlock *, 8> ToBeDeletedBBs; |
| 4563 | ToBeDeletedBBs.reserve(NumDeadBlocks); |
| 4564 | ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end()); |
| 4565 | DeleteDeadBlocks(ToBeDeletedBBs); |
| 4566 | STATS_DECLTRACK(AAIsDead, BasicBlock, |
| 4567 | "Number of dead basic blocks deleted."); |
| 4568 | } |
| Johannes Doerfert | b19cd27 | 2019-09-03 20:42:16 +0000 | [diff] [blame] | 4569 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4570 | STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); |
| 4571 | for (Function *Fn : ToBeDeletedFunctions) { |
| 4572 | Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); |
| 4573 | Fn->eraseFromParent(); |
| 4574 | STATS_TRACK(AAIsDead, Function); |
| 4575 | } |
| 4576 | |
| 4577 | // Identify dead internal functions and delete them. This happens outside |
| 4578 | // the other fixpoint analysis as we might treat potentially dead functions |
| 4579 | // as live to lower the number of iterations. If they happen to be dead, the |
| 4580 | // below fixpoint loop will identify and eliminate them. |
| 4581 | SmallVector<Function *, 8> InternalFns; |
| 4582 | for (Function &F : M) |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 4583 | if (F.hasLocalLinkage()) |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4584 | InternalFns.push_back(&F); |
| 4585 | |
| 4586 | bool FoundDeadFn = true; |
| 4587 | while (FoundDeadFn) { |
| 4588 | FoundDeadFn = false; |
| 4589 | for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { |
| 4590 | Function *F = InternalFns[u]; |
| 4591 | if (!F) |
| 4592 | continue; |
| 4593 | |
| 4594 | const auto *LivenessAA = |
| 4595 | lookupAAFor<AAIsDead>(IRPosition::function(*F)); |
| 4596 | if (LivenessAA && |
| Johannes Doerfert | 661db04 | 2019-10-07 23:14:58 +0000 | [diff] [blame] | 4597 | !checkForAllCallSites([](AbstractCallSite ACS) { return false; }, |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4598 | *LivenessAA, true)) |
| 4599 | continue; |
| 4600 | |
| 4601 | STATS_TRACK(AAIsDead, Function); |
| 4602 | F->replaceAllUsesWith(UndefValue::get(F->getType())); |
| 4603 | F->eraseFromParent(); |
| 4604 | InternalFns[u] = nullptr; |
| 4605 | FoundDeadFn = true; |
| 4606 | } |
| 4607 | } |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 4610 | if (VerifyMaxFixpointIterations && |
| 4611 | IterationCounter != MaxFixpointIterations) { |
| 4612 | errs() << "\n[Attributor] Fixpoint iteration done after: " |
| 4613 | << IterationCounter << "/" << MaxFixpointIterations |
| 4614 | << " iterations\n"; |
| 4615 | llvm_unreachable("The fixpoint was not reached with exactly the number of " |
| 4616 | "specified iterations!"); |
| 4617 | } |
| 4618 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4619 | return ManifestChange; |
| 4620 | } |
| 4621 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4622 | void Attributor::initializeInformationCache(Function &F) { |
| 4623 | |
| 4624 | // Walk all instructions to find interesting instructions that might be |
| 4625 | // queried by abstract attributes during their initialization or update. |
| 4626 | // This has to happen before we create attributes. |
| 4627 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; |
| 4628 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; |
| 4629 | |
| 4630 | for (Instruction &I : instructions(&F)) { |
| 4631 | bool IsInterestingOpcode = false; |
| 4632 | |
| 4633 | // To allow easy access to all instructions in a function with a given |
| 4634 | // opcode we store them in the InfoCache. As not all opcodes are interesting |
| 4635 | // to concrete attributes we only cache the ones that are as identified in |
| 4636 | // the following switch. |
| 4637 | // Note: There are no concrete attributes now so this is initially empty. |
| 4638 | switch (I.getOpcode()) { |
| 4639 | default: |
| 4640 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && |
| 4641 | "New call site/base instruction type needs to be known int the " |
| 4642 | "Attributor."); |
| 4643 | break; |
| 4644 | case Instruction::Load: |
| 4645 | // The alignment of a pointer is interesting for loads. |
| 4646 | case Instruction::Store: |
| 4647 | // The alignment of a pointer is interesting for stores. |
| 4648 | case Instruction::Call: |
| 4649 | case Instruction::CallBr: |
| 4650 | case Instruction::Invoke: |
| 4651 | case Instruction::CleanupRet: |
| 4652 | case Instruction::CatchSwitch: |
| 4653 | case Instruction::Resume: |
| 4654 | case Instruction::Ret: |
| 4655 | IsInterestingOpcode = true; |
| 4656 | } |
| 4657 | if (IsInterestingOpcode) |
| 4658 | InstOpcodeMap[I.getOpcode()].push_back(&I); |
| 4659 | if (I.mayReadOrWriteMemory()) |
| 4660 | ReadOrWriteInsts.push_back(&I); |
| 4661 | } |
| 4662 | } |
| 4663 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 4664 | void Attributor::identifyDefaultAbstractAttributes(Function &F) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4665 | if (!VisitedFunctions.insert(&F).second) |
| 4666 | return; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4667 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4668 | IRPosition FPos = IRPosition::function(F); |
| 4669 | |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 4670 | // Check for dead BasicBlocks in every function. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 4671 | // We need dead instruction detection because we do not want to deal with |
| 4672 | // broken IR in which SSA rules do not apply. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4673 | getOrCreateAAFor<AAIsDead>(FPos); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 4674 | |
| 4675 | // Every function might be "will-return". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4676 | getOrCreateAAFor<AAWillReturn>(FPos); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 4677 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 4678 | // Every function can be nounwind. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4679 | getOrCreateAAFor<AANoUnwind>(FPos); |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 4680 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 4681 | // Every function might be marked "nosync" |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4682 | getOrCreateAAFor<AANoSync>(FPos); |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 4683 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 4684 | // Every function might be "no-free". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4685 | getOrCreateAAFor<AANoFree>(FPos); |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 4686 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4687 | // Every function might be "no-return". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4688 | getOrCreateAAFor<AANoReturn>(FPos); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 4689 | |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 4690 | // Every function might be "no-recurse". |
| 4691 | getOrCreateAAFor<AANoRecurse>(FPos); |
| 4692 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4693 | // Every function might be "readnone/readonly/writeonly/...". |
| 4694 | getOrCreateAAFor<AAMemoryBehavior>(FPos); |
| 4695 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4696 | // Every function might be applicable for Heap-To-Stack conversion. |
| 4697 | if (EnableHeapToStack) |
| 4698 | getOrCreateAAFor<AAHeapToStack>(FPos); |
| 4699 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 4700 | // Return attributes are only appropriate if the return type is non void. |
| 4701 | Type *ReturnType = F.getReturnType(); |
| 4702 | if (!ReturnType->isVoidTy()) { |
| 4703 | // Argument attribute "returned" --- Create only one per function even |
| 4704 | // though it is an argument attribute. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4705 | getOrCreateAAFor<AAReturnedValues>(FPos); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4706 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4707 | IRPosition RetPos = IRPosition::returned(F); |
| 4708 | |
| 4709 | // Every function might be simplified. |
| 4710 | getOrCreateAAFor<AAValueSimplify>(RetPos); |
| 4711 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 4712 | if (ReturnType->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4713 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4714 | // Every function with pointer return type might be marked align. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4715 | getOrCreateAAFor<AAAlign>(RetPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4716 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 4717 | // Every function with pointer return type might be marked nonnull. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4718 | getOrCreateAAFor<AANonNull>(RetPos); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 4719 | |
| 4720 | // Every function with pointer return type might be marked noalias. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4721 | getOrCreateAAFor<AANoAlias>(RetPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4722 | |
| 4723 | // Every function with pointer return type might be marked |
| 4724 | // dereferenceable. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4725 | getOrCreateAAFor<AADereferenceable>(RetPos); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 4726 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4729 | for (Argument &Arg : F.args()) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4730 | IRPosition ArgPos = IRPosition::argument(Arg); |
| 4731 | |
| 4732 | // Every argument might be simplified. |
| 4733 | getOrCreateAAFor<AAValueSimplify>(ArgPos); |
| 4734 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4735 | if (Arg.getType()->isPointerTy()) { |
| 4736 | // Every argument with pointer type might be marked nonnull. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4737 | getOrCreateAAFor<AANonNull>(ArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4738 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 4739 | // Every argument with pointer type might be marked noalias. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4740 | getOrCreateAAFor<AANoAlias>(ArgPos); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 4741 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4742 | // Every argument with pointer type might be marked dereferenceable. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4743 | getOrCreateAAFor<AADereferenceable>(ArgPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4744 | |
| 4745 | // Every argument with pointer type might be marked align. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4746 | getOrCreateAAFor<AAAlign>(ArgPos); |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4747 | |
| 4748 | // Every argument with pointer type might be marked nocapture. |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4749 | getOrCreateAAFor<AANoCapture>(ArgPos); |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4750 | |
| 4751 | // Every argument with pointer type might be marked |
| 4752 | // "readnone/readonly/writeonly/..." |
| 4753 | getOrCreateAAFor<AAMemoryBehavior>(ArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4754 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4757 | auto CallSitePred = [&](Instruction &I) -> bool { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4758 | CallSite CS(&I); |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4759 | if (CS.getCalledFunction()) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4760 | for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) { |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4761 | |
| 4762 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); |
| 4763 | |
| 4764 | // Call site argument might be simplified. |
| 4765 | getOrCreateAAFor<AAValueSimplify>(CSArgPos); |
| 4766 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4767 | if (!CS.getArgument(i)->getType()->isPointerTy()) |
| 4768 | continue; |
| 4769 | |
| 4770 | // Call site argument attribute "non-null". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4771 | getOrCreateAAFor<AANonNull>(CSArgPos); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4772 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 4773 | // Call site argument attribute "no-alias". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4774 | getOrCreateAAFor<AANoAlias>(CSArgPos); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 4775 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 4776 | // Call site argument attribute "dereferenceable". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4777 | getOrCreateAAFor<AADereferenceable>(CSArgPos); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 4778 | |
| 4779 | // Call site argument attribute "align". |
| Johannes Doerfert | 97fd582 | 2019-09-04 16:26:20 +0000 | [diff] [blame] | 4780 | getOrCreateAAFor<AAAlign>(CSArgPos); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 4781 | } |
| 4782 | } |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4783 | return true; |
| 4784 | }; |
| 4785 | |
| 4786 | auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); |
| 4787 | bool Success, AnyDead = false; |
| 4788 | Success = checkForAllInstructionsImpl( |
| 4789 | OpcodeInstMap, CallSitePred, nullptr, AnyDead, |
| 4790 | {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 4791 | (unsigned)Instruction::Call}); |
| 4792 | (void)Success; |
| 4793 | assert(Success && !AnyDead && "Expected the check call to be successful!"); |
| 4794 | |
| 4795 | auto LoadStorePred = [&](Instruction &I) -> bool { |
| 4796 | if (isa<LoadInst>(I)) |
| 4797 | getOrCreateAAFor<AAAlign>( |
| 4798 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); |
| 4799 | else |
| 4800 | getOrCreateAAFor<AAAlign>( |
| 4801 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); |
| 4802 | return true; |
| 4803 | }; |
| 4804 | Success = checkForAllInstructionsImpl( |
| 4805 | OpcodeInstMap, LoadStorePred, nullptr, AnyDead, |
| 4806 | {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); |
| 4807 | (void)Success; |
| 4808 | assert(Success && !AnyDead && "Expected the check call to be successful!"); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /// Helpers to ease debugging through output streams and print calls. |
| 4812 | /// |
| 4813 | ///{ |
| 4814 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { |
| 4815 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); |
| 4816 | } |
| 4817 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4818 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4819 | switch (AP) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4820 | case IRPosition::IRP_INVALID: |
| 4821 | return OS << "inv"; |
| 4822 | case IRPosition::IRP_FLOAT: |
| 4823 | return OS << "flt"; |
| 4824 | case IRPosition::IRP_RETURNED: |
| 4825 | return OS << "fn_ret"; |
| 4826 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 4827 | return OS << "cs_ret"; |
| 4828 | case IRPosition::IRP_FUNCTION: |
| 4829 | return OS << "fn"; |
| 4830 | case IRPosition::IRP_CALL_SITE: |
| 4831 | return OS << "cs"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4832 | case IRPosition::IRP_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4833 | return OS << "arg"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4834 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4835 | return OS << "cs_arg"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4836 | } |
| 4837 | llvm_unreachable("Unknown attribute position!"); |
| 4838 | } |
| 4839 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4840 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 4841 | const Value &AV = Pos.getAssociatedValue(); |
| 4842 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4843 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; |
| 4844 | } |
| 4845 | |
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 4846 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerState &S) { |
| 4847 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" |
| 4848 | << static_cast<const AbstractState &>(S); |
| 4849 | } |
| 4850 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4851 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { |
| 4852 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); |
| 4853 | } |
| 4854 | |
| 4855 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { |
| 4856 | AA.print(OS); |
| 4857 | return OS; |
| 4858 | } |
| 4859 | |
| 4860 | void AbstractAttribute::print(raw_ostream &OS) const { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 4861 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() |
| 4862 | << "]"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4863 | } |
| 4864 | ///} |
| 4865 | |
| 4866 | /// ---------------------------------------------------------------------------- |
| 4867 | /// Pass (Manager) Boilerplate |
| 4868 | /// ---------------------------------------------------------------------------- |
| 4869 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 4870 | static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4871 | if (DisableAttributor) |
| 4872 | return false; |
| 4873 | |
| 4874 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() |
| 4875 | << " functions.\n"); |
| 4876 | |
| 4877 | // Create an Attributor and initially empty information cache that is filled |
| 4878 | // while we identify default attribute opportunities. |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 4879 | InformationCache InfoCache(M, AG); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 4880 | Attributor A(InfoCache, DepRecInterval); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4881 | |
| Johannes Doerfert | 3ab9e8b | 2019-09-17 10:52:41 +0000 | [diff] [blame] | 4882 | for (Function &F : M) |
| 4883 | A.initializeInformationCache(F); |
| 4884 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4885 | for (Function &F : M) { |
| Johannes Doerfert | b0412e4 | 2019-09-04 16:16:13 +0000 | [diff] [blame] | 4886 | if (F.hasExactDefinition()) |
| 4887 | NumFnWithExactDefinition++; |
| 4888 | else |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4889 | NumFnWithoutExactDefinition++; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4890 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4891 | // We look at internal functions only on-demand but if any use is not a |
| 4892 | // direct call, we have to do it eagerly. |
| Johannes Doerfert | 766f2cc | 2019-10-07 23:21:52 +0000 | [diff] [blame] | 4893 | if (F.hasLocalLinkage()) { |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4894 | if (llvm::all_of(F.uses(), [](const Use &U) { |
| 4895 | return ImmutableCallSite(U.getUser()) && |
| 4896 | ImmutableCallSite(U.getUser()).isCallee(&U); |
| 4897 | })) |
| 4898 | continue; |
| 4899 | } |
| 4900 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4901 | // Populate the Attributor with abstract attribute opportunities in the |
| 4902 | // function and the information cache with IR information. |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 4903 | A.identifyDefaultAbstractAttributes(F); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| Johannes Doerfert | 2f62206 | 2019-09-04 16:35:20 +0000 | [diff] [blame] | 4906 | return A.run(M) == ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { |
| Hideto Ueno | 63f6066 | 2019-09-21 15:13:19 +0000 | [diff] [blame] | 4910 | AnalysisGetter AG(AM); |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 4911 | if (runAttributorOnModule(M, AG)) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4912 | // FIXME: Think about passes we will preserve and add them here. |
| 4913 | return PreservedAnalyses::none(); |
| 4914 | } |
| 4915 | return PreservedAnalyses::all(); |
| 4916 | } |
| 4917 | |
| 4918 | namespace { |
| 4919 | |
| 4920 | struct AttributorLegacyPass : public ModulePass { |
| 4921 | static char ID; |
| 4922 | |
| 4923 | AttributorLegacyPass() : ModulePass(ID) { |
| 4924 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 4925 | } |
| 4926 | |
| 4927 | bool runOnModule(Module &M) override { |
| 4928 | if (skipModule(M)) |
| 4929 | return false; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4930 | |
| Hideto Ueno | 3bb5cbc | 2019-09-17 05:45:18 +0000 | [diff] [blame] | 4931 | AnalysisGetter AG; |
| 4932 | return runAttributorOnModule(M, AG); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 4936 | // FIXME: Think about passes we will preserve and add them here. |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4937 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 4938 | } |
| 4939 | }; |
| 4940 | |
| 4941 | } // end anonymous namespace |
| 4942 | |
| 4943 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } |
| 4944 | |
| 4945 | char AttributorLegacyPass::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 4946 | |
| 4947 | const char AAReturnedValues::ID = 0; |
| 4948 | const char AANoUnwind::ID = 0; |
| 4949 | const char AANoSync::ID = 0; |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 4950 | const char AANoFree::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 4951 | const char AANonNull::ID = 0; |
| 4952 | const char AANoRecurse::ID = 0; |
| 4953 | const char AAWillReturn::ID = 0; |
| 4954 | const char AANoAlias::ID = 0; |
| 4955 | const char AANoReturn::ID = 0; |
| 4956 | const char AAIsDead::ID = 0; |
| 4957 | const char AADereferenceable::ID = 0; |
| 4958 | const char AAAlign::ID = 0; |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 4959 | const char AANoCapture::ID = 0; |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 4960 | const char AAValueSimplify::ID = 0; |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 4961 | const char AAHeapToStack::ID = 0; |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 4962 | const char AAMemoryBehavior::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 4963 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4964 | // Macro magic to create the static generator function for attributes that |
| 4965 | // follow the naming scheme. |
| 4966 | |
| 4967 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ |
| 4968 | case IRPosition::PK: \ |
| 4969 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); |
| 4970 | |
| 4971 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ |
| 4972 | case IRPosition::PK: \ |
| 4973 | AA = new CLASS##SUFFIX(IRP); \ |
| 4974 | break; |
| 4975 | |
| 4976 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 4977 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 4978 | CLASS *AA = nullptr; \ |
| 4979 | switch (IRP.getPositionKind()) { \ |
| 4980 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 4981 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 4982 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 4983 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 4984 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 4985 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 4986 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 4987 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 4988 | } \ |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 4989 | return *AA; \ |
| 4990 | } |
| 4991 | |
| 4992 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 4993 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 4994 | CLASS *AA = nullptr; \ |
| 4995 | switch (IRP.getPositionKind()) { \ |
| 4996 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 4997 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ |
| 4998 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 4999 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 5000 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 5001 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 5002 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 5003 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 5004 | } \ |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5005 | return *AA; \ |
| 5006 | } |
| 5007 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5008 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 5009 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 5010 | CLASS *AA = nullptr; \ |
| 5011 | switch (IRP.getPositionKind()) { \ |
| 5012 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 5013 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 5014 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 5015 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 5016 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 5017 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 5018 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 5019 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 5020 | } \ |
| 5021 | return *AA; \ |
| 5022 | } |
| 5023 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5024 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 5025 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 5026 | CLASS *AA = nullptr; \ |
| 5027 | switch (IRP.getPositionKind()) { \ |
| 5028 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 5029 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 5030 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 5031 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 5032 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 5033 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 5034 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 5035 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 5036 | } \ |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5037 | return *AA; \ |
| 5038 | } |
| 5039 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5040 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 5041 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 5042 | CLASS *AA = nullptr; \ |
| 5043 | switch (IRP.getPositionKind()) { \ |
| 5044 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 5045 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 5046 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 5047 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 5048 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 5049 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 5050 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 5051 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 5052 | } \ |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5053 | return *AA; \ |
| 5054 | } |
| 5055 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5056 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) |
| 5057 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) |
| 5058 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) |
| 5059 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) |
| 5060 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) |
| 5061 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) |
| 5062 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) |
| 5063 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) |
| 5064 | |
| 5065 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) |
| 5066 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) |
| 5067 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) |
| 5068 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) |
| Johannes Doerfert | 7516a5e | 2019-09-03 20:37:24 +0000 | [diff] [blame] | 5069 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5070 | |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5071 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) |
| 5072 | |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5073 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) |
| 5074 | |
| Johannes Doerfert | 1097fab | 2019-10-07 21:07:57 +0000 | [diff] [blame] | 5075 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) |
| 5076 | |
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 5077 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5078 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | d4bea88 | 2019-10-07 23:28:54 +0000 | [diff] [blame] | 5079 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5080 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Hideto Ueno | f2b9dc4 | 2019-09-07 07:03:05 +0000 | [diff] [blame] | 5081 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 5082 | #undef SWITCH_PK_CREATE |
| 5083 | #undef SWITCH_PK_INV |
| 5084 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5085 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", |
| 5086 | "Deduce and propagate attributes", false, false) |
| Stefan Stipanovic | 431141c | 2019-09-15 21:47:41 +0000 | [diff] [blame] | 5087 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 5088 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", |
| 5089 | "Deduce and propagate attributes", false, false) |