ref: c6229ce9770538a712b32fe753b7105cee770773
parent: ed1a8159a3f06e7c4da43567e8dee276d392f3bd
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Nov 1 12:06:24 EDT 2021
cc1: Improve simplify() This commits add a print of the value before and after the simplification, helping a lot with debugging. It also tries multiple optimizations instead of stopping just in the first optimization.
--- a/src/cmd/cc/cc1/fold.c
+++ b/src/cmd/cc/cc1/fold.c
@@ -419,6 +419,9 @@
Node *p, *lp = np->left, *rp = np->right;
Type *tp = np->type;
+ if (!lp && !rp)
+ return np;
+
if ((op == ODIV || op == OMOD) && cmpnode(rp, 0)) {
warn("division by 0");
return NULL;
@@ -607,7 +610,7 @@
static Node *
foldternary(Node *np, Node *cond, Node *body)
{
- if (!(cond->flags & NCONST))
+ if ((cond->flags & NCONST) == 0)
return np;
if (cmpnode(cond, 0)) {
np = body->right;
@@ -628,7 +631,7 @@
/* TODO: fold OCOMMA */
Node *
-simplify(Node *np)
+xsimplify(Node *np)
{
Node *p, *l, *r;
@@ -635,12 +638,9 @@
if (!np)
return NULL;
- if (enadebug)
- prtree("simplify", np);
+ l = np->left = xsimplify(np->left);
+ r = np->right = xsimplify(np->right);
- l = np->left = simplify(np->left);
- r = np->right = simplify(np->right);
-
switch (np->op) {
case OASK:
return foldternary(np, l, r);
@@ -669,16 +669,27 @@
case ONEG:
assert(!r);
if ((p = foldunary(np, l)) != NULL)
- return p;
+ np = p;
if ((p = fold(np)) != NULL)
- return p;
+ np = p;
return np;
default:
commutative(np, l, r);
if ((p = fold(np)) != NULL)
- return p;
+ np = p;
if ((p = identity(np)) != NULL)
- return p;
+ np = p;
return np;
}
+}
+
+Node *
+simplify(Node *np)
+{
+ if (enadebug)
+ prtree("simplify before", np);
+ np = xsimplify(np);
+ if (enadebug)
+ prtree("simplify after", np);
+ return np;
}