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/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/Support/Compiler.h"
27 /// The statepoint intrinsic accepts a set of flags as its third argument.
28 /// Valid values come out of this set.
29 enum class StatepointFlags {
31 GCTransition = 1, ///< Indicates that this statepoint is a transition from
32 ///< GC-aware code to code that is not GC-aware.
34 MaskAll = GCTransition ///< A bitmask that includes all valid flags.
37 class GCRelocateOperands;
38 class ImmutableStatepoint;
40 bool isStatepoint(const ImmutableCallSite &CS);
41 bool isStatepoint(const Value *inst);
42 bool isStatepoint(const Value &inst);
44 bool isGCRelocate(const Value *inst);
45 bool isGCRelocate(const ImmutableCallSite &CS);
47 bool isGCResult(const Value *inst);
48 bool isGCResult(const ImmutableCallSite &CS);
50 /// Analogous to CallSiteBase, this provides most of the actual
51 /// functionality for Statepoint and ImmutableStatepoint. It is
52 /// templatized to allow easily specializing of const and non-const
53 /// concrete subtypes. This is structured analogous to CallSite
54 /// rather than the IntrinsicInst.h helpers since we want to support
55 /// invokable statepoints in the near future.
56 /// TODO: This does not currently allow the if(Statepoint S = ...)
57 /// idiom used with CallSites. Consider refactoring to support.
58 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
59 class StatepointBase {
60 CallSiteTy StatepointCS;
61 void *operator new(size_t, unsigned) = delete;
62 void *operator new(size_t s) = delete;
65 explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
66 assert(isStatepoint(I));
68 explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
69 assert(isStatepoint(CS));
73 typedef typename CallSiteTy::arg_iterator arg_iterator;
81 /// Return the underlying CallSite.
82 CallSiteTy getCallSite() { return StatepointCS; }
84 uint64_t getFlags() const {
85 return cast<ConstantInt>(StatepointCS.getArgument(2))->getZExtValue();
88 /// Return the value actually being called or invoked.
89 ValueTy *getActualCallee() {
90 return StatepointCS.getArgument(ActualCalleePos);
93 /// Return the type of the value returned by the call underlying the
95 Type *getActualReturnType() {
96 auto *FTy = cast<FunctionType>(
97 cast<PointerType>(getActualCallee()->getType())->getElementType());
98 return FTy->getReturnType();
101 /// Number of arguments to be passed to the actual callee.
102 int getNumCallArgs() {
103 const Value *NumCallArgsVal = StatepointCS.getArgument(NumCallArgsPos);
104 return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
107 typename CallSiteTy::arg_iterator call_args_begin() {
108 assert(CallArgsBeginPos <= (int)StatepointCS.arg_size());
109 return StatepointCS.arg_begin() + CallArgsBeginPos;
111 typename CallSiteTy::arg_iterator call_args_end() {
112 int Offset = CallArgsBeginPos + getNumCallArgs();
113 assert(Offset <= (int)StatepointCS.arg_size());
114 return StatepointCS.arg_begin() + Offset;
117 /// range adapter for call arguments
118 iterator_range<arg_iterator> call_args() {
119 return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
122 /// Number of GC transition args.
123 int getNumTotalGCTransitionArgs() {
124 const Value *NumGCTransitionArgs = *gc_transition_args_begin();
125 return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
127 typename CallSiteTy::arg_iterator gc_transition_args_begin() {
128 int Offset = call_args_end() - StatepointCS.arg_begin();
129 assert(Offset <= (int)StatepointCS.arg_size());
130 return StatepointCS.arg_begin() + Offset;
132 typename CallSiteTy::arg_iterator gc_transition_args_end() {
133 int Offset = (gc_transition_args_begin() + 1 +
134 getNumTotalGCTransitionArgs()) - StatepointCS.arg_begin();
135 assert(Offset <= (int)StatepointCS.arg_size());
136 return StatepointCS.arg_begin() + Offset;
139 /// range adapter for GC transition arguments
140 iterator_range<arg_iterator> gc_transition_args() {
141 return iterator_range<arg_iterator>(gc_transition_args_begin(),
142 gc_transition_args_end());
145 /// Number of additional arguments excluding those intended
146 /// for garbage collection.
147 int getNumTotalVMSArgs() {
148 Value *NumVMSArgs = *vm_state_begin();
149 return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
152 typename CallSiteTy::arg_iterator vm_state_begin() {
153 return gc_transition_args_end();
155 typename CallSiteTy::arg_iterator vm_state_end() {
156 int Offset = (gc_transition_args_end() + 1 + getNumTotalVMSArgs()) -
157 StatepointCS.arg_begin();
158 assert(Offset <= (int)StatepointCS.arg_size());
159 return StatepointCS.arg_begin() + Offset;
162 /// range adapter for vm state arguments
163 iterator_range<arg_iterator> vm_state_args() {
164 return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
167 typename CallSiteTy::arg_iterator gc_args_begin() { return vm_state_end(); }
168 typename CallSiteTy::arg_iterator gc_args_end() {
169 return StatepointCS.arg_end();
172 /// range adapter for gc arguments
173 iterator_range<arg_iterator> gc_args() {
174 return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
177 /// Get list of all gc reloactes linked to this statepoint
178 /// May contain several relocations for the same base/derived pair.
179 /// For example this could happen due to relocations on unwinding
181 std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
184 /// Asserts if this statepoint is malformed. Common cases for failure
185 /// include incorrect length prefixes for variable length sections or
186 /// illegal values for parameters.
188 assert(getNumCallArgs() >= 0 &&
189 "number of arguments to actually callee can't be negative");
191 // The internal asserts in the iterator accessors do the rest.
192 (void)call_args_begin();
193 (void)call_args_end();
194 (void)gc_transition_args_begin();
195 (void)gc_transition_args_end();
196 (void)vm_state_begin();
197 (void)vm_state_end();
198 (void)gc_args_begin();
204 /// A specialization of it's base class for read only access
205 /// to a gc.statepoint.
206 class ImmutableStatepoint
207 : public StatepointBase<const Instruction, const Value, ImmutableCallSite> {
208 typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
212 explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
213 explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
216 /// A specialization of it's base class for read-write access
217 /// to a gc.statepoint.
218 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
219 typedef StatepointBase<Instruction, Value, CallSite> Base;
222 explicit Statepoint(Instruction *I) : Base(I) {}
223 explicit Statepoint(CallSite CS) : Base(CS) {}
226 /// Wraps a call to a gc.relocate and provides access to it's operands.
227 /// TODO: This should likely be refactored to resememble the wrappers in
228 /// InstrinsicInst.h.
229 class GCRelocateOperands {
230 ImmutableCallSite RelocateCS;
233 GCRelocateOperands(const User *U) : RelocateCS(U) { assert(isGCRelocate(U)); }
234 GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
235 assert(isGCRelocate(inst));
237 GCRelocateOperands(CallSite CS) : RelocateCS(CS) { assert(isGCRelocate(CS)); }
239 /// Return true if this relocate is tied to the invoke statepoint.
240 /// This includes relocates which are on the unwinding path.
241 bool isTiedToInvoke() const {
242 const Value *Token = RelocateCS.getArgument(0);
244 return isa<ExtractValueInst>(Token) || isa<InvokeInst>(Token);
247 /// Get enclosed relocate intrinsic
248 ImmutableCallSite getUnderlyingCallSite() { return RelocateCS; }
250 /// The statepoint with which this gc.relocate is associated.
251 const Instruction *getStatepoint() {
252 const Value *Token = RelocateCS.getArgument(0);
254 // This takes care both of relocates for call statepoints and relocates
255 // on normal path of invoke statepoint.
256 if (!isa<ExtractValueInst>(Token)) {
257 return cast<Instruction>(Token);
260 // This relocate is on exceptional path of an invoke statepoint
261 const BasicBlock *InvokeBB =
262 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
264 assert(InvokeBB && "safepoints should have unique landingpads");
265 assert(InvokeBB->getTerminator() &&
266 "safepoint block should be well formed");
267 assert(isStatepoint(InvokeBB->getTerminator()));
269 return InvokeBB->getTerminator();
272 /// The index into the associate statepoint's argument list
273 /// which contains the base pointer of the pointer whose
274 /// relocation this gc.relocate describes.
275 unsigned getBasePtrIndex() {
276 return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
279 /// The index into the associate statepoint's argument list which
280 /// contains the pointer whose relocation this gc.relocate describes.
281 unsigned getDerivedPtrIndex() {
282 return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
285 Value *getBasePtr() {
286 ImmutableCallSite CS(getStatepoint());
287 return *(CS.arg_begin() + getBasePtrIndex());
290 Value *getDerivedPtr() {
291 ImmutableCallSite CS(getStatepoint());
292 return *(CS.arg_begin() + getDerivedPtrIndex());
296 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
297 std::vector<GCRelocateOperands>
298 StatepointBase<InstructionTy, ValueTy, CallSiteTy>::getRelocates(
299 ImmutableStatepoint &IS) {
301 std::vector<GCRelocateOperands> Result;
303 ImmutableCallSite StatepointCS = IS.getCallSite();
305 // Search for relocated pointers. Note that working backwards from the
306 // gc_relocates ensures that we only get pairs which are actually relocated
307 // and used after the statepoint.
308 for (const User *U : StatepointCS.getInstruction()->users())
310 Result.push_back(GCRelocateOperands(U));
312 if (!StatepointCS.isInvoke())
315 // We need to scan thorough exceptional relocations if it is invoke statepoint
316 LandingPadInst *LandingPad =
317 cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
319 // Search for extract value from landingpad instruction to which
320 // gc relocates will be attached
321 for (const User *LandingPadUser : LandingPad->users()) {
322 if (!isa<ExtractValueInst>(LandingPadUser))
325 // gc relocates should be attached to this extract value
326 for (const User *U : LandingPadUser->users())
328 Result.push_back(GCRelocateOperands(U));