PARSING src/test/java/dk/camelot64/kickc/test/kc/incd020.kc
// Incrementing / decrementing pointer content should result in code modifying the memory location - eg. inc $d020.
// Currently it does not but instead leads to just reading the value a few times.

byte* BGCOL = $d020;
void main() {
    do {
        ++*BGCOL;
        (*BGCOL)--;
    } while (true);
}
Adding pre/post-modifier *((byte*) BGCOL) ← ++ *((byte*) BGCOL)
Adding pre/post-modifier *((byte*) BGCOL) ← -- *((byte*) BGCOL)

STATEMENTS
  (byte*) BGCOL ← (word/dword/signed dword) 53280
proc (void()) main()
main::@1:
  *((byte*) BGCOL) ← ++ *((byte*) BGCOL)
  *((byte*) BGCOL) ← -- *((byte*) BGCOL)
  if(true) goto main::@1
main::@return:
  return 
endproc // main()
  call main 

SYMBOLS
(byte*) BGCOL
(void()) main()
(label) main::@1
(label) main::@return

Promoting word/dword/signed dword to byte* in BGCOL ← ((byte*)) 53280
INITIAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  (byte*) BGCOL ← ((byte*)) (word/dword/signed dword) 53280
  to:@1
main: scope:[main]  from
  to:main::@1
main::@1: scope:[main]  from main main::@1
  *((byte*) BGCOL) ← ++ *((byte*) BGCOL)
  *((byte*) BGCOL) ← -- *((byte*) BGCOL)
  if(true) goto main::@1
  to:main::@2
main::@2: scope:[main]  from main::@1
  to:main::@return
main::@return: scope:[main]  from main::@2
  return 
  to:@return
@1: scope:[]  from @begin
  call main 
  to:@end
@end: scope:[]  from @1

Removing empty block main::@2
PROCEDURE MODIFY VARIABLE ANALYSIS

Completing Phi functions...
Completing Phi functions...
Completing Phi functions...

CONTROL FLOW GRAPH SSA WITH ASSIGNMENT CALL & RETURN
@begin: scope:[]  from
  (byte*) BGCOL#0 ← ((byte*)) (word/dword/signed dword) 53280
  to:@1
main: scope:[main]  from @1
  (byte*) BGCOL#2 ← phi( @1/(byte*) BGCOL#3 )
  to:main::@1
