Culled Empty Block (label) @1
Culled Empty Block (label) popup_selector::@4
Culled Empty Block (label) popup_selector::@3
Culled Empty Block (label) popup_selector::@5
Culled Empty Block (label) popup_selector::@6

CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  (byte) opcode#0 ← (byte) 'a'
  to:@2

(void()) main()
main: scope:[main]  from @2
  (byte) opcode#6 ← phi( @2/(byte) opcode#11 )
  *((const byte*) screen + (number) $28) ← (byte) opcode#6
  call popup_selector 
  to:main::@1
main::@1: scope:[main]  from main
  (byte) opcode#7 ← phi( main/(byte) opcode#4 )
  (byte) opcode#1 ← (byte) opcode#7
  *((const byte*) screen + (number) $29) ← (byte) opcode#1
  to:main::@return
main::@return: scope:[main]  from main::@1
  (byte) opcode#8 ← phi( main::@1/(byte) opcode#1 )
  (byte) opcode#2 ← (byte) opcode#8
  return 
  to:@return

(void()) popup_selector()
popup_selector: scope:[popup_selector]  from main
  (byte) opcode#13 ← phi( main/(byte) opcode#6 )
  (byte) popup_selector::k#0 ← (byte) 0
  to:popup_selector::@1
popup_selector::@1: scope:[popup_selector]  from popup_selector popup_selector::@2
  (byte) opcode#12 ← phi( popup_selector/(byte) opcode#13 popup_selector::@2/(byte) opcode#3 )
  (byte) popup_selector::k#2 ← phi( popup_selector/(byte) popup_selector::k#0 popup_selector::@2/(byte) popup_selector::k#1 )
  (bool~) popup_selector::$0 ← (byte) popup_selector::k#2 <= (number) 2
  if((bool~) popup_selector::$0) goto popup_selector::@2
  to:popup_selector::@return
