diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 89ed89d..6fa42c9 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8954,7 +8954,7 @@ mask_with_tz (tree type, const wide_int &x, const wide_int &y) is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change *STRICT_OVERFLOW_P. */ -static bool +bool tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p) { tree type = TREE_TYPE (t); @@ -13402,9 +13402,15 @@ tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) return true; /* Constants are never weak. */ - if (CONSTANT_CLASS_P (base)) + if (CONSTANT_CLASS_P (base) + || TREE_CODE (base) == PARM_DECL + || TREE_CODE (base) == RESULT_DECL) return true; + if (VAR_P (base) + && (!is_global_var (base) + || varpool_node::get (base)->nonzero_address ())) + return true; return false; } diff --git a/gcc/fold-const.h b/gcc/fold-const.h index bc22c88..a19a27a 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -198,6 +198,7 @@ extern tree fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off); /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */ extern tree fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off); +extern bool tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p); #define fold_build_pointer_plus_hwi(p,o) \ fold_build_pointer_plus_hwi_loc (UNKNOWN_LOCATION, p, o) diff --git a/gcc/ipa-prop.c b/gcc/ipa-prop.c index 353b638..55a2ea1 100644 --- a/gcc/ipa-prop.c +++ b/gcc/ipa-prop.c @@ -1670,9 +1670,20 @@ ipa_compute_jump_functions_for_edge (struct ipa_func_body_info *fbi, if (POINTER_TYPE_P (TREE_TYPE (arg))) { + bool addr_nonzero = false; + bool strict_overflow = false; + if (TREE_CODE (arg) == SSA_NAME && param_type && get_ptr_nonnull (arg)) + addr_nonzero = true; + else if (tree_expr_nonzero_warnv_p (arg, &strict_overflow)) + { + if (!strict_overflow) + addr_nonzero = true; + } + + if (addr_nonzero) { jfunc->vr_known = true; jfunc->m_vr.type = VR_ANTI_RANGE; diff --git a/gcc/testsuite/gcc.dg/ipa/vrp5.c b/gcc/testsuite/gcc.dg/ipa/vrp5.c index e69de29..571798d 100644 --- a/gcc/testsuite/gcc.dg/ipa/vrp5.c +++ b/gcc/testsuite/gcc.dg/ipa/vrp5.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-ipa-cp-details -fdump-tree-vrp1" } */ + +static __attribute__((noinline, noclone)) +int foo (int *p) +{ + if (!p) + return 0; + *p = 1; +} + +struct st +{ + int a; + int b; +}; + +int arr1[10]; +int a; +int bar (struct st *s) +{ + int arr2[10]; + int b; + if (!s) + return 0; + foo (&s->a); + foo (&a); + foo (&b); + foo (&arr1[1]); + foo (&arr2[1]); +} + +/* { dg-final { scan-ipa-dump "Setting nonnull for 0" "cp" } } */ +/* { dg-final { scan-tree-dump-times "if" 1 "vrp1" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/vrp6.c b/gcc/testsuite/gcc.dg/ipa/vrp6.c index e69de29..971db44 100644 --- a/gcc/testsuite/gcc.dg/ipa/vrp6.c +++ b/gcc/testsuite/gcc.dg/ipa/vrp6.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-ipa-cp-details -fdump-tree-vrp1" } */ + +static __attribute__((noinline, noclone)) +int foo (int *p) +{ + if (!p) + return 0; + *p = 1; +} + +struct st +{ + int a; + int b; +}; + +struct st s2; +int a; +int bar (struct st *s) +{ + struct st s3; + int b; + if (!s) + return 0; + foo (&s->a); + foo (&s2.a); + foo (&s3.a); + foo (&a); + foo (&b); +} + +/* { dg-final { scan-ipa-dump "Setting nonnull for 0" "cp" } } */ +/* { dg-final { scan-tree-dump-times "if" 1 "vrp1" } } */