diff options
Diffstat (limited to 'include/llvm/GlobalVariable.h')
-rw-r--r-- | include/llvm/GlobalVariable.h | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/include/llvm/GlobalVariable.h b/include/llvm/GlobalVariable.h index 633e8b4..1769c66 100644 --- a/include/llvm/GlobalVariable.h +++ b/include/llvm/GlobalVariable.h @@ -68,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; } @@ -80,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 @@ -88,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! @@ -142,7 +169,8 @@ public: }; template <> -struct OperandTraits<GlobalVariable> : public OptionalOperandTraits<> { +struct OperandTraits<GlobalVariable> : + public OptionalOperandTraits<GlobalVariable> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) |