CONTROL FLOW GRAPH SSA void main() main: scope:[main] from __start fibs[0] = 0 fibs[1] = 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::$3 = fibs[main::i#3] + fibs[main::$2] fibs[main::$1] = 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 void __start() const byte* fibs[$19] = { fill( $19, 0) } void main() bool~ main::$0 number~ main::$1 number~ main::$2 byte~ main::$3 byte main::i byte main::i#0 byte main::i#1 byte main::i#2 byte main::i#3 Adding number conversion cast (unumber) 0 in fibs[0] = 0 Adding number conversion cast (unumber) 0 in fibs[0] = ((unumber)) 0 Adding number conversion cast (unumber) 1 in fibs[1] = 1 Adding number conversion cast (unumber) 1 in fibs[1] = ((unumber)) 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 Successful SSA optimization PassNAddNumberTypeConversions Inlining cast fibs[(unumber)0] = (unumber)0 Inlining cast fibs[(unumber)1] = (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 (byte) 0 Finalized unsigned number type (byte) 0 Finalized unsigned number type (byte) 1 Finalized unsigned number type (byte) 1 Finalized unsigned number type (byte) 2 Finalized unsigned number type (byte) 1 Successful SSA optimization PassNFinalizeNumberTypeConversions Inferred type updated to byte in main::$1 = main::i#3 + 2 Inferred type updated to byte in main::$2 = main::i#3 + 1 Alias main::i#2 = main::i#3 Successful SSA optimization Pass2AliasElimination Simple Condition main::$0 [5] if(main::i#2<(byte)$19-2) goto main::@2 Successful SSA optimization Pass2ConditionalJumpSimplification Constant main::i#0 = 0 Successful SSA optimization Pass2ConstantIdentification Simplifying expression containing zero fibs in [0] fibs[0] = 0 Successful SSA optimization PassNSimplifyExpressionWithZero 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 Inlining constant with var siblings main::i#0 Constant inlined main::i#0 = 0 Successful SSA optimization Pass2ConstantInlining Consolidated array index constant in *(fibs+1) Consolidated array index constant in assignment (fibs+1)[main::$2] Consolidated array index constant in assignment (fibs+2)[main::$1] Successful SSA optimization Pass2ConstantAdditionElimination Alias main::i#2 = main::$1 main::$2 Successful SSA optimization Pass2AliasElimination Finalized unsigned number type (byte) $19 Finalized unsigned number type (byte) $19 Finalized unsigned number type (byte) $19 Finalized unsigned number type (byte) 2 Successful SSA optimization PassNFinalizeNumberTypeConversions Simplifying constant integer cast $19-2 Successful SSA optimization PassNCastSimplification CALL GRAPH Created 1 initial phi equivalence classes Coalesced [8] 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) = 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::$3 = fibs[main::i#2] + (fibs+1)[main::i#2] [6] (fibs+2)[main::i#2] = main::$3 [7] main::i#1 = ++ main::i#2 to:main::@1 VARIABLE REGISTER WEIGHTS void main() byte~ main::$3 22.0 byte main::i byte main::i#1 22.0 byte main::i#2 16.5 Initial phi equivalence classes [ main::i#2 main::i#1 ] Added variable main::$3 to live range equivalence class [ main::$3 ] Complete equivalence classes [ main::i#2 main::i#1 ] [ main::$3 ] Allocated zp[1]:2 [ main::i#2 main::i#1 ] Allocated zp[1]:3 [ main::$3 ] REGISTER UPLIFT POTENTIAL REGISTERS Statement [0] *fibs = 0 [ ] ( [ ] { } ) always clobbers reg byte a Statement [1] *(fibs+1) = 1 [ ] ( [ ] { } ) always clobbers reg byte a Statement [5] main::$3 = fibs[main::i#2] + (fibs+1)[main::i#2] [ main::i#2 main::$3 ] ( [ main::i#2 main::$3 ] { } ) always clobbers reg byte a Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::i#2 main::i#1 ] Statement [0] *fibs = 0 [ ] ( [ ] { } ) always clobbers reg byte a Statement [1] *(fibs+1) = 1 [ ] ( [ ] { } ) always clobbers reg byte a Statement [5] main::$3 = fibs[main::i#2] + (fibs+1)[main::i#2] [ main::i#2 main::$3 ] ( [ main::i#2 main::$3 ] { } ) 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]:3 [ main::$3 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , REGISTER UPLIFT SCOPES Uplift Scope [main] 38.5: zp[1]:2 [ main::i#2 main::i#1 ] 22: zp[1]:3 [ main::$3 ] Uplift Scope [] Uplifting [main] best 353 combination reg byte x [ main::i#2 main::i#1 ] reg byte a [ main::$3 ] Uplifting [] best 353 combination ASSEMBLER BEFORE OPTIMIZATION // File Comments // Test array index pointer rewriting // 8bit array with 8bit index // Fibonacci calculation uses adjacent indices inside the loop // Upstart // Commodore 64 PRG executable file .file [name="index-pointer-rewrite-4.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: { // [0] *fibs = 0 -- _deref_pbuc1=vbuc2 lda #0 sta fibs // [1] *(fibs+1) = 1 -- _deref_pbuc1=vbuc2 lda #1 sta fibs+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] -- vbuxx=vbuc1 ldx #0 jmp __b1 // main::@1 __b1: // [3] if(main::i#2<$19-2) goto main::@2 -- vbuxx_lt_vbuc1_then_la1 cpx #$19-2 bcc __b2 jmp __breturn // main::@return __breturn: // [4] return rts // main::@2 __b2: // [5] main::$3 = fibs[main::i#2] + (fibs+1)[main::i#2] -- vbuaa=pbuc1_derefidx_vbuxx_plus_pbuc2_derefidx_vbuxx lda fibs,x clc adc fibs+1,x // [6] (fibs+2)[main::i#2] = main::$3 -- pbuc1_derefidx_vbuxx=vbuaa sta fibs+2,x // [7] main::i#1 = ++ main::i#2 -- vbuxx=_inc_vbuxx inx // [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 $19, 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 const byte* fibs[$19] = { fill( $19, 0) } void main() byte~ main::$3 reg byte a 22.0 byte main::i byte main::i#1 reg byte x 22.0 byte main::i#2 reg byte x 16.5 reg byte x [ main::i#2 main::i#1 ] reg byte a [ main::$3 ] FINAL ASSEMBLER Score: 293 // File Comments // Test array index pointer rewriting // 8bit array with 8bit index // Fibonacci calculation uses adjacent indices inside the loop // Upstart // Commodore 64 PRG executable file .file [name="index-pointer-rewrite-4.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: { // fibs[0] = 0 // [0] *fibs = 0 -- _deref_pbuc1=vbuc2 lda #0 sta fibs // fibs[1] = 1 // [1] *(fibs+1) = 1 -- _deref_pbuc1=vbuc2 lda #1 sta fibs+1 // [2] phi from main to main::@1 [phi:main->main::@1] // [2] phi main::i#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 ldx #0 // main::@1 __b1: // for(char i=0;imain::@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 $19, 0