tclquadcode

Check-in [a7682cd36e]
Login

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

Overview
Comment:Merge the code to generate the lifetimes from the model
Timelines: family | ancestors | descendants | both | llvm-integration-memory
Files: files | file ages | folders
SHA1:a7682cd36e4d3434b8e776a211e0a5545aa737b5
User & Date: dkf 2015-03-10 14:51:20
Context
2015-03-11
14:11
Remove helper methods no longer required. check-in: ba59d0e754 user: dkf tags: llvm-integration-memory
2015-03-10
14:51
Merge the code to generate the lifetimes from the model check-in: a7682cd36e user: dkf tags: llvm-integration-memory
2015-03-09
02:42
Try to restore retrograde type inference - second attempt Closed-Leaf check-in: f68b06d6b1 user: kbk tags: llvm-integration-memory-part2
2015-03-08
15:28
Working towards using the lifetime information. Also move the cast-to-STRING code into the type-cast builder. check-in: 89816c024e user: dkf tags: llvm-integration-memory
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to codegen/compile.tcl.

39
40
41
42
43
44
45





46
47
48
49
50
51
52
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
...
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
...
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
...
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
...
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
...
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
	quads-list-dataflows $quads $vars
	db reachdef $quads
	# Enumerate the data sources for all phi functions
	set phis [db ssa2 $quads]
	# Rewrite the three-address code into SSA form
	set quads [db ssa3 $quads [dict keys $vars] $phis]






	db destroy

	# Stratify the variable dependencies
	set vdep [vardepends $quads]
	set vtypes {}
	bdd::datalog::scc x $vdep {
	    if {[llength $x] > 1} {
................................................................................
	}

	# Compute the argument types
	set argl {}
	set argn {}
	foreach l [dict get $bytecode variables] {
	    if {"arg" in [lindex $l 0]} {
		set argName [list var [lindex $l 1] -1]
		set type [nameOfType [typeOfOperand $vtypes $argName]]
		lappend argn $type
		lappend argl [Type $type]
	    }
	}

	# Compute the return type
................................................................................
	    } else {
		return [$b packInt64 [Const $value int64]]
	    }
	} else {
	    error "unhandled type for literal \"${value}\": \"$type\""
	}
    }
 
    method TransitiveClosure {transitivearray sourcearray keys} {
	upvar 1 $transitivearray trans $sourcearray rel
	unset -nocomplain trans
	array set trans [set ser [array get rel]]
	while 1 {
	    foreach k $keys {
		set trans($k) [lsort -unique [
		    concat $trans($k) {*}[lmap n $rel($k) {set trans($n)}]
		]]
	    }
	    set s2 [lsort -integer -stride 2 -index 0 [array get trans]]
	    if {$s2 eq $ser} break
	    set ser $s2
	}
    }

    method PropagatesVariables {blockAddresses} {
	set pc -1
	set blockAddresses [lrange [lsort -integer $blockAddresses] 1 end]
	array set phis {}
	foreach q $quads {
	    incr pc
	    if {$pc in $blockAddresses} {
		if {[info exist b]} {
		    if {[lindex $quads [expr {$pc-1}] 0] ni {jump return}} {
			lappend next($b) $pc
		    }
		}
		set b $pc
		set reads($b) {}
		set writes($b) {}
		set next($b) {}
		set phis($b) {}
	    }
	    catch {
		my ValueTypes [lindex $q 1]
		if {[lindex $q 1 0] ne "literal" && [lindex $q 1 2] != -1} {
		    lappend writes($b) [lindex $q 1]
		}
	    }
	    foreach v [lrange $q 2 end] {
		catch {
		    my ValueTypes $v
		    if {[lindex $v 0] ne "literal" && [lindex $v 2] != -1} {
			lappend reads($b) $v
		    }
		}
	    }
	    if {[lindex $q 0] in {jump jumpFalse jumpTrue}} {
		lappend next($b) [lindex $q 1 1]
	    } elseif {[lindex $q 0] eq "phi"} {
		lappend phis($b) $q
	    }
	}
	set prev(0) {}
	foreach b $blockAddresses {
	    set reads($b) [lsort -unique $reads($b)]
	    set writes($b) [lsort -unique $writes($b)]
	    foreach n $next($b) {
		lappend prev($n) $b
	    }
	}

	my TransitiveClosure transitivenext next $blockAddresses
	my TransitiveClosure transitiveprev prev $blockAddresses
	set loopAddresses {}
	foreach b $blockAddresses {
	    if {[set loop($b) [expr {$b in $transitivenext($b)}]]} {
		lappend loopAddresses $b
	    }
	}

	set later(0) {}
	foreach b $blockAddresses {
	    set later($b) [lsort -unique [concat {*}[
		lmap n $transitivenext($b) {
		    if {$n == $b} continue {set reads($n)}}]]]
	}

	foreach b $blockAddresses {
	    set loopblocks($b) {}
	    set loopphis($b) {}
	    set exits($b) {}
	    if {!$loop($b)} continue
	    foreach l $blockAddresses {
		if {$l in $transitivenext($b) && $l in $transitiveprev($b)} {
		    lappend loopblocks($b) $l
		}
	    }
	    foreach l $next($b) {
		if {$l ni $loopblocks($b)} {
		    lappend exits($b) $l
		}
	    }
	    foreach l $loopblocks($b) {
		foreach p $phis($l) {
		    foreach {v from} [lrange $p 2 end] {
			lappend loopphis($b) $v
		    }
		}
	    }
	    set loopphis($b) [lmap v [lsort -unique $loopphis($b)] {
		set w false
		foreach l $loopblocks($b) {
		    if {$v in $writes($l)} {
			set w true
			break
		    }
		}
		if {$w} {string cat $v} continue
	    }]
	}

	foreach b $blockAddresses {
	    set unsetend($b) {}
	    set unsetstart($b) {}
	}

	set inf -2
	foreach b $loopAddresses {
	    if {![llength $phis($b)]} continue
	    unset -nocomplain thisloopnext thisloopreads thisloopwrites
	    array set thisloopnext {}
	    set thisloopnext($inf) {}
	    array set thisloopreads {}
	    array set thisloopwrites {}
	    foreach x $exits($b) {
		lappend thisloopreads($inf) {*}$reads($x) {*}$later($x)
	    }
	    set ps {}
	    foreach p $phis($b) {
		foreach {v from} [lrange $p 2 end] {
		    lappend thisloopreads($inf) $v
		    lappend ps $v
		}
	    }
	    foreach l $loopblocks($b) {
		foreach n $next($l) {
		    if {$n in $loopblocks($b)} {
			if {$n == $b} {
			    set n $inf
			}
			lappend thisloopnext($l) $n
		    } elseif {![info exist thisloopnext($n)]} {
			lappend thisloopnext($l) $n
			set todo [list $n]
			for {set i 0} {$i < [llength $todo]} {incr i} {
			    set n [lindex $todo $i]
			    if {![info exist thisloopnext($n)]} {
				set thisloopnext($n) $next($n)
				set thisloopreads($n) $reads($n)
				lappend todo {*}$next($n)
			    }
			}
		    }
		}
		set thisloopwrites($l) $writes($l)
		if {$l != $b} {
		    set thisloopreads($l) $reads($l)
		} else {
		    foreach r $reads($l) {
			if {$r ni $thisloopreads($inf)} {
			    lappend thisloopreads($l) $r
			}
		    }
		}
	    }
	    my TransitiveClosure thistransnext thisloopnext $loopblocks($b)
	    foreach l $loopblocks($b) {
		set thistransnext($b) [lsort -command {apply {{x y} {
		    if {$x == $y} {return 0}
		    upvar 1 thistransnext next
		    expr {$x in $next($y) ? 1 : -1}
		}}} $thistransnext($b)]
	    }
	    unset -nocomplain thistransread
	    foreach l [array names thistransnext] {
		set thistransread($l) [concat {*}[lmap n $thistransnext($l) {set thisloopreads($n)}]]
	    }
	    foreach l $loopblocks($b) {
		foreach v $thisloopwrites($l) {
		    foreach follow [list $l {*}$thistransnext($l)] {
			if {$follow == $inf} {
			    continue
			}
			if {$v ni $thistransread($follow)} {
			    # puts "Step 0: remove $v at end of $follow"
			    lappend unsetend($follow) $v
			    break
			}
		    }
		}
	    }
	    foreach v $thisloopreads($inf) {
		if {$v ni $ps} {
		    lappend ps $v
		}
	    }
	}

	foreach b $blockAddresses {
	    set forwardsFromB {}
	    foreach v $writes($b) {
		if {$v ni $later($b)} {
		    # puts "Step 1: remove '$v' at end of $b"
		    lappend unsetend($b) $v
		} else {
		    lappend forwardsFromB $v
		}
	    }
	    set writes($b) $forwardsFromB
	}

	foreach b $blockAddresses {
	    set newwrites {}
	    foreach w $writes($b) {
		set terminationfrontier {}
		foreach n $transitivenext($b) {
		    if {$w ni $later($n) && $w ni $reads($n)} {
			foreach b2 [list $n {*}$transitivenext($n)] {
			    if {$b2 ni $terminationfrontier} {
				lappend terminationfrontier $n
			    }
			}
		    }
		}
		set terminationfrontier [lmap t $terminationfrontier {
		    set remove false
		    foreach p $prev($t) {
			if {$p in $terminationfrontier} {set remove true}
		    }
		    if {$remove} continue {set t}
		}]
		foreach t $terminationfrontier {
		    # puts "Step 2: remove '$w' at start of $t"
		    lappend unsetstart($t) $w
		}
		if {$loop($b)} {
		    lappend newwrites $w
		}
	    }
	    set writes($b) $newwrites
	}

	foreach b $blockAddresses {
	    if {!$loop($b)} continue
	    set ws [concat {*}[lmap lb $loopblocks($b) {set writes($b)}]]
	    foreach x $exits($b) {
		foreach w $ws {
		    if {$w ni $reads($x) && $w ni $later($x)} {
			# puts "Step 3a: remove '$w' at start of $x"
			lappend unsetstart($x) $w
		    }
		}
	    }
	    foreach w $writes($b) {
		if {$w ni $loopphis($b)} {
		    foreach p $prev($b) {
			if {$p in $loop($b)} {
			    # puts "Step 3b: remove '$w' at end of $p"
			    lappend unsetend($p) $w
			}
		    }
		}
	    }
	}

	foreach b $blockAddresses {
	    if {!$loop($b)} continue
	    foreach w $writes($b) {
		if {$w in $loopphis($b)} {
		    # puts "Step 4a: remove '$w' at start of $b"
		    lappend unsetstart($b) $w
		} else {
		    foreach p $prev($b) {
			if {$p in $loopblocks($b)} {
			    # puts "Step 4b: remove '$w' at end of $p"
			    lappend unsetend($p) $w
			}
		    }
		}
	    }
	}

	foreach b $blockAddresses {
	    set unsetstart($b) [lsort -unique $unsetstart($b)]
	    set unsetend($b) [lsort -unique $unsetend($b)]
	}

	# parray reads
	# parray writes
	# parray next
	# parray transitivenext
	# parray prev
	# parray transitiveprev
	# parray loopblocks
	# parray exits
	# parray later
	# parray unsetstart
	# parray unsetend
	# parray loopphis
	# 

	foreach v [concat {*}[lmap {b v} [array get reads] {set v}]] {
	    set found 0
	    foreach b $blockAddresses {
		if {$v in $unsetstart($b) || $v in $unsetend($b)} {
		    set found 1;break
		}
	    }
	    if {!$found} {
		error "Do not know where '$v' is finished"
	    }
	}

	return [list [array get unsetstart] [array get unsetend]]
    }
 
    method StoreResult {desc value} {
	if {[info exist variables($desc)]} {
	    ReplaceAllUsesWith $variables($desc) $value
	}
	set variables($desc) $value
	return
    }
