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"
28 class GCRelocateOperands;
29 class ImmutableStatepoint;
31 bool isStatepoint(const ImmutableCallSite &CS);
32 bool isStatepoint(const Value *inst);
33 bool isStatepoint(const Value &inst);
35 bool isGCRelocate(const Value *inst);
36 bool isGCRelocate(const ImmutableCallSite &CS);
38 bool isGCResult(const Value *inst);
39 bool isGCResult(const ImmutableCallSite &CS);
41 /// Analogous to CallSiteBase, this provides most of the actual
42 /// functionality for Statepoint and ImmutableStatepoint. It is
43 /// templatized to allow easily specializing of const and non-const
44 /// concrete subtypes. This is structured analogous to CallSite
45 /// rather than the IntrinsicInst.h helpers since we want to support
46 /// invokable statepoints in the near future.
47 /// TODO: This does not currently allow the if(Statepoint S = ...)
48 /// idiom used with CallSites. Consider refactoring to support.
49 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
50 class StatepointBase {
51 CallSiteTy StatepointCS;
52 void *operator new(size_t, unsigned) = delete;
53 void *operator new(size_t s) = delete;
56 explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
57 assert(isStatepoint(I));
59 explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
60 assert(isStatepoint(CS));
64 typedef typename CallSiteTy::arg_iterator arg_iterator;
72 /// Return the underlying CallSite.
73 CallSiteTy getCallSite() { return StatepointCS; }
75 /// Return the value actually being called or invoked.
76 ValueTy *getActualCallee() {
77 return StatepointCS.getArgument(ActualCalleePos);
80 /// Return the type of the value returned by the call underlying the
82 Type *getActualReturnType() {
83 auto *FTy = cast<FunctionType>(
84 cast<PointerType>(getActualCallee()->getType())->getElementType());
85 return FTy->getReturnType();
88 /// Number of arguments to be passed to the actual callee.
89 int getNumCallArgs() {
90 const Value *NumCallArgsVal = StatepointCS.getArgument(NumCallArgsPos);
91 return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
94 typename CallSiteTy::arg_iterator call_args_begin() {
95 assert(CallArgsBeginPos <= (int)StatepointCS.arg_size());
96 return StatepointCS.arg_begin() + CallArgsBeginPos;
98 typename CallSiteTy::arg_iterator call_args_end() {
99 int Offset = CallArgsBeginPos + getNumCallArgs();
100 assert(Offset <= (int)StatepointCS.arg_size());
101 return StatepointCS.arg_begin() + Offset;
104 /// range adapter for call arguments
105 iterator_range<arg_iterator> call_args() {
106 return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
109 /// Number of additional arguments excluding those intended
110 /// for garbage collection.
111 int getNumTotalVMSArgs() {
112 Value *NumVMSArgs = *call_args_end();
113 return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
116 typename CallSiteTy::arg_iterator vm_state_begin() { return call_args_end(); }
117 typename CallSiteTy::arg_iterator vm_state_end() {
118 int Offset = CallArgsBeginPos + getNumCallArgs() + 1 + getNumTotalVMSArgs();
119 assert(Offset <= (int)StatepointCS.arg_size());
120 return StatepointCS.arg_begin() + Offset;
123 /// range adapter for vm state arguments
124 iterator_range<arg_iterator> vm_state_args() {
125 return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
128 typename CallSiteTy::arg_iterator gc_args_begin() { return vm_state_end(); }
129 typename CallSiteTy::arg_iterator gc_args_end() {
130 return StatepointCS.arg_end();
133 /// range adapter for gc arguments
134 iterator_range<arg_iterator> gc_args() {
135 return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
138 /// Get list of all gc reloactes linked to this statepoint
139 /// May contain several relocations for the same base/derived pair.
140 /// For example this could happen due to relocations on unwinding
142 std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
145 /// Asserts if this statepoint is malformed. Common cases for failure
146 /// include incorrect length prefixes for variable length sections or
147 /// illegal values for parameters.
149 assert(getNumCallArgs() >= 0 &&
150 "number of arguments to actually callee can't be negative");
152 // The internal asserts in the iterator accessors do the rest.
153 (void)call_args_begin();
154 (void)call_args_end();
155 (void)vm_state_begin();
156 (void)vm_state_end();
157 (void)gc_args_begin();
163 /// A specialization of it's base class for read only access
164 /// to a gc.statepoint.
165 class ImmutableStatepoint
166 : public StatepointBase<const Instruction, const Value, ImmutableCallSite> {
167 typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
171 explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
172 explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
175 /// A specialization of it's base class for read-write access
176 /// to a gc.statepoint.
177 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
178 typedef StatepointBase<Instruction, Value, CallSite> Base;
181 explicit Statepoint(Instruction *I) : Base(I) {}
182 explicit Statepoint(CallSite CS) : Base(CS) {}
185 /// Wraps a call to a gc.relocate and provides access to it's operands.
186 /// TODO: This should likely be refactored to resememble the wrappers in
187 /// InstrinsicInst.h.
188 class GCRelocateOperands {
189 ImmutableCallSite RelocateCS;
192 GCRelocateOperands(const User *U) : RelocateCS(U) { assert(isGCRelocate(U)); }
193 GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
194 assert(isGCRelocate(inst));
196 GCRelocateOperands(CallSite CS) : RelocateCS(CS) { assert(isGCRelocate(CS)); }
198 /// Return true if this relocate is tied to the invoke statepoint.
199 /// This includes relocates which are on the unwinding path.
200 bool isTiedToInvoke() const {
201 const Value *Token = RelocateCS.getArgument(0);
203 return isa<ExtractValueInst>(Token) || isa<InvokeInst>(Token);
206 /// Get enclosed relocate intrinsic
207 ImmutableCallSite getUnderlyingCallSite() { return RelocateCS; }
209 /// The statepoint with which this gc.relocate is associated.
210 const Instruction *getStatepoint() {
211 const Value *Token = RelocateCS.getArgument(0);
213 // This takes care both of relocates for call statepoints and relocates
214 // on normal path of invoke statepoint.
215 if (!isa<ExtractValueInst>(Token)) {
216 return cast<Instruction>(Token);
219 // This relocate is on exceptional path of an invoke statepoint
220 const BasicBlock *InvokeBB =
221 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
223 assert(InvokeBB && "safepoints should have unique landingpads");
224 assert(InvokeBB->getTerminator() &&
225 "safepoint block should be well formed");
226 assert(isStatepoint(InvokeBB->getTerminator()));
228 return InvokeBB->getTerminator();
231 /// The index into the associate statepoint's argument list
232 /// which contains the base pointer of the pointer whose
233 /// relocation this gc.relocate describes.
234 unsigned getBasePtrIndex() {
235 return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
238 /// The index into the associate statepoint's argument list which
239 /// contains the pointer whose relocation this gc.relocate describes.
240 unsigned getDerivedPtrIndex() {
241 return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
244 Value *getBasePtr() {
245 ImmutableCallSite CS(getStatepoint());
246 return *(CS.arg_begin() + getBasePtrIndex());
249 Value *getDerivedPtr() {
250 ImmutableCallSite CS(getStatepoint());
251 return *(CS.arg_begin() + getDerivedPtrIndex());
255 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
256 std::vector<GCRelocateOperands>
257 StatepointBase<InstructionTy, ValueTy, CallSiteTy>::getRelocates(
258 ImmutableStatepoint &IS) {
260 std::vector<GCRelocateOperands> Result;
262 ImmutableCallSite StatepointCS = IS.getCallSite();
264 // Search for relocated pointers. Note that working backwards from the
265 // gc_relocates ensures that we only get pairs which are actually relocated
266 // and used after the statepoint.
267 for (const User *U : StatepointCS.getInstruction()->users())
269 Result.push_back(GCRelocateOperands(U));
271 if (!StatepointCS.isInvoke())
274 // We need to scan thorough exceptional relocations if it is invoke statepoint
275 LandingPadInst *LandingPad =
276 cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
278 // Search for extract value from landingpad instruction to which
279 // gc relocates will be attached
280 for (const User *LandingPadUser : LandingPad->users()) {
281 if (!isa<ExtractValueInst>(LandingPadUser))
284 // gc relocates should be attached to this extract value
285 for (const User *U : LandingPadUser->users())
287 Result.push_back(GCRelocateOperands(U));