You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.6 KiB

  1. From a6219e8e0719b14f474b0dcaa7bde2f4e57474f9 Mon Sep 17 00:00:00 2001
  2. From: Jakub Jelinek <jakub@redhat.com>
  3. Date: Wed, 17 Nov 2021 13:45:53 +0100
  4. Subject: [PATCH] ranger: Fix up fold_using_range::range_of_address [PR103255]
  5. If on &base->member the offset isn't constant or isn't zero and
  6. -fdelete-null-pointer-checks and not -fwrapv-pointer and base has a range
  7. that doesn't include NULL, we return the range of the base.
  8. Usually it isn't a big deal, because for most pointers we just use
  9. varying, range_zero and range_nonzero ranges and nothing beyond that,
  10. but if a pointer is initialized from a constant, we actually track the
  11. exact range and in that case this causes miscompilation.
  12. As discussed on IRC, I think doing something like:
  13. offset_int off2;
  14. if (off_cst && off.is_constant (&off2))
  15. {
  16. tree cst = wide_int_to_tree (sizetype, off2 / BITS_PER_UNIT);
  17. // adjust range r with POINTER_PLUS_EXPR cst
  18. if (!range_includes_zero_p (&r))
  19. return true;
  20. }
  21. // Fallback
  22. r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
  23. return true;
  24. could work, given that most of the pointer ranges are just the simple ones
  25. perhaps it is too much for little benefit.
  26. 2021-11-17 Jakub Jelinek <jakub@redhat.com>
  27. PR tree-optimization/103255
  28. * gimple-range.cc (fold_using_range::range_of_address): Return
  29. range_nonzero rather than unadjusted base's range. Formatting fixes.
  30. * gcc.c-torture/execute/pr103255.c: New test.
  31. (cherry picked from commit c39cb6bf835ca12e590eaa6f90222e51be207c50)
  32. ---
  33. gcc/gimple-range.cc | 16 +++++---
  34. .../gcc.c-torture/execute/pr103255.c | 41 +++++++++++++++++++
  35. 2 files changed, 52 insertions(+), 5 deletions(-)
  36. create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr103255.c
  37. --- a/gcc/gimple-range.cc
  38. +++ b/gcc/gimple-range.cc
  39. @@ -491,14 +491,20 @@ gimple_ranger::range_of_address (irange
  40. }
  41. /* If &X->a is equal to X, the range of X is the result. */
  42. if (off_cst && known_eq (off, 0))
  43. - return true;
  44. + return true;
  45. else if (flag_delete_null_pointer_checks
  46. && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
  47. {
  48. - /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
  49. - allow going from non-NULL pointer to NULL. */
  50. - if(!range_includes_zero_p (&r))
  51. - return true;
  52. + /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
  53. + allow going from non-NULL pointer to NULL. */
  54. + if (!range_includes_zero_p (&r))
  55. + {
  56. + /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
  57. + using POINTER_PLUS_EXPR if off_cst and just fall back to
  58. + this. */
  59. + r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
  60. + return true;
  61. + }
  62. }
  63. /* If MEM_REF has a "positive" offset, consider it non-NULL
  64. always, for -fdelete-null-pointer-checks also "negative"
  65. --- /dev/null
  66. +++ b/gcc/testsuite/gcc.c-torture/execute/pr103255.c
  67. @@ -0,0 +1,41 @@
  68. +/* PR tree-optimization/103255 */
  69. +
  70. +struct H
  71. +{
  72. + unsigned a;
  73. + unsigned b;
  74. + unsigned c;
  75. +};
  76. +
  77. +#if __SIZEOF_POINTER__ >= 4
  78. +#define ADDR 0x400000
  79. +#else
  80. +#define ADDR 0x4000
  81. +#endif
  82. +#define OFF 0x20
  83. +
  84. +int
  85. +main ()
  86. +{
  87. + struct H *h = 0;
  88. + unsigned long o;
  89. + volatile int t = 1;
  90. +
  91. + for (o = OFF; o <= OFF; o += 0x1000)
  92. + {
  93. + struct H *u;
  94. + u = (struct H *) (ADDR + o);
  95. + if (t)
  96. + {
  97. + h = u;
  98. + break;
  99. + }
  100. + }
  101. +
  102. + if (h == 0)
  103. + return 0;
  104. + unsigned *tt = &h->b;
  105. + if ((__SIZE_TYPE__) tt != (ADDR + OFF + __builtin_offsetof (struct H, b)))
  106. + __builtin_abort ();
  107. + return 0;
  108. +}