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
|
2011-04-15 07:18:47 +02:00
|
|
|
// variable may have an initial value, which is copied into the executables .data
|
2001-09-18 06:01:05 +02:00
|
|
|
// area. Global Constants are required to have initializers.
|
2001-09-10 09:58:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_IR_GLOBALVARIABLE_H
|
|
|
|
#define LLVM_IR_GLOBALVARIABLE_H
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2012-12-03 18:02:12 +01:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2014-05-13 20:45:48 +02:00
|
|
|
#include "llvm/IR/GlobalObject.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/OperandTraits.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
|
|
|
|
2014-05-13 20:45:48 +02:00
|
|
|
class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
|
2007-04-17 05:26:42 +02:00
|
|
|
friend class SymbolTableListTraits<GlobalVariable, Module>;
|
2015-02-15 23:54:22 +01:00
|
|
|
void *operator new(size_t, unsigned) = delete;
|
|
|
|
void operator=(const GlobalVariable &) = delete;
|
|
|
|
GlobalVariable(const GlobalVariable &) = delete;
|
2005-12-17 01:19:22 +01:00
|
|
|
|
2002-09-06 23:31:57 +02:00
|
|
|
void setParent(Module *parent);
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2013-02-03 22:54:38 +01:00
|
|
|
bool isConstantGlobal : 1; // Is this a global constant?
|
|
|
|
bool isExternallyInitializedConstant : 1; // Is this a global whose value
|
|
|
|
// can change from its initial
|
|
|
|
// value before global
|
|
|
|
// initializers are run?
|
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
|
|
|
}
|
2012-06-23 13:37:03 +02:00
|
|
|
|
|
|
|
/// GlobalVariable ctor - If a parent module is specified, the global is
|
|
|
|
/// automatically inserted into the end of the specified modules global list.
|
|
|
|
GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
|
2014-04-09 08:08:46 +02:00
|
|
|
Constant *Initializer = nullptr, const Twine &Name = "",
|
2013-02-03 22:54:38 +01:00
|
|
|
ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
|
|
|
|
bool isExternallyInitialized = false);
|
2006-09-30 23:31:26 +02:00
|
|
|
/// GlobalVariable ctor - This creates a global and inserts it before the
|
|
|
|
/// specified other global.
|
2011-07-18 06:54:35 +02:00
|
|
|
GlobalVariable(Module &M, Type *Ty, bool isConstant,
|
2009-07-08 21:03:57 +02:00
|
|
|
LinkageTypes Linkage, Constant *Initializer,
|
2014-04-09 08:08:46 +02:00
|
|
|
const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
|
2013-02-03 22:54:38 +01:00
|
|
|
ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
|
|
|
|
bool isExternallyInitialized = false);
|
2008-05-10 10:32:32 +02:00
|
|
|
|
2015-04-11 04:11:45 +02:00
|
|
|
~GlobalVariable() override {
|
2015-06-12 19:48:10 +02:00
|
|
|
// FIXME: needed by operator delete
|
|
|
|
setGlobalVariableNumOperands(1);
|
2008-05-10 10:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide fast operand accessors
|
|
|
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
|
|
|
|
2013-10-29 14:44:11 +01:00
|
|
|
/// Definitions have initializers, declarations don't.
|
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.
|
2013-02-05 07:53:26 +01:00
|
|
|
!mayBeOverridden() &&
|
|
|
|
// The initializer of a global variable with the externally_initialized
|
2013-02-05 09:01:22 +01:00
|
|
|
// marker may change at runtime before C++ initializers are evaluated.
|
2013-02-05 07:53:26 +01:00
|
|
|
!isExternallyInitialized();
|
2009-03-21 22:27:31 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2013-02-05 07:53:26 +01:00
|
|
|
!isWeakForLinker() &&
|
|
|
|
// It is not safe to modify initializers of global variables with the
|
|
|
|
// external_initializer marker since the value may be changed at runtime
|
2013-02-05 09:01:22 +01:00
|
|
|
// before C++ initializers are evaluated.
|
2013-02-05 07:53:26 +01:00
|
|
|
!isExternallyInitialized();
|
2010-10-19 18:47:23 +02:00
|
|
|
}
|
|
|
|
|
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!
|
|
|
|
///
|
2011-06-19 20:37:11 +02:00
|
|
|
inline const 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
|
|
|
|
2013-02-03 22:54:38 +01:00
|
|
|
bool isExternallyInitialized() const {
|
|
|
|
return isExternallyInitializedConstant;
|
|
|
|
}
|
|
|
|
void setExternallyInitialized(bool Val) {
|
|
|
|
isExternallyInitializedConstant = Val;
|
|
|
|
}
|
|
|
|
|
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.
|
2014-03-05 07:35:38 +01:00
|
|
|
void copyAttributesFrom(const GlobalValue *Src) override;
|
2008-05-26 21:58:59 +02:00
|
|
|
|
2004-10-12 00:21:13 +02:00
|
|
|
/// removeFromParent - This method unlinks 'this' from the containing module,
|
|
|
|
/// but does not delete it.
|
|
|
|
///
|
2014-03-05 07:35:38 +01:00
|
|
|
void removeFromParent() override;
|
2004-10-12 00:21:13 +02:00
|
|
|
|
|
|
|
/// eraseFromParent - This method unlinks 'this' from the containing module
|
|
|
|
/// and deletes it.
|
|
|
|
///
|
2014-03-05 07:35:38 +01:00
|
|
|
void eraseFromParent() override;
|
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 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)
|
|
|
|
|
2015-06-23 11:49:53 +02:00
|
|
|
} // End llvm namespace
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
#endif
|