
CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  (byte[]) msg1#0 ← { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }
  (byte[]) msg2#0 ← { (byte) '1', (byte) '2', (byte) '3', (byte) '4' }
  to:@1
main: scope:[main]  from @2
  (byte) idx#13 ← phi( @2/(byte) idx#14 )
  (byte*) print::m#0 ← (byte[]) msg1#0
  call print 
  to:main::@1
main::@1: scope:[main]  from main
  (byte) idx#7 ← phi( main/(byte) idx#5 )
  (byte) idx#0 ← (byte) idx#7
  (byte*) print::m#1 ← (byte[]) msg2#0
  call print 
  to:main::@2
main::@2: scope:[main]  from main::@1
  (byte) idx#8 ← phi( main::@1/(byte) idx#5 )
  (byte) idx#1 ← (byte) idx#8
  to:main::@return
main::@return: scope:[main]  from main::@2
  (byte) idx#9 ← phi( main::@2/(byte) idx#1 )
  (byte) idx#2 ← (byte) idx#9
  return 
  to:@return
@1: scope:[]  from @begin
  (byte*) SCREEN#0 ← ((byte*)) (number) $400
  (byte) idx#3 ← (number) 0
  to:@2
print: scope:[print]  from main main::@1
  (byte) idx#10 ← phi( main/(byte) idx#13 main::@1/(byte) idx#0 )
  (byte*) print::m#2 ← phi( main/(byte*) print::m#0 main::@1/(byte*) print::m#1 )
  (byte*~) print::$0 ← (byte*) print::m#2 + (number) 2
  *((byte*) SCREEN#0 + (byte) idx#10) ← *((byte*~) print::$0)
  (byte) idx#4 ← ++ (byte) idx#10
  to:print::@return
print::@return: scope:[print]  from print
  (byte) idx#11 ← phi( print/(byte) idx#4 )
  (byte) idx#5 ← (byte) idx#11
  return 
  to:@return
@2: scope:[]  from @1
  (byte) idx#14 ← phi( @1/(byte) idx#3 )
  call main 
  to:@3
@3: scope:[]  from @2
  (byte) idx#12 ← phi( @2/(byte) idx#2 )
  (byte) idx#6 ← (byte) idx#12
  to:@end
@end: scope:[]  from @3

SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @3
(label) @begin
(label) @end
(byte*) SCREEN
(byte*) SCREEN#0
(byte) idx
(byte) idx#0
(byte) idx#1
(byte) idx#10
(byte) idx#11
(byte) idx#12
(byte) idx#13
(byte) idx#14
(byte) idx#2
(byte) idx#3
(byte) idx#4
(byte) idx#5
(byte) idx#6
(byte) idx#7
(byte) idx#8
(byte) idx#9
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@return
(byte[]) msg1
(byte[]) msg1#0
(byte[]) msg2
(byte[]) msg2#0
(void()) print((byte*) print::m)
(byte*~) print::$0
(label) print::@return
(byte*) print::m
(byte*) print::m#0
(byte*) print::m#1
(byte*) print::m#2

Adding number conversion cast (unumber) 0 in (byte) idx#3 ← (number) 0
Adding number conversion cast (unumber) 2 in (byte*~) print::$0 ← (byte*) print::m#2 + (number) 2
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (byte*) SCREEN#0 ← (byte*)(number) $400
Inlining cast (byte) idx#3 ← (unumber)(number) 0
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 0
Simplifying constant integer cast 2
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 2
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias (byte) idx#0 = (byte) idx#7 
Alias (byte) idx#1 = (byte) idx#8 (byte) idx#9 (byte) idx#2 
Alias (byte) idx#11 = (byte) idx#4 (byte) idx#5 
Alias (byte) idx#14 = (byte) idx#3 
Alias (byte) idx#12 = (byte) idx#6 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) idx#13 (byte) idx#14
Identical Phi Values (byte) idx#0 (byte) idx#11
Identical Phi Values (byte) idx#1 (byte) idx#11
Identical Phi Values (byte) idx#12 (byte) idx#1
Successful SSA optimization Pass2IdenticalPhiElimination
Constant right-side identified [0] (byte[]) msg1#0 ← { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }
Constant right-side identified [1] (byte[]) msg2#0 ← { (byte) '1', (byte) '2', (byte) '3', (byte) '4' }
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant (const byte[]) msg1#0 = { 'a', 'b', 'c', 'd' }
Constant (const byte[]) msg2#0 = { '1', '2', '3', '4' }
Constant (const byte*) SCREEN#0 = (byte*) 1024
Constant (const byte) idx#14 = 0
Successful SSA optimization Pass2ConstantIdentification
Constant (const byte*) print::m#0 = msg1#0
Constant (const byte*) print::m#1 = msg2#0
Successful SSA optimization Pass2ConstantIdentification
Converting *(pointer+n) to pointer[n] [18] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*~) print::$0)  -- *(print::m#2 + 2)
Successful SSA optimization Pass2InlineDerefIdx
Eliminating unused variable (byte*~) print::$0 and assignment [4] (byte*~) print::$0 ← (byte*) print::m#2 + (byte) 2
Successful SSA optimization PassNEliminateUnusedVars
Inlining constant with var siblings (const byte*) print::m#0
Inlining constant with var siblings (const byte*) print::m#1
Inlining constant with var siblings (const byte) idx#14
Constant inlined idx#14 = (byte) 0
Constant inlined print::m#1 = (const byte[]) msg2#0
Constant inlined print::m#0 = (const byte[]) msg1#0
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @2
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@2
CALL GRAPH
Calls in [] to main:3 
Calls in [main] to print:7 print:9 

Created 2 initial phi equivalence classes
Coalesced [8] idx#15 ← idx#11
Coalesced down to 2 phi equivalence classes
Culled Empty Block (label) @1
Culled Empty Block (label) @3
Culled Empty Block (label) main::@2
Renumbering block @2 to @1
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@1

FINAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  [0] phi()
  to:@1
@1: scope:[]  from @begin
  [1] phi()
  [2] call main 
  to:@end
@end: scope:[]  from @1
  [3] phi()
main: scope:[main]  from @1
  [4] phi()
  [5] call print 
  to:main::@1
main::@1: scope:[main]  from main
  [6] phi()
  [7] call print 
  to:main::@return
main::@return: scope:[main]  from main::@1
  [8] return 
  to:@return
print: scope:[print]  from main main::@1
  [9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 )
  [9] (byte*) print::m#2 ← phi( main/(const byte[]) msg1#0 main::@1/(const byte[]) msg2#0 )
  [10] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*) print::m#2 + (byte) 2)
  [11] (byte) idx#11 ← ++ (byte) idx#10
  to:print::@return
