
CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  main::idx#0 = 0
  main::$0 = sizeof  settings
  main::$1 = main::$0 / SIZEOF_STRUCT_SETTING
  main::len#0 = (char)main::$1
  main::setting#0 = settings
  to:main::@1
main::@1: scope:[main]  from main main::@3
  main::idx#4 = phi( main/main::idx#0, main::@3/main::idx#5 )
  main::setting#2 = phi( main/main::setting#0, main::@3/main::setting#1 )
  main::len#1 = phi( main/main::len#0, main::@3/main::len#2 )
  main::$6 = main::len#1 * SIZEOF_STRUCT_SETTING
  main::$2 = settings + main::$6
  main::$3 = main::setting#2 < main::$2
  if(main::$3) goto main::@2
  to:main::@return
main::@2: scope:[main]  from main::@1
  main::len#3 = phi( main::@1/main::len#1 )
  main::idx#3 = phi( main::@1/main::idx#4 )
  main::setting#3 = phi( main::@1/main::setting#2 )
  main::$9 = (char *)main::setting#3
  main::$7 = main::$9 + OFFSET_STRUCT_SETTING_OFF
  main::$11 = 0 != *main::$7
  main::$4 = ! main::$11
  main::$5 = ! main::$4
  if(main::$5) goto main::@3
  to:main::@4
main::@3: scope:[main]  from main::@2 main::@4
  main::idx#5 = phi( main::@2/main::idx#3, main::@4/main::idx#1 )
  main::len#2 = phi( main::@2/main::len#3, main::@4/main::len#4 )
  main::setting#4 = phi( main::@2/main::setting#3, main::@4/main::setting#5 )
  main::setting#1 = main::setting#4 + SIZEOF_STRUCT_SETTING
  to:main::@1
