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 "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-01-05 04:13:26 -06:00
|
|
|
struct OptReduceWorker
|
|
|
|
{
|
|
|
|
RTLIL::Design *design;
|
|
|
|
RTLIL::Module *module;
|
|
|
|
SigMap assign_map;
|
|
|
|
|
|
|
|
int total_count;
|
|
|
|
bool did_something;
|
|
|
|
|
2014-12-28 14:27:05 -06:00
|
|
|
void opt_reduce(pool<RTLIL::Cell*> &cells, SigSet<RTLIL::Cell*> &drivers, RTLIL::Cell *cell)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (cells.count(cell) == 0)
|
|
|
|
return;
|
|
|
|
cells.erase(cell);
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
|
2014-12-28 14:27:05 -06:00
|
|
|
pool<RTLIL::SigBit> new_sig_a_bits;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-23 09:09:27 -05:00
|
|
|
for (auto &bit : sig_a.to_sigbit_set())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-23 09:09:27 -05:00
|
|
|
if (bit == RTLIL::State::S0) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (cell->type == "$reduce_and") {
|
2014-07-23 09:09:27 -05:00
|
|
|
new_sig_a_bits.clear();
|
|
|
|
new_sig_a_bits.insert(RTLIL::State::S0);
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-23 09:09:27 -05:00
|
|
|
if (bit == RTLIL::State::S1) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (cell->type == "$reduce_or") {
|
2014-07-23 09:09:27 -05:00
|
|
|
new_sig_a_bits.clear();
|
|
|
|
new_sig_a_bits.insert(RTLIL::State::S1);
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-23 09:09:27 -05:00
|
|
|
if (bit.wire == NULL) {
|
|
|
|
new_sig_a_bits.insert(bit);
|
2014-03-06 07:18:34 -06:00
|
|
|
continue;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool imported_children = false;
|
2014-07-23 09:09:27 -05:00
|
|
|
for (auto child_cell : drivers.find(bit)) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (child_cell->type == cell->type) {
|
|
|
|
opt_reduce(cells, drivers, child_cell);
|
2014-07-31 09:38:54 -05:00
|
|
|
if (child_cell->getPort("\\Y")[0] == bit) {
|
2014-12-28 14:27:05 -06:00
|
|
|
pool<RTLIL::SigBit> child_sig_a_bits = assign_map(child_cell->getPort("\\A")).to_sigbit_pool();
|
2014-07-23 09:09:27 -05:00
|
|
|
new_sig_a_bits.insert(child_sig_a_bits.begin(), child_sig_a_bits.end());
|
|
|
|
} else
|
|
|
|
new_sig_a_bits.insert(RTLIL::State::S0);
|
2013-01-05 04:13:26 -06:00
|
|
|
imported_children = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!imported_children)
|
2014-07-23 09:09:27 -05:00
|
|
|
new_sig_a_bits.insert(bit);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-23 09:09:27 -05:00
|
|
|
|
|
|
|
RTLIL::SigSpec new_sig_a(new_sig_a_bits);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (new_sig_a != sig_a || sig_a.size() != cell->getPort("\\A").size()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
log(" New input vector for %s cell %s: %s\n", cell->type.c_str(), cell->name.c_str(), log_signal(new_sig_a));
|
|
|
|
did_something = true;
|
|
|
|
total_count++;
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\A", new_sig_a);
|
2014-07-22 13:15:14 -05:00
|
|
|
cell->parameters["\\A_WIDTH"] = RTLIL::Const(new_sig_a.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void opt_mux(RTLIL::Cell *cell)
|
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_a = assign_map(cell->getPort("\\A"));
|
|
|
|
RTLIL::SigSpec sig_b = assign_map(cell->getPort("\\B"));
|
|
|
|
RTLIL::SigSpec sig_s = assign_map(cell->getPort("\\S"));
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
RTLIL::SigSpec new_sig_b, new_sig_s;
|
2014-12-28 14:27:05 -06:00
|
|
|
pool<RTLIL::SigSpec> handled_sig;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
handled_sig.insert(sig_a);
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < sig_s.size(); i++)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-22 13:15:14 -05:00
|
|
|
RTLIL::SigSpec this_b = sig_b.extract(i*sig_a.size(), sig_a.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
if (handled_sig.count(this_b) > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
RTLIL::SigSpec this_s = sig_s.extract(i, 1);
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int j = i+1; j < sig_s.size(); j++) {
|
|
|
|
RTLIL::SigSpec that_b = sig_b.extract(j*sig_a.size(), sig_a.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
if (this_b == that_b)
|
|
|
|
this_s.append(sig_s.extract(j, 1));
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (this_s.size() > 1)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-25 08:05:18 -05:00
|
|
|
RTLIL::Cell *reduce_or_cell = module->addCell(NEW_ID, "$reduce_or");
|
2014-07-31 09:38:54 -05:00
|
|
|
reduce_or_cell->setPort("\\A", this_s);
|
2013-11-10 17:02:28 -06:00
|
|
|
reduce_or_cell->parameters["\\A_SIGNED"] = RTLIL::Const(0);
|
2014-07-22 13:15:14 -05:00
|
|
|
reduce_or_cell->parameters["\\A_WIDTH"] = RTLIL::Const(this_s.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
reduce_or_cell->parameters["\\Y_WIDTH"] = RTLIL::Const(1);
|
|
|
|
|
2014-07-25 08:05:18 -05:00
|
|
|
RTLIL::Wire *reduce_or_wire = module->addWire(NEW_ID);
|
2013-01-05 04:13:26 -06:00
|
|
|
this_s = RTLIL::SigSpec(reduce_or_wire);
|
2014-07-31 09:38:54 -05:00
|
|
|
reduce_or_cell->setPort("\\Y", this_s);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
new_sig_b.append(this_b);
|
|
|
|
new_sig_s.append(this_s);
|
|
|
|
handled_sig.insert(this_b);
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (new_sig_s.size() != sig_s.size()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
log(" New ctrl vector for %s cell %s: %s\n", cell->type.c_str(), cell->name.c_str(), log_signal(new_sig_s));
|
|
|
|
did_something = true;
|
|
|
|
total_count++;
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (new_sig_s.size() == 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
module->connect(RTLIL::SigSig(cell->getPort("\\Y"), cell->getPort("\\A")));
|
|
|
|
assign_map.add(cell->getPort("\\Y"), cell->getPort("\\A"));
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\B", new_sig_b);
|
|
|
|
cell->setPort("\\S", new_sig_s);
|
2014-07-22 13:15:14 -05:00
|
|
|
if (new_sig_s.size() > 1) {
|
|
|
|
cell->parameters["\\S_WIDTH"] = RTLIL::Const(new_sig_s.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
} else {
|
|
|
|
cell->type = "$mux";
|
|
|
|
cell->parameters.erase("\\S_WIDTH");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 06:37:41 -05:00
|
|
|
void opt_mux_bits(RTLIL::Cell *cell)
|
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
std::vector<RTLIL::SigBit> sig_a = assign_map(cell->getPort("\\A")).to_sigbit_vector();
|
|
|
|
std::vector<RTLIL::SigBit> sig_b = assign_map(cell->getPort("\\B")).to_sigbit_vector();
|
|
|
|
std::vector<RTLIL::SigBit> sig_y = assign_map(cell->getPort("\\Y")).to_sigbit_vector();
|
2014-07-16 06:37:41 -05:00
|
|
|
|
|
|
|
std::vector<RTLIL::SigBit> new_sig_y;
|
|
|
|
RTLIL::SigSig old_sig_conn;
|
|
|
|
|
|
|
|
std::vector<std::vector<RTLIL::SigBit>> consolidated_in_tuples;
|
|
|
|
std::map<std::vector<RTLIL::SigBit>, RTLIL::SigBit> consolidated_in_tuples_map;
|
|
|
|
|
|
|
|
for (int i = 0; i < int(sig_y.size()); i++)
|
|
|
|
{
|
|
|
|
std::vector<RTLIL::SigBit> in_tuple;
|
2014-07-17 05:12:04 -05:00
|
|
|
bool all_tuple_bits_same = true;
|
|
|
|
|
2014-07-16 06:37:41 -05:00
|
|
|
in_tuple.push_back(sig_a.at(i));
|
2014-07-17 05:12:04 -05:00
|
|
|
for (int j = i; j < int(sig_b.size()); j += int(sig_a.size())) {
|
|
|
|
if (sig_b.at(j) != sig_a.at(i))
|
|
|
|
all_tuple_bits_same = false;
|
2014-07-16 06:37:41 -05:00
|
|
|
in_tuple.push_back(sig_b.at(j));
|
2014-07-17 05:12:04 -05:00
|
|
|
}
|
2014-07-16 06:37:41 -05:00
|
|
|
|
2014-07-17 05:12:04 -05:00
|
|
|
if (all_tuple_bits_same)
|
|
|
|
{
|
|
|
|
old_sig_conn.first.append_bit(sig_y.at(i));
|
|
|
|
old_sig_conn.second.append_bit(sig_a.at(i));
|
|
|
|
}
|
|
|
|
else if (consolidated_in_tuples_map.count(in_tuple))
|
2014-07-16 06:37:41 -05:00
|
|
|
{
|
|
|
|
old_sig_conn.first.append_bit(sig_y.at(i));
|
|
|
|
old_sig_conn.second.append_bit(consolidated_in_tuples_map.at(in_tuple));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
consolidated_in_tuples_map[in_tuple] = sig_y.at(i);
|
|
|
|
consolidated_in_tuples.push_back(in_tuple);
|
|
|
|
new_sig_y.push_back(sig_y.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_sig_y.size() != sig_y.size())
|
|
|
|
{
|
|
|
|
log(" Consolidated identical input bits for %s cell %s:\n", cell->type.c_str(), cell->name.c_str());
|
2014-07-31 09:38:54 -05:00
|
|
|
log(" Old ports: A=%s, B=%s, Y=%s\n", log_signal(cell->getPort("\\A")),
|
|
|
|
log_signal(cell->getPort("\\B")), log_signal(cell->getPort("\\Y")));
|
2014-07-16 06:37:41 -05:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\A", RTLIL::SigSpec());
|
2014-07-26 08:57:57 -05:00
|
|
|
for (auto &in_tuple : consolidated_in_tuples) {
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec new_a = cell->getPort("\\A");
|
2014-07-26 08:57:57 -05:00
|
|
|
new_a.append(in_tuple.at(0));
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\A", new_a);
|
2014-07-26 08:57:57 -05:00
|
|
|
}
|
2014-07-16 06:37:41 -05:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\B", RTLIL::SigSpec());
|
|
|
|
for (int i = 1; i <= cell->getPort("\\S").size(); i++)
|
2014-07-26 08:57:57 -05:00
|
|
|
for (auto &in_tuple : consolidated_in_tuples) {
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec new_b = cell->getPort("\\B");
|
2014-07-26 08:57:57 -05:00
|
|
|
new_b.append(in_tuple.at(i));
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\B", new_b);
|
2014-07-26 08:57:57 -05:00
|
|
|
}
|
2014-07-16 06:37:41 -05:00
|
|
|
|
|
|
|
cell->parameters["\\WIDTH"] = RTLIL::Const(new_sig_y.size());
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\Y", new_sig_y);
|
2014-07-16 06:37:41 -05:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
log(" New ports: A=%s, B=%s, Y=%s\n", log_signal(cell->getPort("\\A")),
|
|
|
|
log_signal(cell->getPort("\\B")), log_signal(cell->getPort("\\Y")));
|
2014-07-17 05:12:04 -05:00
|
|
|
log(" New connections: %s = %s\n", log_signal(old_sig_conn.first), log_signal(old_sig_conn.second));
|
|
|
|
|
2014-07-26 07:32:50 -05:00
|
|
|
module->connect(old_sig_conn);
|
2014-07-16 06:37:41 -05:00
|
|
|
module->check();
|
|
|
|
|
|
|
|
did_something = true;
|
|
|
|
total_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 09:34:16 -05:00
|
|
|
OptReduceWorker(RTLIL::Design *design, RTLIL::Module *module, bool do_fine) :
|
2013-01-05 04:13:26 -06:00
|
|
|
design(design), module(module), assign_map(module)
|
|
|
|
{
|
|
|
|
log(" Optimizing cells in module %s.\n", module->name.c_str());
|
|
|
|
|
|
|
|
total_count = 0;
|
|
|
|
did_something = true;
|
|
|
|
|
2014-07-16 06:37:41 -05:00
|
|
|
SigPool mem_wren_sigs;
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &cell_it : module->cells_) {
|
2014-07-16 06:37:41 -05:00
|
|
|
RTLIL::Cell *cell = cell_it.second;
|
|
|
|
if (cell->type == "$mem")
|
2014-07-31 09:38:54 -05:00
|
|
|
mem_wren_sigs.add(assign_map(cell->getPort("\\WR_EN")));
|
2014-07-16 06:37:41 -05:00
|
|
|
if (cell->type == "$memwr")
|
2014-07-31 09:38:54 -05:00
|
|
|
mem_wren_sigs.add(assign_map(cell->getPort("\\EN")));
|
2014-07-16 06:37:41 -05:00
|
|
|
}
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &cell_it : module->cells_) {
|
2014-07-16 06:37:41 -05:00
|
|
|
RTLIL::Cell *cell = cell_it.second;
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->type == "$dff" && mem_wren_sigs.check_any(assign_map(cell->getPort("\\Q"))))
|
|
|
|
mem_wren_sigs.add(assign_map(cell->getPort("\\D")));
|
2014-07-16 06:37:41 -05:00
|
|
|
}
|
|
|
|
|
2014-07-18 03:28:45 -05:00
|
|
|
bool keep_expanding_mem_wren_sigs = true;
|
|
|
|
while (keep_expanding_mem_wren_sigs) {
|
|
|
|
keep_expanding_mem_wren_sigs = false;
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &cell_it : module->cells_) {
|
2014-07-18 03:28:45 -05:00
|
|
|
RTLIL::Cell *cell = cell_it.second;
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->type == "$mux" && mem_wren_sigs.check_any(assign_map(cell->getPort("\\Y")))) {
|
|
|
|
if (!mem_wren_sigs.check_all(assign_map(cell->getPort("\\A"))) ||
|
|
|
|
!mem_wren_sigs.check_all(assign_map(cell->getPort("\\B"))))
|
2014-07-18 03:28:45 -05:00
|
|
|
keep_expanding_mem_wren_sigs = true;
|
2014-07-31 09:38:54 -05:00
|
|
|
mem_wren_sigs.add(assign_map(cell->getPort("\\A")));
|
|
|
|
mem_wren_sigs.add(assign_map(cell->getPort("\\B")));
|
2014-07-18 03:28:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
while (did_something)
|
|
|
|
{
|
|
|
|
did_something = false;
|
|
|
|
|
|
|
|
// merge trees of reduce_* cells to one single cell and unify input vectors
|
2015-08-14 03:56:05 -05:00
|
|
|
// (only handle reduce_and and reduce_or for various reasons)
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
const char *type_list[] = { "$reduce_or", "$reduce_and" };
|
|
|
|
for (auto type : type_list)
|
|
|
|
{
|
|
|
|
SigSet<RTLIL::Cell*> drivers;
|
2014-12-28 14:27:05 -06:00
|
|
|
pool<RTLIL::Cell*> cells;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &cell_it : module->cells_) {
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::Cell *cell = cell_it.second;
|
|
|
|
if (cell->type != type || !design->selected(module, cell))
|
|
|
|
continue;
|
2014-07-31 09:38:54 -05:00
|
|
|
drivers.insert(assign_map(cell->getPort("\\Y")), cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
cells.insert(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cells.size() > 0) {
|
|
|
|
RTLIL::Cell *cell = *cells.begin();
|
|
|
|
opt_reduce(cells, drivers, cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge identical inputs on $mux and $pmux cells
|
|
|
|
|
2014-07-25 05:49:51 -05:00
|
|
|
std::vector<RTLIL::Cell*> cells;
|
2014-07-16 06:37:41 -05:00
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_)
|
2014-08-14 04:39:46 -05:00
|
|
|
if ((it.second->type == "$mux" || it.second->type == "$pmux") && design->selected(module, it.second))
|
2014-07-25 05:49:51 -05:00
|
|
|
cells.push_back(it.second);
|
|
|
|
|
|
|
|
for (auto cell : cells)
|
|
|
|
{
|
2014-07-16 06:37:41 -05:00
|
|
|
// this optimization is to aggressive for most coarse-grain applications.
|
|
|
|
// but we always want it for multiplexers driving write enable ports.
|
2014-07-31 09:38:54 -05:00
|
|
|
if (do_fine || mem_wren_sigs.check_any(assign_map(cell->getPort("\\Y"))))
|
2014-07-16 06:37:41 -05:00
|
|
|
opt_mux_bits(cell);
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
opt_mux(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OptReducePass : public Pass {
|
2013-03-01 01:58:55 -06:00
|
|
|
OptReducePass() : Pass("opt_reduce", "simplify large MUXes and AND/OR gates") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2014-07-21 09:34:16 -05:00
|
|
|
log(" opt_reduce [options] [selection]\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
log("\n");
|
|
|
|
log("This pass performs two interlinked optimizations:\n");
|
|
|
|
log("\n");
|
|
|
|
log("1. it consolidates trees of large AND gates or OR gates and eliminates\n");
|
|
|
|
log("duplicated inputs.\n");
|
|
|
|
log("\n");
|
|
|
|
log("2. it identifies duplicated inputs to MUXes and replaces them with a single\n");
|
|
|
|
log("input with the original control signals OR'ed together.\n");
|
|
|
|
log("\n");
|
2014-07-21 09:34:16 -05:00
|
|
|
log(" -fine\n");
|
|
|
|
log(" perform fine-grain optimizations\n");
|
|
|
|
log("\n");
|
2014-10-30 21:36:51 -05:00
|
|
|
log(" -full\n");
|
|
|
|
log(" alias for -fine\n");
|
|
|
|
log("\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
2014-07-21 09:34:16 -05:00
|
|
|
bool do_fine = false;
|
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing OPT_REDUCE pass (consolidate $*mux and $reduce_* inputs).\n");
|
2014-07-21 09:34:16 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
if (args[argidx] == "-fine") {
|
|
|
|
do_fine = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-30 21:36:51 -05:00
|
|
|
if (args[argidx] == "-full") {
|
|
|
|
do_fine = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-21 09:34:16 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
int total_count = 0;
|
2015-02-03 16:45:01 -06:00
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
while (1) {
|
|
|
|
OptReduceWorker worker(design, module, do_fine);
|
2014-08-02 08:12:16 -05:00
|
|
|
total_count += worker.total_count;
|
|
|
|
if (worker.total_count == 0)
|
|
|
|
break;
|
2015-02-03 16:45:01 -06:00
|
|
|
}
|
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("Performed a total of %d changes.\n", total_count);
|
|
|
|
}
|
|
|
|
} OptReducePass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|