Improve opt_clean handling of unused wires

Signed-off-by: Clifford Wolf <clifford@clifford.at>
This commit is contained in:
Clifford Wolf 2019-05-04 09:47:16 +02:00
parent 66d6ca2de2
commit a01386c0e4
1 changed files with 22 additions and 10 deletions

View File

@ -242,6 +242,10 @@ void rmunused_module_signals(RTLIL::Module *module, bool purge_mode, bool verbos
} }
} }
SigPool raw_used_signals_noaliases;
for (auto &it : module->connections_)
raw_used_signals_noaliases.add(it.second);
module->connections_.clear(); module->connections_.clear();
SigPool used_signals; SigPool used_signals;
@ -251,6 +255,7 @@ void rmunused_module_signals(RTLIL::Module *module, bool purge_mode, bool verbos
for (auto &it2 : cell->connections_) { for (auto &it2 : cell->connections_) {
assign_map.apply(it2.second); assign_map.apply(it2.second);
used_signals.add(it2.second); used_signals.add(it2.second);
raw_used_signals_noaliases.add(it2.second);
if (!ct_all.cell_output(cell->type, it2.first)) if (!ct_all.cell_output(cell->type, it2.first))
used_signals_nodrivers.add(it2.second); used_signals_nodrivers.add(it2.second);
} }
@ -277,23 +282,30 @@ void rmunused_module_signals(RTLIL::Module *module, bool purge_mode, bool verbos
SigSpec s1 = SigSpec(wire), s2 = assign_map(s1); SigSpec s1 = SigSpec(wire), s2 = assign_map(s1);
log_assert(GetSize(s1) == GetSize(s2)); log_assert(GetSize(s1) == GetSize(s2));
Const initval;
if (wire->attributes.count("\\init"))
initval = wire->attributes.at("\\init");
if (GetSize(initval) != GetSize(wire))
initval.bits.resize(GetSize(wire), State::Sx);
if (initval.is_fully_undef())
wire->attributes.erase("\\init");
bool maybe_del = false; bool maybe_del = false;
if ((!purge_mode && check_public_name(wire->name)) || wire->port_id != 0 || wire->get_bool_attribute("\\keep") || wire->attributes.count("\\init")) { if (wire->port_id != 0 || wire->get_bool_attribute("\\keep") || !initval.is_fully_undef()) {
if (!used_signals.check_any(s2) && wire->port_id == 0 && !wire->get_bool_attribute("\\keep")) /* do not delete anything with "keep" or module ports or initialized wires */
maybe_del = true; } else
if (!purge_mode && check_public_name(wire->name)) {
/* do not get rid of public names unless in purge mode */
} else { } else {
if (!used_signals.check_any(s2)) if (!raw_used_signals_noaliases.check_any(s1))
maybe_del = true;
if (!used_signals_nodrivers.check_any(s2))
maybe_del = true; maybe_del = true;
} }
if (maybe_del) { if (maybe_del) {
maybe_del_wires.push_back(wire); maybe_del_wires.push_back(wire);
} else { } else {
Const initval;
if (wire->attributes.count("\\init"))
initval = wire->attributes.at("\\init");
if (GetSize(initval) != GetSize(wire))
initval.bits.resize(GetSize(wire), State::Sx);
RTLIL::SigSig new_conn; RTLIL::SigSig new_conn;
for (int i = 0; i < GetSize(s1); i++) for (int i = 0; i < GetSize(s1); i++)
if (s1[i] != s2[i]) { if (s1[i] != s2[i]) {
@ -341,7 +353,7 @@ void rmunused_module_signals(RTLIL::Module *module, bool purge_mode, bool verbos
int del_wires_count = 0; int del_wires_count = 0;
for (auto wire : maybe_del_wires) { for (auto wire : maybe_del_wires) {
SigSpec s1 = SigSpec(wire); SigSpec s1 = SigSpec(wire);
if (used_signals.check_any(s1)) { if (used_signals_nodrivers.check_any(s1)) {
SigSpec s2 = assign_map(s1); SigSpec s2 = assign_map(s1);
Const initval; Const initval;
if (wire->attributes.count("\\init")) if (wire->attributes.count("\\init"))