Normalization puts constants on teh right side of commutative nodes.
[libfirm] / ir / ir / iropt.c
index 58fcd5c..093b1f3 100644 (file)
@@ -1,15 +1,28 @@
 /*
- * Project:     libFIRM
- * File name:   ir/ir/iropt.c
- * Purpose:     iropt --- optimizations intertwined with IR construction.
- * Author:      Christian Schaefer
- * Modified by: Goetz Lindenmaier, Michael Beck
- * Created:
- * CVS-ID:      $Id$
- * Copyright:   (c) 1998-2006 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
+/**
+ * @file
+ * @brief   iropt --- optimizations intertwined with IR construction.
+ * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
+ * @version $Id$
+ */
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
@@ -3654,9 +3667,17 @@ ir_node *identify_remember(pset *value_table, ir_node *n) {
                if (is_op_commutative(get_irn_op(n))) {
                        ir_node *l = get_binop_left(n);
                        ir_node *r = get_binop_right(n);
-
-                       /* for commutative operators perform  a OP b == b OP a */
-                       if (l > r) {
+                       int l_idx = get_irn_idx(l);
+                       int r_idx = get_irn_idx(r);
+
+                       /* For commutative operators perform  a OP b == b OP a but keep
+                          constants on the RIGHT side. This helps greatly in some optimizations.
+                          Moreover we use the idx number to make the form deterministic. */
+                       if (is_irn_constlike(l))
+                               l_idx = -l_idx;
+                       if (is_irn_constlike(r))
+                               r_idx = -r_idx;
+                       if (l_idx < r_idx) {
                                set_binop_left(n, r);
                                set_binop_right(n, l);
                        }
@@ -3664,7 +3685,7 @@ ir_node *identify_remember(pset *value_table, ir_node *n) {
        }
 
        /* lookup or insert in hash table with given hash key. */
-       o = pset_insert (value_table, n, ir_node_hash (n));
+       o = pset_insert(value_table, n, ir_node_hash(n));
 
        if (o != n) {
                DBG_OPT_CSE(n, o);