2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/register.h"
21 #include "kernel/sigtools.h"
22 #include "kernel/log.h"
23 #include "kernel/celltypes.h"
29 PRIVATE_NAMESPACE_BEGIN
36 dict
<Module
*, bool> cache
;
38 void reset(Design
*design
= nullptr)
40 this->design
= design
;
44 bool query(Module
*module
)
46 log_assert(design
!= nullptr);
48 if (module
== nullptr)
51 if (cache
.count(module
))
52 return cache
.at(module
);
55 if (!module
->get_bool_attribute("\\keep")) {
56 bool found_keep
= false;
57 for (auto cell
: module
->cells())
58 if (query(cell
)) found_keep
= true;
59 cache
[module
] = found_keep
;
65 bool query(Cell
*cell
)
67 if (cell
->type
.in("$memwr", "$meminit", "$assert", "$assume", "$live", "$fair", "$cover", "$specify2", "$specify3", "$specrule"))
70 if (cell
->has_keep_attr())
73 if (cell
->module
&& cell
->module
->design
)
74 return query(cell
->module
->design
->module(cell
->type
));
80 keep_cache_t keep_cache
;
81 CellTypes ct_reg
, ct_all
;
82 int count_rm_cells
, count_rm_wires
;
84 void rmunused_module_cells(Module
*module
, bool verbose
)
86 SigMap
sigmap(module
);
87 pool
<Cell
*> queue
, unused
;
88 dict
<SigBit
, pool
<Cell
*>> wire2driver
;
90 for (auto &it
: module
->cells_
) {
91 Cell
*cell
= it
.second
;
92 for (auto &it2
: cell
->connections()) {
93 if (!ct_all
.cell_known(cell
->type
) || ct_all
.cell_output(cell
->type
, it2
.first
))
94 for (auto raw_bit
: it2
.second
) {
95 if (raw_bit
.wire
== nullptr)
97 auto bit
= sigmap(raw_bit
);
98 if (bit
.wire
== nullptr)
99 log_warning("Driver-driver conflict for %s between cell %s.%s and constant %s in %s: Resolved using constant.\n",
100 log_signal(raw_bit
), log_id(cell
), log_id(it2
.first
), log_signal(bit
), log_id(module
));
101 if (bit
.wire
!= nullptr)
102 wire2driver
[bit
].insert(cell
);
105 if (keep_cache
.query(cell
))
111 for (auto &it
: module
->wires_
) {
112 Wire
*wire
= it
.second
;
113 if (wire
->port_output
|| wire
->get_bool_attribute("\\keep")) {
114 for (auto bit
: sigmap(wire
))
115 for (auto c
: wire2driver
[bit
])
116 queue
.insert(c
), unused
.erase(c
);
120 while (!queue
.empty())
123 for (auto cell
: queue
)
124 for (auto &it
: cell
->connections())
125 if (!ct_all
.cell_known(cell
->type
) || ct_all
.cell_input(cell
->type
, it
.first
))
126 for (auto bit
: sigmap(it
.second
))
130 for (auto bit
: bits
)
131 for (auto c
: wire2driver
[bit
])
133 queue
.insert(c
), unused
.erase(c
);
136 unused
.sort(RTLIL::sort_by_name_id
<RTLIL::Cell
>());
138 for (auto cell
: unused
) {
140 log_debug(" removing unused `%s' cell `%s'.\n", cell
->type
.c_str(), cell
->name
.c_str());
141 module
->design
->scratchpad_set_bool("opt.did_something", true);
142 module
->remove(cell
);
147 int count_nontrivial_wire_attrs(RTLIL::Wire
*w
)
149 int count
= w
->attributes
.size();
150 count
-= w
->attributes
.count("\\src");
151 count
-= w
->attributes
.count("\\unused_bits");
155 bool compare_signals(RTLIL::SigBit
&s1
, RTLIL::SigBit
&s2
, SigPool
®s
, SigPool
&conns
, pool
<RTLIL::Wire
*> &direct_wires
)
157 RTLIL::Wire
*w1
= s1
.wire
;
158 RTLIL::Wire
*w2
= s2
.wire
;
160 if (w1
== NULL
|| w2
== NULL
)
163 if (w1
->port_input
!= w2
->port_input
)
164 return w2
->port_input
;
166 if ((w1
->port_input
&& w1
->port_output
) != (w2
->port_input
&& w2
->port_output
))
167 return !(w2
->port_input
&& w2
->port_output
);
169 if (w1
->name
[0] == '\\' && w2
->name
[0] == '\\') {
170 if (regs
.check_any(s1
) != regs
.check_any(s2
))
171 return regs
.check_any(s2
);
172 if (direct_wires
.count(w1
) != direct_wires
.count(w2
))
173 return direct_wires
.count(w2
) != 0;
174 if (conns
.check_any(s1
) != conns
.check_any(s2
))
175 return conns
.check_any(s2
);
178 if (w1
->port_output
!= w2
->port_output
)
179 return w2
->port_output
;
181 if (w1
->name
[0] != w2
->name
[0])
182 return w2
->name
[0] == '\\';
184 int attrs1
= count_nontrivial_wire_attrs(w1
);
185 int attrs2
= count_nontrivial_wire_attrs(w2
);
187 if (attrs1
!= attrs2
)
188 return attrs2
> attrs1
;
190 return strcmp(w2
->name
.c_str(), w1
->name
.c_str()) < 0;
193 bool check_public_name(RTLIL::IdString id
)
195 const std::string
&id_str
= id
.str();
196 if (id_str
[0] == '$')
198 if (id_str
.substr(0, 2) == "\\_" && (id_str
[id_str
.size()-1] == '_' || id_str
.find("_[") != std::string::npos
))
200 if (id_str
.find(".$") != std::string::npos
)
205 void rmunused_module_signals(RTLIL::Module
*module
, bool purge_mode
, bool verbose
)
207 SigPool register_signals
;
208 SigPool connected_signals
;
211 for (auto &it
: module
->cells_
) {
212 RTLIL::Cell
*cell
= it
.second
;
213 if (ct_reg
.cell_known(cell
->type
))
214 for (auto &it2
: cell
->connections())
215 if (ct_reg
.cell_output(cell
->type
, it2
.first
))
216 register_signals
.add(it2
.second
);
217 for (auto &it2
: cell
->connections())
218 connected_signals
.add(it2
.second
);
221 SigMap
assign_map(module
);
222 pool
<RTLIL::SigSpec
> direct_sigs
;
223 pool
<RTLIL::Wire
*> direct_wires
;
224 for (auto &it
: module
->cells_
) {
225 RTLIL::Cell
*cell
= it
.second
;
226 if (ct_all
.cell_known(cell
->type
))
227 for (auto &it2
: cell
->connections())
228 if (ct_all
.cell_output(cell
->type
, it2
.first
))
229 direct_sigs
.insert(assign_map(it2
.second
));
231 for (auto &it
: module
->wires_
) {
232 if (direct_sigs
.count(assign_map(it
.second
)) || it
.second
->port_input
)
233 direct_wires
.insert(it
.second
);
236 for (auto &it
: module
->wires_
) {
237 RTLIL::Wire
*wire
= it
.second
;
238 for (int i
= 0; i
< wire
->width
; i
++) {
239 RTLIL::SigBit s1
= RTLIL::SigBit(wire
, i
), s2
= assign_map(s1
);
240 if (!compare_signals(s1
, s2
, register_signals
, connected_signals
, direct_wires
))
245 module
->connections_
.clear();
247 SigPool used_signals
;
248 SigPool used_signals_nodrivers
;
249 for (auto &it
: module
->cells_
) {
250 RTLIL::Cell
*cell
= it
.second
;
251 for (auto &it2
: cell
->connections_
) {
252 assign_map
.apply(it2
.second
);
253 used_signals
.add(it2
.second
);
254 if (!ct_all
.cell_output(cell
->type
, it2
.first
))
255 used_signals_nodrivers
.add(it2
.second
);
258 for (auto &it
: module
->wires_
) {
259 RTLIL::Wire
*wire
= it
.second
;
260 if (wire
->port_id
> 0) {
261 RTLIL::SigSpec sig
= RTLIL::SigSpec(wire
);
262 assign_map
.apply(sig
);
263 used_signals
.add(sig
);
264 if (!wire
->port_input
)
265 used_signals_nodrivers
.add(sig
);
267 if (wire
->get_bool_attribute("\\keep")) {
268 RTLIL::SigSpec sig
= RTLIL::SigSpec(wire
);
269 assign_map
.apply(sig
);
270 used_signals
.add(sig
);
274 std::vector
<RTLIL::Wire
*> maybe_del_wires
;
275 for (auto wire
: module
->wires())
277 SigSpec s1
= SigSpec(wire
), s2
= assign_map(s1
);
278 log_assert(GetSize(s1
) == GetSize(s2
));
280 bool maybe_del
= false;
281 if ((!purge_mode
&& check_public_name(wire
->name
)) || wire
->port_id
!= 0 || wire
->get_bool_attribute("\\keep") || wire
->attributes
.count("\\init")) {
282 if (!used_signals
.check_any(s2
) && wire
->port_id
== 0 && !wire
->get_bool_attribute("\\keep"))
285 if (!used_signals
.check_any(s2
))
290 maybe_del_wires
.push_back(wire
);
293 if (wire
->attributes
.count("\\init"))
294 initval
= wire
->attributes
.at("\\init");
295 if (GetSize(initval
) != GetSize(wire
))
296 initval
.bits
.resize(GetSize(wire
), State::Sx
);
297 RTLIL::SigSig new_conn
;
298 for (int i
= 0; i
< GetSize(s1
); i
++)
299 if (s1
[i
] != s2
[i
]) {
300 if (s2
[i
] == State::Sx
&& (initval
[i
] == State::S0
|| initval
[i
] == State::S1
)) {
302 initval
[i
] = State::Sx
;
304 new_conn
.first
.append_bit(s1
[i
]);
305 new_conn
.second
.append_bit(s2
[i
]);
307 if (new_conn
.first
.size() > 0) {
308 if (initval
.is_fully_undef())
309 wire
->attributes
.erase("\\init");
311 wire
->attributes
.at("\\init") = initval
;
312 used_signals
.add(new_conn
.first
);
313 used_signals
.add(new_conn
.second
);
314 module
->connect(new_conn
);
318 if (!used_signals_nodrivers
.check_all(s2
)) {
319 std::string unused_bits
;
320 for (int i
= 0; i
< GetSize(s2
); i
++) {
321 if (s2
[i
].wire
== NULL
)
323 if (!used_signals_nodrivers
.check(s2
[i
])) {
324 if (!unused_bits
.empty())
326 unused_bits
+= stringf("%d", i
);
329 if (unused_bits
.empty() || wire
->port_id
!= 0)
330 wire
->attributes
.erase("\\unused_bits");
332 wire
->attributes
["\\unused_bits"] = RTLIL::Const(unused_bits
);
334 wire
->attributes
.erase("\\unused_bits");
339 pool
<RTLIL::Wire
*> del_wires
;
341 int del_wires_count
= 0;
342 for (auto wire
: maybe_del_wires
) {
343 SigSpec s1
= SigSpec(wire
);
344 if (used_signals
.check_any(s1
)) {
345 SigSpec s2
= assign_map(s1
);
347 if (wire
->attributes
.count("\\init"))
348 initval
= wire
->attributes
.at("\\init");
349 if (GetSize(initval
) != GetSize(wire
))
350 initval
.bits
.resize(GetSize(wire
), State::Sx
);
351 RTLIL::SigSig new_conn
;
352 for (int i
= 0; i
< GetSize(s1
); i
++)
353 if (s1
[i
] != s2
[i
]) {
354 if (s2
[i
] == State::Sx
&& (initval
[i
] == State::S0
|| initval
[i
] == State::S1
)) {
356 initval
[i
] = State::Sx
;
358 new_conn
.first
.append_bit(s1
[i
]);
359 new_conn
.second
.append_bit(s2
[i
]);
361 if (new_conn
.first
.size() > 0) {
362 if (initval
.is_fully_undef())
363 wire
->attributes
.erase("\\init");
365 wire
->attributes
.at("\\init") = initval
;
366 module
->connect(new_conn
);
369 if (ys_debug() || (check_public_name(wire
->name
) && verbose
)) {
370 log_debug(" removing unused non-port wire %s.\n", wire
->name
.c_str());
372 del_wires
.insert(wire
);
377 module
->remove(del_wires
);
378 count_rm_wires
+= del_wires
.size();
380 if (verbose
&& del_wires_count
> 0)
381 log_debug(" removed %d unused temporary wires.\n", del_wires_count
);
384 bool rmunused_module_init(RTLIL::Module
*module
, bool purge_mode
, bool verbose
)
386 bool did_something
= false;
388 fftypes
.setup_internals_mem();
390 SigMap
sigmap(module
);
391 dict
<SigBit
, State
> qbits
;
393 for (auto cell
: module
->cells())
394 if (fftypes
.cell_known(cell
->type
) && cell
->hasPort("\\Q"))
396 SigSpec sig
= cell
->getPort("\\Q");
398 for (int i
= 0; i
< GetSize(sig
); i
++)
402 if (bit
.wire
== nullptr || bit
.wire
->attributes
.count("\\init") == 0)
405 Const init
= bit
.wire
->attributes
.at("\\init");
407 if (i
>= GetSize(init
) || init
[i
] == State::Sx
|| init
[i
] == State::Sz
)
411 qbits
[bit
] = init
[i
];
415 for (auto wire
: module
->wires())
417 if (!purge_mode
&& wire
->name
[0] == '\\')
420 if (wire
->attributes
.count("\\init") == 0)
423 Const init
= wire
->attributes
.at("\\init");
425 for (int i
= 0; i
< GetSize(wire
) && i
< GetSize(init
); i
++)
427 if (init
[i
] == State::Sx
|| init
[i
] == State::Sz
)
430 SigBit wire_bit
= SigBit(wire
, i
);
431 SigBit mapped_wire_bit
= sigmap(wire_bit
);
433 if (wire_bit
== mapped_wire_bit
)
436 if (qbits
.count(sigmap(SigBit(wire
, i
))) == 0)
439 if (qbits
.at(sigmap(SigBit(wire
, i
))) != init
[i
])
444 log_debug(" removing redundant init attribute on %s.\n", log_id(wire
));
446 wire
->attributes
.erase("\\init");
447 did_something
= true;
451 return did_something
;
454 void rmunused_module(RTLIL::Module
*module
, bool purge_mode
, bool verbose
, bool rminit
)
457 log("Finding unused cells or wires in module %s..\n", module
->name
.c_str());
459 std::vector
<RTLIL::Cell
*> delcells
;
460 for (auto cell
: module
->cells())
461 if (cell
->type
.in("$pos", "$_BUF_")) {
462 bool is_signed
= cell
->type
== "$pos" && cell
->getParam("\\A_SIGNED").as_bool();
463 RTLIL::SigSpec a
= cell
->getPort("\\A");
464 RTLIL::SigSpec y
= cell
->getPort("\\Y");
465 a
.extend_u0(GetSize(y
), is_signed
);
466 module
->connect(y
, a
);
467 delcells
.push_back(cell
);
469 for (auto cell
: delcells
) {
471 log_debug(" removing buffer cell `%s': %s = %s\n", cell
->name
.c_str(),
472 log_signal(cell
->getPort("\\Y")), log_signal(cell
->getPort("\\A")));
473 module
->remove(cell
);
475 if (!delcells
.empty())
476 module
->design
->scratchpad_set_bool("opt.did_something", true);
478 rmunused_module_cells(module
, verbose
);
479 rmunused_module_signals(module
, purge_mode
, verbose
);
481 if (rminit
&& rmunused_module_init(module
, purge_mode
, verbose
))
482 rmunused_module_signals(module
, purge_mode
, verbose
);
485 struct OptCleanPass
: public Pass
{
486 OptCleanPass() : Pass("opt_clean", "remove unused cells and wires") { }
487 void help() YS_OVERRIDE
489 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
491 log(" opt_clean [options] [selection]\n");
493 log("This pass identifies wires and cells that are unused and removes them. Other\n");
494 log("passes often remove cells but leave the wires in the design or reconnect the\n");
495 log("wires but leave the old cells in the design. This pass can be used to clean up\n");
496 log("after the passes that do the actual work.\n");
498 log("This pass only operates on completely selected modules without processes.\n");
501 log(" also remove internal nets if they have a public name\n");
504 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
506 bool purge_mode
= false;
508 log_header(design
, "Executing OPT_CLEAN pass (remove unused cells and wires).\n");
512 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
513 if (args
[argidx
] == "-purge") {
519 extra_args(args
, argidx
, design
);
521 keep_cache
.reset(design
);
523 ct_reg
.setup_internals_mem();
524 ct_reg
.setup_stdcells_mem();
526 ct_all
.setup(design
);
531 for (auto module
: design
->selected_whole_modules_warn()) {
532 if (module
->has_processes_warn())
534 rmunused_module(module
, purge_mode
, true, true);
537 if (count_rm_cells
> 0 || count_rm_wires
> 0)
538 log("Removed %d unused cells and %d unused wires.\n", count_rm_cells
, count_rm_wires
);
551 struct CleanPass
: public Pass
{
552 CleanPass() : Pass("clean", "remove unused cells and wires") { }
553 void help() YS_OVERRIDE
555 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
557 log(" clean [options] [selection]\n");
559 log("This is identical to 'opt_clean', but less verbose.\n");
561 log("When commands are separated using the ';;' token, this command will be executed\n");
562 log("between the commands.\n");
564 log("When commands are separated using the ';;;' token, this command will be executed\n");
565 log("in -purge mode between the commands.\n");
568 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
570 bool purge_mode
= false;
573 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
574 if (args
[argidx
] == "-purge") {
580 if (argidx
< args
.size())
581 extra_args(args
, argidx
, design
);
583 keep_cache
.reset(design
);
585 ct_reg
.setup_internals_mem();
586 ct_reg
.setup_stdcells_mem();
588 ct_all
.setup(design
);
593 for (auto module
: design
->selected_whole_modules()) {
594 if (module
->has_processes())
596 rmunused_module(module
, purge_mode
, ys_debug(), false);
600 if (count_rm_cells
> 0 || count_rm_wires
> 0)
601 log("Removed %d unused cells and %d unused wires.\n", count_rm_cells
, count_rm_wires
);
613 PRIVATE_NAMESPACE_END