X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FGlobalVariable.h;h=597583b2ceed10dc0f2f1af8efd7e58e85869db4;hb=0f899c78e18e39a8ccd08386aaa493a1b9a712a3;hp=28eaa624e9d2b2e710cce9d0f0568748712b5696;hpb=e9b11b431308f4766b73cda93e38ec930c912122;p=oota-llvm.git diff --git a/include/llvm/GlobalVariable.h b/include/llvm/GlobalVariable.h index 28eaa624e9d..597583b2cee 100644 --- a/include/llvm/GlobalVariable.h +++ b/include/llvm/GlobalVariable.h @@ -23,12 +23,12 @@ #include "llvm/GlobalValue.h" #include "llvm/OperandTraits.h" #include "llvm/ADT/ilist_node.h" +#include "llvm/ADT/Twine.h" namespace llvm { class Module; class Constant; -class LLVMContext; template class SymbolTableListTraits; @@ -50,15 +50,14 @@ public: } /// GlobalVariable ctor - If a parent module is specified, the global is /// automatically inserted into the end of the specified modules global list. - GlobalVariable(LLVMContext &Context, const Type *Ty, bool isConstant, - LinkageTypes Linkage, - Constant *Initializer = 0, const std::string &Name = "", + GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage, + Constant *Initializer = 0, const Twine &Name = "", bool ThreadLocal = false, unsigned AddressSpace = 0); /// GlobalVariable ctor - This creates a global and inserts it before the /// specified other global. GlobalVariable(Module &M, const Type *Ty, bool isConstant, LinkageTypes Linkage, Constant *Initializer, - const std::string &Name, + const Twine &Name, GlobalVariable *InsertBefore = 0, bool ThreadLocal = false, unsigned AddressSpace = 0); @@ -69,7 +68,7 @@ public: /// Provide fast operand accessors DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); - /// isDeclaration - Is this global variable lacking an initializer? If so, + /// isDeclaration - Is this global variable lacking an initializer? If so, /// the global variable is defined in some other translation unit, and is thus /// only a declaration here. virtual bool isDeclaration() const { return getNumOperands() == 0; } @@ -81,7 +80,21 @@ public: inline bool hasInitializer() const { return !isDeclaration(); } /// hasDefinitiveInitializer - Whether the global variable has an initializer, - /// and this is the initializer that will be used in the final executable. + /// and any other instances of the global (this can happen due to weak + /// linkage) are guaranteed to have the same initializer. + /// + /// Note that if you want to transform a global, you must use + /// hasUniqueInitializer() instead, because of the *_odr linkage type. + /// + /// Example: + /// + /// @a = global SomeType* null - Initializer is both definitive and unique. + /// + /// @b = global weak SomeType* null - Initializer is neither definitive nor + /// unique. + /// + /// @c = global weak_odr SomeType* null - Initializer is definitive, but not + /// unique. inline bool hasDefinitiveInitializer() const { return hasInitializer() && // The initializer of a global variable with weak linkage may change at @@ -89,6 +102,19 @@ public: !mayBeOverridden(); } + /// hasUniqueInitializer - Whether the global variable has an initializer, and + /// any changes made to the initializer will turn up in the final executable. + inline bool hasUniqueInitializer() const { + return hasInitializer() && + // It's not safe to modify initializers of global variables with weak + // linkage, because the linker might choose to discard the initializer and + // use the initializer from another instance of the global variable + // instead. It is wrong to modify the initializer of a global variable + // with *_odr linkage because then different instances of the global may + // have different initializers, breaking the One Definition Rule. + !isWeakForLinker(); + } + /// getInitializer - Return the initializer for this global variable. It is /// illegal to call this method if the global is external, because we cannot /// tell what the value is initialized to! @@ -101,18 +127,10 @@ public: assert(hasInitializer() && "GV doesn't have initializer!"); return static_cast(Op<0>().get()); } - inline void setInitializer(Constant *CPV) { - if (CPV == 0) { - if (hasInitializer()) { - Op<0>().set(0); - NumOperands = 0; - } - } else { - if (!hasInitializer()) - NumOperands = 1; - Op<0>().set(CPV); - } - } + /// setInitializer - Sets the initializer for this global variable, removing + /// any existing initializer if InitVal==NULL. If this GV has type T*, the + /// initializer must have type T. + void setInitializer(Constant *InitVal); /// If the value is a global constant, its value is immutable throughout the /// runtime execution of the program. Assigning a value into the constant @@ -151,7 +169,7 @@ public: }; template <> -struct OperandTraits : OptionalOperandTraits<> { +struct OperandTraits : public OptionalOperandTraits<> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)