[llvm] 954a15d - [SelectionDAG] Check use before combining into USUBSAT

Qiu Chaofan via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 12 23:53:17 PDT 2021


Author: Qiu Chaofan
Date: 2021-07-13T14:50:26+08:00
New Revision: 954a15d6398edbb839d4b842534b50e85d4aa8cd

URL: https://github.com/llvm/llvm-project/commit/954a15d6398edbb839d4b842534b50e85d4aa8cd
DIFF: https://github.com/llvm/llvm-project/commit/954a15d6398edbb839d4b842534b50e85d4aa8cd.diff

LOG: [SelectionDAG] Check use before combining into USUBSAT

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D105789

Added: 
    

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/test/CodeGen/PowerPC/sat-add.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 1a47af6fc16f..1d89ef83243a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -3209,7 +3209,7 @@ SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N) {
 
   // Try to find umax(a,b) - b or a - umin(a,b) patterns
   // they may be converted to usubsat(a,b).
-  if (Op0.getOpcode() == ISD::UMAX) {
+  if (Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
     SDValue MaxLHS = Op0.getOperand(0);
     SDValue MaxRHS = Op0.getOperand(1);
     if (MaxLHS == Op1)
@@ -3218,7 +3218,7 @@ SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N) {
       return getTruncatedUSUBSAT(DstVT, SubVT, MaxLHS, Op1, DAG, SDLoc(N));
   }
 
-  if (Op1.getOpcode() == ISD::UMIN) {
+  if (Op1.getOpcode() == ISD::UMIN && Op1.hasOneUse()) {
     SDValue MinLHS = Op1.getOperand(0);
     SDValue MinRHS = Op1.getOperand(1);
     if (MinLHS == Op0)
@@ -3229,7 +3229,8 @@ SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N) {
 
   // sub(a,trunc(umin(zext(a),b))) -> usubsat(a,trunc(umin(b,SatLimit)))
   if (Op1.getOpcode() == ISD::TRUNCATE &&
-      Op1.getOperand(0).getOpcode() == ISD::UMIN) {
+      Op1.getOperand(0).getOpcode() == ISD::UMIN &&
+      Op1.getOperand(0).hasOneUse()) {
     SDValue MinLHS = Op1.getOperand(0).getOperand(0);
     SDValue MinRHS = Op1.getOperand(0).getOperand(1);
     if (MinLHS.getOpcode() == ISD::ZERO_EXTEND && MinLHS.getOperand(0) == Op0)

diff  --git a/llvm/test/CodeGen/PowerPC/sat-add.ll b/llvm/test/CodeGen/PowerPC/sat-add.ll
index 403cde6c75b9..6f23d316d8ce 100644
--- a/llvm/test/CodeGen/PowerPC/sat-add.ll
+++ b/llvm/test/CodeGen/PowerPC/sat-add.ll
@@ -864,3 +864,33 @@ define <4 x i128> @sadd(<4 x i128> %a, <4 x i128> %b) local_unnamed_addr {
   ret <4 x i128> %c
 }
 
+define i64 @unsigned_sat_constant_i64_with_single_use(i64 %x) {
+; CHECK-LABEL: unsigned_sat_constant_i64_with_single_use:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    addi 4, 3, -4
+; CHECK-NEXT:    cmpld 4, 3
+; CHECK-NEXT:    iselgt 3, 0, 4
+; CHECK-NEXT:    blr
+  %umin = call i64 @llvm.umin.i64(i64 %x, i64 4)
+  %sub = sub i64 %x, %umin
+  ret i64 %sub
+}
+
+define i64 @unsigned_sat_constant_i64_with_multiple_use(i64 %x, i64 %y) {
+; CHECK-LABEL: unsigned_sat_constant_i64_with_multiple_use:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    li 5, 4
+; CHECK-NEXT:    cmpldi 3, 4
+; CHECK-NEXT:    isellt 5, 3, 5
+; CHECK-NEXT:    sub 3, 3, 5
+; CHECK-NEXT:    add 4, 4, 5
+; CHECK-NEXT:    mulld 3, 3, 4
+; CHECK-NEXT:    blr
+  %umin = call i64 @llvm.umin.i64(i64 %x, i64 4)
+  %sub = sub i64 %x, %umin
+  %add = add i64 %y, %umin
+  %res = mul i64 %sub, %add
+  ret i64 %res
+}
+
+declare i64 @llvm.umin.i64(i64, i64)


        


More information about the llvm-commits mailing list