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