Fixing pointer array-indexing *((const word*) SCREEN + (byte) screen_idx)

CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  to:@1

(void()) main()
main: scope:[main]  from @2
  (byte) screen_idx#13 ← phi( @2/(byte) screen_idx#14 )
  (byte*) print::m#0 ← (const byte*) msg1
  call print 
  to:main::@1
main::@1: scope:[main]  from main
  (byte) screen_idx#7 ← phi( main/(byte) screen_idx#5 )
  (byte) screen_idx#0 ← (byte) screen_idx#7
  (byte*) print::m#1 ← (const byte*) msg2
  call print 
  to:main::@2
main::@2: scope:[main]  from main::@1
  (byte) screen_idx#8 ← phi( main::@1/(byte) screen_idx#5 )
  (byte) screen_idx#1 ← (byte) screen_idx#8
  to:main::@return
main::@return: scope:[main]  from main::@2
  (byte) screen_idx#9 ← phi( main::@2/(byte) screen_idx#1 )
  (byte) screen_idx#2 ← (byte) screen_idx#9
  return 
  to:@return
@1: scope:[]  from @begin
  (byte) screen_idx#3 ← (byte) 0
  to:@2

(void()) print((byte*) print::m)
print: scope:[print]  from main main::@1
  (byte) screen_idx#10 ← phi( main/(byte) screen_idx#13 main::@1/(byte) screen_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
  (word*~) print::$1 ← ((word*)) (byte*~) print::$0
  (byte~) print::$2 ← (byte) screen_idx#10 * (const byte) SIZEOF_WORD
  *((const word*) SCREEN + (byte~) print::$2) ← *((word*~) print::$1)
  (byte) screen_idx#4 ← ++ (byte) screen_idx#10
  to:print::@return
print::@return: scope:[print]  from print
  (byte) screen_idx#11 ← phi( print/(byte) screen_idx#4 )
  (byte) screen_idx#5 ← (byte) screen_idx#11
  return 
  to:@return
@2: scope:[]  from @1
  (byte) screen_idx#14 ← phi( @1/(byte) screen_idx#3 )
  call main 
  to:@3
@3: scope:[]  from @2
  (byte) screen_idx#12 ← phi( @2/(byte) screen_idx#2 )
  (byte) screen_idx#6 ← (byte) screen_idx#12
  to:@end
@end: scope:[]  from @3

SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @3
(label) @begin
(label) @end
(const word*) SCREEN = (word*)(number) $400
(const byte) SIZEOF_WORD = (byte) 2
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@return
(const byte*) msg1[]  = { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }
(const byte*) msg2[]  = { (byte) '1', (byte) '2', (byte) '3', (byte) '4' }
(void()) print((byte*) print::m)
(byte*~) print::$0
(word*~) print::$1
(byte~) print::$2
(label) print::@return
(byte*) print::m
(byte*) print::m#0
(byte*) print::m#1
(byte*) print::m#2
(byte) screen_idx
(byte) screen_idx#0
(byte) screen_idx#1
(byte) screen_idx#10
(byte) screen_idx#11
(byte) screen_idx#12
(byte) screen_idx#13
(byte) screen_idx#14
(byte) screen_idx#2
(byte) screen_idx#3
(byte) screen_idx#4
(byte) screen_idx#5
(byte) screen_idx#6
(byte) screen_idx#7
(byte) screen_idx#8
(byte) screen_idx#9

Adding number conversion cast (unumber) 2 in (byte*~) print::$0 ← (byte*) print::m#2 + (number) 2
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (word*~) print::$1 ← (word*)(byte*~) print::$0
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (word*) 1024
Simplifying constant integer cast 2
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 2
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias (byte) screen_idx#0 = (byte) screen_idx#7 
Alias (byte) screen_idx#1 = (byte) screen_idx#8 (byte) screen_idx#9 (byte) screen_idx#2 
Alias (byte) screen_idx#11 = (byte) screen_idx#4 (byte) screen_idx#5 
Alias (byte) screen_idx#14 = (byte) screen_idx#3 
Alias (byte) screen_idx#12 = (byte) screen_idx#6 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) screen_idx#13 (byte) screen_idx#14
Identical Phi Values (byte) screen_idx#0 (byte) screen_idx#11
Identical Phi Values (byte) screen_idx#1 (byte) screen_idx#11
Identical Phi Values (byte) screen_idx#12 (byte) screen_idx#1
Successful SSA optimization Pass2IdenticalPhiElimination
Constant (const byte*) print::m#0 = msg1
Constant (const byte*) print::m#1 = msg2
Constant (const byte) screen_idx#14 = 0
Successful SSA optimization Pass2ConstantIdentification
Converting *(pointer+n) to pointer[n] [13] *((const word*) SCREEN + (byte~) print::$2) ← *((word*~) print::$1)  -- *((word*)print::m#2 + 2)
Successful SSA optimization Pass2InlineDerefIdx
Eliminating unused variable (word*~) print::$1 and assignment [5] (word*~) print::$1 ← (word*)(byte*~) print::$0
Successful SSA optimization PassNEliminateUnusedVars
Eliminating unused variable (byte*~) print::$0 and assignment [4] (byte*~) print::$0 ← (byte*) print::m#2 + (byte) 2
Successful SSA optimization PassNEliminateUnusedVars
Rewriting multiplication to use shift [4] (byte~) print::$2 ← (byte) screen_idx#10 * (const byte) SIZEOF_WORD
Successful SSA optimization Pass2MultiplyToShiftRewriting
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) screen_idx#14
Constant inlined print::m#1 = (const byte*) msg2
Constant inlined print::m#0 = (const byte*) msg1
Constant inlined screen_idx#14 = (byte) 0
Successful SSA optimization Pass2ConstantInlining
Eliminating unused constant (const byte) SIZEOF_WORD
Successful SSA optimization PassNEliminateUnusedVars
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] screen_idx#15 ← screen_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()

