Bug 21082 - &a[b] - &a[c] is not folded to b - c
Summary: &a[b] - &a[c] is not folded to b - c
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.0
: P2 enhancement
Target Milestone: 4.1.0
Assignee: Richard Biener
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: missed-optimization, patch, TREE
Depends on:
Blocks: 19986 19987 19807
  Show dependency treegraph
Reported: 2005-04-18 03:06 UTC by Andrew Pinski
Modified: 2005-04-23 21:37 UTC (History)
1 user (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2005-04-23 21:05:50


Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-04-18 03:06:10 UTC
The following C++ code is not fully as optimizated as the C version:
typedef __SIZE_TYPE__ size_t;
size_t a[100];
size_t f(size_t b, size_t c)
  return &a[b] - &a[c];

Found this while looking into the tree dump for mgrid.
Comment 1 Andrew Pinski 2005-04-18 03:19:54 UTC
This should improve mgrid and IV selection.
Comment 2 Andrew Pinski 2005-04-18 04:06:45 UTC
The code in comment #0 should produce no multiply/divides/shifts but does currently (again with the 
C++ front-end).
Take the following code:
typedef __SIZE_TYPE__ size_t;
size_t a[100];
size_t f(size_t b, size_t c)
  return (&a[b] - &a[c])*sizeof(*a);
We get the good code but only after combine so this blocks both the generic one and the RTL based 
missed fold optimizators and we can add the TREE keyword too for the above testcase.
Comment 3 James A. Morrison 2005-04-19 14:57:38 UTC
 Yup, the dumps from the c++ front end is:
;; Function size_t f(size_t, size_t) (_Z1fjj)

size_t f(size_t, size_t) (b, c)
<bb 0>:
  return (size_t) (((int) &a[b] - (int) &a[c]) /[ex] 4);


 and the C front-end dump is:
;; Function f (f)

f (b, c)
<bb 0>:
  return (size_t) (b - c);

Comment 4 Richard Biener 2005-04-23 14:31:20 UTC
PR19807 is related.
Comment 5 Andrew Pinski 2005-04-23 16:24:43 UTC
Zdenek was complaining about this in:
Comment 6 Richard Biener 2005-04-23 21:05:50 UTC
Patch at
Comment 7 CVS Commits 2005-04-23 21:34:56 UTC
Subject: Bug 21082

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	rguenth@gcc.gnu.org	2005-04-23 21:34:42

Modified files:
	gcc            : ChangeLog fold-const.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/g++.dg/tree-ssa: pr21082.C 

Log message:
	2005-04-23  Richard Guenther  <rguenth@gcc.gnu.org>
	PR middle-end/21082
	* fold-const.c: Fold &a[i]-&a[j] to i-j.
	* g++.dg/tree-ssa/pr21082.C: New testcase.


Comment 8 Richard Biener 2005-04-23 21:36:53 UTC