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