CONTROL FLOW GRAPH SSA @begin: scope:[] from to:@1 (void()) main() main: scope:[main] from @1 (byte) main::a#0 ← (byte) $80 (byte) main::b#0 ← (byte) 2 to:main::@1 main::@1: scope:[main] from main main::@4 main::@5 (byte) main::b#2 ← phi( main/(byte) main::b#0 main::@4/(byte) main::b#4 main::@5/(byte) main::b#1 ) (byte) main::a#2 ← phi( main/(byte) main::a#0 main::@4/(byte) main::a#1 main::@5/(byte) main::a#6 ) (bool~) main::$0 ← (byte) main::a#2 != (byte) main::b#2 if((bool~) main::$0) goto main::@2 to:main::@3 main::@2: scope:[main] from main::@1 (byte) main::b#3 ← phi( main::@1/(byte) main::b#2 ) (byte) main::a#3 ← phi( main::@1/(byte) main::a#2 ) (bool~) main::$1 ← (byte) main::a#3 > (byte) main::b#3 if((bool~) main::$1) goto main::@4 to:main::@5 main::@3: scope:[main] from main::@1 (byte) main::a#4 ← phi( main::@1/(byte) main::a#2 ) *((const byte*) SCREEN) ← (byte) main::a#4 to:main::@return main::@4: scope:[main] from main::@2 (byte) main::b#4 ← phi( main::@2/(byte) main::b#3 ) (byte) main::a#5 ← phi( main::@2/(byte) main::a#3 ) (byte~) main::$3 ← (byte) main::a#5 - (byte) main::b#4 (byte) main::a#1 ← (byte~) main::$3 to:main::@1 main::@5: scope:[main] from main::@2 (byte) main::a#6 ← phi( main::@2/(byte) main::a#3 ) (byte) main::b#5 ← phi( main::@2/(byte) main::b#3 ) (byte~) main::$2 ← (byte) main::b#5 - (byte) main::a#6 (byte) main::b#1 ← (byte~) main::$2 to:main::@1 main::@return: scope:[main] from main::@3 return to:@return @1: scope:[] from @begin call main to:@2 @2: scope:[] from @1 to:@end @end: scope:[] from @2 SYMBOL TABLE SSA (label) @1 (label) @2 (label) @begin (label) @end (const byte*) SCREEN = (byte*)(number) $400 (void()) main() (bool~) main::$0 (bool~) main::$1 (byte~) main::$2 (byte~) main::$3 (label) main::@1 (label) main::@2 (label) main::@3 (label) main::@4 (label) main::@5 (label) main::@return (byte) main::a (byte) main::a#0 (byte) main::a#1 (byte) main::a#2 (byte) main::a#3 (byte) main::a#4 (byte) main::a#5 (byte) main::a#6 (byte) main::b (byte) main::b#0 (byte) main::b#1 (byte) main::b#2 (byte) main::b#3 (byte) main::b#4 (byte) main::b#5 Simplifying constant pointer cast (byte*) 1024 Successful SSA optimization PassNCastSimplification Alias main::a#2 = main::a#3 main::a#4 main::a#5 main::a#6 Alias main::b#2 = main::b#3 main::b#4 main::b#5 Alias main::a#1 = main::$3 Alias main::b#1 = main::$2 Successful SSA optimization Pass2AliasElimination Simple Condition (bool~) main::$0 [4] if((byte) main::a#2!=(byte) main::b#2) goto main::@2 Simple Condition (bool~) main::$1 [6] if((byte) main::a#2>(byte) main::b#2) goto main::@4 Successful SSA optimization Pass2ConditionalJumpSimplification Constant (const byte) main::a#0 = $80 Constant (const byte) main::b#0 = 2 Successful SSA optimization Pass2ConstantIdentification Inlining constant with var siblings (const byte) main::a#0 Inlining constant with var siblings (const byte) main::b#0 Constant inlined main::a#0 = (byte) $80 Constant inlined main::b#0 = (byte) 2 Successful SSA optimization Pass2ConstantInlining Adding NOP phi() at start of @begin Adding NOP phi() at start of @1 Adding NOP phi() at start of @2 Adding NOP phi() at start of @end Adding NOP phi() at start of main CALL GRAPH Calls in [] to main:2 Created 2 initial phi equivalence classes Coalesced (already) [12] main::a#8 ← main::a#2 Coalesced [13] main::b#7 ← main::b#1 Coalesced [15] main::a#7 ← main::a#1 Coalesced (already) [16] main::b#6 ← main::b#2 Coalesced down to 2 phi equivalence classes Culled Empty Block (label) @2 Adding NOP phi() at start of @begin Adding NOP phi() at start of @1 Adding NOP phi() at start of @end Adding NOP phi() at start of main FINAL CONTROL FLOW GRAPH @begin: scope:[] from [0] phi() to:@1 @1: scope:[] from @begin [1] phi() [2] call main to:@end @end: scope:[] from @1 [3] phi() (void()) main() main: scope:[main] from @1 [4] phi() to:main::@1 main::@1: scope:[main] from main main::@4 main::@5 [5] (byte) main::b#2 ← phi( main/(byte) 2 main::@4/(byte) main::b#2 main::@5/(byte) main::b#1 ) [5] (byte) main::a#2 ← phi( main/(byte) $80 main::@4/(byte) main::a#1 main::@5/(byte) main::a#2 ) [6] if((byte) main::a#2!=(byte) main::b#2) goto main::@2 to:main::@3 main::@3: scope:[main] from main::@1 [7] *((const byte*) SCREEN) ← (byte) main::a#2 to:main::@return main::@return: scope:[main] from main::@3 [8] return to:@return main::@2: scope:[main] from main::@1 [9] if((byte) main::a#2>(byte) main::b#2) goto main::@4 to:main::@5 main::@5: scope:[main] from main::@2 [10] (byte) main::b#1 ← (byte) main::b#2 - (byte) main::a#2 to:main::@1 main::@4: scope:[main] from main::@2 [11] (byte) main::a#1 ← (byte) main::a#2 - (byte) main::b#2 to:main::@1 VARIABLE REGISTER WEIGHTS (void()) main() (byte) main::a (byte) main::a#1 202.0 (byte) main::a#2 179.5 (byte) main::b (byte) main::b#1 202.0 (byte) main::b#2 176.75 Initial phi equivalence classes [ main::a#2 main::a#1 ] [ main::b#2 main::b#1 ] Complete equivalence classes [ main::a#2 main::a#1 ] [ main::b#2 main::b#1 ] Allocated zp[1]:2 [ main::a#2 main::a#1 ] Allocated zp[1]:3 [ main::b#2 main::b#1 ] INITIAL ASM Target platform is c64basic / MOS6502X // File Comments // Demonstrates a problem where wrong alive ranges result in clobbering an alive variable // The compiler does not realize that A is alive in the statement b=b-a - and thus can clobber it. // Upstart .pc = $801 "Basic" :BasicUpstart(main) .pc = $80d "Program" // Global Constants & labels .label SCREEN = $400 // @begin __bbegin: // [1] phi from @begin to @1 [phi:@begin->@1] __b1_from___bbegin: jmp __b1 // @1 __b1: // [2] call main // [4] phi from @1 to main [phi:@1->main] main_from___b1: jsr main // [3] phi from @1 to @end [phi:@1->@end] __bend_from___b1: jmp __bend // @end __bend: // main main: { .label a = 2 .label b = 3 // [5] phi from main to main::@1 [phi:main->main::@1] __b1_from_main: // [5] phi (byte) main::b#2 = (byte) 2 [phi:main->main::@1#0] -- vbuz1=vbuc1 lda #2 sta.z b // [5] phi (byte) main::a#2 = (byte) $80 [phi:main->main::@1#1] -- vbuz1=vbuc1 lda #$80 sta.z a jmp __b1 // main::@1 __b1: // [6] if((byte) main::a#2!=(byte) main::b#2) goto main::@2 -- vbuz1_neq_vbuz2_then_la1 lda.z a cmp.z b bne __b2 jmp __b3 // main::@3 __b3: // [7] *((const byte*) SCREEN) ← (byte) main::a#2 -- _deref_pbuc1=vbuz1 lda.z a sta SCREEN jmp __breturn // main::@return __breturn: // [8] return rts // main::@2 __b2: // [9] if((byte) main::a#2>(byte) main::b#2) goto main::@4 -- vbuz1_gt_vbuz2_then_la1 lda.z b cmp.z a bcc __b4 jmp __b5 // main::@5 __b5: // [10] (byte) main::b#1 ← (byte) main::b#2 - (byte) main::a#2 -- vbuz1=vbuz1_minus_vbuz2 lda.z b sec sbc.z a sta.z b // [5] phi from main::@4 main::@5 to main::@1 [phi:main::@4/main::@5->main::@1] __b1_from___b4: __b1_from___b5: // [5] phi (byte) main::b#2 = (byte) main::b#2 [phi:main::@4/main::@5->main::@1#0] -- register_copy // [5] phi (byte) main::a#2 = (byte) main::a#1 [phi:main::@4/main::@5->main::@1#1] -- register_copy jmp __b1 // main::@4 __b4: // [11] (byte) main::a#1 ← (byte) main::a#2 - (byte) main::b#2 -- vbuz1=vbuz1_minus_vbuz2 lda.z a sec sbc.z b sta.z a jmp __b1_from___b4 } // File Data REGISTER UPLIFT POTENTIAL REGISTERS Statement [10] (byte) main::b#1 ← (byte) main::b#2 - (byte) main::a#2 [ main::a#2 main::b#1 ] ( main:2 [ main::a#2 main::b#1 ] { } ) always clobbers reg byte a Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::a#2 main::a#1 ] Statement [11] (byte) main::a#1 ← (byte) main::a#2 - (byte) main::b#2 [ main::b#2 main::a#1 ] ( main:2 [ main::b#2 main::a#1 ] { } ) always clobbers reg byte a Removing always clobbered register reg byte a as potential for zp[1]:3 [ main::b#2 main::b#1 ] Statement [10] (byte) main::b#1 ← (byte) main::b#2 - (byte) main::a#2 [ main::a#2 main::b#1 ] ( main:2 [ main::a#2 main::b#1 ] { } ) always clobbers reg byte a Statement [11] (byte) main::a#1 ← (byte) main::a#2 - (byte) main::b#2 [ main::b#2 main::a#1 ] ( main:2 [ main::b#2 main::a#1 ] { } ) always clobbers reg byte a Potential registers zp[1]:2 [ main::a#2 main::a#1 ] : zp[1]:2 , reg byte x , reg byte y , Potential registers zp[1]:3 [ main::b#2 main::b#1 ] : zp[1]:3 , reg byte x , reg byte y , REGISTER UPLIFT SCOPES Uplift Scope [main] 381.5: zp[1]:2 [ main::a#2 main::a#1 ] 378.75: zp[1]:3 [ main::b#2 main::b#1 ] Uplift Scope [] Uplifting [main] best 568 combination zp[1]:2 [ main::a#2 main::a#1 ] reg byte x [ main::b#2 main::b#1 ] Uplifting [] best 568 combination Attempting to uplift remaining variables inzp[1]:2 [ main::a#2 main::a#1 ] Uplifting [main] best 568 combination zp[1]:2 [ main::a#2 main::a#1 ] ASSEMBLER BEFORE OPTIMIZATION // File Comments // Demonstrates a problem where wrong alive ranges result in clobbering an alive variable // The compiler does not realize that A is alive in the statement b=b-a - and thus can clobber it. // Upstart .pc = $801 "Basic" :BasicUpstart(main) .pc = $80d "Program" // Global Constants & labels .label SCREEN = $400 // @begin __bbegin: // [1] phi from @begin to @1 [phi:@begin->@1] __b1_from___bbegin: jmp __b1 // @1 __b1: // [2] call main // [4] phi from @1 to main [phi:@1->main] main_from___b1: jsr main // [3] phi from @1 to @end [phi:@1->@end] __bend_from___b1: jmp __bend // @end __bend: // main main: { .label a = 2 // [5] phi from main to main::@1 [phi:main->main::@1] __b1_from_main: // [5] phi (byte) main::b#2 = (byte) 2 [phi:main->main::@1#0] -- vbuxx=vbuc1 ldx #2 // [5] phi (byte) main::a#2 = (byte) $80 [phi:main->main::@1#1] -- vbuz1=vbuc1 lda #$80 sta.z a jmp __b1 // main::@1 __b1: // [6] if((byte) main::a#2!=(byte) main::b#2) goto main::@2 -- vbuz1_neq_vbuxx_then_la1 cpx.z a bne __b2 jmp __b3 // main::@3 __b3: // [7] *((const byte*) SCREEN) ← (byte) main::a#2 -- _deref_pbuc1=vbuz1 lda.z a sta SCREEN jmp __breturn // main::@return __breturn: // [8] return rts // main::@2 __b2: // [9] if((byte) main::a#2>(byte) main::b#2) goto main::@4 -- vbuz1_gt_vbuxx_then_la1 cpx.z a bcc __b4 jmp __b5 // main::@5 __b5: // [10] (byte) main::b#1 ← (byte) main::b#2 - (byte) main::a#2 -- vbuxx=vbuxx_minus_vbuz1 txa sec sbc.z a tax // [5] phi from main::@4 main::@5 to main::@1 [phi:main::@4/main::@5->main::@1] __b1_from___b4: __b1_from___b5: // [5] phi (byte) main::b#2 = (byte) main::b#2 [phi:main::@4/main::@5->main::@1#0] -- register_copy // [5] phi (byte) main::a#2 = (byte) main::a#1 [phi:main::@4/main::@5->main::@1#1] -- register_copy jmp __b1 // main::@4 __b4: // [11] (byte) main::a#1 ← (byte) main::a#2 - (byte) main::b#2 -- vbuz1=vbuz1_minus_vbuxx txa eor #$ff sec adc.z a sta.z a jmp __b1_from___b4 } // File Data ASSEMBLER OPTIMIZATIONS Removing instruction jmp __b1 Removing instruction jmp __bend Removing instruction jmp __b1 Removing instruction jmp __b3 Removing instruction jmp __breturn Removing instruction jmp __b5 Succesful ASM optimization Pass5NextJumpElimination Replacing label __b1_from___b4 with __b1_from___b5 Removing instruction __b1_from___bbegin: Removing instruction __b1: Removing instruction main_from___b1: Removing instruction __bend_from___b1: Removing instruction __b1_from___b4: Succesful ASM optimization Pass5RedundantLabelElimination Removing instruction __bbegin: Removing instruction __bend: Removing instruction __b1_from_main: Removing instruction __b3: Removing instruction __breturn: Removing instruction __b5: Succesful ASM optimization Pass5UnusedLabelElimination Removing instruction jsr main Succesful ASM optimization Pass5SkipBegin Skipping double jump to __b1 in jmp __b1_from___b5 Succesful ASM optimization Pass5DoubleJumpElimination Relabelling long label __b1_from___b5 to __b3 Succesful ASM optimization Pass5RelabelLongLabels Removing instruction __b3: Succesful ASM optimization Pass5UnusedLabelElimination FINAL SYMBOL TABLE (label) @1 (label) @begin (label) @end (const byte*) SCREEN = (byte*) 1024 (void()) main() (label) main::@1 (label) main::@2 (label) main::@3 (label) main::@4 (label) main::@5 (label) main::@return (byte) main::a (byte) main::a#1 a zp[1]:2 202.0 (byte) main::a#2 a zp[1]:2 179.5 (byte) main::b (byte) main::b#1 reg byte x 202.0 (byte) main::b#2 reg byte x 176.75 zp[1]:2 [ main::a#2 main::a#1 ] reg byte x [ main::b#2 main::b#1 ] FINAL ASSEMBLER Score: 463 // File Comments // Demonstrates a problem where wrong alive ranges result in clobbering an alive variable // The compiler does not realize that A is alive in the statement b=b-a - and thus can clobber it. // Upstart .pc = $801 "Basic" :BasicUpstart(main) .pc = $80d "Program" // Global Constants & labels .label SCREEN = $400 // @begin // [1] phi from @begin to @1 [phi:@begin->@1] // @1 // [2] call main // [4] phi from @1 to main [phi:@1->main] // [3] phi from @1 to @end [phi:@1->@end] // @end // main main: { .label a = 2 // [5] phi from main to main::@1 [phi:main->main::@1] // [5] phi (byte) main::b#2 = (byte) 2 [phi:main->main::@1#0] -- vbuxx=vbuc1 ldx #2 // [5] phi (byte) main::a#2 = (byte) $80 [phi:main->main::@1#1] -- vbuz1=vbuc1 lda #$80 sta.z a // main::@1 __b1: // while (a!=b) // [6] if((byte) main::a#2!=(byte) main::b#2) goto main::@2 -- vbuz1_neq_vbuxx_then_la1 cpx.z a bne __b2 // main::@3 // *SCREEN = a // [7] *((const byte*) SCREEN) ← (byte) main::a#2 -- _deref_pbuc1=vbuz1 lda.z a sta SCREEN // main::@return // } // [8] return rts // main::@2 __b2: // if(a>b) // [9] if((byte) main::a#2>(byte) main::b#2) goto main::@4 -- vbuz1_gt_vbuxx_then_la1 cpx.z a bcc __b4 // main::@5 // b = b-a // [10] (byte) main::b#1 ← (byte) main::b#2 - (byte) main::a#2 -- vbuxx=vbuxx_minus_vbuz1 txa sec sbc.z a tax // [5] phi from main::@4 main::@5 to main::@1 [phi:main::@4/main::@5->main::@1] // [5] phi (byte) main::b#2 = (byte) main::b#2 [phi:main::@4/main::@5->main::@1#0] -- register_copy // [5] phi (byte) main::a#2 = (byte) main::a#1 [phi:main::@4/main::@5->main::@1#1] -- register_copy jmp __b1 // main::@4 __b4: // a = a-b // [11] (byte) main::a#1 ← (byte) main::a#2 - (byte) main::b#2 -- vbuz1=vbuz1_minus_vbuxx txa eor #$ff sec adc.z a sta.z a jmp __b1 } // File Data