Index: include/clang/AST/VTableBuilder.h =================================================================== --- include/clang/AST/VTableBuilder.h +++ include/clang/AST/VTableBuilder.h @@ -384,10 +384,6 @@ VPtrInfo(const CXXRecordDecl *RD) : ReusingBase(RD), BaseWithVPtr(RD), NextBaseToMangle(RD) {} - // Copy constructor. - // FIXME: Uncomment when we've moved to C++11. - // VPtrInfo(const VPtrInfo &) = default; - /// The vtable will hold all of the virtual bases or virtual methods of /// ReusingBase. This may or may not be the same class as VPtrSubobject.Base. /// A derived class will reuse the vptr of the first non-virtual base Index: lib/CodeGen/CGCXXABI.h =================================================================== --- lib/CodeGen/CGCXXABI.h +++ lib/CodeGen/CGCXXABI.h @@ -346,13 +346,23 @@ virtual void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD) = 0; + virtual bool isVirtualOffsetNeeded(CodeGenFunction &CGF, + const CXXRecordDecl *NearestVBase) = 0; + + virtual bool canInitilizeVPtr(const CXXRecordDecl *VTableClass, + const CXXRecordDecl *Base, + const CXXRecordDecl *NearestVBase) = 0; + + virtual llvm::Constant *getVTableAddressPoint( + BaseSubobject Base, const CXXRecordDecl *VTableClass) = 0; + /// Get the address point of the vtable for the given base subobject while /// building a constructor or a destructor. On return, NeedsVirtualOffset /// tells if a virtual base adjustment is needed in order to get the offset /// of the base subobject. virtual llvm::Value *getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base, - const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0; + const CXXRecordDecl *NearestVBase) = 0; /// Get the address point of the vtable for the given base subobject while /// building a constexpr. Index: lib/CodeGen/CGClass.cpp =================================================================== --- lib/CodeGen/CGClass.cpp +++ lib/CodeGen/CGClass.cpp @@ -1781,12 +1781,14 @@ bool ForVirtualBase, bool Delegating, llvm::Value *This, const CXXConstructExpr *E) { + const CXXRecordDecl *ClassDecl = D->getParent(); + // C++11 [class.mfct.non-static]p2: // If a non-static member function of a class X is called for an object that // is not of type X, or of a type derived from X, the behavior is undefined. // FIXME: Provide a source location here. EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, SourceLocation(), This, - getContext().getRecordType(D->getParent())); + getContext().getRecordType(ClassDecl)); if (D->isTrivial() && D->isDefaultConstructor()) { assert(E->getNumArgs() == 0 && "trivial default ctor with args"); @@ -1802,7 +1804,7 @@ const Expr *Arg = E->getArg(0); QualType SrcTy = Arg->getType(); llvm::Value *Src = EmitLValue(Arg).getAddress(); - QualType DestTy = getContext().getTypeDeclType(D->getParent()); + QualType DestTy = getContext().getTypeDeclType(ClassDecl); EmitAggregateCopyCtor(This, Src, DestTy, SrcTy); return; } @@ -1825,6 +1827,39 @@ const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall(Args, D, Type, ExtraArgs); EmitCall(Info, Callee, ReturnValueSlot(), Args, D); + + // generate vtable assumptions if we are not in another ctor and + // if we calling dynamic class ctor + if (CGM.getCodeGenOpts().OptimizationLevel > 0 && + ClassDecl->isDynamicClass() && Type != Ctor_Base) + EmitVTableAssumptionLoads(ClassDecl, This); +} + +void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &vptr, + llvm::Value *This) { + llvm::Value *VTableGlobal = + CGM.getCXXABI().getVTableAddressPoint(vptr.Base, vptr.VTableClass); + if (!VTableGlobal) return; + + // We can just use the base offset in the complete class. + CharUnits NonVirtualOffset = vptr.Base.getBaseOffset(); + + if (!NonVirtualOffset.isZero()) + This = + ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr); + + llvm::Value *VPtrValue = GetVTablePtr(This, VTableGlobal->getType()); + llvm::Value *Cmp = + Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables"); + Builder.CreateAssumption(Cmp); +} + +void CodeGenFunction::EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, + llvm::Value *This) { + for (const VPtr &vptr : getVTablePointers(ClassDecl)) + if (CGM.getCXXABI().canInitilizeVPtr(vptr.VTableClass, vptr.Base.getBase(), + vptr.NearestVBase)) + EmitVTableAssumptionLoad(vptr, This); } void @@ -1992,24 +2027,12 @@ PushDestructorCleanup(D, Addr); } -void -CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, - const CXXRecordDecl *NearestVBase, - CharUnits OffsetFromNearestVBase, - const CXXRecordDecl *VTableClass) { - const CXXRecordDecl *RD = Base.getBase(); - - // Don't initialize the vtable pointer if the class is marked with the - // 'novtable' attribute. - if ((RD == VTableClass || RD == NearestVBase) && - VTableClass->hasAttr()) - return; - +void CodeGenFunction::InitializeVTablePointer(const VPtr &vptr) { // Compute the address point. - bool NeedsVirtualOffset; llvm::Value *VTableAddressPoint = CGM.getCXXABI().getVTableAddressPointInStructor( - *this, VTableClass, Base, NearestVBase, NeedsVirtualOffset); + *this, vptr.VTableClass, vptr.Base, vptr.NearestVBase); + if (!VTableAddressPoint) return; @@ -2017,17 +2040,15 @@ llvm::Value *VirtualOffset = nullptr; CharUnits NonVirtualOffset = CharUnits::Zero(); - if (NeedsVirtualOffset) { + if (CGM.getCXXABI().isVirtualOffsetNeeded(*this, vptr.NearestVBase)) { // We need to use the virtual base offset offset because the virtual base // might have a different offset in the most derived class. - VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(*this, - LoadCXXThis(), - VTableClass, - NearestVBase); - NonVirtualOffset = OffsetFromNearestVBase; + VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset( + *this, LoadCXXThis(), vptr.VTableClass, vptr.NearestVBase); + NonVirtualOffset = vptr.OffsetFromNearestVBase; } else { // We can just use the base offset in the complete class. - NonVirtualOffset = Base.getBaseOffset(); + NonVirtualOffset = vptr.Base.getBaseOffset(); } // Apply the offsets. @@ -2046,23 +2067,36 @@ ->getPointerTo(); VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo()); VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy); + llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr()); } -void -CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, - const CXXRecordDecl *NearestVBase, - CharUnits OffsetFromNearestVBase, - bool BaseIsNonVirtualPrimaryBase, - const CXXRecordDecl *VTableClass, - VisitedVirtualBasesSetTy& VBases) { +CodeGenFunction::VPtrsVector CodeGenFunction::getVTablePointers( + const CXXRecordDecl *VTableClass) { + CodeGenFunction::VPtrsVector VPtrsResult; + VisitedVirtualBasesSetTy VBases; + getVTablePointers(BaseSubobject(VTableClass, CharUnits::Zero()), + /*NearestVBase=*/nullptr, + /*OffsetFromNearestVBase=*/CharUnits::Zero(), + /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases, + VPtrsResult); + return VPtrsResult; +} + +void CodeGenFunction::getVTablePointers(BaseSubobject Base, + const CXXRecordDecl *NearestVBase, + CharUnits OffsetFromNearestVBase, + bool BaseIsNonVirtualPrimaryBase, + const CXXRecordDecl *VTableClass, + VisitedVirtualBasesSetTy &VBases, + VPtrsVector &vptrs) { // If this base is a non-virtual primary base the address point has already // been set. if (!BaseIsNonVirtualPrimaryBase) { // Initialize the vtable pointer for this base. - InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase, - VTableClass); + VPtr vptr{Base, NearestVBase, OffsetFromNearestVBase, VTableClass}; + vptrs.push_back(vptr); } const CXXRecordDecl *RD = Base.getBase(); @@ -2100,11 +2134,10 @@ BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; } - InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), - I.isVirtual() ? BaseDecl : NearestVBase, - BaseOffsetFromNearestVBase, - BaseDeclIsNonVirtualPrimaryBase, - VTableClass, VBases); + getVTablePointers( + BaseSubobject(BaseDecl, BaseOffset), + I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase, + BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, vptrs); } } @@ -2113,12 +2146,13 @@ if (!RD->isDynamicClass()) return; + auto vptrs = getVTablePointers(RD); + // Initialize the vtable pointers for this class and all of its bases. - VisitedVirtualBasesSetTy VBases; - InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()), - /*NearestVBase=*/nullptr, - /*OffsetFromNearestVBase=*/CharUnits::Zero(), - /*BaseIsNonVirtualPrimaryBase=*/false, RD, VBases); + for (const VPtr &vptr : vptrs) + if (CGM.getCXXABI().canInitilizeVPtr(vptr.VTableClass, vptr.Base.getBase(), + vptr.NearestVBase)) + InitializeVTablePointer(vptr); if (RD->getNumVBases()) CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD); Index: lib/CodeGen/CodeGenFunction.h =================================================================== --- lib/CodeGen/CodeGenFunction.h +++ lib/CodeGen/CodeGenFunction.h @@ -1309,21 +1309,29 @@ void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init, ArrayRef ArrayIndexes); + // struct containg all informations needed to initilize vptrs + struct VPtr { + BaseSubobject Base; + const CXXRecordDecl *NearestVBase; + CharUnits OffsetFromNearestVBase; + const CXXRecordDecl *VTableClass; + }; + /// InitializeVTablePointer - Initialize the vtable pointer of the given /// subobject. /// - void InitializeVTablePointer(BaseSubobject Base, - const CXXRecordDecl *NearestVBase, - CharUnits OffsetFromNearestVBase, - const CXXRecordDecl *VTableClass); + void InitializeVTablePointer(const VPtr &vptr); + + typedef llvm::SmallVector VPtrsVector; typedef llvm::SmallPtrSet VisitedVirtualBasesSetTy; - void InitializeVTablePointers(BaseSubobject Base, - const CXXRecordDecl *NearestVBase, - CharUnits OffsetFromNearestVBase, - bool BaseIsNonVirtualPrimaryBase, - const CXXRecordDecl *VTableClass, - VisitedVirtualBasesSetTy& VBases); + VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass); + + void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, + CharUnits OffsetFromNearestVBase, + bool BaseIsNonVirtualPrimaryBase, + const CXXRecordDecl *VTableClass, + VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs); void InitializeVTablePointers(const CXXRecordDecl *ClassDecl); @@ -1756,6 +1764,13 @@ bool ForVirtualBase, bool Delegating, llvm::Value *This, const CXXConstructExpr *E); + // Emit assumption load for all bases + void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, + llvm::Value *This); + + // Emit assumption that vptr load == global vtable + void EmitVTableAssumptionLoad(const VPtr &vptr, llvm::Value *This); + void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, llvm::Value *This, llvm::Value *Src, const CXXConstructExpr *E); Index: lib/CodeGen/ItaniumCXXABI.cpp =================================================================== --- lib/CodeGen/ItaniumCXXABI.cpp +++ lib/CodeGen/ItaniumCXXABI.cpp @@ -190,10 +190,25 @@ void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD) override; + bool isVirtualOffsetNeeded(CodeGenFunction &CGF, + const CXXRecordDecl *NearestVBase) override; + + bool canInitilizeVPtr(const CXXRecordDecl *VTableClass, + const CXXRecordDecl *Base, + const CXXRecordDecl *NearestVBase) override { + return true; + } + + llvm::Constant *getVTableAddressPoint( + BaseSubobject Base, const CXXRecordDecl *VTableClass) override; + llvm::Value *getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, - BaseSubobject Base, const CXXRecordDecl *NearestVBase, - bool &NeedsVirtualOffset) override; + BaseSubobject Base, const CXXRecordDecl *NearestVBase) override; + + llvm::Value *getVTableAddressPointInStructorWithVTT( + CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, + BaseSubobject Base, const CXXRecordDecl *NearestVBase); llvm::Constant * getVTableAddressPointForConstExpr(BaseSubobject Base, @@ -1376,41 +1391,28 @@ CGM.EmitVTableBitSetEntries(VTable, VTLayout); } +bool ItaniumCXXABI::isVirtualOffsetNeeded(CodeGenFunction &CGF, + const CXXRecordDecl *NearestVBase) { + if (NearestVBase == nullptr) + return false; + return NeedsVTTParameter(CGF.CurGD); +} + llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, - const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { - bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD); - NeedsVirtualOffset = (NeedsVTTParam && NearestVBase); - - llvm::Value *VTableAddressPoint; - if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) { - // Get the secondary vpointer index. - uint64_t VirtualPointerIndex = - CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); - - /// Load the VTT. - llvm::Value *VTT = CGF.LoadCXXVTT(); - if (VirtualPointerIndex) - VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); - - // And load the address point from the VTT. - VTableAddressPoint = CGF.Builder.CreateLoad(VTT); - } else { - llvm::Constant *VTable = - CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits()); - uint64_t AddressPoint = CGM.getItaniumVTableContext() - .getVTableLayout(VTableClass) - .getAddressPoint(Base); - VTableAddressPoint = - CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); + const CXXRecordDecl *NearestVBase) { + // TODO check it + if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) && + NeedsVTTParameter(CGF.CurGD)) { + return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base, + NearestVBase); } - - return VTableAddressPoint; + return getVTableAddressPoint(Base, VTableClass); } -llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( +llvm::Constant *ItaniumCXXABI::getVTableAddressPoint( BaseSubobject Base, const CXXRecordDecl *VTableClass) { - auto *VTable = getAddrOfVTable(VTableClass, CharUnits()); + llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits()); // Find the appropriate vtable within the vtable group. uint64_t AddressPoint = CGM.getItaniumVTableContext() @@ -1425,6 +1427,30 @@ VTable, Indices); } +llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT( + CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, + const CXXRecordDecl *NearestVBase) { + assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) && + NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT"); + + // Get the secondary vpointer index. + uint64_t VirtualPointerIndex = + CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); + + /// Load the VTT. + llvm::Value *VTT = CGF.LoadCXXVTT(); + if (VirtualPointerIndex) + VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); + + // And load the address point from the VTT. + return CGF.Builder.CreateLoad(VTT); +} + +llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( + BaseSubobject Base, const CXXRecordDecl *VTableClass) { + return getVTableAddressPoint(Base, VTableClass); +} + llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, CharUnits VPtrOffset) { assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); Index: lib/CodeGen/MicrosoftCXXABI.cpp =================================================================== --- lib/CodeGen/MicrosoftCXXABI.cpp +++ lib/CodeGen/MicrosoftCXXABI.cpp @@ -215,10 +215,24 @@ void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD) override; + bool isVirtualOffsetNeeded(CodeGenFunction &CGF, + const CXXRecordDecl *NearestVBase) override; + + // Can't initialize the vtable pointer if the class is marked + // with the 'novtable' attribute. + bool canInitilizeVPtr(const CXXRecordDecl *VTableClass, + const CXXRecordDecl *Base, + const CXXRecordDecl *NearestVBase) override { + return !VTableClass->hasAttr() || + (Base != VTableClass && Base != NearestVBase); + } + + llvm::Constant *getVTableAddressPoint( + BaseSubobject Base, const CXXRecordDecl *VTableClass) override; + llvm::Value *getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, - BaseSubobject Base, const CXXRecordDecl *NearestVBase, - bool &NeedsVirtualOffset) override; + BaseSubobject Base, const CXXRecordDecl *NearestVBase) override; llvm::Constant * getVTableAddressPointForConstExpr(BaseSubobject Base, @@ -1564,14 +1578,15 @@ } } +bool MicrosoftCXXABI::isVirtualOffsetNeeded(CodeGenFunction &CGF, + const CXXRecordDecl *NearestVBase) { + return (NearestVBase != nullptr); +} + llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, - const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { - NeedsVirtualOffset = (NearestVBase != nullptr); - - (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); - VFTableIdTy ID(VTableClass, Base.getBaseOffset()); - llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID]; + const CXXRecordDecl *NearestVBase) { + llvm::Constant *VTableAddressPoint = getVTableAddressPoint(Base, VTableClass); if (!VTableAddressPoint) { assert(Base.getBase()->getNumVBases() && !getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); @@ -1586,11 +1601,16 @@ MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out); } -llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( +llvm::Constant *MicrosoftCXXABI::getVTableAddressPoint( BaseSubobject Base, const CXXRecordDecl *VTableClass) { (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); VFTableIdTy ID(VTableClass, Base.getBaseOffset()); - llvm::GlobalValue *VFTable = VFTablesMap[ID]; + return VFTablesMap[ID]; +} + +llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( + BaseSubobject Base, const CXXRecordDecl *VTableClass) { + llvm::Constant *VFTable = getVTableAddressPoint(Base, VTableClass); assert(VFTable && "Couldn't find a vftable for the given base?"); return VFTable; } @@ -1600,6 +1620,7 @@ // getAddrOfVTable may return 0 if asked to get an address of a vtable which // shouldn't be used in the given record type. We want to cache this result in // VFTablesMap, thus a simple zero check is not sufficient. + VFTableIdTy ID(RD, VPtrOffset); VTablesMapTy::iterator I; bool Inserted; @@ -1653,10 +1674,11 @@ if (llvm::GlobalValue *VFTable = CGM.getModule().getNamedGlobal(VFTableName)) { VFTablesMap[ID] = VFTable; - return VTableAliasIsRequred - ? cast( - cast(VFTable)->getBaseObject()) - : cast(VFTable); + VTable = VTableAliasIsRequred + ? cast( + cast(VFTable)->getBaseObject()) + : cast(VFTable); + return VTable; } uint64_t NumVTableSlots = Index: test/CodeGen/available-externally-hidden.cpp =================================================================== --- test/CodeGen/available-externally-hidden.cpp +++ test/CodeGen/available-externally-hidden.cpp @@ -27,6 +27,6 @@ }; int main() { -TestSyncMessageFilter* f = new TestSyncMessageFilter; + TestSyncMessageFilter* f = new TestSyncMessageFilter; f->Send(new Message); } Index: test/CodeGenCXX/ctor-globalopt.cpp =================================================================== --- test/CodeGenCXX/ctor-globalopt.cpp +++ test/CodeGenCXX/ctor-globalopt.cpp @@ -1,7 +1,7 @@ // RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s -// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -o - %s -O1 | FileCheck %s --check-prefix=O1 +// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -o - %s -O2 | opt - -S -globalopt -o - | FileCheck %s --check-prefix=O1 // RUN: %clang_cc1 -triple %ms_abi_triple -emit-llvm -o - %s | FileCheck %s -// RUN: %clang_cc1 -triple %ms_abi_triple -emit-llvm -o - %s -O1 | FileCheck %s --check-prefix=O1 +// RUN: %clang_cc1 -triple %ms_abi_triple -emit-llvm -o - %s -O2 | opt - -S -globalopt -o - | FileCheck %s --check-prefix=O1 // Check that GlobalOpt can eliminate static constructors for simple implicit // constructors. This is a targetted integration test to make sure that LLVM's Index: test/CodeGenCXX/template-instantiation.cpp =================================================================== --- test/CodeGenCXX/template-instantiation.cpp +++ test/CodeGenCXX/template-instantiation.cpp @@ -1,5 +1,7 @@ // RUN: %clang_cc1 %s -O1 -disable-llvm-optzns -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s +// CHECK: @_ZTVN5test018stdio_sync_filebufIA4_iEE = linkonce_odr unnamed_addr constant + // CHECK: @_ZN7PR100011xE = global // CHECK-NOT: @_ZN7PR100014kBarE = external global i32 // @@ -12,7 +14,6 @@ // CHECK: @_ZN7PR100011SIiE3arrE = linkonce_odr global [3 x i32] // CHECK-NOT: @_ZN7PR100011SIiE3arr2E = linkonce_odr global [3 x i32]A -// CHECK: @_ZTVN5test018stdio_sync_filebufIA4_iEE = linkonce_odr unnamed_addr constant // CHECK-NOT: _ZTVN5test31SIiEE // CHECK-NOT: _ZTSN5test31SIiEE Index: test/CodeGenCXX/thunks.cpp =================================================================== --- test/CodeGenCXX/thunks.cpp +++ test/CodeGenCXX/thunks.cpp @@ -1,5 +1,9 @@ -// RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o - | FileCheck %s -// RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o - -O1 -disable-llvm-optzns | FileCheck %s +// RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o %t +// RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o %t.opt -O1 -disable-llvm-optzns +// RUN: FileCheck %s < %t +// RUN: FileCheck %s < %t.opt +// RUN: FileCheck --check-prefix=CHECK-NONOPT %s < %t +// RUN: FileCheck --check-prefix=CHECK-OPT %s < %t.opt namespace Test1 { @@ -380,13 +384,25 @@ /**** The following has to go at the end of the file ****/ +// checking without opt +// CHECK-NONOPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv( +// CHECK-NONOPT-NOT: comdat + // This is from Test5: -// CHECK-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv( -// CHECK-NOT: comdat -// CHECK-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv +// CHECK-NONOPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv + +// This is from Test10: +// CHECK-NONOPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv +// CHECK-NONOPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv + +// Checking with opt +// CHECK-OPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(%"struct.Test4B::(anonymous namespace)::C"* %this) unnamed_addr #0 align 2 // This is from Test10: -// CHECK-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv -// CHECK-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv +// CHECK-OPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv +// CHECK-OPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv + +// This is from Test5: +// CHECK-OPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv // CHECK: attributes [[NUW]] = { nounwind uwtable{{.*}} } Index: test/CodeGenCXX/virtual-base-ctor.cpp =================================================================== --- test/CodeGenCXX/virtual-base-ctor.cpp +++ test/CodeGenCXX/virtual-base-ctor.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -emit-llvm -triple %itanium_abi_triple -o - -O2 | FileCheck %s +// RUN: %clang_cc1 %s -emit-llvm -triple %itanium_abi_triple -o - -O2 | opt - -S -globalopt -o - | FileCheck %s struct B; extern B x; Index: test/CodeGenCXX/vtable-assume-load.cpp =================================================================== --- /dev/null +++ test/CodeGenCXX/vtable-assume-load.cpp @@ -0,0 +1,170 @@ +// RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o %t.ll -O1 -disable-llvm-optzns -fms-extensions +// RUN: %clang_cc1 %s -triple i686-pc-win32 -emit-llvm -o %t.ms.ll -O1 -disable-llvm-optzns -fms-extensions + +// RUN: FileCheck --check-prefix=CHECK1 --input-file=%t.ll %s +// RUN: FileCheck --check-prefix=CHECK2 --input-file=%t.ll %s +// RUN: FileCheck --check-prefix=CHECK3 --input-file=%t.ll %s +// RUN: FileCheck --check-prefix=CHECK4 --input-file=%t.ll %s +// RUN: FileCheck --check-prefix=CHECK-MS --input-file=%t.ms.ll %s + +namespace test1 { + +struct A { + A(); + virtual void foo(); +}; + +struct B : A { + virtual void foo(); +}; + +void g(A* a) { a->foo(); } + +void fooA() { + A a; + g(&a); +} +void fooB() { + B b; + g(&b); +} +// CHECK1-LABEL: define void @_ZN5test14fooAEv() +// CHECK1: %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTVN5test11AE, i64 0, i64 2) +// CHECK1: call void @llvm.assume(i1 %cmp.vtables) +// CHECK1-LABEL: } + +// CHECK1-LABEL: define void @_ZN5test14fooBEv() +// CHECK1: call void @_ZN5test11BC1Ev(%"struct.test1::B"* %b) +// CHECK1: %vtable = load i8**, i8*** %1, !tbaa !5 +// CHECK1: %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTVN5test11BE, i64 0, i64 2) +// CHECK1: call void @llvm.assume(i1 %cmp.vtables) +// CHECK1-LABLE: } + +// there should not be any assumes in the ctor that calls base ctor +// CHECK1-LABEL: define linkonce_odr void @_ZN5test11BC2Ev(%"struct.test1::B"* %this) +// CHECK1-NOT: @llvm.assume( +// CHECK1-LABEL: } +} +namespace test2 { +struct A { + A(); + virtual void foo(); +}; + +struct B { + B(); + virtual void bar(); +}; + +struct C : A, B { + C(); + virtual void foo(); +}; +void g(A* a) { a->foo(); } +void h(B* b) { b->bar(); } + +// CHECK2-LABEL: define void @_ZN5test24testEv() +// CHECK2: call void @_ZN5test21CC1Ev(%"struct.test2::C"* %c) +// CHECK2: vtable = load i8**, i8*** %1, !tbaa !5 +// CHECK2: %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTVN5test21CE, i64 0, i64 2) +// CHECK2: call void @llvm.assume(i1 %cmp.vtables) + +// CHECK2: %2 = bitcast %"struct.test2::C"* %c to i8* +// CHECK2: %add.ptr = getelementptr inbounds i8, i8* %2, i64 8 +// CHECK2: %3 = bitcast i8* %add.ptr to i8*** +// CHECK2: %vtable1 = load i8**, i8*** %3, !tbaa !5 +// CHECK2: %cmp.vtables2 = icmp eq i8** %vtable1, getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTVN5test21CE, i64 0, i64 5) +// CHECK2: call void @llvm.assume(i1 %cmp.vtables2) + +// CHECK2: call void @_ZN5test21gEPNS_1AE( +// CHECK2-LABEL: } + +void test() { + C c; + g(&c); + h(&c); +} +} + +namespace test3 { +struct A { + A(); +}; + +struct B : A { + B(); + virtual void foo(); +}; + +struct C : virtual A, B { + C(); + virtual void foo(); +}; +void g(B* a) { a->foo(); } + +// CHECK3-LABEL: define void @_ZN5test34testEv() +// CHECK3: call void @_ZN5test31CC1Ev(%"struct.test3::C"* %c) +// CHECK3: %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([4 x i8*], [4 x i8*]* @_ZTVN5test31CE, i64 0, i64 3) +// CHECK3: call void @llvm.assume(i1 %cmp.vtables) +// CHECK3-LABLEL: } +void test() { + C c; + g(&c); +} +} // test3 + +namespace test4 { +struct A { + A(); + virtual void foo(); +}; + +struct B : virtual A { + B(); + virtual void foo(); +}; +struct C : B { + C(); + virtual void foo(); +}; + +void g(C* c) { c->foo(); } + +// CHECK4-LABEL: define void @_ZN5test44testEv() +// CHECK4: call void @_ZN5test41CC1Ev(%"struct.test4::C"* %c) +// CHECK4: %vtable = load i8**, i8*** %1, !tbaa !5 +// CHECK4: %cmp.vtables = icmp eq i8** %vtable, getelementptr inbounds ([5 x i8*], [5 x i8*]* @_ZTVN5test41CE, i64 0, i64 4) +// CHECK4: call void @llvm.assume(i1 %cmp.vtables) + +// CHECK4: %2 = bitcast %"struct.test4::C"* %c to i8*** +// CHECK4: %vtable1 = load i8**, i8*** %2, !tbaa !5 +// CHECK4: %cmp.vtables2 = icmp eq i8** %vtable1, getelementptr inbounds ([5 x i8*], [5 x i8*]* @_ZTVN5test41CE, i64 0, i64 4) +// CHECK4: call void @llvm.assume(i1 %cmp.vtables2) +// CHECK4-LABEL: } + +void test() { + C c; + g(&c); +} +} // test4 + +namespace test5 { + +struct __declspec(novtable) S { + virtual void foo(); +}; + +void g(S& s) { s.foo(); } + +// if struct has novtable specifier, then we can't generate assumes +// CHECK-MS-LABEL: define void @"\01?test@test5@@YAXXZ"() +// CHECK-MS: call x86_thiscallcc %"struct.test5::S"* @"\01??0S@test5@@QAE@XZ"( +// CHECK-MS-NOT: @llvm.assume +// CHECK-MS-LABEL: } + +void test() { + S s; + g(s); +} + +} // test5 Index: test/CodeGenCXX/vtable-available-externally.cpp =================================================================== --- test/CodeGenCXX/vtable-available-externally.cpp +++ test/CodeGenCXX/vtable-available-externally.cpp @@ -182,8 +182,8 @@ namespace Test9 { // all virtual functions are outline, so we can assume that it will // be generated in translation unit where foo is defined -// CHECK-TEST9: @_ZTVN5Test91AE = available_externally unnamed_addr constant -// CHECK-TEST9: @_ZTVN5Test91BE = available_externally unnamed_addr constant +// CHECK-TEST9-DAG: @_ZTVN5Test91AE = available_externally unnamed_addr constant +// CHECK-TEST9-DAG: @_ZTVN5Test91BE = available_externally unnamed_addr constant struct A { virtual void foo(); virtual void bar(); @@ -206,7 +206,7 @@ namespace Test10 { // because A's key function is defined here, vtable is generated in this TU -// CHECK-TEST10: @_ZTVN6Test101AE = unnamed_addr constant +// CHECK-TEST10-DAG: @_ZTVN6Test101AE = unnamed_addr constant struct A { virtual void foo(); virtual void bar(); @@ -214,14 +214,14 @@ void A::foo() {} // Because key function is inline we will generate vtable as linkonce_odr -// CHECK-TEST10: @_ZTVN6Test101DE = linkonce_odr unnamed_addr constant +// CHECK-TEST10-DAG: @_ZTVN6Test101DE = linkonce_odr unnamed_addr constant struct D : A { void bar(); }; inline void D::bar() {} // because B has outline key function then we can refer to -// CHECK-TEST10: @_ZTVN6Test101BE = available_externally unnamed_addr constant +// CHECK-TEST10-DAG: @_ZTVN6Test101BE = available_externally unnamed_addr constant struct B : A { void foo(); void bar(); @@ -230,7 +230,7 @@ // C's key function (car) is outline, but C has inline virtual function so we // can't guarantee that we will be able to refer to bar from name // so (at the moment) we can't emit vtable available_externally -// CHECK-TEST10: @_ZTVN6Test101CE = external unnamed_addr constant +// CHECK-TEST10-DAG: @_ZTVN6Test101CE = external unnamed_addr constant struct C : A { void bar() {} // defined in body - not key function virtual inline void gar(); // inline in body - not key function @@ -238,7 +238,7 @@ }; // no key function, vtable will be generated everywhere it will be used -// CHECK-TEST10: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant +// CHECK-TEST10-DAG: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant struct E : A {}; void g(A& a) {