Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains utility functions and a wrapper class analogous to |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 10 | // CallBase for accessing the fields of gc.statepoint, gc.relocate, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 11 | // gc.result intrinsics; and some general utilities helpful when dealing with |
| 12 | // gc.statepoint. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_IR_STATEPOINT_H |
| 17 | #define LLVM_IR_STATEPOINT_H |
| 18 | |
| 19 | #include "llvm/ADT/Optional.h" |
| 20 | #include "llvm/ADT/iterator_range.h" |
| 21 | #include "llvm/IR/Attributes.h" |
| 22 | #include "llvm/IR/BasicBlock.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/Function.h" |
| 25 | #include "llvm/IR/Instruction.h" |
| 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/IntrinsicInst.h" |
| 28 | #include "llvm/IR/Intrinsics.h" |
| 29 | #include "llvm/Support/Casting.h" |
| 30 | #include "llvm/Support/MathExtras.h" |
| 31 | #include <cassert> |
| 32 | #include <cstddef> |
| 33 | #include <cstdint> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace llvm { |
| 37 | |
| 38 | /// The statepoint intrinsic accepts a set of flags as its third argument. |
| 39 | /// Valid values come out of this set. |
| 40 | enum class StatepointFlags { |
| 41 | None = 0, |
| 42 | GCTransition = 1, ///< Indicates that this statepoint is a transition from |
| 43 | ///< GC-aware code to code that is not GC-aware. |
| 44 | /// Mark the deopt arguments associated with the statepoint as only being |
| 45 | /// "live-in". By default, deopt arguments are "live-through". "live-through" |
| 46 | /// requires that they the value be live on entry, on exit, and at any point |
| 47 | /// during the call. "live-in" only requires the value be available at the |
| 48 | /// start of the call. In particular, "live-in" values can be placed in |
| 49 | /// unused argument registers or other non-callee saved registers. |
| 50 | DeoptLiveIn = 2, |
| 51 | |
| 52 | MaskAll = 3 ///< A bitmask that includes all valid flags. |
| 53 | }; |
| 54 | |
| 55 | class GCRelocateInst; |
| 56 | class GCResultInst; |
| 57 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 58 | bool isStatepoint(const CallBase *Call); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | bool isStatepoint(const Value *V); |
| 60 | bool isStatepoint(const Value &V); |
| 61 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 62 | bool isGCRelocate(const CallBase *Call); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 63 | bool isGCRelocate(const Value *V); |
| 64 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 65 | bool isGCResult(const CallBase *Call); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 66 | bool isGCResult(const Value *V); |
| 67 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 68 | /// A wrapper around a GC intrinsic call, this provides most of the actual |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 69 | /// functionality for Statepoint and ImmutableStatepoint. It is |
| 70 | /// templatized to allow easily specializing of const and non-const |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 71 | /// concrete subtypes. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | template <typename FunTy, typename InstructionTy, typename ValueTy, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 73 | typename CallBaseTy> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | class StatepointBase { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 75 | CallBaseTy *StatepointCall; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | |
| 77 | protected: |
| 78 | explicit StatepointBase(InstructionTy *I) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 79 | StatepointCall = isStatepoint(I) ? cast<CallBaseTy>(I) : nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 82 | explicit StatepointBase(CallBaseTy *Call) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 83 | StatepointCall = isStatepoint(Call) ? Call : nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 87 | using arg_iterator = typename CallBaseTy::const_op_iterator; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | |
| 89 | enum { |
| 90 | IDPos = 0, |
| 91 | NumPatchBytesPos = 1, |
| 92 | CalledFunctionPos = 2, |
| 93 | NumCallArgsPos = 3, |
| 94 | FlagsPos = 4, |
| 95 | CallArgsBeginPos = 5, |
| 96 | }; |
| 97 | |
| 98 | void *operator new(size_t, unsigned) = delete; |
| 99 | void *operator new(size_t s) = delete; |
| 100 | |
| 101 | explicit operator bool() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 102 | // We do not assign non-statepoint call instructions to StatepointCall. |
| 103 | return (bool)StatepointCall; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 106 | /// Return the underlying call instruction. |
| 107 | CallBaseTy *getCall() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 108 | assert(*this && "check validity first!"); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 109 | return StatepointCall; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | uint64_t getFlags() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 113 | return cast<ConstantInt>(getCall()->getArgOperand(FlagsPos)) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 114 | ->getZExtValue(); |
| 115 | } |
| 116 | |
| 117 | /// Return the ID associated with this statepoint. |
| 118 | uint64_t getID() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 119 | const Value *IDVal = getCall()->getArgOperand(IDPos); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | return cast<ConstantInt>(IDVal)->getZExtValue(); |
| 121 | } |
| 122 | |
| 123 | /// Return the number of patchable bytes associated with this statepoint. |
| 124 | uint32_t getNumPatchBytes() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 125 | const Value *NumPatchBytesVal = getCall()->getArgOperand(NumPatchBytesPos); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | uint64_t NumPatchBytes = |
| 127 | cast<ConstantInt>(NumPatchBytesVal)->getZExtValue(); |
| 128 | assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!"); |
| 129 | return NumPatchBytes; |
| 130 | } |
| 131 | |
| 132 | /// Return the value actually being called or invoked. |
| 133 | ValueTy *getCalledValue() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 134 | return getCall()->getArgOperand(CalledFunctionPos); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 137 | // FIXME: Migrate users of this to `getCall` and remove it. |
| 138 | InstructionTy *getInstruction() const { return getCall(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 139 | |
| 140 | /// Return the function being called if this is a direct call, otherwise |
| 141 | /// return null (if it's an indirect call). |
| 142 | FunTy *getCalledFunction() const { |
| 143 | return dyn_cast<Function>(getCalledValue()); |
| 144 | } |
| 145 | |
| 146 | /// Return the caller function for this statepoint. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 147 | FunTy *getCaller() const { return getCall()->getCaller(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 148 | |
| 149 | /// Determine if the statepoint cannot unwind. |
| 150 | bool doesNotThrow() const { |
| 151 | Function *F = getCalledFunction(); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 152 | return getCall()->doesNotThrow() || (F ? F->doesNotThrow() : false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /// Return the type of the value returned by the call underlying the |
| 156 | /// statepoint. |
| 157 | Type *getActualReturnType() const { |
| 158 | auto *FTy = cast<FunctionType>( |
| 159 | cast<PointerType>(getCalledValue()->getType())->getElementType()); |
| 160 | return FTy->getReturnType(); |
| 161 | } |
| 162 | |
| 163 | /// Number of arguments to be passed to the actual callee. |
| 164 | int getNumCallArgs() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 165 | const Value *NumCallArgsVal = getCall()->getArgOperand(NumCallArgsPos); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 166 | return cast<ConstantInt>(NumCallArgsVal)->getZExtValue(); |
| 167 | } |
| 168 | |
| 169 | size_t arg_size() const { return getNumCallArgs(); } |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 170 | arg_iterator arg_begin() const { |
| 171 | assert(CallArgsBeginPos <= (int)getCall()->arg_size()); |
| 172 | return getCall()->arg_begin() + CallArgsBeginPos; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 173 | } |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 174 | arg_iterator arg_end() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 175 | auto I = arg_begin() + arg_size(); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 176 | assert((getCall()->arg_end() - I) >= 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 177 | return I; |
| 178 | } |
| 179 | |
| 180 | ValueTy *getArgument(unsigned Index) { |
| 181 | assert(Index < arg_size() && "out of bounds!"); |
| 182 | return *(arg_begin() + Index); |
| 183 | } |
| 184 | |
| 185 | /// range adapter for call arguments |
| 186 | iterator_range<arg_iterator> call_args() const { |
| 187 | return make_range(arg_begin(), arg_end()); |
| 188 | } |
| 189 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 190 | /// Return true if the call or the callee has the given attribute. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 191 | bool paramHasAttr(unsigned i, Attribute::AttrKind A) const { |
| 192 | Function *F = getCalledFunction(); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 193 | return getCall()->paramHasAttr(i + CallArgsBeginPos, A) || |
| 194 | (F ? F->getAttributes().hasAttribute(i, A) : false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /// Number of GC transition args. |
| 198 | int getNumTotalGCTransitionArgs() const { |
| 199 | const Value *NumGCTransitionArgs = *arg_end(); |
| 200 | return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue(); |
| 201 | } |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 202 | arg_iterator gc_transition_args_begin() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 203 | auto I = arg_end() + 1; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 204 | assert((getCall()->arg_end() - I) >= 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 205 | return I; |
| 206 | } |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 207 | arg_iterator gc_transition_args_end() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 208 | auto I = gc_transition_args_begin() + getNumTotalGCTransitionArgs(); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 209 | assert((getCall()->arg_end() - I) >= 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 210 | return I; |
| 211 | } |
| 212 | |
| 213 | /// range adapter for GC transition arguments |
| 214 | iterator_range<arg_iterator> gc_transition_args() const { |
| 215 | return make_range(gc_transition_args_begin(), gc_transition_args_end()); |
| 216 | } |
| 217 | |
| 218 | /// Number of additional arguments excluding those intended |
| 219 | /// for garbage collection. |
| 220 | int getNumTotalVMSArgs() const { |
| 221 | const Value *NumVMSArgs = *gc_transition_args_end(); |
| 222 | return cast<ConstantInt>(NumVMSArgs)->getZExtValue(); |
| 223 | } |
| 224 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 225 | arg_iterator deopt_begin() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 226 | auto I = gc_transition_args_end() + 1; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 227 | assert((getCall()->arg_end() - I) >= 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 228 | return I; |
| 229 | } |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 230 | arg_iterator deopt_end() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 231 | auto I = deopt_begin() + getNumTotalVMSArgs(); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 232 | assert((getCall()->arg_end() - I) >= 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 233 | return I; |
| 234 | } |
| 235 | |
| 236 | /// range adapter for vm state arguments |
| 237 | iterator_range<arg_iterator> deopt_operands() const { |
| 238 | return make_range(deopt_begin(), deopt_end()); |
| 239 | } |
| 240 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 241 | arg_iterator gc_args_begin() const { return deopt_end(); } |
| 242 | arg_iterator gc_args_end() const { return getCall()->arg_end(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 243 | |
| 244 | unsigned gcArgsStartIdx() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 245 | return gc_args_begin() - getCall()->op_begin(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /// range adapter for gc arguments |
| 249 | iterator_range<arg_iterator> gc_args() const { |
| 250 | return make_range(gc_args_begin(), gc_args_end()); |
| 251 | } |
| 252 | |
| 253 | /// Get list of all gc reloactes linked to this statepoint |
| 254 | /// May contain several relocations for the same base/derived pair. |
| 255 | /// For example this could happen due to relocations on unwinding |
| 256 | /// path of invoke. |
| 257 | std::vector<const GCRelocateInst *> getRelocates() const; |
| 258 | |
| 259 | /// Get the experimental_gc_result call tied to this statepoint. Can be |
| 260 | /// nullptr if there isn't a gc_result tied to this statepoint. Guaranteed to |
| 261 | /// be a CallInst if non-null. |
| 262 | const GCResultInst *getGCResult() const { |
| 263 | for (auto *U : getInstruction()->users()) |
| 264 | if (auto *GRI = dyn_cast<GCResultInst>(U)) |
| 265 | return GRI; |
| 266 | return nullptr; |
| 267 | } |
| 268 | |
| 269 | #ifndef NDEBUG |
| 270 | /// Asserts if this statepoint is malformed. Common cases for failure |
| 271 | /// include incorrect length prefixes for variable length sections or |
| 272 | /// illegal values for parameters. |
| 273 | void verify() { |
| 274 | assert(getNumCallArgs() >= 0 && |
| 275 | "number of arguments to actually callee can't be negative"); |
| 276 | |
| 277 | // The internal asserts in the iterator accessors do the rest. |
| 278 | (void)arg_begin(); |
| 279 | (void)arg_end(); |
| 280 | (void)gc_transition_args_begin(); |
| 281 | (void)gc_transition_args_end(); |
| 282 | (void)deopt_begin(); |
| 283 | (void)deopt_end(); |
| 284 | (void)gc_args_begin(); |
| 285 | (void)gc_args_end(); |
| 286 | } |
| 287 | #endif |
| 288 | }; |
| 289 | |
| 290 | /// A specialization of it's base class for read only access |
| 291 | /// to a gc.statepoint. |
| 292 | class ImmutableStatepoint |
| 293 | : public StatepointBase<const Function, const Instruction, const Value, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 294 | const CallBase> { |
| 295 | using Base = StatepointBase<const Function, const Instruction, const Value, |
| 296 | const CallBase>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 297 | |
| 298 | public: |
| 299 | explicit ImmutableStatepoint(const Instruction *I) : Base(I) {} |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 300 | explicit ImmutableStatepoint(const CallBase *Call) : Base(Call) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | /// A specialization of it's base class for read-write access |
| 304 | /// to a gc.statepoint. |
| 305 | class Statepoint |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 306 | : public StatepointBase<Function, Instruction, Value, CallBase> { |
| 307 | using Base = StatepointBase<Function, Instruction, Value, CallBase>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 308 | |
| 309 | public: |
| 310 | explicit Statepoint(Instruction *I) : Base(I) {} |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 311 | explicit Statepoint(CallBase *Call) : Base(Call) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 312 | }; |
| 313 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 314 | /// Common base class for representing values projected from a statepoint. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 315 | /// Currently, the only projections available are gc.result and gc.relocate. |
| 316 | class GCProjectionInst : public IntrinsicInst { |
| 317 | public: |
| 318 | static bool classof(const IntrinsicInst *I) { |
| 319 | return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate || |
| 320 | I->getIntrinsicID() == Intrinsic::experimental_gc_result; |
| 321 | } |
| 322 | |
| 323 | static bool classof(const Value *V) { |
| 324 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 325 | } |
| 326 | |
| 327 | /// Return true if this relocate is tied to the invoke statepoint. |
| 328 | /// This includes relocates which are on the unwinding path. |
| 329 | bool isTiedToInvoke() const { |
| 330 | const Value *Token = getArgOperand(0); |
| 331 | |
| 332 | return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token); |
| 333 | } |
| 334 | |
| 335 | /// The statepoint with which this gc.relocate is associated. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 336 | const CallBase *getStatepoint() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 337 | const Value *Token = getArgOperand(0); |
| 338 | |
| 339 | // This takes care both of relocates for call statepoints and relocates |
| 340 | // on normal path of invoke statepoint. |
| 341 | if (!isa<LandingPadInst>(Token)) { |
| 342 | assert(isStatepoint(Token)); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 343 | return cast<CallBase>(Token); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // This relocate is on exceptional path of an invoke statepoint |
| 347 | const BasicBlock *InvokeBB = |
| 348 | cast<Instruction>(Token)->getParent()->getUniquePredecessor(); |
| 349 | |
| 350 | assert(InvokeBB && "safepoints should have unique landingpads"); |
| 351 | assert(InvokeBB->getTerminator() && |
| 352 | "safepoint block should be well formed"); |
| 353 | assert(isStatepoint(InvokeBB->getTerminator())); |
| 354 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 355 | return cast<CallBase>(InvokeBB->getTerminator()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 356 | } |
| 357 | }; |
| 358 | |
| 359 | /// Represents calls to the gc.relocate intrinsic. |
| 360 | class GCRelocateInst : public GCProjectionInst { |
| 361 | public: |
| 362 | static bool classof(const IntrinsicInst *I) { |
| 363 | return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate; |
| 364 | } |
| 365 | |
| 366 | static bool classof(const Value *V) { |
| 367 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 368 | } |
| 369 | |
| 370 | /// The index into the associate statepoint's argument list |
| 371 | /// which contains the base pointer of the pointer whose |
| 372 | /// relocation this gc.relocate describes. |
| 373 | unsigned getBasePtrIndex() const { |
| 374 | return cast<ConstantInt>(getArgOperand(1))->getZExtValue(); |
| 375 | } |
| 376 | |
| 377 | /// The index into the associate statepoint's argument list which |
| 378 | /// contains the pointer whose relocation this gc.relocate describes. |
| 379 | unsigned getDerivedPtrIndex() const { |
| 380 | return cast<ConstantInt>(getArgOperand(2))->getZExtValue(); |
| 381 | } |
| 382 | |
| 383 | Value *getBasePtr() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 384 | return *(getStatepoint()->arg_begin() + getBasePtrIndex()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | Value *getDerivedPtr() const { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 388 | return *(getStatepoint()->arg_begin() + getDerivedPtrIndex()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 389 | } |
| 390 | }; |
| 391 | |
| 392 | /// Represents calls to the gc.result intrinsic. |
| 393 | class GCResultInst : public GCProjectionInst { |
| 394 | public: |
| 395 | static bool classof(const IntrinsicInst *I) { |
| 396 | return I->getIntrinsicID() == Intrinsic::experimental_gc_result; |
| 397 | } |
| 398 | |
| 399 | static bool classof(const Value *V) { |
| 400 | return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | template <typename FunTy, typename InstructionTy, typename ValueTy, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 405 | typename CallBaseTy> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 406 | std::vector<const GCRelocateInst *> |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 407 | StatepointBase<FunTy, InstructionTy, ValueTy, CallBaseTy>::getRelocates() |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 408 | const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 409 | std::vector<const GCRelocateInst *> Result; |
| 410 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 411 | // Search for relocated pointers. Note that working backwards from the |
| 412 | // gc_relocates ensures that we only get pairs which are actually relocated |
| 413 | // and used after the statepoint. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 414 | for (const User *U : StatepointCall->users()) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 415 | if (auto *Relocate = dyn_cast<GCRelocateInst>(U)) |
| 416 | Result.push_back(Relocate); |
| 417 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 418 | auto *StatepointInvoke = dyn_cast<InvokeInst>(StatepointCall); |
| 419 | if (!StatepointInvoke) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 420 | return Result; |
| 421 | |
| 422 | // We need to scan thorough exceptional relocations if it is invoke statepoint |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 423 | LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 424 | |
| 425 | // Search for gc relocates that are attached to this landingpad. |
| 426 | for (const User *LandingPadUser : LandingPad->users()) { |
| 427 | if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser)) |
| 428 | Result.push_back(Relocate); |
| 429 | } |
| 430 | return Result; |
| 431 | } |
| 432 | |
| 433 | /// Call sites that get wrapped by a gc.statepoint (currently only in |
| 434 | /// RewriteStatepointsForGC and potentially in other passes in the future) can |
| 435 | /// have attributes that describe properties of gc.statepoint call they will be |
| 436 | /// eventually be wrapped in. This struct is used represent such directives. |
| 437 | struct StatepointDirectives { |
| 438 | Optional<uint32_t> NumPatchBytes; |
| 439 | Optional<uint64_t> StatepointID; |
| 440 | |
| 441 | static const uint64_t DefaultStatepointID = 0xABCDEF00; |
| 442 | static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F; |
| 443 | }; |
| 444 | |
| 445 | /// Parse out statepoint directives from the function attributes present in \p |
| 446 | /// AS. |
| 447 | StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS); |
| 448 | |
| 449 | /// Return \c true if the \p Attr is an attribute that is a statepoint |
| 450 | /// directive. |
| 451 | bool isStatepointDirectiveAttr(Attribute Attr); |
| 452 | |
| 453 | } // end namespace llvm |
| 454 | |
| 455 | #endif // LLVM_IR_STATEPOINT_H |