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, and
12 // gc.result intrinsics
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_STATEPOINT_H
17 #define LLVM_IR_STATEPOINT_H
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/Support/Compiler.h"
29 /// The statepoint intrinsic accepts a set of flags as its third argument.
30 /// Valid values come out of this set.
31 enum class StatepointFlags {
33 GCTransition = 1, ///< Indicates that this statepoint is a transition from
34 ///< GC-aware code to code that is not GC-aware.
36 MaskAll = GCTransition ///< A bitmask that includes all valid flags.
39 class GCRelocateOperands;
40 class ImmutableStatepoint;
42 bool isStatepoint(const ImmutableCallSite &CS);
43 bool isStatepoint(const Value *V);
44 bool isStatepoint(const Value &V);
46 bool isGCRelocate(const Value *V);
47 bool isGCRelocate(const ImmutableCallSite &CS);
49 bool isGCResult(const Value *V);
50 bool isGCResult(const ImmutableCallSite &CS);
52 /// Analogous to CallSiteBase, this provides most of the actual
53 /// functionality for Statepoint and ImmutableStatepoint. It is
54 /// templatized to allow easily specializing of const and non-const
55 /// concrete subtypes. This is structured analogous to CallSite
56 /// rather than the IntrinsicInst.h helpers since we want to support
57 /// invokable statepoints in the near future.
58 template <typename FunTy, typename InstructionTy, typename ValueTy,
60 class StatepointBase {
61 CallSiteTy StatepointCS;
62 void *operator new(size_t, unsigned) = delete;
63 void *operator new(size_t s) = delete;
66 explicit StatepointBase(InstructionTy *I) {
67 if (isStatepoint(I)) {
68 StatepointCS = CallSiteTy(I);
69 assert(StatepointCS && "isStatepoint implies CallSite");
72 explicit StatepointBase(CallSiteTy CS) {
78 typedef typename CallSiteTy::arg_iterator arg_iterator;
83 CalledFunctionPos = 2,
89 explicit operator bool() const {
90 // We do not assign non-statepoint CallSites to StatepointCS.
91 return (bool)StatepointCS;
94 /// Return the underlying CallSite.
95 CallSiteTy getCallSite() const {
96 assert(*this && "check validity first!");
100 uint64_t getFlags() const {
101 return cast<ConstantInt>(getCallSite().getArgument(FlagsPos))
105 /// Return the ID associated with this statepoint.
106 uint64_t getID() const {
107 const Value *IDVal = getCallSite().getArgument(IDPos);
108 return cast<ConstantInt>(IDVal)->getZExtValue();
111 /// Return the number of patchable bytes associated with this statepoint.
112 uint32_t getNumPatchBytes() const {
113 const Value *NumPatchBytesVal = getCallSite().getArgument(NumPatchBytesPos);
114 uint64_t NumPatchBytes =
115 cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
116 assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
117 return NumPatchBytes;
120 /// Return the value actually being called or invoked.
121 ValueTy *getCalledValue() const {
122 return getCallSite().getArgument(CalledFunctionPos);
125 InstructionTy *getInstruction() const {
126 return getCallSite().getInstruction();
129 /// Return the function being called if this is a direct call, otherwise
130 /// return null (if it's an indirect call).
131 FunTy *getCalledFunction() const {
132 return dyn_cast<Function>(getCalledValue());
135 /// Return the caller function for this statepoint.
136 FunTy *getCaller() const { return getCallSite().getCaller(); }
138 /// Determine if the statepoint cannot unwind.
139 bool doesNotThrow() const {
140 Function *F = getCalledFunction();
141 return getCallSite().doesNotThrow() || (F ? F->doesNotThrow() : false);
144 /// Return the type of the value returned by the call underlying the
146 Type *getActualReturnType() const {
147 auto *FTy = cast<FunctionType>(
148 cast<PointerType>(getCalledValue()->getType())->getElementType());
149 return FTy->getReturnType();
152 /// Number of arguments to be passed to the actual callee.
153 int getNumCallArgs() const {
154 const Value *NumCallArgsVal = getCallSite().getArgument(NumCallArgsPos);
155 return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
158 size_t arg_size() const { return getNumCallArgs(); }
159 typename CallSiteTy::arg_iterator arg_begin() const {
160 assert(CallArgsBeginPos <= (int)getCallSite().arg_size());
161 return getCallSite().arg_begin() + CallArgsBeginPos;
163 typename CallSiteTy::arg_iterator arg_end() const {
164 auto I = arg_begin() + arg_size();
165 assert((getCallSite().arg_end() - I) >= 0);
169 ValueTy *getArgument(unsigned Index) {
170 assert(Index < arg_size() && "out of bounds!");
171 return *(arg_begin() + Index);
174 /// range adapter for call arguments
175 iterator_range<arg_iterator> call_args() const {
176 return iterator_range<arg_iterator>(arg_begin(), arg_end());
179 /// \brief Return true if the call or the callee has the given attribute.
180 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
181 Function *F = getCalledFunction();
182 return getCallSite().paramHasAttr(i + CallArgsBeginPos, A) ||
183 (F ? F->getAttributes().hasAttribute(i, A) : false);
186 /// Number of GC transition args.
187 int getNumTotalGCTransitionArgs() const {
188 const Value *NumGCTransitionArgs = *arg_end();
189 return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
191 typename CallSiteTy::arg_iterator gc_transition_args_begin() const {
192 auto I = arg_end() + 1;
193 assert((getCallSite().arg_end() - I) >= 0);
196 typename CallSiteTy::arg_iterator gc_transition_args_end() const {
197 auto I = gc_transition_args_begin() + getNumTotalGCTransitionArgs();
198 assert((getCallSite().arg_end() - I) >= 0);
202 /// range adapter for GC transition arguments
203 iterator_range<arg_iterator> gc_transition_args() const {
204 return iterator_range<arg_iterator>(gc_transition_args_begin(),
205 gc_transition_args_end());
208 /// Number of additional arguments excluding those intended
209 /// for garbage collection.
210 int getNumTotalVMSArgs() const {
211 const Value *NumVMSArgs = *gc_transition_args_end();
212 return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
215 typename CallSiteTy::arg_iterator vm_state_begin() const {
216 auto I = gc_transition_args_end() + 1;
217 assert((getCallSite().arg_end() - I) >= 0);
220 typename CallSiteTy::arg_iterator vm_state_end() const {
221 auto I = vm_state_begin() + getNumTotalVMSArgs();
222 assert((getCallSite().arg_end() - I) >= 0);
226 /// range adapter for vm state arguments
227 iterator_range<arg_iterator> vm_state_args() const {
228 return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
231 typename CallSiteTy::arg_iterator gc_args_begin() const {
232 return vm_state_end();
234 typename CallSiteTy::arg_iterator gc_args_end() const {
235 return getCallSite().arg_end();
238 unsigned gcArgsStartIdx() const {
239 return gc_args_begin() - getInstruction()->op_begin();
242 /// range adapter for gc arguments
243 iterator_range<arg_iterator> gc_args() const {
244 return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
247 /// Get list of all gc reloactes linked to this statepoint
248 /// May contain several relocations for the same base/derived pair.
249 /// For example this could happen due to relocations on unwinding
251 std::vector<GCRelocateOperands> getRelocates() const;
253 /// Get the experimental_gc_result call tied to this statepoint. Can be
254 /// nullptr if there isn't a gc_result tied to this statepoint. Guaranteed to
255 /// be a CallInst if non-null.
256 InstructionTy *getGCResult() const {
257 for (auto *U : getInstruction()->users())
259 return cast<CallInst>(U);
265 /// Asserts if this statepoint is malformed. Common cases for failure
266 /// include incorrect length prefixes for variable length sections or
267 /// illegal values for parameters.
269 assert(getNumCallArgs() >= 0 &&
270 "number of arguments to actually callee can't be negative");
272 // The internal asserts in the iterator accessors do the rest.
275 (void)gc_transition_args_begin();
276 (void)gc_transition_args_end();
277 (void)vm_state_begin();
278 (void)vm_state_end();
279 (void)gc_args_begin();
285 /// A specialization of it's base class for read only access
286 /// to a gc.statepoint.
287 class ImmutableStatepoint
288 : public StatepointBase<const Function, const Instruction, const Value,
290 typedef StatepointBase<const Function, const Instruction, const Value,
291 ImmutableCallSite> Base;
294 explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
295 explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
298 /// A specialization of it's base class for read-write access
299 /// to a gc.statepoint.
301 : public StatepointBase<Function, Instruction, Value, CallSite> {
302 typedef StatepointBase<Function, Instruction, Value, CallSite> Base;
305 explicit Statepoint(Instruction *I) : Base(I) {}
306 explicit Statepoint(CallSite CS) : Base(CS) {}
309 /// Wraps a call to a gc.relocate and provides access to it's operands.
310 /// TODO: This should likely be refactored to resememble the wrappers in
311 /// InstrinsicInst.h.
312 class GCRelocateOperands {
313 ImmutableCallSite RelocateCS;
316 GCRelocateOperands(const User *U) : RelocateCS(U) { assert(isGCRelocate(U)); }
317 GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
318 assert(isGCRelocate(inst));
320 GCRelocateOperands(CallSite CS) : RelocateCS(CS) { assert(isGCRelocate(CS)); }
322 /// Return true if this relocate is tied to the invoke statepoint.
323 /// This includes relocates which are on the unwinding path.
324 bool isTiedToInvoke() const {
325 const Value *Token = RelocateCS.getArgument(0);
327 return isa<ExtractValueInst>(Token) || isa<InvokeInst>(Token);
330 /// Get enclosed relocate intrinsic
331 ImmutableCallSite getUnderlyingCallSite() { return RelocateCS; }
333 /// The statepoint with which this gc.relocate is associated.
334 const Instruction *getStatepoint() {
335 const Value *Token = RelocateCS.getArgument(0);
337 // This takes care both of relocates for call statepoints and relocates
338 // on normal path of invoke statepoint.
339 if (!isa<ExtractValueInst>(Token)) {
340 return cast<Instruction>(Token);
343 // This relocate is on exceptional path of an invoke statepoint
344 const BasicBlock *InvokeBB =
345 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
347 assert(InvokeBB && "safepoints should have unique landingpads");
348 assert(InvokeBB->getTerminator() &&
349 "safepoint block should be well formed");
350 assert(isStatepoint(InvokeBB->getTerminator()));
352 return InvokeBB->getTerminator();
355 /// The index into the associate statepoint's argument list
356 /// which contains the base pointer of the pointer whose
357 /// relocation this gc.relocate describes.
358 unsigned getBasePtrIndex() {
359 return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
362 /// The index into the associate statepoint's argument list which
363 /// contains the pointer whose relocation this gc.relocate describes.
364 unsigned getDerivedPtrIndex() {
365 return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
368 Value *getBasePtr() {
369 ImmutableCallSite CS(getStatepoint());
370 return *(CS.arg_begin() + getBasePtrIndex());
373 Value *getDerivedPtr() {
374 ImmutableCallSite CS(getStatepoint());
375 return *(CS.arg_begin() + getDerivedPtrIndex());
379 template <typename FunTy, typename InstructionTy, typename ValueTy,
381 std::vector<GCRelocateOperands>
382 StatepointBase<FunTy, InstructionTy, ValueTy, CallSiteTy>::getRelocates()
385 std::vector<GCRelocateOperands> Result;
387 CallSiteTy StatepointCS = getCallSite();
389 // Search for relocated pointers. Note that working backwards from the
390 // gc_relocates ensures that we only get pairs which are actually relocated
391 // and used after the statepoint.
392 for (const User *U : getInstruction()->users())
394 Result.push_back(GCRelocateOperands(U));
396 if (!StatepointCS.isInvoke())
399 // We need to scan thorough exceptional relocations if it is invoke statepoint
400 LandingPadInst *LandingPad =
401 cast<InvokeInst>(getInstruction())->getLandingPadInst();
403 // Search for extract value from landingpad instruction to which
404 // gc relocates will be attached
405 for (const User *LandingPadUser : LandingPad->users()) {
406 if (!isa<ExtractValueInst>(LandingPadUser))
409 // gc relocates should be attached to this extract value
410 for (const User *U : LandingPadUser->users())
412 Result.push_back(GCRelocateOperands(U));