[clang] [clang][Sema] Create ConstantExprs in AnalyzeComparison (PR #151464)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 31 00:09:43 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/151464
If the `getIntegerConstantExpr()` calls succeed, we can as well replace the `BinaryOperator` children with `ConstantExpr`s wrapping the previous LHS/RHS.
This shows small speedups: https://llvm-compile-time-tracker.com/compare.php?from=cc8c941e17558ba427de06e72c8ad96d7b17ced1&to=112af8e62e734938547d50eeb7b416c8dd666f45&stat=instructions:u
>From b5264dbf5e34e16d20967d69689600ad56870dfb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 31 Jul 2025 06:09:27 +0200
Subject: [PATCH] ConstantExpr
---
clang/lib/Sema/SemaChecking.cpp | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index c74b67106ad74..9b2eed3f5270f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11374,11 +11374,27 @@ static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
LHS->getIntegerConstantExpr(S.Context);
// We don't care about expressions whose result is a constant.
- if (RHSValue && LHSValue)
+ if (RHSValue && LHSValue) {
+ auto *LHSCE = ConstantExpr::Create(S.Context, LHS, APValue(*LHSValue));
+ auto *RHSCE = ConstantExpr::Create(S.Context, RHS, APValue(*RHSValue));
+ E->setLHS(LHSCE);
+ E->setRHS(RHSCE);
return AnalyzeImpConvsInComparison(S, E);
+ }
// We only care about expressions where just one side is literal
if ((bool)RHSValue ^ (bool)LHSValue) {
+
+ if (LHSValue) {
+ auto *LHSCE = ConstantExpr::Create(S.Context, LHS, APValue(*LHSValue));
+ E->setLHS(LHSCE);
+ LHS = LHSCE;
+ } else if (RHSValue) {
+ auto *RHSCE = ConstantExpr::Create(S.Context, RHS, APValue(*RHSValue));
+ E->setRHS(RHSCE);
+ RHS = RHSCE;
+ }
+
// Is the constant on the RHS or LHS?
const bool RhsConstant = (bool)RHSValue;
Expr *Const = RhsConstant ? RHS : LHS;
More information about the cfe-commits
mailing list