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