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

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) screen_idx#13 ← phi( @2/(byte) screen_idx#14 )
  (byte*) print::m#0 ← (byte[]) msg1#0
  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 ← (byte[]) msg2#0
  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
  (word*) SCREEN#0 ← ((word*)) (number) $400
  (byte) screen_idx#3 ← (number) 0
  to:@2
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
  *((word*) SCREEN#0 + (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
(word*) SCREEN
(word*) SCREEN#0
(const byte) SIZEOF_WORD = (byte) 2
(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
(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) 0 in (byte) screen_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 (word*) SCREEN#0 ← (word*)(number) $400
Inlining cast (byte) screen_idx#3 ← (unumber)(number) 0
Inlining cast (word*~) print::$1 ← (word*)(byte*~) print::$0
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (word*) 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) 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
Identified constant from value list (byte[]) { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }
Identified constant from value list (byte[]) { (byte) '1', (byte) '2', (byte) '3', (byte) '4' }
Successful SSA optimization Pass2ConstantInitializerValueLists
Constant (const byte[]) msg1#0 = { 'a', 'b', 'c', 'd' }
Constant (const byte[]) msg2#0 = { '1', '2', '3', '4' }
Constant (const word*) SCREEN#0 = (word*) 1024
Constant (const byte) screen_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] [20] *((const word*) SCREEN#0 + (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#0
Constant inlined print::m#0 = (const byte[]) msg1#0
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()
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) screen_idx#10 ← phi( main/(byte) 0 main::@1/(byte) screen_idx#11 )
  [9] (byte*) print::m#2 ← phi( main/(const byte[]) msg1#0 main::@1/(const byte[]) msg2#0 )
  [10] (byte~) print::$2 ← (byte) screen_idx#10 << (byte) 1
  [11] *((const word*) SCREEN#0 + (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
(word*) SCREEN
(void()) main()
(byte[]) msg1
(byte[]) msg2
(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 zero page equivalence class [ print::$2 ]
Complete equivalence classes
[ print::m#2 ]
[ screen_idx#10 screen_idx#11 ]
[ print::$2 ]
Allocated zp ZP_WORD:2 [ print::m#2 ]
Allocated zp ZP_BYTE:4 [ screen_idx#10 screen_idx#11 ]
Allocated zp ZP_BYTE:5 [ print::$2 ]

INITIAL ASM
Target platform is c64basic
  // 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#0 [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#0 [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* zeropage(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#0 + (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 ZP_BYTE:4 [ screen_idx#10 screen_idx#11 ]
Statement [11] *((const word*) SCREEN#0 + (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 ZP_BYTE: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#0 + (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 ZP_WORD:2 [ print::m#2 ] : zp ZP_WORD:2 , 
Potential registers zp ZP_BYTE:4 [ screen_idx#10 screen_idx#11 ] : zp ZP_BYTE:4 , reg byte x , 
Potential registers zp ZP_BYTE:5 [ print::$2 ] : zp ZP_BYTE:5 , reg byte a , reg byte x , reg byte y , 

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

Uplifting [print] best 107 combination reg byte a [ print::$2 ] zp ZP_WORD:2 [ print::m#2 ] 
Uplifting [] best 107 combination zp ZP_BYTE:4 [ screen_idx#10 screen_idx#11 ] 
Uplifting [main] best 107 combination 
Attempting to uplift remaining variables inzp ZP_BYTE:4 [ screen_idx#10 screen_idx#11 ]
Uplifting [] best 107 combination zp ZP_BYTE: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#0 [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#0 [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* zeropage(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#0 + (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
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
(word*) SCREEN
(const word*) SCREEN#0 SCREEN = (word*) 1024
(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)
(byte~) print::$2 reg byte a 4.0
(label) print::@return
(byte*) print::m
(byte*) print::m#2 m zp ZP_WORD:2
(byte) screen_idx
(byte) screen_idx#10 screen_idx zp ZP_BYTE:4 2.0
(byte) screen_idx#11 screen_idx zp ZP_BYTE:4 1.0

zp ZP_WORD:2 [ print::m#2 ]
zp ZP_BYTE: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#0 [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#0 [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* zeropage(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#0 + (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'

