[llvm] [PowerPC] Combine sub within setcc back to sext (PR #66978)
Qiu Chaofan via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 31 09:46:08 PST 2024
https://github.com/ecnelises updated https://github.com/llvm/llvm-project/pull/66978
>From 5f17702364dffc36066b471aafaf3757709454af Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Wed, 6 Sep 2023 16:16:34 +0800
Subject: [PATCH] [PowerPC] Combine sub within setcc back to sext
---
llvm/lib/Target/PowerPC/PPCISelLowering.cpp | 60 +++++++++++++++++----
llvm/test/CodeGen/PowerPC/setcc-to-sub.ll | 51 ++++++++----------
2 files changed, 72 insertions(+), 39 deletions(-)
diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index 4c24f9e1c8709..ff92c6bb54b3d 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -14406,15 +14406,18 @@ SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
ShiftCst);
}
-SDValue PPCTargetLowering::combineSetCC(SDNode *N,
- DAGCombinerInfo &DCI) const {
- assert(N->getOpcode() == ISD::SETCC &&
- "Should be called with a SETCC node");
+SDValue PPCTargetLowering::combineSetCC(SDNode *N, DAGCombinerInfo &DCI) const {
+ assert(N->getOpcode() == ISD::SETCC && "Should be called with a SETCC node");
ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
+ SDValue LHS = N->getOperand(0);
+ SDValue RHS = N->getOperand(1);
+ SDLoc DL(N);
+ SelectionDAG &DAG = DCI.DAG;
+ EVT VT = N->getValueType(0);
+ EVT OpVT = LHS.getValueType();
+
if (CC == ISD::SETNE || CC == ISD::SETEQ) {
- SDValue LHS = N->getOperand(0);
- SDValue RHS = N->getOperand(1);
// If there is a '0 - y' pattern, canonicalize the pattern to the RHS.
if (LHS.getOpcode() == ISD::SUB && isNullConstant(LHS.getOperand(0)) &&
@@ -14425,15 +14428,52 @@ SDValue PPCTargetLowering::combineSetCC(SDNode *N,
// x != 0-y --> x+y != 0
if (RHS.getOpcode() == ISD::SUB && isNullConstant(RHS.getOperand(0)) &&
RHS.hasOneUse()) {
- SDLoc DL(N);
- SelectionDAG &DAG = DCI.DAG;
- EVT VT = N->getValueType(0);
- EVT OpVT = LHS.getValueType();
SDValue Add = DAG.getNode(ISD::ADD, DL, OpVT, LHS, RHS.getOperand(1));
return DAG.getSetCC(DL, VT, Add, DAG.getConstant(0, DL, OpVT), CC);
}
}
+ if (CC == ISD::SETULT && isa<ConstantSDNode>(RHS)) {
+ uint64_t RHSVal = cast<ConstantSDNode>(RHS)->getZExtValue();
+ if (LHS.getOpcode() == ISD::ADD && isa<ConstantSDNode>(LHS.getOperand(1))) {
+ uint64_t Addend = cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue();
+ if (OpVT == MVT::i64) {
+ uint64_t ShiftVal = ~Addend + 1;
+ uint64_t CmpVal = ~RHSVal + 1;
+ if (isPowerOf2_64(ShiftVal) && ShiftVal << 1 == CmpVal) {
+ unsigned DestBits = Log2_64(CmpVal);
+ if (DestBits == 8 || DestBits == 16 || DestBits == 32) {
+ SDValue Conv = DAG.getSExtOrTrunc(
+ DAG.getSExtOrTrunc(LHS.getOperand(0), DL,
+ MVT::getIntegerVT(DestBits)),
+ DL, OpVT);
+ return DAG.getSetCC(DL, VT, LHS.getOperand(0), Conv, ISD::SETNE);
+ }
+ }
+ } else if (OpVT == MVT::i32) {
+ if (RHSVal == 0xffffff00 && Addend == 0xffffff80) {
+ SDValue Conv = DAG.getSExtOrTrunc(
+ DAG.getSExtOrTrunc(LHS.getOperand(0), DL, MVT::i8), DL, OpVT);
+ return DAG.getSetCC(DL, VT, LHS.getOperand(0), Conv, ISD::SETNE);
+ }
+ }
+ } else if (LHS.getOpcode() == ISD::SRL &&
+ LHS.getOperand(0).getOpcode() == ISD::ADD &&
+ isa<ConstantSDNode>(LHS.getOperand(1)) &&
+ isa<ConstantSDNode>(LHS.getOperand(0).getOperand(1))) {
+ if (RHSVal == 0xffff &&
+ cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 16 &&
+ cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))
+ ->getZExtValue() == 0xffff8000) {
+ SDValue Conv = DAG.getSExtOrTrunc(
+ DAG.getSExtOrTrunc(LHS.getOperand(0).getOperand(0), DL, MVT::i16),
+ DL, OpVT);
+ return DAG.getSetCC(DL, VT, LHS.getOperand(0).getOperand(0), Conv,
+ ISD::SETNE);
+ }
+ }
+ }
+
return DAGCombineTruncBoolExt(N, DCI);
}
diff --git a/llvm/test/CodeGen/PowerPC/setcc-to-sub.ll b/llvm/test/CodeGen/PowerPC/setcc-to-sub.ll
index 20dcb8ccf4908..1e4d2db2509b2 100644
--- a/llvm/test/CodeGen/PowerPC/setcc-to-sub.ll
+++ b/llvm/test/CodeGen/PowerPC/setcc-to-sub.ll
@@ -92,12 +92,10 @@ entry:
define zeroext i1 @test5(i64 %a) {
; CHECK-LABEL: test5:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: li 4, -1
-; CHECK-NEXT: addis 3, 3, -32768
-; CHECK-NEXT: rldic 4, 4, 32, 0
-; CHECK-NEXT: subc 4, 3, 4
-; CHECK-NEXT: subfe 3, 3, 3
-; CHECK-NEXT: neg 3, 3
+; CHECK-NEXT: extsw 4, 3
+; CHECK-NEXT: xor 3, 3, 4
+; CHECK-NEXT: addic 4, 3, -1
+; CHECK-NEXT: subfe 3, 4, 3
; CHECK-NEXT: blr
entry:
%0 = add i64 %a, -2147483648
@@ -108,11 +106,10 @@ entry:
define zeroext i1 @test6(i64 %a) {
; CHECK-LABEL: test6:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: addi 3, 3, -32768
-; CHECK-NEXT: lis 4, -1
-; CHECK-NEXT: subc 4, 3, 4
-; CHECK-NEXT: subfe 3, 3, 3
-; CHECK-NEXT: neg 3, 3
+; CHECK-NEXT: extsh 4, 3
+; CHECK-NEXT: xor 3, 3, 4
+; CHECK-NEXT: addic 4, 3, -1
+; CHECK-NEXT: subfe 3, 4, 3
; CHECK-NEXT: blr
entry:
%0 = add i64 %a, -32768
@@ -123,11 +120,10 @@ entry:
define zeroext i1 @test7(i64 %a) {
; CHECK-LABEL: test7:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: addi 3, 3, -128
-; CHECK-NEXT: li 4, -256
-; CHECK-NEXT: subc 4, 3, 4
-; CHECK-NEXT: subfe 3, 3, 3
-; CHECK-NEXT: neg 3, 3
+; CHECK-NEXT: extsb 4, 3
+; CHECK-NEXT: xor 3, 3, 4
+; CHECK-NEXT: addic 4, 3, -1
+; CHECK-NEXT: subfe 3, 4, 3
; CHECK-NEXT: blr
entry:
%0 = add i64 %a, -128
@@ -138,12 +134,11 @@ entry:
define zeroext i1 @test8(i32 %a) {
; CHECK-LABEL: test8:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: addi 3, 3, -32768
-; CHECK-NEXT: lis 4, -1
-; CHECK-NEXT: rlwinm 3, 3, 16, 16, 31
-; CHECK-NEXT: ori 4, 4, 1
-; CHECK-NEXT: add 3, 3, 4
-; CHECK-NEXT: rldicl 3, 3, 1, 63
+; CHECK-NEXT: extsh 4, 3
+; CHECK-NEXT: xor 3, 3, 4
+; CHECK-NEXT: cntlzw 3, 3
+; CHECK-NEXT: srwi 3, 3, 5
+; CHECK-NEXT: xori 3, 3, 1
; CHECK-NEXT: blr
entry:
%0 = add i32 %a, -32768
@@ -154,13 +149,11 @@ entry:
define zeroext i1 @test9(i32 %a) {
; CHECK-LABEL: test9:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: lis 4, -256
-; CHECK-NEXT: addi 3, 3, -128
-; CHECK-NEXT: ori 4, 4, 1
-; CHECK-NEXT: clrldi 3, 3, 32
-; CHECK-NEXT: rldic 4, 4, 8, 0
-; CHECK-NEXT: add 3, 3, 4
-; CHECK-NEXT: rldicl 3, 3, 1, 63
+; CHECK-NEXT: extsb 4, 3
+; CHECK-NEXT: xor 3, 3, 4
+; CHECK-NEXT: cntlzw 3, 3
+; CHECK-NEXT: srwi 3, 3, 5
+; CHECK-NEXT: xori 3, 3, 1
; CHECK-NEXT: blr
entry:
%0 = add i32 %a, -128
More information about the llvm-commits
mailing list