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