Skip to content

Commit 51ff032

Browse files
anbbnaCyanoxygen
authored andcommitted
[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 llvm#99783
1 parent f46eb14 commit 51ff032

File tree

4 files changed

+321
-182
lines changed

4 files changed

+321
-182
lines changed

llvm/lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 28 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,30 @@ 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+
TargetLowering::DAGCombinerInfo &DCI,
1216+
const MipsSubtarget &Subtarget) {
1217+
SDValue N0 = N->getOperand(0);
1218+
EVT VT = N->getValueType(0);
1219+
1220+
// $dst = sign_extend (xor (trunc $src), imm)
1221+
// => $dst = sign_extend (trunc (xor $src, imm))
1222+
if (N0.getOpcode() == ISD::XOR &&
1223+
N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1224+
N0.getOperand(1).getOpcode() == ISD::Constant) {
1225+
SDValue TruncateOperand = N0.getOperand(0).getOperand(0);
1226+
APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
1227+
1228+
SDValue Xor = DAG.getNode(ISD::XOR, SDLoc(N), VT, TruncateOperand,
1229+
DAG.getTargetConstant(Mask, SDLoc(N), VT));
1230+
SDValue Truncate =
1231+
DAG.getNode(ISD::TRUNCATE, SDLoc(N), N0->getValueType(0), Xor);
1232+
return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Truncate);
1233+
}
1234+
1235+
return SDValue();
1236+
}
1237+
12131238
SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12141239
const {
12151240
SelectionDAG &DAG = DCI.DAG;
@@ -1235,6 +1260,8 @@ SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
12351260
return performSHLCombine(N, DAG, DCI, Subtarget);
12361261
case ISD::SUB:
12371262
return performSUBCombine(N, DAG, DCI, Subtarget);
1263+
case ISD::SIGN_EXTEND:
1264+
return performSignExtendCombine(N, DAG, DCI, Subtarget);
12381265
}
12391266

12401267
return SDValue();

0 commit comments

Comments
 (0)