GlobalVariable.h revision 309124
1249259Sdim//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file contains the declaration of the GlobalVariable class, which 11249259Sdim// represents a single global variable (or constant) in the VM. 12249259Sdim// 13249259Sdim// Global variables are constant pointers that refer to hunks of space that are 14249259Sdim// allocated by either the VM, or by the linker in a static compiler. A global 15249259Sdim// variable may have an initial value, which is copied into the executables .data 16249259Sdim// area. Global Constants are required to have initializers. 17249259Sdim// 18249259Sdim//===----------------------------------------------------------------------===// 19249259Sdim 20249259Sdim#ifndef LLVM_IR_GLOBALVARIABLE_H 21249259Sdim#define LLVM_IR_GLOBALVARIABLE_H 22249259Sdim 23249259Sdim#include "llvm/ADT/Twine.h" 24249259Sdim#include "llvm/ADT/ilist_node.h" 25276479Sdim#include "llvm/IR/GlobalObject.h" 26249259Sdim#include "llvm/IR/OperandTraits.h" 27249259Sdim 28249259Sdimnamespace llvm { 29249259Sdim 30249259Sdimclass Module; 31249259Sdimclass Constant; 32296417Sdimtemplate <typename ValueSubClass> class SymbolTableListTraits; 33249259Sdim 34276479Sdimclass GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> { 35296417Sdim friend class SymbolTableListTraits<GlobalVariable>; 36288943Sdim void *operator new(size_t, unsigned) = delete; 37288943Sdim void operator=(const GlobalVariable &) = delete; 38288943Sdim GlobalVariable(const GlobalVariable &) = delete; 39249259Sdim 40249259Sdim void setParent(Module *parent); 41249259Sdim 42249259Sdim bool isConstantGlobal : 1; // Is this a global constant? 43249259Sdim bool isExternallyInitializedConstant : 1; // Is this a global whose value 44249259Sdim // can change from its initial 45249259Sdim // value before global 46249259Sdim // initializers are run? 47249259Sdimpublic: 48249259Sdim // allocate space for exactly one operand 49249259Sdim void *operator new(size_t s) { 50249259Sdim return User::operator new(s, 1); 51249259Sdim } 52249259Sdim 53249259Sdim /// GlobalVariable ctor - If a parent module is specified, the global is 54249259Sdim /// automatically inserted into the end of the specified modules global list. 55249259Sdim GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage, 56276479Sdim Constant *Initializer = nullptr, const Twine &Name = "", 57249259Sdim ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, 58249259Sdim bool isExternallyInitialized = false); 59249259Sdim /// GlobalVariable ctor - This creates a global and inserts it before the 60249259Sdim /// specified other global. 61249259Sdim GlobalVariable(Module &M, Type *Ty, bool isConstant, 62249259Sdim LinkageTypes Linkage, Constant *Initializer, 63276479Sdim const Twine &Name = "", GlobalVariable *InsertBefore = nullptr, 64249259Sdim ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, 65249259Sdim bool isExternallyInitialized = false); 66249259Sdim 67288943Sdim ~GlobalVariable() override { 68309124Sdim dropAllReferences(); 69309124Sdim 70288943Sdim // FIXME: needed by operator delete 71288943Sdim setGlobalVariableNumOperands(1); 72249259Sdim } 73249259Sdim 74249259Sdim /// Provide fast operand accessors 75249259Sdim DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 76249259Sdim 77261991Sdim /// Definitions have initializers, declarations don't. 78249259Sdim /// 79249259Sdim inline bool hasInitializer() const { return !isDeclaration(); } 80249259Sdim 81249259Sdim /// hasDefinitiveInitializer - Whether the global variable has an initializer, 82249259Sdim /// and any other instances of the global (this can happen due to weak 83249259Sdim /// linkage) are guaranteed to have the same initializer. 84249259Sdim /// 85249259Sdim /// Note that if you want to transform a global, you must use 86249259Sdim /// hasUniqueInitializer() instead, because of the *_odr linkage type. 87249259Sdim /// 88249259Sdim /// Example: 89249259Sdim /// 90249259Sdim /// @a = global SomeType* null - Initializer is both definitive and unique. 91249259Sdim /// 92249259Sdim /// @b = global weak SomeType* null - Initializer is neither definitive nor 93249259Sdim /// unique. 94249259Sdim /// 95249259Sdim /// @c = global weak_odr SomeType* null - Initializer is definitive, but not 96249259Sdim /// unique. 97249259Sdim inline bool hasDefinitiveInitializer() const { 98249259Sdim return hasInitializer() && 99309124Sdim // The initializer of a global variable may change to something arbitrary 100309124Sdim // at link time. 101309124Sdim !isInterposable() && 102249259Sdim // The initializer of a global variable with the externally_initialized 103249259Sdim // marker may change at runtime before C++ initializers are evaluated. 104249259Sdim !isExternallyInitialized(); 105249259Sdim } 106249259Sdim 107249259Sdim /// hasUniqueInitializer - Whether the global variable has an initializer, and 108249259Sdim /// any changes made to the initializer will turn up in the final executable. 109249259Sdim inline bool hasUniqueInitializer() const { 110296417Sdim return 111296417Sdim // We need to be sure this is the definition that will actually be used 112296417Sdim isStrongDefinitionForLinker() && 113296417Sdim // It is not safe to modify initializers of global variables with the 114296417Sdim // external_initializer marker since the value may be changed at runtime 115296417Sdim // before C++ initializers are evaluated. 116296417Sdim !isExternallyInitialized(); 117249259Sdim } 118249259Sdim 119249259Sdim /// getInitializer - Return the initializer for this global variable. It is 120249259Sdim /// illegal to call this method if the global is external, because we cannot 121249259Sdim /// tell what the value is initialized to! 122249259Sdim /// 123249259Sdim inline const Constant *getInitializer() const { 124249259Sdim assert(hasInitializer() && "GV doesn't have initializer!"); 125249259Sdim return static_cast<Constant*>(Op<0>().get()); 126249259Sdim } 127249259Sdim inline Constant *getInitializer() { 128249259Sdim assert(hasInitializer() && "GV doesn't have initializer!"); 129249259Sdim return static_cast<Constant*>(Op<0>().get()); 130249259Sdim } 131249259Sdim /// setInitializer - Sets the initializer for this global variable, removing 132249259Sdim /// any existing initializer if InitVal==NULL. If this GV has type T*, the 133249259Sdim /// initializer must have type T. 134249259Sdim void setInitializer(Constant *InitVal); 135249259Sdim 136249259Sdim /// If the value is a global constant, its value is immutable throughout the 137249259Sdim /// runtime execution of the program. Assigning a value into the constant 138249259Sdim /// leads to undefined behavior. 139249259Sdim /// 140249259Sdim bool isConstant() const { return isConstantGlobal; } 141249259Sdim void setConstant(bool Val) { isConstantGlobal = Val; } 142249259Sdim 143249259Sdim bool isExternallyInitialized() const { 144249259Sdim return isExternallyInitializedConstant; 145249259Sdim } 146249259Sdim void setExternallyInitialized(bool Val) { 147249259Sdim isExternallyInitializedConstant = Val; 148249259Sdim } 149249259Sdim 150249259Sdim /// copyAttributesFrom - copy all additional attributes (those not needed to 151249259Sdim /// create a GlobalVariable) from the GlobalVariable Src to this one. 152276479Sdim void copyAttributesFrom(const GlobalValue *Src) override; 153249259Sdim 154249259Sdim /// removeFromParent - This method unlinks 'this' from the containing module, 155249259Sdim /// but does not delete it. 156249259Sdim /// 157276479Sdim void removeFromParent() override; 158249259Sdim 159249259Sdim /// eraseFromParent - This method unlinks 'this' from the containing module 160249259Sdim /// and deletes it. 161249259Sdim /// 162276479Sdim void eraseFromParent() override; 163249259Sdim 164309124Sdim /// Drop all references in preparation to destroy the GlobalVariable. This 165309124Sdim /// drops not only the reference to the initializer but also to any metadata. 166309124Sdim void dropAllReferences(); 167309124Sdim 168249259Sdim // Methods for support type inquiry through isa, cast, and dyn_cast: 169249259Sdim static inline bool classof(const Value *V) { 170249259Sdim return V->getValueID() == Value::GlobalVariableVal; 171249259Sdim } 172249259Sdim}; 173249259Sdim 174249259Sdimtemplate <> 175249259Sdimstruct OperandTraits<GlobalVariable> : 176249259Sdim public OptionalOperandTraits<GlobalVariable> { 177249259Sdim}; 178249259Sdim 179249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) 180249259Sdim 181249259Sdim} // End llvm namespace 182249259Sdim 183249259Sdim#endif 184