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 // Note, the num operands is used to compute the offset of the operand, so
218 // the order here matters. Clearing the operand then clearing the num
219 // operands ensures we have the correct offset to the operand.
220 Op<0>().set(nullptr);
221 setGlobalVariableNumOperands(0);
224 assert(InitVal->getType() == getType()->getElementType() &&
225 "Initializer type must match GlobalVariable type");
226 // Note, the num operands is used to compute the offset of the operand, so
227 // the order here matters. We need to set num operands to 1 first so that
228 // we get the correct offset to the first operand when we set it.
229 if (!hasInitializer())
230 setGlobalVariableNumOperands(1);
231 Op<0>().set(InitVal);
235 /// copyAttributesFrom - copy all additional attributes (those not needed to
236 /// create a GlobalVariable) from the GlobalVariable Src to this one.
237 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
238 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
239 GlobalObject::copyAttributesFrom(Src);
240 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
241 setThreadLocalMode(SrcVar->getThreadLocalMode());
242 setExternallyInitialized(SrcVar->isExternallyInitialized());
246 //===----------------------------------------------------------------------===//
247 // GlobalAlias Implementation
248 //===----------------------------------------------------------------------===//
250 GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
251 Constant *Aliasee, Module *ParentModule)
252 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
256 ParentModule->getAliasList().push_back(this);
259 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
260 const Twine &Name, Constant *Aliasee,
261 Module *ParentModule) {
262 return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
265 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
266 const Twine &Name, Module *Parent) {
267 return create(Ty, Linkage, Name, nullptr, Parent);
270 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
271 const Twine &Name, GlobalValue *Aliasee) {
272 return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
275 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
276 GlobalValue *Aliasee) {
277 PointerType *PTy = Aliasee->getType();
278 return create(PTy, Link, Name, Aliasee);
281 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
282 return create(Aliasee->getLinkage(), Name, Aliasee);
285 void GlobalAlias::setParent(Module *parent) {
289 void GlobalAlias::removeFromParent() {
290 getParent()->getAliasList().remove(this);
293 void GlobalAlias::eraseFromParent() {
294 getParent()->getAliasList().erase(this);
297 void GlobalAlias::setAliasee(Constant *Aliasee) {
298 assert((!Aliasee || Aliasee->getType() == getType()) &&
299 "Alias and aliasee types should match!");
300 setOperand(0, Aliasee);