GlobalIFunc.h 4.2 KB

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