
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::lasti#0 = -1
  main::i#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  main::lasti#3 = phi( main/main::lasti#0, main::@2/main::lasti#1 )
  main::i#2 = phi( main/main::i#0, main::@2/main::i#1 )
  main::$0 = main::i#2 < $a
  if(main::$0) goto main::@2
  to:main::@return
main::@2: scope:[main]  from main::@1
  main::i#3 = phi( main::@1/main::i#2 )
  main::lasti#2 = phi( main::@1/main::lasti#3 )
  main::screen[main::i#3] = (char)main::lasti#2
  main::lasti#1 = main::i#3
  main::i#1 = ++ main::i#3
  to:main::@1
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()
bool main::$0
int main::i
int main::i#0
int main::i#1
int main::i#2
int main::i#3
int main::lasti
int main::lasti#0
int main::lasti#1
int main::lasti#2
int main::lasti#3
__constant char *main::screen = (char *)$400

Adding number conversion cast (snumber) $a in main::$0 = main::i#2 < $a
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast $a
Successful SSA optimization PassNCastSimplification
Finalized signed number type (signed char) $a
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias candidate removed (phi-usage) main::i#2 = main::i#3 main::lasti#1 
Alias main::lasti#2 = main::lasti#3 
Successful SSA optimization Pass2AliasElimination
Alias candidate removed (phi-usage) main::i#2 = main::i#3 main::lasti#1 
Identical Phi Values main::i#3 main::i#2
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition main::$0 [4] if(main::i#2<$a) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::lasti#0 = -1
Constant main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
De-inlining pointer[w] to *(pointer+w)   [6] main::screen[main::i#2] = (char)main::lasti#2
Successful SSA optimization Pass2DeInlineWordDerefIdx
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
Alias candidate removed (phi-usage) main::lasti#1 = main::i#2 
Inlining constant with var siblings main::lasti#0
Inlining constant with var siblings main::i#0
Constant inlined main::i#0 = 0
Constant inlined main::lasti#0 = -1
Successful SSA optimization Pass2ConstantInlining
Alias candidate removed (phi-usage) main::lasti#1 = main::i#2 
Alias candidate removed (phi-usage) main::lasti#1 = main::i#2 
Alias candidate removed (phi-usage) main::lasti#1 = main::i#2 
Adding NOP phi() at start of main
CALL GRAPH

Created 2 initial phi equivalence classes
Coalesced [8] main::i#4 = main::i#1
Coalesced [9] main::lasti#4 = main::lasti#1
Coalesced down to 2 phi equivalence classes
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::@2
  [1] main::lasti#2 = phi( main/-1, main::@2/main::lasti#1 )
  [1] main::i#2 = phi( main/0, main::@2/main::i#1 )
  [2] if(main::i#2<$a) goto main::@2
  to:main::@return
main::@return: scope:[main]  from main::@1
  [3] return 
  to:@return
main::@2: scope:[main]  from main::@1
  [4] main::$1 = main::screen + main::i#2
  [5] *main::$1 = (char)main::lasti#2
  [6] main::lasti#1 = main::i#2
  [7] main::i#1 = ++ main::i#2
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char *main::$1 // 22.0
int main::i
int main::i#1 // 22.0
int main::i#2 // 11.0
int main::lasti
int main::lasti#1 // 11.0
int main::lasti#2 // 3.6666666666666665

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

REGISTER UPLIFT SCOPES
Uplift Scope [main] 33: zp[2]:2 [ main::i#2 main::i#1 ] 22: zp[2]:4 [ main::$1 ] 14.67: zp[2]:6 [ main::lasti#2 main::lasti#1 ] 
Uplift Scope [] 

Uplifting [main] best 1031 combination zp[2]:2 [ main::i#2 main::i#1 ] zp[2]:4 [ main::$1 ] zp[2]:6 [ main::lasti#2 main::lasti#1 ] 
Uplifting [] best 1031 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// https://gitlab.com/camelot/kickc/issues/336
// ASM Static Register Value analysis erronously believes >-1 == 0
  // Upstart
  // Commodore 64 PRG executable file
.file [name="static-register-optimization-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: {
    .label screen = $400
    .label lasti = 6
    .label i = 2
    .label __1 = 4
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::lasti#2 = -1 [phi:main->main::@1#0] -- vwsz1=vwsc1 
    lda #<-1
    sta.z lasti
    lda #>-1
    sta.z lasti+1
    // [1] phi main::i#2 = 0 [phi:main->main::@1#1] -- vwsz1=vwsc1 
    lda #<0
    sta.z i
    lda #>0
    sta.z i+1
    jmp __b1
    // main::@1
  __b1:
    // [2] if(main::i#2<$a) goto main::@2 -- vwsz1_lt_vwuc1_then_la1 
    lda.z i+1
    bmi __b2
    cmp #>$a
    bcc __b2
    bne !+
    lda.z i
    cmp #<$a
    bcc __b2
  !:
    jmp __breturn
    // main::@return
  __breturn:
    // [3] return 
    rts
    // main::@2
  __b2:
    // [4] main::$1 = main::screen + main::i#2 -- pbuz1=pbuc1_plus_vwsz2 
    lda #<screen
    clc
    adc.z i
    sta.z __1
    lda #>screen
    adc.z i+1
    sta.z __1+1
    // [5] *main::$1 = (char)main::lasti#2 -- _deref_pbuz1=_byte_vwsz2 
    lda.z lasti
    ldy #0
    sta (__1),y
    // [6] main::lasti#1 = main::i#2 -- vwsz1=vwsz2 
    lda.z i
    sta.z lasti
    lda.z i+1
    sta.z lasti+1
    // [7] main::i#1 = ++ main::i#2 -- vwsz1=_inc_vwsz1 
    inc.z i
    bne !+
    inc.z i+1
  !:
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
  __b1_from___b2:
    // [1] phi main::lasti#2 = main::lasti#1 [phi:main::@2->main::@1#0] -- register_copy 
    // [1] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#1] -- register_copy 
    jmp __b1
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #>-1
Removing instruction lda #>0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction __b1_from_main:
Removing instruction __breturn:
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
void main()
char *main::$1 // zp[2]:4 22.0
int main::i
int main::i#1 // i zp[2]:2 22.0
int main::i#2 // i zp[2]:2 11.0
int main::lasti
int main::lasti#1 // lasti zp[2]:6 11.0
int main::lasti#2 // lasti zp[2]:6 3.6666666666666665
__constant char *main::screen = (char *) 1024

zp[2]:2 [ main::i#2 main::i#1 ]
zp[2]:6 [ main::lasti#2 main::lasti#1 ]
zp[2]:4 [ main::$1 ]


FINAL ASSEMBLER
Score: 931

  // File Comments
// https://gitlab.com/camelot/kickc/issues/336
// ASM Static Register Value analysis erronously believes >-1 == 0
  // Upstart
  // Commodore 64 PRG executable file
.file [name="static-register-optimization-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: {
    .label screen = $400
    .label lasti = 6
    .label i = 2
    .label __1 = 4
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::lasti#2 = -1 [phi:main->main::@1#0] -- vwsz1=vwsc1 
    lda #<-1
    sta.z lasti
    sta.z lasti+1
    // [1] phi main::i#2 = 0 [phi:main->main::@1#1] -- vwsz1=vwsc1 
    lda #<0
    sta.z i
    sta.z i+1
    // main::@1
  __b1:
    // for(int i=0;i<10;i++)
    // [2] if(main::i#2<$a) goto main::@2 -- vwsz1_lt_vwuc1_then_la1 
    lda.z i+1
    bmi __b2
    cmp #>$a
    bcc __b2
    bne !+
    lda.z i
    cmp #<$a
    bcc __b2
  !:
    // main::@return
    // }
    // [3] return 
    rts
    // main::@2
  __b2:
    // screen[i] = (char)lasti
    // [4] main::$1 = main::screen + main::i#2 -- pbuz1=pbuc1_plus_vwsz2 
    lda #<screen
    clc
    adc.z i
    sta.z __1
    lda #>screen
    adc.z i+1
    sta.z __1+1
    // [5] *main::$1 = (char)main::lasti#2 -- _deref_pbuz1=_byte_vwsz2 
    lda.z lasti
    ldy #0
    sta (__1),y
    // lasti = i
    // [6] main::lasti#1 = main::i#2 -- vwsz1=vwsz2 
    lda.z i
    sta.z lasti
    lda.z i+1
    sta.z lasti+1
    // for(int i=0;i<10;i++)
    // [7] main::i#1 = ++ main::i#2 -- vwsz1=_inc_vwsz1 
    inc.z i
    bne !+
    inc.z i+1
  !:
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
    // [1] phi main::lasti#2 = main::lasti#1 [phi:main::@2->main::@1#0] -- register_copy 
    // [1] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#1] -- register_copy 
    jmp __b1
}
  // File Data

