diff mbox

Fix POINTER_PLUS_EXPR oversight

Message ID 4F4FDABB.5060308@t-online.de
State New
Headers show

Commit Message

Bernd Schmidt March 1, 2012, 8:23 p.m. UTC
In pointer_diff we still expect pointer addition to use PLUS_EXPR. I
discovered this while working on a new port with somewhat unusual
pointer types.

Interestingly, the C++ frontend also has a pointer_diff function, but
doesn't seem to attempt to optimize. Is there a reason for this?

Bootstrapped and tested on i686-linux. Ok for now or stage1?


Bernd
* c-typeck.c (pointer_diff): Check for POINTER_PLUS_EXPR, not
	PLUS_EXPR.

Comments

Richard Biener March 2, 2012, 9:33 a.m. UTC | #1
On Thu, Mar 1, 2012 at 9:23 PM, Bernd Schmidt <bernds_cb1@t-online.de> wrote:
> In pointer_diff we still expect pointer addition to use PLUS_EXPR. I
> discovered this while working on a new port with somewhat unusual
> pointer types.
>
> Interestingly, the C++ frontend also has a pointer_diff function, but
> doesn't seem to attempt to optimize. Is there a reason for this?

Frontends should not optimize when they are not required to.

> Bootstrapped and tested on i686-linux. Ok for now or stage1?
>
>
> Bernd
Mike Stump March 2, 2012, 5:59 p.m. UTC | #2
On Mar 2, 2012, at 1:33 AM, Richard Guenther wrote:
>> Interestingly, the C++ frontend also has a pointer_diff function, but
>> doesn't seem to attempt to optimize. Is there a reason for this?
> 
> Frontends should not optimize when they are not required to.

Actually, the frontends should optimize as long as it produces a faster compilation (and they are permitted to).  You're thinking of the rule that proceeded this one, we switch away from many many years back.
Richard Biener March 3, 2012, 1:51 p.m. UTC | #3
On Fri, Mar 2, 2012 at 6:59 PM, Mike Stump <mikestump@comcast.net> wrote:
> On Mar 2, 2012, at 1:33 AM, Richard Guenther wrote:
>>> Interestingly, the C++ frontend also has a pointer_diff function, but
>>> doesn't seem to attempt to optimize. Is there a reason for this?
>>
>> Frontends should not optimize when they are not required to.
>
> Actually, the frontends should optimize as long as it produces a faster compilation (and they are permitted to).  You're thinking of the rule that proceeded this one, we switch away from many many years back.

Well, I think we should work towards the FEs preserving as much of the
AST that reflects the source program as possible.

Richard.
diff mbox

Patch

Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 183969)
+++ gcc/c-typeck.c	(working copy)
@@ -3447,7 +3447,9 @@  pointer_diff (location_t loc, tree op0,
   else
     con1 = op1;
 
-  if (TREE_CODE (con0) == PLUS_EXPR)
+  gcc_assert (TREE_CODE (con0) != PLUS_EXPR
+	      && TREE_CODE (con1) != PLUS_EXPR);
+  if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
     {
       lit0 = TREE_OPERAND (con0, 1);
       con0 = TREE_OPERAND (con0, 0);
@@ -3455,7 +3457,7 @@  pointer_diff (location_t loc, tree op0,
   else
     lit0 = integer_zero_node;
 
-  if (TREE_CODE (con1) == PLUS_EXPR)
+  if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
     {
       lit1 = TREE_OPERAND (con1, 1);
       con1 = TREE_OPERAND (con1, 0);