зеркало из https://github.com/microsoft/clang-1.git
CodeGen: Implement hint values for dynamic_cast as described in the Itanium C++ ABI.
This can yield dramatic speedups of dynamic_cast for simple inheritance trees, at least with libsupc++. Neither libcxxabi nor libcxxrt make use of this hint currently, it was never implemented because clang didn't support it. There was some concern about the number of class hierarchy walks this change introduces. If it turns out to be an issue we can add caching either at the cast pair level or even deeper, but we also do a lot of walks in Sema so this codepath is probably fairly optimized already. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174293 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
922cec29a9
Коммит
ae3f760875
|
@ -1713,6 +1713,58 @@ static void EmitBadCastCall(CodeGenFunction &CGF) {
|
|||
CGF.Builder.CreateUnreachable();
|
||||
}
|
||||
|
||||
/// \brief Compute the src2dst_offset hint as described in the
|
||||
/// Itanium C++ ABI [2.9.7]
|
||||
static CharUnits computeOffsetHint(ASTContext &Context,
|
||||
const CXXRecordDecl *Src,
|
||||
const CXXRecordDecl *Dst) {
|
||||
CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
|
||||
/*DetectVirtual=*/false);
|
||||
|
||||
// If Dst is not derived from Src we can skip the whole computation below and
|
||||
// return that Src is not a public base of Dst. Record all inheritance paths.
|
||||
if (!Dst->isDerivedFrom(Src, Paths))
|
||||
return CharUnits::fromQuantity(-2ULL);
|
||||
|
||||
unsigned NumPublicPaths = 0;
|
||||
CharUnits Offset;
|
||||
|
||||
// Now walk all possible inheritance paths.
|
||||
for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end();
|
||||
I != E; ++I) {
|
||||
if (I->Access != AS_public) // Ignore non-public inheritance.
|
||||
continue;
|
||||
|
||||
++NumPublicPaths;
|
||||
|
||||
for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) {
|
||||
// If the path contains a virtual base class we can't give any hint.
|
||||
// -1: no hint.
|
||||
if (J->Base->isVirtual())
|
||||
return CharUnits::fromQuantity(-1ULL);
|
||||
|
||||
if (NumPublicPaths > 1) // Won't use offsets, skip computation.
|
||||
continue;
|
||||
|
||||
// Accumulate the base class offsets.
|
||||
const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class);
|
||||
Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl());
|
||||
}
|
||||
}
|
||||
|
||||
// -2: Src is not a public base of Dst.
|
||||
if (NumPublicPaths == 0)
|
||||
return CharUnits::fromQuantity(-2ULL);
|
||||
|
||||
// -3: Src is a multiple public base type but never a virtual base type.
|
||||
if (NumPublicPaths > 1)
|
||||
return CharUnits::fromQuantity(-3ULL);
|
||||
|
||||
// Otherwise, the Src type is a unique public nonvirtual base type of Dst.
|
||||
// Return the offset of Src from the origin of Dst.
|
||||
return Offset;
|
||||
}
|
||||
|
||||
static llvm::Value *
|
||||
EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
|
||||
QualType SrcTy, QualType DestTy,
|
||||
|
@ -1762,8 +1814,13 @@ EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
|
|||
llvm::Value *DestRTTI =
|
||||
CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
|
||||
|
||||
// FIXME: Actually compute a hint here.
|
||||
llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL);
|
||||
// Compute the offset hint.
|
||||
const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
|
||||
const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
|
||||
llvm::Value *OffsetHint =
|
||||
llvm::ConstantInt::get(PtrDiffLTy,
|
||||
computeOffsetHint(CGF.getContext(), SrcDecl,
|
||||
DestDecl).getQuantity());
|
||||
|
||||
// Emit the call to __dynamic_cast.
|
||||
Value = CGF.EmitCastToVoidPtr(Value);
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
// RUN: %clang_cc1 -triple x86_64-apple-darwin12 -emit-llvm -o - %s | FileCheck %s
|
||||
|
||||
class A { virtual ~A() {} };
|
||||
class B { virtual ~B() {} };
|
||||
|
||||
class C : A { char x; };
|
||||
class D : public A { short y; };
|
||||
class E : public A, public B { int z; };
|
||||
class F : public virtual A { long long w; };
|
||||
class G : virtual A { long long w; };
|
||||
|
||||
class H : public E { int a; };
|
||||
class I : public F { char b; };
|
||||
|
||||
class J : public H { char q; };
|
||||
class K : public C, public B { char q; };
|
||||
|
||||
class XA : public A { };
|
||||
class XB : public A { };
|
||||
class XC : public virtual A { };
|
||||
class X : public XA, public XB, public XC { };
|
||||
|
||||
void test(A *a, B *b) {
|
||||
volatile C *ac = dynamic_cast<C *>(a);
|
||||
// CHECK: call i8* @__dynamic_cast(i8* %2, i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64 }* @_ZTI1C to i8*), i64 -2)
|
||||
volatile D *ad = dynamic_cast<D *>(a);
|
||||
// CHECK: call i8* @__dynamic_cast(i8* %8, i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1D to i8*), i64 0)
|
||||
volatile E *ae = dynamic_cast<E *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64, i8*, i64 }* @_ZTI1E to i8*), i64 0)
|
||||
volatile F *af = dynamic_cast<F *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64 }* @_ZTI1F to i8*), i64 -1)
|
||||
volatile G *ag = dynamic_cast<G *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64 }* @_ZTI1G to i8*), i64 -2)
|
||||
volatile H *ah = dynamic_cast<H *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1H to i8*), i64 0)
|
||||
volatile I *ai = dynamic_cast<I *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1I to i8*), i64 -1)
|
||||
volatile J *aj = dynamic_cast<J *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1J to i8*), i64 0)
|
||||
volatile K *ak = dynamic_cast<K *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64, i8*, i64 }* @_ZTI1K to i8*), i64 -2)
|
||||
volatile X *ax = dynamic_cast<X *>(a);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64, i8*, i64, i8*, i64 }* @_ZTI1X to i8*), i64 -1)
|
||||
|
||||
volatile E *be = dynamic_cast<E *>(b);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1B to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64, i8*, i64 }* @_ZTI1E to i8*), i64 8)
|
||||
volatile G *bg = dynamic_cast<G *>(b);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1B to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64 }* @_ZTI1G to i8*), i64 -2)
|
||||
volatile J *bj = dynamic_cast<J *>(b);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1B to i8*), i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1J to i8*), i64 8)
|
||||
volatile K *bk = dynamic_cast<K *>(b);
|
||||
// CHECK: i8* bitcast ({ i8*, i8* }* @_ZTI1B to i8*), i8* bitcast ({ i8*, i8*, i32, i32, i8*, i64, i8*, i64 }* @_ZTI1K to i8*), i64 16)
|
||||
}
|
Загрузка…
Ссылка в новой задаче