-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdd.c
2479 lines (2334 loc) · 74.4 KB
/
bdd.c
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
/* Implementation of ref-based BDD */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdbool.h>
#ifdef QUEUE
#include <pthread.h>
#endif
#include "dtype.h"
#include "table.h"
#include "chunk.h"
#include "report.h"
#include "msg.h"
#include "console.h"
#include "agent.h"
#include "bdd.h"
/*
Parameters controlling garbage collection
*/
/* Max ratio between current number of unique nodes
and number at end of previous GC */
#define GC_RATIO 2
/* Minimum node count below which will not trigger GC */
#define GC_THRESHOLD 100000
/* Can GC be requested based on growth characteristics? */
bool auto_gc_enabled = true;
/* Unique table uses a linked list for each possible hash value. */
/*
Distinguish among elements having same hash with unique ID,
having value >= 1.
These are encoded as part of the ref for the function
*/
typedef struct LELE {
ref_t ref;
chunk_ptr data; /* Chunk containing vref, hiref, loref */
struct LELE *next;
} ulist_ele, *ulist_ptr;
/* Create new element for ulist */
static ulist_ptr ulist_new(chunk_ptr data, ref_t ref) {
ulist_ptr result = (ulist_ptr) malloc_or_fail(sizeof(ulist_ele),
"ulist_new");
result->data = data;
result->ref = ref;
result->next = NULL;
return result;
}
/* Free all elements in a ulist */
static void ulist_free(ulist_ptr ele) {
while (ele) {
ulist_ptr nele = ele->next;
chunk_free(ele->data);
free_block((void *) ele, sizeof(ulist_ele));
ele = nele;
}
}
/* Encapsulate 3 refs in chunk */
chunk_ptr ref3_encode(ref_t r1, ref_t r2, ref_t r3) {
chunk_ptr ucp = chunk_new(3);
chunk_insert_word(ucp, (word_t) r1, 0);
chunk_insert_word(ucp, (word_t) r2, 1);
chunk_insert_word(ucp, (word_t) r3, 2);
return ucp;
}
/* Compute hash code for vref, hiref, loref. Used to create hash for ref */
size_t utable_hash(chunk_ptr ucp) {
size_t val = chunk_hash((word_t) ucp);
// return (val % (2147483629UL)) & REF_MASK_HASH;
return val & REF_MASK_HASH;
}
/* Functions to use key/value table to implement unique table */
/* Use refs as keys, aliased to void* */
/* Use hash values as keys in unique table */
size_t ref_hash(word_t vval) {
ref_t r = (ref_t) vval;
size_t h = REF_GET_HASH(r);
return h;
}
bool ref_hash_equal(word_t vval1, word_t vval2) {
word_t h1 = REF_GET_HASH((ref_t) vval1);
word_t h2 = REF_GET_HASH((ref_t) vval2);
return h1 == h2;
}
ref_mgr new_ref_mgr() {
ref_mgr mgr = malloc_or_fail(sizeof(ref_mgr_ele), "new_mgr");
mgr->variable_cnt = 0;
mgr->unique_table = keyvalue_new(word_hash, word_equal);
mgr->ite_table = keyvalue_new(chunk_hash, chunk_equal);
size_t i;
for (i = 0; i < NSTAT; i++)
mgr->stat_counter[i] = 0;
mgr->last_nelements = 0;
return mgr;
}
ref_t ref_new_variable(ref_mgr mgr) {
int idx = mgr->variable_cnt++;
return REF_VAR(idx);
}
/* Function to clear out lists in unique table */
static void clear_ulist(word_t key, word_t value) {
ulist_ptr ulist = (ulist_ptr) value;
ulist_free(ulist);
}
/* Function to remove keys from ITE cache */
static void clear_ite_entry(word_t key, word_t value) {
chunk_ptr cp = (chunk_ptr) key;
chunk_free(cp);
}
/* Clear ITE cache */
static void clear_ite_table(ref_mgr mgr) {
keyvalue_apply(mgr->ite_table, clear_ite_entry);
keyvalue_free(mgr->ite_table);
mgr->ite_table = keyvalue_new(chunk_hash, chunk_equal);
mgr->stat_counter[STATB_ITEC_CURR] = 0;
}
void free_ref_mgr(ref_mgr mgr) {
keyvalue_apply(mgr->unique_table, clear_ulist);
keyvalue_free(mgr->unique_table);
clear_ite_table(mgr);
keyvalue_free(mgr->ite_table);
free_block(mgr, sizeof(ref_mgr_ele));
}
/* Create string representation of ref */
void ref_show(ref_t r, char *buf) {
char nc = REF_GET_NEG(r) ? '-' : '+';
int var = REF_GET_VAR(r);
word_t hash = REF_GET_HASH(r);
int uniq = REF_GET_UNIQ(r);
switch(REF_GET_TYPE(r)) {
case BDD_NULL:
sprintf(buf, "NULL");
break;
case BDD_CONSTANT:
sprintf(buf, "%cC", nc);
break;
case BDD_VARIABLE:
sprintf(buf, "%cV.%d", nc, var);
break;
case BDD_FUNCTION:
sprintf(buf, "%cF.%d.%" PRIx64 "+%d", nc, var, hash, uniq);
break;
case BDD_RECURSE:
sprintf(buf, "%cR.%d.%" PRIx64 "+%d", nc, var, hash, uniq);
break;
case BDD_INVALID:
sprintf(buf, "%cI.%d.%" PRIx64 "+%d", nc, var, hash, uniq);
break;
default:
sprintf(buf, "%c?.%d.%" PRIx64 "+%d", nc, var, hash, uniq);
break;
}
}
/* Do preparatory steps in canonize.
Return ref_t if completed, and either REF_RECURSE or its negation if not.
Variants of REF_RECURSE indicate whether or not final value should be negated.
In latter case, set cpp to chunk_ptr that can serve as key to unique table
*/
ref_t ref_canonize_local(ref_t vref, ref_t hiref, ref_t loref, chunk_ptr *ucpp) {
if (hiref == REF_INVALID || loref == REF_INVALID)
return REF_INVALID;
word_t vlev = REF_GET_VAR(vref);
word_t hilev = REF_GET_VAR(hiref);
word_t lolev = REF_GET_VAR(loref);
if (vlev >= hilev || vlev >= lolev) {
err(false, "Invalid levels for canonize. var:%lu, hi:%lu, lo:%lu",
vlev, hilev, lolev);
return REF_INVALID;
}
if (hiref==loref)
return hiref;
if (hiref==REF_ONE && loref==REF_ZERO)
return vref;
if (hiref==REF_ZERO && loref==REF_ONE)
return REF_NEGATE(vref);
ref_t return_val = REF_RECURSE;
if (REF_GET_NEG(hiref)) {
return_val = REF_NEGATE(return_val);
hiref = REF_NEGATE(hiref);
loref = REF_NEGATE(loref);
}
chunk_ptr ucp = ref3_encode(vref, hiref, loref);
*ucpp = ucp;
return return_val;
}
/* Perform lookup / creation portion of canonize.
Assumes arguments already fixed up and stored in form
suitable for unique table
*/
static ref_t ref_canonize_lookup(ref_mgr mgr, chunk_ptr ucp) {
size_t h = utable_hash(ucp);
ref_t vref = chunk_get_word(ucp, 0);
ulist_ptr ls = NULL;
bool have_list = keyvalue_find(mgr->unique_table,
(word_t) h, (word_t *) &ls);
bool found = false;
/* Keep track of location pointing to last element in list */
ulist_ptr *tailp = NULL;
/* Keep track of largest uniquifier encountered in list */
size_t largest_used = 0;
ref_t r;
/* Traverse list, looking for entry with matching var, hi, lo */
while (ls) {
if (chunk_equal((word_t) ucp, (word_t) ls->data)) {
/* Found entry */
r = ls->ref;
found = true;
break;
}
size_t uniquifier = REF_GET_UNIQ(ls->ref);
if (uniquifier > largest_used)
largest_used = uniquifier;
tailp = &ls->next;
ls = ls->next;
}
if (found) {
/* Don't need this chunk */
chunk_free(ucp);
} else {
/* Came to end of list without finding matching entry.
Create a new one. */
size_t uniquifier = largest_used + 1;
/* See if have exceeded bounds for uniquifier */
if (uniquifier > REF_MASK_UNIQ)
err(true, "Exceeded uniquifier bounds. Hash = 0x%llx", h);
if (uniquifier > mgr->stat_counter[STATB_UNIQ_MAX])
mgr->stat_counter[STATB_UNIQ_MAX] = uniquifier;
r = PACK_REF(0, BDD_FUNCTION, REF_GET_VAR(vref), h, uniquifier);
ls = ulist_new(ucp, r);
#if RPT >= 4
ref_t vref = chunk_get_word(ucp, 0);
ref_t hiref = chunk_get_word(ucp, 1);
ref_t loref = chunk_get_word(ucp, 2);
char vbuf[24], hibuf[24], lobuf[24], rbuf[24];
ref_show(vref, vbuf);
ref_show(hiref, hibuf);
ref_show(loref, lobuf);
ref_show(r, rbuf);
report(4, "Creating unique table entry [%s,%s,%s] --> %s",
vbuf, hibuf, lobuf, rbuf);
#endif
mgr->stat_counter[STATB_UNIQ_CURR]++;
if (mgr->stat_counter[STATB_UNIQ_CURR]
> mgr->stat_counter[STATB_UNIQ_PEAK])
mgr->stat_counter[STATB_UNIQ_PEAK]
= mgr->stat_counter[STATB_UNIQ_CURR];
mgr->stat_counter[STATB_UNIQ_TOTAL]++;
if (have_list) {
*tailp = ls;
mgr->stat_counter[STATB_UNIQ_COLLIDE]++;
} else {
/* First entry for this hash */
keyvalue_insert(mgr->unique_table, (word_t) h, (word_t) ls);
}
}
return r;
}
ref_t ref_canonize(ref_mgr mgr, ref_t vref, ref_t hiref, ref_t loref) {
chunk_ptr ucp;
ref_t r = ref_canonize_local(vref, hiref, loref, &ucp);
if (REF_IS_RECURSE(r)) {
size_t neg = REF_GET_NEG(r);
r = ref_canonize_lookup(mgr, ucp);
if (neg)
r = REF_NEGATE(r);
}
return r;
}
/* Do dereferencing steps that can be done locally.
Return true if successful */
bool ref_deref_local(ref_t r, ref_t *vrefp, ref_t *hirefp, ref_t *lorefp) {
switch (REF_GET_TYPE(r)) {
case BDD_CONSTANT:
*vrefp = r; *hirefp = r; *lorefp = r;
return true;
case BDD_VARIABLE:
*vrefp = r;
if (REF_GET_NEG(r)) {
*hirefp = REF_ZERO; *lorefp = REF_ONE;
} else {
*hirefp = REF_ONE; *lorefp = REF_ZERO;
}
return true;
case BDD_FUNCTION:
return false;
case BDD_RECURSE:
case BDD_INVALID:
err(false, "Invalid reference encountered during dereferencing");
return false;
default:
err(false, "Unexpected ref type %d\n", REF_GET_TYPE(r));
return false;
}
}
chunk_ptr ref_deref_lookup(ref_mgr mgr, ref_t r) {
if (REF_GET_TYPE(r) != BDD_FUNCTION) {
err(false, "Attempted to dereference non-function node");
return NULL;
}
size_t h = REF_GET_HASH(r);
ulist_ptr ls = NULL;
ulist_ptr ele = ls;
keyvalue_find(mgr->unique_table, (word_t) h, (word_t *) &ls);
ele = ls;
while (ele) {
if (ele->ref == r)
return ele->data;
ele = ele->next;
}
#if RPT >= 3
if (ls) {
char buf[24];
ref_show(r, buf);
report(3, "Looking for ref %s. Found list in hash table, but no entry",
buf);
ele = ls;
while (ele) {
char ebuf[24];
ref_show(ele->ref, ebuf);
report(3, "\tMismatch with %s", ebuf);
ele = ele->next;
}
}
#endif
return NULL;
}
void ref_deref(ref_mgr mgr, ref_t r,
ref_t *vrefp, ref_t *hirefp, ref_t *lorefp) {
if (ref_deref_local(r, vrefp, hirefp, lorefp))
return;
ref_t ar = REF_ABSVAL(r);
chunk_ptr cp = ref_deref_lookup(mgr, ar);
if (cp == NULL) {
char buf[24];
ref_show(ar, buf);
err(false, "Could not find unique table entry for %s", buf);
*vrefp = *hirefp = *lorefp = REF_INVALID;
return;
}
*vrefp = (ref_t) chunk_get_word(cp, 0);
ref_t hiref = (ref_t) chunk_get_word(cp, 1);
ref_t loref = (ref_t) chunk_get_word(cp, 2);
if (REF_GET_NEG(r)) {
*hirefp = REF_NEGATE(hiref); *lorefp = REF_NEGATE(loref);
} else {
*hirefp = hiref; *lorefp = loref;
}
}
/* Perform local parts of ITE.
Return either result or (possibly negated) recurse ref.
In latter case, set *ucpp to triple with ITE arguments
*/
ref_t ref_ite_local(ref_mgr mgr, ref_t iref,
ref_t tref, ref_t eref, chunk_ptr *ucpp) {
ref_t r;
#if RPT >= 4
ref_t siref = iref;
ref_t stref = tref;
ref_t seref = eref;
#endif
mgr->stat_counter[STATB_ITE_CNT]++;
/* Simple cases */
if (iref == REF_ONE)
r = tref;
else if (iref == REF_ZERO)
r = eref;
else if (tref == eref)
r = tref;
else if (tref == REF_ONE && eref == REF_ZERO)
r = iref;
else if (tref == REF_ZERO && eref == REF_ONE)
r = REF_NEGATE(iref);
else {
/* Cannot be handled locally. Fix up arguments */
bool negate = false;
/* Don't want iref to be complemented */
if (REF_GET_NEG(iref)) {
ref_t sref = tref;
tref = eref;
eref = sref;
iref = REF_NEGATE(iref);
}
/*
Don't want tref to be complemented.
Handles case where tref is 0.
Also does And -> Or through DeMorgan's conversion
*/
if (REF_GET_NEG(tref)) {
negate = !negate;
tref = REF_NEGATE(tref);
eref = REF_NEGATE(eref);
}
/* Absorption rules */
if (iref == tref) {
tref = REF_ONE;
/* Might have terminal case */
if (tref == eref)
return negate ? REF_NEGATE(tref) : tref;
if (tref == REF_ONE && eref == REF_ZERO)
return negate ? REF_NEGATE(iref) : iref;
}
if (iref == eref) {
eref = REF_ZERO;
/* Might have terminal case */
if (tref == eref)
return negate ? REF_NEGATE(tref) : tref;
if (tref == REF_ONE && eref == REF_ZERO)
return negate ? REF_NEGATE(iref) : iref;
}
if (iref == REF_NEGATE(eref)) {
eref = REF_ONE;
}
/* Mirrorings */
/* Canonical ordering of And arguments */
if (eref == REF_ZERO && iref > tref) {
ref_t sref = iref;
iref = tref;
tref = sref;
}
/* Canonical ordering of Xor arguments */
if (tref == REF_NEGATE(eref) && iref > tref) {
ref_t sref = iref;
iref = tref;
tref = sref;
eref = REF_NEGATE(tref);
}
*ucpp = ref3_encode(iref, tref, eref);
r = REF_RECURSE;
if (negate)
r = REF_NEGATE(r);
}
if (!REF_IS_RECURSE(r))
mgr->stat_counter[STATB_ITE_LOCAL_CNT]++;
#if RPT >= 4
char sibuf[24], stbuf[24], sebuf[24];
ref_show(siref, sibuf);
ref_show(stref, stbuf);
ref_show(seref, sebuf);
if (REF_IS_RECURSE(r)) {
char ibuf[24], tbuf[24], ebuf[24];
ref_show(iref, ibuf);
ref_show(tref, tbuf);
ref_show(eref, ebuf);
char *ns = REF_GET_NEG(r) ? "!" : "";
report(4, "ITE Local(%s, %s, %s) -> %sITE(%s,%s,%s)",
sibuf, stbuf, sebuf, ns, ibuf, tbuf, ebuf);
} else {
char rbuf[24];
ref_show(r, rbuf);
report(4, "ITE Local(%s, %s, %s) -> %s",
sibuf, stbuf, sebuf, rbuf);
}
#endif
return r;
}
ref_t ref_ite_lookup(ref_mgr mgr, chunk_ptr ucp) {
ref_t r;
word_t w;
if (keyvalue_find(mgr->ite_table, (word_t) ucp, (word_t *) &w)) {
r = (ref_t) w;
mgr->stat_counter[STATB_ITE_HIT_CNT]++;
} else
r = REF_RECURSE;
#if RPT >= 4
ref_t iref = chunk_get_word(ucp, 0);
ref_t tref = chunk_get_word(ucp, 1);
ref_t eref = chunk_get_word(ucp, 2);
char ibuf[24], tbuf[24], ebuf[24], rbuf[24];
ref_show(iref, ibuf);
ref_show(tref, tbuf);
ref_show(eref, ebuf);
ref_show(r, rbuf);
report(4, "ITELookup(%s,%s,%s) --> %s",
ibuf, tbuf, ebuf, rbuf);
#endif
return r;
}
void ref_ite_store(ref_mgr mgr, chunk_ptr ucp, ref_t r) {
#if RPT >= 4
ref_t iref = chunk_get_word(ucp, 0);
ref_t tref = chunk_get_word(ucp, 1);
ref_t eref = chunk_get_word(ucp, 2);
char ibuf[24], tbuf[24], ebuf[24], rbuf[24];
ref_show(iref, ibuf);
ref_show(tref, tbuf);
ref_show(eref, ebuf);
ref_show(r, rbuf);
report(4, "Storing ITE(%s,%s,%s) --> %s",
ibuf, tbuf, ebuf, rbuf);
#endif
keyvalue_insert(mgr->ite_table, (word_t) ucp, (word_t) r);
mgr->stat_counter[STATB_ITEC_TOTAL]++;
mgr->stat_counter[STATB_ITEC_CURR]++;
if (mgr->stat_counter[STATB_ITEC_CURR] > mgr->stat_counter[STATB_ITEC_PEAK])
mgr->stat_counter[STATB_ITEC_PEAK] = mgr->stat_counter[STATB_ITEC_CURR];
}
/* Recursive calls for ITE */
static void ref_ite_recurse(ref_mgr mgr,
ref_t irefhi, ref_t ireflo,
ref_t trefhi, ref_t treflo,
ref_t erefhi, ref_t ereflo,
ref_t *newhip, ref_t *newlop) {
*newhip = ref_ite(mgr, irefhi, trefhi, erefhi);
*newlop = ref_ite(mgr, ireflo, treflo, ereflo);
mgr->stat_counter[STATB_ITE_NEW_CNT]++;
}
/* ITE operation */
ref_t ref_ite(ref_mgr mgr, ref_t iref, ref_t tref, ref_t eref) {
chunk_ptr ucp;
ref_t r = ref_ite_local(mgr, iref, tref, eref, &ucp);
if (!REF_IS_RECURSE(r)) {
return r;
}
bool neg = REF_GET_NEG(r) == 1;
r = ref_ite_lookup(mgr, ucp);
if (!REF_IS_RECURSE(r)) {
chunk_free(ucp);
if (neg)
r = REF_NEGATE(r);
#if RPT >= 4
char buf1[24];
ref_show(r, buf1);
report(4, "\tFound via lookup: %s", buf1);
#endif
return r;
}
/* Must do recursion */
/* Retrieve possibly rearranged parameters */
iref = (ref_t) chunk_get_word(ucp, 0);
tref = (ref_t) chunk_get_word(ucp, 1);
eref = (ref_t) chunk_get_word(ucp, 2);
#if RPT >= 4
char buf1[24], buf2[24], buf3[24];
iref = (ref_t) chunk_get_word(ucp, 0);
tref = (ref_t) chunk_get_word(ucp, 1);
eref = (ref_t) chunk_get_word(ucp, 2);
ref_show(iref, buf1); ref_show(tref, buf2); ref_show(eref, buf3);
char *ns = neg ? "!" : "";
report(4, "Computing %sITE(%s, %s, %s)", ns, buf1, buf2, buf3);
#endif
size_t ivar = REF_GET_VAR(iref);
size_t tvar = REF_GET_VAR(tref);
size_t evar = REF_GET_VAR(eref);
size_t var = ivar;
if (tvar < var) { var = tvar; }
if (evar < var) { var = evar; }
#if RPT >= 4
ref_show(REF_VAR(var), buf1);
report(4, "\tSplitting on variable %s", buf1);
#endif
ref_t ivref, irefhi, ireflo, tvref, trefhi, treflo, evref, erefhi, ereflo;
if (ivar == var)
ref_deref(mgr, iref, &ivref, &irefhi, &ireflo);
else
irefhi = ireflo = iref;
if (tvar == var)
ref_deref(mgr, tref, &tvref, &trefhi, &treflo);
else
trefhi = treflo = tref;
if (evar == var)
ref_deref(mgr, eref, &evref, &erefhi, &ereflo);
else
erefhi = ereflo = eref;
ref_t newhi, newlo;
ref_ite_recurse(mgr, irefhi, ireflo, trefhi, treflo,
erefhi, ereflo, &newhi, &newlo);
ref_t vref = REF_VAR(var);
r = ref_canonize(mgr, vref, newhi, newlo);
ref_ite_store(mgr, ucp, r);
if (neg)
r = REF_NEGATE(r);
#if RPT >= 4
ref_show(r, buf1);
report(4, "\tGot recursively computed case: %s", buf1);
#endif
return r;
}
/* Special cases of ITE */
ref_t ref_and(ref_mgr mgr, ref_t aref, ref_t bref) {
return ref_ite(mgr, aref, bref, REF_ZERO);
}
ref_t ref_or(ref_mgr mgr, ref_t aref, ref_t bref) {
return ref_ite(mgr, aref, REF_ONE, bref);
}
ref_t ref_xor(ref_mgr mgr, ref_t aref, ref_t bref) {
return ref_ite(mgr, aref, REF_NEGATE(bref), bref);
}
/* See if it's time to perform a GC */
bool ref_gc_check(ref_mgr mgr) {
if (!auto_gc_enabled)
return false;
size_t n = mgr->stat_counter[STATB_UNIQ_CURR];
if (n <= GC_THRESHOLD)
return false;
if (n <= GC_RATIO * mgr->last_nelements)
return false;
return true;
}
/*** Implementation of unary operations ****/
/* Supported operations */
typedef enum {
UOP_MARK,
UOP_SUPPORT,
UOP_DENSITY,
UOP_PCOUNT,
UOP_COFACTOR,
UOP_EQUANT,
UOP_SHIFT
} uop_type_t;
typedef struct UELE uop_mgr_ele, *uop_mgr_ptr;
struct UELE {
ref_mgr mgr;
unsigned id;
uop_type_t operation;
/* Mapping from nodes to values */
keyvalue_table_ptr map;
void *auxinfo;
/* For distributed implementation */
keyvalue_table_ptr deferred_uop_table;
/* Form in linked list */
uop_mgr_ptr next;
};
/* Unary functions defined in terms of operation performed at each node */
typedef word_t (*uop_node_fun)(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
/* Forward declarations */
static word_t uop_node_mark(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static word_t uop_node_support(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static word_t uop_node_density(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static word_t uop_node_pcount(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static word_t uop_node_cofactor(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static word_t uop_node_equant(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static word_t uop_node_shift(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo);
static uop_node_fun uop_node_functions[] = {
uop_node_mark, uop_node_support, uop_node_density,
uop_node_pcount,
uop_node_cofactor, uop_node_equant, uop_node_shift
};
/* Extra information included when distributed */
static uop_mgr_ptr new_uop(ref_mgr mgr, unsigned id,
uop_type_t op, void *auxinfo, bool dist) {
uop_mgr_ptr umgr = (uop_mgr_ptr) malloc_or_fail(sizeof(uop_mgr_ele),
"new_uop");
umgr->mgr = mgr;
umgr->id = id;
umgr->operation = op;
umgr->map = word_keyvalue_new();
umgr->auxinfo = auxinfo;
umgr->deferred_uop_table = NULL;
umgr->next = NULL;
if (dist)
umgr->deferred_uop_table = word_keyvalue_new();
return umgr;
}
/* In distributed implementation, require tables to hold deferred operations */
/* Each table maps from a key to a set of deferred operations */
/* For ITEs, require destination plus whether to negate */
/* For Unary operations, only require destination */
/* Linked list to hold values in deferred ITE table */
typedef struct IELE ilist_ele, *ilist_ptr;
struct IELE {
dword_t dest;
bool negate;
ilist_ptr next;
};
/* Create a new list element */
static ilist_ptr ilist_new(dword_t dest, bool negate) {
ilist_ptr ele = malloc_or_fail(sizeof(ilist_ele), "ilist_new");
ele->dest = dest;
ele->negate = negate;
ele->next = NULL;
return ele;
}
/* Free all elements in a ilist */
static void ilist_free(ilist_ptr ls) {
ilist_ptr ele = ls;
while (ele) {
ilist_ptr nele = ele->next;
free_block((void *) ele, sizeof(ilist_ele));
ele = nele;
}
}
static void free_uop(uop_mgr_ptr umgr) {
keyvalue_free(umgr->map);
if (umgr->deferred_uop_table) {
word_t wv;
while (keyvalue_removenext(umgr->deferred_uop_table, NULL, &wv)) {
ilist_ptr ilist = (ilist_ptr) wv;
ilist_free(ilist);
}
keyvalue_free(umgr->deferred_uop_table);
}
free_block((void *) umgr, sizeof(uop_mgr_ele));
}
/* Depth-first recursive traversal to implement unary operation */
static word_t uop_traverse(uop_mgr_ptr umgr, ref_t r) {
#if RPT >= 4
char buf[24];
ref_show(r, buf);
report(4, "Uop traversal hits %s", buf);
#endif
/* See if already done */
word_t val;
if (keyvalue_find(umgr->map, (word_t) r, &val)) {
#if RPT >= 4
report(4, "\tRetrieved previous value 0x%llx", val);
#endif
return val;
}
/* See if terminal case */
if (REF_IS_CONST(r)) {
val = uop_node_functions[umgr->operation](umgr, r, (word_t) 0,
(word_t) 0, umgr->auxinfo);
#if RPT >= 4
report(4, "\tConstant node yields 0x%llx", val);
#endif
} else {
/* Apply recursively */
ref_t vref, hiref, loref;
ref_deref(umgr->mgr, r, &vref, &hiref, &loref);
word_t hival = uop_traverse(umgr, hiref);
word_t loval = uop_traverse(umgr, loref);
val = uop_node_functions[umgr->operation](umgr, r, hival, loval,
umgr->auxinfo);
#if RPT >= 4
report(4, "\tComputed value 0x%llx", val);
#endif
}
keyvalue_insert(umgr->map, (word_t) r, val);
return val;
}
/* Initiate unary operation */
static void uop_go(uop_mgr_ptr umgr, set_ptr roots) {
word_t w;
set_iterstart(roots);
while (set_iternext(roots, &w)) {
ref_t r = (ref_t) w;
uop_traverse(umgr, r);
}
}
/* Retrieve value computed for uop */
word_t uop_getval(uop_mgr_ptr umgr, ref_t r) {
word_t val;
if (!keyvalue_find(umgr->map, (word_t) r, (word_t *) &val)) {
char buf[24];
ref_show(r, buf);
err(false, "Couldn't find ref %s in map for unary operation #%d",
buf, umgr->id);
return (word_t) 0;
}
return val;
}
static word_t uop_node_mark(uop_mgr_ptr umgr, ref_t r, word_t hival,
word_t loval, void *auxinfo) {
/* Aux info is a set indicating already reached nodes */
ref_t ar = REF_ABSVAL(r);
set_ptr rset = (set_ptr) auxinfo;
if (!REF_IS_CONST(r) && !set_member(rset, (word_t) ar, false))
set_insert(rset, (word_t) ar);
return (word_t) 1;
}
#if 0
/* Convert set of variables into bit vector */
static word_t vset2bv(set_ptr set) {
word_t vset = 0;
word_t wr;
set_iterstart(set);
while (set_iternext(set, &wr)) {
ref_t r = (ref_t) wr;
unsigned idx = REF_GET_VAR(r);
vset |= ((word_t) 1<<idx);
}
return vset;
}
#endif
/* Convert bit vector into set of variables */
static set_ptr bv2vset(word_t vset) {
unsigned idx;
set_ptr set = word_set_new();
for (idx = 0; vset; idx++) {
if (vset & 0x1) {
ref_t r = REF_VAR(idx);
set_insert(set, (word_t) r);
}
vset >>= 1;
}
return set;
}
static word_t uop_node_support(uop_mgr_ptr umgr, ref_t r, word_t hival,
word_t loval, void *auxinfo) {
/* Result is bit vector encoding local and downward support */
/* Aux info is a set indicating already found support members */
if (REF_IS_CONST(r))
return (word_t) 0;
/* Set form */
set_ptr supset = (set_ptr) auxinfo;
ref_t vr = REF_VAR(REF_GET_VAR(r));
if (!set_member(supset, (word_t) vr, false)) {
set_insert(supset, (word_t) vr);
}
/* Bit vector form */
word_t lbv = (word_t) 1 << REF_GET_VAR(r);
return lbv | hival | loval;
}
static word_t d2w(double d) {
union {
double d;
word_t w;
} x;
x.d = d;
return x.w;
}
static double w2d(word_t w) {
union {
double d;
word_t w;
} x;
x.w = w;
return x.d;
}
static word_t uop_node_density(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo) {
/* Aux info is null */
double val;
if (r == REF_ONE) {
val = 1.0;
} else if (r == REF_ZERO) {
val = 0.0;
} else {
double hval = w2d(hival);
double lval = w2d(loval);
val = (hval + lval)/2.0;
}
return d2w(val);
}
/* Uop counting packs index and count (relative to that index)
into single word */
static word_t pack_count(unsigned idx, word_t cnt) {
return ((word_t) idx << 48) | cnt;
}
static unsigned unpack_index(word_t pval) {
return pval >> 48;
}
static word_t unpack_val(word_t pval) {
return pval & ~((~0UL) << 48);
}
/* Unpack packed value and weight according to specified index */
word_t pval2cnt(word_t pval, unsigned idx) {
unsigned pidx = unpack_index(pval);
word_t pcnt = unpack_val(pval);
word_t wt = 1L << (pidx-idx);
return wt * pcnt;
}
static word_t uop_node_pcount(uop_mgr_ptr umgr, ref_t r, word_t hival,
word_t loval, void *auxinfo) {
/* Aux info is count of number of variables */
/* Compute count relative to top-level index.
Pack index into high-order 16 bits */
word_t nvars = *(word_t *) auxinfo;
word_t cnt;
word_t idx = REF_GET_VAR(r);
if (idx > nvars)
idx = nvars;
if (r == REF_ONE) {
cnt = 1;
} else if (r == REF_ZERO) {
cnt = 0;
} else {
word_t hcnt = pval2cnt(hival, idx+1);
word_t lcnt = pval2cnt(loval, idx+1);
cnt = hcnt + lcnt;
}
return pack_count(idx, cnt);
}
static word_t uop_node_cofactor(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo) {
if (REF_IS_CONST(r))
return (word_t) r;
/* Auxinfo is a set of literals */
/* Return value is ref of cofactored function */
set_ptr litset = (set_ptr) auxinfo;
ref_t vr = REF_VAR(REF_GET_VAR(r));
ref_t hiref = (ref_t) hival;
ref_t loref = (ref_t) loval;
ref_t nr;
if (set_member(litset, (word_t) vr, false))
/* Want positive cofactor */
nr = hiref;
else if (set_member(litset, (word_t) REF_NEGATE(vr), false))
/* Want negative cofactor */
nr = loref;
else
nr = ref_canonize(umgr->mgr, vr, hiref, loref);
return (word_t) nr;
}
static word_t uop_node_equant(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo) {
if (REF_IS_CONST(r))
return (word_t) r;
/* Auxinfo is a set of variables */
/* Return value is ref of quantified function */
set_ptr varset = (set_ptr) auxinfo;
ref_t vr = REF_VAR(REF_GET_VAR(r));
ref_t hiref = (ref_t) hival;
ref_t loref = (ref_t) loval;
ref_t nr;
if (set_member(varset, (word_t) vr, false))
nr = ref_or(umgr->mgr, hiref, loref);
else
nr = ref_canonize(umgr->mgr, vr, hiref, loref);
return (word_t) nr;
}
/* Relabel old variables to new. Must maintain compatible variable ordering */
/* Auxinfo is table mapping old vref's to new ones */
static word_t uop_node_shift(uop_mgr_ptr umgr, ref_t r,
word_t hival, word_t loval, void *auxinfo) {
if (REF_IS_CONST(r))
return (word_t) r;
/* Return value is ref of shifted function */
keyvalue_table_ptr vmap = (keyvalue_table_ptr) auxinfo;
ref_t vr = REF_VAR(REF_GET_VAR(r));