Inlined call call __init
Eliminating unused variable with no statement __start::__init1_$0
Eliminating unused variable with no statement __start::__init1_$1

CONTROL FLOW GRAPH SSA

char * malloc()
malloc: scope:[malloc]  from __start::@2 __start::__init1
  MEM#6 = phi( __start::@2/MEM#3, __start::__init1/MEM#2 )
  MEM#0 = ++ MEM#6
  malloc::return#0 = MEM#0
  to:malloc::@return
malloc::@return: scope:[malloc]  from malloc
  MEM#7 = phi( malloc/MEM#0 )
  malloc::return#4 = phi( malloc/malloc::return#0 )
  malloc::return#1 = malloc::return#4
  MEM#1 = MEM#7
  return 
  to:@return

void main()
main: scope:[main]  from __start::@1
  SCREEN_2#2 = phi( __start::@1/SCREEN_2#4 )
  SCREEN_1#2 = phi( __start::@1/SCREEN_1#4 )
  *SCREEN_1#2 = 0
  *SCREEN_2#2 = 0
  to:main::@return
main::@return: scope:[main]  from main
  return 
  to:@return

void __start()
__start: scope:[__start]  from
  to:__start::__init1
__start::__init1: scope:[__start]  from __start
  MEM#2 = (char *)$400
  call malloc
  malloc::return#2 = malloc::return#1
  to:__start::@2
__start::@2: scope:[__start]  from __start::__init1
  MEM#8 = phi( __start::__init1/MEM#1 )
  malloc::return#5 = phi( __start::__init1/malloc::return#2 )
  SCREEN_1#0 = malloc::return#5
  MEM#3 = MEM#8
  call malloc
  malloc::return#3 = malloc::return#1
  to:__start::@3
__start::@3: scope:[__start]  from __start::@2
  SCREEN_1#6 = phi( __start::@2/SCREEN_1#0 )
  MEM#9 = phi( __start::@2/MEM#1 )
  malloc::return#6 = phi( __start::@2/malloc::return#3 )
  SCREEN_2#0 = malloc::return#6
  MEM#4 = MEM#9
  to:__start::@1
__start::@1: scope:[__start]  from __start::@3
  MEM#12 = phi( __start::@3/MEM#4 )
  SCREEN_2#4 = phi( __start::@3/SCREEN_2#0 )
  SCREEN_1#4 = phi( __start::@3/SCREEN_1#6 )
  call main
  to:__start::@4
__start::@4: scope:[__start]  from __start::@1
  SCREEN_2#5 = phi( __start::@1/SCREEN_2#4 )
  SCREEN_1#5 = phi( __start::@1/SCREEN_1#4 )
  MEM#11 = phi( __start::@1/MEM#12 )
  to:__start::@return
__start::@return: scope:[__start]  from __start::@4
  SCREEN_2#3 = phi( __start::@4/SCREEN_2#5 )
  SCREEN_1#3 = phi( __start::@4/SCREEN_1#5 )
  MEM#10 = phi( __start::@4/MEM#11 )
  MEM#5 = MEM#10
  SCREEN_1#1 = SCREEN_1#3
  SCREEN_2#1 = SCREEN_2#3
  return 
  to:@return

SYMBOL TABLE SSA
char *MEM
char *MEM#0
char *MEM#1
char *MEM#10
char *MEM#11
char *MEM#12
char *MEM#2
char *MEM#3
char *MEM#4
char *MEM#5
char *MEM#6
char *MEM#7
char *MEM#8
char *MEM#9
char *SCREEN_1
char *SCREEN_1#0
char *SCREEN_1#1
char *SCREEN_1#2
char *SCREEN_1#3
char *SCREEN_1#4
char *SCREEN_1#5
char *SCREEN_1#6
char *SCREEN_2
char *SCREEN_2#0
char *SCREEN_2#1
char *SCREEN_2#2
char *SCREEN_2#3
char *SCREEN_2#4
char *SCREEN_2#5
void __start()
void main()
char * malloc()
char *malloc::return
char *malloc::return#0
char *malloc::return#1
char *malloc::return#2
char *malloc::return#3
char *malloc::return#4
char *malloc::return#5
char *malloc::return#6

