
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::i#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  main::i#2 = phi( main/main::i#0, main::@2/main::i#1 )
  b::i#0 = main::i#2
  call b
  to:main::@2
main::@2: scope:[main]  from main::@1
  main::i#3 = phi( main::@1/main::i#2 )
  main::i#1 = main::i#3 + rangenext(0,$64)
  main::$1 = main::i#1 != rangelast(0,$64)
  if(main::$1) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@2
  return 
  to:@return

void b(char i)
b: scope:[b]  from main::@1
  b::i#1 = phi( main::@1/b::i#0 )
  c::i#0 = b::i#1
  call c
  to:b::@1
b::@1: scope:[b]  from b
  to:b::@return
b::@return: scope:[b]  from b::@1
  return 
  to:@return

void c(char i)
c: scope:[c]  from b
  c::i#2 = phi( b/c::i#0 )
  c::j#0 = 0
  to:c::@1
c::@1: scope:[c]  from c c::@1
  c::j#2 = phi( c/c::j#0, c::@1/c::j#1 )
  c::i#1 = phi( c/c::i#2, c::@1/c::i#1 )
  SCREEN[c::j#2] = c::i#1
  c::j#1 = c::j#2 + rangenext(0,$64)
  c::$0 = c::j#1 != rangelast(0,$64)
  if(c::$0) goto c::@1
  to:c::@return
c::@return: scope:[c]  from c::@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 * const SCREEN = (char *)$400
void __start()
void b(char i)
char b::i
char b::i#0
char b::i#1
void c(char i)
bool c::$0
char c::i
char c::i#0
char c::i#1
char c::i#2
char c::j
char c::j#0
char c::j#1
char c::j#2
void main()
bool main::$1
char main::i
char main::i#0
char main::i#1
char main::i#2
char main::i#3

Simplifying constant pointer cast (char *) 1024
Successful SSA optimization PassNCastSimplification
Alias main::i#2 = main::i#3 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values b::i#1 b::i#0
Identical Phi Values c::i#2 c::i#0
Identical Phi Values c::i#1 c::i#2
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition main::$1 [6] if(main::i#1!=rangelast(0,$64)) goto main::@1
Simple Condition c::$0 [18] if(c::j#1!=rangelast(0,$64)) goto c::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::i#0 = 0
Constant c::j#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [4] main::i#1 = ++ main::i#2 to ++
Resolved ranged comparison value [6] if(main::i#1!=rangelast(0,$64)) goto main::@1 to $65
Resolved ranged next value [16] c::j#1 = ++ c::j#2 to ++
Resolved ranged comparison value [18] if(c::j#1!=rangelast(0,$64)) goto c::@1 to $65
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) $65 in [4] if(main::i#1!=$65) goto main::@1
Adding number conversion cast (unumber) $65 in [12] if(c::j#1!=$65) goto c::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast $65
Simplifying constant integer cast $65
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) $65
Finalized unsigned number type (char) $65
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inlining constant with var siblings main::i#0
Inlining constant with var siblings c::j#0
Constant inlined main::i#0 = 0
Constant inlined c::j#0 = 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting main::@3(between main::@2 and main::@1)
Added new block during phi lifting c::@2(between c::@1 and c::@1)
Adding NOP phi() at start of main
Adding NOP phi() at start of b::@1
Adding NOP phi() at start of c
CALL GRAPH
Calls in [main] to b:3 
Calls in [b] to c:9 

Created 2 initial phi equivalence classes
Coalesced [7] main::i#4 = main::i#1
Coalesced [18] c::j#3 = c::j#1
Coalesced down to 2 phi equivalence classes
Culled Empty Block label main::@3
Culled Empty Block label b::@1
Culled Empty Block label c::@2
Adding NOP phi() at start of main
Adding NOP phi() at start of c

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 )
  [2] b::i#0 = main::i#2
  [3] call b
  to:main::@2
main::@2: scope:[main]  from main::@1
  [4] main::i#1 = ++ main::i#2
  [5] if(main::i#1!=$65) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@2
  [6] return 
  to:@return

void b(char i)
b: scope:[b]  from main::@1
  [7] c::i#0 = b::i#0
  [8] call c
  to:b::@return
b::@return: scope:[b]  from b
  [9] return 
  to:@return

void c(char i)
c: scope:[c]  from b
  [10] phi()
  to:c::@1
c::@1: scope:[c]  from c c::@1
  [11] c::j#2 = phi( c/0, c::@1/c::j#1 )
  [12] SCREEN[c::j#2] = c::i#0
  [13] c::j#1 = ++ c::j#2
  [14] if(c::j#1!=$65) goto c::@1
  to:c::@return
c::@return: scope:[c]  from c::@1
  [15] return 
  to:@return


VARIABLE REGISTER WEIGHTS
void b(char i)
char b::i
char b::i#0 // 112.0
void c(char i)
char c::i
char c::i#0 // 16683.666666666664
char c::j
char c::j#1 // 150001.5
char c::j#2 // 150001.5
void main()
char main::i
char main::i#1 // 16.5
char main::i#2 // 11.0

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

REGISTER UPLIFT SCOPES
Uplift Scope [c] 300,003: zp[1]:2 [ c::j#2 c::j#1 ] 16,683.67: zp[1]:3 [ c::i#0 ] 
Uplift Scope [b] 112: zp[1]:4 [ b::i#0 ] 
Uplift Scope [main] 27.5: zp[1]:5 [ main::i#2 main::i#1 ] 
Uplift Scope [] 

Uplifting [c] best 2695 combination reg byte x [ c::j#2 c::j#1 ] reg byte a [ c::i#0 ] 
Uplifting [b] best 2662 combination reg byte a [ b::i#0 ] 
Uplifting [main] best 2562 combination reg byte a [ main::i#2 main::i#1 ] 
Uplifting [] best 2562 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
  // Upstart
  // Commodore 64 PRG executable file
.file [name="loopnest3.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: {
    // [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] -- vbuaa=vbuc1 
    lda #0
    jmp __b1
    // [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 
    jmp __b1
    // main::@1
  __b1:
    // [2] b::i#0 = main::i#2
    // [3] call b
    jsr b
    jmp __b2
    // main::@2
  __b2:
    // [4] main::i#1 = ++ main::i#2 -- vbuaa=_inc_vbuaa 
    clc
    adc #1
    // [5] if(main::i#1!=$65) goto main::@1 -- vbuaa_neq_vbuc1_then_la1 
    cmp #$65
    bne __b1_from___b2
    jmp __breturn
    // main::@return
  __breturn:
    // [6] return 
    rts
}
  // b
// void b(__register(A) char i)
b: {
    // [7] c::i#0 = b::i#0
    // [8] call c
    // [10] phi from b to c [phi:b->c]
  c_from_b:
    jsr c
    jmp __breturn
    // b::@return
  __breturn:
    // [9] return 
    rts
}
  // c
// void c(__register(A) char i)
c: {
    // [11] phi from c to c::@1 [phi:c->c::@1]
  __b1_from_c:
    // [11] phi c::j#2 = 0 [phi:c->c::@1#0] -- vbuxx=vbuc1 
    ldx #0
    jmp __b1
    // [11] phi from c::@1 to c::@1 [phi:c::@1->c::@1]
  __b1_from___b1:
    // [11] phi c::j#2 = c::j#1 [phi:c::@1->c::@1#0] -- register_copy 
    jmp __b1
    // c::@1
  __b1:
    // [12] SCREEN[c::j#2] = c::i#0 -- pbuc1_derefidx_vbuxx=vbuaa 
    sta SCREEN,x
    // [13] c::j#1 = ++ c::j#2 -- vbuxx=_inc_vbuxx 
    inx
    // [14] if(c::j#1!=$65) goto c::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #$65
    bne __b1_from___b1
    jmp __breturn
    // c::@return
  __breturn:
    // [15] return 
    rts
}
  // File Data

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

FINAL SYMBOL TABLE
__constant char * const SCREEN = (char *) 1024
void b(char i)
char b::i
char b::i#0 // reg byte a 112.0
void c(char i)
char c::i
char c::i#0 // reg byte a 16683.666666666664
char c::j
char c::j#1 // reg byte x 150001.5
char c::j#2 // reg byte x 150001.5
void main()
char main::i
char main::i#1 // reg byte a 16.5
char main::i#2 // reg byte a 11.0

reg byte a [ main::i#2 main::i#1 ]
reg byte x [ c::j#2 c::j#1 ]
reg byte a [ b::i#0 ]
reg byte a [ c::i#0 ]


FINAL ASSEMBLER
Score: 1539

  // File Comments
  // Upstart
  // Commodore 64 PRG executable file
.file [name="loopnest3.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: {
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuaa=vbuc1 
    lda #0
    // [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 
    // main::@1
  __b1:
    // b(i)
    // [2] b::i#0 = main::i#2
    // [3] call b
    jsr b
    // main::@2
    // for(byte i:0..100)
    // [4] main::i#1 = ++ main::i#2 -- vbuaa=_inc_vbuaa 
    clc
    adc #1
    // [5] if(main::i#1!=$65) goto main::@1 -- vbuaa_neq_vbuc1_then_la1 
    cmp #$65
    bne __b1
    // main::@return
    // }
    // [6] return 
    rts
}
  // b
// void b(__register(A) char i)
b: {
    // c(i)
    // [7] c::i#0 = b::i#0
    // [8] call c
    // [10] phi from b to c [phi:b->c]
    jsr c
    // b::@return
    // }
    // [9] return 
    rts
}
  // c
// void c(__register(A) char i)
c: {
    // [11] phi from c to c::@1 [phi:c->c::@1]
    // [11] phi c::j#2 = 0 [phi:c->c::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // [11] phi from c::@1 to c::@1 [phi:c::@1->c::@1]
    // [11] phi c::j#2 = c::j#1 [phi:c::@1->c::@1#0] -- register_copy 
    // c::@1
  __b1:
    // SCREEN[j] = i
    // [12] SCREEN[c::j#2] = c::i#0 -- pbuc1_derefidx_vbuxx=vbuaa 
    sta SCREEN,x
    // for( byte j: 0..100)
    // [13] c::j#1 = ++ c::j#2 -- vbuxx=_inc_vbuxx 
    inx
    // [14] if(c::j#1!=$65) goto c::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #$65
    bne __b1
    // c::@return
    // }
    // [15] return 
    rts
}
  // File Data

