aboutsummaryrefslogtreecommitdiffstats
path: root/passes/cmds/clean_zerowidth.cc
blob: bac6b1521328c1bfc258c825da06f14a2fa1f0f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2021  Marcelina Kościelnicka <mwk@0x04.net>
 *
 *  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/yosys.h"
#include "kernel/celltypes.h"
#include "kernel/mem.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct CleanZeroWidthPass : public Pass {
	CleanZeroWidthPass() : Pass("clean_zerowidth", "clean zero-width connections from the design") { }
	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    clean_zerowidth [selection]\n");
		log("\n");
		log("Fixes the selected cells and processes to contain no zero-width connections.\n");
		log("Depending on the cell type, this may be implemented by removing the connection,\n");
		log("widening it to 1-bit, or removing the cell altogether.\n");
		log("\n");
	}

	void clean_case(RTLIL::CaseRule *cs)
	{
		std::vector<SigSig> new_actions;
		for (auto &action : cs->actions)
			if (GetSize(action.first) != 0)
				new_actions.push_back(action);
		std::swap(new_actions, cs->actions);
		for (auto sw : cs->switches)
			for (auto scs : sw->cases)
				clean_case(scs);
	}

	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			break;
		}
		extra_args(args, argidx, design);

		CellTypes ct;
		ct.setup();

		for (auto module : design->selected_modules())
		{
			for (auto cell : module->selected_cells())
			{
				if (!ct.cell_known(cell->type)) {
					// User-defined cell: just prune zero-width connections.
					for (auto it: cell->connections()) {
						if (GetSize(it.second) == 0) {
							cell->unsetPort(it.first);
						}
					}
				} else if (RTLIL::builtin_ff_cell_types().count(cell->type)) {
					// Coarse FF cells: remove if WIDTH == 0 (no outputs).
					// This will also trigger on fine cells, so use the Q port
					// width instead of actual WIDTH parameter.
					if (GetSize(cell->getPort(ID::Q)) == 0) {
						module->remove(cell);
					}
				} else if (cell->type.in(ID($pmux), ID($bmux), ID($demux))) {
					// Remove altogether if WIDTH is 0, replace with
					// a connection if S_WIDTH is 0.
					if (cell->getParam(ID::WIDTH).as_int() == 0) {
						module->remove(cell);
					}
					if (cell->getParam(ID::S_WIDTH).as_int() == 0) {
						module->connect(cell->getPort(ID::Y), cell->getPort(ID::A));
						module->remove(cell);
					}
				} else if (cell->type == ID($concat)) {
					// If a concat has a zero-width input: replace with direct
					// connection to the other input.
					if (cell->getParam(ID::A_WIDTH).as_int() == 0) {
						module->connect(cell->getPort(ID::Y), cell->getPort(ID::B));
						module->remove(cell);
					} else if (cell->getParam(ID::B_WIDTH).as_int() == 0) {
						module->connect(cell->getPort(ID::Y), cell->getPort(ID::A));
						module->remove(cell);
					}
				} else if (cell->type == ID($fsm)) {
					// TODO: not supported
				} else if (cell->is_mem_cell()) {
					// Skip — will be handled below.
				} else if (cell->type == ID($lut)) {
					// Zero-width LUT is just a const driver.
					if (cell->getParam(ID::WIDTH).as_int() == 0) {
						module->connect(cell->getPort(ID::Y), cell->getParam(ID::LUT)[0]);
						module->remove(cell);
					}
				} else if (cell->type == ID($sop)) {
					// Zero-width SOP is just a const driver.
					if (cell->getParam(ID::WIDTH).as_int() == 0) {
						// The value is 1 iff DEPTH is non-0.
						bool val = cell->getParam(ID::DEPTH).as_int() != 0;
						module->connect(cell->getPort(ID::Y), val);
						module->remove(cell);
					}
				} else if (cell->hasParam(ID::WIDTH)) {
					// For cells with WIDTH parameter: remove if zero.
					if (cell->getParam(ID::WIDTH).as_int() == 0) {
						module->remove(cell);
					}
				} else if (cell->hasParam(ID::Y_WIDTH)) {
					// For most operators: remove if Y width is 0, expand
					// A and B to 1-bit if their width is 0.
					if (cell->getParam(ID::Y_WIDTH).as_int() == 0) {
						module->remove(cell);
					} else if (cell->type == ID($macc)) {
						// TODO: fixing zero-width A and B not supported.
					} else {
						if (cell->getParam(ID::A_WIDTH).as_int() == 0) {
							cell->setPort(ID::A, State::S0);
							cell->setParam(ID::A_WIDTH, 1);
						}
						if (cell->hasParam(ID::B_WIDTH) && cell->getParam(ID::B_WIDTH).as_int() == 0) {
							cell->setPort(ID::B, State::S0);
							cell->setParam(ID::B_WIDTH, 1);
						}
					}
				}
			}

			// NOTE: Zero-width switch signals are left alone for processes, as there
			// is no simple way of cleaning them up.
			for (auto &it: module->processes) {
				if (!design->selected(module, it.second))
					continue;
				clean_case(&it.second->root_case);
				for (auto sync : it.second->syncs) {
					std::vector<int> swizzle;
					std::vector<RTLIL::MemWriteAction> new_memwr_actions;
					for (int i = 0; i < GetSize(sync->mem_write_actions); i++) {
						auto &memwr = sync->mem_write_actions[i];
						if (GetSize(memwr.data) == 0)
							continue;
						if (GetSize(memwr.address) == 0)
							memwr.address = State::S0;
						Const priority_mask;
						for (auto x : swizzle) {
							priority_mask.bits.push_back(memwr.priority_mask.bits[x]);
						}
						memwr.priority_mask = priority_mask;
						swizzle.push_back(i);
						new_memwr_actions.push_back(memwr);
					}
					std::swap(new_memwr_actions, sync->mem_write_actions);
					std::vector<SigSig> new_actions;
					for (auto &action : sync->actions)
						if (GetSize(action.first) != 0)
							new_actions.push_back(action);
					std::swap(new_actions, sync->actions);
				}
			}

			for (auto &mem : Mem::get_selected_memories(module)) {
				if (mem.width == 0) {
					mem.remove();
					continue;
				}
				for (auto &init : mem.inits) {
					if (GetSize(init.addr) == 0) {
						init.addr = State::S0;
					}
				}
				for (auto &port : mem.rd_ports) {
					if (GetSize(port.addr) == 0) {
						port.addr = State::S0;
					}
				}
				for (auto &port : mem.wr_ports) {
					if (GetSize(port.addr) == 0) {
						port.addr = State::S0;
					}
				}
				mem.emit();
			}

			std::vector<SigSig> new_conns;
			for (auto &conn : module->connections())
				if (GetSize(conn.first) != 0)
					new_conns.push_back(conn);
			module->new_connections(new_conns);
		}
	}
} CleanZeroWidthPass;

PRIVATE_NAMESPACE_END