shithub: scc

Download patch

ref: 5b895e06a58fa8318c891e6f04401180506b209f
parent: 3fe369d20c2ac3e00272ce3e9fded43f3d9a5afc
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Jan 8 07:19:42 EST 2016

Recover optimization of ternary operators

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

--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -990,7 +990,7 @@
 		expect(':');
 		ifno = ternary();
 		np = chkternary(ifyes, ifno);
-		cond = node(OASK, np->type, cond, np);
+		cond = simplify(OASK, np->type, cond, np);
 	}
 	return cond;
 }
--- 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 @@
 	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 @@
 {
 	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;