Fixing pointer array-indexing *((signed word*) SCREEN + (byte) main::i)
Fixing pointer array-indexing *((signed word*) SCREEN + (byte) main::i)
Identified constant variable (signed word*) SCREEN
Culled Empty Block (label) main::@4
Culled Empty Block (label) main::@3
Culled Empty Block (label) main::@5
Culled Empty Block (label) main::@6

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

(void()) main()
main: scope:[main]  from @1
  (byte) main::i ← (byte) 0
  (signed word) main::j ← (signed word) 0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  (bool~) main::$0 ← (byte) main::i < (number) 4
  if((bool~) main::$0) goto main::@2
  to:main::@return
main::@2: scope:[main]  from main::@1
  (byte~) main::$3 ← (byte) main::i * (const byte) SIZEOF_SIGNED_WORD
  *((const signed word*) SCREEN + (byte~) main::$3) ← (signed word) main::j
  (byte) main::i ← ++ (byte) main::i
  (signed word) main::j ← ++ (signed word) main::j
  (signed word~) main::$1 ← ((signed word)) (byte) main::i
  (number~) main::$2 ← (signed word~) main::$1 * (number) 2
  (signed word) main::k#0 ← (number~) main::$2
  (byte~) main::$4 ← (byte) main::i * (const byte) SIZEOF_SIGNED_WORD
  *((const signed word*) SCREEN + (byte~) main::$4) ← (signed word) main::k#0
  (byte) main::i ← ++ (byte) main::i
  to:main::@1
main::@return: scope:[main]  from main::@1
  return 
  to:@return
@1: scope:[]  from @begin
  call main 
  to:@2
@2: scope:[]  from @1
  to:@end
@end: scope:[]  from @2

SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(const signed word*) SCREEN = (signed word*)(number) $400
(const byte) SIZEOF_SIGNED_WORD = (byte) 2
(void()) main()
(bool~) main::$0
(signed word~) main::$1
(number~) main::$2
(byte~) main::$3
(byte~) main::$4
(label) main::@1
(label) main::@2
(label) main::@return
(byte) main::i loadstore !zp[-1]:2
(signed word) main::j loadstore !zp[-1]:4
(signed word) main::k
(signed word) main::k#0

Adding number conversion cast (unumber) 4 in (bool~) main::$0 ← (byte) main::i < (number) 4
Adding number conversion cast (snumber) 2 in (number~) main::$2 ← (signed word~) main::$1 * (number) 2
Adding number conversion cast (snumber) main::$2 in (number~) main::$2 ← (signed word~) main::$1 * (snumber)(number) 2
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (signed word~) main::$1 ← (signed word)(byte) main::i
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (signed word*) 1024
Simplifying constant integer cast 4
Simplifying constant integer cast 2
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 4
Finalized signed number type (signed byte) 2
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to signed word in (snumber~) main::$2 ← (signed word~) main::$1 * (signed byte) 2
Alias (signed word) main::k#0 = (signed word~) main::$2 
Successful SSA optimization Pass2AliasElimination
Simple Condition (bool~) main::$0 [3] if((byte) main::i<(byte) 4) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Rewriting multiplication to use shift [3] (byte~) main::$3 ← (byte) main::i * (const byte) SIZEOF_SIGNED_WORD
Rewriting multiplication to use shift [8] (signed word) main::k#0 ← (signed word~) main::$1 * (signed byte) 2
Rewriting multiplication to use shift [9] (byte~) main::$4 ← (byte) main::i * (const byte) SIZEOF_SIGNED_WORD
Successful SSA optimization Pass2MultiplyToShiftRewriting
Eliminating unused constant (const byte) SIZEOF_SIGNED_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 @end
CALL GRAPH
Calls in [] to main:2 

Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Culled Empty Block (label) @2
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end

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] (byte) main::i ← (byte) 0
  [5] (signed word) main::j ← (signed word) 0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  [6] if((byte) main::i<(byte) 4) goto main::@2
  to:main::@return
main::@return: scope:[main]  from main::@1
  [7] return 
  to:@return
main::@2: scope:[main]  from main::@1
  [8] (byte~) main::$3 ← (byte) main::i << (byte) 1
  [9] *((const signed word*) SCREEN + (byte~) main::$3) ← (signed word) main::j
  [10] (byte) main::i ← ++ (byte) main::i
  [11] (signed word) main::j ← ++ (signed word) main::j
  [12] (signed word~) main::$1 ← (signed word)(byte) main::i
  [13] (signed word) main::k#0 ← (signed word~) main::$1 << (byte) 1
  [14] (byte~) main::$4 ← (byte) main::i << (byte) 1
  [15] *((const signed word*) SCREEN + (byte~) main::$4) ← (signed word) main::k#0
  [16] (byte) main::i ← ++ (byte) main::i
  to:main::@1


