Add missing dereferenceable_or_null getters
[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
28 class GCRelocateOperands;
29 class ImmutableStatepoint;
30
31 bool isStatepoint(const ImmutableCallSite &CS);
32 bool isStatepoint(const Value *inst);
33 bool isStatepoint(const Value &inst);
34
35 bool isGCRelocate(const Value *inst);
36 bool isGCRelocate(const ImmutableCallSite &CS);
37
38 bool isGCResult(const Value *inst);
39 bool isGCResult(const ImmutableCallSite &CS);
40
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;
54
55 protected:
56   explicit StatepointBase(InstructionTy *I) : StatepointCS(I) {
57     assert(isStatepoint(I));
58   }
59   explicit StatepointBase(CallSiteTy CS) : StatepointCS(CS) {
60     assert(isStatepoint(CS));
61   }
62
63 public:
64   typedef typename CallSiteTy::arg_iterator arg_iterator;
65
66   enum {
67     ActualCalleePos = 0,
68     NumCallArgsPos = 1,
69     CallArgsBeginPos = 3,
70   };
71
72   /// Return the underlying CallSite.
73   CallSiteTy getCallSite() { return StatepointCS; }
74
75   /// Return the value actually being called or invoked.
76   ValueTy *getActualCallee() {
77     return StatepointCS.getArgument(ActualCalleePos);
78   }
79
80   /// Return the type of the value returned by the call underlying the
81   /// statepoint.
82   Type *getActualReturnType() {
83     auto *FTy = cast<FunctionType>(
84         cast<PointerType>(getActualCallee()->getType())->getElementType());
85     return FTy->getReturnType();
86   }
87
88   /// Number of arguments to be passed to the actual callee.
89   int getNumCallArgs() {
90     const Value *NumCallArgsVal = StatepointCS.getArgument(NumCallArgsPos);
91     return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
92   }
93
94   typename CallSiteTy::arg_iterator call_args_begin() {
95     assert(CallArgsBeginPos <= (int)StatepointCS.arg_size());
96     return StatepointCS.arg_begin() + CallArgsBeginPos;
97   }
98   typename CallSiteTy::arg_iterator call_args_end() {
99     int Offset = CallArgsBeginPos + getNumCallArgs();
100     assert(Offset <= (int)StatepointCS.arg_size());
101     return StatepointCS.arg_begin() + Offset;
102   }
103
104   /// range adapter for call arguments
105   iterator_range<arg_iterator> call_args() {
106     return iterator_range<arg_iterator>(call_args_begin(), call_args_end());
107   }
108
109   /// Number of additional arguments excluding those intended
110   /// for garbage collection.
111   int getNumTotalVMSArgs() {
112     Value *NumVMSArgs = *call_args_end();
113     return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
114   }
115
116   typename CallSiteTy::arg_iterator vm_state_begin() { return call_args_end(); }
117   typename CallSiteTy::arg_iterator vm_state_end() {
118     int Offset = CallArgsBeginPos + getNumCallArgs() + 1 + getNumTotalVMSArgs();
119     assert(Offset <= (int)StatepointCS.arg_size());
120     return StatepointCS.arg_begin() + Offset;
121   }
122
123   /// range adapter for vm state arguments
124   iterator_range<arg_iterator> vm_state_args() {
125     return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
126   }
127
128   typename CallSiteTy::arg_iterator gc_args_begin() { return vm_state_end(); }
129   typename CallSiteTy::arg_iterator gc_args_end() {
130     return StatepointCS.arg_end();
131   }
132
133   /// range adapter for gc arguments
134   iterator_range<arg_iterator> gc_args() {
135     return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
136   }
137
138   /// Get list of all gc reloactes linked to this statepoint
139   /// May contain several relocations for the same base/derived pair.
140   /// For example this could happen due to relocations on unwinding
141   /// path of invoke.
142   std::vector<GCRelocateOperands> getRelocates(ImmutableStatepoint &IS);
143
144 #ifndef NDEBUG
145   /// Asserts if this statepoint is malformed.  Common cases for failure
146   /// include incorrect length prefixes for variable length sections or
147   /// illegal values for parameters.
148   void verify() {
149     assert(getNumCallArgs() >= 0 &&
150            "number of arguments to actually callee can't be negative");
151
152     // The internal asserts in the iterator accessors do the rest.
153     (void)call_args_begin();
154     (void)call_args_end();
155     (void)vm_state_begin();
156     (void)vm_state_end();
157     (void)gc_args_begin();
158     (void)gc_args_end();
159   }
160 #endif
161 };
162
163 /// A specialization of it's base class for read only access
164 /// to a gc.statepoint.
165 class ImmutableStatepoint
166     : public StatepointBase<const Instruction, const Value, ImmutableCallSite> {
167   typedef StatepointBase<const Instruction, const Value, ImmutableCallSite>
168       Base;
169
170 public:
171   explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
172   explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
173 };
174
175 /// A specialization of it's base class for read-write access
176 /// to a gc.statepoint.
177 class Statepoint : public StatepointBase<Instruction, Value, CallSite> {
178   typedef StatepointBase<Instruction, Value, CallSite> Base;
179
180 public:
181   explicit Statepoint(Instruction *I) : Base(I) {}
182   explicit Statepoint(CallSite CS) : Base(CS) {}
183 };
184
185 /// Wraps a call to a gc.relocate and provides access to it's operands.
186 /// TODO: This should likely be refactored to resememble the wrappers in
187 /// InstrinsicInst.h.
188 class GCRelocateOperands {
189   ImmutableCallSite RelocateCS;
190
191 public:
192   GCRelocateOperands(const User *U) : RelocateCS(U) { assert(isGCRelocate(U)); }
193   GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
194     assert(isGCRelocate(inst));
195   }
196   GCRelocateOperands(CallSite CS) : RelocateCS(CS) { assert(isGCRelocate(CS)); }
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) || isa<InvokeInst>(Token);
204   }
205
206   /// Get enclosed relocate intrinsic
207   ImmutableCallSite getUnderlyingCallSite() { return RelocateCS; }
208
209   /// The statepoint with which this gc.relocate is associated.
210   const Instruction *getStatepoint() {
211     const Value *Token = RelocateCS.getArgument(0);
212
213     // This takes care both of relocates for call statepoints and relocates
214     // on normal path of invoke statepoint.
215     if (!isa<ExtractValueInst>(Token)) {
216       return cast<Instruction>(Token);
217     }
218
219     // This relocate is on exceptional path of an invoke statepoint
220     const BasicBlock *InvokeBB =
221         cast<Instruction>(Token)->getParent()->getUniquePredecessor();
222
223     assert(InvokeBB && "safepoints should have unique landingpads");
224     assert(InvokeBB->getTerminator() &&
225            "safepoint block should be well formed");
226     assert(isStatepoint(InvokeBB->getTerminator()));
227
228     return InvokeBB->getTerminator();
229   }
230
231   /// The index into the associate statepoint's argument list
232   /// which contains the base pointer of the pointer whose
233   /// relocation this gc.relocate describes.
234   unsigned getBasePtrIndex() {
235     return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
236   }
237
238   /// The index into the associate statepoint's argument list which
239   /// contains the pointer whose relocation this gc.relocate describes.
240   unsigned getDerivedPtrIndex() {
241     return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
242   }
243
244   Value *getBasePtr() {
245     ImmutableCallSite CS(getStatepoint());
246     return *(CS.arg_begin() + getBasePtrIndex());
247   }
248
249   Value *getDerivedPtr() {
250     ImmutableCallSite CS(getStatepoint());
251     return *(CS.arg_begin() + getDerivedPtrIndex());
252   }
253 };
254
255 template <typename InstructionTy, typename ValueTy, typename CallSiteTy>
256 std::vector<GCRelocateOperands>
257 StatepointBase<InstructionTy, ValueTy, CallSiteTy>::getRelocates(
258     ImmutableStatepoint &IS) {
259
260   std::vector<GCRelocateOperands> Result;
261
262   ImmutableCallSite StatepointCS = IS.getCallSite();
263
264   // Search for relocated pointers.  Note that working backwards from the
265   // gc_relocates ensures that we only get pairs which are actually relocated
266   // and used after the statepoint.
267   for (const User *U : StatepointCS.getInstruction()->users())
268     if (isGCRelocate(U))
269       Result.push_back(GCRelocateOperands(U));
270
271   if (!StatepointCS.isInvoke())
272     return Result;
273
274   // We need to scan thorough exceptional relocations if it is invoke statepoint
275   LandingPadInst *LandingPad =
276       cast<InvokeInst>(StatepointCS.getInstruction())->getLandingPadInst();
277
278   // Search for extract value from landingpad instruction to which
279   // gc relocates will be attached
280   for (const User *LandingPadUser : LandingPad->users()) {
281     if (!isa<ExtractValueInst>(LandingPadUser))
282       continue;
283
284     // gc relocates should be attached to this extract value
285     for (const User *U : LandingPadUser->users())
286       if (isGCRelocate(U))
287         Result.push_back(GCRelocateOperands(U));
288   }
289   return Result;
290 }
291 }
292
293 #endif