
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::idx#0 = 0
  main::i#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@1
  main::idx#4 = phi( main/main::idx#0, main::@1/main::idx#3 )
  main::i#2 = phi( main/main::i#0, main::@1/main::i#1 )
  main::$3 = main::i#2 * SIZEOF_UNSIGNED_INT
  main::w#0 = main::words[main::$3]
  main::$0 = byte1  main::w#0
  main::SCREEN[main::idx#4] = main::$0
  main::idx#1 = ++ main::idx#4
  main::$1 = byte0  main::w#0
  main::SCREEN[main::idx#1] = main::$1
  main::idx#2 = ++ main::idx#1
  main::idx#3 = ++ main::idx#2
  main::i#1 = main::i#2 + rangenext(0,3)
  main::$2 = main::i#1 != rangelast(0,3)
  if(main::$2) 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 char SIZEOF_UNSIGNED_INT = 2
void __start()
void main()
char main::$0
char main::$1
bool main::$2
char main::$3
__constant char * const main::SCREEN = (char *)$400
char main::i
char main::i#0
char main::i#1
char main::i#2
char main::idx
char main::idx#0
char main::idx#1
char main::idx#2
char main::idx#3
char main::idx#4
unsigned int main::w
unsigned int main::w#0
__constant unsigned int main::words[] = { $3031, $3233, $3435, $3637 }

Simplifying constant pointer cast (char *) 1024
Successful SSA optimization PassNCastSimplification
Simple Condition main::$2 [14] if(main::i#1!=rangelast(0,3)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::idx#0 = 0
Constant main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [12] main::i#1 = ++ main::i#2 to ++
Resolved ranged comparison value [14] if(main::i#1!=rangelast(0,3)) goto main::@1 to 4
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) 4 in [11] if(main::i#1!=4) goto main::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast 4
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 4
Successful SSA optimization PassNFinalizeNumberTypeConversions
Rewriting multiplication to use shift [1] main::$3 = main::i#2 * SIZEOF_UNSIGNED_INT
Successful SSA optimization Pass2MultiplyToShiftRewriting
Inlining constant with var siblings main::idx#0
Inlining constant with var siblings main::i#0
Constant inlined main::i#0 = 0
Constant inlined main::idx#0 = 0
Successful SSA optimization Pass2ConstantInlining
Eliminating unused constant SIZEOF_UNSIGNED_INT
Successful SSA optimization PassNEliminateUnusedVars
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 [14] main::i#3 = main::i#1
Coalesced [15] main::idx#5 = main::idx#3
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::idx#4 = phi( main/0, main::@1/main::idx#3 )
  [1] main::i#2 = phi( main/0, main::@1/main::i#1 )
  [2] main::$3 = main::i#2 << 1
  [3] main::w#0 = main::words[main::$3]
  [4] main::$0 = byte1  main::w#0
  [5] main::SCREEN[main::idx#4] = main::$0
  [6] main::idx#1 = ++ main::idx#4
  [7] main::$1 = byte0  main::w#0
  [8] main::SCREEN[main::idx#1] = main::$1
  [9] main::idx#2 = ++ main::idx#1
  [10] main::idx#3 = ++ main::idx#2
  [11] main::i#1 = ++ main::i#2
  [12] if(main::i#1!=4) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@1
  [13] return 
  to:@return


VARIABLE REGISTER WEIGHTS
void main()
char main::$0 // 22.0
char main::$1 // 22.0
char main::$3 // 22.0
char main::i
char main::i#1 // 16.5
char main::i#2 // 3.3000000000000003
char main::idx
char main::idx#1 // 11.0
char main::idx#2 // 22.0
char main::idx#3 // 7.333333333333333
char main::idx#4 // 6.6000000000000005
unsigned int main::w
unsigned int main::w#0 // 8.25

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

Uplifting [main] best 981 combination reg byte a [ main::$3 ] reg byte a [ main::$0 ] reg byte a [ main::$1 ] reg byte a [ main::idx#2 ] zp[1]:6 [ main::i#2 main::i#1 ] zp[1]:7 [ main::idx#4 main::idx#3 ] zp[1]:8 [ main::idx#1 ] zp[2]:9 [ main::w#0 ] 
Limited combination testing to 100 combinations of 9216 possible.
Uplifting [] best 981 combination 
Attempting to uplift remaining variables inzp[1]:6 [ main::i#2 main::i#1 ]
Uplifting [main] best 881 combination reg byte x [ main::i#2 main::i#1 ] 
Attempting to uplift remaining variables inzp[1]:7 [ main::idx#4 main::idx#3 ]
Uplifting [main] best 881 combination zp[1]:7 [ main::idx#4 main::idx#3 ] 
Attempting to uplift remaining variables inzp[1]:8 [ main::idx#1 ]
Uplifting [main] best 791 combination reg byte y [ main::idx#1 ] 
Allocated (was zp[1]:7) zp[1]:2 [ main::idx#4 main::idx#3 ]
Allocated (was zp[2]:9) zp[2]:3 [ main::w#0 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Tests a simple word array
  // Upstart
  // Commodore 64 PRG executable file
.file [name="word-array-1.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 = $400
    .label w = 3
    .label idx = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::idx#4 = 0 [phi:main->main::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta.z idx
    // [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::idx#4 = main::idx#3 [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::w#0 = main::words[main::$3] -- vwuz1=pwuc1_derefidx_vbuaa 
    tay
    lda words,y
    sta.z w
    lda words+1,y
    sta.z w+1
    // [4] main::$0 = byte1  main::w#0 -- vbuaa=_byte1_vwuz1 
    lda.z w+1
    // [5] main::SCREEN[main::idx#4] = main::$0 -- pbuc1_derefidx_vbuz1=vbuaa 
    ldy.z idx
    sta SCREEN,y
    // [6] main::idx#1 = ++ main::idx#4 -- vbuyy=_inc_vbuz1 
    ldy.z idx
    iny
    // [7] main::$1 = byte0  main::w#0 -- vbuaa=_byte0_vwuz1 
    lda.z w
    // [8] main::SCREEN[main::idx#1] = main::$1 -- pbuc1_derefidx_vbuyy=vbuaa 
    sta SCREEN,y
    // [9] main::idx#2 = ++ main::idx#1 -- vbuaa=_inc_vbuyy 
    iny
    tya
    // [10] main::idx#3 = ++ main::idx#2 -- vbuz1=_inc_vbuaa 
    clc
    adc #1
    sta.z idx
    // [11] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [12] if(main::i#1!=4) goto main::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #4
    bne __b1_from___b1
    jmp __breturn
    // main::@return
  __breturn:
    // [13] return 
    rts
  .segment Data
    // Clever word array that represents C64 numbers 0-7
    words: .word $3031, $3233, $3435, $3637
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing instruction ldx #0 with TAX
Removing instruction lda.z w+1
Removing instruction ldy.z idx
Succesful ASM optimization Pass5UnnecesaryLoadElimination
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::$3 // reg byte a 22.0
__constant char * const main::SCREEN = (char *) 1024
char main::i
char main::i#1 // reg byte x 16.5
char main::i#2 // reg byte x 3.3000000000000003
char main::idx
char main::idx#1 // reg byte y 11.0
char main::idx#2 // reg byte a 22.0
char main::idx#3 // idx zp[1]:2 7.333333333333333
char main::idx#4 // idx zp[1]:2 6.6000000000000005
unsigned int main::w
unsigned int main::w#0 // w zp[2]:3 8.25
__constant unsigned int main::words[] = { $3031, $3233, $3435, $3637 }

reg byte x [ main::i#2 main::i#1 ]
zp[1]:2 [ main::idx#4 main::idx#3 ]
reg byte a [ main::$3 ]
zp[2]:3 [ main::w#0 ]
reg byte a [ main::$0 ]
reg byte y [ main::idx#1 ]
reg byte a [ main::$1 ]
reg byte a [ main::idx#2 ]


FINAL ASSEMBLER
Score: 641

  // File Comments
// Tests a simple word array
  // Upstart
  // Commodore 64 PRG executable file
.file [name="word-array-1.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 = $400
    .label w = 3
    .label idx = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::idx#4 = 0 [phi:main->main::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta.z idx
    // [1] phi main::i#2 = 0 [phi:main->main::@1#1] -- vbuxx=vbuc1 
    tax
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
    // [1] phi main::idx#4 = main::idx#3 [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:
    // word w = words[i]
    // [2] main::$3 = main::i#2 << 1 -- vbuaa=vbuxx_rol_1 
    txa
    asl
    // [3] main::w#0 = main::words[main::$3] -- vwuz1=pwuc1_derefidx_vbuaa 
    tay
    lda words,y
    sta.z w
    lda words+1,y
    sta.z w+1
    // BYTE1(w)
    // [4] main::$0 = byte1  main::w#0 -- vbuaa=_byte1_vwuz1 
    // SCREEN[idx++] = BYTE1(w)
    // [5] main::SCREEN[main::idx#4] = main::$0 -- pbuc1_derefidx_vbuz1=vbuaa 
    ldy.z idx
    sta SCREEN,y
    // SCREEN[idx++] = BYTE1(w);
    // [6] main::idx#1 = ++ main::idx#4 -- vbuyy=_inc_vbuz1 
    iny
    // BYTE0(w)
    // [7] main::$1 = byte0  main::w#0 -- vbuaa=_byte0_vwuz1 
    lda.z w
    // SCREEN[idx++] = BYTE0(w)
    // [8] main::SCREEN[main::idx#1] = main::$1 -- pbuc1_derefidx_vbuyy=vbuaa 
    sta SCREEN,y
    // SCREEN[idx++] = BYTE0(w);
    // [9] main::idx#2 = ++ main::idx#1 -- vbuaa=_inc_vbuyy 
    iny
    tya
    // idx++;
    // [10] main::idx#3 = ++ main::idx#2 -- vbuz1=_inc_vbuaa 
    clc
    adc #1
    sta.z idx
    // for( byte i: 0..3)
    // [11] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [12] if(main::i#1!=4) goto main::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #4
    bne __b1
    // main::@return
    // }
    // [13] return 
    rts
  .segment Data
    // Clever word array that represents C64 numbers 0-7
    words: .word $3031, $3233, $3435, $3637
}
  // File Data

