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