tclquadcode

Check-in [dc90afa15d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Finish forward analysis in the jump threading pass.
Timelines: family | ancestors | descendants | both | notworking | kbk-jumpthread
Files: files | file ages | folders
SHA3-256:dc90afa15da03698d19ea19488876d2b769ee8d745fca28d56252a45fc75fdd3
User & Date: kbk 2018-12-16 04:43:56
Context
2018-12-16
04:55
Add a 'cos2' test case to illustrate the cost of non-numeric ordering comparisons check-in: 86167d6917 user: kbk tags: notworking, kbk-jumpthread
04:43
Finish forward analysis in the jump threading pass. check-in: dc90afa15d user: kbk tags: notworking, kbk-jumpthread
2018-12-10
04:51
Remove vestiges of the old node splitter from 'inline.tcl'. Make console dribble in 'jumpthread.tcl' contingent on debug-jumpthread. check-in: fd2ea3e6f1 user: kbk tags: notworking, kbk-jumpthread
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to quadcode/jumpthread.tcl.

159
160
161
162
163
164
165
166
167
168












169
170
171
172
173
174
175
...
239
240
241
242
243
244
245
246
247



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
...
349
350
351
352
353
354
355
356
357

358
359


360
361

362


363
364
365
366
367
368
369
370
...
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
...
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
    # block.

    my jt_backward

    # Identify which subsets of the conditions are reachable on specific
    # control flow paths, so that blocks can be replicated to have known
    # entry conditions. Also report the (up to two) successors for each
    # variant block

    my jt_forward













    # TODO: Once all the variants have been listed, if any block has more than
    #       one variant, deconstruct SSA.  Replicate the blocks into variants,
    #       redirecting their exits as needed (and tracking preds). Sort the
    #       blocks.  Reconstruct SSA, solve ud- and du-chains, propagate
    #       copies, remove unreachable code, and recalculate bbidom/bblevel.
    #	    (May want to inspect the result to see whether another try at
................................................................................
# Side effects:
#
#	Constructs the list, jt_antin, indexed by basic block number,
#	containing dictionaries.  The dictionaries describe the conditions
#	that will inform jump threading downstream of the entry to the basic
#	blocks. The dictionaries have two levels. The first level key gives
#	the name of a value in the quadcode, and the second gives a condition
#	on that value's type.  It is a combination of
#	




oo::define quadcode::transformer method jt_backward {} {

    namespace upvar ::quadcode jt_removable jt_removable

    my variable jt_antin


    set jt_antin [lrepeat [llength $bbcontent] {}]

    set changed 1
    while {$changed} {
        set changed 0

        my debug-jumpthread {
................................................................................
                        }
                        dict unset antin $dest
                    }
                }
            }

            if {$antin ne [lindex $jt_antin $b]} {
                lset jt_antin $b $antin
                set changed 1

                my debug-jumpthread {
                    puts "  $b: anticipable conditions:"


                    dict for {v conds} $antin {
                        dict for {c -} $conds {

                            lassign $c what type


                            puts "    $v $what $type ([nameOfType $type])"
                        }
                    }
                }
            }
        }
    }

................................................................................
#	in basic blocks prior to attempting to split them.
#
# Results:
#	None.
#
# Side effects:
#
#	A list of dictionaries, 'jt_variants' is created. The list is
#	indexed by basic block number. The list members are dictionaries,
#	whose keys are bit vectors identifying which of the anticipibale
#	conditions is true on entry to the block, and whose values are
#	immaterial on return from this procedure.



