8128705670dabe10af26efaacafecf1fc95ab790
[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/CallSite.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/Support/Compiler.h"
24
25 namespace llvm {
26
27 class GCRelocateOperands;
28 class ImmutableStatepoint;
29
30 bool isStatepoint(const ImmutableCallSite &CS);
31 bool isStatepoint(const Value *inst);
32 bool isStatepoint(const Value &inst);
33
34 bool isGCRelocate(const Value *inst);
35 bool isGCRelocate(const ImmutableCallSite &CS);
36
37 bool isGCResult(const Value *inst);
38 bool isGCResult(const ImmutableCallSite &CS);
39
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;
53
54  protected:
55   explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
56     assert(isStatepoint(I));
57   }
58   explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
59     assert(isStatepoint(CS));
60   }
61
62  public:
63   typedef typename CallSiteTy::arg_iterator arg_iterator;
64
65   /// Return the underlying CallSite.
66   CallSiteTy getCallSite() {
67     return StatepointCS;
68   }
69
70   /// Return the value actually being called or invoked.
71   ValueTy *actualCallee() {
72     return StatepointCS.getArgument(0);
73   }
74   /// Number of arguments to be passed to the actual callee.
75   int numCallArgs() {
76     return cast<ConstantInt>(StatepointCS.getArgument(1))->getZExtValue();
77   }
78   /// Number of additional arguments excluding those intended
79   /// for garbage collection.
80   int numTotalVMSArgs() {
81     return cast<ConstantInt>(StatepointCS.getArgument(3 + numCallArgs()))->getZExtValue();
82   }
83
84   typename CallSiteTy::arg_iterator call_args_begin() {
85     // 3 = callTarget, #callArgs, flag
86     int Offset = 3;
87     assert(Offset <= (int)StatepointCS.arg_size());
88     return StatepointCS.arg_begin() + Offset;
89   }
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;
94   }
95
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());
99   }
100
101   typename CallSiteTy::arg_iterator vm_state_begin() {
102     return call_args_end();
103   }
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;
108   }
109
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());
113   }
114
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;
119   }
120
121   typename CallSiteTy::arg_iterator gc_args_begin() {
122     return vm_state_end();
123   }
124   typename CallSiteTy::arg_iterator gc_args_end() {
125     return StatepointCS.arg_end();
126   }
127
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());
131   }
132
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
136   /// path of invoke.
137   std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
138
139 #ifndef NDEBUG
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.
143   void verify() {
144     assert(numCallArgs() >= 0 &&
145            "number of arguments to actually callee can't be negative");
146
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();
153     (void)gc_args_end();
154   }
155 #endif
156 };
157
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,
162                             ImmutableCallSite> {
163   typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
164       Base;
165
166 public:
167   explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
168   explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
169 };
170
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;
175
176 public:
177   explicit Statepoint(Instruction *I) : Base(I) {}
178   explicit Statepoint(CallSite CS) : Base(CS) {}
179 };
180
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;
186
187  public:
188   GCRelocateOperands(const User* U) : RelocateCS(U) {
189     assert(isGCRelocate(U));
190   }
191   GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
192     assert(isGCRelocate(inst));
193   }
194   GCRelocateOperands(CallSite CS) : RelocateCS(CS) {
195     assert(isGCRelocate(CS));
196   }
197
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);
202
203     return isa<ExtractValueInst>(Token) ||
204       isa<InvokeInst>(Token);
205   }
206
207   /// Get enclosed relocate intrinsic
208   ImmutableCallSite getUnderlyingCallSite() {
209     return RelocateCS;
210   }
211
212   /// The statepoint with which this gc.relocate is associated.
213   const Instruction *statepoint() {
214     const Value *token = RelocateCS.getArgument(0);
215
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);
220     }
221
222     // This relocate is on exceptional path of an invoke statepoint
223     const BasicBlock *invokeBB =
224       cast<Instruction>(token)->getParent()->getUniquePredecessor();
225
226     assert(invokeBB && "safepoints should have unique landingpads");
227     assert(invokeBB->getTerminator() && "safepoint block should be well formed");
228     assert(isStatepoint(invokeBB->getTerminator()));
229
230     return invokeBB->getTerminator();
231   }
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();
237   }
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();
242   }
243   Value *basePtr() {
244     ImmutableCallSite CS(statepoint());
245     return *(CS.arg_begin() + basePtrIndex());
246   }
247   Value *derivedPtr() {
248     ImmutableCallSite CS(statepoint());
249     return *(CS.arg_begin() + derivedPtrIndex());
250   }
251 };
252
253 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
254 std::vector<GCRelocateOperands>
255   StatepointBase<InstructionTy, ValueTy, CallSiteTy>::
256     getRelocates(ImmutableStatepoint &IS) {
257
258   std::vector<GCRelocateOperands> res;
259
260   ImmutableCallSite StatepointCS = IS.getCallSite();
261
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));
268     }
269   }
270
271   if (!StatepointCS.isInvoke()) {
272     return res;
273   }
274
275   // We need to scan thorough exceptional relocations if it is invoke statepoint
276   LandingPadInst *LandingPad =
277     cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
278
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)) {
283       continue;
284     }
285
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));
290       }
291     }
292   }
293   return res;
294 }
295
296 }
297 #endif