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