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 const DataLayout *GlobalValue::getDataLayout() const {
46 return getParent()->getDataLayout();
49 /// Override destroyConstant to make sure it doesn't get called on
50 /// GlobalValue's because they shouldn't be treated like other constants.
51 void GlobalValue::destroyConstant() {
52 llvm_unreachable("You can't GV->destroyConstant()!");
55 /// copyAttributesFrom - copy all additional attributes (those not needed to
56 /// create a GlobalValue) from the GlobalValue Src to this one.
57 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
58 setVisibility(Src->getVisibility());
59 setUnnamedAddr(Src->hasUnnamedAddr());
60 setDLLStorageClass(Src->getDLLStorageClass());
63 unsigned GlobalValue::getAlignment() const {
64 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
65 // In general we cannot compute this at the IR level, but we try.
66 if (const GlobalObject *GO = GA->getBaseObject())
67 return GO->getAlignment();
69 // FIXME: we should also be able to handle:
70 // Alias = Global + Offset
74 return cast<GlobalObject>(this)->getAlignment();
77 void GlobalObject::setAlignment(unsigned Align) {
78 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
79 assert(Align <= MaximumAlignment &&
80 "Alignment is greater than MaximumAlignment!");
81 unsigned AlignmentData = Log2_32(Align) + 1;
82 unsigned OldData = getGlobalValueSubClassData();
83 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
84 assert(getAlignment() == Align && "Alignment representation error!");
87 unsigned GlobalObject::getGlobalObjectSubClassData() const {
88 unsigned ValueData = getGlobalValueSubClassData();
89 return ValueData >> AlignmentBits;
92 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
93 unsigned OldData = getGlobalValueSubClassData();
94 setGlobalValueSubClassData((OldData & AlignmentMask) |
95 (Val << AlignmentBits));
96 assert(getGlobalObjectSubClassData() == Val && "representation error");
99 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
100 const auto *GV = cast<GlobalObject>(Src);
101 GlobalValue::copyAttributesFrom(GV);
102 setAlignment(GV->getAlignment());
103 setSection(GV->getSection());
106 const char *GlobalValue::getSection() const {
107 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
108 // In general we cannot compute this at the IR level, but we try.
109 if (const GlobalObject *GO = GA->getBaseObject())
110 return GO->getSection();
113 return cast<GlobalObject>(this)->getSection();
116 Comdat *GlobalValue::getComdat() {
117 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
118 // In general we cannot compute this at the IR level, but we try.
119 if (const GlobalObject *GO = GA->getBaseObject())
120 return const_cast<GlobalObject *>(GO)->getComdat();
123 return cast<GlobalObject>(this)->getComdat();
126 void GlobalObject::setSection(StringRef S) { Section = S; }
128 bool GlobalValue::isDeclaration() const {
129 // Globals are definitions if they have an initializer.
130 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
131 return GV->getNumOperands() == 0;
133 // Functions are definitions if they have a body.
134 if (const Function *F = dyn_cast<Function>(this))
135 return F->empty() && !F->isMaterializable();
137 // Aliases are always definitions.
138 assert(isa<GlobalAlias>(this));
142 //===----------------------------------------------------------------------===//
143 // GlobalVariable Implementation
144 //===----------------------------------------------------------------------===//
146 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
147 Constant *InitVal, const Twine &Name,
148 ThreadLocalMode TLMode, unsigned AddressSpace,
149 bool isExternallyInitialized)
150 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
151 OperandTraits<GlobalVariable>::op_begin(this),
152 InitVal != nullptr, Link, Name),
153 isConstantGlobal(constant),
154 isExternallyInitializedConstant(isExternallyInitialized) {
155 setThreadLocalMode(TLMode);
157 assert(InitVal->getType() == Ty &&
158 "Initializer should be the same type as the GlobalVariable!");
163 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
164 LinkageTypes Link, Constant *InitVal,
165 const Twine &Name, GlobalVariable *Before,
166 ThreadLocalMode TLMode, unsigned AddressSpace,
167 bool isExternallyInitialized)
168 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
169 OperandTraits<GlobalVariable>::op_begin(this),
170 InitVal != nullptr, Link, Name),
171 isConstantGlobal(constant),
172 isExternallyInitializedConstant(isExternallyInitialized) {
173 setThreadLocalMode(TLMode);
175 assert(InitVal->getType() == Ty &&
176 "Initializer should be the same type as the GlobalVariable!");
181 Before->getParent()->getGlobalList().insert(Before, this);
183 M.getGlobalList().push_back(this);
186 void GlobalVariable::setParent(Module *parent) {
190 void GlobalVariable::removeFromParent() {
191 getParent()->getGlobalList().remove(this);
194 void GlobalVariable::eraseFromParent() {
195 getParent()->getGlobalList().erase(this);
198 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
200 // If you call this, then you better know this GVar has a constant
201 // initializer worth replacing. Enforce that here.
202 assert(getNumOperands() == 1 &&
203 "Attempt to replace uses of Constants on a GVar with no initializer");
205 // And, since you know it has an initializer, the From value better be
206 // the initializer :)
207 assert(getOperand(0) == From &&
208 "Attempt to replace wrong constant initializer in GVar");
210 // And, you better have a constant for the replacement value
211 assert(isa<Constant>(To) &&
212 "Attempt to replace GVar initializer with non-constant");
214 // Okay, preconditions out of the way, replace the constant initializer.
215 this->setOperand(0, cast<Constant>(To));
218 void GlobalVariable::setInitializer(Constant *InitVal) {
220 if (hasInitializer()) {
221 Op<0>().set(nullptr);
225 assert(InitVal->getType() == getType()->getElementType() &&
226 "Initializer type must match GlobalVariable type");
227 if (!hasInitializer())
229 Op<0>().set(InitVal);
233 /// copyAttributesFrom - copy all additional attributes (those not needed to
234 /// create a GlobalVariable) from the GlobalVariable Src to this one.
235 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
236 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
237 GlobalObject::copyAttributesFrom(Src);
238 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
239 setThreadLocalMode(SrcVar->getThreadLocalMode());
240 setExternallyInitialized(SrcVar->isExternallyInitialized());
244 //===----------------------------------------------------------------------===//
245 // GlobalAlias Implementation
246 //===----------------------------------------------------------------------===//
248 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
249 const Twine &Name, Constant *Aliasee,
250 Module *ParentModule)
251 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
252 &Op<0>(), 1, Link, Name) {
256 ParentModule->getAliasList().push_back(this);
259 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
260 LinkageTypes Link, const Twine &Name,
261 Constant *Aliasee, Module *ParentModule) {
262 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
265 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
266 LinkageTypes Linkage, const Twine &Name,
268 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
271 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
272 LinkageTypes Linkage, const Twine &Name,
273 GlobalValue *Aliasee) {
274 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
277 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
278 GlobalValue *Aliasee) {
279 PointerType *PTy = Aliasee->getType();
280 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
284 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
285 return create(Aliasee->getLinkage(), Name, Aliasee);
288 void GlobalAlias::setParent(Module *parent) {
292 void GlobalAlias::removeFromParent() {
293 getParent()->getAliasList().remove(this);
296 void GlobalAlias::eraseFromParent() {
297 getParent()->getAliasList().erase(this);
300 void GlobalAlias::setAliasee(Constant *Aliasee) {
301 assert((!Aliasee || Aliasee->getType() == getType()) &&
302 "Alias and aliasee types should match!");
303 setOperand(0, Aliasee);