Bug 13962

Summary: [tree-ssa] make "fold" use alias information to optimize pointer comparisons
Product: gcc Reporter: Dan Nicolaescu <dann>
Component: tree-optimizationAssignee: Richard Biener <rguenth>
Status: RESOLVED FIXED    
Severity: enhancement CC: dimhen, gabravier, gcc-bugs, msebor, sjames, zhongyunde
Priority: P2 Keywords: alias, missed-optimization, xfail
Version: tree-ssa   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96564
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87313
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110103
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2011-05-22 17:04:56
Bug Depends on:    
Bug Blocks: 19987, 113164, 61818, 65686    
Attachments: patch

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. ***
Comment 14 Richard Biener 2018-02-05 11:14:59 UTC
*** Bug 84188 has been marked as a duplicate of this bug. ***
Comment 15 Richard Biener 2023-06-05 06:46:06 UTC
*** Bug 110103 has been marked as a duplicate of this bug. ***
Comment 16 GCC Commits 2024-05-16 12:44:43 UTC
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:f3e5f4c58591f5dacdd14a65ec47bbe310df02a0

commit r15-580-gf3e5f4c58591f5dacdd14a65ec47bbe310df02a0
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Mar 11 11:17:32 2024 +0100

    tree-optimization/13962 - handle ptr-ptr compares in ptrs_compare_unequal
    
    Now that we handle pt.null conservatively we can implement the missing
    tracking of constant pool entries (aka STRING_CST) and handle
    ptr-ptr compares using points-to info in ptrs_compare_unequal.
    
            PR tree-optimization/13962
            PR tree-optimization/96564
            * tree-ssa-alias.h (pt_solution::const_pool): New flag.
            * tree-ssa-alias.cc (ptrs_compare_unequal): Handle pointer-pointer
            compares.
            (dump_points_to_solution): Dump the const_pool flag, fix guard
            of flag dumping.
            * gimple-pretty-print.cc (pp_points_to_solution): Likewise.
            * tree-ssa-structalias.cc (find_what_var_points_to): Set
            the const_pool flag for STRING.
            (pt_solution_ior_into): Handle the const_pool flag.
            (ipa_escaped_pt): Initialize it.
    
            * gcc.dg/tree-ssa/alias-39.c: New testcase.
            * g++.dg/vect/pr68145.cc: Use -fno-tree-pta to avoid UB
            to manifest in transforms no longer vectorizing this testcase
            for an ICE.
Comment 17 Richard Biener 2024-05-16 12:47:04 UTC
I'm closing this as fixed now.  The feature is there, improvements can be made I guess but first get testcases that are not handled (combine with prange info is one item).