Skip to content

Commit 2288ba2

Browse files
committed
[M68k] Fix CMP pattern matching and emission logic
CMP nodes with an immediate operand always have the immediate on the right-hand side. But the pattern defined in TableGen had the immediate on the left-hand side, causing the match to fail, so this has been corrected. Also, `emitCmp()` contained logic ported from X86 that is not relevant to M68k, such as imposing restrictions on which ValueTypes can be compared, so that logic has been removed. It would also replace CMP with SUB to enable a specific optimization, but (as of now) on M68k it usually results in a register copy before the SUB, which loses efficiency. With these changes, CMP instructions more reliably take advantage of their addressing modes, and redundancy is significantly reduced.
1 parent 35feb9b commit 2288ba2

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

llvm/lib/Target/M68k/M68kISelLowering.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,8 +1910,8 @@ SDValue M68kTargetLowering::EmitTest(SDValue Op, unsigned M68kCC,
19101910
// we prove that the arithmetic won't overflow, we can't use OF or CF.
19111911
if (Op.getResNo() != 0 || NeedOF || NeedCF) {
19121912
// Emit a CMP with 0, which is the TEST pattern.
1913-
return DAG.getNode(M68kISD::CMP, DL, MVT::i8,
1914-
DAG.getConstant(0, DL, Op.getValueType()), Op);
1913+
return DAG.getNode(M68kISD::CMP, DL, MVT::i8, Op,
1914+
DAG.getConstant(0, DL, Op.getValueType()));
19151915
}
19161916
unsigned Opcode = 0;
19171917
unsigned NumOperands = 0;
@@ -2068,8 +2068,8 @@ SDValue M68kTargetLowering::EmitTest(SDValue Op, unsigned M68kCC,
20682068

20692069
if (Opcode == 0) {
20702070
// Emit a CMP with 0, which is the TEST pattern.
2071-
return DAG.getNode(M68kISD::CMP, DL, MVT::i8,
2072-
DAG.getConstant(0, DL, Op.getValueType()), Op);
2071+
return DAG.getNode(M68kISD::CMP, DL, MVT::i8, Op,
2072+
DAG.getConstant(0, DL, Op.getValueType()));
20732073
}
20742074
SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i8);
20752075
SmallVector<SDValue, 4> Ops(Op->op_begin(), Op->op_begin() + NumOperands);
@@ -2107,24 +2107,7 @@ SDValue M68kTargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned M68kCC,
21072107
assert(!(isa<ConstantSDNode>(Op1) && Op0.getValueType() == MVT::i1) &&
21082108
"Unexpected comparison operation for MVT::i1 operands");
21092109

2110-
if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
2111-
Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
2112-
// Only promote the compare up to I32 if it is a 16 bit operation
2113-
// with an immediate. 16 bit immediates are to be avoided.
2114-
if ((Op0.getValueType() == MVT::i16 &&
2115-
(isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1))) &&
2116-
!DAG.getMachineFunction().getFunction().hasMinSize()) {
2117-
unsigned ExtendOp =
2118-
isM68kCCUnsigned(M68kCC) ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND;
2119-
Op0 = DAG.getNode(ExtendOp, DL, MVT::i32, Op0);
2120-
Op1 = DAG.getNode(ExtendOp, DL, MVT::i32, Op1);
2121-
}
2122-
// Use SUB instead of CMP to enable CSE between SUB and CMP.
2123-
SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i8);
2124-
SDValue Sub = DAG.getNode(M68kISD::SUB, DL, VTs, Op0, Op1);
2125-
return SDValue(Sub.getNode(), 1);
2126-
}
2127-
return DAG.getNode(M68kISD::CMP, DL, MVT::i8, Op0, Op1);
2110+
return DAG.getNode(M68kISD::CMP, DL, Op0.getValueType(), Op0, Op1);
21282111
}
21292112

21302113
/// Result of 'and' or 'trunc to i1' is compared against zero.
@@ -2305,7 +2288,7 @@ SDValue M68kTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
23052288
}
23062289

23072290
Cmp = DAG.getNode(M68kISD::CMP, DL, MVT::i8,
2308-
DAG.getConstant(1, DL, CmpOp0.getValueType()), CmpOp0);
2291+
CmpOp0, DAG.getConstant(1, DL, CmpOp0.getValueType()));
23092292

23102293
SDValue Res = // Res = 0 or -1.
23112294
DAG.getNode(M68kISD::SETCC_CARRY, DL, Op.getValueType(),

llvm/lib/Target/M68k/M68kInstrArithmetic.td

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ defm XOR : MxBiArOp_DF_EAd<"eor", MxXor, 0xB, 0xA>;
394394
// CMP
395395
//===----------------------------------------------------------------------===//
396396

397-
let Defs = [CCR] in {
397+
let isCompare = 1, Defs = [CCR] in {
398398
class MxCmp_RR<MxType LHS_TYPE, MxType RHS_TYPE = LHS_TYPE>
399399
: MxInst<(outs), (ins LHS_TYPE.ROp:$lhs, RHS_TYPE.ROp:$rhs),
400400
"cmp."#RHS_TYPE.Prefix#"\t$lhs, $rhs",
@@ -414,7 +414,7 @@ class MxCmp_RR<MxType LHS_TYPE, MxType RHS_TYPE = LHS_TYPE>
414414
class MxCmp_RI<MxType TYPE>
415415
: MxInst<(outs), (ins TYPE.IOp:$imm, TYPE.ROp:$reg),
416416
"cmpi."#TYPE.Prefix#"\t$imm, $reg",
417-
[(set CCR, (MxCmp TYPE.IPat:$imm, TYPE.VT:$reg))]> {
417+
[(set CCR, (MxCmp TYPE.VT:$reg, TYPE.IPat:$imm))]> {
418418
let Inst = (ascend
419419
(descend 0b00001100,
420420
!cast<MxEncSize>("MxEncSize"#TYPE.Size).Value,
@@ -433,7 +433,7 @@ class MxCmp_MI<MxType TYPE, MxOperand MEMOpd, ComplexPattern MEMPat,
433433
MxEncMemOp MEM_ENC>
434434
: MxInst<(outs), (ins TYPE.IOp:$imm, MEMOpd:$mem),
435435
"cmpi."#TYPE.Prefix#"\t$imm, $mem",
436-
[(set CCR, (MxCmp TYPE.IPat:$imm, (load MEMPat:$mem)))]> {
436+
[(set CCR, (MxCmp (load MEMPat:$mem), TYPE.IPat:$imm))]> {
437437
let Inst = (ascend
438438
(descend 0b00001100,
439439
!cast<MxEncSize>("MxEncSize"#TYPE.Size).Value,
@@ -449,8 +449,8 @@ class MxCmp_MI<MxType TYPE, MxOperand MEMOpd, ComplexPattern MEMPat,
449449
class MxCmp_BI<MxType TYPE>
450450
: MxInst<(outs), (ins TYPE.IOp:$imm, MxAL32:$abs),
451451
"cmpi."#TYPE.Prefix#"\t$imm, $abs",
452-
[(set CCR, (MxCmp TYPE.IPat:$imm,
453-
(load (i32 (MxWrapper tglobaladdr:$abs)))))]> {
452+
[(set CCR, (MxCmp (load (i32 (MxWrapper tglobaladdr:$abs))),
453+
TYPE.IPat:$imm))]> {
454454
defvar AbsEncoding = MxEncAddrMode_abs<"abs", true>;
455455
let Inst = (ascend
456456
(descend 0b00001100,

0 commit comments

Comments
 (0)