Skip to content

Commit 013a7b7

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 013a7b7

File tree

4 files changed

+309
-158
lines changed

4 files changed

+309
-158
lines changed

llvm/lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 39 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,41 @@ static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
12101211
DAG.getConstant(SMSize, DL, MVT::i32));
12111212
}
12121213

1214+
static SDValue performSignExtendCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI,const MipsSubtarget &Subtarget) {
1215+
if (DCI.Level != AfterLegalizeDAG) {
1216+
return SDValue();
1217+
}
1218+
1219+
if (!Subtarget.isGP64bit()) {
1220+
return SDValue();
1221+
}
1222+
1223+
SDValue N0 = N->getOperand(0);
1224+
EVT VT = N->getValueType(0);
1225+
1226+
if (N0->getNumOperands() != 2) {
1227+
return SDValue();
1228+
}
1229+
1230+
// Pattern match XOR.
1231+
// $dst = (sign_extend (xor (trunc $src, i32), -1), i64)
1232+
// => $dst = (xor ($src, -1), i64)
1233+
if (N0.getOpcode() == ISD::XOR &&
1234+
N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1235+
N0.getOperand(1).getOpcode() == ISD::Constant) {
1236+
SDValue TruncateOperand = N0.getOperand(0).getOperand(0);
1237+
if (VT == MVT::i64 && VT == TruncateOperand->getValueType(0)) {
1238+
APInt MinusOne(32, -1, true);
1239+
if (N0.getConstantOperandAPInt(1) == MinusOne) {
1240+
return DAG.getNode(ISD::XOR, SDLoc(N0), VT, TruncateOperand,
1241+
DAG.getTargetConstant(-1, SDLoc(N0), VT));
1242+
}
1243+
}
1244+
}
1245+
1246+
return SDValue();
1247+
}
1248+
12131249
SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12141250
const {
12151251
SelectionDAG &DAG = DCI.DAG;
@@ -1235,6 +1271,8 @@ SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12351271
return performSHLCombine(N, DAG, DCI, Subtarget);
12361272
case ISD::SUB:
12371273
return performSUBCombine(N, DAG, DCI, Subtarget);
1274+
case ISD::SIGN_EXTEND:
1275+
return performSignExtendCombine(N, DAG, DCI, Subtarget);
12381276
}
12391277

12401278
return SDValue();

0 commit comments

Comments
 (0)