* src/SDCCast.c (decorateType): optimized '?' for equal operands
authorMaartenBrock <MaartenBrock@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sun, 10 Jun 2007 20:55:33 +0000 (20:55 +0000)
committerMaartenBrock <MaartenBrock@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sun, 10 Jun 2007 20:55:33 +0000 (20:55 +0000)
* 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
src/SDCCast.c
src/SDCCicode.c

index 5edeabf8afec029ac6a4dfc852c206da58b042c0..dc4dc234daa6a27e0cde521dfdd09630bf224568 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-06-10 Maarten Brock <sourceforge.brock AT dse.nl>
+
+       * src/SDCCast.c (decorateType): optimized '?' for equal operands
+       * src/SDCCicode.c (geniCodeConditional): optimization for bit result
+         with literal operands
+
 2007-06-10 Borut Razem <borut.razem AT siol.net>
 
        * as/link/z80/lklibr.c: fixed mingw build warning
index a81dd39c904cb6f6062ff96b51f54d8c0d5df37f..26888cd94001bc32b9686241bf466d0ea0cba743 100644 (file)
@@ -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)))
index 7e397fcd5f434338486ad41d1214c381ea63a784..601a20eeffec7823caf36b05f121db43dd641b82 100644 (file)
@@ -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);