User account creation filtered due to spam.

Bug 13962 - [tree-ssa] make "fold" use alias information to optimize pointer comparisons
Summary: [tree-ssa] make "fold" use alias information to optimize pointer comparisons
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 enhancement
Target Milestone: ---
Assignee: Richard Biener
URL:
Keywords: alias, missed-optimization, xfail
: 78412 (view as bug list)
Depends on:
Blocks: 19987 61818 65686
  Show dependency treegraph
 
Reported: 2004-02-01 18:22 UTC by Dan Nicolaescu
Modified: 2016-11-18 08:18 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-05-22 17:04:56


Attachments
patch (986 bytes, patch)
2014-08-15 08:39 UTC, Richard Biener
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dan Nicolaescu 2004-02-01 18:22:51 UTC
Given 2 pointers p and q, if the alias information says that p and q 
cannot alias, then comparisons like p == q and p != q can be optimized away.

There are quite a few examples in:

testsuite/gcc.dg/tree-ssa/ssa-ccp-3.c
Comment 1 Diego Novillo 2004-02-01 18:31:13 UTC
Mine
Comment 2 Richard Henderson 2004-02-04 02:49:05 UTC
Enhancement request.
Comment 3 Steven Bosscher 2006-12-10 18:18:26 UTC
Is this fixed?
Comment 4 Dan Nicolaescu 2006-12-12 06:01:29 UTC
Subject: Re:  [tree-ssa] make "fold" use alias information to optimize pointer comparisons

"steven at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:

  > ------- Comment #3 from steven at gcc dot gnu dot org  2006-12-10 18:18 -------
  > Is this fixed?

No, it's not.
Comment 5 Steven Bosscher 2009-02-22 19:09:19 UTC
Richi, this may be easy to fix on the AIB...
Comment 6 Richard Biener 2009-02-22 20:20:56 UTC
Easy but dangerous ;)
Comment 7 Marc Glisse 2014-08-14 17:42:02 UTC
While looking at some unrelated issue, I noticed the following:

  _41 = operator new (28);
...
  if (_41 != &_S_empty_rep_storage)

which happens with a simple use of std::string (with some extra inlining). __builtin_malloc(42)==&var is not optimized either, so it isn't (only) an issue with operator new.

If the general case is too dangerous, maybe there is at least some subset that could safely be optimized?
Comment 8 rguenther@suse.de 2014-08-15 07:52:19 UTC
On Thu, 14 Aug 2014, glisse at gcc dot gnu.org wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=13962
> 
> --- Comment #7 from Marc Glisse <glisse at gcc dot gnu.org> ---
> While looking at some unrelated issue, I noticed the following:
> 
>   _41 = operator new (28);
> ...
>   if (_41 != &_S_empty_rep_storage)
> 
> which happens with a simple use of std::string (with some extra inlining).
> __builtin_malloc(42)==&var is not optimized either, so it isn't (only) an issue
> with operator new.
> 
> If the general case is too dangerous, maybe there is at least some subset that
> could safely be optimized?

Well, not sure if really "dangerous", but yes, doing pointer against
address-of-decl disambiguation should be easily possible.

I'll try to hack sth together later today.
Comment 9 Richard Biener 2014-08-15 08:39:06 UTC
Created attachment 33336 [details]
patch

Like this - does it help the particular case?

Note that one issue is that points-to doesn't track NULL-ness reliably
(like may-point to zero if it may point to a weak decl), and we drop
maybe-NULL-ness because we don't use it.  Likewise we don't keep track
of pointers to CONST_DECLs or LABEL_DECLs or FUNCTION_DECLs (we simply
drop them on the floor rather than treating them as "anything").

The patch doesn't bootstrap which means it probably reveals bugs in
points-to.  This is what I meant with "dangerous" ;)
Comment 10 Marc Glisse 2014-08-15 09:29:30 UTC
(In reply to Richard Biener from comment #9)
> Created attachment 33336 [details]
> patch
> 
> Like this - does it help the particular case?

_S_empty_rep_storage has DECL_WEAK, so it doesn't help here :-(
Comment 11 Richard Biener 2016-04-29 08:30:32 UTC
For GCC 7 we can now do ptr != decl (for certain kinds of decls).  I have
patches extending this to the ptr != ptr case but they need work because PTA
needs to handle points-to NULL (aka nothing) and STRING_CST (and LABEL_DECL
and FUNCTION_DECL, etc.) conservatively.  Comparing LABEL_DECL against
FUNCTION_DECL will also be interesting, so not sure if we can ever make
this work (maybe glob LABEL_DECLs with their containing FUNCTION_DECLs for
PTA purposes).
Comment 12 Richard Biener 2016-04-29 08:37:21 UTC
Author: rguenth
Date: Fri Apr 29 08:36:49 2016
New Revision: 235622

URL: https://gcc.gnu.org/viewcvs?rev=235622&root=gcc&view=rev
Log:
2016-04-29  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/13962
	PR tree-optimization/65686
	* tree-ssa-alias.h (ptrs_compare_unequal): Declare.
	* tree-ssa-alias.c (ptrs_compare_unequal): New function
	using PTA to compare pointers.
	* match.pd: Add pattern for pointer equality compare simplification
	using ptrs_compare_unequal.

	* gcc.dg/uninit-pr65686.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/uninit-pr65686.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-alias.c
    trunk/gcc/tree-ssa-alias.h
Comment 13 Richard Biener 2016-11-18 08:18:09 UTC
*** Bug 78412 has been marked as a duplicate of this bug. ***