Globals.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the GlobalValue & GlobalVariable classes for the IR
  11. // library.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/GlobalValue.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/GlobalAlias.h"
  19. #include "llvm/IR/GlobalVariable.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/Operator.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. using namespace llvm;
  24. //===----------------------------------------------------------------------===//
  25. // GlobalValue Class
  26. //===----------------------------------------------------------------------===//
  27. bool GlobalValue::isMaterializable() const {
  28. if (const Function *F = dyn_cast<Function>(this))
  29. return F->isMaterializable();
  30. return false;
  31. }
  32. bool GlobalValue::isDematerializable() const {
  33. return getParent() && getParent()->isDematerializable(this);
  34. }
  35. std::error_code GlobalValue::materialize() {
  36. return getParent()->materialize(this);
  37. }
  38. void GlobalValue::dematerialize() {
  39. getParent()->dematerialize(this);
  40. }
  41. /// Override destroyConstantImpl to make sure it doesn't get called on
  42. /// GlobalValue's because they shouldn't be treated like other constants.
  43. void GlobalValue::destroyConstantImpl() {
  44. llvm_unreachable("You can't GV->destroyConstantImpl()!");
  45. }
  46. Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) {
  47. llvm_unreachable("Unsupported class for handleOperandChange()!");
  48. }
  49. /// copyAttributesFrom - copy all additional attributes (those not needed to
  50. /// create a GlobalValue) from the GlobalValue Src to this one.
  51. void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
  52. setVisibility(Src->getVisibility());
  53. setUnnamedAddr(Src->hasUnnamedAddr());
  54. setDLLStorageClass(Src->getDLLStorageClass());
  55. }
  56. unsigned GlobalValue::getAlignment() const {
  57. if (auto *GA = dyn_cast<GlobalAlias>(this)) {
  58. // In general we cannot compute this at the IR level, but we try.
  59. if (const GlobalObject *GO = GA->getBaseObject())
  60. return GO->getAlignment();
  61. // FIXME: we should also be able to handle:
  62. // Alias = Global + Offset
  63. // Alias = Absolute
  64. return 0;
  65. }
  66. return cast<GlobalObject>(this)->getAlignment();
  67. }
  68. void GlobalObject::setAlignment(unsigned Align) {
  69. assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
  70. assert(Align <= MaximumAlignment &&
  71. "Alignment is greater than MaximumAlignment!");
  72. unsigned AlignmentData = Log2_32(Align) + 1;
  73. unsigned OldData = getGlobalValueSubClassData();
  74. setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
  75. assert(getAlignment() == Align && "Alignment representation error!");
  76. }
  77. unsigned GlobalObject::getGlobalObjectSubClassData() const {
  78. unsigned ValueData = getGlobalValueSubClassData();
  79. return ValueData >> AlignmentBits;
  80. }
  81. void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
  82. unsigned OldData = getGlobalValueSubClassData();
  83. setGlobalValueSubClassData((OldData & AlignmentMask) |
  84. (Val << AlignmentBits));
  85. assert(getGlobalObjectSubClassData() == Val && "representation error");
  86. }
  87. void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
  88. const auto *GV = cast<GlobalObject>(Src);
  89. GlobalValue::copyAttributesFrom(GV);
  90. setAlignment(GV->getAlignment());
  91. setSection(GV->getSection());
  92. }
  93. const char *GlobalValue::getSection() const {
  94. if (auto *GA = dyn_cast<GlobalAlias>(this)) {
  95. // In general we cannot compute this at the IR level, but we try.
  96. if (const GlobalObject *GO = GA->getBaseObject())
  97. return GO->getSection();
  98. return "";
  99. }
  100. return cast<GlobalObject>(this)->getSection();
  101. }
  102. Comdat *GlobalValue::getComdat() {
  103. if (auto *GA = dyn_cast<GlobalAlias>(this)) {
  104. // In general we cannot compute this at the IR level, but we try.
  105. if (const GlobalObject *GO = GA->getBaseObject())
  106. return const_cast<GlobalObject *>(GO)->getComdat();
  107. return nullptr;
  108. }
  109. return cast<GlobalObject>(this)->getComdat();
  110. }
  111. void GlobalObject::setSection(StringRef S) { Section = S; }
  112. bool GlobalValue::isDeclaration() const {
  113. // Globals are definitions if they have an initializer.
  114. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
  115. return GV->getNumOperands() == 0;
  116. // Functions are definitions if they have a body.
  117. if (const Function *F = dyn_cast<Function>(this))
  118. return F->empty() && !F->isMaterializable();
  119. // Aliases are always definitions.
  120. assert(isa<GlobalAlias>(this));
  121. return false;
  122. }
  123. //===----------------------------------------------------------------------===//
  124. // GlobalVariable Implementation
  125. //===----------------------------------------------------------------------===//
  126. GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
  127. Constant *InitVal, const Twine &Name,
  128. ThreadLocalMode TLMode, unsigned AddressSpace,
  129. bool isExternallyInitialized)
  130. : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
  131. OperandTraits<GlobalVariable>::op_begin(this),
  132. InitVal != nullptr, Link, Name),
  133. isConstantGlobal(constant),
  134. isExternallyInitializedConstant(isExternallyInitialized) {
  135. setThreadLocalMode(TLMode);
  136. if (InitVal) {
  137. assert(InitVal->getType() == Ty &&
  138. "Initializer should be the same type as the GlobalVariable!");
  139. Op<0>() = InitVal;
  140. }
  141. }
  142. GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
  143. LinkageTypes Link, Constant *InitVal,
  144. const Twine &Name, GlobalVariable *Before,
  145. ThreadLocalMode TLMode, unsigned AddressSpace,
  146. bool isExternallyInitialized)
  147. : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
  148. OperandTraits<GlobalVariable>::op_begin(this),
  149. InitVal != nullptr, Link, Name),
  150. isConstantGlobal(constant),
  151. isExternallyInitializedConstant(isExternallyInitialized) {
  152. setThreadLocalMode(TLMode);
  153. if (InitVal) {
  154. assert(InitVal->getType() == Ty &&
  155. "Initializer should be the same type as the GlobalVariable!");
  156. Op<0>() = InitVal;
  157. }
  158. if (Before)
  159. Before->getParent()->getGlobalList().insert(Before, this);
  160. else
  161. M.getGlobalList().push_back(this);
  162. }
  163. void GlobalVariable::setParent(Module *parent) {
  164. Parent = parent;
  165. }
  166. void GlobalVariable::removeFromParent() {
  167. getParent()->CallRemoveGlobalHook(this); // HLSL Change
  168. getParent()->getGlobalList().remove(this);
  169. }
  170. void GlobalVariable::eraseFromParent() {
  171. getParent()->CallRemoveGlobalHook(this); // HLSL Change
  172. getParent()->getGlobalList().erase(this);
  173. }
  174. void GlobalVariable::setInitializer(Constant *InitVal) {
  175. if (!InitVal) {
  176. if (hasInitializer()) {
  177. // Note, the num operands is used to compute the offset of the operand, so
  178. // the order here matters. Clearing the operand then clearing the num
  179. // operands ensures we have the correct offset to the operand.
  180. Op<0>().set(nullptr);
  181. setGlobalVariableNumOperands(0);
  182. }
  183. } else {
  184. assert(InitVal->getType() == getType()->getElementType() &&
  185. "Initializer type must match GlobalVariable type");
  186. // Note, the num operands is used to compute the offset of the operand, so
  187. // the order here matters. We need to set num operands to 1 first so that
  188. // we get the correct offset to the first operand when we set it.
  189. if (!hasInitializer())
  190. setGlobalVariableNumOperands(1);
  191. Op<0>().set(InitVal);
  192. }
  193. }
  194. /// copyAttributesFrom - copy all additional attributes (those not needed to
  195. /// create a GlobalVariable) from the GlobalVariable Src to this one.
  196. void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
  197. assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
  198. GlobalObject::copyAttributesFrom(Src);
  199. const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
  200. setThreadLocalMode(SrcVar->getThreadLocalMode());
  201. setExternallyInitialized(SrcVar->isExternallyInitialized());
  202. }
  203. //===----------------------------------------------------------------------===//
  204. // GlobalAlias Implementation
  205. //===----------------------------------------------------------------------===//
  206. GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
  207. Constant *Aliasee, Module *ParentModule)
  208. : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
  209. Op<0>() = Aliasee;
  210. if (ParentModule)
  211. ParentModule->getAliasList().push_back(this);
  212. }
  213. GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
  214. const Twine &Name, Constant *Aliasee,
  215. Module *ParentModule) {
  216. return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
  217. }
  218. GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
  219. const Twine &Name, Module *Parent) {
  220. return create(Ty, Linkage, Name, nullptr, Parent);
  221. }
  222. GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
  223. const Twine &Name, GlobalValue *Aliasee) {
  224. return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
  225. }
  226. GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
  227. GlobalValue *Aliasee) {
  228. PointerType *PTy = Aliasee->getType();
  229. return create(PTy, Link, Name, Aliasee);
  230. }
  231. GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
  232. return create(Aliasee->getLinkage(), Name, Aliasee);
  233. }
  234. void GlobalAlias::setParent(Module *parent) {
  235. Parent = parent;
  236. }
  237. void GlobalAlias::removeFromParent() {
  238. getParent()->getAliasList().remove(this);
  239. }
  240. void GlobalAlias::eraseFromParent() {
  241. getParent()->getAliasList().erase(this);
  242. }
  243. void GlobalAlias::setAliasee(Constant *Aliasee) {
  244. assert((!Aliasee || Aliasee->getType() == getType()) &&
  245. "Alias and aliasee types should match!");
  246. setOperand(0, Aliasee);
  247. }