
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::s#0 = MESSAGE
  main::c#0 = 0
  main::i#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  main::i#3 = phi( main/main::i#0, main::@2/main::i#1 )
  main::s#2 = phi( main/main::s#0, main::@2/main::s#3 )
  main::c#1 = *main::s#2
  main::$0 = main::c#1
  main::s#1 = ++ main::s#2
  main::$1 = 0 != main::$0
  if(main::$1) goto main::@2
  to:main::@return
main::@2: scope:[main]  from main::@1
  main::s#3 = phi( main::@1/main::s#1 )
  main::i#2 = phi( main::@1/main::i#3 )
  main::c#2 = phi( main::@1/main::c#1 )
  SCREEN[main::i#2] = main::c#2
  main::i#1 = ++ main::i#2
  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
__constant char MESSAGE[] = "hello world!"
__constant char * const SCREEN = (char *)$400
void __start()
void main()
char main::$0
bool main::$1
char main::c
char main::c#0
char main::c#1
char main::c#2
char main::i
char main::i#0
char main::i#1
char main::i#2
char main::i#3
char *main::s
char *main::s#0
char *main::s#1
char *main::s#2
char *main::s#3

Adding number conversion cast (unumber) 0 in main::$1 = 0 != main::$0
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast 0
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias main::c#1 = main::$0 main::c#2 
Alias main::i#2 = main::i#3 
Alias main::s#1 = main::s#3 
Successful SSA optimization Pass2AliasElimination
Simple Condition main::$1 [7] if(0!=main::c#1) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::s#0 = MESSAGE
Constant main::c#0 = 0
Constant main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Eliminating unused constant main::c#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
Inlining constant with var siblings main::s#0
Inlining constant with var siblings main::i#0
Constant inlined main::i#0 = 0
Constant inlined main::s#0 = MESSAGE
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of main
CALL GRAPH

Created 2 initial phi equivalence classes
Coalesced [8] main::s#4 = main::s#1
Coalesced [9] main::i#4 = main::i#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::i#2 = phi( main/0, main::@2/main::i#1 )
  [1] main::s#2 = phi( main/MESSAGE, main::@2/main::s#1 )
  [2] main::c#1 = *main::s#2
  [3] main::s#1 = ++ main::s#2
  [4] if(0!=main::c#1) goto main::@2
  to:main::@return
main::@return: scope:[main]  from main::@1
  [5] return 
  to:@return
main::@2: scope:[main]  from main::@1
  [6] SCREEN[main::i#2] = main::c#1
  [7] main::i#1 = ++ main::i#2
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char main::c
char main::c#1 // 11.0
char main::i
char main::i#1 // 22.0
char main::i#2 // 6.6000000000000005
char *main::s
char *main::s#1 // 5.5
char *main::s#2 // 16.5

Initial phi equivalence classes
[ main::s#2 main::s#1 ]
[ main::i#2 main::i#1 ]
Added variable main::c#1 to live range equivalence class [ main::c#1 ]
Complete equivalence classes
[ main::s#2 main::s#1 ]
[ main::i#2 main::i#1 ]
[ main::c#1 ]
Allocated zp[1]:2 [ main::i#2 main::i#1 ]
Allocated zp[2]:3 [ main::s#2 main::s#1 ]
Allocated zp[1]:5 [ main::c#1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [2] main::c#1 = *main::s#2 [ main::s#2 main::i#2 main::c#1 ] (  [ main::s#2 main::i#2 main::c#1 ] { }  ) always clobbers reg byte a reg byte y 
Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::i#2 main::i#1 ]
Removing always clobbered register reg byte y as potential for zp[1]:2 [ main::i#2 main::i#1 ]
Statement [2] main::c#1 = *main::s#2 [ main::s#2 main::i#2 main::c#1 ] (  [ main::s#2 main::i#2 main::c#1 ] { }  ) always clobbers reg byte a reg byte y 
Potential registers zp[2]:3 [ main::s#2 main::s#1 ] : zp[2]:3 , 
Potential registers zp[1]:2 [ main::i#2 main::i#1 ] : zp[1]:2 , reg byte x , 
Potential registers zp[1]:5 [ main::c#1 ] : zp[1]:5 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 28.6: zp[1]:2 [ main::i#2 main::i#1 ] 22: zp[2]:3 [ main::s#2 main::s#1 ] 11: zp[1]:5 [ main::c#1 ] 
Uplift Scope [] 

Uplifting [main] best 531 combination reg byte x [ main::i#2 main::i#1 ] zp[2]:3 [ main::s#2 main::s#1 ] reg byte a [ main::c#1 ] 
Uplifting [] best 531 combination 
Allocated (was zp[2]:3) zp[2]:2 [ main::s#2 main::s#1 ]

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-4.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
  .label SCREEN = $400
.segment Code
  // main
main: {
    .label s = 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] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi main::s#2 = MESSAGE [phi:main->main::@1#1] -- pbuz1=pbuc1 
    lda #<MESSAGE
    sta.z s
    lda #>MESSAGE
    sta.z s+1
    jmp __b1
    // main::@1
  __b1:
    // [2] main::c#1 = *main::s#2 -- vbuaa=_deref_pbuz1 
    ldy #0
    lda (s),y
    // [3] main::s#1 = ++ main::s#2 -- pbuz1=_inc_pbuz1 
    inc.z s
    bne !+
    inc.z s+1
  !:
    // [4] if(0!=main::c#1) goto main::@2 -- 0_neq_vbuaa_then_la1 
    cmp #0
    bne __b2
    jmp __breturn
    // main::@return
  __breturn:
    // [5] return 
    rts
    // main::@2
  __b2:
    // [6] SCREEN[main::i#2] = main::c#1 -- pbuc1_derefidx_vbuxx=vbuaa 
    sta SCREEN,x
    // [7] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
  __b1_from___b2:
    // [1] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#0] -- register_copy 
    // [1] phi main::s#2 = main::s#1 [phi:main::@2->main::@1#1] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  MESSAGE: .text "hello world!"
  .byte 0

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1_from_main:
Removing instruction __breturn:
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
__constant char MESSAGE[] = "hello world!"
__constant char * const SCREEN = (char *) 1024
void main()
char main::c
char main::c#1 // reg byte a 11.0
char main::i
char main::i#1 // reg byte x 22.0
char main::i#2 // reg byte x 6.6000000000000005
char *main::s
char *main::s#1 // s zp[2]:2 5.5
char *main::s#2 // s zp[2]:2 16.5

zp[2]:2 [ main::s#2 main::s#1 ]
reg byte x [ main::i#2 main::i#1 ]
reg byte a [ main::c#1 ]


FINAL ASSEMBLER
Score: 471

  // 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-4.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
  .label SCREEN = $400
.segment Code
  // main
main: {
    .label s = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi main::s#2 = MESSAGE [phi:main->main::@1#1] -- pbuz1=pbuc1 
    lda #<MESSAGE
    sta.z s
    lda #>MESSAGE
    sta.z s+1
    // main::@1
  __b1:
    // while(c=*s++)
    // [2] main::c#1 = *main::s#2 -- vbuaa=_deref_pbuz1 
    ldy #0
    lda (s),y
    // [3] main::s#1 = ++ main::s#2 -- pbuz1=_inc_pbuz1 
    inc.z s
    bne !+
    inc.z s+1
  !:
    // [4] if(0!=main::c#1) goto main::@2 -- 0_neq_vbuaa_then_la1 
    cmp #0
    bne __b2
    // main::@return
    // }
    // [5] return 
    rts
    // main::@2
  __b2:
    // SCREEN[i++] = c
    // [6] SCREEN[main::i#2] = main::c#1 -- pbuc1_derefidx_vbuxx=vbuaa 
    sta SCREEN,x
    // SCREEN[i++] = c;
    // [7] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
    // [1] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#0] -- register_copy 
    // [1] phi main::s#2 = main::s#1 [phi:main::@2->main::@1#1] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  MESSAGE: .text "hello world!"
  .byte 0

