1683ade38a996fb869a6dd57e3168575acee1c167Chris Lattner//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===// 29769ab22265b313171d201b5928688524a01bd87Misha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 79769ab22265b313171d201b5928688524a01bd87Misha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 970cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner// 1070cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner// This file contains the declaration of the GlobalVariable class, which 11d70684f7585a85c4248c1c224059478108741c70Chris Lattner// represents a single global variable (or constant) in the VM. 1270cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner// 1370cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner// Global variables are constant pointers that refer to hunks of space that are 14d70684f7585a85c4248c1c224059478108741c70Chris Lattner// allocated by either the VM, or by the linker in a static compiler. A global 157a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner// variable may have an initial value, which is copied into the executables .data 16d70684f7585a85c4248c1c224059478108741c70Chris Lattner// area. Global Constants are required to have initializers. 1770cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner// 1870cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner//===----------------------------------------------------------------------===// 1970cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner 20674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_GLOBALVARIABLE_H 21674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_GLOBALVARIABLE_H 2270cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner 23255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/Twine.h" 24255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/ilist_node.h" 25dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/IR/GlobalObject.h" 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/OperandTraits.h" 277a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 28d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 29d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 3070cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattnerclass Module; 31e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass Constant; 3217fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattnertemplate<typename ValueSubClass, typename ItemParentClass> 3317fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner class SymbolTableListTraits; 3470cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner 35dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> { 3617fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner friend class SymbolTableListTraits<GlobalVariable, Module>; 37c2945e44d5d485ce080a09900fbd5245e01165fcCraig Topper void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 38c2945e44d5d485ce080a09900fbd5245e01165fcCraig Topper void operator=(const GlobalVariable &) LLVM_DELETED_FUNCTION; 39c2945e44d5d485ce080a09900fbd5245e01165fcCraig Topper GlobalVariable(const GlobalVariable &) LLVM_DELETED_FUNCTION; 40c5f24a2c2a16681f7816ac053c8f46a2692a3b7aJeff Cohen 41fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner void setParent(Module *parent); 4270cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner 43af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman bool isConstantGlobal : 1; // Is this a global constant? 44af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman bool isExternallyInitializedConstant : 1; // Is this a global whose value 45af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman // can change from its initial 46af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman // value before global 47af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman // initializers are run? 485181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner 4970cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattnerpublic: 50efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif // allocate space for exactly one operand 51051a950000e21935165db56695e35bade668193bGabor Greif void *operator new(size_t s) { 52efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif return User::operator new(s, 1); 53051a950000e21935165db56695e35bade668193bGabor Greif } 54ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg 55ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg /// GlobalVariable ctor - If a parent module is specified, the global is 56ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg /// automatically inserted into the end of the specified modules global list. 57ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage, 58dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Constant *Initializer = nullptr, const Twine &Name = "", 59af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, 60af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman bool isExternallyInitialized = false); 61adc95467e41d6c46793f665168468cbf348c3638Chris Lattner /// GlobalVariable ctor - This creates a global and inserts it before the 62adc95467e41d6c46793f665168468cbf348c3638Chris Lattner /// specified other global. 63db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner GlobalVariable(Module &M, Type *Ty, bool isConstant, 64e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson LinkageTypes Linkage, Constant *Initializer, 65dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const Twine &Name = "", GlobalVariable *InsertBefore = nullptr, 66af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, 67af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman bool isExternallyInitialized = false); 68efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif 69efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif ~GlobalVariable() { 70efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif NumOperands = 1; // FIXME: needed by operator delete 71efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif } 72efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif 73efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif /// Provide fast operand accessors 74efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 75efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif 761313a223ed2e8d41b6a13d3738123042b5573b88Rafael Espindola /// Definitions have initializers, declarations don't. 77ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// 785cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer inline bool hasInitializer() const { return !isDeclaration(); } 79ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner 8064da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands /// hasDefinitiveInitializer - Whether the global variable has an initializer, 8199fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// and any other instances of the global (this can happen due to weak 8299fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// linkage) are guaranteed to have the same initializer. 8399fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// 8499fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// Note that if you want to transform a global, you must use 8599fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// hasUniqueInitializer() instead, because of the *_odr linkage type. 8699fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// 8799fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// Example: 8899fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// 8999fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// @a = global SomeType* null - Initializer is both definitive and unique. 9099fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// 9199fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// @b = global weak SomeType* null - Initializer is neither definitive nor 9299fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// unique. 9399fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// 9499fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// @c = global weak_odr SomeType* null - Initializer is definitive, but not 9599fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// unique. 9664da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands inline bool hasDefinitiveInitializer() const { 9764da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands return hasInitializer() && 9864da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands // The initializer of a global variable with weak linkage may change at 9964da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands // link time. 100e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman !mayBeOverridden() && 101e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman // The initializer of a global variable with the externally_initialized 102ca531fd4869a17b7a6b16008b9b85741aa6b61e8Michael Gottesman // marker may change at runtime before C++ initializers are evaluated. 103e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman !isExternallyInitialized(); 10464da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands } 10564da9409cb27f43825a8ab0a3b49c2a73cae36baDuncan Sands 10699fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// hasUniqueInitializer - Whether the global variable has an initializer, and 10799fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov /// any changes made to the initializer will turn up in the final executable. 10899fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov inline bool hasUniqueInitializer() const { 10999fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov return hasInitializer() && 11099fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov // It's not safe to modify initializers of global variables with weak 11199fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov // linkage, because the linker might choose to discard the initializer and 11299fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov // use the initializer from another instance of the global variable 11399fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov // instead. It is wrong to modify the initializer of a global variable 11499fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov // with *_odr linkage because then different instances of the global may 11599fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov // have different initializers, breaking the One Definition Rule. 116e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman !isWeakForLinker() && 117e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman // It is not safe to modify initializers of global variables with the 118e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman // external_initializer marker since the value may be changed at runtime 119ca531fd4869a17b7a6b16008b9b85741aa6b61e8Michael Gottesman // before C++ initializers are evaluated. 120e1b6b5290373073c95f6865ceaf76fa7848ecf44Michael Gottesman !isExternallyInitialized(); 12199fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov } 12299fca5de96d3435e8eb7c84e8366cee98ef5416aMikhail Glushenkov 123ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// getInitializer - Return the initializer for this global variable. It is 124ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// illegal to call this method if the global is external, because we cannot 125ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// tell what the value is initialized to! 126ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// 1277d715dfe6d66be257926f626df96a0e2bd38dc1fJay Foad inline const Constant *getInitializer() const { 12816ddd025451aff4573adad3e1a1fc2a5f9aa59a3Chris Lattner assert(hasInitializer() && "GV doesn't have initializer!"); 129efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif return static_cast<Constant*>(Op<0>().get()); 130d70684f7585a85c4248c1c224059478108741c70Chris Lattner } 131e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner inline Constant *getInitializer() { 13216ddd025451aff4573adad3e1a1fc2a5f9aa59a3Chris Lattner assert(hasInitializer() && "GV doesn't have initializer!"); 133efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif return static_cast<Constant*>(Op<0>().get()); 134d70684f7585a85c4248c1c224059478108741c70Chris Lattner } 135dc1472b6d9281893771a64eb95b4599dff9aadfdJeffrey Yasskin /// setInitializer - Sets the initializer for this global variable, removing 136dc1472b6d9281893771a64eb95b4599dff9aadfdJeffrey Yasskin /// any existing initializer if InitVal==NULL. If this GV has type T*, the 137dc1472b6d9281893771a64eb95b4599dff9aadfdJeffrey Yasskin /// initializer must have type T. 138dc1472b6d9281893771a64eb95b4599dff9aadfdJeffrey Yasskin void setInitializer(Constant *InitVal); 139d70684f7585a85c4248c1c224059478108741c70Chris Lattner 140ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// If the value is a global constant, its value is immutable throughout the 141ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// runtime execution of the program. Assigning a value into the constant 142ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// leads to undefined behavior. 143ac8a58d50067bd1e1fc009cdb526d872b4cd5155Chris Lattner /// 1448bac08bf047d54acac33b656ee4a0d244770f6d3Chris Lattner bool isConstant() const { return isConstantGlobal; } 14513d57320bd212483463d4f8992d5787b29eda5dfBill Wendling void setConstant(bool Val) { isConstantGlobal = Val; } 146b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner 147af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman bool isExternallyInitialized() const { 148af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman return isExternallyInitializedConstant; 149af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman } 150af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman void setExternallyInitialized(bool Val) { 151af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman isExternallyInitializedConstant = Val; 152af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman } 153af2f494c2d87a77c46efb85856f235ddd7f3c6abMichael Gottesman 15428c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands /// copyAttributesFrom - copy all additional attributes (those not needed to 15528c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands /// create a GlobalVariable) from the GlobalVariable Src to this one. 15636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void copyAttributesFrom(const GlobalValue *Src) override; 15728c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands 158b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner /// removeFromParent - This method unlinks 'this' from the containing module, 159b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner /// but does not delete it. 160b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner /// 16136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void removeFromParent() override; 162b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner 163b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner /// eraseFromParent - This method unlinks 'this' from the containing module 164b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner /// and deletes it. 165b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner /// 16636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void eraseFromParent() override; 1679769ab22265b313171d201b5928688524a01bd87Misha Brukman 1689769ab22265b313171d201b5928688524a01bd87Misha Brukman /// Override Constant's implementation of this method so we can 169efd5dd80c9f6e32eae10d5977e8120a23ee017f8Reid Spencer /// replace constant initializers. 17036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override; 1719769ab22265b313171d201b5928688524a01bd87Misha Brukman 172cfe26c930ae691ff3012736555846c45087e1a9eChris Lattner // Methods for support type inquiry through isa, cast, and dyn_cast: 173b00c582b6d40e6b9ff2d1ed4f5eaf7930e792aceChris Lattner static inline bool classof(const Value *V) { 174a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman return V->getValueID() == Value::GlobalVariableVal; 175cfe26c930ae691ff3012736555846c45087e1a9eChris Lattner } 17670cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner}; 17770cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner 178efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <> 17967c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foadstruct OperandTraits<GlobalVariable> : 18067c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foad public OptionalOperandTraits<GlobalVariable> { 181efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif}; 182efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif 183efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) 184efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif 185d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 186d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 18770cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner#endif 188