1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a common base class of all globally definable objects. As such, 11 // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is 12 // used because you can do certain things with these global objects that you 13 // can't do to anything else. For example, use the address of one as a 14 // constant. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_IR_GLOBALVALUE_H 19 #define LLVM_IR_GLOBALVALUE_H 20 21 #include "llvm/IR/Constant.h" 22 #include "llvm/IR/DerivedTypes.h" 23 24 namespace llvm { 25 26 class Comdat; 27 class PointerType; 28 class Module; 29 30 class GlobalValue : public Constant { 31 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION; 32 public: 33 /// @brief An enumeration for the kinds of linkage for global values. 34 enum LinkageTypes { 35 ExternalLinkage = 0,///< Externally visible function 36 AvailableExternallyLinkage, ///< Available for inspection, not emission. 37 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline) 38 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent. 39 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak) 40 WeakODRLinkage, ///< Same, but only replaced by something equivalent. 41 AppendingLinkage, ///< Special purpose, only applies to global arrays 42 InternalLinkage, ///< Rename collisions when linking (static functions). 43 PrivateLinkage, ///< Like Internal, but omit from symbol table. 44 ExternalWeakLinkage,///< ExternalWeak linkage description. 45 CommonLinkage ///< Tentative definitions. 46 }; 47 48 /// @brief An enumeration for the kinds of visibility of global values. 49 enum VisibilityTypes { 50 DefaultVisibility = 0, ///< The GV is visible 51 HiddenVisibility, ///< The GV is hidden 52 ProtectedVisibility ///< The GV is protected 53 }; 54 55 /// @brief Storage classes of global values for PE targets. 56 enum DLLStorageClassTypes { 57 DefaultStorageClass = 0, 58 DLLImportStorageClass = 1, ///< Function to be imported from DLL 59 DLLExportStorageClass = 2 ///< Function to be accessible from DLL. 60 }; 61 62 protected: GlobalValue(Type * Ty,ValueTy VTy,Use * Ops,unsigned NumOps,LinkageTypes Linkage,const Twine & Name)63 GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, 64 LinkageTypes Linkage, const Twine &Name) 65 : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage), 66 Visibility(DefaultVisibility), UnnamedAddr(0), 67 DllStorageClass(DefaultStorageClass), 68 ThreadLocal(NotThreadLocal), Parent(nullptr) { 69 setName(Name); 70 } 71 72 // Note: VC++ treats enums as signed, so an extra bit is required to prevent 73 // Linkage and Visibility from turning into negative values. 74 LinkageTypes Linkage : 5; // The linkage of this global 75 unsigned Visibility : 2; // The visibility style of this global 76 unsigned UnnamedAddr : 1; // This value's address is not significant 77 unsigned DllStorageClass : 2; // DLL storage class 78 79 unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is 80 // the desired model? 81 82 private: 83 // Give subclasses access to what otherwise would be wasted padding. 84 // (19 + 3 + 2 + 1 + 2 + 5) == 32. 85 unsigned SubClassData : 19; 86 protected: getGlobalValueSubClassData()87 unsigned getGlobalValueSubClassData() const { 88 return SubClassData; 89 } setGlobalValueSubClassData(unsigned V)90 void setGlobalValueSubClassData(unsigned V) { 91 assert(V < (1 << 19) && "It will not fit"); 92 SubClassData = V; 93 } 94 95 Module *Parent; // The containing module. 96 public: 97 enum ThreadLocalMode { 98 NotThreadLocal = 0, 99 GeneralDynamicTLSModel, 100 LocalDynamicTLSModel, 101 InitialExecTLSModel, 102 LocalExecTLSModel 103 }; 104 ~GlobalValue()105 ~GlobalValue() { 106 removeDeadConstantUsers(); // remove any dead constants using this. 107 } 108 109 unsigned getAlignment() const; 110 hasUnnamedAddr()111 bool hasUnnamedAddr() const { return UnnamedAddr; } setUnnamedAddr(bool Val)112 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; } 113 hasComdat()114 bool hasComdat() const { return getComdat() != nullptr; } 115 Comdat *getComdat(); getComdat()116 const Comdat *getComdat() const { 117 return const_cast<GlobalValue *>(this)->getComdat(); 118 } 119 getVisibility()120 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } hasDefaultVisibility()121 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; } hasHiddenVisibility()122 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } hasProtectedVisibility()123 bool hasProtectedVisibility() const { 124 return Visibility == ProtectedVisibility; 125 } setVisibility(VisibilityTypes V)126 void setVisibility(VisibilityTypes V) { 127 assert((!hasLocalLinkage() || V == DefaultVisibility) && 128 "local linkage requires default visibility"); 129 Visibility = V; 130 } 131 132 /// If the value is "Thread Local", its value isn't shared by the threads. isThreadLocal()133 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; } setThreadLocal(bool Val)134 void setThreadLocal(bool Val) { 135 setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal); 136 } setThreadLocalMode(ThreadLocalMode Val)137 void setThreadLocalMode(ThreadLocalMode Val) { 138 assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal); 139 ThreadLocal = Val; 140 } getThreadLocalMode()141 ThreadLocalMode getThreadLocalMode() const { 142 return static_cast<ThreadLocalMode>(ThreadLocal); 143 } 144 getDLLStorageClass()145 DLLStorageClassTypes getDLLStorageClass() const { 146 return DLLStorageClassTypes(DllStorageClass); 147 } hasDLLImportStorageClass()148 bool hasDLLImportStorageClass() const { 149 return DllStorageClass == DLLImportStorageClass; 150 } hasDLLExportStorageClass()151 bool hasDLLExportStorageClass() const { 152 return DllStorageClass == DLLExportStorageClass; 153 } setDLLStorageClass(DLLStorageClassTypes C)154 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; } 155 hasSection()156 bool hasSection() const { return !StringRef(getSection()).empty(); } 157 // It is unfortunate that we have to use "char *" in here since this is 158 // always non NULL, but: 159 // * The C API expects a null terminated string, so we cannot use StringRef. 160 // * The C API expects us to own it, so we cannot use a std:string. 161 // * For GlobalAliases we can fail to find the section and we have to 162 // return "", so we cannot use a "const std::string &". 163 const char *getSection() const; 164 165 /// Global values are always pointers. getType()166 inline PointerType *getType() const { 167 return cast<PointerType>(User::getType()); 168 } 169 getLinkOnceLinkage(bool ODR)170 static LinkageTypes getLinkOnceLinkage(bool ODR) { 171 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; 172 } getWeakLinkage(bool ODR)173 static LinkageTypes getWeakLinkage(bool ODR) { 174 return ODR ? WeakODRLinkage : WeakAnyLinkage; 175 } 176 isExternalLinkage(LinkageTypes Linkage)177 static bool isExternalLinkage(LinkageTypes Linkage) { 178 return Linkage == ExternalLinkage; 179 } isAvailableExternallyLinkage(LinkageTypes Linkage)180 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) { 181 return Linkage == AvailableExternallyLinkage; 182 } isLinkOnceODRLinkage(LinkageTypes Linkage)183 static bool isLinkOnceODRLinkage(LinkageTypes Linkage) { 184 return Linkage == LinkOnceODRLinkage; 185 } isLinkOnceLinkage(LinkageTypes Linkage)186 static bool isLinkOnceLinkage(LinkageTypes Linkage) { 187 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage; 188 } isWeakAnyLinkage(LinkageTypes Linkage)189 static bool isWeakAnyLinkage(LinkageTypes Linkage) { 190 return Linkage == WeakAnyLinkage; 191 } isWeakODRLinkage(LinkageTypes Linkage)192 static bool isWeakODRLinkage(LinkageTypes Linkage) { 193 return Linkage == WeakODRLinkage; 194 } isWeakLinkage(LinkageTypes Linkage)195 static bool isWeakLinkage(LinkageTypes Linkage) { 196 return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage); 197 } isAppendingLinkage(LinkageTypes Linkage)198 static bool isAppendingLinkage(LinkageTypes Linkage) { 199 return Linkage == AppendingLinkage; 200 } isInternalLinkage(LinkageTypes Linkage)201 static bool isInternalLinkage(LinkageTypes Linkage) { 202 return Linkage == InternalLinkage; 203 } isPrivateLinkage(LinkageTypes Linkage)204 static bool isPrivateLinkage(LinkageTypes Linkage) { 205 return Linkage == PrivateLinkage; 206 } isLocalLinkage(LinkageTypes Linkage)207 static bool isLocalLinkage(LinkageTypes Linkage) { 208 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage); 209 } isExternalWeakLinkage(LinkageTypes Linkage)210 static bool isExternalWeakLinkage(LinkageTypes Linkage) { 211 return Linkage == ExternalWeakLinkage; 212 } isCommonLinkage(LinkageTypes Linkage)213 static bool isCommonLinkage(LinkageTypes Linkage) { 214 return Linkage == CommonLinkage; 215 } 216 217 /// Whether the definition of this global may be discarded if it is not used 218 /// in its compilation unit. isDiscardableIfUnused(LinkageTypes Linkage)219 static bool isDiscardableIfUnused(LinkageTypes Linkage) { 220 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage); 221 } 222 223 /// Whether the definition of this global may be replaced by something 224 /// non-equivalent at link time. For example, if a function has weak linkage 225 /// then the code defining it may be replaced by different code. mayBeOverridden(LinkageTypes Linkage)226 static bool mayBeOverridden(LinkageTypes Linkage) { 227 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage || 228 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage; 229 } 230 231 /// Whether the definition of this global may be replaced at link time. NB: 232 /// Using this method outside of the code generators is almost always a 233 /// mistake: when working at the IR level use mayBeOverridden instead as it 234 /// knows about ODR semantics. isWeakForLinker(LinkageTypes Linkage)235 static bool isWeakForLinker(LinkageTypes Linkage) { 236 return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage || 237 Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage || 238 Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage || 239 Linkage == ExternalWeakLinkage; 240 } 241 hasExternalLinkage()242 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); } hasAvailableExternallyLinkage()243 bool hasAvailableExternallyLinkage() const { 244 return isAvailableExternallyLinkage(Linkage); 245 } hasLinkOnceLinkage()246 bool hasLinkOnceLinkage() const { 247 return isLinkOnceLinkage(Linkage); 248 } hasWeakLinkage()249 bool hasWeakLinkage() const { 250 return isWeakLinkage(Linkage); 251 } hasWeakAnyLinkage()252 bool hasWeakAnyLinkage() const { 253 return isWeakAnyLinkage(Linkage); 254 } hasWeakODRLinkage()255 bool hasWeakODRLinkage() const { 256 return isWeakODRLinkage(Linkage); 257 } hasAppendingLinkage()258 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); } hasInternalLinkage()259 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); } hasPrivateLinkage()260 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); } hasLocalLinkage()261 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); } hasExternalWeakLinkage()262 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); } hasCommonLinkage()263 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); } 264 setLinkage(LinkageTypes LT)265 void setLinkage(LinkageTypes LT) { 266 if (isLocalLinkage(LT)) 267 Visibility = DefaultVisibility; 268 Linkage = LT; 269 } getLinkage()270 LinkageTypes getLinkage() const { return Linkage; } 271 isDiscardableIfUnused()272 bool isDiscardableIfUnused() const { 273 return isDiscardableIfUnused(Linkage); 274 } 275 mayBeOverridden()276 bool mayBeOverridden() const { return mayBeOverridden(Linkage); } 277 isWeakForLinker()278 bool isWeakForLinker() const { return isWeakForLinker(Linkage); } 279 280 /// Copy all additional attributes (those not needed to create a GlobalValue) 281 /// from the GlobalValue Src to this one. 282 virtual void copyAttributesFrom(const GlobalValue *Src); 283 284 /// If special LLVM prefix that is used to inform the asm printer to not emit 285 /// usual symbol prefix before the symbol name is used then return linkage 286 /// name after skipping this special LLVM prefix. getRealLinkageName(StringRef Name)287 static StringRef getRealLinkageName(StringRef Name) { 288 if (!Name.empty() && Name[0] == '\1') 289 return Name.substr(1); 290 return Name; 291 } 292 293 /// @name Materialization 294 /// Materialization is used to construct functions only as they're needed. This 295 /// is useful to reduce memory usage in LLVM or parsing work done by the 296 /// BitcodeReader to load the Module. 297 /// @{ 298 299 /// If this function's Module is being lazily streamed in functions from disk 300 /// or some other source, this method can be used to check to see if the 301 /// function has been read in yet or not. 302 bool isMaterializable() const; 303 304 /// Returns true if this function was loaded from a GVMaterializer that's 305 /// still attached to its Module and that knows how to dematerialize the 306 /// function. 307 bool isDematerializable() const; 308 309 /// Make sure this GlobalValue is fully read. If the module is corrupt, this 310 /// returns true and fills in the optional string with information about the 311 /// problem. If successful, this returns false. 312 bool Materialize(std::string *ErrInfo = nullptr); 313 314 /// If this GlobalValue is read in, and if the GVMaterializer supports it, 315 /// release the memory for the function, and set it up to be materialized 316 /// lazily. If !isDematerializable(), this method is a noop. 317 void Dematerialize(); 318 319 /// @} 320 321 /// Override from Constant class. 322 void destroyConstant() override; 323 324 /// Return true if the primary definition of this global value is outside of 325 /// the current translation unit. 326 bool isDeclaration() const; 327 328 /// This method unlinks 'this' from the containing module, but does not delete 329 /// it. 330 virtual void removeFromParent() = 0; 331 332 /// This method unlinks 'this' from the containing module and deletes it. 333 virtual void eraseFromParent() = 0; 334 335 /// Get the module that this global value is contained inside of... getParent()336 inline Module *getParent() { return Parent; } getParent()337 inline const Module *getParent() const { return Parent; } 338 339 const DataLayout *getDataLayout() const; 340 341 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Value * V)342 static inline bool classof(const Value *V) { 343 return V->getValueID() == Value::FunctionVal || 344 V->getValueID() == Value::GlobalVariableVal || 345 V->getValueID() == Value::GlobalAliasVal; 346 } 347 }; 348 349 } // End llvm namespace 350 351 #endif 352