................................................................................
		if {![info exists block($tgt)]} {
		    set block($tgt) [$func block]
		}
		set next_is_ipath $pc
	    }
	}
	$block(-1) build-in $b
	lassign [my PropagatesVariables [array names block]] \
	    unsetstart unsetend

	# Create stack and stack pointer
	set stack [Stack new $b $stackDepth]
	set curr_block $block(-1)
	set ends_with_jump($curr_block) 0
	set 0 [$b packInt32 [Const 0]]

	# Load arguments into llvm
	dict for {name typecode} $vtypes {
	    set type [nameOfType $typecode]
	    lassign $name kind formalname origin

	    if {$kind eq "var" && $origin == -1} {
		set idx -1
		foreach arg [dict get $bytecode variables] {
		    incr idx
		    if {
			[lindex $arg 0] eq "scalar arg"
			&& [lindex $arg 1] eq $formalname
................................................................................
	set maxpc 0
	set pc -1
	foreach l $quads {
	    incr pc
	    set opcode [lindex $l 0]
	    set maxpc $pc
	    if {[info exists block($pc)]} {
		set unsetsToProcess [dict get $unsetstart $pc]
		$block($pc) build-in $b
		set unsetsAtEnd [dict get $unsetend $pc]
		set curr_block $block($pc)
		set block_finished 0
	    }
	    if {$block_finished} {
		# Instructions after something that terminates a block
		# should be ignored. Tcl's built-in optimizer doesn't trim
		# all of them.
		continue
	    }
	    set ends_with_jump($curr_block) 0
	    unset -nocomplain tgt
	    if {[info exist unsetsToProcess] && $opcode ni {confluence phi}} {
		my ProcessUnsets $unsetsToProcess
		unset unsetsToProcess
	    }

	    switch -exact -- $opcode {
		"confluence" {
		    # Do nothing; required for SSA computations only
		}
		"bitor" - "bitxor" - "bitand" - "lshift" - "rshift" -
		"add" - "sub" - "mult" - "div" - "mod" - "uminus" -
................................................................................
		"eq" - "neq" - "lt" - "gt" - "le" - "ge" - "streq" {
		    my CompareOp $b $l
		}
		"copy" {
		    lassign $l opcode tgt src
		    my StoreResult $tgt [my LoadOrLiteral $src]
		}








		"jumpTrue" {
		    lassign $l opcode tgt src
		    set name [my LocalVarName $src]
		    set tgt [lindex $tgt 1]
		    set neq neq([my ValueTypes $src],INT)
		    set test [$b $neq [my LoadOrLiteral $src] $0 test_$name]
		    my ProcessUnsets $unsetsAtEnd
		    $b condBr(INT) $test $block($tgt) $block($ipath($pc))
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"jumpFalse" {
		    lassign $l opcode tgt src
		    set name [my LocalVarName $src]
		    set tgt [lindex $tgt 1]
		    set neq neq([my ValueTypes $src],INT)
		    set test [$b $neq [my LoadOrLiteral $src] $0 test_$name]
		    my ProcessUnsets $unsetsAtEnd
		    $b condBr(INT) $test $block($ipath($pc)) $block($tgt)
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"jump" {
		    my ProcessUnsets $unsetsAtEnd
		    $b br $block([lindex $l 1 1])
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"return" {
		    lassign $l opcode -> src
		    set val [my LoadOrLiteral $src]
		    my ProcessUnsets $unsetsAtEnd
		    $b ret $val
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"phi" {
		    set values {}
		    set sources {}
................................................................................

	# Set increment paths
	foreach {pc blk} [array get block] {
	    $blk build-in $b
	    if {$ends_with_jump($blk)} continue
	    while {[incr pc] <= $maxpc} {
		if {[info exists block($pc)]} {
		    my ProcessUnsets $unsetsAtEnd
		    $b br $block($pc)
		    break
		}
	    }
	}

	# Cleanup and return
	$stack destroy
	return [$func ref]
    }

    method ProcessUnsets {unsetList} {
	upvar 1 b b
	foreach var $unsetList {
	    set type [nameOfType [typeOfOperand $vtypes $var]]
	    if {$type ni {INT {INT BOOLEAN} DOUBLE}} {
		$b dropReference $variables($var)
	    }
	}
    }

    method SimpleOp {b l} {
	set srcs [lassign $l opcode tgt]
	set name [my LocalVarName $tgt]
	append opcode ( [my ValueTypes {*}$srcs] )
	set result [$b $opcode {*}[lmap s $srcs {my LoadOrLiteral $s}] $name]







>
>
>
>
>







 







|







 







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
<











>







 







<

<











<
<
<
<







 







>
>
>
>
>
>
>







<










<





<







<







 







<









<
<
<
<
<
<
<
<
<
<







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
..
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
...
153
154
155
156
157
158
159
160






























































































































































































































































































































161
162
163
164
165
166
167
...
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
...
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
...
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
...
344
345
346
347
348
349
350

351
352
353
354
355
356
357
358
359










360
361
362
363
364
365
366
	quads-list-dataflows $quads $vars
	db reachdef $quads
	# Enumerate the data sources for all phi functions
	set phis [db ssa2 $quads]
	# Rewrite the three-address code into SSA form
	set quads [db ssa3 $quads [dict keys $vars] $phis]

	# Add instructions to clean up memory
	set vars [initDB $quads]
	quads-list-dataflows $quads $vars
	set quads [db freeValues $quads [dict keys $vars]]

	db destroy

	# Stratify the variable dependencies
	set vdep [vardepends $quads]
	set vtypes {}
	bdd::datalog::scc x $vdep {
	    if {[llength $x] > 1} {
................................................................................
	}

	# Compute the argument types
	set argl {}
	set argn {}
	foreach l [dict get $bytecode variables] {
	    if {"arg" in [lindex $l 0]} {
		set argName [list var [lindex $l 1]]
		set type [nameOfType [typeOfOperand $vtypes $argName]]
		lappend argn $type
		lappend argl [Type $type]
	    }
	}

	# Compute the return type
................................................................................
	    } else {
		return [$b packInt64 [Const $value int64]]
	    }
	} else {
	    error "unhandled type for literal \"${value}\": \"$type\""
	}
    }































































































































































































































































































































    method StoreResult {desc value} {
	if {[info exist variables($desc)]} {
	    ReplaceAllUsesWith $variables($desc) $value
	}
	set variables($desc) $value
	return
    }
................................................................................
		if {![info exists block($tgt)]} {
		    set block($tgt) [$func block]
		}
		set next_is_ipath $pc
	    }
	}
	$block(-1) build-in $b



	# Create stack and stack pointer
	set stack [Stack new $b $stackDepth]
	set curr_block $block(-1)
	set ends_with_jump($curr_block) 0
	set 0 [$b packInt32 [Const 0]]

	# Load arguments into llvm
	dict for {name typecode} $vtypes {
	    set type [nameOfType $typecode]
	    lassign $name kind formalname origin
	    if {$origin eq {}} { set origin -1 }
	    if {$kind eq "var" && $origin == -1} {
		set idx -1
		foreach arg [dict get $bytecode variables] {
		    incr idx
		    if {
			[lindex $arg 0] eq "scalar arg"
			&& [lindex $arg 1] eq $formalname
................................................................................
	set maxpc 0
	set pc -1
	foreach l $quads {
	    incr pc
	    set opcode [lindex $l 0]
	    set maxpc $pc
	    if {[info exists block($pc)]} {

		$block($pc) build-in $b

		set curr_block $block($pc)
		set block_finished 0
	    }
	    if {$block_finished} {
		# Instructions after something that terminates a block
		# should be ignored. Tcl's built-in optimizer doesn't trim
		# all of them.
		continue
	    }
	    set ends_with_jump($curr_block) 0
	    unset -nocomplain tgt





	    switch -exact -- $opcode {
		"confluence" {
		    # Do nothing; required for SSA computations only
		}
		"bitor" - "bitxor" - "bitand" - "lshift" - "rshift" -
		"add" - "sub" - "mult" - "div" - "mod" - "uminus" -
................................................................................
		"eq" - "neq" - "lt" - "gt" - "le" - "ge" - "streq" {
		    my CompareOp $b $l
		}
		"copy" {
		    lassign $l opcode tgt src
		    my StoreResult $tgt [my LoadOrLiteral $src]
		}
		"free" {
		    lassign $l opcode tgt src
		    set type [nameOfType [typeOfOperand $vtypes $src]]
		    if {$type ni {INT {INT BOOLEAN} DOUBLE}} {
			$b dropReference $variables($src)
		    }
		}

		"jumpTrue" {
		    lassign $l opcode tgt src
		    set name [my LocalVarName $src]
		    set tgt [lindex $tgt 1]
		    set neq neq([my ValueTypes $src],INT)
		    set test [$b $neq [my LoadOrLiteral $src] $0 test_$name]

		    $b condBr(INT) $test $block($tgt) $block($ipath($pc))
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"jumpFalse" {
		    lassign $l opcode tgt src
		    set name [my LocalVarName $src]
		    set tgt [lindex $tgt 1]
		    set neq neq([my ValueTypes $src],INT)
		    set test [$b $neq [my LoadOrLiteral $src] $0 test_$name]

		    $b condBr(INT) $test $block($ipath($pc)) $block($tgt)
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"jump" {

		    $b br $block([lindex $l 1 1])
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"return" {
		    lassign $l opcode -> src
		    set val [my LoadOrLiteral $src]

		    $b ret $val
		    set ends_with_jump($curr_block) 1
		    set block_finished 1
		}
		"phi" {
		    set values {}
		    set sources {}
................................................................................

	# Set increment paths
	foreach {pc blk} [array get block] {
	    $blk build-in $b
	    if {$ends_with_jump($blk)} continue
	    while {[incr pc] <= $maxpc} {
		if {[info exists block($pc)]} {

		    $b br $block($pc)
		    break
		}
	    }
	}

	# Cleanup and return
	$stack destroy
	return [$func ref]










    }

    method SimpleOp {b l} {
	set srcs [lassign $l opcode tgt]
	set name [my LocalVarName $tgt]
	append opcode ( [my ValueTypes {*}$srcs] )
	set result [$b $opcode {*}[lmap s $srcs {my LoadOrLiteral $s}] $name]

Changes to demo.tcl.

200
201
202
203
204
205
206




207
208
209
210
211
212
213
# puts "After converting to SSA form:"
# set i 0
# foreach q $quads {
#     puts "$i: $q"
#     incr i
# }





# Stratify the dependencies of one SSA variable on another. These
# strata are strongly connected components of the dependency graph,
# and are the natural places to attempt structural induction.

puts "vardepends: [time {
    set vdep [vardepends $quads]
}]"







>
>
>
>







200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# puts "After converting to SSA form:"
# set i 0
# foreach q $quads {
#     puts "$i: $q"
#     incr i
# }

set vars [initDB $quads]
quads-list-dataflows $quads $vars
set quads [db freeValues $quads [dict keys $vars]]

# Stratify the dependencies of one SSA variable on another. These
# strata are strongly connected components of the dependency graph,
# and are the natural places to attempt structural induction.

puts "vardepends: [time {
    set vdep [vardepends $quads]
}]"

Changes to quadcode.tcl.

133
134
135
136
137
138
139

140
141
142
143
144
145
146
...
298
299
300
301
302
303
304

305
306
307
308
309
310
311
...
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
...
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
...
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
....
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
....
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
....
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
....
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
....
1628
1629
1630
1631
1632
1633
1634































































































































































































1635
1636
1637
1638
1639
1640
1641
....
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
....
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
....
2364
2365
2366
2367
2368
2369
2370

2371
2372
2373
2374
2375
2376

2377
2378
2379
2380
2381
2382
2383
....
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464

2465
2466
2467















2468
2469
2470
2471
2472
2473
2474
		bytecode-stack-state-worker bytecode \
		    $target $head $depth
		# fallthrough to do the 'branch not taken'
	    }
	    add -
	    div -
	    eq -

	    gt -
	    land -
	    le -
	    listIndexImm -
	    lor -
	    lshift -
	    lt -
................................................................................
	}

	# Translate the current bytecode
	switch -exact -- [lindex $insn 0] {
	    add -
	    div -
	    eq -

	    gt -
	    le -
	    lt -
	    mod -
	    lshift -
	    rshift -
	    mult -
................................................................................

    $db relation dom st st2;	# st dominates st2
    $db relation sdom st st2;	# st dominates st2 strictly
    $db relation frontier st st2;
    ;				# st2 is in st's dominance frontier

    $db relation liveat v st;	# v is live on entry to st

    $db relation needphi v st2
    ;				# a phi function is needed at st2 for
    ;				# variable v
    $db relation defreaches isphi v st st2
    ;				# a phi function or unique assignment
    ;                           # for v before st reaches st2
    $db relation valreaches v st st2
    ;				# the value of v on entry to st reaches st2
    $db relation reachesNonUniquely v st st2
    ;				# The assignment at st of v has another
    ;				# reaching definition at st2
















    $db relation hasCantBeIntOperand st;
    ;				# st has a non-integer operand on at least one
    ;				# code path
    $db relation hasCantBeDoubleOperand st;
    ;				# st has a non-real operand on at least one
    ;				# code path
}
................................................................................
#
# Side effects:
#	Values are loaded into the given relations.

proc quads-list-dataflows {quads vars} {
    global verbose

    # There is an artificial entry block whose index is two slots
    # beyond the end of the input list, and an artificial exit block
    # at one slot beyond the end of the input list. The entry writes
    # all variables (this rule detects uninitialized values)

    set fakeEntry [expr {[llength $quads] + 1}]
    loadSeq $fakeEntry 0
    dict for {vname vnum} $vars {
	loadWrites $fakeEntry $vnum
    }

    # Walk through the instructions, and record their effects on dataflow.

    set i 0
    foreach insn $quads {
	switch -exact -- [lindex $insn 1 0] {
	    temp -
	    var {
................................................................................
	define-db-methods $vbits $stbits
    } else {
	db clearAll
    }
    return $vars
}


proc define-db-methods {vbits stbits} {
    
    #########################################################################
    #
    # Now that the database schema is loaded, it is possible to compile
    # Datalog code against it. The compiler requires the relation
    # definitions and the bit assignments of the domains, so cannot be run
................................................................................
    #	None
    #
    # Side effects:
    #	The reaching definitions are added to the database in the
    #	'flowsPast' and 'reaches' relations.
    
    db datalogMethod reachdef {quads} {
	set fakeEntry [expr {[llength $quads] + 1}]
    } {
	% The flowspast relation tracks code paths that are transparent to a var
	flowsPast(_, st, st2) :- seq(st, st2).
	flowsPast(v, st3, st2) :- flowsPast(v, st3, st),
                                  !writes(st, v),
                                  flowsPast(v, st, st2).

................................................................................
    #	A.
    #
    # In this case, the operation at A can be replaced with v2 :- ...,
    # and all the copies B can be elided
    
    db datalogMethod revprop {quads vnames} {
	set dead {}
	set fakeEntry [expr {[llength $quads] + 1}]
    } {
	
	% An assignment of variable v at statement st can be replaced with
	% variable v2 to eliminate a copy of v to v2 at statement st2 if
	% there's no reason not to do so.
	
	revprop(v, v2, st, st2) :- reaches(v, st, st2),
................................................................................
	cantRevprop2(_, v2, _, st2) :- !writes(st2, v2).
	cantRevprop2(v, _, st, st2) :- reaches(v, st3, st2), st3 != st.

	% Reverse propagation can't eliminate anything from the 
	% function prelude. (Otherwise, it will try to rename literals
	% to the variables into which they're copied.)

	cantRevprop(_, _, $fakeEntry).
	
	revprop(v, v2, st, st2)?

    } d {
	dict with d {
	    lset quads $st 1 [lindex $vnames $v2]
	    dict set dead $st2 {}
	}
................................................................................
    #	None. Results appear in the databse
    #
    # Side effects:
    #   The relations, 'reachable', 'reachableWithout' and 'dom'
    #   are stored back into the database.

    db datalogMethod dominators {quads} {
	set fakeEntry [expr {[llength $quads] + 1}]
    } {
	% What statements are reachable from the entry point?
	reachable($fakeEntry).
	reachable(st2) :- reachable(st), seq(st, st2).
	
	% What statements st are reachable without visiting statement st2?
	reachableWithout($fakeEntry, _).
	reachableWithout(st2, st3) :- reachableWithout(st, st3),
	                              seq(st, st2),
                                      st2 != st3.

	% The entry dominates everything
	dom($fakeEntry, st) :- reachable(st).

	% Every reachable statement dominates itself.
	dom(st, st2) :- reachable(st), st = st2.
	
	% Statement st dominates st2 if there is no path from the entry to
	% st2 that does not visit st.
	dom(st, st2) :- reachable(st2), !reachableWithout(st2, st).
................................................................................
		}
	    }
	    lset quads $pc $newquad
	    incr pc
	}
	return $quads
    }
































































































































































































    # types1 --
    #
    #	Attempts to assign data type to program variables in SSA form.
    #
    # Parameters:
    #	vnames - List of variable names 
................................................................................
#	Returns a type code
#
# Totally half-arsed implementation needed to get the LLVM connection going

proc requiredInputType {q v} {
    switch -exact -- [lindex $q 0] {
	invoke {
	    if {[lindex $q 2 0] eq "literal" && [lindex $q 2 2] == -1} {
		switch [lindex [builtinCommandType [lindex $q 2 1]] 0 0] {
		    INT {
			return $dataType::INT
		    }
		    DOUBLE {
			return $dataType::DOUBLE
		    }
................................................................................

proc assignParameterTypes {vtypesVar quads} {
    upvar 1 $vtypesVar vtypes

    # set all parameter types to 'void'

    dict for {v type} $vtypes {
	if {[lindex $v 0] eq {var} && [lindex $v 2] == -1} {
	    dict set vtypes $v $dataType::VOID
	}
    }

    # constrain all parameter types according to how they are used in the
    # three address code

    foreach q $quads {
	foreach v [lrange $q 2 end] {
	    if {[lindex $v 0] eq {var} && [lindex $v 2] == -1} {
		set t [requiredInputType $q $v]
		dict set vtypes $v [expr {[dict get $vtypes $v] & $t}]
	    }
	}
    }
}

................................................................................
	set xx [expr { $xx + 1.0 }]
	set ser [expr { $ser + $cof / $xx }]
	set cof -5.36382e-6
	set xx [expr { $xx + 1.0 }]
	set ser [expr { $ser + $cof / $xx }]
	return [expr { $tmp + log( 2.50662827465 * $ser ) }]
    }

    #
    #########################################################################
    
    # Create bytecode for the example procedure, and translate to three-address
    # code.
    set procname math::ln_Gamma

} else {
    package require math
    auto_load ::math::ln_Gamma
    set procname math::ln_Gamma
}
set bytecode [::tcl::unsupported::getbytecode proc $procname]

................................................................................
    set i 0
    foreach q $quads {
	puts "$i: $q"
	incr i
    }
}

    set vars [initDB $quads]
    quads-list-dataflows $quads $vars
    db reachdef $quads

# Enumerate the data sources for all phi functions
    set phis [db ssa2 $quads]

# Rewrite the three-address code into SSA form
    set quads [db ssa3 $quads [dict keys $vars] $phis]


if {$verbose} {
    puts "After converting to SSA form:"
    set i 0















    foreach q $quads {
	puts "$i: $q"
	incr i
    }
}

# Experimental datalog-based type analysis







>







 







>







 







>












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







 







<
<
<
<
<
<
<
<
<
<
<







 







<







 







<







 







<







 







<
<







 







|


|



|





|







 







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







 







|







 







|









|







 







>





|
>







 







|
|
|


|


|

>



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







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
...
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
...
800
801
802
803
804
805
806











807
808
809
810
811
812
813
...
961
962
963
964
965
966
967

968
969
970
971
972
973
974
....
1035
1036
1037
1038
1039
1040
1041

1042
1043
1044
1045
1046
1047
1048
....
1238
1239
1240
1241
1242
1243
1244

1245
1246
1247
1248
1249
1250
1251
....
1269
1270
1271
1272
1273
1274
1275


1276
1277
1278
1279
1280
1281
1282
....
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
....
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
....
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
....
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
....
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
....
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
		bytecode-stack-state-worker bytecode \
		    $target $head $depth
		# fallthrough to do the 'branch not taken'
	    }
	    add -
	    div -
	    eq -
	    ge -
	    gt -
	    land -
	    le -
	    listIndexImm -
	    lor -
	    lshift -
	    lt -
................................................................................
	}

	# Translate the current bytecode
	switch -exact -- [lindex $insn 0] {
	    add -
	    div -
	    eq -
	    ge -
	    gt -
	    le -
	    lt -
	    mod -
	    lshift -
	    rshift -
	    mult -
................................................................................

    $db relation dom st st2;	# st dominates st2
    $db relation sdom st st2;	# st dominates st2 strictly
    $db relation frontier st st2;
    ;				# st2 is in st's dominance frontier

    $db relation liveat v st;	# v is live on entry to st

    $db relation needphi v st2
    ;				# a phi function is needed at st2 for
    ;				# variable v
    $db relation defreaches isphi v st st2
    ;				# a phi function or unique assignment
    ;                           # for v before st reaches st2
    $db relation valreaches v st st2
    ;				# the value of v on entry to st reaches st2
    $db relation reachesNonUniquely v st st2
    ;				# The assignment at st of v has another
    ;				# reaching definition at st2

    $db relation killsLocal st v
    ;				# The statement st kills visibility of v
    ;				# either by writing v or being a phi that reads
    ;				# v.
    $db relation nonKillingPath v st st2
    ;				# Visibility of value v is preserved (not killed
    ;				# on some path from st to st2.
    $db relation usedOnEntry v st
    ;				# The value v is used on entry to st
    $db relation visibleOnExit v st
    ;				# The value v is visible on exit from st
    $db relation needsFree v st st2
    ;				# The variable v needs to be freed
    ;				# before passing control from st to st2
    
    $db relation hasCantBeIntOperand st;
    ;				# st has a non-integer operand on at least one
    ;				# code path
    $db relation hasCantBeDoubleOperand st;
    ;				# st has a non-real operand on at least one
    ;				# code path
}
................................................................................
#
# Side effects:
#	Values are loaded into the given relations.

proc quads-list-dataflows {quads vars} {
    global verbose












    # Walk through the instructions, and record their effects on dataflow.

    set i 0
    foreach insn $quads {
	switch -exact -- [lindex $insn 1 0] {
	    temp -
	    var {
................................................................................
	define-db-methods $vbits $stbits
    } else {
	db clearAll
    }
    return $vars
}


proc define-db-methods {vbits stbits} {
    
    #########################################################################
    #
    # Now that the database schema is loaded, it is possible to compile
    # Datalog code against it. The compiler requires the relation
    # definitions and the bit assignments of the domains, so cannot be run
................................................................................
    #	None
    #
    # Side effects:
    #	The reaching definitions are added to the database in the
    #	'flowsPast' and 'reaches' relations.
    
    db datalogMethod reachdef {quads} {

    } {
	% The flowspast relation tracks code paths that are transparent to a var
	flowsPast(_, st, st2) :- seq(st, st2).
	flowsPast(v, st3, st2) :- flowsPast(v, st3, st),
                                  !writes(st, v),
                                  flowsPast(v, st, st2).

................................................................................
    #	A.
    #
    # In this case, the operation at A can be replaced with v2 :- ...,
    # and all the copies B can be elided
    
    db datalogMethod revprop {quads vnames} {
	set dead {}

    } {
	
	% An assignment of variable v at statement st can be replaced with
	% variable v2 to eliminate a copy of v to v2 at statement st2 if
	% there's no reason not to do so.
	
	revprop(v, v2, st, st2) :- reaches(v, st, st2),
................................................................................
	cantRevprop2(_, v2, _, st2) :- !writes(st2, v2).
	cantRevprop2(v, _, st, st2) :- reaches(v, st3, st2), st3 != st.

	% Reverse propagation can't eliminate anything from the 
	% function prelude. (Otherwise, it will try to rename literals
	% to the variables into which they're copied.)



	revprop(v, v2, st, st2)?

    } d {
	dict with d {
	    lset quads $st 1 [lindex $vnames $v2]
	    dict set dead $st2 {}
	}
................................................................................
    #	None. Results appear in the databse
    #
    # Side effects:
    #   The relations, 'reachable', 'reachableWithout' and 'dom'
    #   are stored back into the database.

    db datalogMethod dominators {quads} {
	set entryBlock 0
    } {
	% What statements are reachable from the entry point?
	reachable($entryBlock).
	reachable(st2) :- reachable(st), seq(st, st2).
	
	% What statements st are reachable without visiting statement st2?
	reachableWithout($entryBlock, _).
	reachableWithout(st2, st3) :- reachableWithout(st, st3),
	                              seq(st, st2),
                                      st2 != st3.

	% The entry dominates everything
	dom($entryBlock, st) :- reachable(st).

	% Every reachable statement dominates itself.
	dom(st, st2) :- reachable(st), st = st2.
	
	% Statement st dominates st2 if there is no path from the entry to
	% st2 that does not visit st.
	dom(st, st2) :- reachable(st2), !reachableWithout(st2, st).
................................................................................
		}
	    }
	    lset quads $pc $newquad
	    incr pc
	}
	return $quads
    }

    # freeValues --
    #
    #	Determines the points in SSA code at which a value must be freed
    #
    # Parameters:
    #	vnames - List of variable names
    #
    # Results:
    #	Returns a list of triples: {st1 st2 v} where the value v must be
    #	freed when passing control from st1 to st2
    #
    # Side effects:
    #	Leaves liveness information in the database

    db datalogMethod freeValues {quads vnames} {
	set actions {}
    } {

	% A value needs its refcount decremented between st and st2 if
	% st writes the value directly and st2 does not use it.
	needsFree(v, st, st2) :-
		writes(st, v), seq(st, st2), !usedOnEntry(v, st2).

	% A value needs its refcount decremented between st and st2
	% if st uses v, st is not a phi, st2 follows st, and st2 does not
	% use v.
	needsFree(v, st, st2) :-
		usedOnEntry(v, st), !isPhi(st), seq(st, st2), 
		!isLiteral(v), visibleOnExit(v, st), !usedOnEntry(v, st2).

	% A value is visible on exit from a statement if the statement writes
	% the value.
	visibleOnExit(v, st) :- writes(st, v).

	% A value is visible on exit from a statement if a path from a point
	% that writes the value reaches the exit of the statement.
	visibleOnExit(v, st3) :- 
		writes(st, v), 
		seq(st, st2),
		nonKillingPath(v, st2, st3).

	% A value is used on entry to a statement if it is directly
	% read in that statement.
	usedOnEntry(v, st) :- reads(st, v).

	% A value is used on entry to a statement if it is used by another
	% statement st3, and there is a code path from st to st3 that
	% preserves the value
	usedOnEntry(v, st) :-
		nonKillingPath(v, st, st2),
	        seq(st2, st3),
		reads(st3, v).

	% A value is preserved from the entry of st to the exit of
	% st if it's not killed locally
	nonKillingPath(v, st, st2) :- st=st2, !killsLocal(st, v).

	% A value is preserved from the entry of st to the exit of
	% st2 if it can be traced through a series of statements
	% that preserve it
	nonKillingPath(v, st, st2) :- 
		nonKillingPath(v, st, st3), 
		seq(st3, st4),
		nonKillingPath(v, st4, st2).

	% A value is killed locally by a statement if the statement
	% writes the value.
	killsLocal(st, v) :- writes(st, v).

	% A value is killed locally by a statement if the statement
	% is a phi that interacts with it.
	killsLocal(st, v) :- isPhi(st), reads(st, v).

        needsFree(v, st, st2)?
    } d {
	dict with d {
	    dict set actions $st $st2 [lindex $vnames $v] {}
	}
    } {

	# Now we know where memory has to be freed in the program.
	# [dict get $actions $pc $pc2] is a dictionary whose keys are
	# the names of variables that must be freed as control passes
	# from $pc to $pc2.
	#
	# The twist with this rewriting is that if control passes from
	# $pc to $pc2 by branching, we'll need to put the 'free's out of
	# line, alter the branch to jump to them and follow the 'free's
	# with a branch to the original branch target.

	set quads2 {};		# Output instruction sequence
	set fixup {};		# Dictionary whose keys are input instruction
	;			# indices and whose values are lists of
	;			# pairs: the instruction that has the given
	;			# index as a jump target and the operand
	;			# index of the target.
	set addr2 {};		# Dictionary whose keys are input instruction
	;			# locations and whose vlaues are the
	;			# indices of the corresponding output
	;			# instructions
	set returnLoc {};	# [dict get $returnLoc $from $to $j
	;			# is the place to return-jump to when
	;			# inserting code on the arc from $from to
	;			# $to; $j is the index in the instruction
	;			# where the pc has to be fixed

	set i 0
	foreach q $quads {

	    # Record the output instruction index for this input instruction
	    
	    dict set addr2 $i [llength $quads2]
	    
	    # Fix up any forward jumps that target this input instruction
	    
	    if {[dict exists $fixup $i]} {
		foreach f [dict get $fixup $i] {
		    lset quads2 {*}$f 1 [llength $quads2]
		} 
	    }

	    # Rewrite the current instruction, replacing PC references
	    # with the corresponding addresses in the output instruction
	    # stream. Leave forward jumps for any flow graph arcs that
	    # require out-of-line code to be added

	    for {set j 1} {$j < [llength $q]} {incr j} {
		switch -exact -- [lindex $q $j 0] {
		    pc {
			set target [lindex $q $j 1]
			if {[lindex $q 0] ne {phi}
			    && [dict exists $actions $i $target]
			    && $target != $i+1} {
			    dict set returnLoc $i $target $j [llength $quads2]
			    lset q $j 1 ?
			} elseif {[dict exists $addr2 $target]} {
			    lset q $j 1 [dict get $addr2 $target]
			} else {
			    lset q $j 1 ?
			    dict lappend fixup $target [list [llength $quads2] $j]
			}
		    }
		}
	    }

	    lappend quads2 $q

	    # Insert 'free' instructions on the fallthrough path, if any

	    if {[dict exists $actions $i [expr {$i+1}]]} {
		dict for {v -} [dict get $actions $i [expr {$i+1}]] {
		    lappend quads2 [list free {} $v]
		}
		dict unset actions $i [expr {$i+1}]
	    }
	    incr i
	}

	# Now we do the out-of-line actions

	dict for {from l} $actions {
	    dict for {to vs} $l {
		dict for {j newFrom} [dict get $returnLoc $from $to] {
		    lset quads2 $newFrom $j 1 [llength $quads2]
		}
		dict for {v -} $vs {
		    lappend quads2 [list free {} $v]
		}
		lappend quads2 [list jump [list pc [dict get $addr2 $to]]]
	    }
	}

	# If code is using 'assignTypeToVariable', then it will need the
	# PC's of variable origins patched. That also has to be done here

	set i 0
	foreach q $quads2 {
	    set j 0
	    foreach field $q {
		if {[lindex $field 0] in {var literal temp}
		    && [llength $field] >= 3
		    && [dict exists $addr2 [set addr [lindex $field 2]]]} {
		    lset quads2 $i $j 2 [dict get $addr2 $addr]
		}
		incr j
	    }
	    incr i
	}
	return $quads2
    }

    # types1 --
    #
    #	Attempts to assign data type to program variables in SSA form.
    #
    # Parameters:
    #	vnames - List of variable names 
................................................................................
#	Returns a type code
#
# Totally half-arsed implementation needed to get the LLVM connection going

proc requiredInputType {q v} {
    switch -exact -- [lindex $q 0] {
	invoke {
	    if {[lindex $q 2 0] eq "literal" && [llength [lindex $q 2]] < 3} {
		switch [lindex [builtinCommandType [lindex $q 2 1]] 0 0] {
		    INT {
			return $dataType::INT
		    }
		    DOUBLE {
			return $dataType::DOUBLE
		    }
................................................................................

proc assignParameterTypes {vtypesVar quads} {
    upvar 1 $vtypesVar vtypes

    # set all parameter types to 'void'

    dict for {v type} $vtypes {
	if {[lindex $v 0] eq {var} && [llength $v] < 3} {
	    dict set vtypes $v $dataType::VOID
	}
    }

    # constrain all parameter types according to how they are used in the
    # three address code

    foreach q $quads {
	foreach v [lrange $q 2 end] {
	    if {[lindex $v 0] eq {var} && [llength $v] < 3} {
		set t [requiredInputType $q $v]
		dict set vtypes $v [expr {[dict get $vtypes $v] & $t}]
	    }
	}
    }
}

................................................................................
	set xx [expr { $xx + 1.0 }]
	set ser [expr { $ser + $cof / $xx }]
	set cof -5.36382e-6
	set xx [expr { $xx + 1.0 }]
	set ser [expr { $ser + $cof / $xx }]
	return [expr { $tmp + log( 2.50662827465 * $ser ) }]
    }

    #
    #########################################################################
    
    # Create bytecode for the example procedure, and translate to three-address
    # code.
    set procname cos

} else {
    package require math
    auto_load ::math::ln_Gamma
    set procname math::ln_Gamma
}
set bytecode [::tcl::unsupported::getbytecode proc $procname]

................................................................................
    set i 0
    foreach q $quads {
	puts "$i: $q"
	incr i
    }
}

set vars [initDB $quads]
quads-list-dataflows $quads $vars
db reachdef $quads

# Enumerate the data sources for all phi functions
set phis [db ssa2 $quads]

# Rewrite the three-address code into SSA form
set quads [db ssa3 $quads [dict keys $vars] $phis]

set verbose 0
if {$verbose} {
    puts "After converting to SSA form:"
    set i 0
    foreach q $quads {
	puts "$i: $q"
	incr i
    }
}

# Try to determine refcount information

set vars [initDB $quads]
quads-list-dataflows $quads $vars
set quads [db freeValues $quads [dict keys $vars]]

if {$verbose} {
    puts "After memory management:"
    set i 0
    foreach q $quads {
	puts "$i: $q"
	incr i
    }
}

# Experimental datalog-based type analysis