Extend the statepoint intrinsic to allow statepoints to be marked as transitions...
[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/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"
25
26 namespace llvm {
27 /// The statepoint intrinsic accepts a set of flags as its third argument.
28 /// Valid values come out of this set.
29 enum class StatepointFlags {
30   None = 0,
31   GCTransition = 1, ///< Indicates that this statepoint is a transition from
32                     ///< GC-aware code to code that is not GC-aware.
33
34   MaskAll = GCTransition ///< A bitmask that includes all valid flags.
35 };
36
37 class GCRelocateOperands;
38 class ImmutableStatepoint;
39
40 bool isStatepoint(const ImmutableCallSite &CS);
41 bool isStatepoint(const Value *inst);
42 bool isStatepoint(const Value &inst);
43
44 bool isGCRelocate(const Value *inst);
45 bool isGCRelocate(const ImmutableCallSite &CS);
46
47 bool isGCResult(const Value *inst);
48 bool isGCResult(const ImmutableCallSite &CS);
49
50 /// Analogous to CallSiteBase, this provides most of the actual
51 /// functionality for Statepoint and ImmutableStatepoint.  It is
52 /// templatized to allow easily specializing of const and non-const
53 /// concrete subtypes.  This is structured analogous to CallSite
54 /// rather than the IntrinsicInst.h helpers since we want to support
55 /// invokable statepoints in the near future.
56 /// TODO: This does not currently allow the if(Statepoint S = ...)
57 ///   idiom used with CallSites.  Consider refactoring to support.
58 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
59 class StatepointBase {
60   CallSiteTy StatepointCS;
61   void *operator new(size_t, unsigned) = delete;
62   void *operator new(size_t s) = delete;
63
64 protected:
65   explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
66     assert(isStatepoint(I));
67   }
68   explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
69     assert(isStatepoint(CS));
70   }
71
72 public:
73   typedef typename CallSiteTy::arg_iterator arg_iterator;
74
75   enum {
76     ActualCalleePos = 0,
77     NumCallArgsPos = 1,
78     CallArgsBeginPos = 3,
79   };
80
81   /// Return the underlying CallSite.
82   CallSiteTy getCallSite() { return StatepointCS; }
83
84   uint64_t getFlags() const {
85     return cast<ConstantInt>(StatepointCS.getArgument(2))->getZExtValue();
86   }
87
88   /// Return the value actually being called or invoked.
89   ValueTy *getActualCallee() {
90     return StatepointCS.getArgument(ActualCalleePos);
91   }
92
93   /// Return the type of the value returned by the call underlying the
94   /// statepoint.
95   Type *getActualReturnType() {
96     auto *FTy = cast<FunctionType>(
97         cast<PointerType>(getActualCallee()->getType())->getElementType());
98     return FTy->getReturnType();
99   }
100
101   /// Number of arguments to be passed to the actual callee.
102   int getNumCallArgs() {
103     const Value *NumCallArgsVal = StatepointCS.getArgument(NumCallArgsPos);
104     return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
105   }
106
107   typename CallSiteTy::arg_iterator call_args_begin() {
108     assert(CallArgsBeginPos <= (int)StatepointCS.arg_size());
109     return StatepointCS.arg_begin() + CallArgsBeginPos;
110   }
111   typename CallSiteTy::arg_iterator call_args_end() {
112     int Offset = CallArgsBeginPos + getNumCallArgs();
113     assert(Offset <= (int)StatepointCS.arg_size());
114     return StatepointCS.arg_begin() + Offset;
115   }
116
117   /// range adapter for call arguments
118   iterator_range<arg_iterator> call_args() {
119     return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
120   }
121
122   /// Number of GC transition args.
123   int getNumTotalGCTransitionArgs() {
124     const Value *NumGCTransitionArgs = *gc_transition_args_begin();
125     return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
126   }
127   typename CallSiteTy::arg_iterator gc_transition_args_begin() {
128     int Offset = call_args_end() - StatepointCS.arg_begin();
129     assert(Offset <= (int)StatepointCS.arg_size());
130     return StatepointCS.arg_begin() + Offset;
131   }
132   typename CallSiteTy::arg_iterator gc_transition_args_end() {
133     int Offset = (gc_transition_args_begin() + 1 +
134                   getNumTotalGCTransitionArgs()) - StatepointCS.arg_begin();
135     assert(Offset <= (int)StatepointCS.arg_size());
136     return StatepointCS.arg_begin() + Offset;
137   }
138
139   /// range adapter for GC transition arguments
140   iterator_range<arg_iterator> gc_transition_args() {
141     return iterator_range<arg_iterator>(gc_transition_args_begin(),
142                                         gc_transition_args_end());
143   }
144
145   /// Number of additional arguments excluding those intended
146   /// for garbage collection.
147   int getNumTotalVMSArgs() {
148     Value *NumVMSArgs = *vm_state_begin();
149     return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
150   }
151
152   typename CallSiteTy::arg_iterator vm_state_begin() {
153     return gc_transition_args_end();
154   }
155   typename CallSiteTy::arg_iterator vm_state_end() {
156     int Offset = (gc_transition_args_end() + 1 + getNumTotalVMSArgs()) -
157                  StatepointCS.arg_begin();
158     assert(Offset <= (int)StatepointCS.arg_size());
159     return StatepointCS.arg_begin() + Offset;
160   }
161
162   /// range adapter for vm state arguments
163   iterator_range<arg_iterator> vm_state_args() {
164     return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
165   }
166
167   typename CallSiteTy::arg_iterator gc_args_begin() { return vm_state_end(); }
168   typename CallSiteTy::arg_iterator gc_args_end() {
169     return StatepointCS.arg_end();
170   }
171
172   /// range adapter for gc arguments
173   iterator_range<arg_iterator> gc_args() {
174     return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
175   }
176
177   /// Get list of all gc reloactes linked to this statepoint
178   /// May contain several relocations for the same base/derived pair.
179   /// For example this could happen due to relocations on unwinding
180   /// path of invoke.
181   std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
182
183 #ifndef NDEBUG
184   /// Asserts if this statepoint is malformed.  Common cases for failure
185   /// include incorrect length prefixes for variable length sections or
186   /// illegal values for parameters.
187   void verify() {
188     assert(getNumCallArgs() >= 0 &&
189            "number of arguments to actually callee can't be negative");
190
191     // The internal asserts in the iterator accessors do the rest.
192     (void)call_args_begin();
193     (void)call_args_end();
194     (void)gc_transition_args_begin();
195     (void)gc_transition_args_end();
196     (void)vm_state_begin();
197     (void)vm_state_end();
198     (void)gc_args_begin();
199     (void)gc_args_end();
200   }
201 #endif
202 };
203
204 /// A specialization of it's base class for read only access
205 /// to a gc.statepoint.
206 class ImmutableStatepoint
207     : public StatepointBase<const Instruction, const Value, ImmutableCallSite> {
208   typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
209       Base;
210
211 public:
212   explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
213   explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
214 };
215
216 /// A specialization of it's base class for read-write access
217 /// to a gc.statepoint.
218 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
219   typedef StatepointBase<Instruction, Value, CallSite> Base;
220
221 public:
222   explicit Statepoint(Instruction *I) : Base(I) {}
223   explicit Statepoint(CallSite CS) : Base(CS) {}
224 };
225
226 /// Wraps a call to a gc.relocate and provides access to it's operands.
227 /// TODO: This should likely be refactored to resememble the wrappers in
228 /// InstrinsicInst.h.
229 class GCRelocateOperands {
230   ImmutableCallSite RelocateCS;
231
232 public:
233   GCRelocateOperands(const User *U) : RelocateCS(U) { assert(isGCRelocate(U)); }
234   GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
235     assert(isGCRelocate(inst));
236   }
237   GCRelocateOperands(CallSite CS) : RelocateCS(CS) { assert(isGCRelocate(CS)); }
238
239   /// Return true if this relocate is tied to the invoke statepoint.
240   /// This includes relocates which are on the unwinding path.
241   bool isTiedToInvoke() const {
242     const Value *Token = RelocateCS.getArgument(0);
243
244     return isa<ExtractValueInst>(Token) || isa<InvokeInst>(Token);
245   }
246
247   /// Get enclosed relocate intrinsic
248   ImmutableCallSite getUnderlyingCallSite() { return RelocateCS; }
249
250   /// The statepoint with which this gc.relocate is associated.
251   const Instruction *getStatepoint() {
252     const Value *Token = RelocateCS.getArgument(0);
253
254     // This takes care both of relocates for call statepoints and relocates
255     // on normal path of invoke statepoint.
256     if (!isa<ExtractValueInst>(Token)) {
257       return cast<Instruction>(Token);
258     }
259
260     // This relocate is on exceptional path of an invoke statepoint
261     const BasicBlock *InvokeBB =
262         cast<Instruction>(Token)->getParent()->getUniquePredecessor();
263
264     assert(InvokeBB && "safepoints should have unique landingpads");
265     assert(InvokeBB->getTerminator() &&
266            "safepoint block should be well formed");
267     assert(isStatepoint(InvokeBB->getTerminator()));
268
269     return InvokeBB->getTerminator();
270   }
271
272   /// The index into the associate statepoint's argument list
273   /// which contains the base pointer of the pointer whose
274   /// relocation this gc.relocate describes.
275   unsigned getBasePtrIndex() {
276     return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
277   }
278
279   /// The index into the associate statepoint's argument list which
280   /// contains the pointer whose relocation this gc.relocate describes.
281   unsigned getDerivedPtrIndex() {
282     return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
283   }
284
285   Value *getBasePtr() {
286     ImmutableCallSite CS(getStatepoint());
287     return *(CS.arg_begin() + getBasePtrIndex());
288   }
289
290   Value *getDerivedPtr() {
291     ImmutableCallSite CS(getStatepoint());
292     return *(CS.arg_begin() + getDerivedPtrIndex());
293   }
294 };
295
296 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
297 std::vector<GCRelocateOperands>
298 StatepointBase<InstructionTy, ValueTy, CallSiteTy>::getRelocates(
299     ImmutableStatepoint &IS) {
300
301   std::vector<GCRelocateOperands> Result;
302
303   ImmutableCallSite StatepointCS = IS.getCallSite();
304
305   // Search for relocated pointers.  Note that working backwards from the
306   // gc_relocates ensures that we only get pairs which are actually relocated
307   // and used after the statepoint.
308   for (const User *U : StatepointCS.getInstruction()->users())
309     if (isGCRelocate(U))
310       Result.push_back(GCRelocateOperands(U));
311
312   if (!StatepointCS.isInvoke())
313     return Result;
314
315   // We need to scan thorough exceptional relocations if it is invoke statepoint
316   LandingPadInst *LandingPad =
317       cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
318
319   // Search for extract value from landingpad instruction to which
320   // gc relocates will be attached
321   for (const User *LandingPadUser : LandingPad->users()) {
322     if (!isa<ExtractValueInst>(LandingPadUser))
323       continue;
324
325     // gc relocates should be attached to this extract value
326     for (const User *U : LandingPadUser->users())
327       if (isGCRelocate(U))
328         Result.push_back(GCRelocateOperands(U));
329   }
330   return Result;
331 }
332 }
333
334 #endif