
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::screen#0 = (char *)$400
  main::i#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@1
  main::screen#3 = phi( main/main::screen#0, main::@1/main::screen#2 )
  main::i#2 = phi( main/main::i#0, main::@1/main::i#1 )
  main::$0 = main::i#2 * 2
  main::$1 = main::$0 + main::i#2
  main::$2 = main::$1 + 3
  *main::screen#3 = main::$2
  main::screen#1 = ++ main::screen#3
  main::$3 = main::i#2 * 2
  main::$4 = main::$3 + main::i#2
  main::$5 = main::$4 + 3
  *main::screen#1 = main::$5
  main::screen#2 = ++ main::screen#1
  main::i#1 = main::i#2 + rangenext(0,2)
  main::$6 = main::i#1 != rangelast(0,2)
  if(main::$6) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@1
  return 
  to:@return

void __start()
__start: scope:[__start]  from
  call main
  to:__start::@1
__start::@1: scope:[__start]  from __start
  to:__start::@return
__start::@return: scope:[__start]  from __start::@1
  return 
  to:@return

SYMBOL TABLE SSA
void __start()
void main()
number main::$0
number main::$1
number main::$2
number main::$3
number main::$4
number main::$5
bool main::$6
char main::i
char main::i#0
char main::i#1
char main::i#2
char *main::screen
char *main::screen#0
char *main::screen#1
char *main::screen#2
char *main::screen#3

