From 27c60b512a5f9f602602748eb592d6c6909671c4 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 3 Jun 2014 02:42:01 +0000 Subject: [PATCH] Update for llvm API change. Aliases in llvm now hold an arbitrary expression. llvm-svn: 210063 --- clang/lib/CodeGen/CGCXX.cpp | 15 ++-- clang/lib/CodeGen/CodeGenModule.cpp | 80 +++++++++++++--------- clang/test/CodeGen/alias.c | 4 +- clang/test/CodeGen/attributes.c | 2 +- clang/test/CodeGenCXX/ctor-dtor-alias.cpp | 2 +- clang/test/CodeGenCXX/destructors.cpp | 4 +- .../CodeGenCXX/microsoft-abi-structors-alias.cpp | 2 +- clang/test/CodeGenCXX/virtual-destructor-calls.cpp | 4 +- clang/test/Sema/attr-alias-cycle.c | 16 ----- clang/test/Sema/attr-alias-elf.c | 7 ++ 10 files changed, 71 insertions(+), 65 deletions(-) delete mode 100644 clang/test/Sema/attr-alias-cycle.c diff --git a/clang/lib/CodeGen/CGCXX.cpp b/clang/lib/CodeGen/CGCXX.cpp index 24dc89d..545c5ef 100644 --- a/clang/lib/CodeGen/CGCXX.cpp +++ b/clang/lib/CodeGen/CGCXX.cpp @@ -142,9 +142,9 @@ bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, // which case the caller is responsible for ensuring the soundness // of these semantics. auto *Ref = cast(GetAddrOfGlobal(TargetDecl)); - auto *Aliasee = dyn_cast(Ref); - if (!Aliasee) - Aliasee = cast(Ref)->getAliasee(); + llvm::Constant *Aliasee = Ref; + if (Ref->getType() != AliasType) + Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); // Instead of creating as alias to a linkonce_odr, replace all of the uses // of the aliasee. @@ -156,10 +156,7 @@ bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, // members with attribute "AlwaysInline" and expect no reference to // be generated. It is desirable to reenable this optimisation after // corresponding LLVM changes. - llvm::Constant *Replacement = Aliasee; - if (Aliasee->getType() != AliasType) - Replacement = llvm::ConstantExpr::getBitCast(Aliasee, AliasType); - Replacements[MangledName] = Replacement; + Replacements[MangledName] = Aliasee; return false; } @@ -167,7 +164,7 @@ bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, /// If we don't have a definition for the destructor yet, don't /// emit. We can't emit aliases to declarations; that's just not /// how aliases work. - if (Aliasee->isDeclaration()) + if (Ref->isDeclaration()) return true; } @@ -180,7 +177,7 @@ bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, // Create the alias with no name. auto *Alias = llvm::GlobalAlias::create(AliasType->getElementType(), 0, - Linkage, "", Aliasee); + Linkage, "", Aliasee, &getModule()); // Switch any previous uses to the alias. if (Entry) { diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index b0e08a2..5f23bd4 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -225,6 +225,25 @@ void CodeGenModule::applyReplacements() { } } +// This is only used in aliases that we created and we know they have a +// linear structure. +static const llvm::GlobalObject *getAliasedGlobal(const llvm::GlobalAlias &GA) { + llvm::SmallPtrSet Visited; + const llvm::Constant *C = &GA; + for (;;) { + C = cast(C->stripPointerCasts()); + if (auto *GO = dyn_cast(C)) + return GO; + // stripPointerCasts will not walk over weak aliases. + auto *GA2 = dyn_cast(C); + if (!GA2) + return nullptr; + if (!Visited.insert(GA2)) + return nullptr; + C = GA2->getAliasee(); + } +} + void CodeGenModule::checkAliases() { // Check if the constructed aliases are well formed. It is really unfortunate // that we have to do this in CodeGen, but we only construct mangled names @@ -239,19 +258,43 @@ void CodeGenModule::checkAliases() { StringRef MangledName = getMangledName(GD); llvm::GlobalValue *Entry = GetGlobalValue(MangledName); auto *Alias = cast(Entry); - llvm::GlobalValue *GV = Alias->getAliasee(); - if (GV->isDeclaration()) { + const llvm::GlobalValue *GV = getAliasedGlobal(*Alias); + if (!GV) { + Error = true; + Diags.Report(AA->getLocation(), diag::err_cyclic_alias); + } else if (GV->isDeclaration()) { Error = true; Diags.Report(AA->getLocation(), diag::err_alias_to_undefined); } - llvm::GlobalObject *Aliasee = Alias->getAliasee(); + llvm::Constant *Aliasee = Alias->getAliasee(); + llvm::GlobalValue *AliaseeGV; + if (auto CE = dyn_cast(Aliasee)) + AliaseeGV = cast(CE->getOperand(0)); + else + AliaseeGV = cast(Aliasee); + if (const SectionAttr *SA = D->getAttr()) { StringRef AliasSection = SA->getName(); - if (AliasSection != Aliasee->getSection()) + if (AliasSection != AliaseeGV->getSection()) Diags.Report(SA->getLocation(), diag::warn_alias_with_section) << AliasSection; } + + // We have to handle alias to weak aliases in here. LLVM itself disallows + // this since the object semantics would not match the IL one. For + // compatibility with gcc we implement it by just pointing the alias + // to its aliasee's aliasee. We also warn, since the user is probably + // expecting the link to be weak. + if (auto GA = dyn_cast(AliaseeGV)) { + if (GA->mayBeOverridden()) { + Diags.Report(AA->getLocation(), diag::warn_alias_to_weak_alias) + << GV->getName() << GA->getName(); + Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( + GA->getAliasee(), Alias->getType()); + Alias->setAliasee(Aliasee); + } + } } if (!Error) return; @@ -2228,29 +2271,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, AddGlobalAnnotations(D, Fn); } -static llvm::GlobalObject &getGlobalObjectInExpr(DiagnosticsEngine &Diags, - const AliasAttr *AA, - llvm::Constant *C) { - if (auto *GO = dyn_cast(C)) - return *GO; - - auto *GA = dyn_cast(C); - if (GA) { - if (GA->mayBeOverridden()) { - Diags.Report(AA->getLocation(), diag::warn_alias_to_weak_alias) - << GA->getAliasee()->getName() << GA->getName(); - } - - return *GA->getAliasee(); - } - - auto *CE = cast(C); - assert(CE->getOpcode() == llvm::Instruction::BitCast || - CE->getOpcode() == llvm::Instruction::GetElementPtr || - CE->getOpcode() == llvm::Instruction::AddrSpaceCast); - return *cast(CE->getOperand(0)); -} - void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { const auto *D = cast(GD.getDecl()); const AliasAttr *AA = D->getAttr(); @@ -2282,8 +2302,7 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { // Create the new alias itself, but don't set a name yet. auto *GA = llvm::GlobalAlias::create( cast(Aliasee->getType())->getElementType(), 0, - llvm::Function::ExternalLinkage, "", - &getGlobalObjectInExpr(Diags, AA, Aliasee)); + llvm::Function::ExternalLinkage, "", Aliasee, &getModule()); if (Entry) { if (GA->getAliasee() == Entry) { @@ -3208,8 +3227,7 @@ void CodeGenModule::EmitStaticExternCAliases() { IdentifierInfo *Name = I->first; llvm::GlobalValue *Val = I->second; if (Val && !getModule().getNamedValue(Name->getName())) - addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), - cast(Val))); + addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); } } diff --git a/clang/test/CodeGen/alias.c b/clang/test/CodeGen/alias.c index 5aafd95..98449d3 100644 --- a/clang/test/CodeGen/alias.c +++ b/clang/test/CodeGen/alias.c @@ -14,8 +14,8 @@ void f0(void) { } extern void f1(void); extern void f1(void) __attribute((alias("f0"))); // CHECKBASIC-DAG: @f1 = alias void ()* @f0 -// CHECKBASIC-DAG: @test8_foo = alias weak void (...), void ()* @test8_bar -// CHECKBASIC-DAG: @test8_zed = alias void (...), void ()* @test8_bar +// CHECKBASIC-DAG: @test8_foo = alias weak bitcast (void ()* @test8_bar to void (...)*) +// CHECKBASIC-DAG: @test8_zed = alias bitcast (void ()* @test8_bar to void (...)*) // CHECKBASIC-DAG: @test9_zed = alias void ()* @test9_bar // CHECKBASIC: define void @f0() [[NUW:#[0-9]+]] { diff --git a/clang/test/CodeGen/attributes.c b/clang/test/CodeGen/attributes.c index 388ea82..356a179 100644 --- a/clang/test/CodeGen/attributes.c +++ b/clang/test/CodeGen/attributes.c @@ -26,7 +26,7 @@ int t6 __attribute__((visibility("protected"))); // CHECK: @t12 = global i32 0, section "SECT" int t12 __attribute__((section("SECT"))); -// CHECK: @t9 = alias weak void (...), void ()* @__t8 +// CHECK: @t9 = alias weak bitcast (void ()* @__t8 to void (...)*) void __t8() {} void t9() __attribute__((weak, alias("__t8"))); diff --git a/clang/test/CodeGenCXX/ctor-dtor-alias.cpp b/clang/test/CodeGenCXX/ctor-dtor-alias.cpp index 8779311..d869a2b 100644 --- a/clang/test/CodeGenCXX/ctor-dtor-alias.cpp +++ b/clang/test/CodeGenCXX/ctor-dtor-alias.cpp @@ -119,7 +119,7 @@ namespace test7 { namespace test8 { // Test that we replace ~zed with ~bar which is an alias to ~foo. - // CHECK-DAG: call i32 @__cxa_atexit({{.*}}@_ZN5test83fooD2Ev + // CHECK-DAG: call i32 @__cxa_atexit({{.*}}@_ZN5test83barD2Ev // CHECK-DAG: @_ZN5test83barD2Ev = alias {{.*}} @_ZN5test83fooD2Ev struct foo { ~foo(); diff --git a/clang/test/CodeGenCXX/destructors.cpp b/clang/test/CodeGenCXX/destructors.cpp index 3d6e603..5c43048 100644 --- a/clang/test/CodeGenCXX/destructors.cpp +++ b/clang/test/CodeGenCXX/destructors.cpp @@ -4,13 +4,13 @@ // CHECK-DAG: @_ZN5test11MD2Ev = alias {{.*}} @_ZN5test11AD2Ev // CHECK-DAG: @_ZN5test11ND2Ev = alias {{.*}} @_ZN5test11AD2Ev // CHECK-DAG: @_ZN5test11OD2Ev = alias {{.*}} @_ZN5test11AD2Ev -// CHECK-DAG: @_ZN5test11SD2Ev = alias {{.*}} @_ZN5test11AD2Ev +// CHECK-DAG: @_ZN5test11SD2Ev = alias bitcast {{.*}} @_ZN5test11AD2Ev // WIN32-DAG: @_ZN5test01AD1Ev = alias {{.*}} @_ZN5test01AD2Ev // WIN32-DAG: @_ZN5test11MD2Ev = alias {{.*}} @_ZN5test11AD2Ev // WIN32-DAG: @_ZN5test11ND2Ev = alias {{.*}} @_ZN5test11AD2Ev // WIN32-DAG: @_ZN5test11OD2Ev = alias {{.*}} @_ZN5test11AD2Ev -// WIN32-DAG: @_ZN5test11SD2Ev = alias {{.*}} @_ZN5test11AD2Ev +// WIN32-DAG: @_ZN5test11SD2Ev = alias bitcast {{.*}} @_ZN5test11AD2Ev struct A { diff --git a/clang/test/CodeGenCXX/microsoft-abi-structors-alias.cpp b/clang/test/CodeGenCXX/microsoft-abi-structors-alias.cpp index 59ced74..f977556 100644 --- a/clang/test/CodeGenCXX/microsoft-abi-structors-alias.cpp +++ b/clang/test/CodeGenCXX/microsoft-abi-structors-alias.cpp @@ -22,5 +22,5 @@ B::~B() {} void foo() { B b; } -// CHECK-DAG: @"\01??1B@test2@@UAE@XZ" = alias void (%"struct.test2::B"*), void (%"struct.test2::A"*)* @"\01??1A@test2@@UAE@XZ" +// CHECK-DAG: @"\01??1B@test2@@UAE@XZ" = alias bitcast (void (%"struct.test2::A"*)* @"\01??1A@test2@@UAE@XZ" to void (%"struct.test2::B"*)*) } diff --git a/clang/test/CodeGenCXX/virtual-destructor-calls.cpp b/clang/test/CodeGenCXX/virtual-destructor-calls.cpp index 46a446b..3e7fa82 100644 --- a/clang/test/CodeGenCXX/virtual-destructor-calls.cpp +++ b/clang/test/CodeGenCXX/virtual-destructor-calls.cpp @@ -17,8 +17,8 @@ struct B : A { // CHECK: @_ZN1BD1Ev = alias {{.*}} @_ZN1BD2Ev // (aliases from C) -// CHECK: @_ZN1CD1Ev = alias {{.*}} @_ZN1BD2Ev -// CHECK: @_ZN1CD2Ev = alias {{.*}} @_ZN1BD2Ev +// CHECK: @_ZN1CD1Ev = alias {{.*}} @_ZN1CD2Ev +// CHECK: @_ZN1CD2Ev = alias bitcast {{.*}} @_ZN1BD2Ev // Base dtor: actually calls A's base dtor. // CHECK-LABEL: define void @_ZN1BD2Ev(%struct.B* %this) unnamed_addr diff --git a/clang/test/Sema/attr-alias-cycle.c b/clang/test/Sema/attr-alias-cycle.c deleted file mode 100644 index ce024c3..0000000 --- a/clang/test/Sema/attr-alias-cycle.c +++ /dev/null @@ -1,16 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64-pc-linux -fsyntax-only -verify -emit-llvm-only %s - -// FIXME: The attributes use mangled names. Since we only keep a mapping from -// mangled name to llvm GlobalValue, we don't see the clang level decl for -// an alias target when constructing the alias. Given that and that alias cycles -// are not representable in LLVM, we only note the issues when the cycle is -// first formed. - -// FIXME: This error is detected early in CodeGen. Once the first error is -// found, Diags.hasErrorOccurred() returs true and we stop the codegen of the -// file. The consequence is that we don't find any subsequent error. - -void f1() __attribute__((alias("g1"))); -void g1() __attribute__((alias("f1"))); // expected-error {{alias definition is part of a cycle}} - -void h1() __attribute__((alias("g1"))); diff --git a/clang/test/Sema/attr-alias-elf.c b/clang/test/Sema/attr-alias-elf.c index 82ab076..04d1392 100644 --- a/clang/test/Sema/attr-alias-elf.c +++ b/clang/test/Sema/attr-alias-elf.c @@ -35,6 +35,13 @@ void h9() __attribute__((alias("f9"))); void f9() __attribute__((alias("g9"))); void g9() {} +void f10() __attribute__((alias("g10"))); // expected-error {{alias definition is part of a cycle}} +void g10() __attribute__((alias("f10"))); // expected-error {{alias definition is part of a cycle}} + +// FIXME: This could be a bit better, h10 is not part of the cycle, it points +// to it. +void h10() __attribute__((alias("g10"))); // expected-error {{alias definition is part of a cycle}} + extern int a1 __attribute__((alias("b1"))); int b1 = 42; -- 2.7.4