-
Notifications
You must be signed in to change notification settings - Fork 0
/
toymap.cc
1768 lines (1507 loc) · 43.5 KB
/
toymap.cc
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// toymap -- Toy technology mapper of logic networks
//
#define NPRIORITY_CUTS 8
#define CUT_MAXIMUM 6
#include <algorithm>
#include <random>
#include <cstdlib>
#include <vector>
#include <map>
#include <cstdint>
// Include Yosys stuff
#include "kernel/rtlil.h"
#include "kernel/sigtools.h"
#include "kernel/log.h"
#include "kernel/ff.h"
#include "library.h"
template<> struct Yosys::hash_ops<uint64_t> : hash_int_ops
{
static inline unsigned int hash(uint64_t a) {
return mkhash((unsigned int)(a), (unsigned int)(a >> 32));
}
};
typedef uint64_t u64;
namespace RTLIL = Yosys::RTLIL;
using Yosys::dict;
using Yosys::log;
using Yosys::log_error;
using Yosys::log_warning;
using Yosys::log_id;
using Yosys::ys_debug;
using Yosys::State;
struct AndNode;
struct CoverFaninList;
// A node in the acyclic cover of the cyclic graph
struct CoverNode {
int lag;
AndNode *img;
CoverNode shift(int delta)
{
return CoverNode{ lag + delta, img };
}
bool operator==(const AndNode *node) const
{ return node == img && !lag; }
bool operator<(const CoverNode other) const
{ return std::tie(lag, img) < std::tie(other.lag, other.img); }
bool operator==(const CoverNode other) const
{ return std::tie(lag, img) == std::tie(other.lag, other.img); }
CoverFaninList fanins();
};
struct CutList {
CoverNode *array;
int size;
int lag_inject;
CutList(CoverNode *array, int inject=0)
: array(array), lag_inject(inject)
{
CoverNode *p;
for (p = array; (p < array + CUT_MAXIMUM) && (p->img != NULL); p++);
size = p - array;
}
CutList inject_lag(int delta)
{
CutList ret = *this;
ret.lag_inject += delta;
return ret;
}
class iterator: public std::iterator<std::input_iterator_tag, CoverNode> {
CoverNode *p;
int lag_inject;
public:
iterator(CoverNode *p,int inject) : p(p), lag_inject(inject) {}
iterator& operator++() { p++; return *this; }
bool operator==(const iterator &other) const
{ return p == other.p && lag_inject == other.lag_inject; }
int operator-(const iterator &other) const
{ log_assert(other.lag_inject == lag_inject); return p - other.p; }
bool operator!=(const iterator &other) const { return !(*this == other); }
CoverNode operator*() const { return CoverNode{p->lag + lag_inject, p->img}; }
};
iterator begin() const { return iterator(array, lag_inject); };
iterator end() const { return iterator(array + size, lag_inject); };
};
State invert(State state)
{
switch (state) {
case State::S0: return State::S1;
case State::S1: return State::S0;
case State::Sx: return State::Sx;
default: log_assert(false); return State::Sx;
}
}
struct NodeInput {
NodeInput() {}
NodeInput(CoverNode node, bool negated)
{
set_node(node.img);
feat.lag = node.lag;
feat.negated = negated;
}
AndNode *node = NULL;
struct EdgeFeatures {
bool negated = false;
int lag = 0;
std::vector<State> initvals;
bool empty() const
{
return !negated && lag == 0;
}
// Add the features of another edge when the two edges are being
// chained -- this one is closer to the load and the other one is
// closer to the driver
void add(EdgeFeatures other)
{
if (other.negated) {
negated ^= true;
for (int i = 0; i < lag; i++)
initvals[i] = invert(initvals[i]);
}
initvals.insert(initvals.end(), other.initvals.begin(),
other.initvals.end());
lag += other.lag;
}
bool crop_const_lag()
{
log_assert((int) initvals.size() == lag);
int i;
for (i = initvals.size() - 1; i >= 0; i--)
if (initvals[i] == State::Sx ||
initvals[i] == State::S0)
initvals.pop_back();
else
break;
if (i < lag - 1) {
lag = (int) initvals.size();
return true;
} else {
return false;
}
}
bool initvals_undef()
{
for (auto bit : initvals)
if (bit != RTLIL::State::Sx)
return false;
return true;
}
void fixup_initvals()
{
initvals = std::vector<RTLIL::State>(lag, RTLIL::State::Sx);
}
bool operator<(const EdgeFeatures &other) const
{ return std::tie(negated, initvals) < std::tie(other.negated, other.initvals); }
bool operator==(const EdgeFeatures &other) const
{ return std::tie(negated, initvals) == std::tie(other.negated, other.initvals); }
} feat;
void negate() { feat.negated ^= true; }
void delay(State initval) { feat.lag++; feat.initvals.push_back(initval); }
void set_node(AndNode *source) { feat = {}; node = source; }
void set_const(int state)
{
node = NULL;
feat = {};
log_assert(state == 1 || state == 0);
if (state)
negate();
}
bool is_const() { return !node && feat.lag == 0; }
bool eval() { log_assert(is_const()); return feat.negated; }
u64 weval();
CoverNode cover_node() { log_assert(node); return CoverNode{feat.lag, node}; }
std::string describe(int descend);
bool expand();
bool tied_to(const NodeInput &other);
bool eval_conditionally(const NodeInput &other);
bool assume(const NodeInput &other);
std::vector<bool> truth_table(CutList cutlist);
bool operator<(const NodeInput &other) const
{
if (!node != !other.node)
return !node < !other.node;
return std::tie(feat, node) < std::tie(other.feat, other.node);
}
bool operator==(const NodeInput &other) const
{ return std::tie(feat, node) == std::tie(other.feat, other.node); }
unsigned int hash() const
{
log_assert(!feat.lag); // TODO
return (uintptr_t) node + feat.negated;
}
};
struct AndNode {
bool pi = false;
bool po = false;
NodeInput ins[2];
RTLIL::IdString label;
AndNode() {};
// Scratch area for algorithms
bool visited;
RTLIL::SigBit yw = {};
union {
int refs;
struct {
CoverNode cut[CUT_MAXIMUM];
double area_flow;
int edge_flow;
int map_fanouts;
};
bool has_foreign_cell_users;
int timedelta;
struct {
AndNode *replacement;
bool feeds_inverter;
int andtree_counter;
};
};
int fanouts;
int depth_limit;
int fid; // frontier index
int depth;
u64 weval;
void propagate_weval()
{
if (pi)
return;
weval = ins[0].weval() & ins[1].weval();
}
void apply_replacements()
{
for (int i = 0; i < 2; i++)
while (ins[i].node && ins[i].node->replacement) {
log_assert(ins[i].node != ins[i].node->replacement);
ins[i].node = ins[i].node->replacement;
}
}
std::vector<bool> truth_table()
{
return truth_table(CutList{cut, 0});
}
std::vector<bool> truth_table(CutList cutlist, bool negate=false)
{
for (auto it = cutlist.begin(); it != cutlist.end(); ++it)
if (*it == this) {
int index = it - cutlist.begin();
std::vector<bool> ret;
for (int i = 0; i < (1 << cutlist.size); i++)
ret.push_back((((i >> index) & 1) ^ negate) != 0);
return ret;
}
log_assert(!pi && "spilled cut");
std::vector<bool> a = ins[0].truth_table(cutlist);
std::vector<bool> b = ins[1].truth_table(cutlist);
log_assert(a.size() == 1 << cutlist.size && b.size() == 1 << cutlist.size);
std::vector<bool> ret;
for (int i = 0; i < (1 << cutlist.size); i++)
ret.push_back((a[i] && b[i]) ^ negate);
return ret;
}
void combine_label(RTLIL::IdString other_label)
{
if (other_label.empty())
return;
if (label.empty()) {
label = other_label;
return;
}
if (std::make_pair(other_label.isPublic(), -other_label.size())
> std::make_pair(label.isPublic(), -label.size()))
label = other_label;
}
bool expand();
struct FaninList {
AndNode *node;
class iterator: public std::iterator<std::input_iterator_tag, AndNode *> {
AndNode *node; int pos;
public:
iterator(AndNode *node, int pos)
: node(node), pos(pos) {}
iterator& operator++() { pos++; return *this; }
bool operator==(const iterator &other) const
{ return std::tie(node, pos) == std::tie(other.node, other.pos); }
bool operator!=(const iterator &other) const
{ return !(*this == other); }
AndNode *operator*() const
{ return node->ins[pos].node; }
};
iterator begin() const
{ return iterator(node, node->pi ? 0 : (node->ins[0].node == NULL)); };
iterator end() const
{ return node->pi ? begin() : iterator(node, (node->ins[1].node == NULL) ? 1 : 2); };
};
FaninList fanins()
{
return FaninList{ this };
}
};
struct CoverFaninList {
CoverNode node;
class iterator: public std::iterator<std::input_iterator_tag, CoverNode> {
CoverNode node; int pos;
public:
iterator(CoverNode node, int pos)
: node(node), pos(pos) {}
iterator& operator++() { pos++; return *this; }
bool operator==(const iterator &other) const
{ return std::tie(node, pos) == std::tie(other.node, other.pos); }
bool operator!=(const iterator &other) const
{ return !(*this == other); }
CoverNode operator*() const
{ return CoverNode{node.lag + node.img->ins[pos].feat.lag,
node.img->ins[pos].node}; }
};
iterator begin() const
{ return iterator(node, node.img->pi ? 0 : (node.img->ins[0].node == NULL)); };
iterator end() const
{ return node.img->pi ? begin() : iterator(node, (node.img->ins[1].node == NULL) ? 1 : 2); };
};
CoverFaninList CoverNode::fanins()
{
return CoverFaninList{ *this };
}
#if 0
std::string NodeInput::describe(int descend)
{
std::string ret;
if (feat.negated) ret += "~";
if (feat.lag) {
ret += "[";
for (auto it = feat.initvals.rbegin(); it != feat.initvals.rend(); it++)
switch (*it) {
case State::S1:
ret += "1";
break;
case State::S0:
ret += "0";
break;
default:
ret += "x";
break;
}
ret += "]";
}
if (!node) {
ret += "0";
} else if (descend && !node->pi) {
std::string in0 = node->ins[0].describe(descend - 1);
std::string in1 = node->ins[1].describe(descend - 1);
ret += Yosys::stringf("(%s && %s)", in0.c_str(), in1.c_str());
} else {
ret += Yosys::stringf("%s", node->label.c_str());
}
return ret;
}
#endif
std::vector<bool> NodeInput::truth_table(CutList cutlist)
{
log_assert(feat.initvals_undef());
if (!node) {
return std::vector<bool>(1 << cutlist.size, feat.negated);
} else {
return node->truth_table(cutlist.inject_lag(-feat.lag), feat.negated);
}
}
u64 NodeInput::weval()
{
if (!node) {
log_assert(is_const());
return eval() ? ~(u64) 0 : 0;
} else {
log_assert(!feat.lag);
return feat.negated ? ~node->weval : node->weval;
}
}
bool NodeInput::expand()
{
if (is_const())
return false;
if (!node)
return feat.crop_const_lag();
if (node->pi)
return false;
NodeInput *in0, *in1;
in0 = &node->ins[0];
in1 = &node->ins[1];
if (!in0->is_const())
std::swap(in0, in1);
if (!in0->is_const())
return false;
if (in0->eval()) {
if (in1->node == node)
return false;
feat.add(in1->feat);
node = in1->node;
} else {
set_const(feat.negated);
}
return true;
}
bool NodeInput::tied_to(const NodeInput &other)
{
return node && other.node &&
node == other.node && feat.lag == other.feat.lag;
}
bool NodeInput::eval_conditionally(const NodeInput &other)
{
log_assert(tied_to(other));
return !(feat.negated ^ other.feat.negated);
}
bool NodeInput::assume(const NodeInput &other)
{
if (is_const())
return false;
if (tied_to(other)) {
set_const(eval_conditionally(other));
return true;
}
NodeInput *in0, *in1;
in0 = &node->ins[0];
in1 = &node->ins[1];
if (in0->is_const() || !in0->tied_to(other))
std::swap(in0, in1);
if (in0->is_const() || !in0->tied_to(other))
return false;
if (in0->eval_conditionally(other)) {
EdgeFeatures save = feat;
*this = *in1;
feat.add(save);
} else {
set_const(feat.negated);
}
return true;
}
bool AndNode::expand()
{
if (pi) return false;
bool did = false;
while (ins[0].expand()) did = true;
while (ins[1].expand()) did = true;
while (ins[0].assume(ins[1]) || ins[1].assume(ins[0]))
did = true;
return did;
}
struct Network {
std::vector<AndNode*> nodes;
bool impure_module = false;
int frontier_size = 0;
Network() {}
~Network() {
for (auto node : nodes)
delete node;
}
Network (const Network&) = delete;
Network& operator= (const Network&) = delete;
Network(Network&& other) {
impure_module = other.impure_module;
frontier_size = other.frontier_size;
}
void yosys_import(RTLIL::Module *m, bool import_ff=false)
{
Yosys::SigMap sigmap(m);
Yosys::dict<RTLIL::SigBit, AndNode*> wire_nodes;
AndNode *node;
wire_nodes[RTLIL::State::S0] = node = new AndNode();
node->has_foreign_cell_users = false;
node->visited = false;
nodes.push_back(node);
node->ins[0].set_const(0);
node->ins[1].set_const(0);
wire_nodes[RTLIL::State::S1] = node = new AndNode();
node->has_foreign_cell_users = false;
node->visited = false;
nodes.push_back(node);
node->ins[0].set_const(1);
node->ins[1].set_const(1);
// uh oh
wire_nodes[RTLIL::State::Sx] = node = new AndNode();
node->has_foreign_cell_users = false;
node->visited = false;
nodes.push_back(node);
node->ins[0].set_const(0);
node->ins[1].set_const(0);
// Generate nodes for all wires, these will be pruned
// based on usage later
for (auto wire : m->wires())
for (int i = 0; i < wire->width; i++) {
RTLIL::SigBit bit(wire, i);
RTLIL::SigBit mapped = sigmap(bit);
if (!mapped.wire)
continue;
if (wire_nodes.count(mapped)) {
node = wire_nodes.at(mapped);
} else {
wire_nodes[mapped] = node = new AndNode();
node->has_foreign_cell_users = false;
node->visited = false;
node->yw = mapped;
nodes.push_back(node);
}
if (wire->port_input)
node->pi = true;
if (wire->port_output)
node->has_foreign_cell_users = true;
if (bit.wire->width == 1)
node->combine_label(bit.wire->name);
else
node->combine_label(Yosys::stringf("%s[%d]",
bit.wire->name.c_str(), bit.offset));
}
Yosys::pool<RTLIL::IdString> known_cells;
if (import_ff)
known_cells = {ID($_AND_), ID($_NOT_), ID($ff)};
else
known_cells = {ID($_AND_), ID($_NOT_)};
for (auto cell : m->cells())
if (!known_cells.count(cell->type))
for (auto &conn : cell->connections_) {
for (auto bit : sigmap(conn.second))
if (bit.wire) {
wire_nodes.at(bit)->has_foreign_cell_users = true;
log_assert(wire_nodes.at(bit)->yw.wire);
}
}
Yosys::FfInitVals initvals;
if (import_ff)
initvals.set(&sigmap, m);
std::vector<RTLIL::Cell *> imported_cells;
for (auto cell : m->cells()) {
if (cell->type == ID($ff) && import_ff) {
for (int i = 0; i < cell->getParam(Yosys::ID::WIDTH).as_int(); i++) {
RTLIL::SigBit q = sigmap(cell->getPort(Yosys::ID::Q)[i]);
AndNode *node = wire_nodes.at(q);
NodeInput *ins = node->ins;
if (node->has_foreign_cell_users) {
node->po = true;
log_assert(node->yw.wire);
}
ins[0].set_node(wire_nodes.at(sigmap(cell->getPort(Yosys::ID::D)[i])));
ins[0].delay(initvals(q));
ins[1].set_const(1);
}
imported_cells.push_back(cell);
} else if (cell->type.in(ID($_AND_), ID($_NOT_))) {
AndNode *node = wire_nodes.at(sigmap(cell->getPort(Yosys::ID::Y)));
if (node->has_foreign_cell_users) {
node->po = true;
log_assert(node->yw.wire);
AndNode *indirect_node = new AndNode();
node->ins[0].set_node(indirect_node);
node->ins[1].set_const(1);
node = indirect_node;
node->has_foreign_cell_users = false;
node->visited = false;
nodes.push_back(node);
}
NodeInput *ins = node->ins;
if (cell->type == ID($_AND_)) {
ins[0].set_node(wire_nodes.at(sigmap(cell->getPort(Yosys::ID::A))));
ins[0].node->visited = true;
ins[1].set_node(wire_nodes.at(sigmap(cell->getPort(Yosys::ID::B))));
ins[1].node->visited = true;
} else if (cell->type == ID($_NOT_)) {
ins[0].set_node(wire_nodes.at(sigmap(cell->getPort(Yosys::ID::A))));
ins[0].node->visited = true;
ins[0].negate();
ins[1].set_const(1);
}
imported_cells.push_back(cell);
} else {
// There are foreign cells in the module
impure_module = true;
}
}
for (auto cell : imported_cells)
m->remove(cell);
for (auto node : nodes)
if (node->has_foreign_cell_users && node->visited && !node->po)
node->pi = true;
clean(false);
compact();
clean(false);
int nnodes = 0;
for (auto node : nodes)
if (!node->po && !node->pi)
nnodes++;
log("Imported %d nodes\n", nnodes);
}
void yosys_perimeter(RTLIL::Module *m)
{
(void) m;
for (auto node : nodes)
if (!node->po && !node->pi)
node->yw = RTLIL::SigBit();
else
log_assert(node->yw.wire);
}
void yosys_wires(RTLIL::Module *m, bool mapping_only=false)
{
for (auto node : nodes) {
if (mapping_only && !node->map_fanouts)
continue;
if (node->yw.wire)
continue;
RTLIL::Wire *w;
RTLIL::IdString label = node->label;
if (label.empty())
label = NEW_ID;
while (m->wire(label))
label = Yosys::stringf("%s_", label.c_str());
w = m->addWire(label, 1);
node->yw = RTLIL::SigBit(w, 0);
}
}
void yosys_export(RTLIL::Module *m)
{
yosys_perimeter(m);
yosys_wires(m);
for (auto node : nodes) {
if (node->pi) continue;
NodeInput *nin = node->ins;
RTLIL::SigBit yin[2];
if (nin[0].feat.negated && !nin[1].feat.negated)
std::swap(nin[0], nin[1]);
for (int j = 0; j < 2; j++) {
if (nin[j].node)
yin[j] = nin[j].node->yw;
else
yin[j] = RTLIL::State::S0;
log_assert(nin[j].feat.lag == (int) nin[j].feat.initvals.size());
for (auto it = nin[j].feat.initvals.rbegin();
it != nin[j].feat.initvals.rend(); it++) {
RTLIL::SigBit q = m->addWire(NEW_ID, 1);
m->addFf(NEW_ID, yin[j], q);
if (*it != State::Sx)
q.wire->attributes[RTLIL::ID::init] =
RTLIL::Const(*it != State::S0 ? 1 : 0, 1);
yin[j] = q;
}
if (nin[j].feat.negated)
yin[j] = m->NotGate(NEW_ID, yin[j]);
}
m->addAndGate(NEW_ID, yin[0], yin[1], node->yw);
}
}
int clean(bool verbose=true)
{
std::vector<AndNode*> used;
for (auto node : nodes)
if (node->po || node->pi) {
node->visited = true;
used.push_back(node);
} else {
node->visited = false;
}
for (int i = 0; i < (int) used.size(); i++) {
for (auto in : used[i]->fanins()) {
if (!in->visited) {
used.push_back(in);
in->visited = true;
}
}
}
int nremoved = 0;
for (auto node : nodes)
if (!node->visited) {
nremoved++;
delete node;
}
std::reverse(used.begin(), used.end());
used.swap(nodes);
if (verbose)
log("Removed %d unused nodes\n", nremoved);
return nremoved;
}
void compact(bool verbose=false)
{
bool did = false;
while (true) {
did = false;
for (auto node : nodes)
did |= node->expand();
if (!did)
break;
}
clean(verbose);
}
void check_sort()
{
for (auto node : nodes)
node->visited = false;
for (auto node : nodes) {
if (!node->pi) {
if (node->ins[0].node)
log_assert(node->ins[0].node->visited);
if (node->ins[1].node)
log_assert(node->ins[1].node->visited);
}
node->visited = true;
}
}
void tsort()
{
std::vector<AndNode*> order;
for (auto node : nodes)
node->refs = 0;
for (auto node : nodes)
for (auto fanin : node->fanins())
fanin->refs++;
for (auto node : nodes)
if (!node->refs)
order.push_back(node);
for (int i = 0; i < (int) order.size(); i++)
for (auto fanin : order[i]->fanins()) {
fanin->refs--;
if (!fanin->refs)
order.push_back(fanin);
log_assert(fanin->refs >= 0);
}
std::reverse(order.begin(), order.end());
log_assert(order.size() == nodes.size());
nodes.swap(order);
check_sort();
}
void check_frontier()
{
AndNode **cache = new AndNode*[frontier_size];
for (auto node : nodes) {
cache[node->fid] = node;
AndNode *n1 = node->ins[0].node;
AndNode *n2 = node->ins[1].node;
if (n1)
log_assert(cache[n1->fid] == n1);
if (n2)
log_assert(cache[n2->fid] == n2);
}
delete[] cache;
}
void frontier()
{
frontier_size = 1; // first item is special (used for PO scratch)
std::vector<int> free_indices;
for (auto node : nodes) {
node->fid = 0;
node->visited = false;
}
for (auto it = nodes.rbegin(); it != nodes.rend(); it++) {
for (auto node : (*it)->fanins()) {
log_assert(!node->visited);
if (!node->fid) {
if (free_indices.empty())
free_indices.push_back(frontier_size++);
node->fid = free_indices.back();
free_indices.pop_back();
}
}
// free our index
if ((*it)->fid != 0)
free_indices.push_back((*it)->fid);
(*it)->visited = true;
}
check_frontier();
log("Frontier is %d wide at its peak\n", frontier_size);
}
int walk_mapping(const LutLibrary &lib, bool verbose=false)
{
for (auto it = nodes.rbegin(); it != nodes.rend(); it++) {
AndNode *node = *it;
log_assert(node->map_fanouts >= 0);
log_assert(node->map_fanouts <= node->po ? 1 : 0);
if (node->map_fanouts)
deref_cut(node);
node->map_fanouts = 0;
}
for (auto node : nodes)
if (node->po) {
log_assert(node->map_fanouts == 0);
if (!node->map_fanouts++)
ref_cut(node);
}
int area = 0, support_area = 0;
for (auto node : nodes) {
if (node->pi || node->po)
continue;
if (node->map_fanouts)
area += lib.lookup(CutList(node->cut).size).cost;
if (node->map_fanouts == 1)
support_area++;
}
if (verbose)
log("Mapping: Area is %d (of this %d nodes are single-fanout)\n",
area, support_area);
return area;
}
void unique()
{
tsort();
for (auto node : nodes) {
node->replacement = NULL;
if (node->ins[1] < node->ins[0])
std::swap(node->ins[0], node->ins[1]);
}
dict<std::pair<NodeInput, NodeInput>, AndNode*> repr;
for (auto node : nodes)
if (!node->pi) {
node->apply_replacements();
if (node->po) continue;
auto in_pair = std::make_pair(node->ins[0], node->ins[1]);
if (!repr.count(in_pair))
repr[in_pair] = node;
else
node->replacement = repr.at(in_pair);
}
clean();
}
void collect(std::vector<NodeInput> &vec, CoverNode node)
{
if (node.img->pi) {
vec.emplace_back(node, false);
return;
}
for (int i = 0; i < 2; i++)
if (node.img->ins[i].node) {
if (node.img->ins[i].node->fanouts > 1 || node.img->ins[i].node->feeds_inverter) {
vec.emplace_back(node.img->ins[i].cover_node(),
node.img->ins[i].feat.negated);
} else {
collect(vec, node.img->ins[i].cover_node());
}
}
}
AndNode *balance_tree(AndNode *root)
{
std::vector<NodeInput> vec;
collect(vec, CoverNode{0, root});
std::sort(vec.begin(), vec.end(), [](NodeInput a, NodeInput b){
return a.node->depth > b.node->depth;
});
log_assert(vec.size() > 1);
while (vec.size() > 1) {
AndNode *new_node = new AndNode();
nodes.push_back(new_node);
new_node->ins[0] = vec.back(); vec.pop_back();
new_node->ins[1] = vec.back(); vec.pop_back();
new_node->fanouts = 1;
new_node->depth = std::max(
new_node->ins[0].node->depth, new_node->ins[1].node->depth) + 1;
vec.emplace_back(CoverNode{0, new_node}, false);
std::sort(vec.begin(), vec.end(), [](NodeInput a, NodeInput b){
return a.node->depth > b.node->depth;
});
}
log_assert(vec.size() == 1);
AndNode *ret = vec.front().node;
log_assert(!vec.front().feat.negated);
std::swap(ret->fanouts, root->fanouts);
ret->feeds_inverter = root->feeds_inverter;
ret->andtree_counter = root->andtree_counter;
ret->replacement = NULL;
return ret;
}
void balance()