1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-17 00:29:31 +00:00

Another minor improvement in compares.

git-svn-id: svn://svn.cc65.org/cc65/trunk@4745 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2010-07-11 21:53:48 +00:00
parent ce886f4c5e
commit 477b77f528

View File

@ -2029,6 +2029,10 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
} else {
/* Determine the signedness of the operands */
int LeftSigned = IsSignSigned (Expr->Type);
int RightSigned = IsSignSigned (Expr2.Type);
/* If the right hand side is constant, and the generator function
* expects the lhs in the primary, remove the push of the primary
* now.
@ -2045,11 +2049,9 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
/* Determine the type of the operation. */
if (IsTypeChar (Expr->Type) && rconst) {
/* Left side is unsigned char, right side is constant */
int LeftSigned = IsSignSigned (Expr->Type);
int RightSigned = IsSignSigned (Expr2.Type);
/* Determine the minimum and maximum values */
/* Left side is unsigned char, right side is constant.
* Determine the minimum and maximum values
*/
int LeftMin, LeftMax;
if (LeftSigned) {
LeftMin = -128;
@ -2111,11 +2113,6 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
WarnConstCompareResult ();
RemoveCode (&Mark0);
goto Done;
} else if (!LeftSigned && Expr2.IVal == 0) {
/* We can replace this by a compare to zero, because
* the value of lhs may never be negative.
*/
GenFunc = g_eq;
}
break;
@ -2134,11 +2131,6 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
WarnConstCompareResult ();
RemoveCode (&Mark0);
goto Done;
} else if (!LeftSigned && Expr2.IVal == 0) {
/* We can replace this by a compare to zero, because
* the value of lhs may never be negative.
*/
GenFunc = g_ne;
}
break;
@ -2165,7 +2157,7 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
if (rconst) {
flags |= CF_FORCECHAR;
}
if (IsSignUnsigned (Expr->Type)) {
if (!LeftSigned) {
flags |= CF_UNSIGNED;
}
} else {
@ -2173,6 +2165,59 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
flags |= g_typeadjust (ltype, rtype);
}
/* If the left side is an unsigned and the right is a constant,
* we may be able to change the compares to something more
* effective.
*/
if (!LeftSigned && rconst) {
switch (Tok) {
case TOK_LT:
if (Expr2.IVal == 1) {
/* An unsigned compare to one means that the value
* must be zero.
*/
GenFunc = g_eq;
Expr2.IVal = 0;
}
break;
case TOK_LE:
if (Expr2.IVal == 0) {
/* An unsigned compare to zero means that the value
* must be zero.
*/
GenFunc = g_eq;
}
break;
case TOK_GE:
if (Expr2.IVal == 1) {
/* An unsigned compare to one means that the value
* must not be zero.
*/
GenFunc = g_ne;
Expr2.IVal = 0;
}
break;
case TOK_GT:
if (Expr2.IVal == 0) {
/* An unsigned compare to zero means that the value
* must not be zero.
*/
GenFunc = g_ne;
}
break;
default:
break;
}
}
/* Generate code */
GenFunc (flags, Expr2.IVal);