1249259Sdim//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2249259Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6249259Sdim//
7249259Sdim//===----------------------------------------------------------------------===//
8249259Sdim//
9249259Sdim// This file implements the GlobalValue & GlobalVariable classes for the IR
10249259Sdim// library.
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim
14321369Sdim#include "LLVMContextImpl.h"
15249259Sdim#include "llvm/ADT/SmallPtrSet.h"
16296417Sdim#include "llvm/ADT/Triple.h"
17321369Sdim#include "llvm/IR/ConstantRange.h"
18249259Sdim#include "llvm/IR/Constants.h"
19249259Sdim#include "llvm/IR/DerivedTypes.h"
20249259Sdim#include "llvm/IR/GlobalAlias.h"
21296417Sdim#include "llvm/IR/GlobalValue.h"
22249259Sdim#include "llvm/IR/GlobalVariable.h"
23249259Sdim#include "llvm/IR/Module.h"
24276479Sdim#include "llvm/IR/Operator.h"
25314564Sdim#include "llvm/Support/Error.h"
26249259Sdim#include "llvm/Support/ErrorHandling.h"
27249259Sdimusing namespace llvm;
28249259Sdim
29249259Sdim//===----------------------------------------------------------------------===//
30249259Sdim//                            GlobalValue Class
31249259Sdim//===----------------------------------------------------------------------===//
32249259Sdim
33314564Sdim// GlobalValue should be a Constant, plus a type, a module, some flags, and an
34314564Sdim// intrinsic ID. Add an assert to prevent people from accidentally growing
35314564Sdim// GlobalValue while adding flags.
36314564Sdimstatic_assert(sizeof(GlobalValue) ==
37314564Sdim                  sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
38314564Sdim              "unexpected GlobalValue size growth");
39314564Sdim
40314564Sdim// GlobalObject adds a comdat.
41314564Sdimstatic_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
42314564Sdim              "unexpected GlobalObject size growth");
43314564Sdim
44249259Sdimbool GlobalValue::isMaterializable() const {
45280031Sdim  if (const Function *F = dyn_cast<Function>(this))
46280031Sdim    return F->isMaterializable();
47280031Sdim  return false;
48249259Sdim}
49314564SdimError GlobalValue::materialize() {
50280031Sdim  return getParent()->materialize(this);
51249259Sdim}
52249259Sdim
53288943Sdim/// Override destroyConstantImpl to make sure it doesn't get called on
54288943Sdim/// GlobalValue's because they shouldn't be treated like other constants.
55288943Sdimvoid GlobalValue::destroyConstantImpl() {
56288943Sdim  llvm_unreachable("You can't GV->destroyConstantImpl()!");
57276479Sdim}
58276479Sdim
59309124SdimValue *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
60288943Sdim  llvm_unreachable("Unsupported class for handleOperandChange()!");
61249259Sdim}
62249259Sdim
63249259Sdim/// copyAttributesFrom - copy all additional attributes (those not needed to
64249259Sdim/// create a GlobalValue) from the GlobalValue Src to this one.
65249259Sdimvoid GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
66249259Sdim  setVisibility(Src->getVisibility());
67309124Sdim  setUnnamedAddr(Src->getUnnamedAddr());
68276479Sdim  setDLLStorageClass(Src->getDLLStorageClass());
69327952Sdim  setDSOLocal(Src->isDSOLocal());
70353358Sdim  setPartition(Src->getPartition());
71249259Sdim}
72249259Sdim
73321369Sdimvoid GlobalValue::removeFromParent() {
74321369Sdim  switch (getValueID()) {
75321369Sdim#define HANDLE_GLOBAL_VALUE(NAME)                                              \
76321369Sdim  case Value::NAME##Val:                                                       \
77321369Sdim    return static_cast<NAME *>(this)->removeFromParent();
78321369Sdim#include "llvm/IR/Value.def"
79321369Sdim  default:
80321369Sdim    break;
81321369Sdim  }
82321369Sdim  llvm_unreachable("not a global");
83321369Sdim}
84321369Sdim
85321369Sdimvoid GlobalValue::eraseFromParent() {
86321369Sdim  switch (getValueID()) {
87321369Sdim#define HANDLE_GLOBAL_VALUE(NAME)                                              \
88321369Sdim  case Value::NAME##Val:                                                       \
89321369Sdim    return static_cast<NAME *>(this)->eraseFromParent();
90321369Sdim#include "llvm/IR/Value.def"
91321369Sdim  default:
92321369Sdim    break;
93321369Sdim  }
94321369Sdim  llvm_unreachable("not a global");
95321369Sdim}
96321369Sdim
97276479Sdimunsigned GlobalValue::getAlignment() const {
98276479Sdim  if (auto *GA = dyn_cast<GlobalAlias>(this)) {
99276479Sdim    // In general we cannot compute this at the IR level, but we try.
100276479Sdim    if (const GlobalObject *GO = GA->getBaseObject())
101276479Sdim      return GO->getAlignment();
102276479Sdim
103276479Sdim    // FIXME: we should also be able to handle:
104276479Sdim    // Alias = Global + Offset
105276479Sdim    // Alias = Absolute
106276479Sdim    return 0;
107276479Sdim  }
108276479Sdim  return cast<GlobalObject>(this)->getAlignment();
109276479Sdim}
110276479Sdim
111344779Sdimunsigned GlobalValue::getAddressSpace() const {
112344779Sdim  PointerType *PtrTy = getType();
113344779Sdim  return PtrTy->getAddressSpace();
114344779Sdim}
115344779Sdim
116276479Sdimvoid GlobalObject::setAlignment(unsigned Align) {
117360784Sdim  setAlignment(MaybeAlign(Align));
118360784Sdim}
119360784Sdim
120360784Sdimvoid GlobalObject::setAlignment(MaybeAlign Align) {
121360784Sdim  assert((!Align || Align <= MaximumAlignment) &&
122249259Sdim         "Alignment is greater than MaximumAlignment!");
123360784Sdim  unsigned AlignmentData = encode(Align);
124280031Sdim  unsigned OldData = getGlobalValueSubClassData();
125280031Sdim  setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
126360784Sdim  assert(MaybeAlign(getAlignment()) == Align &&
127360784Sdim         "Alignment representation error!");
128249259Sdim}
129249259Sdim
130321369Sdimvoid GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
131296417Sdim  GlobalValue::copyAttributesFrom(Src);
132360784Sdim  setAlignment(MaybeAlign(Src->getAlignment()));
133321369Sdim  setSection(Src->getSection());
134276479Sdim}
135276479Sdim
136309124Sdimstd::string GlobalValue::getGlobalIdentifier(StringRef Name,
137309124Sdim                                             GlobalValue::LinkageTypes Linkage,
138309124Sdim                                             StringRef FileName) {
139309124Sdim
140309124Sdim  // Value names may be prefixed with a binary '1' to indicate
141309124Sdim  // that the backend should not modify the symbols due to any platform
142309124Sdim  // naming convention. Do not include that '1' in the PGO profile name.
143309124Sdim  if (Name[0] == '\1')
144309124Sdim    Name = Name.substr(1);
145309124Sdim
146309124Sdim  std::string NewName = Name;
147309124Sdim  if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
148309124Sdim    // For local symbols, prepend the main file name to distinguish them.
149309124Sdim    // Do not include the full path in the file name since there's no guarantee
150309124Sdim    // that it will stay the same, e.g., if the files are checked out from
151309124Sdim    // version control in different locations.
152309124Sdim    if (FileName.empty())
153309124Sdim      NewName = NewName.insert(0, "<unknown>:");
154309124Sdim    else
155309124Sdim      NewName = NewName.insert(0, FileName.str() + ":");
156309124Sdim  }
157309124Sdim  return NewName;
158309124Sdim}
159309124Sdim
160309124Sdimstd::string GlobalValue::getGlobalIdentifier() const {
161309124Sdim  return getGlobalIdentifier(getName(), getLinkage(),
162309124Sdim                             getParent()->getSourceFileName());
163309124Sdim}
164309124Sdim
165309124SdimStringRef GlobalValue::getSection() const {
166276479Sdim  if (auto *GA = dyn_cast<GlobalAlias>(this)) {
167276479Sdim    // In general we cannot compute this at the IR level, but we try.
168276479Sdim    if (const GlobalObject *GO = GA->getBaseObject())
169276479Sdim      return GO->getSection();
170276479Sdim    return "";
171276479Sdim  }
172276479Sdim  return cast<GlobalObject>(this)->getSection();
173276479Sdim}
174276479Sdim
175321369Sdimconst Comdat *GlobalValue::getComdat() const {
176276479Sdim  if (auto *GA = dyn_cast<GlobalAlias>(this)) {
177276479Sdim    // In general we cannot compute this at the IR level, but we try.
178276479Sdim    if (const GlobalObject *GO = GA->getBaseObject())
179276479Sdim      return const_cast<GlobalObject *>(GO)->getComdat();
180276479Sdim    return nullptr;
181276479Sdim  }
182309124Sdim  // ifunc and its resolver are separate things so don't use resolver comdat.
183309124Sdim  if (isa<GlobalIFunc>(this))
184309124Sdim    return nullptr;
185276479Sdim  return cast<GlobalObject>(this)->getComdat();
186276479Sdim}
187276479Sdim
188353358SdimStringRef GlobalValue::getPartition() const {
189353358Sdim  if (!hasPartition())
190353358Sdim    return "";
191353358Sdim  return getContext().pImpl->GlobalValuePartitions[this];
192353358Sdim}
193353358Sdim
194353358Sdimvoid GlobalValue::setPartition(StringRef S) {
195353358Sdim  // Do nothing if we're clearing the partition and it is already empty.
196353358Sdim  if (!hasPartition() && S.empty())
197353358Sdim    return;
198353358Sdim
199353358Sdim  // Get or create a stable partition name string and put it in the table in the
200353358Sdim  // context.
201353358Sdim  if (!S.empty())
202353358Sdim    S = getContext().pImpl->Saver.save(S);
203353358Sdim  getContext().pImpl->GlobalValuePartitions[this] = S;
204353358Sdim
205353358Sdim  // Update the HasPartition field. Setting the partition to the empty string
206353358Sdim  // means this global no longer has a partition.
207353358Sdim  HasPartition = !S.empty();
208353358Sdim}
209353358Sdim
210314564SdimStringRef GlobalObject::getSectionImpl() const {
211314564Sdim  assert(hasSection());
212314564Sdim  return getContext().pImpl->GlobalObjectSections[this];
213314564Sdim}
214314564Sdim
215309124Sdimvoid GlobalObject::setSection(StringRef S) {
216314564Sdim  // Do nothing if we're clearing the section and it is already empty.
217314564Sdim  if (!hasSection() && S.empty())
218314564Sdim    return;
219276479Sdim
220314564Sdim  // Get or create a stable section name string and put it in the table in the
221314564Sdim  // context.
222353358Sdim  if (!S.empty())
223353358Sdim    S = getContext().pImpl->Saver.save(S);
224314564Sdim  getContext().pImpl->GlobalObjectSections[this] = S;
225314564Sdim
226314564Sdim  // Update the HasSectionHashEntryBit. Setting the section to the empty string
227314564Sdim  // means this global no longer has a section.
228314564Sdim  setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
229309124Sdim}
230309124Sdim
231249259Sdimbool GlobalValue::isDeclaration() const {
232249259Sdim  // Globals are definitions if they have an initializer.
233249259Sdim  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
234249259Sdim    return GV->getNumOperands() == 0;
235249259Sdim
236249259Sdim  // Functions are definitions if they have a body.
237249259Sdim  if (const Function *F = dyn_cast<Function>(this))
238280031Sdim    return F->empty() && !F->isMaterializable();
239249259Sdim
240309124Sdim  // Aliases and ifuncs are always definitions.
241309124Sdim  assert(isa<GlobalIndirectSymbol>(this));
242249259Sdim  return false;
243249259Sdim}
244276479Sdim
245296417Sdimbool GlobalValue::canIncreaseAlignment() const {
246296417Sdim  // Firstly, can only increase the alignment of a global if it
247296417Sdim  // is a strong definition.
248296417Sdim  if (!isStrongDefinitionForLinker())
249296417Sdim    return false;
250296417Sdim
251296417Sdim  // It also has to either not have a section defined, or, not have
252296417Sdim  // alignment specified. (If it is assigned a section, the global
253296417Sdim  // could be densely packed with other objects in the section, and
254296417Sdim  // increasing the alignment could cause padding issues.)
255296417Sdim  if (hasSection() && getAlignment() > 0)
256296417Sdim    return false;
257296417Sdim
258296417Sdim  // On ELF platforms, we're further restricted in that we can't
259296417Sdim  // increase the alignment of any variable which might be emitted
260296417Sdim  // into a shared library, and which is exported. If the main
261296417Sdim  // executable accesses a variable found in a shared-lib, the main
262296417Sdim  // exe actually allocates memory for and exports the symbol ITSELF,
263296417Sdim  // overriding the symbol found in the library. That is, at link
264296417Sdim  // time, the observed alignment of the variable is copied into the
265296417Sdim  // executable binary. (A COPY relocation is also generated, to copy
266296417Sdim  // the initial data from the shadowed variable in the shared-lib
267296417Sdim  // into the location in the main binary, before running code.)
268296417Sdim  //
269296417Sdim  // And thus, even though you might think you are defining the
270296417Sdim  // global, and allocating the memory for the global in your object
271296417Sdim  // file, and thus should be able to set the alignment arbitrarily,
272296417Sdim  // that's not actually true. Doing so can cause an ABI breakage; an
273296417Sdim  // executable might have already been built with the previous
274296417Sdim  // alignment of the variable, and then assuming an increased
275296417Sdim  // alignment will be incorrect.
276296417Sdim
277296417Sdim  // Conservatively assume ELF if there's no parent pointer.
278296417Sdim  bool isELF =
279296417Sdim      (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF());
280344779Sdim  if (isELF && !isDSOLocal())
281296417Sdim    return false;
282296417Sdim
283296417Sdim  return true;
284296417Sdim}
285296417Sdim
286321369Sdimconst GlobalObject *GlobalValue::getBaseObject() const {
287314564Sdim  if (auto *GO = dyn_cast<GlobalObject>(this))
288314564Sdim    return GO;
289321369Sdim  if (auto *GA = dyn_cast<GlobalIndirectSymbol>(this))
290314564Sdim    return GA->getBaseObject();
291314564Sdim  return nullptr;
292314564Sdim}
293314564Sdim
294314564Sdimbool GlobalValue::isAbsoluteSymbolRef() const {
295314564Sdim  auto *GO = dyn_cast<GlobalObject>(this);
296314564Sdim  if (!GO)
297314564Sdim    return false;
298314564Sdim
299314564Sdim  return GO->getMetadata(LLVMContext::MD_absolute_symbol);
300314564Sdim}
301314564Sdim
302314564SdimOptional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const {
303314564Sdim  auto *GO = dyn_cast<GlobalObject>(this);
304314564Sdim  if (!GO)
305314564Sdim    return None;
306314564Sdim
307314564Sdim  MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
308314564Sdim  if (!MD)
309314564Sdim    return None;
310314564Sdim
311314564Sdim  return getConstantRangeFromMetadata(*MD);
312314564Sdim}
313314564Sdim
314341825Sdimbool GlobalValue::canBeOmittedFromSymbolTable() const {
315341825Sdim  if (!hasLinkOnceODRLinkage())
316341825Sdim    return false;
317341825Sdim
318341825Sdim  // We assume that anyone who sets global unnamed_addr on a non-constant
319341825Sdim  // knows what they're doing.
320341825Sdim  if (hasGlobalUnnamedAddr())
321341825Sdim    return true;
322341825Sdim
323341825Sdim  // If it is a non constant variable, it needs to be uniqued across shared
324341825Sdim  // objects.
325341825Sdim  if (auto *Var = dyn_cast<GlobalVariable>(this))
326341825Sdim    if (!Var->isConstant())
327341825Sdim      return false;
328341825Sdim
329341825Sdim  return hasAtLeastLocalUnnamedAddr();
330341825Sdim}
331341825Sdim
332249259Sdim//===----------------------------------------------------------------------===//
333249259Sdim// GlobalVariable Implementation
334249259Sdim//===----------------------------------------------------------------------===//
335249259Sdim
336249259SdimGlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
337276479Sdim                               Constant *InitVal, const Twine &Name,
338276479Sdim                               ThreadLocalMode TLMode, unsigned AddressSpace,
339249259Sdim                               bool isExternallyInitialized)
340296417Sdim    : GlobalObject(Ty, Value::GlobalVariableVal,
341276479Sdim                   OperandTraits<GlobalVariable>::op_begin(this),
342296417Sdim                   InitVal != nullptr, Link, Name, AddressSpace),
343276479Sdim      isConstantGlobal(constant),
344276479Sdim      isExternallyInitializedConstant(isExternallyInitialized) {
345321369Sdim  assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
346321369Sdim         "invalid type for global variable");
347276479Sdim  setThreadLocalMode(TLMode);
348249259Sdim  if (InitVal) {
349249259Sdim    assert(InitVal->getType() == Ty &&
350249259Sdim           "Initializer should be the same type as the GlobalVariable!");
351249259Sdim    Op<0>() = InitVal;
352249259Sdim  }
353249259Sdim}
354249259Sdim
355249259SdimGlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
356249259Sdim                               LinkageTypes Link, Constant *InitVal,
357276479Sdim                               const Twine &Name, GlobalVariable *Before,
358276479Sdim                               ThreadLocalMode TLMode, unsigned AddressSpace,
359249259Sdim                               bool isExternallyInitialized)
360296417Sdim    : GlobalObject(Ty, Value::GlobalVariableVal,
361276479Sdim                   OperandTraits<GlobalVariable>::op_begin(this),
362296417Sdim                   InitVal != nullptr, Link, Name, AddressSpace),
363276479Sdim      isConstantGlobal(constant),
364276479Sdim      isExternallyInitializedConstant(isExternallyInitialized) {
365321369Sdim  assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
366321369Sdim         "invalid type for global variable");
367276479Sdim  setThreadLocalMode(TLMode);
368249259Sdim  if (InitVal) {
369249259Sdim    assert(InitVal->getType() == Ty &&
370249259Sdim           "Initializer should be the same type as the GlobalVariable!");
371249259Sdim    Op<0>() = InitVal;
372249259Sdim  }
373276479Sdim
374249259Sdim  if (Before)
375296417Sdim    Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
376249259Sdim  else
377249259Sdim    M.getGlobalList().push_back(this);
378249259Sdim}
379249259Sdim
380249259Sdimvoid GlobalVariable::removeFromParent() {
381296417Sdim  getParent()->getGlobalList().remove(getIterator());
382249259Sdim}
383249259Sdim
384249259Sdimvoid GlobalVariable::eraseFromParent() {
385296417Sdim  getParent()->getGlobalList().erase(getIterator());
386249259Sdim}
387249259Sdim
388249259Sdimvoid GlobalVariable::setInitializer(Constant *InitVal) {
389276479Sdim  if (!InitVal) {
390249259Sdim    if (hasInitializer()) {
391288943Sdim      // Note, the num operands is used to compute the offset of the operand, so
392288943Sdim      // the order here matters.  Clearing the operand then clearing the num
393288943Sdim      // operands ensures we have the correct offset to the operand.
394276479Sdim      Op<0>().set(nullptr);
395288943Sdim      setGlobalVariableNumOperands(0);
396249259Sdim    }
397249259Sdim  } else {
398309124Sdim    assert(InitVal->getType() == getValueType() &&
399249259Sdim           "Initializer type must match GlobalVariable type");
400288943Sdim    // Note, the num operands is used to compute the offset of the operand, so
401288943Sdim    // the order here matters.  We need to set num operands to 1 first so that
402288943Sdim    // we get the correct offset to the first operand when we set it.
403249259Sdim    if (!hasInitializer())
404288943Sdim      setGlobalVariableNumOperands(1);
405249259Sdim    Op<0>().set(InitVal);
406249259Sdim  }
407249259Sdim}
408249259Sdim
409296417Sdim/// Copy all additional attributes (those not needed to create a GlobalVariable)
410296417Sdim/// from the GlobalVariable Src to this one.
411321369Sdimvoid GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) {
412276479Sdim  GlobalObject::copyAttributesFrom(Src);
413321369Sdim  setThreadLocalMode(Src->getThreadLocalMode());
414321369Sdim  setExternallyInitialized(Src->isExternallyInitialized());
415321369Sdim  setAttributes(Src->getAttributes());
416249259Sdim}
417249259Sdim
418309124Sdimvoid GlobalVariable::dropAllReferences() {
419309124Sdim  User::dropAllReferences();
420309124Sdim  clearMetadata();
421309124Sdim}
422249259Sdim
423249259Sdim//===----------------------------------------------------------------------===//
424309124Sdim// GlobalIndirectSymbol Implementation
425309124Sdim//===----------------------------------------------------------------------===//
426309124Sdim
427309124SdimGlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy,
428309124Sdim    unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name,
429309124Sdim    Constant *Symbol)
430309124Sdim    : GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) {
431309124Sdim    Op<0>() = Symbol;
432309124Sdim}
433309124Sdim
434360784Sdimstatic const GlobalObject *
435360784SdimfindBaseObject(const Constant *C, DenseSet<const GlobalAlias *> &Aliases) {
436360784Sdim  if (auto *GO = dyn_cast<GlobalObject>(C))
437360784Sdim    return GO;
438360784Sdim  if (auto *GA = dyn_cast<GlobalAlias>(C))
439360784Sdim    if (Aliases.insert(GA).second)
440360784Sdim      return findBaseObject(GA->getOperand(0), Aliases);
441360784Sdim  if (auto *CE = dyn_cast<ConstantExpr>(C)) {
442360784Sdim    switch (CE->getOpcode()) {
443360784Sdim    case Instruction::Add: {
444360784Sdim      auto *LHS = findBaseObject(CE->getOperand(0), Aliases);
445360784Sdim      auto *RHS = findBaseObject(CE->getOperand(1), Aliases);
446360784Sdim      if (LHS && RHS)
447360784Sdim        return nullptr;
448360784Sdim      return LHS ? LHS : RHS;
449360784Sdim    }
450360784Sdim    case Instruction::Sub: {
451360784Sdim      if (findBaseObject(CE->getOperand(1), Aliases))
452360784Sdim        return nullptr;
453360784Sdim      return findBaseObject(CE->getOperand(0), Aliases);
454360784Sdim    }
455360784Sdim    case Instruction::IntToPtr:
456360784Sdim    case Instruction::PtrToInt:
457360784Sdim    case Instruction::BitCast:
458360784Sdim    case Instruction::GetElementPtr:
459360784Sdim      return findBaseObject(CE->getOperand(0), Aliases);
460360784Sdim    default:
461360784Sdim      break;
462360784Sdim    }
463360784Sdim  }
464360784Sdim  return nullptr;
465360784Sdim}
466309124Sdim
467360784Sdimconst GlobalObject *GlobalIndirectSymbol::getBaseObject() const {
468360784Sdim  DenseSet<const GlobalAlias *> Aliases;
469360784Sdim  return findBaseObject(getOperand(0), Aliases);
470360784Sdim}
471360784Sdim
472309124Sdim//===----------------------------------------------------------------------===//
473249259Sdim// GlobalAlias Implementation
474249259Sdim//===----------------------------------------------------------------------===//
475249259Sdim
476296417SdimGlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
477296417Sdim                         const Twine &Name, Constant *Aliasee,
478296417Sdim                         Module *ParentModule)
479309124Sdim    : GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name,
480309124Sdim                           Aliasee) {
481249259Sdim  if (ParentModule)
482249259Sdim    ParentModule->getAliasList().push_back(this);
483249259Sdim}
484249259Sdim
485296417SdimGlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
486296417Sdim                                 LinkageTypes Link, const Twine &Name,
487296417Sdim                                 Constant *Aliasee, Module *ParentModule) {
488296417Sdim  return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
489276479Sdim}
490276479Sdim
491296417SdimGlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
492296417Sdim                                 LinkageTypes Linkage, const Twine &Name,
493296417Sdim                                 Module *Parent) {
494296417Sdim  return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
495276479Sdim}
496276479Sdim
497296417SdimGlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
498296417Sdim                                 LinkageTypes Linkage, const Twine &Name,
499296417Sdim                                 GlobalValue *Aliasee) {
500296417Sdim  return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
501276479Sdim}
502276479Sdim
503276479SdimGlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
504276479Sdim                                 GlobalValue *Aliasee) {
505276479Sdim  PointerType *PTy = Aliasee->getType();
506296417Sdim  return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
507296417Sdim                Aliasee);
508276479Sdim}
509276479Sdim
510276479SdimGlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
511276479Sdim  return create(Aliasee->getLinkage(), Name, Aliasee);
512276479Sdim}
513276479Sdim
514249259Sdimvoid GlobalAlias::removeFromParent() {
515296417Sdim  getParent()->getAliasList().remove(getIterator());
516249259Sdim}
517249259Sdim
518249259Sdimvoid GlobalAlias::eraseFromParent() {
519296417Sdim  getParent()->getAliasList().erase(getIterator());
520249259Sdim}
521249259Sdim
522249259Sdimvoid GlobalAlias::setAliasee(Constant *Aliasee) {
523249259Sdim  assert((!Aliasee || Aliasee->getType() == getType()) &&
524249259Sdim         "Alias and aliasee types should match!");
525309124Sdim  setIndirectSymbol(Aliasee);
526249259Sdim}
527309124Sdim
528309124Sdim//===----------------------------------------------------------------------===//
529309124Sdim// GlobalIFunc Implementation
530309124Sdim//===----------------------------------------------------------------------===//
531309124Sdim
532309124SdimGlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
533309124Sdim                         const Twine &Name, Constant *Resolver,
534309124Sdim                         Module *ParentModule)
535309124Sdim    : GlobalIndirectSymbol(Ty, Value::GlobalIFuncVal, AddressSpace, Link, Name,
536309124Sdim                           Resolver) {
537309124Sdim  if (ParentModule)
538309124Sdim    ParentModule->getIFuncList().push_back(this);
539309124Sdim}
540309124Sdim
541309124SdimGlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace,
542309124Sdim                                 LinkageTypes Link, const Twine &Name,
543309124Sdim                                 Constant *Resolver, Module *ParentModule) {
544309124Sdim  return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule);
545309124Sdim}
546309124Sdim
547309124Sdimvoid GlobalIFunc::removeFromParent() {
548309124Sdim  getParent()->getIFuncList().remove(getIterator());
549309124Sdim}
550309124Sdim
551309124Sdimvoid GlobalIFunc::eraseFromParent() {
552309124Sdim  getParent()->getIFuncList().erase(getIterator());
553309124Sdim}
554