dplassgit / d2lang

D2 is a strongly-typed, statically-typed, (mostly) inferred-type compiled language.
MIT License
6 stars 0 forks source link

Optimize comparisons #201

Closed dplassgit closed 3 months ago

dplassgit commented 1 year ago
 ; SOURCE: __temp76 = NUM_PLANETS > 0
 ; Allocating __temp76 to RBX
 cmp DWORD [_NUM_PLANETS], 0  ; direct comparison
 setg BL

 ; SOURCE: if not __temp76 goto __loop_end_75
 cmp BYTE BL, 0
 je __loop_end_75

should be:

 cmp DWORD [_NUM_PLANETS], 0  ; direct comparison
 jl __loop_end_75  ; setg->jl
dplassgit commented 1 year ago

This will require changes to the IL Code generator to "Merge" the last boolean calculation with the IfOp OR to have a much more complex ifOp

dplassgit commented 3 months ago

I bet we could do this in the Nasm optimizer

dplassgit commented 3 months ago
 cmp .*
 set(x) (8-byte register)
 cmp BYTE $1, 0
 je (dest)

gets replaced with

   cmp .*
   j(x) (dest)
dplassgit commented 3 months ago
cmp DWORD [_NUM_PLANETS], 0
setg BL
cmp BYTE BL, 0
je __loop_end_75

pattern is:

^  cmp (.*)$
^  set([a-z]) ([AL|BL|CL|DL|SIL|DIL|R[89]b|R1[0-5]b])$
^  cmp BYTE \3, 0$     ; this forces it to be the same register as in the previous line
^  je (.*)$

becomes:

  cmp $1
  jg $4