GlobalObject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. ObjComdat(nullptr) {
  47. setGlobalValueSubClassData(0);
  48. }
  49. ~GlobalObject();
  50. Comdat *ObjComdat;
  51. enum {
  52. LastAlignmentBit = 5,
  53. HasSectionHashEntryBit,
  54. GlobalObjectBits,
  55. };
  56. static const unsigned GlobalObjectSubClassDataBits =
  57. GlobalValueSubClassDataBits - GlobalObjectBits;
  58. private:
  59. static const unsigned AlignmentBits = LastAlignmentBit + 1;
  60. static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
  61. static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
  62. public:
  63. GlobalObject(const GlobalObject &) = delete;
  64. /// FIXME: Remove this function once transition to Align is over.
  65. uint64_t getAlignment() const {
  66. MaybeAlign Align = getAlign();
  67. return Align ? Align->value() : 0;
  68. }
  69. /// Returns the alignment of the given variable or function.
  70. ///
  71. /// Note that for functions this is the alignment of the code, not the
  72. /// alignment of a function pointer.
  73. MaybeAlign getAlign() const {
  74. unsigned Data = getGlobalValueSubClassData();
  75. unsigned AlignmentData = Data & AlignmentMask;
  76. return decodeMaybeAlign(AlignmentData);
  77. }
  78. void setAlignment(MaybeAlign Align);
  79. unsigned getGlobalObjectSubClassData() const {
  80. unsigned ValueData = getGlobalValueSubClassData();
  81. return ValueData >> GlobalObjectBits;
  82. }
  83. void setGlobalObjectSubClassData(unsigned Val) {
  84. unsigned OldData = getGlobalValueSubClassData();
  85. setGlobalValueSubClassData((OldData & GlobalObjectMask) |
  86. (Val << GlobalObjectBits));
  87. assert(getGlobalObjectSubClassData() == Val && "representation error");
  88. }
  89. /// Check if this global has a custom object file section.
  90. ///
  91. /// This is more efficient than calling getSection() and checking for an empty
  92. /// string.
  93. bool hasSection() const {
  94. return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
  95. }
  96. /// Get the custom section of this global if it has one.
  97. ///
  98. /// If this global does not have a custom section, this will be empty and the
  99. /// default object file section (.text, .data, etc) will be used.
  100. StringRef getSection() const {
  101. return hasSection() ? getSectionImpl() : StringRef();
  102. }
  103. /// Change the section for this global.
  104. ///
  105. /// Setting the section to the empty string tells LLVM to choose an
  106. /// appropriate default object file section.
  107. void setSection(StringRef S);
  108. bool hasComdat() const { return getComdat() != nullptr; }
  109. const Comdat *getComdat() const { return ObjComdat; }
  110. Comdat *getComdat() { return ObjComdat; }
  111. void setComdat(Comdat *C);
  112. using Value::addMetadata;
  113. using Value::clearMetadata;
  114. using Value::eraseMetadata;
  115. using Value::getAllMetadata;
  116. using Value::getMetadata;
  117. using Value::hasMetadata;
  118. using Value::setMetadata;
  119. /// Copy metadata from Src, adjusting offsets by Offset.
  120. void copyMetadata(const GlobalObject *Src, unsigned Offset);
  121. void addTypeMetadata(unsigned Offset, Metadata *TypeID);
  122. void setVCallVisibilityMetadata(VCallVisibility Visibility);
  123. VCallVisibility getVCallVisibility() const;
  124. /// Returns true if the alignment of the value can be unilaterally
  125. /// increased.
  126. ///
  127. /// Note that for functions this is the alignment of the code, not the
  128. /// alignment of a function pointer.
  129. bool canIncreaseAlignment() const;
  130. protected:
  131. void copyAttributesFrom(const GlobalObject *Src);
  132. public:
  133. // Methods for support type inquiry through isa, cast, and dyn_cast:
  134. static bool classof(const Value *V) {
  135. return V->getValueID() == Value::FunctionVal ||
  136. V->getValueID() == Value::GlobalVariableVal ||
  137. V->getValueID() == Value::GlobalIFuncVal;
  138. }
  139. private:
  140. void setGlobalObjectFlag(unsigned Bit, bool Val) {
  141. unsigned Mask = 1 << Bit;
  142. setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
  143. (Val ? Mask : 0u));
  144. }
  145. StringRef getSectionImpl() const;
  146. };
  147. } // end namespace llvm
  148. #endif // LLVM_IR_GLOBALOBJECT_H
  149. #ifdef __GNUC__
  150. #pragma GCC diagnostic pop
  151. #endif