LLVM  mainline
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/GlobalObject.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 GlobalObject, public ilist_node<GlobalVariable> {
00036   friend class SymbolTableListTraits<GlobalVariable, Module>;
00037   void *operator new(size_t, unsigned) = delete;
00038   void operator=(const GlobalVariable &) = delete;
00039   GlobalVariable(const GlobalVariable &) = delete;
00040 
00041   void setParent(Module *parent);
00042 
00043   bool isConstantGlobal : 1;                   // Is this a global constant?
00044   bool isExternallyInitializedConstant : 1;    // Is this a global whose value
00045                                                // can change from its initial
00046                                                // value before global
00047                                                // initializers are run?
00048 
00049 public:
00050   // allocate space for exactly one operand
00051   void *operator new(size_t s) {
00052     return User::operator new(s, 1);
00053   }
00054 
00055   /// GlobalVariable ctor - If a parent module is specified, the global is
00056   /// automatically inserted into the end of the specified modules global list.
00057   GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
00058                  Constant *Initializer = nullptr, const Twine &Name = "",
00059                  ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
00060                  bool isExternallyInitialized = false);
00061   /// GlobalVariable ctor - This creates a global and inserts it before the
00062   /// specified other global.
00063   GlobalVariable(Module &M, Type *Ty, bool isConstant,
00064                  LinkageTypes Linkage, Constant *Initializer,
00065                  const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
00066                  ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
00067                  bool isExternallyInitialized = false);
00068 
00069   ~GlobalVariable() override {
00070     // FIXME: needed by operator delete
00071     setGlobalVariableNumOperands(1);
00072   }
00073 
00074   /// Provide fast operand accessors
00075   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00076 
00077   /// Definitions have initializers, declarations don't.
00078   ///
00079   inline bool hasInitializer() const { return !isDeclaration(); }
00080 
00081   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
00082   /// and any other instances of the global (this can happen due to weak
00083   /// linkage) are guaranteed to have the same initializer.
00084   ///
00085   /// Note that if you want to transform a global, you must use
00086   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
00087   ///
00088   /// Example:
00089   ///
00090   /// @a = global SomeType* null - Initializer is both definitive and unique.
00091   ///
00092   /// @b = global weak SomeType* null - Initializer is neither definitive nor
00093   /// unique.
00094   ///
00095   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
00096   /// unique.
00097   inline bool hasDefinitiveInitializer() const {
00098     return hasInitializer() &&
00099       // The initializer of a global variable with weak linkage may change at
00100       // link time.
00101       !mayBeOverridden() &&
00102       // The initializer of a global variable with the externally_initialized
00103       // marker may change at runtime before C++ initializers are evaluated.
00104       !isExternallyInitialized();
00105   }
00106 
00107   /// hasUniqueInitializer - Whether the global variable has an initializer, and
00108   /// any changes made to the initializer will turn up in the final executable.
00109   inline bool hasUniqueInitializer() const {
00110     return hasInitializer() &&
00111       // It's not safe to modify initializers of global variables with weak
00112       // linkage, because the linker might choose to discard the initializer and
00113       // use the initializer from another instance of the global variable
00114       // instead. It is wrong to modify the initializer of a global variable
00115       // with *_odr linkage because then different instances of the global may
00116       // have different initializers, breaking the One Definition Rule.
00117       !isWeakForLinker() &&
00118       // It is not safe to modify initializers of global variables with the
00119       // external_initializer marker since the value may be changed at runtime
00120       // before C++ initializers are evaluated.
00121       !isExternallyInitialized();
00122   }
00123 
00124   /// getInitializer - Return the initializer for this global variable.  It is
00125   /// illegal to call this method if the global is external, because we cannot
00126   /// tell what the value is initialized to!
00127   ///
00128   inline const Constant *getInitializer() const {
00129     assert(hasInitializer() && "GV doesn't have initializer!");
00130     return static_cast<Constant*>(Op<0>().get());
00131   }
00132   inline Constant *getInitializer() {
00133     assert(hasInitializer() && "GV doesn't have initializer!");
00134     return static_cast<Constant*>(Op<0>().get());
00135   }
00136   /// setInitializer - Sets the initializer for this global variable, removing
00137   /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
00138   /// initializer must have type T.
00139   void setInitializer(Constant *InitVal);
00140 
00141   /// If the value is a global constant, its value is immutable throughout the
00142   /// runtime execution of the program.  Assigning a value into the constant
00143   /// leads to undefined behavior.
00144   ///
00145   bool isConstant() const { return isConstantGlobal; }
00146   void setConstant(bool Val) { isConstantGlobal = Val; }
00147 
00148   bool isExternallyInitialized() const {
00149     return isExternallyInitializedConstant;
00150   }
00151   void setExternallyInitialized(bool Val) {
00152     isExternallyInitializedConstant = Val;
00153   }
00154 
00155   /// copyAttributesFrom - copy all additional attributes (those not needed to
00156   /// create a GlobalVariable) from the GlobalVariable Src to this one.
00157   void copyAttributesFrom(const GlobalValue *Src) override;
00158 
00159   /// removeFromParent - This method unlinks 'this' from the containing module,
00160   /// but does not delete it.
00161   ///
00162   void removeFromParent() override;
00163 
00164   /// eraseFromParent - This method unlinks 'this' from the containing module
00165   /// and deletes it.
00166   ///
00167   void eraseFromParent() override;
00168 
00169   // Methods for support type inquiry through isa, cast, and dyn_cast:
00170   static inline bool classof(const Value *V) {
00171     return V->getValueID() == Value::GlobalVariableVal;
00172   }
00173 };
00174 
00175 template <>
00176 struct OperandTraits<GlobalVariable> :
00177   public OptionalOperandTraits<GlobalVariable> {
00178 };
00179 
00180 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
00181 
00182 } // End llvm namespace
00183 
00184 #endif