Fixing pointer addition (word*~) bsearch16u::$7 ← (word*) bsearch16u::items + (byte~) bsearch16u::$6 Fixing pointer addition (word*~) bsearch16u::$15 ← (word*) bsearch16u::pivot + (number) 1 Fixing pointer addition (word*~) bsearch16u::$1 ← (word*) bsearch16u::items - (number) 1 Identified constant variable (byte*) HEAP_START Culled Empty Block (label) malloc::@1 Culled Empty Block (label) @1 Culled Empty Block (label) @2 Culled Empty Block (label) main::@2 CONTROL FLOW GRAPH SSA @begin: scope:[] from (byte*) HEAP_START#0 ← ((byte*)) (number) $c000 (byte*) heap_head#0 ← (byte*) HEAP_START#0 to:@3 malloc: scope:[malloc] from @3 (word) malloc::size#1 ← phi( @3/(word) malloc::size#0 ) (byte*) heap_head#4 ← phi( @3/(byte*) heap_head#7 ) (byte*) malloc::mem#0 ← (byte*) heap_head#4 (byte*) heap_head#1 ← (byte*) heap_head#4 + (word) malloc::size#1 (void*) malloc::return#0 ← ((void*)) (byte*) malloc::mem#0 to:malloc::@return malloc::@return: scope:[malloc] from malloc (byte*) heap_head#5 ← phi( malloc/(byte*) heap_head#1 ) (void*) malloc::return#3 ← phi( malloc/(void*) malloc::return#0 ) (void*) malloc::return#1 ← (void*) malloc::return#3 (byte*) heap_head#2 ← (byte*) heap_head#5 return to:@return @3: scope:[] from @begin (byte*) heap_head#7 ← phi( @begin/(byte*) heap_head#0 ) (word) malloc::size#0 ← (number) $100 call malloc (void*) malloc::return#2 ← (void*) malloc::return#1 to:@5 @5: scope:[] from @3 (byte*) heap_head#6 ← phi( @3/(byte*) heap_head#2 ) (void*) malloc::return#4 ← phi( @3/(void*) malloc::return#2 ) (void*~) $0 ← (void*) malloc::return#4 (byte*) heap_head#3 ← (byte*) heap_head#6 (byte*) BYTES#0 ← ((byte*)) (void*~) $0 to:@4 main: scope:[main] from @4 (byte*) BYTES#2 ← phi( @4/(byte*) BYTES#3 ) (byte) main::i#0 ← (byte) 0 to:main::@1 main::@1: scope:[main] from main main::@1 (byte*) BYTES#1 ← phi( main/(byte*) BYTES#2 main::@1/(byte*) BYTES#1 ) (byte) main::i#2 ← phi( main/(byte) main::i#0 main::@1/(byte) main::i#1 ) *((byte*) BYTES#1 + (byte) main::i#2) ← (byte) main::i#2 (byte) main::i#1 ← (byte) main::i#2 + rangenext(0,$ff) (bool~) main::$0 ← (byte) main::i#1 != rangelast(0,$ff) if((bool~) main::$0) goto main::@1 to:main::@return main::@return: scope:[main] from main::@1 return to:@return @4: scope:[] from @5 (byte*) BYTES#3 ← phi( @5/(byte*) BYTES#0 ) call main to:@6 @6: scope:[] from @4 to:@end @end: scope:[] from @6 SYMBOL TABLE SSA (void*~) $0 (label) @3 (label) @4 (label) @5 (label) @6 (label) @begin (label) @end (byte*) BYTES (byte*) BYTES#0 (byte*) BYTES#1 (byte*) BYTES#2 (byte*) BYTES#3 (byte*) HEAP_START (byte*) HEAP_START#0 (byte*) heap_head (byte*) heap_head#0 (byte*) heap_head#1 (byte*) heap_head#2 (byte*) heap_head#3 (byte*) heap_head#4 (byte*) heap_head#5 (byte*) heap_head#6 (byte*) heap_head#7 (void()) main() (bool~) main::$0 (label) main::@1 (label) main::@return (byte) main::i (byte) main::i#0 (byte) main::i#1 (byte) main::i#2 (void*()) malloc((word) malloc::size) (label) malloc::@return (byte*) malloc::mem (byte*) malloc::mem#0 (void*) malloc::return (void*) malloc::return#0 (void*) malloc::return#1 (void*) malloc::return#2 (void*) malloc::return#3 (void*) malloc::return#4 (word) malloc::size (word) malloc::size#0 (word) malloc::size#1 Adding number conversion cast (unumber) $100 in (word) malloc::size#0 ← (number) $100 Successful SSA optimization PassNAddNumberTypeConversions Inlining cast (byte*) HEAP_START#0 ← (byte*)(number) $c000 Inlining cast (void*) malloc::return#0 ← (void*)(byte*) malloc::mem#0 Inlining cast (word) malloc::size#0 ← (unumber)(number) $100 Inlining cast (byte*) BYTES#0 ← (byte*)(void*~) $0 Successful SSA optimization Pass2InlineCast Simplifying constant pointer cast (byte*) 49152 Simplifying constant integer cast $100 Successful SSA optimization PassNCastSimplification Finalized unsigned number type (word) $100 Successful SSA optimization PassNFinalizeNumberTypeConversions Alias (byte*) HEAP_START#0 = (byte*) heap_head#0 (byte*) heap_head#7 Alias (void*) malloc::return#0 = (void*) malloc::return#3 (void*) malloc::return#1 Alias (byte*) heap_head#1 = (byte*) heap_head#5 (byte*) heap_head#2 Alias (void*) malloc::return#2 = (void*) malloc::return#4 Alias (byte*) heap_head#3 = (byte*) heap_head#6 Alias (byte*) BYTES#0 = (byte*) BYTES#3 Successful SSA optimization Pass2AliasElimination Self Phi Eliminated (byte*) BYTES#1 Successful SSA optimization Pass2SelfPhiElimination Identical Phi Values (byte*) heap_head#4 (byte*) HEAP_START#0 Identical Phi Values (word) malloc::size#1 (word) malloc::size#0 Identical Phi Values (byte*) heap_head#3 (byte*) heap_head#1 Identical Phi Values (byte*) BYTES#2 (byte*) BYTES#0 Identical Phi Values (byte*) BYTES#1 (byte*) BYTES#2 Successful SSA optimization Pass2IdenticalPhiElimination Simple Condition (bool~) main::$0 [24] if((byte) main::i#1!=rangelast(0,$ff)) goto main::@1 Successful SSA optimization Pass2ConditionalJumpSimplification Constant (const byte*) HEAP_START#0 = (byte*) 49152 Constant (const word) malloc::size#0 = $100 Constant (const byte) main::i#0 = 0 Successful SSA optimization Pass2ConstantIdentification Constant (const byte*) malloc::mem#0 = HEAP_START#0 Successful SSA optimization Pass2ConstantIdentification Constant value identified (void*)malloc::mem#0 in [5] (void*) malloc::return#0 ← (void*)(const byte*) malloc::mem#0 Successful SSA optimization Pass2ConstantValues Resolved ranged next value [22] main::i#1 ← ++ main::i#2 to ++ Resolved ranged comparison value [24] if(main::i#1!=rangelast(0,$ff)) goto main::@1 to (number) 0 Eliminating unused variable (byte*) heap_head#1 and assignment [0] (byte*) heap_head#1 ← (const byte*) HEAP_START#0 + (const word) malloc::size#0 Successful SSA optimization PassNEliminateUnusedVars Eliminating unused constant (const word) malloc::size#0 Successful SSA optimization PassNEliminateUnusedVars Adding number conversion cast (unumber) 0 in if((byte) main::i#1!=(number) 0) goto main::@1 Successful SSA optimization PassNAddNumberTypeConversions Simplifying constant integer cast 0 Successful SSA optimization PassNCastSimplification Finalized unsigned number type (byte) 0 Successful SSA optimization PassNFinalizeNumberTypeConversions Constant (const void*) malloc::return#0 = (void*)malloc::mem#0 Successful SSA optimization Pass2ConstantIdentification Constant (const void*) malloc::return#2 = malloc::return#0 Successful SSA optimization Pass2ConstantIdentification Constant (const void*) $0 = malloc::return#2 Successful SSA optimization Pass2ConstantIdentification Constant value identified (byte*)$0 in [5] (byte*) BYTES#0 ← (byte*)(const void*) $0 Successful SSA optimization Pass2ConstantValues Constant (const byte*) BYTES#0 = (byte*)$0 Successful SSA optimization Pass2ConstantIdentification Inlining constant with different constant siblings (const void*) malloc::return#2 Inlining constant with var siblings (const byte) main::i#0 Constant inlined main::i#0 = (byte) 0 Constant inlined malloc::return#2 = (const void*) malloc::return#0 Constant inlined malloc::mem#0 = (const byte*) HEAP_START#0 Constant inlined $0 = (const void*) malloc::return#0 Successful SSA optimization Pass2ConstantInlining Added new block during phi lifting main::@3(between main::@1 and main::@1) Adding NOP phi() at start of @begin Adding NOP phi() at start of @3 Adding NOP phi() at start of @5 Adding NOP phi() at start of @4 Adding NOP phi() at start of @6 Adding NOP phi() at start of @end Adding NOP phi() at start of main Adding NOP phi() at start of malloc CALL GRAPH Calls in [] to malloc:2 main:5 Created 1 initial phi equivalence classes Coalesced [14] main::i#3 ← main::i#1 Coalesced down to 1 phi equivalence classes Culled Empty Block (label) @5 Culled Empty Block (label) @6 Culled Empty Block (label) main::@3 Renumbering block @3 to @1 Renumbering block @4 to @2 Adding NOP phi() at start of @begin Adding NOP phi() at start of @1 Adding NOP phi() at start of @2 Adding NOP phi() at start of @end Adding NOP phi() at start of main Adding NOP phi() at start of malloc FINAL CONTROL FLOW GRAPH @begin: scope:[] from [0] phi() to:@1 @1: scope:[] from @begin [1] phi() [2] call malloc to:@2 @2: scope:[] from @1 [3] phi() [4] call main to:@end @end: scope:[] from @2 [5] phi() main: scope:[main] from @2 [6] phi() to:main::@1 main::@1: scope:[main] from main main::@1 [7] (byte) main::i#2 ← phi( main/(byte) 0 main::@1/(byte) main::i#1 ) [8] *((const byte*) BYTES#0 + (byte) main::i#2) ← (byte) main::i#2 [9] (byte) main::i#1 ← ++ (byte) main::i#2 [10] if((byte) main::i#1!=(byte) 0) goto main::@1 to:main::@return main::@return: scope:[main] from main::@1 [11] return to:@return malloc: scope:[malloc] from @1 [12] phi() to:malloc::@return malloc::@return: scope:[malloc] from malloc [13] return to:@return VARIABLE REGISTER WEIGHTS (byte*) BYTES (byte*) HEAP_START (byte*) heap_head (void()) main() (byte) main::i (byte) main::i#1 16.5 (byte) main::i#2 22.0 (void*()) malloc((word) malloc::size) (byte*) malloc::mem (void*) malloc::return (word) malloc::size Initial phi equivalence classes [ main::i#2 main::i#1 ] Complete equivalence classes [ main::i#2 main::i#1 ] Allocated zp ZP_BYTE:2 [ main::i#2 main::i#1 ] INITIAL ASM // File Comments // Experiments with malloc() - a byte array // Basic Upstart .pc = $801 "Basic" :BasicUpstart(bbegin) .pc = $80d "Program" // Global Constants & labels // Start of the heap used by malloc() .label HEAP_START = $c000 .label BYTES = malloc.return // @begin bbegin: // [1] phi from @begin to @1 [phi:@begin->@1] b1_from_bbegin: jmp b1 // @1 b1: // [2] call malloc // [12] phi from @1 to malloc [phi:@1->malloc] malloc_from_b1: jsr malloc // [3] phi from @1 to @2 [phi:@1->@2] b2_from_b1: jmp b2 // @2 b2: // [4] call main // [6] phi from @2 to main [phi:@2->main] main_from_b2: jsr main // [5] phi from @2 to @end [phi:@2->@end] bend_from_b2: jmp bend // @end bend: // main main: { .label i = 2 // [7] phi from main to main::@1 [phi:main->main::@1] b1_from_main: // [7] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuz1=vbuc1 lda #0 sta i jmp b1 // [7] phi from main::@1 to main::@1 [phi:main::@1->main::@1] b1_from_b1: // [7] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy jmp b1 // main::@1 b1: // [8] *((const byte*) BYTES#0 + (byte) main::i#2) ← (byte) main::i#2 -- pbuc1_derefidx_vbuz1=vbuz1 ldy i tya sta BYTES,y // [9] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuz1=_inc_vbuz1 inc i // [10] if((byte) main::i#1!=(byte) 0) goto main::@1 -- vbuz1_neq_0_then_la1 lda i cmp #0 bne b1_from_b1 jmp breturn // main::@return breturn: // [11] return rts } // malloc // Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. // The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. malloc: { .label return = HEAP_START jmp breturn // malloc::@return breturn: // [13] return rts } // File Data REGISTER UPLIFT POTENTIAL REGISTERS Potential registers zp ZP_BYTE:2 [ main::i#2 main::i#1 ] : zp ZP_BYTE:2 , reg byte a , reg byte x , reg byte y , REGISTER UPLIFT SCOPES Uplift Scope [main] 38.5: zp ZP_BYTE:2 [ main::i#2 main::i#1 ] Uplift Scope [malloc] Uplift Scope [] Uplifting [main] best 308 combination reg byte x [ main::i#2 main::i#1 ] Uplifting [malloc] best 308 combination Uplifting [] best 308 combination ASSEMBLER BEFORE OPTIMIZATION // File Comments // Experiments with malloc() - a byte array // Basic Upstart .pc = $801 "Basic" :BasicUpstart(bbegin) .pc = $80d "Program" // Global Constants & labels // Start of the heap used by malloc() .label HEAP_START = $c000 .label BYTES = malloc.return // @begin bbegin: // [1] phi from @begin to @1 [phi:@begin->@1] b1_from_bbegin: jmp b1 // @1 b1: // [2] call malloc // [12] phi from @1 to malloc [phi:@1->malloc] malloc_from_b1: jsr malloc // [3] phi from @1 to @2 [phi:@1->@2] b2_from_b1: jmp b2 // @2 b2: // [4] call main // [6] phi from @2 to main [phi:@2->main] main_from_b2: jsr main // [5] phi from @2 to @end [phi:@2->@end] bend_from_b2: jmp bend // @end bend: // main main: { // [7] phi from main to main::@1 [phi:main->main::@1] b1_from_main: // [7] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 ldx #0 jmp b1 // [7] phi from main::@1 to main::@1 [phi:main::@1->main::@1] b1_from_b1: // [7] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy jmp b1 // main::@1 b1: // [8] *((const byte*) BYTES#0 + (byte) main::i#2) ← (byte) main::i#2 -- pbuc1_derefidx_vbuxx=vbuxx txa sta BYTES,x // [9] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx inx // [10] if((byte) main::i#1!=(byte) 0) goto main::@1 -- vbuxx_neq_0_then_la1 cpx #0 bne b1_from_b1 jmp breturn // main::@return breturn: // [11] return rts } // malloc // Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. // The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. malloc: { .label return = HEAP_START jmp breturn // malloc::@return breturn: // [13] return rts } // File Data ASSEMBLER OPTIMIZATIONS Removing instruction jmp b1 Removing instruction jmp b2 Removing instruction jmp bend Removing instruction jmp b1 Removing instruction jmp breturn Removing instruction jmp breturn Succesful ASM optimization Pass5NextJumpElimination Replacing label b1_from_b1 with b1 Removing instruction b1_from_bbegin: Removing instruction b1: Removing instruction malloc_from_b1: Removing instruction b2_from_b1: Removing instruction main_from_b2: Removing instruction bend_from_b2: Removing instruction b1_from_b1: Succesful ASM optimization Pass5RedundantLabelElimination Removing instruction b2: Removing instruction bend: Removing instruction b1_from_main: Removing instruction breturn: Removing instruction breturn: Succesful ASM optimization Pass5UnusedLabelElimination Adding RTS to root block Succesful ASM optimization Pass5AddMainRts Removing instruction jmp b1 Succesful ASM optimization Pass5NextJumpElimination FINAL SYMBOL TABLE (label) @1 (label) @2 (label) @begin (label) @end (byte*) BYTES (const byte*) BYTES#0 BYTES = (byte*)(const void*) malloc::return#0 (byte*) HEAP_START (const byte*) HEAP_START#0 HEAP_START = (byte*) 49152 (byte*) heap_head (void()) main() (label) main::@1 (label) main::@return (byte) main::i (byte) main::i#1 reg byte x 16.5 (byte) main::i#2 reg byte x 22.0 (void*()) malloc((word) malloc::size) (label) malloc::@return (byte*) malloc::mem (void*) malloc::return (const void*) malloc::return#0 return = (void*)(const byte*) HEAP_START#0 (word) malloc::size reg byte x [ main::i#2 main::i#1 ] FINAL ASSEMBLER Score: 185 // File Comments // Experiments with malloc() - a byte array // Basic Upstart .pc = $801 "Basic" :BasicUpstart(bbegin) .pc = $80d "Program" // Global Constants & labels // Start of the heap used by malloc() .label HEAP_START = $c000 .label BYTES = malloc.return // @begin bbegin: // [1] phi from @begin to @1 [phi:@begin->@1] // @1 // malloc(0x100) // [2] call malloc // [12] phi from @1 to malloc [phi:@1->malloc] jsr malloc // [3] phi from @1 to @2 [phi:@1->@2] // @2 // [4] call main // [6] phi from @2 to main [phi:@2->main] jsr main rts // [5] phi from @2 to @end [phi:@2->@end] // @end // main main: { // [7] phi from main to main::@1 [phi:main->main::@1] // [7] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 ldx #0 // [7] phi from main::@1 to main::@1 [phi:main::@1->main::@1] // [7] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy // main::@1 b1: // BYTES[i] = i // [8] *((const byte*) BYTES#0 + (byte) main::i#2) ← (byte) main::i#2 -- pbuc1_derefidx_vbuxx=vbuxx txa sta BYTES,x // for( byte i: 0..255) // [9] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx inx // [10] if((byte) main::i#1!=(byte) 0) goto main::@1 -- vbuxx_neq_0_then_la1 cpx #0 bne b1 // main::@return // } // [11] return rts } // malloc // Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. // The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. malloc: { .label return = HEAP_START // malloc::@return // [13] return rts } // File Data