LLVM API Documentation

GlobalVariable.h
Go to the documentation of this file.
00001 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the GlobalVariable class, which
00011 // represents a single global variable (or constant) in the VM.
00012 //
00013 // Global variables are constant pointers that refer to hunks of space that are
00014 // allocated by either the VM, or by the linker in a static compiler.  A global
00015 // variable may have an initial value, which is copied into the executables .data
00016 // area.  Global Constants are required to have initializers.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_IR_GLOBALVARIABLE_H
00021 #define LLVM_IR_GLOBALVARIABLE_H
00022 
00023 #include "llvm/ADT/Twine.h"
00024 #include "llvm/ADT/ilist_node.h"
00025 #include "llvm/IR/GlobalValue.h"
00026 #include "llvm/IR/OperandTraits.h"
00027 
00028 namespace llvm {
00029 
00030 class Module;
00031 class Constant;
00032 template<typename ValueSubClass, typename ItemParentClass>
00033   class SymbolTableListTraits;
00034 
00035 class GlobalVariable : public GlobalValue, public ilist_node<GlobalVariable> {
00036   friend class SymbolTableListTraits<GlobalVariable, Module>;
00037   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00038   void operator=(const GlobalVariable &) LLVM_DELETED_FUNCTION;
00039   GlobalVariable(const GlobalVariable &) LLVM_DELETED_FUNCTION;
00040 
00041   void setParent(Module *parent);
00042 
00043   bool isConstantGlobal : 1;                   // Is this a global constant?
00044   unsigned threadLocalMode : 3;                // Is this symbol "Thread Local",
00045                                                // if so, what is the desired
00046                                                // model?
00047   bool isExternallyInitializedConstant : 1;    // Is this a global whose value
00048                                                // can change from its initial
00049                                                // value before global
00050                                                // initializers are run?
00051 
00052 public:
00053   // allocate space for exactly one operand
00054   void *operator new(size_t s) {
00055     return User::operator new(s, 1);
00056   }
00057 
00058   enum ThreadLocalMode {
00059     NotThreadLocal = 0,
00060     GeneralDynamicTLSModel,
00061     LocalDynamicTLSModel,
00062     InitialExecTLSModel,
00063     LocalExecTLSModel
00064   };
00065 
00066   /// GlobalVariable ctor - If a parent module is specified, the global is
00067   /// automatically inserted into the end of the specified modules global list.
00068   GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
00069                  Constant *Initializer = nullptr, const Twine &Name = "",
00070                  ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
00071                  bool isExternallyInitialized = false);
00072   /// GlobalVariable ctor - This creates a global and inserts it before the
00073   /// specified other global.
00074   GlobalVariable(Module &M, Type *Ty, bool isConstant,
00075                  LinkageTypes Linkage, Constant *Initializer,
00076                  const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
00077                  ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
00078                  bool isExternallyInitialized = false);
00079 
00080   ~GlobalVariable() {
00081     NumOperands = 1; // FIXME: needed by operator delete
00082   }
00083 
00084   /// Provide fast operand accessors
00085   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00086 
00087   /// Definitions have initializers, declarations don't.
00088   ///
00089   inline bool hasInitializer() const { return !isDeclaration(); }
00090 
00091   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
00092   /// and any other instances of the global (this can happen due to weak
00093   /// linkage) are guaranteed to have the same initializer.
00094   ///
00095   /// Note that if you want to transform a global, you must use
00096   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
00097   ///
00098   /// Example:
00099   ///
00100   /// @a = global SomeType* null - Initializer is both definitive and unique.
00101   ///
00102   /// @b = global weak SomeType* null - Initializer is neither definitive nor
00103   /// unique.
00104   ///
00105   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
00106   /// unique.
00107   inline bool hasDefinitiveInitializer() const {
00108     return hasInitializer() &&
00109       // The initializer of a global variable with weak linkage may change at
00110       // link time.
00111       !mayBeOverridden() &&
00112       // The initializer of a global variable with the externally_initialized
00113       // marker may change at runtime before C++ initializers are evaluated.
00114       !isExternallyInitialized();
00115   }
00116 
00117   /// hasUniqueInitializer - Whether the global variable has an initializer, and
00118   /// any changes made to the initializer will turn up in the final executable.
00119   inline bool hasUniqueInitializer() const {
00120     return hasInitializer() &&
00121       // It's not safe to modify initializers of global variables with weak
00122       // linkage, because the linker might choose to discard the initializer and
00123       // use the initializer from another instance of the global variable
00124       // instead. It is wrong to modify the initializer of a global variable
00125       // with *_odr linkage because then different instances of the global may
00126       // have different initializers, breaking the One Definition Rule.
00127       !isWeakForLinker() &&
00128       // It is not safe to modify initializers of global variables with the
00129       // external_initializer marker since the value may be changed at runtime
00130       // before C++ initializers are evaluated.
00131       !isExternallyInitialized();
00132   }
00133 
00134   /// getInitializer - Return the initializer for this global variable.  It is
00135   /// illegal to call this method if the global is external, because we cannot
00136   /// tell what the value is initialized to!
00137   ///
00138   inline const Constant *getInitializer() const {
00139     assert(hasInitializer() && "GV doesn't have initializer!");
00140     return static_cast<Constant*>(Op<0>().get());
00141   }
00142   inline Constant *getInitializer() {
00143     assert(hasInitializer() && "GV doesn't have initializer!");
00144     return static_cast<Constant*>(Op<0>().get());
00145   }
00146   /// setInitializer - Sets the initializer for this global variable, removing
00147   /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
00148   /// initializer must have type T.
00149   void setInitializer(Constant *InitVal);
00150 
00151   /// If the value is a global constant, its value is immutable throughout the
00152   /// runtime execution of the program.  Assigning a value into the constant
00153   /// leads to undefined behavior.
00154   ///
00155   bool isConstant() const { return isConstantGlobal; }
00156   void setConstant(bool Val) { isConstantGlobal = Val; }
00157 
00158   /// If the value is "Thread Local", its value isn't shared by the threads.
00159   bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
00160   void setThreadLocal(bool Val) {
00161     threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
00162   }
00163   void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
00164   ThreadLocalMode getThreadLocalMode() const {
00165     return static_cast<ThreadLocalMode>(threadLocalMode);
00166   }
00167 
00168   bool isExternallyInitialized() const {
00169     return isExternallyInitializedConstant;
00170   }
00171   void setExternallyInitialized(bool Val) {
00172     isExternallyInitializedConstant = Val;
00173   }
00174 
00175   /// copyAttributesFrom - copy all additional attributes (those not needed to
00176   /// create a GlobalVariable) from the GlobalVariable Src to this one.
00177   void copyAttributesFrom(const GlobalValue *Src) override;
00178 
00179   /// removeFromParent - This method unlinks 'this' from the containing module,
00180   /// but does not delete it.
00181   ///
00182   void removeFromParent() override;
00183 
00184   /// eraseFromParent - This method unlinks 'this' from the containing module
00185   /// and deletes it.
00186   ///
00187   void eraseFromParent() override;
00188 
00189   /// Override Constant's implementation of this method so we can
00190   /// replace constant initializers.
00191   void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
00192 
00193   // Methods for support type inquiry through isa, cast, and dyn_cast:
00194   static inline bool classof(const Value *V) {
00195     return V->getValueID() == Value::GlobalVariableVal;
00196   }
00197 };
00198 
00199 template <>
00200 struct OperandTraits<GlobalVariable> :
00201   public OptionalOperandTraits<GlobalVariable> {
00202 };
00203 
00204 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
00205 
00206 } // End llvm namespace
00207 
00208 #endif