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 23 namespace llvm { 24 25 class PointerType; 26 class Module; 27 28 class GlobalValue : public Constant { 29 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION; 30 public: 31 /// @brief An enumeration for the kinds of linkage for global values. 32 enum LinkageTypes { 33 ExternalLinkage = 0,///< Externally visible function 34 AvailableExternallyLinkage, ///< Available for inspection, not emission. 35 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline) 36 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent. 37 LinkOnceODRAutoHideLinkage, ///< Like LinkOnceODRLinkage but addr not taken. 38 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak) 39 WeakODRLinkage, ///< Same, but only replaced by something equivalent. 40 AppendingLinkage, ///< Special purpose, only applies to global arrays 41 InternalLinkage, ///< Rename collisions when linking (static functions). 42 PrivateLinkage, ///< Like Internal, but omit from symbol table. 43 LinkerPrivateLinkage, ///< Like Private, but linker removes. 44 LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak. 45 DLLImportLinkage, ///< Function to be imported from DLL 46 DLLExportLinkage, ///< Function to be accessible from DLL. 47 ExternalWeakLinkage,///< ExternalWeak linkage description. 48 CommonLinkage ///< Tentative definitions. 49 }; 50 51 /// @brief An enumeration for the kinds of visibility of global values. 52 enum VisibilityTypes { 53 DefaultVisibility = 0, ///< The GV is visible 54 HiddenVisibility, ///< The GV is hidden 55 ProtectedVisibility ///< The GV is protected 56 }; 57 58 protected: 59 GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps, 60 LinkageTypes linkage, const Twine &Name) 61 : Constant(ty, vty, Ops, NumOps), Linkage(linkage), 62 Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0), Parent(0) { 63 setName(Name); 64 } 65 66 // Note: VC++ treats enums as signed, so an extra bit is required to prevent 67 // Linkage and Visibility from turning into negative values. 68 LinkageTypes Linkage : 5; // The linkage of this global 69 unsigned Visibility : 2; // The visibility style of this global 70 unsigned Alignment : 16; // Alignment of this symbol, must be power of two 71 unsigned UnnamedAddr : 1; // This value's address is not significant 72 Module *Parent; // The containing module. 73 std::string Section; // Section to emit this into, empty mean default 74 public: 75 ~GlobalValue() { 76 removeDeadConstantUsers(); // remove any dead constants using this. 77 } 78 79 unsigned getAlignment() const { 80 return (1u << Alignment) >> 1; 81 } 82 void setAlignment(unsigned Align); 83 84 bool hasUnnamedAddr() const { return UnnamedAddr; } 85 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; } 86 87 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } 88 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; } 89 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } 90 bool hasProtectedVisibility() const { 91 return Visibility == ProtectedVisibility; 92 } 93 void setVisibility(VisibilityTypes V) { Visibility = V; } 94 95 bool hasSection() const { return !Section.empty(); } 96 const std::string &getSection() const { return Section; } 97 void setSection(StringRef S) { Section = S; } 98 99 /// If the usage is empty (except transitively dead constants), then this 100 /// global value can be safely deleted since the destructor will 101 /// delete the dead constants as well. 102 /// @brief Determine if the usage of this global value is empty except 103 /// for transitively dead constants. 104 bool use_empty_except_constants(); 105 106 /// getType - Global values are always pointers. 107 inline PointerType *getType() const { 108 return reinterpret_cast<PointerType*>(User::getType()); 109 } 110 111 static LinkageTypes getLinkOnceLinkage(bool ODR) { 112 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; 113 } 114 static LinkageTypes getWeakLinkage(bool ODR) { 115 return ODR ? WeakODRLinkage : WeakAnyLinkage; 116 } 117 118 static bool isExternalLinkage(LinkageTypes Linkage) { 119 return Linkage == ExternalLinkage; 120 } 121 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) { 122 return Linkage == AvailableExternallyLinkage; 123 } 124 static bool isLinkOnceLinkage(LinkageTypes Linkage) { 125 return Linkage == LinkOnceAnyLinkage || 126 Linkage == LinkOnceODRLinkage || 127 Linkage == LinkOnceODRAutoHideLinkage; 128 } 129 static bool isLinkOnceODRAutoHideLinkage(LinkageTypes Linkage) { 130 return Linkage == LinkOnceODRAutoHideLinkage; 131 } 132 static bool isWeakLinkage(LinkageTypes Linkage) { 133 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage; 134 } 135 static bool isAppendingLinkage(LinkageTypes Linkage) { 136 return Linkage == AppendingLinkage; 137 } 138 static bool isInternalLinkage(LinkageTypes Linkage) { 139 return Linkage == InternalLinkage; 140 } 141 static bool isPrivateLinkage(LinkageTypes Linkage) { 142 return Linkage == PrivateLinkage; 143 } 144 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) { 145 return Linkage == LinkerPrivateLinkage; 146 } 147 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) { 148 return Linkage == LinkerPrivateWeakLinkage; 149 } 150 static bool isLocalLinkage(LinkageTypes Linkage) { 151 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) || 152 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage); 153 } 154 static bool isDLLImportLinkage(LinkageTypes Linkage) { 155 return Linkage == DLLImportLinkage; 156 } 157 static bool isDLLExportLinkage(LinkageTypes Linkage) { 158 return Linkage == DLLExportLinkage; 159 } 160 static bool isExternalWeakLinkage(LinkageTypes Linkage) { 161 return Linkage == ExternalWeakLinkage; 162 } 163 static bool isCommonLinkage(LinkageTypes Linkage) { 164 return Linkage == CommonLinkage; 165 } 166 167 /// isDiscardableIfUnused - Whether the definition of this global may be 168 /// discarded if it is not used in its compilation unit. 169 static bool isDiscardableIfUnused(LinkageTypes Linkage) { 170 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage); 171 } 172 173 /// mayBeOverridden - Whether the definition of this global may be replaced 174 /// by something non-equivalent at link time. For example, if a function has 175 /// weak linkage then the code defining it may be replaced by different code. 176 static bool mayBeOverridden(LinkageTypes Linkage) { 177 return Linkage == WeakAnyLinkage || 178 Linkage == LinkOnceAnyLinkage || 179 Linkage == CommonLinkage || 180 Linkage == ExternalWeakLinkage || 181 Linkage == LinkerPrivateWeakLinkage; 182 } 183 184 /// isWeakForLinker - Whether the definition of this global may be replaced at 185 /// link time. NB: Using this method outside of the code generators is almost 186 /// always a mistake: when working at the IR level use mayBeOverridden instead 187 /// as it knows about ODR semantics. 188 static bool isWeakForLinker(LinkageTypes Linkage) { 189 return Linkage == AvailableExternallyLinkage || 190 Linkage == WeakAnyLinkage || 191 Linkage == WeakODRLinkage || 192 Linkage == LinkOnceAnyLinkage || 193 Linkage == LinkOnceODRLinkage || 194 Linkage == LinkOnceODRAutoHideLinkage || 195 Linkage == CommonLinkage || 196 Linkage == ExternalWeakLinkage || 197 Linkage == LinkerPrivateWeakLinkage; 198 } 199 200 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); } 201 bool hasAvailableExternallyLinkage() const { 202 return isAvailableExternallyLinkage(Linkage); 203 } 204 bool hasLinkOnceLinkage() const { 205 return isLinkOnceLinkage(Linkage); 206 } 207 bool hasLinkOnceODRAutoHideLinkage() const { 208 return isLinkOnceODRAutoHideLinkage(Linkage); 209 } 210 bool hasWeakLinkage() const { 211 return isWeakLinkage(Linkage); 212 } 213 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); } 214 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); } 215 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); } 216 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); } 217 bool hasLinkerPrivateWeakLinkage() const { 218 return isLinkerPrivateWeakLinkage(Linkage); 219 } 220 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); } 221 bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); } 222 bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); } 223 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); } 224 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); } 225 226 void setLinkage(LinkageTypes LT) { Linkage = LT; } 227 LinkageTypes getLinkage() const { return Linkage; } 228 229 bool isDiscardableIfUnused() const { 230 return isDiscardableIfUnused(Linkage); 231 } 232 233 bool mayBeOverridden() const { return mayBeOverridden(Linkage); } 234 235 bool isWeakForLinker() const { return isWeakForLinker(Linkage); } 236 237 /// copyAttributesFrom - copy all additional attributes (those not needed to 238 /// create a GlobalValue) from the GlobalValue Src to this one. 239 virtual void copyAttributesFrom(const GlobalValue *Src); 240 241 /// @name Materialization 242 /// Materialization is used to construct functions only as they're needed. This 243 /// is useful to reduce memory usage in LLVM or parsing work done by the 244 /// BitcodeReader to load the Module. 245 /// @{ 246 247 /// isMaterializable - If this function's Module is being lazily streamed in 248 /// functions from disk or some other source, this method can be used to check 249 /// to see if the function has been read in yet or not. 250 bool isMaterializable() const; 251 252 /// isDematerializable - Returns true if this function was loaded from a 253 /// GVMaterializer that's still attached to its Module and that knows how to 254 /// dematerialize the function. 255 bool isDematerializable() const; 256 257 /// Materialize - make sure this GlobalValue is fully read. If the module is 258 /// corrupt, this returns true and fills in the optional string with 259 /// information about the problem. If successful, this returns false. 260 bool Materialize(std::string *ErrInfo = 0); 261 262 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer 263 /// supports it, release the memory for the function, and set it up to be 264 /// materialized lazily. If !isDematerializable(), this method is a noop. 265 void Dematerialize(); 266 267 /// @} 268 269 /// Override from Constant class. 270 virtual void destroyConstant(); 271 272 /// isDeclaration - Return true if the primary definition of this global 273 /// value is outside of the current translation unit. 274 bool isDeclaration() const; 275 276 /// removeFromParent - This method unlinks 'this' from the containing module, 277 /// but does not delete it. 278 virtual void removeFromParent() = 0; 279 280 /// eraseFromParent - This method unlinks 'this' from the containing module 281 /// and deletes it. 282 virtual void eraseFromParent() = 0; 283 284 /// getParent - Get the module that this global value is contained inside 285 /// of... 286 inline Module *getParent() { return Parent; } 287 inline const Module *getParent() const { return Parent; } 288 289 // Methods for support type inquiry through isa, cast, and dyn_cast: 290 static inline bool classof(const Value *V) { 291 return V->getValueID() == Value::FunctionVal || 292 V->getValueID() == Value::GlobalVariableVal || 293 V->getValueID() == Value::GlobalAliasVal; 294 } 295 }; 296 297 } // End llvm namespace 298 299 #endif 300