
CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  (byte*) SCREEN#0 ← ((byte*)) (word/signed word/dword/signed dword) 1024
  to:@3
main: scope:[main]  from @3
  (byte*) SCREEN#1 ← phi( @3/(byte*) SCREEN#3 )
  *((byte*) SCREEN#1) ← (byte) '0'
  call d 
  to:main::@1
main::@1: scope:[main]  from main
  (byte*) SCREEN#7 ← phi( main/(byte*) SCREEN#1 )
  call b 
  to:main::@2
main::@2: scope:[main]  from main::@1
  to:main::@return
main::@return: scope:[main]  from main::@2
  return 
  to:@return
b: scope:[b]  from main::@1
  (byte*) SCREEN#5 ← phi( main::@1/(byte*) SCREEN#7 )
  (byte) b::i#0 ← (byte/signed byte/word/signed word/dword/signed dword) 0
  to:b::@1
b::@1: scope:[b]  from b b::@3
  (byte*) SCREEN#4 ← phi( b/(byte*) SCREEN#5 b::@3/(byte*) SCREEN#6 )
  (byte) b::i#3 ← phi( b/(byte) b::i#0 b::@3/(byte) b::i#1 )
  call d 
  to:b::@3
b::@3: scope:[b]  from b::@1
  (byte*) SCREEN#6 ← phi( b::@1/(byte*) SCREEN#4 )
  (byte) b::i#2 ← phi( b::@1/(byte) b::i#3 )
  (byte) b::i#1 ← (byte) b::i#2 + rangenext(0,3)
  (bool~) b::$1 ← (byte) b::i#1 != rangelast(0,3)
  if((bool~) b::$1) goto b::@1
  to:b::@return
b::@return: scope:[b]  from b::@3
  return 
  to:@return
d: scope:[d]  from b::@1 main
  (byte*) SCREEN#2 ← phi( b::@1/(byte*) SCREEN#4 main/(byte*) SCREEN#1 )
  *((byte*) SCREEN#2) ← ++ *((byte*) SCREEN#2)
  to:d::@return
d::@return: scope:[d]  from d
  return 
  to:@return
@3: scope:[]  from @begin
  (byte*) SCREEN#3 ← phi( @begin/(byte*) SCREEN#0 )
  call main 
  to:@4
@4: scope:[]  from @3
  to:@end
@end: scope:[]  from @4

SYMBOL TABLE SSA
(label) @3
(label) @4
(label) @begin
(label) @end
(byte*) SCREEN
(byte*) SCREEN#0
(byte*) SCREEN#1
(byte*) SCREEN#2
(byte*) SCREEN#3
(byte*) SCREEN#4
(byte*) SCREEN#5
(byte*) SCREEN#6
(byte*) SCREEN#7
(void()) b()
(bool~) b::$1
(label) b::@1
(label) b::@3
(label) b::@return
(byte) b::i
(byte) b::i#0
(byte) b::i#1
(byte) b::i#2
(byte) b::i#3
(void()) d()
(label) d::@return
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@return

Culled Empty Block (label) main::@2
Culled Empty Block (label) @4
Successful SSA optimization Pass2CullEmptyBlocks
Alias (byte*) SCREEN#1 = (byte*) SCREEN#7 
Alias (byte) b::i#2 = (byte) b::i#3 
Alias (byte*) SCREEN#4 = (byte*) SCREEN#6 
Alias (byte*) SCREEN#0 = (byte*) SCREEN#3 
Successful SSA optimization Pass2AliasElimination
Self Phi Eliminated (byte*) SCREEN#4
Successful SSA optimization Pass2SelfPhiElimination
Redundant Phi (byte*) SCREEN#1 (byte*) SCREEN#0
Redundant Phi (byte*) SCREEN#5 (byte*) SCREEN#1
Redundant Phi (byte*) SCREEN#4 (byte*) SCREEN#5
Successful SSA optimization Pass2RedundantPhiElimination
Redundant Phi (byte*) SCREEN#2 (byte*) SCREEN#0
Successful SSA optimization Pass2RedundantPhiElimination
Simple Condition (bool~) b::$1 [14] if((byte) b::i#1!=rangelast(0,3)) goto b::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte*) SCREEN#0 = ((byte*))1024
Constant (const byte) b::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value b::i#1 ← ++ b::i#2 to ++
Resolved ranged comparison value if(b::i#1!=rangelast(0,3)) goto b::@1 to (byte/signed byte/word/signed word/dword/signed dword) 4
Inlining constant with var siblings (const byte) b::i#0
Constant inlined b::i#0 = (byte/signed byte/word/signed word/dword/signed dword) 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting b::@4(between b::@3 and b::@1)
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main::@1
Adding NOP phi() at start of b
CALL GRAPH
Calls in [] to main:2 
Calls in [main] to d:5 b:7 
Calls in [b] to d:11 

Created 1 initial phi equivalence classes
Coalesced [15] b::i#4 ← b::i#1
Coalesced down to 1 phi equivalence classes
Culled Empty Block (label) b::@4
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main::@1
Adding NOP phi() at start of b

FINAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  [0] phi()
  to:@3
@3: scope:[]  from @begin
  [1] phi()
  [2] call main 
  to:@end
@end: scope:[]  from @3
  [3] phi()
main: scope:[main]  from @3
  [4] *((const byte*) SCREEN#0) ← (byte) '0'
  [5] call d 
  to:main::@1
main::@1: scope:[main]  from main
  [6] phi()
  [7] call b 
  to:main::@return
main::@return: scope:[main]  from main::@1
  [8] return 
  to:@return
b: scope:[b]  from main::@1
  [9] phi()
  to:b::@1
b::@1: scope:[b]  from b b::@3
  [10] (byte) b::i#2 ← phi( b/(byte/signed byte/word/signed word/dword/signed dword) 0 b::@3/(byte) b::i#1 )
  [11] call d 
  to:b::@3
b::@3: scope:[b]  from b::@1
  [12] (byte) b::i#1 ← ++ (byte) b::i#2
  [13] if((byte) b::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 4) goto b::@1
  to:b::@return
b::@return: scope:[b]  from b::@3
  [14] return 
  to:@return
d: scope:[d]  from b::@1 main
  [15] *((const byte*) SCREEN#0) ← ++ *((const byte*) SCREEN#0)
  to:d::@return
d::@return: scope:[d]  from d
  [16] return 
  to:@return

null depth in calling loop Loop head: b::@1 tails: b::@3 blocks: b::@3 b::@1  in scope d

VARIABLE REGISTER WEIGHTS
(byte*) SCREEN
(void()) b()
(byte) b::i
(byte) b::i#1 16.5
(byte) b::i#2 11.0
(void()) d()
(void()) main()

Initial phi equivalence classes
[ b::i#2 b::i#1 ]
Complete equivalence classes
[ b::i#2 b::i#1 ]
Allocated zp ZP_BYTE:2 [ b::i#2 b::i#1 ]

INITIAL ASM
//SEG0 File Comments
// A simple loop results in NullPointerException during loop analysis
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label SCREEN = $400
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
b3_from_bbegin:
  jmp b3
//SEG5 @3
b3:
//SEG6 [2] call main 
  jsr main
//SEG7 [3] phi from @3 to @end [phi:@3->@end]
bend_from_b3:
  jmp bend
//SEG8 @end
bend:
//SEG9 main
main: {
  //SEG10 [4] *((const byte*) SCREEN#0) ← (byte) '0' -- _deref_pbuc1=vbuc2 
    lda #'0'
    sta SCREEN
  //SEG11 [5] call d 
    jsr d
  //SEG12 [6] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
    jmp b1
  //SEG13 main::@1
  b1:
  //SEG14 [7] call b 
  //SEG15 [9] phi from main::@1 to b [phi:main::@1->b]
  b_from_b1:
    jsr b
    jmp breturn
  //SEG16 main::@return
  breturn:
  //SEG17 [8] return 
    rts
}
//SEG18 b
b: {
    .label i = 2
  //SEG19 [10] phi from b to b::@1 [phi:b->b::@1]
  b1_from_b:
  //SEG20 [10] phi (byte) b::i#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:b->b::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta i
    jmp b1
  //SEG21 [10] phi from b::@3 to b::@1 [phi:b::@3->b::@1]
  b1_from_b3:
  //SEG22 [10] phi (byte) b::i#2 = (byte) b::i#1 [phi:b::@3->b::@1#0] -- register_copy 
    jmp b1
  //SEG23 b::@1
  b1:
  //SEG24 [11] call d 
    jsr d
    jmp b3
  //SEG25 b::@3
  b3:
  //SEG26 [12] (byte) b::i#1 ← ++ (byte) b::i#2 -- vbuz1=_inc_vbuz1 
    inc i
  //SEG27 [13] if((byte) b::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 4) goto b::@1 -- vbuz1_neq_vbuc1_then_la1 
    lda i
    cmp #4
    bne b1_from_b3
    jmp breturn
  //SEG28 b::@return
  breturn:
  //SEG29 [14] return 
    rts
}
//SEG30 d
d: {
  //SEG31 [15] *((const byte*) SCREEN#0) ← ++ *((const byte*) SCREEN#0) -- _deref_pbuc1=_inc__deref_pbuc1 
    inc SCREEN
    jmp breturn
  //SEG32 d::@return
  breturn:
  //SEG33 [16] return 
    rts
}

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const byte*) SCREEN#0) ← (byte) '0' [ ] ( main:2 [ ] ) always clobbers reg byte a 
Potential registers zp ZP_BYTE:2 [ b::i#2 b::i#1 ] : zp ZP_BYTE:2 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [b] 27.5: zp ZP_BYTE:2 [ b::i#2 b::i#1 ] 
Uplift Scope [main] 
Uplift Scope [d] 
Uplift Scope [] 

Uplifting [b] best 328 combination reg byte x [ b::i#2 b::i#1 ] 
Uplifting [main] best 328 combination 
Uplifting [d] best 328 combination 
Uplifting [] best 328 combination 

ASSEMBLER BEFORE OPTIMIZATION
//SEG0 File Comments
// A simple loop results in NullPointerException during loop analysis
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label SCREEN = $400
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
b3_from_bbegin:
  jmp b3
//SEG5 @3
b3:
//SEG6 [2] call main 
  jsr main
//SEG7 [3] phi from @3 to @end [phi:@3->@end]
bend_from_b3:
  jmp bend
//SEG8 @end
bend:
//SEG9 main
main: {
  //SEG10 [4] *((const byte*) SCREEN#0) ← (byte) '0' -- _deref_pbuc1=vbuc2 
    lda #'0'
    sta SCREEN
  //SEG11 [5] call d 
    jsr d
  //SEG12 [6] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
    jmp b1
  //SEG13 main::@1
  b1:
  //SEG14 [7] call b 
  //SEG15 [9] phi from main::@1 to b [phi:main::@1->b]
  b_from_b1:
    jsr b
    jmp breturn
  //SEG16 main::@return
  breturn:
  //SEG17 [8] return 
    rts
}
//SEG18 b
b: {
  //SEG19 [10] phi from b to b::@1 [phi:b->b::@1]
  b1_from_b:
  //SEG20 [10] phi (byte) b::i#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:b->b::@1#0] -- vbuxx=vbuc1 
    ldx #0
    jmp b1
  //SEG21 [10] phi from b::@3 to b::@1 [phi:b::@3->b::@1]
  b1_from_b3:
  //SEG22 [10] phi (byte) b::i#2 = (byte) b::i#1 [phi:b::@3->b::@1#0] -- register_copy 
    jmp b1
  //SEG23 b::@1
  b1:
  //SEG24 [11] call d 
    jsr d
    jmp b3
  //SEG25 b::@3
  b3:
  //SEG26 [12] (byte) b::i#1 ← ++ (byte) b::i#2 -- vbuxx=_inc_vbuxx 
    inx
  //SEG27 [13] if((byte) b::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 4) goto b::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #4
    bne b1_from_b3
    jmp breturn
  //SEG28 b::@return
  breturn:
  //SEG29 [14] return 
    rts
}
//SEG30 d
d: {
  //SEG31 [15] *((const byte*) SCREEN#0) ← ++ *((const byte*) SCREEN#0) -- _deref_pbuc1=_inc__deref_pbuc1 
    inc SCREEN
    jmp breturn
  //SEG32 d::@return
  breturn:
  //SEG33 [16] return 
    rts
}

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b3
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp breturn
Removing instruction jmp b1
Removing instruction jmp b3
Removing instruction jmp breturn
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing label b1_from_b3 with b1
Removing instruction b3_from_bbegin:
Removing instruction b3:
Removing instruction bend_from_b3:
Removing instruction b1_from_main:
Removing instruction b_from_b1:
Removing instruction b1_from_b3:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b1:
Removing instruction breturn:
Removing instruction b1_from_b:
Removing instruction b3:
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 jmp b1
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @3
(label) @begin
(label) @end
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = ((byte*))(word/signed word/dword/signed dword) 1024
(void()) b()
(label) b::@1
(label) b::@3
(label) b::@return
(byte) b::i
(byte) b::i#1 reg byte x 16.5
(byte) b::i#2 reg byte x 11.0
(void()) d()
(label) d::@return
(void()) main()
(label) main::@1
(label) main::@return

reg byte x [ b::i#2 b::i#1 ]


FINAL ASSEMBLER
Score: 187

//SEG0 File Comments
// A simple loop results in NullPointerException during loop analysis
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label SCREEN = $400
//SEG3 @begin
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
//SEG5 @3
//SEG6 [2] call main 
//SEG7 [3] phi from @3 to @end [phi:@3->@end]
//SEG8 @end
//SEG9 main
main: {
  //SEG10 [4] *((const byte*) SCREEN#0) ← (byte) '0' -- _deref_pbuc1=vbuc2 
    lda #'0'
    sta SCREEN
  //SEG11 [5] call d 
    jsr d
  //SEG12 [6] phi from main to main::@1 [phi:main->main::@1]
  //SEG13 main::@1
  //SEG14 [7] call b 
  //SEG15 [9] phi from main::@1 to b [phi:main::@1->b]
    jsr b
  //SEG16 main::@return
  //SEG17 [8] return 
    rts
}
//SEG18 b
b: {
  //SEG19 [10] phi from b to b::@1 [phi:b->b::@1]
  //SEG20 [10] phi (byte) b::i#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:b->b::@1#0] -- vbuxx=vbuc1 
    ldx #0
  //SEG21 [10] phi from b::@3 to b::@1 [phi:b::@3->b::@1]
  //SEG22 [10] phi (byte) b::i#2 = (byte) b::i#1 [phi:b::@3->b::@1#0] -- register_copy 
  //SEG23 b::@1
  b1:
  //SEG24 [11] call d 
    jsr d
  //SEG25 b::@3
  //SEG26 [12] (byte) b::i#1 ← ++ (byte) b::i#2 -- vbuxx=_inc_vbuxx 
    inx
  //SEG27 [13] if((byte) b::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 4) goto b::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #4
    bne b1
  //SEG28 b::@return
  //SEG29 [14] return 
    rts
}
//SEG30 d
d: {
  //SEG31 [15] *((const byte*) SCREEN#0) ← ++ *((const byte*) SCREEN#0) -- _deref_pbuc1=_inc__deref_pbuc1 
    inc SCREEN
  //SEG32 d::@return
  //SEG33 [16] return 
    rts
}

