
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::$4 = 0 * SIZEOF_UNSIGNED_INT
  fibs[main::$4] = 0
  main::$5 = 1 * SIZEOF_UNSIGNED_INT
  fibs[main::$5] = 1
  main::i#0 = 0
  to:main::@1
main::@1: scope:[main]  from main main::@2
  main::i#2 = phi( main/main::i#0, main::@2/main::i#1 )
  main::$0 = main::i#2 < $19-2
  if(main::$0) goto main::@2
  to:main::@return
main::@2: scope:[main]  from main::@1
  main::i#3 = phi( main::@1/main::i#2 )
  main::$1 = main::i#3 + 2
  main::$2 = main::i#3 + 1
  main::$6 = main::i#3 * SIZEOF_UNSIGNED_INT
  main::$7 = main::$2 * SIZEOF_UNSIGNED_INT
  main::$3 = fibs[main::$6] + fibs[main::$7]
  main::$8 = main::$1 * SIZEOF_UNSIGNED_INT
  fibs[main::$8] = main::$3
  main::i#1 = ++ main::i#3
  to:main::@1
main::@return: scope:[main]  from main::@1
  return 
  to:@return

void __start()
__start: scope:[__start]  from
  call main
  to:__start::@1
__start::@1: scope:[__start]  from __start
  to:__start::@return
__start::@return: scope:[__start]  from __start::@1
  return 
  to:@return

SYMBOL TABLE SSA
__constant char SIZEOF_UNSIGNED_INT = 2
void __start()
__constant unsigned int fibs[$19] = { fill( $19, 0) }
void main()
bool main::$0
number main::$1
number main::$2
unsigned int main::$3
number main::$4
number main::$5
char main::$6
number main::$7
number main::$8
char main::i
char main::i#0
char main::i#1
char main::i#2
char main::i#3

