Skip to content

Commit 591d423

Browse files
committed
[MIPS]Optimize (sign_extend (xor (trunc X), imm)) to xor
Optimize '$dst = sign_extend (xor (trunc $src), imm)' to '$dst = sign_extend (trunc (xor $src, imm))'. Fix #99783
1 parent 79382eb commit 591d423

File tree

4 files changed

+307
-169
lines changed

4 files changed

+307
-169
lines changed

llvm/lib/Target/Mips/MipsISelLowering.cpp

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

521521
setTargetDAGCombine({ISD::SDIVREM, ISD::UDIVREM, ISD::SELECT, ISD::AND,
522-
ISD::OR, ISD::ADD, ISD::SUB, ISD::AssertZext, ISD::SHL});
522+
ISD::OR, ISD::ADD, ISD::SUB, ISD::AssertZext, ISD::SHL,
523+
ISD::SIGN_EXTEND});
523524

524525
if (Subtarget.isGP64bit())
525526
setMaxAtomicSizeInBitsSupported(64);
@@ -1213,6 +1214,29 @@ static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
12131214
DAG.getConstant(SMSize, DL, MVT::i32));
12141215
}
12151216

1217+
static SDValue performSignExtendCombine(SDNode *N, SelectionDAG &DAG,
1218+
TargetLowering::DAGCombinerInfo &DCI,
1219+
const MipsSubtarget &Subtarget) {
1220+
SDValue N0 = N->getOperand(0);
1221+
EVT VT = N->getValueType(0);
1222+
1223+
// $dst = sign_extend (xor (trunc $src), imm)
1224+
// => $dst = sign_extend (trunc (xor $src, imm))
1225+
if (N0.getOpcode() == ISD::XOR &&
1226+
N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1227+
N0.getOperand(1).getOpcode() == ISD::Constant) {
1228+
SDValue TruncateOperand = N0.getOperand(0).getOperand(0);
1229+
APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
1230+
1231+
SDValue Xor = DAG.getNode(ISD::XOR, SDLoc(N), VT, TruncateOperand,
1232+
DAG.getTargetConstant(Mask, SDLoc(N), VT));
1233+
SDValue Truncate = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N0->getValueType(0), Xor);
1234+
return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Truncate);
1235+
}
1236+
1237+
return SDValue();
1238+
}
1239+
12161240
SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12171241
const {
12181242
SelectionDAG &DAG = DCI.DAG;
@@ -1238,6 +1262,8 @@ SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12381262
return performSHLCombine(N, DAG, DCI, Subtarget);
12391263
case ISD::SUB:
12401264
return performSUBCombine(N, DAG, DCI, Subtarget);
1265+
case ISD::SIGN_EXTEND:
1266+
return performSignExtendCombine(N, DAG, DCI, Subtarget);
12411267
}
12421268

12431269
return SDValue();

0 commit comments

Comments
 (0)