[hackers] [scc] Recover optimization of ternary operators || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 8 Jan 2016 13:14:00 +0100 (CET)

commit 5b895e06a58fa8318c891e6f04401180506b209f
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Fri Jan 8 12:19:42 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Fri Jan 8 12:19:42 2016 +0100

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

diff --git a/cc1/expr.c b/cc1/expr.c
index 7909096..86a4176 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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
index e20bf78..a168f2d 100644
--- a/cc1/fold.c
+++ b/cc1/fold.c
_AT_@ -1,5 +1,6 @@
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "../inc/cc.h"
 #include "cc1.h"
_AT_@ -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
_AT_@ -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;
Received on Fri Jan 08 2016 - 13:14:00 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 08 2016 - 13:25:10 CET