Globals.cpp 10 KB

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