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/IR/Operator.h"
24 #include "llvm/Support/ErrorHandling.h"
27 //===----------------------------------------------------------------------===//
29 //===----------------------------------------------------------------------===//
31 bool GlobalValue::isMaterializable() const {
32 if (const Function *F = dyn_cast<Function>(this))
33 return F->isMaterializable();
36 bool GlobalValue::isDematerializable() const {
37 return getParent() && getParent()->isDematerializable(this);
39 std::error_code GlobalValue::materialize() {
40 return getParent()->materialize(this);
42 void GlobalValue::Dematerialize() {
43 getParent()->Dematerialize(this);
46 const DataLayout *GlobalValue::getDataLayout() const {
47 return getParent()->getDataLayout();
50 /// Override destroyConstant to make sure it doesn't get called on
51 /// GlobalValue's because they shouldn't be treated like other constants.
52 void GlobalValue::destroyConstant() {
53 llvm_unreachable("You can't GV->destroyConstant()!");
56 /// copyAttributesFrom - copy all additional attributes (those not needed to
57 /// create a GlobalValue) from the GlobalValue Src to this one.
58 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
59 setVisibility(Src->getVisibility());
60 setUnnamedAddr(Src->hasUnnamedAddr());
61 setDLLStorageClass(Src->getDLLStorageClass());
64 unsigned GlobalValue::getAlignment() const {
65 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
66 // In general we cannot compute this at the IR level, but we try.
67 if (const GlobalObject *GO = GA->getBaseObject())
68 return GO->getAlignment();
70 // FIXME: we should also be able to handle:
71 // Alias = Global + Offset
75 return cast<GlobalObject>(this)->getAlignment();
78 void GlobalObject::setAlignment(unsigned Align) {
79 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
80 assert(Align <= MaximumAlignment &&
81 "Alignment is greater than MaximumAlignment!");
82 unsigned AlignmentData = Log2_32(Align) + 1;
83 unsigned OldData = getGlobalValueSubClassData();
84 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
85 assert(getAlignment() == Align && "Alignment representation error!");
88 unsigned GlobalObject::getGlobalObjectSubClassData() const {
89 unsigned ValueData = getGlobalValueSubClassData();
90 return ValueData >> AlignmentBits;
93 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
94 unsigned OldData = getGlobalValueSubClassData();
95 setGlobalValueSubClassData((OldData & AlignmentMask) |
96 (Val << AlignmentBits));
97 assert(getGlobalObjectSubClassData() == Val && "representation error");
100 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
101 const auto *GV = cast<GlobalObject>(Src);
102 GlobalValue::copyAttributesFrom(GV);
103 setAlignment(GV->getAlignment());
104 setSection(GV->getSection());
107 const char *GlobalValue::getSection() const {
108 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
109 // In general we cannot compute this at the IR level, but we try.
110 if (const GlobalObject *GO = GA->getBaseObject())
111 return GO->getSection();
114 return cast<GlobalObject>(this)->getSection();
117 Comdat *GlobalValue::getComdat() {
118 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
119 // In general we cannot compute this at the IR level, but we try.
120 if (const GlobalObject *GO = GA->getBaseObject())
121 return const_cast<GlobalObject *>(GO)->getComdat();
124 return cast<GlobalObject>(this)->getComdat();
127 void GlobalObject::setSection(StringRef S) { Section = S; }
129 bool GlobalValue::isDeclaration() const {
130 // Globals are definitions if they have an initializer.
131 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
132 return GV->getNumOperands() == 0;
134 // Functions are definitions if they have a body.
135 if (const Function *F = dyn_cast<Function>(this))
136 return F->empty() && !F->isMaterializable();
138 // Aliases are always definitions.
139 assert(isa<GlobalAlias>(this));
143 //===----------------------------------------------------------------------===//
144 // GlobalVariable Implementation
145 //===----------------------------------------------------------------------===//
147 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
148 Constant *InitVal, const Twine &Name,
149 ThreadLocalMode TLMode, unsigned AddressSpace,
150 bool isExternallyInitialized)
151 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
152 OperandTraits<GlobalVariable>::op_begin(this),
153 InitVal != nullptr, Link, Name),
154 isConstantGlobal(constant),
155 isExternallyInitializedConstant(isExternallyInitialized) {
156 setThreadLocalMode(TLMode);
158 assert(InitVal->getType() == Ty &&
159 "Initializer should be the same type as the GlobalVariable!");
163 LeakDetector::addGarbageObject(this);
166 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
167 LinkageTypes Link, Constant *InitVal,
168 const Twine &Name, GlobalVariable *Before,
169 ThreadLocalMode TLMode, unsigned AddressSpace,
170 bool isExternallyInitialized)
171 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
172 OperandTraits<GlobalVariable>::op_begin(this),
173 InitVal != nullptr, Link, Name),
174 isConstantGlobal(constant),
175 isExternallyInitializedConstant(isExternallyInitialized) {
176 setThreadLocalMode(TLMode);
178 assert(InitVal->getType() == Ty &&
179 "Initializer should be the same type as the GlobalVariable!");
183 LeakDetector::addGarbageObject(this);
186 Before->getParent()->getGlobalList().insert(Before, this);
188 M.getGlobalList().push_back(this);
191 void GlobalVariable::setParent(Module *parent) {
193 LeakDetector::addGarbageObject(this);
196 LeakDetector::removeGarbageObject(this);
199 void GlobalVariable::removeFromParent() {
200 getParent()->getGlobalList().remove(this);
203 void GlobalVariable::eraseFromParent() {
204 getParent()->getGlobalList().erase(this);
207 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
209 // If you call this, then you better know this GVar has a constant
210 // initializer worth replacing. Enforce that here.
211 assert(getNumOperands() == 1 &&
212 "Attempt to replace uses of Constants on a GVar with no initializer");
214 // And, since you know it has an initializer, the From value better be
215 // the initializer :)
216 assert(getOperand(0) == From &&
217 "Attempt to replace wrong constant initializer in GVar");
219 // And, you better have a constant for the replacement value
220 assert(isa<Constant>(To) &&
221 "Attempt to replace GVar initializer with non-constant");
223 // Okay, preconditions out of the way, replace the constant initializer.
224 this->setOperand(0, cast<Constant>(To));
227 void GlobalVariable::setInitializer(Constant *InitVal) {
229 if (hasInitializer()) {
230 Op<0>().set(nullptr);
234 assert(InitVal->getType() == getType()->getElementType() &&
235 "Initializer type must match GlobalVariable type");
236 if (!hasInitializer())
238 Op<0>().set(InitVal);
242 /// copyAttributesFrom - copy all additional attributes (those not needed to
243 /// create a GlobalVariable) from the GlobalVariable Src to this one.
244 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
245 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
246 GlobalObject::copyAttributesFrom(Src);
247 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
248 setThreadLocalMode(SrcVar->getThreadLocalMode());
249 setExternallyInitialized(SrcVar->isExternallyInitialized());
253 //===----------------------------------------------------------------------===//
254 // GlobalAlias Implementation
255 //===----------------------------------------------------------------------===//
257 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
258 const Twine &Name, Constant *Aliasee,
259 Module *ParentModule)
260 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
261 &Op<0>(), 1, Link, Name) {
262 LeakDetector::addGarbageObject(this);
266 ParentModule->getAliasList().push_back(this);
269 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
270 LinkageTypes Link, const Twine &Name,
271 Constant *Aliasee, Module *ParentModule) {
272 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
275 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
276 LinkageTypes Linkage, const Twine &Name,
278 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
281 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
282 LinkageTypes Linkage, const Twine &Name,
283 GlobalValue *Aliasee) {
284 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
287 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
288 GlobalValue *Aliasee) {
289 PointerType *PTy = Aliasee->getType();
290 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
294 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
295 return create(Aliasee->getLinkage(), Name, Aliasee);
298 void GlobalAlias::setParent(Module *parent) {
300 LeakDetector::addGarbageObject(this);
303 LeakDetector::removeGarbageObject(this);
306 void GlobalAlias::removeFromParent() {
307 getParent()->getAliasList().remove(this);
310 void GlobalAlias::eraseFromParent() {
311 getParent()->getAliasList().erase(this);
314 void GlobalAlias::setAliasee(Constant *Aliasee) {
315 assert((!Aliasee || Aliasee->getType() == getType()) &&
316 "Alias and aliasee types should match!");
317 setOperand(0, Aliasee);