Skip to content

Commit

Permalink
[CSKY] Optimize subtraction with SUBI32/SUBI16
Browse files Browse the repository at this point in the history
Reviewed By: zixuan-wu

Differential Revision: https://reviews.llvm.org/D153326
  • Loading branch information
benshi001 committed Jun 30, 2023
1 parent 742939f commit f40682a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 52 deletions.
28 changes: 28 additions & 0 deletions llvm/lib/Target/CSKY/CSKYInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class oimm<int num> : Operand<i32>,
let DecoderMethod = "decodeOImmOperand<"#num#">";
}

def imm_neg_XFORM : SDNodeXForm<imm, [{
return CurDAG->getTargetConstant(-N->getSExtValue(), SDLoc(N), MVT::i32);
}]>;

class oimm_neg<int num> : Operand<i32>,
ImmLeaf<i32, "return isUInt<"#num#">(-Imm - 1);"> {
}

class uimm<int num, int shift = 0> : Operand<i32>,
ImmLeaf<i32, "return isShiftedUInt<"#num#", "#shift#">(Imm);"> {
let EncoderMethod = "getImmOpValue<"#shift#">";
Expand Down Expand Up @@ -259,6 +267,23 @@ def oimm16 : oimm<16> {
}];
}

def oimm8_neg : oimm_neg<8> {
let MCOperandPredicate = [{
int64_t Imm;
if (MCOp.evaluateAsConstantImm(Imm))
return isUInt<8>(-Imm - 1);
return MCOp.isBareSymbolRef();
}];
}
def oimm12_neg : oimm_neg<12> {
let MCOperandPredicate = [{
int64_t Imm;
if (MCOp.evaluateAsConstantImm(Imm))
return isUInt<12>(-Imm - 1);
return MCOp.isBareSymbolRef();
}];
}

def nimm12 : nimm<12>;

def uimm1 : uimm<1>;
Expand Down Expand Up @@ -518,6 +543,9 @@ let Predicates = [iHasE2] in {
let Size = 8 in
def RSUBI32 : CSKYPseudo<(outs GPR:$rd), (ins GPR:$rx, uimm12:$imm12), "rsubi32 $rd, $rx, $imm12", []>;

def : Pat<(add GPR:$rs1, (oimm12_neg:$im)),
(SUBI32 GPR:$rs1, (imm_neg_XFORM oimm12_neg:$im))>;

def LSL32 : R_YXZ_SP_F1<0x10, 0x1,
BinOpFrag<(shl node:$LHS, node:$RHS)>, "lsl32">;
def LSR32 : R_YXZ_SP_F1<0x10, 0x2,
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/CSKY/CSKYInstrInfo16Instr.td
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ let Constraints = "$rZ = $rz", isReMaterializable = 1, isAsCheapAsAMove = 1 in {
def SUBI16 : I16_Z_8<0b101, (ins mGPR:$rZ, oimm8:$imm8), "subi16\t$rz, $imm8">;
}

def : Pat<(add GPR:$rs1, (oimm8_neg:$im)),
(SUBI16 GPR:$rs1, (imm_neg_XFORM oimm8_neg:$im))>;

let isAdd = 1 in
def ADDI16ZSP : I16_Z_8<0b011, (ins GPRSP:$sp, uimm8_2:$imm8),
"addi16\t$rz, $sp, $imm8">;
Expand Down

0 comments on commit f40682a

Please sign in to comment.