File size: 5,757 Bytes
649fefb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Claire Xenia Wolf <claire@yosyshq.com>
 *
 *  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/bitpattern.h"

YOSYS_NAMESPACE_BEGIN

BitPatternPool::BitPatternPool(RTLIL::SigSpec sig) : width(GetSize(sig))
{
	gave_up = width > MAX_WIDTH;
	if (gave_up)
		return;
	universe.assign(width, RTLIL::State::Sa);
	for (int i = 0; i < width; i++)
		if (sig[i].wire == nullptr && sig[i].data <= RTLIL::State::S1)
			universe[i] = sig[i].data;
	rebuild();
}

BitPatternPool::BitPatternPool(int width) : BitPatternPool(RTLIL::SigSpec(RTLIL::State::Sa, width)) {}

bool BitPatternPool::covers_nothing(RTLIL::SigSpec sig)
{
	for (auto bit : sig)
		if (bit.wire == nullptr && (bit.data == RTLIL::State::Sx || bit.data == RTLIL::State::Sz))
			return true;
	return false;
}

int BitPatternPool::budget() { return max_nodes >> (3 * ((MAX_ATTEMPTS - 1 - attempt) / 2)); }

int BitPatternPool::bit_index(int level) { return attempt % 2 ? level : width - 1 - level; }

int BitPatternPool::make_node(int level, int low, int high)
{
	if (low == high)
		return low;
	return nodes(std::make_tuple(level, low, high));
}

bool BitPatternPool::reaches(int node, const std::vector<RTLIL::State> &bits, int terminal, pool<int> &misses)
{
	if (node == int(Terminal::Empty) || node == int(Terminal::Full))
		return node == terminal;
	if (misses.count(node))
		return false;
	auto [level, low, high] = nodes[node];
	RTLIL::State bit = bits[bit_index(level)];
	if (bit != RTLIL::State::S1 && reaches(low, bits, terminal, misses))
		return true;
	if (bit != RTLIL::State::S0 && reaches(high, bits, terminal, misses))
		return true;
	misses.insert(node);
	return false;
}

// node minus cube
int BitPatternPool::remove_cube(int node, int level, const std::vector<RTLIL::State> &bits, dict<std::pair<int, int>, int> &cache)
{
	if (node == int(Terminal::Empty))
		return int(Terminal::Empty);
	while (level < width && bits[bit_index(level)] > RTLIL::State::S1)
		level++;
	if (level == width || GetSize(nodes) >= budget())
		return int(Terminal::Empty);
	auto it = cache.find({node, level});
	if (it != cache.end())
		return it->second;

	auto [node_level, low, high] = nodes[node];
	int result;
	if (node_level < level) {
		low = remove_cube(low, level, bits, cache);
		high = remove_cube(high, level, bits, cache);
		result = make_node(node_level, low, high);
	} else {
		if (node_level > level)
			low = high = node;
		if (bits[bit_index(level)] == RTLIL::State::S0)
			low = remove_cube(low, level + 1, bits, cache);
		else
			high = remove_cube(high, level + 1, bits, cache);
		result = make_node(level, low, high);
	}
	cache[{node, level}] = result;
	return result;
}

// false if over budget
bool BitPatternPool::remove(const std::vector<RTLIL::State> &bits)
{
	log_assert(GetSize(bits) == width);
	dict<std::pair<int, int>, int> cache;
	int new_root = remove_cube(root, 0, bits, cache);
	if (GetSize(nodes) >= budget())
		return false;
	root = new_root;
	return true;
}

bool BitPatternPool::rebuild()
{
	nodes.clear();
	nodes(std::make_tuple(width, int(Terminal::Empty), int(Terminal::Empty)));
	nodes(std::make_tuple(width, int(Terminal::Full), int(Terminal::Full)));
	root = width > 0 ? int(Terminal::Full) : int(Terminal::Empty);
	for (int level = width - 1; level >= 0; level--) {
		RTLIL::State bit = universe[bit_index(level)];
		if (bit == RTLIL::State::S1)
			root = make_node(level, int(Terminal::Empty), root);
		else if (bit == RTLIL::State::S0)
			root = make_node(level, root, int(Terminal::Empty));
	}
	for (auto &bits : taken)
		if (!remove(bits))
			return false;
	return true;
}

bool BitPatternPool::has_any(RTLIL::SigSpec sig)
{
	if (covers_nothing(sig))
		return false;
	if (gave_up)
		return true;
	log_assert(GetSize(sig) == width);
	pool<int> misses;
	return reaches(root, sig.as_const().to_bits(), int(Terminal::Full), misses);
}

bool BitPatternPool::has_all(RTLIL::SigSpec sig)
{
	if (covers_nothing(sig))
		return true;
	if (gave_up)
		return false;
	log_assert(GetSize(sig) == width);
	pool<int> misses;
	return !reaches(root, sig.as_const().to_bits(), int(Terminal::Empty), misses);
}

bool BitPatternPool::take(RTLIL::SigSpec sig)
{
	if (covers_nothing(sig))
		return false;
	if (gave_up)
		return true;
	if (root == int(Terminal::Empty))
		return false;
	std::vector<RTLIL::State> bits = sig.as_const().to_bits();
	int old_root = root;
	bool removed = remove(bits);
	while (!removed && attempt + 1 < MAX_ATTEMPTS) {
		attempt++;
		if (rebuild()) {
			old_root = root;
			removed = remove(bits);
		}
	}
	if (!removed) {
		gave_up = true;
		nodes = {};
		taken = {};
		return true;
	}
	// hash-consed, same root means no overlap
	if (root == old_root)
		return false;
	if (attempt + 1 < MAX_ATTEMPTS)
		taken.push_back(bits);
	return true;
}

bool BitPatternPool::take_all()
{
	if (empty())
		return false;
	root = int(Terminal::Empty);
	gave_up = false;
	return true;
}

bool BitPatternPool::empty() { return !gave_up && root == int(Terminal::Empty); }

YOSYS_NAMESPACE_END