1//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the GlobalValue & GlobalVariable classes for the IR
10// library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLVMContextImpl.h"
15#include "llvm/IR/ConstantRange.h"
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/DerivedTypes.h"
18#include "llvm/IR/GlobalAlias.h"
19#include "llvm/IR/GlobalValue.h"
20#include "llvm/IR/GlobalVariable.h"
21#include "llvm/IR/Module.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/TargetParser/Triple.h"
25using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28//                            GlobalValue Class
29//===----------------------------------------------------------------------===//
30
31// GlobalValue should be a Constant, plus a type, a module, some flags, and an
32// intrinsic ID. Add an assert to prevent people from accidentally growing
33// GlobalValue while adding flags.
34static_assert(sizeof(GlobalValue) ==
35                  sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
36              "unexpected GlobalValue size growth");
37
38// GlobalObject adds a comdat.
39static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
40              "unexpected GlobalObject size growth");
41
42bool GlobalValue::isMaterializable() const {
43  if (const Function *F = dyn_cast<Function>(this))
44    return F->isMaterializable();
45  return false;
46}
47Error GlobalValue::materialize() { return getParent()->materialize(this); }
48
49/// Override destroyConstantImpl to make sure it doesn't get called on
50/// GlobalValue's because they shouldn't be treated like other constants.
51void GlobalValue::destroyConstantImpl() {
52  llvm_unreachable("You can't GV->destroyConstantImpl()!");
53}
54
55Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
56  llvm_unreachable("Unsupported class for handleOperandChange()!");
57}
58
59/// copyAttributesFrom - copy all additional attributes (those not needed to
60/// create a GlobalValue) from the GlobalValue Src to this one.
61void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
62  setVisibility(Src->getVisibility());
63  setUnnamedAddr(Src->getUnnamedAddr());
64  setThreadLocalMode(Src->getThreadLocalMode());
65  setDLLStorageClass(Src->getDLLStorageClass());
66  setDSOLocal(Src->isDSOLocal());
67  setPartition(Src->getPartition());
68  if (Src->hasSanitizerMetadata())
69    setSanitizerMetadata(Src->getSanitizerMetadata());
70  else
71    removeSanitizerMetadata();
72}
73
74void GlobalValue::removeFromParent() {
75  switch (getValueID()) {
76#define HANDLE_GLOBAL_VALUE(NAME)                                              \
77  case Value::NAME##Val:                                                       \
78    return static_cast<NAME *>(this)->removeFromParent();
79#include "llvm/IR/Value.def"
80  default:
81    break;
82  }
83  llvm_unreachable("not a global");
84}
85
86void GlobalValue::eraseFromParent() {
87  switch (getValueID()) {
88#define HANDLE_GLOBAL_VALUE(NAME)                                              \
89  case Value::NAME##Val:                                                       \
90    return static_cast<NAME *>(this)->eraseFromParent();
91#include "llvm/IR/Value.def"
92  default:
93    break;
94  }
95  llvm_unreachable("not a global");
96}
97
98GlobalObject::~GlobalObject() { setComdat(nullptr); }
99
100bool GlobalValue::isInterposable() const {
101  if (isInterposableLinkage(getLinkage()))
102    return true;
103  return getParent() && getParent()->getSemanticInterposition() &&
104         !isDSOLocal();
105}
106
107bool GlobalValue::canBenefitFromLocalAlias() const {
108  // See AsmPrinter::getSymbolPreferLocal(). For a deduplicate comdat kind,
109  // references to a discarded local symbol from outside the group are not
110  // allowed, so avoid the local alias.
111  auto isDeduplicateComdat = [](const Comdat *C) {
112    return C && C->getSelectionKind() != Comdat::NoDeduplicate;
113  };
114  return hasDefaultVisibility() &&
115         GlobalObject::isExternalLinkage(getLinkage()) && !isDeclaration() &&
116         !isa<GlobalIFunc>(this) && !isDeduplicateComdat(getComdat());
117}
118
119void GlobalObject::setAlignment(MaybeAlign Align) {
120  assert((!Align || *Align <= MaximumAlignment) &&
121         "Alignment is greater than MaximumAlignment!");
122  unsigned AlignmentData = encode(Align);
123  unsigned OldData = getGlobalValueSubClassData();
124  setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
125  assert(getAlign() == Align && "Alignment representation error!");
126}
127
128void GlobalObject::setAlignment(Align Align) {
129  assert(Align <= MaximumAlignment &&
130         "Alignment is greater than MaximumAlignment!");
131  unsigned AlignmentData = encode(Align);
132  unsigned OldData = getGlobalValueSubClassData();
133  setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
134  assert(getAlign() && *getAlign() == Align &&
135         "Alignment representation error!");
136}
137
138void GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
139  GlobalValue::copyAttributesFrom(Src);
140  setAlignment(Src->getAlign());
141  setSection(Src->getSection());
142}
143
144std::string GlobalValue::getGlobalIdentifier(StringRef Name,
145                                             GlobalValue::LinkageTypes Linkage,
146                                             StringRef FileName) {
147  // Value names may be prefixed with a binary '1' to indicate
148  // that the backend should not modify the symbols due to any platform
149  // naming convention. Do not include that '1' in the PGO profile name.
150  Name.consume_front("\1");
151
152  std::string GlobalName;
153  if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
154    // For local symbols, prepend the main file name to distinguish them.
155    // Do not include the full path in the file name since there's no guarantee
156    // that it will stay the same, e.g., if the files are checked out from
157    // version control in different locations.
158    if (FileName.empty())
159      GlobalName += "<unknown>";
160    else
161      GlobalName += FileName;
162
163    GlobalName += kGlobalIdentifierDelimiter;
164  }
165  GlobalName += Name;
166  return GlobalName;
167}
168
169std::string GlobalValue::getGlobalIdentifier() const {
170  return getGlobalIdentifier(getName(), getLinkage(),
171                             getParent()->getSourceFileName());
172}
173
174StringRef GlobalValue::getSection() const {
175  if (auto *GA = dyn_cast<GlobalAlias>(this)) {
176    // In general we cannot compute this at the IR level, but we try.
177    if (const GlobalObject *GO = GA->getAliaseeObject())
178      return GO->getSection();
179    return "";
180  }
181  return cast<GlobalObject>(this)->getSection();
182}
183
184const Comdat *GlobalValue::getComdat() const {
185  if (auto *GA = dyn_cast<GlobalAlias>(this)) {
186    // In general we cannot compute this at the IR level, but we try.
187    if (const GlobalObject *GO = GA->getAliaseeObject())
188      return const_cast<GlobalObject *>(GO)->getComdat();
189    return nullptr;
190  }
191  // ifunc and its resolver are separate things so don't use resolver comdat.
192  if (isa<GlobalIFunc>(this))
193    return nullptr;
194  return cast<GlobalObject>(this)->getComdat();
195}
196
197void GlobalObject::setComdat(Comdat *C) {
198  if (ObjComdat)
199    ObjComdat->removeUser(this);
200  ObjComdat = C;
201  if (C)
202    C->addUser(this);
203}
204
205StringRef GlobalValue::getPartition() const {
206  if (!hasPartition())
207    return "";
208  return getContext().pImpl->GlobalValuePartitions[this];
209}
210
211void GlobalValue::setPartition(StringRef S) {
212  // Do nothing if we're clearing the partition and it is already empty.
213  if (!hasPartition() && S.empty())
214    return;
215
216  // Get or create a stable partition name string and put it in the table in the
217  // context.
218  if (!S.empty())
219    S = getContext().pImpl->Saver.save(S);
220  getContext().pImpl->GlobalValuePartitions[this] = S;
221
222  // Update the HasPartition field. Setting the partition to the empty string
223  // means this global no longer has a partition.
224  HasPartition = !S.empty();
225}
226
227using SanitizerMetadata = GlobalValue::SanitizerMetadata;
228const SanitizerMetadata &GlobalValue::getSanitizerMetadata() const {
229  assert(hasSanitizerMetadata());
230  assert(getContext().pImpl->GlobalValueSanitizerMetadata.count(this));
231  return getContext().pImpl->GlobalValueSanitizerMetadata[this];
232}
233
234void GlobalValue::setSanitizerMetadata(SanitizerMetadata Meta) {
235  getContext().pImpl->GlobalValueSanitizerMetadata[this] = Meta;
236  HasSanitizerMetadata = true;
237}
238
239void GlobalValue::removeSanitizerMetadata() {
240  DenseMap<const GlobalValue *, SanitizerMetadata> &MetadataMap =
241      getContext().pImpl->GlobalValueSanitizerMetadata;
242  MetadataMap.erase(this);
243  HasSanitizerMetadata = false;
244}
245
246StringRef GlobalObject::getSectionImpl() const {
247  assert(hasSection());
248  return getContext().pImpl->GlobalObjectSections[this];
249}
250
251void GlobalObject::setSection(StringRef S) {
252  // Do nothing if we're clearing the section and it is already empty.
253  if (!hasSection() && S.empty())
254    return;
255
256  // Get or create a stable section name string and put it in the table in the
257  // context.
258  if (!S.empty())
259    S = getContext().pImpl->Saver.save(S);
260  getContext().pImpl->GlobalObjectSections[this] = S;
261
262  // Update the HasSectionHashEntryBit. Setting the section to the empty string
263  // means this global no longer has a section.
264  setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
265}
266
267bool GlobalValue::isNobuiltinFnDef() const {
268  const Function *F = dyn_cast<Function>(this);
269  if (!F || F->empty())
270    return false;
271  return F->hasFnAttribute(Attribute::NoBuiltin);
272}
273
274bool GlobalValue::isDeclaration() const {
275  // Globals are definitions if they have an initializer.
276  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
277    return GV->getNumOperands() == 0;
278
279  // Functions are definitions if they have a body.
280  if (const Function *F = dyn_cast<Function>(this))
281    return F->empty() && !F->isMaterializable();
282
283  // Aliases and ifuncs are always definitions.
284  assert(isa<GlobalAlias>(this) || isa<GlobalIFunc>(this));
285  return false;
286}
287
288bool GlobalObject::canIncreaseAlignment() const {
289  // Firstly, can only increase the alignment of a global if it
290  // is a strong definition.
291  if (!isStrongDefinitionForLinker())
292    return false;
293
294  // It also has to either not have a section defined, or, not have
295  // alignment specified. (If it is assigned a section, the global
296  // could be densely packed with other objects in the section, and
297  // increasing the alignment could cause padding issues.)
298  if (hasSection() && getAlign())
299    return false;
300
301  // On ELF platforms, we're further restricted in that we can't
302  // increase the alignment of any variable which might be emitted
303  // into a shared library, and which is exported. If the main
304  // executable accesses a variable found in a shared-lib, the main
305  // exe actually allocates memory for and exports the symbol ITSELF,
306  // overriding the symbol found in the library. That is, at link
307  // time, the observed alignment of the variable is copied into the
308  // executable binary. (A COPY relocation is also generated, to copy
309  // the initial data from the shadowed variable in the shared-lib
310  // into the location in the main binary, before running code.)
311  //
312  // And thus, even though you might think you are defining the
313  // global, and allocating the memory for the global in your object
314  // file, and thus should be able to set the alignment arbitrarily,
315  // that's not actually true. Doing so can cause an ABI breakage; an
316  // executable might have already been built with the previous
317  // alignment of the variable, and then assuming an increased
318  // alignment will be incorrect.
319
320  // Conservatively assume ELF if there's no parent pointer.
321  bool isELF =
322      (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF());
323  if (isELF && !isDSOLocal())
324    return false;
325
326  return true;
327}
328
329template <typename Operation>
330static const GlobalObject *
331findBaseObject(const Constant *C, DenseSet<const GlobalAlias *> &Aliases,
332               const Operation &Op) {
333  if (auto *GO = dyn_cast<GlobalObject>(C)) {
334    Op(*GO);
335    return GO;
336  }
337  if (auto *GA = dyn_cast<GlobalAlias>(C)) {
338    Op(*GA);
339    if (Aliases.insert(GA).second)
340      return findBaseObject(GA->getOperand(0), Aliases, Op);
341  }
342  if (auto *CE = dyn_cast<ConstantExpr>(C)) {
343    switch (CE->getOpcode()) {
344    case Instruction::Add: {
345      auto *LHS = findBaseObject(CE->getOperand(0), Aliases, Op);
346      auto *RHS = findBaseObject(CE->getOperand(1), Aliases, Op);
347      if (LHS && RHS)
348        return nullptr;
349      return LHS ? LHS : RHS;
350    }
351    case Instruction::Sub: {
352      if (findBaseObject(CE->getOperand(1), Aliases, Op))
353        return nullptr;
354      return findBaseObject(CE->getOperand(0), Aliases, Op);
355    }
356    case Instruction::IntToPtr:
357    case Instruction::PtrToInt:
358    case Instruction::BitCast:
359    case Instruction::GetElementPtr:
360      return findBaseObject(CE->getOperand(0), Aliases, Op);
361    default:
362      break;
363    }
364  }
365  return nullptr;
366}
367
368const GlobalObject *GlobalValue::getAliaseeObject() const {
369  DenseSet<const GlobalAlias *> Aliases;
370  return findBaseObject(this, Aliases, [](const GlobalValue &) {});
371}
372
373bool GlobalValue::isAbsoluteSymbolRef() const {
374  auto *GO = dyn_cast<GlobalObject>(this);
375  if (!GO)
376    return false;
377
378  return GO->getMetadata(LLVMContext::MD_absolute_symbol);
379}
380
381std::optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const {
382  auto *GO = dyn_cast<GlobalObject>(this);
383  if (!GO)
384    return std::nullopt;
385
386  MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
387  if (!MD)
388    return std::nullopt;
389
390  return getConstantRangeFromMetadata(*MD);
391}
392
393bool GlobalValue::canBeOmittedFromSymbolTable() const {
394  if (!hasLinkOnceODRLinkage())
395    return false;
396
397  // We assume that anyone who sets global unnamed_addr on a non-constant
398  // knows what they're doing.
399  if (hasGlobalUnnamedAddr())
400    return true;
401
402  // If it is a non constant variable, it needs to be uniqued across shared
403  // objects.
404  if (auto *Var = dyn_cast<GlobalVariable>(this))
405    if (!Var->isConstant())
406      return false;
407
408  return hasAtLeastLocalUnnamedAddr();
409}
410
411//===----------------------------------------------------------------------===//
412// GlobalVariable Implementation
413//===----------------------------------------------------------------------===//
414
415GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
416                               Constant *InitVal, const Twine &Name,
417                               ThreadLocalMode TLMode, unsigned AddressSpace,
418                               bool isExternallyInitialized)
419    : GlobalObject(Ty, Value::GlobalVariableVal,
420                   OperandTraits<GlobalVariable>::op_begin(this),
421                   InitVal != nullptr, Link, Name, AddressSpace),
422      isConstantGlobal(constant),
423      isExternallyInitializedConstant(isExternallyInitialized) {
424  assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
425         "invalid type for global variable");
426  setThreadLocalMode(TLMode);
427  if (InitVal) {
428    assert(InitVal->getType() == Ty &&
429           "Initializer should be the same type as the GlobalVariable!");
430    Op<0>() = InitVal;
431  }
432}
433
434GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
435                               LinkageTypes Link, Constant *InitVal,
436                               const Twine &Name, GlobalVariable *Before,
437                               ThreadLocalMode TLMode,
438                               std::optional<unsigned> AddressSpace,
439                               bool isExternallyInitialized)
440    : GlobalVariable(Ty, constant, Link, InitVal, Name, TLMode,
441                     AddressSpace
442                         ? *AddressSpace
443                         : M.getDataLayout().getDefaultGlobalsAddressSpace(),
444                     isExternallyInitialized) {
445  if (Before)
446    Before->getParent()->insertGlobalVariable(Before->getIterator(), this);
447  else
448    M.insertGlobalVariable(this);
449}
450
451void GlobalVariable::removeFromParent() {
452  getParent()->removeGlobalVariable(this);
453}
454
455void GlobalVariable::eraseFromParent() {
456  getParent()->eraseGlobalVariable(this);
457}
458
459void GlobalVariable::setInitializer(Constant *InitVal) {
460  if (!InitVal) {
461    if (hasInitializer()) {
462      // Note, the num operands is used to compute the offset of the operand, so
463      // the order here matters.  Clearing the operand then clearing the num
464      // operands ensures we have the correct offset to the operand.
465      Op<0>().set(nullptr);
466      setGlobalVariableNumOperands(0);
467    }
468  } else {
469    assert(InitVal->getType() == getValueType() &&
470           "Initializer type must match GlobalVariable type");
471    // Note, the num operands is used to compute the offset of the operand, so
472    // the order here matters.  We need to set num operands to 1 first so that
473    // we get the correct offset to the first operand when we set it.
474    if (!hasInitializer())
475      setGlobalVariableNumOperands(1);
476    Op<0>().set(InitVal);
477  }
478}
479
480/// Copy all additional attributes (those not needed to create a GlobalVariable)
481/// from the GlobalVariable Src to this one.
482void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) {
483  GlobalObject::copyAttributesFrom(Src);
484  setExternallyInitialized(Src->isExternallyInitialized());
485  setAttributes(Src->getAttributes());
486  if (auto CM = Src->getCodeModel())
487    setCodeModel(*CM);
488}
489
490void GlobalVariable::dropAllReferences() {
491  User::dropAllReferences();
492  clearMetadata();
493}
494
495void GlobalVariable::setCodeModel(CodeModel::Model CM) {
496  unsigned CodeModelData = static_cast<unsigned>(CM) + 1;
497  unsigned OldData = getGlobalValueSubClassData();
498  unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
499                     (CodeModelData << CodeModelShift);
500  setGlobalValueSubClassData(NewData);
501  assert(getCodeModel() == CM && "Code model representation error!");
502}
503
504//===----------------------------------------------------------------------===//
505// GlobalAlias Implementation
506//===----------------------------------------------------------------------===//
507
508GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
509                         const Twine &Name, Constant *Aliasee,
510                         Module *ParentModule)
511    : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name,
512                  AddressSpace) {
513  setAliasee(Aliasee);
514  if (ParentModule)
515    ParentModule->insertAlias(this);
516}
517
518GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
519                                 LinkageTypes Link, const Twine &Name,
520                                 Constant *Aliasee, Module *ParentModule) {
521  return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
522}
523
524GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
525                                 LinkageTypes Linkage, const Twine &Name,
526                                 Module *Parent) {
527  return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
528}
529
530GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
531                                 LinkageTypes Linkage, const Twine &Name,
532                                 GlobalValue *Aliasee) {
533  return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
534}
535
536GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
537                                 GlobalValue *Aliasee) {
538  return create(Aliasee->getValueType(), Aliasee->getAddressSpace(), Link, Name,
539                Aliasee);
540}
541
542GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
543  return create(Aliasee->getLinkage(), Name, Aliasee);
544}
545
546void GlobalAlias::removeFromParent() { getParent()->removeAlias(this); }
547
548void GlobalAlias::eraseFromParent() { getParent()->eraseAlias(this); }
549
550void GlobalAlias::setAliasee(Constant *Aliasee) {
551  assert((!Aliasee || Aliasee->getType() == getType()) &&
552         "Alias and aliasee types should match!");
553  Op<0>().set(Aliasee);
554}
555
556const GlobalObject *GlobalAlias::getAliaseeObject() const {
557  DenseSet<const GlobalAlias *> Aliases;
558  return findBaseObject(getOperand(0), Aliases, [](const GlobalValue &) {});
559}
560
561//===----------------------------------------------------------------------===//
562// GlobalIFunc Implementation
563//===----------------------------------------------------------------------===//
564
565GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
566                         const Twine &Name, Constant *Resolver,
567                         Module *ParentModule)
568    : GlobalObject(Ty, Value::GlobalIFuncVal, &Op<0>(), 1, Link, Name,
569                   AddressSpace) {
570  setResolver(Resolver);
571  if (ParentModule)
572    ParentModule->insertIFunc(this);
573}
574
575GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace,
576                                 LinkageTypes Link, const Twine &Name,
577                                 Constant *Resolver, Module *ParentModule) {
578  return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule);
579}
580
581void GlobalIFunc::removeFromParent() { getParent()->removeIFunc(this); }
582
583void GlobalIFunc::eraseFromParent() { getParent()->eraseIFunc(this); }
584
585const Function *GlobalIFunc::getResolverFunction() const {
586  return dyn_cast<Function>(getResolver()->stripPointerCastsAndAliases());
587}
588
589void GlobalIFunc::applyAlongResolverPath(
590    function_ref<void(const GlobalValue &)> Op) const {
591  DenseSet<const GlobalAlias *> Aliases;
592  findBaseObject(getResolver(), Aliases, Op);
593}
594