popup_selector::@2: scope:[popup_selector]  from popup_selector::@1
  (byte) popup_selector::k#3 ← phi( popup_selector::@1/(byte) popup_selector::k#2 )
  (byte) opcode#3 ← (byte) 'b'
  *((const byte*) screen + (byte) popup_selector::k#3) ← (byte) opcode#3
  (byte) popup_selector::k#1 ← ++ (byte) popup_selector::k#3
  to:popup_selector::@1
popup_selector::@return: scope:[popup_selector]  from popup_selector::@1
  (byte) opcode#9 ← phi( popup_selector::@1/(byte) opcode#12 )
  (byte) opcode#4 ← (byte) opcode#9
  return 
  to:@return
@2: scope:[]  from @begin
  (byte) opcode#11 ← phi( @begin/(byte) opcode#0 )
  call main 
  to:@3
@3: scope:[]  from @2
  (byte) opcode#10 ← phi( @2/(byte) opcode#2 )
  (byte) opcode#5 ← (byte) opcode#10
  to:@end
@end: scope:[]  from @3

SYMBOL TABLE SSA
(label) @2
(label) @3
(label) @begin
(label) @end
(void()) main()
(label) main::@1
(label) main::@return
(byte) opcode
(byte) opcode#0
(byte) opcode#1
(byte) opcode#10
(byte) opcode#11
(byte) opcode#12
(byte) opcode#13
(byte) opcode#2
(byte) opcode#3
(byte) opcode#4
(byte) opcode#5
(byte) opcode#6
(byte) opcode#7
(byte) opcode#8
(byte) opcode#9
(void()) popup_selector()
(bool~) popup_selector::$0
(label) popup_selector::@1
(label) popup_selector::@2
(label) popup_selector::@return
(byte) popup_selector::k
(byte) popup_selector::k#0
(byte) popup_selector::k#1
(byte) popup_selector::k#2
(byte) popup_selector::k#3
(const byte*) screen = (byte*)(number) $400

Adding number conversion cast (unumber) $28 in *((const byte*) screen + (number) $28) ← (byte) opcode#6
Adding number conversion cast (unumber) $29 in *((const byte*) screen + (number) $29) ← (byte) opcode#1
Adding number conversion cast (unumber) 2 in (bool~) popup_selector::$0 ← (byte) popup_selector::k#2 <= (number) 2
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast $28
Simplifying constant integer cast $29
Simplifying constant integer cast 2
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) $28
Finalized unsigned number type (byte) $29
Finalized unsigned number type (byte) 2
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias (byte) opcode#1 = (byte) opcode#7 (byte) opcode#8 (byte) opcode#2 
Alias (byte) popup_selector::k#2 = (byte) popup_selector::k#3 
Alias (byte) opcode#12 = (byte) opcode#9 (byte) opcode#4 
Alias (byte) opcode#0 = (byte) opcode#11 
Alias (byte) opcode#10 = (byte) opcode#5 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) opcode#6 (byte) opcode#0
Identical Phi Values (byte) opcode#1 (byte) opcode#12
Identical Phi Values (byte) opcode#13 (byte) opcode#6
Identical Phi Values (byte) opcode#10 (byte) opcode#1
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition (bool~) popup_selector::$0 [11] if((byte) popup_selector::k#2<=(byte) 2) goto popup_selector::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte) opcode#0 = 'a'
Constant (const byte) popup_selector::k#0 = 0
Constant (const byte) opcode#3 = 'b'
Successful SSA optimization Pass2ConstantIdentification
Rewriting conditional comparison [11] if((byte) popup_selector::k#2<=(byte) 2) goto popup_selector::@2
Adding number conversion cast (unumber) 2+1 in if((byte) popup_selector::k#2<(byte) 2+(number) 1) goto popup_selector::@2
Adding number conversion cast (unumber) 1 in if((byte) popup_selector::k#2<(unumber)(byte) 2+(number) 1) goto popup_selector::@2
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast (byte) 2+(unumber)(number) 1
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inlining constant with var siblings (const byte) popup_selector::k#0
Inlining constant with var siblings (const byte) opcode#0
Inlining constant with var siblings (const byte) opcode#3
Constant inlined opcode#3 = (byte) 'b'
Constant inlined opcode#0 = (byte) 'a'
Constant inlined popup_selector::k#0 = (byte) 0
Successful SSA optimization Pass2ConstantInlining
Consolidated array index constant in *(screen+$28)
Consolidated array index constant in *(screen+$29)
Successful SSA optimization Pass2ConstantAdditionElimination
Adding NOP phi() at start of @begin
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 popup_selector
CALL GRAPH
Calls in [] to main:2 
Calls in [main] to popup_selector:6 

Created 2 initial phi equivalence classes
Coalesced [15] popup_selector::k#4 ← popup_selector::k#1
Coalesced down to 2 phi equivalence classes
Culled Empty Block (label) @3
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 popup_selector

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] *((const byte*) screen+(byte) $28) ← (byte) 'a'
  [5] call popup_selector 
  to:main::@1
main::@1: scope:[main]  from main
  [6] *((const byte*) screen+(byte) $29) ← (byte) opcode#12
  to:main::@return
main::@return: scope:[main]  from main::@1
  [7] return 
  to:@return

(void()) popup_selector()
popup_selector: scope:[popup_selector]  from main
  [8] phi()
  to:popup_selector::@1
popup_selector::@1: scope:[popup_selector]  from popup_selector popup_selector::@2
  [9] (byte) opcode#12 ← phi( popup_selector/(byte) 'a' popup_selector::@2/(byte) 'b' )
  [9] (byte) popup_selector::k#2 ← phi( popup_selector/(byte) 0 popup_selector::@2/(byte) popup_selector::k#1 )
  [10] if((byte) popup_selector::k#2<(byte) 2+(byte) 1) goto popup_selector::@2
  to:popup_selector::@return
popup_selector::@return: scope:[popup_selector]  from popup_selector::@1
  [11] return 
  to:@return
popup_selector::@2: scope:[popup_selector]  from popup_selector::@1
  [12] *((const byte*) screen + (byte) popup_selector::k#2) ← (byte) 'b'
  [13] (byte) popup_selector::k#1 ← ++ (byte) popup_selector::k#2
  to:popup_selector::@1


VARIABLE REGISTER WEIGHTS
(void()) main()
(byte) opcode
(byte) opcode#12 0.5
(void()) popup_selector()
(byte) popup_selector::k
(byte) popup_selector::k#1 22.0
(byte) popup_selector::k#2 14.666666666666666

Initial phi equivalence classes
[ popup_selector::k#2 popup_selector::k#1 ]
[ opcode#12 ]
Complete equivalence classes
[ popup_selector::k#2 popup_selector::k#1 ]
[ opcode#12 ]
Allocated zp[1]:2 [ popup_selector::k#2 popup_selector::k#1 ]
Allocated zp[1]:3 [ opcode#12 ]

INITIAL ASM
Target platform is c64basic / MOS6502X
  // File Comments
// Demonstrates a problem where constant loophead unrolling results in an error
// The result is a NullPointerException
// The cause is that the Unroller does not handle the variable opcode correctly.
// The Unroller gets the verwions for opcode wrong because it misses the fact that it is modified inside call to popup_selector()
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label screen = $400
  .label opcode = 3
  // @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
// Offending unroll variable
main: {
    // [4] *((const byte*) screen+(byte) $28) ← (byte) 'a' -- _deref_pbuc1=vbuc2 
    lda #'a'
    sta screen+$28
    // [5] call popup_selector 
    // [8] phi from main to popup_selector [phi:main->popup_selector]
  popup_selector_from_main:
    jsr popup_selector
    jmp __b1
    // main::@1
  __b1:
    // [6] *((const byte*) screen+(byte) $29) ← (byte) opcode#12 -- _deref_pbuc1=vbuz1 
    lda.z opcode
    sta screen+$29
    jmp __breturn
    // main::@return
  __breturn:
    // [7] return 
    rts
}
  // popup_selector
popup_selector: {
    .label k = 2
    // [9] phi from popup_selector to popup_selector::@1 [phi:popup_selector->popup_selector::@1]
  __b1_from_popup_selector:
    // [9] phi (byte) opcode#12 = (byte) 'a' [phi:popup_selector->popup_selector::@1#0] -- vbuz1=vbuc1 
    lda #'a'
    sta.z opcode
    // [9] phi (byte) popup_selector::k#2 = (byte) 0 [phi:popup_selector->popup_selector::@1#1] -- vbuz1=vbuc1 
    lda #0
    sta.z k
    jmp __b1
    // popup_selector::@1
  __b1:
    // [10] if((byte) popup_selector::k#2<(byte) 2+(byte) 1) goto popup_selector::@2 -- vbuz1_lt_vbuc1_then_la1 
    lda.z k
    cmp #2+1
    bcc __b2
    jmp __breturn
    // popup_selector::@return
  __breturn:
    // [11] return 
    rts
    // popup_selector::@2
  __b2:
    // [12] *((const byte*) screen + (byte) popup_selector::k#2) ← (byte) 'b' -- pbuc1_derefidx_vbuz1=vbuc2 
    lda #'b'
    ldy.z k
    sta screen,y
    // [13] (byte) popup_selector::k#1 ← ++ (byte) popup_selector::k#2 -- vbuz1=_inc_vbuz1 
    inc.z k
    // [9] phi from popup_selector::@2 to popup_selector::@1 [phi:popup_selector::@2->popup_selector::@1]
  __b1_from___b2:
    // [9] phi (byte) opcode#12 = (byte) 'b' [phi:popup_selector::@2->popup_selector::@1#0] -- vbuz1=vbuc1 
    lda #'b'
    sta.z opcode
    // [9] phi (byte) popup_selector::k#2 = (byte) popup_selector::k#1 [phi:popup_selector::@2->popup_selector::@1#1] -- register_copy 
    jmp __b1
}
  // File Data

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const byte*) screen+(byte) $28) ← (byte) 'a' [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [12] *((const byte*) screen + (byte) popup_selector::k#2) ← (byte) 'b' [ popup_selector::k#2 ] ( main:2::popup_selector:5 [ popup_selector::k#2 ] ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:2 [ popup_selector::k#2 popup_selector::k#1 ]
Statement [4] *((const byte*) screen+(byte) $28) ← (byte) 'a' [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [12] *((const byte*) screen + (byte) popup_selector::k#2) ← (byte) 'b' [ popup_selector::k#2 ] ( main:2::popup_selector:5 [ popup_selector::k#2 ] ) always clobbers reg byte a 
Potential registers zp[1]:2 [ popup_selector::k#2 popup_selector::k#1 ] : zp[1]:2 , reg byte x , reg byte y , 
Potential registers zp[1]:3 [ opcode#12 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [popup_selector] 36.67: zp[1]:2 [ popup_selector::k#2 popup_selector::k#1 ] 
Uplift Scope [] 0.5: zp[1]:3 [ opcode#12 ] 
Uplift Scope [main] 

Uplifting [popup_selector] best 394 combination reg byte x [ popup_selector::k#2 popup_selector::k#1 ] 
Uplifting [] best 331 combination reg byte a [ opcode#12 ] 
Uplifting [main] best 331 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Demonstrates a problem where constant loophead unrolling results in an error
// The result is a NullPointerException
// The cause is that the Unroller does not handle the variable opcode correctly.
// The Unroller gets the verwions for opcode wrong because it misses the fact that it is modified inside call to popup_selector()
  // 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
// Offending unroll variable
main: {
    // [4] *((const byte*) screen+(byte) $28) ← (byte) 'a' -- _deref_pbuc1=vbuc2 
    lda #'a'
    sta screen+$28
    // [5] call popup_selector 
    // [8] phi from main to popup_selector [phi:main->popup_selector]
  popup_selector_from_main:
    jsr popup_selector
    jmp __b1
    // main::@1
  __b1:
    // [6] *((const byte*) screen+(byte) $29) ← (byte) opcode#12 -- _deref_pbuc1=vbuaa 
    sta screen+$29
    jmp __breturn
    // main::@return
  __breturn:
    // [7] return 
    rts
}
  // popup_selector
popup_selector: {
    // [9] phi from popup_selector to popup_selector::@1 [phi:popup_selector->popup_selector::@1]
  __b1_from_popup_selector:
    // [9] phi (byte) opcode#12 = (byte) 'a' [phi:popup_selector->popup_selector::@1#0] -- vbuaa=vbuc1 
    lda #'a'
    // [9] phi (byte) popup_selector::k#2 = (byte) 0 [phi:popup_selector->popup_selector::@1#1] -- vbuxx=vbuc1 
    ldx #0
    jmp __b1
    // popup_selector::@1
  __b1:
    // [10] if((byte) popup_selector::k#2<(byte) 2+(byte) 1) goto popup_selector::@2 -- vbuxx_lt_vbuc1_then_la1 
    cpx #2+1
    bcc __b2
    jmp __breturn
    // popup_selector::@return
  __breturn:
    // [11] return 
    rts
    // popup_selector::@2
  __b2:
    // [12] *((const byte*) screen + (byte) popup_selector::k#2) ← (byte) 'b' -- pbuc1_derefidx_vbuxx=vbuc2 
    lda #'b'
    sta screen,x
    // [13] (byte) popup_selector::k#1 ← ++ (byte) popup_selector::k#2 -- vbuxx=_inc_vbuxx 
    inx
    // [9] phi from popup_selector::@2 to popup_selector::@1 [phi:popup_selector::@2->popup_selector::@1]
  __b1_from___b2:
    // [9] phi (byte) opcode#12 = (byte) 'b' [phi:popup_selector::@2->popup_selector::@1#0] -- vbuaa=vbuc1 
    lda #'b'
    // [9] phi (byte) popup_selector::k#2 = (byte) popup_selector::k#1 [phi:popup_selector::@2->popup_selector::@1#1] -- register_copy 
    jmp __b1
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
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 popup_selector_from_main:
Removing instruction __b1:
Removing instruction __breturn:
Removing instruction __b1_from_popup_selector:
Removing instruction __breturn:
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction lda #'b'
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction __b1:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(void()) main()
(label) main::@1
(label) main::@return
(byte) opcode
(byte) opcode#12 reg byte a 0.5
(void()) popup_selector()
(label) popup_selector::@1
(label) popup_selector::@2
(label) popup_selector::@return
(byte) popup_selector::k
(byte) popup_selector::k#1 reg byte x 22.0
(byte) popup_selector::k#2 reg byte x 14.666666666666666
(const byte*) screen = (byte*) 1024

reg byte x [ popup_selector::k#2 popup_selector::k#1 ]
reg byte a [ opcode#12 ]


FINAL ASSEMBLER
Score: 233

  // File Comments
// Demonstrates a problem where constant loophead unrolling results in an error
// The result is a NullPointerException
// The cause is that the Unroller does not handle the variable opcode correctly.
// The Unroller gets the verwions for opcode wrong because it misses the fact that it is modified inside call to popup_selector()
  // 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
// Offending unroll variable
main: {
    // screen[40] = opcode
    // [4] *((const byte*) screen+(byte) $28) ← (byte) 'a' -- _deref_pbuc1=vbuc2 
    lda #'a'
    sta screen+$28
    // popup_selector()
    // [5] call popup_selector 
    // [8] phi from main to popup_selector [phi:main->popup_selector]
    jsr popup_selector
    // main::@1
    // screen[41] = opcode
    // [6] *((const byte*) screen+(byte) $29) ← (byte) opcode#12 -- _deref_pbuc1=vbuaa 
    sta screen+$29
    // main::@return
    // }
    // [7] return 
    rts
}
  // popup_selector
popup_selector: {
    // [9] phi from popup_selector to popup_selector::@1 [phi:popup_selector->popup_selector::@1]
    // [9] phi (byte) opcode#12 = (byte) 'a' [phi:popup_selector->popup_selector::@1#0] -- vbuaa=vbuc1 
    lda #'a'
    // [9] phi (byte) popup_selector::k#2 = (byte) 0 [phi:popup_selector->popup_selector::@1#1] -- vbuxx=vbuc1 
    ldx #0
    // popup_selector::@1
  __b1:
    // for (byte k = 0; k <= 2; k++)
    // [10] if((byte) popup_selector::k#2<(byte) 2+(byte) 1) goto popup_selector::@2 -- vbuxx_lt_vbuc1_then_la1 
    cpx #2+1
    bcc __b2
    // popup_selector::@return
    // }
    // [11] return 
    rts
    // popup_selector::@2
  __b2:
    // screen[k] = opcode
    // [12] *((const byte*) screen + (byte) popup_selector::k#2) ← (byte) 'b' -- pbuc1_derefidx_vbuxx=vbuc2 
    lda #'b'
    sta screen,x
    // for (byte k = 0; k <= 2; k++)
    // [13] (byte) popup_selector::k#1 ← ++ (byte) popup_selector::k#2 -- vbuxx=_inc_vbuxx 
    inx
    // [9] phi from popup_selector::@2 to popup_selector::@1 [phi:popup_selector::@2->popup_selector::@1]
    // [9] phi (byte) opcode#12 = (byte) 'b' [phi:popup_selector::@2->popup_selector::@1#0] -- vbuaa=vbuc1 
    // [9] phi (byte) popup_selector::k#2 = (byte) popup_selector::k#1 [phi:popup_selector::@2->popup_selector::@1#1] -- register_copy 
    jmp __b1
}
  // File Data

