[pr19844] Add thread local mode to aliases.
[oota-llvm.git] / lib / IR / Globals.cpp
1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the GlobalValue & GlobalVariable classes for the IR
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
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/Support/ErrorHandling.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 //                            GlobalValue Class
28 //===----------------------------------------------------------------------===//
29
30 bool GlobalValue::isMaterializable() const {
31   return getParent() && getParent()->isMaterializable(this);
32 }
33 bool GlobalValue::isDematerializable() const {
34   return getParent() && getParent()->isDematerializable(this);
35 }
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37   return getParent()->Materialize(this, ErrInfo);
38 }
39 void GlobalValue::Dematerialize() {
40   getParent()->Dematerialize(this);
41 }
42
43 const DataLayout *GlobalValue::getDataLayout() const {
44   return getParent()->getDataLayout();
45 }
46
47 /// Override destroyConstant to make sure it doesn't get called on
48 /// GlobalValue's because they shouldn't be treated like other constants.
49 void GlobalValue::destroyConstant() {
50   llvm_unreachable("You can't GV->destroyConstant()!");
51 }
52
53 /// copyAttributesFrom - copy all additional attributes (those not needed to
54 /// create a GlobalValue) from the GlobalValue Src to this one.
55 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
56   setVisibility(Src->getVisibility());
57   setUnnamedAddr(Src->hasUnnamedAddr());
58   setDLLStorageClass(Src->getDLLStorageClass());
59 }
60
61 unsigned GlobalValue::getAlignment() const {
62   if (auto *GA = dyn_cast<GlobalAlias>(this))
63     return GA->getAliasee()->getAlignment();
64
65   return cast<GlobalObject>(this)->getAlignment();
66 }
67
68 void GlobalObject::setAlignment(unsigned Align) {
69   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
70   assert(Align <= MaximumAlignment &&
71          "Alignment is greater than MaximumAlignment!");
72   setGlobalValueSubClassData(Log2_32(Align) + 1);
73   assert(getAlignment() == Align && "Alignment representation error!");
74 }
75
76 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
77   const auto *GV = cast<GlobalObject>(Src);
78   GlobalValue::copyAttributesFrom(GV);
79   setAlignment(GV->getAlignment());
80   setSection(GV->getSection());
81 }
82
83 const std::string &GlobalValue::getSection() const {
84   if (auto *GA = dyn_cast<GlobalAlias>(this))
85     return GA->getAliasee()->getSection();
86   return cast<GlobalObject>(this)->getSection();
87 }
88
89 void GlobalObject::setSection(StringRef S) { Section = S; }
90
91 bool GlobalValue::isDeclaration() const {
92   // Globals are definitions if they have an initializer.
93   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
94     return GV->getNumOperands() == 0;
95
96   // Functions are definitions if they have a body.
97   if (const Function *F = dyn_cast<Function>(this))
98     return F->empty();
99
100   // Aliases are always definitions.
101   assert(isa<GlobalAlias>(this));
102   return false;
103 }
104
105 //===----------------------------------------------------------------------===//
106 // GlobalVariable Implementation
107 //===----------------------------------------------------------------------===//
108
109 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
110                                Constant *InitVal, const Twine &Name,
111                                ThreadLocalMode TLMode, unsigned AddressSpace,
112                                bool isExternallyInitialized)
113     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
114                    OperandTraits<GlobalVariable>::op_begin(this),
115                    InitVal != nullptr, Link, Name),
116       isConstantGlobal(constant),
117       isExternallyInitializedConstant(isExternallyInitialized) {
118   setThreadLocalMode(TLMode);
119   if (InitVal) {
120     assert(InitVal->getType() == Ty &&
121            "Initializer should be the same type as the GlobalVariable!");
122     Op<0>() = InitVal;
123   }
124
125   LeakDetector::addGarbageObject(this);
126 }
127
128 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
129                                LinkageTypes Link, Constant *InitVal,
130                                const Twine &Name, GlobalVariable *Before,
131                                ThreadLocalMode TLMode, unsigned AddressSpace,
132                                bool isExternallyInitialized)
133     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
134                    OperandTraits<GlobalVariable>::op_begin(this),
135                    InitVal != nullptr, Link, Name),
136       isConstantGlobal(constant),
137       isExternallyInitializedConstant(isExternallyInitialized) {
138   setThreadLocalMode(TLMode);
139   if (InitVal) {
140     assert(InitVal->getType() == Ty &&
141            "Initializer should be the same type as the GlobalVariable!");
142     Op<0>() = InitVal;
143   }
144
145   LeakDetector::addGarbageObject(this);
146
147   if (Before)
148     Before->getParent()->getGlobalList().insert(Before, this);
149   else
150     M.getGlobalList().push_back(this);
151 }
152
153 void GlobalVariable::setParent(Module *parent) {
154   if (getParent())
155     LeakDetector::addGarbageObject(this);
156   Parent = parent;
157   if (getParent())
158     LeakDetector::removeGarbageObject(this);
159 }
160
161 void GlobalVariable::removeFromParent() {
162   getParent()->getGlobalList().remove(this);
163 }
164
165 void GlobalVariable::eraseFromParent() {
166   getParent()->getGlobalList().erase(this);
167 }
168
169 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
170                                                  Use *U) {
171   // If you call this, then you better know this GVar has a constant
172   // initializer worth replacing. Enforce that here.
173   assert(getNumOperands() == 1 &&
174          "Attempt to replace uses of Constants on a GVar with no initializer");
175
176   // And, since you know it has an initializer, the From value better be
177   // the initializer :)
178   assert(getOperand(0) == From &&
179          "Attempt to replace wrong constant initializer in GVar");
180
181   // And, you better have a constant for the replacement value
182   assert(isa<Constant>(To) &&
183          "Attempt to replace GVar initializer with non-constant");
184
185   // Okay, preconditions out of the way, replace the constant initializer.
186   this->setOperand(0, cast<Constant>(To));
187 }
188
189 void GlobalVariable::setInitializer(Constant *InitVal) {
190   if (!InitVal) {
191     if (hasInitializer()) {
192       Op<0>().set(nullptr);
193       NumOperands = 0;
194     }
195   } else {
196     assert(InitVal->getType() == getType()->getElementType() &&
197            "Initializer type must match GlobalVariable type");
198     if (!hasInitializer())
199       NumOperands = 1;
200     Op<0>().set(InitVal);
201   }
202 }
203
204 /// copyAttributesFrom - copy all additional attributes (those not needed to
205 /// create a GlobalVariable) from the GlobalVariable Src to this one.
206 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
207   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
208   GlobalObject::copyAttributesFrom(Src);
209   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
210   setThreadLocalMode(SrcVar->getThreadLocalMode());
211 }
212
213
214 //===----------------------------------------------------------------------===//
215 // GlobalAlias Implementation
216 //===----------------------------------------------------------------------===//
217
218 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
219                          const Twine &Name, GlobalObject *Aliasee,
220                          Module *ParentModule)
221     : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
222                   &Op<0>(), 1, Link, Name) {
223   LeakDetector::addGarbageObject(this);
224   Op<0>() = Aliasee;
225
226   if (ParentModule)
227     ParentModule->getAliasList().push_back(this);
228 }
229
230 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
231                                  LinkageTypes Link, const Twine &Name,
232                                  GlobalObject *Aliasee, Module *ParentModule) {
233   return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
234 }
235
236 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
237                                  LinkageTypes Linkage, const Twine &Name,
238                                  Module *Parent) {
239   return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
240 }
241
242 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
243                                  LinkageTypes Linkage, const Twine &Name,
244                                  GlobalObject *Aliasee) {
245   return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
246 }
247
248 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
249                                  GlobalObject *Aliasee) {
250   PointerType *PTy = Aliasee->getType();
251   return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
252                 Aliasee);
253 }
254
255 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalObject *Aliasee) {
256   return create(Aliasee->getLinkage(), Name, Aliasee);
257 }
258
259 void GlobalAlias::setParent(Module *parent) {
260   if (getParent())
261     LeakDetector::addGarbageObject(this);
262   Parent = parent;
263   if (getParent())
264     LeakDetector::removeGarbageObject(this);
265 }
266
267 void GlobalAlias::removeFromParent() {
268   getParent()->getAliasList().remove(this);
269 }
270
271 void GlobalAlias::eraseFromParent() {
272   getParent()->getAliasList().erase(this);
273 }
274
275 void GlobalAlias::setAliasee(GlobalObject *Aliasee) { setOperand(0, Aliasee); }