summaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorebotcazou <ebotcazou@138bc75d-0d04-0410-961f-82ee72b054a4>2006-01-16 14:26:32 +0000
committerebotcazou <ebotcazou@138bc75d-0d04-0410-961f-82ee72b054a4>2006-01-16 14:26:32 +0000
commit7e50ecaeb4fe7c6b78cf5bbd8709ec113231235e (patch)
tree10316cdc614740e5e71d0548c7f3e20c0d1c8902 /gcc
parentc78f0f8766230b43640828a4ec673fd5f7d7d9c2 (diff)
downloadgcc-7e50ecaeb4fe7c6b78cf5bbd8709ec113231235e.tar.gz
* fold-const.c (fold_minmax): New static function.
(fold_binary) <MIN_EXPR>: Call it. <MAX_EXPR>: Likewise. * stor-layout.c (place_field): Use DECL_SIZE consistently in the computation of the new record size. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@109747 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog9
-rw-r--r--gcc/fold-const.c49
-rw-r--r--gcc/stor-layout.c4
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/minmax-1.c83
5 files changed, 148 insertions, 2 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index e4c1b1b7bc3..ef9198510aa 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2006-01-16 Eric Botcazou <ebotcazou@adacore.com>
+ Roger Sayle <roger@eyesopen.com>
+
+ * fold-const.c (fold_minmax): New static function.
+ (fold_binary) <MIN_EXPR>: Call it.
+ <MAX_EXPR>: Likewise.
+ * stor-layout.c (place_field): Use DECL_SIZE consistently
+ in the computation of the new record size.
+
2006-01-16 Kazu Hirata <kazu@codesourcery.com>
* cse.c (cse_condition_code_reg): Make it static.
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 7cab3c464be..d568d8d3528 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -7184,6 +7184,49 @@ fold_unary (enum tree_code code, tree type, tree op0)
}
/* Fold a binary expression of code CODE and type TYPE with operands
+ OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
+ Return the folded expression if folding is successful. Otherwise,
+ return NULL_TREE. */
+
+static tree
+fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
+{
+ enum tree_code compl_code;
+
+ if (code == MIN_EXPR)
+ compl_code = MAX_EXPR;
+ else if (code == MAX_EXPR)
+ compl_code = MIN_EXPR;
+ else
+ gcc_assert (FALSE);
+
+ /* MIN (MAX (a, b), b) == b.  */
+ if (TREE_CODE (op0) == compl_code
+ && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
+ return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
+
+ /* MIN (MAX (b, a), b) == b.  */
+ if (TREE_CODE (op0) == compl_code
+ && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
+ && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
+ return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
+
+ /* MIN (a, MAX (a, b)) == a.  */
+ if (TREE_CODE (op1) == compl_code
+ && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
+ && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
+ return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
+
+ /* MIN (a, MAX (b, a)) == a.  */
+ if (TREE_CODE (op1) == compl_code
+ && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
+ && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
+ return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
+
+ return NULL_TREE;
+}
+
+/* Fold a binary expression of code CODE and type TYPE with operands
OP0 and OP1. Return the folded expression if folding is
successful. Otherwise, return NULL_TREE. */
@@ -8721,6 +8764,9 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
if (INTEGRAL_TYPE_P (type)
&& operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
return omit_one_operand (type, arg1, arg0);
+ tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
+ if (tem)
+ return tem;
goto associate;
case MAX_EXPR:
@@ -8730,6 +8776,9 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
&& TYPE_MAX_VALUE (type)
&& operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
return omit_one_operand (type, arg1, arg0);
+ tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
+ if (tem)
+ return tem;
goto associate;
case TRUTH_ANDIF_EXPR:
diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c
index 7db35673d57..cb57cb378b8 100644
--- a/gcc/stor-layout.c
+++ b/gcc/stor-layout.c
@@ -1222,8 +1222,8 @@ place_field (record_layout_info rli, tree field)
is printed in finish_struct. */
if (DECL_SIZE (field) == 0)
/* Do nothing. */;
- else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
- || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
+ else if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
+ || TREE_CONSTANT_OVERFLOW (DECL_SIZE (field)))
{
rli->offset
= size_binop (PLUS_EXPR, rli->offset,
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 02a313e979c..94d4a20fc56 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2006-01-16 Eric Botcazou <ebotcazou@adacore.com>
+ Andrew Pinski <pinskia@physics.uc.edu>
+
+ * gcc.dg/minmax-1.c: New test.
+
2006-01-16 Ben Elliston <bje@au.ibm.com>
* gcc.dg/dfp/dfp.exp: Correct FSF address.
diff --git a/gcc/testsuite/gcc.dg/minmax-1.c b/gcc/testsuite/gcc.dg/minmax-1.c
new file mode 100644
index 00000000000..e279c0e20fe
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/minmax-1.c
@@ -0,0 +1,83 @@
+/* { dg-do run } */
+/* { dg-options "-fdump-tree-original" } */
+
+/* Check that MIN-MAX and MAX-MIN combinations are folded. */
+
+extern void abort (void);
+
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+
+int f1(int a, int b)
+{
+ return MIN (MAX (a, b), b); /* == b */
+}
+
+int f2(int a, int b)
+{
+ return MAX (MIN (a, b), b); /* == b */
+}
+
+int f3(int a, int b)
+{
+ return MIN (MAX (b, a), b); /* == b */
+}
+
+int f4(int a, int b)
+{
+ return MAX (MIN (b, a), b); /* == b */
+}
+
+
+int g1(int a, int b)
+{
+ return MIN (a, MAX (a, b)); /* == a */
+}
+
+int g2(int a, int b)
+{
+ return MAX (a, MIN (a, b)); /* == a */
+}
+
+int g3(int a, int b)
+{
+ return MIN (a, MAX (b, a)); /* == a */
+}
+
+int g4(int a, int b)
+{
+ return MAX (a, MIN (b, a)); /* == a */
+}
+
+int main(void)
+{
+ if (f1 (1, 2) != 2)
+ abort ();
+
+ if (f2 (1, 2) != 2)
+ abort ();
+
+ if (f3 (1, 2) != 2)
+ abort ();
+
+ if (f4 (1, 2) != 2)
+ abort ();
+
+ if (g1 (1, 2) != 1)
+ abort ();
+
+ if (g2 (1, 2) != 1)
+ abort ();
+
+ if (g3 (1, 2) != 1)
+ abort ();
+
+ if (g4 (1, 2) != 1)
+ abort ();
+
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "original"} } */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "original"} } */
+/* { dg-final { cleanup-tree-dump "original" } } */