
CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  (word) w1#0 ← (byte/signed byte/word/signed word/dword/signed dword) 0
  (word) w2#0 ← (byte/signed byte/word/signed word/dword/signed dword) 0
  to:@3
main: scope:[main]  from @3
  (word) w2#16 ← phi( @3/(word) w2#15 )
  (word) w1#13 ← phi( @3/(word) w1#16 )
  call incw1 
  to:main::@1
main::@1: scope:[main]  from main
  (word) w2#13 ← phi( main/(word) w2#16 )
  (word) w1#7 ← phi( main/(word) w1#5 )
  (word) w1#1 ← (word) w1#7
  call incw2 
  to:main::@2
main::@2: scope:[main]  from main::@1
  (word) w1#14 ← phi( main::@1/(word) w1#1 )
  (word) w2#7 ← phi( main::@1/(word) w2#5 )
  (word) w2#1 ← (word) w2#7
  call incw1 
  to:main::@3
main::@3: scope:[main]  from main::@2
  (word) w2#14 ← phi( main::@2/(word) w2#1 )
  (word) w1#8 ← phi( main::@2/(word) w1#5 )
  (word) w1#2 ← (word) w1#8
  call incw2 
  to:main::@4
main::@4: scope:[main]  from main::@3
  (word) w1#15 ← phi( main::@3/(word) w1#2 )
  (word) w2#8 ← phi( main::@3/(word) w2#5 )
  (word) w2#2 ← (word) w2#8
  to:main::@return
main::@return: scope:[main]  from main::@4
  (word) w2#9 ← phi( main::@4/(word) w2#2 )
  (word) w1#9 ← phi( main::@4/(word) w1#15 )
  (word) w1#3 ← (word) w1#9
  (word) w2#3 ← (word) w2#9
  return 
  to:@return
incw1: scope:[incw1]  from main main::@2
  (word) w1#10 ← phi( main/(word) w1#13 main::@2/(word) w1#14 )
  (word) w1#4 ← ++ (word) w1#10
  to:incw1::@return
incw1::@return: scope:[incw1]  from incw1
  (word) w1#11 ← phi( incw1/(word) w1#4 )
  (word) w1#5 ← (word) w1#11
  return 
  to:@return
incw2: scope:[incw2]  from main::@1 main::@3
  (word) w2#10 ← phi( main::@1/(word) w2#13 main::@3/(word) w2#14 )
  (word) w2#4 ← ++ (word) w2#10
  to:incw2::@return
incw2::@return: scope:[incw2]  from incw2
  (word) w2#11 ← phi( incw2/(word) w2#4 )
  (word) w2#5 ← (word) w2#11
  return 
  to:@return
@3: scope:[]  from @begin
  (word) w2#15 ← phi( @begin/(word) w2#0 )
  (word) w1#16 ← phi( @begin/(word) w1#0 )
  call main 
  to:@4
@4: scope:[]  from @3
  (word) w2#12 ← phi( @3/(word) w2#3 )
  (word) w1#12 ← phi( @3/(word) w1#3 )
  (word) w1#6 ← (word) w1#12
  (word) w2#6 ← (word) w2#12
  to:@end
@end: scope:[]  from @4

SYMBOL TABLE SSA
(label) @3
(label) @4
(label) @begin
(label) @end
(void()) incw1()
(label) incw1::@return
(void()) incw2()
(label) incw2::@return
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@return
(word) w1
(word) w1#0
(word) w1#1
(word) w1#10
(word) w1#11
(word) w1#12
(word) w1#13
(word) w1#14
(word) w1#15
(word) w1#16
(word) w1#2
(word) w1#3
(word) w1#4
(word) w1#5
(word) w1#6
(word) w1#7
(word) w1#8
(word) w1#9
(word) w2
(word) w2#0
(word) w2#1
(word) w2#10
(word) w2#11
(word) w2#12
(word) w2#13
(word) w2#14
(word) w2#15
(word) w2#16
(word) w2#2
(word) w2#3
(word) w2#4
(word) w2#5
(word) w2#6
(word) w2#7
(word) w2#8
(word) w2#9

