From ab67b30df6b14e8e90f4778215ed4e1d5939638e Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 16 May 2014 13:34:04 +0000 Subject: [PATCH] Change the GlobalAlias constructor to look a bit more like GlobalVariable. This is part of the fix for pr10367. A GlobalAlias always has a pointer type, so just have the constructor build the type. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208983 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/GlobalAlias.h | 7 ++++--- lib/AsmParser/LLParser.cpp | 6 ++++-- lib/Bitcode/Reader/BitcodeReader.cpp | 6 ++++-- lib/IR/Core.cpp | 6 ++++-- lib/IR/Globals.cpp | 16 +++++++++------- lib/Linker/LinkModules.cpp | 7 ++++--- lib/Transforms/IPO/MergeFunctions.cpp | 6 ++++-- lib/Transforms/Utils/CloneModule.cpp | 6 ++++-- unittests/IR/VerifierTest.cpp | 5 ++--- unittests/Transforms/Utils/SpecialCaseList.cpp | 5 +++-- 10 files changed, 42 insertions(+), 28 deletions(-) diff --git a/include/llvm/IR/GlobalAlias.h b/include/llvm/IR/GlobalAlias.h index 5aa42612562..da228471c91 100644 --- a/include/llvm/IR/GlobalAlias.h +++ b/include/llvm/IR/GlobalAlias.h @@ -38,10 +38,11 @@ public: void *operator new(size_t s) { return User::operator new(s, 1); } - /// GlobalAlias ctor - If a parent module is specified, the alias is - /// automatically inserted into the end of the specified module's alias list. + /// If a parent module is specified, the alias is automatically inserted into + /// the end of the specified module's alias list. GlobalAlias(Type *Ty, LinkageTypes Linkage, const Twine &Name = "", - Constant* Aliasee = nullptr, Module *Parent = nullptr); + Constant* Aliasee = nullptr, Module *Parent = nullptr, + unsigned AddressSpace = 0); /// Provide fast operand accessors DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index d0b380066c5..e5813f0d210 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -673,8 +673,10 @@ bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, return Error(AliaseeLoc, "alias must have pointer type"); // Okay, create the alias but do not insert it into the module yet. - std::unique_ptr GA(new GlobalAlias( - Aliasee->getType(), (GlobalValue::LinkageTypes)Linkage, Name, Aliasee)); + PointerType *PTy = cast(Aliasee->getType()); + std::unique_ptr GA( + new GlobalAlias(PTy->getElementType(), (GlobalValue::LinkageTypes)Linkage, + Name, Aliasee, nullptr, PTy->getAddressSpace())); GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index d0ce237ee67..a1ae6baff9f 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1966,8 +1966,10 @@ error_code BitcodeReader::ParseModule(bool Resume) { if (!Ty->isPointerTy()) return Error(InvalidTypeForValue); - GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), - "", nullptr, TheModule); + auto *PTy = cast(Ty); + GlobalAlias *NewGA = + new GlobalAlias(PTy->getElementType(), GetDecodedLinkage(Record[2]), + "", nullptr, TheModule, PTy->getAddressSpace()); // Old bitcode files didn't have visibility field. // Local linkage must have default visibility. if (Record.size() > 3 && !NewGA->hasLocalLinkage()) diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 81d137edfe0..40c1c70257e 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -1488,8 +1488,10 @@ void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, const char *Name) { - return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name, - unwrap(Aliasee), unwrap (M))); + auto *PTy = cast(unwrap(Ty)); + return wrap(new GlobalAlias( + PTy->getElementType(), GlobalValue::ExternalLinkage, Name, + unwrap(Aliasee), unwrap(M), PTy->getAddressSpace())); } /*--.. Operations on functions .............................................--*/ diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp index 0ec54fe3c08..8e7478496f0 100644 --- a/lib/IR/Globals.cpp +++ b/lib/IR/Globals.cpp @@ -213,15 +213,17 @@ void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { // GlobalAlias Implementation //===----------------------------------------------------------------------===// -GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, - const Twine &Name, Constant* aliasee, - Module *ParentModule) - : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { +GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, const Twine &Name, + Constant *Aliasee, Module *ParentModule, + unsigned AddressSpace) + : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal, + &Op<0>(), 1, Link, Name) { LeakDetector::addGarbageObject(this); - if (aliasee) - assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); - Op<0>() = aliasee; + if (Aliasee) + assert(Aliasee->getType() == getType() && + "Alias and aliasee types should match!"); + Op<0>() = Aliasee; if (ParentModule) ParentModule->getAliasList().push_back(this); diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 820f3acacef..0507c5acc9c 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -919,9 +919,10 @@ bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) { // If there is no linkage to be performed or we're linking from the source, // bring over SGA. - GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()), - SGA->getLinkage(), SGA->getName(), - /*aliasee*/nullptr, DstM); + auto *PTy = cast(TypeMap.get(SGA->getType())); + GlobalAlias *NewDA = + new GlobalAlias(PTy->getElementType(), SGA->getLinkage(), SGA->getName(), + /*aliasee*/ nullptr, DstM, PTy->getAddressSpace()); copyGVAttributes(NewDA, SGA); if (NewVisibility) NewDA->setVisibility(*NewVisibility); diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp index 59b6c22aa46..a8106e0c32f 100644 --- a/lib/Transforms/IPO/MergeFunctions.cpp +++ b/lib/Transforms/IPO/MergeFunctions.cpp @@ -1328,8 +1328,10 @@ void MergeFunctions::writeThunk(Function *F, Function *G) { // Replace G with an alias to F and delete G. void MergeFunctions::writeAlias(Function *F, Function *G) { Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); - GlobalAlias *GA = new GlobalAlias(G->getType(), G->getLinkage(), "", - BitcastF, G->getParent()); + PointerType *PTy = G->getType(); + GlobalAlias *GA = + new GlobalAlias(PTy->getElementType(), G->getLinkage(), "", BitcastF, + G->getParent(), PTy->getAddressSpace()); F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); GA->takeName(G); GA->setVisibility(G->getVisibility()); diff --git a/lib/Transforms/Utils/CloneModule.cpp b/lib/Transforms/Utils/CloneModule.cpp index 5d180a5478c..1a3641452d9 100644 --- a/lib/Transforms/Utils/CloneModule.cpp +++ b/lib/Transforms/Utils/CloneModule.cpp @@ -67,8 +67,10 @@ Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) { // Loop over the aliases in the module for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); I != E; ++I) { - GlobalAlias *GA = new GlobalAlias(I->getType(), I->getLinkage(), - I->getName(), nullptr, New); + auto *PTy = cast(I->getType()); + auto *GA = + new GlobalAlias(PTy->getElementType(), I->getLinkage(), I->getName(), + nullptr, New, PTy->getAddressSpace()); GA->copyAttributesFrom(I); VMap[I] = GA; } diff --git a/unittests/IR/VerifierTest.cpp b/unittests/IR/VerifierTest.cpp index 0a660a6b913..92f25b3e37a 100644 --- a/unittests/IR/VerifierTest.cpp +++ b/unittests/IR/VerifierTest.cpp @@ -52,9 +52,8 @@ TEST(VerifierTest, AliasUnnamedAddr) { GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true, GlobalValue::ExternalLinkage, Init, "foo"); - GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C), - GlobalValue::ExternalLinkage, - "bar", Aliasee, &M); + auto *GA = + new GlobalAlias(Ty, GlobalValue::ExternalLinkage, "bar", Aliasee, &M); GA->setUnnamedAddr(true); std::string Error; raw_string_ostream ErrorOS(Error); diff --git a/unittests/Transforms/Utils/SpecialCaseList.cpp b/unittests/Transforms/Utils/SpecialCaseList.cpp index 748834f85e0..6863ea5a601 100644 --- a/unittests/Transforms/Utils/SpecialCaseList.cpp +++ b/unittests/Transforms/Utils/SpecialCaseList.cpp @@ -35,8 +35,9 @@ protected: } GlobalAlias *makeAlias(StringRef Name, GlobalValue *Aliasee) { - return new GlobalAlias(Aliasee->getType(), GlobalValue::ExternalLinkage, - Name, Aliasee, Aliasee->getParent()); + return new GlobalAlias(Aliasee->getType()->getElementType(), + GlobalValue::ExternalLinkage, Name, Aliasee, + Aliasee->getParent()); } SpecialCaseList *makeSpecialCaseList(StringRef List, std::string &Error) {