
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::ptr#0 = mmap
  main::n#0 = *main::ptr#0
  main::ptr#1 = ++ main::ptr#0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  main::n#2 = phi( main/main::n#0, main::@2/main::n#3 )
  main::$0 = main::n#2
  main::n#1 = -- main::n#2
  main::$1 = 0 != main::$0
  if(main::$1) goto main::@2
  to:main::@return
main::@2: scope:[main]  from main::@1
  main::n#3 = phi( main::@1/main::n#1 )
  *((char *)$400) = main::n#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()
char main::$0
bool main::$1
char main::n
char main::n#0
char main::n#1
char main::n#2
char main::n#3
char *main::ptr
char *main::ptr#0
char *main::ptr#1
__constant const char mmap[] = { 2, $5c, $1a, 3, $60, $1a, 7 }

Adding number conversion cast (unumber) 0 in main::$1 = 0 != main::$0
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast 0
Simplifying constant pointer cast (char *) 1024
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias main::n#2 = main::$0 
Alias main::n#1 = main::n#3 
Successful SSA optimization Pass2AliasElimination
Simple Condition main::$1 [6] if(0!=main::n#2) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::ptr#0 = mmap
Successful SSA optimization Pass2ConstantIdentification
Eliminating unused variable main::ptr#1 and assignment [1] main::ptr#1 = ++ main::ptr#0
Successful SSA optimization PassNEliminateUnusedVars
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
Constant inlined main::ptr#0 = mmap
Successful SSA optimization Pass2ConstantInlining
CALL GRAPH

Created 1 initial phi equivalence classes
Coalesced [1] main::n#4 = main::n#0
Not coalescing [7] main::n#5 = main::n#1
Coalesced down to 2 phi equivalence classes

FINAL CONTROL FLOW GRAPH

void main()
main: scope:[main]  from
  [0] main::n#0 = *mmap
  to:main::@1
main::@1: scope:[main]  from main main::@2
  [1] main::n#2 = phi( main/main::n#0, main::@2/main::n#5 )
  [2] main::n#1 = -- main::n#2
  [3] if(0!=main::n#2) goto main::@2
  to:main::@return
main::@return: scope:[main]  from main::@1
  [4] return 
  to:@return
main::@2: scope:[main]  from main::@1
  [5] *((char *) 1024) = main::n#1
  [6] main::n#5 = main::n#1
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char main::n
char main::n#0 // 4.0
char main::n#1 // 11.0
char main::n#2 // 17.5
char main::n#5 // 22.0
char *main::ptr

Initial phi equivalence classes
[ main::n#2 main::n#0 main::n#5 ]
Added variable main::n#1 to live range equivalence class [ main::n#1 ]
Complete equivalence classes
[ main::n#2 main::n#0 main::n#5 ]
[ main::n#1 ]
Allocated zp[1]:2 [ main::n#2 main::n#0 main::n#5 ]
Allocated zp[1]:3 [ main::n#1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Potential registers zp[1]:2 [ main::n#2 main::n#0 main::n#5 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:3 [ main::n#1 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 43.5: zp[1]:2 [ main::n#2 main::n#0 main::n#5 ] 11: zp[1]:3 [ main::n#1 ] 
Uplift Scope [] 

Uplifting [main] best 245 combination reg byte a [ main::n#2 main::n#0 main::n#5 ] reg byte x [ main::n#1 ] 
Uplifting [] best 245 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Illustrates a problem with post-incrementing inside the while loop condition
// https://gitlab.com/camelot/kickc/-/issues/486
  // Upstart
  // Commodore 64 PRG executable file
.file [name="post-increment-problem-3.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: {
    // [0] main::n#0 = *mmap -- vbuaa=_deref_pbuc1 
    lda mmap
    // [1] phi from main main::@2 to main::@1 [phi:main/main::@2->main::@1]
  __b1_from_main:
  __b1_from___b2:
    // [1] phi main::n#2 = main::n#0 [phi:main/main::@2->main::@1#0] -- register_copy 
    jmp __b1
    // main::@1
  __b1:
    // [2] main::n#1 = -- main::n#2 -- vbuxx=_dec_vbuaa 
    tax
    dex
    // [3] if(0!=main::n#2) goto main::@2 -- 0_neq_vbuaa_then_la1 
    cmp #0
    bne __b2
    jmp __breturn
    // main::@return
  __breturn:
    // [4] return 
    rts
    // main::@2
  __b2:
    // [5] *((char *) 1024) = main::n#1 -- _deref_pbuc1=vbuxx 
    stx $400
    // [6] main::n#5 = main::n#1 -- vbuaa=vbuxx 
    txa
    jmp __b1_from___b2
}
  // File Data
.segment Data
  mmap: .byte 2, $5c, $1a, 3, $60, $1a, 7

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing label __b1_from___b2 with __b1
Removing instruction __b1_from_main:
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
void main()
char main::n
char main::n#0 // reg byte a 4.0
char main::n#1 // reg byte x 11.0
char main::n#2 // reg byte a 17.5
char main::n#5 // reg byte a 22.0
char *main::ptr
__constant const char mmap[] = { 2, $5c, $1a, 3, $60, $1a, 7 }

reg byte a [ main::n#2 main::n#0 main::n#5 ]
reg byte x [ main::n#1 ]


FINAL ASSEMBLER
Score: 185

  // File Comments
// Illustrates a problem with post-incrementing inside the while loop condition
// https://gitlab.com/camelot/kickc/-/issues/486
  // Upstart
  // Commodore 64 PRG executable file
.file [name="post-increment-problem-3.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: {
    // unsigned char n = *(ptr++)
    // [0] main::n#0 = *mmap -- vbuaa=_deref_pbuc1 
    lda mmap
    // [1] phi from main main::@2 to main::@1 [phi:main/main::@2->main::@1]
    // [1] phi main::n#2 = main::n#0 [phi:main/main::@2->main::@1#0] -- register_copy 
    // main::@1
  __b1:
    // while (n--)
    // [2] main::n#1 = -- main::n#2 -- vbuxx=_dec_vbuaa 
    tax
    dex
    // [3] if(0!=main::n#2) goto main::@2 -- 0_neq_vbuaa_then_la1 
    cmp #0
    bne __b2
    // main::@return
    // }
    // [4] return 
    rts
    // main::@2
  __b2:
    // *((unsigned char *)0x400) = n
    // [5] *((char *) 1024) = main::n#1 -- _deref_pbuc1=vbuxx 
    stx $400
    // [6] main::n#5 = main::n#1 -- vbuaa=vbuxx 
    txa
    jmp __b1
}
  // File Data
.segment Data
  mmap: .byte 2, $5c, $1a, 3, $60, $1a, 7

