Skip to content

Commit 525bdfb

Browse files
committed
[MIPS]Optimize (signext (xor (trunc X, i32), -1), i64) to (xor (X, -1))
The constant operand of the XOR is an i32, which makes the two operands being truncated to i32, and sign extended to i64 afterwards. Since both of the i32 operands are sign extended to i64 before performing the TRUNCATE operation, we just remove the unnecessary TRUNCATE and SIGN_EXTEND. Fix #99783
1 parent 8035d38 commit 525bdfb

File tree

4 files changed

+297
-158
lines changed

4 files changed

+297
-158
lines changed

llvm/lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
517517
setOperationAction(ISD::TRAP, MVT::Other, Legal);
518518

519519
setTargetDAGCombine({ISD::SDIVREM, ISD::UDIVREM, ISD::SELECT, ISD::AND,
520-
ISD::OR, ISD::ADD, ISD::SUB, ISD::AssertZext, ISD::SHL});
520+
ISD::OR, ISD::ADD, ISD::SUB, ISD::AssertZext, ISD::SHL,
521+
ISD::SIGN_EXTEND});
521522

522523
if (Subtarget.isGP64bit())
523524
setMaxAtomicSizeInBitsSupported(64);
@@ -1210,6 +1211,29 @@ static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
12101211
DAG.getConstant(SMSize, DL, MVT::i32));
12111212
}
12121213

1214+
static SDValue performSignExtendCombine(SDNode *N, SelectionDAG &DAG) {
1215+
SDValue N0 = N->getOperand(0);
1216+
EVT VT = N->getValueType(0);
1217+
SDValue TruncateOperand = N0.getOperand(0).getOperand(0);
1218+
1219+
// Pattern match XOR.
1220+
// $dst = (sign_extend (xor (trunc $src, i32), -1), i64)
1221+
// => $dst = (xor ($src, -1), i64)
1222+
if (N0.getOpcode() == ISD::XOR &&
1223+
N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1224+
N0.getOperand(1).getOpcode() == ISD::Constant) {
1225+
if (VT == MVT::i64 && VT == TruncateOperand->getValueType(0)) {
1226+
APInt MinusOne(32, -1, true);
1227+
if (N0.getConstantOperandAPInt(1) == MinusOne) {
1228+
return DAG.getNode(ISD::XOR, SDLoc(N0), VT, TruncateOperand,
1229+
DAG.getTargetConstant(-1, SDLoc(N0), VT));
1230+
}
1231+
}
1232+
}
1233+
1234+
return SDValue();
1235+
}
1236+
12131237
SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12141238
const {
12151239
SelectionDAG &DAG = DCI.DAG;
@@ -1235,6 +1259,8 @@ SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12351259
return performSHLCombine(N, DAG, DCI, Subtarget);
12361260
case ISD::SUB:
12371261
return performSUBCombine(N, DAG, DCI, Subtarget);
1262+
case ISD::SIGN_EXTEND:
1263+
return performSignExtendCombine(N, DAG);
12381264
}
12391265

12401266
return SDValue();

0 commit comments

Comments
 (0)