main::@1: scope:[main]  from main main::@1
  (byte*) BGCOL#1 ← phi( main/(byte*) BGCOL#2 main::@1/(byte*) BGCOL#1 )
  *((byte*) BGCOL#1) ← ++ *((byte*) BGCOL#1)
  *((byte*) BGCOL#1) ← -- *((byte*) BGCOL#1)
  if(true) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@1
  return 
  to:@return
@1: scope:[]  from @begin
  (byte*) BGCOL#3 ← phi( @begin/(byte*) BGCOL#0 )
  call main param-assignment
  to:@2
@2: scope:[]  from @1
  to:@end
@end: scope:[]  from @2

SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(byte*) BGCOL
(byte*) BGCOL#0
(byte*) BGCOL#1
(byte*) BGCOL#2
(byte*) BGCOL#3
(void()) main()
(label) main::@1
(label) main::@return

OPTIMIZING CONTROL FLOW GRAPH
Culled Empty Block (label) @2
Succesful SSA optimization Pass2CullEmptyBlocks
Not aliassing across scopes: BGCOL#2 BGCOL#3
Alias (byte*) BGCOL#0 = (byte*) BGCOL#3 
Succesful SSA optimization Pass2AliasElimination
Not aliassing across scopes: BGCOL#2 BGCOL#0
Self Phi Eliminated (byte*) BGCOL#1
Succesful SSA optimization Pass2SelfPhiElimination
Redundant Phi (byte*) BGCOL#2 (byte*) BGCOL#0
Redundant Phi (byte*) BGCOL#1 (byte*) BGCOL#2
Succesful SSA optimization Pass2RedundantPhiElimination
Constant (const byte*) BGCOL#0 = ((byte*))53280
Succesful SSA optimization Pass2ConstantIdentification
OPTIMIZING CONTROL FLOW GRAPH
Block Sequence Planned @begin @1 @end main main::@1 main::@return 
Block Sequence Planned @begin @1 @end main main::@1 main::@return 
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 main
CALL GRAPH
Calls in [] to main:2 

Propagating live ranges...
Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Block Sequence Planned @begin @1 @end main main::@1 main::@return 
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 main
Propagating live ranges...

FINAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  [0] phi() [ ] ( )
  to:@1
@1: scope:[]  from @begin
  [1] phi() [ ] ( )
  [2] call main param-assignment [ ] ( )
  to:@end
@end: scope:[]  from @1
  [3] phi() [ ] ( )
main: scope:[main]  from @1
  [4] phi() [ ] ( main:2 [ ] )
  to:main::@1
main::@1: scope:[main]  from main main::@1
  [5] *((const byte*) BGCOL#0) ← ++ *((const byte*) BGCOL#0) [ ] ( main:2 [ ] )
  [6] *((const byte*) BGCOL#0) ← -- *((const byte*) BGCOL#0) [ ] ( main:2 [ ] )
  [7] if(true) goto main::@1 [ ] ( main:2 [ ] )
  to:main::@return
main::@return: scope:[main]  from main::@1
  [8] return  [ ] ( main:2 [ ] )
  to:@return

DOMINATORS
@begin dominated by  @begin 
@1 dominated by  @1 @begin 
@end dominated by  @1 @begin @end 
main dominated by  @1 @begin main 
main::@1 dominated by  @1 @begin main::@1 main 
main::@return dominated by  main::@return @1 @begin main::@1 main 

NATURAL LOOPS
Found back edge: Loop head: main::@1 tails: main::@1 blocks: null
Populated: Loop head: main::@1 tails: main::@1 blocks: main::@1 
Loop head: main::@1 tails: main::@1 blocks: main::@1 

NATURAL LOOPS WITH DEPTH
Found 0 loops in scope []
Found 1 loops in scope [main]
  Loop head: main::@1 tails: main::@1 blocks: main::@1 
Loop head: main::@1 tails: main::@1 blocks: main::@1  depth: 1


VARIABLE REGISTER WEIGHTS
(byte*) BGCOL
(void()) main()

Initial phi equivalence classes
Complete equivalence classes

INITIAL ASM
//SEG0 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG1 Global Constants & labels
  .label BGCOL = $d020
//SEG2 @begin
bbegin:
//SEG3 [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
//SEG4 @1
b1:
//SEG5 [2] call main param-assignment [ ] ( )
//SEG6 [4] phi from @1 to main [phi:@1->main]
main_from_b1:
  jsr main
//SEG7 [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
//SEG8 @end
bend:
//SEG9 main
main: {
    jmp b1
  //SEG10 main::@1
  b1:
  //SEG11 [5] *((const byte*) BGCOL#0) ← ++ *((const byte*) BGCOL#0) [ ] ( main:2 [ ] ) -- _deref_pbuc1=_inc__deref_pbuc1 
    inc BGCOL
  //SEG12 [6] *((const byte*) BGCOL#0) ← -- *((const byte*) BGCOL#0) [ ] ( main:2 [ ] ) -- _deref_pbuc1=_dec__deref_pbuc1 
    dec BGCOL
  //SEG13 [7] if(true) goto main::@1 [ ] ( main:2 [ ] ) -- true_then_la1 
    jmp b1
    jmp breturn
  //SEG14 main::@return
  breturn:
  //SEG15 [8] return  [ ] ( main:2 [ ] )
    rts
}

REGISTER UPLIFT POTENTIAL REGISTERS

REGISTER UPLIFT SCOPES
Uplift Scope [main] 
Uplift Scope [] 

Uplifting [main] best 228 combination 
Uplifting [] best 228 combination 

ASSEMBLER BEFORE OPTIMIZATION
//SEG0 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG1 Global Constants & labels
  .label BGCOL = $d020
//SEG2 @begin
bbegin:
//SEG3 [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
//SEG4 @1
b1:
//SEG5 [2] call main param-assignment [ ] ( )
//SEG6 [4] phi from @1 to main [phi:@1->main]
main_from_b1:
  jsr main
//SEG7 [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
//SEG8 @end
bend:
//SEG9 main
main: {
    jmp b1
  //SEG10 main::@1
  b1:
  //SEG11 [5] *((const byte*) BGCOL#0) ← ++ *((const byte*) BGCOL#0) [ ] ( main:2 [ ] ) -- _deref_pbuc1=_inc__deref_pbuc1 
    inc BGCOL
  //SEG12 [6] *((const byte*) BGCOL#0) ← -- *((const byte*) BGCOL#0) [ ] ( main:2 [ ] ) -- _deref_pbuc1=_dec__deref_pbuc1 
    dec BGCOL
  //SEG13 [7] if(true) goto main::@1 [ ] ( main:2 [ ] ) -- true_then_la1 
    jmp b1
    jmp breturn
  //SEG14 main::@return
  breturn:
  //SEG15 [8] return  [ ] ( main:2 [ ] )
    rts
}

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction bbegin:
Removing instruction b1_from_bbegin:
Removing instruction main_from_b1:
Removing instruction bend_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction b1:
Removing instruction bend:
Removing instruction breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Removing unreachable instruction rts
Succesful ASM optimization Pass5UnreachableCodeElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(byte*) BGCOL
(const byte*) BGCOL#0 BGCOL = ((byte*))(word/dword/signed dword) 53280
(void()) main()
(label) main::@1
(label) main::@return



FINAL ASSEMBLER
Score: 156

//SEG0 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG1 Global Constants & labels
  .label BGCOL = $d020
//SEG2 @begin
//SEG3 [1] phi from @begin to @1 [phi:@begin->@1]
//SEG4 @1
//SEG5 [2] call main param-assignment [ ] ( )
//SEG6 [4] phi from @1 to main [phi:@1->main]
  jsr main
//SEG7 [3] phi from @1 to @end [phi:@1->@end]
//SEG8 @end
//SEG9 main
main: {
  //SEG10 main::@1
  b1:
  //SEG11 [5] *((const byte*) BGCOL#0) ← ++ *((const byte*) BGCOL#0) [ ] ( main:2 [ ] ) -- _deref_pbuc1=_inc__deref_pbuc1 
    inc BGCOL
  //SEG12 [6] *((const byte*) BGCOL#0) ← -- *((const byte*) BGCOL#0) [ ] ( main:2 [ ] ) -- _deref_pbuc1=_dec__deref_pbuc1 
    dec BGCOL
  //SEG13 [7] if(true) goto main::@1 [ ] ( main:2 [ ] ) -- true_then_la1 
    jmp b1
  //SEG14 main::@return
  //SEG15 [8] return  [ ] ( main:2 [ ] )
}

