
CONTROL FLOW GRAPH SSA

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

Adding number conversion cast (unumber) $800 in main::$0 = main::sc#2 < $800
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast $800
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (unsigned int) $800
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias main::sc#2 = main::sc#3 
Successful SSA optimization Pass2AliasElimination
Simple Condition main::$0 [3] if(main::sc#2<$800) goto main::@3
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::sc#0 = (char *) 1024
Successful SSA optimization Pass2ConstantIdentification
Removing unused block main::@return
Successful SSA optimization Pass2EliminateUnusedBlocks
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::sc#0
Constant inlined main::sc#0 = (char *) 1024
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@1
CALL GRAPH

Created 1 initial phi equivalence classes
Coalesced [6] main::sc#4 = main::sc#1
Coalesced down to 1 phi equivalence classes
Culled Empty Block label main::@1
Renumbering block main::@2 to main::@1
Renumbering block main::@3 to 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 main::@2
  [1] main::sc#2 = phi( main/(char *) 1024, main::@2/main::sc#1, main::@1/(char *) 1024 )
  [2] if(main::sc#2<$800) goto main::@2
  to:main::@1
main::@2: scope:[main]  from main::@1
  [3] *main::sc#2 = ++ *main::sc#2
  [4] main::sc#1 = ++ main::sc#2
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char *main::sc
char *main::sc#1 // 22.0
char *main::sc#2 // 48.33333333333332

Initial phi equivalence classes
[ main::sc#2 main::sc#1 ]
Complete equivalence classes
[ main::sc#2 main::sc#1 ]
Allocated zp[2]:2 [ main::sc#2 main::sc#1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [2] if(main::sc#2<$800) goto main::@2 [ main::sc#2 ] (  [ main::sc#2 ] { }  ) always clobbers reg byte a 
Statement [3] *main::sc#2 = ++ *main::sc#2 [ main::sc#2 ] (  [ main::sc#2 ] { }  ) always clobbers reg byte a reg byte y 
Potential registers zp[2]:2 [ main::sc#2 main::sc#1 ] : zp[2]:2 , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 70.33: zp[2]:2 [ main::sc#2 main::sc#1 ] 
Uplift Scope [] 

Uplifting [main] best 3970 combination zp[2]:2 [ main::sc#2 main::sc#1 ] 
Uplifting [] best 3970 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// A loop that compiles to a wrong sequence - skipping the initilization
  // Upstart
  // Commodore 64 PRG executable file
.file [name="loop-problem3.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 sc = 2
    // [1] phi from main main::@1 to main::@1 [phi:main/main::@1->main::@1]
  __b1_from_main:
  __b1_from___b1:
    // [1] phi main::sc#2 = (char *) 1024 [phi:main/main::@1->main::@1#0] -- pbuz1=pbuc1 
    lda #<$400
    sta.z sc
    lda #>$400
    sta.z sc+1
    jmp __b1
    // main::@1
  __b1:
    // [2] if(main::sc#2<$800) goto main::@2 -- pbuz1_lt_vwuc1_then_la1 
    lda.z sc+1
    cmp #>$800
    bcc __b2
    bne !+
    lda.z sc
    cmp #<$800
    bcc __b2
  !:
    jmp __b1_from___b1
    // main::@2
  __b2:
    // [3] *main::sc#2 = ++ *main::sc#2 -- _deref_pbuz1=_inc__deref_pbuz1 
    ldy #0
    lda (sc),y
    clc
    adc #1
    ldy #0
    sta (sc),y
    // [4] main::sc#1 = ++ main::sc#2 -- pbuz1=_inc_pbuz1 
    inc.z sc
    bne !+
    inc.z sc+1
  !:
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
  __b1_from___b2:
    // [1] phi main::sc#2 = main::sc#1 [phi:main::@2->main::@1#0] -- register_copy 
    jmp __b1
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction ldy #0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction __b1_from_main:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5UnusedLabelElimination
Relabelling long label __b1_from___b1 to __b3
Succesful ASM optimization Pass5RelabelLongLabels

FINAL SYMBOL TABLE
void main()
char *main::sc
char *main::sc#1 // sc zp[2]:2 22.0
char *main::sc#2 // sc zp[2]:2 48.33333333333332

zp[2]:2 [ main::sc#2 main::sc#1 ]


FINAL ASSEMBLER
Score: 3650

  // File Comments
// A loop that compiles to a wrong sequence - skipping the initilization
  // Upstart
  // Commodore 64 PRG executable file
.file [name="loop-problem3.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 sc = 2
    // [1] phi from main main::@1 to main::@1 [phi:main/main::@1->main::@1]
  __b3:
    // [1] phi main::sc#2 = (char *) 1024 [phi:main/main::@1->main::@1#0] -- pbuz1=pbuc1 
    lda #<$400
    sta.z sc
    lda #>$400
    sta.z sc+1
    // main::@1
  __b1:
    // for(char* sc = (char*)0x0400;sc<0x0800; sc++)
    // [2] if(main::sc#2<$800) goto main::@2 -- pbuz1_lt_vwuc1_then_la1 
    lda.z sc+1
    cmp #>$800
    bcc __b2
    bne !+
    lda.z sc
    cmp #<$800
    bcc __b2
  !:
    jmp __b3
    // main::@2
  __b2:
    // (*sc)++;
    // [3] *main::sc#2 = ++ *main::sc#2 -- _deref_pbuz1=_inc__deref_pbuz1 
    ldy #0
    lda (sc),y
    clc
    adc #1
    sta (sc),y
    // for(char* sc = (char*)0x0400;sc<0x0800; sc++)
    // [4] main::sc#1 = ++ main::sc#2 -- pbuz1=_inc_pbuz1 
    inc.z sc
    bne !+
    inc.z sc+1
  !:
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
    // [1] phi main::sc#2 = main::sc#1 [phi:main::@2->main::@1#0] -- register_copy 
    jmp __b1
}
  // File Data

