xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/IR/Statepoint.h (revision 2aa3ef285a23d802f0bd6c7281612e16834e9b68)
1 //===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains utility functions and a wrapper class analogous to
10 // CallBase for accessing the fields of gc.statepoint, gc.relocate,
11 // gc.result intrinsics; and some general utilities helpful when dealing with
12 // gc.statepoint.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_IR_STATEPOINT_H
17 #define LLVM_IR_STATEPOINT_H
18 
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/MathExtras.h"
31 #include <cassert>
32 #include <cstddef>
33 #include <cstdint>
34 #include <vector>
35 
36 namespace llvm {
37 
38 /// The statepoint intrinsic accepts a set of flags as its third argument.
39 /// Valid values come out of this set.
40 enum class StatepointFlags {
41   None = 0,
42   GCTransition = 1, ///< Indicates that this statepoint is a transition from
43                     ///< GC-aware code to code that is not GC-aware.
44   /// Mark the deopt arguments associated with the statepoint as only being
45   /// "live-in". By default, deopt arguments are "live-through".  "live-through"
46   /// requires that they the value be live on entry, on exit, and at any point
47   /// during the call.  "live-in" only requires the value be available at the
48   /// start of the call.  In particular, "live-in" values can be placed in
49   /// unused argument registers or other non-callee saved registers.
50   DeoptLiveIn = 2,
51 
52   MaskAll = 3 ///< A bitmask that includes all valid flags.
53 };
54 
55 class GCRelocateInst;
56 class GCResultInst;
57 
58 /// Represents a gc.statepoint intrinsic call.  This extends directly from
59 /// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
60 /// invokable.
61 class GCStatepointInst : public CallBase {
62 public:
63   GCStatepointInst() = delete;
64   GCStatepointInst(const GCStatepointInst &) = delete;
65   GCStatepointInst &operator=(const GCStatepointInst &) = delete;
66 
67   static bool classof(const CallBase *I) {
68     if (const Function *CF = I->getCalledFunction())
69       return CF->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
70     return false;
71   }
72 
73   static bool classof(const Value *V) {
74     return isa<CallBase>(V) && classof(cast<CallBase>(V));
75   }
76 
77   enum {
78     IDPos = 0,
79     NumPatchBytesPos = 1,
80     CalledFunctionPos = 2,
81     NumCallArgsPos = 3,
82     FlagsPos = 4,
83     CallArgsBeginPos = 5,
84   };
85 
86   /// Return the ID associated with this statepoint.
87   uint64_t getID() const {
88     return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
89   }
90 
91   /// Return the number of patchable bytes associated with this statepoint.
92   uint32_t getNumPatchBytes() const {
93     const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
94     uint64_t NumPatchBytes =
95       cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
96     assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
97     return NumPatchBytes;
98   }
99 
100   /// Number of arguments to be passed to the actual callee.
101   int getNumCallArgs() const {
102     return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
103   }
104 
105   uint64_t getFlags() const {
106     return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
107   }
108 
109   /// Return the value actually being called or invoked.
110   Value *getActualCalledOperand() const {
111     return getArgOperand(CalledFunctionPos);
112   }
113 
114   /// Returns the function called if this is a wrapping a direct call, and null
115   /// otherwise.
116   Function *getActualCalledFunction() const {
117     return dyn_cast_or_null<Function>(getActualCalledOperand());
118   }
119 
120   /// Return the type of the value returned by the call underlying the
121   /// statepoint.
122   Type *getActualReturnType() const {
123     auto *CalleeTy =
124       cast<PointerType>(getActualCalledOperand()->getType())->getElementType();
125     return cast<FunctionType>(CalleeTy)->getReturnType();
126   }
127 
128 
129   /// Return the number of arguments to the underlying call.
130   size_t actual_arg_size() const { return getNumCallArgs(); }
131   /// Return an iterator to the begining of the arguments to the underlying call
132   const_op_iterator actual_arg_begin() const {
133     assert(CallArgsBeginPos <= (int)arg_size());
134     return arg_begin() + CallArgsBeginPos;
135   }
136   /// Return an end iterator of the arguments to the underlying call
137   const_op_iterator actual_arg_end() const {
138     auto I = actual_arg_begin() + actual_arg_size();
139     assert((arg_end() - I) >= 0);
140     return I;
141   }
142   /// range adapter for actual call arguments
143   iterator_range<const_op_iterator> actual_args() const {
144     return make_range(actual_arg_begin(), actual_arg_end());
145   }
146 
147   const_op_iterator gc_transition_args_begin() const {
148     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
149       return Opt->Inputs.begin();
150     auto I = actual_arg_end() + 1;
151     assert((arg_end() - I) >= 0);
152     return I;
153   }
154   const_op_iterator gc_transition_args_end() const {
155     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
156       return Opt->Inputs.end();
157     auto I = gc_transition_args_begin() + getNumDeoptArgs();
158     assert((arg_end() - I) >= 0);
159     return I;
160   }
161 
162   /// range adapter for GC transition arguments
163   iterator_range<const_op_iterator> gc_transition_args() const {
164     return make_range(gc_transition_args_begin(), gc_transition_args_end());
165   }
166 
167   const_op_iterator deopt_begin() const {
168     if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
169       return Opt->Inputs.begin();
170     // The current format has two length prefix bundles between call args and
171     // start of gc args.  This will be removed in the near future.
172     uint64_t NumTrans = getNumGCTransitionArgs();
173     const_op_iterator I = actual_arg_end() + 2 + NumTrans;
174     assert((arg_end() - I) >= 0);
175     return I;
176   }
177   const_op_iterator deopt_end() const {
178     if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
179       return Opt->Inputs.end();
180     auto I = deopt_begin() + getNumDeoptArgs();
181     assert((arg_end() - I) >= 0);
182     return I;
183   }
184 
185   /// range adapter for vm state arguments
186   iterator_range<const_op_iterator> deopt_operands() const {
187     return make_range(deopt_begin(), deopt_end());
188   }
189 
190   /// Returns an iterator to the begining of the argument range describing gc
191   /// values for the statepoint.
192   const_op_iterator gc_args_begin() const {
193     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
194       return Opt->Inputs.begin();
195 
196     // The current format has two length prefix bundles between call args and
197     // start of gc args.  This will be removed in the near future.
198     uint64_t NumTrans = getNumGCTransitionArgs();
199     uint64_t NumDeopt = getNumDeoptArgs();
200     auto I = actual_arg_end() + 2 + NumTrans + NumDeopt;
201     assert((arg_end() - I) >= 0);
202     return I;
203   }
204 
205   /// Return an end iterator for the gc argument range
206   const_op_iterator gc_args_end() const {
207     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
208       return Opt->Inputs.end();
209 
210     return arg_end();
211   }
212 
213   /// Return the operand index at which the gc args begin
214   unsigned gcArgsStartIdx() const {
215     assert(!getOperandBundle(LLVMContext::OB_gc_live));
216     return gc_args_begin() - op_begin();
217   }
218 
219   /// range adapter for gc arguments
220   iterator_range<const_op_iterator> gc_args() const {
221     return make_range(gc_args_begin(), gc_args_end());
222   }
223 
224 
225   /// Get list of all gc reloactes linked to this statepoint
226   /// May contain several relocations for the same base/derived pair.
227   /// For example this could happen due to relocations on unwinding
228   /// path of invoke.
229   inline std::vector<const GCRelocateInst *> getGCRelocates() const;
230 
231   /// Get the experimental_gc_result call tied to this statepoint if there is
232   /// one, otherwise return nullptr.
233   const GCResultInst *getGCResult() const {
234     for (auto *U : users())
235       if (auto *GRI = dyn_cast<GCResultInst>(U))
236         return GRI;
237     return nullptr;
238   }
239 
240 private:
241   int getNumGCTransitionArgs() const {
242     const Value *NumGCTransitionArgs = *actual_arg_end();
243     return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
244   }
245 
246   int getNumDeoptArgs() const {
247     uint64_t NumTrans = getNumGCTransitionArgs();
248     const_op_iterator trans_end = actual_arg_end() + 1 + NumTrans;
249     const Value *NumDeoptArgs = *trans_end;
250     return cast<ConstantInt>(NumDeoptArgs)->getZExtValue();
251   }
252 };
253 
254 /// Common base class for representing values projected from a statepoint.
255 /// Currently, the only projections available are gc.result and gc.relocate.
256 class GCProjectionInst : public IntrinsicInst {
257 public:
258   static bool classof(const IntrinsicInst *I) {
259     return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
260       I->getIntrinsicID() == Intrinsic::experimental_gc_result;
261   }
262 
263   static bool classof(const Value *V) {
264     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
265   }
266 
267   /// Return true if this relocate is tied to the invoke statepoint.
268   /// This includes relocates which are on the unwinding path.
269   bool isTiedToInvoke() const {
270     const Value *Token = getArgOperand(0);
271 
272     return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
273   }
274 
275   /// The statepoint with which this gc.relocate is associated.
276   const GCStatepointInst *getStatepoint() const {
277     const Value *Token = getArgOperand(0);
278 
279     // This takes care both of relocates for call statepoints and relocates
280     // on normal path of invoke statepoint.
281     if (!isa<LandingPadInst>(Token))
282       return cast<GCStatepointInst>(Token);
283 
284     // This relocate is on exceptional path of an invoke statepoint
285     const BasicBlock *InvokeBB =
286         cast<Instruction>(Token)->getParent()->getUniquePredecessor();
287 
288     assert(InvokeBB && "safepoints should have unique landingpads");
289     assert(InvokeBB->getTerminator() &&
290            "safepoint block should be well formed");
291 
292     return cast<GCStatepointInst>(InvokeBB->getTerminator());
293   }
294 };
295 
296 /// Represents calls to the gc.relocate intrinsic.
297 class GCRelocateInst : public GCProjectionInst {
298 public:
299   static bool classof(const IntrinsicInst *I) {
300     return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
301   }
302 
303   static bool classof(const Value *V) {
304     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
305   }
306 
307   /// The index into the associate statepoint's argument list
308   /// which contains the base pointer of the pointer whose
309   /// relocation this gc.relocate describes.
310   unsigned getBasePtrIndex() const {
311     return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
312   }
313 
314   /// The index into the associate statepoint's argument list which
315   /// contains the pointer whose relocation this gc.relocate describes.
316   unsigned getDerivedPtrIndex() const {
317     return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
318   }
319 
320   Value *getBasePtr() const {
321     if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
322       return *(Opt->Inputs.begin() + getBasePtrIndex());
323     return *(getStatepoint()->arg_begin() + getBasePtrIndex());
324   }
325 
326   Value *getDerivedPtr() const {
327     if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
328       return *(Opt->Inputs.begin() + getDerivedPtrIndex());
329     return *(getStatepoint()->arg_begin() + getDerivedPtrIndex());
330   }
331 };
332 
333 /// Represents calls to the gc.result intrinsic.
334 class GCResultInst : public GCProjectionInst {
335 public:
336   static bool classof(const IntrinsicInst *I) {
337     return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
338   }
339 
340   static bool classof(const Value *V) {
341     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
342   }
343 };
344 
345 std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
346   std::vector<const GCRelocateInst *> Result;
347 
348   // Search for relocated pointers.  Note that working backwards from the
349   // gc_relocates ensures that we only get pairs which are actually relocated
350   // and used after the statepoint.
351   for (const User *U : users())
352     if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
353       Result.push_back(Relocate);
354 
355   auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
356   if (!StatepointInvoke)
357     return Result;
358 
359   // We need to scan thorough exceptional relocations if it is invoke statepoint
360   LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
361 
362   // Search for gc relocates that are attached to this landingpad.
363   for (const User *LandingPadUser : LandingPad->users()) {
364     if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
365       Result.push_back(Relocate);
366   }
367   return Result;
368 }
369 
370 /// Call sites that get wrapped by a gc.statepoint (currently only in
371 /// RewriteStatepointsForGC and potentially in other passes in the future) can
372 /// have attributes that describe properties of gc.statepoint call they will be
373 /// eventually be wrapped in.  This struct is used represent such directives.
374 struct StatepointDirectives {
375   Optional<uint32_t> NumPatchBytes;
376   Optional<uint64_t> StatepointID;
377 
378   static const uint64_t DefaultStatepointID = 0xABCDEF00;
379   static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
380 };
381 
382 /// Parse out statepoint directives from the function attributes present in \p
383 /// AS.
384 StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
385 
386 /// Return \c true if the \p Attr is an attribute that is a statepoint
387 /// directive.
388 bool isStatepointDirectiveAttr(Attribute Attr);
389 
390 } // end namespace llvm
391 
392 #endif // LLVM_IR_STATEPOINT_H
393