
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::i#2 = phi( main/main::i#0, main::@1/main::i#1 )
  main::screen#2 = phi( main/main::screen#0, main::@1/main::screen#1 )
  main::$3 = *main::screen#2 * SIZEOF_UNSIGNED_INT
  CHAR_COUNTS[main::$3] = ++ CHAR_COUNTS[main::$3]
  main::screen#1 = ++ main::screen#2
  main::i#1 = main::i#2 + rangenext(0,$3e7)
  main::$1 = main::i#1 != rangelast(0,$3e7)
  if(main::$1) 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
__constant const unsigned int CHAR_COUNTS[$100] = { fill( $100, 0) }
__constant char SIZEOF_UNSIGNED_INT = 2
void __start()
void main()
bool main::$1
char main::$3
unsigned int main::i
unsigned int main::i#0
unsigned int main::i#1
unsigned int main::i#2
char *main::screen
char *main::screen#0
char *main::screen#1
char *main::screen#2

Simplifying constant pointer cast (char *) 1024
Successful SSA optimization PassNCastSimplification
Simple Condition main::$1 [8] if(main::i#1!=rangelast(0,$3e7)) 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 [6] main::i#1 = ++ main::i#2 to ++
Resolved ranged comparison value [8] if(main::i#1!=rangelast(0,$3e7)) goto main::@1 to $3e8
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) $3e8 in [5] if(main::i#1!=$3e8) goto main::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast $3e8
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (unsigned int) $3e8
Successful SSA optimization PassNFinalizeNumberTypeConversions
Rewriting multiplication to use shift [1] main::$3 = *main::screen#2 * SIZEOF_UNSIGNED_INT
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
Eliminating unused constant SIZEOF_UNSIGNED_INT
Successful SSA optimization PassNEliminateUnusedVars
Finalized unsigned number type (unsigned int) $100
Finalized unsigned number type (unsigned int) $100
Successful SSA optimization PassNFinalizeNumberTypeConversions
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 [8] main::screen#3 = main::screen#1
Coalesced [9] main::i#3 = main::i#1
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::i#2 = phi( main/0, main::@1/main::i#1 )
  [1] main::screen#2 = phi( main/(char *) 1024, main::@1/main::screen#1 )
  [2] main::$3 = *main::screen#2 << 1
  [3] CHAR_COUNTS[main::$3] = ++ CHAR_COUNTS[main::$3]
  [4] main::screen#1 = ++ main::screen#2
  [5] main::i#1 = ++ main::i#2
  [6] if(main::i#1!=$3e8) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@1
  [7] return 
  to:@return


VARIABLE REGISTER WEIGHTS
void main()
char main::$3 // 33.0
unsigned int main::i
unsigned int main::i#1 // 16.5
unsigned int main::i#2 // 5.5
char *main::screen
char *main::screen#1 // 7.333333333333333
char *main::screen#2 // 11.0

Initial phi equivalence classes
[ main::screen#2 main::screen#1 ]
[ main::i#2 main::i#1 ]
Added variable main::$3 to live range equivalence class [ main::$3 ]
Complete equivalence classes
[ main::screen#2 main::screen#1 ]
[ main::i#2 main::i#1 ]
[ main::$3 ]
Allocated zp[1]:2 [ main::$3 ]
Allocated zp[2]:3 [ main::i#2 main::i#1 ]
Allocated zp[2]:5 [ main::screen#2 main::screen#1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [2] main::$3 = *main::screen#2 << 1 [ main::screen#2 main::i#2 main::$3 ] (  [ main::screen#2 main::i#2 main::$3 ] { }  ) always clobbers reg byte a reg byte y 
Statement [6] if(main::i#1!=$3e8) goto main::@1 [ main::screen#1 main::i#1 ] (  [ main::screen#1 main::i#1 ] { }  ) always clobbers reg byte a 
Potential registers zp[2]:5 [ main::screen#2 main::screen#1 ] : zp[2]:5 , 
Potential registers zp[2]:3 [ main::i#2 main::i#1 ] : zp[2]:3 , 
Potential registers zp[1]:2 [ main::$3 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 33: zp[1]:2 [ main::$3 ] 22: zp[2]:3 [ main::i#2 main::i#1 ] 18.33: zp[2]:5 [ main::screen#2 main::screen#1 ] 
Uplift Scope [] 

Uplifting [main] best 976 combination reg byte a [ main::$3 ] zp[2]:3 [ main::i#2 main::i#1 ] zp[2]:5 [ main::screen#2 main::screen#1 ] 
Uplifting [] best 976 combination 
Allocated (was zp[2]:3) zp[2]:2 [ main::i#2 main::i#1 ]
Allocated (was zp[2]:5) zp[2]:4 [ main::screen#2 main::screen#1 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Inner increment is not being done properly (screen++)
  // Upstart
  // Commodore 64 PRG executable file
.file [name="inner-increment-problem.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: {
    // Count the number of the different chars on the screen
    .label screen = 4
    .label i = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::i#2 = 0 [phi:main->main::@1#0] -- vwuz1=vwuc1 
    lda #<0
    sta.z i
    lda #>0
    sta.z i+1
    // [1] phi main::screen#2 = (char *) 1024 [phi:main->main::@1#1] -- pbuz1=pbuc1 
    lda #<$400
    sta.z screen
    lda #>$400
    sta.z screen+1
    jmp __b1
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
  __b1_from___b1:
    // [1] phi main::i#2 = main::i#1 [phi:main::@1->main::@1#0] -- register_copy 
    // [1] phi main::screen#2 = main::screen#1 [phi:main::@1->main::@1#1] -- register_copy 
    jmp __b1
    // main::@1
  __b1:
    // [2] main::$3 = *main::screen#2 << 1 -- vbuaa=_deref_pbuz1_rol_1 
    ldy #0
    lda (screen),y
    asl
    // [3] CHAR_COUNTS[main::$3] = ++ CHAR_COUNTS[main::$3] -- pwuc1_derefidx_vbuaa=_inc_pwuc1_derefidx_vbuaa 
    tax
    inc CHAR_COUNTS,x
    bne !+
    inc CHAR_COUNTS+1,x
  !:
    // [4] main::screen#1 = ++ main::screen#2 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // [5] main::i#1 = ++ main::i#2 -- vwuz1=_inc_vwuz1 
    inc.z i
    bne !+
    inc.z i+1
  !:
    // [6] if(main::i#1!=$3e8) goto main::@1 -- vwuz1_neq_vwuc1_then_la1 
    lda.z i+1
    cmp #>$3e8
    bne __b1_from___b1
    lda.z i
    cmp #<$3e8
    bne __b1_from___b1
    jmp __breturn
    // main::@return
  __breturn:
    // [7] return 
    rts
}
  // File Data
.segment Data
  CHAR_COUNTS: .fill 2*$100, 0

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #>0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Replacing label __b1_from___b1 with __b1
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
__constant const unsigned int CHAR_COUNTS[$100] = { fill( $100, 0) }
void main()
char main::$3 // reg byte a 33.0
unsigned int main::i
unsigned int main::i#1 // i zp[2]:2 16.5
unsigned int main::i#2 // i zp[2]:2 5.5
char *main::screen
char *main::screen#1 // screen zp[2]:4 7.333333333333333
char *main::screen#2 // screen zp[2]:4 11.0

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


FINAL ASSEMBLER
Score: 866

  // File Comments
// Inner increment is not being done properly (screen++)
  // Upstart
  // Commodore 64 PRG executable file
.file [name="inner-increment-problem.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: {
    // Count the number of the different chars on the screen
    .label screen = 4
    .label i = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::i#2 = 0 [phi:main->main::@1#0] -- vwuz1=vwuc1 
    lda #<0
    sta.z i
    sta.z i+1
    // [1] phi main::screen#2 = (char *) 1024 [phi:main->main::@1#1] -- pbuz1=pbuc1 
    lda #<$400
    sta.z screen
    lda #>$400
    sta.z screen+1
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
    // [1] phi main::i#2 = main::i#1 [phi:main::@1->main::@1#0] -- register_copy 
    // [1] phi main::screen#2 = main::screen#1 [phi:main::@1->main::@1#1] -- register_copy 
    // main::@1
  __b1:
    // CHAR_COUNTS[*screen++]++;
    // [2] main::$3 = *main::screen#2 << 1 -- vbuaa=_deref_pbuz1_rol_1 
    ldy #0
    lda (screen),y
    asl
    // [3] CHAR_COUNTS[main::$3] = ++ CHAR_COUNTS[main::$3] -- pwuc1_derefidx_vbuaa=_inc_pwuc1_derefidx_vbuaa 
    tax
    inc CHAR_COUNTS,x
    bne !+
    inc CHAR_COUNTS+1,x
  !:
    // [4] main::screen#1 = ++ main::screen#2 -- pbuz1=_inc_pbuz1 
    inc.z screen
    bne !+
    inc.z screen+1
  !:
    // for( word i:0..999)
    // [5] main::i#1 = ++ main::i#2 -- vwuz1=_inc_vwuz1 
    inc.z i
    bne !+
    inc.z i+1
  !:
    // [6] if(main::i#1!=$3e8) goto main::@1 -- vwuz1_neq_vwuc1_then_la1 
    lda.z i+1
    cmp #>$3e8
    bne __b1
    lda.z i
    cmp #<$3e8
    bne __b1
    // main::@return
    // }
    // [7] return 
    rts
}
  // File Data
.segment Data
  CHAR_COUNTS: .fill 2*$100, 0

