Author: Zequan Wu Date: 2020-09-15T12:15:47-07:00 New Revision: f975ae4867d1fdfaba11a3ec7e479da8fbfd82d8
URL: https://github.com/llvm/llvm-project/commit/f975ae4867d1fdfaba11a3ec7e479da8fbfd82d8 DIFF: https://github.com/llvm/llvm-project/commit/f975ae4867d1fdfaba11a3ec7e479da8fbfd82d8.diff LOG: [CodeGen][typeid] Emit typeinfo directly if type is known at compile-time Differential Revision: https://reviews.llvm.org/D87425 Added: Modified: clang/include/clang/AST/ExprCXX.h clang/lib/AST/ExprCXX.cpp clang/lib/CodeGen/CGExprCXX.cpp clang/test/CodeGenCXX/microsoft-abi-typeid.cpp Removed: ################################################################################ diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 0ba5e417fd58e..9658f37723e18 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -858,6 +858,10 @@ class CXXTypeidExpr : public Expr { /// evaluated, per C++11 [expr.typeid]p3. bool isPotentiallyEvaluated() const; + /// Best-effort check if the expression operand refers to a most derived + /// object. This is not a strong guarantee. + bool isMostDerived(ASTContext &Context) const; + bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } /// Retrieves the type operand of this typeid() expression after diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index 3f3f2303587dd..1fd2b8e3b4e26 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -146,6 +146,18 @@ bool CXXTypeidExpr::isPotentiallyEvaluated() const { return false; } +bool CXXTypeidExpr::isMostDerived(ASTContext &Context) const { + assert(!isTypeOperand() && "Cannot call isMostDerived for typeid(type)"); + const Expr *E = getExprOperand()->IgnoreParenNoopCasts(Context); + if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { + QualType Ty = DRE->getDecl()->getType(); + if (!Ty->isPointerType() && !Ty->isReferenceType()) + return true; + } + + return false; +} + QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const { assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); Qualifiers Quals; diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index 50b6079bd80bf..e33730b9ae901 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -2199,7 +2199,8 @@ llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { // polymorphic class type, the result refers to a std::type_info object // representing the type of the most derived object (that is, the dynamic // type) to which the glvalue refers. - if (E->isPotentiallyEvaluated()) + // If the operand is already most derived object, no need to look up vtable. + if (E->isPotentiallyEvaluated() && !E->isMostDerived(getContext())) return EmitTypeidFromVTable(*this, E->getExprOperand(), StdTypeInfoPtrTy); diff --git a/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp b/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp index f3bd7e6fd6c80..8598396f06441 100644 --- a/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp +++ b/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp @@ -46,9 +46,11 @@ const std::type_info* test4_typeid() { return &typeid(b); } const std::type_info* test5_typeid() { return &typeid(v); } // CHECK: define dso_local %struct.type_info* @"?test5_typeid@@YAPBUtype_info@@XZ"() -// CHECK: [[RT:%.*]] = call i8* @__RTtypeid(i8* bitcast (%struct.V* @"?v@@3UV@@A" to i8*)) -// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[RT]] to %struct.type_info* -// CHECK-NEXT: ret %struct.type_info* [[RET]] +// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to %struct.type_info*) + +const std::type_info *test6_typeid() { return &typeid((V &)v); } +// CHECK: define dso_local %struct.type_info* @"?test6_typeid@@YAPBUtype_info@@XZ"() +// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to %struct.type_info*) namespace PR26329 { struct Polymorphic { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits