Skip to content

Commit b2f9cfc

Browse files
authored
Add feature flag for the removal of neg and the new semantics of sub (#76)
1 parent 4ca616d commit b2f9cfc

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

llvm/lib/Target/SBF/SBF.td

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def FeatureRelocAbs64 : SubtargetFeature<"reloc-abs64", "UseRelocAbs64", "true",
3838
def FeatureStaticSyscalls : SubtargetFeature<"static-syscalls", "HasStaticSyscalls", "true",
3939
"Marker feature used for conditional compilation">;
4040

41+
def FeatureDisableNeg : SubtargetFeature<"no-neg", "DisableNeg", "true",
42+
"Disable the neg instruction">;
43+
44+
def FeatureReverseSubImm : SubtargetFeature<"reverse-sub", "ReverseSubImm", "true",
45+
"Reverse the operands in the 'sub reg, imm' instruction">;
46+
4147
class Proc<string Name, list<SubtargetFeature> Features>
4248
: Processor<Name, NoItineraries, Features>;
4349

@@ -46,7 +52,8 @@ def : Proc<"v1", []>;
4652
def : Proc<"v2", []>;
4753
def : Proc<"v3", []>;
4854
def : Proc<"probe", []>;
49-
def : Proc<"sbfv2", [FeatureSolana, FeatureDynamicFrames, FeatureSdiv, FeatureRelocAbs64, FeatureStaticSyscalls]>;
55+
def : Proc<"sbfv2", [FeatureSolana, FeatureDynamicFrames, FeatureSdiv, FeatureRelocAbs64, FeatureStaticSyscalls,
56+
FeatureDisableNeg, FeatureReverseSubImm]>;
5057

5158
//===----------------------------------------------------------------------===//
5259
// Assembly printer

llvm/lib/Target/SBF/SBFInstrInfo.td

+12-8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def SBFNoALU32 : Predicate<"!Subtarget->getHasAlu32()">;
5656
def SBFSubtargetSolana : Predicate<"Subtarget->isSolana()">;
5757
def SBFv2 : Predicate<"Subtarget->isSBFv2()">;
5858
def NoSBFv2 : Predicate<"!Subtarget->isSBFv2()">;
59+
def SBFHasNeg : Predicate<"!Subtarget->getDisableNeg()">;
60+
def SBFNoNeg: Predicate<"Subtarget->getDisableNeg()">;
61+
def SBFRevSub : Predicate<"Subtarget->getReverseSubImm()">;
62+
def SBFNoRevSub : Predicate<"!Subtarget->getReverseSubImm()">;
5963

6064
def brtarget : Operand<OtherVT> {
6165
let PrintMethod = "printBrTargetOperand";
@@ -347,14 +351,14 @@ let Constraints = "$dst = $src2" in {
347351
// In SBFv1, `sub reg, imm` is interpreted as reg = reg - imm,
348352
// but in SBFv2 it means reg = imm - reg
349353
def : Pat<(sub GPR:$src, i64immSExt32:$imm),
350-
(SUB_ri GPR:$src, i64immSExt32:$imm)>, Requires<[NoSBFv2]>;
354+
(SUB_ri GPR:$src, i64immSExt32:$imm)>, Requires<[SBFNoRevSub]>;
351355
def : Pat<(sub GPR32:$src, i32immSExt32:$imm),
352-
(SUB_ri_32 GPR32:$src, i32immSExt32:$imm)>, Requires<[NoSBFv2]>;
356+
(SUB_ri_32 GPR32:$src, i32immSExt32:$imm)>, Requires<[SBFNoRevSub]>;
353357

354358
def : Pat<(sub i64immSExt32:$imm, GPR:$src),
355-
(SUB_ri GPR:$src, i64immSExt32:$imm)>, Requires<[SBFv2]>;
359+
(SUB_ri GPR:$src, i64immSExt32:$imm)>, Requires<[SBFRevSub]>;
356360
def : Pat<(sub i32immSExt32:$imm, GPR32:$src),
357-
(SUB_ri_32 GPR32:$src, i32immSExt32:$imm)>, Requires<[SBFv2]>;
361+
(SUB_ri_32 GPR32:$src, i32immSExt32:$imm)>, Requires<[SBFRevSub]>;
358362

359363
class NEG_RR<SBFOpClass Class, SBFArithOp Opc,
360364
dag outs, dag ins, string asmstr, list<dag> pattern>
@@ -376,11 +380,11 @@ let Constraints = "$dst = $src", isAsCheapAsAMove = 1 in {
376380

377381
// Instruction `neg` exists on SBFv1, but not on SBFv2
378382
// In SBFv2, the negate operation is done with a subtraction
379-
def : Pat<(ineg i64:$src), (NEG_64 GPR:$src)>, Requires<[NoSBFv2]>;
380-
def : Pat<(ineg i32:$src), (NEG_32 GPR32:$src)>, Requires<[NoSBFv2]>;
383+
def : Pat<(ineg i64:$src), (NEG_64 GPR:$src)>, Requires<[SBFHasNeg]>;
384+
def : Pat<(ineg i32:$src), (NEG_32 GPR32:$src)>, Requires<[SBFHasNeg]>;
381385

382-
def : Pat<(ineg i64:$src), (SUB_ri GPR:$src, 0)>, Requires<[SBFv2]>;
383-
def : Pat<(ineg i32:$src), (SUB_ri_32 GPR32:$src, 0)>, Requires<[SBFv2]>;
386+
def : Pat<(ineg i64:$src), (SUB_ri GPR:$src, 0)>, Requires<[SBFNoNeg]>;
387+
def : Pat<(ineg i32:$src), (SUB_ri_32 GPR32:$src, 0)>, Requires<[SBFNoNeg]>;
384388

385389

386390
class LD_IMM64<bits<4> Pseudo, string Mnemonic>

llvm/lib/Target/SBF/SBFSubtarget.h

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class SBFSubtarget : public SBFGenSubtargetInfo {
7575
// whether we are targeting SBFv2
7676
bool IsSBFv2;
7777

78+
// Whether to disable the negate (neg) instruction
79+
bool DisableNeg;
80+
81+
// Whether to consider 'sub reg, imm' as 'reg = imm - reg', instead of 'reg =
82+
// reg - imm'.
83+
bool ReverseSubImm;
84+
7885
public:
7986
// This constructor initializes the data members to match that
8087
// of the specified triple.
@@ -95,6 +102,8 @@ class SBFSubtarget : public SBFGenSubtargetInfo {
95102
bool getHasSdiv() const { return HasSdiv; }
96103
bool getUseDwarfRIS() const { return UseDwarfRIS; }
97104
bool isSBFv2() const { return IsSBFv2; }
105+
bool getDisableNeg() const { return DisableNeg; }
106+
bool getReverseSubImm() const { return ReverseSubImm; }
98107

99108
const SBFInstrInfo *getInstrInfo() const override { return &InstrInfo; }
100109
const SBFFrameLowering *getFrameLowering() const override {

0 commit comments

Comments
 (0)