Skip to content

Commit 73447cc

Browse files
author
rguenth
committed
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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@235622 138bc75d-0d04-0410-961f-82ee72b054a4
1 parent fb13bff commit 73447cc

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

gcc/ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2016-04-29 Richard Biener <rguenther@suse.de>
2+
3+
PR tree-optimization/13962
4+
PR tree-optimization/65686
5+
* tree-ssa-alias.h (ptrs_compare_unequal): Declare.
6+
* tree-ssa-alias.c (ptrs_compare_unequal): New function
7+
using PTA to compare pointers.
8+
* match.pd: Add pattern for pointer equality compare simplification
9+
using ptrs_compare_unequal.
10+
111
2016-04-29 Richard Biener <rguenther@suse.de>
212

313
* stor-layout.c (layout_type): Do not build a pointer-to-element

gcc/match.pd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
24002400
(if (cmp == NE_EXPR)
24012401
{ constant_boolean_node (true, type); })))))))))
24022402

2403+
/* Simplify pointer equality compares using PTA. */
2404+
(for neeq (ne eq)
2405+
(simplify
2406+
(neeq @0 @1)
2407+
(if (POINTER_TYPE_P (TREE_TYPE (@0))
2408+
&& ptrs_compare_unequal (@0, @1))
2409+
{ neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2410+
24032411
/* Non-equality compare simplifications from fold_binary */
24042412
(for cmp (lt gt le ge)
24052413
/* Comparisons with the highest or lowest possible integer of

gcc/testsuite/ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2016-04-29 Richard Biener <rguenther@suse.de>
2+
3+
PR tree-optimization/13962
4+
PR tree-optimization/65686
5+
* gcc.dg/uninit-pr65686.c: New testcase.
6+
17
2016-04-29 Jakub Jelinek <jakub@redhat.com>
28

39
PR middle-end/70843

gcc/testsuite/gcc.dg/uninit-pr65686.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O2 -Wall -fdump-tree-optimized" } */
3+
4+
typedef unsigned mytype;
5+
6+
struct S {
7+
mytype *pu;
8+
};
9+
10+
mytype f(struct S *e)
11+
{
12+
mytype x; /* { dg-bogus { "uninitialized" } } */
13+
if(&x != e->pu)
14+
__builtin_memcpy(&x, e->pu, sizeof(unsigned));
15+
return x;
16+
}
17+
18+
/* { dg-final { scan-tree-dump-not "if" "optimized" } } */

gcc/tree-ssa-alias.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,66 @@ ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
321321
return true;
322322
}
323323

324+
/* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
325+
326+
bool
327+
ptrs_compare_unequal (tree ptr1, tree ptr2)
328+
{
329+
/* First resolve the pointers down to a SSA name pointer base or
330+
a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
331+
not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
332+
or STRING_CSTs which needs points-to adjustments to track them
333+
in the points-to sets. */
334+
tree obj1 = NULL_TREE;
335+
tree obj2 = NULL_TREE;
336+
if (TREE_CODE (ptr1) == ADDR_EXPR)
337+
{
338+
tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
339+
if (! tem)
340+
return false;
341+
if (TREE_CODE (tem) == VAR_DECL
342+
|| TREE_CODE (tem) == PARM_DECL
343+
|| TREE_CODE (tem) == RESULT_DECL)
344+
obj1 = tem;
345+
else if (TREE_CODE (tem) == MEM_REF)
346+
ptr1 = TREE_OPERAND (tem, 0);
347+
}
348+
if (TREE_CODE (ptr2) == ADDR_EXPR)
349+
{
350+
tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
351+
if (! tem)
352+
return false;
353+
if (TREE_CODE (tem) == VAR_DECL
354+
|| TREE_CODE (tem) == PARM_DECL
355+
|| TREE_CODE (tem) == RESULT_DECL)
356+
obj2 = tem;
357+
else if (TREE_CODE (tem) == MEM_REF)
358+
ptr2 = TREE_OPERAND (tem, 0);
359+
}
360+
361+
if (obj1 && obj2)
362+
/* Other code handles this correctly, no need to duplicate it here. */;
363+
else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
364+
{
365+
struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
366+
if (!pi)
367+
return false;
368+
return !pt_solution_includes (&pi->pt, obj1);
369+
}
370+
else if (TREE_CODE (ptr1) == SSA_NAME && obj2)
371+
{
372+
struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr1);
373+
if (!pi)
374+
return false;
375+
return !pt_solution_includes (&pi->pt, obj2);
376+
}
377+
378+
/* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
379+
but those require pt.null to be conservatively correct. */
380+
381+
return false;
382+
}
383+
324384
/* Returns whether reference REF to BASE may refer to global memory. */
325385

326386
static bool

gcc/tree-ssa-alias.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ extern alias_set_type ao_ref_alias_set (ao_ref *);
101101
extern alias_set_type ao_ref_base_alias_set (ao_ref *);
102102
extern bool ptr_deref_may_alias_global_p (tree);
103103
extern bool ptr_derefs_may_alias_p (tree, tree);
104+
extern bool ptrs_compare_unequal (tree, tree);
104105
extern bool ref_may_alias_global_p (tree);
105106
extern bool ref_may_alias_global_p (ao_ref *);
106107
extern bool refs_may_alias_p (tree, tree);

0 commit comments

Comments
 (0)