Bug 15347 - [tree-ssa] fold if ("<12ers" + 1 == 0).
Summary: [tree-ssa] fold if ("<12ers" + 1 == 0).
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 enhancement
Target Milestone: 4.6.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization, TREE
Depends on: 15459
Blocks: 19986
  Show dependency treegraph
 
Reported: 2004-05-09 19:25 UTC by Kazu Hirata
Modified: 2011-04-28 09:57 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-09-17 08:44:52


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-05-09 19:25:40 UTC
void bar ();

int
foo (void)
{
  if ("<12ers" + 1 == 0)
    bar ();
}

which is reduced from from

extern const char tree_code_type[];

extern char *strchr (__const char *__s, int __c) __attribute__ ((__pure__));

extern void abort (void) __attribute__ ((__noreturn__));

int
foo (int a)
{
  if (tree_code_type[a] == '1')
    if (strchr ("<12ers", tree_code_type[a]) == 0)
      abort ();
}

which is in turn reduced from find_base_decl() in alias.c.
Comment 1 Andrew Pinski 2004-05-09 19:41:04 UTC
I think the two test cases are not related at all, the second one only needs to fold strchr ("<12ers", '1' ) 
to 1 (which it does when expanding to RTL).  There are two problems with the first testcase, first 
"<12ers" + 1 == 0 is "folded" to "<12ers" == -1 by fold, and but if you add a tempary variable then "< 
12ers" + 1 == 0 is not folded to false.  Confirmed otherwise, the orginal testcase is not done on the 
RTL level but the other two are.
Comment 2 Kazu Hirata 2004-05-09 20:15:26 UTC
Andrew, I forgot to say that the first testcase can be obtained more or less
if you use Steven's strchr optimization patch.
IIRC, His strchr optimization happens in CCP, so
fold does not get to fold "<12ers" + 1 == 0 into false.

In any case, all the stupidity in this area should go away.
Comment 3 Kazu Hirata 2004-05-09 20:22:49 UTC
See also PR 14819.
If strchr() is folded to NULL, we don't have to worry about anything.
Otherwise, resolution of this PR is important.
Comment 4 Kazu Hirata 2004-06-01 06:45:00 UTC
Now that Steven's strchr patch is in, let me reiterate the problem.

void bar (void);

void
foo (void)
{
  int c = '1';

  if (strchr ("<12ers", c) == 0)
    bar ();
}

void
baz (void)
{
  int c = '1';

  if ("<12ers" + 1 == 0)
    bar ();
}

The last tree-ssa form looks like:

;; Function foo (foo)

foo ()
{
  int c;
  char * T.0;

<bb 0>:
  T.0_2 = "<12ers" + 1B;
  if (T.0_2 == 0B) goto <L0>; else goto <L1>;

<L0>:;
  bar () [tail call];

<L1>:;
  return;

}



;; Function baz (baz)

baz ()
{
  int c;

<bb 0>:
  if ("<12ers" == -1B) goto <L0>; else goto <L1>;

<L0>:;
  bar () [tail call];

<L1>:;
  return;

}

In either case, we miss the folding opportunity.
Comment 5 Kazu Hirata 2004-06-01 06:47:59 UTC
"int c = '1';" in baz() in comment #4 is an unintended garbage.
Please ignore it.
Comment 6 Andrew Pinski 2004-06-03 23:22:23 UTC
I should note that "12ers" == 0 is folded to false by:
      /* If this is an equality comparison of the address of a non-weak
         object against zero, then we know the result.  */

So improving that part will help.
Comment 7 Andrew Pinski 2005-12-07 03:23:13 UTC
I should note that the first testcase in comment #0 in the bug is fixed but not the second, non reduced one.
Comment 8 Andrew Pinski 2006-09-17 08:44:52 UTC
Just to clarify what testcase is still broken, here it is:
void baz (void)
{
  if ("<12ers" + 1 == 0)
    bar ();
}

------
The reason why strchr testcase is fixed is because we turn the strchr call into &"<12ers"[1].
Comment 9 Matt Hargett 2011-04-27 17:52:06 UTC
This appears to be fixed in 4.6.0. Mark as resolved?
Comment 10 H.J. Lu 2011-04-27 18:32:34 UTC
(In reply to comment #9)
> This appears to be fixed in 4.6.0. Mark as resolved?

Need to add a few testcases first.
Comment 11 Richard Biener 2011-04-28 09:55:46 UTC
Author: rguenth
Date: Thu Apr 28 09:55:41 2011
New Revision: 173064

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173064
Log:
2011-04-28  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/40052
	PR tree-optimization/15347
	* gcc.dg/tree-ssa/vrp57.c: New testcase.
	* gcc.dg/pr15347.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/pr15347.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/vrp57.c
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 12 Richard Biener 2011-04-28 09:56:52 UTC
Fixed.  Thanks for noticing.
Comment 13 Richard Biener 2011-04-28 09:57:05 UTC
Fi-xed.