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;
66 /// Return the underlying CallSite.
67 CallSiteTy getCallSite() {
71 /// Return the value actually being called or invoked.
72 ValueTy *actualCallee() {
73 return StatepointCS.getArgument(0);
75 /// Number of arguments to be passed to the actual callee.
77 return cast<ConstantInt>(StatepointCS.getArgument(1))->getZExtValue();
79 /// Number of additional arguments excluding those intended
80 /// for garbage collection.
81 int numTotalVMSArgs() {
82 return cast<ConstantInt>(StatepointCS.getArgument(3 + numCallArgs()))->getZExtValue();
85 typename CallSiteTy::arg_iterator call_args_begin() {
86 // 3 = callTarget, #callArgs, flag
88 assert(Offset <= (int)StatepointCS.arg_size());
89 return StatepointCS.arg_begin() + Offset;
91 typename CallSiteTy::arg_iterator call_args_end() {
92 int Offset = 3 + numCallArgs();
93 assert(Offset <= (int)StatepointCS.arg_size());
94 return StatepointCS.arg_begin() + Offset;
97 /// range adapter for call arguments
98 iterator_range<arg_iterator> call_args() {
99 return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
102 typename CallSiteTy::arg_iterator vm_state_begin() {
103 return call_args_end();
105 typename CallSiteTy::arg_iterator vm_state_end() {
106 int Offset = 3 + numCallArgs() + 1 + numTotalVMSArgs();
107 assert(Offset <= (int)StatepointCS.arg_size());
108 return StatepointCS.arg_begin() + Offset;
111 /// range adapter for vm state arguments
112 iterator_range<arg_iterator> vm_state_args() {
113 return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
116 typename CallSiteTy::arg_iterator first_vm_state_stack_begin() {
117 // 6 = numTotalVMSArgs, 1st_objectID, 1st_bci,
118 // 1st_#stack, 1st_#local, 1st_#monitor
119 return vm_state_begin() + 6;
122 typename CallSiteTy::arg_iterator gc_args_begin() {
123 return vm_state_end();
125 typename CallSiteTy::arg_iterator gc_args_end() {
126 return StatepointCS.arg_end();
129 /// range adapter for gc arguments
130 iterator_range<arg_iterator> gc_args() {
131 return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
134 /// Get list of all gc reloactes linked to this statepoint
135 /// May contain several relocations for the same base/derived pair.
136 /// For example this could happen due to relocations on unwinding
138 std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
141 /// Asserts if this statepoint is malformed. Common cases for failure
142 /// include incorrect length prefixes for variable length sections or
143 /// illegal values for parameters.
145 assert(numCallArgs() >= 0 &&
146 "number of arguments to actually callee can't be negative");
148 // The internal asserts in the iterator accessors do the rest.
149 (void)call_args_begin();
150 (void)call_args_end();
151 (void)vm_state_begin();
152 (void)vm_state_end();
153 (void)gc_args_begin();
159 /// A specialization of it's base class for read only access
160 /// to a gc.statepoint.
161 class ImmutableStatepoint
162 : public StatepointBase<const Instruction, const Value,
164 typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
168 explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
169 explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
172 /// A specialization of it's base class for read-write access
173 /// to a gc.statepoint.
174 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
175 typedef StatepointBase<Instruction, Value, CallSite> Base;
178 explicit Statepoint(Instruction *I) : Base(I) {}
179 explicit Statepoint(CallSite CS) : Base(CS) {}
182 /// Wraps a call to a gc.relocate and provides access to it's operands.
183 /// TODO: This should likely be refactored to resememble the wrappers in
184 /// InstrinsicInst.h.
185 class GCRelocateOperands {
186 ImmutableCallSite RelocateCS;
189 GCRelocateOperands(const User* U) : RelocateCS(U) {
190 assert(isGCRelocate(U));
192 GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
193 assert(isGCRelocate(inst));
195 GCRelocateOperands(CallSite CS) : RelocateCS(CS) {
196 assert(isGCRelocate(CS));
199 /// Return true if this relocate is tied to the invoke statepoint.
200 /// This includes relocates which are on the unwinding path.
201 bool isTiedToInvoke() const {
202 const Value *Token = RelocateCS.getArgument(0);
204 return isa<ExtractValueInst>(Token) ||
205 isa<InvokeInst>(Token);
208 /// Get enclosed relocate intrinsic
209 ImmutableCallSite getUnderlyingCallSite() {
213 /// The statepoint with which this gc.relocate is associated.
214 const Instruction *statepoint() {
215 const Value *token = RelocateCS.getArgument(0);
217 // This takes care both of relocates for call statepoints and relocates
218 // on normal path of invoke statepoint.
219 if (!isa<ExtractValueInst>(token)) {
220 return cast<Instruction>(token);
223 // This relocate is on exceptional path of an invoke statepoint
224 const BasicBlock *invokeBB =
225 cast<Instruction>(token)->getParent()->getUniquePredecessor();
227 assert(invokeBB && "safepoints should have unique landingpads");
228 assert(invokeBB->getTerminator() && "safepoint block should be well formed");
229 assert(isStatepoint(invokeBB->getTerminator()));
231 return invokeBB->getTerminator();
233 /// The index into the associate statepoint's argument list
234 /// which contains the base pointer of the pointer whose
235 /// relocation this gc.relocate describes.
236 unsigned basePtrIndex() {
237 return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
239 /// The index into the associate statepoint's argument list which
240 /// contains the pointer whose relocation this gc.relocate describes.
241 unsigned derivedPtrIndex() {
242 return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
245 ImmutableCallSite CS(statepoint());
246 return *(CS.arg_begin() + basePtrIndex());
248 Value *derivedPtr() {
249 ImmutableCallSite CS(statepoint());
250 return *(CS.arg_begin() + derivedPtrIndex());
254 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
255 std::vector<GCRelocateOperands>
256 StatepointBase<InstructionTy, ValueTy, CallSiteTy>::
257 getRelocates(ImmutableStatepoint &IS) {
259 std::vector<GCRelocateOperands> res;
261 ImmutableCallSite StatepointCS = IS.getCallSite();
263 // Search for relocated pointers. Note that working backwards from the
264 // gc_relocates ensures that we only get pairs which are actually relocated
265 // and used after the statepoint.
266 for (const User *U : StatepointCS.getInstruction()->users()) {
267 if (isGCRelocate(U)) {
268 res.push_back(GCRelocateOperands(U));
272 if (!StatepointCS.isInvoke()) {
276 // We need to scan thorough exceptional relocations if it is invoke statepoint
277 LandingPadInst *LandingPad =
278 cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
280 // Search for extract value from landingpad instruction to which
281 // gc relocates will be attached
282 for (const User *LandingPadUser : LandingPad->users()) {
283 if (!isa<ExtractValueInst>(LandingPadUser)) {
287 // gc relocates should be attached to this extract value
288 for (const User *U : LandingPadUser->users()) {
289 if (isGCRelocate(U)) {
290 res.push_back(GCRelocateOperands(U));