[hackers] [scc] Add more algebraic identities || Roberto E. Vargas Caballero
commit c9538a1cb5279a7a70695c86ad31ea9f85436d59
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Sep 2 14:57:54 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Sep 2 18:24:03 2015 +0200
Add more algebraic identities
diff --git a/cc1/fold.c b/cc1/fold.c
index 45d5a20..44608bf 100644
--- a/cc1/fold.c
+++ b/cc1/fold.c
_AT_@ -376,36 +376,81 @@ ones(int n)
return v;
}
+/*
+ * i || 0 => i,0
+ * i || 1 => i
+ * i && 0 => i,0
+ * i && 1 => i
+ * i >> 0 => i
+ * i << 0 => i
+ * i + 0 => i
+ * i - 0 => i
+ * i | 0 => i
+ * i ^ 0 => i
+ * i * 0 => i,0
+ * i * 1 => i
+ * i / 1 => i
+ * i & ~0 => i
+ * i % 1 => i,1
+ */
static Node *
-identity(int op, Node *lp, Node *rp)
+identity(int *op, Node *lp, Node *rp)
{
- int val;
+ int iszero, isone, istrue;
- switch (op) {
+ if (!rp)
+ return NULL;
+
+ iszero = cmpnode(rp, 0);
+ isone = cmpnode(rp, 1),
+ istrue = !iszero && rp->constant;
+
+ switch (*op) {
+ case OOR:
+ if (istrue)
+ goto change_to_comma;
+ if (iszero)
+ break;
+ return NULL;
+ case OAND:
+ if (iszero)
+ goto change_to_comma;
+ if (istrue)
+ break;
+ return NULL;
case OSHL:
case OSHR:
case OBXOR:
case OADD:
+ case OBOR:
case OSUB:
- val = 0;
- break;
- case ODIV:
- case OMOD:
+ if (iszero)
+ break;
+ return NULL;
case OMUL:
- case OBOR:
- val = 1;
- break;
+ if (iszero)
+ goto change_to_comma;
+ case ODIV:
+ if (isone)
+ break;
+ return NULL;
case OBAND:
- if (cmpnode(lp, ones(lp->type->size * 8)))
- goto free_right;
+ if (cmpnode(rp, ones(lp->type->size * 8)))
+ break;
+ return NULL;
+ case OMOD:
+ if (isone)
+ goto change_to_comma;
default:
return NULL;
}
- if (!cmpnode(rp, val))
- return NULL;
-free_right:
+
freetree(rp);
return lp;
+
+change_to_comma:
+ *op = OCOMMA;
+ return NULL;
}
Node *
_AT_@ -416,7 +461,7 @@ simplify(int op, Type *tp, Node *lp, Node *rp)
commutative(&op, &lp, &rp);
if ((np = fold(op, tp, lp, rp)) != NULL)
return np;
- if ((np = identity(op, lp, rp)) != NULL)
+ if ((np = identity(&op, lp, rp)) != NULL)
return np;
return node(op, tp, lp, rp);
}
Received on Wed Sep 02 2015 - 18:25:57 CEST
This archive was generated by hypermail 2.3.0
: Wed Sep 02 2015 - 18:36:14 CEST