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