2004-07-02 20:41:18 +02:00
|
|
|
//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-10 09:58:01 +02:00
|
|
|
//
|
|
|
|
// This file contains the declaration of the GlobalVariable class, which
|
2001-09-18 06:01:05 +02:00
|
|
|
// represents a single global variable (or constant) in the VM.
|
2001-09-10 09:58:01 +02:00
|
|
|
//
|
|
|
|
// Global variables are constant pointers that refer to hunks of space that are
|
2001-09-18 06:01:05 +02:00
|
|
|
// allocated by either the VM, or by the linker in a static compiler. A global
|
|
|
|
// variable may have an intial value, which is copied into the executables .data
|
|
|
|
// area. Global Constants are required to have initializers.
|
2001-09-10 09:58:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_GLOBAL_VARIABLE_H
|
|
|
|
#define LLVM_GLOBAL_VARIABLE_H
|
|
|
|
|
2001-10-03 16:53:21 +02:00
|
|
|
#include "llvm/GlobalValue.h"
|
2008-05-10 10:32:32 +02:00
|
|
|
#include "llvm/OperandTraits.h"
|
2008-07-28 23:51:04 +02:00
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2009-12-29 08:12:03 +01:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2003-06-30 23:59:07 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
class Module;
|
2001-12-03 23:26:30 +01:00
|
|
|
class Constant;
|
2007-04-17 05:26:42 +02:00
|
|
|
template<typename ValueSubClass, typename ItemParentClass>
|
|
|
|
class SymbolTableListTraits;
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2008-07-28 23:51:04 +02:00
|
|
|
class GlobalVariable : public GlobalValue, public ilist_node<GlobalVariable> {
|
2007-04-17 05:26:42 +02:00
|
|
|
friend class SymbolTableListTraits<GlobalVariable, Module>;
|
2008-04-06 22:25:17 +02:00
|
|
|
void *operator new(size_t, unsigned); // Do not implement
|
2005-12-17 01:19:22 +01:00
|
|
|
void operator=(const GlobalVariable &); // Do not implement
|
|
|
|
GlobalVariable(const GlobalVariable &); // Do not implement
|
|
|
|
|
2002-09-06 23:31:57 +02:00
|
|
|
void setParent(Module *parent);
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2007-04-17 20:15:04 +02:00
|
|
|
bool isConstantGlobal : 1; // Is this a global constant?
|
|
|
|
bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
|
2005-01-29 01:33:00 +01:00
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
public:
|
2008-05-10 10:32:32 +02:00
|
|
|
// allocate space for exactly one operand
|
2008-04-06 22:25:17 +02:00
|
|
|
void *operator new(size_t s) {
|
2008-05-10 10:32:32 +02:00
|
|
|
return User::operator new(s, 1);
|
2008-04-06 22:25:17 +02:00
|
|
|
}
|
2002-09-06 23:31:57 +02:00
|
|
|
/// GlobalVariable ctor - If a parent module is specified, the global is
|
|
|
|
/// automatically inserted into the end of the specified modules global list.
|
2009-11-06 05:27:31 +01:00
|
|
|
GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
|
2009-07-25 06:41:11 +02:00
|
|
|
Constant *Initializer = 0, const Twine &Name = "",
|
2009-07-08 21:03:57 +02:00
|
|
|
bool ThreadLocal = false, unsigned AddressSpace = 0);
|
2006-09-30 23:31:26 +02:00
|
|
|
/// GlobalVariable ctor - This creates a global and inserts it before the
|
|
|
|
/// specified other global.
|
2009-07-08 21:03:57 +02:00
|
|
|
GlobalVariable(Module &M, const Type *Ty, bool isConstant,
|
|
|
|
LinkageTypes Linkage, Constant *Initializer,
|
2009-07-25 06:41:11 +02:00
|
|
|
const Twine &Name,
|
2009-07-08 21:03:57 +02:00
|
|
|
GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
|
2007-12-11 09:59:05 +01:00
|
|
|
unsigned AddressSpace = 0);
|
2008-05-10 10:32:32 +02:00
|
|
|
|
|
|
|
~GlobalVariable() {
|
|
|
|
NumOperands = 1; // FIXME: needed by operator delete
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide fast operand accessors
|
|
|
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
|
|
|
|
2010-10-19 18:47:15 +02:00
|
|
|
/// isDeclaration - Is this global variable lacking an initializer? If so,
|
2007-01-30 21:08:39 +01:00
|
|
|
/// the global variable is defined in some other translation unit, and is thus
|
|
|
|
/// only a declaration here.
|
|
|
|
virtual bool isDeclaration() const { return getNumOperands() == 0; }
|
2002-10-07 00:29:58 +02:00
|
|
|
|
|
|
|
/// hasInitializer - Unless a global variable isExternal(), it has an
|
|
|
|
/// initializer. The initializer for the global variable/constant is held by
|
2005-01-29 01:33:00 +01:00
|
|
|
/// Initializer if an initializer is specified.
|
2002-10-07 00:29:58 +02:00
|
|
|
///
|
2007-01-30 21:08:39 +01:00
|
|
|
inline bool hasInitializer() const { return !isDeclaration(); }
|
2002-10-07 00:29:58 +02:00
|
|
|
|
2009-03-21 22:27:31 +01:00
|
|
|
/// hasDefinitiveInitializer - Whether the global variable has an initializer,
|
2010-10-19 18:47:23 +02:00
|
|
|
/// 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.
|
2009-03-21 22:27:31 +01:00
|
|
|
inline bool hasDefinitiveInitializer() const {
|
|
|
|
return hasInitializer() &&
|
|
|
|
// The initializer of a global variable with weak linkage may change at
|
|
|
|
// link time.
|
|
|
|
!mayBeOverridden();
|
|
|
|
}
|
|
|
|
|
2010-10-19 18:47:23 +02:00
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
|
2002-10-07 00:29:58 +02:00
|
|
|
/// 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!
|
|
|
|
///
|
2008-05-10 10:32:32 +02:00
|
|
|
inline /*const FIXME*/ Constant *getInitializer() const {
|
2001-10-13 08:12:38 +02:00
|
|
|
assert(hasInitializer() && "GV doesn't have initializer!");
|
2008-05-10 10:32:32 +02:00
|
|
|
return static_cast<Constant*>(Op<0>().get());
|
2001-09-18 06:01:05 +02:00
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
inline Constant *getInitializer() {
|
2001-10-13 08:12:38 +02:00
|
|
|
assert(hasInitializer() && "GV doesn't have initializer!");
|
2008-05-10 10:32:32 +02:00
|
|
|
return static_cast<Constant*>(Op<0>().get());
|
2001-09-18 06:01:05 +02:00
|
|
|
}
|
2009-11-17 01:43:13 +01:00
|
|
|
/// 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);
|
2001-09-18 06:01:05 +02:00
|
|
|
|
2002-10-07 00:29:58 +02:00
|
|
|
/// If the value is a global constant, its value is immutable throughout the
|
|
|
|
/// runtime execution of the program. Assigning a value into the constant
|
|
|
|
/// leads to undefined behavior.
|
|
|
|
///
|
2003-02-02 17:40:40 +01:00
|
|
|
bool isConstant() const { return isConstantGlobal; }
|
2008-05-19 22:15:12 +02:00
|
|
|
void setConstant(bool Val) { isConstantGlobal = Val; }
|
2004-10-12 00:21:13 +02:00
|
|
|
|
2007-04-12 20:32:50 +02:00
|
|
|
/// If the value is "Thread Local", its value isn't shared by the threads.
|
|
|
|
bool isThreadLocal() const { return isThreadLocalSymbol; }
|
2008-05-19 22:15:12 +02:00
|
|
|
void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
|
2007-04-12 20:32:50 +02:00
|
|
|
|
2008-05-26 21:58:59 +02:00
|
|
|
/// copyAttributesFrom - copy all additional attributes (those not needed to
|
|
|
|
/// create a GlobalVariable) from the GlobalVariable Src to this one.
|
|
|
|
void copyAttributesFrom(const GlobalValue *Src);
|
|
|
|
|
2004-10-12 00:21:13 +02:00
|
|
|
/// removeFromParent - This method unlinks 'this' from the containing module,
|
|
|
|
/// but does not delete it.
|
|
|
|
///
|
2008-08-29 09:30:15 +02:00
|
|
|
virtual void removeFromParent();
|
2004-10-12 00:21:13 +02:00
|
|
|
|
|
|
|
/// eraseFromParent - This method unlinks 'this' from the containing module
|
|
|
|
/// and deletes it.
|
|
|
|
///
|
2008-08-29 09:30:15 +02:00
|
|
|
virtual void eraseFromParent();
|
2005-04-21 22:19:05 +02:00
|
|
|
|
|
|
|
/// Override Constant's implementation of this method so we can
|
2004-07-18 01:29:46 +02:00
|
|
|
/// replace constant initializers.
|
2005-10-04 20:12:13 +02:00
|
|
|
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
|
2005-04-21 22:19:05 +02:00
|
|
|
|
2001-10-01 20:26:53 +02:00
|
|
|
// Methods for support type inquiry through isa, cast, and dyn_cast:
|
2001-10-02 05:41:24 +02:00
|
|
|
static inline bool classof(const GlobalVariable *) { return true; }
|
|
|
|
static inline bool classof(const Value *V) {
|
2007-04-13 20:12:09 +02:00
|
|
|
return V->getValueID() == Value::GlobalVariableVal;
|
2001-10-01 20:26:53 +02:00
|
|
|
}
|
2001-09-10 09:58:01 +02:00
|
|
|
};
|
|
|
|
|
2008-05-10 10:32:32 +02:00
|
|
|
template <>
|
2011-01-11 16:07:38 +01:00
|
|
|
struct OperandTraits<GlobalVariable> :
|
|
|
|
public OptionalOperandTraits<GlobalVariable> {
|
2008-05-10 10:32:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
#endif
|