| 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" |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Argument.h" |
| 29 | #include "llvm/IR/Attributes.h" |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 30 | #include "llvm/IR/CFG.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 31 | #include "llvm/IR/InstIterator.h" |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IntrinsicInst.h" |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 37 | #include "llvm/Transforms/Utils/Local.h" |
| 38 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 39 | #include <cassert> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | #define DEBUG_TYPE "attributor" |
| 44 | |
| 45 | STATISTIC(NumFnWithExactDefinition, |
| 46 | "Number of function with exact definitions"); |
| 47 | STATISTIC(NumFnWithoutExactDefinition, |
| 48 | "Number of function without exact definitions"); |
| 49 | STATISTIC(NumAttributesTimedOut, |
| 50 | "Number of abstract attributes timed out before fixpoint"); |
| 51 | STATISTIC(NumAttributesValidFixpoint, |
| 52 | "Number of abstract attributes in a valid fixpoint state"); |
| 53 | STATISTIC(NumAttributesManifested, |
| 54 | "Number of abstract attributes manifested in IR"); |
| 55 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 56 | // Some helper macros to deal with statistics tracking. |
| 57 | // |
| 58 | // Usage: |
| 59 | // For simple IR attribute tracking overload trackStatistics in the abstract |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 60 | // attribute and choose the right STATS_DECLTRACK_********* macro, |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 61 | // e.g.,: |
| 62 | // void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 63 | // STATS_DECLTRACK_ARG_ATTR(returned) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 64 | // } |
| 65 | // If there is a single "increment" side one can use the macro |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 66 | // STATS_DECLTRACK with a custom message. If there are multiple increment |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 67 | // sides, STATS_DECL and STATS_TRACK can also be used separatly. |
| 68 | // |
| 69 | #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \ |
| 70 | ("Number of " #TYPE " marked '" #NAME "'") |
| 71 | #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME |
| 72 | #define STATS_DECL(NAME, TYPE, MSG) STATISTIC(BUILD_STAT_NAME(NAME, TYPE), MSG); |
| 73 | #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 74 | #define STATS_DECLTRACK(NAME, TYPE, MSG) \ |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 75 | { \ |
| 76 | STATS_DECL(NAME, TYPE, MSG) \ |
| 77 | STATS_TRACK(NAME, TYPE) \ |
| 78 | } |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 79 | #define STATS_DECLTRACK_ARG_ATTR(NAME) \ |
| 80 | STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) |
| 81 | #define STATS_DECLTRACK_CSARG_ATTR(NAME) \ |
| 82 | STATS_DECLTRACK(NAME, CSArguments, \ |
| 83 | BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) |
| 84 | #define STATS_DECLTRACK_FN_ATTR(NAME) \ |
| 85 | STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) |
| 86 | #define STATS_DECLTRACK_CS_ATTR(NAME) \ |
| 87 | STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) |
| 88 | #define STATS_DECLTRACK_FNRET_ATTR(NAME) \ |
| 89 | STATS_DECLTRACK(NAME, FunctionReturn, \ |
| Johannes Doerfert | 2db8528 | 2019-08-21 20:56:56 +0000 | [diff] [blame] | 90 | BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 91 | #define STATS_DECLTRACK_CSRET_ATTR(NAME) \ |
| 92 | STATS_DECLTRACK(NAME, CSReturn, \ |
| 93 | BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) |
| 94 | #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \ |
| 95 | STATS_DECLTRACK(NAME, Floating, \ |
| 96 | ("Number of floating values known to be '" #NAME "'")) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 97 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 98 | // TODO: Determine a good default value. |
| 99 | // |
| 100 | // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads |
| 101 | // (when run with the first 5 abstract attributes). The results also indicate |
| 102 | // that we never reach 32 iterations but always find a fixpoint sooner. |
| 103 | // |
| 104 | // This will become more evolved once we perform two interleaved fixpoint |
| 105 | // iterations: bottom-up and top-down. |
| 106 | static cl::opt<unsigned> |
| 107 | MaxFixpointIterations("attributor-max-iterations", cl::Hidden, |
| 108 | cl::desc("Maximal number of fixpoint iterations."), |
| 109 | cl::init(32)); |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 110 | static cl::opt<bool> VerifyMaxFixpointIterations( |
| 111 | "attributor-max-iterations-verify", cl::Hidden, |
| 112 | cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), |
| 113 | cl::init(false)); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 114 | |
| 115 | static cl::opt<bool> DisableAttributor( |
| 116 | "attributor-disable", cl::Hidden, |
| 117 | cl::desc("Disable the attributor inter-procedural deduction pass."), |
| Johannes Doerfert | 282d34e | 2019-06-14 14:53:41 +0000 | [diff] [blame] | 118 | cl::init(true)); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 119 | |
| 120 | static cl::opt<bool> VerifyAttributor( |
| 121 | "attributor-verify", cl::Hidden, |
| 122 | cl::desc("Verify the Attributor deduction and " |
| 123 | "manifestation of attributes -- may issue false-positive errors"), |
| 124 | cl::init(false)); |
| 125 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 126 | static cl::opt<unsigned> DepRecInterval( |
| 127 | "attributor-dependence-recompute-interval", cl::Hidden, |
| 128 | cl::desc("Number of iterations until dependences are recomputed."), |
| 129 | cl::init(4)); |
| 130 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 131 | /// Logic operators for the change status enum class. |
| 132 | /// |
| 133 | ///{ |
| 134 | ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { |
| 135 | return l == ChangeStatus::CHANGED ? l : r; |
| 136 | } |
| 137 | ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { |
| 138 | return l == ChangeStatus::UNCHANGED ? l : r; |
| 139 | } |
| 140 | ///} |
| 141 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 142 | /// 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] | 143 | /// will be done by looking through cast instructions, selects, phis, and calls |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 144 | /// with the "returned" attribute. Once we cannot look through the value any |
| 145 | /// further, the callback \p VisitValueCB is invoked and passed the current |
| 146 | /// value, the \p State, and a flag to indicate if we stripped anything. To |
| 147 | /// limit how much effort is invested, we will never visit more values than |
| 148 | /// specified by \p MaxValues. |
| 149 | template <typename AAType, typename StateTy> |
| 150 | bool genericValueTraversal( |
| 151 | Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 152 | const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 153 | int MaxValues = 8) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 154 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 155 | const AAIsDead *LivenessAA = nullptr; |
| 156 | if (IRP.getAnchorScope()) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 157 | LivenessAA = &A.getAAFor<AAIsDead>( |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 158 | QueryingAA, IRPosition::function(*IRP.getAnchorScope()), |
| 159 | /* TrackDependence */ false); |
| 160 | bool AnyDead = false; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 161 | |
| 162 | // TODO: Use Positions here to allow context sensitivity in VisitValueCB |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 163 | SmallPtrSet<Value *, 16> Visited; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 164 | SmallVector<Value *, 16> Worklist; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 165 | Worklist.push_back(&IRP.getAssociatedValue()); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 166 | |
| 167 | int Iteration = 0; |
| 168 | do { |
| 169 | Value *V = Worklist.pop_back_val(); |
| 170 | |
| 171 | // Check if we should process the current value. To prevent endless |
| 172 | // recursion keep a record of the values we followed! |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 173 | if (!Visited.insert(V).second) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 174 | continue; |
| 175 | |
| 176 | // Make sure we limit the compile time for complex expressions. |
| 177 | if (Iteration++ >= MaxValues) |
| 178 | return false; |
| 179 | |
| 180 | // Explicitly look through calls with a "returned" attribute if we do |
| 181 | // not have a pointer as stripPointerCasts only works on them. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 182 | Value *NewV = nullptr; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 183 | if (V->getType()->isPointerTy()) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 184 | NewV = V->stripPointerCasts(); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 185 | } else { |
| 186 | CallSite CS(V); |
| 187 | if (CS && CS.getCalledFunction()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 188 | for (Argument &Arg : CS.getCalledFunction()->args()) |
| 189 | if (Arg.hasReturnedAttr()) { |
| 190 | NewV = CS.getArgOperand(Arg.getArgNo()); |
| 191 | break; |
| 192 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 195 | if (NewV && NewV != V) { |
| 196 | Worklist.push_back(NewV); |
| 197 | continue; |
| 198 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 199 | |
| 200 | // Look through select instructions, visit both potential values. |
| 201 | if (auto *SI = dyn_cast<SelectInst>(V)) { |
| 202 | Worklist.push_back(SI->getTrueValue()); |
| 203 | Worklist.push_back(SI->getFalseValue()); |
| 204 | continue; |
| 205 | } |
| 206 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 207 | // Look through phi nodes, visit all live operands. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 208 | if (auto *PHI = dyn_cast<PHINode>(V)) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 209 | assert(LivenessAA && |
| 210 | "Expected liveness in the presence of instructions!"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 211 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { |
| 212 | const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 213 | if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) { |
| 214 | AnyDead =true; |
| 215 | continue; |
| 216 | } |
| 217 | Worklist.push_back(PHI->getIncomingValue(u)); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 218 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 219 | continue; |
| 220 | } |
| 221 | |
| 222 | // Once a leaf is reached we inform the user through the callback. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 223 | if (!VisitValueCB(*V, State, Iteration > 1)) |
| 224 | return false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 225 | } while (!Worklist.empty()); |
| 226 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 227 | // If we actually used liveness information so we have to record a dependence. |
| 228 | if (AnyDead) |
| 229 | A.recordDependence(*LivenessAA, QueryingAA); |
| 230 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 231 | // All values have been visited. |
| 232 | return true; |
| 233 | } |
| 234 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 235 | /// Return true if \p New is equal or worse than \p Old. |
| 236 | static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { |
| 237 | if (!Old.isIntAttribute()) |
| 238 | return true; |
| 239 | |
| 240 | return Old.getValueAsInt() >= New.getValueAsInt(); |
| 241 | } |
| 242 | |
| 243 | /// Return true if the information provided by \p Attr was added to the |
| 244 | /// 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] | 245 | /// 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] | 246 | static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 247 | AttributeList &Attrs, int AttrIdx) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 248 | |
| 249 | if (Attr.isEnumAttribute()) { |
| 250 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
| 251 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 252 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 253 | return false; |
| 254 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 255 | return true; |
| 256 | } |
| 257 | if (Attr.isStringAttribute()) { |
| 258 | StringRef Kind = Attr.getKindAsString(); |
| 259 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 260 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 261 | return false; |
| 262 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 263 | return true; |
| 264 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 265 | if (Attr.isIntAttribute()) { |
| 266 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
| 267 | if (Attrs.hasAttribute(AttrIdx, Kind)) |
| 268 | if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) |
| 269 | return false; |
| 270 | Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); |
| 271 | Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); |
| 272 | return true; |
| 273 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 274 | |
| 275 | llvm_unreachable("Expected enum or string attribute!"); |
| 276 | } |
| 277 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 278 | ChangeStatus AbstractAttribute::update(Attributor &A) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 279 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| 280 | if (getState().isAtFixpoint()) |
| 281 | return HasChanged; |
| 282 | |
| 283 | LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); |
| 284 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 285 | HasChanged = updateImpl(A); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 286 | |
| 287 | LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this |
| 288 | << "\n"); |
| 289 | |
| 290 | return HasChanged; |
| 291 | } |
| 292 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 293 | ChangeStatus |
| 294 | IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP, |
| 295 | const ArrayRef<Attribute> &DeducedAttrs) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 296 | Function *ScopeFn = IRP.getAssociatedFunction(); |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 297 | IRPosition::Kind PK = IRP.getPositionKind(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 298 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 299 | // In the following some generic code that will manifest attributes in |
| 300 | // DeducedAttrs if they improve the current IR. Due to the different |
| 301 | // annotation positions we use the underlying AttributeList interface. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 302 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 303 | AttributeList Attrs; |
| 304 | switch (PK) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 305 | case IRPosition::IRP_INVALID: |
| 306 | case IRPosition::IRP_FLOAT: |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 307 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 308 | case IRPosition::IRP_ARGUMENT: |
| 309 | case IRPosition::IRP_FUNCTION: |
| 310 | case IRPosition::IRP_RETURNED: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 311 | Attrs = ScopeFn->getAttributes(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 312 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 313 | case IRPosition::IRP_CALL_SITE: |
| 314 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 315 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 316 | Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 317 | break; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 320 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 321 | LLVMContext &Ctx = IRP.getAnchorValue().getContext(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 322 | for (const Attribute &Attr : DeducedAttrs) { |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 323 | if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 324 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 325 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 326 | HasChanged = ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (HasChanged == ChangeStatus::UNCHANGED) |
| 330 | return HasChanged; |
| 331 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 332 | switch (PK) { |
| 333 | case IRPosition::IRP_ARGUMENT: |
| 334 | case IRPosition::IRP_FUNCTION: |
| 335 | case IRPosition::IRP_RETURNED: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 336 | ScopeFn->setAttributes(Attrs); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 337 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 338 | case IRPosition::IRP_CALL_SITE: |
| 339 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 340 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Kristina Brooks | 26e60f0 | 2019-08-06 19:53:19 +0000 | [diff] [blame] | 341 | CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); |
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 342 | break; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 343 | case IRPosition::IRP_INVALID: |
| Johannes Doerfert | 4395b31 | 2019-08-14 21:46:28 +0000 | [diff] [blame] | 344 | case IRPosition::IRP_FLOAT: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 345 | break; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return HasChanged; |
| 349 | } |
| 350 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 351 | const IRPosition IRPosition::EmptyKey(255); |
| 352 | const IRPosition IRPosition::TombstoneKey(256); |
| 353 | |
| 354 | SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { |
| 355 | IRPositions.emplace_back(IRP); |
| 356 | |
| 357 | ImmutableCallSite ICS(&IRP.getAnchorValue()); |
| 358 | switch (IRP.getPositionKind()) { |
| 359 | case IRPosition::IRP_INVALID: |
| 360 | case IRPosition::IRP_FLOAT: |
| 361 | case IRPosition::IRP_FUNCTION: |
| 362 | return; |
| 363 | case IRPosition::IRP_ARGUMENT: |
| 364 | case IRPosition::IRP_RETURNED: |
| 365 | IRPositions.emplace_back( |
| 366 | IRPosition::function(*IRP.getAssociatedFunction())); |
| 367 | return; |
| 368 | case IRPosition::IRP_CALL_SITE: |
| 369 | assert(ICS && "Expected call site!"); |
| 370 | // TODO: We need to look at the operand bundles similar to the redirection |
| 371 | // in CallBase. |
| 372 | if (!ICS.hasOperandBundles()) |
| 373 | if (const Function *Callee = ICS.getCalledFunction()) |
| 374 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 375 | return; |
| 376 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 377 | assert(ICS && "Expected call site!"); |
| 378 | // TODO: We need to look at the operand bundles similar to the redirection |
| 379 | // in CallBase. |
| 380 | if (!ICS.hasOperandBundles()) { |
| 381 | if (const Function *Callee = ICS.getCalledFunction()) { |
| 382 | IRPositions.emplace_back(IRPosition::returned(*Callee)); |
| 383 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 384 | } |
| 385 | } |
| 386 | IRPositions.emplace_back( |
| 387 | IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); |
| 388 | return; |
| 389 | case IRPosition::IRP_CALL_SITE_ARGUMENT: { |
| 390 | int ArgNo = IRP.getArgNo(); |
| 391 | assert(ICS && ArgNo >= 0 && "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 | const Function *Callee = ICS.getCalledFunction(); |
| 396 | if (Callee && Callee->arg_size() > unsigned(ArgNo)) |
| 397 | IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); |
| 398 | if (Callee) |
| 399 | IRPositions.emplace_back(IRPosition::function(*Callee)); |
| 400 | } |
| 401 | IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs) const { |
| 408 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) |
| 409 | for (Attribute::AttrKind AK : AKs) |
| 410 | if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) |
| 411 | return true; |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, |
| 416 | SmallVectorImpl<Attribute> &Attrs) const { |
| 417 | for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) |
| 418 | for (Attribute::AttrKind AK : AKs) { |
| 419 | const Attribute &Attr = EquivIRP.getAttr(AK); |
| 420 | if (Attr.getKindAsEnum() == AK) |
| 421 | Attrs.push_back(Attr); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void IRPosition::verify() { |
| 426 | switch (KindOrArgNo) { |
| 427 | default: |
| 428 | assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); |
| 429 | assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && |
| 430 | "Expected call base or argument for positive attribute index!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 431 | if (isa<Argument>(AnchorVal)) { |
| 432 | assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 433 | "Argument number mismatch!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 434 | assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && |
| 435 | "Associated value mismatch!"); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 436 | } else { |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 437 | assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 438 | "Call site argument number mismatch!"); |
| Simon Pilgrim | 920b040 | 2019-08-29 10:08:45 +0000 | [diff] [blame] | 439 | assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == |
| 440 | &getAssociatedValue() && |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 441 | "Associated value mismatch!"); |
| 442 | } |
| 443 | break; |
| 444 | case IRP_INVALID: |
| 445 | assert(!AnchorVal && "Expected no value for an invalid position!"); |
| 446 | break; |
| 447 | case IRP_FLOAT: |
| 448 | assert((!isa<CallBase>(&getAssociatedValue()) && |
| 449 | !isa<Argument>(&getAssociatedValue())) && |
| 450 | "Expected specialized kind for call base and argument values!"); |
| 451 | break; |
| 452 | case IRP_RETURNED: |
| 453 | assert(isa<Function>(AnchorVal) && |
| 454 | "Expected function for a 'returned' position!"); |
| 455 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 456 | break; |
| 457 | case IRP_CALL_SITE_RETURNED: |
| 458 | assert((isa<CallBase>(AnchorVal)) && |
| 459 | "Expected call base for 'call site returned' position!"); |
| 460 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 461 | break; |
| 462 | case IRP_CALL_SITE: |
| 463 | assert((isa<CallBase>(AnchorVal)) && |
| 464 | "Expected call base for 'call site function' position!"); |
| 465 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 466 | break; |
| 467 | case IRP_FUNCTION: |
| 468 | assert(isa<Function>(AnchorVal) && |
| 469 | "Expected function for a 'function' position!"); |
| 470 | assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 475 | /// Helper functions to clamp a state \p S of type \p StateType with the |
| 476 | /// information in \p R and indicate/return if \p S did change (as-in update is |
| 477 | /// required to be run again). |
| 478 | /// |
| 479 | ///{ |
| 480 | template <typename StateType> |
| 481 | ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R); |
| 482 | |
| 483 | template <> |
| 484 | ChangeStatus clampStateAndIndicateChange<IntegerState>(IntegerState &S, |
| 485 | const IntegerState &R) { |
| 486 | auto Assumed = S.getAssumed(); |
| 487 | S ^= R; |
| 488 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED |
| 489 | : ChangeStatus::CHANGED; |
| 490 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 491 | |
| 492 | template <> |
| 493 | ChangeStatus clampStateAndIndicateChange<BooleanState>(BooleanState &S, |
| 494 | const BooleanState &R) { |
| 495 | return clampStateAndIndicateChange<IntegerState>(S, R); |
| 496 | } |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 497 | ///} |
| 498 | |
| 499 | /// Clamp the information known for all returned values of a function |
| 500 | /// (identified by \p QueryingAA) into \p S. |
| 501 | template <typename AAType, typename StateType = typename AAType::StateType> |
| 502 | static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, |
| 503 | StateType &S) { |
| 504 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " |
| 505 | << static_cast<const AbstractAttribute &>(QueryingAA) |
| 506 | << " into " << S << "\n"); |
| 507 | |
| 508 | assert((QueryingAA.getIRPosition().getPositionKind() == |
| 509 | IRPosition::IRP_RETURNED || |
| 510 | QueryingAA.getIRPosition().getPositionKind() == |
| 511 | IRPosition::IRP_CALL_SITE_RETURNED) && |
| 512 | "Can only clamp returned value states for a function returned or call " |
| 513 | "site returned position!"); |
| 514 | |
| 515 | // Use an optional state as there might not be any return values and we want |
| 516 | // to join (IntegerState::operator&) the state of all there are. |
| 517 | Optional<StateType> T; |
| 518 | |
| 519 | // Callback for each possibly returned value. |
| 520 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 521 | const IRPosition &RVPos = IRPosition::value(RV); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 522 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); |
| 523 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() |
| 524 | << " @ " << RVPos << "\n"); |
| 525 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 526 | if (T.hasValue()) |
| 527 | *T &= AAS; |
| 528 | else |
| 529 | T = AAS; |
| 530 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T |
| 531 | << "\n"); |
| 532 | return T->isValidState(); |
| 533 | }; |
| 534 | |
| 535 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) |
| 536 | S.indicatePessimisticFixpoint(); |
| 537 | else if (T.hasValue()) |
| 538 | S ^= *T; |
| 539 | } |
| 540 | |
| 541 | /// Helper class for generic deduction: return value -> returned position. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 542 | template <typename AAType, typename Base, |
| 543 | typename StateType = typename AAType::StateType> |
| 544 | struct AAReturnedFromReturnedValues : public Base { |
| 545 | AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 546 | |
| 547 | /// See AbstractAttribute::updateImpl(...). |
| 548 | ChangeStatus updateImpl(Attributor &A) override { |
| 549 | StateType S; |
| 550 | clampReturnedValueStates<AAType, StateType>(A, *this, S); |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 551 | // TODO: If we know we visited all returned values, thus no are assumed |
| 552 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 553 | return clampStateAndIndicateChange<StateType>(this->getState(), S); |
| 554 | } |
| 555 | }; |
| 556 | |
| 557 | /// Clamp the information known at all call sites for a given argument |
| 558 | /// (identified by \p QueryingAA) into \p S. |
| 559 | template <typename AAType, typename StateType = typename AAType::StateType> |
| 560 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, |
| 561 | StateType &S) { |
| 562 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " |
| 563 | << static_cast<const AbstractAttribute &>(QueryingAA) |
| 564 | << " into " << S << "\n"); |
| 565 | |
| 566 | assert(QueryingAA.getIRPosition().getPositionKind() == |
| 567 | IRPosition::IRP_ARGUMENT && |
| 568 | "Can only clamp call site argument states for an argument position!"); |
| 569 | |
| 570 | // Use an optional state as there might not be any return values and we want |
| 571 | // to join (IntegerState::operator&) the state of all there are. |
| 572 | Optional<StateType> T; |
| 573 | |
| 574 | // The argument number which is also the call site argument number. |
| 575 | unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); |
| 576 | |
| 577 | auto CallSiteCheck = [&](CallSite CS) { |
| 578 | const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 579 | const AAType &AA = A.getAAFor<AAType>(QueryingAA, CSArgPos); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 580 | LLVM_DEBUG(dbgs() << "[Attributor] CS: " << *CS.getInstruction() |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 581 | << " AA: " << AA.getAsStr() << " @" << CSArgPos << "\n"); |
| 582 | const StateType &AAS = static_cast<const StateType &>(AA.getState()); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 583 | if (T.hasValue()) |
| 584 | *T &= AAS; |
| 585 | else |
| 586 | T = AAS; |
| 587 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T |
| 588 | << "\n"); |
| 589 | return T->isValidState(); |
| 590 | }; |
| 591 | |
| 592 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true)) |
| 593 | S.indicatePessimisticFixpoint(); |
| 594 | else if (T.hasValue()) |
| 595 | S ^= *T; |
| 596 | } |
| 597 | |
| 598 | /// Helper class for generic deduction: call site argument -> argument position. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 599 | template <typename AAType, typename Base, |
| 600 | typename StateType = typename AAType::StateType> |
| 601 | struct AAArgumentFromCallSiteArguments : public Base { |
| 602 | AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 603 | |
| 604 | /// See AbstractAttribute::updateImpl(...). |
| 605 | ChangeStatus updateImpl(Attributor &A) override { |
| 606 | StateType S; |
| 607 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 608 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 609 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 610 | return clampStateAndIndicateChange<StateType>(this->getState(), S); |
| 611 | } |
| 612 | }; |
| 613 | |
| 614 | /// Helper class for generic replication: function returned -> cs returned. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 615 | template <typename AAType, typename Base> |
| 616 | struct AACallSiteReturnedFromReturned : public Base { |
| 617 | AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 618 | |
| 619 | /// See AbstractAttribute::updateImpl(...). |
| 620 | ChangeStatus updateImpl(Attributor &A) override { |
| 621 | assert(this->getIRPosition().getPositionKind() == |
| 622 | IRPosition::IRP_CALL_SITE_RETURNED && |
| 623 | "Can only wrap function returned positions for call site returned " |
| 624 | "positions!"); |
| 625 | auto &S = this->getState(); |
| 626 | |
| 627 | const Function *AssociatedFunction = |
| 628 | this->getIRPosition().getAssociatedFunction(); |
| 629 | if (!AssociatedFunction) |
| 630 | return S.indicatePessimisticFixpoint(); |
| 631 | |
| 632 | IRPosition FnPos = IRPosition::returned(*AssociatedFunction); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 633 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 634 | return clampStateAndIndicateChange( |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 635 | S, static_cast<const typename AAType::StateType &>(AA.getState())); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 636 | } |
| 637 | }; |
| 638 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 639 | /// -----------------------NoUnwind Function Attribute-------------------------- |
| 640 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 641 | struct AANoUnwindImpl : AANoUnwind { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 642 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 643 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 644 | /// See AbstractAttribute::initialize(...). |
| 645 | void initialize(Attributor &A) override { |
| 646 | if (hasAttr({Attribute::NoUnwind})) |
| 647 | indicateOptimisticFixpoint(); |
| 648 | } |
| 649 | |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 650 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 651 | return getAssumed() ? "nounwind" : "may-unwind"; |
| 652 | } |
| 653 | |
| 654 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 655 | ChangeStatus updateImpl(Attributor &A) override { |
| 656 | auto Opcodes = { |
| 657 | (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 658 | (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, |
| 659 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; |
| 660 | |
| 661 | auto CheckForNoUnwind = [&](Instruction &I) { |
| 662 | if (!I.mayThrow()) |
| 663 | return true; |
| 664 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 665 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 666 | const auto &NoUnwindAA = |
| 667 | A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); |
| 668 | return NoUnwindAA.isAssumedNoUnwind(); |
| 669 | } |
| 670 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) |
| 674 | return indicatePessimisticFixpoint(); |
| 675 | |
| 676 | return ChangeStatus::UNCHANGED; |
| 677 | } |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 678 | }; |
| 679 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 680 | struct AANoUnwindFunction final : public AANoUnwindImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 681 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 682 | |
| 683 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 684 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 687 | /// NoUnwind attribute deduction for a call sites. |
| 688 | using AANoUnwindCallSite = AANoUnwindFunction; |
| 689 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 690 | /// --------------------- Function Return Values ------------------------------- |
| 691 | |
| 692 | /// "Attribute" that collects all potential returned values and the return |
| 693 | /// instructions that they arise from. |
| 694 | /// |
| 695 | /// If there is a unique returned value R, the manifest method will: |
| 696 | /// - mark R with the "returned" attribute, if R is an argument. |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 697 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 698 | |
| 699 | /// Mapping of values potentially returned by the associated function to the |
| 700 | /// return instructions that might return them. |
| Johannes Doerfert | a4a308c | 2019-08-26 17:51:23 +0000 | [diff] [blame] | 701 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 702 | |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 703 | /// Mapping to remember the number of returned values for a call site such |
| 704 | /// that we can avoid updates if nothing changed. |
| 705 | DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; |
| 706 | |
| 707 | /// Set of unresolved calls returned by the associated function. |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 708 | SmallSetVector<CallBase *, 4> UnresolvedCalls; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 709 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 710 | /// State flags |
| 711 | /// |
| 712 | ///{ |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 713 | bool IsFixed = false; |
| 714 | bool IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 715 | ///} |
| 716 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 717 | public: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 718 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 719 | |
| 720 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 721 | void initialize(Attributor &A) override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 722 | // Reset the state. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 723 | IsFixed = false; |
| 724 | IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 725 | ReturnedValues.clear(); |
| 726 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 727 | Function *F = getAssociatedFunction(); |
| 728 | if (!F || !F->hasExactDefinition()) { |
| 729 | indicatePessimisticFixpoint(); |
| 730 | return; |
| 731 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 732 | |
| 733 | // The map from instruction opcodes to those instructions in the function. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 734 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 735 | |
| 736 | // 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] | 737 | for (Argument &Arg : F->args()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 738 | if (Arg.hasReturnedAttr()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 739 | auto &ReturnInstSet = ReturnedValues[&Arg]; |
| 740 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) |
| 741 | ReturnInstSet.insert(cast<ReturnInst>(RI)); |
| 742 | |
| 743 | indicateOptimisticFixpoint(); |
| 744 | return; |
| 745 | } |
| 746 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /// See AbstractAttribute::manifest(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 750 | ChangeStatus manifest(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 751 | |
| 752 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 753 | AbstractState &getState() override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 754 | |
| 755 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 756 | const AbstractState &getState() const override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 757 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 758 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 759 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 760 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 761 | llvm::iterator_range<iterator> returned_values() override { |
| 762 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 763 | } |
| 764 | |
| 765 | llvm::iterator_range<const_iterator> returned_values() const override { |
| 766 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 767 | } |
| 768 | |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 769 | const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 770 | return UnresolvedCalls; |
| 771 | } |
| 772 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 773 | /// Return the number of potential return values, -1 if unknown. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 774 | size_t getNumReturnValues() const override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 775 | return isValidState() ? ReturnedValues.size() : -1; |
| 776 | } |
| 777 | |
| 778 | /// Return an assumed unique return value if a single candidate is found. If |
| 779 | /// there cannot be one, return a nullptr. If it is not clear yet, return the |
| 780 | /// Optional::NoneType. |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 781 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 782 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 783 | /// See AbstractState::checkForAllReturnedValues(...). |
| 784 | bool checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 785 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 786 | &Pred) const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 787 | |
| 788 | /// Pretty print the attribute similar to the IR representation. |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 789 | const std::string getAsStr() const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 790 | |
| 791 | /// See AbstractState::isAtFixpoint(). |
| 792 | bool isAtFixpoint() const override { return IsFixed; } |
| 793 | |
| 794 | /// See AbstractState::isValidState(). |
| 795 | bool isValidState() const override { return IsValidState; } |
| 796 | |
| 797 | /// See AbstractState::indicateOptimisticFixpoint(...). |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 798 | ChangeStatus indicateOptimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 799 | IsFixed = true; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 800 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 801 | } |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 802 | |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 803 | ChangeStatus indicatePessimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 804 | IsFixed = true; |
| 805 | IsValidState = false; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 806 | return ChangeStatus::CHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 807 | } |
| 808 | }; |
| 809 | |
| 810 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { |
| 811 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 812 | |
| 813 | // Bookkeeping. |
| 814 | assert(isValidState()); |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 815 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, |
| 816 | "Number of function with known return values"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 817 | |
| 818 | // 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] | 819 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 820 | |
| 821 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) |
| 822 | return Changed; |
| 823 | |
| 824 | // Bookkeeping. |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 825 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, |
| 826 | "Number of function with unique return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 827 | |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 828 | // Callback to replace the uses of CB with the constant C. |
| 829 | auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) { |
| 830 | if (CB.getNumUses() == 0) |
| 831 | return ChangeStatus::UNCHANGED; |
| 832 | CB.replaceAllUsesWith(&C); |
| 833 | return ChangeStatus::CHANGED; |
| 834 | }; |
| 835 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 836 | // If the assumed unique return value is an argument, annotate it. |
| 837 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 838 | getIRPosition() = IRPosition::argument(*UniqueRVArg); |
| Johannes Doerfert | 23400e61 | 2019-08-23 17:41:37 +0000 | [diff] [blame] | 839 | Changed = IRAttribute::manifest(A); |
| 840 | } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { |
| 841 | // We can replace the returned value with the unique returned constant. |
| 842 | Value &AnchorValue = getAnchorValue(); |
| 843 | if (Function *F = dyn_cast<Function>(&AnchorValue)) { |
| 844 | for (const Use &U : F->uses()) |
| 845 | if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) |
| 846 | if (CB->isCallee(&U)) |
| 847 | Changed = ReplaceCallSiteUsersWith(*CB, *RVC) | Changed; |
| 848 | } else { |
| 849 | assert(isa<CallBase>(AnchorValue) && |
| 850 | "Expcected a function or call base anchor!"); |
| 851 | Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVC); |
| 852 | } |
| 853 | if (Changed == ChangeStatus::CHANGED) |
| 854 | STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, |
| 855 | "Number of function returns replaced by constant return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | return Changed; |
| 859 | } |
| 860 | |
| 861 | const std::string AAReturnedValuesImpl::getAsStr() const { |
| 862 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + |
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 863 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 864 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 867 | Optional<Value *> |
| 868 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { |
| 869 | // If checkForAllReturnedValues provides a unique value, ignoring potential |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 870 | // undef values that can also be present, it is assumed to be the actual |
| 871 | // return value and forwarded to the caller of this method. If there are |
| 872 | // multiple, a nullptr is returned indicating there cannot be a unique |
| 873 | // returned value. |
| 874 | Optional<Value *> UniqueRV; |
| 875 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 876 | auto Pred = [&](Value &RV) -> bool { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 877 | // If we found a second returned value and neither the current nor the saved |
| 878 | // one is an undef, there is no unique returned value. Undefs are special |
| 879 | // since we can pretend they have any value. |
| 880 | if (UniqueRV.hasValue() && UniqueRV != &RV && |
| 881 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { |
| 882 | UniqueRV = nullptr; |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | // Do not overwrite a value with an undef. |
| 887 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) |
| 888 | UniqueRV = &RV; |
| 889 | |
| 890 | return true; |
| 891 | }; |
| 892 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 893 | if (!A.checkForAllReturnedValues(Pred, *this)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 894 | UniqueRV = nullptr; |
| 895 | |
| 896 | return UniqueRV; |
| 897 | } |
| 898 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 899 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 900 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 901 | &Pred) const { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 902 | if (!isValidState()) |
| 903 | return false; |
| 904 | |
| 905 | // Check all returned values but ignore call sites as long as we have not |
| 906 | // encountered an overdefined one during an update. |
| 907 | for (auto &It : ReturnedValues) { |
| 908 | Value *RV = It.first; |
| 909 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 910 | CallBase *CB = dyn_cast<CallBase>(RV); |
| 911 | if (CB && !UnresolvedCalls.count(CB)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 912 | continue; |
| 913 | |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 914 | if (!Pred(*RV, It.second)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | |
| 918 | return true; |
| 919 | } |
| 920 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 921 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 922 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); |
| 923 | bool Changed = false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 924 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 925 | // State used in the value traversals starting in returned values. |
| 926 | struct RVState { |
| 927 | // The map in which we collect return values -> return instrs. |
| 928 | decltype(ReturnedValues) &RetValsMap; |
| 929 | // The flag to indicate a change. |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 930 | bool &Changed; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 931 | // The return instrs we come from. |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 932 | SmallSetVector<ReturnInst *, 4> RetInsts; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 933 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 934 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 935 | // Callback for a leaf value returned by the associated function. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 936 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 937 | auto Size = RVS.RetValsMap[&Val].size(); |
| 938 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); |
| 939 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; |
| 940 | RVS.Changed |= Inserted; |
| 941 | LLVM_DEBUG({ |
| 942 | if (Inserted) |
| 943 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val |
| 944 | << " => " << RVS.RetInsts.size() << "\n"; |
| 945 | }); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 946 | return true; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 947 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 948 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 949 | // Helper method to invoke the generic value traversal. |
| 950 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { |
| 951 | IRPosition RetValPos = IRPosition::value(RV); |
| 952 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, |
| 953 | RVS, VisitValueCB); |
| 954 | }; |
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 955 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 956 | // Callback for all "return intructions" live in the associated function. |
| 957 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { |
| 958 | ReturnInst &Ret = cast<ReturnInst>(I); |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 959 | RVState RVS({ReturnedValues, Changed, {}}); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 960 | RVS.RetInsts.insert(&Ret); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 961 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); |
| 962 | }; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 963 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 964 | // Start by discovering returned values from all live returned instructions in |
| 965 | // the associated function. |
| 966 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) |
| 967 | return indicatePessimisticFixpoint(); |
| 968 | |
| 969 | // Once returned values "directly" present in the code are handled we try to |
| 970 | // resolve returned calls. |
| 971 | decltype(ReturnedValues) NewRVsMap; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 972 | for (auto &It : ReturnedValues) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 973 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first |
| 974 | << " by #" << It.second.size() << " RIs\n"); |
| 975 | CallBase *CB = dyn_cast<CallBase>(It.first); |
| 976 | if (!CB || UnresolvedCalls.count(CB)) |
| 977 | continue; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 978 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 979 | if (!CB->getCalledFunction()) { |
| 980 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 981 | << "\n"); |
| 982 | UnresolvedCalls.insert(CB); |
| 983 | continue; |
| 984 | } |
| 985 | |
| 986 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 987 | const auto &RetValAA = A.getAAFor<AAReturnedValues>( |
| 988 | *this, IRPosition::function(*CB->getCalledFunction())); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 989 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " |
| 990 | << static_cast<const AbstractAttribute &>(RetValAA) |
| 991 | << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 992 | |
| 993 | // Skip dead ends, thus if we do not know anything about the returned |
| 994 | // call we mark it as unresolved and it will stay that way. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 995 | if (!RetValAA.getState().isValidState()) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 996 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 997 | << "\n"); |
| 998 | UnresolvedCalls.insert(CB); |
| 999 | continue; |
| 1000 | } |
| 1001 | |
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1002 | // Do not try to learn partial information. If the callee has unresolved |
| 1003 | // return values we will treat the call as unresolved/opaque. |
| 1004 | auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); |
| 1005 | if (!RetValAAUnresolvedCalls.empty()) { |
| 1006 | UnresolvedCalls.insert(CB); |
| 1007 | continue; |
| 1008 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1009 | |
| Johannes Doerfert | de7674c | 2019-08-19 21:35:31 +0000 | [diff] [blame] | 1010 | // Now check if we can track transitively returned values. If possible, thus |
| 1011 | // if all return value can be represented in the current scope, do so. |
| 1012 | bool Unresolved = false; |
| 1013 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 1014 | Value *RetVal = RetValAAIt.first; |
| 1015 | if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || |
| 1016 | isa<Constant>(RetVal)) |
| 1017 | continue; |
| 1018 | // Anything that did not fit in the above categories cannot be resolved, |
| 1019 | // mark the call as unresolved. |
| 1020 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " |
| 1021 | "cannot be translated: " |
| 1022 | << *RetVal << "\n"); |
| 1023 | UnresolvedCalls.insert(CB); |
| 1024 | Unresolved = true; |
| 1025 | break; |
| 1026 | } |
| 1027 | |
| 1028 | if (Unresolved) |
| 1029 | continue; |
| 1030 | |
| Johannes Doerfert | deb9ea3 | 2019-08-23 15:42:19 +0000 | [diff] [blame] | 1031 | // Now track transitively returned values. |
| 1032 | unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; |
| 1033 | if (NumRetAA == RetValAA.getNumReturnValues()) { |
| 1034 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " |
| 1035 | "changed since it was seen last\n"); |
| 1036 | continue; |
| 1037 | } |
| 1038 | NumRetAA = RetValAA.getNumReturnValues(); |
| 1039 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1040 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 1041 | Value *RetVal = RetValAAIt.first; |
| 1042 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { |
| 1043 | // Arguments are mapped to call site operands and we begin the traversal |
| 1044 | // again. |
| Johannes Doerfert | 056f1b5 | 2019-08-19 19:14:10 +0000 | [diff] [blame] | 1045 | bool Unused = false; |
| 1046 | RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1047 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); |
| 1048 | continue; |
| 1049 | } else if (isa<CallBase>(RetVal)) { |
| 1050 | // Call sites are resolved by the callee attribute over time, no need to |
| 1051 | // do anything for us. |
| 1052 | continue; |
| 1053 | } else if (isa<Constant>(RetVal)) { |
| 1054 | // Constants are valid everywhere, we can simply take them. |
| 1055 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); |
| 1056 | continue; |
| 1057 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1058 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1061 | // To avoid modifications to the ReturnedValues map while we iterate over it |
| 1062 | // we kept record of potential new entries in a copy map, NewRVsMap. |
| 1063 | for (auto &It : NewRVsMap) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1064 | assert(!It.second.empty() && "Entry does not add anything."); |
| 1065 | auto &ReturnInsts = ReturnedValues[It.first]; |
| 1066 | for (ReturnInst *RI : It.second) |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 1067 | if (ReturnInsts.insert(RI)) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1068 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " |
| 1069 | << *It.first << " => " << *RI << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1070 | Changed = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1074 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); |
| 1075 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1078 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { |
| 1079 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 1080 | |
| 1081 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1082 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1083 | }; |
| 1084 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1085 | /// Returned values information for a call sites. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1086 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { |
| 1087 | AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 1088 | |
| 1089 | /// See AbstractAttribute::initialize(...). |
| 1090 | void initialize(Attributor &A) override { |
| 1091 | // TODO: Once we have call site specific value information we can provide |
| 1092 | // call site specific liveness liveness information and then it makes |
| 1093 | // sense to specialize attributes for call sites instead of |
| 1094 | // redirecting requests to the callee. |
| 1095 | llvm_unreachable("Abstract attributes for returned values are not " |
| 1096 | "supported for call sites yet!"); |
| 1097 | } |
| 1098 | |
| 1099 | /// See AbstractAttribute::updateImpl(...). |
| 1100 | ChangeStatus updateImpl(Attributor &A) override { |
| 1101 | return indicatePessimisticFixpoint(); |
| 1102 | } |
| 1103 | |
| 1104 | /// See AbstractAttribute::trackStatistics() |
| 1105 | void trackStatistics() const override {} |
| 1106 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1107 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1108 | /// ------------------------ NoSync Function Attribute ------------------------- |
| 1109 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1110 | struct AANoSyncImpl : AANoSync { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1111 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1112 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1113 | /// See AbstractAttribute::initialize(...). |
| 1114 | void initialize(Attributor &A) override { |
| 1115 | if (hasAttr({Attribute::NoSync})) |
| 1116 | indicateOptimisticFixpoint(); |
| 1117 | } |
| 1118 | |
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 1119 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1120 | return getAssumed() ? "nosync" : "may-sync"; |
| 1121 | } |
| 1122 | |
| 1123 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1124 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1125 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1126 | /// Helper function used to determine whether an instruction is non-relaxed |
| 1127 | /// atomic. In other words, if an atomic instruction does not have unordered |
| 1128 | /// or monotonic ordering |
| 1129 | static bool isNonRelaxedAtomic(Instruction *I); |
| 1130 | |
| 1131 | /// Helper function used to determine whether an instruction is volatile. |
| 1132 | static bool isVolatile(Instruction *I); |
| 1133 | |
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 1134 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, |
| 1135 | /// memset). |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1136 | static bool isNoSyncIntrinsic(Instruction *I); |
| 1137 | }; |
| 1138 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1139 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1140 | if (!I->isAtomic()) |
| 1141 | return false; |
| 1142 | |
| 1143 | AtomicOrdering Ordering; |
| 1144 | switch (I->getOpcode()) { |
| 1145 | case Instruction::AtomicRMW: |
| 1146 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); |
| 1147 | break; |
| 1148 | case Instruction::Store: |
| 1149 | Ordering = cast<StoreInst>(I)->getOrdering(); |
| 1150 | break; |
| 1151 | case Instruction::Load: |
| 1152 | Ordering = cast<LoadInst>(I)->getOrdering(); |
| 1153 | break; |
| 1154 | case Instruction::Fence: { |
| 1155 | auto *FI = cast<FenceInst>(I); |
| 1156 | if (FI->getSyncScopeID() == SyncScope::SingleThread) |
| 1157 | return false; |
| 1158 | Ordering = FI->getOrdering(); |
| 1159 | break; |
| 1160 | } |
| 1161 | case Instruction::AtomicCmpXchg: { |
| 1162 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); |
| 1163 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); |
| 1164 | // Only if both are relaxed, than it can be treated as relaxed. |
| 1165 | // Otherwise it is non-relaxed. |
| 1166 | if (Success != AtomicOrdering::Unordered && |
| 1167 | Success != AtomicOrdering::Monotonic) |
| 1168 | return true; |
| 1169 | if (Failure != AtomicOrdering::Unordered && |
| 1170 | Failure != AtomicOrdering::Monotonic) |
| 1171 | return true; |
| 1172 | return false; |
| 1173 | } |
| 1174 | default: |
| 1175 | llvm_unreachable( |
| 1176 | "New atomic operations need to be known in the attributor."); |
| 1177 | } |
| 1178 | |
| 1179 | // Relaxed. |
| 1180 | if (Ordering == AtomicOrdering::Unordered || |
| 1181 | Ordering == AtomicOrdering::Monotonic) |
| 1182 | return false; |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
| 1186 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. |
| 1187 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1188 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1189 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 1190 | switch (II->getIntrinsicID()) { |
| 1191 | /// Element wise atomic memory intrinsics are can only be unordered, |
| 1192 | /// therefore nosync. |
| 1193 | case Intrinsic::memset_element_unordered_atomic: |
| 1194 | case Intrinsic::memmove_element_unordered_atomic: |
| 1195 | case Intrinsic::memcpy_element_unordered_atomic: |
| 1196 | return true; |
| 1197 | case Intrinsic::memset: |
| 1198 | case Intrinsic::memmove: |
| 1199 | case Intrinsic::memcpy: |
| 1200 | if (!cast<MemIntrinsic>(II)->isVolatile()) |
| 1201 | return true; |
| 1202 | return false; |
| 1203 | default: |
| 1204 | return false; |
| 1205 | } |
| 1206 | } |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1210 | bool AANoSyncImpl::isVolatile(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1211 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && |
| 1212 | "Calls should not be checked here"); |
| 1213 | |
| 1214 | switch (I->getOpcode()) { |
| 1215 | case Instruction::AtomicRMW: |
| 1216 | return cast<AtomicRMWInst>(I)->isVolatile(); |
| 1217 | case Instruction::Store: |
| 1218 | return cast<StoreInst>(I)->isVolatile(); |
| 1219 | case Instruction::Load: |
| 1220 | return cast<LoadInst>(I)->isVolatile(); |
| 1221 | case Instruction::AtomicCmpXchg: |
| 1222 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); |
| 1223 | default: |
| 1224 | return false; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1228 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1229 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1230 | auto CheckRWInstForNoSync = [&](Instruction &I) { |
| 1231 | /// We are looking for volatile instructions or Non-Relaxed atomics. |
| 1232 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1233 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1234 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) |
| 1235 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1236 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1237 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 1238 | if (ICS.hasFnAttr(Attribute::NoSync)) |
| 1239 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1240 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1241 | const auto &NoSyncAA = |
| 1242 | A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); |
| 1243 | if (NoSyncAA.isAssumedNoSync()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1244 | return true; |
| 1245 | return false; |
| 1246 | } |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1247 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1248 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) |
| 1249 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1250 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1251 | return false; |
| 1252 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1253 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1254 | auto CheckForNoSync = [&](Instruction &I) { |
| 1255 | // At this point we handled all read/write effects and they are all |
| 1256 | // nosync, so they can be skipped. |
| 1257 | if (I.mayReadOrWriteMemory()) |
| 1258 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1259 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1260 | // non-convergent and readnone imply nosync. |
| 1261 | return !ImmutableCallSite(&I).isConvergent(); |
| 1262 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1263 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1264 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || |
| 1265 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1266 | return indicatePessimisticFixpoint(); |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 1267 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 1268 | return ChangeStatus::UNCHANGED; |
| 1269 | } |
| 1270 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1271 | struct AANoSyncFunction final : public AANoSyncImpl { |
| 1272 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} |
| 1273 | |
| 1274 | /// See AbstractAttribute::trackStatistics() |
| 1275 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } |
| 1276 | }; |
| 1277 | |
| 1278 | /// NoSync attribute deduction for a call sites. |
| 1279 | using AANoSyncCallSite = AANoSyncFunction; |
| 1280 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1281 | /// ------------------------ No-Free Attributes ---------------------------- |
| 1282 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1283 | struct AANoFreeImpl : public AANoFree { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1284 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1285 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1286 | /// See AbstractAttribute::initialize(...). |
| 1287 | void initialize(Attributor &A) override { |
| 1288 | if (hasAttr({Attribute::NoFree})) |
| 1289 | indicateOptimisticFixpoint(); |
| 1290 | } |
| 1291 | |
| 1292 | /// See AbstractAttribute::updateImpl(...). |
| 1293 | ChangeStatus updateImpl(Attributor &A) override { |
| 1294 | auto CheckForNoFree = [&](Instruction &I) { |
| 1295 | ImmutableCallSite ICS(&I); |
| 1296 | if (ICS.hasFnAttr(Attribute::NoFree)) |
| 1297 | return true; |
| 1298 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1299 | const auto &NoFreeAA = |
| 1300 | A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); |
| 1301 | return NoFreeAA.isAssumedNoFree(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1302 | }; |
| 1303 | |
| 1304 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) |
| 1305 | return indicatePessimisticFixpoint(); |
| 1306 | return ChangeStatus::UNCHANGED; |
| 1307 | } |
| 1308 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1309 | /// See AbstractAttribute::getAsStr(). |
| 1310 | const std::string getAsStr() const override { |
| 1311 | return getAssumed() ? "nofree" : "may-free"; |
| 1312 | } |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1313 | }; |
| 1314 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1315 | struct AANoFreeFunction final : public AANoFreeImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1316 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1317 | |
| 1318 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1319 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1320 | }; |
| 1321 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1322 | /// NoFree attribute deduction for a call sites. |
| 1323 | using AANoFreeCallSite = AANoFreeFunction; |
| 1324 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1325 | /// ------------------------ NonNull Argument Attribute ------------------------ |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1326 | struct AANonNullImpl : AANonNull { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1327 | AANonNullImpl(const IRPosition &IRP) : AANonNull(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1328 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1329 | /// See AbstractAttribute::initialize(...). |
| 1330 | void initialize(Attributor &A) override { |
| 1331 | if (hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) |
| 1332 | indicateOptimisticFixpoint(); |
| 1333 | } |
| 1334 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1335 | /// See AbstractAttribute::getAsStr(). |
| 1336 | const std::string getAsStr() const override { |
| 1337 | return getAssumed() ? "nonnull" : "may-null"; |
| 1338 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1339 | }; |
| 1340 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1341 | /// NonNull attribute for a floating value. |
| 1342 | struct AANonNullFloating : AANonNullImpl { |
| 1343 | AANonNullFloating(const IRPosition &IRP) : AANonNullImpl(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1344 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1345 | /// See AbstractAttribute::initialize(...). |
| 1346 | void initialize(Attributor &A) override { |
| 1347 | AANonNullImpl::initialize(A); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1348 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1349 | if (isAtFixpoint()) |
| 1350 | return; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1351 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1352 | const IRPosition &IRP = getIRPosition(); |
| 1353 | const Value &V = IRP.getAssociatedValue(); |
| 1354 | const DataLayout &DL = A.getDataLayout(); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1355 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1356 | // TODO: This context sensitive query should be removed once we can do |
| 1357 | // context sensitive queries in the genericValueTraversal below. |
| 1358 | if (isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, IRP.getCtxI(), |
| 1359 | /* TODO: DT */ nullptr)) |
| 1360 | indicateOptimisticFixpoint(); |
| 1361 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1362 | |
| 1363 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1364 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1365 | const DataLayout &DL = A.getDataLayout(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1366 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1367 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, |
| 1368 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1369 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); |
| 1370 | if (!Stripped && this == &AA) { |
| 1371 | if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1372 | /* TODO: CtxI */ nullptr, |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1373 | /* TODO: DT */ nullptr)) |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1374 | T.indicatePessimisticFixpoint(); |
| 1375 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1376 | // Use abstract attribute information. |
| 1377 | const AANonNull::StateType &NS = |
| 1378 | static_cast<const AANonNull::StateType &>(AA.getState()); |
| 1379 | T ^= NS; |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1380 | } |
| 1381 | return T.isValidState(); |
| 1382 | }; |
| 1383 | |
| 1384 | StateType T; |
| 1385 | if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, |
| 1386 | T, VisitValueCB)) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1387 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1388 | |
| 1389 | return clampStateAndIndicateChange(getState(), T); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1390 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1391 | |
| 1392 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1393 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1394 | }; |
| 1395 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1396 | /// NonNull attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1397 | struct AANonNullReturned final |
| 1398 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1399 | AANonNullReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1400 | : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1401 | |
| 1402 | /// See AbstractAttribute::trackStatistics() |
| 1403 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| 1404 | }; |
| 1405 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1406 | /// NonNull attribute for function argument. |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1407 | struct AANonNullArgument final |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1408 | : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1409 | AANonNullArgument(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1410 | : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1411 | |
| 1412 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1413 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1414 | }; |
| 1415 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1416 | struct AANonNullCallSiteArgument final : AANonNullFloating { |
| 1417 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1418 | |
| 1419 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 1aac182 | 2019-08-29 01:26:09 +0000 | [diff] [blame] | 1420 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1421 | }; |
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1422 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1423 | /// NonNull attribute for a call site return position. |
| 1424 | struct AANonNullCallSiteReturned final |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1425 | : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> { |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1426 | AANonNullCallSiteReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1427 | : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP) {} |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 1428 | |
| 1429 | /// See AbstractAttribute::trackStatistics() |
| 1430 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } |
| 1431 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1432 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1433 | /// ------------------------ No-Recurse Attributes ---------------------------- |
| 1434 | |
| 1435 | struct AANoRecurseImpl : public AANoRecurse { |
| 1436 | AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} |
| 1437 | |
| 1438 | /// See AbstractAttribute::initialize(...). |
| 1439 | void initialize(Attributor &A) override { |
| 1440 | if (hasAttr({getAttrKind()})) { |
| 1441 | indicateOptimisticFixpoint(); |
| 1442 | return; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | /// See AbstractAttribute::getAsStr() |
| 1447 | const std::string getAsStr() const override { |
| 1448 | return getAssumed() ? "norecurse" : "may-recurse"; |
| 1449 | } |
| 1450 | }; |
| 1451 | |
| 1452 | struct AANoRecurseFunction final : AANoRecurseImpl { |
| 1453 | AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} |
| 1454 | |
| 1455 | /// See AbstractAttribute::updateImpl(...). |
| 1456 | ChangeStatus updateImpl(Attributor &A) override { |
| 1457 | // TODO: Implement this. |
| 1458 | return indicatePessimisticFixpoint(); |
| 1459 | } |
| 1460 | |
| 1461 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } |
| 1462 | }; |
| 1463 | |
| 1464 | using AANoRecurseCallSite = AANoRecurseFunction; |
| 1465 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1466 | /// ------------------------ Will-Return Attributes ---------------------------- |
| 1467 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1468 | // Helper function that checks whether a function has any cycle. |
| 1469 | // TODO: Replace with more efficent code |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1470 | static bool containsCycle(Function &F) { |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1471 | SmallPtrSet<BasicBlock *, 32> Visited; |
| 1472 | |
| 1473 | // Traverse BB by dfs and check whether successor is already visited. |
| 1474 | for (BasicBlock *BB : depth_first(&F)) { |
| 1475 | Visited.insert(BB); |
| 1476 | for (auto *SuccBB : successors(BB)) { |
| 1477 | if (Visited.count(SuccBB)) |
| 1478 | return true; |
| 1479 | } |
| 1480 | } |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | // Helper function that checks the function have a loop which might become an |
| 1485 | // endless loop |
| 1486 | // FIXME: Any cycle is regarded as endless loop for now. |
| 1487 | // We have to allow some patterns. |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1488 | static bool containsPossiblyEndlessLoop(Function *F) { |
| 1489 | return !F || !F->hasExactDefinition() || containsCycle(*F); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1492 | struct AAWillReturnImpl : public AAWillReturn { |
| 1493 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1494 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1495 | /// See AbstractAttribute::initialize(...). |
| 1496 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1497 | if (hasAttr({Attribute::WillReturn})) { |
| 1498 | indicateOptimisticFixpoint(); |
| 1499 | return; |
| 1500 | } |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1501 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1502 | Function *F = getAssociatedFunction(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1503 | if (containsPossiblyEndlessLoop(F)) |
| 1504 | indicatePessimisticFixpoint(); |
| 1505 | } |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1506 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1507 | /// See AbstractAttribute::updateImpl(...). |
| 1508 | ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1509 | auto CheckForWillReturn = [&](Instruction &I) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1510 | IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); |
| 1511 | const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); |
| 1512 | if (WillReturnAA.isKnownWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1513 | return true; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1514 | if (!WillReturnAA.isAssumedWillReturn()) |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1515 | return false; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1516 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); |
| 1517 | return NoRecurseAA.isAssumedNoRecurse(); |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1518 | }; |
| 1519 | |
| 1520 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) |
| 1521 | return indicatePessimisticFixpoint(); |
| 1522 | |
| 1523 | return ChangeStatus::UNCHANGED; |
| 1524 | } |
| 1525 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1526 | /// See AbstractAttribute::getAsStr() |
| 1527 | const std::string getAsStr() const override { |
| 1528 | return getAssumed() ? "willreturn" : "may-noreturn"; |
| 1529 | } |
| 1530 | }; |
| 1531 | |
| 1532 | struct AAWillReturnFunction final : AAWillReturnImpl { |
| 1533 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| 1534 | |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1535 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1536 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1537 | }; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1538 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1539 | /// WillReturn attribute deduction for a call sites. |
| 1540 | using AAWillReturnCallSite = AAWillReturnFunction; |
| 1541 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1542 | /// ------------------------ NoAlias Argument Attribute ------------------------ |
| 1543 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1544 | struct AANoAliasImpl : AANoAlias { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1545 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1546 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1547 | /// See AbstractAttribute::initialize(...). |
| 1548 | void initialize(Attributor &A) override { |
| 1549 | if (hasAttr({Attribute::NoAlias})) |
| 1550 | indicateOptimisticFixpoint(); |
| 1551 | } |
| 1552 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1553 | const std::string getAsStr() const override { |
| 1554 | return getAssumed() ? "noalias" : "may-alias"; |
| 1555 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1556 | }; |
| 1557 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1558 | /// NoAlias attribute for a floating value. |
| 1559 | struct AANoAliasFloating final : AANoAliasImpl { |
| 1560 | AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 1561 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1562 | /// See AbstractAttribute::initialize(...). |
| 1563 | void initialize(Attributor &A) override { |
| 1564 | // TODO: It isn't sound to initialize as the same with `AANoAliasImpl` |
| 1565 | // because `noalias` may not be valid in the current position. |
| 1566 | } |
| 1567 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1568 | /// See AbstractAttribute::updateImpl(...). |
| 1569 | ChangeStatus updateImpl(Attributor &A) override { |
| 1570 | // TODO: Implement this. |
| 1571 | return indicatePessimisticFixpoint(); |
| 1572 | } |
| 1573 | |
| 1574 | /// See AbstractAttribute::trackStatistics() |
| 1575 | void trackStatistics() const override { |
| 1576 | STATS_DECLTRACK_FLOATING_ATTR(noalias) |
| 1577 | } |
| 1578 | }; |
| 1579 | |
| 1580 | /// NoAlias attribute for an argument. |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1581 | struct AANoAliasArgument final |
| 1582 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { |
| 1583 | AANoAliasArgument(const IRPosition &IRP) |
| 1584 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {} |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1585 | |
| 1586 | /// See AbstractAttribute::trackStatistics() |
| 1587 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } |
| 1588 | }; |
| 1589 | |
| 1590 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { |
| 1591 | AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| 1592 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1593 | /// See AbstractAttribute::initialize(...). |
| 1594 | void initialize(Attributor &A) override { |
| Hideto Ueno | 6381b14 | 2019-08-30 10:00:32 +0000 | [diff] [blame] | 1595 | // See callsite argument attribute and callee argument attribute. |
| 1596 | ImmutableCallSite ICS(&getAnchorValue()); |
| 1597 | if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) |
| 1598 | indicateOptimisticFixpoint(); |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1601 | /// See AbstractAttribute::updateImpl(...). |
| 1602 | ChangeStatus updateImpl(Attributor &A) override { |
| 1603 | // TODO: Implement this. |
| 1604 | return indicatePessimisticFixpoint(); |
| 1605 | } |
| 1606 | |
| 1607 | /// See AbstractAttribute::trackStatistics() |
| 1608 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } |
| 1609 | }; |
| 1610 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1611 | /// NoAlias attribute for function return value. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1612 | struct AANoAliasReturned final : AANoAliasImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1613 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1614 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1615 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1616 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| 1617 | |
| 1618 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| 1619 | if (Constant *C = dyn_cast<Constant>(&RV)) |
| 1620 | if (C->isNullValue() || isa<UndefValue>(C)) |
| 1621 | return true; |
| 1622 | |
| 1623 | /// For now, we can only deduce noalias if we have call sites. |
| 1624 | /// FIXME: add more support. |
| 1625 | ImmutableCallSite ICS(&RV); |
| 1626 | if (!ICS) |
| 1627 | return false; |
| 1628 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1629 | const auto &NoAliasAA = |
| 1630 | A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS)); |
| 1631 | if (!NoAliasAA.isAssumedNoAlias()) |
| 1632 | return false; |
| Johannes Doerfert | fe6dbad | 2019-08-16 19:36:17 +0000 | [diff] [blame] | 1633 | |
| 1634 | /// FIXME: We can improve capture check in two ways: |
| 1635 | /// 1. Use the AANoCapture facilities. |
| 1636 | /// 2. Use the location of return insts for escape queries. |
| 1637 | if (PointerMayBeCaptured(&RV, /* ReturnCaptures */ false, |
| 1638 | /* StoreCaptures */ true)) |
| 1639 | return false; |
| 1640 | |
| 1641 | return true; |
| 1642 | }; |
| 1643 | |
| 1644 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) |
| 1645 | return indicatePessimisticFixpoint(); |
| 1646 | |
| 1647 | return ChangeStatus::UNCHANGED; |
| 1648 | } |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1649 | |
| 1650 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1651 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1652 | }; |
| 1653 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1654 | /// NoAlias attribute deduction for a call site return value. |
| 1655 | using AANoAliasCallSiteReturned = AANoAliasReturned; |
| 1656 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1657 | /// -------------------AAIsDead Function Attribute----------------------- |
| 1658 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1659 | struct AAIsDeadImpl : public AAIsDead { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1660 | AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {} |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1661 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1662 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1663 | const Function *F = getAssociatedFunction(); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (F->hasInternalLinkage()) |
| 1666 | return; |
| 1667 | |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1668 | if (!F || !F->hasExactDefinition()) { |
| 1669 | indicatePessimisticFixpoint(); |
| 1670 | return; |
| 1671 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1672 | |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 1673 | exploreFromEntry(A, F); |
| 1674 | } |
| 1675 | |
| 1676 | void exploreFromEntry(Attributor &A, const Function *F) { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1677 | ToBeExploredPaths.insert(&(F->getEntryBlock().front())); |
| 1678 | AssumedLiveBlocks.insert(&(F->getEntryBlock())); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 1679 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1680 | for (size_t i = 0; i < ToBeExploredPaths.size(); ++i) |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1681 | if (const Instruction *NextNoReturnI = |
| 1682 | findNextNoReturn(A, ToBeExploredPaths[i])) |
| 1683 | NoReturnCalls.insert(NextNoReturnI); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1686 | /// Find the next assumed noreturn instruction in the block of \p I starting |
| 1687 | /// from, thus including, \p I. |
| 1688 | /// |
| 1689 | /// The caller is responsible to monitor the ToBeExploredPaths set as new |
| 1690 | /// instructions discovered in other basic block will be placed in there. |
| 1691 | /// |
| 1692 | /// \returns The next assumed noreturn instructions in the block of \p I |
| 1693 | /// starting from, thus including, \p I. |
| 1694 | const Instruction *findNextNoReturn(Attributor &A, const Instruction *I); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1695 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1696 | /// See AbstractAttribute::getAsStr(). |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1697 | const std::string getAsStr() const override { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1698 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1699 | std::to_string(getAssociatedFunction()->size()) + "][#NRI " + |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1700 | std::to_string(NoReturnCalls.size()) + "]"; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /// See AbstractAttribute::manifest(...). |
| 1704 | ChangeStatus manifest(Attributor &A) override { |
| 1705 | assert(getState().isValidState() && |
| 1706 | "Attempted to manifest an invalid state!"); |
| 1707 | |
| 1708 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 1709 | Function &F = *getAssociatedFunction(); |
| 1710 | |
| 1711 | if (AssumedLiveBlocks.empty()) { |
| 1712 | F.replaceAllUsesWith(UndefValue::get(F.getType())); |
| 1713 | return ChangeStatus::CHANGED; |
| 1714 | } |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1715 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1716 | // Flag to determine if we can change an invoke to a call assuming the |
| 1717 | // callee is nounwind. This is not possible if the personality of the |
| 1718 | // function allows to catch asynchronous exceptions. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1719 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1720 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1721 | for (const Instruction *NRC : NoReturnCalls) { |
| 1722 | Instruction *I = const_cast<Instruction *>(NRC); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1723 | BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1724 | Instruction *SplitPos = I->getNextNode(); |
| Johannes Doerfert | d410805 | 2019-08-21 20:56:41 +0000 | [diff] [blame] | 1725 | // TODO: mark stuff before unreachable instructions as dead. |
| 1726 | if (isa_and_nonnull<UnreachableInst>(SplitPos)) |
| 1727 | continue; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1728 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1729 | if (auto *II = dyn_cast<InvokeInst>(I)) { |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 1730 | // If we keep the invoke the split position is at the beginning of the |
| 1731 | // normal desitination block (it invokes a noreturn function after all). |
| 1732 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 1733 | SplitPos = &NormalDestBB->front(); |
| 1734 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1735 | /// Invoke is replaced with a call and unreachable is placed after it if |
| 1736 | /// the callee is nounwind and noreturn. Otherwise, we keep the invoke |
| 1737 | /// and only place an unreachable in the normal successor. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1738 | if (Invoke2CallAllowed) { |
| Michael Liao | a99086d | 2019-08-20 21:02:31 +0000 | [diff] [blame] | 1739 | if (II->getCalledFunction()) { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1740 | const IRPosition &IPos = IRPosition::callsite_function(*II); |
| 1741 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); |
| 1742 | if (AANoUnw.isAssumedNoUnwind()) { |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1743 | LLVM_DEBUG(dbgs() |
| 1744 | << "[AAIsDead] Replace invoke with call inst\n"); |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 1745 | // We do not need an invoke (II) but instead want a call followed |
| 1746 | // by an unreachable. However, we do not remove II as other |
| 1747 | // abstract attributes might have it cached as part of their |
| 1748 | // results. Given that we modify the CFG anyway, we simply keep II |
| 1749 | // around but in a new dead block. To avoid II being live through |
| 1750 | // a different edge we have to ensure the block we place it in is |
| 1751 | // only reached from the current block of II and then not reached |
| 1752 | // at all when we insert the unreachable. |
| 1753 | SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); |
| 1754 | CallInst *CI = createCallMatchingInvoke(II); |
| 1755 | CI->insertBefore(II); |
| 1756 | CI->takeName(II); |
| 1757 | II->replaceAllUsesWith(CI); |
| 1758 | SplitPos = CI->getNextNode(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1759 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1760 | } |
| 1761 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 1764 | BB = SplitPos->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1765 | SplitBlock(BB, SplitPos); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1766 | changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false); |
| 1767 | HasChanged = ChangeStatus::CHANGED; |
| 1768 | } |
| 1769 | |
| 1770 | return HasChanged; |
| 1771 | } |
| 1772 | |
| 1773 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1774 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1775 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1776 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1777 | bool isAssumedDead(const BasicBlock *BB) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1778 | assert(BB->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1779 | "BB must be in the same anchor scope function."); |
| 1780 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1781 | if (!getAssumed()) |
| 1782 | return false; |
| 1783 | return !AssumedLiveBlocks.count(BB); |
| 1784 | } |
| 1785 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1786 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1787 | bool isKnownDead(const BasicBlock *BB) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1788 | return getKnown() && isAssumedDead(BB); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1791 | /// See AAIsDead::isAssumed(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1792 | bool isAssumedDead(const Instruction *I) const override { |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1793 | assert(I->getParent()->getParent() == getAssociatedFunction() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1794 | "Instruction must be in the same anchor scope function."); |
| 1795 | |
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 1796 | if (!getAssumed()) |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1797 | return false; |
| 1798 | |
| 1799 | // If it is not in AssumedLiveBlocks then it for sure dead. |
| 1800 | // Otherwise, it can still be after noreturn call in a live block. |
| 1801 | if (!AssumedLiveBlocks.count(I->getParent())) |
| 1802 | return true; |
| 1803 | |
| 1804 | // If it is not after a noreturn call, than it is live. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1805 | return isAfterNoReturn(I); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | /// See AAIsDead::isKnownDead(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1809 | bool isKnownDead(const Instruction *I) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1810 | return getKnown() && isAssumedDead(I); |
| 1811 | } |
| 1812 | |
| 1813 | /// Check if instruction is after noreturn call, in other words, assumed dead. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1814 | bool isAfterNoReturn(const Instruction *I) const; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1815 | |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1816 | /// Determine if \p F might catch asynchronous exceptions. |
| 1817 | static bool mayCatchAsynchronousExceptions(const Function &F) { |
| 1818 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); |
| 1819 | } |
| 1820 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1821 | /// Collection of to be explored paths. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1822 | SmallSetVector<const Instruction *, 8> ToBeExploredPaths; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1823 | |
| 1824 | /// Collection of all assumed live BasicBlocks. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1825 | DenseSet<const BasicBlock *> AssumedLiveBlocks; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1826 | |
| 1827 | /// Collection of calls with noreturn attribute, assumed or knwon. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1828 | SmallSetVector<const Instruction *, 4> NoReturnCalls; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1829 | }; |
| 1830 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1831 | struct AAIsDeadFunction final : public AAIsDeadImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1832 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1833 | |
| 1834 | /// See AbstractAttribute::trackStatistics() |
| 1835 | void trackStatistics() const override { |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 1836 | STATS_DECL(DeadInternalFunction, Function, |
| 1837 | "Number of internal functions classified as dead (no live callsite)"); |
| 1838 | BUILD_STAT_NAME(DeadInternalFunction, Function) += |
| 1839 | (getAssociatedFunction()->hasInternalLinkage() && |
| 1840 | AssumedLiveBlocks.empty()) |
| 1841 | ? 1 |
| 1842 | : 0; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1843 | STATS_DECL(DeadBlocks, Function, |
| 1844 | "Number of basic blocks classified as dead"); |
| 1845 | BUILD_STAT_NAME(DeadBlocks, Function) += |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1846 | getAssociatedFunction()->size() - AssumedLiveBlocks.size(); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1847 | STATS_DECL(PartiallyDeadBlocks, Function, |
| 1848 | "Number of basic blocks classified as partially dead"); |
| 1849 | BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size(); |
| 1850 | } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1851 | }; |
| 1852 | |
| 1853 | bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1854 | const Instruction *PrevI = I->getPrevNode(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1855 | while (PrevI) { |
| 1856 | if (NoReturnCalls.count(PrevI)) |
| 1857 | return true; |
| 1858 | PrevI = PrevI->getPrevNode(); |
| 1859 | } |
| 1860 | return false; |
| 1861 | } |
| 1862 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1863 | const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A, |
| 1864 | const Instruction *I) { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1865 | const BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1866 | const Function &F = *BB->getParent(); |
| 1867 | |
| 1868 | // Flag to determine if we can change an invoke to a call assuming the callee |
| 1869 | // is nounwind. This is not possible if the personality of the function allows |
| 1870 | // to catch asynchronous exceptions. |
| 1871 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1872 | |
| 1873 | // TODO: We should have a function that determines if an "edge" is dead. |
| 1874 | // Edges could be from an instruction to the next or from a terminator |
| 1875 | // to the successor. For now, we need to special case the unwind block |
| 1876 | // of InvokeInst below. |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1877 | |
| 1878 | while (I) { |
| 1879 | ImmutableCallSite ICS(I); |
| 1880 | |
| 1881 | if (ICS) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1882 | const IRPosition &IPos = IRPosition::callsite_function(ICS); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1883 | // Regarless of the no-return property of an invoke instruction we only |
| 1884 | // learn that the regular successor is not reachable through this |
| 1885 | // instruction but the unwind block might still be. |
| 1886 | if (auto *Invoke = dyn_cast<InvokeInst>(I)) { |
| 1887 | // Use nounwind to justify the unwind block is dead as well. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1888 | const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); |
| 1889 | if (!Invoke2CallAllowed || !AANoUnw.isAssumedNoUnwind()) { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1890 | AssumedLiveBlocks.insert(Invoke->getUnwindDest()); |
| 1891 | ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front()); |
| 1892 | } |
| 1893 | } |
| 1894 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 1895 | const auto &NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos); |
| 1896 | if (NoReturnAA.isAssumedNoReturn()) |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1897 | return I; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | I = I->getNextNode(); |
| 1901 | } |
| 1902 | |
| 1903 | // get new paths (reachable blocks). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1904 | for (const BasicBlock *SuccBB : successors(BB)) { |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1905 | AssumedLiveBlocks.insert(SuccBB); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1906 | ToBeExploredPaths.insert(&SuccBB->front()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1909 | // No noreturn instruction found. |
| 1910 | return nullptr; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1913 | ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 1914 | const Function *F = getAssociatedFunction(); |
| 1915 | ChangeStatus Status = ChangeStatus::UNCHANGED; |
| 1916 | |
| 1917 | if (F->hasInternalLinkage() && AssumedLiveBlocks.empty()) { |
| 1918 | auto CallSiteCheck = [&](CallSite) { return false; }; |
| 1919 | |
| 1920 | // All callsites of F are dead. |
| 1921 | if (A.checkForAllCallSites(CallSiteCheck, *this, true)) |
| 1922 | return ChangeStatus::UNCHANGED; |
| 1923 | |
| 1924 | // There exists at least one live call site, so we explore the function. |
| 1925 | Status = ChangeStatus::CHANGED; |
| 1926 | |
| 1927 | exploreFromEntry(A, F); |
| 1928 | } |
| 1929 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1930 | // Temporary collection to iterate over existing noreturn instructions. This |
| 1931 | // will alow easier modification of NoReturnCalls collection |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1932 | SmallVector<const Instruction *, 8> NoReturnChanged; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1933 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1934 | for (const Instruction *I : NoReturnCalls) |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1935 | NoReturnChanged.push_back(I); |
| 1936 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1937 | for (const Instruction *I : NoReturnChanged) { |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1938 | size_t Size = ToBeExploredPaths.size(); |
| 1939 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1940 | const Instruction *NextNoReturnI = findNextNoReturn(A, I); |
| 1941 | if (NextNoReturnI != I) { |
| 1942 | Status = ChangeStatus::CHANGED; |
| 1943 | NoReturnCalls.remove(I); |
| 1944 | if (NextNoReturnI) |
| 1945 | NoReturnCalls.insert(NextNoReturnI); |
| 1946 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1947 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1948 | // Explore new paths. |
| 1949 | while (Size != ToBeExploredPaths.size()) { |
| 1950 | Status = ChangeStatus::CHANGED; |
| 1951 | if (const Instruction *NextNoReturnI = |
| 1952 | findNextNoReturn(A, ToBeExploredPaths[Size++])) |
| 1953 | NoReturnCalls.insert(NextNoReturnI); |
| 1954 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1957 | LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: " |
| 1958 | << AssumedLiveBlocks.size() << " Total number of blocks: " |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1959 | << getAssociatedFunction()->size() << "\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1960 | |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 1961 | // If we know everything is live there is no need to query for liveness. |
| 1962 | if (NoReturnCalls.empty() && |
| Johannes Doerfert | 6dedc78 | 2019-08-16 21:31:11 +0000 | [diff] [blame] | 1963 | getAssociatedFunction()->size() == AssumedLiveBlocks.size()) { |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 1964 | // Indicating a pessimistic fixpoint will cause the state to be "invalid" |
| 1965 | // which will cause the Attributor to not return the AAIsDead on request, |
| 1966 | // which will prevent us from querying isAssumedDead(). |
| 1967 | indicatePessimisticFixpoint(); |
| 1968 | assert(!isValidState() && "Expected an invalid state!"); |
| Johannes Doerfert | 62a9c1d | 2019-08-29 01:26:58 +0000 | [diff] [blame] | 1969 | Status = ChangeStatus::CHANGED; |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1972 | return Status; |
| 1973 | } |
| 1974 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1975 | /// Liveness information for a call sites. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 1976 | struct AAIsDeadCallSite final : AAIsDeadImpl { |
| 1977 | AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadImpl(IRP) {} |
| 1978 | |
| 1979 | /// See AbstractAttribute::initialize(...). |
| 1980 | void initialize(Attributor &A) override { |
| 1981 | // TODO: Once we have call site specific value information we can provide |
| 1982 | // call site specific liveness liveness information and then it makes |
| 1983 | // sense to specialize attributes for call sites instead of |
| 1984 | // redirecting requests to the callee. |
| 1985 | llvm_unreachable("Abstract attributes for liveness are not " |
| 1986 | "supported for call sites yet!"); |
| 1987 | } |
| 1988 | |
| 1989 | /// See AbstractAttribute::updateImpl(...). |
| 1990 | ChangeStatus updateImpl(Attributor &A) override { |
| 1991 | return indicatePessimisticFixpoint(); |
| 1992 | } |
| 1993 | |
| 1994 | /// See AbstractAttribute::trackStatistics() |
| 1995 | void trackStatistics() const override {} |
| 1996 | }; |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 1997 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1998 | /// -------------------- Dereferenceable Argument Attribute -------------------- |
| 1999 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2000 | template <> |
| 2001 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, |
| 2002 | const DerefState &R) { |
| 2003 | ChangeStatus CS0 = clampStateAndIndicateChange<IntegerState>( |
| 2004 | S.DerefBytesState, R.DerefBytesState); |
| 2005 | ChangeStatus CS1 = |
| 2006 | clampStateAndIndicateChange<IntegerState>(S.GlobalState, R.GlobalState); |
| 2007 | return CS0 | CS1; |
| 2008 | } |
| 2009 | |
| Hideto Ueno | 70576ca | 2019-08-22 14:18:29 +0000 | [diff] [blame] | 2010 | struct AADereferenceableImpl : AADereferenceable { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2011 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2012 | using StateType = DerefState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2013 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 2014 | void initialize(Attributor &A) override { |
| 2015 | SmallVector<Attribute, 4> Attrs; |
| 2016 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, |
| 2017 | Attrs); |
| 2018 | for (const Attribute &Attr : Attrs) |
| 2019 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); |
| 2020 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2021 | NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition()); |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2024 | /// See AbstractAttribute::getState() |
| 2025 | /// { |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2026 | StateType &getState() override { return *this; } |
| 2027 | const StateType &getState() const override { return *this; } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2028 | /// } |
| 2029 | |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 2030 | void getDeducedAttributes(LLVMContext &Ctx, |
| 2031 | SmallVectorImpl<Attribute> &Attrs) const override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2032 | // TODO: Add *_globally support |
| 2033 | if (isAssumedNonNull()) |
| 2034 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( |
| 2035 | Ctx, getAssumedDereferenceableBytes())); |
| 2036 | else |
| 2037 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( |
| 2038 | Ctx, getAssumedDereferenceableBytes())); |
| 2039 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2040 | |
| 2041 | /// See AbstractAttribute::getAsStr(). |
| 2042 | const std::string getAsStr() const override { |
| 2043 | if (!getAssumedDereferenceableBytes()) |
| 2044 | return "unknown-dereferenceable"; |
| 2045 | return std::string("dereferenceable") + |
| 2046 | (isAssumedNonNull() ? "" : "_or_null") + |
| 2047 | (isAssumedGlobal() ? "_globally" : "") + "<" + |
| 2048 | std::to_string(getKnownDereferenceableBytes()) + "-" + |
| 2049 | std::to_string(getAssumedDereferenceableBytes()) + ">"; |
| 2050 | } |
| 2051 | }; |
| 2052 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2053 | /// Dereferenceable attribute for a floating value. |
| 2054 | struct AADereferenceableFloating : AADereferenceableImpl { |
| 2055 | AADereferenceableFloating(const IRPosition &IRP) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2056 | : AADereferenceableImpl(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2057 | |
| 2058 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2059 | ChangeStatus updateImpl(Attributor &A) override { |
| 2060 | const DataLayout &DL = A.getDataLayout(); |
| 2061 | |
| 2062 | auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { |
| 2063 | unsigned IdxWidth = |
| 2064 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); |
| 2065 | APInt Offset(IdxWidth, 0); |
| 2066 | const Value *Base = |
| 2067 | V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 2068 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2069 | const auto &AA = |
| 2070 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2071 | int64_t DerefBytes = 0; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2072 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2073 | // Use IR information if we did not strip anything. |
| 2074 | // TODO: track globally. |
| 2075 | bool CanBeNull; |
| 2076 | DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); |
| 2077 | T.GlobalState.indicatePessimisticFixpoint(); |
| 2078 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2079 | const DerefState &DS = static_cast<const DerefState &>(AA.getState()); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2080 | DerefBytes = DS.DerefBytesState.getAssumed(); |
| 2081 | T.GlobalState &= DS.GlobalState; |
| 2082 | } |
| 2083 | |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 2084 | // For now we do not try to "increase" dereferenceability due to negative |
| 2085 | // indices as we first have to come up with code to deal with loops and |
| 2086 | // for overflows of the dereferenceable bytes. |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2087 | int64_t OffsetSExt = Offset.getSExtValue(); |
| 2088 | if (OffsetSExt < 0) |
| Johannes Doerfert | 2f2d7c3 | 2019-08-23 15:45:46 +0000 | [diff] [blame] | 2089 | Offset = 0; |
| 2090 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2091 | T.takeAssumedDerefBytesMinimum( |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2092 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2093 | |
| Johannes Doerfert | 785fad3 | 2019-08-23 17:29:23 +0000 | [diff] [blame] | 2094 | if (this == &AA) { |
| 2095 | if (!Stripped) { |
| 2096 | // If nothing was stripped IR information is all we got. |
| 2097 | T.takeKnownDerefBytesMaximum( |
| 2098 | std::max(int64_t(0), DerefBytes - OffsetSExt)); |
| 2099 | T.indicatePessimisticFixpoint(); |
| 2100 | } else if (OffsetSExt > 0) { |
| 2101 | // If something was stripped but there is circular reasoning we look |
| 2102 | // for the offset. If it is positive we basically decrease the |
| 2103 | // dereferenceable bytes in a circluar loop now, which will simply |
| 2104 | // drive them down to the known value in a very slow way which we |
| 2105 | // can accelerate. |
| 2106 | T.indicatePessimisticFixpoint(); |
| 2107 | } |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | return T.isValidState(); |
| 2111 | }; |
| 2112 | |
| 2113 | DerefState T; |
| 2114 | if (!genericValueTraversal<AADereferenceable, DerefState>( |
| 2115 | A, getIRPosition(), *this, T, VisitValueCB)) |
| 2116 | return indicatePessimisticFixpoint(); |
| 2117 | |
| 2118 | return clampStateAndIndicateChange(getState(), T); |
| 2119 | } |
| 2120 | |
| 2121 | /// See AbstractAttribute::trackStatistics() |
| 2122 | void trackStatistics() const override { |
| 2123 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) |
| 2124 | } |
| 2125 | }; |
| 2126 | |
| 2127 | /// Dereferenceable attribute for a return value. |
| 2128 | struct AADereferenceableReturned final |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2129 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, |
| 2130 | DerefState> { |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2131 | AADereferenceableReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2132 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, |
| 2133 | DerefState>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2134 | |
| 2135 | /// See AbstractAttribute::trackStatistics() |
| 2136 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2137 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2138 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2139 | }; |
| 2140 | |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2141 | /// Dereferenceable attribute for an argument |
| 2142 | struct AADereferenceableArgument final |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2143 | : AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl, |
| 2144 | DerefState> { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2145 | AADereferenceableArgument(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2146 | : AAArgumentFromCallSiteArguments<AADereferenceable, |
| 2147 | AADereferenceableImpl, DerefState>( |
| 2148 | IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2149 | |
| 2150 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2151 | void trackStatistics() const override{ |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 2152 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) |
| 2153 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2154 | }; |
| 2155 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2156 | /// Dereferenceable attribute for a call site argument. |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2157 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2158 | AADereferenceableCallSiteArgument(const IRPosition &IRP) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2159 | : AADereferenceableFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2160 | |
| 2161 | /// See AbstractAttribute::trackStatistics() |
| 2162 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2163 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2164 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2165 | }; |
| 2166 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2167 | /// Dereferenceable attribute deduction for a call site return value. |
| 2168 | using AADereferenceableCallSiteReturned = AADereferenceableReturned; |
| 2169 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2170 | // ------------------------ Align Argument Attribute ------------------------ |
| 2171 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2172 | struct AAAlignImpl : AAAlign { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2173 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2174 | |
| 2175 | // Max alignemnt value allowed in IR |
| 2176 | static const unsigned MAX_ALIGN = 1U << 29; |
| 2177 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2178 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2179 | void initialize(Attributor &A) override { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2180 | takeAssumedMinimum(MAX_ALIGN); |
| 2181 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2182 | SmallVector<Attribute, 4> Attrs; |
| 2183 | getAttrs({Attribute::Alignment}, Attrs); |
| 2184 | for (const Attribute &Attr : Attrs) |
| 2185 | takeKnownMaximum(Attr.getValueAsInt()); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2188 | /// See AbstractAttribute::manifest(...). |
| 2189 | ChangeStatus manifest(Attributor &A) override { |
| 2190 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 2191 | |
| 2192 | // Check for users that allow alignment annotations. |
| 2193 | Value &AnchorVal = getIRPosition().getAnchorValue(); |
| 2194 | for (const Use &U : AnchorVal.uses()) { |
| 2195 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { |
| 2196 | if (SI->getPointerOperand() == &AnchorVal) |
| 2197 | if (SI->getAlignment() < getAssumedAlign()) { |
| 2198 | STATS_DECLTRACK(AAAlign, Store, |
| 2199 | "Number of times alignemnt added to a store"); |
| 2200 | SI->setAlignment(getAssumedAlign()); |
| 2201 | Changed = ChangeStatus::CHANGED; |
| 2202 | } |
| 2203 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { |
| 2204 | if (LI->getPointerOperand() == &AnchorVal) |
| 2205 | if (LI->getAlignment() < getAssumedAlign()) { |
| 2206 | LI->setAlignment(getAssumedAlign()); |
| 2207 | STATS_DECLTRACK(AAAlign, Load, |
| 2208 | "Number of times alignemnt added to a load"); |
| 2209 | Changed = ChangeStatus::CHANGED; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame^] | 2214 | return AAAlign::manifest(A) | Changed; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| Johannes Doerfert | 81df452 | 2019-08-30 15:22:28 +0000 | [diff] [blame^] | 2217 | // TODO: Provide a helper to determine the implied ABI alignment and check in |
| 2218 | // the existing manifest method and a new one for AAAlignImpl that value |
| 2219 | // to avoid making the alignment explicit if it did not improve. |
| 2220 | |
| 2221 | /// See AbstractAttribute::getDeducedAttributes |
| 2222 | virtual void |
| 2223 | getDeducedAttributes(LLVMContext &Ctx, |
| 2224 | SmallVectorImpl<Attribute> &Attrs) const override { |
| 2225 | if (getAssumedAlign() > 1) |
| 2226 | Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign())); |
| 2227 | } |
| 2228 | |
| 2229 | /// See AbstractAttribute::getAsStr(). |
| 2230 | const std::string getAsStr() const override { |
| 2231 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + |
| 2232 | "-" + std::to_string(getAssumedAlign()) + ">") |
| 2233 | : "unknown-align"; |
| 2234 | } |
| 2235 | }; |
| 2236 | |
| 2237 | /// Align attribute for a floating value. |
| 2238 | struct AAAlignFloating : AAAlignImpl { |
| 2239 | AAAlignFloating(const IRPosition &IRP) : AAAlignImpl(IRP) {} |
| 2240 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2241 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2242 | ChangeStatus updateImpl(Attributor &A) override { |
| 2243 | const DataLayout &DL = A.getDataLayout(); |
| 2244 | |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2245 | auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, |
| 2246 | bool Stripped) -> bool { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2247 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); |
| 2248 | if (!Stripped && this == &AA) { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2249 | // Use only IR information if we did not strip anything. |
| 2250 | T.takeKnownMaximum(V.getPointerAlignment(DL)); |
| 2251 | T.indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2252 | } else { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2253 | // Use abstract attribute information. |
| 2254 | const AAAlign::StateType &DS = |
| 2255 | static_cast<const AAAlign::StateType &>(AA.getState()); |
| 2256 | T ^= DS; |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2257 | } |
| Johannes Doerfert | b9b8791 | 2019-08-20 06:02:39 +0000 | [diff] [blame] | 2258 | return T.isValidState(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2259 | }; |
| 2260 | |
| 2261 | StateType T; |
| 2262 | if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, |
| 2263 | VisitValueCB)) |
| Johannes Doerfert | cfcca1a | 2019-08-20 06:08:35 +0000 | [diff] [blame] | 2264 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2265 | |
| Johannes Doerfert | 028b2aa | 2019-08-20 05:57:01 +0000 | [diff] [blame] | 2266 | // TODO: If we know we visited all incoming values, thus no are assumed |
| 2267 | // dead, we can take the known information from the state T. |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2268 | return clampStateAndIndicateChange(getState(), T); |
| 2269 | } |
| 2270 | |
| 2271 | /// See AbstractAttribute::trackStatistics() |
| 2272 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } |
| 2273 | }; |
| 2274 | |
| 2275 | /// Align attribute for function return value. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2276 | struct AAAlignReturned final |
| 2277 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2278 | AAAlignReturned(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2279 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2280 | |
| 2281 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2282 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2283 | }; |
| 2284 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2285 | /// Align attribute for function argument. |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2286 | struct AAAlignArgument final |
| 2287 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> { |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2288 | AAAlignArgument(const IRPosition &IRP) |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2289 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2290 | |
| 2291 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 169af99 | 2019-08-20 06:09:56 +0000 | [diff] [blame] | 2292 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2293 | }; |
| 2294 | |
| Johannes Doerfert | 234eda5 | 2019-08-16 19:51:23 +0000 | [diff] [blame] | 2295 | struct AAAlignCallSiteArgument final : AAAlignFloating { |
| 2296 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2297 | |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2298 | /// See AbstractAttribute::manifest(...). |
| 2299 | ChangeStatus manifest(Attributor &A) override { |
| 2300 | return AAAlignImpl::manifest(A); |
| 2301 | } |
| 2302 | |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2303 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2304 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2305 | }; |
| 2306 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2307 | /// Align attribute deduction for a call site return value. |
| 2308 | using AAAlignCallSiteReturned = AAAlignReturned; |
| 2309 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2310 | /// ------------------ Function No-Return Attribute ---------------------------- |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2311 | struct AANoReturnImpl : public AANoReturn { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2312 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2313 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2314 | /// See AbstractAttribute::getAsStr(). |
| 2315 | const std::string getAsStr() const override { |
| 2316 | return getAssumed() ? "noreturn" : "may-return"; |
| 2317 | } |
| 2318 | |
| 2319 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2320 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2321 | if (hasAttr({getAttrKind()})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2322 | indicateOptimisticFixpoint(); |
| 2323 | } |
| 2324 | |
| 2325 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2326 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2327 | auto CheckForNoReturn = [](Instruction &) { return false; }; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2328 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2329 | {(unsigned)Instruction::Ret})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2330 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2331 | return ChangeStatus::UNCHANGED; |
| 2332 | } |
| 2333 | }; |
| 2334 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2335 | struct AANoReturnFunction final : AANoReturnImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2336 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2337 | |
| 2338 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2339 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2340 | }; |
| 2341 | |
| Johannes Doerfert | 66cf87e | 2019-08-16 19:49:00 +0000 | [diff] [blame] | 2342 | /// NoReturn attribute deduction for a call sites. |
| 2343 | using AANoReturnCallSite = AANoReturnFunction; |
| 2344 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2345 | /// ---------------------------------------------------------------------------- |
| 2346 | /// Attributor |
| 2347 | /// ---------------------------------------------------------------------------- |
| 2348 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2349 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, |
| 2350 | const AAIsDead *LivenessAA) { |
| 2351 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); |
| 2352 | if (!CtxI) |
| 2353 | return false; |
| 2354 | |
| 2355 | if (!LivenessAA) |
| 2356 | LivenessAA = |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2357 | &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), |
| 2358 | /* TrackDependence */ false); |
| Stefan Stipanovic | 26121ae | 2019-08-20 23:16:57 +0000 | [diff] [blame] | 2359 | |
| 2360 | // Don't check liveness for AAIsDead. |
| 2361 | if (&AA == LivenessAA) |
| 2362 | return false; |
| 2363 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2364 | if (!LivenessAA->isAssumedDead(CtxI)) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2365 | return false; |
| 2366 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2367 | // We actually used liveness information so we have to record a dependence. |
| 2368 | recordDependence(*LivenessAA, AA); |
| 2369 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2370 | return true; |
| 2371 | } |
| 2372 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2373 | bool Attributor::checkForAllCallSites(const function_ref<bool(CallSite)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2374 | const AbstractAttribute &QueryingAA, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2375 | bool RequireAllCallSites) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2376 | // We can try to determine information from |
| 2377 | // the call sites. However, this is only possible all call sites are known, |
| 2378 | // hence the function has internal linkage. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2379 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2380 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2381 | if (!AssociatedFunction) |
| 2382 | return false; |
| 2383 | |
| 2384 | if (RequireAllCallSites && !AssociatedFunction->hasInternalLinkage()) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2385 | LLVM_DEBUG( |
| 2386 | dbgs() |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 2387 | << "[Attributor] Function " << AssociatedFunction->getName() |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2388 | << " has no internal linkage, hence not all call sites are known\n"); |
| 2389 | return false; |
| 2390 | } |
| 2391 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2392 | for (const Use &U : AssociatedFunction->uses()) { |
| Johannes Doerfert | d98f975 | 2019-08-21 21:48:56 +0000 | [diff] [blame] | 2393 | Instruction *I = dyn_cast<Instruction>(U.getUser()); |
| 2394 | // TODO: Deal with abstract call sites here. |
| 2395 | if (!I) |
| 2396 | return false; |
| 2397 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2398 | Function *Caller = I->getFunction(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2399 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2400 | const auto &LivenessAA = getAAFor<AAIsDead>( |
| 2401 | QueryingAA, IRPosition::function(*Caller), /* TrackDependence */ false); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2402 | |
| 2403 | // Skip dead calls. |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2404 | if (LivenessAA.isAssumedDead(I)) { |
| 2405 | // We actually used liveness information so we have to record a |
| 2406 | // dependence. |
| 2407 | recordDependence(LivenessAA, QueryingAA); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2408 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2409 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2410 | |
| 2411 | CallSite CS(U.getUser()); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2412 | if (!CS || !CS.isCallee(&U) || !CS.getCaller()->hasExactDefinition()) { |
| 2413 | if (!RequireAllCallSites) |
| 2414 | continue; |
| 2415 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 2416 | LLVM_DEBUG(dbgs() << "[Attributor] User " << *U.getUser() |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2417 | << " is an invalid use of " |
| 2418 | << AssociatedFunction->getName() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2419 | return false; |
| 2420 | } |
| 2421 | |
| 2422 | if (Pred(CS)) |
| 2423 | continue; |
| 2424 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame] | 2425 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2426 | << *CS.getInstruction() << "\n"); |
| 2427 | return false; |
| 2428 | } |
| 2429 | |
| 2430 | return true; |
| 2431 | } |
| 2432 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2433 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 2434 | const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2435 | &Pred, |
| 2436 | const AbstractAttribute &QueryingAA) { |
| 2437 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2438 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2439 | // Since we need to provide return instructions we have to have an exact |
| 2440 | // definition. |
| 2441 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2442 | if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2443 | return false; |
| 2444 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2445 | // If this is a call site query we use the call site specific return values |
| 2446 | // and liveness information. |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2447 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 2448 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2449 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2450 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2451 | return false; |
| 2452 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2453 | return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | bool Attributor::checkForAllReturnedValues( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2457 | const function_ref<bool(Value &)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2458 | const AbstractAttribute &QueryingAA) { |
| 2459 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2460 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2461 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2462 | if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2463 | return false; |
| 2464 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2465 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 2466 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2467 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2468 | if (!AARetVal.getState().isValidState()) |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2469 | return false; |
| 2470 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2471 | return AARetVal.checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 695089e | 2019-08-23 15:23:49 +0000 | [diff] [blame] | 2472 | [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 2473 | return Pred(RV); |
| 2474 | }); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2477 | bool Attributor::checkForAllInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2478 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2479 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2480 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2481 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2482 | // Since we need to provide instructions we have to have an exact definition. |
| 2483 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2484 | if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) |
| 2485 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2486 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2487 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 2488 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2489 | const auto &LivenessAA = |
| 2490 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| 2491 | bool AnyDead = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2492 | |
| 2493 | auto &OpcodeInstMap = |
| 2494 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2495 | for (unsigned Opcode : Opcodes) { |
| 2496 | for (Instruction *I : OpcodeInstMap[Opcode]) { |
| 2497 | // Skip dead instructions. |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2498 | if (LivenessAA.isAssumedDead(I)) { |
| 2499 | AnyDead = true; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2500 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2501 | } |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2502 | |
| 2503 | if (!Pred(*I)) |
| 2504 | return false; |
| 2505 | } |
| 2506 | } |
| 2507 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2508 | // If we actually used liveness information so we have to record a dependence. |
| 2509 | if (AnyDead) |
| 2510 | recordDependence(LivenessAA, QueryingAA); |
| 2511 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2512 | return true; |
| 2513 | } |
| 2514 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2515 | bool Attributor::checkForAllReadWriteInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2516 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2517 | AbstractAttribute &QueryingAA) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2518 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2519 | const Function *AssociatedFunction = |
| 2520 | QueryingAA.getIRPosition().getAssociatedFunction(); |
| 2521 | if (!AssociatedFunction) |
| 2522 | return false; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2523 | |
| Johannes Doerfert | 07a5c12 | 2019-08-28 14:09:14 +0000 | [diff] [blame] | 2524 | // TODO: use the function scope once we have call site AAReturnedValues. |
| 2525 | const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); |
| 2526 | const auto &LivenessAA = |
| 2527 | getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2528 | bool AnyDead = false; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2529 | |
| 2530 | for (Instruction *I : |
| 2531 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2532 | // Skip dead instructions. |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2533 | if (LivenessAA.isAssumedDead(I)) { |
| 2534 | AnyDead = true; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2535 | continue; |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2536 | } |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2537 | |
| 2538 | if (!Pred(*I)) |
| 2539 | return false; |
| 2540 | } |
| 2541 | |
| Johannes Doerfert | 19b0043 | 2019-08-26 17:48:05 +0000 | [diff] [blame] | 2542 | // If we actually used liveness information so we have to record a dependence. |
| 2543 | if (AnyDead) |
| 2544 | recordDependence(LivenessAA, QueryingAA); |
| 2545 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2546 | return true; |
| 2547 | } |
| 2548 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2549 | ChangeStatus Attributor::run() { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2550 | // Initialize all abstract attributes, allow new ones to be created. |
| 2551 | for (unsigned u = 0; u < AllAbstractAttributes.size(); u++) |
| 2552 | AllAbstractAttributes[u]->initialize(*this); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2553 | |
| 2554 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " |
| 2555 | << AllAbstractAttributes.size() |
| 2556 | << " abstract attributes.\n"); |
| 2557 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2558 | // Now that all abstract attributes are collected and initialized we start |
| 2559 | // the abstract analysis. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2560 | |
| 2561 | unsigned IterationCounter = 1; |
| 2562 | |
| 2563 | SmallVector<AbstractAttribute *, 64> ChangedAAs; |
| 2564 | SetVector<AbstractAttribute *> Worklist; |
| 2565 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); |
| 2566 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 2567 | bool RecomputeDependences = false; |
| 2568 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2569 | do { |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2570 | // Remember the size to determine new attributes. |
| 2571 | size_t NumAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2572 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter |
| 2573 | << ", Worklist size: " << Worklist.size() << "\n"); |
| 2574 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 2575 | // If dependences (=QueryMap) are recomputed we have to look at all abstract |
| 2576 | // attributes again, regardless of what changed in the last iteration. |
| 2577 | if (RecomputeDependences) { |
| 2578 | LLVM_DEBUG( |
| 2579 | dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); |
| 2580 | QueryMap.clear(); |
| 2581 | ChangedAAs.clear(); |
| 2582 | Worklist.insert(AllAbstractAttributes.begin(), |
| 2583 | AllAbstractAttributes.end()); |
| 2584 | } |
| 2585 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2586 | // Add all abstract attributes that are potentially dependent on one that |
| 2587 | // changed to the work list. |
| 2588 | for (AbstractAttribute *ChangedAA : ChangedAAs) { |
| 2589 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| 2590 | Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end()); |
| 2591 | } |
| 2592 | |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 2593 | LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter |
| 2594 | << ", Worklist+Dependent size: " << Worklist.size() |
| 2595 | << "\n"); |
| 2596 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2597 | // Reset the changed set. |
| 2598 | ChangedAAs.clear(); |
| 2599 | |
| 2600 | // Update all abstract attribute in the work list and record the ones that |
| 2601 | // changed. |
| 2602 | for (AbstractAttribute *AA : Worklist) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2603 | if (!isAssumedDead(*AA, nullptr)) |
| 2604 | if (AA->update(*this) == ChangeStatus::CHANGED) |
| 2605 | ChangedAAs.push_back(AA); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2606 | |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 2607 | // Check if we recompute the dependences in the next iteration. |
| 2608 | RecomputeDependences = (DepRecomputeInterval > 0 && |
| 2609 | IterationCounter % DepRecomputeInterval == 0); |
| 2610 | |
| Johannes Doerfert | 9543f14 | 2019-08-23 15:24:57 +0000 | [diff] [blame] | 2611 | // Add attributes to the changed set if they have been created in the last |
| 2612 | // iteration. |
| 2613 | ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, |
| 2614 | AllAbstractAttributes.end()); |
| 2615 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2616 | // Reset the work list and repopulate with the changed abstract attributes. |
| 2617 | // Note that dependent ones are added above. |
| 2618 | Worklist.clear(); |
| 2619 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); |
| 2620 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 2621 | } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || |
| 2622 | VerifyMaxFixpointIterations)); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 2623 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2624 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " |
| 2625 | << IterationCounter << "/" << MaxFixpointIterations |
| 2626 | << " iterations\n"); |
| 2627 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 2628 | size_t NumFinalAAs = AllAbstractAttributes.size(); |
| Johannes Doerfert | b504eb8 | 2019-08-26 18:55:47 +0000 | [diff] [blame] | 2629 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2630 | bool FinishedAtFixpoint = Worklist.empty(); |
| 2631 | |
| 2632 | // Reset abstract arguments not settled in a sound fixpoint by now. This |
| 2633 | // happens when we stopped the fixpoint iteration early. Note that only the |
| 2634 | // ones marked as "changed" *and* the ones transitively depending on them |
| 2635 | // need to be reverted to a pessimistic state. Others might not be in a |
| 2636 | // fixpoint state but we can use the optimistic results for them anyway. |
| 2637 | SmallPtrSet<AbstractAttribute *, 32> Visited; |
| 2638 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { |
| 2639 | AbstractAttribute *ChangedAA = ChangedAAs[u]; |
| 2640 | if (!Visited.insert(ChangedAA).second) |
| 2641 | continue; |
| 2642 | |
| 2643 | AbstractState &State = ChangedAA->getState(); |
| 2644 | if (!State.isAtFixpoint()) { |
| 2645 | State.indicatePessimisticFixpoint(); |
| 2646 | |
| 2647 | NumAttributesTimedOut++; |
| 2648 | } |
| 2649 | |
| 2650 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| 2651 | ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end()); |
| 2652 | } |
| 2653 | |
| 2654 | LLVM_DEBUG({ |
| 2655 | if (!Visited.empty()) |
| 2656 | dbgs() << "\n[Attributor] Finalized " << Visited.size() |
| 2657 | << " abstract attributes.\n"; |
| 2658 | }); |
| 2659 | |
| 2660 | unsigned NumManifested = 0; |
| 2661 | unsigned NumAtFixpoint = 0; |
| 2662 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; |
| 2663 | for (AbstractAttribute *AA : AllAbstractAttributes) { |
| 2664 | AbstractState &State = AA->getState(); |
| 2665 | |
| 2666 | // If there is not already a fixpoint reached, we can now take the |
| 2667 | // optimistic state. This is correct because we enforced a pessimistic one |
| 2668 | // on abstract attributes that were transitively dependent on a changed one |
| 2669 | // already above. |
| 2670 | if (!State.isAtFixpoint()) |
| 2671 | State.indicateOptimisticFixpoint(); |
| 2672 | |
| 2673 | // If the state is invalid, we do not try to manifest it. |
| 2674 | if (!State.isValidState()) |
| 2675 | continue; |
| 2676 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2677 | // Skip dead code. |
| 2678 | if (isAssumedDead(*AA, nullptr)) |
| 2679 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2680 | // Manifest the state and record if we changed the IR. |
| 2681 | ChangeStatus LocalChange = AA->manifest(*this); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2682 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) |
| 2683 | AA->trackStatistics(); |
| 2684 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2685 | ManifestChange = ManifestChange | LocalChange; |
| 2686 | |
| 2687 | NumAtFixpoint++; |
| 2688 | NumManifested += (LocalChange == ChangeStatus::CHANGED); |
| 2689 | } |
| 2690 | |
| 2691 | (void)NumManifested; |
| 2692 | (void)NumAtFixpoint; |
| 2693 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested |
| 2694 | << " arguments while " << NumAtFixpoint |
| 2695 | << " were in a valid fixpoint state\n"); |
| 2696 | |
| 2697 | // If verification is requested, we finished this run at a fixpoint, and the |
| 2698 | // IR was changed, we re-run the whole fixpoint analysis, starting at |
| 2699 | // re-initialization of the arguments. This re-run should not result in an IR |
| 2700 | // change. Though, the (virtual) state of attributes at the end of the re-run |
| 2701 | // might be more optimistic than the known state or the IR state if the better |
| 2702 | // state cannot be manifested. |
| 2703 | if (VerifyAttributor && FinishedAtFixpoint && |
| 2704 | ManifestChange == ChangeStatus::CHANGED) { |
| 2705 | VerifyAttributor = false; |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2706 | ChangeStatus VerifyStatus = run(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2707 | if (VerifyStatus != ChangeStatus::UNCHANGED) |
| 2708 | llvm_unreachable( |
| 2709 | "Attributor verification failed, re-run did result in an IR change " |
| 2710 | "even after a fixpoint was reached in the original run. (False " |
| 2711 | "positives possible!)"); |
| 2712 | VerifyAttributor = true; |
| 2713 | } |
| 2714 | |
| 2715 | NumAttributesManifested += NumManifested; |
| 2716 | NumAttributesValidFixpoint += NumAtFixpoint; |
| 2717 | |
| Fangrui Song | f182617 | 2019-08-20 07:21:43 +0000 | [diff] [blame] | 2718 | (void)NumFinalAAs; |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2719 | assert( |
| 2720 | NumFinalAAs == AllAbstractAttributes.size() && |
| 2721 | "Expected the final number of abstract attributes to remain unchanged!"); |
| Johannes Doerfert | 39681e7 | 2019-08-27 04:57:54 +0000 | [diff] [blame] | 2722 | |
| 2723 | // Delete stuff at the end to avoid invalid references and a nice order. |
| 2724 | LLVM_DEBUG(dbgs() << "\n[Attributor] Delete " << ToBeDeletedFunctions.size() |
| 2725 | << " functions and " << ToBeDeletedBlocks.size() |
| 2726 | << " blocks and " << ToBeDeletedInsts.size() |
| 2727 | << " instructions\n"); |
| 2728 | for (Instruction *I : ToBeDeletedInsts) { |
| 2729 | if (I->hasNUsesOrMore(1)) |
| 2730 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 2731 | I->eraseFromParent(); |
| 2732 | } |
| 2733 | for (BasicBlock *BB : ToBeDeletedBlocks) { |
| 2734 | // TODO: Check if we need to replace users (PHIs, indirect branches?) |
| 2735 | BB->eraseFromParent(); |
| 2736 | } |
| 2737 | for (Function *Fn : ToBeDeletedFunctions) { |
| 2738 | Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); |
| 2739 | Fn->eraseFromParent(); |
| 2740 | } |
| 2741 | |
| Johannes Doerfert | bf11213 | 2019-08-29 01:29:44 +0000 | [diff] [blame] | 2742 | if (VerifyMaxFixpointIterations && |
| 2743 | IterationCounter != MaxFixpointIterations) { |
| 2744 | errs() << "\n[Attributor] Fixpoint iteration done after: " |
| 2745 | << IterationCounter << "/" << MaxFixpointIterations |
| 2746 | << " iterations\n"; |
| 2747 | llvm_unreachable("The fixpoint was not reached with exactly the number of " |
| 2748 | "specified iterations!"); |
| 2749 | } |
| 2750 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2751 | return ManifestChange; |
| 2752 | } |
| 2753 | |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2754 | /// Helper function that checks if an abstract attribute of type \p AAType |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2755 | /// should be created for IR position \p IRP and if so creates and registers it |
| 2756 | /// with the Attributor \p A. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2757 | /// |
| 2758 | /// This method will look at the provided whitelist. If one is given and the |
| 2759 | /// kind \p AAType::ID is not contained, no abstract attribute is created. |
| 2760 | /// |
| 2761 | /// \returns The created abstract argument, or nullptr if none was created. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2762 | template <typename AAType> |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 2763 | static const AAType *checkAndRegisterAA(const IRPosition &IRP, Attributor &A, |
| 2764 | DenseSet<const char *> *Whitelist) { |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2765 | if (Whitelist && !Whitelist->count(&AAType::ID)) |
| 2766 | return nullptr; |
| 2767 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2768 | return &A.registerAA<AAType>(*new AAType(IRP)); |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2771 | void Attributor::identifyDefaultAbstractAttributes( |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2772 | Function &F, DenseSet<const char *> *Whitelist) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2773 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2774 | IRPosition FPos = IRPosition::function(F); |
| 2775 | |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 2776 | // Check for dead BasicBlocks in every function. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2777 | // We need dead instruction detection because we do not want to deal with |
| 2778 | // broken IR in which SSA rules do not apply. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2779 | checkAndRegisterAA<AAIsDeadFunction>(FPos, *this, /* Whitelist */ nullptr); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 2780 | |
| 2781 | // Every function might be "will-return". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2782 | checkAndRegisterAA<AAWillReturnFunction>(FPos, *this, Whitelist); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 2783 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2784 | // Every function can be nounwind. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2785 | checkAndRegisterAA<AANoUnwindFunction>(FPos, *this, Whitelist); |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2786 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 2787 | // Every function might be marked "nosync" |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2788 | checkAndRegisterAA<AANoSyncFunction>(FPos, *this, Whitelist); |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 2789 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 2790 | // Every function might be "no-free". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2791 | checkAndRegisterAA<AANoFreeFunction>(FPos, *this, Whitelist); |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 2792 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2793 | // Every function might be "no-return". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2794 | checkAndRegisterAA<AANoReturnFunction>(FPos, *this, Whitelist); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2795 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 2796 | // Return attributes are only appropriate if the return type is non void. |
| 2797 | Type *ReturnType = F.getReturnType(); |
| 2798 | if (!ReturnType->isVoidTy()) { |
| 2799 | // Argument attribute "returned" --- Create only one per function even |
| 2800 | // though it is an argument attribute. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2801 | checkAndRegisterAA<AAReturnedValuesFunction>(FPos, *this, Whitelist); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2802 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2803 | if (ReturnType->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2804 | IRPosition RetPos = IRPosition::returned(F); |
| 2805 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2806 | // Every function with pointer return type might be marked align. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2807 | checkAndRegisterAA<AAAlignReturned>(RetPos, *this, Whitelist); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2808 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2809 | // Every function with pointer return type might be marked nonnull. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2810 | checkAndRegisterAA<AANonNullReturned>(RetPos, *this, Whitelist); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2811 | |
| 2812 | // Every function with pointer return type might be marked noalias. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2813 | checkAndRegisterAA<AANoAliasReturned>(RetPos, *this, Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2814 | |
| 2815 | // Every function with pointer return type might be marked |
| 2816 | // dereferenceable. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2817 | checkAndRegisterAA<AADereferenceableReturned>(RetPos, *this, Whitelist); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2818 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2821 | for (Argument &Arg : F.args()) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2822 | if (Arg.getType()->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2823 | IRPosition ArgPos = IRPosition::argument(Arg); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2824 | // Every argument with pointer type might be marked nonnull. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2825 | checkAndRegisterAA<AANonNullArgument>(ArgPos, *this, Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2826 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2827 | // Every argument with pointer type might be marked noalias. |
| 2828 | checkAndRegisterAA<AANoAliasArgument>(ArgPos, *this, Whitelist); |
| 2829 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2830 | // Every argument with pointer type might be marked dereferenceable. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2831 | checkAndRegisterAA<AADereferenceableArgument>(ArgPos, *this, Whitelist); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2832 | |
| 2833 | // Every argument with pointer type might be marked align. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2834 | checkAndRegisterAA<AAAlignArgument>(ArgPos, *this, Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2835 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2838 | // Walk all instructions to find more attribute opportunities and also |
| 2839 | // interesting instructions that might be queried by abstract attributes |
| 2840 | // during their initialization or update. |
| 2841 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; |
| 2842 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; |
| 2843 | |
| 2844 | for (Instruction &I : instructions(&F)) { |
| 2845 | bool IsInterestingOpcode = false; |
| 2846 | |
| 2847 | // To allow easy access to all instructions in a function with a given |
| 2848 | // opcode we store them in the InfoCache. As not all opcodes are interesting |
| 2849 | // to concrete attributes we only cache the ones that are as identified in |
| 2850 | // the following switch. |
| 2851 | // Note: There are no concrete attributes now so this is initially empty. |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2852 | switch (I.getOpcode()) { |
| 2853 | default: |
| 2854 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && |
| 2855 | "New call site/base instruction type needs to be known int the " |
| 2856 | "attributor."); |
| 2857 | break; |
| Johannes Doerfert | 5a5a139 | 2019-08-23 20:20:10 +0000 | [diff] [blame] | 2858 | case Instruction::Load: |
| 2859 | // The alignment of a pointer is interesting for loads. |
| 2860 | checkAndRegisterAA<AAAlignFloating>( |
| 2861 | IRPosition::value(*cast<LoadInst>(I).getPointerOperand()), *this, |
| 2862 | Whitelist); |
| 2863 | break; |
| 2864 | case Instruction::Store: |
| 2865 | // The alignment of a pointer is interesting for stores. |
| 2866 | checkAndRegisterAA<AAAlignFloating>( |
| 2867 | IRPosition::value(*cast<StoreInst>(I).getPointerOperand()), *this, |
| 2868 | Whitelist); |
| 2869 | break; |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2870 | case Instruction::Call: |
| 2871 | case Instruction::CallBr: |
| 2872 | case Instruction::Invoke: |
| 2873 | case Instruction::CleanupRet: |
| 2874 | case Instruction::CatchSwitch: |
| 2875 | case Instruction::Resume: |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 2876 | case Instruction::Ret: |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2877 | IsInterestingOpcode = true; |
| 2878 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2879 | if (IsInterestingOpcode) |
| 2880 | InstOpcodeMap[I.getOpcode()].push_back(&I); |
| 2881 | if (I.mayReadOrWriteMemory()) |
| 2882 | ReadOrWriteInsts.push_back(&I); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2883 | |
| 2884 | CallSite CS(&I); |
| 2885 | if (CS && CS.getCalledFunction()) { |
| 2886 | for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) { |
| 2887 | if (!CS.getArgument(i)->getType()->isPointerTy()) |
| 2888 | continue; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2889 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2890 | |
| 2891 | // Call site argument attribute "non-null". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2892 | checkAndRegisterAA<AANonNullCallSiteArgument>(CSArgPos, *this, |
| 2893 | Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2894 | |
| Hideto Ueno | cbab334 | 2019-08-29 05:52:00 +0000 | [diff] [blame] | 2895 | // Call site argument attribute "no-alias". |
| 2896 | checkAndRegisterAA<AANoAliasCallSiteArgument>(CSArgPos, *this, |
| 2897 | Whitelist); |
| 2898 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2899 | // Call site argument attribute "dereferenceable". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2900 | checkAndRegisterAA<AADereferenceableCallSiteArgument>(CSArgPos, *this, |
| 2901 | Whitelist); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2902 | |
| 2903 | // Call site argument attribute "align". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2904 | checkAndRegisterAA<AAAlignCallSiteArgument>(CSArgPos, *this, Whitelist); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2905 | } |
| 2906 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | /// Helpers to ease debugging through output streams and print calls. |
| 2911 | /// |
| 2912 | ///{ |
| 2913 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { |
| 2914 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); |
| 2915 | } |
| 2916 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2917 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2918 | switch (AP) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2919 | case IRPosition::IRP_INVALID: |
| 2920 | return OS << "inv"; |
| 2921 | case IRPosition::IRP_FLOAT: |
| 2922 | return OS << "flt"; |
| 2923 | case IRPosition::IRP_RETURNED: |
| 2924 | return OS << "fn_ret"; |
| 2925 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 2926 | return OS << "cs_ret"; |
| 2927 | case IRPosition::IRP_FUNCTION: |
| 2928 | return OS << "fn"; |
| 2929 | case IRPosition::IRP_CALL_SITE: |
| 2930 | return OS << "cs"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2931 | case IRPosition::IRP_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2932 | return OS << "arg"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2933 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2934 | return OS << "cs_arg"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2935 | } |
| 2936 | llvm_unreachable("Unknown attribute position!"); |
| 2937 | } |
| 2938 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2939 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2940 | const Value &AV = Pos.getAssociatedValue(); |
| 2941 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2942 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; |
| 2943 | } |
| 2944 | |
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 2945 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerState &S) { |
| 2946 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" |
| 2947 | << static_cast<const AbstractState &>(S); |
| 2948 | } |
| 2949 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2950 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { |
| 2951 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); |
| 2952 | } |
| 2953 | |
| 2954 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { |
| 2955 | AA.print(OS); |
| 2956 | return OS; |
| 2957 | } |
| 2958 | |
| 2959 | void AbstractAttribute::print(raw_ostream &OS) const { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2960 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() |
| 2961 | << "]"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2962 | } |
| 2963 | ///} |
| 2964 | |
| 2965 | /// ---------------------------------------------------------------------------- |
| 2966 | /// Pass (Manager) Boilerplate |
| 2967 | /// ---------------------------------------------------------------------------- |
| 2968 | |
| 2969 | static bool runAttributorOnModule(Module &M) { |
| 2970 | if (DisableAttributor) |
| 2971 | return false; |
| 2972 | |
| 2973 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() |
| 2974 | << " functions.\n"); |
| 2975 | |
| 2976 | // Create an Attributor and initially empty information cache that is filled |
| 2977 | // while we identify default attribute opportunities. |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2978 | InformationCache InfoCache(M.getDataLayout()); |
| Johannes Doerfert | f7ca0fe | 2019-08-28 16:58:52 +0000 | [diff] [blame] | 2979 | Attributor A(InfoCache, DepRecInterval); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2980 | |
| 2981 | for (Function &F : M) { |
| 2982 | // TODO: Not all attributes require an exact definition. Find a way to |
| 2983 | // enable deduction for some but not all attributes in case the |
| 2984 | // definition might be changed at runtime, see also |
| 2985 | // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html. |
| 2986 | // TODO: We could always determine abstract attributes and if sufficient |
| 2987 | // information was found we could duplicate the functions that do not |
| 2988 | // have an exact definition. |
| 2989 | if (!F.hasExactDefinition()) { |
| 2990 | NumFnWithoutExactDefinition++; |
| 2991 | continue; |
| 2992 | } |
| 2993 | |
| 2994 | // For now we ignore naked and optnone functions. |
| 2995 | if (F.hasFnAttribute(Attribute::Naked) || |
| 2996 | F.hasFnAttribute(Attribute::OptimizeNone)) |
| 2997 | continue; |
| 2998 | |
| 2999 | NumFnWithExactDefinition++; |
| 3000 | |
| 3001 | // Populate the Attributor with abstract attribute opportunities in the |
| 3002 | // function and the information cache with IR information. |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3003 | A.identifyDefaultAbstractAttributes(F); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 3006 | return A.run() == ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 3010 | if (runAttributorOnModule(M)) { |
| 3011 | // FIXME: Think about passes we will preserve and add them here. |
| 3012 | return PreservedAnalyses::none(); |
| 3013 | } |
| 3014 | return PreservedAnalyses::all(); |
| 3015 | } |
| 3016 | |
| 3017 | namespace { |
| 3018 | |
| 3019 | struct AttributorLegacyPass : public ModulePass { |
| 3020 | static char ID; |
| 3021 | |
| 3022 | AttributorLegacyPass() : ModulePass(ID) { |
| 3023 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 3024 | } |
| 3025 | |
| 3026 | bool runOnModule(Module &M) override { |
| 3027 | if (skipModule(M)) |
| 3028 | return false; |
| 3029 | return runAttributorOnModule(M); |
| 3030 | } |
| 3031 | |
| 3032 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 3033 | // FIXME: Think about passes we will preserve and add them here. |
| 3034 | AU.setPreservesCFG(); |
| 3035 | } |
| 3036 | }; |
| 3037 | |
| 3038 | } // end anonymous namespace |
| 3039 | |
| 3040 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } |
| 3041 | |
| 3042 | char AttributorLegacyPass::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 3043 | |
| 3044 | const char AAReturnedValues::ID = 0; |
| 3045 | const char AANoUnwind::ID = 0; |
| 3046 | const char AANoSync::ID = 0; |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 3047 | const char AANoFree::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 3048 | const char AANonNull::ID = 0; |
| 3049 | const char AANoRecurse::ID = 0; |
| 3050 | const char AAWillReturn::ID = 0; |
| 3051 | const char AANoAlias::ID = 0; |
| 3052 | const char AANoReturn::ID = 0; |
| 3053 | const char AAIsDead::ID = 0; |
| 3054 | const char AADereferenceable::ID = 0; |
| 3055 | const char AAAlign::ID = 0; |
| 3056 | |
| Johannes Doerfert | 12cbbab | 2019-08-20 06:15:50 +0000 | [diff] [blame] | 3057 | // Macro magic to create the static generator function for attributes that |
| 3058 | // follow the naming scheme. |
| 3059 | |
| 3060 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ |
| 3061 | case IRPosition::PK: \ |
| 3062 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); |
| 3063 | |
| 3064 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ |
| 3065 | case IRPosition::PK: \ |
| 3066 | AA = new CLASS##SUFFIX(IRP); \ |
| 3067 | break; |
| 3068 | |
| 3069 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 3070 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 3071 | CLASS *AA = nullptr; \ |
| 3072 | switch (IRP.getPositionKind()) { \ |
| 3073 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 3074 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ |
| 3075 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ |
| 3076 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ |
| 3077 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ |
| 3078 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ |
| 3079 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ |
| 3080 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ |
| 3081 | } \ |
| 3082 | AA->initialize(A); \ |
| 3083 | return *AA; \ |
| 3084 | } |
| 3085 | |
| 3086 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ |
| 3087 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ |
| 3088 | CLASS *AA = nullptr; \ |
| 3089 | switch (IRP.getPositionKind()) { \ |
| 3090 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ |
| 3091 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ |
| 3092 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ |
| 3093 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ |
| 3094 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ |
| 3095 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ |
| 3096 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ |
| 3097 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ |
| 3098 | } \ |
| 3099 | AA->initialize(A); \ |
| 3100 | return *AA; \ |
| 3101 | } |
| 3102 | |
| 3103 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) |
| 3104 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) |
| 3105 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) |
| 3106 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) |
| 3107 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) |
| 3108 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) |
| 3109 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) |
| 3110 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) |
| 3111 | |
| 3112 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) |
| 3113 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) |
| 3114 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) |
| 3115 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) |
| 3116 | |
| 3117 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| 3118 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION |
| 3119 | #undef SWITCH_PK_CREATE |
| 3120 | #undef SWITCH_PK_INV |
| 3121 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 3122 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", |
| 3123 | "Deduce and propagate attributes", false, false) |
| 3124 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", |
| 3125 | "Deduce and propagate attributes", false, false) |