Adding number conversion cast (unumber) 0 in *SCREEN_1#2 = 0
Adding number conversion cast (unumber) 0 in *SCREEN_2#2 = 0
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast *SCREEN_1#2 = (unumber)0
Inlining cast *SCREEN_2#2 = (unumber)0
Successful SSA optimization Pass2InlineCast
Simplifying constant integer cast 0
Simplifying constant integer cast 0
Simplifying constant pointer cast (char *) 1024
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 0
Finalized unsigned number type (char) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias malloc::return#0 = malloc::return#4 malloc::return#1 
Alias MEM#0 = MEM#7 MEM#1 
Alias malloc::return#2 = malloc::return#5 
Alias MEM#3 = MEM#8 
Alias malloc::return#3 = malloc::return#6 
Alias SCREEN_1#0 = SCREEN_1#6 SCREEN_1#4 SCREEN_1#5 SCREEN_1#3 SCREEN_1#1 
Alias MEM#10 = MEM#4 MEM#9 MEM#12 MEM#11 MEM#5 
Alias SCREEN_2#0 = SCREEN_2#4 SCREEN_2#5 SCREEN_2#3 SCREEN_2#1 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values SCREEN_1#2 SCREEN_1#0
Identical Phi Values SCREEN_2#2 SCREEN_2#0
Identical Phi Values MEM#3 MEM#0
Identical Phi Values MEM#10 MEM#0
Successful SSA optimization Pass2IdenticalPhiElimination
Constant MEM#2 = (char *) 1024
Successful SSA optimization Pass2ConstantIdentification
Inlining constant with var siblings MEM#2
Constant inlined MEM#2 = (char *) 1024
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of __start
Adding NOP phi() at start of __start::__init1
Adding NOP phi() at start of __start::@1
Adding NOP phi() at start of __start::@4
CALL GRAPH
Calls in [__start] to malloc:2 malloc:6 main:10 

Created 1 initial phi equivalence classes
Coalesced [5] MEM#13 = MEM#0
Coalesced down to 1 phi equivalence classes
Culled Empty Block label __start::@4
Adding NOP phi() at start of __start
Adding NOP phi() at start of __start::__init1
Adding NOP phi() at start of __start::@1

FINAL CONTROL FLOW GRAPH

void __start()
__start: scope:[__start]  from
  [0] phi()
  to:__start::__init1
__start::__init1: scope:[__start]  from __start
  [1] phi()
  [2] call malloc
  [3] malloc::return#2 = malloc::return#0
  to:__start::@2
__start::@2: scope:[__start]  from __start::__init1
  [4] SCREEN_1#0 = malloc::return#2
  [5] call malloc
  [6] malloc::return#3 = malloc::return#0
  to:__start::@3
__start::@3: scope:[__start]  from __start::@2
  [7] SCREEN_2#0 = malloc::return#3
  to:__start::@1
__start::@1: scope:[__start]  from __start::@3
  [8] phi()
  [9] call main
  to:__start::@return
__start::@return: scope:[__start]  from __start::@1
  [10] return 
  to:@return

char * malloc()
malloc: scope:[malloc]  from __start::@2 __start::__init1
  [11] MEM#6 = phi( __start::@2/MEM#0, __start::__init1/(char *) 1024 )
  [12] MEM#0 = ++ MEM#6
  [13] malloc::return#0 = MEM#0
  to:malloc::@return
malloc::@return: scope:[malloc]  from malloc
  [14] return 
  to:@return

void main()
main: scope:[main]  from __start::@1
  [15] *SCREEN_1#0 = 0
  [16] *SCREEN_2#0 = 0
  to:main::@return
main::@return: scope:[main]  from main
  [17] return 
  to:@return


VARIABLE REGISTER WEIGHTS
char *MEM
char *MEM#0 // 4.0
char *MEM#6 // 13.0
char *SCREEN_1
char *SCREEN_1#0 // 2.6
char *SCREEN_2
char *SCREEN_2#0 // 4.333333333333333
void __start()
void main()
char * malloc()
char *malloc::return
char *malloc::return#0 // 3.75
char *malloc::return#2 // 4.0
char *malloc::return#3 // 4.0

