From 39e7388a193f9e0b7d161b37ff133fe19764a7a8 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 29 Apr 2015 21:22:39 +0000 Subject: [PATCH] [opaque pointer type] Pass GlobalAlias the actual pointer type rather than decomposing it into pointee type + address space Many of the callers already have the pointer type anyway, and for the couple of callers that don't it's pretty easy to call PointerType::get on the pointee type and address space. This avoids LLParser from using PointerType::getElementType when parsing GlobalAliases from IR. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236160 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/GlobalAlias.h | 20 ++++++------- lib/AsmParser/LLParser.cpp | 6 ++-- lib/Bitcode/Reader/BitcodeReader.cpp | 3 +- lib/CodeGen/GlobalMerge.cpp | 3 +- lib/ExecutionEngine/Orc/CloneSubModule.cpp | 4 +-- lib/IR/Core.cpp | 3 +- lib/IR/Globals.cpp | 33 +++++++++------------- lib/Linker/LinkModules.cpp | 3 +- lib/Transforms/IPO/LowerBitSets.cpp | 14 ++++----- lib/Transforms/IPO/MergeFunctions.cpp | 3 +- lib/Transforms/Utils/CloneModule.cpp | 4 +-- unittests/IR/ConstantsTest.cpp | 4 +-- 12 files changed, 41 insertions(+), 59 deletions(-) diff --git a/include/llvm/IR/GlobalAlias.h b/include/llvm/IR/GlobalAlias.h index d0672c8aac0..ce73b7af8ca 100644 --- a/include/llvm/IR/GlobalAlias.h +++ b/include/llvm/IR/GlobalAlias.h @@ -33,8 +33,8 @@ class GlobalAlias : public GlobalValue, public ilist_node { void setParent(Module *parent); - GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, - const Twine &Name, Constant *Aliasee, Module *Parent); + GlobalAlias(PointerType *Ty, LinkageTypes Linkage, const Twine &Name, + Constant *Aliasee, Module *Parent); public: // allocate space for exactly one operand @@ -44,19 +44,17 @@ public: /// If a parent module is specified, the alias is automatically inserted into /// the end of the specified module's alias list. - static GlobalAlias *create(Type *Ty, unsigned AddressSpace, - LinkageTypes Linkage, const Twine &Name, - Constant *Aliasee, Module *Parent); + static GlobalAlias *create(PointerType *Ty, LinkageTypes Linkage, + const Twine &Name, Constant *Aliasee, + Module *Parent); // Without the Aliasee. - static GlobalAlias *create(Type *Ty, unsigned AddressSpace, - LinkageTypes Linkage, const Twine &Name, - Module *Parent); + static GlobalAlias *create(PointerType *Ty, LinkageTypes Linkage, + const Twine &Name, Module *Parent); // The module is taken from the Aliasee. - static GlobalAlias *create(Type *Ty, unsigned AddressSpace, - LinkageTypes Linkage, const Twine &Name, - GlobalValue *Aliasee); + static GlobalAlias *create(PointerType *Ty, LinkageTypes Linkage, + const Twine &Name, GlobalValue *Aliasee); // Type, Parent and AddressSpace taken from the Aliasee. static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name, diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 1a2e1f85e81..e69fa3828ed 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -663,13 +663,11 @@ bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, unsigned L, auto *PTy = dyn_cast(AliaseeType); if (!PTy) return Error(AliaseeLoc, "An alias must have pointer type"); - Type *Ty = PTy->getElementType(); - unsigned AddrSpace = PTy->getAddressSpace(); // Okay, create the alias but do not insert it into the module yet. std::unique_ptr GA( - GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage, - Name, Aliasee, /*Parent*/ nullptr)); + GlobalAlias::create(PTy, (GlobalValue::LinkageTypes)Linkage, Name, + Aliasee, /*Parent*/ nullptr)); GA->setThreadLocalMode(TLM); 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 565e46bd820..f49a53805c9 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -3041,8 +3041,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume, return Error("Invalid type for value"); auto *NewGA = - GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - getDecodedLinkage(Record[2]), "", TheModule); + GlobalAlias::create(PTy, getDecodedLinkage(Record[2]), "", TheModule); // Old bitcode files didn't have visibility field. // Local linkage must have default visibility. if (Record.size() > 3 && !NewGA->hasLocalLinkage()) diff --git a/lib/CodeGen/GlobalMerge.cpp b/lib/CodeGen/GlobalMerge.cpp index aaa37896293..79de17567f7 100644 --- a/lib/CodeGen/GlobalMerge.cpp +++ b/lib/CodeGen/GlobalMerge.cpp @@ -459,8 +459,7 @@ bool GlobalMerge::doMerge(SmallVectorImpl &Globals, if (Linkage != GlobalValue::InternalLinkage) { // Generate a new alias... auto *PTy = cast(GEP->getType()); - GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - Linkage, Name, GEP, &M); + GlobalAlias::create(PTy, Linkage, Name, GEP, &M); } NumMerged++; diff --git a/lib/ExecutionEngine/Orc/CloneSubModule.cpp b/lib/ExecutionEngine/Orc/CloneSubModule.cpp index a3196ad2f78..c9810097d7d 100644 --- a/lib/ExecutionEngine/Orc/CloneSubModule.cpp +++ b/lib/ExecutionEngine/Orc/CloneSubModule.cpp @@ -61,9 +61,7 @@ void CloneSubModule(llvm::Module &Dst, const Module &Src, for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end(); I != E; ++I) { auto *PTy = cast(I->getType()); - auto *GA = - GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - I->getLinkage(), I->getName(), &Dst); + auto *GA = GlobalAlias::create(PTy, I->getLinkage(), I->getName(), &Dst); GA->copyAttributesFrom(I); VMap[I] = GA; } diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 0c1023c7348..9557cda5a9c 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -1634,8 +1634,7 @@ void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, const char *Name) { auto *PTy = cast(unwrap(Ty)); - return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - GlobalValue::ExternalLinkage, Name, + return wrap(GlobalAlias::create(PTy, GlobalValue::ExternalLinkage, Name, unwrap(Aliasee), unwrap(M))); } diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp index 5a6adb3f304..31f864d2115 100644 --- a/lib/IR/Globals.cpp +++ b/lib/IR/Globals.cpp @@ -241,40 +241,35 @@ void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { // GlobalAlias Implementation //===----------------------------------------------------------------------===// -GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, - const Twine &Name, Constant *Aliasee, - Module *ParentModule) - : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal, - &Op<0>(), 1, Link, Name) { +GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name, + Constant *Aliasee, Module *ParentModule) + : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { Op<0>() = Aliasee; if (ParentModule) ParentModule->getAliasList().push_back(this); } -GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, - LinkageTypes Link, const Twine &Name, - Constant *Aliasee, Module *ParentModule) { - return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); +GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link, + const Twine &Name, Constant *Aliasee, + Module *ParentModule) { + return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule); } -GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, - LinkageTypes Linkage, const Twine &Name, - Module *Parent) { - return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); +GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage, + const Twine &Name, Module *Parent) { + return create(Ty, Linkage, Name, nullptr, Parent); } -GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, - LinkageTypes Linkage, const Twine &Name, - GlobalValue *Aliasee) { - return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); +GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage, + const Twine &Name, GlobalValue *Aliasee) { + return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent()); } GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, GlobalValue *Aliasee) { PointerType *PTy = Aliasee->getType(); - return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, - Aliasee); + return create(PTy, Link, Name, Aliasee); } GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index b0cf109bb00..27b7ffd795a 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -582,8 +582,7 @@ static GlobalAlias *copyGlobalAliasProto(TypeMapTy &TypeMap, Module &DstM, // If there is no linkage to be performed or we're linking from the source, // bring over SGA. auto *PTy = cast(TypeMap.get(SGA->getType())); - return GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - SGA->getLinkage(), SGA->getName(), &DstM); + return GlobalAlias::create(PTy, SGA->getLinkage(), SGA->getName(), &DstM); } static GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, Module &DstM, diff --git a/lib/Transforms/IPO/LowerBitSets.cpp b/lib/Transforms/IPO/LowerBitSets.cpp index f3f8529911c..bffeebb6e2e 100644 --- a/lib/Transforms/IPO/LowerBitSets.cpp +++ b/lib/Transforms/IPO/LowerBitSets.cpp @@ -358,8 +358,9 @@ void LowerBitSets::allocateByteArrays() { if (LinkerSubsectionsViaSymbols) { BAI->ByteArray->replaceAllUsesWith(GEP); } else { - GlobalAlias *Alias = GlobalAlias::create( - Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, M); + GlobalAlias *Alias = + GlobalAlias::create(PointerType::getUnqual(Int8Ty), + GlobalValue::PrivateLinkage, "bits", GEP, M); BAI->ByteArray->replaceAllUsesWith(Alias); } BAI->ByteArray->eraseFromParent(); @@ -401,7 +402,7 @@ Value *LowerBitSets::createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI, // Each use of the byte array uses a different alias. This makes the // backend less likely to reuse previously computed byte array addresses, // improving the security of the CFI mechanism based on this pass. - ByteArray = GlobalAlias::create(BAI->ByteArray->getValueType(), 0, + ByteArray = GlobalAlias::create(BAI->ByteArray->getType(), GlobalValue::PrivateLinkage, "bits_use", ByteArray, M); } @@ -553,10 +554,9 @@ void LowerBitSets::buildBitSetsFromGlobals( if (LinkerSubsectionsViaSymbols) { Globals[I]->replaceAllUsesWith(CombinedGlobalElemPtr); } else { - GlobalAlias *GAlias = GlobalAlias::create( - Globals[I]->getType()->getElementType(), - Globals[I]->getType()->getAddressSpace(), Globals[I]->getLinkage(), - "", CombinedGlobalElemPtr, M); + GlobalAlias *GAlias = + GlobalAlias::create(Globals[I]->getType(), Globals[I]->getLinkage(), + "", CombinedGlobalElemPtr, M); GAlias->takeName(Globals[I]); Globals[I]->replaceAllUsesWith(GAlias); } diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp index 596674d6816..396ab9ba505 100644 --- a/lib/Transforms/IPO/MergeFunctions.cpp +++ b/lib/Transforms/IPO/MergeFunctions.cpp @@ -1358,8 +1358,7 @@ void MergeFunctions::writeThunk(Function *F, Function *G) { // Replace G with an alias to F and delete G. void MergeFunctions::writeAlias(Function *F, Function *G) { PointerType *PTy = G->getType(); - auto *GA = GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - G->getLinkage(), "", F); + auto *GA = GlobalAlias::create(PTy, G->getLinkage(), "", F); 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 fae9ff5bce0..269332206da 100644 --- a/lib/Transforms/Utils/CloneModule.cpp +++ b/lib/Transforms/Utils/CloneModule.cpp @@ -69,9 +69,7 @@ Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) { for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); I != E; ++I) { auto *PTy = cast(I->getType()); - auto *GA = - GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - I->getLinkage(), I->getName(), New); + auto *GA = GlobalAlias::create(PTy, I->getLinkage(), I->getName(), New); GA->copyAttributesFrom(I); VMap[I] = GA; } diff --git a/unittests/IR/ConstantsTest.cpp b/unittests/IR/ConstantsTest.cpp index 7741b448fa8..2df008a78cc 100644 --- a/unittests/IR/ConstantsTest.cpp +++ b/unittests/IR/ConstantsTest.cpp @@ -348,7 +348,7 @@ TEST(ConstantsTest, GEPReplaceWithConstant) { std::unique_ptr M(new Module("MyModule", Context)); Type *IntTy = Type::getInt32Ty(Context); - Type *PtrTy = PointerType::get(IntTy, 0); + auto *PtrTy = PointerType::get(IntTy, 0); auto *C1 = ConstantInt::get(IntTy, 1); auto *Placeholder = new GlobalVariable( *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr); @@ -361,7 +361,7 @@ TEST(ConstantsTest, GEPReplaceWithConstant) { auto *Global = new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, nullptr); - auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage, + auto *Alias = GlobalAlias::create(PtrTy, GlobalValue::ExternalLinkage, "alias", Global, M.get()); Placeholder->replaceAllUsesWith(Alias); ASSERT_EQ(GEP, Ref->getInitializer()); -- 2.34.1