
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::w#0 = (unsigned int)0
  main::j#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@1
  main::j#2 = phi( main/main::j#0, main::@1/main::j#1 )
  main::w#2 = phi( main/main::w#0, main::@1/main::w#1 )
  main::$0 = main::w#2 + main::j#2
  main::w#1 = main::$0
  main::j#1 = main::j#2 + rangenext(0,$a)
  main::$1 = main::j#1 != rangelast(0,$a)
  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
void __start()
void main()
unsigned int main::$0
bool main::$1
char main::j
char main::j#0
char main::j#1
char main::j#2
unsigned int main::w
unsigned int main::w#0
unsigned int main::w#1
unsigned int main::w#2

Simplifying constant integer cast 0
Successful SSA optimization PassNCastSimplification
Alias main::w#1 = main::$0 
Successful SSA optimization Pass2AliasElimination
Simple Condition main::$1 [6] if(main::j#1!=rangelast(0,$a)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::w#0 = 0
Constant main::j#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [4] main::j#1 = ++ main::j#2 to ++
Resolved ranged comparison value [6] if(main::j#1!=rangelast(0,$a)) goto main::@1 to $b
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) $b in [3] if(main::j#1!=$b) goto main::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast $b
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) $b
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inlining constant with var siblings main::w#0
Inlining constant with var siblings main::j#0
Constant inlined main::w#0 = 0
Constant inlined main::j#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 [6] main::w#3 = main::w#1
Coalesced [7] main::j#3 = main::j#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::j#2 = phi( main/0, main::@1/main::j#1 )
  [1] main::w#2 = phi( main/0, main::@1/main::w#1 )
  [2] main::w#1 = main::w#2 + main::j#2
  [3] main::j#1 = ++ main::j#2
  [4] if(main::j#1!=$b) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@1
  [5] return 
  to:@return


VARIABLE REGISTER WEIGHTS
void main()
char main::j
char main::j#1 // 16.5
char main::j#2 // 16.5
unsigned int main::w
unsigned int main::w#1 // 7.333333333333333
unsigned int main::w#2 // 22.0

Initial phi equivalence classes
[ main::w#2 main::w#1 ]
[ main::j#2 main::j#1 ]
Complete equivalence classes
[ main::w#2 main::w#1 ]
[ main::j#2 main::j#1 ]
Allocated zp[1]:2 [ main::j#2 main::j#1 ]
Allocated zp[2]:3 [ main::w#2 main::w#1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [2] main::w#1 = main::w#2 + main::j#2 [ main::j#2 main::w#1 ] (  [ main::j#2 main::w#1 ] { }  ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::j#2 main::j#1 ]
Statement [2] main::w#1 = main::w#2 + main::j#2 [ main::j#2 main::w#1 ] (  [ main::j#2 main::w#1 ] { }  ) always clobbers reg byte a 
Potential registers zp[2]:3 [ main::w#2 main::w#1 ] : zp[2]:3 , 
Potential registers zp[1]:2 [ main::j#2 main::j#1 ] : zp[1]:2 , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 33: zp[1]:2 [ main::j#2 main::j#1 ] 29.33: zp[2]:3 [ main::w#2 main::w#1 ] 
Uplift Scope [] 

Uplifting [main] best 456 combination reg byte x [ main::j#2 main::j#1 ] zp[2]:3 [ main::w#2 main::w#1 ] 
Uplifting [] best 456 combination 
Allocated (was zp[2]:3) zp[2]:2 [ main::w#2 main::w#1 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Tests that immediate zero values are reused - even when assigning to words
  // Upstart
  // Commodore 64 PRG executable file
.file [name="immzero.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 w = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::j#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi main::w#2 = 0 [phi:main->main::@1#1] -- vwuz1=vwuc1 
    lda #<0
    sta.z w
    lda #>0
    sta.z w+1
    jmp __b1
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
  __b1_from___b1:
    // [1] phi main::j#2 = main::j#1 [phi:main::@1->main::@1#0] -- register_copy 
    // [1] phi main::w#2 = main::w#1 [phi:main::@1->main::@1#1] -- register_copy 
    jmp __b1
    // main::@1
  __b1:
    // [2] main::w#1 = main::w#2 + main::j#2 -- vwuz1=vwuz1_plus_vbuxx 
    txa
    clc
    adc.z w
    sta.z w
    bcc !+
    inc.z w+1
  !:
    // [3] main::j#1 = ++ main::j#2 -- vbuxx=_inc_vbuxx 
    inx
    // [4] if(main::j#1!=$b) goto main::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #$b
    bne __b1_from___b1
    jmp __breturn
    // main::@return
  __breturn:
    // [5] return 
    rts
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing instruction lda #<0 with TXA
Removing instruction lda #>0
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::j
char main::j#1 // reg byte x 16.5
char main::j#2 // reg byte x 16.5
unsigned int main::w
unsigned int main::w#1 // w zp[2]:2 7.333333333333333
unsigned int main::w#2 // w zp[2]:2 22.0

zp[2]:2 [ main::w#2 main::w#1 ]
reg byte x [ main::j#2 main::j#1 ]


FINAL ASSEMBLER
Score: 346

  // File Comments
// Tests that immediate zero values are reused - even when assigning to words
  // Upstart
  // Commodore 64 PRG executable file
.file [name="immzero.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 w = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::j#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi main::w#2 = 0 [phi:main->main::@1#1] -- vwuz1=vwuc1 
    txa
    sta.z w
    sta.z w+1
    // [1] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
    // [1] phi main::j#2 = main::j#1 [phi:main::@1->main::@1#0] -- register_copy 
    // [1] phi main::w#2 = main::w#1 [phi:main::@1->main::@1#1] -- register_copy 
    // main::@1
  __b1:
    // w = w + j
    // [2] main::w#1 = main::w#2 + main::j#2 -- vwuz1=vwuz1_plus_vbuxx 
    txa
    clc
    adc.z w
    sta.z w
    bcc !+
    inc.z w+1
  !:
    // for ( byte j : 0..10)
    // [3] main::j#1 = ++ main::j#2 -- vbuxx=_inc_vbuxx 
    inx
    // [4] if(main::j#1!=$b) goto main::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #$b
    bne __b1
    // main::@return
    // }
    // [5] return 
    rts
}
  // File Data