Initial phi equivalence classes
[ MEM#6 MEM#0 ]
Added variable malloc::return#2 to live range equivalence class [ malloc::return#2 ]
Added variable SCREEN_1#0 to live range equivalence class [ SCREEN_1#0 ]
Added variable malloc::return#3 to live range equivalence class [ malloc::return#3 ]
Added variable SCREEN_2#0 to live range equivalence class [ SCREEN_2#0 ]
Added variable malloc::return#0 to live range equivalence class [ malloc::return#0 ]
Complete equivalence classes
[ MEM#6 MEM#0 ]
[ malloc::return#2 ]
[ SCREEN_1#0 ]
[ malloc::return#3 ]
[ SCREEN_2#0 ]
[ malloc::return#0 ]
Allocated zp[2]:2 [ MEM#6 MEM#0 ]
Allocated zp[2]:4 [ SCREEN_2#0 ]
Allocated zp[2]:6 [ malloc::return#2 ]
Allocated zp[2]:8 [ malloc::return#3 ]
Allocated zp[2]:10 [ malloc::return#0 ]
Allocated zp[2]:12 [ SCREEN_1#0 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [3] malloc::return#2 = malloc::return#0 [ malloc::return#2 MEM#0 ] (  [ malloc::return#2 MEM#0 ] { { malloc::return#0 = malloc::return#2 } }  ) always clobbers reg byte a 
Statement [4] SCREEN_1#0 = malloc::return#2 [ SCREEN_1#0 MEM#0 ] (  [ SCREEN_1#0 MEM#0 ] { { MEM#0 = MEM#6 } { malloc::return#0 = malloc::return#3 } }  ) always clobbers reg byte a 
Statement [6] malloc::return#3 = malloc::return#0 [ SCREEN_1#0 malloc::return#3 ] (  [ SCREEN_1#0 malloc::return#3 ] { { MEM#0 = MEM#6 } { malloc::return#0 = malloc::return#3 } }  ) always clobbers reg byte a 
Statement [7] SCREEN_2#0 = malloc::return#3 [ SCREEN_1#0 SCREEN_2#0 ] (  [ SCREEN_1#0 SCREEN_2#0 ] { }  ) always clobbers reg byte a 
Statement [13] malloc::return#0 = MEM#0 [ malloc::return#0 MEM#0 ] ( malloc:2 [ malloc::return#0 MEM#0 ] { { malloc::return#0 = malloc::return#2 } }  malloc:5 [ SCREEN_1#0 malloc::return#0 MEM#0 ] { { MEM#0 = MEM#6 } { malloc::return#0 = malloc::return#3 } }  ) always clobbers reg byte a 
Statement [15] *SCREEN_1#0 = 0 [ SCREEN_2#0 ] ( main:9 [ SCREEN_2#0 ] { }  ) always clobbers reg byte a reg byte y 
Statement [16] *SCREEN_2#0 = 0 [ ] ( main:9 [ ] { }  ) always clobbers reg byte a reg byte y 
Potential registers zp[2]:2 [ MEM#6 MEM#0 ] : zp[2]:2 , 
Potential registers zp[2]:6 [ malloc::return#2 ] : zp[2]:6 , 
Potential registers zp[2]:12 [ SCREEN_1#0 ] : zp[2]:12 , 
Potential registers zp[2]:8 [ malloc::return#3 ] : zp[2]:8 , 
Potential registers zp[2]:4 [ SCREEN_2#0 ] : zp[2]:4 , 
Potential registers zp[2]:10 [ malloc::return#0 ] : zp[2]:10 , 

REGISTER UPLIFT SCOPES
Uplift Scope [] 17: zp[2]:2 [ MEM#6 MEM#0 ] 4.33: zp[2]:4 [ SCREEN_2#0 ] 2.6: zp[2]:12 [ SCREEN_1#0 ] 
Uplift Scope [malloc] 4: zp[2]:6 [ malloc::return#2 ] 4: zp[2]:8 [ malloc::return#3 ] 3.75: zp[2]:10 [ malloc::return#0 ] 
Uplift Scope [main] 
Uplift Scope [__start] 

Uplifting [] best 159 combination zp[2]:2 [ MEM#6 MEM#0 ] zp[2]:4 [ SCREEN_2#0 ] zp[2]:12 [ SCREEN_1#0 ] 
Uplifting [malloc] best 159 combination zp[2]:6 [ malloc::return#2 ] zp[2]:8 [ malloc::return#3 ] zp[2]:10 [ malloc::return#0 ] 
Uplifting [main] best 159 combination 
Uplifting [__start] best 159 combination 
Coalescing zero page register [ zp[2]:6 [ malloc::return#2 ] ] with [ zp[2]:12 [ SCREEN_1#0 ] ] - score: 1
Coalescing zero page register [ zp[2]:8 [ malloc::return#3 ] ] with [ zp[2]:4 [ SCREEN_2#0 ] ] - score: 1
Coalescing zero page register [ zp[2]:8 [ malloc::return#3 SCREEN_2#0 ] ] with [ zp[2]:10 [ malloc::return#0 ] ] - score: 1
Allocated (was zp[2]:8) zp[2]:4 [ malloc::return#3 SCREEN_2#0 malloc::return#0 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Error where the compiler is reusing the same ZP for two byte* variables.
// SCREEN_1 and SCREEN_2 are both allocated to ZP: 4
// Problem is that outside main() scope statements have zero call-paths and then isStatementAllocationOverlapping() never checks liveranges
// CallPath code must be rewritten to use @begin as the outermost call instead of main()
  // Upstart
  // Commodore 64 PRG executable file
.file [name="liverange-problem-0.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(__start)
  // Global Constants & labels
  .label MEM = 2
  .label SCREEN_1 = 6
  .label SCREEN_2 = 4
.segment Code
  // __start
__start: {
    // [1] phi from __start to __start::__init1 [phi:__start->__start::__init1]
  __init1_from___start:
    jmp __init1
    // __start::__init1
  __init1:
    // [2] call malloc
    // [11] phi from __start::__init1 to malloc [phi:__start::__init1->malloc]
  malloc_from___init1:
    // [11] phi MEM#6 = (char *) 1024 [phi:__start::__init1->malloc#0] -- pbuz1=pbuc1 
    lda #<$400
    sta.z MEM
    lda #>$400
    sta.z MEM+1
    jsr malloc
    // [3] malloc::return#2 = malloc::return#0 -- pbuz1=pbuz2 
    lda.z malloc.return
    sta.z malloc.return_1
    lda.z malloc.return+1
    sta.z malloc.return_1+1
    jmp __b2
    // __start::@2
  __b2:
    // [4] SCREEN_1#0 = malloc::return#2
    // [5] call malloc
    // [11] phi from __start::@2 to malloc [phi:__start::@2->malloc]
  malloc_from___b2:
    // [11] phi MEM#6 = MEM#0 [phi:__start::@2->malloc#0] -- register_copy 
    jsr malloc
    // [6] malloc::return#3 = malloc::return#0
    jmp __b3
    // __start::@3
  __b3:
    // [7] SCREEN_2#0 = malloc::return#3
    // [8] phi from __start::@3 to __start::@1 [phi:__start::@3->__start::@1]
  __b1_from___b3:
    jmp __b1
    // __start::@1
  __b1:
    // [9] call main
    jsr main
    jmp __breturn
    // __start::@return
  __breturn:
    // [10] return 
    rts
}
  // malloc
malloc: {
    .label return = 4
    .label return_1 = 6
    // [12] MEM#0 = ++ MEM#6 -- pbuz1=_inc_pbuz1 
    inc.z MEM
    bne !+
    inc.z MEM+1
  !:
    // [13] malloc::return#0 = MEM#0 -- pbuz1=pbuz2 
    lda.z MEM
    sta.z return
    lda.z MEM+1
    sta.z return+1
    jmp __breturn
    // malloc::@return
  __breturn:
    // [14] return 
    rts
}
  // main
main: {
    // [15] *SCREEN_1#0 = 0 -- _deref_pbuz1=vbuc1 
    lda #0
    ldy #0
    sta (SCREEN_1),y
    // [16] *SCREEN_2#0 = 0 -- _deref_pbuz1=vbuc1 
    lda #0
    ldy #0
    sta (SCREEN_2),y
    jmp __breturn
    // main::@return
  __breturn:
    // [17] return 
    rts
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __init1
Removing instruction jmp __b2
Removing instruction jmp __b3
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing instruction ldy #0 with TAY
Removing instruction lda #0
Removing instruction ldy #0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction __init1_from___start:
Removing instruction malloc_from___init1:
Removing instruction malloc_from___b2:
Removing instruction __b3:
Removing instruction __b1_from___b3:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __init1:
Removing instruction __b2:
Removing instruction __b1:
Removing instruction __breturn:
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
char *MEM
char *MEM#0 // MEM zp[2]:2 4.0
char *MEM#6 // MEM zp[2]:2 13.0
char *SCREEN_1
char *SCREEN_1#0 // SCREEN_1 zp[2]:6 2.6
char *SCREEN_2
char *SCREEN_2#0 // SCREEN_2 zp[2]:4 4.333333333333333
void __start()
void main()
char * malloc()
char *malloc::return
char *malloc::return#0 // return zp[2]:4 3.75
char *malloc::return#2 // return_1 zp[2]:6 4.0
char *malloc::return#3 // return zp[2]:4 4.0

zp[2]:2 [ MEM#6 MEM#0 ]
zp[2]:6 [ malloc::return#2 SCREEN_1#0 ]
zp[2]:4 [ malloc::return#3 SCREEN_2#0 malloc::return#0 ]


FINAL ASSEMBLER
Score: 98

  // File Comments
// Error where the compiler is reusing the same ZP for two byte* variables.
// SCREEN_1 and SCREEN_2 are both allocated to ZP: 4
// Problem is that outside main() scope statements have zero call-paths and then isStatementAllocationOverlapping() never checks liveranges
// CallPath code must be rewritten to use @begin as the outermost call instead of main()
  // Upstart
  // Commodore 64 PRG executable file
.file [name="liverange-problem-0.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(__start)
  // Global Constants & labels
  .label MEM = 2
  .label SCREEN_1 = 6
  .label SCREEN_2 = 4
.segment Code
  // __start
__start: {
    // [1] phi from __start to __start::__init1 [phi:__start->__start::__init1]
    // __start::__init1
    // byte* SCREEN_1 = malloc()
    // [2] call malloc
    // [11] phi from __start::__init1 to malloc [phi:__start::__init1->malloc]
    // [11] phi MEM#6 = (char *) 1024 [phi:__start::__init1->malloc#0] -- pbuz1=pbuc1 
    lda #<$400
    sta.z MEM
    lda #>$400
    sta.z MEM+1
    jsr malloc
    // byte* SCREEN_1 = malloc()
    // [3] malloc::return#2 = malloc::return#0 -- pbuz1=pbuz2 
    lda.z malloc.return
    sta.z malloc.return_1
    lda.z malloc.return+1
    sta.z malloc.return_1+1
    // __start::@2
    // [4] SCREEN_1#0 = malloc::return#2
    // byte* SCREEN_2 = malloc()
    // [5] call malloc
    // [11] phi from __start::@2 to malloc [phi:__start::@2->malloc]
    // [11] phi MEM#6 = MEM#0 [phi:__start::@2->malloc#0] -- register_copy 
    jsr malloc
    // byte* SCREEN_2 = malloc()
    // [6] malloc::return#3 = malloc::return#0
    // __start::@3
    // [7] SCREEN_2#0 = malloc::return#3
    // [8] phi from __start::@3 to __start::@1 [phi:__start::@3->__start::@1]
    // __start::@1
    // [9] call main
    jsr main
    // __start::@return
    // [10] return 
    rts
}
  // malloc
malloc: {
    .label return = 4
    .label return_1 = 6
    // return ++MEM;
    // [12] MEM#0 = ++ MEM#6 -- pbuz1=_inc_pbuz1 
    inc.z MEM
    bne !+
    inc.z MEM+1
  !:
    // [13] malloc::return#0 = MEM#0 -- pbuz1=pbuz2 
    lda.z MEM
    sta.z return
    lda.z MEM+1
    sta.z return+1
    // malloc::@return
    // }
    // [14] return 
    rts
}
  // main
main: {
    // *SCREEN_1 = 0
    // [15] *SCREEN_1#0 = 0 -- _deref_pbuz1=vbuc1 
    lda #0
    tay
    sta (SCREEN_1),y
    // *SCREEN_2 = 0
    // [16] *SCREEN_2#0 = 0 -- _deref_pbuz1=vbuc1 
    sta (SCREEN_2),y
    // main::@return
    // }
    // [17] return 
    rts
}
  // File Data