Adding number conversion cast (unumber) 2 in main::$0 = main::i#2 * 2
Adding number conversion cast (unumber) main::$0 in main::$0 = main::i#2 * (unumber)2
Adding number conversion cast (unumber) main::$1 in main::$1 = main::$0 + main::i#2
Adding number conversion cast (unumber) 3 in main::$2 = main::$1 + 3
Adding number conversion cast (unumber) main::$2 in main::$2 = main::$1 + (unumber)3
Adding number conversion cast (unumber) 2 in main::$3 = main::i#2 * 2
Adding number conversion cast (unumber) main::$3 in main::$3 = main::i#2 * (unumber)2
Adding number conversion cast (unumber) main::$4 in main::$4 = main::$3 + main::i#2
Adding number conversion cast (unumber) 3 in main::$5 = main::$4 + 3
Adding number conversion cast (unumber) main::$5 in main::$5 = main::$4 + (unumber)3
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast 2
Simplifying constant integer cast 3
Simplifying constant integer cast 2
Simplifying constant integer cast 3
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 2
Finalized unsigned number type (char) 3
Finalized unsigned number type (char) 2
Finalized unsigned number type (char) 3
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to char in main::$0 = main::i#2 * 2
Inferred type updated to char in main::$1 = main::$0 + main::i#2
Inferred type updated to char in main::$2 = main::$1 + 3
Inferred type updated to char in main::$3 = main::i#2 * 2
Inferred type updated to char in main::$4 = main::$3 + main::i#2
Inferred type updated to char in main::$5 = main::$4 + 3
Identified duplicate assignment right side [8] main::$3 = main::i#2 * 2
Successful SSA optimization Pass2DuplicateRValueIdentification
Simple Condition main::$6 [15] if(main::i#1!=rangelast(0,2)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::screen#0 = (char *) 1024
Constant main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [13] main::i#1 = ++ main::i#2 to ++
Resolved ranged comparison value [15] if(main::i#1!=rangelast(0,2)) goto main::@1 to 3
Removing unused procedure __start
Removing unused procedure block __start
Removing unused procedure block __start::@1
Removing unused procedure block __start::@return
Successful SSA optimization PassNEliminateEmptyStart
Adding number conversion cast (unumber) 3 in [12] if(main::i#1!=3) goto main::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast 3
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 3
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias main::$3 = main::$0 
Successful SSA optimization Pass2AliasElimination
Identified duplicate assignment right side [7] main::$4 = main::$3 + main::i#2
Successful SSA optimization Pass2DuplicateRValueIdentification
Alias main::$4 = main::$1 
Successful SSA optimization Pass2AliasElimination
Rewriting multiplication to use shift [1] main::$3 = main::i#2 * 2
Successful SSA optimization Pass2MultiplyToShiftRewriting
Inlining constant with var siblings main::screen#0
Inlining constant with var siblings main::i#0
Constant inlined main::screen#0 = (char *) 1024
Constant inlined main::i#0 = 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting main::@2(between main::@1 and main::@1)
Adding NOP phi() at start of main
CALL GRAPH

Created 2 initial phi equivalence classes
Coalesced [13] main::i#3 = main::i#1
Coalesced [14] main::screen#4 = main::screen#2
Coalesced down to 2 phi equivalence classes
Culled Empty Block label main::@2
Adding NOP phi() at start of main

FINAL CONTROL FLOW GRAPH

void main()
main: scope:[main]  from
  [0] phi()
  to:main::@1
main::@1: scope:[main]  from main main::@1
  [1] main::screen#3 = phi( main/(char *) 1024, main::@1/main::screen#2 )
  [1] main::i#2 = phi( main/0, main::@1/main::i#1 )
  [2] main::$3 = main::i#2 << 1
  [3] main::$4 = main::$3 + main::i#2
  [4] main::$2 = main::$4 + 3
  [5] *main::screen#3 = main::$2
  [6] main::screen#1 = ++ main::screen#3
  [7] main::$5 = main::$4 + 3
  [8] *main::screen#1 = main::$5
  [9] main::screen#2 = ++ main::screen#1
  [10] main::i#1 = ++ main::i#2
  [11] if(main::i#1!=3) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@1
  [12] return 
  to:@return


VARIABLE REGISTER WEIGHTS
void main()
char main::$2 // 22.0
char main::$3 // 22.0
char main::$4 // 8.25
char main::$5 // 22.0
char main::i
char main::i#1 // 16.5
char main::i#2 // 4.888888888888889
char *main::screen
char *main::screen#1 // 11.0
char *main::screen#2 // 7.333333333333333
char *main::screen#3 // 6.6000000000000005

Initial phi equivalence classes
[ main::i#2 main::i#1 ]
[ main::screen#3 main::screen#2 ]
Added variable main::$3 to live range equivalence class [ main::$3 ]
Added variable main::$4 to live range equivalence class [ main::$4 ]
Added variable main::$2 to live range equivalence class [ main::$2 ]
Added variable main::screen#1 to live range equivalence class [ main::screen#1 ]
Added variable main::$5 to live range equivalence class [ main::$5 ]
Complete equivalence classes
[ main::i#2 main::i#1 ]
[ main::screen#3 main::screen#2 ]
[ main::$3 ]
[ main::$4 ]
[ main::$2 ]
[ main::screen#1 ]
[ main::$5 ]
Allocated zp[1]:2 [ main::$3 ]
Allocated zp[1]:3 [ main::$2 ]
Allocated zp[1]:4 [ main::$5 ]
Allocated zp[1]:5 [ main::i#2 main::i#1 ]
Allocated zp[2]:6 [ main::screen#3 main::screen#2 ]
Allocated zp[2]:8 [ main::screen#1 ]
Allocated zp[1]:10 [ main::$4 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [2] main::$3 = main::i#2 << 1 [ main::i#2 main::screen#3 main::$3 ] (  [ main::i#2 main::screen#3 main::$3 ] { }  ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:5 [ main::i#2 main::i#1 ]
Statement [3] main::$4 = main::$3 + main::i#2 [ main::i#2 main::screen#3 main::$4 ] (  [ main::i#2 main::screen#3 main::$4 ] { }  ) always clobbers reg byte a 
Statement [5] *main::screen#3 = main::$2 [ main::i#2 main::screen#3 main::$4 ] (  [ main::i#2 main::screen#3 main::$4 ] { }  ) always clobbers reg byte y 
Removing always clobbered register reg byte y as potential for zp[1]:5 [ main::i#2 main::i#1 ]
Removing always clobbered register reg byte y as potential for zp[1]:10 [ main::$4 ]
Statement [6] main::screen#1 = ++ main::screen#3 [ main::i#2 main::$4 main::screen#1 ] (  [ main::i#2 main::$4 main::screen#1 ] { }  ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:10 [ main::$4 ]
Statement [7] main::$5 = main::$4 + 3 [ main::i#2 main::screen#1 main::$5 ] (  [ main::i#2 main::screen#1 main::$5 ] { }  ) always clobbers reg byte a 
Statement [8] *main::screen#1 = main::$5 [ main::i#2 main::screen#1 ] (  [ main::i#2 main::screen#1 ] { }  ) always clobbers reg byte y 
Statement [9] main::screen#2 = ++ main::screen#1 [ main::i#2 main::screen#2 ] (  [ main::i#2 main::screen#2 ] { }  ) always clobbers reg byte a 
Statement [2] main::$3 = main::i#2 << 1 [ main::i#2 main::screen#3 main::$3 ] (  [ main::i#2 main::screen#3 main::$3 ] { }  ) always clobbers reg byte a 
Statement [3] main::$4 = main::$3 + main::i#2 [ main::i#2 main::screen#3 main::$4 ] (  [ main::i#2 main::screen#3 main::$4 ] { }  ) always clobbers reg byte a 
Statement [4] main::$2 = main::$4 + 3 [ main::i#2 main::screen#3 main::$4 main::$2 ] (  [ main::i#2 main::screen#3 main::$4 main::$2 ] { }  ) always clobbers reg byte a 
Statement [5] *main::screen#3 = main::$2 [ main::i#2 main::screen#3 main::$4 ] (  [ main::i#2 main::screen#3 main::$4 ] { }  ) always clobbers reg byte y 
Statement [6] main::screen#1 = ++ main::screen#3 [ main::i#2 main::$4 main::screen#1 ] (  [ main::i#2 main::$4 main::screen#1 ] { }  ) always clobbers reg byte a 
Statement [7] main::$5 = main::$4 + 3 [ main::i#2 main::screen#1 main::$5 ] (  [ main::i#2 main::screen#1 main::$5 ] { }  ) always clobbers reg byte a 
Statement [8] *main::screen#1 = main::$5 [ main::i#2 main::screen#1 ] (  [ main::i#2 main::screen#1 ] { }  ) always clobbers reg byte y 
Statement [9] main::screen#2 = ++ main::screen#1 [ main::i#2 main::screen#2 ] (  [ main::i#2 main::screen#2 ] { }  ) always clobbers reg byte a 
Potential registers zp[1]:5 [ main::i#2 main::i#1 ] : zp[1]:5 , reg byte x , 
Potential registers zp[2]:6 [ main::screen#3 main::screen#2 ] : zp[2]:6 , 
Potential registers zp[1]:2 [ main::$3 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:10 [ main::$4 ] : zp[1]:10 , reg byte x , 
Potential registers zp[1]:3 [ main::$2 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[2]:8 [ main::screen#1 ] : zp[2]:8 , 
Potential registers zp[1]:4 [ main::$5 ] : zp[1]:4 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 22: zp[1]:2 [ main::$3 ] 22: zp[1]:3 [ main::$2 ] 22: zp[1]:4 [ main::$5 ] 21.39: zp[1]:5 [ main::i#2 main::i#1 ] 13.93: zp[2]:6 [ main::screen#3 main::screen#2 ] 11: zp[2]:8 [ main::screen#1 ] 8.25: zp[1]:10 [ main::$4 ] 
Uplift Scope [] 

Uplifting [main] best 1091 combination reg byte a [ main::$3 ] reg byte a [ main::$2 ] reg byte a [ main::$5 ] reg byte x [ main::i#2 main::i#1 ] zp[2]:6 [ main::screen#3 main::screen#2 ] zp[2]:8 [ main::screen#1 ] zp[1]:10 [ main::$4 ] 
Limited combination testing to 100 combinations of 256 possible.
Uplifting [] best 1091 combination 
Attempting to uplift remaining variables inzp[1]:10 [ main::$4 ]
Uplifting [main] best 1091 combination zp[1]:10 [ main::$4 ] 
Coalescing zero page register [ zp[2]:6 [ main::screen#3 main::screen#2 ] ] with [ zp[2]:8 [ main::screen#1 ] ] - score: 2
Allocated (was zp[2]:6) zp[2]:2 [ main::screen#3 main::screen#2 main::screen#1 ]
Allocated (was zp[1]:10) zp[1]:4 [ main::$4 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Tests optimization of identical sub-expressions
  // Upstart
  // Commodore 64 PRG executable file
.file [name="subexpr-optimize-3.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
  // Global Constants & labels
.segment Code
  // main
main: {
    .label __4 = 4
    .label screen = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::screen#3 = (char *) 1024 [phi:main->main::@1#0] -- pbuz1=pbuc1 
    lda #<$400
    sta.z screen
    lda #>$400
    sta.z screen+1
    // [1] phi main::i#2 = 0 [phi:main->main::@1#1] -- vbuxx=vbuc1 
    ldx #0
    jmp __b1
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
  __b1_from___b1:
    // [1] phi main::screen#3 = main::screen#2 [phi:main::@1->main::@1#0] -- register_copy 
    // [1] phi main::i#2 = main::i#1 [phi:main::@1->main::@1#1] -- register_copy 
    jmp __b1
    // main::@1
  __b1:
    // [2] main::$3 = main::i#2 << 1 -- vbuaa=vbuxx_rol_1 
    txa
    asl
    // [3] main::$4 = main::$3 + main::i#2 -- vbuz1=vbuaa_plus_vbuxx 
    stx.z $ff
    clc
    adc.z $ff
    sta.z __4
    // [4] main::$2 = main::$4 + 3 -- vbuaa=vbuz1_plus_vbuc1 
    lda #3
    clc
    adc.z __4
    // [5] *main::screen#3 = main::$2 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // [6] main::screen#1 = ++ main::screen#3 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // [7] main::$5 = main::$4 + 3 -- vbuaa=vbuz1_plus_vbuc1 
    lda #3
    clc
    adc.z __4
    // [8] *main::screen#1 = main::$5 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // [9] main::screen#2 = ++ main::screen#1 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // [10] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [11] if(main::i#1!=3) goto main::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #3
    bne __b1_from___b1
    jmp __breturn
    // main::@return
  __breturn:
    // [12] return 
    rts
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing label __b1_from___b1 with __b1
Removing instruction __b1_from___b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __b1_from_main:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Removing instruction jmp __b1
Succesful ASM optimization Pass5NextJumpElimination

FINAL SYMBOL TABLE
void main()
char main::$2 // reg byte a 22.0
char main::$3 // reg byte a 22.0
char main::$4 // zp[1]:4 8.25
char main::$5 // reg byte a 22.0
char main::i
char main::i#1 // reg byte x 16.5
char main::i#2 // reg byte x 4.888888888888889
char *main::screen
char *main::screen#1 // screen zp[2]:2 11.0
char *main::screen#2 // screen zp[2]:2 7.333333333333333
char *main::screen#3 // screen zp[2]:2 6.6000000000000005

reg byte x [ main::i#2 main::i#1 ]
zp[2]:2 [ main::screen#3 main::screen#2 main::screen#1 ]
reg byte a [ main::$3 ]
zp[1]:4 [ main::$4 ]
reg byte a [ main::$2 ]
reg byte a [ main::$5 ]


FINAL ASSEMBLER
Score: 891

  // File Comments
// Tests optimization of identical sub-expressions
  // Upstart
  // Commodore 64 PRG executable file
.file [name="subexpr-optimize-3.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
  // Global Constants & labels
.segment Code
  // main
main: {
    .label __4 = 4
    .label screen = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::screen#3 = (char *) 1024 [phi:main->main::@1#0] -- pbuz1=pbuc1 
    lda #<$400
    sta.z screen
    lda #>$400
    sta.z screen+1
    // [1] phi main::i#2 = 0 [phi:main->main::@1#1] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
    // [1] phi main::screen#3 = main::screen#2 [phi:main::@1->main::@1#0] -- register_copy 
    // [1] phi main::i#2 = main::i#1 [phi:main::@1->main::@1#1] -- register_copy 
    // main::@1
  __b1:
    // i*2
    // [2] main::$3 = main::i#2 << 1 -- vbuaa=vbuxx_rol_1 
    txa
    asl
    // i*2+i
    // [3] main::$4 = main::$3 + main::i#2 -- vbuz1=vbuaa_plus_vbuxx 
    stx.z $ff
    clc
    adc.z $ff
    sta.z __4
    // i*2+i+3
    // [4] main::$2 = main::$4 + 3 -- vbuaa=vbuz1_plus_vbuc1 
    lda #3
    clc
    adc.z __4
    // *screen++ = i*2+i+3
    // [5] *main::screen#3 = main::$2 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // *screen++ = i*2+i+3;
    // [6] main::screen#1 = ++ main::screen#3 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // i*2+i+3
    // [7] main::$5 = main::$4 + 3 -- vbuaa=vbuz1_plus_vbuc1 
    lda #3
    clc
    adc.z __4
    // *screen++ = i*2+i+3
    // [8] *main::screen#1 = main::$5 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // *screen++ = i*2+i+3;
    // [9] main::screen#2 = ++ main::screen#1 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // for( byte i: 0..2)
    // [10] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [11] if(main::i#1!=3) goto main::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #3
    bne __b1
    // main::@return
    // }
    // [12] return 
    rts
}
  // File Data

