scc

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

commit 5b895e06a58fa8318c891e6f04401180506b209f
parent 3fe369d20c2ac3e00272ce3e9fded43f3d9a5afc
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Jan 2016 12:19:42 +0100

Recover optimization of ternary operators

This optimization was lost in previous commits because
the changes were easier without worrying about this
optimization.

Diffstat:
Mcc1/expr.c | 2+-
Mcc1/fold.c | 22++++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/cc1/expr.c b/cc1/expr.c @@ -990,7 +990,7 @@ ternary(void) expect(':'); ifno = ternary(); np = chkternary(ifyes, ifno); - cond = node(OASK, np->type, cond, np); + cond = simplify(OASK, np->type, cond, np); } return cond; } diff --git a/cc1/fold.c b/cc1/fold.c @@ -1,5 +1,6 @@ #include <stdio.h> +#include <stdlib.h> #include "../inc/cc.h" #include "cc1.h" @@ -480,6 +481,25 @@ change_to_comma: return NULL; } +static Node * +foldternary(int op, Type *tp, Node *cond, Node *body) +{ + Node *np; + + if (!cond->constant) + return node(op, tp, cond, body); + if (cmpnode(cond, 0)) { + np = body->right; + freetree(body->left); + } else { + np = body->left; + freetree(body->right); + } + freetree(cond); + free(body); + return np; +} + /* * TODO: transform simplify in a recursivity * function, because we are losing optimization @@ -490,6 +510,8 @@ simplify(int op, Type *tp, Node *lp, Node *rp) { Node *np; + if (op == OASK) + return foldternary(op, tp, lp, rp); commutative(&op, &lp, &rp); if ((np = fold(op, tp, lp, rp)) != NULL) return np;