oo::define quadcode::transformer method jt_forward {} {

    # jt_stack is a list of alternating basic block number and
    # condition bit vector, used to track work that still needs to be
    # done.
    my variable jt_stack
    my variable jt_variants





    # Initially, the work list contains just the entry node, and
    # nothing is known on entry to it (there shouldn't
    # be any anticipated conditions there!)
    set jt_stack [list 0 0]
    set jt_variants [lrepeat [llength $bbcontent] {}]
    lset jt_variants [dict create 0 -1]
    
    # Pop entries off the worklist and process them

    while {[llength $jt_stack] > 0} {
        set b [lindex $jt_stack end-1]
        set condMask [lindex $jt_stack end]
        set jt_stack [lreplace $jt_stack[set jt_stack ""] end-1 end]
................................................................................
#	For each successor to the block, propagates the promised
#	conditions forward into the successor. If the successor has
#	a set of conditions that has not yet been visited, adds it
#	to 'jt_variants' and stacks it for processing.

oo::define quadcode::transformer method jt_forward_worker {b mask} {

    my variable jt_antin
    set antin [lindex $jt_antin $b]
    set mask_expanded {}

    my debug-jumpthread {
        puts "  bb $b"
    }


    # Expand the mask to the list of assertions.
    




























































































































































































































































































    set asserted {}





    set bit 1
    dict for {v conds} $antin {
        dicr for {c -} $conds {




            if {$mask & $bit} {
                dict set asserted $v $c {}
            }
            set bit [expr {$bit << 1}]
        }
    }

    my debug-jumpthread {
        puts "    asserted on entry:"

        dict for {v conds} $asserted {
            dict for {c -} $conds {

                puts "        $v $c"
            }
        }
    }
    
    # Walk through the quads of the block, applying the assertions
    # to the types of the results, accumulating a private set of
    # types.


    set localtypes {}
    set bb [lindex $bbcontent $b]
    set pc -1
    foreach q $bb {
        incr pc

        lassign $q opcode result operand1
        set op [lindex $opcode 0]

        
        # Disregard quads that do not yield a result
        if {[lindex $result 0] ni {"temp" "var"}} {
            continue
        }



        # Narrow the result type to conform with any assertions.
        set ty [::quadcode::typeOfOperand $types $result]
        if {$op eq "copy" && [dict exists $asserted $operand1]} {
            dict set asserted $result [dict get $asserted $operand1]














        }
        if {[dict exists $asserted $result]} {
            set ty [my jt_applyAssertions $ty [dict get $asserted $result]]

        }


        my debug-jt {
            puts "    type of $result is [format %#x $ty]\
                      ([quadcode::nameOfType $ty])"

        }
        dict set localtypes $result $ty






    }






    # TODO - Look at and phi-translate the ANTIN for each successor block.
    #        Compare against first localtypes, then typeOfOperand, to
    #	     get the type of the input. Find out which constraints the
    #        computed type satisfies, and make a mask for them. Queue the
    #        correct variant of the successor block.
    
    

}
 
# quadcode::transformer method jt_applyAssertions --
#
#	Apply assertions about a value to its type descriptor to produce
#	a narrowed type
#
# Parameters:
#	ty - Type to narrow
#	as - Assertions to apply





#
# Results:
#	Returns the narrowed type.

oo::define quadcode::transformer method jt_applyAssertions {ty as} {

    namespace upvar quadcode::dataTypes \
        ARRAY ARRAY FAIL FAIL IMPURE IMPURE NEXIST NEXIST

    dict for {c -} $as {
        lassign $c kind type
        switch -exact -- $kind {
            is {
                set mask $type
            }
            isnot {
                set mask [quadcode::dataTypes::allbut $type]
            }
        }
        if {$type & ~($ARRAY | $FAIL | $IMPURE | $NEXIST)} {
            set mask [expr {$mask &~ $IMPURE}]
        }
        set ty [expr {$ty & $mask}]
            
    }

    return $ty























































































































































































































}
 
# quadcode::transformer method jt_cleanup --
#
#	Cleans up working storage after the jump threading pass.
#
# Results:







|


>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
>
>
>






<
<







 







<

>

|
>
>
|
<
>
|
>
>
|







 







|
|
|
|
|
>
>








>
>
>
>






|







 







|
|
<


|


>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
|

|
>
>
>
>
|
|

<


>

|
>


>
|



|
<
<
<
>
|
<
<
<
<
<
|
<
<
>
|
<
<
<
<
>
>
|
<
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
>
|
>

|
<
<
>
|
<

>
>
>
>
>
|
>
>
>
>
>
|
<
<
<
<
<
|
|
>









|
>
>
>
>
>






|









|



|






|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
...
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
...
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
...
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
...
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
    # block.

    my jt_backward

    # Identify which subsets of the conditions are reachable on specific
    # control flow paths, so that blocks can be replicated to have known
    # entry conditions. Also report the (up to two) successors for each
    # variant block.

    my jt_forward

    my debug-jumpthread {
        my variable jt_variants
        puts "Variants found:"
        set b -1
        foreach vs $jt_variants {
            incr b
            dict for {m ss} $vs {
                puts [format "  %d (%llx): %s" $b $m $ss]
            }
        }
    }

    # TODO: Once all the variants have been listed, if any block has more than
    #       one variant, deconstruct SSA.  Replicate the blocks into variants,
    #       redirecting their exits as needed (and tracking preds). Sort the
    #       blocks.  Reconstruct SSA, solve ud- and du-chains, propagate
    #       copies, remove unreachable code, and recalculate bbidom/bblevel.
    #	    (May want to inspect the result to see whether another try at
................................................................................
# Side effects:
#
#	Constructs the list, jt_antin, indexed by basic block number,
#	containing dictionaries.  The dictionaries describe the conditions
#	that will inform jump threading downstream of the entry to the basic
#	blocks. The dictionaries have two levels. The first level key gives
#	the name of a value in the quadcode, and the second gives a condition
#	on that value's type.  The second key is either 'is TYPECONST'
#	or 'isnot TYPECONST' for a given value in quadcode::dataTypes.
#	The values in the dictionary are ordinal numbers of conditions in the
#	block, and will be used to construct bit vectors of what conditions
#	are satisfied in a copy of the block.

oo::define quadcode::transformer method jt_backward {} {

    namespace upvar ::quadcode jt_removable jt_removable

    my variable jt_antin


    set jt_antin [lrepeat [llength $bbcontent] {}]

    set changed 1
    while {$changed} {
        set changed 0

        my debug-jumpthread {
................................................................................
                        }
                        dict unset antin $dest
                    }
                }
            }

            if {$antin ne [lindex $jt_antin $b]} {

                set changed 1
                lset jt_antin $b $antin
                my debug-jumpthread {
                    puts "  bb $b: anticipable conditions:"
                }
                set cn -1
                dict for {v conds} $antin {

                    foreach c [dict keys $conds] {
                        lassign $c what type
                        my debug-jumpthread {
                            puts "    [incr cn]: $v $what $type\
                                      ([nameOfType $type])"
                        }
                    }
                }
            }
        }
    }

