llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) <details> <summary>Changes</summary> We often see initializers like unsigned a = 10; which take an integer literal and immediately cast it to another type. Recognize this pattern and omit the cast, simply emitting the value as a different type directly. This reduces the instruction count by up to 0.13%: http://llvm-compile-time-tracker.com/compare.php?from=303436c6d16518b35288d63a859506ffcc1681e4&to=648f5202f906d1606390b2d1081e4502dc74acc2&stat=instructions:u --- Full diff: https://github.com/llvm/llvm-project/pull/138879.diff 1 Files Affected: - (modified) clang/lib/AST/ByteCode/Compiler.cpp (+15-6) ``````````diff diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index ae6574cf99159..524473ea17a03 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -486,9 +486,24 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) { std::optional<PrimType> FromT = classify(SubExpr->getType()); std::optional<PrimType> ToT = classify(CE->getType()); + auto maybeNegate = [&]() -> bool { + if (CE->getCastKind() == CK_BooleanToSignedIntegral) + return this->emitNeg(*ToT, CE); + return true; + }; + if (!FromT || !ToT) return false; + // Small performance optimization: If the integral cast just casts the + // value of an IntegerLiteral, do that directly. Initializers often + // contain large quantities of these. + if (ToT != PT_IntAP && ToT != PT_IntAPS && + !CE->getType()->isEnumeralType()) { + if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) + return this->emitConst(IL->getValue(), CE) && maybeNegate(); + } + if (!this->visit(SubExpr)) return false; @@ -502,12 +517,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) { } } - auto maybeNegate = [&]() -> bool { - if (CE->getCastKind() == CK_BooleanToSignedIntegral) - return this->emitNeg(*ToT, CE); - return true; - }; - if (ToT == PT_IntAP) return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE) && maybeNegate(); `````````` </details> https://github.com/llvm/llvm-project/pull/138879 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits