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/CallSite.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/Support/Compiler.h"
27 class GCRelocateOperands;
28 class ImmutableStatepoint;
30 bool isStatepoint(const ImmutableCallSite &CS);
31 bool isStatepoint(const Value *inst);
32 bool isStatepoint(const Value &inst);
34 bool isGCRelocate(const Value *inst);
35 bool isGCRelocate(const ImmutableCallSite &CS);
37 bool isGCResult(const Value *inst);
38 bool isGCResult(const ImmutableCallSite &CS);
40 /// Analogous to CallSiteBase, this provides most of the actual
41 /// functionality for Statepoint and ImmutableStatepoint. It is
42 /// templatized to allow easily specializing of const and non-const
43 /// concrete subtypes. This is structured analogous to CallSite
44 /// rather than the IntrinsicInst.h helpers since we want to support
45 /// invokable statepoints in the near future.
46 /// TODO: This does not currently allow the if(Statepoint S = ...)
47 /// idiom used with CallSites. Consider refactoring to support.
48 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
49 class StatepointBase {
50 CallSiteTy StatepointCS;
51 void *operator new(size_t, unsigned) = delete;
52 void *operator new(size_t s) = delete;
55 explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
56 assert(isStatepoint(I));
58 explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
59 assert(isStatepoint(CS));
63 typedef typename CallSiteTy::arg_iterator arg_iterator;
65 /// Return the underlying CallSite.
66 CallSiteTy getCallSite() {
70 /// Return the value actually being called or invoked.
71 ValueTy *actualCallee() {
72 return StatepointCS.getArgument(0);
74 /// Number of arguments to be passed to the actual callee.
76 return cast<ConstantInt>(StatepointCS.getArgument(1))->getZExtValue();
78 /// Number of additional arguments excluding those intended
79 /// for garbage collection.
80 int numTotalVMSArgs() {
81 return cast<ConstantInt>(StatepointCS.getArgument(3 + numCallArgs()))->getZExtValue();
84 typename CallSiteTy::arg_iterator call_args_begin() {
85 // 3 = callTarget, #callArgs, flag
87 assert(Offset <= (int)StatepointCS.arg_size());
88 return StatepointCS.arg_begin() + Offset;
90 typename CallSiteTy::arg_iterator call_args_end() {
91 int Offset = 3 + numCallArgs();
92 assert(Offset <= (int)StatepointCS.arg_size());
93 return StatepointCS.arg_begin() + Offset;
96 /// range adapter for call arguments
97 iterator_range<arg_iterator> call_args() {
98 return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
101 typename CallSiteTy::arg_iterator vm_state_begin() {
102 return call_args_end();
104 typename CallSiteTy::arg_iterator vm_state_end() {
105 int Offset = 3 + numCallArgs() + 1 + numTotalVMSArgs();
106 assert(Offset <= (int)StatepointCS.arg_size());
107 return StatepointCS.arg_begin() + Offset;
110 /// range adapter for vm state arguments
111 iterator_range<arg_iterator> vm_state_args() {
112 return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
115 typename CallSiteTy::arg_iterator first_vm_state_stack_begin() {
116 // 6 = numTotalVMSArgs, 1st_objectID, 1st_bci,
117 // 1st_#stack, 1st_#local, 1st_#monitor
118 return vm_state_begin() + 6;
121 typename CallSiteTy::arg_iterator gc_args_begin() {
122 return vm_state_end();
124 typename CallSiteTy::arg_iterator gc_args_end() {
125 return StatepointCS.arg_end();
128 /// range adapter for gc arguments
129 iterator_range<arg_iterator> gc_args() {
130 return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
133 /// Get list of all gc reloactes linked to this statepoint
134 /// May contain several relocations for the same base/derived pair.
135 /// For example this could happen due to relocations on unwinding
137 std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
140 /// Asserts if this statepoint is malformed. Common cases for failure
141 /// include incorrect length prefixes for variable length sections or
142 /// illegal values for parameters.
144 assert(numCallArgs() >= 0 &&
145 "number of arguments to actually callee can't be negative");
147 // The internal asserts in the iterator accessors do the rest.
148 (void)call_args_begin();
149 (void)call_args_end();
150 (void)vm_state_begin();
151 (void)vm_state_end();
152 (void)gc_args_begin();
158 /// A specialization of it's base class for read only access
159 /// to a gc.statepoint.
160 class ImmutableStatepoint
161 : public StatepointBase<const Instruction, const Value,
163 typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
167 explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
168 explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
171 /// A specialization of it's base class for read-write access
172 /// to a gc.statepoint.
173 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
174 typedef StatepointBase<Instruction, Value, CallSite> Base;
177 explicit Statepoint(Instruction *I) : Base(I) {}
178 explicit Statepoint(CallSite CS) : Base(CS) {}
181 /// Wraps a call to a gc.relocate and provides access to it's operands.
182 /// TODO: This should likely be refactored to resememble the wrappers in
183 /// InstrinsicInst.h.
184 class GCRelocateOperands {
185 ImmutableCallSite RelocateCS;
188 GCRelocateOperands(const User* U) : RelocateCS(U) {
189 assert(isGCRelocate(U));
191 GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
192 assert(isGCRelocate(inst));
194 GCRelocateOperands(CallSite CS) : RelocateCS(CS) {
195 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) ||
204 isa<InvokeInst>(Token);
207 /// Get enclosed relocate intrinsic
208 ImmutableCallSite getUnderlyingCallSite() {
212 /// The statepoint with which this gc.relocate is associated.
213 const Instruction *statepoint() {
214 const Value *token = RelocateCS.getArgument(0);
216 // This takes care both of relocates for call statepoints and relocates
217 // on normal path of invoke statepoint.
218 if (!isa<ExtractValueInst>(token)) {
219 return cast<Instruction>(token);
222 // This relocate is on exceptional path of an invoke statepoint
223 const BasicBlock *invokeBB =
224 cast<Instruction>(token)->getParent()->getUniquePredecessor();
226 assert(invokeBB && "safepoints should have unique landingpads");
227 assert(invokeBB->getTerminator() && "safepoint block should be well formed");
228 assert(isStatepoint(invokeBB->getTerminator()));
230 return invokeBB->getTerminator();
232 /// The index into the associate statepoint's argument list
233 /// which contains the base pointer of the pointer whose
234 /// relocation this gc.relocate describes.
235 unsigned basePtrIndex() {
236 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 derivedPtrIndex() {
241 return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
244 ImmutableCallSite CS(statepoint());
245 return *(CS.arg_begin() + basePtrIndex());
247 Value *derivedPtr() {
248 ImmutableCallSite CS(statepoint());
249 return *(CS.arg_begin() + derivedPtrIndex());
253 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
254 std::vector<GCRelocateOperands>
255 StatepointBase<InstructionTy, ValueTy, CallSiteTy>::
256 getRelocates(ImmutableStatepoint &IS) {
258 std::vector<GCRelocateOperands> res;
260 ImmutableCallSite StatepointCS = IS.getCallSite();
262 // Search for relocated pointers. Note that working backwards from the
263 // gc_relocates ensures that we only get pairs which are actually relocated
264 // and used after the statepoint.
265 for (const User *U : StatepointCS.getInstruction()->users()) {
266 if (isGCRelocate(U)) {
267 res.push_back(GCRelocateOperands(U));
271 if (!StatepointCS.isInvoke()) {
275 // We need to scan thorough exceptional relocations if it is invoke statepoint
276 LandingPadInst *LandingPad =
277 cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
279 // Search for extract value from landingpad instruction to which
280 // gc relocates will be attached
281 for (const User *LandingPadUser : LandingPad->users()) {
282 if (!isa<ExtractValueInst>(LandingPadUser)) {
286 // gc relocates should be attached to this extract value
287 for (const User *U : LandingPadUser->users()) {
288 if (isGCRelocate(U)) {
289 res.push_back(GCRelocateOperands(U));