[hackers] [scc] Fix algebraic identities about logical operators || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 4 Sep 2015 23:21:05 +0200 (CEST)

commit 7e457b1611c514787ce2f3c7c04b0fc46aa32850
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Fri Sep 4 22:47:56 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Fri Sep 4 23:20:46 2015 +0200

    Fix algebraic identities about logical operators
    
    If the right part of a logic operator is a constant (which comes from
    the folding of some comparision), then the logic operator can be
    changed to a comma operator.

diff --git a/cc1/fold.c b/cc1/fold.c
index 09c3ed4..fe6e7d9 100644
--- a/cc1/fold.c
+++ b/cc1/fold.c
_AT_@ -377,8 +377,10 @@ ones(int n)
 }
 
 /*
- * i || k => i,k
- * i && k => i,k
+ * 0 || i => i
+ * 1 || i => 1
+ * 0 && i => 0
+ * 1 && i => i
  * i >> 0 => i
  * i << 0 => i
  * i + 0 => i
_AT_@ -394,7 +396,7 @@ ones(int n)
 static Node *
 identity(int *op, Node *lp, Node *rp)
 {
- int iszero, isone, istrue;
+ int iszero, isone, istrue, val;
 
         if (!rp)
                 return NULL;
_AT_@ -404,11 +406,12 @@ identity(int *op, Node *lp, Node *rp)
         istrue = !iszero && rp->constant;
 
         switch (*op) {
- case OAND:
         case OOR:
- if (rp->constant)
- goto change_to_comma;
- return NULL;
+ val = 1;
+ goto logic_operator;
+ case OAND:
+ val = 0;
+ goto logic_operator;
         case OSHL:
         case OSHR:
         case OBXOR:
_AT_@ -416,18 +419,18 @@ identity(int *op, Node *lp, Node *rp)
         case OBOR:
         case OSUB:
                 if (iszero)
- break;
+ goto free_right;
                 return NULL;
         case OMUL:
                 if (iszero)
                         goto change_to_comma;
         case ODIV:
                 if (isone)
- break;
+ goto free_right;
                 return NULL;
         case OBAND:
                 if (cmpnode(rp, ones(lp->type->size * 8)))
- break;
+ goto free_right;
                 return NULL;
         case OMOD:
                 if (isone)
_AT_@ -436,6 +439,17 @@ identity(int *op, Node *lp, Node *rp)
                 return NULL;
         }
 
+logic_operator:
+ if (!lp->constant)
+ return NULL;
+ if (cmpnode(lp, val))
+ goto free_right;
+
+free_left:
+ freetree(lp);
+ return rp;
+
+free_right:
         freetree(rp);
         return lp;
 
Received on Fri Sep 04 2015 - 23:21:05 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 04 2015 - 23:24:11 CEST