main::@4: scope:[main]  from main::@2
  main::len#4 = phi( main::@2/main::len#3 )
  main::idx#2 = phi( main::@2/main::idx#3 )
  main::setting#5 = phi( main::@2/main::setting#3 )
  main::$10 = (char *)main::setting#5
  main::$8 = main::$10 + OFFSET_STRUCT_SETTING_ID
  SCREEN[main::idx#2] = *main::$8
  main::idx#1 = ++ main::idx#2
  to:main::@3
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 OFFSET_STRUCT_SETTING_ID = 1
__constant char OFFSET_STRUCT_SETTING_OFF = 0
__constant char * const SCREEN = (char *)$400
__constant char SIZEOF_STRUCT_SETTING = 2
void __start()
void main()
unsigned int main::$0
unsigned int main::$1
char *main::$10
bool main::$11
const struct Setting *main::$2
bool main::$3
bool main::$4
bool main::$5
char main::$6
char *main::$7
char *main::$8
char *main::$9
char main::idx
char main::idx#0
char main::idx#1
char main::idx#2
char main::idx#3
char main::idx#4
char main::idx#5
char main::len
char main::len#0
char main::len#1
char main::len#2
char main::len#3
char main::len#4
struct Setting *main::setting
struct Setting *main::setting#0
struct Setting *main::setting#1
struct Setting *main::setting#2
struct Setting *main::setting#3
struct Setting *main::setting#4
struct Setting *main::setting#5
__constant const struct Setting settings[] = { { off: 0, id: 'a' }, { off: 1, id: 'b' }, { off: 0, id: 'c' } }

Adding number conversion cast (unumber) 0 in main::$11 = 0 != *main::$7
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast 0
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inversing boolean not [14] main::$4 = 0 == *main::$7 from [13] main::$11 = 0 != *main::$7
Inversing boolean not [15] main::$5 = 0 != *main::$7 from [14] main::$4 = 0 == *main::$7
Successful SSA optimization Pass2UnaryNotSimplification
Alias main::setting#2 = main::setting#3 main::setting#5 
Alias main::idx#2 = main::idx#3 main::idx#4 
Alias main::len#1 = main::len#3 main::len#4 
Successful SSA optimization Pass2AliasElimination
Alias main::setting#2 = main::setting#4 
Alias main::len#1 = main::len#2 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values main::len#1 main::len#0
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition main::$3 [9] if(main::setting#2<main::$2) goto main::@2
Simple Condition main::$5 [13] if(0!=*main::$7) goto main::@3
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant right-side identified [1] main::$0 = sizeof  settings
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant main::idx#0 = 0
Constant main::$0 = sizeof settings
Constant main::setting#0 = settings
Successful SSA optimization Pass2ConstantIdentification
Converting *(pointer+n) to pointer[n] [13] if(0!=*main::$7) goto main::@3  -- main::$9[OFFSET_STRUCT_SETTING_OFF]
Converting *(pointer+n) to pointer[n] [18] SCREEN[main::idx#2] = *main::$8  -- main::$10[OFFSET_STRUCT_SETTING_ID]
Successful SSA optimization Pass2InlineDerefIdx
Simplifying expression containing zero main::$9 in [11] main::$7 = main::$9 + OFFSET_STRUCT_SETTING_OFF
Simplifying expression containing zero main::$9 in [13] if(0!=main::$9[OFFSET_STRUCT_SETTING_OFF]) goto main::@3
Successful SSA optimization PassNSimplifyExpressionWithZero
Eliminating unused variable main::$7 and assignment [7] main::$7 = main::$9
Eliminating unused variable main::$8 and assignment [12] main::$8 = main::$10 + OFFSET_STRUCT_SETTING_ID
Eliminating unused constant OFFSET_STRUCT_SETTING_OFF
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
Resolving array sizeof() sizeof settings
Successful SSA optimization PassNSizeOfSimplification
Constant right-side identified [0] main::$1 = main::$0 / SIZEOF_STRUCT_SETTING
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant main::$1 = main::$0/SIZEOF_STRUCT_SETTING
Successful SSA optimization Pass2ConstantIdentification
Constant main::len#0 = (char)main::$1
Successful SSA optimization Pass2ConstantIdentification
Adding number conversion cast (unumber) 3 in 
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast 3
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 3
Successful SSA optimization PassNFinalizeNumberTypeConversions
Constant right-side identified [1] main::$6 = main::len#0 * SIZEOF_STRUCT_SETTING
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant main::$6 = main::len#0*SIZEOF_STRUCT_SETTING
Successful SSA optimization Pass2ConstantIdentification
Constant right-side identified [1] main::$2 = settings + main::$6
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant main::$2 = settings+main::$6
Successful SSA optimization Pass2ConstantIdentification
Inlining Noop Cast [2] main::$9 = (char *)main::setting#2 keeping main::setting#2
Inlining Noop Cast [6] main::$10 = (char *)main::setting#2 keeping main::setting#2
Successful SSA optimization Pass2NopCastInlining
Inlining constant with var siblings main::idx#0
Inlining constant with var siblings main::setting#0
Constant inlined main::$6 = main::len#0*SIZEOF_STRUCT_SETTING
Constant inlined main::idx#0 = 0
Constant inlined main::$1 = 3*SIZEOF_STRUCT_SETTING/SIZEOF_STRUCT_SETTING
Constant inlined main::setting#0 = settings
Constant inlined main::$2 = settings+main::len#0*SIZEOF_STRUCT_SETTING
Constant inlined main::$0 = 3*SIZEOF_STRUCT_SETTING
Successful SSA optimization Pass2ConstantInlining
Simplifying constant integer cast 3*SIZEOF_STRUCT_SETTING/SIZEOF_STRUCT_SETTING
Successful SSA optimization PassNCastSimplification
Added new block during phi lifting main::@5(between main::@2 and main::@3)
Adding NOP phi() at start of main
CALL GRAPH

Created 3 initial phi equivalence classes
Coalesced [7] main::idx#8 = main::idx#1
Coalesced [10] main::setting#6 = main::setting#1
Coalesced [11] main::idx#6 = main::idx#5
Coalesced (already) [12] main::idx#7 = main::idx#2
Coalesced down to 2 phi equivalence classes
Culled Empty Block label main::@5
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::@3
  [1] main::idx#2 = phi( main/0, main::@3/main::idx#5 )
  [1] main::setting#2 = phi( main/settings, main::@3/main::setting#1 )
  [2] if(main::setting#2<settings+main::len#0*SIZEOF_STRUCT_SETTING) 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] if(0!=*((char *)main::setting#2)) goto main::@3
  to:main::@4
main::@4: scope:[main]  from main::@2
  [5] SCREEN[main::idx#2] = ((char *)main::setting#2)[OFFSET_STRUCT_SETTING_ID]
  [6] main::idx#1 = ++ main::idx#2
  to:main::@3
main::@3: scope:[main]  from main::@2 main::@4
  [7] main::idx#5 = phi( main::@2/main::idx#2, main::@4/main::idx#1 )
  [8] main::setting#1 = main::setting#2 + SIZEOF_STRUCT_SETTING
  to:main::@1


VARIABLE REGISTER WEIGHTS
void main()
char main::idx
char main::idx#1 // 22.0
char main::idx#2 // 11.0
char main::idx#5 // 16.5
char main::len
struct Setting *main::setting
struct Setting *main::setting#1 // 22.0
struct Setting *main::setting#2 // 5.5

Initial phi equivalence classes
[ main::setting#2 main::setting#1 ]
[ main::idx#2 main::idx#5 main::idx#1 ]
Complete equivalence classes
[ main::setting#2 main::setting#1 ]
[ main::idx#2 main::idx#5 main::idx#1 ]
Allocated zp[1]:2 [ main::idx#2 main::idx#5 main::idx#1 ]
Allocated zp[2]:3 [ main::setting#2 main::setting#1 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [2] if(main::setting#2<settings+main::len#0*SIZEOF_STRUCT_SETTING) goto main::@2 [ main::setting#2 main::idx#2 ] (  [ main::setting#2 main::idx#2 ] { }  ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::idx#2 main::idx#5 main::idx#1 ]
Statement [4] if(0!=*((char *)main::setting#2)) goto main::@3 [ main::setting#2 main::idx#2 ] (  [ main::setting#2 main::idx#2 ] { }  ) always clobbers reg byte a reg byte y 
Removing always clobbered register reg byte y as potential for zp[1]:2 [ main::idx#2 main::idx#5 main::idx#1 ]
Statement [5] SCREEN[main::idx#2] = ((char *)main::setting#2)[OFFSET_STRUCT_SETTING_ID] [ main::setting#2 main::idx#2 ] (  [ main::setting#2 main::idx#2 ] { }  ) always clobbers reg byte a reg byte y 
Statement [8] main::setting#1 = main::setting#2 + SIZEOF_STRUCT_SETTING [ main::setting#1 main::idx#5 ] (  [ main::setting#1 main::idx#5 ] { }  ) always clobbers reg byte a 
Statement [2] if(main::setting#2<settings+main::len#0*SIZEOF_STRUCT_SETTING) goto main::@2 [ main::setting#2 main::idx#2 ] (  [ main::setting#2 main::idx#2 ] { }  ) always clobbers reg byte a 
Statement [4] if(0!=*((char *)main::setting#2)) goto main::@3 [ main::setting#2 main::idx#2 ] (  [ main::setting#2 main::idx#2 ] { }  ) always clobbers reg byte a reg byte y 
Statement [5] SCREEN[main::idx#2] = ((char *)main::setting#2)[OFFSET_STRUCT_SETTING_ID] [ main::setting#2 main::idx#2 ] (  [ main::setting#2 main::idx#2 ] { }  ) always clobbers reg byte a reg byte y 
Statement [8] main::setting#1 = main::setting#2 + SIZEOF_STRUCT_SETTING [ main::setting#1 main::idx#5 ] (  [ main::setting#1 main::idx#5 ] { }  ) always clobbers reg byte a 
Potential registers zp[2]:3 [ main::setting#2 main::setting#1 ] : zp[2]:3 , 
Potential registers zp[1]:2 [ main::idx#2 main::idx#5 main::idx#1 ] : zp[1]:2 , reg byte x , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 49.5: zp[1]:2 [ main::idx#2 main::idx#5 main::idx#1 ] 27.5: zp[2]:3 [ main::setting#2 main::setting#1 ] 
Uplift Scope [Setting] 
Uplift Scope [] 

Uplifting [main] best 891 combination reg byte x [ main::idx#2 main::idx#5 main::idx#1 ] zp[2]:3 [ main::setting#2 main::setting#1 ] 
Uplifting [Setting] best 891 combination 
Uplifting [] best 891 combination 
Allocated (was zp[2]:3) zp[2]:2 [ main::setting#2 main::setting#1 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Demonstrates problem with conditions using negated struct references
  // Upstart
  // Commodore 64 PRG executable file
.file [name="struct-ptr-20.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_STRUCT_SETTING = 2
  .const OFFSET_STRUCT_SETTING_ID = 1
  .label SCREEN = $400
.segment Code
  // main
main: {
    .const len = 3*SIZEOF_STRUCT_SETTING/SIZEOF_STRUCT_SETTING
    .label setting = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
  __b1_from_main:
    // [1] phi main::idx#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi main::setting#2 = settings [phi:main->main::@1#1] -- pssz1=pssc1 
    lda #<settings
    sta.z setting
    lda #>settings
    sta.z setting+1
    jmp __b1
    // main::@1
  __b1:
    // [2] if(main::setting#2<settings+main::len#0*SIZEOF_STRUCT_SETTING) goto main::@2 -- pssz1_lt_pssc1_then_la1 
    lda.z setting+1
    cmp #>settings+len*SIZEOF_STRUCT_SETTING
    bcc __b2
    bne !+
    lda.z setting
    cmp #<settings+len*SIZEOF_STRUCT_SETTING
    bcc __b2
  !:
    jmp __breturn
    // main::@return
  __breturn:
    // [3] return 
    rts
    // main::@2
  __b2:
    // [4] if(0!=*((char *)main::setting#2)) goto main::@3 -- 0_neq__deref_pbuz1_then_la1 
    ldy #0
    lda (setting),y
    cmp #0
    bne __b3_from___b2
    jmp __b4
    // main::@4
  __b4:
    // [5] SCREEN[main::idx#2] = ((char *)main::setting#2)[OFFSET_STRUCT_SETTING_ID] -- pbuc1_derefidx_vbuxx=pbuz1_derefidx_vbuc2 
    ldy #OFFSET_STRUCT_SETTING_ID
    lda (setting),y
    sta SCREEN,x
    // [6] main::idx#1 = ++ main::idx#2 -- vbuxx=_inc_vbuxx 
    inx
    // [7] phi from main::@2 main::@4 to main::@3 [phi:main::@2/main::@4->main::@3]
  __b3_from___b2:
  __b3_from___b4:
    // [7] phi main::idx#5 = main::idx#2 [phi:main::@2/main::@4->main::@3#0] -- register_copy 
    jmp __b3
    // main::@3
  __b3:
    // [8] main::setting#1 = main::setting#2 + SIZEOF_STRUCT_SETTING -- pssz1=pssz1_plus_vbuc1 
    lda #SIZEOF_STRUCT_SETTING
    clc
    adc.z setting
    sta.z setting
    bcc !+
    inc.z setting+1
  !:
    // [1] phi from main::@3 to main::@1 [phi:main::@3->main::@1]
  __b1_from___b3:
    // [1] phi main::idx#2 = main::idx#5 [phi:main::@3->main::@1#0] -- register_copy 
    // [1] phi main::setting#2 = main::setting#1 [phi:main::@3->main::@1#1] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  settings: .byte 0, 'a', 1, 'b', 0, 'c'

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __b4
Removing instruction jmp __b3
Succesful ASM optimization Pass5NextJumpElimination
Replacing label __b3_from___b2 with __b3
Removing instruction __b3_from___b2:
Removing instruction __b3_from___b4:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __b1_from_main:
Removing instruction __breturn:
Removing instruction __b4:
Removing instruction __b1_from___b3:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
__constant char OFFSET_STRUCT_SETTING_ID = 1
__constant char * const SCREEN = (char *) 1024
__constant char SIZEOF_STRUCT_SETTING = 2
void main()
char main::idx
char main::idx#1 // reg byte x 22.0
char main::idx#2 // reg byte x 11.0
char main::idx#5 // reg byte x 16.5
char main::len
__constant char main::len#0 = 3*SIZEOF_STRUCT_SETTING/SIZEOF_STRUCT_SETTING // len
struct Setting *main::setting
struct Setting *main::setting#1 // setting zp[2]:2 22.0
struct Setting *main::setting#2 // setting zp[2]:2 5.5
__constant const struct Setting settings[] = { { off: 0, id: 'a' }, { off: 1, id: 'b' }, { off: 0, id: 'c' } }

zp[2]:2 [ main::setting#2 main::setting#1 ]
reg byte x [ main::idx#2 main::idx#5 main::idx#1 ]


FINAL ASSEMBLER
Score: 771

  // File Comments
// Demonstrates problem with conditions using negated struct references
  // Upstart
  // Commodore 64 PRG executable file
.file [name="struct-ptr-20.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_STRUCT_SETTING = 2
  .const OFFSET_STRUCT_SETTING_ID = 1
  .label SCREEN = $400
.segment Code
  // main
main: {
    .const len = 3*SIZEOF_STRUCT_SETTING/SIZEOF_STRUCT_SETTING
    .label setting = 2
    // [1] phi from main to main::@1 [phi:main->main::@1]
    // [1] phi main::idx#2 = 0 [phi:main->main::@1#0] -- vbuxx=vbuc1 
    ldx #0
    // [1] phi main::setting#2 = settings [phi:main->main::@1#1] -- pssz1=pssc1 
    lda #<settings
    sta.z setting
    lda #>settings
    sta.z setting+1
    // main::@1
  __b1:
    // for(struct Setting* setting = settings; setting<settings+len; setting++)
    // [2] if(main::setting#2<settings+main::len#0*SIZEOF_STRUCT_SETTING) goto main::@2 -- pssz1_lt_pssc1_then_la1 
    lda.z setting+1
    cmp #>settings+len*SIZEOF_STRUCT_SETTING
    bcc __b2
    bne !+
    lda.z setting
    cmp #<settings+len*SIZEOF_STRUCT_SETTING
    bcc __b2
  !:
    // main::@return
    // }
    // [3] return 
    rts
    // main::@2
  __b2:
    // if (! setting->off)
    // [4] if(0!=*((char *)main::setting#2)) goto main::@3 -- 0_neq__deref_pbuz1_then_la1 
    ldy #0
    lda (setting),y
    cmp #0
    bne __b3
    // main::@4
    // SCREEN[idx++] = setting->id
    // [5] SCREEN[main::idx#2] = ((char *)main::setting#2)[OFFSET_STRUCT_SETTING_ID] -- pbuc1_derefidx_vbuxx=pbuz1_derefidx_vbuc2 
    ldy #OFFSET_STRUCT_SETTING_ID
    lda (setting),y
    sta SCREEN,x
    // SCREEN[idx++] = setting->id;
    // [6] main::idx#1 = ++ main::idx#2 -- vbuxx=_inc_vbuxx 
    inx
    // [7] phi from main::@2 main::@4 to main::@3 [phi:main::@2/main::@4->main::@3]
    // [7] phi main::idx#5 = main::idx#2 [phi:main::@2/main::@4->main::@3#0] -- register_copy 
    // main::@3
  __b3:
    // for(struct Setting* setting = settings; setting<settings+len; setting++)
    // [8] main::setting#1 = main::setting#2 + SIZEOF_STRUCT_SETTING -- pssz1=pssz1_plus_vbuc1 
    lda #SIZEOF_STRUCT_SETTING
    clc
    adc.z setting
    sta.z setting
    bcc !+
    inc.z setting+1
  !:
    // [1] phi from main::@3 to main::@1 [phi:main::@3->main::@1]
    // [1] phi main::idx#2 = main::idx#5 [phi:main::@3->main::@1#0] -- register_copy 
    // [1] phi main::setting#2 = main::setting#1 [phi:main::@3->main::@1#1] -- register_copy 
    jmp __b1
}
  // File Data
.segment Data
  settings: .byte 0, 'a', 1, 'b', 0, 'c'

