1 //===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains utility functions and a wrapper class analogous to
11 // CallSite for accessing the fields of gc.statepoint, gc.relocate,
12 // gc.result intrinsics; and some general utilities helpful when dealing with
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_STATEPOINT_H
18 #define LLVM_IR_STATEPOINT_H
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/CallSite.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/MathExtras.h"
40 /// The statepoint intrinsic accepts a set of flags as its third argument.
41 /// Valid values come out of this set.
42 enum class StatepointFlags {
44 GCTransition = 1, ///< Indicates that this statepoint is a transition from
45 ///< GC-aware code to code that is not GC-aware.
46 /// Mark the deopt arguments associated with the statepoint as only being
47 /// "live-in". By default, deopt arguments are "live-through". "live-through"
48 /// requires that they the value be live on entry, on exit, and at any point
49 /// during the call. "live-in" only requires the value be available at the
50 /// start of the call. In particular, "live-in" values can be placed in
51 /// unused argument registers or other non-callee saved registers.
54 MaskAll = 3 ///< A bitmask that includes all valid flags.
60 bool isStatepoint(ImmutableCallSite CS);
61 bool isStatepoint(const Value *V);
62 bool isStatepoint(const Value &V);
64 bool isGCRelocate(ImmutableCallSite CS);
65 bool isGCRelocate(const Value *V);
67 bool isGCResult(ImmutableCallSite CS);
68 bool isGCResult(const Value *V);
70 /// Analogous to CallSiteBase, this provides most of the actual
71 /// functionality for Statepoint and ImmutableStatepoint. It is
72 /// templatized to allow easily specializing of const and non-const
73 /// concrete subtypes. This is structured analogous to CallSite
74 /// rather than the IntrinsicInst.h helpers since we need to support
75 /// invokable statepoints.
76 template <typename FunTy, typename InstructionTy, typename ValueTy,
78 class StatepointBase {
79 CallSiteTy StatepointCS;
82 explicit StatepointBase(InstructionTy *I) {
83 if (isStatepoint(I)) {
84 StatepointCS = CallSiteTy(I);
85 assert(StatepointCS && "isStatepoint implies CallSite");
89 explicit StatepointBase(CallSiteTy CS) {
95 using arg_iterator = typename CallSiteTy::arg_iterator;
100 CalledFunctionPos = 2,
103 CallArgsBeginPos = 5,
106 void *operator new(size_t, unsigned) = delete;
107 void *operator new(size_t s) = delete;
109 explicit operator bool() const {
110 // We do not assign non-statepoint CallSites to StatepointCS.
111 return (bool)StatepointCS;
114 /// Return the underlying CallSite.
115 CallSiteTy getCallSite() const {
116 assert(*this && "check validity first!");
120 uint64_t getFlags() const {
121 return cast<ConstantInt>(getCallSite().getArgument(FlagsPos))
125 /// Return the ID associated with this statepoint.
126 uint64_t getID() const {
127 const Value *IDVal = getCallSite().getArgument(IDPos);
128 return cast<ConstantInt>(IDVal)->getZExtValue();
131 /// Return the number of patchable bytes associated with this statepoint.
132 uint32_t getNumPatchBytes() const {
133 const Value *NumPatchBytesVal = getCallSite().getArgument(NumPatchBytesPos);
134 uint64_t NumPatchBytes =
135 cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
136 assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
137 return NumPatchBytes;
140 /// Return the value actually being called or invoked.
141 ValueTy *getCalledValue() const {
142 return getCallSite().getArgument(CalledFunctionPos);
145 InstructionTy *getInstruction() const {
146 return getCallSite().getInstruction();
149 /// Return the function being called if this is a direct call, otherwise
150 /// return null (if it's an indirect call).
151 FunTy *getCalledFunction() const {
152 return dyn_cast<Function>(getCalledValue());
155 /// Return the caller function for this statepoint.
156 FunTy *getCaller() const { return getCallSite().getCaller(); }
158 /// Determine if the statepoint cannot unwind.
159 bool doesNotThrow() const {
160 Function *F = getCalledFunction();
161 return getCallSite().doesNotThrow() || (F ? F->doesNotThrow() : false);
164 /// Return the type of the value returned by the call underlying the
166 Type *getActualReturnType() const {
167 auto *FTy = cast<FunctionType>(
168 cast<PointerType>(getCalledValue()->getType())->getElementType());
169 return FTy->getReturnType();
172 /// Number of arguments to be passed to the actual callee.
173 int getNumCallArgs() const {
174 const Value *NumCallArgsVal = getCallSite().getArgument(NumCallArgsPos);
175 return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
178 size_t arg_size() const { return getNumCallArgs(); }
179 typename CallSiteTy::arg_iterator arg_begin() const {
180 assert(CallArgsBeginPos <= (int)getCallSite().arg_size());
181 return getCallSite().arg_begin() + CallArgsBeginPos;
183 typename CallSiteTy::arg_iterator arg_end() const {
184 auto I = arg_begin() + arg_size();
185 assert((getCallSite().arg_end() - I) >= 0);
189 ValueTy *getArgument(unsigned Index) {
190 assert(Index < arg_size() && "out of bounds!");
191 return *(arg_begin() + Index);
194 /// range adapter for call arguments
195 iterator_range<arg_iterator> call_args() const {
196 return make_range(arg_begin(), arg_end());
199 /// \brief Return true if the call or the callee has the given attribute.
200 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
201 Function *F = getCalledFunction();
202 return getCallSite().paramHasAttr(i + CallArgsBeginPos, A) ||
203 (F ? F->getAttributes().hasAttribute(i, A) : false);
206 /// Number of GC transition args.
207 int getNumTotalGCTransitionArgs() const {
208 const Value *NumGCTransitionArgs = *arg_end();
209 return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
211 typename CallSiteTy::arg_iterator gc_transition_args_begin() const {
212 auto I = arg_end() + 1;
213 assert((getCallSite().arg_end() - I) >= 0);
216 typename CallSiteTy::arg_iterator gc_transition_args_end() const {
217 auto I = gc_transition_args_begin() + getNumTotalGCTransitionArgs();
218 assert((getCallSite().arg_end() - I) >= 0);
222 /// range adapter for GC transition arguments
223 iterator_range<arg_iterator> gc_transition_args() const {
224 return make_range(gc_transition_args_begin(), gc_transition_args_end());
227 /// Number of additional arguments excluding those intended
228 /// for garbage collection.
229 int getNumTotalVMSArgs() const {
230 const Value *NumVMSArgs = *gc_transition_args_end();
231 return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
234 typename CallSiteTy::arg_iterator deopt_begin() const {
235 auto I = gc_transition_args_end() + 1;
236 assert((getCallSite().arg_end() - I) >= 0);
239 typename CallSiteTy::arg_iterator deopt_end() const {
240 auto I = deopt_begin() + getNumTotalVMSArgs();
241 assert((getCallSite().arg_end() - I) >= 0);
245 /// range adapter for vm state arguments
246 iterator_range<arg_iterator> deopt_operands() const {
247 return make_range(deopt_begin(), deopt_end());
250 typename CallSiteTy::arg_iterator gc_args_begin() const {
253 typename CallSiteTy::arg_iterator gc_args_end() const {
254 return getCallSite().arg_end();
257 unsigned gcArgsStartIdx() const {
258 return gc_args_begin() - getInstruction()->op_begin();
261 /// range adapter for gc arguments
262 iterator_range<arg_iterator> gc_args() const {
263 return make_range(gc_args_begin(), gc_args_end());
266 /// Get list of all gc reloactes linked to this statepoint
267 /// May contain several relocations for the same base/derived pair.
268 /// For example this could happen due to relocations on unwinding
270 std::vector<const GCRelocateInst *> getRelocates() const;
272 /// Get the experimental_gc_result call tied to this statepoint. Can be
273 /// nullptr if there isn't a gc_result tied to this statepoint. Guaranteed to
274 /// be a CallInst if non-null.
275 const GCResultInst *getGCResult() const {
276 for (auto *U : getInstruction()->users())
277 if (auto *GRI = dyn_cast<GCResultInst>(U))
283 /// Asserts if this statepoint is malformed. Common cases for failure
284 /// include incorrect length prefixes for variable length sections or
285 /// illegal values for parameters.
287 assert(getNumCallArgs() >= 0 &&
288 "number of arguments to actually callee can't be negative");
290 // The internal asserts in the iterator accessors do the rest.
293 (void)gc_transition_args_begin();
294 (void)gc_transition_args_end();
297 (void)gc_args_begin();
303 /// A specialization of it's base class for read only access
304 /// to a gc.statepoint.
305 class ImmutableStatepoint
306 : public StatepointBase<const Function, const Instruction, const Value,
309 StatepointBase<const Function, const Instruction, const Value,
313 explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
314 explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
317 /// A specialization of it's base class for read-write access
318 /// to a gc.statepoint.
320 : public StatepointBase<Function, Instruction, Value, CallSite> {
321 using Base = StatepointBase<Function, Instruction, Value, CallSite>;
324 explicit Statepoint(Instruction *I) : Base(I) {}
325 explicit Statepoint(CallSite CS) : Base(CS) {}
328 /// Common base class for representing values projected from a statepoint.
329 /// Currently, the only projections available are gc.result and gc.relocate.
330 class GCProjectionInst : public IntrinsicInst {
332 static bool classof(const IntrinsicInst *I) {
333 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
334 I->getIntrinsicID() == Intrinsic::experimental_gc_result;
337 static bool classof(const Value *V) {
338 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
341 /// Return true if this relocate is tied to the invoke statepoint.
342 /// This includes relocates which are on the unwinding path.
343 bool isTiedToInvoke() const {
344 const Value *Token = getArgOperand(0);
346 return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
349 /// The statepoint with which this gc.relocate is associated.
350 const Instruction *getStatepoint() const {
351 const Value *Token = getArgOperand(0);
353 // This takes care both of relocates for call statepoints and relocates
354 // on normal path of invoke statepoint.
355 if (!isa<LandingPadInst>(Token)) {
356 assert(isStatepoint(Token));
357 return cast<Instruction>(Token);
360 // This relocate is on exceptional path of an invoke statepoint
361 const BasicBlock *InvokeBB =
362 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
364 assert(InvokeBB && "safepoints should have unique landingpads");
365 assert(InvokeBB->getTerminator() &&
366 "safepoint block should be well formed");
367 assert(isStatepoint(InvokeBB->getTerminator()));
369 return InvokeBB->getTerminator();
373 /// Represents calls to the gc.relocate intrinsic.
374 class GCRelocateInst : public GCProjectionInst {
376 static bool classof(const IntrinsicInst *I) {
377 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
380 static bool classof(const Value *V) {
381 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
384 /// The index into the associate statepoint's argument list
385 /// which contains the base pointer of the pointer whose
386 /// relocation this gc.relocate describes.
387 unsigned getBasePtrIndex() const {
388 return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
391 /// The index into the associate statepoint's argument list which
392 /// contains the pointer whose relocation this gc.relocate describes.
393 unsigned getDerivedPtrIndex() const {
394 return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
397 Value *getBasePtr() const {
398 ImmutableCallSite CS(getStatepoint());
399 return *(CS.arg_begin() + getBasePtrIndex());
402 Value *getDerivedPtr() const {
403 ImmutableCallSite CS(getStatepoint());
404 return *(CS.arg_begin() + getDerivedPtrIndex());
408 /// Represents calls to the gc.result intrinsic.
409 class GCResultInst : public GCProjectionInst {
411 static bool classof(const IntrinsicInst *I) {
412 return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
415 static bool classof(const Value *V) {
416 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
420 template <typename FunTy, typename InstructionTy, typename ValueTy,
422 std::vector<const GCRelocateInst *>
423 StatepointBase<FunTy, InstructionTy, ValueTy, CallSiteTy>::getRelocates()
426 std::vector<const GCRelocateInst *> Result;
428 CallSiteTy StatepointCS = getCallSite();
430 // Search for relocated pointers. Note that working backwards from the
431 // gc_relocates ensures that we only get pairs which are actually relocated
432 // and used after the statepoint.
433 for (const User *U : getInstruction()->users())
434 if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
435 Result.push_back(Relocate);
437 if (!StatepointCS.isInvoke())
440 // We need to scan thorough exceptional relocations if it is invoke statepoint
441 LandingPadInst *LandingPad =
442 cast<InvokeInst>(getInstruction())->getLandingPadInst();
444 // Search for gc relocates that are attached to this landingpad.
445 for (const User *LandingPadUser : LandingPad->users()) {
446 if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
447 Result.push_back(Relocate);
452 /// Call sites that get wrapped by a gc.statepoint (currently only in
453 /// RewriteStatepointsForGC and potentially in other passes in the future) can
454 /// have attributes that describe properties of gc.statepoint call they will be
455 /// eventually be wrapped in. This struct is used represent such directives.
456 struct StatepointDirectives {
457 Optional<uint32_t> NumPatchBytes;
458 Optional<uint64_t> StatepointID;
460 static const uint64_t DefaultStatepointID = 0xABCDEF00;
461 static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
464 /// Parse out statepoint directives from the function attributes present in \p
466 StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
468 /// Return \c true if the the \p Attr is an attribute that is a statepoint
470 bool isStatepointDirectiveAttr(Attribute Attr);
472 } // end namespace llvm
474 #endif // LLVM_IR_STATEPOINT_H