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