Browse code

script engine: optimize $v - ct

- $v - ct transformed into $v + (-ct) which is easier to optimize
further

Andrei Pelinescu-Onciul authored on 18/12/2008 08:45:06
Showing 1 changed files
... ...
@@ -1992,7 +1992,24 @@ static int rve_optimize(struct rval_expr* rve)
1992 1992
 					rval_type_name(bad_type), rval_type_name(exp_type));
1993 1993
 			return 0;
1994 1994
 		}
1995
-		/* TODO: $v - a => $v + (-a) */
1995
+		/* $v - a => $v + (-a)  (easier to optimize)*/
1996
+		if ((rve->op==RVE_MINUS_OP) && (rve_is_constant(rve->right.rve))){
1997
+			if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
1998
+				ERR("optimization failure, bad expression\n");
1999
+				goto error;
2000
+			}
2001
+			if (rv->type==RV_INT){
2002
+				rv->v.l=-rv->v.l;
2003
+				if (rve_replace_with_ct_rv(rve->right.rve, rv)<0)
2004
+					goto error;
2005
+				rve->op=RVE_PLUS_OP;
2006
+				DBG("FIXUP RVE: optimized $v - a into $v + (%d)\n",
2007
+								(int)rve->right.rve->left.rval.v.l);
2008
+			}
2009
+			rval_destroy(rv);
2010
+			rv=0;
2011
+		}
2012
+		
1996 2013
 		/* TODO: $v * 0 => 0; $v * 1 => $v (for *, /, &, |, &&, ||, +, -) */
1997 2014
 		
1998 2015
 		/* op(op($v, a), b) => op($v, op(a,b)) */