Typo in fold engine?

  • From: Thomas Schilling <nominolo@xxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 17 Jul 2012 17:13:03 +0100

Mike,

I was looking through the code for the FOLD engine of LuaJIT and it
looks like there's a typo in this code:

LJFOLD(SUB SUB any)
LJFOLDF(simplify_intsubsub_leftcancel)
{
  if (!irt_isnum(fins->t)) {
    PHIBARRIER(fleft);
    if (fins->op1 == fleft->op1) {  /* (i - j) - i ==> 0 - j */    <--- HERE
      fins->op1 = (IRRef1)lj_ir_kint(J, 0);
      fins->op2 = fleft->op2;
      return RETRYFOLD;
    }
  }
  return NEXTFOLD;
}

I think it should read "fins->op2" instead of "fins->op1". As it
stands the test can only evaluate to true if we have an IR instruction
    x = SUB x y
which is impossible in SSA.  So it's not a bug, just a (tiny) failure
to optimise.

I tried to come up with a test case, but since this is an integer
optimisations its somewhat hard to trigger as only loop counters seem
to be narrowed to integers.

Patch is attached.

/ Thomas

Other related posts: