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 = 0, 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 = 0,
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   /// hasInitializer - Unless a global variable isExternal(), it has an
00088   /// initializer.  The initializer for the global variable/constant is held by
00089   /// Initializer if an initializer is specified.
00090   ///
00091   inline bool hasInitializer() const { return !isDeclaration(); }
00092 
00093   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
00094   /// and any other instances of the global (this can happen due to weak
00095   /// linkage) are guaranteed to have the same initializer.
00096   ///
00097   /// Note that if you want to transform a global, you must use
00098   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
00099   ///
00100   /// Example:
00101   ///
00102   /// @a = global SomeType* null - Initializer is both definitive and unique.
00103   ///
00104   /// @b = global weak SomeType* null - Initializer is neither definitive nor
00105   /// unique.
00106   ///
00107   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
00108   /// unique.
00109   inline bool hasDefinitiveInitializer() const {
00110     return hasInitializer() &&
00111       // The initializer of a global variable with weak linkage may change at
00112       // link time.
00113       !mayBeOverridden() &&
00114       // The initializer of a global variable with the externally_initialized
00115       // marker may change at runtime before C++ initializers are evaluated.
00116       !isExternallyInitialized();
00117   }
00118 
00119   /// hasUniqueInitializer - Whether the global variable has an initializer, and
00120   /// any changes made to the initializer will turn up in the final executable.
00121   inline bool hasUniqueInitializer() const {
00122     return hasInitializer() &&
00123       // It's not safe to modify initializers of global variables with weak
00124       // linkage, because the linker might choose to discard the initializer and
00125       // use the initializer from another instance of the global variable
00126       // instead. It is wrong to modify the initializer of a global variable
00127       // with *_odr linkage because then different instances of the global may
00128       // have different initializers, breaking the One Definition Rule.
00129       !isWeakForLinker() &&
00130       // It is not safe to modify initializers of global variables with the
00131       // external_initializer marker since the value may be changed at runtime
00132       // before C++ initializers are evaluated.
00133       !isExternallyInitialized();
00134   }
00135 
00136   /// getInitializer - Return the initializer for this global variable.  It is
00137   /// illegal to call this method if the global is external, because we cannot
00138   /// tell what the value is initialized to!
00139   ///
00140   inline const Constant *getInitializer() const {
00141     assert(hasInitializer() && "GV doesn't have initializer!");
00142     return static_cast<Constant*>(Op<0>().get());
00143   }
00144   inline Constant *getInitializer() {
00145     assert(hasInitializer() && "GV doesn't have initializer!");
00146     return static_cast<Constant*>(Op<0>().get());
00147   }
00148   /// setInitializer - Sets the initializer for this global variable, removing
00149   /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
00150   /// initializer must have type T.
00151   void setInitializer(Constant *InitVal);
00152 
00153   /// If the value is a global constant, its value is immutable throughout the
00154   /// runtime execution of the program.  Assigning a value into the constant
00155   /// leads to undefined behavior.
00156   ///
00157   bool isConstant() const { return isConstantGlobal; }
00158   void setConstant(bool Val) { isConstantGlobal = Val; }
00159 
00160   /// If the value is "Thread Local", its value isn't shared by the threads.
00161   bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
00162   void setThreadLocal(bool Val) {
00163     threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
00164   }
00165   void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
00166   ThreadLocalMode getThreadLocalMode() const {
00167     return static_cast<ThreadLocalMode>(threadLocalMode);
00168   }
00169 
00170   bool isExternallyInitialized() const {
00171     return isExternallyInitializedConstant;
00172   }
00173   void setExternallyInitialized(bool Val) {
00174     isExternallyInitializedConstant = Val;
00175   }
00176 
00177   /// copyAttributesFrom - copy all additional attributes (those not needed to
00178   /// create a GlobalVariable) from the GlobalVariable Src to this one.
00179   void copyAttributesFrom(const GlobalValue *Src);
00180 
00181   /// removeFromParent - This method unlinks 'this' from the containing module,
00182   /// but does not delete it.
00183   ///
00184   virtual void removeFromParent();
00185 
00186   /// eraseFromParent - This method unlinks 'this' from the containing module
00187   /// and deletes it.
00188   ///
00189   virtual void eraseFromParent();
00190 
00191   /// Override Constant's implementation of this method so we can
00192   /// replace constant initializers.
00193   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
00194 
00195   // Methods for support type inquiry through isa, cast, and dyn_cast:
00196   static inline bool classof(const Value *V) {
00197     return V->getValueID() == Value::GlobalVariableVal;
00198   }
00199 };
00200 
00201 template <>
00202 struct OperandTraits<GlobalVariable> :
00203   public OptionalOperandTraits<GlobalVariable> {
00204 };
00205 
00206 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
00207 
00208 } // End llvm namespace
00209 
00210 #endif