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/Module.h"
22 #include "llvm/IR/Operator.h"
23 #include "llvm/Support/ErrorHandling.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 bool GlobalValue::isMaterializable() const {
31 if (const Function *F = dyn_cast<Function>(this))
32 return F->isMaterializable();
35 bool GlobalValue::isDematerializable() const {
36 return getParent() && getParent()->isDematerializable(this);
38 std::error_code GlobalValue::materialize() {
39 return getParent()->materialize(this);
41 void GlobalValue::Dematerialize() {
42 getParent()->Dematerialize(this);
45 /// Override destroyConstant to make sure it doesn't get called on
46 /// GlobalValue's because they shouldn't be treated like other constants.
47 void GlobalValue::destroyConstant() {
48 llvm_unreachable("You can't GV->destroyConstant()!");
51 /// copyAttributesFrom - copy all additional attributes (those not needed to
52 /// create a GlobalValue) from the GlobalValue Src to this one.
53 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
54 setVisibility(Src->getVisibility());
55 setUnnamedAddr(Src->hasUnnamedAddr());
56 setDLLStorageClass(Src->getDLLStorageClass());
59 unsigned GlobalValue::getAlignment() const {
60 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
61 // In general we cannot compute this at the IR level, but we try.
62 if (const GlobalObject *GO = GA->getBaseObject())
63 return GO->getAlignment();
65 // FIXME: we should also be able to handle:
66 // Alias = Global + Offset
70 return cast<GlobalObject>(this)->getAlignment();
73 void GlobalObject::setAlignment(unsigned Align) {
74 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
75 assert(Align <= MaximumAlignment &&
76 "Alignment is greater than MaximumAlignment!");
77 unsigned AlignmentData = Log2_32(Align) + 1;
78 unsigned OldData = getGlobalValueSubClassData();
79 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
80 assert(getAlignment() == Align && "Alignment representation error!");
83 unsigned GlobalObject::getGlobalObjectSubClassData() const {
84 unsigned ValueData = getGlobalValueSubClassData();
85 return ValueData >> AlignmentBits;
88 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
89 unsigned OldData = getGlobalValueSubClassData();
90 setGlobalValueSubClassData((OldData & AlignmentMask) |
91 (Val << AlignmentBits));
92 assert(getGlobalObjectSubClassData() == Val && "representation error");
95 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
96 const auto *GV = cast<GlobalObject>(Src);
97 GlobalValue::copyAttributesFrom(GV);
98 setAlignment(GV->getAlignment());
99 setSection(GV->getSection());
102 const char *GlobalValue::getSection() const {
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 GO->getSection();
109 return cast<GlobalObject>(this)->getSection();
112 Comdat *GlobalValue::getComdat() {
113 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
114 // In general we cannot compute this at the IR level, but we try.
115 if (const GlobalObject *GO = GA->getBaseObject())
116 return const_cast<GlobalObject *>(GO)->getComdat();
119 return cast<GlobalObject>(this)->getComdat();
122 void GlobalObject::setSection(StringRef S) { Section = S; }
124 bool GlobalValue::isDeclaration() const {
125 // Globals are definitions if they have an initializer.
126 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
127 return GV->getNumOperands() == 0;
129 // Functions are definitions if they have a body.
130 if (const Function *F = dyn_cast<Function>(this))
131 return F->empty() && !F->isMaterializable();
133 // Aliases are always definitions.
134 assert(isa<GlobalAlias>(this));
138 //===----------------------------------------------------------------------===//
139 // GlobalVariable Implementation
140 //===----------------------------------------------------------------------===//
142 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
143 Constant *InitVal, const Twine &Name,
144 ThreadLocalMode TLMode, unsigned AddressSpace,
145 bool isExternallyInitialized)
146 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
147 OperandTraits<GlobalVariable>::op_begin(this),
148 InitVal != nullptr, Link, Name),
149 isConstantGlobal(constant),
150 isExternallyInitializedConstant(isExternallyInitialized) {
151 setThreadLocalMode(TLMode);
153 assert(InitVal->getType() == Ty &&
154 "Initializer should be the same type as the GlobalVariable!");
159 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
160 LinkageTypes Link, Constant *InitVal,
161 const Twine &Name, GlobalVariable *Before,
162 ThreadLocalMode TLMode, unsigned AddressSpace,
163 bool isExternallyInitialized)
164 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
165 OperandTraits<GlobalVariable>::op_begin(this),
166 InitVal != nullptr, Link, Name),
167 isConstantGlobal(constant),
168 isExternallyInitializedConstant(isExternallyInitialized) {
169 setThreadLocalMode(TLMode);
171 assert(InitVal->getType() == Ty &&
172 "Initializer should be the same type as the GlobalVariable!");
177 Before->getParent()->getGlobalList().insert(Before, this);
179 M.getGlobalList().push_back(this);
182 void GlobalVariable::setParent(Module *parent) {
186 void GlobalVariable::removeFromParent() {
187 getParent()->getGlobalList().remove(this);
190 void GlobalVariable::eraseFromParent() {
191 getParent()->getGlobalList().erase(this);
194 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
196 // If you call this, then you better know this GVar has a constant
197 // initializer worth replacing. Enforce that here.
198 assert(getNumOperands() == 1 &&
199 "Attempt to replace uses of Constants on a GVar with no initializer");
201 // And, since you know it has an initializer, the From value better be
202 // the initializer :)
203 assert(getOperand(0) == From &&
204 "Attempt to replace wrong constant initializer in GVar");
206 // And, you better have a constant for the replacement value
207 assert(isa<Constant>(To) &&
208 "Attempt to replace GVar initializer with non-constant");
210 // Okay, preconditions out of the way, replace the constant initializer.
211 this->setOperand(0, cast<Constant>(To));
214 void GlobalVariable::setInitializer(Constant *InitVal) {
216 if (hasInitializer()) {
217 Op<0>().set(nullptr);
221 assert(InitVal->getType() == getType()->getElementType() &&
222 "Initializer type must match GlobalVariable type");
223 if (!hasInitializer())
225 Op<0>().set(InitVal);
229 /// copyAttributesFrom - copy all additional attributes (those not needed to
230 /// create a GlobalVariable) from the GlobalVariable Src to this one.
231 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
232 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
233 GlobalObject::copyAttributesFrom(Src);
234 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
235 setThreadLocalMode(SrcVar->getThreadLocalMode());
236 setExternallyInitialized(SrcVar->isExternallyInitialized());
240 //===----------------------------------------------------------------------===//
241 // GlobalAlias Implementation
242 //===----------------------------------------------------------------------===//
244 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
245 const Twine &Name, Constant *Aliasee,
246 Module *ParentModule)
247 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
248 &Op<0>(), 1, Link, Name) {
252 ParentModule->getAliasList().push_back(this);
255 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
256 LinkageTypes Link, const Twine &Name,
257 Constant *Aliasee, Module *ParentModule) {
258 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
261 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
262 LinkageTypes Linkage, const Twine &Name,
264 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
267 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
268 LinkageTypes Linkage, const Twine &Name,
269 GlobalValue *Aliasee) {
270 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
273 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
274 GlobalValue *Aliasee) {
275 PointerType *PTy = Aliasee->getType();
276 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
280 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
281 return create(Aliasee->getLinkage(), Name, Aliasee);
284 void GlobalAlias::setParent(Module *parent) {
288 void GlobalAlias::removeFromParent() {
289 getParent()->getAliasList().remove(this);
292 void GlobalAlias::eraseFromParent() {
293 getParent()->getAliasList().erase(this);
296 void GlobalAlias::setAliasee(Constant *Aliasee) {
297 assert((!Aliasee || Aliasee->getType() == getType()) &&
298 "Alias and aliasee types should match!");
299 setOperand(0, Aliasee);