~pali/+junk/llvm-toolchain-3.7

« back to all changes in this revision

Viewing changes to lib/IR/Globals.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2015-07-15 17:51:08 UTC
  • Revision ID: package-import@ubuntu.com-20150715175108-l8mynwovkx4zx697
Tags: upstream-3.7~+rc2
ImportĀ upstreamĀ versionĀ 3.7~+rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
23
#include "llvm/Support/ErrorHandling.h"
 
24
using namespace llvm;
 
25
 
 
26
//===----------------------------------------------------------------------===//
 
27
//                            GlobalValue Class
 
28
//===----------------------------------------------------------------------===//
 
29
 
 
30
bool GlobalValue::isMaterializable() const {
 
31
  if (const Function *F = dyn_cast<Function>(this))
 
32
    return F->isMaterializable();
 
33
  return false;
 
34
}
 
35
bool GlobalValue::isDematerializable() const {
 
36
  return getParent() && getParent()->isDematerializable(this);
 
37
}
 
38
std::error_code GlobalValue::materialize() {
 
39
  return getParent()->materialize(this);
 
40
}
 
41
void GlobalValue::dematerialize() {
 
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.
 
57
void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
 
58
  setVisibility(Src->getVisibility());
 
59
  setUnnamedAddr(Src->hasUnnamedAddr());
 
60
  setDLLStorageClass(Src->getDLLStorageClass());
 
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
 
 
77
void GlobalObject::setAlignment(unsigned Align) {
 
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
 
 
87
unsigned GlobalObject::getGlobalObjectSubClassData() const {
 
88
  unsigned ValueData = getGlobalValueSubClassData();
 
89
  return ValueData >> AlignmentBits;
 
90
}
 
91
 
 
92
void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
 
93
  unsigned OldData = getGlobalValueSubClassData();
 
94
  setGlobalValueSubClassData((OldData & AlignmentMask) |
 
95
                             (Val << AlignmentBits));
 
96
  assert(getGlobalObjectSubClassData() == Val && "representation error");
 
97
}
 
98
 
 
99
void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
 
100
  const auto *GV = cast<GlobalObject>(Src);
 
101
  GlobalValue::copyAttributesFrom(GV);
 
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
 
 
116
Comdat *GlobalValue::getComdat() {
 
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
 
 
126
void GlobalObject::setSection(StringRef S) { Section = S; }
 
127
 
 
128
bool GlobalValue::isDeclaration() const {
 
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
 
 
190
void GlobalVariable::removeFromParent() {
 
191
  getParent()->getGlobalList().remove(this);
 
192
}
 
193
 
 
194
void GlobalVariable::eraseFromParent() {
 
195
  getParent()->getGlobalList().erase(this);
 
196
}
 
197
 
 
198
void GlobalVariable::setInitializer(Constant *InitVal) {
 
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);
 
205
      setGlobalVariableNumOperands(0);
 
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())
 
214
      setGlobalVariableNumOperands(1);
 
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.
 
221
void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
 
222
  assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
 
223
  GlobalObject::copyAttributesFrom(Src);
 
224
  const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
 
225
  setThreadLocalMode(SrcVar->getThreadLocalMode());
 
226
  setExternallyInitialized(SrcVar->isExternallyInitialized());
 
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
 
 
243
GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
 
244
                                 const Twine &Name, Constant *Aliasee,
 
245
                                 Module *ParentModule) {
 
246
  return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
 
247
}
 
248
 
 
249
GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
 
250
                                 const Twine &Name, Module *Parent) {
 
251
  return create(Ty, Linkage, Name, nullptr, Parent);
 
252
}
 
253
 
 
254
GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
 
255
                                 const Twine &Name, GlobalValue *Aliasee) {
 
256
  return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
 
257
}
 
258
 
 
259
GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
 
260
                                 GlobalValue *Aliasee) {
 
261
  PointerType *PTy = Aliasee->getType();
 
262
  return create(PTy, Link, Name, Aliasee);
 
263
}
 
264
 
 
265
GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
 
266
  return create(Aliasee->getLinkage(), Name, Aliasee);
 
267
}
 
268
 
 
269
void GlobalAlias::setParent(Module *parent) {
 
270
  Parent = parent;
 
271
}
 
272
 
 
273
void GlobalAlias::removeFromParent() {
 
274
  getParent()->getAliasList().remove(this);
 
275
}
 
276
 
 
277
void GlobalAlias::eraseFromParent() {
 
278
  getParent()->getAliasList().erase(this);
 
279
}
 
280
 
 
281
void GlobalAlias::setAliasee(Constant *Aliasee) {
 
282
  assert((!Aliasee || Aliasee->getType() == getType()) &&
 
283
         "Alias and aliasee types should match!");
 
284
  setOperand(0, Aliasee);
 
285
}