VARIABLE REGISTER WEIGHTS
(void()) main()
(signed word~) main::$1 22.0
(byte~) main::$3 22.0
(byte~) main::$4 22.0
(byte) main::i loadstore !zp[-1]:2 9.875
(signed word) main::j loadstore !zp[-1]:4 3.8888888888888893
(signed word) main::k
(signed word) main::k#0 11.0

Initial phi equivalence classes
Added variable main::i to live range equivalence class [ main::i ]
Added variable main::j to live range equivalence class [ main::j ]
Added variable main::$3 to live range equivalence class [ main::$3 ]
Added variable main::$1 to live range equivalence class [ main::$1 ]
Added variable main::k#0 to live range equivalence class [ main::k#0 ]
Added variable main::$4 to live range equivalence class [ main::$4 ]
Complete equivalence classes
[ main::i ]
[ main::j ]
[ main::$3 ]
[ main::$1 ]
[ main::k#0 ]
[ main::$4 ]
Allocated zp[1]:3 [ main::$3 ]
Allocated zp[2]:6 [ main::$1 ]
Allocated zp[2]:8 [ main::k#0 ]
Allocated zp[1]:10 [ main::$4 ]

INITIAL ASM
Target platform is c64basic / MOS6502X
  // File Comments
// Test declaring a variable as register on a specific ZP address
  // 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 
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
  jmp __bend
  // @end
__bend:
  // main
main: {
    .label i = 2
    .label j = 4
    .label __1 = 6
    .label __3 = 3
    .label __4 = $a
    .label k = 8
    // [4] (byte) main::i ← (byte) 0 -- vbuz1=vbuc1 
    lda #0
    sta.z i
    // [5] (signed word) main::j ← (signed word) 0 -- vwsz1=vwsc1 
    lda #<0
    sta.z j
    lda #>0
    sta.z j+1
    jmp __b1
    // main::@1
  __b1:
    // [6] if((byte) main::i<(byte) 4) goto main::@2 -- vbuz1_lt_vbuc1_then_la1 
    lda.z i
    cmp #4
    bcc __b2
    jmp __breturn
    // main::@return
  __breturn:
    // [7] return 
    rts
    // main::@2
  __b2:
    // [8] (byte~) main::$3 ← (byte) main::i << (byte) 1 -- vbuz1=vbuz2_rol_1 
    lda.z i
    asl
    sta.z __3
    // [9] *((const signed word*) SCREEN + (byte~) main::$3) ← (signed word) main::j -- pwsc1_derefidx_vbuz1=vwsz2 
    ldy.z __3
    lda.z j
    sta SCREEN,y
    lda.z j+1
    sta SCREEN+1,y
    // [10] (byte) main::i ← ++ (byte) main::i -- vbuz1=_inc_vbuz1 
    inc.z i
    // [11] (signed word) main::j ← ++ (signed word) main::j -- vwsz1=_inc_vwsz1 
    inc.z j
    bne !+
    inc.z j+1
  !:
    // [12] (signed word~) main::$1 ← (signed word)(byte) main::i -- vwsz1=_sword_vbuz2 
    lda.z i
    sta.z __1
    lda #0
    sta.z __1+1
    // [13] (signed word) main::k#0 ← (signed word~) main::$1 << (byte) 1 -- vwsz1=vwsz2_rol_1 
    lda.z __1
    asl
    sta.z k
    lda.z __1+1
    rol
    sta.z k+1
    // [14] (byte~) main::$4 ← (byte) main::i << (byte) 1 -- vbuz1=vbuz2_rol_1 
    lda.z i
    asl
    sta.z __4
    // [15] *((const signed word*) SCREEN + (byte~) main::$4) ← (signed word) main::k#0 -- pwsc1_derefidx_vbuz1=vwsz2 
    ldy.z __4
    lda.z k
    sta SCREEN,y
    lda.z k+1
    sta SCREEN+1,y
    // [16] (byte) main::i ← ++ (byte) main::i -- vbuz1=_inc_vbuz1 
    inc.z i
    jmp __b1
}
  // File Data

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] (byte) main::i ← (byte) 0 [ main::i ] ( main:2 [ main::i ] ) always clobbers reg byte a 
Statement [5] (signed word) main::j ← (signed word) 0 [ main::i main::j ] ( main:2 [ main::i main::j ] ) always clobbers reg byte a 
Statement [6] if((byte) main::i<(byte) 4) goto main::@2 [ main::i main::j ] ( main:2 [ main::i main::j ] ) always clobbers reg byte a 
Statement [8] (byte~) main::$3 ← (byte) main::i << (byte) 1 [ main::j main::$3 ] ( main:2 [ main::j main::$3 ] ) always clobbers reg byte a 
Statement [9] *((const signed word*) SCREEN + (byte~) main::$3) ← (signed word) main::j [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [12] (signed word~) main::$1 ← (signed word)(byte) main::i [ main::i main::j main::$1 ] ( main:2 [ main::i main::j main::$1 ] ) always clobbers reg byte a 
Statement [13] (signed word) main::k#0 ← (signed word~) main::$1 << (byte) 1 [ main::i main::j main::k#0 ] ( main:2 [ main::i main::j main::k#0 ] ) always clobbers reg byte a 
Statement [14] (byte~) main::$4 ← (byte) main::i << (byte) 1 [ main::j main::k#0 main::$4 ] ( main:2 [ main::j main::k#0 main::$4 ] ) always clobbers reg byte a 
Statement [15] *((const signed word*) SCREEN + (byte~) main::$4) ← (signed word) main::k#0 [ main::j ] ( main:2 [ main::j ] ) always clobbers reg byte a 
Potential registers zp[1]:2 [ main::i ] : zp[1]:2 , 
Potential registers zp[2]:4 [ main::j ] : zp[2]:4 , 
Potential registers zp[1]:3 [ main::$3 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[2]:6 [ main::$1 ] : zp[2]:6 , 
Potential registers zp[2]:8 [ main::k#0 ] : zp[2]:8 , 
Potential registers zp[1]:10 [ main::$4 ] : zp[1]:10 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 22: zp[1]:3 [ main::$3 ] 22: zp[2]:6 [ main::$1 ] 22: zp[1]:10 [ main::$4 ] 11: zp[2]:8 [ main::k#0 ] 9.88: zp[1]:2 [ main::i ] 3.89: zp[2]:4 [ main::j ] 
Uplift Scope [] 

Uplifting [main] best 1126 combination reg byte a [ main::$3 ] zp[2]:6 [ main::$1 ] reg byte a [ main::$4 ] zp[2]:8 [ main::k#0 ] zp[1]:2 [ main::i ] zp[2]:4 [ main::j ] 
Uplifting [] best 1126 combination 
Attempting to uplift remaining variables inzp[1]:2 [ main::i ]
Uplifting [main] best 1126 combination zp[1]:2 [ main::i ] 
Coalescing zero page register [ zp[2]:6 [ main::$1 ] ] with [ zp[2]:8 [ main::k#0 ] ] - score: 1

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Test declaring a variable as register on a specific ZP address
  // 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 
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
  jmp __bend
  // @end
__bend:
  // main
main: {
    .label i = 2
    .label j = 4
    .label __1 = 6
    .label k = 6
    // [4] (byte) main::i ← (byte) 0 -- vbuz1=vbuc1 
    lda #0
    sta.z i
    // [5] (signed word) main::j ← (signed word) 0 -- vwsz1=vwsc1 
    lda #<0
    sta.z j
    lda #>0
    sta.z j+1
    jmp __b1
    // main::@1
  __b1:
    // [6] if((byte) main::i<(byte) 4) goto main::@2 -- vbuz1_lt_vbuc1_then_la1 
    lda.z i
    cmp #4
    bcc __b2
    jmp __breturn
    // main::@return
  __breturn:
    // [7] return 
    rts
    // main::@2
  __b2:
    // [8] (byte~) main::$3 ← (byte) main::i << (byte) 1 -- vbuaa=vbuz1_rol_1 
    lda.z i
    asl
    // [9] *((const signed word*) SCREEN + (byte~) main::$3) ← (signed word) main::j -- pwsc1_derefidx_vbuaa=vwsz1 
    tay
    lda.z j
    sta SCREEN,y
    lda.z j+1
    sta SCREEN+1,y
    // [10] (byte) main::i ← ++ (byte) main::i -- vbuz1=_inc_vbuz1 
    inc.z i
    // [11] (signed word) main::j ← ++ (signed word) main::j -- vwsz1=_inc_vwsz1 
    inc.z j
    bne !+
    inc.z j+1
  !:
    // [12] (signed word~) main::$1 ← (signed word)(byte) main::i -- vwsz1=_sword_vbuz2 
    lda.z i
    sta.z __1
    lda #0
    sta.z __1+1
    // [13] (signed word) main::k#0 ← (signed word~) main::$1 << (byte) 1 -- vwsz1=vwsz1_rol_1 
    asl.z k
    rol.z k+1
    // [14] (byte~) main::$4 ← (byte) main::i << (byte) 1 -- vbuaa=vbuz1_rol_1 
    lda.z i
    asl
    // [15] *((const signed word*) SCREEN + (byte~) main::$4) ← (signed word) main::k#0 -- pwsc1_derefidx_vbuaa=vwsz1 
    tay
    lda.z k
    sta SCREEN,y
    lda.z k+1
    sta SCREEN+1,y
    // [16] (byte) main::i ← ++ (byte) main::i -- vbuz1=_inc_vbuz1 
    inc.z i
    jmp __b1
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #<0
Removing instruction lda #>0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Replacing label __bbegin with __b1
Removing instruction __bbegin:
Removing instruction __b1_from___bbegin:
Removing instruction __bend_from___b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __bend:
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 signed word*) SCREEN = (signed word*) 1024
(void()) main()
(signed word~) main::$1 zp[2]:6 22.0
(byte~) main::$3 reg byte a 22.0
(byte~) main::$4 reg byte a 22.0
(label) main::@1
(label) main::@2
(label) main::@return
(byte) main::i loadstore !zp[-1]:2 zp[1]:2 9.875
(signed word) main::j loadstore !zp[-1]:4 zp[2]:4 3.8888888888888893
(signed word) main::k
(signed word) main::k#0 k zp[2]:6 11.0

zp[1]:2 [ main::i ]
zp[2]:4 [ main::j ]
reg byte a [ main::$3 ]
zp[2]:6 [ main::$1 main::k#0 ]
reg byte a [ main::$4 ]


FINAL ASSEMBLER
Score: 1017

  // File Comments
// Test declaring a variable as register on a specific ZP address
  // 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 
  // [3] phi from @1 to @end [phi:@1->@end]
  // @end
  // main
main: {
    .label i = 2
    .label j = 4
    .label __1 = 6
    .label k = 6
    // i=0
    // [4] (byte) main::i ← (byte) 0 -- vbuz1=vbuc1 
    lda #0
    sta.z i
    // j=0
    // [5] (signed word) main::j ← (signed word) 0 -- vwsz1=vwsc1 
    sta.z j
    sta.z j+1
    // main::@1
  __b1:
    // while(i<4)
    // [6] if((byte) main::i<(byte) 4) goto main::@2 -- vbuz1_lt_vbuc1_then_la1 
    lda.z i
    cmp #4
    bcc __b2
    // main::@return
    // }
    // [7] return 
    rts
    // main::@2
  __b2:
    // SCREEN[i++] = j++
    // [8] (byte~) main::$3 ← (byte) main::i << (byte) 1 -- vbuaa=vbuz1_rol_1 
    lda.z i
    asl
    // [9] *((const signed word*) SCREEN + (byte~) main::$3) ← (signed word) main::j -- pwsc1_derefidx_vbuaa=vwsz1 
    tay
    lda.z j
    sta SCREEN,y
    lda.z j+1
    sta SCREEN+1,y
    // SCREEN[i++] = j++;
    // [10] (byte) main::i ← ++ (byte) main::i -- vbuz1=_inc_vbuz1 
    inc.z i
    // [11] (signed word) main::j ← ++ (signed word) main::j -- vwsz1=_inc_vwsz1 
    inc.z j
    bne !+
    inc.z j+1
  !:
    // (int)i
    // [12] (signed word~) main::$1 ← (signed word)(byte) main::i -- vwsz1=_sword_vbuz2 
    lda.z i
    sta.z __1
    lda #0
    sta.z __1+1
    // k = (int)i*2
    // [13] (signed word) main::k#0 ← (signed word~) main::$1 << (byte) 1 -- vwsz1=vwsz1_rol_1 
    asl.z k
    rol.z k+1
    // SCREEN[i++] = k
    // [14] (byte~) main::$4 ← (byte) main::i << (byte) 1 -- vbuaa=vbuz1_rol_1 
    lda.z i
    asl
    // [15] *((const signed word*) SCREEN + (byte~) main::$4) ← (signed word) main::k#0 -- pwsc1_derefidx_vbuaa=vwsz1 
    tay
    lda.z k
    sta SCREEN,y
    lda.z k+1
    sta SCREEN+1,y
    // SCREEN[i++] = k;
    // [16] (byte) main::i ← ++ (byte) main::i -- vbuz1=_inc_vbuz1 
    inc.z i
    jmp __b1
}
  // File Data

