Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | OOPS! Didn't add the new 'jumpthread.tcl' module! Remove the old 'nodesplit.tcl', and remove the 'renameTemps' pass since 'nodesplit' was the only thing that ever depended on it. |
---|---|
Timelines: | family | ancestors | descendants | both | notworking | kbk-jumpthread |
Files: | files | file ages | folders |
SHA3-256: |
67b989853b4d87b43a290b5f999d59bc |
User & Date: | kbk 2018-12-10 04:09:31.416 |
Context
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 | |
04:09 | OOPS! Didn't add the new 'jumpthread.tcl' module! Remove the old 'nodesplit.tcl', and remove the 'renameTemps' pass since 'nodesplit' was the only thing that ever depended on it. check-in: 67b989853b user: kbk tags: notworking, kbk-jumpthread | |
01:34 | Calculation of anticipable tests for jump threading check-in: f138663832 user: kbk tags: notworking, kbk-jumpthread | |
Changes
Added quadcode/jumpthread.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | # jumpthread.tcl -- # # Compiler passes to perform jump threading on quadcode. # # Copyright (c) 2018 by Kevin B. Kenny # # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # #------------------------------------------------------------------------------ # Jump threading is a surprisingly important optimization in the # processing of quadcode. The reason is that it allows for separation # of paths that would otherwise require boxing and unboxing of values # at every access. Consider, for instance, a procedure like: # # proc processRange {a b} { # for {set i $a} {$i < $b} {incr i} { # doSomethingWith $i # } # } # # At the entry to the [for] loop, nothing is known about the values of # $a and $b. The comparison {$i < $b} will therefore be expensive, # requiring that the types of both $i and $b be identified and either # string or numeric comparision be performed according to the # type. Moreover, on return to the top of the loop - after [incr i] # has been guarantted to produce an integer - in order to keep types # consistent at the phi operation at the type of the loop, $i will be # widened to a string again, forcing it to be boxed in a Tcl object. # # While the particular case of $i could be addressed by 'loop # peeling', duplicating the loop body so that the problematic first # iteration executes separately from the others, the comparison # {$i < $b} is not helped by loop peeling. On each trip through the loop, # $b's type will be checked, and its value (which is almost certain # to be an integer) will be unboxed. # # Jump threading addresses this issue by splitting the path from the # detection of the type of $b to the next use of $b, so that numeric # and non-numeric values for $b will take separate paths throughout # the code. # # The drawback to jump threading is that it can possibly result in a # combinatorial explosion in code volume. For this reason, there need # to be safety checks and triage to only the most promising # opportunities. # # # Most of the algorithms used in this module derive indirectly from: # # [Prie17] Priesner, Joachim. 'Generalized jump threading in libFIRM." # Masterarbeit, Fakultät für Informatik, Institut für # Programmstrukturen und Dantenorganisation (IPD), Karlsruher Institut # für Technologie (January 2017). # https://pp.ipd.kit.edu/uploads/publikationen/priesner17masterarbeit.pdf # # Priesner's work, however, deals chiefly with threading of # conditional branches and conditions involving Presburger arithmetic, # rather than type assertions surrounding values in a dynamic # language, so a fair amount of rethinking is present here. Instead of # considering a threading opportunity as a sequence of basic blocks # (with a conditional jump at the penultimate block), the logic here # considers a threading opportunity in terms of a sequence of # operations (reduced to a walk in the flowgraph) from an assignment # to a value that gives it a known type (possibly this can be expanded # to other constraints) to a use of the value that can be deleted from # the program if a given constraint is satisfied. The basic data flow # analysis, however - accumulate anticipated decisions from back to # front in the program, and then accumulate threading opportunities # from front to back - follows the general ideas in Priesner's thesis. # Map from instructions to the types that trigger their removal. # An instruction is removable if its sole operand matches the # type expression 'is $TYPE' or 'isnot $TYPE' namespace eval quadcode { # jt_removable carries the instructions that jump threading is trying # to let the optimizer rewrite, together with they type conditions # that the instructions are testing. variable jt_removable proc init {} { variable jt_removable namespace upvar ::quadcode::dataType \ ARRAY ARRAY CONST0 CONST0 FAIL FAIL IMPURE IMPURE NEXIST NEXIST dict set jt_removable "arrayExists" \ [list [list is $ARRAY] [list isnot $ARRAY]] dict set jt_removable "exists" \ [list [list is $NEXIST] [list isnot $NEXIST]] dict set jt_removable "initArrayIfNotExists" \ [dict get $jt_removable "exists"] dict set jt_removable "initIfNotExists" \ [dict get $jt_removable "exists"] dict set jt_removable "jumpFalse" \ [list [list is $CONST0] [list isnot $CONST0]] dict set jt_removable "jumpMaybe" \ [list [list is $FAIL] [list isnot $FAIL]] dict set jt_removable "jumpTrue" \ [dict get $jt_removable "jumpFalse"] dict set jt_removable "purify" \ [list [list isnot $IMPURE]] rename init {} } init } # quadcode::transformer method jumpthread -- # # Performs jump threading on a quadcode sequence. # # Results: # # Returns 1 if the sequence was modified, 0 otherwise. # # Side effects: # # Performs nearly arbitrary surgery on the sequence. While ud- # and du-chains are kept up to date, and critical edges will be # split, the dominance tree will need to be rebuilt, and the # resulting program may contain unreachable code, basic blocks # subject to coalescence, constants that need folding, redundant # conditional jumps, chains of copy operations, and similar # messes that need tidying. Type analysis will also need to be # repeated. oo::define quadcode::transformer method jumpthread {} { my debug-jumpthread { puts "Before jump threading" my dump-bb } # Unpack phi operations into the jt_phis, which is a multilevel # dictionary. [dict get $jt_phis $b $v $p], where $b is a basic block # number, $v is a variable and $p is the basic block number of a # predecessor of $b, identifies the data source in $p that corresponds to # variable $v in $b. my jt_unpackPhis # Identify sets of conditions that may benefit from threading. The # conditionals appear in the dictionary jt_condition and are identified by # number. The anticipability of the conditions is tracked in jt_antin, # which records what conditions are anticipable at the start of each basic # 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 # loop inversion might help.) my debug-jumpthread { puts "NOT DONE!" } # Clean up the working storage my jt_cleanup return 0 } # quadcode::transformer method jt_unpackPhis -- # # Unpacks phi operations for fast lookup when doing jump threading. # # Results: # # None. # # Side effects: # # Creates a multilevel dictionary $jt_phis. If value $v is the result of # a phi in basic block $b, and $p is a predecessor block of $b, then # [dict get $jt_phis $b $v $p] will give the corresponding value in $p. oo::define quadcode::transformer method jt_unpackPhis {} { my variable jt_phis set jt_phis {} set b -1 foreach bb $bbcontent { incr b set pc -1 foreach q $bb { incr pc if {[lindex $q 0 0] ne "phi"} break set v [lindex $q 1] foreach {source w} [lrange $q 2 end] { set p [lindex $source 1] dict set jt_phis $b $v $p $w } } } return } # quadcode::transformer method jt_backward -- # # Perform one or more passes of backward data flow analysis # in support of jump threading. # # Results: # None. # # 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 { puts "Start a pass of anticipability for jump threading" } foreach b [my bbrorder] { set bb [lindex $bbcontent $b] my debug-jumpthread { puts "bb $b:" } # Construct the conditions anticipable on output. It is # possible that the conditions will refer to literals, # in which case any possible threading opportunity will begin # on the exit from this block to the successor. set antout {} foreach s [my bbsucc $b] { dict for {w conds} [lindex $jt_antin $s] { set v [my jt_translate_phi $s $w $b] dict for {c -} $conds { dict set antout $v $c {} } } } # Construct the conditions anticipable on input. Begin by # filtering any constant conditions out of the output conditions. set antin {} dict for {v conds} $antout { if {[lindex $v 0] in {"temp" "var"}} { dict set antin $v $conds } } # Run backward through the instructions in the current block. # Remove any conditions that depend on instructions in the # block. Add any conditions that inform the removal of instructions # in the block. set pc [llength $bb] while {$pc > 0} { incr pc -1 set q [lindex $bb $pc] lassign $q opcode dest source1 set op [lindex $opcode 0] switch -exact -- $op { phi { # At a phi, we're done with the content of this # block. break } copy { # For a copy, the conditions on the destination # turn into conditions on the source set conds {} if {[dict exists $antin $dest]} { foreach {c -} [dict get $antin $dest] { dict set antin $source1 $c {} } dict unset antin $dest } } instanceOf { # For instanceOf, the conditions are that the # value is definitely/is definitely not an # instance of the given type. set wanted [lindex $opcode 1] dict set antin $source1 [list is $wanted] {} dict set antin $source1 [list isnot $wanted] {} dict unset antin $dest } default { # Otherwise, if this is an instruction that might # be removed depending on the type of its operand, # record what that type is. if {[dict exists $jt_removable $op]} { foreach c [dict get $jt_removable $op] { dict set antin $source1 $c {} } } 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])" } } } } } } return } # quadcode::transformer method jt_translate_phi -- # # Given a variable in a successor block, finds out what the # corresponding variable in the predecessor block is. # # Parameters: # b - Successor block # v - Variable name # p - Predecessor block # # Results: # Returns the name of the corresponding variable in the predecessor oo::define quadcode::transformer method jt_translate_phi {b v p} { my variable jt_phis if {[dict exists $jt_phis $b $v $p]} { return [dict get $jt_phis $b $v $p] } else { return $v } } # quadcode::translate method jt_forward -- # # Works through the forward propagation of knowledge about the # program to determine what sets of conditions should be assumed # 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] my jt_forward_worker $b $condMask } return } # quadcode::transformer method jt_forward_worker -- # # Performs forward jump threading analysis through one basic # block, propagating facts into the successors. # # Parameters: # b - Basic block being analyzed # mask - Mask identifying the conditions that are promised on # entry to the block. # # Results: # None. # # Side effects: # 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]] } 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: # None. oo::define quadcode::transformer method jt_cleanup {} { my variable jt_antin my variable jt_phis my variable jt_stack my variable jt_variants unset -nocomplain jt_antin unset -nocomplain jt_phis unset -nocomplain jt_stack unset -nocomplain jt_variants return } # Local Variables: # mode: tcl # fill-column: 78 # auto-fill-function: nil # buffer-file-coding-system: utf-8-unix # indent-tabs-mode: nil # End: |
Deleted quadcode/nodesplit.tcl.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted quadcode/renameTemps.tcl.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to quadcode/transformer.tcl.
︙ | ︙ | |||
316 317 318 319 320 321 322 | foreach pass { bbpartition constJumpPeephole sortbb loopinv callFrameMotion ssa | < | 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | foreach pass { bbpartition constJumpPeephole sortbb loopinv callFrameMotion ssa ud_du_chain copyprop fqcmd varargs deadbb bbidom bblevel |
︙ | ︙ |