[前][次][番号順一覧][スレッド一覧]

ruby-changes:37354

From: nobu <ko1@a...>
Date: Thu, 29 Jan 2015 17:03:38 +0900 (JST)
Subject: [ruby-changes:37354] nobu:r49435 (trunk): parse.y: optimize conditions

nobu	2015-01-29 17:03:24 +0900 (Thu, 29 Jan 2015)

  New Revision: 49435

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49435

  Log:
    parse.y: optimize conditions
    
    * parse.y (new_if_gen, logop_gen): simplify constant conditional
      expressions to help the optimizer.

  Modified files:
    trunk/parse.y
Index: parse.y
===================================================================
--- parse.y	(revision 49434)
+++ parse.y	(revision 49435)
@@ -374,6 +374,8 @@ static NODE* node_newnode(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L374
 
 static NODE *cond_gen(struct parser_params*,NODE*);
 #define cond(node) cond_gen(parser, (node))
+static NODE *new_if_gen(struct parser_params*,NODE*,NODE*,NODE*);
+#define new_if(cc,left,right) new_if_gen(parser, (cc), (left), (right))
 static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*);
 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
 
@@ -1144,7 +1146,7 @@ stmt		: keyword_alias fitem {lex_state = https://github.com/ruby/ruby/blob/trunk/parse.y#L1146
 		| stmt modifier_if expr_value
 		    {
 		    /*%%%*/
-			$$ = NEW_IF(cond($3), remove_begin($1), 0);
+			$$ = new_if($3, remove_begin($1), 0);
 			fixpos($$, $3);
 		    /*%
 			$$ = dispatch2(if_mod, $3, $1);
@@ -2373,7 +2375,7 @@ arg		: lhs '=' arg https://github.com/ruby/ruby/blob/trunk/parse.y#L2375
 		    {
 		    /*%%%*/
 			value_expr($1);
-			$$ = NEW_IF(cond($1), $4, $8);
+			$$ = new_if($1, $4, $8);
 			fixpos($$, $1);
 		    /*%
 			$$ = dispatch3(ifop, $1, $4, $8);
@@ -2825,7 +2827,7 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2827
 		  k_end
 		    {
 		    /*%%%*/
-			$$ = NEW_IF(cond($2), $4, $5);
+			$$ = new_if($2, $4, $5);
 			fixpos($$, $2);
 		    /*%
 			$$ = dispatch3(if, $2, $4, escape_Qundef($5));
@@ -3206,7 +3208,7 @@ if_tail		: opt_else https://github.com/ruby/ruby/blob/trunk/parse.y#L3208
 		  if_tail
 		    {
 		    /*%%%*/
-			$$ = NEW_IF(cond($2), $4, $5);
+			$$ = new_if($2, $4, $5);
 			fixpos($$, $2);
 		    /*%
 			$$ = dispatch3(elsif, $2, $4, escape_Qundef($5));
@@ -9655,10 +9657,31 @@ cond_gen(struct parser_params *parser, N https://github.com/ruby/ruby/blob/trunk/parse.y#L9657
 }
 
 static NODE*
+new_if_gen(struct parser_params *parser, NODE *cc, NODE *left, NODE *right)
+{
+    if (!cc) return right;
+    cc = cond0(parser, cc);
+    switch (nd_type(cc)) {
+      case NODE_NIL:
+      case NODE_FALSE:
+	return right;
+      case NODE_TRUE:
+      case NODE_LIT:
+      case NODE_STR:
+	return left;
+    }
+    return NEW_IF(cc, left, right);
+}
+
+static NODE*
 logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right)
 {
     value_expr(left);
-    if (left && (enum node_type)nd_type(left) == type) {
+    if (!left) {
+	if (type == NODE_AND) return 0;
+	/* make NODE_OR not to be "void value expression" */
+    }
+    else if ((enum node_type)nd_type(left) == type) {
 	NODE *node = left, *second;
 	while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
 	    node = second;
@@ -9666,6 +9689,21 @@ logop_gen(struct parser_params *parser, https://github.com/ruby/ruby/blob/trunk/parse.y#L9689
 	node->nd_2nd = NEW_NODE(type, second, right, 0);
 	return left;
     }
+    else {
+	switch (nd_type(left)) {
+	  case NODE_NIL:
+	  case NODE_FALSE:
+	    if (type == NODE_AND) return left;
+	    left = 0;
+	    break;
+	  case NODE_TRUE:
+	  case NODE_LIT:
+	  case NODE_STR:
+	    if (type != NODE_AND) return left;
+	    nd_set_type(left, NODE_TRUE);
+	    break;
+	}
+    }
     return NEW_NODE(type, left, right, 0);
 }
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]