................................................................................
#	in basic blocks prior to attempting to split them.
#
# Results:
#	None.
#
# Side effects:
#
#	The ultimate output of this procedure is a list, 'jt_variants',
#	with one entry per basic block of the original sequence. The
#	elements of the list are dictionaries whose keys are bit vectors
#	that specify the set of anticipated conditions that are satisfied
#	in a desired copy of the block. The values are lists of up to two
#	bit vectors, that give the sets of anticipated conditions that
#	are satisfied in the block's successors.

oo::define quadcode::transformer method jt_forward {} {

    # jt_stack is a list of alternating basic block number and
    # condition bit vector, used to track work that still needs to be
    # done.
    my variable jt_stack
    my variable jt_variants

    my debug-jumpthread {
        puts "Begin forward jump threading analysis for [my full-name]"
    }

    # Initially, the work list contains just the entry node, and
    # nothing is known on entry to it (there shouldn't
    # be any anticipated conditions there!)
    set jt_stack [list 0 0]
    set jt_variants [lrepeat [llength $bbcontent] {}]
    lset jt_variants 0 {0 {}}
    
    # Pop entries off the worklist and process them

    while {[llength $jt_stack] > 0} {
        set b [lindex $jt_stack end-1]
        set condMask [lindex $jt_stack end]
        set jt_stack [lreplace $jt_stack[set jt_stack ""] end-1 end]
................................................................................
#	For each successor to the block, propagates the promised
#	conditions forward into the successor. If the successor has
#	a set of conditions that has not yet been visited, adds it
#	to 'jt_variants' and stacks it for processing.

oo::define quadcode::transformer method jt_forward_worker {b mask} {

    namespace upvar ::quadcode::dataType ARRAY ARRAY CONST0 CONST0 \
        CONST1 CONST1 IMPURE IMPURE NEXIST NEXIST


    my debug-jumpthread {
        puts "  bb $b:"
    }

    # Find out what assertions are guaranteed by $mask
    set asserted [my jt_maskToAssertions $b $mask]

    # Narrow the types of variables flowing into the block, according
    # to the assertions.
    set localtypes [my jt_applyAllAssertions $asserted]

    # Analyze the instructions of the block in the forward direction.
    # If an instruction is one that participates in jump threading, update
    # the assertions that apply to the block accordingly. If the instruction
    # is a threaded jump, set up to process the block's successor(s).
    
    set bb [lindex $bbcontent $b]
    set pc -1
    foreach q $bb {
        incr pc

        my debug-jumpthread {
            puts "    $pc: $q"
        }

        lassign $q opcode result operand1
        lassign $opcode op totype typename
        set fromtype [my jt_localtype $operand1 $localtypes]

        switch -exact -- $op {

            "arrayExists" {
                if {[quadcode::dataType::isa $fromtype $ARRAY]} {
                    dict set localtypes $result $CONST1
                } elseif {![quadcode::dataType::mightbea $fromtype $ARRAY]} {
                    dict set localtypes $result $CONST0
                }
            }

            "copy" {
                dict set localtypes $result $fromtype
            }

            "exists" {
                if {$fromtype == $NEXIST} {
                    dict set localtypes $result $CONST0
                } elseif {!($fromtype & $NEXIST)} {
                    dict set localtypes $result $CONST1
                }
            }

            "instanceOf" {
                if {[quadcode::dataType::isa $fromtype $totype]} {
                    dict set localtypes $result $CONST1
                } elseif {![quadcode::dataType::mightbea $fromtype $totype]} {
                    dict set localtypes $result $CONST0
                }
            }
            
            "jump" {
                my jt_processSuccessor $b $mask \
                    [lindex $result 1] $localtypes
                break
            }

            "jumpFalse" {
                set falsebranch [lindex $result 1]
                set truebranch [lindex $bb end 1 1]
                my jt_processCondBranch $b $mask \
                    $fromtype $truebranch $falsebranch $localtypes
                break
            }

            "jumpTrue" {
                set truebranch [lindex $result 1]
                set falsebranch [lindex $bb end 1 1]
                my jt_processCondBranch $b $mask \
                    $fromtype $truebranch $falsebranch $localtypes
                break
            }

            "jumpMaybe" {
                set failbranch [lindex $result 1]
                set okbranch [lindex $bb end 1 1]
                my jt_processJumpMaybe $b $mask \
                    $fromtype $failbranch $okbranch $localtypes
                break
            }

            "narrowToType" {
                dict set localtypes $result [expr {$fromtype & $totype}]
            }

            "purify" {
                dict set localtypes $result [expr {$fromtype & ~$IMPURE}]
            }

            default {
                # do nothing
            }
        }
    }

    return
}
 
# quadcode::transformer method jt_processCondBranch --
#
#	Updates the state when jump threading encounters a conditional
#	branch.
#
# Parameters:
#	b - Predecessor block
#	mask - Bit vector identifying assertions satisfied in the predecessor
#	otype - Type of the conditional jump operand
#	truebranch - Successor block if the operand is true
#	falsebranch - Successor block if the operand is false
#	localtypes - Dictionary giving types of variables that are
#	             overridden by assertions made in jump threading.
#
# Results:
#	None.
#
# Side effects:
#	One or both successors is added to the successors of the block.

oo::define quadcode::transformer method jt_processCondBranch {b mask otype
                                                              truebranch
                                                              falsebranch
                                                              localtypes} {
    namespace upvar ::quadcode::dataType CONST0 CONST0

    my debug-jumpthread {
        puts "      block $b branches to $truebranch or $falsebranch"
        puts [format "      and might be optimized based on %#llx (%s)" \
                  $otype [nameOfType $otype]]
    }

    # Include the true branch if it represents a possible condition

    if {[quadcode::dataType::isa $otype $CONST0]} {
        my debug-jumpthread {
            puts "        branch to $truebranch cannot be taken"
        }
    } else {
        my jt_processSuccessor $b $mask $truebranch $localtypes
    }

    # Include the false branch if it represents a possible condition

    if {![quadcode::dataType::mightbea $otype $CONST0]} {
        my debug-jumpthread {
            puts "        branch to $falsebranch cannot be taken"
        }
    } else {
        my jt_processSuccessor $b $mask $falsebranch $localtypes
    }

    return
}
 
# quadcode::transformer method jt_processJumpMaybe --
#
#	Updates the state when jump threading encounters a
#	jumpMaybe instruction
#
# Parameters:
#	b - Predecessor block
#	mask - Bit vector identifying assertions satisfied in the predecessor
#	otype - Type of the conditional jump operand
#	failbranch - Successor block if the operand represents a failure
#	okbranch - Successor block if the operand represents a success
#	localtypes - Dictionary giving types of variables that are
#	             overridden by assertions made in jump threading.
#
# Results:
#	None.
#
# Side effects:
#	One or both successors is added to the successors of the block.

oo::define quadcode::transformer method jt_processJumpMaybe {b mask otype
                                                             failbranch
                                                             okbranch
                                                             localtypes} {
    namespace upvar ::quadcode::dataType FAIL FAIL

    my debug-jumpthread {
        puts "      block $b branches to $failbranch or $okbranch"
        puts [format "      and might be optimized based on %#llx (%s)" \
                  $otype [nameOfType $otype]]
    }

    # Include the true branch if it represents a possible condition

    if {[quadcode::dataType::isa $otype $FAIL]} {
        my debug-jumpthread {
            puts "        branch to $okbranch cannot be taken"
        }
    } else {
        my jt_processSuccessor $b $mask $okbranch $localtypes
    }

    # Include the false branch if it represents a possible condition

    if {![quadcode::dataType::mightbea $otype $FAIL]} {
        my debug-jumpthread {
            puts "        branch to $failbranch cannot be taken"
        }
    } else {
        my jt_processSuccessor $b $mask $failbranch $localtypes
    }

    return
}
 
# quadcode::transformer method jt_processSuccessor --
#
#	Updates the state when jump threading handles a jump that
#	proceeds from a given predecessor block to its successor.
#
# Parameters:
#	b - Predecessor block
#	mask - Bit vector identifying assertions satisfied in the predecessor
#	s - Successor block
#	localtypes - Dictionary giving types of variables that are
#	             overridden by assertions made in jump threading.
#
# Results:
#	None.
#
# Side effects:
#	Determines the assertions satisfied in the successor block.
#	Records that set in the predecessor's 'jt_variants' record.
#	If the successor block has not yet been visited, creates a
#	'jt_variants' record for it, and pushes the block onto 'jt_stack'
#	for processing.

oo::define quadcode::transformer method jt_processSuccessor {b mask s
                                                             localtypes} {
    my variable jt_variants
    my variable jt_stack

    # Find the assertion mask for the successor block

    set smask [my jt_assertionsToMask $b $mask $localtypes $s]

    my debug-jumpthread {

        puts [format {  bb %s (%#llx) will be followed by %s (%#llx)} \
                  $b $mask $s $smask]
    }

    # If the successor block has not been seen, we will need subsequently
    # to process it.

    set vs [lindex $jt_variants $s]
    if {![dict exists $vs $smask]} {
        lset jt_variants $s {}
        dict set vs $smask {}
        lset jt_variants $s $vs
        lappend jt_stack $s $smask
    }

    # Record that the successor follows this block

    set vs [lindex $jt_variants $b]
    lset jt_variants $b {}
    dict set vs $mask $s $smask
    lset jt_variants $b $vs

    return
}
 
# quadcode::transformer method jt_maskToAssertions --
#
#	Takes the bitmask corresponding to the conditions asserted at the
#	entry to a basic block, and expands to a full dictionary of assertions
#
# Return value:
#
#	Returns a dictionary var -> {kind type} -> cn
#	where var is the name of a variable
#	      kind is 'is' or 'isnot'
#	      type is the numeric code for a data type
#	      cn is the condition's position in the set of conditions for b

oo::define quadcode::transformer method jt_maskToAssertions {b mask} {

    my variable jt_antin
    set antin [lindex $jt_antin $b]
    set asserted {}

    my debug-jumpthread {
        puts [format "   anticipated on entry to %s (%#llx):" $b $mask]
    }

    set cn -1
    dict for {v conds} $antin {
        dict for {c -} $conds {
            incr cn
            my debug-jumpthread {
                puts "        $cn: $v $c"
            }
            if {$mask & (1 << $cn)} {
                dict set asserted $v $c $cn
            }

        }
    }

    my debug-jumpthread {
        puts "   asserted on entry:"
        set cn -1
        dict for {v conds} $asserted {
            dict for {c -} $conds {
                incr cn
                puts "        $cn: $v $c"
            }
        }
    }




    return $asserted
}





 


# quadcode::transformer method jt_applyAllAssertions --
#




#	Applies all the assertions that are active at the start of a block
#	and creates a dictionary holding narrowed types of variables
#




# Parameters:
#	asserted - Dictionary of the form var->{kind type}->cond#
#		where var is the name of a variable
#		      kind is 'is' or 'isnot'
#		      type is the numeric code for a data type
#	              cond# is an ordinal number of the condition in a
#		            block's list of conditions
#
# Results:
#	Returns a dictionary whose keys are variable names and whose
#	values are type codes of the narrowed data types.
#
# Used to calculate the initial data types of tracked variables on entry
# to a block.



oo::define quadcode:::transformer method jt_applyAllAssertions {asserted} {

    set localtypes {}

    my debug-jumpthread {


        puts "    Type calculated on entry:"
    }


    foreach {v conds} $asserted {
        set globaltype [dict get $types $v]
        my debug-jumpthread {
            puts [format {      %s global type %#llx (%s)} \
                      $v $globaltype [nameOfType $globaltype]]
        }
        set localtype [my jt_applyAssertions $globaltype $conds]
        dict set localtypes $v $localtype
        my debug-jumpthread {
            puts [format {      %s local  type %#llx (%s)} \
                      $v $localtype [nameOfType $localtype]]
        }





    }

    return $localtypes
}
 
# quadcode::transformer method jt_applyAssertions --
#
#	Apply assertions about a value to its type descriptor to produce
#	a narrowed type
#
# Parameters:
#	ty - Type to narrow
#	as - Assertions to apply, expressed as a dictionary:
#		{kind type}->cond#
#		where kind is 'is' or 'isnot'
#      		      type is the numeric code of a data type
#		      cond# is the ordinal number of the condition among
#		            the conditions applicable at the start of a block.
#
# Results:
#	Returns the narrowed type.

oo::define quadcode::transformer method jt_applyAssertions {ty as} {

    namespace upvar ::quadcode::dataType \
        ARRAY ARRAY FAIL FAIL IMPURE IMPURE NEXIST NEXIST

    dict for {c -} $as {
        lassign $c kind type
        switch -exact -- $kind {
            is {
                set mask $type
            }
            isnot {
                set mask [quadcode::dataType::allbut $type]
            }
        }
        if {$type & ~($ARRAY | $FAIL | $IMPURE | $NEXIST)} {
            set mask [expr {$mask | $IMPURE}]
        }
        set ty [expr {$ty & $mask}]
            
    }

    return $ty
}
 
# quadcode::transformer method jt_assertionsToMask
#
#	Given a predecessor block, the types of objects assigned to in the
#	predecessor block, and a mask of anticipated conditions satisfied at
#	the entry to the predecessor block, determines the mask of assertions
#	satisfied at the entry to the successor block.
#
# Parameters:
#	p - Predecessor block
#	pmask - Bit vector specifying the anticipated conditions satisfied
#	        at entry to block $p.
#	ptypes - Dictionary keyed by variable name giving the types of
#	         values assigned in block $p
#	s - Successor block.
#
# Results:
#	Returns a bit vector of anticipated conditions satisfied at entry
#	to block $s.

oo::define quadcode::transformer method jt_assertionsToMask {p pmask ptypes s} {

    my variable jt_antin

    my debug-pre {
        puts "\t    make assertion mask for $p -> $s"
    }

    set smask 0
    set cn -1
    dict for {v conds} [lindex $jt_antin $s] {
        dict for {c -} $conds {
            incr cn
            if {[my jt_test_assertion $p $pmask $ptypes $s $v $c]} {
                my debug-pre {
                    puts "\t      include $v $c"
                }
                set smask [expr {$smask | (1 << $cn)}]
            }
        }
    }
    return $smask
}
 
# quadcode::transformer method jt_test_assertion --
#
#	Tests whether an anticipated condition is satisfied at the entry of
#	successor block $s from predecessor block $p.
#
# Parameters:
#	p - Predecssor block
#	pmask - Mask of bits indicating which of p's anticipated conditions
#	        are satisfied.
#	ptypes - Dictionary whose keys are the names of values defined in p
#	         and whose values are the types of the values.
#	s - Successor block
#	v - Variable name in the successor block
#	cond - Condition being tested
#
# Results:
#	Returns 1 if the condition is satisfied, 0 otherwise

oo::define quadcode::transformer method jt_test_assertion {p pmask ptypes
                                                           s v cond} {
    lassign $cond kind t

    # Find the name of the variable in the predecessor
    set w [my jt_translate_phi $s $v $p]

    if {[lindex $w 0] eq "literal"} {

        # The operand is a literal - extract its data type
        set u [::quadcode::typeOfLiteral [lindex $w 1]]
    } elseif {[dict exists $ptypes $w]} {

        # The operand is defined in the predecessor block, get its type
        set u [dict get $ptypes $w]
    } else {

        # The operand is not defined in the predecessor block, get its
        # conditions from the anticipable conditions in the predecessor
        return [my jt_masked_condition $p $pmask $w [list $kind $t]]
    }

    # We have the type of the operand created in the predecessor block.
    # Determine whether it satisfies the condition
    switch -exact -- $kind {
        is {
            return [my jt_type_is $u $t]
        }
        isnot {
            return [my jt_type_isnot $u $t]
        }
        default {
            return -code error error "bad condition $kind, can't happen"
        }
    }
}
 
# quadcode::transformer method jt_masked_condition --
#
#	Tests whether a block with a given mask of satisfied conditions
#	satisfies a particular named condition
#
# Parameters:
#	b - Block whose condition is being tested
#	mask - Bit mask indicating what numbered conditions are satisfied
#	v - Variable to which the desired condition applies
#	cond - Condition to test
#
# Results:
#	Returns 1 if the given condition is asserted, 0 otherwise.

oo::define quadcode::transformer method jt_masked_condition {b mask v cond} {

    my variable jt_antin

    if {![dict exists $jt_antin $b $v $cond]} {

        # The condition is not anticipable in the predecessor. This
        # should not happen!
        return 0
    } 

    # The condition is anticipated in the predecessor. Find out whether
    # it is asserted there.
    set bit [dict get $jt_antin $b $v $cond]
    if {$mask & (1 << $bit)} {
        return 1
    } else {
        return 0
    }
}
 
# quadcode::transformer method jt_localtype --
#
#	Returns the type of a quadcode operand taking into account
#	local type assertions.
#
# Parameters:
#	opd - Operand being analyzed
#	localtypes - Dictionary giving locally asserted types of variables
#
# Results:
#	Returns the type of the operand, or 0 if the operand does not
#	represent a value.

oo::define quadcode::transformer method jt_localtype {opd localtypes} {

    switch -exact -- [lindex $opd 0] {
        "literal" {
            return [quadcode::typeOfLiteral [lindex $opd 1]]
        }
        "temp" - "var" {
            if {[dict exists $localtypes $opd]} {
                return [dict get $localtypes $opd]
            } else {
                return [dict get $types $opd]
            }
        } default {
            return 0
        }
    }
}

 
# quadcode::transformer method jt_type_is --
#
#	Tests whether one type is always an instance of another.
#
# Parameters:
#	u - Type being tested
#	v - Reference type
#
# Results:
#	Returns 1 if u is always an instance of t, 0 otherwise.
#
# Notes:
#	Ignores the IMPURE bit unless it is being tested specifically.

oo::define quadcode::transformer method jt_type_is {u v} {
    namespace upvar ::quadcode::dataType IMPURE IMPURE
    if {($v == $IMPURE)} {
        return [expr {$u == $IMPURE}]; # Can't be true
    } elseif {[quadcode::dataType::isa $u $v]} {
        return 1
    } else {
        return 0
    }
}
 
# quadcode::transformer method jt_type_isnot --
#
#	Tests whether one type is never an instance of another.
#
# Parameters:
#	u - Type being tested
#	t - Reference type
#
# Results:
#	Returns 1 if u is never an instance of t, 0 otherwise.
#
# Notes:
#	Ignores the IMPURE bit unless it is being tested specifically.

oo::define quadcode::transformer method jt_type_isnot {u v} {
    namespace upvar ::quadcode::dataType IMPURE IMPURE
    if {($v == $IMPURE)} {
        return [expr {($u & $IMPURE) == 0}]; # Can't be true
    } elseif {[quadcode::dataType::mightbea $u $v]} {
        return 0
    } else {
        return 1
    }
}
 
# quadcode::transformer method jt_cleanup --
#
#	Cleans up working storage after the jump threading pass.
#
# Results: