Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add bitwise Boolean operations to constant folding. Modify partial redundancy elimination to use bit vectors for dataflow calculations and debug the dataflow equations. |
---|---|
Timelines: | family | ancestors | descendants | both | kbk-pre |
Files: | files | file ages | folders |
SHA3-256: |
71a4e793a15de0a4465095a50e2546a1 |
User & Date: | kbk 2018-11-11 22:34:10.264 |
Context
2018-11-12
| ||
01:38 | Change signatures in preparation for code rewriting: include enough information in signatures so that INSERT can reconstruct an instruction check-in: c098311242 user: kbk tags: kbk-pre | |
2018-11-11
| ||
22:34 | Add bitwise Boolean operations to constant folding. Modify partial redundancy elimination to use bit vectors for dataflow calculations and debug the dataflow equations. check-in: 71a4e793a1 user: kbk tags: kbk-pre | |
2018-11-10
| ||
22:17 | Use the lexical-name algorithm for 'altered' for now. May change to value-driven later. check-in: 592020ed7a user: kbk tags: notworking, kbk-pre | |
Changes
Changes to quadcode/constfold.tcl.
︙ | ︙ | |||
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | lset bbcontent $b [incr newpc] $q } "add" { lassign $argl x y set res [list literal [expr {$x + $y}]] my debug-constfold { puts "$b:$pc: $q" puts " replace [lindex $q 1] with $res" } my replaceUses [lindex $q 1] $res set changed 1 } | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | lset bbcontent $b [incr newpc] $q } "add" { lassign $argl x y set res [list literal [expr {$x + $y}]] my debug-constfold { puts "$b:$pc: $q" puts " replace [lindex $q 1] with $res" } my replaceUses [lindex $q 1] $res set changed 1 } "bitand" { lassign $argl x y set res [list literal [expr {$x & $y}]] my debug-constfold { puts "$b:$pc: $q" puts " replace [lindex $q 1] with $res" } my replaceUses [lindex $q 1] $res set changed 1 } "bitnot" { lassign $argl x set res [list literal [expr {~$x}]] my debug-constfold { puts "$b:$pc: $q" puts " replace [lindex $q 1] with $res" } my replaceUses [lindex $q 1] $res set changed 1 } "bitor" { lassign $argl x y set res [list literal [expr {$x | $y}]] my debug-constfold { puts "$b:$pc: $q" puts " replace [lindex $q 1] with $res" } my replaceUses [lindex $q 1] $res set changed 1 } "bitxor" { lassign $argl x y set res [list literal [expr {$x ^ $y}]] my debug-constfold { puts "$b:$pc: $q" puts " replace [lindex $q 1] with $res" } my replaceUses [lindex $q 1] $res set changed 1 } |
︙ | ︙ |
Changes to quadcode/pre.tcl.
︙ | ︙ | |||
86 87 88 89 90 91 92 93 94 95 | oo::define quadcode::transformer method partialredundancy {} { my debug-pre { puts "Before partial redundancy elimination:" my dump-bb } # Compute Global Value Numbering for the values present in the quadcode | > | | > | | > | | | > | | | | | | | | | > > | < | > > > > | | < < < | > > > > > > | > > > > > | > | > > > > > > > | > | > > > | > | | | | > | | 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 | oo::define quadcode::transformer method partialredundancy {} { my debug-pre { puts "Before partial redundancy elimination:" my dump-bb } my variable pre_exemplar # Compute Global Value Numbering for the values present in the quadcode lassign [my pre_gvn] VN pre_exemplar cands my debug-pre { puts "Value numbering:" dict for {k n} [lsort -stride 2 -index 0 -ascii -increasing $VN] { set v [lindex $pre_exemplar $n] if {[lindex $v 0] ne $k} { puts " found congruence $k -> $n ($v)" } } puts "Candidates for PRE: [my pre_format_bitset $cands]" } # Compute the 'defined' predicate for the basic blocks set defined [my pre_defined $VN] my debug-pre { puts "Defined expressions:" set b -1 foreach vset $defined { puts "[incr b]: [my pre_format_bitset $vset]" } } # Find sets of available expressions lassign [my pre_avail $defined] AVIN AVOUT my debug-pre { puts "Available expressions" set b -1 foreach inset $AVIN outset $AVOUT { incr b puts " Block #$b:" puts " AVIN = [my pre_format_bitset $inset]" puts " AVOUT = [my pre_format_bitset $outset]" } } # Find sets of expressions altered within each block set altered [my pre_altered $pre_exemplar $cands $AVIN $AVOUT] my debug-pre { puts "Altered expressions" set b -1 foreach al $altered { incr b puts " Block #$b: [my pre_format_bitset $al]" } } # Find expressions that are locally anticipable at entry to blocks set antloc [my pre_antloc $cands $defined $altered] my debug-pre { puts "Locally anticipable expressions" set b -1 foreach an $antloc { incr b puts " Block #$b: [my pre_format_bitset $an]" } } # Compute globally anticipable expressions lassign [my pre_ant_global $altered $antloc] ANTIN ANTOUT my debug-pre { puts "Globally anticipable expressions" set b -1 foreach inset $ANTIN outset $ANTOUT { incr b puts " Block #$b:" puts " ANTIN = [my pre_format_bitset $inset]" puts " ANTOUT = [my pre_format_bitset $outset]" } } # Compute earliest points where an expression may be moved. # These are edges on the call graph where the expression may # appear. (Since we have no critical edges, the expression # can always be inserted at either the tail of the predecessor # block or the head of the successor). set EARLIEST [my pre_earliest $AVOUT $altered $ANTIN $ANTOUT] my debug-pre { puts "Earliest insertion points:" dict for {edge vars} $EARLIEST { if {$vars != 0} { lassign $edge from to puts " $from->$to: [my pre_format_bitset $vars]" } } } # Compute LATER, which is an indication that an expression # may be moved later than an edge (LATER) or the start of # a block (LATERIN) lassign [my pre_later $antloc $EARLIEST] LATERIN LATER # Report on insertion and deletion points dict for {edge exprs} $LATER { lassign $edge from to my debug-pre { puts "Edge $from -> $to: LATER = [my pre_format_bitset $exprs]" puts "Node $to: LATERIN =\ [my pre_format_bitset [lindex $LATERIN $to]]" } set insert [expr {$exprs & ~[lindex $LATERIN $to]}] if {$insert != 0} { puts "Insert on edge ($from -> $to): [my pre_format_bitset $insert]" } } set b -1 foreach exprs $antloc laters $LATERIN { incr b if {$b == 0} continue my debug-pre { puts "Block $b: ANTLOC = [my pre_format_bitset $exprs]" puts "Block $b: LATERIN = [my pre_format_bitset $laters]" } set delete [expr {$exprs & ~$laters}] if {$delete != 0} { puts "Delete from block $b: [my pre_format_bitset $delete]" } } unset pre_exemplar return 0 } # quadcode::transformer method pre_gvn -- # # Calculates a Global Value Numbering for values in a quadcode # sequence. # # Results: # Returns a tjree-element list. # # The first element is a dictionary whose keys are the names of # program variables and whose values are value numbers. Two value # numbers are distinct if it cannot be shown that they result # from the same computation. # # The second element is a dictionary whose keys are the value # numbers and whose values are ordered pairs: program variables # that represent the values, and the expressions that compute the # values. # # The third element is a bit set containing those values that are # candidates - that is, could conceivably be moved from block to block # # This procedure implements the 'RPO' algorithm presented in # Figure 4.3 of [Simpson] (see references in the file header. # While perhaps not as fast, it is significantly simpler than # the 'SCC' algorithm and yields the same results. # # This procedure assumes that 'deadbb' has been run before it, # so that it will find basic blocks already in depth-first order. oo::define quadcode::transformer method pre_gvn {} { variable ::quadcode::gvn_eliminable # Initially all value numbers are unknown set VN {} # Iterate to convergence while {1} { set changed 0 set VNforSig {} set exemplarForVN {} set cands 0 # Iterate through all basic blocks in depth-first order set b -1 foreach bb $bbcontent { incr b # Walk through the instructions of each block set pc -1 foreach q $bb { incr pc # Ignore instructions that do not produce a value set result [lindex $q 1] if {[lindex $result 0] ni {"temp" "var"}} { continue } # TODO - Detect meaningless phi? # Make a signature for the expression computed # by the current instruction set sig [my pre_gvn_sig $VN $q] # If there is already a value number for the signature, use # it. Otherwise, make the current result the exemplar of the # congruence class. Set 'changed' if the the value number # changes from the previous round. if {![dict exists $VNforSig $sig]} { set vn [llength $exemplarForVN] dict set VNforSig $sig $vn lappend exemplarForVN [list $result $sig] if {[dict exists $gvn_eliminable [lindex $q 0 0]]} { set cands [expr {$cands | (1 << $vn)}] } } set vn [dict get $VNforSig $sig] if {![dict exists $VN $result] || [dict get $VN $result] ne $vn} { set changed 1 my debug-pre { puts "Value number for $result is now $vn" } dict set VN $result $vn } } } if {!$changed} break } return [list $VN $exemplarForVN $cands] } # quadcode::transformer method pre_gvn_sig -- # # Develops a signature for a quadcode instruction's result # in Global Value Numbering |
︙ | ︙ | |||
335 336 337 338 339 340 341 | bb - pc { lappend signature {} } temp - var { if {![dict exists $VN $a]} { lappend signature TOP } else { | | | < | < | | | < < | | < | | < | > > | | | > > > | | | | < < < | | < | < < < < < < < < < < | < | < < < | | | < | < < < | | | | | < | | | | | | | | | | | | > > > > | | > > > > | | < | < | < < < | < | < < < < < < | < < < < < < < < < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < | > | | > > > > | | | > > | | | > | | < | | | > > | > > < | | | | > | | < | > > | < < < < | | | < | | < | | | | | | | | > > | | < < < | | < < | < < < < < < < < < < | < < < < | < < < < < | < < < < < | | | | | | | | | | | | | | | | | > | | | | | | | | | | | | | < | | < | < | < | < < < < < < | < < | < < < | < < < < < < < < | < < < < < < < < < < | < | < | | | | | < < | < | | | < | | < | < | < > > > | > > > | | | | > > > | | | | | | < < < < | < < < < < < < < < < | < | < < < < < < < < < < | < < | | | < < | > > | | < | 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 | bb - pc { lappend signature {} } temp - var { if {![dict exists $VN $a]} { lappend signature TOP } else { lappend signature [list value [dict get $VN $a]] } } default { lappend signature $a } } } return $signature } # quadcode::transformer method pre_defined -- # # Computes the local predicate, 'defined', for use in the # dataflow equations. # # Results: # Returns a list of bit vectors. Each vector represents the set # of values defined in a basic block. oo::define quadcode::transformer method pre_defined {VN} { set DEFINED {} # Walk through the basic blocks set b -1 foreach bb $bbcontent { incr b set defined 0 # Walk through the individual instructions set pc -1 foreach q $bb { incr pc lassign $q opcode result # If an instruction defines a value, add the value to the set if {$result ne {} && [dict exists $VN $result]} { set defined [expr {$defined | (1 << [dict get $VN $result])}] } } # Add the set to the list lappend DEFINED $defined } return $DEFINED } # quadcode::transformer method pre_avail -- # # Calculates the available expressions at entry and exit to each # basic block. # # Parameters: # defined - List of bit vectors corresponding to the sets of values # defined in the basic blocks # # Results: # Returns an ordered pair {AVIN AVOUT}. # # AVIN is a list of bit vectors giving the sets of values available # at the start of the basic blocks. # # AVOUT is a list of bit vectors giving thes sets of values available # at the end of the basic blocks # # This procedure solves the data flow equations given in Figure 5.6 # on page 63 of [Simpson]. oo::define quadcode::transformer method pre_avail {defined} { my debug-pre { puts "Calculate available expressions:" } # Initially, assume all variables are available on input to all blocks # other than the entry, and then prove otherwise. set AVIN [lrepeat [llength $defined] -1] set AVOUT [lrepeat [llength $defined] -1] # Initially, the worklist is the complete set of basic blocks, # except for the entry block, in depth-first order set pending [lrepeat [llength $defined] 0] set worklist [::quadcode::numheap new] for {set i 0} {$i < [llength $defined]} {incr i} { $worklist add $i lset pending $i 1 } # Pop blocks from the worklist for analysis while {[$worklist size] > 0} { set b [$worklist removeFirst] lset pending $b 0 my debug-pre { puts " Block #$b:" } # Calculate AVIN for the block as the intersection of AVOUT # of all predecessors if {$b == 0} { set avin 0 } else { set otherpreds [lassign [dict keys [lindex $bbpred $b]] firstpred] set avin [lindex $AVOUT $firstpred] foreach p $otherpreds { set avin [expr {$avin & [lindex $AVOUT $p]}] } } my debug-pre { puts " Available on input: [my pre_format_bitset $avin]" } # If AVIN has changed, we have to recalculate AVOUT if {$avin != [lindex $AVIN $b]} { my debug-pre { puts " AVIN has changed, must update AVOUT" } lset AVIN $b $avin set avout [expr {$avin | [lindex $defined $b]}] my debug-pre { puts " Available on output: [my pre_format_bitset $avout]" } # If AVOUT has changed, we have to revisit the successors of this # block if {$avout != [lindex $AVOUT $b]} { my debug-pre { puts " AVOUT has changed, must update successors" } lset AVOUT $b $avout foreach s [my bbsucc $b] { if {![lindex $pending $s]} { my debug-pre { puts " Add $s to worklist" } $worklist add $s lset pending $s 1 } else { my debug-pre { puts " $s is already on worklist" } } } } } } $worklist destroy return [list $AVIN $AVOUT] } # quadcode::transformer method pre_altered -- # # For each basic block in the program, compute the set of values # that are altered by fixed values in the block # # Parameters: # exemplars - List of ordered pairs (name, expression) that # show exemplar names and expression computed for # each value. # cands - Bit set that identifies which values are candidates for # code motion # AVIN - List of bit vectors indicating the expressions available # on entry to the program's blocks # AVOUT - List of bit vectors indicating the expressions available # on exit from the program's blocks # # Results: # Returns a list indexed by basic block number. The list values # are dictionaries whose keys are the exemplars of altered values # and whose values are immaterial # # This is the algorithm of Figure 7.1 on page 75 of [Simpson]. Simpson # is quite unclear on the issue that (AVOUT-AVIN) should apply only to # fixed values, and then the altered candidates appear only as a # consequence of tracking the dependents on thosed fixed # values. Nevertheless, if this filtering is not done, nothing ever # becomes locally anticipable, because its own calculation turns up in # (AVOUT-AVIN). oo::define quadcode::transformer method pre_altered {exemplars cands AVIN AVOUT} { set ALTERED {} # Walk through the basic blocks set b -1 foreach avin $AVIN avout $AVOUT { incr b set altered [expr {$avout & ~$avin & ~$cands}] # Walk through all expressions in bottom-up order, and through # the operands of each expression set e -1 foreach ex $exemplars { incr e set argl [lassign [lindex $ex 1] opcode] foreach a $argl { lassign $a kind o if {$kind ne "value"} continue # Add altered values to the bit vector if {$altered & (1 << $o)} { set altered [expr {$altered | (1 << $e)}] } } } lappend ALTERED $altered } return $ALTERED } # quadcode::transformer method pre_antloc -- # # For each basic block in the program, compute the set of locally # anticipable expressions, that is, ones that can be moved ahead of # the block. # # Parameters: # cands - Bit set that identifies candidate expressions for # partial redundancy elimination # defined - List of bit sets, indexed by basic block number, indicating # what values are defined within the block # altered - List of bit sets, indexed by basic block number, indicating # what values are altered within the block # # Results: # Returns a list of bit sets, indexed by basic block number, indicating # what values are locally anticipable within the block. Note that # # Page 76 of [Simpson]: antloc_b = defined_b - altered_b # Simpson does not appear to constrain this to candidates, but it doesn't # ever make sense to move a non-candidate. oo::define quadcode::transformer method pre_antloc {cands defined altered} { set ANTLOC {} set b -1 foreach defns $defined alts $altered { incr b lappend ANTLOC [expr {$defns & ~$alts & $cands}] } return $ANTLOC } # quadcode::transformer method pre_ant_global -- # # Calculates the anticipable expressions at entry and exit to each # basic block. # # Parameters: # altered - List of bit vectors that enumerate the values altered # in each block. # antloc - List of bit vectors that enumerate the candidate values # for code motion that are locally anticipable in each block # # Results: # Returns an ordered pair {ANTIN ANTOUT}. # # ANTIN is a list indexed by basic block number of bit vectors # that enumerate the candidate values that are anticipable on # entry into basic blocks. # # ANTOUT is a list indexed by basic block number of bit vectors # that enumerate the candidate values that are anticipable on # exit from basic blocks # # This procedure solves data flow equations given in Figure 6.3 # on page 69 of [Simpson]. oo::define quadcode::transformer method pre_ant_global {altered antloc} { my debug-pre { puts "Calculate global anticipable expressions:" } set ANTOUT [lrepeat [llength $altered] -1] set ANTIN [lrepeat [llength $altered] -1] # Initially, the worklist is the complete set of basic blocks, # except for the entry block, in reverse depth-first order set pending [lrepeat [llength $altered] 0] set worklist [::quadcode::numheap new] for {set i 0} {$i < [llength $altered]} {incr i} { $worklist add [expr {-$i}] lset pending $i 1 } # Pop blocks from the worklist for analysis while {[$worklist size] > 0} { set b [expr {-[$worklist removeFirst]}] lset pending $b 0 my debug-pre { puts " Block #$b:" } set alt [lindex $altered $b] set aloc [lindex $antloc $b] # Calculate ANTOUT for the block as the intersection of ANTIN # of all predecessors set succs [my bbsucc $b] if {[llength $succs] == 0} { # Exit block - nothing anticipable on output set antout 0 } else { set othersuccs [lassign $succs firstsucc] set antout [lindex $ANTIN $firstsucc] foreach s $othersuccs { set antout [expr {$antout & [lindex $ANTIN $s]}] } } my debug-pre { puts " Anticipable on output: [my pre_format_bitset $antout]" } # If ANTOUT has changed, we have to recalculate ANTIN if {$antout != [lindex $ANTOUT $b]} { lset ANTOUT $b $antout set antin [expr {($antout & ~$alt) | $aloc}] # If ANTIN has changed, we have to revisit the predecessors of this # block if {$antin != [lindex $ANTIN $b]} { my debug-pre { puts " ANTIN has changed, must update predecessors" } lset ANTIN $b $antin dict for {p -} [lindex $bbpred $b] { if {![lindex $pending $p]} { my debug-pre { puts " Add $p to worklist" } $worklist add [expr {-$p}] lset pending $p 1 } else { my debug-pre { puts " $p is already on worklist" } } } } } } $worklist destroy return [list $ANTIN $ANTOUT] } # quadcode::transformer method pre_earliest -- # # Calculate the earliest edge in the control flow graph where the # calculation of a given expression may appear. # # Parameters: # AVOUT - List, indexed by basic block number, of bit vectors that # enumerate the values available on exit from the corresponding # basic blocks # altered - List, indexed by basic block number, of bit vectors that # enumerate the values that are spoilt in the corresponding # basic blocks # ANTIN - List, indexed by basic block number, of bit vectors that # enumerate the values that are globally anticipable on entry # to the corresponding basic blocks. # ANTOUT - List, indexed by basic block number, of bit vectors that # enumerate the values that are globally anticipable on exit # from the corresponding basic blocks. # # Results: # Returns a dictionary whose keys are ordered pairs {from to} # representing edges in the control flow graph (where 'from' and # 'to' are basic block numbers), and whose values are bit vectors # representing the sets of values for which the edges are the # earliest points where the values may be computed. # # This analysis is part of the data flow equations in Figure 6.3 on # page 69 of [Simpson]. oo::define quadcode::transformer method pre_earliest {AVOUT altered ANTIN ANTOUT} { set EARLIEST {} # Walk through flowgraph edges (i -> j) set i -1 foreach avout_i $AVOUT altered_i $altered antout_i $ANTOUT { incr i foreach j [my bbsucc $i] { # Edge (i, j) exists in the flow graph. Calculate EARLIEST set antin_j [lindex $ANTIN $j] set earliest [expr {$antin_j & ~$avout_i & ($altered_i | ~$antout_i)}] dict set EARLIEST [list $i $j] $earliest } } return $EARLIEST } # quadcode::transformer method pre_later -- # # Calculates an indication of whether an expression can be # shifted later in the flowgraph # # Parameters: # ANTLOC - List of bit sets, indexed by basic block number, indicating # what values are locally anticipable within the block. # # EARLIEST - Dictionary whose keys are ordered pairs {i j} # identifying edges {i -> j} in the # control flow graph. The values are bit vectors enumerating # sets of values for which the corresponding edgs is the # earliest possible placement. # # Results: # Returns an ordered pair {LATER LATERIN}. # # LATER is a dictionary whose keys are ordered pairs {i j} # identifying edges {i -> j} in the control flow graph. # The values are bit sets that enumerate the candidate values # that may be computed on the corresponding edge or a later point # in the program. # # LATERIN is a list indexed by basic block number of bit vectors # that enumerate the candidate values that may be calculated # on entry to the corresponding basic block or later in the program. # # This procedure solves part of the dataflow equations shown in # Figure 6.4 on page 70 of [Simpson]. oo::define quadcode::transformer method pre_later {ANTLOC EARLIEST} { my debug-pre { puts "Calculate LATER:" } # Initialize LATER with the universal set for every flowgraph edge, # and LATERIN with the universal set for every basic block set LATER [] dict for {edge -} $EARLIEST { dict set LATER $edge -1 } set LATERIN [lrepeat [llength $ANTLOC] -1] # Initially, the worklist is the complete set of basic blocks, # in depth-first order set pending [lrepeat [llength $ANTLOC] 0] set worklist [::quadcode::numheap new] for {set i 0} {$i < [llength $ANTLOC]} {incr i} { $worklist add $i lset pending $i 1 } # Pop blocks from the worklist for analysis while {[$worklist size] > 0} { set b [$worklist removeFirst] lset pending $b 0 my debug-pre { puts " Block #$b:" } # Calculate LATERIN for the block as the intersection of # LATER on each of its afferent edges. if {$b == 0} { set laterin 0 } else { set otherpreds [lassign [dict keys [lindex $bbpred $b]] firstpred] set edge [list $firstpred $b] set laterin [dict get $LATER $edge] foreach p $otherpreds { set edge [list $p $b] set laterin [expr {$laterin & [dict get $LATER $edge]}] } } my debug-pre { puts " LATERIN: [my pre_format_bitset $laterin]" } # If LATERIN has changed, we have to recalculate LATER for # each successor if {$laterin != [lindex $LATERIN $b]} { my debug-pre { puts " LATERIN has changed, must update LATER" } lset LATERIN $b $laterin set intsct [expr {$laterin & ~[lindex $ANTLOC $b]}] foreach s [my bbsucc $b] { my debug-pre { puts " $b -> $s:" } set edge [list $b $s] set later [expr {$intsct | [dict get $EARLIEST $edge]}] my debug-pre { puts " LATER = [my pre_format_bitset $later]" } # If LATER has changed, we need to requeue the successor # node. if {$later != [dict get $LATER $edge]} { my debug-pre { puts " LATER has changed, must update bb $s" } dict set LATER $edge $later if {![lindex $pending $s]} { my debug-pre { puts " Add $s to worklist" |
︙ | ︙ | |||
1045 1046 1047 1048 1049 1050 1051 | } $worklist destroy return [list $LATERIN $LATER] } | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | } $worklist destroy return [list $LATERIN $LATER] } # quadcode::transformer method pre_format_bitset -- # # Formats a set of values for debug printing # # Parameters: # vset - Set of values to be printed # # Results: # Returns a list of exemplars corresponding to the set of values oo::define quadcode::transformer method pre_format_bitset {vset} { my variable pre_exemplar if {$vset == -1} { return "EVERYTHING" } if {$vset < 0} { set result "EVERYTHING BUT" set vset [expr {~$vset}] } else { set result {} } set vn -1 foreach ex $pre_exemplar { incr vn if {$vset & (1 << $vn)} { lappend result [lindex $ex 0] } } return $result } |