Skip to content

Commit 34ca0bd

Browse files
committed
Support inst-same-cost attribute
Support inst-same-cost attribute
1 parent 99a9d52 commit 34ca0bd

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

llvm/lib/Target/Mips/Mips.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ def FeatureStrictAlign
212212
: SubtargetFeature<"strict-align", "StrictAlign", "true",
213213
"Disable unaligned load store for r6">;
214214

215+
def FeatureInstSameCost
216+
: SubtargetFeature<"inst-same-cost", "InstSameCost", "true",
217+
"All the instructions have the same cost">;
218+
215219
//===----------------------------------------------------------------------===//
216220
// Register File, Calling Conv, Instruction Descriptions
217221
//===----------------------------------------------------------------------===//

llvm/lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,14 @@ bool MipsTargetLowering::isCheapToSpeculateCtlz(Type *Ty) const {
12481248
return Subtarget.hasMips32();
12491249
}
12501250

1251+
bool MipsTargetLowering::isIntDivCheap(EVT VT, AttributeList Attr) const {
1252+
// When aggressively optimizing for code size, we prefer to use a div
1253+
// instruction, as it is usually smaller than the alternative sequence.
1254+
bool OptSize = Attr.hasFnAttr(Attribute::MinSize);
1255+
bool isInstSameCost = Subtarget.isInstSameCost();
1256+
return OptSize || isInstSameCost;
1257+
}
1258+
12511259
bool MipsTargetLowering::hasBitTest(SDValue X, SDValue Y) const {
12521260
// We can use ANDI+SLTIU as a bit test. Y contains the bit position.
12531261
// For MIPSR2 or later, we may be able to use the `ext` instruction or its'

llvm/lib/Target/Mips/MipsISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ class TargetRegisterClass;
293293
bool shouldFoldConstantShiftPairToMask(const SDNode *N,
294294
CombineLevel Level) const override;
295295

296+
bool isIntDivCheap(EVT VT, AttributeList Attr) const override;
297+
296298
/// Return the register type for a given MVT, ensuring vectors are treated
297299
/// as a series of gpr sized integers.
298300
MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC,

llvm/lib/Target/Mips/MipsSubtarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ MipsSubtarget::MipsSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
8383
AllowMixed16_32(Mixed16_32 || Mips_Os16), Os16(Mips_Os16), HasMSA(false),
8484
UseTCCInDIV(false), HasSym32(false), HasEVA(false), DisableMadd4(false),
8585
HasMT(false), HasCRC(false), HasVirt(false), HasGINV(false),
86-
UseIndirectJumpsHazard(false), StrictAlign(false),
86+
UseIndirectJumpsHazard(false), StrictAlign(false), InstSameCost(false),
8787
StackAlignOverride(StackAlignOverride), TM(TM), TargetTriple(TT),
8888
InstrInfo(
8989
MipsInstrInfo::create(initializeSubtargetDependencies(CPU, FS, TM))),

llvm/lib/Target/Mips/MipsSubtarget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ class MipsSubtarget : public MipsGenSubtargetInfo {
202202
// Disable unaligned load store for r6.
203203
bool StrictAlign;
204204

205+
// All the instructions have the same cost.
206+
bool InstSameCost;
207+
205208
/// The minimum alignment known to hold of the stack frame on
206209
/// entry to the function and which must be maintained by every function.
207210
Align stackAlignment;
@@ -382,6 +385,8 @@ class MipsSubtarget : public MipsGenSubtargetInfo {
382385
return hasMips32r6() && !StrictAlign;
383386
}
384387

388+
bool isInstSameCost() const { return InstSameCost; }
389+
385390
// Set helper classes
386391
void setHelperClassesMips16();
387392
void setHelperClassesMipsSE();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=mips < %s | FileCheck %s -check-prefix=CHECK
3+
; RUN: llc -mtriple=mips --mcpu=zkm < %s | FileCheck %s -check-prefix=CHECK-CPU
4+
; RUN: llc -mtriple=mips --mattr=+inst-same-cost < %s | FileCheck %s -check-prefix=CHECK-CPU
5+
6+
define dso_local noundef range(i32 -5554, 5555) i32 @x(i32 noundef signext %a) local_unnamed_addr #0 {
7+
; CHECK-LABEL: x:
8+
; CHECK: # %bb.0: # %entry
9+
; CHECK-NEXT: lui $1, 12080
10+
; CHECK-NEXT: ori $1, $1, 52665
11+
; CHECK-NEXT: mult $4, $1
12+
; CHECK-NEXT: mfhi $1
13+
; CHECK-NEXT: srl $2, $1, 31
14+
; CHECK-NEXT: sra $1, $1, 10
15+
; CHECK-NEXT: addu $1, $1, $2
16+
; CHECK-NEXT: addiu $2, $zero, 5555
17+
; CHECK-NEXT: mul $1, $1, $2
18+
; CHECK-NEXT: jr $ra
19+
; CHECK-NEXT: subu $2, $4, $1
20+
;
21+
; CHECK-CPU-LABEL: x:
22+
; CHECK-CPU: # %bb.0: # %entry
23+
; CHECK-CPU-NEXT: addiu $1, $zero, 5555
24+
; CHECK-CPU-NEXT: div $zero, $4, $1
25+
; CHECK-CPU-NEXT: teq $1, $zero, 7
26+
; CHECK-CPU-NEXT: jr $ra
27+
; CHECK-CPU-NEXT: mfhi $2
28+
entry:
29+
%rem = srem i32 %a, 5555
30+
ret i32 %rem
31+
}
32+
33+
define dso_local noundef range(i32 -5554, 5555) i32 @y(i32 noundef signext %a) local_unnamed_addr #1 {
34+
; CHECK-LABEL: y:
35+
; CHECK: # %bb.0: # %entry
36+
; CHECK-NEXT: addiu $1, $zero, 5555
37+
; CHECK-NEXT: div $zero, $4, $1
38+
; CHECK-NEXT: teq $1, $zero, 7
39+
; CHECK-NEXT: jr $ra
40+
; CHECK-NEXT: mfhi $2
41+
;
42+
; CHECK-CPU-LABEL: y:
43+
; CHECK-CPU: # %bb.0: # %entry
44+
; CHECK-CPU-NEXT: addiu $1, $zero, 5555
45+
; CHECK-CPU-NEXT: div $zero, $4, $1
46+
; CHECK-CPU-NEXT: teq $1, $zero, 7
47+
; CHECK-CPU-NEXT: jr $ra
48+
; CHECK-CPU-NEXT: mfhi $2
49+
entry:
50+
%rem = srem i32 %a, 5555
51+
ret i32 %rem
52+
}
53+
54+
define dso_local noundef range(i32 -5554, 5555) i32 @z(i32 noundef signext %a) local_unnamed_addr #2 {
55+
; CHECK-LABEL: z:
56+
; CHECK: # %bb.0: # %entry
57+
; CHECK-NEXT: addiu $1, $zero, 5555
58+
; CHECK-NEXT: div $zero, $4, $1
59+
; CHECK-NEXT: teq $1, $zero, 7
60+
; CHECK-NEXT: jr $ra
61+
; CHECK-NEXT: mfhi $2
62+
;
63+
; CHECK-CPU-LABEL: z:
64+
; CHECK-CPU: # %bb.0: # %entry
65+
; CHECK-CPU-NEXT: addiu $1, $zero, 5555
66+
; CHECK-CPU-NEXT: div $zero, $4, $1
67+
; CHECK-CPU-NEXT: teq $1, $zero, 7
68+
; CHECK-CPU-NEXT: jr $ra
69+
; CHECK-CPU-NEXT: mfhi $2
70+
entry:
71+
%rem = srem i32 %a, 5555
72+
ret i32 %rem
73+
}
74+
75+
attributes #0 = { "target-features"="" }
76+
attributes #1 = { "target-features"="+inst-same-cost" }
77+
attributes #2 = { "target-cpu"="zkm" }

0 commit comments

Comments
 (0)