2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
SigMap assign_map, dff_init_map;
|
|
|
|
SigSet<RTLIL::Cell*> mux_drivers;
|
2015-08-18 06:50:15 -05:00
|
|
|
dict<SigBit, pool<SigBit>> init_attributes;
|
|
|
|
|
|
|
|
void remove_init_attr(SigSpec sig)
|
|
|
|
{
|
|
|
|
for (auto bit : assign_map(sig))
|
|
|
|
if (init_attributes.count(bit))
|
|
|
|
for (auto wbit : init_attributes.at(bit))
|
|
|
|
wbit.wire->attributes.at("\\init")[wbit.offset] = State::Sx;
|
|
|
|
}
|
2014-09-27 09:17:53 -05:00
|
|
|
|
2015-05-23 02:45:48 -05:00
|
|
|
bool handle_dlatch(RTLIL::Module *mod, RTLIL::Cell *dlatch)
|
|
|
|
{
|
|
|
|
SigSpec sig_e = dlatch->getPort("\\EN");
|
|
|
|
|
|
|
|
if (sig_e == State::S0)
|
|
|
|
{
|
|
|
|
RTLIL::Const val_init;
|
|
|
|
for (auto bit : dff_init_map(dlatch->getPort("\\Q")))
|
|
|
|
val_init.bits.push_back(bit.wire == NULL ? bit.data : State::Sx);
|
|
|
|
mod->connect(dlatch->getPort("\\Q"), val_init);
|
|
|
|
goto delete_dlatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig_e == State::S1)
|
|
|
|
{
|
|
|
|
mod->connect(dlatch->getPort("\\Q"), dlatch->getPort("\\D"));
|
|
|
|
goto delete_dlatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
delete_dlatch:
|
2016-08-29 18:34:04 -05:00
|
|
|
log("Removing %s (%s) from module %s.\n", log_id(dlatch), log_id(dlatch->type), log_id(mod));
|
2015-08-18 06:50:15 -05:00
|
|
|
remove_init_attr(dlatch->getPort("\\Q"));
|
2015-05-23 02:45:48 -05:00
|
|
|
mod->remove(dlatch);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
bool handle_dff(RTLIL::Module *mod, RTLIL::Cell *dff)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
RTLIL::SigSpec sig_d, sig_q, sig_c, sig_r;
|
|
|
|
RTLIL::Const val_cp, val_rp, val_rv;
|
|
|
|
|
|
|
|
if (dff->type == "$_DFF_N_" || dff->type == "$_DFF_P_") {
|
2014-07-31 09:38:54 -05:00
|
|
|
sig_d = dff->getPort("\\D");
|
|
|
|
sig_q = dff->getPort("\\Q");
|
|
|
|
sig_c = dff->getPort("\\C");
|
2013-01-05 04:13:26 -06:00
|
|
|
val_cp = RTLIL::Const(dff->type == "$_DFF_P_", 1);
|
|
|
|
}
|
|
|
|
else if (dff->type.substr(0,6) == "$_DFF_" && dff->type.substr(9) == "_" &&
|
|
|
|
(dff->type[6] == 'N' || dff->type[6] == 'P') &&
|
|
|
|
(dff->type[7] == 'N' || dff->type[7] == 'P') &&
|
|
|
|
(dff->type[8] == '0' || dff->type[8] == '1')) {
|
2014-07-31 09:38:54 -05:00
|
|
|
sig_d = dff->getPort("\\D");
|
|
|
|
sig_q = dff->getPort("\\Q");
|
|
|
|
sig_c = dff->getPort("\\C");
|
|
|
|
sig_r = dff->getPort("\\R");
|
2013-01-05 04:13:26 -06:00
|
|
|
val_cp = RTLIL::Const(dff->type[6] == 'P', 1);
|
|
|
|
val_rp = RTLIL::Const(dff->type[7] == 'P', 1);
|
|
|
|
val_rv = RTLIL::Const(dff->type[8] == '1', 1);
|
|
|
|
}
|
|
|
|
else if (dff->type == "$dff") {
|
2014-07-31 09:38:54 -05:00
|
|
|
sig_d = dff->getPort("\\D");
|
|
|
|
sig_q = dff->getPort("\\Q");
|
|
|
|
sig_c = dff->getPort("\\CLK");
|
2013-01-05 04:13:26 -06:00
|
|
|
val_cp = RTLIL::Const(dff->parameters["\\CLK_POLARITY"].as_bool(), 1);
|
|
|
|
}
|
|
|
|
else if (dff->type == "$adff") {
|
2014-07-31 09:38:54 -05:00
|
|
|
sig_d = dff->getPort("\\D");
|
|
|
|
sig_q = dff->getPort("\\Q");
|
|
|
|
sig_c = dff->getPort("\\CLK");
|
|
|
|
sig_r = dff->getPort("\\ARST");
|
2013-01-05 04:13:26 -06:00
|
|
|
val_cp = RTLIL::Const(dff->parameters["\\CLK_POLARITY"].as_bool(), 1);
|
|
|
|
val_rp = RTLIL::Const(dff->parameters["\\ARST_POLARITY"].as_bool(), 1);
|
|
|
|
val_rv = dff->parameters["\\ARST_VALUE"];
|
|
|
|
}
|
|
|
|
else
|
2013-05-24 05:32:06 -05:00
|
|
|
log_abort();
|
2013-05-23 00:48:18 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
assign_map.apply(sig_d);
|
|
|
|
assign_map.apply(sig_q);
|
|
|
|
assign_map.apply(sig_c);
|
|
|
|
assign_map.apply(sig_r);
|
|
|
|
|
2014-02-04 16:00:32 -06:00
|
|
|
bool has_init = false;
|
2014-02-04 05:02:47 -06:00
|
|
|
RTLIL::Const val_init;
|
|
|
|
for (auto bit : dff_init_map(sig_q).to_sigbit_vector()) {
|
|
|
|
if (bit.wire == NULL)
|
|
|
|
has_init = true;
|
|
|
|
val_init.bits.push_back(bit.wire == NULL ? bit.data : RTLIL::State::Sx);
|
|
|
|
}
|
|
|
|
|
2015-04-18 01:04:31 -05:00
|
|
|
if (dff->type == "$dff" && mux_drivers.has(sig_d)) {
|
2013-05-23 00:48:18 -05:00
|
|
|
std::set<RTLIL::Cell*> muxes;
|
|
|
|
mux_drivers.find(sig_d, muxes);
|
|
|
|
for (auto mux : muxes) {
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_a = assign_map(mux->getPort("\\A"));
|
|
|
|
RTLIL::SigSpec sig_b = assign_map(mux->getPort("\\B"));
|
2015-04-18 01:04:31 -05:00
|
|
|
if (sig_a == sig_q && sig_b.is_fully_const() && (!has_init || val_init == sig_b.as_const())) {
|
|
|
|
mod->connect(sig_q, sig_b);
|
2013-05-23 00:48:18 -05:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
2015-04-18 01:04:31 -05:00
|
|
|
if (sig_b == sig_q && sig_a.is_fully_const() && (!has_init || val_init == sig_a.as_const())) {
|
|
|
|
mod->connect(sig_q, sig_a);
|
2013-05-23 00:48:18 -05:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 01:04:31 -05:00
|
|
|
if (sig_c.is_fully_const() && (!sig_r.size() || !has_init || val_init == val_rv)) {
|
2014-02-02 14:09:08 -06:00
|
|
|
if (val_rv.bits.size() == 0)
|
2014-02-04 05:02:47 -06:00
|
|
|
val_rv = val_init;
|
2015-07-25 05:01:25 -05:00
|
|
|
mod->connect(sig_q, val_rv);
|
2014-02-02 14:09:08 -06:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
|
|
|
|
2015-04-18 01:04:31 -05:00
|
|
|
if (sig_d.is_fully_undef() && sig_r.size() && (!has_init || val_init == val_rv)) {
|
2015-07-25 05:01:25 -05:00
|
|
|
mod->connect(sig_q, val_rv);
|
2014-01-17 09:42:40 -06:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (sig_d.is_fully_undef() && !sig_r.size() && has_init) {
|
2015-07-25 05:01:25 -05:00
|
|
|
mod->connect(sig_q, val_init);
|
2014-02-04 05:02:47 -06:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
|
|
|
|
2015-07-24 07:12:50 -05:00
|
|
|
if (sig_d.is_fully_const() && (!sig_r.size() || val_rv == sig_d.as_const()) && (!has_init || val_init == sig_d.as_const())) {
|
2015-07-25 05:01:25 -05:00
|
|
|
mod->connect(sig_q, sig_d);
|
2013-01-05 04:13:26 -06:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
|
|
|
|
2015-04-18 01:04:31 -05:00
|
|
|
if (sig_d == sig_q && (!sig_r.size() || !has_init || val_init == val_rv)) {
|
2015-07-25 05:01:25 -05:00
|
|
|
if (sig_r.size())
|
|
|
|
mod->connect(sig_q, val_rv);
|
|
|
|
if (has_init)
|
|
|
|
mod->connect(sig_q, val_init);
|
2013-01-05 04:13:26 -06:00
|
|
|
goto delete_dff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
delete_dff:
|
2016-08-29 18:34:04 -05:00
|
|
|
log("Removing %s (%s) from module %s.\n", log_id(dff), log_id(dff->type), log_id(mod));
|
2015-08-18 06:50:15 -05:00
|
|
|
remove_init_attr(dff->getPort("\\Q"));
|
2014-07-25 08:05:18 -05:00
|
|
|
mod->remove(dff);
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OptRmdffPass : public Pass {
|
2013-03-01 01:58:55 -06:00
|
|
|
OptRmdffPass() : Pass("opt_rmdff", "remove DFFs with constant inputs") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" opt_rmdff [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass identifies flip-flops with constant inputs and replaces them with\n");
|
|
|
|
log("a constant driver.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
2016-08-29 18:34:04 -05:00
|
|
|
int total_count = 0, total_initdrv = 0;
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing OPT_RMDFF pass (remove dff with constant values).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
extra_args(args, 1, design);
|
|
|
|
|
2016-08-29 18:34:04 -05:00
|
|
|
for (auto module : design->selected_modules())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2016-08-29 18:34:04 -05:00
|
|
|
pool<SigBit> driven_bits;
|
|
|
|
dict<SigBit, State> init_bits;
|
|
|
|
|
|
|
|
assign_map.set(module);
|
|
|
|
dff_init_map.set(module);
|
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
|
|
|
if (wire->attributes.count("\\init") != 0) {
|
|
|
|
Const initval = wire->attributes.at("\\init");
|
|
|
|
dff_init_map.add(wire, initval);
|
|
|
|
for (int i = 0; i < GetSize(wire); i++) {
|
|
|
|
SigBit wire_bit(wire, i), mapped_bit = assign_map(wire_bit);
|
|
|
|
if (mapped_bit.wire) {
|
2015-08-18 06:50:15 -05:00
|
|
|
init_attributes[mapped_bit].insert(wire_bit);
|
2016-08-29 18:34:04 -05:00
|
|
|
if (i < GetSize(initval))
|
|
|
|
init_bits[mapped_bit] = initval[i];
|
|
|
|
}
|
2015-08-18 06:50:15 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-29 18:34:04 -05:00
|
|
|
|
|
|
|
if (wire->port_input) {
|
|
|
|
for (auto bit : assign_map(wire))
|
|
|
|
driven_bits.insert(bit);
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 00:48:18 -05:00
|
|
|
mux_drivers.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-08-02 06:11:01 -05:00
|
|
|
std::vector<RTLIL::IdString> dff_list;
|
2015-05-23 02:45:48 -05:00
|
|
|
std::vector<RTLIL::IdString> dlatch_list;
|
2016-08-29 18:34:04 -05:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
{
|
|
|
|
for (auto &conn : cell->connections())
|
|
|
|
if (cell->output(conn.first) || !cell->known())
|
|
|
|
for (auto bit : assign_map(conn.second))
|
|
|
|
driven_bits.insert(bit);
|
|
|
|
|
|
|
|
if (cell->type == "$mux" || cell->type == "$pmux") {
|
|
|
|
if (cell->getPort("\\A").size() == cell->getPort("\\B").size())
|
|
|
|
mux_drivers.insert(assign_map(cell->getPort("\\Y")), cell);
|
2013-05-23 00:48:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-29 18:34:04 -05:00
|
|
|
|
|
|
|
if (!design->selected(module, cell))
|
2013-03-01 01:58:55 -06:00
|
|
|
continue;
|
2016-08-29 18:34:04 -05:00
|
|
|
|
|
|
|
if (cell->type.in("$_DFF_N_", "$_DFF_P_",
|
|
|
|
"$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
|
|
|
|
"$_DFF_PN0_", "$_DFF_PN1_", "$_DFF_PP0_", "$_DFF_PP1_",
|
|
|
|
"$dff", "$adff"))
|
|
|
|
dff_list.push_back(cell->name);
|
|
|
|
|
|
|
|
if (cell->type == "$dlatch")
|
|
|
|
dlatch_list.push_back(cell->name);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &id : dff_list) {
|
2016-08-29 18:34:04 -05:00
|
|
|
if (module->cell(id) != nullptr &&
|
|
|
|
handle_dff(module, module->cells_[id]))
|
2013-01-05 04:13:26 -06:00
|
|
|
total_count++;
|
|
|
|
}
|
2015-05-23 02:45:48 -05:00
|
|
|
|
|
|
|
for (auto &id : dlatch_list) {
|
2016-08-29 18:34:04 -05:00
|
|
|
if (module->cell(id) != nullptr &&
|
|
|
|
handle_dlatch(module, module->cells_[id]))
|
2015-05-23 02:45:48 -05:00
|
|
|
total_count++;
|
|
|
|
}
|
2016-08-29 18:34:04 -05:00
|
|
|
|
|
|
|
SigSpec const_init_sigs;
|
|
|
|
|
|
|
|
for (auto bit : init_bits)
|
|
|
|
if (!driven_bits.count(bit.first))
|
|
|
|
const_init_sigs.append(bit.first);
|
|
|
|
|
|
|
|
const_init_sigs.sort_and_unify();
|
|
|
|
|
|
|
|
for (SigSpec sig : const_init_sigs.chunks())
|
|
|
|
{
|
|
|
|
Const val;
|
|
|
|
|
|
|
|
for (auto bit : sig)
|
|
|
|
val.bits.push_back(init_bits.at(bit));
|
|
|
|
|
|
|
|
log("Promoting init spec %s = %s to constant driver in module %s.\n",
|
|
|
|
log_signal(sig), log_signal(val), log_id(module));
|
|
|
|
|
|
|
|
module->connect(sig, val);
|
|
|
|
remove_init_attr(sig);
|
|
|
|
total_initdrv++;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
assign_map.clear();
|
2013-05-23 00:48:18 -05:00
|
|
|
mux_drivers.clear();
|
2014-08-30 12:37:12 -05:00
|
|
|
|
2016-08-29 18:34:04 -05:00
|
|
|
if (total_count || total_initdrv)
|
2014-08-30 12:37:12 -05:00
|
|
|
design->scratchpad_set_bool("opt.did_something", true);
|
2016-08-29 18:34:04 -05:00
|
|
|
|
|
|
|
if (total_initdrv)
|
|
|
|
log("Promoted %d init specs to constant drivers.\n", total_initdrv);
|
|
|
|
|
|
|
|
if (total_count)
|
|
|
|
log("Replaced %d DFF cells.\n", total_count);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
} OptRmdffPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|