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 std::error_code GlobalValue::materialize() {
36 return getParent()->materialize(this);
39 /// Override destroyConstantImpl to make sure it doesn't get called on
40 /// GlobalValue's because they shouldn't be treated like other constants.
41 void GlobalValue::destroyConstantImpl() {
42 llvm_unreachable("You can't GV->destroyConstantImpl()!");
45 Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) {
46 llvm_unreachable("Unsupported class for handleOperandChange()!");
49 /// copyAttributesFrom - copy all additional attributes (those not needed to
50 /// create a GlobalValue) from the GlobalValue Src to this one.
51 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52 setVisibility(Src->getVisibility());
53 setUnnamedAddr(Src->hasUnnamedAddr());
54 setDLLStorageClass(Src->getDLLStorageClass());
57 unsigned GlobalValue::getAlignment() const {
58 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
59 // In general we cannot compute this at the IR level, but we try.
60 if (const GlobalObject *GO = GA->getBaseObject())
61 return GO->getAlignment();
63 // FIXME: we should also be able to handle:
64 // Alias = Global + Offset
68 return cast<GlobalObject>(this)->getAlignment();
71 void GlobalObject::setAlignment(unsigned Align) {
72 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
73 assert(Align <= MaximumAlignment &&
74 "Alignment is greater than MaximumAlignment!");
75 unsigned AlignmentData = Log2_32(Align) + 1;
76 unsigned OldData = getGlobalValueSubClassData();
77 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
78 assert(getAlignment() == Align && "Alignment representation error!");
81 unsigned GlobalObject::getGlobalObjectSubClassData() const {
82 unsigned ValueData = getGlobalValueSubClassData();
83 return ValueData >> AlignmentBits;
86 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
87 unsigned OldData = getGlobalValueSubClassData();
88 setGlobalValueSubClassData((OldData & AlignmentMask) |
89 (Val << AlignmentBits));
90 assert(getGlobalObjectSubClassData() == Val && "representation error");
93 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
94 GlobalValue::copyAttributesFrom(Src);
95 if (const auto *GV = dyn_cast<GlobalObject>(Src)) {
96 setAlignment(GV->getAlignment());
97 setSection(GV->getSection());
101 const char *GlobalValue::getSection() const {
102 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
103 // In general we cannot compute this at the IR level, but we try.
104 if (const GlobalObject *GO = GA->getBaseObject())
105 return GO->getSection();
108 return cast<GlobalObject>(this)->getSection();
111 Comdat *GlobalValue::getComdat() {
112 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
113 // In general we cannot compute this at the IR level, but we try.
114 if (const GlobalObject *GO = GA->getBaseObject())
115 return const_cast<GlobalObject *>(GO)->getComdat();
118 return cast<GlobalObject>(this)->getComdat();
121 void GlobalObject::setSection(StringRef S) { Section = S; }
123 bool GlobalValue::isDeclaration() const {
124 // Globals are definitions if they have an initializer.
125 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
126 return GV->getNumOperands() == 0;
128 // Functions are definitions if they have a body.
129 if (const Function *F = dyn_cast<Function>(this))
130 return F->empty() && !F->isMaterializable();
132 // Aliases are always definitions.
133 assert(isa<GlobalAlias>(this));
137 //===----------------------------------------------------------------------===//
138 // GlobalVariable Implementation
139 //===----------------------------------------------------------------------===//
141 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
142 Constant *InitVal, const Twine &Name,
143 ThreadLocalMode TLMode, unsigned AddressSpace,
144 bool isExternallyInitialized)
145 : GlobalObject(Ty, Value::GlobalVariableVal,
146 OperandTraits<GlobalVariable>::op_begin(this),
147 InitVal != nullptr, Link, Name, AddressSpace),
148 isConstantGlobal(constant),
149 isExternallyInitializedConstant(isExternallyInitialized) {
150 setThreadLocalMode(TLMode);
152 assert(InitVal->getType() == Ty &&
153 "Initializer should be the same type as the GlobalVariable!");
158 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
159 LinkageTypes Link, Constant *InitVal,
160 const Twine &Name, GlobalVariable *Before,
161 ThreadLocalMode TLMode, unsigned AddressSpace,
162 bool isExternallyInitialized)
163 : GlobalObject(Ty, Value::GlobalVariableVal,
164 OperandTraits<GlobalVariable>::op_begin(this),
165 InitVal != nullptr, Link, Name, AddressSpace),
166 isConstantGlobal(constant),
167 isExternallyInitializedConstant(isExternallyInitialized) {
168 setThreadLocalMode(TLMode);
170 assert(InitVal->getType() == Ty &&
171 "Initializer should be the same type as the GlobalVariable!");
176 Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
178 M.getGlobalList().push_back(this);
181 void GlobalVariable::setParent(Module *parent) {
185 void GlobalVariable::removeFromParent() {
186 getParent()->getGlobalList().remove(getIterator());
189 void GlobalVariable::eraseFromParent() {
190 getParent()->getGlobalList().erase(getIterator());
193 void GlobalVariable::setInitializer(Constant *InitVal) {
195 if (hasInitializer()) {
196 // Note, the num operands is used to compute the offset of the operand, so
197 // the order here matters. Clearing the operand then clearing the num
198 // operands ensures we have the correct offset to the operand.
199 Op<0>().set(nullptr);
200 setGlobalVariableNumOperands(0);
203 assert(InitVal->getType() == getType()->getElementType() &&
204 "Initializer type must match GlobalVariable type");
205 // Note, the num operands is used to compute the offset of the operand, so
206 // the order here matters. We need to set num operands to 1 first so that
207 // we get the correct offset to the first operand when we set it.
208 if (!hasInitializer())
209 setGlobalVariableNumOperands(1);
210 Op<0>().set(InitVal);
214 /// Copy all additional attributes (those not needed to create a GlobalVariable)
215 /// from the GlobalVariable Src to this one.
216 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
217 GlobalObject::copyAttributesFrom(Src);
218 if (const GlobalVariable *SrcVar = dyn_cast<GlobalVariable>(Src)) {
219 setThreadLocalMode(SrcVar->getThreadLocalMode());
220 setExternallyInitialized(SrcVar->isExternallyInitialized());
225 //===----------------------------------------------------------------------===//
226 // GlobalAlias Implementation
227 //===----------------------------------------------------------------------===//
229 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
230 const Twine &Name, Constant *Aliasee,
231 Module *ParentModule)
232 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name,
237 ParentModule->getAliasList().push_back(this);
240 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
241 LinkageTypes Link, const Twine &Name,
242 Constant *Aliasee, Module *ParentModule) {
243 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
246 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
247 LinkageTypes Linkage, const Twine &Name,
249 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
252 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
253 LinkageTypes Linkage, const Twine &Name,
254 GlobalValue *Aliasee) {
255 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
258 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
259 GlobalValue *Aliasee) {
260 PointerType *PTy = Aliasee->getType();
261 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
265 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
266 return create(Aliasee->getLinkage(), Name, Aliasee);
269 void GlobalAlias::setParent(Module *parent) {
273 void GlobalAlias::removeFromParent() {
274 getParent()->getAliasList().remove(getIterator());
277 void GlobalAlias::eraseFromParent() {
278 getParent()->getAliasList().erase(getIterator());
281 void GlobalAlias::setAliasee(Constant *Aliasee) {
282 assert((!Aliasee || Aliasee->getType() == getType()) &&
283 "Alias and aliasee types should match!");
284 setOperand(0, Aliasee);