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"
|
2013-02-27 02:32:19 -06:00
|
|
|
#include "libs/sha1/sha1.h"
|
2013-01-05 04:13:26 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#define USE_CELL_HASH_CACHE
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2016-03-31 01:52:49 -05:00
|
|
|
struct OptMergeWorker
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
RTLIL::Design *design;
|
|
|
|
RTLIL::Module *module;
|
|
|
|
SigMap assign_map;
|
2014-02-04 05:02:47 -06:00
|
|
|
SigMap dff_init_map;
|
2015-05-31 07:24:34 -05:00
|
|
|
bool mode_share_all;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
CellTypes ct;
|
|
|
|
int total_count;
|
|
|
|
#ifdef USE_CELL_HASH_CACHE
|
2014-12-28 10:51:16 -06:00
|
|
|
dict<const RTLIL::Cell*, std::string> cell_hash_cache;
|
2013-01-05 04:13:26 -06:00
|
|
|
#endif
|
|
|
|
|
2016-03-31 02:58:55 -05:00
|
|
|
static void sort_pmux_conn(dict<RTLIL::IdString, RTLIL::SigSpec> &conn)
|
|
|
|
{
|
2019-08-09 11:58:14 -05:00
|
|
|
SigSpec sig_s = conn.at(ID(S));
|
2019-08-15 16:50:10 -05:00
|
|
|
SigSpec sig_b = conn.at(ID::B);
|
2016-03-31 02:58:55 -05:00
|
|
|
|
|
|
|
int s_width = GetSize(sig_s);
|
|
|
|
int width = GetSize(sig_b) / s_width;
|
|
|
|
|
|
|
|
vector<pair<SigBit, SigSpec>> sb_pairs;
|
|
|
|
for (int i = 0; i < s_width; i++)
|
|
|
|
sb_pairs.push_back(pair<SigBit, SigSpec>(sig_s[i], sig_b.extract(i*width, width)));
|
|
|
|
|
|
|
|
std::sort(sb_pairs.begin(), sb_pairs.end());
|
|
|
|
|
2019-08-09 11:58:14 -05:00
|
|
|
conn[ID(S)] = SigSpec();
|
2019-08-15 16:50:10 -05:00
|
|
|
conn[ID::B] = SigSpec();
|
2016-03-31 02:58:55 -05:00
|
|
|
|
|
|
|
for (auto &it : sb_pairs) {
|
2019-08-09 11:58:14 -05:00
|
|
|
conn[ID(S)].append(it.first);
|
2019-08-15 16:50:10 -05:00
|
|
|
conn[ID::B].append(it.second);
|
2016-03-31 02:58:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
#ifdef USE_CELL_HASH_CACHE
|
|
|
|
std::string int_to_hash_string(unsigned int v)
|
|
|
|
{
|
|
|
|
if (v == 0)
|
|
|
|
return "0";
|
|
|
|
std::string str = "";
|
|
|
|
while (v > 0) {
|
|
|
|
str += 'a' + (v & 15);
|
|
|
|
v = v >> 4;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string hash_cell_parameters_and_connections(const RTLIL::Cell *cell)
|
|
|
|
{
|
|
|
|
if (cell_hash_cache.count(cell) > 0)
|
|
|
|
return cell_hash_cache[cell];
|
|
|
|
|
2014-08-02 06:11:01 -05:00
|
|
|
std::string hash_string = cell->type.str() + "\n";
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (auto &it : cell->parameters)
|
2014-08-02 06:11:01 -05:00
|
|
|
hash_string += "P " + it.first.str() + "=" + it.second.as_string() + "\n";
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-12-26 03:53:21 -06:00
|
|
|
const dict<RTLIL::IdString, RTLIL::SigSpec> *conn = &cell->connections();
|
|
|
|
dict<RTLIL::IdString, RTLIL::SigSpec> alt_conn;
|
2013-03-29 05:01:26 -05:00
|
|
|
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell->type.in(ID($and), ID($or), ID($xor), ID($xnor), ID($add), ID($mul),
|
|
|
|
ID($logic_and), ID($logic_or), ID($_AND_), ID($_OR_), ID($_XOR_))) {
|
2013-03-29 05:01:26 -05:00
|
|
|
alt_conn = *conn;
|
2019-08-15 16:50:10 -05:00
|
|
|
if (assign_map(alt_conn.at(ID::A)) < assign_map(alt_conn.at(ID::B))) {
|
|
|
|
alt_conn[ID::A] = conn->at(ID::B);
|
|
|
|
alt_conn[ID::B] = conn->at(ID::A);
|
2013-03-29 05:01:26 -05:00
|
|
|
}
|
|
|
|
conn = &alt_conn;
|
2013-03-29 05:19:21 -05:00
|
|
|
} else
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell->type.in(ID($reduce_xor), ID($reduce_xnor))) {
|
2013-03-29 05:19:21 -05:00
|
|
|
alt_conn = *conn;
|
2019-08-15 16:50:10 -05:00
|
|
|
assign_map.apply(alt_conn.at(ID::A));
|
|
|
|
alt_conn.at(ID::A).sort();
|
2013-03-29 05:19:21 -05:00
|
|
|
conn = &alt_conn;
|
|
|
|
} else
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell->type.in(ID($reduce_and), ID($reduce_or), ID($reduce_bool))) {
|
2013-03-29 05:19:21 -05:00
|
|
|
alt_conn = *conn;
|
2019-08-15 16:50:10 -05:00
|
|
|
assign_map.apply(alt_conn.at(ID::A));
|
|
|
|
alt_conn.at(ID::A).sort_and_unify();
|
2013-03-29 05:19:21 -05:00
|
|
|
conn = &alt_conn;
|
2016-03-31 02:58:55 -05:00
|
|
|
} else
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell->type == ID($pmux)) {
|
2016-03-31 02:58:55 -05:00
|
|
|
alt_conn = *conn;
|
2019-08-15 16:50:10 -05:00
|
|
|
assign_map.apply(alt_conn.at(ID::A));
|
|
|
|
assign_map.apply(alt_conn.at(ID::B));
|
2019-08-09 11:58:14 -05:00
|
|
|
assign_map.apply(alt_conn.at(ID(S)));
|
2016-03-31 02:58:55 -05:00
|
|
|
sort_pmux_conn(alt_conn);
|
|
|
|
conn = &alt_conn;
|
2013-03-29 05:01:26 -05:00
|
|
|
}
|
|
|
|
|
2016-03-31 02:58:55 -05:00
|
|
|
vector<string> hash_conn_strings;
|
|
|
|
|
2013-03-29 05:01:26 -05:00
|
|
|
for (auto &it : *conn) {
|
2015-05-31 07:24:34 -05:00
|
|
|
if (cell->output(it.first))
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
RTLIL::SigSpec sig = it.second;
|
|
|
|
assign_map.apply(sig);
|
2016-03-31 02:58:55 -05:00
|
|
|
string s = "C " + it.first.str() + "=";
|
2014-07-22 13:15:14 -05:00
|
|
|
for (auto &chunk : sig.chunks()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (chunk.wire)
|
2016-03-31 02:58:55 -05:00
|
|
|
s += "{" + chunk.wire->name.str() + " " +
|
2013-01-05 04:13:26 -06:00
|
|
|
int_to_hash_string(chunk.offset) + " " +
|
|
|
|
int_to_hash_string(chunk.width) + "}";
|
|
|
|
else
|
2016-03-31 02:58:55 -05:00
|
|
|
s += RTLIL::Const(chunk.data).as_string();
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2016-03-31 02:58:55 -05:00
|
|
|
hash_conn_strings.push_back(s + "\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 02:58:55 -05:00
|
|
|
std::sort(hash_conn_strings.begin(), hash_conn_strings.end());
|
|
|
|
|
|
|
|
for (auto it : hash_conn_strings)
|
|
|
|
hash_string += it;
|
|
|
|
|
2014-08-01 12:01:10 -05:00
|
|
|
cell_hash_cache[cell] = sha1(hash_string);
|
2013-01-05 04:13:26 -06:00
|
|
|
return cell_hash_cache[cell];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool compare_cell_parameters_and_connections(const RTLIL::Cell *cell1, const RTLIL::Cell *cell2, bool <)
|
|
|
|
{
|
|
|
|
#ifdef USE_CELL_HASH_CACHE
|
|
|
|
std::string hash1 = hash_cell_parameters_and_connections(cell1);
|
|
|
|
std::string hash2 = hash_cell_parameters_and_connections(cell2);
|
|
|
|
|
|
|
|
if (hash1 != hash2) {
|
|
|
|
lt = hash1 < hash2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (cell1->parameters != cell2->parameters) {
|
2014-12-26 03:53:21 -06:00
|
|
|
std::map<RTLIL::IdString, RTLIL::Const> p1(cell1->parameters.begin(), cell1->parameters.end());
|
|
|
|
std::map<RTLIL::IdString, RTLIL::Const> p2(cell2->parameters.begin(), cell2->parameters.end());
|
|
|
|
lt = p1 < p2;
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-26 03:53:21 -06:00
|
|
|
dict<RTLIL::IdString, RTLIL::SigSpec> conn1 = cell1->connections();
|
|
|
|
dict<RTLIL::IdString, RTLIL::SigSpec> conn2 = cell2->connections();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (auto &it : conn1) {
|
2015-05-31 07:24:34 -05:00
|
|
|
if (cell1->output(it.first))
|
2013-01-05 04:13:26 -06:00
|
|
|
it.second = RTLIL::SigSpec();
|
|
|
|
else
|
|
|
|
assign_map.apply(it.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &it : conn2) {
|
2015-05-31 07:24:34 -05:00
|
|
|
if (cell2->output(it.first))
|
2013-01-05 04:13:26 -06:00
|
|
|
it.second = RTLIL::SigSpec();
|
|
|
|
else
|
|
|
|
assign_map.apply(it.second);
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell1->type == ID($and) || cell1->type == ID($or) || cell1->type == ID($xor) || cell1->type == ID($xnor) || cell1->type == ID($add) || cell1->type == ID($mul) ||
|
|
|
|
cell1->type == ID($logic_and) || cell1->type == ID($logic_or) || cell1->type == ID($_AND_) || cell1->type == ID($_OR_) || cell1->type == ID($_XOR_)) {
|
2019-08-15 16:50:10 -05:00
|
|
|
if (conn1.at(ID::A) < conn1.at(ID::B)) {
|
|
|
|
RTLIL::SigSpec tmp = conn1[ID::A];
|
|
|
|
conn1[ID::A] = conn1[ID::B];
|
|
|
|
conn1[ID::B] = tmp;
|
2013-03-29 05:01:26 -05:00
|
|
|
}
|
2019-08-15 16:50:10 -05:00
|
|
|
if (conn2.at(ID::A) < conn2.at(ID::B)) {
|
|
|
|
RTLIL::SigSpec tmp = conn2[ID::A];
|
|
|
|
conn2[ID::A] = conn2[ID::B];
|
|
|
|
conn2[ID::B] = tmp;
|
2013-03-29 05:01:26 -05:00
|
|
|
}
|
2013-03-29 05:19:21 -05:00
|
|
|
} else
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell1->type == ID($reduce_xor) || cell1->type == ID($reduce_xnor)) {
|
2019-08-15 16:50:10 -05:00
|
|
|
conn1[ID::A].sort();
|
|
|
|
conn2[ID::A].sort();
|
2013-03-29 05:19:21 -05:00
|
|
|
} else
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell1->type == ID($reduce_and) || cell1->type == ID($reduce_or) || cell1->type == ID($reduce_bool)) {
|
2019-08-15 16:50:10 -05:00
|
|
|
conn1[ID::A].sort_and_unify();
|
|
|
|
conn2[ID::A].sort_and_unify();
|
2016-03-31 02:58:55 -05:00
|
|
|
} else
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell1->type == ID($pmux)) {
|
2016-03-31 02:58:55 -05:00
|
|
|
sort_pmux_conn(conn1);
|
|
|
|
sort_pmux_conn(conn2);
|
2013-03-29 05:01:26 -05:00
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
if (conn1 != conn2) {
|
2014-12-26 03:53:21 -06:00
|
|
|
std::map<RTLIL::IdString, RTLIL::SigSpec> c1(conn1.begin(), conn1.end());
|
|
|
|
std::map<RTLIL::IdString, RTLIL::SigSpec> c2(conn2.begin(), conn2.end());
|
|
|
|
lt = c1 < c2;
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:00:04 -06:00
|
|
|
if (conn1.count(ID(Q)) != 0 && (cell1->type.begins_with("$dff") || cell1->type.begins_with("$dlatch") ||
|
|
|
|
cell1->type.begins_with("$_DFF") || cell1->type.begins_with("$_DLATCH") || cell1->type.begins_with("$_SR_") ||
|
|
|
|
cell1->type.in("$adff", "$sr", "$ff", "$_FF_"))) {
|
2019-08-09 11:58:14 -05:00
|
|
|
std::vector<RTLIL::SigBit> q1 = dff_init_map(cell1->getPort(ID(Q))).to_sigbit_vector();
|
|
|
|
std::vector<RTLIL::SigBit> q2 = dff_init_map(cell2->getPort(ID(Q))).to_sigbit_vector();
|
2014-02-04 05:02:47 -06:00
|
|
|
for (size_t i = 0; i < q1.size(); i++)
|
|
|
|
if ((q1.at(i).wire == NULL || q2.at(i).wire == NULL) && q1.at(i) != q2.at(i)) {
|
|
|
|
lt = q1.at(i) < q2.at(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool compare_cells(const RTLIL::Cell *cell1, const RTLIL::Cell *cell2)
|
|
|
|
{
|
|
|
|
if (cell1->type != cell2->type)
|
|
|
|
return cell1->type < cell2->type;
|
|
|
|
|
2015-05-31 07:24:34 -05:00
|
|
|
if ((!mode_share_all && !ct.cell_known(cell1->type)) || !cell1->known())
|
2013-01-05 04:13:26 -06:00
|
|
|
return cell1 < cell2;
|
|
|
|
|
2014-09-29 05:51:54 -05:00
|
|
|
if (cell1->has_keep_attr() || cell2->has_keep_attr())
|
2014-02-04 05:02:47 -06:00
|
|
|
return cell1 < cell2;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
bool lt;
|
|
|
|
if (compare_cell_parameters_and_connections(cell1, cell2, lt))
|
|
|
|
return lt;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CompareCells {
|
2016-03-31 01:52:49 -05:00
|
|
|
OptMergeWorker *that;
|
|
|
|
CompareCells(OptMergeWorker *that) : that(that) {}
|
2013-01-05 04:13:26 -06:00
|
|
|
bool operator()(const RTLIL::Cell *cell1, const RTLIL::Cell *cell2) const {
|
|
|
|
return that->compare_cells(cell1, cell2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-31 01:52:49 -05:00
|
|
|
OptMergeWorker(RTLIL::Design *design, RTLIL::Module *module, bool mode_nomux, bool mode_share_all) :
|
2015-05-31 07:24:34 -05:00
|
|
|
design(design), module(module), assign_map(module), mode_share_all(mode_share_all)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
total_count = 0;
|
|
|
|
ct.setup_internals();
|
|
|
|
ct.setup_internals_mem();
|
|
|
|
ct.setup_stdcells();
|
|
|
|
ct.setup_stdcells_mem();
|
|
|
|
|
|
|
|
if (mode_nomux) {
|
2019-08-09 11:58:14 -05:00
|
|
|
ct.cell_types.erase(ID($mux));
|
|
|
|
ct.cell_types.erase(ID($pmux));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2019-08-09 11:58:14 -05:00
|
|
|
ct.cell_types.erase(ID($tribuf));
|
|
|
|
ct.cell_types.erase(ID($_TBUF_));
|
|
|
|
ct.cell_types.erase(ID($anyseq));
|
|
|
|
ct.cell_types.erase(ID($anyconst));
|
|
|
|
ct.cell_types.erase(ID($allseq));
|
|
|
|
ct.cell_types.erase(ID($allconst));
|
2017-02-28 15:17:00 -06:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
log("Finding identical cells in module `%s'.\n", module->name.c_str());
|
|
|
|
assign_map.set(module);
|
|
|
|
|
2014-02-04 05:02:47 -06:00
|
|
|
dff_init_map.set(module);
|
2014-07-26 18:49:51 -05:00
|
|
|
for (auto &it : module->wires_)
|
2019-08-09 11:58:14 -05:00
|
|
|
if (it.second->attributes.count(ID(init)) != 0) {
|
|
|
|
Const initval = it.second->attributes.at(ID(init));
|
2017-02-09 09:06:58 -06:00
|
|
|
for (int i = 0; i < GetSize(initval) && i < GetSize(it.second); i++)
|
|
|
|
if (initval[i] == State::S0 || initval[i] == State::S1)
|
|
|
|
dff_init_map.add(SigBit(it.second, i), initval[i]);
|
|
|
|
}
|
2014-02-04 05:02:47 -06:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
bool did_something = true;
|
|
|
|
while (did_something)
|
|
|
|
{
|
|
|
|
#ifdef USE_CELL_HASH_CACHE
|
|
|
|
cell_hash_cache.clear();
|
|
|
|
#endif
|
|
|
|
std::vector<RTLIL::Cell*> cells;
|
2014-07-26 18:51:45 -05:00
|
|
|
cells.reserve(module->cells_.size());
|
|
|
|
for (auto &it : module->cells_) {
|
2015-05-31 07:24:34 -05:00
|
|
|
if (!design->selected(module, it.second))
|
|
|
|
continue;
|
|
|
|
if (ct.cell_known(it.second->type) || (mode_share_all && it.second->known()))
|
2013-01-05 04:13:26 -06:00
|
|
|
cells.push_back(it.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
did_something = false;
|
|
|
|
std::map<RTLIL::Cell*, RTLIL::Cell*, CompareCells> sharemap(CompareCells(this));
|
|
|
|
for (auto cell : cells)
|
|
|
|
{
|
|
|
|
if (sharemap.count(cell) > 0) {
|
|
|
|
did_something = true;
|
2019-04-22 10:25:52 -05:00
|
|
|
log_debug(" Cell `%s' is identical to cell `%s'.\n", cell->name.c_str(), sharemap[cell]->name.c_str());
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &it : cell->connections()) {
|
2015-05-31 07:24:34 -05:00
|
|
|
if (cell->output(it.first)) {
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec other_sig = sharemap[cell]->getPort(it.first);
|
2019-04-22 10:25:52 -05:00
|
|
|
log_debug(" Redirecting output %s: %s = %s\n", it.first.c_str(),
|
2013-01-05 04:13:26 -06:00
|
|
|
log_signal(it.second), log_signal(other_sig));
|
2014-07-26 07:32:50 -05:00
|
|
|
module->connect(RTLIL::SigSig(it.second, other_sig));
|
2013-01-05 04:13:26 -06:00
|
|
|
assign_map.add(it.second, other_sig);
|
2019-12-13 12:26:37 -06:00
|
|
|
|
2019-12-20 15:00:04 -06:00
|
|
|
if (it.first == ID(Q) && (cell->type.begins_with("$dff") || cell->type.begins_with("$dlatch") ||
|
|
|
|
cell->type.begins_with("$_DFF") || cell->type.begins_with("$_DLATCH") || cell->type.begins_with("$_SR_") ||
|
|
|
|
cell->type.in("$adff", "$sr", "$ff", "$_FF_"))) {
|
2019-12-13 12:26:37 -06:00
|
|
|
for (auto c : it.second.chunks()) {
|
|
|
|
auto jt = c.wire->attributes.find(ID(init));
|
|
|
|
if (jt == c.wire->attributes.end())
|
|
|
|
continue;
|
|
|
|
for (int i = c.offset; i < c.offset + c.width; i++)
|
|
|
|
jt->second[i] = State::Sx;
|
|
|
|
}
|
|
|
|
dff_init_map.add(it.second, Const(State::Sx, GetSize(it.second)));
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 10:25:52 -05:00
|
|
|
log_debug(" Removing %s cell `%s' from module `%s'.\n", cell->type.c_str(), cell->name.c_str(), module->name.c_str());
|
2015-08-10 15:01:44 -05:00
|
|
|
#ifdef USE_CELL_HASH_CACHE
|
2014-12-28 14:26:36 -06:00
|
|
|
cell_hash_cache.erase(cell);
|
2015-08-10 15:01:44 -05:00
|
|
|
#endif
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
total_count++;
|
|
|
|
} else {
|
|
|
|
sharemap[cell] = cell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 10:25:52 -05:00
|
|
|
|
|
|
|
log_suppressed();
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-31 01:52:49 -05:00
|
|
|
struct OptMergePass : public Pass {
|
|
|
|
OptMergePass() : Pass("opt_merge", "consolidate identical cells") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_OVERRIDE
|
2013-03-01 01:58:55 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2016-03-31 01:52:49 -05:00
|
|
|
log(" opt_merge [options] [selection]\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
log("\n");
|
|
|
|
log("This pass identifies cells with identical type and input signals. Such cells\n");
|
|
|
|
log("are then merged to one cell.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -nomux\n");
|
|
|
|
log(" Do not merge MUX cells.\n");
|
|
|
|
log("\n");
|
2015-05-31 07:24:34 -05:00
|
|
|
log(" -share_all\n");
|
|
|
|
log(" Operate on all cell types, not just built-in types.\n");
|
|
|
|
log("\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing OPT_MERGE pass (detect identical cells).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
bool mode_nomux = false;
|
2015-05-31 07:24:34 -05:00
|
|
|
bool mode_share_all = false;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
std::string arg = args[argidx];
|
|
|
|
if (arg == "-nomux") {
|
|
|
|
mode_nomux = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-31 07:24:34 -05:00
|
|
|
if (arg == "-share_all") {
|
|
|
|
mode_share_all = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
int total_count = 0;
|
2015-02-03 16:45:01 -06:00
|
|
|
for (auto module : design->selected_modules()) {
|
2016-03-31 01:52:49 -05:00
|
|
|
OptMergeWorker worker(design, module, mode_nomux, mode_share_all);
|
2013-01-05 04:13:26 -06:00
|
|
|
total_count += worker.total_count;
|
|
|
|
}
|
|
|
|
|
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 a total of %d cells.\n", total_count);
|
|
|
|
}
|
2016-03-31 01:52:49 -05:00
|
|
|
} OptMergePass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|