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 "opt_status.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/log.h"
24 #include "kernel/celltypes.h"
33 static int count_rm_cells
, count_rm_wires
;
35 static void rmunused_module_cells(RTLIL::Module
*module
, bool verbose
)
37 SigMap
assign_map(module
);
38 std::set
<RTLIL::Cell
*, RTLIL::sort_by_name
<RTLIL::Cell
>> queue
, unused
;
40 SigSet
<RTLIL::Cell
*> wire2driver
;
41 for (auto &it
: module
->cells
) {
42 RTLIL::Cell
*cell
= it
.second
;
43 for (auto &it2
: cell
->connections
) {
44 if (!ct
.cell_input(cell
->type
, it2
.first
)) {
45 RTLIL::SigSpec sig
= it2
.second
;
46 assign_map
.apply(sig
);
47 wire2driver
.insert(sig
, cell
);
50 if (cell
->type
== "$memwr")
55 for (auto &it
: module
->wires
) {
56 RTLIL::Wire
*wire
= it
.second
;
57 if (wire
->port_output
) {
58 std::set
<RTLIL::Cell
*> cell_list
;
59 RTLIL::SigSpec sig
= RTLIL::SigSpec(wire
);
60 assign_map
.apply(sig
);
61 wire2driver
.find(sig
, cell_list
);
62 for (auto cell
: cell_list
)
67 while (queue
.size() > 0)
69 std::set
<RTLIL::Cell
*, RTLIL::sort_by_name
<RTLIL::Cell
>> new_queue
;
70 for (auto cell
: queue
)
72 for (auto cell
: queue
) {
73 for (auto &it
: cell
->connections
) {
74 if (!ct
.cell_output(cell
->type
, it
.first
)) {
75 std::set
<RTLIL::Cell
*> cell_list
;
76 RTLIL::SigSpec sig
= it
.second
;
77 assign_map
.apply(sig
);
78 wire2driver
.find(sig
, cell_list
);
79 for (auto cell
: cell_list
) {
80 if (unused
.count(cell
) > 0)
81 new_queue
.insert(cell
);
86 queue
.swap(new_queue
);
89 for (auto cell
: unused
) {
91 log(" removing unused `%s' cell `%s'.\n", cell
->type
.c_str(), cell
->name
.c_str());
92 OPT_DID_SOMETHING
= true;
93 module
->cells
.erase(cell
->name
);
99 static bool compare_signals(RTLIL::SigSpec
&s1
, RTLIL::SigSpec
&s2
)
101 assert(s1
.width
== 1);
102 assert(s2
.width
== 1);
103 assert(s1
.chunks
.size() == 1);
104 assert(s2
.chunks
.size() == 1);
106 RTLIL::Wire
*w1
= s1
.chunks
[0].wire
;
107 RTLIL::Wire
*w2
= s2
.chunks
[0].wire
;
109 if (w1
== NULL
|| w2
== NULL
)
112 if (w1
->port_input
!= w2
->port_input
)
113 return w2
->port_input
;
115 if (w1
->name
[0] != w2
->name
[0])
116 return w2
->name
[0] == '\\';
118 if (w1
->attributes
.size() != w2
->attributes
.size())
119 return w2
->attributes
.size() > w1
->attributes
.size();
121 return w2
->name
< w1
->name
;
124 static bool check_public_name(RTLIL::IdString id
)
128 if (id
.substr(0, 2) == "\\_" && (id
[id
.size()-1] == '_' || id
.find("_[") != std::string::npos
))
130 if (id
.find(".$") != std::string::npos
)
135 static void rmunused_module_signals(RTLIL::Module
*module
, bool purge_mode
, bool verbose
)
137 SigMap
assign_map(module
);
138 for (auto &it
: module
->wires
) {
139 RTLIL::Wire
*wire
= it
.second
;
140 for (int i
= 0; i
< wire
->width
; i
++) {
141 RTLIL::SigSpec s1
= RTLIL::SigSpec(wire
, 1, i
), s2
= assign_map(s1
);
142 if (!compare_signals(s1
, s2
))
147 module
->connections
.clear();
149 SigPool used_signals
;
150 SigPool used_signals_nodrivers
;
151 for (auto &it
: module
->cells
) {
152 RTLIL::Cell
*cell
= it
.second
;
153 for (auto &it2
: cell
->connections
) {
154 assign_map
.apply(it2
.second
);
155 used_signals
.add(it2
.second
);
156 if (!ct
.cell_output(cell
->type
, it2
.first
))
157 used_signals_nodrivers
.add(it2
.second
);
160 for (auto &it
: module
->wires
) {
161 RTLIL::Wire
*wire
= it
.second
;
162 if (wire
->port_id
> 0) {
163 RTLIL::SigSpec sig
= RTLIL::SigSpec(wire
);
164 assign_map
.apply(sig
);
165 used_signals
.add(sig
);
166 if (!wire
->port_input
)
167 used_signals_nodrivers
.add(sig
);
171 std::vector
<RTLIL::Wire
*> del_wires
;
172 for (auto &it
: module
->wires
) {
173 RTLIL::Wire
*wire
= it
.second
;
174 if ((!purge_mode
&& check_public_name(wire
->name
)) || wire
->port_id
!= 0) {
175 RTLIL::SigSpec s1
= RTLIL::SigSpec(wire
), s2
= s1
;
176 assign_map
.apply(s2
);
177 if (!used_signals
.check_any(s2
) && wire
->port_id
== 0) {
178 del_wires
.push_back(wire
);
182 assert(s1
.chunks
.size() == s2
.chunks
.size());
183 RTLIL::SigSig new_conn
;
184 for (size_t i
= 0; i
< s1
.chunks
.size(); i
++)
185 if (s1
.chunks
[i
] != s2
.chunks
[i
]) {
186 new_conn
.first
.append(s1
.chunks
[i
]);
187 new_conn
.second
.append(s2
.chunks
[i
]);
189 if (new_conn
.first
.width
> 0) {
190 new_conn
.first
.optimize();
191 new_conn
.second
.optimize();
192 used_signals
.add(new_conn
.first
);
193 used_signals
.add(new_conn
.second
);
194 module
->connections
.push_back(new_conn
);
198 if (!used_signals
.check_any(RTLIL::SigSpec(wire
)))
199 del_wires
.push_back(wire
);
201 RTLIL::SigSpec sig
= assign_map(RTLIL::SigSpec(wire
));
202 if (!used_signals_nodrivers
.check_any(sig
)) {
203 std::string unused_bits
;
205 for (size_t i
= 0; i
< sig
.chunks
.size(); i
++) {
206 if (sig
.chunks
[i
].wire
== NULL
)
208 if (!used_signals_nodrivers
.check_any(sig
)) {
209 if (!unused_bits
.empty())
211 unused_bits
+= stringf("%zd", i
);
214 if (unused_bits
.empty() || wire
->port_id
!= 0)
215 wire
->attributes
.erase("\\unused_bits");
217 wire
->attributes
["\\unused_bits"] = RTLIL::Const(unused_bits
);
219 wire
->attributes
.erase("\\unused_bits");
223 int del_wires_count
= 0;
224 for (auto wire
: del_wires
)
225 if (!used_signals
.check_any(RTLIL::SigSpec(wire
))) {
226 if (check_public_name(wire
->name
) && verbose
) {
227 log(" removing unused non-port wire %s.\n", wire
->name
.c_str());
230 module
->wires
.erase(wire
->name
);
235 if (del_wires_count
> 0)
236 log(" removed %d unused temporary wires.\n", del_wires_count
);
239 static void rmunused_module(RTLIL::Module
*module
, bool purge_mode
, bool verbose
)
242 log("Finding unused cells or wires in module %s..\n", module
->name
.c_str());
244 rmunused_module_cells(module
, verbose
);
245 rmunused_module_signals(module
, purge_mode
, verbose
);
248 struct OptCleanPass
: public Pass
{
249 OptCleanPass() : Pass("opt_clean", "remove unused cells and wires") { }
252 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
254 log(" opt_clean [options] [selection]\n");
256 log("This pass identifies wires and cells that are unused and removes them. Other\n");
257 log("passes often remove cells but leave the wires in the design or reconnect the\n");
258 log("wires but leave the old cells in the design. This pass can be used to clean up\n");
259 log("after the passes that do the actual work.\n");
261 log("This pass only operates on completely selected modules without processes.\n");
264 log(" also remove internal nets if they have a public name\n");
267 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
269 bool purge_mode
= false;
271 log_header("Executing OPT_CLEAN pass (remove unused cells and wires).\n");
275 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
276 if (args
[argidx
] == "-purge") {
281 extra_args(args
, argidx
, design
);
283 ct
.setup_internals();
284 ct
.setup_internals_mem();
286 ct
.setup_stdcells_mem();
288 for (auto &mod_it
: design
->modules
) {
289 if (!design
->selected_whole_module(mod_it
.first
)) {
290 if (design
->selected(mod_it
.second
))
291 log("Skipping module %s as it is only partially selected.\n", id2cstr(mod_it
.second
->name
));
294 if (mod_it
.second
->processes
.size() > 0) {
295 log("Skipping module %s as it contains processes.\n", mod_it
.second
->name
.c_str());
297 rmunused_module(mod_it
.second
, purge_mode
, true);
306 struct CleanPass
: public Pass
{
307 CleanPass() : Pass("clean", "remove unused cells and wires") { }
310 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
312 log(" clean [selection]\n");
314 log("This is identical to opt_clean, but less verbose.\n");
317 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
319 extra_args(args
, 1, design
);
321 ct
.setup_internals();
322 ct
.setup_internals_mem();
324 ct
.setup_stdcells_mem();
329 for (auto &mod_it
: design
->modules
) {
330 if (design
->selected_whole_module(mod_it
.first
) && mod_it
.second
->processes
.size() == 0)
332 OPT_DID_SOMETHING
= false;
333 rmunused_module(mod_it
.second
, false, false);
334 } while (OPT_DID_SOMETHING
);
337 if (count_rm_cells
> 0 || count_rm_wires
> 0)
338 log("Removed %d unused cells and %d unused wires.\n", count_rm_cells
, count_rm_wires
);