[llvm] [DAG] SelectionDAG::isKnownToBeAPowerOfTwo - add ISD::TRUNCATE handling and tests (PR #184365)

Pranshu Goyal via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 07:53:22 PST 2026


https://github.com/pranshoe updated https://github.com/llvm/llvm-project/pull/184365

>From 98d86d4764506d3384dd8466d6c6ac9041a1dbcb Mon Sep 17 00:00:00 2001
From: pranshoe <pranshu.goyal71 at gmail.com>
Date: Tue, 3 Mar 2026 15:45:18 +0000
Subject: [PATCH] [DAG] SelectionDAG::isKnownToBeAPowerOfTwo - add
 ISD::TRUNCATE handling and tests

---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp |  3 ++
 llvm/test/CodeGen/X86/known-pow2.ll           | 35 +++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 507cd27b7f501..faad46ea55a3a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4780,6 +4780,9 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
     // vscale(power-of-two) is a power-of-two
     return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
                                   Depth + 1);
+
+  case ISD::TRUNCATE:
+    return isKnownToBeAPowerOfTwo(Val.getOperand(0), OrZero, Depth + 1);
   }
 
   // More could be done here, though the above checks are enough
diff --git a/llvm/test/CodeGen/X86/known-pow2.ll b/llvm/test/CodeGen/X86/known-pow2.ll
index 2457f3344592c..847cc2756f548 100644
--- a/llvm/test/CodeGen/X86/known-pow2.ll
+++ b/llvm/test/CodeGen/X86/known-pow2.ll
@@ -1006,3 +1006,38 @@ define i32 @pow2_blsi_sub(i32 %x, i32 %a) {
   %r = and i32 %x_sub_y, %y
   ret i32 %r
 }
+
+define i8 @pow2_trunc_fail(i32 %x, i32 %a){
+; CHECK-LABEL: pow2_trunc_fail:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    subb %sil, %al
+; CHECK-NEXT:    andb %sil, %al
+; CHECK-NEXT:    # kill: def $al killed $al killed $eax
+; CHECK-NEXT:    retq
+  %y = and i32 %a, 255
+  %x8 = trunc i32 %x to i8
+  %y8 = trunc i32 %y to i8
+  %x_sub_y = sub i8 %x8, %y8
+  %r = and i8 %x_sub_y, %y8
+  ret i8 %r
+}
+
+define i8 @pow2_trunc(i32 %x, i32 %a){
+; CHECK-LABEL: pow2_trunc:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %esi, %eax
+; CHECK-NEXT:    negl %eax
+; CHECK-NEXT:    andl %esi, %eax
+; CHECK-NEXT:    notb %dil
+; CHECK-NEXT:    andb %dil, %al
+; CHECK-NEXT:    # kill: def $al killed $al killed $eax
+; CHECK-NEXT:    retq
+  %neg_a = sub i32 0, %a
+  %y = and i32 %a, %neg_a
+  %x8 = trunc i32 %x to i8
+  %y8 = trunc i32 %y to i8
+  %x_sub_y = sub i8 %x8, %y8
+  %r = and i8 %x_sub_y, %y8
+  ret i8 %r
+}



More information about the llvm-commits mailing list