Skip to content

Commit fbb7e63

Browse files
committed
[PowerPC] Combine sub within setcc back to sext
1 parent e5f169f commit fbb7e63

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14407,15 +14407,18 @@ SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
1440714407
ShiftCst);
1440814408
}
1440914409

14410-
SDValue PPCTargetLowering::combineSetCC(SDNode *N,
14411-
DAGCombinerInfo &DCI) const {
14412-
assert(N->getOpcode() == ISD::SETCC &&
14413-
"Should be called with a SETCC node");
14410+
SDValue PPCTargetLowering::combineSetCC(SDNode *N, DAGCombinerInfo &DCI) const {
14411+
assert(N->getOpcode() == ISD::SETCC && "Should be called with a SETCC node");
1441414412

1441514413
ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
14414+
SDValue LHS = N->getOperand(0);
14415+
SDValue RHS = N->getOperand(1);
14416+
SDLoc DL(N);
14417+
SelectionDAG &DAG = DCI.DAG;
14418+
EVT VT = N->getValueType(0);
14419+
EVT OpVT = LHS.getValueType();
14420+
1441614421
if (CC == ISD::SETNE || CC == ISD::SETEQ) {
14417-
SDValue LHS = N->getOperand(0);
14418-
SDValue RHS = N->getOperand(1);
1441914422

1442014423
// If there is a '0 - y' pattern, canonicalize the pattern to the RHS.
1442114424
if (LHS.getOpcode() == ISD::SUB && isNullConstant(LHS.getOperand(0)) &&
@@ -14426,15 +14429,29 @@ SDValue PPCTargetLowering::combineSetCC(SDNode *N,
1442614429
// x != 0-y --> x+y != 0
1442714430
if (RHS.getOpcode() == ISD::SUB && isNullConstant(RHS.getOperand(0)) &&
1442814431
RHS.hasOneUse()) {
14429-
SDLoc DL(N);
14430-
SelectionDAG &DAG = DCI.DAG;
14431-
EVT VT = N->getValueType(0);
14432-
EVT OpVT = LHS.getValueType();
1443314432
SDValue Add = DAG.getNode(ISD::ADD, DL, OpVT, LHS, RHS.getOperand(1));
1443414433
return DAG.getSetCC(DL, VT, Add, DAG.getConstant(0, DL, OpVT), CC);
1443514434
}
1443614435
}
1443714436

14437+
// Combine (a-2^(M-1)) => sext(trunc(a, M), 64)
14438+
if (CC == ISD::SETULT && LHS.getOpcode() == ISD::ADD && OpVT == MVT::i64 &&
14439+
isa<ConstantSDNode>(RHS) && isa<ConstantSDNode>(LHS.getOperand(1))) {
14440+
uint64_t ShiftVal =
14441+
~(cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) + 1;
14442+
uint64_t CmpVal = ~(cast<ConstantSDNode>(RHS)->getZExtValue()) + 1;
14443+
if (isPowerOf2_64(ShiftVal) && ShiftVal << 1 == CmpVal) {
14444+
unsigned DestBits = Log2_64(CmpVal);
14445+
if (DestBits == 8 || DestBits == 16 || DestBits == 32) {
14446+
SDValue Conv =
14447+
DAG.getSExtOrTrunc(DAG.getSExtOrTrunc(LHS.getOperand(0), DL,
14448+
MVT::getIntegerVT(DestBits)),
14449+
DL, OpVT);
14450+
return DAG.getSetCC(DL, VT, LHS.getOperand(0), Conv, ISD::SETNE);
14451+
}
14452+
}
14453+
}
14454+
1443814455
return DAGCombineTruncBoolExt(N, DCI);
1443914456
}
1444014457

llvm/test/CodeGen/PowerPC/setcc-to-sub.ll

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,48 @@ entry:
8989
ret i1 %cmp.i5
9090
}
9191

92+
define zeroext i1 @test5(i64 %a) {
93+
; CHECK-LABEL: test5:
94+
; CHECK: # %bb.0: # %entry
95+
; CHECK-NEXT: extsw 4, 3
96+
; CHECK-NEXT: xor 3, 3, 4
97+
; CHECK-NEXT: addic 4, 3, -1
98+
; CHECK-NEXT: subfe 3, 4, 3
99+
; CHECK-NEXT: blr
100+
entry:
101+
%0 = add i64 %a, -2147483648
102+
%cmp = icmp ult i64 %0, -4294967296
103+
ret i1 %cmp
104+
}
105+
106+
define zeroext i1 @test6(i64 %a) {
107+
; CHECK-LABEL: test6:
108+
; CHECK: # %bb.0: # %entry
109+
; CHECK-NEXT: extsh 4, 3
110+
; CHECK-NEXT: xor 3, 3, 4
111+
; CHECK-NEXT: addic 4, 3, -1
112+
; CHECK-NEXT: subfe 3, 4, 3
113+
; CHECK-NEXT: blr
114+
entry:
115+
%0 = add i64 %a, -32768
116+
%cmp = icmp ult i64 %0, -65536
117+
ret i1 %cmp
118+
}
119+
120+
define zeroext i1 @test7(i64 %a) {
121+
; CHECK-LABEL: test7:
122+
; CHECK: # %bb.0: # %entry
123+
; CHECK-NEXT: extsb 4, 3
124+
; CHECK-NEXT: xor 3, 3, 4
125+
; CHECK-NEXT: addic 4, 3, -1
126+
; CHECK-NEXT: subfe 3, 4, 3
127+
; CHECK-NEXT: blr
128+
entry:
129+
%0 = add i64 %a, -128
130+
%cmp = icmp ult i64 %0, -256
131+
ret i1 %cmp
132+
}
133+
92134
!1 = !{!2, !2, i64 0}
93135
!2 = !{!"int", !3, i64 0}
94136
!3 = !{!"omnipotent char", !4, i64 0}

0 commit comments

Comments
 (0)