[llvm] 66d78d0 - [PowerPC] fix Inefficient std::bit_floor(x) (#183361)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 06:58:36 PDT 2026


Author: zhijian lin
Date: 2026-04-14T09:58:31-04:00
New Revision: 66d78d00e517f75b4011ce669d1319289876bb7b

URL: https://github.com/llvm/llvm-project/commit/66d78d00e517f75b4011ce669d1319289876bb7b
DIFF: https://github.com/llvm/llvm-project/commit/66d78d00e517f75b4011ce669d1319289876bb7b.diff

LOG: [PowerPC] fix Inefficient std::bit_floor(x)  (#183361)

Fix  Inefficient asm of std::bit_floor(x) for powerpc.

Added: 
    

Modified: 
    llvm/lib/Target/PowerPC/PPCISelLowering.cpp
    llvm/test/CodeGen/PowerPC/bit_floor.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index d21ccb59f9962..96c0339603031 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -17269,6 +17269,81 @@ static SDValue DAGCombineAddc(SDNode *N,
   return SDValue();
 }
 
+/// Optimize the bitfloor(X) pattern for PowerPC.
+/// Transforms: select_cc X, 0, 0, (srl MinSignedValue, (ctlz X)), seteq
+/// Into: srl MinSignedValue, (ctlz X)
+///
+/// This is safe on PowerPC because the srw instruction returns 0 when the
+/// shift amount is == bitwidth, which matches the behavior we need for X=0.
+static SDValue combineSELECT_CCBitFloor(SDNode *N, SelectionDAG &DAG) {
+  if (N->getOpcode() != ISD::SELECT_CC)
+    return SDValue();
+
+  // SELECT_CC operands: LHS, RHS, TrueVal, FalseVal, CC
+  SDValue CmpLHS = N->getOperand(0);
+  SDValue CmpRHS = N->getOperand(1);
+  SDValue TrueVal = N->getOperand(2);
+  SDValue FalseVal = N->getOperand(3);
+  ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
+
+  // Check if condition is (X == 0)
+  if (CC != ISD::SETEQ || !isNullConstant(CmpRHS))
+    return SDValue();
+
+  // Check if TrueVal is constant 0
+  if (!isNullConstant(TrueVal))
+    return SDValue();
+
+  // This combine is replacing a select_cc with a PPC srl, not an srl with a
+  // PPC srl. If the original srl had multiple uses it would just remain in the
+  // code. This is at most a performance consideration.
+  if (FalseVal.getOpcode() != ISD::SRL || !FalseVal.hasOneUse())
+    return SDValue();
+
+  SDValue ShiftVal = FalseVal.getOperand(0);
+  SDValue ShiftAmt = FalseVal.getOperand(1);
+
+  // Check if ShiftVal is MinSignedValue
+  auto *ShiftConst = dyn_cast<ConstantSDNode>(ShiftVal);
+  if (!ShiftConst || !ShiftConst->getAPIntValue().isMinSignedValue())
+    return SDValue();
+
+  SDValue CtlzArg;
+  // Check if ShiftAmt is (ctlz CmpLHS) or (truncate (ctlz ...))
+  if (ShiftAmt.getOpcode() != ISD::CTLZ) {
+    // Look through truncate if present (for i64 ctlz truncated to i32 shift
+    // amount)
+    if (ShiftAmt.getOpcode() != ISD::TRUNCATE)
+      return SDValue();
+
+    // Verify the truncate target type is appropriate for shift amount (i32, not
+    // i1 or other)
+    if (ShiftAmt.getValueType() != MVT::i32)
+      return SDValue();
+
+    SDValue CtlzNode = ShiftAmt.getOperand(0);
+
+    if (CtlzNode.getOpcode() != ISD::CTLZ)
+      return SDValue();
+
+    CtlzArg = CtlzNode.getOperand(0);
+  } else {
+    CtlzArg = ShiftAmt.getOperand(0);
+  }
+
+  // Check if ctlz operates on the same value as the comparison
+  if (CtlzArg != CmpLHS)
+    return SDValue();
+
+  // Using PPCISD::SRL to ensure well-defined behavior.
+  // On PowerPC, PPCISD::SRL guarantees that shift by bitwidth returns 0,
+  // which is exactly what we need for the bitfloor(0) case.
+  SDLoc DL(N);
+  SDValue PPCSrl =
+      DAG.getNode(PPCISD::SRL, DL, FalseVal.getValueType(), ShiftVal, ShiftAmt);
+  return PPCSrl;
+}
+
 // Optimize zero-extension of setcc when the compared value is known to be 0
 // or 1.
 //
@@ -17505,6 +17580,8 @@ SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
       return CSCC;
     [[fallthrough]];
   case ISD::SELECT_CC:
+    if (SDValue V = combineSELECT_CCBitFloor(N, DAG))
+      return V;
     return DAGCombineTruncBoolExt(N, DCI);
   case ISD::SINT_TO_FP:
   case ISD::UINT_TO_FP:

diff  --git a/llvm/test/CodeGen/PowerPC/bit_floor.ll b/llvm/test/CodeGen/PowerPC/bit_floor.ll
index 916ef0a9451c4..2a2ef30eb5630 100644
--- a/llvm/test/CodeGen/PowerPC/bit_floor.ll
+++ b/llvm/test/CodeGen/PowerPC/bit_floor.ll
@@ -6,29 +6,23 @@
 define noundef range(i32 0, -2147483647) i32 @_Z10bitfloor32j(i32 noundef %x) local_unnamed_addr {
 ; AIX32-LABEL: _Z10bitfloor32j:
 ; AIX32:       # %bb.0: # %entry
-; AIX32-NEXT:    cntlzw r4, r3
-; AIX32-NEXT:    lis r5, -32768
-; AIX32-NEXT:    cmplwi r3, 0
-; AIX32-NEXT:    srw r4, r5, r4
-; AIX32-NEXT:    iseleq r3, 0, r4
+; AIX32-NEXT:    cntlzw r3, r3
+; AIX32-NEXT:    lis r4, -32768
+; AIX32-NEXT:    srw r3, r4, r3
 ; AIX32-NEXT:    blr
 ;
 ; AIX64-LABEL: _Z10bitfloor32j:
 ; AIX64:       # %bb.0: # %entry
-; AIX64-NEXT:    cntlzw r4, r3
-; AIX64-NEXT:    lis r5, -32768
-; AIX64-NEXT:    cmplwi r3, 0
-; AIX64-NEXT:    srw r4, r5, r4
-; AIX64-NEXT:    iseleq r3, 0, r4
+; AIX64-NEXT:    cntlzw r3, r3
+; AIX64-NEXT:    lis r4, -32768
+; AIX64-NEXT:    srw r3, r4, r3
 ; AIX64-NEXT:    blr
 ;
 ; LNX64-LABEL: _Z10bitfloor32j:
 ; LNX64:       # %bb.0: # %entry
-; LNX64-NEXT:    cntlzw r4, r3
-; LNX64-NEXT:    cmplwi r3, 0
-; LNX64-NEXT:    lis r5, -32768
-; LNX64-NEXT:    srw r4, r5, r4
-; LNX64-NEXT:    iseleq r3, 0, r4
+; LNX64-NEXT:    cntlzw r3, r3
+; LNX64-NEXT:    lis r4, -32768
+; LNX64-NEXT:    srw r3, r4, r3
 ; LNX64-NEXT:    blr
 entry:
   %cmp.i = icmp eq i32 %x, 0
@@ -62,22 +56,18 @@ define noundef range(i64 0, -9223372036854775807) i64 @_Z10bitfloor64y(i64 nound
 ;
 ; AIX64-LABEL: _Z10bitfloor64y:
 ; AIX64:       # %bb.0: # %entry
-; AIX64-NEXT:    li r5, 1
-; AIX64-NEXT:    cntlzd r4, r3
-; AIX64-NEXT:    cmpldi r3, 0
-; AIX64-NEXT:    rldic r5, r5, 63, 0
-; AIX64-NEXT:    srd r4, r5, r4
-; AIX64-NEXT:    iseleq r3, 0, r4
+; AIX64-NEXT:    li r4, 1
+; AIX64-NEXT:    cntlzd r3, r3
+; AIX64-NEXT:    rldic r4, r4, 63, 0
+; AIX64-NEXT:    srd r3, r4, r3
 ; AIX64-NEXT:    blr
 ;
 ; LNX64-LABEL: _Z10bitfloor64y:
 ; LNX64:       # %bb.0: # %entry
-; LNX64-NEXT:    li r5, 1
-; LNX64-NEXT:    cntlzd r4, r3
-; LNX64-NEXT:    cmpldi r3, 0
-; LNX64-NEXT:    rldic r5, r5, 63, 0
-; LNX64-NEXT:    srd r4, r5, r4
-; LNX64-NEXT:    iseleq r3, 0, r4
+; LNX64-NEXT:    li r4, 1
+; LNX64-NEXT:    cntlzd r3, r3
+; LNX64-NEXT:    rldic r4, r4, 63, 0
+; LNX64-NEXT:    srd r3, r4, r3
 ; LNX64-NEXT:    blr
 entry:
   %cmp.i = icmp eq i64 %x, 0


        


More information about the llvm-commits mailing list