(void()) main()
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

(void()) print((byte*) print::m)
print: scope:[print]  from main main::@1
  [9] (byte) screen_idx#10 ← phi( main/(byte) 0 main::@1/(byte) screen_idx#11 )
  [9] (byte*) print::m#2 ← phi( main/(const byte*) msg1 main::@1/(const byte*) msg2 )
  [10] (byte~) print::$2 ← (byte) screen_idx#10 << (byte) 1
  [11] *((const word*) SCREEN + (byte~) print::$2) ← *((word*)(byte*) print::m#2 + (byte) 2)
  [12] (byte) screen_idx#11 ← ++ (byte) screen_idx#10
  to:print::@return
print::@return: scope:[print]  from print
  [13] return 
  to:@return


VARIABLE REGISTER WEIGHTS
(void()) main()
(void()) print((byte*) print::m)
(byte~) print::$2 4.0
(byte*) print::m
(byte*) print::m#2
(byte) screen_idx
(byte) screen_idx#10 2.0
(byte) screen_idx#11 1.0

Initial phi equivalence classes
[ print::m#2 ]
[ screen_idx#10 screen_idx#11 ]
Added variable print::$2 to live range equivalence class [ print::$2 ]
Complete equivalence classes
[ print::m#2 ]
[ screen_idx#10 screen_idx#11 ]
[ print::$2 ]
Allocated zp[2]:2 [ print::m#2 ]
Allocated zp[1]:4 [ screen_idx#10 screen_idx#11 ]
Allocated zp[1]:5 [ print::$2 ]

INITIAL ASM
Target platform is c64basic / MOS6502X
  // File Comments
// Tests optimizing derefs of *(ptr+b) to ptr[b - even when a noop-cast is needed
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label SCREEN = $400
  .label screen_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) screen_idx#10 = (byte) 0 [phi:main->print#0] -- vbuz1=vbuc1 
    lda #0
    sta.z screen_idx
    // [9] phi (byte*) print::m#2 = (const byte*) msg1 [phi:main->print#1] -- pbuz1=pbuc1 
    lda #<msg1
    sta.z print.m
    lda #>msg1
    sta.z 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) screen_idx#10 = (byte) screen_idx#11 [phi:main::@1->print#0] -- register_copy 
    // [9] phi (byte*) print::m#2 = (const byte*) msg2 [phi:main::@1->print#1] -- pbuz1=pbuc1 
    lda #<msg2
    sta.z print.m
    lda #>msg2
    sta.z print.m+1
    jsr print
    jmp __breturn
    // main::@return
  __breturn:
    // [8] return 
    rts
}
  // print
// print(byte* zp(2) m)
print: {
    .label __2 = 5
    .label m = 2
    // [10] (byte~) print::$2 ← (byte) screen_idx#10 << (byte) 1 -- vbuz1=vbuz2_rol_1 
    lda.z screen_idx
    asl
    sta.z __2
    // [11] *((const word*) SCREEN + (byte~) print::$2) ← *((word*)(byte*) print::m#2 + (byte) 2) -- pwuc1_derefidx_vbuz1=pwuz2_derefidx_vbuc2 
    ldx.z __2
    ldy #2
    lda (m),y
    sta SCREEN,x
    iny
    lda (m),y
    sta SCREEN+1,x
    // [12] (byte) screen_idx#11 ← ++ (byte) screen_idx#10 -- vbuz1=_inc_vbuz1 
    inc.z screen_idx
    jmp __breturn
    // print::@return
  __breturn:
    // [13] return 
    rts
}
  // File Data
  msg1: .byte 'a', 'b', 'c', 'd'
  msg2: .byte '1', '2', '3', '4'

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

REGISTER UPLIFT SCOPES
Uplift Scope [print] 4: zp[1]:5 [ print::$2 ] 0: zp[2]:2 [ print::m#2 ] 
Uplift Scope [] 3: zp[1]:4 [ screen_idx#10 screen_idx#11 ] 
Uplift Scope [main] 

Uplifting [print] best 107 combination reg byte a [ print::$2 ] zp[2]:2 [ print::m#2 ] 
Uplifting [] best 107 combination zp[1]:4 [ screen_idx#10 screen_idx#11 ] 
Uplifting [main] best 107 combination 
Attempting to uplift remaining variables inzp[1]:4 [ screen_idx#10 screen_idx#11 ]
Uplifting [] best 107 combination zp[1]:4 [ screen_idx#10 screen_idx#11 ] 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Tests optimizing derefs of *(ptr+b) to ptr[b - even when a noop-cast is needed
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label SCREEN = $400
  .label screen_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) screen_idx#10 = (byte) 0 [phi:main->print#0] -- vbuz1=vbuc1 
    lda #0
    sta.z screen_idx
    // [9] phi (byte*) print::m#2 = (const byte*) msg1 [phi:main->print#1] -- pbuz1=pbuc1 
    lda #<msg1
    sta.z print.m
    lda #>msg1
    sta.z 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) screen_idx#10 = (byte) screen_idx#11 [phi:main::@1->print#0] -- register_copy 
    // [9] phi (byte*) print::m#2 = (const byte*) msg2 [phi:main::@1->print#1] -- pbuz1=pbuc1 
    lda #<msg2
    sta.z print.m
    lda #>msg2
    sta.z print.m+1
    jsr print
    jmp __breturn
    // main::@return
  __breturn:
    // [8] return 
    rts
}
  // print
// print(byte* zp(2) m)
print: {
    .label m = 2
    // [10] (byte~) print::$2 ← (byte) screen_idx#10 << (byte) 1 -- vbuaa=vbuz1_rol_1 
    lda.z screen_idx
    asl
    // [11] *((const word*) SCREEN + (byte~) print::$2) ← *((word*)(byte*) print::m#2 + (byte) 2) -- pwuc1_derefidx_vbuaa=pwuz1_derefidx_vbuc2 
    ldy #2
    tax
    lda (m),y
    sta SCREEN,x
    iny
    lda (m),y
    sta SCREEN+1,x
    // [12] (byte) screen_idx#11 ← ++ (byte) screen_idx#10 -- vbuz1=_inc_vbuz1 
    inc.z screen_idx
    jmp __breturn
    // print::@return
  __breturn:
    // [13] 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
Replacing label __bbegin with __b1
Removing instruction __bbegin:
Removing instruction __b1_from___bbegin:
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 __b1:
Succesful ASM optimization Pass5UnusedLabelElimination

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

zp[2]:2 [ print::m#2 ]
zp[1]:4 [ screen_idx#10 screen_idx#11 ]
reg byte a [ print::$2 ]


FINAL ASSEMBLER
Score: 86

  // File Comments
// Tests optimizing derefs of *(ptr+b) to ptr[b - even when a noop-cast is needed
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
  // Global Constants & labels
  .label SCREEN = $400
  .label screen_idx = 4
  // @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) screen_idx#10 = (byte) 0 [phi:main->print#0] -- vbuz1=vbuc1 
    lda #0
    sta.z screen_idx
    // [9] phi (byte*) print::m#2 = (const byte*) msg1 [phi:main->print#1] -- pbuz1=pbuc1 
    lda #<msg1
    sta.z print.m
    lda #>msg1
    sta.z 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) screen_idx#10 = (byte) screen_idx#11 [phi:main::@1->print#0] -- register_copy 
    // [9] phi (byte*) print::m#2 = (const byte*) msg2 [phi:main::@1->print#1] -- pbuz1=pbuc1 
    lda #<msg2
    sta.z print.m
    lda #>msg2
    sta.z print.m+1
    jsr print
    // main::@return
    // }
    // [8] return 
    rts
}
  // print
// print(byte* zp(2) m)
print: {
    .label m = 2
    // SCREEN[screen_idx++] = *(word*)(m+2)
    // [10] (byte~) print::$2 ← (byte) screen_idx#10 << (byte) 1 -- vbuaa=vbuz1_rol_1 
    lda.z screen_idx
    asl
    // [11] *((const word*) SCREEN + (byte~) print::$2) ← *((word*)(byte*) print::m#2 + (byte) 2) -- pwuc1_derefidx_vbuaa=pwuz1_derefidx_vbuc2 
    ldy #2
    tax
    lda (m),y
    sta SCREEN,x
    iny
    lda (m),y
    sta SCREEN+1,x
    // SCREEN[screen_idx++] = *(word*)(m+2);
    // [12] (byte) screen_idx#11 ← ++ (byte) screen_idx#10 -- vbuz1=_inc_vbuz1 
    inc.z screen_idx
    // print::@return
    // }
    // [13] return 
    rts
}
  // File Data
  msg1: .byte 'a', 'b', 'c', 'd'
  msg2: .byte '1', '2', '3', '4'

