xref: /llvm-project/llvm/include/llvm/IR/GlobalIFunc.h (revision e3f936eb755d9ae37019ffcc7f53d71d2d58d188)
1 //===-------- llvm/GlobalIFunc.h - GlobalIFunc class ------------*- 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 /// \file
10 /// This file contains the declaration of the GlobalIFunc class, which
11 /// represents a single indirect function in the IR. Indirect function uses
12 /// ELF symbol type extension to mark that the address of a declaration should
13 /// be resolved at runtime by calling a resolver function.
14 ///
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_IR_GLOBALIFUNC_H
18 #define LLVM_IR_GLOBALIFUNC_H
19 
20 #include "llvm/ADT/ilist_node.h"
21 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/GlobalObject.h"
23 #include "llvm/IR/OperandTraits.h"
24 #include "llvm/IR/Value.h"
25 
26 namespace llvm {
27 
28 class Twine;
29 class Module;
30 
31 // Traits class for using GlobalIFunc in symbol table in Module.
32 template <typename ValueSubClass, typename... Args> class SymbolTableListTraits;
33 
34 class GlobalIFunc final : public GlobalObject, public ilist_node<GlobalIFunc> {
35   friend class SymbolTableListTraits<GlobalIFunc>;
36 
37   constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
38 
39   GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage,
40               const Twine &Name, Constant *Resolver, Module *Parent);
41 
42 public:
43   GlobalIFunc(const GlobalIFunc &) = delete;
44   GlobalIFunc &operator=(const GlobalIFunc &) = delete;
45 
46   /// If a parent module is specified, the ifunc is automatically inserted into
47   /// the end of the specified module's ifunc list.
48   static GlobalIFunc *create(Type *Ty, unsigned AddressSpace,
49                              LinkageTypes Linkage, const Twine &Name,
50                              Constant *Resolver, Module *Parent);
51 
52   // allocate space for exactly one operand
53   void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
54   void operator delete(void *Ptr) { User::operator delete(Ptr); }
55 
56   /// Provide fast operand accessors
57   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
58 
59   void copyAttributesFrom(const GlobalIFunc *Src) {
60     GlobalObject::copyAttributesFrom(Src);
61   }
62 
63   /// This method unlinks 'this' from the containing module, but does not
64   /// delete it.
65   void removeFromParent();
66 
67   /// This method unlinks 'this' from the containing module and deletes it.
68   void eraseFromParent();
69 
70   /// These methods retrieve and set ifunc resolver function.
71   void setResolver(Constant *Resolver) { Op<0>().set(Resolver); }
72   const Constant *getResolver() const {
73     return static_cast<Constant *>(Op<0>().get());
74   }
75   Constant *getResolver() { return static_cast<Constant *>(Op<0>().get()); }
76 
77   // Return the resolver function after peeling off potential ConstantExpr
78   // indirection.
79   const Function *getResolverFunction() const;
80   Function *getResolverFunction() {
81     return const_cast<Function *>(
82         static_cast<const GlobalIFunc *>(this)->getResolverFunction());
83   }
84 
85   static bool isValidLinkage(LinkageTypes L) {
86     return isExternalLinkage(L) || isLocalLinkage(L) || isWeakLinkage(L) ||
87            isLinkOnceLinkage(L);
88   }
89 
90   // Methods for support type inquiry through isa, cast, and dyn_cast:
91   static bool classof(const Value *V) {
92     return V->getValueID() == Value::GlobalIFuncVal;
93   }
94 
95   // Apply specific operation to all resolver-related values. If resolver target
96   // is already a global object, then apply the operation to it directly. If
97   // target is a GlobalExpr or a GlobalAlias, evaluate it to its base object and
98   // apply the operation for the base object and all aliases along the path.
99   void applyAlongResolverPath(function_ref<void(const GlobalValue &)> Op) const;
100 };
101 
102 template <>
103 struct OperandTraits<GlobalIFunc>
104     : public FixedNumOperandTraits<GlobalIFunc, 1> {};
105 
106 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalIFunc, Constant)
107 
108 } // end namespace llvm
109 
110 #endif // LLVM_IR_GLOBALIFUNC_H
111