Skip to content

Commit 22bfc58

Browse files
authored
[RISCV] Teach RISCVMakeCompressible handle byte/half load/store for Zcb. (llvm#83375)
For targets with Zcb, this patch makes llvm generate more compress c.lb/lbu/lh/lhu/sb/sh instructions.
1 parent 9c0c98e commit 22bfc58

File tree

2 files changed

+632
-3
lines changed

2 files changed

+632
-3
lines changed

llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ static unsigned log2LdstWidth(unsigned Opcode) {
9999
switch (Opcode) {
100100
default:
101101
llvm_unreachable("Unexpected opcode");
102+
case RISCV::LBU:
103+
case RISCV::SB:
104+
return 0;
105+
case RISCV::LH:
106+
case RISCV::LHU:
107+
case RISCV::SH:
108+
return 1;
102109
case RISCV::LW:
103110
case RISCV::SW:
104111
case RISCV::FLW:
@@ -112,17 +119,47 @@ static unsigned log2LdstWidth(unsigned Opcode) {
112119
}
113120
}
114121

122+
// Return bit field size of immediate operand of Opcode.
123+
static unsigned offsetMask(unsigned Opcode) {
124+
switch (Opcode) {
125+
default:
126+
llvm_unreachable("Unexpected opcode");
127+
case RISCV::LBU:
128+
case RISCV::SB:
129+
return maskTrailingOnes<unsigned>(2U);
130+
case RISCV::LH:
131+
case RISCV::LHU:
132+
case RISCV::SH:
133+
return maskTrailingOnes<unsigned>(1U);
134+
case RISCV::LW:
135+
case RISCV::SW:
136+
case RISCV::FLW:
137+
case RISCV::FSW:
138+
case RISCV::LD:
139+
case RISCV::SD:
140+
case RISCV::FLD:
141+
case RISCV::FSD:
142+
return maskTrailingOnes<unsigned>(5U);
143+
}
144+
}
145+
115146
// Return a mask for the offset bits of a non-stack-pointer based compressed
116147
// load/store.
117148
static uint8_t compressedLDSTOffsetMask(unsigned Opcode) {
118-
return 0x1f << log2LdstWidth(Opcode);
149+
return offsetMask(Opcode) << log2LdstWidth(Opcode);
119150
}
120151

121152
// Return true if Offset fits within a compressed stack-pointer based
122153
// load/store.
123154
static bool compressibleSPOffset(int64_t Offset, unsigned Opcode) {
124-
return log2LdstWidth(Opcode) == 2 ? isShiftedUInt<6, 2>(Offset)
125-
: isShiftedUInt<6, 3>(Offset);
155+
// Compressed sp-based loads and stores only work for 32/64 bits.
156+
switch (log2LdstWidth(Opcode)) {
157+
case 2:
158+
return isShiftedUInt<6, 2>(Offset);
159+
case 3:
160+
return isShiftedUInt<6, 3>(Offset);
161+
}
162+
return false;
126163
}
127164

128165
// Given an offset for a load/store, return the adjustment required to the base
@@ -147,6 +184,10 @@ static bool isCompressibleLoad(const MachineInstr &MI) {
147184
switch (MI.getOpcode()) {
148185
default:
149186
return false;
187+
case RISCV::LBU:
188+
case RISCV::LH:
189+
case RISCV::LHU:
190+
return STI.hasStdExtZcb();
150191
case RISCV::LW:
151192
case RISCV::LD:
152193
return STI.hasStdExtCOrZca();
@@ -164,6 +205,9 @@ static bool isCompressibleStore(const MachineInstr &MI) {
164205
switch (MI.getOpcode()) {
165206
default:
166207
return false;
208+
case RISCV::SB:
209+
case RISCV::SH:
210+
return STI.hasStdExtZcb();
167211
case RISCV::SW:
168212
case RISCV::SD:
169213
return STI.hasStdExtCOrZca();

0 commit comments

Comments
 (0)