llvm.org GIT mirror llvm / c130a20
De-virtualize GlobalValue The erase/remove from parent methods now use a switch table to remove themselves from their appropriate parent ilist. The copyAttributesFrom method is now completely non-virtual, since we only ever copy attributes from a global of the appropriate type. Pre-requisite to de-virtualizing Value to save a vptr (https://reviews.llvm.org/D31261). NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302823 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 2 years ago
9 changed file(s) with 80 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
493493
494494 /// copyAttributesFrom - copy all additional attributes (those not needed to
495495 /// create a Function) from the Function Src to this one.
496 void copyAttributesFrom(const GlobalValue *Src) override;
496 void copyAttributesFrom(const Function *Src);
497497
498498 /// deleteBody - This method deletes the body of the function, and converts
499499 /// the linkage to external.
506506 /// removeFromParent - This method unlinks 'this' from the containing module,
507507 /// but does not delete it.
508508 ///
509 void removeFromParent() override;
509 void removeFromParent();
510510
511511 /// eraseFromParent - This method unlinks 'this' from the containing module
512512 /// and deletes it.
513513 ///
514 void eraseFromParent() override;
514 void eraseFromParent();
515515
516516 /// Steal arguments from another function.
517517 ///
5858 // Linkage, Type, Parent and AddressSpace taken from the Aliasee.
5959 static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee);
6060
61 void copyAttributesFrom(const GlobalValue *Src) {
62 GlobalValue::copyAttributesFrom(Src);
63 }
64
6165 /// removeFromParent - This method unlinks 'this' from the containing module,
6266 /// but does not delete it.
6367 ///
64 void removeFromParent() override;
68 void removeFromParent();
6569
6670 /// eraseFromParent - This method unlinks 'this' from the containing module
6771 /// and deletes it.
6872 ///
69 void eraseFromParent() override;
73 void eraseFromParent();
7074
7175 /// These methods retrieve and set alias target.
7276 void setAliasee(Constant *Aliasee);
4646 LinkageTypes Linkage, const Twine &Name,
4747 Constant *Resolver, Module *Parent);
4848
49 void copyAttributesFrom(const GlobalIFunc *Src) {
50 GlobalValue::copyAttributesFrom(Src);
51 }
52
4953 /// This method unlinks 'this' from the containing module, but does not
5054 /// delete it.
51 void removeFromParent() final;
55 void removeFromParent();
5256
5357 /// This method unlinks 'this' from the containing module and deletes it.
54 void eraseFromParent() final;
58 void eraseFromParent();
5559
5660 /// These methods retrieve and set ifunc resolver function.
5761 void setResolver(Constant *Resolver) {
149149
150150 void addTypeMetadata(unsigned Offset, Metadata *TypeID);
151151
152 void copyAttributesFrom(const GlobalValue *Src) override;
152 protected:
153 void copyAttributesFrom(const GlobalObject *Src);
153154
155 public:
154156 // Methods for support type inquiry through isa, cast, and dyn_cast:
155157 static inline bool classof(const Value *V) {
156158 return V->getValueID() == Value::FunctionVal ||
434434
435435 bool isWeakForLinker() const { return isWeakForLinker(getLinkage()); }
436436
437 protected:
437438 /// Copy all additional attributes (those not needed to create a GlobalValue)
438439 /// from the GlobalValue Src to this one.
439 virtual void copyAttributesFrom(const GlobalValue *Src);
440
440 void copyAttributesFrom(const GlobalValue *Src);
441
442 public:
441443 /// If special LLVM prefix that is used to inform the asm printer to not emit
442444 /// usual symbol prefix before the symbol name is used then return linkage
443445 /// name after skipping this special LLVM prefix.
529531
530532 /// This method unlinks 'this' from the containing module, but does not delete
531533 /// it.
532 virtual void removeFromParent() = 0;
534 void removeFromParent();
533535
534536 /// This method unlinks 'this' from the containing module and deletes it.
535 virtual void eraseFromParent() = 0;
537 void eraseFromParent();
536538
537539 /// Get the module that this global value is contained inside of...
538540 Module *getParent() { return Parent; }
157157
158158 /// copyAttributesFrom - copy all additional attributes (those not needed to
159159 /// create a GlobalVariable) from the GlobalVariable Src to this one.
160 void copyAttributesFrom(const GlobalValue *Src) override;
160 void copyAttributesFrom(const GlobalVariable *Src);
161161
162162 /// removeFromParent - This method unlinks 'this' from the containing module,
163163 /// but does not delete it.
164164 ///
165 void removeFromParent() override;
165 void removeFromParent();
166166
167167 /// eraseFromParent - This method unlinks 'this' from the containing module
168168 /// and deletes it.
169169 ///
170 void eraseFromParent() override;
170 void eraseFromParent();
171171
172172 /// Drop all references in preparation to destroy the GlobalVariable. This
173173 /// drops not only the reference to the initializer but also to any metadata.
415415
416416 /// Copy all additional attributes (those not needed to create a Function) from
417417 /// the Function Src to this one.
418 void Function::copyAttributesFrom(const GlobalValue *Src) {
418 void Function::copyAttributesFrom(const Function *Src) {
419419 GlobalObject::copyAttributesFrom(Src);
420 const Function *SrcF = dyn_cast(Src);
421 if (!SrcF)
422 return;
423
424 setCallingConv(SrcF->getCallingConv());
425 setAttributes(SrcF->getAttributes());
426 if (SrcF->hasGC())
427 setGC(SrcF->getGC());
420 setCallingConv(Src->getCallingConv());
421 setAttributes(Src->getAttributes());
422 if (Src->hasGC())
423 setGC(Src->getGC());
428424 else
429425 clearGC();
430 if (SrcF->hasPersonalityFn())
431 setPersonalityFn(SrcF->getPersonalityFn());
432 if (SrcF->hasPrefixData())
433 setPrefixData(SrcF->getPrefixData());
434 if (SrcF->hasPrologueData())
435 setPrologueData(SrcF->getPrologueData());
426 if (Src->hasPersonalityFn())
427 setPersonalityFn(Src->getPersonalityFn());
428 if (Src->hasPrefixData())
429 setPrefixData(Src->getPrefixData());
430 if (Src->hasPrologueData())
431 setPrologueData(Src->getPrologueData());
436432 }
437433
438434 /// Table of string intrinsic names indexed by enum value.
6868 setDLLStorageClass(Src->getDLLStorageClass());
6969 }
7070
71 void GlobalValue::removeFromParent() {
72 switch (getValueID()) {
73 #define HANDLE_GLOBAL_VALUE(NAME) \
74 case Value::NAME##Val: \
75 return static_cast(this)->removeFromParent();
76 #include "llvm/IR/Value.def"
77 default:
78 break;
79 }
80 llvm_unreachable("not a global");
81 }
82
83 void GlobalValue::eraseFromParent() {
84 switch (getValueID()) {
85 #define HANDLE_GLOBAL_VALUE(NAME) \
86 case Value::NAME##Val: \
87 return static_cast(this)->eraseFromParent();
88 #include "llvm/IR/Value.def"
89 default:
90 break;
91 }
92 llvm_unreachable("not a global");
93 }
94
7195 unsigned GlobalValue::getAlignment() const {
7296 if (auto *GA = dyn_cast(this)) {
7397 // In general we cannot compute this at the IR level, but we try.
92116 assert(getAlignment() == Align && "Alignment representation error!");
93117 }
94118
95 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
119 void GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
96120 GlobalValue::copyAttributesFrom(Src);
97 if (const auto *GV = dyn_cast(Src)) {
98 setAlignment(GV->getAlignment());
99 setSection(GV->getSection());
100 }
121 setAlignment(Src->getAlignment());
122 setSection(Src->getSection());
101123 }
102124
103125 std::string GlobalValue::getGlobalIdentifier(StringRef Name,
332354
333355 /// Copy all additional attributes (those not needed to create a GlobalVariable)
334356 /// from the GlobalVariable Src to this one.
335 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
357 void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) {
336358 GlobalObject::copyAttributesFrom(Src);
337 if (const GlobalVariable *SrcVar = dyn_cast(Src)) {
338 setThreadLocalMode(SrcVar->getThreadLocalMode());
339 setExternallyInitialized(SrcVar->isExternallyInitialized());
340 setAttributes(SrcVar->getAttributes());
341 }
359 setThreadLocalMode(Src->getThreadLocalMode());
360 setExternallyInitialized(Src->isExternallyInitialized());
361 setAttributes(Src->getAttributes());
342362 }
343363
344364 void GlobalVariable::dropAllReferences() {
601601 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
602602 SGVar->getType()->getAddressSpace());
603603 NewDGV->setAlignment(SGVar->getAlignment());
604 NewDGV->copyAttributesFrom(SGVar);
604605 return NewDGV;
605606 }
606607
609610 Function *IRLinker::copyFunctionProto(const Function *SF) {
610611 // If there is no linkage to be performed or we are linking from the source,
611612 // bring SF over.
612 return Function::Create(TypeMap.get(SF->getFunctionType()),
613 GlobalValue::ExternalLinkage, SF->getName(), &DstM);
613 auto *F =
614 Function::Create(TypeMap.get(SF->getFunctionType()),
615 GlobalValue::ExternalLinkage, SF->getName(), &DstM);
616 F->copyAttributesFrom(SF);
617 return F;
614618 }
615619
616620 /// Set up prototypes for any aliases that come over from the source module.
618622 // If there is no linkage to be performed or we're linking from the source,
619623 // bring over SGA.
620624 auto *Ty = TypeMap.get(SGA->getValueType());
621 return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
622 GlobalValue::ExternalLinkage, SGA->getName(),
623 &DstM);
625 auto *GA =
626 GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
627 GlobalValue::ExternalLinkage, SGA->getName(), &DstM);
628 GA->copyAttributesFrom(SGA);
629 return GA;
624630 }
625631
626632 GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
646652 NewGV->setLinkage(SGV->getLinkage());
647653 else if (SGV->hasExternalWeakLinkage())
648654 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
649
650 NewGV->copyAttributesFrom(SGV);
651655
652656 if (auto *NewGO = dyn_cast(NewGV)) {
653657 // Metadata for global variables and function declarations is copied eagerly.