Adding number conversion cast (unumber) 0 in main::$4 = 0 * SIZEOF_UNSIGNED_INT
Adding number conversion cast (unumber) main::$4 in main::$4 = (unumber)0 * SIZEOF_UNSIGNED_INT
Adding number conversion cast (unumber) 0 in fibs[main::$4] = 0
Adding number conversion cast (unumber) 1 in main::$5 = 1 * SIZEOF_UNSIGNED_INT
Adding number conversion cast (unumber) main::$5 in main::$5 = (unumber)1 * SIZEOF_UNSIGNED_INT
Adding number conversion cast (unumber) 1 in fibs[main::$5] = 1
Adding number conversion cast (unumber) $19-2 in main::$0 = main::i#2 < $19-2
Adding number conversion cast (unumber) 2 in main::$1 = main::i#3 + 2
Adding number conversion cast (unumber) main::$1 in main::$1 = main::i#3 + (unumber)2
Adding number conversion cast (unumber) 1 in main::$2 = main::i#3 + 1
Adding number conversion cast (unumber) main::$2 in main::$2 = main::i#3 + (unumber)1
Adding number conversion cast (unumber) main::$7 in main::$7 = main::$2 * SIZEOF_UNSIGNED_INT
Adding number conversion cast (unumber) main::$8 in main::$8 = main::$1 * SIZEOF_UNSIGNED_INT
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast fibs[main::$4] = (unumber)0
Inlining cast fibs[main::$5] = (unumber)1
Successful SSA optimization Pass2InlineCast
Simplifying constant integer cast 0
Simplifying constant integer cast 0
Simplifying constant integer cast 1
Simplifying constant integer cast 1
Simplifying constant integer cast 2
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 0
Finalized unsigned number type (char) 0
Finalized unsigned number type (char) 1
Finalized unsigned number type (char) 1
Finalized unsigned number type (char) 2
Finalized unsigned number type (char) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to char in main::$4 = 0 * SIZEOF_UNSIGNED_INT
Inferred type updated to char in main::$5 = 1 * SIZEOF_UNSIGNED_INT
Inferred type updated to char in main::$1 = main::i#3 + 2
Inferred type updated to char in main::$2 = main::i#3 + 1
Inferred type updated to char in main::$7 = main::$2 * SIZEOF_UNSIGNED_INT
Inferred type updated to char in main::$8 = main::$1 * SIZEOF_UNSIGNED_INT
Alias main::i#2 = main::i#3 
Successful SSA optimization Pass2AliasElimination
Simple Condition main::$0 [7] if(main::i#2<(char)$19-2) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant right-side identified [0] main::$4 = 0 * SIZEOF_UNSIGNED_INT
Constant right-side identified [2] main::$5 = 1 * SIZEOF_UNSIGNED_INT
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant main::$4 = 0*SIZEOF_UNSIGNED_INT
Constant main::$5 = 1*SIZEOF_UNSIGNED_INT
Constant main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Simplifying constant evaluating to zero 0*SIZEOF_UNSIGNED_INT in 
Successful SSA optimization PassNSimplifyConstantZero
Simplifying expression containing zero fibs in [1] fibs[main::$4] = 0
Successful SSA optimization PassNSimplifyExpressionWithZero
Eliminating unused constant main::$4
Successful SSA optimization PassNEliminateUnusedVars
Removing unused procedure __start
Removing unused procedure block __start
Removing unused procedure block __start::@1
Removing unused procedure block __start::@return
Successful SSA optimization PassNEliminateEmptyStart
Rewriting multiplication to use shift [6] main::$6 = main::i#2 * SIZEOF_UNSIGNED_INT
Rewriting multiplication to use shift [7] main::$7 = main::$2 * SIZEOF_UNSIGNED_INT
Rewriting multiplication to use shift [9] main::$8 = main::$1 * SIZEOF_UNSIGNED_INT
Successful SSA optimization Pass2MultiplyToShiftRewriting
Inlining constant with var siblings main::i#0
Constant inlined main::$5 = 1*SIZEOF_UNSIGNED_INT
Constant inlined main::i#0 = 0
Successful SSA optimization Pass2ConstantInlining
Consolidated array index constant in *(fibs+1*SIZEOF_UNSIGNED_INT)
Successful SSA optimization Pass2ConstantAdditionElimination
Finalized unsigned number type (char) $19
Finalized unsigned number type (char) $19
Finalized unsigned number type (char) $19
Finalized unsigned number type (char) 2
Successful SSA optimization PassNFinalizeNumberTypeConversions
Simplifying constant integer cast $19-2
Successful SSA optimization PassNCastSimplification
CALL GRAPH

Created 1 initial phi equivalence classes
Coalesced [13] main::i#4 = main::i#1
Coalesced down to 1 phi equivalence classes

FINAL CONTROL FLOW GRAPH

void main()
main: scope:[main]  from
  [0] *fibs = 0
  [1] *(fibs+1*SIZEOF_UNSIGNED_INT) = 1
  to:main::@1
main::@1: scope:[main]  from main main::@2
  [2] main::i#2 = phi( main/0, main::@2/main::i#1 )
  [3] if(main::i#2<$19-2) goto main::@2
  to:main::@return
main::@return: scope:[main]  from main::@1
  [4] return 
  to:@return
main::@2: scope:[main]  from main::@1
  [5] main::$1 = main::i#2 + 2
  [6] main::$2 = main::i#2 + 1
  [7] main::$6 = main::i#2 << 1
  [8] main::$7 = main::$2 << 1
  [9] main::$3 = fibs[main::$6] + fibs[main::$7]
  [10] main::$8 = main::$1 << 1
  [11] fibs[main::$8] = main::$3
  [12] main::i#1 = ++ main::i#2
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char main::$1 // 4.4
char main::$2 // 11.0
unsigned int main::$3 // 11.0
char main::$6 // 11.0
char main::$7 // 22.0
char main::$8 // 22.0
char main::i
char main::i#1 // 22.0
char main::i#2 // 7.333333333333334

Initial phi equivalence classes
[ main::i#2 main::i#1 ]
Added variable main::$1 to live range equivalence class [ main::$1 ]
Added variable main::$2 to live range equivalence class [ main::$2 ]
Added variable main::$6 to live range equivalence class [ main::$6 ]
Added variable main::$7 to live range equivalence class [ main::$7 ]
Added variable main::$3 to live range equivalence class [ main::$3 ]
Added variable main::$8 to live range equivalence class [ main::$8 ]
Complete equivalence classes
[ main::i#2 main::i#1 ]
[ main::$1 ]
[ main::$2 ]
[ main::$6 ]
[ main::$7 ]
[ main::$3 ]
[ main::$8 ]
Allocated zp[1]:2 [ main::i#2 main::i#1 ]
Allocated zp[1]:3 [ main::$7 ]
Allocated zp[1]:4 [ main::$8 ]
Allocated zp[1]:5 [ main::$2 ]
Allocated zp[1]:6 [ main::$6 ]
Allocated zp[2]:7 [ main::$3 ]
Allocated zp[1]:9 [ main::$1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [0] *fibs = 0 [ ] (  [ ] { }  ) always clobbers reg byte a 
Statement [1] *(fibs+1*SIZEOF_UNSIGNED_INT) = 1 [ ] (  [ ] { }  ) always clobbers reg byte a 
Statement [7] main::$6 = main::i#2 << 1 [ main::i#2 main::$1 main::$2 main::$6 ] (  [ main::i#2 main::$1 main::$2 main::$6 ] { }  ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::i#2 main::i#1 ]
Removing always clobbered register reg byte a as potential for zp[1]:9 [ main::$1 ]
Removing always clobbered register reg byte a as potential for zp[1]:5 [ main::$2 ]
Statement [8] main::$7 = main::$2 << 1 [ main::i#2 main::$1 main::$6 main::$7 ] (  [ main::i#2 main::$1 main::$6 main::$7 ] { }  ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:6 [ main::$6 ]
Statement [9] main::$3 = fibs[main::$6] + fibs[main::$7] [ main::i#2 main::$1 main::$3 ] (  [ main::i#2 main::$1 main::$3 ] { }  ) always clobbers reg byte a 
Statement [10] main::$8 = main::$1 << 1 [ main::i#2 main::$3 main::$8 ] (  [ main::i#2 main::$3 main::$8 ] { }  ) always clobbers reg byte a 
Statement [11] fibs[main::$8] = main::$3 [ main::i#2 ] (  [ main::i#2 ] { }  ) always clobbers reg byte a 
Statement [0] *fibs = 0 [ ] (  [ ] { }  ) always clobbers reg byte a 
Statement [1] *(fibs+1*SIZEOF_UNSIGNED_INT) = 1 [ ] (  [ ] { }  ) always clobbers reg byte a 
Statement [7] main::$6 = main::i#2 << 1 [ main::i#2 main::$1 main::$2 main::$6 ] (  [ main::i#2 main::$1 main::$2 main::$6 ] { }  ) always clobbers reg byte a 
Statement [8] main::$7 = main::$2 << 1 [ main::i#2 main::$1 main::$6 main::$7 ] (  [ main::i#2 main::$1 main::$6 main::$7 ] { }  ) always clobbers reg byte a 
Statement [9] main::$3 = fibs[main::$6] + fibs[main::$7] [ main::i#2 main::$1 main::$3 ] (  [ main::i#2 main::$1 main::$3 ] { }  ) always clobbers reg byte a 
Statement [10] main::$8 = main::$1 << 1 [ main::i#2 main::$3 main::$8 ] (  [ main::i#2 main::$3 main::$8 ] { }  ) always clobbers reg byte a 
Statement [11] fibs[main::$8] = main::$3 [ main::i#2 ] (  [ main::i#2 ] { }  ) always clobbers reg byte a 
Potential registers zp[1]:2 [ main::i#2 main::i#1 ] : zp[1]:2 , reg byte x , reg byte y , 
Potential registers zp[1]:9 [ main::$1 ] : zp[1]:9 , reg byte x , reg byte y , 
Potential registers zp[1]:5 [ main::$2 ] : zp[1]:5 , reg byte x , reg byte y , 
Potential registers zp[1]:6 [ main::$6 ] : zp[1]:6 , reg byte x , reg byte y , 
Potential registers zp[1]:3 [ main::$7 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[2]:7 [ main::$3 ] : zp[2]:7 , 
Potential registers zp[1]:4 [ main::$8 ] : zp[1]:4 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 29.33: zp[1]:2 [ main::i#2 main::i#1 ] 22: zp[1]:3 [ main::$7 ] 22: zp[1]:4 [ main::$8 ] 11: zp[1]:5 [ main::$2 ] 11: zp[1]:6 [ main::$6 ] 11: zp[2]:7 [ main::$3 ] 4.4: zp[1]:9 [ main::$1 ] 
Uplift Scope [] 

Uplifting [main] best 1105 combination zp[1]:2 [ main::i#2 main::i#1 ] reg byte a [ main::$7 ] reg byte a [ main::$8 ] reg byte x [ main::$2 ] zp[1]:6 [ main::$6 ] zp[2]:7 [ main::$3 ] zp[1]:9 [ main::$1 ] 
Limited combination testing to 100 combinations of 1296 possible.
Uplifting [] best 1105 combination 
Attempting to uplift remaining variables inzp[1]:2 [ main::i#2 main::i#1 ]
Uplifting [main] best 1105 combination zp[1]:2 [ main::i#2 main::i#1 ] 
Attempting to uplift remaining variables inzp[1]:6 [ main::$6 ]
Uplifting [main] best 1065 combination reg byte y [ main::$6 ] 
Attempting to uplift remaining variables inzp[1]:9 [ main::$1 ]
Uplifting [main] best 1065 combination zp[1]:9 [ main::$1 ] 
Allocated (was zp[2]:7) zp[2]:3 [ main::$3 ]
Allocated (was zp[1]:9) zp[1]:5 [ main::$1 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Test array index pointer rewriting
// 16bit array with 8bit index
// Fibonacci calculation uses adjacent indices inside the loop
  // Upstart
  // Commodore 64 PRG executable file
.file [name="index-pointer-rewrite-5.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(main)
  // Global Constants & labels
  .const SIZEOF_UNSIGNED_INT = 2
.segment Code
  // main
main: {
    .label __1 = 5
    .label __3 = 3
    .label i = 2
    // [0] *fibs = 0 -- _deref_pwuc1=vbuc2 
    lda #<0
    sta fibs
    lda #>0
    sta fibs+1
    // [1] *(fibs+1*SIZEOF_UNSIGNED_INT) = 1 -- _deref_pwuc1=vbuc2 
    lda #<1
    sta fibs+1*SIZEOF_UNSIGNED_INT
    lda #>1
    sta fibs+1*SIZEOF_UNSIGNED_INT+1
    // [2] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [2] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta.z i
    jmp __b1
    // main::@1
  __b1:
    // [3] if(main::i#2<$19-2) goto main::@2 -- vbuz1_lt_vbuc1_then_la1 
    lda.z i
    cmp #$19-2
    bcc __b2
    jmp __breturn
    // main::@return
  __breturn:
    // [4] return 
    rts
    // main::@2
  __b2:
    // [5] main::$1 = main::i#2 + 2 -- vbuz1=vbuz2_plus_2 
    lda.z i
    clc
    adc #2
    sta.z __1
    // [6] main::$2 = main::i#2 + 1 -- vbuxx=vbuz1_plus_1 
    ldx.z i
    inx
    // [7] main::$6 = main::i#2 << 1 -- vbuyy=vbuz1_rol_1 
    lda.z i
    asl
    tay
    // [8] main::$7 = main::$2 << 1 -- vbuaa=vbuxx_rol_1 
    txa
    asl
    // [9] main::$3 = fibs[main::$6] + fibs[main::$7] -- vwuz1=pwuc1_derefidx_vbuyy_plus_pwuc1_derefidx_vbuaa 
    tax
    clc
    lda fibs,x
    adc fibs,y
    sta.z __3
    lda fibs+1,x
    adc fibs+1,y
    sta.z __3+1
    // [10] main::$8 = main::$1 << 1 -- vbuaa=vbuz1_rol_1 
    lda.z __1
    asl
    // [11] fibs[main::$8] = main::$3 -- pwuc1_derefidx_vbuaa=vwuz1 
    tay
    lda.z __3
    sta fibs,y
    lda.z __3+1
    sta fibs+1,y
    // [12] main::i#1 = ++ main::i#2 -- vbuz1=_inc_vbuz1 
    inc.z i
    // [2] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
  __b1_from___b2:
    // [2] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#0] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  fibs: .fill 2*$19, 0

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #>0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction __b1_from_main:
Removing instruction __breturn:
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5UnusedLabelElimination
Removing instruction lda #0
Succesful ASM optimization Pass5UnnecesaryLoadElimination

FINAL SYMBOL TABLE
__constant char SIZEOF_UNSIGNED_INT = 2
__constant unsigned int fibs[$19] = { fill( $19, 0) }
void main()
char main::$1 // zp[1]:5 4.4
char main::$2 // reg byte x 11.0
unsigned int main::$3 // zp[2]:3 11.0
char main::$6 // reg byte y 11.0
char main::$7 // reg byte a 22.0
char main::$8 // reg byte a 22.0
char main::i
char main::i#1 // i zp[1]:2 22.0
char main::i#2 // i zp[1]:2 7.333333333333334

zp[1]:2 [ main::i#2 main::i#1 ]
zp[1]:5 [ main::$1 ]
reg byte x [ main::$2 ]
reg byte y [ main::$6 ]
reg byte a [ main::$7 ]
zp[2]:3 [ main::$3 ]
reg byte a [ main::$8 ]


FINAL ASSEMBLER
Score: 983

  // File Comments
// Test array index pointer rewriting
// 16bit array with 8bit index
// Fibonacci calculation uses adjacent indices inside the loop
  // Upstart
  // Commodore 64 PRG executable file
.file [name="index-pointer-rewrite-5.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(main)
  // Global Constants & labels
  .const SIZEOF_UNSIGNED_INT = 2
.segment Code
  // main
main: {
    .label __1 = 5
    .label __3 = 3
    .label i = 2
    // fibs[0] = 0
    // [0] *fibs = 0 -- _deref_pwuc1=vbuc2 
    lda #<0
    sta fibs
    sta fibs+1
    // fibs[1] = 1
    // [1] *(fibs+1*SIZEOF_UNSIGNED_INT) = 1 -- _deref_pwuc1=vbuc2 
    lda #<1
    sta fibs+1*SIZEOF_UNSIGNED_INT
    lda #>1
    sta fibs+1*SIZEOF_UNSIGNED_INT+1
    // [2] phi from main to main::@1 [phi:main->main::@1]
    // [2] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuz1=vbuc1 
    sta.z i
    // main::@1
  __b1:
    // for(char i=0;i<NUM_FIBS-2;i++)
    // [3] if(main::i#2<$19-2) goto main::@2 -- vbuz1_lt_vbuc1_then_la1 
    lda.z i
    cmp #$19-2
    bcc __b2
    // main::@return
    // }
    // [4] return 
    rts
    // main::@2
  __b2:
    // i+2
    // [5] main::$1 = main::i#2 + 2 -- vbuz1=vbuz2_plus_2 
    lda.z i
    clc
    adc #2
    sta.z __1
    // i+1
    // [6] main::$2 = main::i#2 + 1 -- vbuxx=vbuz1_plus_1 
    ldx.z i
    inx
    // fibs[i]+fibs[i+1]
    // [7] main::$6 = main::i#2 << 1 -- vbuyy=vbuz1_rol_1 
    lda.z i
    asl
    tay
    // [8] main::$7 = main::$2 << 1 -- vbuaa=vbuxx_rol_1 
    txa
    asl
    // [9] main::$3 = fibs[main::$6] + fibs[main::$7] -- vwuz1=pwuc1_derefidx_vbuyy_plus_pwuc1_derefidx_vbuaa 
    tax
    clc
    lda fibs,x
    adc fibs,y
    sta.z __3
    lda fibs+1,x
    adc fibs+1,y
    sta.z __3+1
    // fibs[i+2] = fibs[i]+fibs[i+1]
    // [10] main::$8 = main::$1 << 1 -- vbuaa=vbuz1_rol_1 
    lda.z __1
    asl
    // [11] fibs[main::$8] = main::$3 -- pwuc1_derefidx_vbuaa=vwuz1 
    tay
    lda.z __3
    sta fibs,y
    lda.z __3+1
    sta fibs+1,y
    // for(char i=0;i<NUM_FIBS-2;i++)
    // [12] main::i#1 = ++ main::i#2 -- vbuz1=_inc_vbuz1 
    inc.z i
    // [2] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
    // [2] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#0] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  fibs: .fill 2*$19, 0

