scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit d2d85525987cfb0822d2cb0163a7323acbbadd1e
parent a73e3bf10ef72545a4a4c0e2c541d740cf9e4e1a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  1 Jul 2017 18:45:00 +0200

[cc1] Optimize simplify()

Simplify is called for every tree, and it was following the same
logic for unary and binary operators, causing unaries waste a
big amount of time when it could be easily avoid

Diffstat:
Mcc1/fold.c | 21+++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/cc1/fold.c b/cc1/fold.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ static char sccsid[] = "@(#) ./cc1/fold.c"; +#include <assert.h> #include <stdio.h> #include <stdlib.h> @@ -428,10 +429,7 @@ fold(Node *np) Node *p, *lp = np->left, *rp = np->right; Type *tp = np->type; - if (!lp && !rp) - return np; - if (!rp && (p = foldunary(np, lp)) != NULL) - return p; + assert(lp && rp); if ((op == ODIV || op == OMOD) && cmpnode(rp, 0)) { warn("division by 0"); return NULL; @@ -657,6 +655,21 @@ simplify(Node *np) return foldternary(np, l, r); case OCALL: case OPAR: + case OSYM: + return np; + case OSNEG: + case OCPL: + case OADDR: + case OPTR: + case INC: + case DEC: + case OASSIGN: + case OA_ADD: + case OA_SUB: + case OCAST: + assert(!r); + if ((p = foldunary(np, l)) != NULL) + return p; return np; default: commutative(np, l, r);