LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 // GlobalValue Class
28 //===----------------------------------------------------------------------===//
29 
31  return getParent() && getParent()->isMaterializable(this);
32 }
34  return getParent() && getParent()->isDematerializable(this);
35 }
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37  return getParent()->Materialize(this, ErrInfo);
38 }
40  getParent()->Dematerialize(this);
41 }
42 
43 /// Override destroyConstant to make sure it doesn't get called on
44 /// GlobalValue's because they shouldn't be treated like other constants.
46  llvm_unreachable("You can't GV->destroyConstant()!");
47 }
48 
49 /// copyAttributesFrom - copy all additional attributes (those not needed to
50 /// create a GlobalValue) from the GlobalValue Src to this one.
52  setAlignment(Src->getAlignment());
53  setSection(Src->getSection());
56 }
57 
59  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
60  assert(Align <= MaximumAlignment &&
61  "Alignment is greater than MaximumAlignment!");
62  Alignment = Log2_32(Align) + 1;
63  assert(getAlignment() == Align && "Alignment representation error!");
64 }
65 
67  // Globals are definitions if they have an initializer.
68  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
69  return GV->getNumOperands() == 0;
70 
71  // Functions are definitions if they have a body.
72  if (const Function *F = dyn_cast<Function>(this))
73  return F->empty();
74 
75  // Aliases are always definitions.
76  assert(isa<GlobalAlias>(this));
77  return false;
78 }
79 
80 //===----------------------------------------------------------------------===//
81 // GlobalVariable Implementation
82 //===----------------------------------------------------------------------===//
83 
84 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
85  Constant *InitVal,
86  const Twine &Name, ThreadLocalMode TLMode,
87  unsigned AddressSpace,
88  bool isExternallyInitialized)
89  : GlobalValue(PointerType::get(Ty, AddressSpace),
90  Value::GlobalVariableVal,
91  OperandTraits<GlobalVariable>::op_begin(this),
92  InitVal != 0, Link, Name),
93  isConstantGlobal(constant), threadLocalMode(TLMode),
94  isExternallyInitializedConstant(isExternallyInitialized) {
95  if (InitVal) {
96  assert(InitVal->getType() == Ty &&
97  "Initializer should be the same type as the GlobalVariable!");
98  Op<0>() = InitVal;
99  }
100 
102 }
103 
104 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
105  LinkageTypes Link, Constant *InitVal,
106  const Twine &Name,
107  GlobalVariable *Before, ThreadLocalMode TLMode,
108  unsigned AddressSpace,
109  bool isExternallyInitialized)
110  : GlobalValue(PointerType::get(Ty, AddressSpace),
111  Value::GlobalVariableVal,
112  OperandTraits<GlobalVariable>::op_begin(this),
113  InitVal != 0, Link, Name),
114  isConstantGlobal(constant), threadLocalMode(TLMode),
115  isExternallyInitializedConstant(isExternallyInitialized) {
116  if (InitVal) {
117  assert(InitVal->getType() == Ty &&
118  "Initializer should be the same type as the GlobalVariable!");
119  Op<0>() = InitVal;
120  }
121 
123 
124  if (Before)
125  Before->getParent()->getGlobalList().insert(Before, this);
126  else
127  M.getGlobalList().push_back(this);
128 }
129 
130 void GlobalVariable::setParent(Module *parent) {
131  if (getParent())
133  Parent = parent;
134  if (getParent())
136 }
137 
139  getParent()->getGlobalList().remove(this);
140 }
141 
143  getParent()->getGlobalList().erase(this);
144 }
145 
147  Use *U) {
148  // If you call this, then you better know this GVar has a constant
149  // initializer worth replacing. Enforce that here.
150  assert(getNumOperands() == 1 &&
151  "Attempt to replace uses of Constants on a GVar with no initializer");
152 
153  // And, since you know it has an initializer, the From value better be
154  // the initializer :)
155  assert(getOperand(0) == From &&
156  "Attempt to replace wrong constant initializer in GVar");
157 
158  // And, you better have a constant for the replacement value
159  assert(isa<Constant>(To) &&
160  "Attempt to replace GVar initializer with non-constant");
161 
162  // Okay, preconditions out of the way, replace the constant initializer.
163  this->setOperand(0, cast<Constant>(To));
164 }
165 
167  if (InitVal == 0) {
168  if (hasInitializer()) {
169  Op<0>().set(0);
170  NumOperands = 0;
171  }
172  } else {
173  assert(InitVal->getType() == getType()->getElementType() &&
174  "Initializer type must match GlobalVariable type");
175  if (!hasInitializer())
176  NumOperands = 1;
177  Op<0>().set(InitVal);
178  }
179 }
180 
181 /// copyAttributesFrom - copy all additional attributes (those not needed to
182 /// create a GlobalVariable) from the GlobalVariable Src to this one.
184  assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
186  const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
187  setThreadLocal(SrcVar->isThreadLocal());
188 }
189 
190 
191 //===----------------------------------------------------------------------===//
192 // GlobalAlias Implementation
193 //===----------------------------------------------------------------------===//
194 
195 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
196  const Twine &Name, Constant* aliasee,
197  Module *ParentModule)
198  : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
200 
201  if (aliasee)
202  assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
203  Op<0>() = aliasee;
204 
205  if (ParentModule)
206  ParentModule->getAliasList().push_back(this);
207 }
208 
209 void GlobalAlias::setParent(Module *parent) {
210  if (getParent())
212  Parent = parent;
213  if (getParent())
215 }
216 
218  getParent()->getAliasList().remove(this);
219 }
220 
222  getParent()->getAliasList().erase(this);
223 }
224 
226  assert((!Aliasee || Aliasee->getType() == getType()) &&
227  "Alias and aliasee types should match!");
228 
229  setOperand(0, Aliasee);
230 }
231 
233  Constant *C = getAliasee();
234  if (C == 0) return 0;
235 
236  if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
237  return GV;
238 
239  ConstantExpr *CE = cast<ConstantExpr>(C);
240  assert((CE->getOpcode() == Instruction::BitCast ||
241  CE->getOpcode() == Instruction::GetElementPtr) &&
242  "Unsupported aliasee");
243 
244  return cast<GlobalValue>(CE->getOperand(0));
245 }
246 
249 
250  // Check if we need to stop early.
251  if (stopOnWeak && mayBeOverridden())
252  return this;
253 
255  Visited.insert(GV);
256 
257  // Iterate over aliasing chain, stopping on weak alias if necessary.
258  while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
259  if (stopOnWeak && GA->mayBeOverridden())
260  break;
261 
262  GV = GA->getAliasedGlobal();
263 
264  if (!Visited.insert(GV))
265  return 0;
266  }
267 
268  return GV;
269 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:93
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:87
virtual void copyAttributesFrom(const GlobalValue *Src)
Definition: Globals.cpp:51
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getAlignment() const
Definition: GlobalValue.h:79
const GlobalListType & getGlobalList() const
Get the Module's list of global variables (constant).
Definition: Module.h:485
unsigned getNumOperands() const
Definition: User.h:108
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
void setSection(StringRef S)
Definition: GlobalValue.h:97
F(f)
void Dematerialize()
Definition: Globals.cpp:39
const AliasListType & getAliasList() const
Get the Module's list of aliases (constant).
Definition: Module.h:499
const Constant * getAliasee() const
Definition: GlobalAlias.h:61
unsigned getOpcode() const
getOpcode - Return the opcode at the root of this constant expression
Definition: Constants.h:1049
unsigned NumOperands
Definition: User.h:49
virtual void eraseFromParent()
Definition: Globals.cpp:221
static void removeGarbageObject(void *Object)
Definition: LeakDetector.h:47
void push_back(NodeTy *val)
Definition: ilist.h:554
void setInitializer(Constant *InitVal)
Definition: Globals.cpp:166
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
#define llvm_unreachable(msg)
Definition: Use.h:60
bool Materialize(GlobalValue *GV, std::string *ErrInfo=0)
Definition: Module.cpp:403
void setThreadLocal(bool Val)
bool isMaterializable() const
Definition: Globals.cpp:30
GlobalValue * resolveAliasedGlobal(bool stopOnWeak=true)
Definition: Globals.cpp:247
Type * getElementType() const
Definition: DerivedTypes.h:319
unsigned Alignment
Definition: GlobalValue.h:70
bool isDematerializable(const GlobalValue *GV) const
Definition: Module.cpp:397
LLVM Constant Representation.
Definition: Constant.h:41
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U)
Definition: Globals.cpp:146
bool isDematerializable() const
Definition: Globals.cpp:33
virtual void eraseFromParent()
Definition: Globals.cpp:142
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
Value * getOperand(unsigned i) const
Definition: User.h:88
const std::string & getSection() const
Definition: GlobalValue.h:96
void Dematerialize(GlobalValue *GV)
Definition: Module.cpp:415
virtual void removeFromParent()
Definition: Globals.cpp:138
iterator erase(iterator where)
Definition: ilist.h:465
void setAlignment(unsigned Align)
Definition: Globals.cpp:58
Type * getType() const
Definition: Value.h:111
void copyAttributesFrom(const GlobalValue *Src)
Definition: Globals.cpp:183
AddressSpace
Definition: NVPTXBaseInfo.h:22
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:85
void setAliasee(Constant *GV)
set/getAliasee - These methods retrive and set alias target.
Definition: Globals.cpp:225
void setOperand(unsigned i, Value *Val)
Definition: User.h:92
unsigned Log2_32(uint32_t Value)
Definition: MathExtras.h:443
bool hasInitializer() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:33
static void addGarbageObject(void *Object)
Definition: LeakDetector.h:37
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
virtual void destroyConstant()
Override from Constant class.
Definition: Globals.cpp:45
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
bool isDeclaration() const
Definition: Globals.cpp:66
virtual void removeFromParent()
Definition: Globals.cpp:217
bool isMaterializable(const GlobalValue *GV) const
Definition: Module.cpp:391
Module * getParent()
Definition: GlobalValue.h:286
LLVM Value Representation.
Definition: Value.h:66
bool hasUnnamedAddr() const
Definition: GlobalValue.h:84
bool mayBeOverridden() const
Definition: GlobalValue.h:224
bool Materialize(std::string *ErrInfo=0)
Definition: Globals.cpp:36
NodeTy * remove(iterator &IT)
Definition: ilist.h:435
GlobalValue * getAliasedGlobal()
Definition: Globals.cpp:232