https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127557

Chenghui Pan <panchenghui at loongson dot cn> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |panchenghui at loongson dot cn

--- Comment #1 from Chenghui Pan <panchenghui at loongson dot cn> ---
Created attachment 66381
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66381&action=edit
Newly reduced test file for the ICE

The original test file seems cannot trigger the ICE after the commit
325d217fb12396db32b56f3ffe24977642a584cb, so I generate a new reduced test file
with coding agent's help, and ICE can be triggered with newly attched file with
option "-O3 -funroll-loops -g".

RTL expression that triggers the crash is:

(ashift:DI (sign_extract:DI (reg:DI 34 $f2 [orig:101 maxIndexRCF ] [101])
        (const_int 30 [0x1e])
        (const_int 0 [0]))
    (const_int 2 [0x2]))

and is being produced while trying to expand a debug insn:

(subreg:SI (ashift:DI (sign_extract:DI (debug_expr:DI D#11)
            (const_int 30 [0x1e])
            (const_int 0 [0]))
        (const_int 2 [0x2])) 0)

It seems like cselib/vartrack tries to get a SImode subreg of DImode FPR with
gen_lowpart() call at simplify-rtx.cc:5099, but in LoongArch's definition, this
operation is not allowed, so the gen_lowpart() call tries to generate a new
pseudo register and copy FPR's value to it:

   5086           /* Now we need to verify the size of the bitfield plus the
shift                                                                           
   5087              count is an integral mode and smaller than MODE.  This is  
   5088              requirement for using SIGN_EXTEND.  We also need to verify
the                                                                             
   5089              field starts at bit location 0 and that the subreg lowpart
also                                                                            
   5090              starts at zero.  */                                        
   5091           if (int_mode_for_size (size, size).exists (&smaller_mode)     
   5092               && mode > smaller_mode                                    
   5093               && (subreg_lowpart_offset (smaller_mode,
mode).to_constant ()                                                            
   5094                   == UINTVAL (XEXP (op0, 2)))                           
   5095               && XEXP (op0, 2) == CONST0_RTX (mode))                    
   5096             {                                                           
   5097               /* Everything passed.  So we just need to get the subreg
of the                                                                          
   5098                  original input, shift it and sign extend the result. 
*/                                                                              
   5099               rtx op = gen_lowpart (smaller_mode, XEXP (op0, 0));       
   5100               rtx x = gen_rtx_ASHIFT (smaller_mode, op, op1);           
   5101               return gen_rtx_SIGN_EXTEND (mode, x);                     
   5102             }                                                          

but the pseudo register generation is not allowed in post-reload stage, so it
crashed.

This ICE seems like not a machine target problem? This triggering point of ICE
in simplify-rtx.cc is introduced in commit bde7b018d45 and tries to do some
optimization for RISC-V. The original commit message is worried about LoongArch
and it seems happens...

Base on my understanding, RISC-V also does not allow get a SImode subreg of
DImode FPR too, but the insn never be generated like the one above with RISC-V
target's RTL optimization combination; but LoongArch has the optimization for
the optimization for bitwise operation and left shift with template
'define_insn_and_split "<optab>_shift_reverse<X:mode>"', and RTL template
itself seems like no problem at all.

The ICE can be fixed by:

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index fb3a83bd3ae..beb8740d4f6 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -5096,7 +5096,10 @@ simplify_ashift:
            {
              /* Everything passed.  So we just need to get the subreg of the
                 original input, shift it and sign extend the result.  */
-             rtx op = gen_lowpart (smaller_mode, XEXP (op0, 0));
+             rtx op = gen_lowpart_if_possible (smaller_mode,
+                                               XEXP (op0, 0));
+             if (!op)
+               goto canonicalize_shift;
              rtx x = gen_rtx_ASHIFT (smaller_mode, op, op1);
              return gen_rtx_SIGN_EXTEND (mode, x);
            }


But I'm not sure it's the correct fix. Any other advice?

Reply via email to