From 9032d2b2414ed22e53a9980a51b835d3caf83c48 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Sat, 19 Dec 2020 12:46:40 +0100
Subject: [PATCH] bswap: Fix up a thinko with empty CONSTRUCTORs [PR98378]

The code I've added recently in find_bswap_or_nop for VECTOR CONSTRUCTORs
is missing punt on an important case - namely empty CONSTRUCTORs, because in that
case the loop will not initialize *n and the code after the loop will then
use the uninitialized structure.

2020-12-19  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/98378
	* gimple-ssa-store-merging.c (find_bswap_or_nop): Punt if CONSTRUCTOR
	has no elements.
---
 gcc/gimple-ssa-store-merging.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/gcc/gimple-ssa-store-merging.c b/gcc/gimple-ssa-store-merging.c
index 2894205fa3c3..2ab51e23c077 100644
--- a/gcc/gimple-ssa-store-merging.c
+++ b/gcc/gimple-ssa-store-merging.c
@@ -873,6 +873,8 @@ find_bswap_or_nop (gimple *stmt, struct symbolic_number *n, bool *bswap)
       if (sz != 16 && sz != 32 && sz != 64)
 	return NULL;
       tree rhs = gimple_assign_rhs1 (stmt);
+      if (CONSTRUCTOR_NELTS (rhs) == 0)
+	return NULL;
       tree eltype = TREE_TYPE (TREE_TYPE (rhs));
       unsigned HOST_WIDE_INT eltsz
 	= int_size_in_bytes (eltype) * BITS_PER_UNIT;
-- 
GitLab