Alias (word) w2#13 = (word) w2#16 
Alias (word) w1#1 = (word) w1#7 (word) w1#14 
Alias (word) w2#1 = (word) w2#7 (word) w2#14 
Alias (word) w1#15 = (word) w1#2 (word) w1#8 (word) w1#9 (word) w1#3 
Alias (word) w2#2 = (word) w2#8 (word) w2#9 (word) w2#3 
Alias (word) w1#11 = (word) w1#4 (word) w1#5 
Alias (word) w2#11 = (word) w2#4 (word) w2#5 
Alias (word) w1#0 = (word) w1#16 
Alias (word) w2#0 = (word) w2#15 
Alias (word) w1#12 = (word) w1#6 
Alias (word) w2#12 = (word) w2#6 
Successful SSA optimization Pass2AliasElimination
Redundant Phi (word) w1#13 (word) w1#0
Redundant Phi (word) w2#13 (word) w2#0
Redundant Phi (word) w1#1 (word) w1#11
Redundant Phi (word) w2#1 (word) w2#11
Redundant Phi (word) w1#15 (word) w1#11
Redundant Phi (word) w2#2 (word) w2#11
Redundant Phi (word) w1#12 (word) w1#15
Redundant Phi (word) w2#12 (word) w2#2
Successful SSA optimization Pass2RedundantPhiElimination
Constant (const word) w1#0 = 0
Constant (const word) w2#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Culled Empty Block (label) main::@4
Culled Empty Block (label) @4
Successful SSA optimization Pass2CullEmptyBlocks
Inlining constant with var siblings (const word) w1#0
Inlining constant with var siblings (const word) w2#0
Constant inlined w1#0 = (byte/signed byte/word/signed word/dword/signed dword) 0
Constant inlined w2#0 = (byte/signed byte/word/signed word/dword/signed dword) 0
Successful SSA optimization Pass2ConstantInlining
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
Adding NOP phi() at start of main::@1
CALL GRAPH
Calls in [] to main:2 
Calls in [main] to incw1:5 incw2:7 incw1:9 incw2:11 

Created 2 initial phi equivalence classes
Coalesced [8] w1#17 ← w1#11
Coalesced [10] w2#17 ← w2#11
Coalesced down to 2 phi equivalence classes
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
Adding NOP phi() at start of main::@1
Adding NOP phi() at start of main::@2
Adding NOP phi() at start of main::@3

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] phi()
  [5] call incw1 
  to:main::@1
main::@1: scope:[main]  from main
  [6] phi()
  [7] call incw2 
  to:main::@2
main::@2: scope:[main]  from main::@1
  [8] phi()
  [9] call incw1 
  to:main::@3
main::@3: scope:[main]  from main::@2
  [10] phi()
  [11] call incw2 
  to:main::@return
main::@return: scope:[main]  from main::@3
  [12] return 
  to:@return
incw2: scope:[incw2]  from main::@1 main::@3
  [13] (word) w2#10 ← phi( main::@1/(byte/signed byte/word/signed word/dword/signed dword) 0 main::@3/(word) w2#11 )
  [14] (word) w2#11 ← ++ (word) w2#10
  to:incw2::@return
incw2::@return: scope:[incw2]  from incw2
  [15] return 
  to:@return
incw1: scope:[incw1]  from main main::@2
  [16] (word) w1#10 ← phi( main/(byte/signed byte/word/signed word/dword/signed dword) 0 main::@2/(word) w1#11 )
  [17] (word) w1#11 ← ++ (word) w1#10
  to:incw1::@return
incw1::@return: scope:[incw1]  from incw1
  [18] return 
  to:@return


