| 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 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 455 | /// -----------------------NoUnwind Function Attribute-------------------------- |
| 456 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 457 | struct AANoUnwindImpl : AANoUnwind { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 458 | AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 459 | |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 460 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 461 | return getAssumed() ? "nounwind" : "may-unwind"; |
| 462 | } |
| 463 | |
| 464 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 465 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 468 | struct AANoUnwindFunction final : public AANoUnwindImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 469 | AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 470 | |
| 471 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 472 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 475 | ChangeStatus AANoUnwindImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 476 | |
| 477 | // The map from instruction opcodes to those instructions in the function. |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 478 | auto Opcodes = { |
| 479 | (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, |
| 480 | (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, |
| 481 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; |
| 482 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 483 | auto CheckForNoUnwind = [&](Instruction &I) { |
| 484 | if (!I.mayThrow()) |
| 485 | return true; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 486 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 487 | auto *NoUnwindAA = A.getAAFor<AANoUnwind>(*this, IRPosition::value(I)); |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 488 | return NoUnwindAA && NoUnwindAA->isAssumedNoUnwind(); |
| 489 | }; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 490 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 491 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 492 | return indicatePessimisticFixpoint(); |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 493 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 494 | return ChangeStatus::UNCHANGED; |
| 495 | } |
| 496 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 497 | /// --------------------- Function Return Values ------------------------------- |
| 498 | |
| 499 | /// "Attribute" that collects all potential returned values and the return |
| 500 | /// instructions that they arise from. |
| 501 | /// |
| 502 | /// If there is a unique returned value R, the manifest method will: |
| 503 | /// - mark R with the "returned" attribute, if R is an argument. |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 504 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 505 | |
| 506 | /// Mapping of values potentially returned by the associated function to the |
| 507 | /// return instructions that might return them. |
| 508 | DenseMap<Value *, SmallPtrSet<ReturnInst *, 2>> ReturnedValues; |
| 509 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 510 | SmallPtrSet<CallBase *, 8> UnresolvedCalls; |
| 511 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 512 | /// State flags |
| 513 | /// |
| 514 | ///{ |
| 515 | bool IsFixed; |
| 516 | bool IsValidState; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 517 | ///} |
| 518 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 519 | public: |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 520 | AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 521 | |
| 522 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 523 | void initialize(Attributor &A) override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 524 | // Reset the state. |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 525 | IsFixed = false; |
| 526 | IsValidState = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 527 | ReturnedValues.clear(); |
| 528 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 529 | Function *F = getAssociatedFunction(); |
| 530 | if (!F || !F->hasExactDefinition()) { |
| 531 | indicatePessimisticFixpoint(); |
| 532 | return; |
| 533 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 534 | |
| 535 | // The map from instruction opcodes to those instructions in the function. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 536 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 537 | |
| 538 | // 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] | 539 | for (Argument &Arg : F->args()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 540 | if (Arg.hasReturnedAttr()) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 541 | auto &ReturnInstSet = ReturnedValues[&Arg]; |
| 542 | for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) |
| 543 | ReturnInstSet.insert(cast<ReturnInst>(RI)); |
| 544 | |
| 545 | indicateOptimisticFixpoint(); |
| 546 | return; |
| 547 | } |
| 548 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /// See AbstractAttribute::manifest(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 552 | ChangeStatus manifest(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 553 | |
| 554 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 555 | AbstractState &getState() override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 556 | |
| 557 | /// See AbstractAttribute::getState(...). |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 558 | const AbstractState &getState() const override { return *this; } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 559 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 560 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 561 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 562 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 563 | llvm::iterator_range<iterator> returned_values() override { |
| 564 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 565 | } |
| 566 | |
| 567 | llvm::iterator_range<const_iterator> returned_values() const override { |
| 568 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); |
| 569 | } |
| 570 | |
| 571 | const SmallPtrSetImpl<CallBase *> &getUnresolvedCalls() const override { |
| 572 | return UnresolvedCalls; |
| 573 | } |
| 574 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 575 | /// Return the number of potential return values, -1 if unknown. |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 576 | size_t getNumReturnValues() const override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 577 | return isValidState() ? ReturnedValues.size() : -1; |
| 578 | } |
| 579 | |
| 580 | /// Return an assumed unique return value if a single candidate is found. If |
| 581 | /// there cannot be one, return a nullptr. If it is not clear yet, return the |
| 582 | /// Optional::NoneType. |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 583 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 584 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 585 | /// See AbstractState::checkForAllReturnedValues(...). |
| 586 | bool checkForAllReturnedValuesAndReturnInsts( |
| 587 | const function_ref<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> |
| 588 | &Pred) const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 589 | |
| 590 | /// Pretty print the attribute similar to the IR representation. |
| Stefan Stipanovic | 15e86f7 | 2019-07-12 17:42:14 +0000 | [diff] [blame] | 591 | const std::string getAsStr() const override; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 592 | |
| 593 | /// See AbstractState::isAtFixpoint(). |
| 594 | bool isAtFixpoint() const override { return IsFixed; } |
| 595 | |
| 596 | /// See AbstractState::isValidState(). |
| 597 | bool isValidState() const override { return IsValidState; } |
| 598 | |
| 599 | /// See AbstractState::indicateOptimisticFixpoint(...). |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 600 | ChangeStatus indicateOptimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 601 | IsFixed = true; |
| 602 | IsValidState &= true; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 603 | return ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 604 | } |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 605 | |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 606 | ChangeStatus indicatePessimisticFixpoint() override { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 607 | IsFixed = true; |
| 608 | IsValidState = false; |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 609 | return ChangeStatus::CHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 610 | } |
| 611 | }; |
| 612 | |
| 613 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { |
| 614 | ChangeStatus Changed = ChangeStatus::UNCHANGED; |
| 615 | |
| 616 | // Bookkeeping. |
| 617 | assert(isValidState()); |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 618 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn, |
| 619 | "Number of function with known return values"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 620 | |
| 621 | // 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] | 622 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 623 | |
| 624 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) |
| 625 | return Changed; |
| 626 | |
| 627 | // Bookkeeping. |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 628 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, |
| 629 | "Number of function with unique return"); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 630 | |
| 631 | // If the assumed unique return value is an argument, annotate it. |
| 632 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 633 | getIRPosition() = IRPosition::argument(*UniqueRVArg); |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 634 | Changed = IRAttribute::manifest(A) | Changed; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | return Changed; |
| 638 | } |
| 639 | |
| 640 | const std::string AAReturnedValuesImpl::getAsStr() const { |
| 641 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + |
| Johannes Doerfert | 6471bb6 | 2019-08-04 18:39:28 +0000 | [diff] [blame] | 642 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 643 | ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 646 | Optional<Value *> |
| 647 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { |
| 648 | // If checkForAllReturnedValues provides a unique value, ignoring potential |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 649 | // undef values that can also be present, it is assumed to be the actual |
| 650 | // return value and forwarded to the caller of this method. If there are |
| 651 | // multiple, a nullptr is returned indicating there cannot be a unique |
| 652 | // returned value. |
| 653 | Optional<Value *> UniqueRV; |
| 654 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 655 | auto Pred = [&](Value &RV) -> bool { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 656 | // If we found a second returned value and neither the current nor the saved |
| 657 | // one is an undef, there is no unique returned value. Undefs are special |
| 658 | // since we can pretend they have any value. |
| 659 | if (UniqueRV.hasValue() && UniqueRV != &RV && |
| 660 | !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { |
| 661 | UniqueRV = nullptr; |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | // Do not overwrite a value with an undef. |
| 666 | if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) |
| 667 | UniqueRV = &RV; |
| 668 | |
| 669 | return true; |
| 670 | }; |
| 671 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 672 | if (!A.checkForAllReturnedValues(Pred, *this)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 673 | UniqueRV = nullptr; |
| 674 | |
| 675 | return UniqueRV; |
| 676 | } |
| 677 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 678 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( |
| 679 | const function_ref<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> |
| 680 | &Pred) const { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 681 | if (!isValidState()) |
| 682 | return false; |
| 683 | |
| 684 | // Check all returned values but ignore call sites as long as we have not |
| 685 | // encountered an overdefined one during an update. |
| 686 | for (auto &It : ReturnedValues) { |
| 687 | Value *RV = It.first; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 688 | const SmallPtrSetImpl<ReturnInst *> &RetInsts = It.second; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 689 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 690 | CallBase *CB = dyn_cast<CallBase>(RV); |
| 691 | if (CB && !UnresolvedCalls.count(CB)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 692 | continue; |
| 693 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 694 | if (!Pred(*RV, RetInsts)) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 695 | return false; |
| 696 | } |
| 697 | |
| 698 | return true; |
| 699 | } |
| 700 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 701 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 702 | size_t NumUnresolvedCalls = UnresolvedCalls.size(); |
| 703 | bool Changed = false; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 704 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 705 | // State used in the value traversals starting in returned values. |
| 706 | struct RVState { |
| 707 | // The map in which we collect return values -> return instrs. |
| 708 | decltype(ReturnedValues) &RetValsMap; |
| 709 | // The flag to indicate a change. |
| 710 | bool Changed; |
| 711 | // The return instrs we come from. |
| 712 | SmallPtrSet<ReturnInst *, 2> RetInsts; |
| 713 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 714 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 715 | // Callback for a leaf value returned by the associated function. |
| 716 | auto VisitValueCB = [](Value &Val, RVState &RVS, bool) { |
| 717 | auto Size = RVS.RetValsMap[&Val].size(); |
| 718 | RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); |
| 719 | bool Inserted = RVS.RetValsMap[&Val].size() != Size; |
| 720 | RVS.Changed |= Inserted; |
| 721 | LLVM_DEBUG({ |
| 722 | if (Inserted) |
| 723 | dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val |
| 724 | << " => " << RVS.RetInsts.size() << "\n"; |
| 725 | }); |
| 726 | }; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 727 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 728 | // Helper method to invoke the generic value traversal. |
| 729 | auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { |
| 730 | IRPosition RetValPos = IRPosition::value(RV); |
| 731 | return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, |
| 732 | RVS, VisitValueCB); |
| 733 | }; |
| Johannes Doerfert | da4d811 | 2019-08-01 16:21:54 +0000 | [diff] [blame] | 734 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 735 | // Callback for all "return intructions" live in the associated function. |
| 736 | auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { |
| 737 | ReturnInst &Ret = cast<ReturnInst>(I); |
| 738 | RVState RVS({ReturnedValues, false}); |
| 739 | RVS.RetInsts.insert(&Ret); |
| 740 | Changed |= RVS.Changed; |
| 741 | return VisitReturnedValue(*Ret.getReturnValue(), RVS); |
| 742 | }; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 743 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 744 | // Start by discovering returned values from all live returned instructions in |
| 745 | // the associated function. |
| 746 | if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) |
| 747 | return indicatePessimisticFixpoint(); |
| 748 | |
| 749 | // Once returned values "directly" present in the code are handled we try to |
| 750 | // resolve returned calls. |
| 751 | decltype(ReturnedValues) NewRVsMap; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 752 | for (auto &It : ReturnedValues) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 753 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first |
| 754 | << " by #" << It.second.size() << " RIs\n"); |
| 755 | CallBase *CB = dyn_cast<CallBase>(It.first); |
| 756 | if (!CB || UnresolvedCalls.count(CB)) |
| 757 | continue; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 758 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 759 | const auto *RetValAAPtr = |
| 760 | A.getAAFor<AAReturnedValues>(*this, IRPosition::callsite_function(*CB)); |
| 761 | |
| 762 | // Skip dead ends, thus if we do not know anything about the returned |
| 763 | // call we mark it as unresolved and it will stay that way. |
| 764 | if (!RetValAAPtr || !RetValAAPtr->getState().isValidState()) { |
| 765 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB |
| 766 | << "\n"); |
| 767 | UnresolvedCalls.insert(CB); |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | const auto &RetValAA = *RetValAAPtr; |
| 772 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " |
| 773 | << static_cast<const AbstractAttribute &>(RetValAA) |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 774 | << "\n"); |
| 775 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 776 | // If we know something but not everyting about the returned values, keep |
| 777 | // track of that too. Hence, remember transitively unresolved calls. |
| 778 | UnresolvedCalls.insert(RetValAA.getUnresolvedCalls().begin(), |
| 779 | RetValAA.getUnresolvedCalls().end()); |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 780 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 781 | // Now track transitively returned values. |
| 782 | for (auto &RetValAAIt : RetValAA.returned_values()) { |
| 783 | Value *RetVal = RetValAAIt.first; |
| 784 | if (Argument *Arg = dyn_cast<Argument>(RetVal)) { |
| 785 | // Arguments are mapped to call site operands and we begin the traversal |
| 786 | // again. |
| 787 | RVState RVS({NewRVsMap, false, RetValAAIt.second}); |
| 788 | VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); |
| 789 | continue; |
| 790 | } else if (isa<CallBase>(RetVal)) { |
| 791 | // Call sites are resolved by the callee attribute over time, no need to |
| 792 | // do anything for us. |
| 793 | continue; |
| 794 | } else if (isa<Constant>(RetVal)) { |
| 795 | // Constants are valid everywhere, we can simply take them. |
| 796 | NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); |
| 797 | continue; |
| 798 | } |
| 799 | // Anything that did not fit in the above categories cannot be resolved, |
| 800 | // mark the call as unresolved. |
| 801 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " |
| 802 | "cannot be translated: " |
| 803 | << *RetVal << "\n"); |
| 804 | UnresolvedCalls.insert(CB); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 805 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 808 | // To avoid modifications to the ReturnedValues map while we iterate over it |
| 809 | // we kept record of potential new entries in a copy map, NewRVsMap. |
| 810 | for (auto &It : NewRVsMap) { |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 811 | assert(!It.second.empty() && "Entry does not add anything."); |
| 812 | auto &ReturnInsts = ReturnedValues[It.first]; |
| 813 | for (ReturnInst *RI : It.second) |
| 814 | if (ReturnInsts.insert(RI).second) { |
| 815 | LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " |
| 816 | << *It.first << " => " << *RI << "\n"); |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 817 | Changed = true; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 821 | Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); |
| 822 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 825 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { |
| 826 | AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} |
| 827 | |
| 828 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 829 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 832 | /// ------------------------ NoSync Function Attribute ------------------------- |
| 833 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 834 | struct AANoSyncImpl : AANoSync { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 835 | AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 836 | |
| Stefan Stipanovic | cb5ecae | 2019-07-12 18:34:06 +0000 | [diff] [blame] | 837 | const std::string getAsStr() const override { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 838 | return getAssumed() ? "nosync" : "may-sync"; |
| 839 | } |
| 840 | |
| 841 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 842 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 843 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 844 | /// Helper function used to determine whether an instruction is non-relaxed |
| 845 | /// atomic. In other words, if an atomic instruction does not have unordered |
| 846 | /// or monotonic ordering |
| 847 | static bool isNonRelaxedAtomic(Instruction *I); |
| 848 | |
| 849 | /// Helper function used to determine whether an instruction is volatile. |
| 850 | static bool isVolatile(Instruction *I); |
| 851 | |
| Johannes Doerfert | c7a1db3 | 2019-07-13 01:09:27 +0000 | [diff] [blame] | 852 | /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, |
| 853 | /// memset). |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 854 | static bool isNoSyncIntrinsic(Instruction *I); |
| 855 | }; |
| 856 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 857 | struct AANoSyncFunction final : public AANoSyncImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 858 | AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 859 | |
| 860 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 861 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 862 | }; |
| 863 | |
| 864 | bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 865 | if (!I->isAtomic()) |
| 866 | return false; |
| 867 | |
| 868 | AtomicOrdering Ordering; |
| 869 | switch (I->getOpcode()) { |
| 870 | case Instruction::AtomicRMW: |
| 871 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); |
| 872 | break; |
| 873 | case Instruction::Store: |
| 874 | Ordering = cast<StoreInst>(I)->getOrdering(); |
| 875 | break; |
| 876 | case Instruction::Load: |
| 877 | Ordering = cast<LoadInst>(I)->getOrdering(); |
| 878 | break; |
| 879 | case Instruction::Fence: { |
| 880 | auto *FI = cast<FenceInst>(I); |
| 881 | if (FI->getSyncScopeID() == SyncScope::SingleThread) |
| 882 | return false; |
| 883 | Ordering = FI->getOrdering(); |
| 884 | break; |
| 885 | } |
| 886 | case Instruction::AtomicCmpXchg: { |
| 887 | AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); |
| 888 | AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); |
| 889 | // Only if both are relaxed, than it can be treated as relaxed. |
| 890 | // Otherwise it is non-relaxed. |
| 891 | if (Success != AtomicOrdering::Unordered && |
| 892 | Success != AtomicOrdering::Monotonic) |
| 893 | return true; |
| 894 | if (Failure != AtomicOrdering::Unordered && |
| 895 | Failure != AtomicOrdering::Monotonic) |
| 896 | return true; |
| 897 | return false; |
| 898 | } |
| 899 | default: |
| 900 | llvm_unreachable( |
| 901 | "New atomic operations need to be known in the attributor."); |
| 902 | } |
| 903 | |
| 904 | // Relaxed. |
| 905 | if (Ordering == AtomicOrdering::Unordered || |
| 906 | Ordering == AtomicOrdering::Monotonic) |
| 907 | return false; |
| 908 | return true; |
| 909 | } |
| 910 | |
| 911 | /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. |
| 912 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 913 | bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 914 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 915 | switch (II->getIntrinsicID()) { |
| 916 | /// Element wise atomic memory intrinsics are can only be unordered, |
| 917 | /// therefore nosync. |
| 918 | case Intrinsic::memset_element_unordered_atomic: |
| 919 | case Intrinsic::memmove_element_unordered_atomic: |
| 920 | case Intrinsic::memcpy_element_unordered_atomic: |
| 921 | return true; |
| 922 | case Intrinsic::memset: |
| 923 | case Intrinsic::memmove: |
| 924 | case Intrinsic::memcpy: |
| 925 | if (!cast<MemIntrinsic>(II)->isVolatile()) |
| 926 | return true; |
| 927 | return false; |
| 928 | default: |
| 929 | return false; |
| 930 | } |
| 931 | } |
| 932 | return false; |
| 933 | } |
| 934 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 935 | bool AANoSyncImpl::isVolatile(Instruction *I) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 936 | assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && |
| 937 | "Calls should not be checked here"); |
| 938 | |
| 939 | switch (I->getOpcode()) { |
| 940 | case Instruction::AtomicRMW: |
| 941 | return cast<AtomicRMWInst>(I)->isVolatile(); |
| 942 | case Instruction::Store: |
| 943 | return cast<StoreInst>(I)->isVolatile(); |
| 944 | case Instruction::Load: |
| 945 | return cast<LoadInst>(I)->isVolatile(); |
| 946 | case Instruction::AtomicCmpXchg: |
| 947 | return cast<AtomicCmpXchgInst>(I)->isVolatile(); |
| 948 | default: |
| 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 953 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 954 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 955 | auto CheckRWInstForNoSync = [&](Instruction &I) { |
| 956 | /// We are looking for volatile instructions or Non-Relaxed atomics. |
| 957 | /// FIXME: We should ipmrove the handling of intrinsics. |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 958 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 959 | if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) |
| 960 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 961 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 962 | if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { |
| 963 | if (ICS.hasFnAttr(Attribute::NoSync)) |
| 964 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 965 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 966 | auto *NoSyncAA = |
| 967 | A.getAAFor<AANoSyncImpl>(*this, IRPosition::callsite_function(ICS)); |
| 968 | if (NoSyncAA && NoSyncAA->isAssumedNoSync()) |
| 969 | return true; |
| 970 | return false; |
| 971 | } |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 972 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 973 | if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) |
| 974 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 975 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 976 | return false; |
| 977 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 978 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 979 | auto CheckForNoSync = [&](Instruction &I) { |
| 980 | // At this point we handled all read/write effects and they are all |
| 981 | // nosync, so they can be skipped. |
| 982 | if (I.mayReadOrWriteMemory()) |
| 983 | return true; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 984 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 985 | // non-convergent and readnone imply nosync. |
| 986 | return !ImmutableCallSite(&I).isConvergent(); |
| 987 | }; |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 988 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 989 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || |
| 990 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 991 | return indicatePessimisticFixpoint(); |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 992 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 993 | return ChangeStatus::UNCHANGED; |
| 994 | } |
| 995 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 996 | /// ------------------------ No-Free Attributes ---------------------------- |
| 997 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 998 | struct AANoFreeImpl : public AANoFree { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 999 | AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1000 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1001 | /// See AbstractAttribute::getAsStr(). |
| 1002 | const std::string getAsStr() const override { |
| 1003 | return getAssumed() ? "nofree" : "may-free"; |
| 1004 | } |
| 1005 | |
| 1006 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1007 | ChangeStatus updateImpl(Attributor &A) override; |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1008 | }; |
| 1009 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1010 | struct AANoFreeFunction final : public AANoFreeImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1011 | AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1012 | |
| 1013 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1014 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1015 | }; |
| 1016 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1017 | ChangeStatus AANoFreeImpl::updateImpl(Attributor &A) { |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1018 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1019 | auto CheckForNoFree = [&](Instruction &I) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1020 | ImmutableCallSite ICS(&I); |
| 1021 | if (ICS.hasFnAttr(Attribute::NoFree)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1022 | return true; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1023 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1024 | auto *NoFreeAA = |
| 1025 | A.getAAFor<AANoFreeImpl>(*this, IRPosition::callsite_function(ICS)); |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1026 | return NoFreeAA && NoFreeAA->isAssumedNoFree(); |
| 1027 | }; |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1028 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1029 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1030 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 1031 | return ChangeStatus::UNCHANGED; |
| 1032 | } |
| 1033 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1034 | /// ------------------------ NonNull Argument Attribute ------------------------ |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1035 | struct AANonNullImpl : AANonNull { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1036 | AANonNullImpl(const IRPosition &IRP) : AANonNull(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1037 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1038 | /// See AbstractAttribute::getAsStr(). |
| 1039 | const std::string getAsStr() const override { |
| 1040 | return getAssumed() ? "nonnull" : "may-null"; |
| 1041 | } |
| 1042 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1043 | /// See AbstractAttribute::initialize(...). |
| 1044 | void initialize(Attributor &A) override { |
| 1045 | if (hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) |
| 1046 | indicateOptimisticFixpoint(); |
| 1047 | } |
| 1048 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1049 | /// Generate a predicate that checks if a given value is assumed nonnull. |
| 1050 | /// The generated function returns true if a value satisfies any of |
| 1051 | /// following conditions. |
| 1052 | /// (i) A value is known nonZero(=nonnull). |
| 1053 | /// (ii) A value is associated with AANonNull and its isAssumedNonNull() is |
| 1054 | /// true. |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1055 | std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> |
| 1056 | generatePredicate(Attributor &); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1057 | }; |
| 1058 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1059 | std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> |
| 1060 | AANonNullImpl::generatePredicate(Attributor &A) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1061 | // FIXME: The `AAReturnedValues` should provide the predicate with the |
| 1062 | // `ReturnInst` vector as well such that we can use the control flow sensitive |
| 1063 | // version of `isKnownNonZero`. This should fix `test11` in |
| 1064 | // `test/Transforms/FunctionAttrs/nonnull.ll` |
| 1065 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1066 | std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred = |
| 1067 | [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool { |
| Johannes Doerfert | 26e5846 | 2019-08-12 22:21:09 +0000 | [diff] [blame] | 1068 | if (isKnownNonZero(&RV, A.getDataLayout())) |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1069 | return true; |
| 1070 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1071 | if (ImmutableCallSite ICS = ImmutableCallSite(&RV)) |
| 1072 | if (ICS.hasRetAttr(Attribute::NonNull)) |
| 1073 | return true; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1074 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1075 | auto *NonNullAA = A.getAAFor<AANonNull>(*this, IRPosition::value(RV)); |
| 1076 | return (NonNullAA && NonNullAA->isAssumedNonNull()); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1077 | }; |
| 1078 | |
| 1079 | return Pred; |
| 1080 | } |
| 1081 | |
| 1082 | /// NonNull attribute for function return value. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1083 | struct AANonNullReturned final : AANonNullImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1084 | AANonNullReturned(const IRPosition &IRP) : AANonNullImpl(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1085 | |
| 1086 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1087 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1088 | |
| 1089 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1090 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1091 | }; |
| 1092 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1093 | ChangeStatus AANonNullReturned::updateImpl(Attributor &A) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1094 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1095 | std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred = |
| 1096 | this->generatePredicate(A); |
| 1097 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1098 | if (!A.checkForAllReturnedValuesAndReturnInsts(Pred, *this)) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1099 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1100 | return ChangeStatus::UNCHANGED; |
| 1101 | } |
| 1102 | |
| 1103 | /// NonNull attribute for function argument. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1104 | struct AANonNullArgument final : AANonNullImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1105 | AANonNullArgument(const IRPosition &IRP) : AANonNullImpl(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1106 | |
| 1107 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1108 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1109 | |
| 1110 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1111 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1112 | }; |
| 1113 | |
| 1114 | /// NonNull attribute for a call site argument. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1115 | struct AANonNullCallSiteArgument final : AANonNullImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1116 | AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullImpl(IRP) {} |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1117 | |
| 1118 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1119 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1120 | AANonNullImpl::initialize(A); |
| 1121 | if (!isKnownNonNull() && |
| 1122 | isKnownNonZero(&getAssociatedValue(), A.getDataLayout())) |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1123 | indicateOptimisticFixpoint(); |
| 1124 | } |
| 1125 | |
| 1126 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1127 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1128 | |
| 1129 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1130 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1131 | }; |
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1132 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1133 | ChangeStatus AANonNullArgument::updateImpl(Attributor &A) { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1134 | unsigned ArgNo = getArgNo(); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1135 | |
| 1136 | // Callback function |
| 1137 | std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) { |
| 1138 | assert(CS && "Sanity check: Call site was not initialized properly!"); |
| 1139 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1140 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, ArgNo); |
| 1141 | if (CSArgPos.hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) |
| 1142 | return true; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Check that NonNullAA is AANonNullCallSiteArgument. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1145 | if (auto *NonNullAA = A.getAAFor<AANonNullImpl>(*this, CSArgPos)) { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1146 | ImmutableCallSite ICS(&NonNullAA->getAnchorValue()); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1147 | if (ICS && CS.getInstruction() == ICS.getInstruction()) |
| 1148 | return NonNullAA->isAssumedNonNull(); |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1152 | Value *V = CS.getArgOperand(ArgNo); |
| Johannes Doerfert | 26e5846 | 2019-08-12 22:21:09 +0000 | [diff] [blame] | 1153 | if (isKnownNonZero(V, A.getDataLayout())) |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1154 | return true; |
| 1155 | |
| 1156 | return false; |
| 1157 | }; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1158 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true)) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1159 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1160 | return ChangeStatus::UNCHANGED; |
| 1161 | } |
| 1162 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1163 | ChangeStatus AANonNullCallSiteArgument::updateImpl(Attributor &A) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1164 | // NOTE: Never look at the argument of the callee in this method. |
| 1165 | // If we do this, "nonnull" is always deduced because of the assumption. |
| 1166 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1167 | Value &V = getAssociatedValue(); |
| 1168 | auto *NonNullAA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1169 | |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1170 | if (!NonNullAA || !NonNullAA->isAssumedNonNull()) |
| 1171 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 1172 | |
| 1173 | return ChangeStatus::UNCHANGED; |
| 1174 | } |
| 1175 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1176 | /// ------------------------ Will-Return Attributes ---------------------------- |
| 1177 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1178 | struct AAWillReturnImpl : public AAWillReturn { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1179 | AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1180 | |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1181 | /// See AbstractAttribute::getAsStr() |
| 1182 | const std::string getAsStr() const override { |
| 1183 | return getAssumed() ? "willreturn" : "may-noreturn"; |
| 1184 | } |
| 1185 | }; |
| 1186 | |
| 1187 | struct AAWillReturnFunction final : AAWillReturnImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1188 | AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1189 | |
| 1190 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1191 | void initialize(Attributor &A) override; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1192 | |
| 1193 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1194 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1195 | |
| 1196 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1197 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1198 | }; |
| 1199 | |
| 1200 | // Helper function that checks whether a function has any cycle. |
| 1201 | // TODO: Replace with more efficent code |
| 1202 | bool containsCycle(Function &F) { |
| 1203 | SmallPtrSet<BasicBlock *, 32> Visited; |
| 1204 | |
| 1205 | // Traverse BB by dfs and check whether successor is already visited. |
| 1206 | for (BasicBlock *BB : depth_first(&F)) { |
| 1207 | Visited.insert(BB); |
| 1208 | for (auto *SuccBB : successors(BB)) { |
| 1209 | if (Visited.count(SuccBB)) |
| 1210 | return true; |
| 1211 | } |
| 1212 | } |
| 1213 | return false; |
| 1214 | } |
| 1215 | |
| 1216 | // Helper function that checks the function have a loop which might become an |
| 1217 | // endless loop |
| 1218 | // FIXME: Any cycle is regarded as endless loop for now. |
| 1219 | // We have to allow some patterns. |
| 1220 | bool containsPossiblyEndlessLoop(Function &F) { return containsCycle(F); } |
| 1221 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1222 | void AAWillReturnFunction::initialize(Attributor &A) { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1223 | Function &F = *getAnchorScope(); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1224 | |
| 1225 | if (containsPossiblyEndlessLoop(F)) |
| 1226 | indicatePessimisticFixpoint(); |
| 1227 | } |
| 1228 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1229 | ChangeStatus AAWillReturnFunction::updateImpl(Attributor &A) { |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1230 | // The map from instruction opcodes to those instructions in the function. |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1231 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1232 | auto CheckForWillReturn = [&](Instruction &I) { |
| 1233 | ImmutableCallSite ICS(&I); |
| 1234 | if (ICS.hasFnAttr(Attribute::WillReturn)) |
| 1235 | return true; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1236 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1237 | IRPosition IPos = IRPosition::callsite_function(ICS); |
| 1238 | auto *WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1239 | if (!WillReturnAA || !WillReturnAA->isAssumedWillReturn()) |
| 1240 | return false; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1241 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1242 | // FIXME: Prohibit any recursion for now. |
| 1243 | if (ICS.hasFnAttr(Attribute::NoRecurse)) |
| 1244 | return true; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1245 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1246 | auto *NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1247 | return NoRecurseAA && NoRecurseAA->isAssumedNoRecurse(); |
| 1248 | }; |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1249 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1250 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 1251 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 11d3710 | 2019-07-17 15:15:43 +0000 | [diff] [blame] | 1252 | |
| 1253 | return ChangeStatus::UNCHANGED; |
| 1254 | } |
| 1255 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1256 | /// ------------------------ NoAlias Argument Attribute ------------------------ |
| 1257 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1258 | struct AANoAliasImpl : AANoAlias { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1259 | AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1260 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1261 | const std::string getAsStr() const override { |
| 1262 | return getAssumed() ? "noalias" : "may-alias"; |
| 1263 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1264 | }; |
| 1265 | |
| 1266 | /// NoAlias attribute for function return value. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1267 | struct AANoAliasReturned final : AANoAliasImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1268 | AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1269 | |
| 1270 | /// See AbstractAttriubute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1271 | void initialize(Attributor &A) override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1272 | Function &F = *getAnchorScope(); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1273 | |
| 1274 | // Already noalias. |
| 1275 | if (F.returnDoesNotAlias()) { |
| 1276 | indicateOptimisticFixpoint(); |
| 1277 | return; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1282 | virtual ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1283 | |
| 1284 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1285 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1286 | }; |
| 1287 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1288 | ChangeStatus AANoAliasReturned::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1289 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1290 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1291 | if (Constant *C = dyn_cast<Constant>(&RV)) |
| 1292 | if (C->isNullValue() || isa<UndefValue>(C)) |
| 1293 | return true; |
| 1294 | |
| 1295 | /// For now, we can only deduce noalias if we have call sites. |
| 1296 | /// FIXME: add more support. |
| 1297 | ImmutableCallSite ICS(&RV); |
| 1298 | if (!ICS) |
| 1299 | return false; |
| 1300 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1301 | if (!ICS.returnDoesNotAlias()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1302 | auto *NoAliasAA = |
| 1303 | A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS)); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1304 | if (!NoAliasAA || !NoAliasAA->isAssumedNoAlias()) |
| 1305 | return false; |
| 1306 | } |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1307 | |
| 1308 | /// FIXME: We can improve capture check in two ways: |
| 1309 | /// 1. Use the AANoCapture facilities. |
| 1310 | /// 2. Use the location of return insts for escape queries. |
| 1311 | if (PointerMayBeCaptured(&RV, /* ReturnCaptures */ false, |
| 1312 | /* StoreCaptures */ true)) |
| 1313 | return false; |
| 1314 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1315 | return true; |
| 1316 | }; |
| 1317 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1318 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1319 | return indicatePessimisticFixpoint(); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 1320 | |
| 1321 | return ChangeStatus::UNCHANGED; |
| 1322 | } |
| 1323 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1324 | /// -------------------AAIsDead Function Attribute----------------------- |
| 1325 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1326 | struct AAIsDeadImpl : public AAIsDead { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1327 | AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {} |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1328 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1329 | void initialize(Attributor &A) override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1330 | const Function &F = *getAnchorScope(); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1331 | |
| 1332 | ToBeExploredPaths.insert(&(F.getEntryBlock().front())); |
| 1333 | AssumedLiveBlocks.insert(&(F.getEntryBlock())); |
| 1334 | for (size_t i = 0; i < ToBeExploredPaths.size(); ++i) |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1335 | if (const Instruction *NextNoReturnI = |
| 1336 | findNextNoReturn(A, ToBeExploredPaths[i])) |
| 1337 | NoReturnCalls.insert(NextNoReturnI); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1340 | /// Find the next assumed noreturn instruction in the block of \p I starting |
| 1341 | /// from, thus including, \p I. |
| 1342 | /// |
| 1343 | /// The caller is responsible to monitor the ToBeExploredPaths set as new |
| 1344 | /// instructions discovered in other basic block will be placed in there. |
| 1345 | /// |
| 1346 | /// \returns The next assumed noreturn instructions in the block of \p I |
| 1347 | /// starting from, thus including, \p I. |
| 1348 | const Instruction *findNextNoReturn(Attributor &A, const Instruction *I); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1349 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1350 | /// See AbstractAttribute::getAsStr(). |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1351 | const std::string getAsStr() const override { |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1352 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1353 | std::to_string(getAnchorScope()->size()) + "][#NRI " + |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1354 | std::to_string(NoReturnCalls.size()) + "]"; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | /// See AbstractAttribute::manifest(...). |
| 1358 | ChangeStatus manifest(Attributor &A) override { |
| 1359 | assert(getState().isValidState() && |
| 1360 | "Attempted to manifest an invalid state!"); |
| 1361 | |
| 1362 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1363 | const Function &F = *getAssociatedFunction(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1364 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1365 | // Flag to determine if we can change an invoke to a call assuming the |
| 1366 | // callee is nounwind. This is not possible if the personality of the |
| 1367 | // function allows to catch asynchronous exceptions. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1368 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1369 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1370 | for (const Instruction *NRC : NoReturnCalls) { |
| 1371 | Instruction *I = const_cast<Instruction *>(NRC); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1372 | BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1373 | Instruction *SplitPos = I->getNextNode(); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1374 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1375 | if (auto *II = dyn_cast<InvokeInst>(I)) { |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 1376 | // If we keep the invoke the split position is at the beginning of the |
| 1377 | // normal desitination block (it invokes a noreturn function after all). |
| 1378 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 1379 | SplitPos = &NormalDestBB->front(); |
| 1380 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1381 | /// Invoke is replaced with a call and unreachable is placed after it if |
| 1382 | /// the callee is nounwind and noreturn. Otherwise, we keep the invoke |
| 1383 | /// and only place an unreachable in the normal successor. |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1384 | if (Invoke2CallAllowed) { |
| 1385 | if (Function *Callee = II->getCalledFunction()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1386 | auto *AANoUnw = |
| 1387 | A.getAAFor<AANoUnwind>(*this, IRPosition::function(*Callee)); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1388 | if (Callee->hasFnAttribute(Attribute::NoUnwind) || |
| 1389 | (AANoUnw && AANoUnw->isAssumedNoUnwind())) { |
| 1390 | LLVM_DEBUG(dbgs() |
| 1391 | << "[AAIsDead] Replace invoke with call inst\n"); |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 1392 | // We do not need an invoke (II) but instead want a call followed |
| 1393 | // by an unreachable. However, we do not remove II as other |
| 1394 | // abstract attributes might have it cached as part of their |
| 1395 | // results. Given that we modify the CFG anyway, we simply keep II |
| 1396 | // around but in a new dead block. To avoid II being live through |
| 1397 | // a different edge we have to ensure the block we place it in is |
| 1398 | // only reached from the current block of II and then not reached |
| 1399 | // at all when we insert the unreachable. |
| 1400 | SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); |
| 1401 | CallInst *CI = createCallMatchingInvoke(II); |
| 1402 | CI->insertBefore(II); |
| 1403 | CI->takeName(II); |
| 1404 | II->replaceAllUsesWith(CI); |
| 1405 | SplitPos = CI->getNextNode(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1406 | } |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| Johannes Doerfert | 3d7bbc6 | 2019-08-05 21:35:02 +0000 | [diff] [blame] | 1411 | BB = SplitPos->getParent(); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1412 | SplitBlock(BB, SplitPos); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1413 | changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false); |
| 1414 | HasChanged = ChangeStatus::CHANGED; |
| 1415 | } |
| 1416 | |
| 1417 | return HasChanged; |
| 1418 | } |
| 1419 | |
| 1420 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1421 | ChangeStatus updateImpl(Attributor &A) override; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1422 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1423 | /// See AAIsDead::isAssumedDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1424 | bool isAssumedDead(const BasicBlock *BB) const override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1425 | assert(BB->getParent() == getAnchorScope() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1426 | "BB must be in the same anchor scope function."); |
| 1427 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1428 | if (!getAssumed()) |
| 1429 | return false; |
| 1430 | return !AssumedLiveBlocks.count(BB); |
| 1431 | } |
| 1432 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1433 | /// See AAIsDead::isKnownDead(BasicBlock *). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1434 | bool isKnownDead(const BasicBlock *BB) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1435 | return getKnown() && isAssumedDead(BB); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1438 | /// See AAIsDead::isAssumed(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1439 | bool isAssumedDead(const Instruction *I) const override { |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1440 | assert(I->getParent()->getParent() == getAnchorScope() && |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1441 | "Instruction must be in the same anchor scope function."); |
| 1442 | |
| Stefan Stipanovic | 7849e41 | 2019-08-03 15:27:41 +0000 | [diff] [blame] | 1443 | if (!getAssumed()) |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | |
| 1446 | // If it is not in AssumedLiveBlocks then it for sure dead. |
| 1447 | // Otherwise, it can still be after noreturn call in a live block. |
| 1448 | if (!AssumedLiveBlocks.count(I->getParent())) |
| 1449 | return true; |
| 1450 | |
| 1451 | // If it is not after a noreturn call, than it is live. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1452 | return isAfterNoReturn(I); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /// See AAIsDead::isKnownDead(Instruction *I). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1456 | bool isKnownDead(const Instruction *I) const override { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1457 | return getKnown() && isAssumedDead(I); |
| 1458 | } |
| 1459 | |
| 1460 | /// Check if instruction is after noreturn call, in other words, assumed dead. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1461 | bool isAfterNoReturn(const Instruction *I) const; |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1462 | |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1463 | /// Determine if \p F might catch asynchronous exceptions. |
| 1464 | static bool mayCatchAsynchronousExceptions(const Function &F) { |
| 1465 | return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); |
| 1466 | } |
| 1467 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1468 | /// Collection of to be explored paths. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1469 | SmallSetVector<const Instruction *, 8> ToBeExploredPaths; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1470 | |
| 1471 | /// Collection of all assumed live BasicBlocks. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1472 | DenseSet<const BasicBlock *> AssumedLiveBlocks; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1473 | |
| 1474 | /// Collection of calls with noreturn attribute, assumed or knwon. |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1475 | SmallSetVector<const Instruction *, 4> NoReturnCalls; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1476 | }; |
| 1477 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1478 | struct AAIsDeadFunction final : public AAIsDeadImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1479 | AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1480 | |
| 1481 | /// See AbstractAttribute::trackStatistics() |
| 1482 | void trackStatistics() const override { |
| 1483 | STATS_DECL(DeadBlocks, Function, |
| 1484 | "Number of basic blocks classified as dead"); |
| 1485 | BUILD_STAT_NAME(DeadBlocks, Function) += |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1486 | getAnchorScope()->size() - AssumedLiveBlocks.size(); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1487 | STATS_DECL(PartiallyDeadBlocks, Function, |
| 1488 | "Number of basic blocks classified as partially dead"); |
| 1489 | BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size(); |
| 1490 | } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1491 | }; |
| 1492 | |
| 1493 | bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1494 | const Instruction *PrevI = I->getPrevNode(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1495 | while (PrevI) { |
| 1496 | if (NoReturnCalls.count(PrevI)) |
| 1497 | return true; |
| 1498 | PrevI = PrevI->getPrevNode(); |
| 1499 | } |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1503 | const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A, |
| 1504 | const Instruction *I) { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1505 | const BasicBlock *BB = I->getParent(); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1506 | const Function &F = *BB->getParent(); |
| 1507 | |
| 1508 | // Flag to determine if we can change an invoke to a call assuming the callee |
| 1509 | // is nounwind. This is not possible if the personality of the function allows |
| 1510 | // to catch asynchronous exceptions. |
| 1511 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1512 | |
| 1513 | // TODO: We should have a function that determines if an "edge" is dead. |
| 1514 | // Edges could be from an instruction to the next or from a terminator |
| 1515 | // to the successor. For now, we need to special case the unwind block |
| 1516 | // of InvokeInst below. |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1517 | |
| 1518 | while (I) { |
| 1519 | ImmutableCallSite ICS(I); |
| 1520 | |
| 1521 | if (ICS) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1522 | const IRPosition &IPos = IRPosition::callsite_function(ICS); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1523 | // Regarless of the no-return property of an invoke instruction we only |
| 1524 | // learn that the regular successor is not reachable through this |
| 1525 | // instruction but the unwind block might still be. |
| 1526 | if (auto *Invoke = dyn_cast<InvokeInst>(I)) { |
| 1527 | // Use nounwind to justify the unwind block is dead as well. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1528 | auto *AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); |
| Johannes Doerfert | 924d213 | 2019-08-05 21:34:45 +0000 | [diff] [blame] | 1529 | if (!Invoke2CallAllowed || |
| 1530 | (!AANoUnw || !AANoUnw->isAssumedNoUnwind())) { |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1531 | AssumedLiveBlocks.insert(Invoke->getUnwindDest()); |
| 1532 | ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front()); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1536 | auto *NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1537 | if (ICS.hasFnAttr(Attribute::NoReturn) || |
| 1538 | (NoReturnAA && NoReturnAA->isAssumedNoReturn())) |
| 1539 | return I; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | I = I->getNextNode(); |
| 1543 | } |
| 1544 | |
| 1545 | // get new paths (reachable blocks). |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1546 | for (const BasicBlock *SuccBB : successors(BB)) { |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1547 | AssumedLiveBlocks.insert(SuccBB); |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1548 | ToBeExploredPaths.insert(&SuccBB->front()); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1551 | // No noreturn instruction found. |
| 1552 | return nullptr; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1555 | ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) { |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1556 | // Temporary collection to iterate over existing noreturn instructions. This |
| 1557 | // will alow easier modification of NoReturnCalls collection |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1558 | SmallVector<const Instruction *, 8> NoReturnChanged; |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1559 | ChangeStatus Status = ChangeStatus::UNCHANGED; |
| 1560 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1561 | for (const Instruction *I : NoReturnCalls) |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1562 | NoReturnChanged.push_back(I); |
| 1563 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1564 | for (const Instruction *I : NoReturnChanged) { |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1565 | size_t Size = ToBeExploredPaths.size(); |
| 1566 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1567 | const Instruction *NextNoReturnI = findNextNoReturn(A, I); |
| 1568 | if (NextNoReturnI != I) { |
| 1569 | Status = ChangeStatus::CHANGED; |
| 1570 | NoReturnCalls.remove(I); |
| 1571 | if (NextNoReturnI) |
| 1572 | NoReturnCalls.insert(NextNoReturnI); |
| 1573 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1574 | |
| Johannes Doerfert | 4361da2 | 2019-08-04 18:38:53 +0000 | [diff] [blame] | 1575 | // Explore new paths. |
| 1576 | while (Size != ToBeExploredPaths.size()) { |
| 1577 | Status = ChangeStatus::CHANGED; |
| 1578 | if (const Instruction *NextNoReturnI = |
| 1579 | findNextNoReturn(A, ToBeExploredPaths[Size++])) |
| 1580 | NoReturnCalls.insert(NextNoReturnI); |
| 1581 | } |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1584 | LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: " |
| 1585 | << AssumedLiveBlocks.size() << " Total number of blocks: " |
| 1586 | << getAnchorScope()->size() << "\n"); |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1587 | |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 1588 | // If we know everything is live there is no need to query for liveness. |
| 1589 | if (NoReturnCalls.empty() && |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1590 | getAnchorScope()->size() == AssumedLiveBlocks.size()) { |
| Johannes Doerfert | d620781 | 2019-08-07 22:32:38 +0000 | [diff] [blame] | 1591 | // Indicating a pessimistic fixpoint will cause the state to be "invalid" |
| 1592 | // which will cause the Attributor to not return the AAIsDead on request, |
| 1593 | // which will prevent us from querying isAssumedDead(). |
| 1594 | indicatePessimisticFixpoint(); |
| 1595 | assert(!isValidState() && "Expected an invalid state!"); |
| 1596 | } |
| 1597 | |
| Stefan Stipanovic | 6058b86 | 2019-07-22 23:58:23 +0000 | [diff] [blame] | 1598 | return Status; |
| 1599 | } |
| 1600 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1601 | /// -------------------- Dereferenceable Argument Attribute -------------------- |
| 1602 | |
| 1603 | struct DerefState : AbstractState { |
| 1604 | |
| 1605 | /// State representing for dereferenceable bytes. |
| 1606 | IntegerState DerefBytesState; |
| 1607 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1608 | /// State representing that whether the value is globaly dereferenceable. |
| 1609 | BooleanState GlobalState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1610 | |
| 1611 | /// See AbstractState::isValidState() |
| 1612 | bool isValidState() const override { return DerefBytesState.isValidState(); } |
| 1613 | |
| Johannes Doerfert | b6acee5 | 2019-08-04 17:55:15 +0000 | [diff] [blame] | 1614 | /// See AbstractState::isAtFixpoint() |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1615 | bool isAtFixpoint() const override { |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1616 | return !isValidState() || |
| 1617 | (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint()); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /// See AbstractState::indicateOptimisticFixpoint(...) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1621 | ChangeStatus indicateOptimisticFixpoint() override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1622 | DerefBytesState.indicateOptimisticFixpoint(); |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1623 | GlobalState.indicateOptimisticFixpoint(); |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1624 | return ChangeStatus::UNCHANGED; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /// See AbstractState::indicatePessimisticFixpoint(...) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1628 | ChangeStatus indicatePessimisticFixpoint() override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1629 | DerefBytesState.indicatePessimisticFixpoint(); |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1630 | GlobalState.indicatePessimisticFixpoint(); |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1631 | return ChangeStatus::CHANGED; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /// Update known dereferenceable bytes. |
| 1635 | void takeKnownDerefBytesMaximum(uint64_t Bytes) { |
| 1636 | DerefBytesState.takeKnownMaximum(Bytes); |
| 1637 | } |
| 1638 | |
| 1639 | /// Update assumed dereferenceable bytes. |
| 1640 | void takeAssumedDerefBytesMinimum(uint64_t Bytes) { |
| 1641 | DerefBytesState.takeAssumedMinimum(Bytes); |
| 1642 | } |
| 1643 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1644 | /// Equality for DerefState. |
| 1645 | bool operator==(const DerefState &R) { |
| 1646 | return this->DerefBytesState == R.DerefBytesState && |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1647 | this->GlobalState == R.GlobalState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1648 | } |
| Johannes Doerfert | e1e844d | 2019-08-14 21:35:20 +0000 | [diff] [blame] | 1649 | |
| 1650 | /// Inequality for IntegerState. |
| 1651 | bool operator!=(const DerefState &R) { return !(*this == R); } |
| 1652 | |
| 1653 | /// See IntegerState::operator^= |
| 1654 | DerefState operator^=(const DerefState &R) { |
| 1655 | DerefBytesState ^= R.DerefBytesState; |
| 1656 | GlobalState ^= R.GlobalState; |
| 1657 | return *this; |
| 1658 | } |
| 1659 | |
| 1660 | /// See IntegerState::operator&= |
| 1661 | DerefState operator&=(const DerefState &R) { |
| 1662 | DerefBytesState &= R.DerefBytesState; |
| 1663 | GlobalState &= R.GlobalState; |
| 1664 | return *this; |
| 1665 | } |
| 1666 | |
| 1667 | /// See IntegerState::operator|= |
| 1668 | DerefState operator|=(const DerefState &R) { |
| 1669 | DerefBytesState |= R.DerefBytesState; |
| 1670 | GlobalState |= R.GlobalState; |
| 1671 | return *this; |
| 1672 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1673 | }; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1674 | |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 1675 | struct AADereferenceableImpl : AADereferenceable, DerefState { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1676 | AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1677 | using StateType = DerefState; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1678 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1679 | void initialize(Attributor &A) override { |
| 1680 | SmallVector<Attribute, 4> Attrs; |
| 1681 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, |
| 1682 | Attrs); |
| 1683 | for (const Attribute &Attr : Attrs) |
| 1684 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); |
| 1685 | |
| 1686 | NonNullAA = A.getAAFor<AANonNull>(*this, getIRPosition()); |
| 1687 | } |
| 1688 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1689 | /// See AbstractAttribute::getState() |
| 1690 | /// { |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1691 | StateType &getState() override { return *this; } |
| 1692 | const StateType &getState() const override { return *this; } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1693 | /// } |
| 1694 | |
| 1695 | /// See AADereferenceable::getAssumedDereferenceableBytes(). |
| 1696 | uint32_t getAssumedDereferenceableBytes() const override { |
| 1697 | return DerefBytesState.getAssumed(); |
| 1698 | } |
| 1699 | |
| 1700 | /// See AADereferenceable::getKnownDereferenceableBytes(). |
| 1701 | uint32_t getKnownDereferenceableBytes() const override { |
| 1702 | return DerefBytesState.getKnown(); |
| 1703 | } |
| 1704 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1705 | /// See AADereferenceable::isAssumedGlobal(). |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1706 | bool isAssumedGlobal() const override { return GlobalState.getAssumed(); } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1707 | |
| 1708 | /// See AADereferenceable::isKnownGlobal(). |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1709 | bool isKnownGlobal() const override { return GlobalState.getKnown(); } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1710 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1711 | bool isAssumedNonNull() const override { |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1712 | return NonNullAA && NonNullAA->isAssumedNonNull(); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 1715 | void getDeducedAttributes(LLVMContext &Ctx, |
| 1716 | SmallVectorImpl<Attribute> &Attrs) const override { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1717 | // TODO: Add *_globally support |
| 1718 | if (isAssumedNonNull()) |
| 1719 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( |
| 1720 | Ctx, getAssumedDereferenceableBytes())); |
| 1721 | else |
| 1722 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( |
| 1723 | Ctx, getAssumedDereferenceableBytes())); |
| 1724 | } |
| 1725 | uint64_t computeAssumedDerefenceableBytes(Attributor &A, Value &V, |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1726 | bool &IsGlobal); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1727 | |
| 1728 | /// See AbstractAttribute::getAsStr(). |
| 1729 | const std::string getAsStr() const override { |
| 1730 | if (!getAssumedDereferenceableBytes()) |
| 1731 | return "unknown-dereferenceable"; |
| 1732 | return std::string("dereferenceable") + |
| 1733 | (isAssumedNonNull() ? "" : "_or_null") + |
| 1734 | (isAssumedGlobal() ? "_globally" : "") + "<" + |
| 1735 | std::to_string(getKnownDereferenceableBytes()) + "-" + |
| 1736 | std::to_string(getAssumedDereferenceableBytes()) + ">"; |
| 1737 | } |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1738 | |
| 1739 | private: |
| 1740 | const AANonNull *NonNullAA = nullptr; |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1741 | }; |
| 1742 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1743 | struct AADereferenceableReturned final : AADereferenceableImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1744 | AADereferenceableReturned(const IRPosition &IRP) |
| 1745 | : AADereferenceableImpl(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1746 | |
| 1747 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1748 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1749 | |
| 1750 | /// See AbstractAttribute::trackStatistics() |
| 1751 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1752 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1753 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1754 | }; |
| 1755 | |
| 1756 | // Helper function that returns dereferenceable bytes. |
| 1757 | static uint64_t calcDifferenceIfBaseIsNonNull(int64_t DerefBytes, |
| 1758 | int64_t Offset, bool IsNonNull) { |
| 1759 | if (!IsNonNull) |
| 1760 | return 0; |
| 1761 | return std::max((int64_t)0, DerefBytes - Offset); |
| 1762 | } |
| 1763 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1764 | uint64_t |
| 1765 | AADereferenceableImpl::computeAssumedDerefenceableBytes(Attributor &A, Value &V, |
| 1766 | bool &IsGlobal) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1767 | // TODO: Tracking the globally flag. |
| 1768 | IsGlobal = false; |
| 1769 | |
| 1770 | // First, we try to get information about V from Attributor. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1771 | if (auto *DerefAA = |
| 1772 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(V))) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1773 | return DerefAA->getAssumedDereferenceableBytes(); |
| 1774 | } |
| 1775 | |
| 1776 | // Otherwise, we try to compute assumed bytes from base pointer. |
| Johannes Doerfert | 26e5846 | 2019-08-12 22:21:09 +0000 | [diff] [blame] | 1777 | const DataLayout &DL = A.getDataLayout(); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1778 | unsigned IdxWidth = |
| 1779 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); |
| 1780 | APInt Offset(IdxWidth, 0); |
| 1781 | Value *Base = V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 1782 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1783 | if (auto *BaseDerefAA = |
| 1784 | A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base))) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1785 | return calcDifferenceIfBaseIsNonNull( |
| 1786 | BaseDerefAA->getAssumedDereferenceableBytes(), Offset.getSExtValue(), |
| 1787 | Offset != 0 || BaseDerefAA->isAssumedNonNull()); |
| 1788 | } |
| 1789 | |
| 1790 | // Then, use IR information. |
| 1791 | |
| 1792 | if (isDereferenceablePointer(Base, Base->getType(), DL)) |
| 1793 | return calcDifferenceIfBaseIsNonNull( |
| 1794 | DL.getTypeStoreSize(Base->getType()->getPointerElementType()), |
| 1795 | Offset.getSExtValue(), |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 1796 | !NullPointerIsDefined(getAnchorScope(), |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1797 | V.getType()->getPointerAddressSpace())); |
| 1798 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1799 | return 0; |
| 1800 | } |
| Johannes Doerfert | 007153e | 2019-08-05 23:26:06 +0000 | [diff] [blame] | 1801 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1802 | ChangeStatus AADereferenceableReturned::updateImpl(Attributor &A) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1803 | auto BeforeState = static_cast<DerefState>(*this); |
| 1804 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1805 | bool IsGlobal = isAssumedGlobal(); |
| 1806 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1807 | auto CheckReturnValue = [&](Value &RV) -> bool { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1808 | takeAssumedDerefBytesMinimum( |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1809 | computeAssumedDerefenceableBytes(A, RV, IsGlobal)); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1810 | return isValidState(); |
| 1811 | }; |
| 1812 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1813 | if (A.checkForAllReturnedValues(CheckReturnValue, *this)) { |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1814 | GlobalState.intersectAssumedBits(IsGlobal); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1815 | return BeforeState == static_cast<DerefState>(*this) |
| 1816 | ? ChangeStatus::UNCHANGED |
| 1817 | : ChangeStatus::CHANGED; |
| 1818 | } |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1819 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1822 | struct AADereferenceableArgument final : AADereferenceableImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1823 | AADereferenceableArgument(const IRPosition &IRP) |
| 1824 | : AADereferenceableImpl(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1825 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1826 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1827 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1828 | |
| 1829 | /// See AbstractAttribute::trackStatistics() |
| 1830 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1831 | STATS_DECLTRACK_ARG_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1832 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1833 | }; |
| 1834 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1835 | ChangeStatus AADereferenceableArgument::updateImpl(Attributor &A) { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1836 | Argument &Arg = cast<Argument>(getAnchorValue()); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1837 | |
| 1838 | auto BeforeState = static_cast<DerefState>(*this); |
| 1839 | |
| 1840 | unsigned ArgNo = Arg.getArgNo(); |
| 1841 | |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1842 | bool IsGlobal = isAssumedGlobal(); |
| 1843 | |
| 1844 | // Callback function |
| 1845 | std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) -> bool { |
| 1846 | assert(CS && "Sanity check: Call site was not initialized properly!"); |
| 1847 | |
| 1848 | // Check that DereferenceableAA is AADereferenceableCallSiteArgument. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1849 | if (auto *DereferenceableAA = A.getAAFor<AADereferenceable>( |
| 1850 | *this, IRPosition::callsite_argument(CS, ArgNo))) { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1851 | ImmutableCallSite ICS( |
| 1852 | &DereferenceableAA->getIRPosition().getAnchorValue()); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1853 | if (ICS && CS.getInstruction() == ICS.getInstruction()) { |
| 1854 | takeAssumedDerefBytesMinimum( |
| 1855 | DereferenceableAA->getAssumedDereferenceableBytes()); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1856 | IsGlobal &= DereferenceableAA->isAssumedGlobal(); |
| 1857 | return isValidState(); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | takeAssumedDerefBytesMinimum(computeAssumedDerefenceableBytes( |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1862 | A, *CS.getArgOperand(ArgNo), IsGlobal)); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1863 | |
| 1864 | return isValidState(); |
| 1865 | }; |
| 1866 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1867 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true)) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1868 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1869 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1870 | GlobalState.intersectAssumedBits(IsGlobal); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1871 | |
| 1872 | return BeforeState == static_cast<DerefState>(*this) ? ChangeStatus::UNCHANGED |
| 1873 | : ChangeStatus::CHANGED; |
| 1874 | } |
| 1875 | |
| 1876 | /// Dereferenceable attribute for a call site argument. |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1877 | struct AADereferenceableCallSiteArgument final : AADereferenceableImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1878 | AADereferenceableCallSiteArgument(const IRPosition &IRP) |
| 1879 | : AADereferenceableImpl(IRP) {} |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1880 | |
| 1881 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1882 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1883 | |
| 1884 | /// See AbstractAttribute::trackStatistics() |
| 1885 | void trackStatistics() const override { |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1886 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable) |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1887 | } |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1888 | }; |
| 1889 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1890 | ChangeStatus AADereferenceableCallSiteArgument::updateImpl(Attributor &A) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1891 | // NOTE: Never look at the argument of the callee in this method. |
| 1892 | // If we do this, "dereferenceable" is always deduced because of the |
| 1893 | // assumption. |
| 1894 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1895 | Value &V = getAssociatedValue(); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1896 | |
| 1897 | auto BeforeState = static_cast<DerefState>(*this); |
| 1898 | |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1899 | bool IsGlobal = isAssumedGlobal(); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1900 | |
| 1901 | takeAssumedDerefBytesMinimum( |
| Johannes Doerfert | 6a1274a | 2019-08-14 21:31:32 +0000 | [diff] [blame] | 1902 | computeAssumedDerefenceableBytes(A, V, IsGlobal)); |
| 1903 | GlobalState.intersectAssumedBits(IsGlobal); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 1904 | |
| 1905 | return BeforeState == static_cast<DerefState>(*this) ? ChangeStatus::UNCHANGED |
| 1906 | : ChangeStatus::CHANGED; |
| 1907 | } |
| 1908 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1909 | // ------------------------ Align Argument Attribute ------------------------ |
| 1910 | |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 1911 | struct AAAlignImpl : AAAlign { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1912 | AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1913 | |
| 1914 | // Max alignemnt value allowed in IR |
| 1915 | static const unsigned MAX_ALIGN = 1U << 29; |
| 1916 | |
| Johannes Doerfert | beb5150 | 2019-08-07 22:36:15 +0000 | [diff] [blame] | 1917 | const std::string getAsStr() const override { |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1918 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + |
| 1919 | "-" + std::to_string(getAssumedAlign()) + ">") |
| 1920 | : "unknown-align"; |
| 1921 | } |
| 1922 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1923 | /// See AbstractAttriubute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1924 | void initialize(Attributor &A) override { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1925 | takeAssumedMinimum(MAX_ALIGN); |
| 1926 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1927 | SmallVector<Attribute, 4> Attrs; |
| 1928 | getAttrs({Attribute::Alignment}, Attrs); |
| 1929 | for (const Attribute &Attr : Attrs) |
| 1930 | takeKnownMaximum(Attr.getValueAsInt()); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | /// See AbstractAttribute::getDeducedAttributes |
| 1934 | virtual void |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 1935 | getDeducedAttributes(LLVMContext &Ctx, |
| 1936 | SmallVectorImpl<Attribute> &Attrs) const override { |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1937 | Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign())); |
| 1938 | } |
| 1939 | }; |
| 1940 | |
| 1941 | /// Align attribute for function return value. |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1942 | struct AAAlignReturned final : AAAlignImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1943 | AAAlignReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1944 | |
| 1945 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1946 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1947 | |
| 1948 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1949 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1950 | }; |
| 1951 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1952 | ChangeStatus AAAlignReturned::updateImpl(Attributor &A) { |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Currently, align<n> is deduced if alignments in return values are assumed |
| 1955 | // as greater than n. We reach pessimistic fixpoint if any of the return value |
| 1956 | // wouldn't have align. If no assumed state was used for reasoning, an |
| 1957 | // optimistic fixpoint is reached earlier. |
| 1958 | |
| 1959 | base_t BeforeState = getAssumed(); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 1960 | auto CheckReturnValue = |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 1961 | [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1962 | auto *AlignAA = A.getAAFor<AAAlign>(*this, IRPosition::value(RV)); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1963 | |
| 1964 | if (AlignAA) |
| 1965 | takeAssumedMinimum(AlignAA->getAssumedAlign()); |
| 1966 | else |
| 1967 | // Use IR information. |
| Johannes Doerfert | 26e5846 | 2019-08-12 22:21:09 +0000 | [diff] [blame] | 1968 | takeAssumedMinimum(RV.getPointerAlignment(A.getDataLayout())); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1969 | |
| 1970 | return isValidState(); |
| 1971 | }; |
| 1972 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1973 | if (!A.checkForAllReturnedValuesAndReturnInsts(CheckReturnValue, *this)) |
| Johannes Doerfert | d1c3793 | 2019-08-04 18:37:38 +0000 | [diff] [blame] | 1974 | return indicatePessimisticFixpoint(); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1975 | |
| 1976 | return (getAssumed() != BeforeState) ? ChangeStatus::CHANGED |
| 1977 | : ChangeStatus::UNCHANGED; |
| 1978 | } |
| 1979 | |
| 1980 | /// Align attribute for function argument. |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1981 | struct AAAlignArgument final : AAAlignImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 1982 | AAAlignArgument(const IRPosition &IRP) : AAAlignImpl(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1983 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1984 | /// See AbstractAttribute::updateImpl(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1985 | virtual ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 1986 | |
| 1987 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 1988 | void trackStatistics() const override{STATS_DECLTRACK_ARG_ATTR(aligned)}; |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1989 | }; |
| 1990 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 1991 | ChangeStatus AAAlignArgument::updateImpl(Attributor &A) { |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1992 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 1993 | Argument &Arg = cast<Argument>(getAnchorValue()); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1994 | |
| 1995 | unsigned ArgNo = Arg.getArgNo(); |
| Johannes Doerfert | 26e5846 | 2019-08-12 22:21:09 +0000 | [diff] [blame] | 1996 | const DataLayout &DL = A.getDataLayout(); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 1997 | |
| 1998 | auto BeforeState = getAssumed(); |
| 1999 | |
| 2000 | // Callback function |
| 2001 | std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) { |
| 2002 | assert(CS && "Sanity check: Call site was not initialized properly!"); |
| 2003 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2004 | auto *AlignAA = |
| 2005 | A.getAAFor<AAAlign>(*this, IRPosition::callsite_argument(CS, ArgNo)); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Check that AlignAA is AAAlignCallSiteArgument. |
| 2008 | if (AlignAA) { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2009 | ImmutableCallSite ICS(&AlignAA->getIRPosition().getAnchorValue()); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2010 | if (ICS && CS.getInstruction() == ICS.getInstruction()) { |
| 2011 | takeAssumedMinimum(AlignAA->getAssumedAlign()); |
| 2012 | return isValidState(); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | Value *V = CS.getArgOperand(ArgNo); |
| 2017 | takeAssumedMinimum(V->getPointerAlignment(DL)); |
| 2018 | return isValidState(); |
| 2019 | }; |
| 2020 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2021 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true)) |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2022 | indicatePessimisticFixpoint(); |
| 2023 | |
| 2024 | return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED |
| 2025 | : ChangeStatus ::CHANGED; |
| 2026 | } |
| 2027 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2028 | struct AAAlignCallSiteArgument final : AAAlignImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2029 | AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignImpl(IRP) {} |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2030 | |
| 2031 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2032 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 26e5846 | 2019-08-12 22:21:09 +0000 | [diff] [blame] | 2033 | takeKnownMaximum( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2034 | getAssociatedValue().getPointerAlignment(A.getDataLayout())); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2038 | ChangeStatus updateImpl(Attributor &A) override; |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2039 | |
| 2040 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2041 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2042 | }; |
| 2043 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2044 | ChangeStatus AAAlignCallSiteArgument::updateImpl(Attributor &A) { |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2045 | // NOTE: Never look at the argument of the callee in this method. |
| 2046 | // If we do this, "align" is always deduced because of the assumption. |
| 2047 | |
| 2048 | auto BeforeState = getAssumed(); |
| 2049 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2050 | Value &V = getAssociatedValue(); |
| 2051 | auto *AlignAA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (AlignAA) |
| 2054 | takeAssumedMinimum(AlignAA->getAssumedAlign()); |
| 2055 | else |
| 2056 | indicatePessimisticFixpoint(); |
| 2057 | |
| 2058 | return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED |
| 2059 | : ChangeStatus::CHANGED; |
| 2060 | } |
| 2061 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2062 | /// ------------------ Function No-Return Attribute ---------------------------- |
| Johannes Doerfert | 344d038 | 2019-08-07 22:34:26 +0000 | [diff] [blame] | 2063 | struct AANoReturnImpl : public AANoReturn { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2064 | AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2065 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2066 | /// See AbstractAttribute::getAsStr(). |
| 2067 | const std::string getAsStr() const override { |
| 2068 | return getAssumed() ? "noreturn" : "may-return"; |
| 2069 | } |
| 2070 | |
| 2071 | /// See AbstractAttribute::initialize(...). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2072 | void initialize(Attributor &A) override { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2073 | if (hasAttr({getAttrKind()})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2074 | indicateOptimisticFixpoint(); |
| 2075 | } |
| 2076 | |
| 2077 | /// See AbstractAttribute::updateImpl(Attributor &A). |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2078 | virtual ChangeStatus updateImpl(Attributor &A) override { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2079 | auto CheckForNoReturn = [](Instruction &) { return false; }; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2080 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2081 | {(unsigned)Instruction::Ret})) |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2082 | return indicatePessimisticFixpoint(); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2083 | return ChangeStatus::UNCHANGED; |
| 2084 | } |
| 2085 | }; |
| 2086 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2087 | struct AANoReturnFunction final : AANoReturnImpl { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2088 | AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2089 | |
| 2090 | /// See AbstractAttribute::trackStatistics() |
| Johannes Doerfert | 17b578b | 2019-08-14 21:46:25 +0000 | [diff] [blame] | 2091 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2092 | }; |
| 2093 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2094 | /// ---------------------------------------------------------------------------- |
| 2095 | /// Attributor |
| 2096 | /// ---------------------------------------------------------------------------- |
| 2097 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2098 | bool Attributor::isAssumedDead(const AbstractAttribute &AA, |
| 2099 | const AAIsDead *LivenessAA) { |
| 2100 | const Instruction *CtxI = AA.getIRPosition().getCtxI(); |
| 2101 | if (!CtxI) |
| 2102 | return false; |
| 2103 | |
| 2104 | if (!LivenessAA) |
| 2105 | LivenessAA = |
| 2106 | getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction())); |
| 2107 | if (!LivenessAA || !LivenessAA->isAssumedDead(CtxI)) |
| 2108 | return false; |
| 2109 | |
| 2110 | // TODO: Do not track dependences automatically but add it here as only a |
| 2111 | // "is-assumed-dead" result causes a dependence. |
| 2112 | return true; |
| 2113 | } |
| 2114 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2115 | bool Attributor::checkForAllCallSites(const function_ref<bool(CallSite)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2116 | const AbstractAttribute &QueryingAA, |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2117 | bool RequireAllCallSites) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2118 | // We can try to determine information from |
| 2119 | // the call sites. However, this is only possible all call sites are known, |
| 2120 | // hence the function has internal linkage. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2121 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2122 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2123 | if (!AssociatedFunction) |
| 2124 | return false; |
| 2125 | |
| 2126 | if (RequireAllCallSites && !AssociatedFunction->hasInternalLinkage()) { |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2127 | LLVM_DEBUG( |
| 2128 | dbgs() |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame^] | 2129 | << "[Attributor] Function " << AssociatedFunction->getName() |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2130 | << " has no internal linkage, hence not all call sites are known\n"); |
| 2131 | return false; |
| 2132 | } |
| 2133 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2134 | for (const Use &U : AssociatedFunction->uses()) { |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2135 | Instruction *I = cast<Instruction>(U.getUser()); |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2136 | Function *Caller = I->getFunction(); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2137 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2138 | auto *LivenessAA = |
| 2139 | getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*Caller)); |
| Stefan Stipanovic | d021617 | 2019-08-02 21:31:22 +0000 | [diff] [blame] | 2140 | |
| 2141 | // Skip dead calls. |
| 2142 | if (LivenessAA && LivenessAA->isAssumedDead(I)) |
| 2143 | continue; |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2144 | |
| 2145 | CallSite CS(U.getUser()); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2146 | if (!CS || !CS.isCallee(&U) || !CS.getCaller()->hasExactDefinition()) { |
| 2147 | if (!RequireAllCallSites) |
| 2148 | continue; |
| 2149 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame^] | 2150 | LLVM_DEBUG(dbgs() << "[Attributor] User " << *U.getUser() |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2151 | << " is an invalid use of " |
| 2152 | << AssociatedFunction->getName() << "\n"); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2153 | return false; |
| 2154 | } |
| 2155 | |
| 2156 | if (Pred(CS)) |
| 2157 | continue; |
| 2158 | |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame^] | 2159 | LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2160 | << *CS.getInstruction() << "\n"); |
| 2161 | return false; |
| 2162 | } |
| 2163 | |
| 2164 | return true; |
| 2165 | } |
| 2166 | |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2167 | bool Attributor::checkForAllReturnedValuesAndReturnInsts( |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2168 | const function_ref<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> |
| 2169 | &Pred, |
| 2170 | const AbstractAttribute &QueryingAA) { |
| 2171 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2172 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2173 | // Since we need to provide return instructions we have to have an exact |
| 2174 | // definition. |
| 2175 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2176 | if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2177 | return false; |
| 2178 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2179 | // If this is a call site query we use the call site specific return values |
| 2180 | // and liveness information. |
| 2181 | const IRPosition &QueryIRP = IRPosition::function_scope(IRP); |
| 2182 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| 2183 | if (!AARetVal || !AARetVal->getState().isValidState()) |
| 2184 | return false; |
| 2185 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 2186 | return AARetVal->checkForAllReturnedValuesAndReturnInsts(Pred); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | bool Attributor::checkForAllReturnedValues( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2190 | const function_ref<bool(Value &)> &Pred, |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2191 | const AbstractAttribute &QueryingAA) { |
| 2192 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2193 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2194 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2195 | if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2196 | return false; |
| 2197 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2198 | const IRPosition &QueryIRP = IRPosition::function_scope(IRP); |
| 2199 | const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); |
| 2200 | if (!AARetVal || !AARetVal->getState().isValidState()) |
| 2201 | return false; |
| 2202 | |
| Johannes Doerfert | def9928 | 2019-08-14 21:29:37 +0000 | [diff] [blame] | 2203 | return AARetVal->checkForAllReturnedValuesAndReturnInsts( |
| 2204 | [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &) { |
| 2205 | return Pred(RV); |
| 2206 | }); |
| Johannes Doerfert | 14a0493 | 2019-08-07 22:27:24 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2209 | bool Attributor::checkForAllInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2210 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2211 | const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2212 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2213 | const IRPosition &IRP = QueryingAA.getIRPosition(); |
| 2214 | // Since we need to provide instructions we have to have an exact definition. |
| 2215 | const Function *AssociatedFunction = IRP.getAssociatedFunction(); |
| 2216 | if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) |
| 2217 | return false; |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2218 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2219 | const IRPosition &QueryIRP = IRPosition::function_scope(IRP); |
| 2220 | const auto &LivenessAA = getAAFor<AAIsDead>(QueryingAA, QueryIRP); |
| 2221 | |
| 2222 | auto &OpcodeInstMap = |
| 2223 | InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); |
| Johannes Doerfert | d0f6400 | 2019-08-06 00:32:43 +0000 | [diff] [blame] | 2224 | for (unsigned Opcode : Opcodes) { |
| 2225 | for (Instruction *I : OpcodeInstMap[Opcode]) { |
| 2226 | // Skip dead instructions. |
| 2227 | if (LivenessAA && LivenessAA->isAssumedDead(I)) |
| 2228 | continue; |
| 2229 | |
| 2230 | if (!Pred(*I)) |
| 2231 | return false; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2238 | bool Attributor::checkForAllReadWriteInstructions( |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2239 | const llvm::function_ref<bool(Instruction &)> &Pred, |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2240 | AbstractAttribute &QueryingAA) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2241 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2242 | const Function *AssociatedFunction = |
| 2243 | QueryingAA.getIRPosition().getAssociatedFunction(); |
| 2244 | if (!AssociatedFunction) |
| 2245 | return false; |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2246 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2247 | const auto &LivenessAA = |
| 2248 | getAAFor<AAIsDead>(QueryingAA, QueryingAA.getIRPosition()); |
| 2249 | |
| 2250 | for (Instruction *I : |
| 2251 | InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { |
| Stefan Stipanovic | aaa5270 | 2019-08-07 18:26:02 +0000 | [diff] [blame] | 2252 | // Skip dead instructions. |
| 2253 | if (LivenessAA && LivenessAA->isAssumedDead(I)) |
| 2254 | continue; |
| 2255 | |
| 2256 | if (!Pred(*I)) |
| 2257 | return false; |
| 2258 | } |
| 2259 | |
| 2260 | return true; |
| 2261 | } |
| 2262 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2263 | ChangeStatus Attributor::run() { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2264 | // Initialize all abstract attributes. |
| 2265 | for (AbstractAttribute *AA : AllAbstractAttributes) |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2266 | AA->initialize(*this); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2267 | |
| 2268 | LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " |
| 2269 | << AllAbstractAttributes.size() |
| 2270 | << " abstract attributes.\n"); |
| 2271 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2272 | // Now that all abstract attributes are collected and initialized we start |
| 2273 | // the abstract analysis. |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2274 | |
| 2275 | unsigned IterationCounter = 1; |
| 2276 | |
| 2277 | SmallVector<AbstractAttribute *, 64> ChangedAAs; |
| 2278 | SetVector<AbstractAttribute *> Worklist; |
| 2279 | Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); |
| 2280 | |
| 2281 | do { |
| 2282 | LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter |
| 2283 | << ", Worklist size: " << Worklist.size() << "\n"); |
| 2284 | |
| 2285 | // Add all abstract attributes that are potentially dependent on one that |
| 2286 | // changed to the work list. |
| 2287 | for (AbstractAttribute *ChangedAA : ChangedAAs) { |
| 2288 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| 2289 | Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end()); |
| 2290 | } |
| 2291 | |
| 2292 | // Reset the changed set. |
| 2293 | ChangedAAs.clear(); |
| 2294 | |
| 2295 | // Update all abstract attribute in the work list and record the ones that |
| 2296 | // changed. |
| 2297 | for (AbstractAttribute *AA : Worklist) |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2298 | if (!isAssumedDead(*AA, nullptr)) |
| 2299 | if (AA->update(*this) == ChangeStatus::CHANGED) |
| 2300 | ChangedAAs.push_back(AA); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2301 | |
| 2302 | // Reset the work list and repopulate with the changed abstract attributes. |
| 2303 | // Note that dependent ones are added above. |
| 2304 | Worklist.clear(); |
| 2305 | Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); |
| 2306 | |
| 2307 | } while (!Worklist.empty() && ++IterationCounter < MaxFixpointIterations); |
| 2308 | |
| 2309 | LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " |
| 2310 | << IterationCounter << "/" << MaxFixpointIterations |
| 2311 | << " iterations\n"); |
| 2312 | |
| 2313 | bool FinishedAtFixpoint = Worklist.empty(); |
| 2314 | |
| 2315 | // Reset abstract arguments not settled in a sound fixpoint by now. This |
| 2316 | // happens when we stopped the fixpoint iteration early. Note that only the |
| 2317 | // ones marked as "changed" *and* the ones transitively depending on them |
| 2318 | // need to be reverted to a pessimistic state. Others might not be in a |
| 2319 | // fixpoint state but we can use the optimistic results for them anyway. |
| 2320 | SmallPtrSet<AbstractAttribute *, 32> Visited; |
| 2321 | for (unsigned u = 0; u < ChangedAAs.size(); u++) { |
| 2322 | AbstractAttribute *ChangedAA = ChangedAAs[u]; |
| 2323 | if (!Visited.insert(ChangedAA).second) |
| 2324 | continue; |
| 2325 | |
| 2326 | AbstractState &State = ChangedAA->getState(); |
| 2327 | if (!State.isAtFixpoint()) { |
| 2328 | State.indicatePessimisticFixpoint(); |
| 2329 | |
| 2330 | NumAttributesTimedOut++; |
| 2331 | } |
| 2332 | |
| 2333 | auto &QuerriedAAs = QueryMap[ChangedAA]; |
| 2334 | ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end()); |
| 2335 | } |
| 2336 | |
| 2337 | LLVM_DEBUG({ |
| 2338 | if (!Visited.empty()) |
| 2339 | dbgs() << "\n[Attributor] Finalized " << Visited.size() |
| 2340 | << " abstract attributes.\n"; |
| 2341 | }); |
| 2342 | |
| 2343 | unsigned NumManifested = 0; |
| 2344 | unsigned NumAtFixpoint = 0; |
| 2345 | ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; |
| 2346 | for (AbstractAttribute *AA : AllAbstractAttributes) { |
| 2347 | AbstractState &State = AA->getState(); |
| 2348 | |
| 2349 | // If there is not already a fixpoint reached, we can now take the |
| 2350 | // optimistic state. This is correct because we enforced a pessimistic one |
| 2351 | // on abstract attributes that were transitively dependent on a changed one |
| 2352 | // already above. |
| 2353 | if (!State.isAtFixpoint()) |
| 2354 | State.indicateOptimisticFixpoint(); |
| 2355 | |
| 2356 | // If the state is invalid, we do not try to manifest it. |
| 2357 | if (!State.isValidState()) |
| 2358 | continue; |
| 2359 | |
| Johannes Doerfert | 9a1a1f9 | 2019-08-14 21:25:08 +0000 | [diff] [blame] | 2360 | // Skip dead code. |
| 2361 | if (isAssumedDead(*AA, nullptr)) |
| 2362 | continue; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2363 | // Manifest the state and record if we changed the IR. |
| 2364 | ChangeStatus LocalChange = AA->manifest(*this); |
| Johannes Doerfert | d1b79e0 | 2019-08-07 22:46:11 +0000 | [diff] [blame] | 2365 | if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) |
| 2366 | AA->trackStatistics(); |
| 2367 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2368 | ManifestChange = ManifestChange | LocalChange; |
| 2369 | |
| 2370 | NumAtFixpoint++; |
| 2371 | NumManifested += (LocalChange == ChangeStatus::CHANGED); |
| 2372 | } |
| 2373 | |
| 2374 | (void)NumManifested; |
| 2375 | (void)NumAtFixpoint; |
| 2376 | LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested |
| 2377 | << " arguments while " << NumAtFixpoint |
| 2378 | << " were in a valid fixpoint state\n"); |
| 2379 | |
| 2380 | // If verification is requested, we finished this run at a fixpoint, and the |
| 2381 | // IR was changed, we re-run the whole fixpoint analysis, starting at |
| 2382 | // re-initialization of the arguments. This re-run should not result in an IR |
| 2383 | // change. Though, the (virtual) state of attributes at the end of the re-run |
| 2384 | // might be more optimistic than the known state or the IR state if the better |
| 2385 | // state cannot be manifested. |
| 2386 | if (VerifyAttributor && FinishedAtFixpoint && |
| 2387 | ManifestChange == ChangeStatus::CHANGED) { |
| 2388 | VerifyAttributor = false; |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2389 | ChangeStatus VerifyStatus = run(); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2390 | if (VerifyStatus != ChangeStatus::UNCHANGED) |
| 2391 | llvm_unreachable( |
| 2392 | "Attributor verification failed, re-run did result in an IR change " |
| 2393 | "even after a fixpoint was reached in the original run. (False " |
| 2394 | "positives possible!)"); |
| 2395 | VerifyAttributor = true; |
| 2396 | } |
| 2397 | |
| 2398 | NumAttributesManifested += NumManifested; |
| 2399 | NumAttributesValidFixpoint += NumAtFixpoint; |
| 2400 | |
| 2401 | return ManifestChange; |
| 2402 | } |
| 2403 | |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2404 | /// Helper function that checks if an abstract attribute of type \p AAType |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2405 | /// should be created for IR position \p IRP and if so creates and registers it |
| 2406 | /// with the Attributor \p A. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2407 | /// |
| 2408 | /// This method will look at the provided whitelist. If one is given and the |
| 2409 | /// kind \p AAType::ID is not contained, no abstract attribute is created. |
| 2410 | /// |
| 2411 | /// \returns The created abstract argument, or nullptr if none was created. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2412 | template <typename AAType> |
| Johannes Doerfert | 5304b72 | 2019-08-14 22:04:28 +0000 | [diff] [blame^] | 2413 | static AAType *checkAndRegisterAA(const IRPosition &IRP, Attributor &A, |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2414 | DenseSet<const char *> *Whitelist) { |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2415 | if (Whitelist && !Whitelist->count(&AAType::ID)) |
| 2416 | return nullptr; |
| 2417 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2418 | return &A.registerAA<AAType>(*new AAType(IRP)); |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2421 | void Attributor::identifyDefaultAbstractAttributes( |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2422 | Function &F, DenseSet<const char *> *Whitelist) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2423 | |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2424 | IRPosition FPos = IRPosition::function(F); |
| 2425 | |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 2426 | // Check for dead BasicBlocks in every function. |
| Johannes Doerfert | 21fe0a3 | 2019-08-06 00:55:11 +0000 | [diff] [blame] | 2427 | // We need dead instruction detection because we do not want to deal with |
| 2428 | // broken IR in which SSA rules do not apply. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2429 | checkAndRegisterAA<AAIsDeadFunction>(FPos, *this, /* Whitelist */ nullptr); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 2430 | |
| 2431 | // Every function might be "will-return". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2432 | checkAndRegisterAA<AAWillReturnFunction>(FPos, *this, Whitelist); |
| Johannes Doerfert | 305b961 | 2019-08-04 18:40:01 +0000 | [diff] [blame] | 2433 | |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2434 | // Every function can be nounwind. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2435 | checkAndRegisterAA<AANoUnwindFunction>(FPos, *this, Whitelist); |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2436 | |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 2437 | // Every function might be marked "nosync" |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2438 | checkAndRegisterAA<AANoSyncFunction>(FPos, *this, Whitelist); |
| Stefan Stipanovic | 0626367 | 2019-07-11 21:37:40 +0000 | [diff] [blame] | 2439 | |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 2440 | // Every function might be "no-free". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2441 | checkAndRegisterAA<AANoFreeFunction>(FPos, *this, Whitelist); |
| Hideto Ueno | 65bbaf9 | 2019-07-12 17:38:51 +0000 | [diff] [blame] | 2442 | |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2443 | // Every function might be "no-return". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2444 | checkAndRegisterAA<AANoReturnFunction>(FPos, *this, Whitelist); |
| Johannes Doerfert | e83f303 | 2019-08-05 23:22:05 +0000 | [diff] [blame] | 2445 | |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 2446 | // Return attributes are only appropriate if the return type is non void. |
| 2447 | Type *ReturnType = F.getReturnType(); |
| 2448 | if (!ReturnType->isVoidTy()) { |
| 2449 | // Argument attribute "returned" --- Create only one per function even |
| 2450 | // though it is an argument attribute. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2451 | checkAndRegisterAA<AAReturnedValuesFunction>(FPos, *this, Whitelist); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2452 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2453 | if (ReturnType->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2454 | IRPosition RetPos = IRPosition::returned(F); |
| 2455 | |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2456 | // Every function with pointer return type might be marked align. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2457 | checkAndRegisterAA<AAAlignReturned>(RetPos, *this, Whitelist); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2458 | |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2459 | // Every function with pointer return type might be marked nonnull. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2460 | checkAndRegisterAA<AANonNullReturned>(RetPos, *this, Whitelist); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Every function with pointer return type might be marked noalias. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2463 | checkAndRegisterAA<AANoAliasReturned>(RetPos, *this, Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Every function with pointer return type might be marked |
| 2466 | // dereferenceable. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2467 | checkAndRegisterAA<AADereferenceableReturned>(RetPos, *this, Whitelist); |
| Stefan Stipanovic | 69ebb02 | 2019-07-22 19:36:27 +0000 | [diff] [blame] | 2468 | } |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2471 | for (Argument &Arg : F.args()) { |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2472 | if (Arg.getType()->isPointerTy()) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2473 | IRPosition ArgPos = IRPosition::argument(Arg); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2474 | // Every argument with pointer type might be marked nonnull. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2475 | checkAndRegisterAA<AANonNullArgument>(ArgPos, *this, Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2476 | |
| 2477 | // Every argument with pointer type might be marked dereferenceable. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2478 | checkAndRegisterAA<AADereferenceableArgument>(ArgPos, *this, Whitelist); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2479 | |
| 2480 | // Every argument with pointer type might be marked align. |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2481 | checkAndRegisterAA<AAAlignArgument>(ArgPos, *this, Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2482 | } |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2485 | // Walk all instructions to find more attribute opportunities and also |
| 2486 | // interesting instructions that might be queried by abstract attributes |
| 2487 | // during their initialization or update. |
| 2488 | auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; |
| 2489 | auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; |
| 2490 | |
| 2491 | for (Instruction &I : instructions(&F)) { |
| 2492 | bool IsInterestingOpcode = false; |
| 2493 | |
| 2494 | // To allow easy access to all instructions in a function with a given |
| 2495 | // opcode we store them in the InfoCache. As not all opcodes are interesting |
| 2496 | // to concrete attributes we only cache the ones that are as identified in |
| 2497 | // the following switch. |
| 2498 | // Note: There are no concrete attributes now so this is initially empty. |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2499 | switch (I.getOpcode()) { |
| 2500 | default: |
| 2501 | assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && |
| 2502 | "New call site/base instruction type needs to be known int the " |
| 2503 | "attributor."); |
| 2504 | break; |
| 2505 | case Instruction::Call: |
| 2506 | case Instruction::CallBr: |
| 2507 | case Instruction::Invoke: |
| 2508 | case Instruction::CleanupRet: |
| 2509 | case Instruction::CatchSwitch: |
| 2510 | case Instruction::Resume: |
| Johannes Doerfert | accd3e8 | 2019-07-08 23:27:20 +0000 | [diff] [blame] | 2511 | case Instruction::Ret: |
| Stefan Stipanovic | 5360589 | 2019-06-27 11:27:54 +0000 | [diff] [blame] | 2512 | IsInterestingOpcode = true; |
| 2513 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2514 | if (IsInterestingOpcode) |
| 2515 | InstOpcodeMap[I.getOpcode()].push_back(&I); |
| 2516 | if (I.mayReadOrWriteMemory()) |
| 2517 | ReadOrWriteInsts.push_back(&I); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2518 | |
| 2519 | CallSite CS(&I); |
| 2520 | if (CS && CS.getCalledFunction()) { |
| 2521 | for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) { |
| 2522 | if (!CS.getArgument(i)->getType()->isPointerTy()) |
| 2523 | continue; |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2524 | IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2525 | |
| 2526 | // Call site argument attribute "non-null". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2527 | checkAndRegisterAA<AANonNullCallSiteArgument>(CSArgPos, *this, |
| 2528 | Whitelist); |
| Hideto Ueno | 19c07af | 2019-07-23 08:16:17 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Call site argument attribute "dereferenceable". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2531 | checkAndRegisterAA<AADereferenceableCallSiteArgument>(CSArgPos, *this, |
| 2532 | Whitelist); |
| Hideto Ueno | e7bea9b | 2019-07-28 07:04:01 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Call site argument attribute "align". |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2535 | checkAndRegisterAA<AAAlignCallSiteArgument>(CSArgPos, *this, Whitelist); |
| Hideto Ueno | 54869ec | 2019-07-15 06:49:04 +0000 | [diff] [blame] | 2536 | } |
| 2537 | } |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | /// Helpers to ease debugging through output streams and print calls. |
| 2542 | /// |
| 2543 | ///{ |
| 2544 | raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { |
| 2545 | return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); |
| 2546 | } |
| 2547 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2548 | raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2549 | switch (AP) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2550 | case IRPosition::IRP_INVALID: |
| 2551 | return OS << "inv"; |
| 2552 | case IRPosition::IRP_FLOAT: |
| 2553 | return OS << "flt"; |
| 2554 | case IRPosition::IRP_RETURNED: |
| 2555 | return OS << "fn_ret"; |
| 2556 | case IRPosition::IRP_CALL_SITE_RETURNED: |
| 2557 | return OS << "cs_ret"; |
| 2558 | case IRPosition::IRP_FUNCTION: |
| 2559 | return OS << "fn"; |
| 2560 | case IRPosition::IRP_CALL_SITE: |
| 2561 | return OS << "cs"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2562 | case IRPosition::IRP_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2563 | return OS << "arg"; |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2564 | case IRPosition::IRP_CALL_SITE_ARGUMENT: |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2565 | return OS << "cs_arg"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2566 | } |
| 2567 | llvm_unreachable("Unknown attribute position!"); |
| 2568 | } |
| 2569 | |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2570 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { |
| Johannes Doerfert | 710ebb0 | 2019-08-14 21:18:01 +0000 | [diff] [blame] | 2571 | const Value &AV = Pos.getAssociatedValue(); |
| 2572 | return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2573 | << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; |
| 2574 | } |
| 2575 | |
| Johannes Doerfert | acc8079 | 2019-08-12 22:07:34 +0000 | [diff] [blame] | 2576 | raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerState &S) { |
| 2577 | return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" |
| 2578 | << static_cast<const AbstractState &>(S); |
| 2579 | } |
| 2580 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2581 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { |
| 2582 | return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); |
| 2583 | } |
| 2584 | |
| 2585 | raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { |
| 2586 | AA.print(OS); |
| 2587 | return OS; |
| 2588 | } |
| 2589 | |
| 2590 | void AbstractAttribute::print(raw_ostream &OS) const { |
| Johannes Doerfert | fb69f76 | 2019-08-05 23:32:31 +0000 | [diff] [blame] | 2591 | OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() |
| 2592 | << "]"; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2593 | } |
| 2594 | ///} |
| 2595 | |
| 2596 | /// ---------------------------------------------------------------------------- |
| 2597 | /// Pass (Manager) Boilerplate |
| 2598 | /// ---------------------------------------------------------------------------- |
| 2599 | |
| 2600 | static bool runAttributorOnModule(Module &M) { |
| 2601 | if (DisableAttributor) |
| 2602 | return false; |
| 2603 | |
| 2604 | LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() |
| 2605 | << " functions.\n"); |
| 2606 | |
| 2607 | // Create an Attributor and initially empty information cache that is filled |
| 2608 | // while we identify default attribute opportunities. |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2609 | InformationCache InfoCache(M.getDataLayout()); |
| 2610 | Attributor A(InfoCache); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2611 | |
| 2612 | for (Function &F : M) { |
| 2613 | // TODO: Not all attributes require an exact definition. Find a way to |
| 2614 | // enable deduction for some but not all attributes in case the |
| 2615 | // definition might be changed at runtime, see also |
| 2616 | // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html. |
| 2617 | // TODO: We could always determine abstract attributes and if sufficient |
| 2618 | // information was found we could duplicate the functions that do not |
| 2619 | // have an exact definition. |
| 2620 | if (!F.hasExactDefinition()) { |
| 2621 | NumFnWithoutExactDefinition++; |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
| 2625 | // For now we ignore naked and optnone functions. |
| 2626 | if (F.hasFnAttribute(Attribute::Naked) || |
| 2627 | F.hasFnAttribute(Attribute::OptimizeNone)) |
| 2628 | continue; |
| 2629 | |
| 2630 | NumFnWithExactDefinition++; |
| 2631 | |
| 2632 | // Populate the Attributor with abstract attribute opportunities in the |
| 2633 | // function and the information cache with IR information. |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2634 | A.identifyDefaultAbstractAttributes(F); |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| Johannes Doerfert | ece8190 | 2019-08-12 22:05:53 +0000 | [diff] [blame] | 2637 | return A.run() == ChangeStatus::CHANGED; |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 2641 | if (runAttributorOnModule(M)) { |
| 2642 | // FIXME: Think about passes we will preserve and add them here. |
| 2643 | return PreservedAnalyses::none(); |
| 2644 | } |
| 2645 | return PreservedAnalyses::all(); |
| 2646 | } |
| 2647 | |
| 2648 | namespace { |
| 2649 | |
| 2650 | struct AttributorLegacyPass : public ModulePass { |
| 2651 | static char ID; |
| 2652 | |
| 2653 | AttributorLegacyPass() : ModulePass(ID) { |
| 2654 | initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 2655 | } |
| 2656 | |
| 2657 | bool runOnModule(Module &M) override { |
| 2658 | if (skipModule(M)) |
| 2659 | return false; |
| 2660 | return runAttributorOnModule(M); |
| 2661 | } |
| 2662 | |
| 2663 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 2664 | // FIXME: Think about passes we will preserve and add them here. |
| 2665 | AU.setPreservesCFG(); |
| 2666 | } |
| 2667 | }; |
| 2668 | |
| 2669 | } // end anonymous namespace |
| 2670 | |
| 2671 | Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } |
| 2672 | |
| 2673 | char AttributorLegacyPass::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 2674 | |
| 2675 | const char AAReturnedValues::ID = 0; |
| 2676 | const char AANoUnwind::ID = 0; |
| 2677 | const char AANoSync::ID = 0; |
| Johannes Doerfert | eccdf08 | 2019-08-05 23:35:12 +0000 | [diff] [blame] | 2678 | const char AANoFree::ID = 0; |
| Johannes Doerfert | 2402062 | 2019-08-05 23:30:01 +0000 | [diff] [blame] | 2679 | const char AANonNull::ID = 0; |
| 2680 | const char AANoRecurse::ID = 0; |
| 2681 | const char AAWillReturn::ID = 0; |
| 2682 | const char AANoAlias::ID = 0; |
| 2683 | const char AANoReturn::ID = 0; |
| 2684 | const char AAIsDead::ID = 0; |
| 2685 | const char AADereferenceable::ID = 0; |
| 2686 | const char AAAlign::ID = 0; |
| 2687 | |
| Johannes Doerfert | aade782 | 2019-06-05 03:02:24 +0000 | [diff] [blame] | 2688 | INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", |
| 2689 | "Deduce and propagate attributes", false, false) |
| 2690 | INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", |
| 2691 | "Deduce and propagate attributes", false, false) |