Skip to content

Conversation

@tbaederr
Copy link
Contributor

@tbaederr tbaederr commented May 7, 2025

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

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels May 7, 2025
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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 --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();

@tbaederr tbaederr force-pushed the integral branch 3 times, most recently from f607c4d to ae3e30c Compare May 8, 2025 10:43
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.

Also, switch classifyPrim() when it's possible and use castAs() instead
of getAs() when we know the type is a MemberPointerType.
@tbaederr tbaederr merged commit 7439d7b into llvm:main May 9, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants