GlobalObject.h 5.5 KB

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