[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / IR / Globals.cpp
blob481a1d802e66b6aa8e749240c49a6359842de21c
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.
12 //===----------------------------------------------------------------------===//
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"
25 using namespace llvm;
27 //===----------------------------------------------------------------------===//
28 // GlobalValue Class
29 //===----------------------------------------------------------------------===//
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.
34 static_assert(sizeof(GlobalValue) ==
35 sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
36 "unexpected GlobalValue size growth");
38 // GlobalObject adds a comdat.
39 static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
40 "unexpected GlobalObject size growth");
42 bool GlobalValue::isMaterializable() const {
43 if (const Function *F = dyn_cast<Function>(this))
44 return F->isMaterializable();
45 return false;
47 Error GlobalValue::materialize() { return getParent()->materialize(this); }
49 /// Override destroyConstantImpl to make sure it doesn't get called on
50 /// GlobalValue's because they shouldn't be treated like other constants.
51 void GlobalValue::destroyConstantImpl() {
52 llvm_unreachable("You can't GV->destroyConstantImpl()!");
55 Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
56 llvm_unreachable("Unsupported class for handleOperandChange()!");
59 /// copyAttributesFrom - copy all additional attributes (those not needed to
60 /// create a GlobalValue) from the GlobalValue Src to this one.
61 void 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();
74 void 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;
83 llvm_unreachable("not a global");
86 void 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;
95 llvm_unreachable("not a global");
98 GlobalObject::~GlobalObject() { setComdat(nullptr); }
100 bool GlobalValue::isInterposable() const {
101 if (isInterposableLinkage(getLinkage()))
102 return true;
103 return getParent() && getParent()->getSemanticInterposition() &&
104 !isDSOLocal();
107 bool 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;
114 return hasDefaultVisibility() &&
115 GlobalObject::isExternalLinkage(getLinkage()) && !isDeclaration() &&
116 !isa<GlobalIFunc>(this) && !isDeduplicateComdat(getComdat());
119 void 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!");
128 void 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!");
138 void GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
139 GlobalValue::copyAttributesFrom(Src);
140 setAlignment(Src->getAlign());
141 setSection(Src->getSection());
144 std::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");
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;
163 GlobalName += kGlobalIdentifierDelimiter;
165 GlobalName += Name;
166 return GlobalName;
169 std::string GlobalValue::getGlobalIdentifier() const {
170 return getGlobalIdentifier(getName(), getLinkage(),
171 getParent()->getSourceFileName());
174 StringRef 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 "";
181 return cast<GlobalObject>(this)->getSection();
184 const 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;
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();
197 void GlobalObject::setComdat(Comdat *C) {
198 if (ObjComdat)
199 ObjComdat->removeUser(this);
200 ObjComdat = C;
201 if (C)
202 C->addUser(this);
205 StringRef GlobalValue::getPartition() const {
206 if (!hasPartition())
207 return "";
208 return getContext().pImpl->GlobalValuePartitions[this];
211 void 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;
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;
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();
227 using SanitizerMetadata = GlobalValue::SanitizerMetadata;
228 const SanitizerMetadata &GlobalValue::getSanitizerMetadata() const {
229 assert(hasSanitizerMetadata());
230 assert(getContext().pImpl->GlobalValueSanitizerMetadata.count(this));
231 return getContext().pImpl->GlobalValueSanitizerMetadata[this];
234 void GlobalValue::setSanitizerMetadata(SanitizerMetadata Meta) {
235 getContext().pImpl->GlobalValueSanitizerMetadata[this] = Meta;
236 HasSanitizerMetadata = true;
239 void GlobalValue::removeSanitizerMetadata() {
240 DenseMap<const GlobalValue *, SanitizerMetadata> &MetadataMap =
241 getContext().pImpl->GlobalValueSanitizerMetadata;
242 MetadataMap.erase(this);
243 HasSanitizerMetadata = false;
246 StringRef GlobalObject::getSectionImpl() const {
247 assert(hasSection());
248 return getContext().pImpl->GlobalObjectSections[this];
251 void 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;
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;
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());
267 bool 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);
274 bool 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;
279 // Functions are definitions if they have a body.
280 if (const Function *F = dyn_cast<Function>(this))
281 return F->empty() && !F->isMaterializable();
283 // Aliases and ifuncs are always definitions.
284 assert(isa<GlobalAlias>(this) || isa<GlobalIFunc>(this));
285 return false;
288 bool 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;
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;
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.)
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.
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;
326 return true;
329 template <typename Operation>
330 static const GlobalObject *
331 findBaseObject(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;
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);
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;
351 case Instruction::Sub: {
352 if (findBaseObject(CE->getOperand(1), Aliases, Op))
353 return nullptr;
354 return findBaseObject(CE->getOperand(0), Aliases, Op);
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;
365 return nullptr;
368 const GlobalObject *GlobalValue::getAliaseeObject() const {
369 DenseSet<const GlobalAlias *> Aliases;
370 return findBaseObject(this, Aliases, [](const GlobalValue &) {});
373 bool GlobalValue::isAbsoluteSymbolRef() const {
374 auto *GO = dyn_cast<GlobalObject>(this);
375 if (!GO)
376 return false;
378 return GO->getMetadata(LLVMContext::MD_absolute_symbol);
381 std::optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const {
382 auto *GO = dyn_cast<GlobalObject>(this);
383 if (!GO)
384 return std::nullopt;
386 MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
387 if (!MD)
388 return std::nullopt;
390 return getConstantRangeFromMetadata(*MD);
393 bool GlobalValue::canBeOmittedFromSymbolTable() const {
394 if (!hasLinkOnceODRLinkage())
395 return false;
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;
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;
408 return hasAtLeastLocalUnnamedAddr();
411 //===----------------------------------------------------------------------===//
412 // GlobalVariable Implementation
413 //===----------------------------------------------------------------------===//
415 GlobalVariable::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;
434 GlobalVariable::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);
451 void GlobalVariable::removeFromParent() {
452 getParent()->removeGlobalVariable(this);
455 void GlobalVariable::eraseFromParent() {
456 getParent()->eraseGlobalVariable(this);
459 void 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);
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);
480 /// Copy all additional attributes (those not needed to create a GlobalVariable)
481 /// from the GlobalVariable Src to this one.
482 void 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);
490 void GlobalVariable::dropAllReferences() {
491 User::dropAllReferences();
492 clearMetadata();
495 void 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!");
504 //===----------------------------------------------------------------------===//
505 // GlobalAlias Implementation
506 //===----------------------------------------------------------------------===//
508 GlobalAlias::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);
518 GlobalAlias *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);
524 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
525 LinkageTypes Linkage, const Twine &Name,
526 Module *Parent) {
527 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
530 GlobalAlias *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());
536 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
537 GlobalValue *Aliasee) {
538 return create(Aliasee->getValueType(), Aliasee->getAddressSpace(), Link, Name,
539 Aliasee);
542 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
543 return create(Aliasee->getLinkage(), Name, Aliasee);
546 void GlobalAlias::removeFromParent() { getParent()->removeAlias(this); }
548 void GlobalAlias::eraseFromParent() { getParent()->eraseAlias(this); }
550 void GlobalAlias::setAliasee(Constant *Aliasee) {
551 assert((!Aliasee || Aliasee->getType() == getType()) &&
552 "Alias and aliasee types should match!");
553 Op<0>().set(Aliasee);
556 const GlobalObject *GlobalAlias::getAliaseeObject() const {
557 DenseSet<const GlobalAlias *> Aliases;
558 return findBaseObject(getOperand(0), Aliases, [](const GlobalValue &) {});
561 //===----------------------------------------------------------------------===//
562 // GlobalIFunc Implementation
563 //===----------------------------------------------------------------------===//
565 GlobalIFunc::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);
575 GlobalIFunc *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);
581 void GlobalIFunc::removeFromParent() { getParent()->removeIFunc(this); }
583 void GlobalIFunc::eraseFromParent() { getParent()->eraseIFunc(this); }
585 const Function *GlobalIFunc::getResolverFunction() const {
586 return dyn_cast<Function>(getResolver()->stripPointerCastsAndAliases());
589 void GlobalIFunc::applyAlongResolverPath(
590 function_ref<void(const GlobalValue &)> Op) const {
591 DenseSet<const GlobalAlias *> Aliases;
592 findBaseObject(getResolver(), Aliases, Op);