
CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  (dword) DVAL#0 ← (number) $20000
  (byte*) SCREEN#0 ← ((byte*)) (number) $400
  to:@1
main: scope:[main]  from @1
  (number~) main::$0 ← (dword) DVAL#0 / (number) $400
  (word~) main::$1 ← ((word)) (number~) main::$0
  (byte~) main::$2 ← < (word~) main::$1
  *((byte*) SCREEN#0 + (number) 0) ← (byte~) main::$2
  (number~) main::$3 ← (dword) DVAL#0 / (number) $400
  (word~) main::$4 ← ((word)) (number~) main::$3
  (byte~) main::$5 ← > (word~) main::$4
  *((byte*) SCREEN#0 + (number) 1) ← (byte~) main::$5
  to:main::@return
main::@return: scope:[main]  from main
  return 
  to:@return
@1: scope:[]  from @begin
  call main 
  to:@2
@2: scope:[]  from @1
  to:@end
@end: scope:[]  from @2

SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(dword) DVAL
(dword) DVAL#0
(byte*) SCREEN
(byte*) SCREEN#0
(void()) main()
(number~) main::$0
(word~) main::$1
(byte~) main::$2
(number~) main::$3
(word~) main::$4
(byte~) main::$5
(label) main::@return

Adding number conversion cast (unumber) $20000 in (dword) DVAL#0 ← (number) $20000
Adding number conversion cast (unumber) $400 in (number~) main::$0 ← (dword) DVAL#0 / (number) $400
Adding number conversion cast (unumber) main::$0 in (number~) main::$0 ← (dword) DVAL#0 / (unumber)(number) $400
Adding number conversion cast (unumber) 0 in *((byte*) SCREEN#0 + (number) 0) ← (byte~) main::$2
Adding number conversion cast (unumber) $400 in (number~) main::$3 ← (dword) DVAL#0 / (number) $400
Adding number conversion cast (unumber) main::$3 in (number~) main::$3 ← (dword) DVAL#0 / (unumber)(number) $400
Adding number conversion cast (unumber) 1 in *((byte*) SCREEN#0 + (number) 1) ← (byte~) main::$5
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (dword) DVAL#0 ← (unumber)(number) $20000
Inlining cast (byte*) SCREEN#0 ← (byte*)(number) $400
Inlining cast (word~) main::$1 ← (word)(unumber~) main::$0
Inlining cast (word~) main::$4 ← (word)(unumber~) main::$3
Successful SSA optimization Pass2InlineCast
Simplifying constant integer cast $20000
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast $400
Simplifying constant integer cast 0
Simplifying constant integer cast $400
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (dword) $20000
Finalized unsigned number type (word) $400
Finalized unsigned number type (byte) 0
Finalized unsigned number type (word) $400
Finalized unsigned number type (byte) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to dword in (unumber~) main::$0 ← (dword) DVAL#0 / (word) $400
Inferred type updated to dword in (unumber~) main::$3 ← (dword) DVAL#0 / (word) $400
Identified duplicate assignment right side [6] (dword~) main::$3 ← (dword) DVAL#0 / (word) $400
Successful SSA optimization Pass2DuplicateRValueIdentification
Constant (const dword) DVAL#0 = $20000
Constant (const byte*) SCREEN#0 = (byte*) 1024
Successful SSA optimization Pass2ConstantIdentification
Simplifying expression containing zero SCREEN#0 in [5] *((const byte*) SCREEN#0 + (byte) 0) ← (byte~) main::$2
Successful SSA optimization PassNSimplifyExpressionWithZero
Alias (dword~) main::$3 = (dword~) main::$0 
Successful SSA optimization Pass2AliasElimination
Constant right-side identified [0] (dword~) main::$3 ← (const dword) DVAL#0 / (word) $400
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant (const dword) main::$3 = DVAL#0/$400
Successful SSA optimization Pass2ConstantIdentification
Constant value identified (word)main::$3 in [1] (word~) main::$1 ← (word)(const dword) main::$3
Constant value identified (word)main::$3 in [5] (word~) main::$4 ← (word)(const dword) main::$3
Successful SSA optimization Pass2ConstantValues
Constant (const word) main::$1 = (word)main::$3
Constant (const word) main::$4 = (word)main::$3
Successful SSA optimization Pass2ConstantIdentification
Constant right-side identified [0] (byte~) main::$2 ← < (const word) main::$1
Constant right-side identified [2] (byte~) main::$5 ← > (const word) main::$4
Successful SSA optimization Pass2ConstantRValueConsolidation
Constant (const byte) main::$2 = <main::$1
Constant (const byte) main::$5 = >main::$4
Successful SSA optimization Pass2ConstantIdentification
Simplifying constant evaluating to zero >(const word) main::$4 in 
Successful SSA optimization PassNSimplifyConstantZero
Eliminating unused constant (const word) main::$4
Successful SSA optimization PassNEliminateUnusedVars
Constant inlined main::$5 = (byte) 0
Constant inlined main::$3 = (const dword) DVAL#0/(word) $400
Constant inlined main::$1 = (word)(const dword) DVAL#0/(word) $400
Constant inlined main::$2 = <(word)(const dword) DVAL#0/(word) $400
Successful SSA optimization Pass2ConstantInlining
Consolidated array index constant in *(SCREEN#0+1)
Successful SSA optimization Pass2ConstantAdditionElimination
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
CALL GRAPH
Calls in [] to main:2 

Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Culled Empty Block (label) @2
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end

FINAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  [0] phi()
  to:@1
@1: scope:[]  from @begin
  [1] phi()
  [2] call main 
  to:@end
@end: scope:[]  from @1
  [3] phi()
main: scope:[main]  from @1
  [4] *((const byte*) SCREEN#0) ← <(word)(const dword) DVAL#0/(word) $400
  [5] *((const byte*) SCREEN#0+(byte) 1) ← (byte) 0
  to:main::@return
main::@return: scope:[main]  from main
  [6] return 
  to:@return


VARIABLE REGISTER WEIGHTS
(dword) DVAL
(byte*) SCREEN
(void()) main()

Initial phi equivalence classes
Complete equivalence classes

INITIAL ASM
Target platform is c64basic
  // File Comments
// Illustrates problem with constant evaluation of lo/hi-operator
// $20000 /$400 results in a byte value - confusing the lo/hi-evaluation
// which currently relies on getting the type from the literal value.
// A fix could be adding support for "declared" types for constant literal values
// - enabling the lo/hi to know that their operand is a word (from the cast).
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .const DVAL = $20000
  .label SCREEN = $400
  // @begin
bbegin:
  // [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
  // @1
b1:
  // [2] call main 
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
  // @end
bend:
  // main
main: {
    // [4] *((const byte*) SCREEN#0) ← <(word)(const dword) DVAL#0/(word) $400 -- _deref_pbuc1=vbuc2 
    lda #<DVAL/$400
    sta SCREEN
    // [5] *((const byte*) SCREEN#0+(byte) 1) ← (byte) 0 -- _deref_pbuc1=vbuc2 
    lda #0
    sta SCREEN+1
    jmp breturn
    // main::@return
  breturn:
    // [6] return 
    rts
}
  // File Data

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const byte*) SCREEN#0) ← <(word)(const dword) DVAL#0/(word) $400 [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [5] *((const byte*) SCREEN#0+(byte) 1) ← (byte) 0 [ ] ( main:2 [ ] ) always clobbers reg byte a 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 
Uplift Scope [] 

Uplifting [main] best 33 combination 
Uplifting [] best 33 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Illustrates problem with constant evaluation of lo/hi-operator
// $20000 /$400 results in a byte value - confusing the lo/hi-evaluation
// which currently relies on getting the type from the literal value.
// A fix could be adding support for "declared" types for constant literal values
// - enabling the lo/hi to know that their operand is a word (from the cast).
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .const DVAL = $20000
  .label SCREEN = $400
  // @begin
bbegin:
  // [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
  // @1
b1:
  // [2] call main 
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
  // @end
bend:
  // main
main: {
    // [4] *((const byte*) SCREEN#0) ← <(word)(const dword) DVAL#0/(word) $400 -- _deref_pbuc1=vbuc2 
    lda #<DVAL/$400
    sta SCREEN
    // [5] *((const byte*) SCREEN#0+(byte) 1) ← (byte) 0 -- _deref_pbuc1=vbuc2 
    lda #0
    sta SCREEN+1
    jmp breturn
    // main::@return
  breturn:
    // [6] return 
    rts
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction bend_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(dword) DVAL
(const dword) DVAL#0 DVAL = (dword) $20000
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(void()) main()
(label) main::@return



FINAL ASSEMBLER
Score: 18

  // File Comments
// Illustrates problem with constant evaluation of lo/hi-operator
// $20000 /$400 results in a byte value - confusing the lo/hi-evaluation
// which currently relies on getting the type from the literal value.
// A fix could be adding support for "declared" types for constant literal values
// - enabling the lo/hi to know that their operand is a word (from the cast).
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
  // Global Constants & labels
  .const DVAL = $20000
  .label SCREEN = $400
  // @begin
  // [1] phi from @begin to @1 [phi:@begin->@1]
  // @1
  // [2] call main 
  // [3] phi from @1 to @end [phi:@1->@end]
  // @end
  // main
main: {
    // SCREEN[0] = <(word)(DVAL/$400)
    // [4] *((const byte*) SCREEN#0) ← <(word)(const dword) DVAL#0/(word) $400 -- _deref_pbuc1=vbuc2 
    lda #<DVAL/$400
    sta SCREEN
    // SCREEN[1] = >(word)(DVAL/$400)
    // [5] *((const byte*) SCREEN#0+(byte) 1) ← (byte) 0 -- _deref_pbuc1=vbuc2 
    lda #0
    sta SCREEN+1
    // main::@return
    // }
    // [6] return 
    rts
}
  // File Data

