
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 + 1
  main::$1 = main::$0 * 2
  *main::screen#3 = main::$1
  main::screen#1 = ++ main::screen#3
  main::$2 = main::i#2 + 1
  main::$3 = main::$2 * 2
  *main::screen#1 = main::$3
  main::screen#2 = ++ main::screen#1
  main::i#1 = main::i#2 + rangenext(0,2)
  main::$4 = main::i#1 != rangelast(0,2)
  if(main::$4) 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
bool main::$4
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) 1 in main::$0 = main::i#2 + 1
Adding number conversion cast (unumber) main::$0 in main::$0 = main::i#2 + (unumber)1
Adding number conversion cast (unumber) 2 in main::$1 = main::$0 * 2
Adding number conversion cast (unumber) main::$1 in main::$1 = main::$0 * (unumber)2
Adding number conversion cast (unumber) 1 in main::$2 = main::i#2 + 1
Adding number conversion cast (unumber) main::$2 in main::$2 = main::i#2 + (unumber)1
Adding number conversion cast (unumber) 2 in main::$3 = main::$2 * 2
Adding number conversion cast (unumber) main::$3 in main::$3 = main::$2 * (unumber)2
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast 1
Simplifying constant integer cast 2
Simplifying constant integer cast 1
Simplifying constant integer cast 2
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 1
Finalized unsigned number type (char) 2
Finalized unsigned number type (char) 1
Finalized unsigned number type (char) 2
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to char in main::$0 = main::i#2 + 1
Inferred type updated to char in main::$1 = main::$0 * 2
Inferred type updated to char in main::$2 = main::i#2 + 1
Inferred type updated to char in main::$3 = main::$2 * 2
Simple Condition main::$4 [13] 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 [11] main::i#1 = ++ main::i#2 to ++
Resolved ranged comparison value [13] 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 [10] 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
Rewriting multiplication to use shift [2] main::$1 = main::$0 * 2
Rewriting multiplication to use shift [6] main::$3 = main::$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::$0 = main::i#2 + 1
  [3] main::$1 = main::$0 << 1
  [4] *main::screen#3 = main::$1
  [5] main::screen#1 = ++ main::screen#3
  [6] main::$2 = main::i#2 + 1
  [7] main::$3 = main::$2 << 1
  [8] *main::screen#1 = main::$3
  [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::$0 // 22.0
char main::$1 // 22.0
char main::$2 // 22.0
char main::$3 // 22.0
char main::i
char main::i#1 // 16.5
char main::i#2 // 4.888888888888889
char *main::screen
char *main::screen#1 // 8.25
char *main::screen#2 // 7.333333333333333
char *main::screen#3 // 8.25

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

REGISTER UPLIFT SCOPES
Uplift Scope [main] 22: zp[1]:2 [ main::$0 ] 22: zp[1]:3 [ main::$1 ] 22: zp[1]:4 [ main::$2 ] 22: zp[1]:5 [ main::$3 ] 21.39: zp[1]:6 [ main::i#2 main::i#1 ] 15.58: zp[2]:7 [ main::screen#3 main::screen#2 ] 8.25: zp[2]:9 [ main::screen#1 ] 
Uplift Scope [] 

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

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Tests optimization of identical sub-expressions
  // Upstart
  // Commodore 64 PRG executable file
.file [name="subexpr-optimize-2.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 screen = 2
    .label i = 4
    // [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] -- vbuz1=vbuc1 
    lda #0
    sta.z i
    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::$0 = main::i#2 + 1 -- vbuaa=vbuz1_plus_1 
    lda.z i
    clc
    adc #1
    // [3] main::$1 = main::$0 << 1 -- vbuaa=vbuaa_rol_1 
    asl
    // [4] *main::screen#3 = main::$1 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // [5] main::screen#1 = ++ main::screen#3 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // [6] main::$2 = main::i#2 + 1 -- vbuaa=vbuz1_plus_1 
    lda.z i
    clc
    adc #1
    // [7] main::$3 = main::$2 << 1 -- vbuaa=vbuaa_rol_1 
    asl
    // [8] *main::screen#1 = main::$3 -- _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 -- vbuz1=_inc_vbuz1 
    inc.z i
    // [11] if(main::i#1!=3) goto main::@1 -- vbuz1_neq_vbuc1_then_la1 
    lda #3
    cmp.z i
    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::$0 // reg byte a 22.0
char main::$1 // reg byte a 22.0
char main::$2 // reg byte a 22.0
char main::$3 // reg byte a 22.0
char main::i
char main::i#1 // i zp[1]:4 16.5
char main::i#2 // i zp[1]:4 4.888888888888889
char *main::screen
char *main::screen#1 // screen zp[2]:2 8.25
char *main::screen#2 // screen zp[2]:2 7.333333333333333
char *main::screen#3 // screen zp[2]:2 8.25

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


FINAL ASSEMBLER
Score: 871

  // File Comments
// Tests optimization of identical sub-expressions
  // Upstart
  // Commodore 64 PRG executable file
.file [name="subexpr-optimize-2.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 screen = 2
    .label i = 4
    // [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] -- vbuz1=vbuc1 
    lda #0
    sta.z i
    // [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+1
    // [2] main::$0 = main::i#2 + 1 -- vbuaa=vbuz1_plus_1 
    lda.z i
    clc
    adc #1
    // (i+1)*2
    // [3] main::$1 = main::$0 << 1 -- vbuaa=vbuaa_rol_1 
    asl
    // *screen++ = (i+1)*2
    // [4] *main::screen#3 = main::$1 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // *screen++ = (i+1)*2;
    // [5] main::screen#1 = ++ main::screen#3 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // i+1
    // [6] main::$2 = main::i#2 + 1 -- vbuaa=vbuz1_plus_1 
    lda.z i
    clc
    adc #1
    // (i+1)*2
    // [7] main::$3 = main::$2 << 1 -- vbuaa=vbuaa_rol_1 
    asl
    // *screen++ = (i+1)*2
    // [8] *main::screen#1 = main::$3 -- _deref_pbuz1=vbuaa 
    ldy #0
    sta (screen),y
    // *screen++ = (i+1)*2;
    // [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 -- vbuz1=_inc_vbuz1 
    inc.z i
    // [11] if(main::i#1!=3) goto main::@1 -- vbuz1_neq_vbuc1_then_la1 
    lda #3
    cmp.z i
    bne __b1
    // main::@return
    // }
    // [12] return 
    rts
}
  // File Data

