
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  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 < 5
  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 * SIZEOF_UNSIGNED_INT
  v[main::$1] = v[main::$1] + 5
  main::$2 = main::i#3 * SIZEOF_UNSIGNED_INT
  x[main::$2] = x[main::$2] + v[main::$2]
  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()
void main()
bool main::$0
char main::$1
char main::$2
char main::i
char main::i#0
char main::i#1
char main::i#2
char main::i#3
__constant unsigned int v[5] = { fill( 5, 0) }
__constant unsigned int x[5] = { fill( 5, 0) }

Adding number conversion cast (unumber) 5 in main::$0 = main::i#2 < 5
Adding number conversion cast (unumber) 5 in v[main::$1] = v[main::$1] + 5
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast 5
Simplifying constant integer cast 5
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 5
Finalized unsigned number type (char) 5
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias main::i#2 = main::i#3 
Successful SSA optimization Pass2AliasElimination
Identified duplicate assignment right side [7] main::$2 = main::i#2 * SIZEOF_UNSIGNED_INT
Successful SSA optimization Pass2DuplicateRValueIdentification
Simple Condition main::$0 [3] if(main::i#2<5) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
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
Alias main::$2 = main::$1 
Successful SSA optimization Pass2AliasElimination
Rewriting multiplication to use shift [2] main::$2 = main::i#2 * SIZEOF_UNSIGNED_INT
Successful SSA optimization Pass2MultiplyToShiftRewriting
Inlining constant with var siblings main::i#0
Constant inlined main::i#0 = 0
Successful SSA optimization Pass2ConstantInlining
Eliminating unused constant SIZEOF_UNSIGNED_INT
Successful SSA optimization PassNEliminateUnusedVars
Finalized unsigned number type (char) 5
Finalized unsigned number type (char) 5
Finalized unsigned number type (char) 5
Finalized unsigned number type (char) 5
Successful SSA optimization PassNFinalizeNumberTypeConversions
Adding NOP phi() at start of main
CALL GRAPH

Created 1 initial phi equivalence classes
Coalesced [8] main::i#4 = main::i#1
Coalesced down to 1 phi equivalence classes
Adding NOP phi() at start of main

FINAL CONTROL FLOW GRAPH

void main()
main: scope:[main]  from
  [0] phi()
  to:main::@1
main::@1: scope:[main]  from main main::@2
  [1] main::i#2 = phi( main/0, main::@2/main::i#1 )
  [2] if(main::i#2<5) goto main::@2
  to:main::@return
main::@return: scope:[main]  from main::@1
  [3] return 
  to:@return
main::@2: scope:[main]  from main::@1
  [4] main::$2 = main::i#2 << 1
  [5] v[main::$2] = v[main::$2] + 5
  [6] x[main::$2] = x[main::$2] + v[main::$2]
  [7] main::i#1 = ++ main::i#2
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char main::$2 // 33.0
char main::i
char main::i#1 // 22.0
char main::i#2 // 8.8

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

REGISTER UPLIFT SCOPES
Uplift Scope [main] 33: zp[1]:2 [ main::$2 ] 30.8: zp[1]:3 [ main::i#2 main::i#1 ] 
Uplift Scope [] 

Uplifting [main] best 791 combination reg byte y [ main::$2 ] reg byte x [ main::i#2 main::i#1 ] 
Uplifting [] best 791 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Demonstrates a problem with inner indexes into arrays where the elemt size>1
  // Upstart
  // Commodore 64 PRG executable file
.file [name="inner-index-problem.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
.segment Code
  // main
main: {
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    jmp __b1
    // main::@1
  __b1:
    // [2] if(main::i#2<5) goto main::@2 -- vbuxx_lt_vbuc1_then_la1 
    cpx #5
    bcc __b2
    jmp __breturn
    // main::@return
  __breturn:
    // [3] return 
    rts
    // main::@2
  __b2:
    // [4] main::$2 = main::i#2 << 1 -- vbuyy=vbuxx_rol_1 
    txa
    asl
    tay
    // [5] v[main::$2] = v[main::$2] + 5 -- pwuc1_derefidx_vbuyy=pwuc1_derefidx_vbuyy_plus_vbuc2 
    clc
    lda v,y
    adc #5
    sta v,y
    lda v+1,y
    adc #0
    sta v+1,y
    // [6] x[main::$2] = x[main::$2] + v[main::$2] -- pwuc1_derefidx_vbuyy=pwuc1_derefidx_vbuyy_plus_pwuc2_derefidx_vbuyy 
    clc
    lda x,y
    adc v,y
    sta x,y
    lda x+1,y
    adc v+1,y
    sta x+1,y
    // [7] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
  __b1_from___b2:
    // [1] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#0] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  v: .fill 2*5, 0
  x: .fill 2*5, 0

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1_from_main:
Removing instruction __breturn:
Removing instruction __b1_from___b2:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
void main()
char main::$2 // reg byte y 33.0
char main::i
char main::i#1 // reg byte x 22.0
char main::i#2 // reg byte x 8.8
__constant unsigned int v[5] = { fill( 5, 0) }
__constant unsigned int x[5] = { fill( 5, 0) }

reg byte x [ main::i#2 main::i#1 ]
reg byte y [ main::$2 ]


FINAL ASSEMBLER
Score: 731

  // File Comments
// Demonstrates a problem with inner indexes into arrays where the elemt size>1
  // Upstart
  // Commodore 64 PRG executable file
.file [name="inner-index-problem.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
.segment Code
  // main
main: {
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // main::@1
  __b1:
    // for(char i=0;i<5;i++)
    // [2] if(main::i#2<5) goto main::@2 -- vbuxx_lt_vbuc1_then_la1 
    cpx #5
    bcc __b2
    // main::@return
    // }
    // [3] return 
    rts
    // main::@2
  __b2:
    // x[i] += ( v[i] += 5 )
    // [4] main::$2 = main::i#2 << 1 -- vbuyy=vbuxx_rol_1 
    txa
    asl
    tay
    // v[i] += 5
    // [5] v[main::$2] = v[main::$2] + 5 -- pwuc1_derefidx_vbuyy=pwuc1_derefidx_vbuyy_plus_vbuc2 
    clc
    lda v,y
    adc #5
    sta v,y
    lda v+1,y
    adc #0
    sta v+1,y
    // x[i] += ( v[i] += 5 )
    // [6] x[main::$2] = x[main::$2] + v[main::$2] -- pwuc1_derefidx_vbuyy=pwuc1_derefidx_vbuyy_plus_pwuc2_derefidx_vbuyy 
    clc
    lda x,y
    adc v,y
    sta x,y
    lda x+1,y
    adc v+1,y
    sta x+1,y
    // for(char i=0;i<5;i++)
    // [7] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx 
    inx
    // [1] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
    // [1] phi main::i#2 = main::i#1 [phi:main::@2->main::@1#0] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  v: .fill 2*5, 0
  x: .fill 2*5, 0

