[llvm] [SimplifyCFG] Handle trunc condition in foldBranchToCommonDest. (PR #135490)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 12 03:42:21 PDT 2025


https://github.com/andjo403 created https://github.com/llvm/llvm-project/pull/135490

Noticed regressions due to this missing fold when trying to add the fold `icmp ne (and X, 1), 0 --> trunc X to i1`

proof: https://alive2.llvm.org/ce/z/v32Aof

>From f2bf8e222e7d0c9db1747ec43ffb39ca242d30d2 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Sat, 12 Apr 2025 12:36:52 +0200
Subject: [PATCH] [SimplifyCFG] Handle trunc condition in
 foldBranchToCommonDest.

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp                    | 4 +---
 .../Transforms/SimplifyCFG/fold-branch-to-common-dest.ll     | 5 ++---
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index eac7e7c209c95..7f53aa7d4f73d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4087,9 +4087,7 @@ bool llvm::foldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
 
   Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
 
-  if (!Cond ||
-      (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond) &&
-       !isa<SelectInst>(Cond)) ||
+  if (!Cond || !isa<CmpInst, BinaryOperator, SelectInst, TruncInst>(Cond) ||
       Cond->getParent() != BB || !Cond->hasOneUse())
     return false;
 
diff --git a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
index 37f48a9a7e03a..7b88ec338cf5e 100644
--- a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
+++ b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
@@ -88,10 +88,9 @@ define void @one_pred_trunc_cond(i8 %v0, i8 %v1) {
 ; CHECK-LABEL: @one_pred_trunc_cond(
 ; CHECK-NEXT:  pred:
 ; CHECK-NEXT:    [[C0:%.*]] = icmp eq i8 [[V0:%.*]], 0
-; CHECK-NEXT:    br i1 [[C0]], label [[DISPATCH:%.*]], label [[FINAL_RIGHT:%.*]]
-; CHECK:       dispatch:
 ; CHECK-NEXT:    [[C1:%.*]] = trunc i8 [[V1:%.*]] to i1
-; CHECK-NEXT:    br i1 [[C1]], label [[FINAL_LEFT:%.*]], label [[FINAL_RIGHT]]
+; CHECK-NEXT:    [[OR_COND:%.*]] = select i1 [[C0]], i1 [[C1]], i1 false
+; CHECK-NEXT:    br i1 [[OR_COND]], label [[FINAL_LEFT:%.*]], label [[FINAL_RIGHT:%.*]]
 ; CHECK:       common.ret:
 ; CHECK-NEXT:    ret void
 ; CHECK:       final_left:



More information about the llvm-commits mailing list