035509c11e63f3f80eca6550b8215cb50ff65622
[oota-llvm.git] / include / llvm / IR / Statepoint.h
1 //===-- llvm/IR/Statepoint.h - gc.statepoint utilities ------ --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef __LLVM_IR_STATEPOINT_H
17 #define __LLVM_IR_STATEPOINT_H
18
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/CallSite.h"
23 #include "llvm/Support/Compiler.h"
24
25 namespace llvm {
26
27 bool isStatepoint(const ImmutableCallSite &CS);
28 bool isStatepoint(const Instruction *inst);
29 bool isStatepoint(const Instruction &inst);
30
31 bool isGCRelocate(const Instruction *inst);
32 bool isGCRelocate(const ImmutableCallSite &CS);
33
34 bool isGCResult(const Instruction *inst);
35 bool isGCResult(const ImmutableCallSite &CS);
36
37 /// Analogous to CallSiteBase, this provides most of the actual
38 /// functionality for Statepoint and ImmutableStatepoint.  It is
39 /// templatized to allow easily specializing of const and non-const
40 /// concrete subtypes.  This is structured analogous to CallSite
41 /// rather than the IntrinsicInst.h helpers since we want to support
42 /// invokable statepoints in the near future.
43 /// TODO: This does not currently allow the if(Statepoint S = ...)
44 ///   idiom used with CallSites.  Consider refactoring to support.
45 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
46 class StatepointBase {
47   CallSiteTy StatepointCS;
48   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
49   void *operator new(size_t s) LLVM_DELETED_FUNCTION;
50
51  protected:
52   explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
53     assert(isStatepoint(I));
54   }
55   explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
56     assert(isStatepoint(CS));
57   }
58
59  public:
60   typedef typename CallSiteTy::arg_iterator arg_iterator;
61
62   /// Return the underlying CallSite.
63   CallSiteTy getCallSite() {
64     return StatepointCS;
65   }
66
67   /// Return the value actually being called or invoked.
68   ValueTy *actualCallee() {
69     return StatepointCS.getArgument(0);
70   }
71   /// Number of arguments to be passed to the actual callee.
72   int numCallArgs() {
73     return cast<ConstantInt>(StatepointCS.getArgument(1))->getZExtValue();
74   }
75   /// Number of additional arguments excluding those intended
76   /// for garbage collection.
77   int numTotalVMSArgs() {
78     return cast<ConstantInt>(StatepointCS.getArgument(3 + numCallArgs()))->getZExtValue();
79   }
80
81   typename CallSiteTy::arg_iterator call_args_begin() {
82     // 3 = callTarget, #callArgs, flag
83     int Offset = 3;
84     assert(Offset <= (int)StatepointCS.arg_size());
85     return StatepointCS.arg_begin() + Offset;
86   }
87   typename CallSiteTy::arg_iterator call_args_end() {
88     int Offset = 3 + numCallArgs();
89     assert(Offset <= (int)StatepointCS.arg_size());
90     return StatepointCS.arg_begin() + Offset;
91   }
92
93   /// range adapter for call arguments
94   iterator_range<arg_iterator> call_args() {
95     return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
96   }
97
98   typename CallSiteTy::arg_iterator vm_state_begin() {
99     return call_args_end();
100   }
101   typename CallSiteTy::arg_iterator vm_state_end() {
102     int Offset = 3 + numCallArgs() + 1 + numTotalVMSArgs();
103     assert(Offset <= (int)StatepointCS.arg_size());
104     return StatepointCS.arg_begin() + Offset;
105   }
106
107   /// range adapter for vm state arguments
108   iterator_range<arg_iterator> vm_state_args() {
109     return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
110   }
111
112   typename CallSiteTy::arg_iterator first_vm_state_stack_begin() {
113     // 6 = numTotalVMSArgs, 1st_objectID, 1st_bci,
114     //     1st_#stack, 1st_#local, 1st_#monitor
115     return vm_state_begin() + 6;
116   }
117
118   typename CallSiteTy::arg_iterator gc_args_begin() {
119     return vm_state_end();
120   }
121   typename CallSiteTy::arg_iterator gc_args_end() {
122     return StatepointCS.arg_end();
123   }
124
125   /// range adapter for gc arguments
126   iterator_range<arg_iterator> gc_args() {
127     return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
128   }
129
130
131 #ifndef NDEBUG
132   /// Asserts if this statepoint is malformed.  Common cases for failure
133   /// include incorrect length prefixes for variable length sections or
134   /// illegal values for parameters.
135   void verify() {
136     assert(numCallArgs() >= 0 &&
137            "number of arguments to actually callee can't be negative");
138
139     // The internal asserts in the iterator accessors do the rest.
140     (void)call_args_begin();
141     (void)call_args_end();
142     (void)vm_state_begin();
143     (void)vm_state_end();
144     (void)gc_args_begin();
145     (void)gc_args_end();
146   }
147 #endif
148 };
149
150 /// A specialization of it's base class for read only access
151 /// to a gc.statepoint.
152 class ImmutableStatepoint
153     : public StatepointBase<const Instruction, const Value,
154                             ImmutableCallSite> {
155   typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
156       Base;
157
158 public:
159   explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
160   explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
161 };
162
163 /// A specialization of it's base class for read-write access
164 /// to a gc.statepoint.
165 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
166   typedef StatepointBase<Instruction, Value, CallSite> Base;
167
168 public:
169   explicit Statepoint(Instruction *I) : Base(I) {}
170   explicit Statepoint(CallSite CS) : Base(CS) {}
171 };
172
173 /// Wraps a call to a gc.relocate and provides access to it's operands.
174 /// TODO: This should likely be refactored to resememble the wrappers in
175 /// InstrinsicInst.h.
176 class GCRelocateOperands {
177   ImmutableCallSite RelocateCS;
178
179  public:
180   GCRelocateOperands(const User* U)
181     : GCRelocateOperands(cast<Instruction>(U)) {}
182   GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
183     assert(isGCRelocate(inst));
184   }
185   GCRelocateOperands(CallSite CS) : RelocateCS(CS) {
186     assert(isGCRelocate(CS));
187   }
188
189   /// The statepoint with which this gc.relocate is associated.
190   const Instruction *statepoint() {
191     return cast<Instruction>(RelocateCS.getArgument(0));
192   }
193   /// The index into the associate statepoint's argument list
194   /// which contains the base pointer of the pointer whose
195   /// relocation this gc.relocate describes.
196   int basePtrIndex() {
197     return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
198   }
199   /// The index into the associate statepoint's argument list which
200   /// contains the pointer whose relocation this gc.relocate describes.
201   int derivedPtrIndex() {
202     return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
203   }
204   const Value *basePtr() {
205     ImmutableCallSite CS(statepoint());
206     return *(CS.arg_begin() + basePtrIndex());
207   }
208   const Value *derivedPtr() {
209     ImmutableCallSite CS(statepoint());
210     return *(CS.arg_begin() + derivedPtrIndex());
211   }
212 };
213 }
214 #endif