From 12b5da19994eedaeaec404859444b7704d204114 Mon Sep 17 00:00:00 2001
From: huzife <634763349@qq.com>
Date: Thu, 5 Jun 2025 14:59:49 +0800
Subject: [PATCH] [array-dse] Ignore debug stmt, add testsuites
---
gcc/ipa-array-dse.cc | 20 +++++++++--
.../alignment-propagation.exp | 35 +++++++++++++++++++
.../alignment_less_than_rhs.c | 18 ++++++++++
.../non_aligned_offset.c | 18 ++++++++++
.../not_address_of_local_var.c | 18 ++++++++++
.../rewrite_aligned_bit_and.c | 26 ++++++++++++++
gcc/testsuite/gcc.dg/array-dse/array-dse.exp | 35 +++++++++++++++++++
.../array-dse_callee_ptr_exceed_range.c | 28 +++++++++++++++
.../array-dse/array-dse_fully_redundant.c | 28 +++++++++++++++
.../array-dse_no_unique_length_param.c | 33 +++++++++++++++++
.../array-dse_non_local_array_in_caller.c | 29 +++++++++++++++
.../array-dse_non_unique_written_base.c | 31 ++++++++++++++++
.../array-dse/array-dse_partial_redundant.c | 28 +++++++++++++++
.../array-dse_read_bound_non_const.c | 27 ++++++++++++++
.../gcc.dg/localize-array/bad-dominance.c | 33 +++++++++++++++++
.../localize-array/escape-by-taking-address.c | 27 ++++++++++++++
.../localize-array-with-multiple-elem.c | 28 +++++++++++++++
.../localize-array-with-single-elem.c | 26 ++++++++++++++
.../gcc.dg/localize-array/localize-array.exp | 35 +++++++++++++++++++
.../localize-array/not-allocated-by-calloc.c | 27 ++++++++++++++
.../used-by-multiple-functions.c | 34 ++++++++++++++++++
21 files changed, 581 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/alignment-propagation/alignment-propagation.exp
create mode 100644 gcc/testsuite/gcc.dg/alignment-propagation/alignment_less_than_rhs.c
create mode 100644 gcc/testsuite/gcc.dg/alignment-propagation/non_aligned_offset.c
create mode 100644 gcc/testsuite/gcc.dg/alignment-propagation/not_address_of_local_var.c
create mode 100644 gcc/testsuite/gcc.dg/alignment-propagation/rewrite_aligned_bit_and.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse.exp
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_callee_ptr_exceed_range.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_fully_redundant.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_no_unique_length_param.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_non_local_array_in_caller.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_non_unique_written_base.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_partial_redundant.c
create mode 100644 gcc/testsuite/gcc.dg/array-dse/array-dse_read_bound_non_const.c
create mode 100644 gcc/testsuite/gcc.dg/localize-array/bad-dominance.c
create mode 100644 gcc/testsuite/gcc.dg/localize-array/escape-by-taking-address.c
create mode 100644 gcc/testsuite/gcc.dg/localize-array/localize-array-with-multiple-elem.c
create mode 100644 gcc/testsuite/gcc.dg/localize-array/localize-array-with-single-elem.c
create mode 100644 gcc/testsuite/gcc.dg/localize-array/localize-array.exp
create mode 100644 gcc/testsuite/gcc.dg/localize-array/not-allocated-by-calloc.c
create mode 100644 gcc/testsuite/gcc.dg/localize-array/used-by-multiple-functions.c
diff --git a/gcc/ipa-array-dse.cc b/gcc/ipa-array-dse.cc
index df973e849ba..7d8bb9f9afa 100644
--- a/gcc/ipa-array-dse.cc
+++ b/gcc/ipa-array-dse.cc
@@ -907,7 +907,7 @@ array_dse_callee::find_candidate_array ()
for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
- if (gimple_clobber_p (stmt))
+ if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
continue;
/* There are 3 kind of stmts may have store ops: GIMPLE_ASSIGN,
@@ -2138,7 +2138,8 @@ array_dse_edge::collect_array_accesses ()
for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
- if (gimple_clobber_p (stmt) || call_stmt_p (stmt))
+ if (gimple_clobber_p (stmt) || call_stmt_p (stmt)
+ || is_gimple_debug (stmt))
continue;
for (unsigned i = 0; i < gimple_num_ops (stmt); i++)
@@ -2495,7 +2496,8 @@ array_dse_edge::calc_read_bound ()
continue;
auto range = calc_ref_range (var);
- if (!integer_cst_p (range.max ()))
+ if (range.undefined_p() || range.varying_p()
+ || !integer_cst_p (range.max ()))
return false;
auto max = tree_to_shwi (range.max ());
@@ -3081,6 +3083,12 @@ ipa_array_dse::apply_array_dse (array_dse_edge *ad_edge)
cfun_saver save (caller);
+ if (dump_file)
+ {
+ fprintf (dump_file, "Remove fully redundant call:\n");
+ print_gimple_stmt (dump_file, call_stmt, 0);
+ }
+
auto gsi = gsi_for_stmt (call_stmt);
basic_block call_bb = gimple_bb (call_stmt);
tree fndecl = gimple_call_fndecl (call_stmt);
@@ -3103,6 +3111,12 @@ ipa_array_dse::apply_array_dse (array_dse_edge *ad_edge)
if (!transform_new_callee (callee, new_callee))
return false;
+ if (dump_file)
+ {
+ fprintf (dump_file, "Rewrite partial redundant call:\n");
+ print_gimple_stmt (dump_file, ad_edge->call_edge->call_stmt, 0);
+ }
+
tree bound_addr = ad_edge->get_bound_addr ();
rewrite_call_edge (ad_edge->call_edge, new_callee, bound_addr);
diff --git a/gcc/testsuite/gcc.dg/alignment-propagation/alignment-propagation.exp b/gcc/testsuite/gcc.dg/alignment-propagation/alignment-propagation.exp
new file mode 100644
index 00000000000..94184949980
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/alignment-propagation/alignment-propagation.exp
@@ -0,0 +1,35 @@
+# Copyright (C) 1997-2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# .
+
+load_lib gcc-dg.exp
+load_lib torture-options.exp
+
+# Initialize `dg'.
+dg-init
+torture-init
+
+set ALIGNMENT_PROPAGATION_TORTURE_OPTIONS [list \
+ { -O3 } \
+ { -Ofast } ]
+
+set-torture-options $ALIGNMENT_PROPAGATION_TORTURE_OPTIONS {{}}
+
+gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] \
+ "" "-fipa-alignment-propagation -fdump-ipa-alignment-propagation-details"
+
+# All done.
+torture-finish
+dg-finish
diff --git a/gcc/testsuite/gcc.dg/alignment-propagation/alignment_less_than_rhs.c b/gcc/testsuite/gcc.dg/alignment-propagation/alignment_less_than_rhs.c
new file mode 100644
index 00000000000..4695968e209
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/alignment-propagation/alignment_less_than_rhs.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+
+#include
+
+void __attribute__((__noinline__)) and_alignment_128(void *p) {
+ if ((unsigned long)p & 127)
+ abort();
+}
+
+int main() {
+ int num[16];
+ and_alignment_128(num);
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Rewrite" "alignment-propagation" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/alignment-propagation/non_aligned_offset.c b/gcc/testsuite/gcc.dg/alignment-propagation/non_aligned_offset.c
new file mode 100644
index 00000000000..f60f4dc18ac
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/alignment-propagation/non_aligned_offset.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+
+#include
+
+void __attribute__((__noinline__)) and_alignment_4(void *p) {
+ if (((unsigned long)p + 3) & 3)
+ abort();
+}
+
+int main() {
+ int num;
+ and_alignment_4(&num);
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Rewrite" "alignment-propagation" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/alignment-propagation/not_address_of_local_var.c b/gcc/testsuite/gcc.dg/alignment-propagation/not_address_of_local_var.c
new file mode 100644
index 00000000000..81d490c9291
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/alignment-propagation/not_address_of_local_var.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+
+#include
+
+void __attribute__((__noinline__)) and_alignment_4(void *p) {
+ if ((unsigned long)p & 3)
+ abort();
+}
+
+int main() {
+ int *p = NULL;
+ and_alignment_4(p);
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Rewrite" "alignment-propagation" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/alignment-propagation/rewrite_aligned_bit_and.c b/gcc/testsuite/gcc.dg/alignment-propagation/rewrite_aligned_bit_and.c
new file mode 100644
index 00000000000..bf381596410
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/alignment-propagation/rewrite_aligned_bit_and.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+
+#include
+
+void __attribute__((__noinline__)) and_alignment_4(void *p) {
+ if ((unsigned long)p & 3)
+ abort();
+}
+
+void __attribute__((__noinline__)) and_alignment_8(void *p) {
+ if ((unsigned long)p & 7)
+ abort();
+}
+
+int main() {
+ int num = 0;
+ int nums[16] = {0};
+ and_alignment_4(&num);
+ and_alignment_4(nums);
+ and_alignment_8(nums);
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump-times "Rewrite" 2 "alignment-propagation" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse.exp b/gcc/testsuite/gcc.dg/array-dse/array-dse.exp
new file mode 100644
index 00000000000..c04d54f000e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse.exp
@@ -0,0 +1,35 @@
+# Copyright (C) 1997-2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# .
+
+load_lib gcc-dg.exp
+load_lib torture-options.exp
+
+# Initialize `dg'.
+dg-init
+torture-init
+
+set ARRAY_DSE_TORTURE_OPTIONS [list \
+ { -O3 } \
+ { -Ofast } ]
+
+set-torture-options $ARRAY_DSE_TORTURE_OPTIONS {{}}
+
+gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] \
+ "" "-fipa-array-dse -fdump-ipa-array-dse-details"
+
+# All done.
+torture-finish
+dg-finish
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_callee_ptr_exceed_range.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_callee_ptr_exceed_range.c
new file mode 100644
index 00000000000..91db9ded743
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_callee_ptr_exceed_range.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ for (int* p = a + n; p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int main() {
+ int num[16];
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num, n);
+
+ for (unsigned i = 0; i < 8; i++) {
+ if (num[i] != i)
+ abort ();
+ }
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Fail finding array dse candidate callees" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_fully_redundant.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_fully_redundant.c
new file mode 100644
index 00000000000..c5d2dce58d2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_fully_redundant.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ for (int* p = a + (n - 1); p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int main() {
+ int num[16];
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num + 9, n);
+
+ for (unsigned i = 0; i < 8; i++) {
+ if (num[i] != i)
+ abort ();
+ }
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Remove fully redundant call" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_no_unique_length_param.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_no_unique_length_param.c
new file mode 100644
index 00000000000..cd20d1b52aa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_no_unique_length_param.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ for (int* p = a + (n - 1); p >= a; p--) {
+ *p = p - a;
+ }
+
+ int n1;
+ for (int* p = a + (n1 - 1); p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int main() {
+ int num[16];
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num, n);
+
+ for (unsigned i = 0; i < 8; i++) {
+ if (num[i] != i)
+ abort ();
+ }
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Fail finding array dse candidate callees" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_non_local_array_in_caller.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_non_local_array_in_caller.c
new file mode 100644
index 00000000000..99af6299c7e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_non_local_array_in_caller.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ for (int* p = a + (n - 1); p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int num[16];
+
+int main() {
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num, n);
+
+ for (unsigned i = 0; i < 8; i++) {
+ if (num[i] != i)
+ abort ();
+ }
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Fail finding array dse candidate edges" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_non_unique_written_base.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_non_unique_written_base.c
new file mode 100644
index 00000000000..8011371d901
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_non_unique_written_base.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ int *a1;
+ *a1 = 0;
+
+ for (int* p = a + (n - 1); p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int main() {
+ int num[16];
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num, n);
+
+ for (unsigned i = 0; i < 8; i++) {
+ if (num[i] != i)
+ abort ();
+ }
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Fail finding array dse candidate callees" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_partial_redundant.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_partial_redundant.c
new file mode 100644
index 00000000000..7eb4af33c40
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_partial_redundant.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ for (int* p = a + (n - 1); p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int main() {
+ int num[16];
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num, n);
+
+ for (unsigned i = 0; i < 8; i++) {
+ if (num[i] != i)
+ abort ();
+ }
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Rewrite partial redundant call" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/array-dse/array-dse_read_bound_non_const.c b/gcc/testsuite/gcc.dg/array-dse/array-dse_read_bound_non_const.c
new file mode 100644
index 00000000000..79cf118e962
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/array-dse/array-dse_read_bound_non_const.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+void __attribute__((__noinline__)) test(int* a, unsigned long n) {
+ for (int* p = a + (n - 1); p >= a; p--) {
+ *p = p - a;
+ }
+}
+
+int main() {
+ int num[16];
+ int n = 0;
+ scanf("%d", &n);
+ if (n)
+ test(num, n);
+
+ int n1;
+ if (num[n1])
+ abort();
+
+ return 0;
+}
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Fail finding array dse candidate edges" "array-dse" } } */
diff --git a/gcc/testsuite/gcc.dg/localize-array/bad-dominance.c b/gcc/testsuite/gcc.dg/localize-array/bad-dominance.c
new file mode 100644
index 00000000000..7f9ec730b4c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/bad-dominance.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+static int* p;
+
+void __attribute__((noinline)) test() {
+ for (unsigned i = 0; i < 10; i++) {
+ p[i] = i;
+ }
+}
+
+/* Set -O0 so that the ssa define by calloc and used by free
+ are not the same one. */
+#pragma GCC push_options
+#pragma GCC optimize("O0")
+int main() {
+ int n;
+ scanf("%d", &n);
+
+ p = calloc(10, sizeof(int));
+ for (unsigned i = 0; i < n; i++) {
+ test();
+ }
+ free(p);
+
+ return 0;
+}
+#pragma GCC pop_options
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Localize global array: p" "localize-array" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/localize-array/escape-by-taking-address.c b/gcc/testsuite/gcc.dg/localize-array/escape-by-taking-address.c
new file mode 100644
index 00000000000..17756c74d90
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/escape-by-taking-address.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+static int* p;
+int** a;
+
+void __attribute__((noinline)) test() {
+ a = &p;
+}
+
+/* Set -O0 so that the ssa define by calloc and used by free
+ are not the same one. */
+#pragma GCC push_options
+#pragma GCC optimize("O0")
+int main() {
+ p = calloc(10, sizeof(int));
+ test();
+ int ret = **a;
+ free(p);
+ return ret;
+}
+#pragma GCC pop_options
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Localize global array" "localize-array" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/localize-array/localize-array-with-multiple-elem.c b/gcc/testsuite/gcc.dg/localize-array/localize-array-with-multiple-elem.c
new file mode 100644
index 00000000000..6917fd45123
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/localize-array-with-multiple-elem.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+static int* p;
+
+void __attribute__((noinline)) test() {
+ for (unsigned i = 0; i < 10; i++) {
+ p[i] = i;
+ }
+}
+
+/* Set -O0 so that the ssa define by calloc and used by free
+ are not the same one. */
+#pragma GCC push_options
+#pragma GCC optimize("O0")
+int main() {
+ p = calloc(10, sizeof(int));
+ test();
+ free(p);
+ return 0;
+}
+#pragma GCC pop_options
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Localize global array: p" "localize-array" } } */
+/* { dg-final { scan-ipa-dump "Insert calloc statement" "localize-array" } } */
diff --git a/gcc/testsuite/gcc.dg/localize-array/localize-array-with-single-elem.c b/gcc/testsuite/gcc.dg/localize-array/localize-array-with-single-elem.c
new file mode 100644
index 00000000000..b79c12d2437
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/localize-array-with-single-elem.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+static int* p;
+
+void __attribute__((noinline)) test() {
+ p[0] = 0;
+}
+
+/* Set -O0 so that the ssa define by calloc and used by free
+ are not the same one. */
+#pragma GCC push_options
+#pragma GCC optimize("O0")
+int main() {
+ p = calloc(1, sizeof(int));
+ test();
+ free(p);
+ return 0;
+}
+#pragma GCC pop_options
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Localize global array: p" "localize-array" } } */
+/* { dg-final { scan-ipa-dump "Insert calloc statement" "localize-array" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/localize-array/localize-array.exp b/gcc/testsuite/gcc.dg/localize-array/localize-array.exp
new file mode 100644
index 00000000000..0bf6cc2cdbe
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/localize-array.exp
@@ -0,0 +1,35 @@
+# Copyright (C) 1997-2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# .
+
+load_lib gcc-dg.exp
+load_lib torture-options.exp
+
+# Initialize `dg'.
+dg-init
+torture-init
+
+set ALIGNMENT_PROPAGATION_TORTURE_OPTIONS [list \
+ { -O3 } \
+ { -Ofast } ]
+
+set-torture-options $ALIGNMENT_PROPAGATION_TORTURE_OPTIONS {{}}
+
+gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] \
+ "" "-fipa-localize-array -fdump-ipa-localize-array-details -fwhole-program"
+
+# All done.
+torture-finish
+dg-finish
diff --git a/gcc/testsuite/gcc.dg/localize-array/not-allocated-by-calloc.c b/gcc/testsuite/gcc.dg/localize-array/not-allocated-by-calloc.c
new file mode 100644
index 00000000000..e519a62d826
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/not-allocated-by-calloc.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+static int* p;
+
+void __attribute__((noinline)) test() {
+ for (unsigned i = 0; i < 10; i++) {
+ p[i] = i;
+ }
+}
+
+/* Set -O0 so that the ssa define by calloc and used by free
+ are not the same one. */
+#pragma GCC push_options
+#pragma GCC optimize("O0")
+int main() {
+ p = malloc(10 * sizeof(int));
+ test();
+ free(p);
+ return 0;
+}
+#pragma GCC pop_options
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Localize global array" "localize-array" { xfail *-*-* } } } */
diff --git a/gcc/testsuite/gcc.dg/localize-array/used-by-multiple-functions.c b/gcc/testsuite/gcc.dg/localize-array/used-by-multiple-functions.c
new file mode 100644
index 00000000000..1ac7969b64c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/localize-array/used-by-multiple-functions.c
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+
+#include
+#include
+
+static int* p;
+
+void __attribute__((noinline)) test() {
+ for (unsigned i = 0; i < 10; i++) {
+ p[i] = i;
+ }
+}
+
+void __attribute__((noinline)) test2() {
+ for (unsigned i = 0; i < 10; i++) {
+ p[i] = i;
+ }
+}
+
+/* Set -O0 so that the ssa define by calloc and used by free
+ are not the same one. */
+#pragma GCC push_options
+#pragma GCC optimize("O0")
+int main() {
+ p = calloc(10, sizeof(int));
+ test();
+ test2();
+ free(p);
+ return 0;
+}
+#pragma GCC pop_options
+
+/*--------------------------------------------------------------------------*/
+/* { dg-final { scan-ipa-dump "Localize global array: p" "localize-array" { xfail *-*-* } } } */
--
Gitee