2023-08-02 11:04:54 -05:00
|
|
|
pattern shiftmul_right
|
2019-05-28 08:33:47 -05:00
|
|
|
//
|
|
|
|
// Optimize mul+shift pairs that result from expressions such as foo[s*W+:W]
|
|
|
|
//
|
2019-04-29 06:38:56 -05:00
|
|
|
|
|
|
|
match shift
|
|
|
|
select shift->type.in($shift, $shiftx, $shr)
|
2023-08-02 08:44:41 -05:00
|
|
|
filter !port(shift, \B).empty()
|
2019-04-29 06:38:56 -05:00
|
|
|
endmatch
|
|
|
|
|
2023-08-02 09:10:27 -05:00
|
|
|
// the right shift amount
|
2023-08-02 08:44:41 -05:00
|
|
|
state <SigSpec> shift_amount
|
2023-08-02 09:10:27 -05:00
|
|
|
// log2 scale factor in interpreting of shift_amount
|
|
|
|
// due to zero padding on the shift cell's B port
|
|
|
|
state <int> log2scale
|
2023-08-02 08:44:41 -05:00
|
|
|
|
2023-08-02 09:10:27 -05:00
|
|
|
code shift_amount log2scale
|
2023-08-02 08:44:41 -05:00
|
|
|
shift_amount = port(shift, \B);
|
|
|
|
if (shift->type.in($shr) || !param(shift, \B_SIGNED).as_bool())
|
|
|
|
shift_amount.append(State::S0);
|
|
|
|
|
|
|
|
// at this point shift_amount is signed, make
|
|
|
|
// sure we can never go negative
|
|
|
|
if (shift_amount.bits().back() != State::S0)
|
2019-04-29 06:38:56 -05:00
|
|
|
reject;
|
2023-08-02 08:44:41 -05:00
|
|
|
|
|
|
|
while (shift_amount.bits().back() == State::S0) {
|
|
|
|
shift_amount.remove(GetSize(shift_amount) - 1);
|
|
|
|
if (shift_amount.empty()) reject;
|
2019-04-29 06:38:56 -05:00
|
|
|
}
|
2023-08-02 08:44:41 -05:00
|
|
|
|
2023-08-02 09:10:27 -05:00
|
|
|
log2scale = 0;
|
|
|
|
while (shift_amount[0] == State::S0) {
|
|
|
|
shift_amount.remove(0);
|
|
|
|
if (shift_amount.empty()) reject;
|
|
|
|
log2scale++;
|
|
|
|
}
|
|
|
|
|
2023-08-02 08:44:41 -05:00
|
|
|
if (GetSize(shift_amount) > 20)
|
2019-04-30 00:59:39 -05:00
|
|
|
reject;
|
2019-04-29 06:38:56 -05:00
|
|
|
endcode
|
|
|
|
|
2023-08-02 08:44:41 -05:00
|
|
|
state <SigSpec> mul_din
|
|
|
|
state <Const> mul_const
|
|
|
|
|
2019-04-29 06:38:56 -05:00
|
|
|
match mul
|
|
|
|
select mul->type.in($mul)
|
2023-08-02 08:44:41 -05:00
|
|
|
index <SigSpec> port(mul, \Y) === shift_amount
|
2020-08-05 14:01:20 -05:00
|
|
|
filter !param(mul, \A_SIGNED).as_bool()
|
2023-08-02 08:44:41 -05:00
|
|
|
|
|
|
|
choice <IdString> constport {\A, \B}
|
|
|
|
filter port(mul, constport).is_fully_const()
|
|
|
|
|
|
|
|
define <IdString> varport (constport == \A ? \B : \A)
|
2023-08-02 09:10:27 -05:00
|
|
|
set mul_const SigSpec({port(mul, constport), SigSpec(State::S0, log2scale)}).as_const()
|
2023-08-02 08:46:50 -05:00
|
|
|
// get mul_din unmapped (so no `port()` shorthand)
|
|
|
|
// because we will be using it to set the \A port
|
|
|
|
// on the shift cell, and we want to stay close
|
|
|
|
// to the original design
|
|
|
|
set mul_din mul->getPort(varport)
|
2019-04-29 06:38:56 -05:00
|
|
|
endmatch
|
|
|
|
|
|
|
|
code
|
2019-08-15 11:34:36 -05:00
|
|
|
{
|
2023-08-02 08:44:41 -05:00
|
|
|
if (mul_const.empty() || GetSize(mul_const) > 20)
|
2019-04-30 00:59:39 -05:00
|
|
|
reject;
|
|
|
|
|
2023-08-02 08:44:41 -05:00
|
|
|
// make sure there's no overlap in the signal
|
|
|
|
// selections by the shiftmul pattern
|
|
|
|
if (GetSize(port(shift, \Y)) > mul_const.as_int())
|
2019-04-30 00:59:39 -05:00
|
|
|
reject;
|
|
|
|
|
2023-08-02 08:44:41 -05:00
|
|
|
int factor_bits = ceil_log2(mul_const.as_int());
|
|
|
|
// make sure the multiplication never wraps around
|
2023-08-02 09:10:27 -05:00
|
|
|
if (GetSize(shift_amount) + log2scale < factor_bits + GetSize(mul_din))
|
2019-05-28 08:33:47 -05:00
|
|
|
reject;
|
|
|
|
|
2019-04-30 03:51:51 -05:00
|
|
|
did_something = true;
|
2023-08-02 11:04:54 -05:00
|
|
|
log("right shiftmul pattern in %s: shift=%s, mul=%s\n", log_id(module), log_id(shift), log_id(mul));
|
2019-04-30 00:59:39 -05:00
|
|
|
|
2023-08-02 08:44:41 -05:00
|
|
|
int const_factor = mul_const.as_int();
|
2019-05-28 08:33:47 -05:00
|
|
|
int new_const_factor = 1 << factor_bits;
|
2019-04-30 00:59:39 -05:00
|
|
|
SigSpec padding(State::Sx, new_const_factor-const_factor);
|
|
|
|
SigSpec old_a = port(shift, \A), new_a;
|
|
|
|
|
|
|
|
for (int i = 0; i*const_factor < GetSize(old_a); i++) {
|
2023-12-06 11:22:19 -06:00
|
|
|
if ((i+1)*const_factor < GetSize(old_a)) {
|
|
|
|
SigSpec slice = old_a.extract(i*const_factor, const_factor);
|
|
|
|
new_a.append(slice);
|
|
|
|
new_a.append(padding);
|
|
|
|
} else {
|
|
|
|
new_a.append(old_a.extract_end(i*const_factor));
|
|
|
|
}
|
2019-04-30 00:59:39 -05:00
|
|
|
}
|
|
|
|
|
2019-05-28 08:33:47 -05:00
|
|
|
SigSpec new_b = {mul_din, SigSpec(State::S0, factor_bits)};
|
2019-04-30 00:59:39 -05:00
|
|
|
if (param(shift, \B_SIGNED).as_bool())
|
|
|
|
new_b.append(State::S0);
|
|
|
|
|
|
|
|
shift->setPort(\A, new_a);
|
|
|
|
shift->setParam(\A_WIDTH, GetSize(new_a));
|
|
|
|
shift->setPort(\B, new_b);
|
|
|
|
shift->setParam(\B_WIDTH, GetSize(new_b));
|
|
|
|
|
|
|
|
blacklist(shift);
|
2019-08-15 15:47:59 -05:00
|
|
|
accept;
|
2019-08-15 11:34:36 -05:00
|
|
|
}
|
2019-04-29 06:38:56 -05:00
|
|
|
endcode
|