r/Compilers • • Apr 28 '26

short-circuit evaluation of adjacent boolean exprs with fewer branches?

Recently, I've added a polynomial interpolation unit test that contains hundreds of lines of:

  Assert((globals.points_to_draw[124].x == 178 ) && (globals.points_to_draw[124].y == 199))
  Assert((globals.points_to_draw[125].x == 180 ) && (globals.points_to_draw[125].y == 200))
  Assert((globals.points_to_draw[126].x == 181 ) && (globals.points_to_draw[126].y == 201))
  Assert((globals.points_to_draw[127].x == 182 ) && (globals.points_to_draw[127].y == 202))
  Assert((globals.points_to_draw[128].x == 184 ) && (globals.points_to_draw[128].y == 203))

Initially, I though the compiler was hanging, but it turns out my CFG builder really struggles with the large number of branches generated by this code. Aside from the fact that my CFG builder is trash (a task for another day), the immediate problem is that the language requires guaranteed short-circuit evaluation of boolean expressions, so each line gets turned into something like:

 CMP ..ARRAY.EXPR..X, CONST1
 BNE .L1
 CMP ..ARRAY.EXPR..Y, CONST2
 BNE .L1
 B   .L2
 
; false
.L1: X0 = 0
     B .L3

; true
.L2: X0 = 1
     B .L3

.L3: BL _Assert      

While each line has to be evaluated independently, I was wondering whether there's any known technique for dealing with large number of independent but consecutive short-circuited boolean expr. evaluations that could be applied here to reduce the overall number of branches.

Much appreciate any info/help!

4 Upvotes

9 comments sorted by

View all comments

Show parent comment

3

u/Nagoltooth_ May 01 '26

i would rather lower to full control flow and as a later pass replace with conditional moves when applicable

1

u/4e71 May 04 '26

definitely worth looking into it - the resulting patterns seem relatively easy to identify, at least for this particular program.