Jakub Jelinek 45f3501bda switchconv: Fix up inbound checks for switch types wider than sizetype [PR122943]
In r14-8302 I've changed switchconv to narrow the CONSTRUCTOR indexes and
the runtime SSA_NAME indexing into the arrays to at most sizetype for
types wider than that (__int128, large _BitInt, for -m32 long long too).
The switchconv partitioning ensures that one partition isn't larger than
that and having CONSTRUCTOR with _BitInt(1024) indexes was causing all kinds
of problems.

Unfortunately, as the following testcase shows, while doing that is
desirable, the later gen_inbound_check call uses the lhs of m_arr_ref_first
statement to determine the type and value that should be compared for the
inbound check (against the highest possible bound cast to the lhs type).
So the PR113491 r14-8302 change broke those inbound checks, instead of
being done in unsigned type corresponding to the precision of the switch
expression they are now sometimes done using sizetype.  That is of course
wrong.

So the following patch fixes it by doing the tidx computation in steps,
one is the utype subtraction, which has m_arr_ref_first as the last
instruction, and then if needed there is a cast to sizetype if utype is
wider than that.  When gen_inbound_check is called, it adds the inbound
check after the m_arr_ref_first instruction and the additional cast is
then inside of the guarded block.

So e.g. in bar for -m32 this patch changes:
 unsigned char bar (long long int val)
 {
   unsigned char result;
-  sizetype _7;
+  sizetype _6;
+  long long unsigned int _7;

   <bb 2> :
-  _7 = (sizetype) val_3(D);
+  _7 = (long long unsigned int) val_3(D);
   if (_7 <= 2)
     goto <bb 4>; [INV]
   else
     goto <bb 3>; [INV]

   <bb 3> :
 <L7>:
-  result_5 = 1;
+  result_4 = 1;
   goto <bb 5>; [100.00%]

   <bb 4> :
 <L8>:
-  result_6 = CSWTCH.2[_7];
+  _6 = (sizetype) _7;
+  result_5 = CSWTCH.2[_6];

   <bb 5> :
-  # result_1 = PHI <result_6(4), result_5(3)>
+  # result_1 = PHI <result_5(4), result_4(3)>
 <L9>:
 <L6>:
   return result_1;

 }

2025-12-03  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/122943
	* tree-switch-conversion.cc (switch_conversion::build_arrays):
	Always gimplify subtraction in utype without cast to tidxtype
	and set m_arr_ref_first to the last stmt of that.  Remove unneeded
	update_stmt call.  If tidxtype is not utype, append after that stmt
	cast to tidxtype and set tidx to the lhs of that cast.

	* gcc.c-torture/execute/pr122943.c: New test.
2025-12-03 16:07:29 +01:00
2025-12-01 00:16:36 +00:00
2025-12-02 09:47:45 +00:00
2025-08-29 00:19:55 +00:00
2025-12-02 09:47:45 +00:00
2025-11-20 00:19:40 +00:00
2025-11-07 00:20:18 +00:00
2025-12-02 09:47:45 +00:00
2025-11-22 00:20:52 +00:00
2025-11-11 00:21:25 +00:00
2025-11-04 00:20:26 +00:00
2025-12-01 11:41:53 +01:00
2025-11-28 00:21:32 +00:00
2025-11-30 00:16:27 +00:00
2025-11-12 00:22:41 +00:00
2025-11-24 00:18:45 +00:00
2025-11-29 12:28:38 +01:00
2025-12-02 09:47:45 +00:00
2025-11-04 00:20:26 +00:00
2025-12-01 00:16:36 +00:00
2025-11-20 00:19:40 +00:00
2025-11-04 00:20:26 +00:00
2025-11-04 00:20:26 +00:00
2025-11-04 00:20:26 +00:00
2025-12-03 00:16:28 +00:00
2025-11-04 00:20:26 +00:00
2025-11-04 00:20:26 +00:00
2025-11-04 00:20:26 +00:00
2025-09-02 00:19:26 +00:00
2025-11-04 00:20:26 +00:00
2025-12-01 11:41:53 +01:00
2025-10-11 11:08:01 +02:00
2025-11-30 01:52:27 +01:00
2025-11-30 01:52:27 +01:00
2025-11-30 01:52:24 +01:00
2025-11-30 01:52:27 +01:00
2025-11-30 01:52:27 +01:00
2025-11-30 01:52:27 +01:00
2025-11-30 01:52:24 +01:00
2025-10-11 11:08:01 +02:00

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.
Description
No description provided
Readme 4.2 GiB
Languages
C++ 30.7%
C 30.2%
Ada 14.4%
D 6.1%
Go 5.7%
Other 12.4%