Alamvic / druid

Meta-compiler to generate an optimised JIT compiler frontend based on an Interpreter definition
8 stars 6 forks source link

Better support for GetConditionCode #102

Open guillep opened 8 months ago

guillep commented 8 months ago

Cogit does not support GetConditionCode instructions and we translated them to a branch sequence. This means:

Example

Right now, they are compiled to:

GetConditionCode > R0 R1

=>

  Cmp R0 R1
  JumpGreater true
  R2 := false
  Jump continue
true:
  R2 := true
continue:
  Cmp R2 true
  …

And then the following instructions usually compare and branch on the R2 result.

Difficulty: Medium

One solution here is to use tail duplication to duplicate the continue block in each branch. This will allow branch optimisations to act more easily and collapse useless branching when the outcome becomes evident (since it’s true in one and false in the other)

  Cmp R0 R1
  JumpGreater true
  R2 := false
  Jump continue
true:
  R2 := true
continue:
  Cmp R2 true
  JumpEquals A
  Jump B

== Tail duplicate ==>

  Cmp R0 R1
  JumpGreater true
  R2 := false
continue’:
  Cmp R2 true // Now this branch is always false!!!
  JumpEquals A
  Jump B
true:
  R2 := true
continue’’:
  Cmp R2 true  // Now this branch is always true!!!
  JumpEquals A
  Jump B

== Branch fold ==>

  Cmp R0 R1
  JumpGreater true
  R2 := false // Now this is dead code!
continue’:
  Jump B
true:
  R2 := true  // Now this is dead code!
continue’’:
  Jump A

== dead code elimination ==>

  Cmp R0 R1
  JumpGreater true
continue’:
  Jump B
true:
  Jump A

== Remove Empty basic block ==>

  Cmp R0 R1
  JumpGreater A
  Jump B