1 //===-- IR/Statepoint.cpp -- gc.statepoint utilities ---  -----------------===//
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 some utility functions to help recognize gc.statepoint
11 // intrinsics.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/Statepoint.h"
16 
17 #include "llvm/IR/Function.h"
18 
19 using namespace llvm;
20 
21 static const Function *getCalledFunction(ImmutableCallSite CS) {
22   if (!CS.getInstruction())
23     return nullptr;
24   return CS.getCalledFunction();
25 }
26 
27 bool llvm::isStatepoint(ImmutableCallSite CS) {
28   if (auto *F = getCalledFunction(CS))
29     return F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
30   return false;
31 }
32 
33 bool llvm::isStatepoint(const Value *V) {
34   if (auto CS = ImmutableCallSite(V))
35     return isStatepoint(CS);
36   return false;
37 }
38 
39 bool llvm::isStatepoint(const Value &V) {
40   return isStatepoint(&V);
41 }
42 
43 bool llvm::isGCRelocate(ImmutableCallSite CS) {
44   return CS.getInstruction() && isa<GCRelocateInst>(CS.getInstruction());
45 }
46 
47 bool llvm::isGCResult(ImmutableCallSite CS) {
48   if (auto *F = getCalledFunction(CS))
49     return F->getIntrinsicID() == Intrinsic::experimental_gc_result;
50   return false;
51 }
52 
53 bool llvm::isGCResult(const Value *V) {
54   return isGCResult(ImmutableCallSite(V));
55 }
56 
57 bool llvm::isStatepointDirectiveAttr(Attribute Attr) {
58   return Attr.hasAttribute("statepoint-id") ||
59          Attr.hasAttribute("statepoint-num-patch-bytes");
60 }
61 
62 StatepointDirectives llvm::parseStatepointDirectivesFromAttrs(AttributeSet AS) {
63   StatepointDirectives Result;
64 
65   Attribute AttrID =
66       AS.getAttribute(AttributeSet::FunctionIndex, "statepoint-id");
67   uint64_t StatepointID;
68   if (AttrID.isStringAttribute())
69     if (!AttrID.getValueAsString().getAsInteger(10, StatepointID))
70       Result.StatepointID = StatepointID;
71 
72   uint32_t NumPatchBytes;
73   Attribute AttrNumPatchBytes = AS.getAttribute(AttributeSet::FunctionIndex,
74                                                 "statepoint-num-patch-bytes");
75   if (AttrNumPatchBytes.isStringAttribute())
76     if (!AttrNumPatchBytes.getValueAsString().getAsInteger(10, NumPatchBytes))
77       Result.NumPatchBytes = NumPatchBytes;
78 
79   return Result;
80 }
81