VARIABLE REGISTER WEIGHTS
(void()) incw1()
(void()) incw2()
(void()) main()
(word) w1
(word) w1#10 4.0
(word) w1#11 0.6666666666666666
(word) w2
(word) w2#10 4.0
(word) w2#11 0.6666666666666666

Initial phi equivalence classes
[ w2#10 w2#11 ]
[ w1#10 w1#11 ]
Complete equivalence classes
[ w2#10 w2#11 ]
[ w1#10 w1#11 ]
Allocated zp ZP_WORD:2 [ w2#10 w2#11 ]
Allocated zp ZP_WORD:4 [ w1#10 w1#11 ]

INITIAL ASM
//SEG0 File Comments
// Live ranges were not functioning properly, when multiple method calls were chained - each modifying different vars.
// w1 and w2 ended up having the same zero-page register as their live range was not propagated properly
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label w1 = 4
  .label w2 = 2
//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 
//SEG7 [4] phi from @3 to main [phi:@3->main]
main_from_b3:
  jsr main
//SEG8 [3] phi from @3 to @end [phi:@3->@end]
bend_from_b3:
  jmp bend
//SEG9 @end
bend:
//SEG10 main
main: {
  //SEG11 [5] call incw1 
  //SEG12 [16] phi from main to incw1 [phi:main->incw1]
  incw1_from_main:
  //SEG13 [16] phi (word) w1#10 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main->incw1#0] -- vwuz1=vbuc1 
    lda #<0
    sta w1
    lda #>0
    sta w1+1
    jsr incw1
  //SEG14 [6] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
    jmp b1
  //SEG15 main::@1
  b1:
  //SEG16 [7] call incw2 
  //SEG17 [13] phi from main::@1 to incw2 [phi:main::@1->incw2]
  incw2_from_b1:
  //SEG18 [13] phi (word) w2#10 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main::@1->incw2#0] -- vwuz1=vbuc1 
    lda #<0
    sta w2
    lda #>0
    sta w2+1
    jsr incw2
  //SEG19 [8] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
  b2_from_b1:
    jmp b2
  //SEG20 main::@2
  b2:
  //SEG21 [9] call incw1 
  //SEG22 [16] phi from main::@2 to incw1 [phi:main::@2->incw1]
  incw1_from_b2:
  //SEG23 [16] phi (word) w1#10 = (word) w1#11 [phi:main::@2->incw1#0] -- register_copy 
    jsr incw1
  //SEG24 [10] phi from main::@2 to main::@3 [phi:main::@2->main::@3]
  b3_from_b2:
    jmp b3
  //SEG25 main::@3
  b3:
  //SEG26 [11] call incw2 
  //SEG27 [13] phi from main::@3 to incw2 [phi:main::@3->incw2]
  incw2_from_b3:
  //SEG28 [13] phi (word) w2#10 = (word) w2#11 [phi:main::@3->incw2#0] -- register_copy 
    jsr incw2
    jmp breturn
  //SEG29 main::@return
  breturn:
  //SEG30 [12] return 
    rts
}
//SEG31 incw2
incw2: {
  //SEG32 [14] (word) w2#11 ← ++ (word) w2#10 -- vwuz1=_inc_vwuz1 
    inc w2
    bne !+
    inc w2+1
  !:
    jmp breturn
  //SEG33 incw2::@return
  breturn:
  //SEG34 [15] return 
    rts
}
//SEG35 incw1
incw1: {
  //SEG36 [17] (word) w1#11 ← ++ (word) w1#10 -- vwuz1=_inc_vwuz1 
    inc w1
    bne !+
    inc w1+1
  !:
    jmp breturn
  //SEG37 incw1::@return
  breturn:
  //SEG38 [18] return 
    rts
}

REGISTER UPLIFT POTENTIAL REGISTERS
Potential registers zp ZP_WORD:2 [ w2#10 w2#11 ] : zp ZP_WORD:2 , 
Potential registers zp ZP_WORD:4 [ w1#10 w1#11 ] : zp ZP_WORD:4 , 

REGISTER UPLIFT SCOPES
Uplift Scope [] 4.67: zp ZP_WORD:2 [ w2#10 w2#11 ] 4.67: zp ZP_WORD:4 [ w1#10 w1#11 ] 
Uplift Scope [main] 
Uplift Scope [incw1] 
Uplift Scope [incw2] 

Uplifting [] best 116 combination zp ZP_WORD:2 [ w2#10 w2#11 ] zp ZP_WORD:4 [ w1#10 w1#11 ] 
Uplifting [main] best 116 combination 
Uplifting [incw1] best 116 combination 
Uplifting [incw2] best 116 combination 

ASSEMBLER BEFORE OPTIMIZATION
//SEG0 File Comments
// Live ranges were not functioning properly, when multiple method calls were chained - each modifying different vars.
// w1 and w2 ended up having the same zero-page register as their live range was not propagated properly
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label w1 = 4
  .label w2 = 2
//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 
//SEG7 [4] phi from @3 to main [phi:@3->main]
main_from_b3:
  jsr main
//SEG8 [3] phi from @3 to @end [phi:@3->@end]
bend_from_b3:
  jmp bend
//SEG9 @end
bend:
//SEG10 main
main: {
  //SEG11 [5] call incw1 
  //SEG12 [16] phi from main to incw1 [phi:main->incw1]
  incw1_from_main:
  //SEG13 [16] phi (word) w1#10 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main->incw1#0] -- vwuz1=vbuc1 
    lda #<0
    sta w1
    lda #>0
    sta w1+1
    jsr incw1
  //SEG14 [6] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
    jmp b1
  //SEG15 main::@1
  b1:
  //SEG16 [7] call incw2 
  //SEG17 [13] phi from main::@1 to incw2 [phi:main::@1->incw2]
  incw2_from_b1:
  //SEG18 [13] phi (word) w2#10 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main::@1->incw2#0] -- vwuz1=vbuc1 
    lda #<0
    sta w2
    lda #>0
    sta w2+1
    jsr incw2
  //SEG19 [8] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
  b2_from_b1:
    jmp b2
  //SEG20 main::@2
  b2:
  //SEG21 [9] call incw1 
  //SEG22 [16] phi from main::@2 to incw1 [phi:main::@2->incw1]
  incw1_from_b2:
  //SEG23 [16] phi (word) w1#10 = (word) w1#11 [phi:main::@2->incw1#0] -- register_copy 
    jsr incw1
  //SEG24 [10] phi from main::@2 to main::@3 [phi:main::@2->main::@3]
  b3_from_b2:
    jmp b3
  //SEG25 main::@3
  b3:
  //SEG26 [11] call incw2 
  //SEG27 [13] phi from main::@3 to incw2 [phi:main::@3->incw2]
  incw2_from_b3:
  //SEG28 [13] phi (word) w2#10 = (word) w2#11 [phi:main::@3->incw2#0] -- register_copy 
    jsr incw2
    jmp breturn
  //SEG29 main::@return
  breturn:
  //SEG30 [12] return 
    rts
}
//SEG31 incw2
incw2: {
  //SEG32 [14] (word) w2#11 ← ++ (word) w2#10 -- vwuz1=_inc_vwuz1 
    inc w2
    bne !+
    inc w2+1
  !:
    jmp breturn
  //SEG33 incw2::@return
  breturn:
  //SEG34 [15] return 
    rts
}
//SEG35 incw1
incw1: {
  //SEG36 [17] (word) w1#11 ← ++ (word) w1#10 -- vwuz1=_inc_vwuz1 
    inc w1
    bne !+
    inc w1+1
  !:
    jmp breturn
  //SEG37 incw1::@return
  breturn:
  //SEG38 [18] return 
    rts
}

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b3
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp b2
Removing instruction jmp b3
Removing instruction jmp breturn
Removing instruction jmp breturn
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #>0
Removing instruction lda #>0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction b3_from_bbegin:
Removing instruction b3:
Removing instruction main_from_b3:
Removing instruction bend_from_b3:
Removing instruction b1_from_main:
Removing instruction incw2_from_b1:
Removing instruction b2_from_b1:
Removing instruction incw1_from_b2:
Removing instruction b3_from_b2:
Removing instruction incw2_from_b3:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction incw1_from_main:
Removing instruction b1:
Removing instruction b2:
Removing instruction b3:
Removing instruction breturn:
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) @3
(label) @begin
(label) @end
(void()) incw1()
(label) incw1::@return
(void()) incw2()
(label) incw2::@return
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@return
(word) w1
(word) w1#10 w1 zp ZP_WORD:4 4.0
(word) w1#11 w1 zp ZP_WORD:4 0.6666666666666666
(word) w2
(word) w2#10 w2 zp ZP_WORD:2 4.0
(word) w2#11 w2 zp ZP_WORD:2 0.6666666666666666

zp ZP_WORD:2 [ w2#10 w2#11 ]
zp ZP_WORD:4 [ w1#10 w1#11 ]


FINAL ASSEMBLER
Score: 82

//SEG0 File Comments
// Live ranges were not functioning properly, when multiple method calls were chained - each modifying different vars.
// w1 and w2 ended up having the same zero-page register as their live range was not propagated properly
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label w1 = 4
  .label w2 = 2
//SEG3 @begin
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
//SEG5 @3
//SEG6 [2] call main 
//SEG7 [4] phi from @3 to main [phi:@3->main]
//SEG8 [3] phi from @3 to @end [phi:@3->@end]
//SEG9 @end
//SEG10 main
main: {
  //SEG11 [5] call incw1 
  //SEG12 [16] phi from main to incw1 [phi:main->incw1]
  //SEG13 [16] phi (word) w1#10 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main->incw1#0] -- vwuz1=vbuc1 
    lda #<0
    sta w1
    sta w1+1
    jsr incw1
  //SEG14 [6] phi from main to main::@1 [phi:main->main::@1]
  //SEG15 main::@1
  //SEG16 [7] call incw2 
  //SEG17 [13] phi from main::@1 to incw2 [phi:main::@1->incw2]
  //SEG18 [13] phi (word) w2#10 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main::@1->incw2#0] -- vwuz1=vbuc1 
    lda #<0
    sta w2
    sta w2+1
    jsr incw2
  //SEG19 [8] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
  //SEG20 main::@2
  //SEG21 [9] call incw1 
  //SEG22 [16] phi from main::@2 to incw1 [phi:main::@2->incw1]
  //SEG23 [16] phi (word) w1#10 = (word) w1#11 [phi:main::@2->incw1#0] -- register_copy 
    jsr incw1
  //SEG24 [10] phi from main::@2 to main::@3 [phi:main::@2->main::@3]
  //SEG25 main::@3
  //SEG26 [11] call incw2 
  //SEG27 [13] phi from main::@3 to incw2 [phi:main::@3->incw2]
  //SEG28 [13] phi (word) w2#10 = (word) w2#11 [phi:main::@3->incw2#0] -- register_copy 
    jsr incw2
  //SEG29 main::@return
  //SEG30 [12] return 
    rts
}
//SEG31 incw2
incw2: {
  //SEG32 [14] (word) w2#11 ← ++ (word) w2#10 -- vwuz1=_inc_vwuz1 
    inc w2
    bne !+
    inc w2+1
  !:
  //SEG33 incw2::@return
  //SEG34 [15] return 
    rts
}
//SEG35 incw1
incw1: {
  //SEG36 [17] (word) w1#11 ← ++ (word) w1#10 -- vwuz1=_inc_vwuz1 
    inc w1
    bne !+
    inc w1+1
  !:
  //SEG37 incw1::@return
  //SEG38 [18] return 
    rts
}

