Browse Source

Strength-reduce 32 to 64 bit widening using scalar evolution analysis.

Mike Pall 14 years ago
parent
commit
1d86090926
2 changed files with 49 additions and 6 deletions
  1. 6 4
      src/lj_crecord.c
  2. 43 2
      src/lj_opt_fold.c

+ 6 - 4
src/lj_crecord.c

@@ -335,15 +335,17 @@ void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
       IRIns *ir = IR(tref_ref(idx));
       if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) &&
 	  ir->o == IR_ADD && irref_isk(ir->op2)) {
+	IRIns *irk = IR(ir->op2);
+	idx = ir->op1;
 	/* This would be rather difficult in FOLD, so do it here:
 	** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
 	*/
-	idx = ir->op1;
 #if LJ_64
-	ofs += (int64_t)ir_kint64(IR(ir->op2))->u64 * sz;
-#else
-	ofs += IR(ir->op2)->i * sz;
+	if (irk->o == IR_KINT64)
+	  ofs += (ptrdiff_t)ir_kint64(irk)->u64 * sz;
+	else
 #endif
+	  ofs += (ptrdiff_t)irk->i * sz;
       }
       idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
       ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);

+ 43 - 2
src/lj_opt_fold.c

@@ -644,8 +644,8 @@ LJFOLD(TOINT ADD any)
 LJFOLD(TOINT SUB any)
 LJFOLD(TOBIT ADD KNUM)
 LJFOLD(TOBIT SUB KNUM)
-LJFOLD(TOI64 ADD any)
-LJFOLD(TOI64 SUB any)
+LJFOLD(TOI64 ADD 5)  /* IRTOINT_TRUNCI64 */
+LJFOLD(TOI64 SUB 5)  /* IRTOINT_TRUNCI64 */
 LJFOLDF(narrow_convert)
 {
   PHIBARRIER(fleft);
@@ -669,6 +669,47 @@ LJFOLDF(cse_toint)
   return EMITFOLD;  /* No fallthrough to regular CSE. */
 }
 
+/* -- Strength reduction of widening -------------------------------------- */
+
+LJFOLD(TOI64 any 3)  /* IRTOINT_ZEXT64 */
+LJFOLDF(simplify_zext64)
+{
+#if LJ_TARGET_X64
+  /* Eliminate widening. All 32 bit ops implicitly zero-extend the result. */
+  return LEFTFOLD;
+#else
+  UNUSED(J);
+  return NEXTFOLD;
+#endif
+}
+
+LJFOLD(TOI64 any 4)  /* IRTOINT_SEXT64 */
+LJFOLDF(simplify_sext64)
+{
+  IRRef ref = fins->op1;
+  int64_t ofs = 0;
+  if (fleft->o == IR_ADD && irref_isk(fleft->op2)) {
+    ofs = (int64_t)IR(fleft->op2)->i;
+    ref = fleft->op1;
+  }
+  /* Use scalar evolution analysis results to strength-reduce sign-extension. */
+  if (ref == J->scev.idx) {
+    IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop;
+    lua_assert(irt_isint(J->scev.t));
+    if (lo && IR(lo)->i + ofs >= 0) {
+#if LJ_TARGET_X64
+      /* Eliminate widening. All 32 bit ops do an implicit zero-extension. */
+      return LEFTFOLD;
+#else
+      /* Reduce to a (cheaper) zero-extension. */
+      fins->op2 = IRTOINT_ZEXT64;
+      return RETRYFOLD;
+#endif
+    }
+  }
+  return NEXTFOLD;
+}
+
 /* -- Integer algebraic simplifications ----------------------------------- */
 
 LJFOLD(ADD any KINT)