From f0f5cd63a2e3713982981688d11f918705ec468e Mon Sep 17 00:00:00 2001 From: MaartenBrock Date: Sun, 10 Jun 2007 20:55:33 +0000 Subject: [PATCH] * src/SDCCast.c (decorateType): optimized '?' for equal operands * src/SDCCicode.c (geniCodeConditional): optimization for bit result with literal operands git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4846 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 6 ++++++ src/SDCCast.c | 8 ++++++++ src/SDCCicode.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5edeabf8..dc4dc234 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-06-10 Maarten Brock + + * src/SDCCast.c (decorateType): optimized '?' for equal operands + * src/SDCCicode.c (geniCodeConditional): optimization for bit result + with literal operands + 2007-06-10 Borut Razem * as/link/z80/lklibr.c: fixed mingw build warning diff --git a/src/SDCCast.c b/src/SDCCast.c index a81dd39c..26888cd9 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -4368,6 +4368,14 @@ decorateType (ast * tree, RESULT_TYPE resultType) case '?': /* the type is value of the colon operator (on the right) */ assert (IS_COLON_OP (tree->right)); + + /* if they are equal then replace the tree */ + if (!astHasVolatile (tree->right) && + isAstEqual (tree->right->left, tree->right->right)) + { + return decorateType (tree->right->left, resultTypeProp); + } + /* if already known then replace the tree : optimizer will do it but faster to do it here */ if (IS_LITERAL (LTYPE (tree))) diff --git a/src/SDCCicode.c b/src/SDCCicode.c index 7e397fcd..601a20ee 100644 --- a/src/SDCCicode.c +++ b/src/SDCCicode.c @@ -3141,18 +3141,46 @@ geniCodeConditional (ast * tree,int lvl) iCode *ic; symbol *falseLabel = newiTempLabel (NULL); symbol *exitLabel = newiTempLabel (NULL); - operand *cond = ast2iCode (tree->left,lvl+1); - operand *true, *false, *result; + ast *astTrue = tree->right->left; + ast *astFalse = tree->right->right; + operand *cond = ast2iCode (tree->left, lvl+1); + operand *result = newiTempOperand (tree->right->ftype, 0); + operand *opTrue, *opFalse; - ic = newiCodeCondition (geniCodeRValue (cond, FALSE), - NULL, falseLabel); + if (IS_AST_LIT_VALUE (astTrue) && IS_AST_LIT_VALUE (astFalse)) + { + double valTrue = AST_LIT_VALUE (astTrue); + double valFalse = AST_LIT_VALUE (astFalse); + + if (IS_BIT (operandType (result))) + { + if ((valTrue != 0) && (valFalse == 0)) + { + /* assign cond to result */ + geniCodeAssign (result, geniCodeRValue (cond, FALSE), 0, 0); + return result; + } + else if ((valTrue == 0) && (valFalse != 0)) + { + /* assign !cond to result */ + result = geniCodeUnary (geniCodeRValue (cond, FALSE), '!'); + return result; + } + else + { + /* they have the same boolean value, make them equal */ + astFalse = astTrue; + } + } + } + + ic = newiCodeCondition (geniCodeRValue (cond, FALSE), NULL, falseLabel); ADDTOCHAIN (ic); - true = ast2iCode (tree->right->left,lvl+1); + opTrue = ast2iCode (astTrue, lvl+1); - /* move the value to a new Operand */ - result = newiTempOperand (tree->right->ftype, 0); - geniCodeAssign (result, geniCodeRValue (true, FALSE), 0, 0); + /* move the value to the new operand */ + geniCodeAssign (result, geniCodeRValue (opTrue, FALSE), 0, 0); /* generate an unconditional goto */ geniCodeGoto (exitLabel); @@ -3160,8 +3188,8 @@ geniCodeConditional (ast * tree,int lvl) /* now for the right side */ geniCodeLabel (falseLabel); - false = ast2iCode (tree->right->right,lvl+1); - geniCodeAssign (result, geniCodeRValue (false, FALSE), 0, 0); + opFalse = ast2iCode (astFalse, lvl+1); + geniCodeAssign (result, geniCodeRValue (opFalse, FALSE), 0, 0); /* create the exit label */ geniCodeLabel (exitLabel); -- 2.30.2