LLVM  3.7.0
Globals.cpp
Go to the documentation of this file.
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/Module.h"
22 #include "llvm/IR/Operator.h"
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 // GlobalValue Class
28 //===----------------------------------------------------------------------===//
29 
31  if (const Function *F = dyn_cast<Function>(this))
32  return F->isMaterializable();
33  return false;
34 }
36  return getParent() && getParent()->isDematerializable(this);
37 }
38 std::error_code GlobalValue::materialize() {
39  return getParent()->materialize(this);
40 }
42  getParent()->dematerialize(this);
43 }
44 
45 /// Override destroyConstantImpl to make sure it doesn't get called on
46 /// GlobalValue's because they shouldn't be treated like other constants.
47 void GlobalValue::destroyConstantImpl() {
48  llvm_unreachable("You can't GV->destroyConstantImpl()!");
49 }
50 
51 Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) {
52  llvm_unreachable("Unsupported class for handleOperandChange()!");
53 }
54 
55 /// copyAttributesFrom - copy all additional attributes (those not needed to
56 /// create a GlobalValue) from the GlobalValue Src to this one.
61 }
62 
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();
68 
69  // FIXME: we should also be able to handle:
70  // Alias = Global + Offset
71  // Alias = Absolute
72  return 0;
73  }
74  return cast<GlobalObject>(this)->getAlignment();
75 }
76 
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!");
85 }
86 
88  unsigned ValueData = getGlobalValueSubClassData();
89  return ValueData >> AlignmentBits;
90 }
91 
93  unsigned OldData = getGlobalValueSubClassData();
94  setGlobalValueSubClassData((OldData & AlignmentMask) |
95  (Val << AlignmentBits));
96  assert(getGlobalObjectSubClassData() == Val && "representation error");
97 }
98 
100  const auto *GV = cast<GlobalObject>(Src);
102  setAlignment(GV->getAlignment());
103  setSection(GV->getSection());
104 }
105 
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();
111  return "";
112  }
113  return cast<GlobalObject>(this)->getSection();
114 }
115 
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();
121  return nullptr;
122  }
123  return cast<GlobalObject>(this)->getComdat();
124 }
125 
127 
129  // Globals are definitions if they have an initializer.
130  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
131  return GV->getNumOperands() == 0;
132 
133  // Functions are definitions if they have a body.
134  if (const Function *F = dyn_cast<Function>(this))
135  return F->empty() && !F->isMaterializable();
136 
137  // Aliases are always definitions.
138  assert(isa<GlobalAlias>(this));
139  return false;
140 }
141 
142 //===----------------------------------------------------------------------===//
143 // GlobalVariable Implementation
144 //===----------------------------------------------------------------------===//
145 
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);
156  if (InitVal) {
157  assert(InitVal->getType() == Ty &&
158  "Initializer should be the same type as the GlobalVariable!");
159  Op<0>() = InitVal;
160  }
161 }
162 
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);
174  if (InitVal) {
175  assert(InitVal->getType() == Ty &&
176  "Initializer should be the same type as the GlobalVariable!");
177  Op<0>() = InitVal;
178  }
179 
180  if (Before)
181  Before->getParent()->getGlobalList().insert(Before, this);
182  else
183  M.getGlobalList().push_back(this);
184 }
185 
186 void GlobalVariable::setParent(Module *parent) {
187  Parent = parent;
188 }
189 
191  getParent()->getGlobalList().remove(this);
192 }
193 
195  getParent()->getGlobalList().erase(this);
196 }
197 
199  if (!InitVal) {
200  if (hasInitializer()) {
201  // Note, the num operands is used to compute the offset of the operand, so
202  // the order here matters. Clearing the operand then clearing the num
203  // operands ensures we have the correct offset to the operand.
204  Op<0>().set(nullptr);
206  }
207  } else {
208  assert(InitVal->getType() == getType()->getElementType() &&
209  "Initializer type must match GlobalVariable type");
210  // Note, the num operands is used to compute the offset of the operand, so
211  // the order here matters. We need to set num operands to 1 first so that
212  // we get the correct offset to the first operand when we set it.
213  if (!hasInitializer())
215  Op<0>().set(InitVal);
216  }
217 }
218 
219 /// copyAttributesFrom - copy all additional attributes (those not needed to
220 /// create a GlobalVariable) from the GlobalVariable Src to this one.
222  assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
224  const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
227 }
228 
229 
230 //===----------------------------------------------------------------------===//
231 // GlobalAlias Implementation
232 //===----------------------------------------------------------------------===//
233 
234 GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
235  Constant *Aliasee, Module *ParentModule)
236  : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
237  Op<0>() = Aliasee;
238 
239  if (ParentModule)
240  ParentModule->getAliasList().push_back(this);
241 }
242 
244  const Twine &Name, Constant *Aliasee,
245  Module *ParentModule) {
246  return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
247 }
248 
250  const Twine &Name, Module *Parent) {
251  return create(Ty, Linkage, Name, nullptr, Parent);
252 }
253 
255  const Twine &Name, GlobalValue *Aliasee) {
256  return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
257 }
258 
260  GlobalValue *Aliasee) {
261  PointerType *PTy = Aliasee->getType();
262  return create(PTy, Link, Name, Aliasee);
263 }
264 
266  return create(Aliasee->getLinkage(), Name, Aliasee);
267 }
268 
269 void GlobalAlias::setParent(Module *parent) {
270  Parent = parent;
271 }
272 
274  getParent()->getAliasList().remove(this);
275 }
276 
278  getParent()->getAliasList().erase(this);
279 }
280 
282  assert((!Aliasee || Aliasee->getType() == getType()) &&
283  "Alias and aliasee types should match!");
284  setOperand(0, Aliasee);
285 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:145
LinkageTypes getLinkage() const
Definition: GlobalValue.h:289
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:139
virtual void copyAttributesFrom(const GlobalValue *Src)
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
Definition: Globals.cpp:57
std::error_code materialize(GlobalValue *GV)
Make sure the GlobalValue is fully read.
Definition: Module.cpp:390
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
void dematerialize(GlobalValue *GV)
If the GlobalValue is read in, and if the GVMaterializer supports it, release the memory for the func...
Definition: Module.cpp:397
const GlobalListType & getGlobalList() const
Get the Module's list of global variables (constant).
Definition: Module.h:512
F(f)
const char * getSection() const
Definition: Globals.cpp:106
void setAlignment(unsigned Align)
Definition: Globals.cpp:77
const AliasListType & getAliasList() const
Get the Module's list of aliases (constant).
Definition: Module.h:526
unsigned getGlobalObjectSubClassData() const
Definition: Globals.cpp:87
void push_back(NodeTy *val)
Definition: ilist.h:554
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:198
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
void setThreadLocalMode(ThreadLocalMode Val)
Definition: GlobalValue.h:156
static const unsigned MaximumAlignment
Definition: Value.h:463
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:194
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:173
unsigned getAlignment() const
Definition: GlobalObject.h:46
bool isMaterializable() const
If this function's Module is being lazily streamed in functions from disk or some other source...
Definition: Globals.cpp:30
Type * getElementType() const
Definition: DerivedTypes.h:323
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
bool isExternallyInitialized() const
bool isDematerializable(const GlobalValue *GV) const
Returns true if this GV was loaded from this Module's GVMaterializer and the GVMaterializer knows how...
Definition: Module.cpp:384
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static GlobalAlias * create(PointerType *Ty, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:243
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isDematerializable() const
Returns true if this function was loaded from a GVMaterializer that's still attached to its Module an...
Definition: Globals.cpp:35
void setExternallyInitialized(bool Val)
void removeFromParent() override
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it...
Definition: Globals.cpp:273
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
void setGlobalVariableNumOperands(unsigned NumOps)
Set the number of operands on a GlobalVariable.
Definition: User.h:148
iterator erase(iterator where)
Definition: ilist.h:465
Comdat * getComdat()
Definition: Globals.cpp:116
std::string Section
Definition: GlobalObject.h:36
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
AddressSpace
Definition: NVPTXBaseInfo.h:22
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:131
void copyAttributesFrom(const GlobalValue *Src) override
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
Definition: Globals.cpp:99
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
void setOperand(unsigned i, Value *Val)
Definition: User.h:122
unsigned Log2_32(uint32_t Value)
Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
Definition: MathExtras.h:468
bool hasInitializer() const
Definitions have initializers, declarations don't.
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:277
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:39
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:109
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:160
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:106
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
static const unsigned AlignmentBits
Definition: GlobalObject.h:38
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
Compile-time customization of User operands.
Definition: User.h:34
void copyAttributesFrom(const GlobalValue *Src) override
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:221
unsigned getAlignment() const
Definition: Globals.cpp:63
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
bool hasUnnamedAddr() const
Definition: GlobalValue.h:130
void dematerialize()
If this GlobalValue is read in, and if the GVMaterializer supports it, release the memory for the fun...
Definition: Globals.cpp:41
DLLStorageClassTypes getDLLStorageClass() const
Definition: GlobalValue.h:164
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
NodeTy * remove(iterator &IT)
Definition: ilist.h:435
std::error_code materialize()
Make sure this GlobalValue is fully read.
Definition: Globals.cpp:38
void setSection(StringRef S)
Definition: Globals.cpp:126
void setGlobalObjectSubClassData(unsigned Val)
Definition: Globals.cpp:92
void setAliasee(Constant *Aliasee)
These methods retrive and set alias target.
Definition: Globals.cpp:281
void removeFromParent() override
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it...
Definition: Globals.cpp:190