1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GlobalValue & GlobalVariable classes for the IR
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/GlobalValue.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/GlobalAlias.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/LeakDetector.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/ErrorHandling.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 bool GlobalValue::isMaterializable() const {
31 return getParent() && getParent()->isMaterializable(this);
33 bool GlobalValue::isDematerializable() const {
34 return getParent() && getParent()->isDematerializable(this);
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37 return getParent()->Materialize(this, ErrInfo);
39 void GlobalValue::Dematerialize() {
40 getParent()->Dematerialize(this);
43 const DataLayout *GlobalValue::getDataLayout() const {
44 return getParent()->getDataLayout();
47 /// Override destroyConstant to make sure it doesn't get called on
48 /// GlobalValue's because they shouldn't be treated like other constants.
49 void GlobalValue::destroyConstant() {
50 llvm_unreachable("You can't GV->destroyConstant()!");
53 /// copyAttributesFrom - copy all additional attributes (those not needed to
54 /// create a GlobalValue) from the GlobalValue Src to this one.
55 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
56 setVisibility(Src->getVisibility());
57 setUnnamedAddr(Src->hasUnnamedAddr());
58 setDLLStorageClass(Src->getDLLStorageClass());
61 unsigned GlobalValue::getAlignment() const {
62 if (auto *GA = dyn_cast<GlobalAlias>(this))
63 return GA->getAliasee()->getAlignment();
65 return cast<GlobalObject>(this)->getAlignment();
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 setGlobalValueSubClassData(Log2_32(Align) + 1);
73 assert(getAlignment() == Align && "Alignment representation error!");
76 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
77 const auto *GV = cast<GlobalObject>(Src);
78 GlobalValue::copyAttributesFrom(GV);
79 setAlignment(GV->getAlignment());
80 setSection(GV->getSection());
83 const std::string &GlobalValue::getSection() const {
84 if (auto *GA = dyn_cast<GlobalAlias>(this))
85 return GA->getAliasee()->getSection();
86 return cast<GlobalObject>(this)->getSection();
89 void GlobalObject::setSection(StringRef S) { Section = S; }
91 bool GlobalValue::isDeclaration() const {
92 // Globals are definitions if they have an initializer.
93 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
94 return GV->getNumOperands() == 0;
96 // Functions are definitions if they have a body.
97 if (const Function *F = dyn_cast<Function>(this))
100 // Aliases are always definitions.
101 assert(isa<GlobalAlias>(this));
105 //===----------------------------------------------------------------------===//
106 // GlobalVariable Implementation
107 //===----------------------------------------------------------------------===//
109 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
110 Constant *InitVal, const Twine &Name,
111 ThreadLocalMode TLMode, unsigned AddressSpace,
112 bool isExternallyInitialized)
113 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
114 OperandTraits<GlobalVariable>::op_begin(this),
115 InitVal != nullptr, Link, Name),
116 isConstantGlobal(constant), threadLocalMode(TLMode),
117 isExternallyInitializedConstant(isExternallyInitialized) {
119 assert(InitVal->getType() == Ty &&
120 "Initializer should be the same type as the GlobalVariable!");
124 LeakDetector::addGarbageObject(this);
127 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
128 LinkageTypes Link, Constant *InitVal,
129 const Twine &Name, GlobalVariable *Before,
130 ThreadLocalMode TLMode, unsigned AddressSpace,
131 bool isExternallyInitialized)
132 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
133 OperandTraits<GlobalVariable>::op_begin(this),
134 InitVal != nullptr, Link, Name),
135 isConstantGlobal(constant), threadLocalMode(TLMode),
136 isExternallyInitializedConstant(isExternallyInitialized) {
138 assert(InitVal->getType() == Ty &&
139 "Initializer should be the same type as the GlobalVariable!");
143 LeakDetector::addGarbageObject(this);
146 Before->getParent()->getGlobalList().insert(Before, this);
148 M.getGlobalList().push_back(this);
151 void GlobalVariable::setParent(Module *parent) {
153 LeakDetector::addGarbageObject(this);
156 LeakDetector::removeGarbageObject(this);
159 void GlobalVariable::removeFromParent() {
160 getParent()->getGlobalList().remove(this);
163 void GlobalVariable::eraseFromParent() {
164 getParent()->getGlobalList().erase(this);
167 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
169 // If you call this, then you better know this GVar has a constant
170 // initializer worth replacing. Enforce that here.
171 assert(getNumOperands() == 1 &&
172 "Attempt to replace uses of Constants on a GVar with no initializer");
174 // And, since you know it has an initializer, the From value better be
175 // the initializer :)
176 assert(getOperand(0) == From &&
177 "Attempt to replace wrong constant initializer in GVar");
179 // And, you better have a constant for the replacement value
180 assert(isa<Constant>(To) &&
181 "Attempt to replace GVar initializer with non-constant");
183 // Okay, preconditions out of the way, replace the constant initializer.
184 this->setOperand(0, cast<Constant>(To));
187 void GlobalVariable::setInitializer(Constant *InitVal) {
189 if (hasInitializer()) {
190 Op<0>().set(nullptr);
194 assert(InitVal->getType() == getType()->getElementType() &&
195 "Initializer type must match GlobalVariable type");
196 if (!hasInitializer())
198 Op<0>().set(InitVal);
202 /// copyAttributesFrom - copy all additional attributes (those not needed to
203 /// create a GlobalVariable) from the GlobalVariable Src to this one.
204 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
205 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
206 GlobalObject::copyAttributesFrom(Src);
207 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
208 setThreadLocalMode(SrcVar->getThreadLocalMode());
212 //===----------------------------------------------------------------------===//
213 // GlobalAlias Implementation
214 //===----------------------------------------------------------------------===//
216 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, const Twine &Name,
217 GlobalObject *Aliasee, Module *ParentModule,
218 unsigned AddressSpace)
219 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
220 &Op<0>(), 1, Link, Name) {
221 LeakDetector::addGarbageObject(this);
225 ParentModule->getAliasList().push_back(this);
228 void GlobalAlias::setParent(Module *parent) {
230 LeakDetector::addGarbageObject(this);
233 LeakDetector::removeGarbageObject(this);
236 void GlobalAlias::removeFromParent() {
237 getParent()->getAliasList().remove(this);
240 void GlobalAlias::eraseFromParent() {
241 getParent()->getAliasList().erase(this);
244 void GlobalAlias::setAliasee(GlobalObject *Aliasee) { setOperand(0, Aliasee); }