GlobalObject.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/GlobalObject.h - Class to represent global objects -*- 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. // This represents an independent object. That is, a function or a global
  15. // variable, but not an alias.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_GLOBALOBJECT_H
  19. #define LLVM_IR_GLOBALOBJECT_H
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/GlobalValue.h"
  22. #include "llvm/IR/Value.h"
  23. #include "llvm/Support/Alignment.h"
  24. namespace llvm {
  25. class Comdat;
  26. class Metadata;
  27. class GlobalObject : public GlobalValue {
  28. public:
  29. // VCallVisibility - values for visibility metadata attached to vtables. This
  30. // describes the scope in which a virtual call could end up being dispatched
  31. // through this vtable.
  32. enum VCallVisibility {
  33. // Type is potentially visible to external code.
  34. VCallVisibilityPublic = 0,
  35. // Type is only visible to code which will be in the current Module after
  36. // LTO internalization.
  37. VCallVisibilityLinkageUnit = 1,
  38. // Type is only visible to code in the current Module.
  39. VCallVisibilityTranslationUnit = 2,
  40. };
  41. protected:
  42. GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
  43. LinkageTypes Linkage, const Twine &Name,
  44. unsigned AddressSpace = 0)
  45. : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace) {
  46. setGlobalValueSubClassData(0);
  47. }
  48. ~GlobalObject();
  49. Comdat *ObjComdat = nullptr;
  50. enum {
  51. LastAlignmentBit = 5,
  52. HasSectionHashEntryBit,
  53. GlobalObjectBits,
  54. };
  55. static const unsigned GlobalObjectSubClassDataBits =
  56. GlobalValueSubClassDataBits - GlobalObjectBits;
  57. private:
  58. static const unsigned AlignmentBits = LastAlignmentBit + 1;
  59. static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
  60. static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
  61. public:
  62. GlobalObject(const GlobalObject &) = delete;
  63. /// FIXME: Remove this function once transition to Align is over.
  64. uint64_t getAlignment() const {
  65. MaybeAlign Align = getAlign();
  66. return Align ? Align->value() : 0;
  67. }
  68. /// Returns the alignment of the given variable or function.
  69. ///
  70. /// Note that for functions this is the alignment of the code, not the
  71. /// alignment of a function pointer.
  72. MaybeAlign getAlign() const {
  73. unsigned Data = getGlobalValueSubClassData();
  74. unsigned AlignmentData = Data & AlignmentMask;
  75. return decodeMaybeAlign(AlignmentData);
  76. }
  77. void setAlignment(MaybeAlign Align);
  78. unsigned getGlobalObjectSubClassData() const {
  79. unsigned ValueData = getGlobalValueSubClassData();
  80. return ValueData >> GlobalObjectBits;
  81. }
  82. void setGlobalObjectSubClassData(unsigned Val) {
  83. unsigned OldData = getGlobalValueSubClassData();
  84. setGlobalValueSubClassData((OldData & GlobalObjectMask) |
  85. (Val << GlobalObjectBits));
  86. assert(getGlobalObjectSubClassData() == Val && "representation error");
  87. }
  88. /// Check if this global has a custom object file section.
  89. ///
  90. /// This is more efficient than calling getSection() and checking for an empty
  91. /// string.
  92. bool hasSection() const {
  93. return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
  94. }
  95. /// Get the custom section of this global if it has one.
  96. ///
  97. /// If this global does not have a custom section, this will be empty and the
  98. /// default object file section (.text, .data, etc) will be used.
  99. StringRef getSection() const {
  100. return hasSection() ? getSectionImpl() : StringRef();
  101. }
  102. /// Change the section for this global.
  103. ///
  104. /// Setting the section to the empty string tells LLVM to choose an
  105. /// appropriate default object file section.
  106. void setSection(StringRef S);
  107. bool hasComdat() const { return getComdat() != nullptr; }
  108. const Comdat *getComdat() const { return ObjComdat; }
  109. Comdat *getComdat() { return ObjComdat; }
  110. void setComdat(Comdat *C);
  111. using Value::addMetadata;
  112. using Value::clearMetadata;
  113. using Value::eraseMetadata;
  114. using Value::getAllMetadata;
  115. using Value::getMetadata;
  116. using Value::hasMetadata;
  117. using Value::setMetadata;
  118. /// Copy metadata from Src, adjusting offsets by Offset.
  119. void copyMetadata(const GlobalObject *Src, unsigned Offset);
  120. void addTypeMetadata(unsigned Offset, Metadata *TypeID);
  121. void setVCallVisibilityMetadata(VCallVisibility Visibility);
  122. VCallVisibility getVCallVisibility() const;
  123. /// Returns true if the alignment of the value can be unilaterally
  124. /// increased.
  125. ///
  126. /// Note that for functions this is the alignment of the code, not the
  127. /// alignment of a function pointer.
  128. bool canIncreaseAlignment() const;
  129. protected:
  130. void copyAttributesFrom(const GlobalObject *Src);
  131. public:
  132. // Methods for support type inquiry through isa, cast, and dyn_cast:
  133. static bool classof(const Value *V) {
  134. return V->getValueID() == Value::FunctionVal ||
  135. V->getValueID() == Value::GlobalVariableVal ||
  136. V->getValueID() == Value::GlobalIFuncVal;
  137. }
  138. private:
  139. void setGlobalObjectFlag(unsigned Bit, bool Val) {
  140. unsigned Mask = 1 << Bit;
  141. setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
  142. (Val ? Mask : 0u));
  143. }
  144. StringRef getSectionImpl() const;
  145. };
  146. } // end namespace llvm
  147. #endif // LLVM_IR_GLOBALOBJECT_H
  148. #ifdef __GNUC__
  149. #pragma GCC diagnostic pop
  150. #endif