From e8fe9208dc965dcbf10e52684e69d36ed677ce6f Mon Sep 17 00:00:00 2001 From: Bernd Schmidt Date: Thu, 30 Nov 2000 11:51:44 +0000 Subject: [PATCH] Backport a change to the 2.95 branch From-SVN: r37875 --- gcc/ChangeLog | 5 +++++ gcc/fold-const.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 78677b51c569..8cfa93c8921a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,10 @@ 2000-11-30 Bernd Schmidt + 2000-07-15 Michael Meissner + * fold-const.c (fold): When optimizing FOO++ == CONST into ++FOO + == CONST + INCREMENT, don't overwrite the tree node for FOO++, + create a new node instead. + 2000-01-01 Bernd Schmidt * expmed.c (emit_store_flag): Prevent losing a pending stack adjust the same way we prevent losing queued increments. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index cbd004e40843..2d4f907aa8cd 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -5625,7 +5625,15 @@ fold (expr) tree newconst = fold (build (PLUS_EXPR, TREE_TYPE (varop), constop, TREE_OPERAND (varop, 1))); - TREE_SET_CODE (varop, PREINCREMENT_EXPR); + + /* Do not overwrite the current varop to be a preincrement, + create a new node so that we won't confuse our caller who + might create trees and throw them away, reusing the + arguments that they passed to build. This shows up in + the THEN or ELSE parts of ?: being postincrements. */ + varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop), + TREE_OPERAND (varop, 0), + TREE_OPERAND (varop, 1)); /* If VAROP is a reference to a bitfield, we must mask the constant by the width of the field. */ @@ -5669,9 +5677,9 @@ fold (expr) } - t = build (code, type, TREE_OPERAND (t, 0), - TREE_OPERAND (t, 1)); - TREE_OPERAND (t, constopnum) = newconst; + t = build (code, type, + (constopnum == 0) ? newconst : varop, + (constopnum == 1) ? newconst : varop); return t; } } @@ -5684,7 +5692,15 @@ fold (expr) tree newconst = fold (build (MINUS_EXPR, TREE_TYPE (varop), constop, TREE_OPERAND (varop, 1))); - TREE_SET_CODE (varop, PREDECREMENT_EXPR); + + /* Do not overwrite the current varop to be a predecrement, + create a new node so that we won't confuse our caller who + might create trees and throw them away, reusing the + arguments that they passed to build. This shows up in + the THEN or ELSE parts of ?: being postdecrements. */ + varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop), + TREE_OPERAND (varop, 0), + TREE_OPERAND (varop, 1)); if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF && DECL_BIT_FIELD(TREE_OPERAND @@ -5723,9 +5739,9 @@ fold (expr) } - t = build (code, type, TREE_OPERAND (t, 0), - TREE_OPERAND (t, 1)); - TREE_OPERAND (t, constopnum) = newconst; + t = build (code, type, + (constopnum == 0) ? newconst : varop, + (constopnum == 1) ? newconst : varop); return t; } } -- 2.47.2