2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 "kernel/celltypes.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <set>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2013-03-03 06:18:37 -06:00
|
|
|
using RTLIL::id2cstr;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
struct OptMuxtreeWorker
|
|
|
|
{
|
|
|
|
RTLIL::Design *design;
|
|
|
|
RTLIL::Module *module;
|
|
|
|
SigMap assign_map;
|
|
|
|
int removed_count;
|
|
|
|
|
|
|
|
struct bitinfo_t {
|
|
|
|
bool seen_non_mux;
|
2015-01-18 05:13:18 -06:00
|
|
|
pool<int> mux_users;
|
|
|
|
pool<int> mux_drivers;
|
2013-01-05 04:13:26 -06:00
|
|
|
};
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
idict<SigBit> bit2num;
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<bitinfo_t> bit2info;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
struct portinfo_t {
|
2015-01-18 04:17:56 -06:00
|
|
|
int ctrl_sig;
|
2015-01-18 05:13:18 -06:00
|
|
|
pool<int> input_sigs;
|
|
|
|
pool<int> input_muxes;
|
2013-01-05 04:13:26 -06:00
|
|
|
bool const_activated;
|
2015-01-18 04:17:56 -06:00
|
|
|
bool const_deactivated;
|
2013-01-05 04:13:26 -06:00
|
|
|
bool enabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct muxinfo_t {
|
|
|
|
RTLIL::Cell *cell;
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<portinfo_t> ports;
|
2013-01-05 04:13:26 -06:00
|
|
|
};
|
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<muxinfo_t> mux2info;
|
|
|
|
vector<bool> root_muxes;
|
2015-01-17 06:56:53 -06:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
OptMuxtreeWorker(RTLIL::Design *design, RTLIL::Module *module) :
|
|
|
|
design(design), module(module), assign_map(module), removed_count(0)
|
|
|
|
{
|
|
|
|
log("Running muxtree optimizier on module %s..\n", module->name.c_str());
|
|
|
|
|
|
|
|
log(" Creating internal representation of mux trees.\n");
|
|
|
|
|
|
|
|
// Populate bit2info[]:
|
|
|
|
// .seen_non_mux
|
|
|
|
// .mux_users
|
|
|
|
// .mux_drivers
|
|
|
|
// Populate mux2info[].ports[]:
|
2015-01-18 04:17:56 -06:00
|
|
|
// .ctrl_sig
|
2013-01-05 04:13:26 -06:00
|
|
|
// .input_sigs
|
|
|
|
// .const_activated
|
2015-01-18 04:17:56 -06:00
|
|
|
// .const_deactivated
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto cell : module->cells())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-08-14 04:39:46 -05:00
|
|
|
if (cell->type == "$mux" || cell->type == "$pmux")
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_a = cell->getPort("\\A");
|
|
|
|
RTLIL::SigSpec sig_b = cell->getPort("\\B");
|
|
|
|
RTLIL::SigSpec sig_s = cell->getPort("\\S");
|
|
|
|
RTLIL::SigSpec sig_y = cell->getPort("\\Y");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
muxinfo_t muxinfo;
|
|
|
|
muxinfo.cell = cell;
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int i = 0; i < GetSize(sig_s); i++) {
|
|
|
|
RTLIL::SigSpec sig = sig_b.extract(i*GetSize(sig_a), GetSize(sig_a));
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec ctrl_sig = assign_map(sig_s.extract(i, 1));
|
|
|
|
portinfo_t portinfo;
|
2015-01-18 04:17:56 -06:00
|
|
|
portinfo.ctrl_sig = sig2bits(ctrl_sig, false).front();
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int idx : sig2bits(sig)) {
|
2015-01-18 05:13:18 -06:00
|
|
|
bit2info[idx].mux_users.insert(GetSize(mux2info));
|
|
|
|
portinfo.input_sigs.insert(idx);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
portinfo.const_activated = ctrl_sig.is_fully_const() && ctrl_sig.as_bool();
|
2015-01-18 04:17:56 -06:00
|
|
|
portinfo.const_deactivated = ctrl_sig.is_fully_const() && !ctrl_sig.as_bool();
|
2013-01-05 04:13:26 -06:00
|
|
|
portinfo.enabled = false;
|
|
|
|
muxinfo.ports.push_back(portinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
portinfo_t portinfo;
|
|
|
|
for (int idx : sig2bits(sig_a)) {
|
2015-01-18 05:13:18 -06:00
|
|
|
bit2info[idx].mux_users.insert(GetSize(mux2info));
|
|
|
|
portinfo.input_sigs.insert(idx);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2015-01-18 04:17:56 -06:00
|
|
|
portinfo.ctrl_sig = -1;
|
2013-01-05 04:13:26 -06:00
|
|
|
portinfo.const_activated = false;
|
2015-01-18 04:17:56 -06:00
|
|
|
portinfo.const_deactivated = false;
|
2013-01-05 04:13:26 -06:00
|
|
|
portinfo.enabled = false;
|
|
|
|
muxinfo.ports.push_back(portinfo);
|
|
|
|
|
|
|
|
for (int idx : sig2bits(sig_y))
|
2015-01-18 05:13:18 -06:00
|
|
|
bit2info[idx].mux_drivers.insert(GetSize(mux2info));
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (int idx : sig2bits(sig_s))
|
|
|
|
bit2info[idx].seen_non_mux = true;
|
|
|
|
|
|
|
|
mux2info.push_back(muxinfo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &it : cell->connections()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int idx : sig2bits(it.second))
|
|
|
|
bit2info[idx].seen_non_mux = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto wire : module->wires()) {
|
|
|
|
if (wire->port_output)
|
|
|
|
for (int idx : sig2bits(RTLIL::SigSpec(wire)))
|
2013-01-05 04:13:26 -06:00
|
|
|
bit2info[idx].seen_non_mux = true;
|
|
|
|
}
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (mux2info.empty()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
log(" No muxes found in this module.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate mux2info[].ports[]:
|
|
|
|
// .input_muxes
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int i = 0; i < GetSize(bit2info); i++)
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int j : bit2info[i].mux_users)
|
|
|
|
for (auto &p : mux2info[j].ports) {
|
2015-01-18 05:13:18 -06:00
|
|
|
if (p.input_sigs.count(i))
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int k : bit2info[i].mux_drivers)
|
2015-01-18 05:13:18 -06:00
|
|
|
p.input_muxes.insert(k);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
log(" Evaluating internal representation of mux trees.\n");
|
|
|
|
|
2015-01-17 06:56:53 -06:00
|
|
|
dict<int, pool<int>> mux_to_users;
|
2015-01-18 05:13:18 -06:00
|
|
|
root_muxes.resize(GetSize(mux2info));
|
2015-01-17 06:56:53 -06:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto &bi : bit2info) {
|
2015-01-17 06:56:53 -06:00
|
|
|
for (int i : bi.mux_drivers)
|
|
|
|
for (int j : bi.mux_users)
|
|
|
|
mux_to_users[i].insert(j);
|
2013-01-05 04:13:26 -06:00
|
|
|
if (!bi.seen_non_mux)
|
|
|
|
continue;
|
|
|
|
for (int mux_idx : bi.mux_drivers)
|
2015-01-18 04:17:56 -06:00
|
|
|
root_muxes.at(mux_idx) = true;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2015-01-17 06:56:53 -06:00
|
|
|
|
|
|
|
for (auto &it : mux_to_users)
|
|
|
|
if (GetSize(it.second) > 1)
|
2015-01-18 04:17:56 -06:00
|
|
|
root_muxes.at(it.first) = true;
|
2015-01-17 06:56:53 -06:00
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
for (int mux_idx = 0; mux_idx < GetSize(root_muxes); mux_idx++)
|
|
|
|
if (root_muxes.at(mux_idx)) {
|
|
|
|
log(" Root of a mux tree: %s\n", log_id(mux2info[mux_idx].cell));
|
|
|
|
eval_root_mux(mux_idx);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
log(" Analyzing evaluation results.\n");
|
|
|
|
|
|
|
|
for (auto &mi : mux2info)
|
|
|
|
{
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> live_ports;
|
2014-10-11 04:42:08 -05:00
|
|
|
for (int port_idx = 0; port_idx < GetSize(mi.ports); port_idx++) {
|
2013-01-05 04:13:26 -06:00
|
|
|
portinfo_t &pi = mi.ports[port_idx];
|
|
|
|
if (pi.enabled) {
|
|
|
|
live_ports.push_back(port_idx);
|
|
|
|
} else {
|
2014-10-11 04:42:08 -05:00
|
|
|
log(" dead port %d/%d on %s %s.\n", port_idx+1, GetSize(mi.ports),
|
2013-01-05 04:13:26 -06:00
|
|
|
mi.cell->type.c_str(), mi.cell->name.c_str());
|
|
|
|
removed_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (GetSize(live_ports) == GetSize(mi.ports))
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (live_ports.empty()) {
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(mi.cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_a = mi.cell->getPort("\\A");
|
|
|
|
RTLIL::SigSpec sig_b = mi.cell->getPort("\\B");
|
|
|
|
RTLIL::SigSpec sig_s = mi.cell->getPort("\\S");
|
|
|
|
RTLIL::SigSpec sig_y = mi.cell->getPort("\\Y");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
RTLIL::SigSpec sig_ports = sig_b;
|
|
|
|
sig_ports.append(sig_a);
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (GetSize(live_ports) == 1)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2015-01-18 05:13:18 -06:00
|
|
|
RTLIL::SigSpec sig_in = sig_ports.extract(live_ports[0]*GetSize(sig_a), GetSize(sig_a));
|
2014-07-26 07:32:50 -05:00
|
|
|
module->connect(RTLIL::SigSig(sig_y, sig_in));
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(mi.cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RTLIL::SigSpec new_sig_a, new_sig_b, new_sig_s;
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int i = 0; i < GetSize(live_ports); i++) {
|
|
|
|
RTLIL::SigSpec sig_in = sig_ports.extract(live_ports[i]*GetSize(sig_a), GetSize(sig_a));
|
|
|
|
if (i == GetSize(live_ports)-1) {
|
2013-01-05 04:13:26 -06:00
|
|
|
new_sig_a = sig_in;
|
|
|
|
} else {
|
|
|
|
new_sig_b.append(sig_in);
|
|
|
|
new_sig_s.append(sig_s.extract(live_ports[i], 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
mi.cell->setPort("\\A", new_sig_a);
|
|
|
|
mi.cell->setPort("\\B", new_sig_b);
|
|
|
|
mi.cell->setPort("\\S", new_sig_s);
|
2015-01-18 05:13:18 -06:00
|
|
|
if (GetSize(new_sig_s) == 1) {
|
2013-01-05 04:13:26 -06:00
|
|
|
mi.cell->type = "$mux";
|
2013-11-10 17:02:28 -06:00
|
|
|
mi.cell->parameters.erase("\\S_WIDTH");
|
2013-01-05 04:13:26 -06:00
|
|
|
} else {
|
2015-01-18 05:13:18 -06:00
|
|
|
mi.cell->parameters["\\S_WIDTH"] = RTLIL::Const(GetSize(new_sig_s));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> sig2bits(RTLIL::SigSpec sig, bool skip_non_wires = true)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> results;
|
2013-01-05 04:13:26 -06:00
|
|
|
assign_map.apply(sig);
|
2014-07-23 09:09:27 -05:00
|
|
|
for (auto &bit : sig)
|
|
|
|
if (bit.wire != NULL) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (bit2num.count(bit) == 0) {
|
|
|
|
bitinfo_t info;
|
|
|
|
info.seen_non_mux = false;
|
2015-01-18 05:13:18 -06:00
|
|
|
bit2num.expect(bit, GetSize(bit2info));
|
2013-01-05 04:13:26 -06:00
|
|
|
bit2info.push_back(info);
|
|
|
|
}
|
2015-01-18 05:13:18 -06:00
|
|
|
results.push_back(bit2num.at(bit));
|
2015-01-17 05:05:19 -06:00
|
|
|
} else if (!skip_non_wires)
|
|
|
|
results.push_back(-1);
|
2013-01-05 04:13:26 -06:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct knowledge_t
|
|
|
|
{
|
|
|
|
// database of known inactive signals
|
2015-01-18 04:17:56 -06:00
|
|
|
// the payload is a reference counter used to manage the
|
2013-01-05 04:13:26 -06:00
|
|
|
// list. when it is non-zero the signal in known to be inactive
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> known_inactive;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
// database of known active signals
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> known_active;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
// this is just used to keep track of visited muxes in order to prohibit
|
|
|
|
// endless recursion in mux loops
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<bool> visited_muxes;
|
2013-01-05 04:13:26 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
void eval_mux_port(knowledge_t &knowledge, int mux_idx, int port_idx)
|
|
|
|
{
|
|
|
|
muxinfo_t &muxinfo = mux2info[mux_idx];
|
2015-01-18 04:17:56 -06:00
|
|
|
|
|
|
|
if (muxinfo.ports[port_idx].const_deactivated)
|
|
|
|
return;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
muxinfo.ports[port_idx].enabled = true;
|
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
for (int i = 0; i < GetSize(muxinfo.ports); i++) {
|
|
|
|
if (i == port_idx)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2015-01-18 04:17:56 -06:00
|
|
|
if (muxinfo.ports[i].ctrl_sig >= 0)
|
|
|
|
knowledge.known_inactive.at(muxinfo.ports[i].ctrl_sig)++;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (port_idx < GetSize(muxinfo.ports)-1 && !muxinfo.ports[port_idx].const_activated)
|
2015-01-18 04:17:56 -06:00
|
|
|
knowledge.known_active.at(muxinfo.ports[port_idx].ctrl_sig)++;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> parent_muxes;
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int m : muxinfo.ports[port_idx].input_muxes) {
|
2015-01-18 04:17:56 -06:00
|
|
|
if (knowledge.visited_muxes[m])
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2015-01-18 04:17:56 -06:00
|
|
|
knowledge.visited_muxes[m] = true;
|
2014-03-19 04:05:01 -05:00
|
|
|
parent_muxes.push_back(m);
|
|
|
|
}
|
|
|
|
for (int m : parent_muxes)
|
2015-01-18 04:17:56 -06:00
|
|
|
if (!root_muxes.at(m))
|
2015-01-17 06:56:53 -06:00
|
|
|
eval_mux(knowledge, m);
|
2014-03-19 04:05:01 -05:00
|
|
|
for (int m : parent_muxes)
|
2015-01-18 04:17:56 -06:00
|
|
|
knowledge.visited_muxes[m] = false;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (port_idx < GetSize(muxinfo.ports)-1 && !muxinfo.ports[port_idx].const_activated)
|
2015-01-18 04:17:56 -06:00
|
|
|
knowledge.known_active.at(muxinfo.ports[port_idx].ctrl_sig)--;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int i = 0; i < GetSize(muxinfo.ports); i++) {
|
|
|
|
if (i == port_idx)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2015-01-18 04:17:56 -06:00
|
|
|
if (muxinfo.ports[i].ctrl_sig >= 0)
|
|
|
|
knowledge.known_inactive.at(muxinfo.ports[i].ctrl_sig)--;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 05:05:19 -06:00
|
|
|
void replace_known(knowledge_t &knowledge, muxinfo_t &muxinfo, IdString portname)
|
|
|
|
{
|
|
|
|
SigSpec sig = muxinfo.cell->getPort(portname);
|
|
|
|
bool did_something = false;
|
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
vector<int> bits = sig2bits(sig, false);
|
2015-01-17 05:05:19 -06:00
|
|
|
for (int i = 0; i < GetSize(bits); i++) {
|
|
|
|
if (bits[i] < 0)
|
|
|
|
continue;
|
2015-01-18 04:17:56 -06:00
|
|
|
if (knowledge.known_inactive.at(bits[i])) {
|
2015-01-17 05:05:19 -06:00
|
|
|
sig[i] = State::S0;
|
|
|
|
did_something = true;
|
2015-01-18 04:17:56 -06:00
|
|
|
} else
|
|
|
|
if (knowledge.known_active.at(bits[i])) {
|
|
|
|
sig[i] = State::S1;
|
|
|
|
did_something = true;
|
2015-01-17 05:05:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (did_something) {
|
2015-01-17 06:56:53 -06:00
|
|
|
log(" Replacing known input bits on port %s of cell %s: %s -> %s\n", log_id(portname),
|
2015-01-17 05:05:19 -06:00
|
|
|
log_id(muxinfo.cell), log_signal(muxinfo.cell->getPort(portname)), log_signal(sig));
|
|
|
|
muxinfo.cell->setPort(portname, sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
void eval_mux(knowledge_t &knowledge, int mux_idx)
|
|
|
|
{
|
|
|
|
muxinfo_t &muxinfo = mux2info[mux_idx];
|
|
|
|
|
2015-01-17 05:05:19 -06:00
|
|
|
// set input ports to constants if we find known active or inactive signals
|
|
|
|
replace_known(knowledge, muxinfo, "\\A");
|
|
|
|
replace_known(knowledge, muxinfo, "\\B");
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
// if there is a constant activated port we just use it
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int port_idx = 0; port_idx < GetSize(muxinfo.ports); port_idx++)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
portinfo_t &portinfo = muxinfo.ports[port_idx];
|
|
|
|
if (portinfo.const_activated) {
|
|
|
|
eval_mux_port(knowledge, mux_idx, port_idx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare ports with known_active signals. if we find a match, only this
|
|
|
|
// port can be active. do not include the last port (its the default port
|
|
|
|
// that has no control signals).
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int port_idx = 0; port_idx < GetSize(muxinfo.ports)-1; port_idx++)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
portinfo_t &portinfo = muxinfo.ports[port_idx];
|
2015-01-18 04:17:56 -06:00
|
|
|
if (knowledge.known_active.at(portinfo.ctrl_sig)) {
|
|
|
|
eval_mux_port(knowledge, mux_idx, port_idx);
|
|
|
|
return;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 04:17:56 -06:00
|
|
|
// compare ports with known_inactive and known_active signals. If the control
|
|
|
|
// signal of the port is known_inactive or if the control signals of all other
|
2013-01-05 04:13:26 -06:00
|
|
|
// ports are known_active this port can't be activated. this loop includes the
|
|
|
|
// default port but no known_inactive match is performed on the default port.
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int port_idx = 0; port_idx < GetSize(muxinfo.ports); port_idx++)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
portinfo_t &portinfo = muxinfo.ports[port_idx];
|
|
|
|
|
2015-01-18 05:13:18 -06:00
|
|
|
if (port_idx < GetSize(muxinfo.ports)-1) {
|
2013-01-05 04:13:26 -06:00
|
|
|
bool found_non_known_inactive = false;
|
2015-01-18 04:17:56 -06:00
|
|
|
if (knowledge.known_inactive.at(portinfo.ctrl_sig) == 0)
|
|
|
|
found_non_known_inactive = true;
|
2013-01-05 04:13:26 -06:00
|
|
|
if (!found_non_known_inactive)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool port_active = true;
|
2015-01-18 05:13:18 -06:00
|
|
|
for (int i = 0; i < GetSize(muxinfo.ports)-1; i++) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (i == port_idx)
|
|
|
|
continue;
|
2015-01-18 04:17:56 -06:00
|
|
|
if (knowledge.known_active.at(muxinfo.ports[i].ctrl_sig))
|
2013-01-05 04:13:26 -06:00
|
|
|
port_active = false;
|
|
|
|
}
|
|
|
|
if (port_active)
|
|
|
|
eval_mux_port(knowledge, mux_idx, port_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void eval_root_mux(int mux_idx)
|
|
|
|
{
|
|
|
|
knowledge_t knowledge;
|
2015-01-18 05:13:18 -06:00
|
|
|
knowledge.known_inactive.resize(GetSize(bit2info));
|
|
|
|
knowledge.known_active.resize(GetSize(bit2info));
|
|
|
|
knowledge.visited_muxes.resize(GetSize(mux2info));
|
2015-01-18 04:17:56 -06:00
|
|
|
knowledge.visited_muxes[mux_idx] = true;
|
2013-01-05 04:13:26 -06:00
|
|
|
eval_mux(knowledge, mux_idx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OptMuxtreePass : public Pass {
|
2013-03-01 01:58:55 -06:00
|
|
|
OptMuxtreePass() : Pass("opt_muxtree", "eliminate dead trees in multiplexer trees") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" opt_muxtree [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass analyzes the control signals for the multiplexer trees in the design\n");
|
2013-03-17 16:02:30 -05:00
|
|
|
log("and identifies inputs that can never be active. It then removes this dead\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
log("branches from the multiplexer trees.\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass only operates on completely selected modules without processes.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2015-01-18 04:17:56 -06:00
|
|
|
virtual void execute(vector<std::string> args, RTLIL::Design *design)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
log_header("Executing OPT_MUXTREE pass (detect dead branches in mux trees).\n");
|
|
|
|
extra_args(args, 1, design);
|
|
|
|
|
|
|
|
int total_count = 0;
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto mod : design->modules()) {
|
|
|
|
if (!design->selected_whole_module(mod)) {
|
|
|
|
if (design->selected(mod))
|
|
|
|
log("Skipping module %s as it is only partially selected.\n", log_id(mod));
|
2013-03-01 01:58:55 -06:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-18 05:13:18 -06:00
|
|
|
if (mod->has_processes_warn())
|
|
|
|
continue;
|
|
|
|
OptMuxtreeWorker worker(design, mod);
|
|
|
|
total_count += worker.removed_count;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-08-30 12:37:12 -05:00
|
|
|
if (total_count)
|
|
|
|
design->scratchpad_set_bool("opt.did_something", true);
|
2013-01-05 04:13:26 -06:00
|
|
|
log("Removed %d multiplexer ports.\n", total_count);
|
|
|
|
}
|
|
|
|
} OptMuxtreePass;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|