print::@return: scope:[print]  from print
  [12] return 
  to:@return


VARIABLE REGISTER WEIGHTS
(byte*) SCREEN
(byte) idx
(byte) idx#10 3.0
(byte) idx#11 1.0
(void()) main()
(byte[]) msg1
(byte[]) msg2
(void()) print((byte*) print::m)
(byte*) print::m
(byte*) print::m#2 2.0

Initial phi equivalence classes
[ print::m#2 ]
[ idx#10 idx#11 ]
Complete equivalence classes
[ print::m#2 ]
[ idx#10 idx#11 ]
Allocated zp ZP_WORD:2 [ print::m#2 ]
Allocated zp ZP_BYTE:4 [ idx#10 idx#11 ]

INITIAL ASM
  // File Comments
// Tests optimizing derefs of *(ptr+b) to ptr[b]
  // Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label SCREEN = $400
  .label idx = 4
  // @begin
bbegin:
  // [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
  // @1
b1:
  // [2] call main 
  // [4] phi from @1 to main [phi:@1->main]
main_from_b1:
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
  // @end
bend:
  // main
main: {
    // [5] call print 
    // [9] phi from main to print [phi:main->print]
  print_from_main:
    // [9] phi (byte) idx#10 = (byte) 0 [phi:main->print#0] -- vbuz1=vbuc1 
    lda #0
    sta idx
    // [9] phi (byte*) print::m#2 = (const byte[]) msg1#0 [phi:main->print#1] -- pbuz1=pbuc1 
    lda #<msg1
    sta print.m
    lda #>msg1
    sta print.m+1
    jsr print
    // [6] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
    jmp b1
    // main::@1
  b1:
    // [7] call print 
    // [9] phi from main::@1 to print [phi:main::@1->print]
  print_from_b1:
    // [9] phi (byte) idx#10 = (byte) idx#11 [phi:main::@1->print#0] -- register_copy 
    // [9] phi (byte*) print::m#2 = (const byte[]) msg2#0 [phi:main::@1->print#1] -- pbuz1=pbuc1 
    lda #<msg2
    sta print.m
    lda #>msg2
    sta print.m+1
    jsr print
    jmp breturn
    // main::@return
  breturn:
    // [8] return 
    rts
}
  // print
// print(byte* zeropage(2) m)
print: {
    .label m = 2
    // [10] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*) print::m#2 + (byte) 2) -- pbuc1_derefidx_vbuz1=pbuz2_derefidx_vbuc2 
    ldx idx
    ldy #2
    lda (m),y
    sta SCREEN,x
    // [11] (byte) idx#11 ← ++ (byte) idx#10 -- vbuz1=_inc_vbuz1 
    inc idx
    jmp breturn
    // print::@return
  breturn:
    // [12] return 
    rts
}
  // File Data
  msg1: .byte 'a', 'b', 'c', 'd'
  msg2: .byte '1', '2', '3', '4'

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [10] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*) print::m#2 + (byte) 2) [ idx#10 ] ( main:2::print:5 [ idx#10 ] main:2::print:7 [ idx#10 ] ) always clobbers reg byte a reg byte y 
Removing always clobbered register reg byte a as potential for zp ZP_BYTE:4 [ idx#10 idx#11 ]
Removing always clobbered register reg byte y as potential for zp ZP_BYTE:4 [ idx#10 idx#11 ]
Statement [10] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*) print::m#2 + (byte) 2) [ idx#10 ] ( main:2::print:5 [ idx#10 ] main:2::print:7 [ idx#10 ] ) always clobbers reg byte a reg byte y 
Potential registers zp ZP_WORD:2 [ print::m#2 ] : zp ZP_WORD:2 , 
Potential registers zp ZP_BYTE:4 [ idx#10 idx#11 ] : zp ZP_BYTE:4 , reg byte x , 

REGISTER UPLIFT SCOPES
Uplift Scope [] 4: zp ZP_BYTE:4 [ idx#10 idx#11 ] 
Uplift Scope [print] 2: zp ZP_WORD:2 [ print::m#2 ] 
Uplift Scope [main] 

Uplifting [] best 81 combination reg byte x [ idx#10 idx#11 ] 
Uplifting [print] best 81 combination zp ZP_WORD:2 [ print::m#2 ] 
Uplifting [main] best 81 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Tests optimizing derefs of *(ptr+b) to ptr[b]
  // Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label SCREEN = $400
  // @begin
bbegin:
  // [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
  // @1
b1:
  // [2] call main 
  // [4] phi from @1 to main [phi:@1->main]
main_from_b1:
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
  // @end
bend:
  // main
main: {
    // [5] call print 
    // [9] phi from main to print [phi:main->print]
  print_from_main:
    // [9] phi (byte) idx#10 = (byte) 0 [phi:main->print#0] -- vbuxx=vbuc1 
    ldx #0
    // [9] phi (byte*) print::m#2 = (const byte[]) msg1#0 [phi:main->print#1] -- pbuz1=pbuc1 
    lda #<msg1
    sta print.m
    lda #>msg1
    sta print.m+1
    jsr print
    // [6] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
    jmp b1
    // main::@1
  b1:
    // [7] call print 
    // [9] phi from main::@1 to print [phi:main::@1->print]
  print_from_b1:
    // [9] phi (byte) idx#10 = (byte) idx#11 [phi:main::@1->print#0] -- register_copy 
    // [9] phi (byte*) print::m#2 = (const byte[]) msg2#0 [phi:main::@1->print#1] -- pbuz1=pbuc1 
    lda #<msg2
    sta print.m
    lda #>msg2
    sta print.m+1
    jsr print
    jmp breturn
    // main::@return
  breturn:
    // [8] return 
    rts
}
  // print
// print(byte* zeropage(2) m)
print: {
    .label m = 2
    // [10] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*) print::m#2 + (byte) 2) -- pbuc1_derefidx_vbuxx=pbuz1_derefidx_vbuc2 
    ldy #2
    lda (m),y
    sta SCREEN,x
    // [11] (byte) idx#11 ← ++ (byte) idx#10 -- vbuxx=_inc_vbuxx 
    inx
    jmp breturn
    // print::@return
  breturn:
    // [12] return 
    rts
}
  // File Data
  msg1: .byte 'a', 'b', 'c', 'd'
  msg2: .byte '1', '2', '3', '4'

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp breturn
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
Removing instruction bend_from_b1:
Removing instruction b1_from_main:
Removing instruction print_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction print_from_main:
Removing instruction b1:
Removing instruction breturn:
Removing instruction breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(byte) idx
(byte) idx#10 reg byte x 3.0
(byte) idx#11 reg byte x 1.0
(void()) main()
(label) main::@1
(label) main::@return
(byte[]) msg1
(const byte[]) msg1#0 msg1 = { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }
(byte[]) msg2
(const byte[]) msg2#0 msg2 = { (byte) '1', (byte) '2', (byte) '3', (byte) '4' }
(void()) print((byte*) print::m)
(label) print::@return
(byte*) print::m
(byte*) print::m#2 m zp ZP_WORD:2 2.0

zp ZP_WORD:2 [ print::m#2 ]
reg byte x [ idx#10 idx#11 ]


FINAL ASSEMBLER
Score: 60

  // File Comments
// Tests optimizing derefs of *(ptr+b) to ptr[b]
  // Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
  // Global Constants & labels
  .label SCREEN = $400
  // @begin
  // [1] phi from @begin to @1 [phi:@begin->@1]
  // @1
  // [2] call main 
  // [4] phi from @1 to main [phi:@1->main]
  // [3] phi from @1 to @end [phi:@1->@end]
  // @end
  // main
main: {
    // print(msg1)
    // [5] call print 
    // [9] phi from main to print [phi:main->print]
    // [9] phi (byte) idx#10 = (byte) 0 [phi:main->print#0] -- vbuxx=vbuc1 
    ldx #0
    // [9] phi (byte*) print::m#2 = (const byte[]) msg1#0 [phi:main->print#1] -- pbuz1=pbuc1 
    lda #<msg1
    sta print.m
    lda #>msg1
    sta print.m+1
    jsr print
    // [6] phi from main to main::@1 [phi:main->main::@1]
    // main::@1
    // print(msg2)
    // [7] call print 
    // [9] phi from main::@1 to print [phi:main::@1->print]
    // [9] phi (byte) idx#10 = (byte) idx#11 [phi:main::@1->print#0] -- register_copy 
    // [9] phi (byte*) print::m#2 = (const byte[]) msg2#0 [phi:main::@1->print#1] -- pbuz1=pbuc1 
    lda #<msg2
    sta print.m
    lda #>msg2
    sta print.m+1
    jsr print
    // main::@return
    // }
    // [8] return 
    rts
}
  // print
// print(byte* zeropage(2) m)
print: {
    .label m = 2
    // SCREEN[idx++] = *(m+2)
    // [10] *((const byte*) SCREEN#0 + (byte) idx#10) ← *((byte*) print::m#2 + (byte) 2) -- pbuc1_derefidx_vbuxx=pbuz1_derefidx_vbuc2 
    ldy #2
    lda (m),y
    sta SCREEN,x
    // SCREEN[idx++] = *(m+2);
    // [11] (byte) idx#11 ← ++ (byte) idx#10 -- vbuxx=_inc_vbuxx 
    inx
    // print::@return
    // }
    // [12] return 
    rts
}
  // File Data
  msg1: .byte 'a', 'b', 'c', 'd'
  msg2: .byte '1', '2', '3', '4'

