-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatpg.c
3881 lines (3195 loc) · 115 KB
/
atpg.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
/*
This file is part of Nenofex.
Nenofex, an expansion-based QBF solver for negation normal form.
Copyright 2008, 2012, 2017 Florian Lonsing.
Nenofex is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Nenofex is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with Nenofex. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "stack.h"
#include "queue.h"
#include "nenofex_types.h"
#include "mem.h"
#define DEFAULT_STACK_SIZE 128
#define DEFAULT_QUEUE_SIZE 128
/*
- if sum of fwd- and bwd-propagations exceeds limit then abort
- default values; actual ones set dynamically depending on subgraph size
*/
static unsigned int ATPG_PROPAGATION_LIMIT = 50000;
static unsigned int GLOBAL_FLOW_PROPAGATION_LIMIT = 50000;
/*
- disabling recommended: print detailed information
*/
#define PRINT_INFO_DETAILS 0
/*
- reset 'changed_subformula' only if it is necessary
- UNKNOWN: might slow down ATPG, possibly has no positive effects?
*/
#define KEEP_CHANGED_SUBFORMULA 0
/*
- enable skipping of non-promising fault nodes in optimizations
- could save needless propagations but also miss redundancies / implications
- use with care
- observation: enabling all four causes largest number of
redundancies/implications to be detected but incurs time/space overhead
-> need to tune propagation limits
*/
#define ATPG_SKIP_FAULT_NODES_NO_LIT_CHILDREN 1
#define ATPG_SKIP_FAULT_NODES_OP_CHILDREN 1
#define GLOBAL_FLOW_SKIP_FAULT_NODES_NO_LIT_CHILDREN 1
#define GLOBAL_FLOW_SKIP_FAULT_NODES_OP_CHILDREN 1
/*
- BUGGY: IF ENABLED, ASSUMPTIONS FOR RELINKING IMPLICANTS RATHER
THAN COPYING D O N O T HOLD ANY LONGER
*/
#define GLOBAL_FLOW_LITERALS_BOTH_PHASES 0 /* DO NOT ENABLE */
/*
- disabling recommended: will not update watchers assigned and justified nodes
*/
#define ALWAYS_UPDATE_WATCHER 0
/*
- if disabled: non-recursive version using a stack is used
- seems to be slower than recursive version?
*/
#define RECURSIVE_BWD_PROP 0
/*
- remove invalid subformula-occurrences of a variable on the fly
- STILL TO BE TESTED
*/
#define CLEAN_UP_SUBFORMULA_OCC_LIST 1
/*
- disabling recommended: option NDEBUG overules the following
- if enabled: possibly expensive, especially on large instances
*/
#define ASSERT_ALL_ATPG_INFO_RESET 0
#define ASSERT_CHILDREN_ASSIGNMENTS 0
#define collect_faults_marked(node) ((node)->mark2)
#define collect_faults_mark(node) ((node)->mark2 = 1)
#define collect_faults_unmark(node) ((node)->mark2 = 0)
void
mark_fault_node_as_deleted (FaultNode * fault_node)
{
fault_node->deleted = 1;
}
static FaultNode *
create_fault_node (ATPGRedundancyRemover * atpg_rr, Node * node)
{
size_t bytes = sizeof (FaultNode);
FaultNode *result = (FaultNode *) mem_malloc (atpg_rr->mm, bytes);
memset (result, 0, bytes);
result->node = node;
return result;
}
static void
delete_fault_node (ATPGRedundancyRemover * atpg_rr, FaultNode * fault_node)
{
mem_free (atpg_rr->mm, fault_node, sizeof (FaultNode));
}
ATPGRedundancyRemover *
create_atpg_redundancy_remover (MemManager *mm)
{
ATPGRedundancyRemover *result;
size_t bytes = sizeof (ATPGRedundancyRemover);
result = (ATPGRedundancyRemover *) mem_malloc (mm, bytes);
assert (result);
memset (result, 0, bytes);
result->mm = mm;
result->subformula_vars = create_stack (mm, DEFAULT_STACK_SIZE);
result->fault_queue = create_queue (mm, DEFAULT_QUEUE_SIZE);
result->propagation_queue = create_queue (mm, DEFAULT_QUEUE_SIZE);
result->touched_nodes = create_stack (mm, DEFAULT_STACK_SIZE);
result->bwd_prop_stack = create_stack (mm, DEFAULT_STACK_SIZE);
result->fault_path_nodes = create_stack (mm, DEFAULT_STACK_SIZE);
result->propagated_vars = create_stack (mm, DEFAULT_STACK_SIZE);
return result;
}
static void
reset_atpg_redundancy_remover (ATPGRedundancyRemover * atpg_rr)
{
reset_queue (atpg_rr->fault_queue);
reset_queue (atpg_rr->propagation_queue);
atpg_rr->conflict =
atpg_rr->prop_cutoff =
atpg_rr->global_flow_prop_cutoff =
atpg_rr->atpg_prop_cutoff = atpg_rr->restricted_clean_up = 0;
atpg_rr->collect_faults = 0;
memset (&(atpg_rr->stats), 0, sizeof (atpg_rr->stats));
atpg_rr->global_flow_fwd_prop_cnt =
atpg_rr->global_flow_bwd_prop_cnt =
atpg_rr->atpg_fwd_prop_cnt = atpg_rr->atpg_bwd_prop_cnt = 0;
reset_stack (atpg_rr->touched_nodes);
reset_stack (atpg_rr->fault_path_nodes);
reset_stack (atpg_rr->bwd_prop_stack);
reset_stack (atpg_rr->propagated_vars);
ATPGInfo *atpg_info_p;
#ifndef NDEBUG
ATPGInfo *end = atpg_rr->end_atpg_info;
#endif
assert (atpg_rr->end_atpg_info == atpg_rr->atpg_info_array +
atpg_rr->byte_size_atpg_info_array / sizeof (ATPGInfo));
for (atpg_info_p = atpg_rr->atpg_info_array;
atpg_info_p->fault_node; atpg_info_p++)
{
assert (atpg_info_p < end);
FaultNode *fault_node = atpg_info_p->fault_node;
/* reset atpg_info-pointer in graph nodes */
if (!fault_node->deleted)
{
Node *node = fault_node->node;
node->atpg_info = 0;
}
if (atpg_info_p->atpg_ch)
delete_stack (atpg_rr->mm, atpg_info_p->atpg_ch);
delete_fault_node (atpg_rr, fault_node);
} /* end: for */
assert (atpg_info_p == atpg_rr->cur_atpg_info);
mem_free (atpg_rr->mm, atpg_rr->atpg_info_array, atpg_rr->byte_size_atpg_info_array);
atpg_rr->atpg_info_array = 0;
atpg_rr->cur_atpg_info = 0;
atpg_rr->end_atpg_info = 0;
atpg_rr->byte_size_atpg_info_array = 0;
/* check and reset subformula-variables - could merge with loop before */
void **var_p;
for (var_p = atpg_rr->subformula_vars->elems;
var_p < atpg_rr->subformula_vars->top; var_p++)
{
Var *var = (Var *) * var_p;
var_unassign (var);
var->atpg_mark = 0;
delete_stack (atpg_rr->mm, var->subformula_pos_occs);
var->subformula_pos_occs = 0;
delete_stack (atpg_rr->mm, var->subformula_neg_occs);
var->subformula_neg_occs = 0;
} /* end: for all variables in subformula */
reset_stack (atpg_rr->subformula_vars);
atpg_rr->global_atpg_test_node_mark = 0;
}
void
free_atpg_redundancy_remover (ATPGRedundancyRemover * atpg_rr)
{
delete_stack (atpg_rr->mm, atpg_rr->subformula_vars);
delete_queue (atpg_rr->mm, atpg_rr->fault_queue);
delete_queue (atpg_rr->mm, atpg_rr->propagation_queue);
delete_stack (atpg_rr->mm, atpg_rr->touched_nodes);
delete_stack (atpg_rr->mm, atpg_rr->bwd_prop_stack);
delete_stack (atpg_rr->mm, atpg_rr->fault_path_nodes);
delete_stack (atpg_rr->mm, atpg_rr->propagated_vars);
assert (!atpg_rr->atpg_info_array);
mem_free (atpg_rr->mm, atpg_rr, sizeof (ATPGRedundancyRemover));
}
/*
- each node in "changed-subformula" gets a pointer to an ATPGInfo-structure
- watchers are assigned to operator nodes
- special case: not all children of a node occur in "changed-subformula"
-> must maintain list of watchers
*/
static void
assign_node_atpg_info (ATPGRedundancyRemover * atpg_rr, Node * new_node)
{
assert (atpg_rr->end_atpg_info ==
(atpg_rr->atpg_info_array +
atpg_rr->byte_size_atpg_info_array / sizeof (ATPGInfo)));
if (atpg_rr->cur_atpg_info == atpg_rr->end_atpg_info - 1)
{ /* could (should NOT) happen */
fprintf (stderr, "We have run out of ATPGInfo pointers...\n");
exit (1);
}
assert (!new_node->atpg_info);
new_node->atpg_info = atpg_rr->cur_atpg_info++;
assert (!new_node->atpg_info->fault_node);
new_node->atpg_info->fault_node = create_fault_node (atpg_rr, new_node);
if (!is_literal_node (new_node))
{ /* average case: assign watchers for operator nodes */
new_node->atpg_info->watcher = new_node->child_list.first;
new_node->atpg_info->unassigned_ch_cnt = new_node->num_children;
assert (!new_node->atpg_info->atpg_ch);
}
}
/*
- visit all nodes of subformula rooted at 'root'
- assign pointer to ATPGInfo
- collect var-occs
*/
static void
init_subformula_atpg_info (Nenofex * nenofex)
{
Node *root = nenofex->changed_subformula.lca;
ATPGRedundancyRemover *atpg_rr = nenofex->atpg_rr;
assert (root);
assert (!is_literal_node (root));
assert (!root->atpg_info);
assign_node_atpg_info (atpg_rr, root);
ATPGInfo *root_atpg_info = root->atpg_info;
Stack *stack = create_stack (atpg_rr->mm, DEFAULT_STACK_SIZE);
Node **ch, *child;
for (ch = nenofex->changed_subformula.children; (child = *ch); ch++)
push_stack (atpg_rr->mm, stack, child);
if (count_stack (stack) < root->num_children)
{ /* not all children participate in global-flow/atpg -> collect on stack */
root->atpg_info->atpg_ch = create_stack (atpg_rr->mm, DEFAULT_STACK_SIZE);
}
Node *cur;
while ((cur = pop_stack (stack)))
{
assert (!cur->atpg_info);
assign_node_atpg_info (atpg_rr, cur);
/* NOTE: this could/should be done outside while-loop -> wasting check */
if (root_atpg_info->atpg_ch && cur->parent == root)
{
push_stack (atpg_rr->mm, root_atpg_info->atpg_ch, cur->atpg_info->fault_node);
}
if (!is_literal_node (cur))
{
Node *ch;
for (ch = cur->child_list.last; ch; ch = ch->level_link.prev)
push_stack (atpg_rr->mm, stack, ch);
}
else
{
Var *var = cur->lit->var;
assert (!var_assigned (var));
if (!var->atpg_mark)
{ /* collect var */
var->atpg_mark = 1;
push_stack (atpg_rr->mm, atpg_rr->subformula_vars, var);
assert (!var->subformula_pos_occs);
var->subformula_pos_occs = create_stack (atpg_rr->mm, DEFAULT_STACK_SIZE);
assert (!var->subformula_neg_occs);
var->subformula_neg_occs = create_stack (atpg_rr->mm, DEFAULT_STACK_SIZE);
}
/* need to collect proxy occs, because must check for deleted occs */
if (cur->lit->negated)
push_stack (atpg_rr->mm, var->subformula_neg_occs, cur->atpg_info->fault_node);
else
push_stack (atpg_rr->mm, var->subformula_pos_occs, cur->atpg_info->fault_node);
}
} /* end: while stack not empty */
assert (atpg_rr->cur_atpg_info <= atpg_rr->end_atpg_info);
if (root_atpg_info->atpg_ch)
{
root_atpg_info->watcher_pos = root_atpg_info->atpg_ch->elems;
root_atpg_info->watcher =
((FaultNode *) * root_atpg_info->watcher_pos)->node;
root_atpg_info->unassigned_ch_cnt =
count_stack (root_atpg_info->atpg_ch);
}
else
{
assert (root_atpg_info->watcher == root->child_list.first);
assert (root_atpg_info->unassigned_ch_cnt == root->num_children);
}
delete_stack (atpg_rr->mm, stack);
}
/*
- visit "changed-subformula" and enqueue nodes
*/
static void
collect_fault_nodes_by_dfs (Nenofex * nenofex)
{
Node *root = nenofex->changed_subformula.lca;
ATPGRedundancyRemover *atpg_rr = nenofex->atpg_rr;
Queue *atpg_rr_fault_queue = atpg_rr->fault_queue;
assert (root->atpg_info);
assert (root->atpg_info->fault_node);
Stack *stack = create_stack (atpg_rr->mm, DEFAULT_STACK_SIZE);
collect_faults_mark (root);
push_stack (atpg_rr->mm, stack, root);
Node **ch, *child;
for (ch = nenofex->changed_subformula.children; (child = *ch); ch++)
push_stack (atpg_rr->mm, stack, child);
Node *cur;
while ((cur = pop_stack (stack)))
{
assert (cur->atpg_info);
assert (cur->atpg_info->fault_node);
if (is_literal_node (cur))
enqueue (atpg_rr->mm, atpg_rr_fault_queue, cur->atpg_info->fault_node);
else
{
if (collect_faults_marked (cur))
{
collect_faults_unmark (cur);
enqueue (atpg_rr->mm, atpg_rr_fault_queue, cur->atpg_info->fault_node);
}
else /* mark and visit children */
{
collect_faults_mark (cur);
push_stack (atpg_rr->mm, stack, cur);
Node *ch;
for (ch = cur->child_list.last; ch; ch = ch->level_link.prev)
push_stack (atpg_rr->mm, stack, ch);
}
}
} /* end: while stack not empty */
delete_stack (atpg_rr->mm, stack);
}
/*
- visit "changed-subformula" and enqueue nodes
*/
static void
collect_fault_nodes_by_bfs (Nenofex * nenofex)
{
Node *root = nenofex->changed_subformula.lca;
ATPGRedundancyRemover *atpg_rr = nenofex->atpg_rr;
Queue *atpg_rr_fault_queue = atpg_rr->fault_queue;
assert (root->atpg_info);
assert (root->atpg_info->fault_node);
Queue *queue = create_queue (atpg_rr->mm, DEFAULT_QUEUE_SIZE);
enqueue (atpg_rr->mm, atpg_rr_fault_queue, root->atpg_info->fault_node);
Node **ch, *child;
for (ch = nenofex->changed_subformula.children; (child = *ch); ch++)
enqueue (atpg_rr->mm, queue, child);
Node *cur;
while ((cur = dequeue (queue)))
{
assert (cur->atpg_info);
assert (cur->atpg_info->fault_node);
enqueue (atpg_rr->mm, atpg_rr_fault_queue, cur->atpg_info->fault_node);
if (!is_literal_node (cur))
{
Node *ch;
for (ch = cur->child_list.first; ch; ch = ch->level_link.next)
enqueue (atpg_rr->mm, queue, ch);
}
} /* end: while queue not empty */
delete_queue (atpg_rr->mm, queue);
}
/*
- visit "changed-subformula" and enqueue nodes
- enqueue literals first then parents in reverse bfs-order
*/
static void
collect_fault_nodes_bottom_up (Nenofex * nenofex)
{
Node *root = nenofex->changed_subformula.lca;
ATPGRedundancyRemover *atpg_rr = nenofex->atpg_rr;
Queue *atpg_rr_fault_queue = atpg_rr->fault_queue;
assert (root->atpg_info);
assert (root->atpg_info->fault_node);
Queue *queue = create_queue (atpg_rr->mm, DEFAULT_QUEUE_SIZE);
Stack *fault_stack = create_stack (atpg_rr->mm, DEFAULT_STACK_SIZE);
push_stack (atpg_rr->mm, fault_stack, root->atpg_info->fault_node);
Node **ch, *child;
for (ch = nenofex->changed_subformula.children; (child = *ch); ch++)
enqueue (atpg_rr->mm, queue, child);
assert (count_queue (queue) >= 2);
Node *cur;
while ((cur = dequeue (queue)))
{
assert (cur->atpg_info);
assert (cur->atpg_info->fault_node);
if (!is_literal_node (cur))
{
push_stack (atpg_rr->mm, fault_stack, cur->atpg_info->fault_node);
Node *ch;
for (ch = cur->child_list.first; ch; ch = ch->level_link.next)
enqueue (atpg_rr->mm, queue, ch);
}
else
{
enqueue (atpg_rr->mm, atpg_rr_fault_queue, cur->atpg_info->fault_node);
}
} /* end: while queue not empty */
while ((cur = pop_stack (fault_stack)))
enqueue (atpg_rr->mm, atpg_rr_fault_queue, cur);
delete_queue (atpg_rr->mm, queue);
delete_stack (atpg_rr->mm, fault_stack);
}
/*
- collected nodes wil be reset after propagation
*/
void
collect_assigned_node (ATPGRedundancyRemover * atpg_rr, Node * node)
{
ATPGInfo *atpg_info = node->atpg_info;
if (!atpg_info->collected)
{
atpg_info->collected = 1;
push_stack (atpg_rr->mm, atpg_rr->touched_nodes, atpg_info);
}
}
/*
- delete invalid nodes from watcher list
- called on demand
*/
static void
clean_up_watcher_atpg_child_list (ATPGInfo * atpg_info)
{
assert (atpg_info->atpg_ch);
assert (atpg_info->watcher_pos);
void **elems = atpg_info->atpg_ch->elems;
void **end = atpg_info->atpg_ch->top;
/* traverse stack and remove all invalid nodes
by copying last node to current position */
void **cur = elems;
while (cur < end)
{
FaultNode *fault_node = *cur;
if (fault_node->deleted)
{
if (cur == end - 1)
{
end--; /* cut off last element */
}
else
{
assert (cur >= elems && cur <= end - 2);
end--;
*cur = *end;
continue;
}
} /* end: if fault node deleted */
cur++;
} /* end: for all nodes on stack */
atpg_info->atpg_ch->top = end;
#ifndef NDEBUG
void **v;
for (v = atpg_info->atpg_ch->elems; v < atpg_info->atpg_ch->top; v++)
{
FaultNode *cur = *v;
assert (!cur->deleted);
}
#endif
}
static void init_counter_and_watcher (Node * node);
/*
- TODO: warning if only 1 unassigned child after initialization -> unclear...
*/
static void
reset_touched_nodes (ATPGRedundancyRemover * atpg_rr)
{
ATPGInfo *atpg_info;
Stack *atpg_rr_touched_nodes = atpg_rr->touched_nodes;
while ((atpg_info = pop_stack (atpg_rr_touched_nodes)))
{
assert (atpg_info->collected);
FaultNode *fault_node = atpg_info->fault_node;
if (fault_node->deleted)
continue;
atpg_info->collected = 0;
Node *node = fault_node->node;
if (node_assigned (node))
{
atpg_info->assignment = 0;
atpg_info->justified = 0;
if (!is_literal_node (node))
{ /* init watchers */
init_counter_and_watcher (node);
}
}
else /* unassigned node -> init watchers */
{
init_counter_and_watcher (node);
} /* end: node not assigned */
/* TODO: warnif only 1 unassigned child after initialization -> unclear... */
} /* end: while stack not empty */
}
/*
- derive var. assignments at fault site -> sensitize the fault
- enqueue any assigned variables for propagation
- BOTTLENECK: skipping of nodes? -> very unlikely since only lit-ch are inspected
*/
static void
fault_sensitization (ATPGRedundancyRemover * atpg_rr,
Node * fault_node, ATPGFaultType fault_type)
{
Queue *atpg_rr_propagation_queue = atpg_rr->propagation_queue;
Var *var;
Lit *lit;
if (fault_type == ATPG_FAULT_TYPE_STUCK_AT_0)
{
assert (is_and_node (fault_node) || is_literal_node (fault_node));
assert (!node_assigned (fault_node));
if (is_literal_node (fault_node))
{
lit = fault_node->lit;
var = lit->var;
assert (!var_assigned (var));
if (lit->negated)
var_assign_false (var);
else
var_assign_true (var);
enqueue (atpg_rr->mm, atpg_rr_propagation_queue, var);
}
else /* set children to 1 */
{
assert (is_and_node (fault_node));
Node *ch;
for (ch = fault_node->child_list.first;
ch && is_literal_node (ch); ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
assert (!node_assigned (ch));
lit = ch->lit;
var = lit->var;
assert (!var_assigned (var));
if (lit->negated)
var_assign_false (var);
else
var_assign_true (var);
enqueue (atpg_rr->mm, atpg_rr_propagation_queue, var);
} /* end: for all literal-children */
} /* end: set children to 1 */
}
else /* FAULT STUCK-AT-1 */
{
assert (fault_type == ATPG_FAULT_TYPE_STUCK_AT_1);
assert (is_or_node (fault_node) || is_literal_node (fault_node));
assert (!node_assigned (fault_node));
if (is_literal_node (fault_node))
{
lit = fault_node->lit;
var = lit->var;
assert (!var_assigned (var));
if (lit->negated)
var_assign_true (var);
else
var_assign_false (var);
enqueue (atpg_rr->mm, atpg_rr_propagation_queue, var);
}
else /* set children to 0 */
{
assert (is_or_node (fault_node));
Node *ch;
for (ch = fault_node->child_list.first;
ch && is_literal_node (ch); ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
assert (!node_assigned (ch));
lit = ch->lit;
var = lit->var;
assert (!var_assigned (var));
if (lit->negated)
var_assign_true (var);
else
var_assign_false (var);
enqueue (atpg_rr->mm, atpg_rr_propagation_queue, var);
} /* end: for all literal-children */
} /* end: set children to 0 */
} /* end: stuck-at-1 */
}
#ifndef NDEBUG
/*
- the following functions are used in assertion-checking only
*/
static int
all_children_assigned_value (Node * parent, ATPGAssignment v)
{
int result = 1;
switch (v)
{
case ATPG_ASSIGNMENT_UNDEFINED:
{
Node *ch;
for (ch = parent->child_list.first; result && ch;
ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
result = !node_assigned (ch);
}
}
break;
case ATPG_ASSIGNMENT_FALSE:
{
Node *ch;
for (ch = parent->child_list.first; result && ch;
ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
result = node_assigned_false (ch);
}
}
break;
case ATPG_ASSIGNMENT_TRUE:
{
Node *ch;
for (ch = parent->child_list.first; result && ch;
ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
result = node_assigned_true (ch);
}
}
break;
}
return result;
}
static int
count_children_assigned_value (Node * parent, ATPGAssignment v)
{
int result = 0;
switch (v)
{
case ATPG_ASSIGNMENT_UNDEFINED:
{
Node *ch;
for (ch = parent->child_list.first; ch; ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
if (!node_assigned (ch))
result++;
}
}
break;
case ATPG_ASSIGNMENT_FALSE:
{
Node *ch;
for (ch = parent->child_list.first; ch; ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
if (node_assigned_false (ch))
result++;
}
}
break;
case ATPG_ASSIGNMENT_TRUE:
{
Node *ch;
for (ch = parent->child_list.first; ch; ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
if (node_assigned_true (ch))
result++;
}
}
break;
}
return result;
}
static Node *
find_child_assigned_value (Node * parent, ATPGAssignment v)
{
Node *result = 0;
switch (v)
{
case ATPG_ASSIGNMENT_UNDEFINED:
{
Node *ch;
for (ch = parent->child_list.first; !result && ch;
ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
if (!node_assigned (ch))
result = ch;
}
}
break;
case ATPG_ASSIGNMENT_FALSE:
{
Node *ch;
for (ch = parent->child_list.first; !result && ch;
ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
if (node_assigned_false (ch))
result = ch;
}
}
break;
case ATPG_ASSIGNMENT_TRUE:
{
Node *ch;
for (ch = parent->child_list.first; !result && ch;
ch = ch->level_link.next)
{
/* atpg restriction */
if (!ch->atpg_info)
continue;
/* end: atpg restriction */
if (node_assigned_true (ch))
result = ch;
}
}
break;
}
return result;
}
#endif /* end: ifndef NDEBUG */
/*
- current watcher has been assigned
- special case: searches for unassigned child in watcher list
- for nodes where not all children participate in optimizations
- watcher update by real child list could become bottleneck for such nodes
*/
static void
update_watcher_by_watcher_list (Node * parent)
{
ATPGInfo *atpg_info = parent->atpg_info;
Node *watcher = atpg_info->watcher;
assert (atpg_info->atpg_ch);
assert (atpg_info->watcher_pos);
assert (((FaultNode *) * atpg_info->watcher_pos)->node == watcher);
if (node_assigned (watcher))
{
void **cur = atpg_info->watcher_pos + 1;
void **end = atpg_info->atpg_ch->top;
assert (cur > atpg_info->watcher_pos && cur <= atpg_info->atpg_ch->top);
while (cur < end)
{
watcher = ((FaultNode *) * cur)->node;
if (!node_assigned (watcher))
break;
cur++;
} /* end: while */
if (cur == end)
{ /* no unassigned child found */
atpg_info->watcher = 0;
atpg_info->watcher_pos = end;
}
else
{
atpg_info->watcher = watcher;
atpg_info->watcher_pos = cur;
}
}
}
/*
- current watcher has been assigned
- average case: searches for unassigned child in child list
*/
static void
update_watcher_by_child_list (Node * parent)
{
ATPGInfo *atpg_info = parent->atpg_info;
Node *watcher = atpg_info->watcher;
assert (!atpg_info->atpg_ch);
assert (!atpg_info->watcher_pos);
if (node_assigned (watcher))
{
do
{
watcher = watcher->level_link.next;
}
while (watcher && node_assigned (watcher));
atpg_info->watcher = watcher;
}
}