xtensa: constantsynth: Exclude the stack pointer

When updating the value of the stack pointer through a sequence of instruc-
tions, only the last instruction in the sequence must modify the stack
pointer, because the stack pointer may be referenced by an interrupt or
other event during the sequence:

     /* example */
     register void *stack_ptr asm ("a1");
     void test(void) {
       stack_ptr = (void *)0x04000000;
     }

     ;; before (-O -mabi=call0)
     test:
     	movi.n	sp, 1		;; An interrupt may occur
     	slli	sp, sp, 26	;; between these instructions
     	ret.n

This patch avoids this problem by excluding constant value assignments to
the stack pointer from 'constantsynth'.

     ;; after (-O -mabi=call0)
     	.literal_position
     	.literal .LC0, 67108864
     test:
     	l32r	sp, .LC0
     	ret.n

gcc/ChangeLog:

	* config/xtensa/xtensa.cc (constantsynth_pass1):
	Add the case where the assignment destination is a stack pointer
	to the exclusion criteria for processing.
This commit is contained in:
Takayuki 'January June' Suwa
2026-02-20 21:17:15 +09:00
committed by Max Filippov
parent 58784833e8
commit 5cc1d83209

View File

@@ -6028,7 +6028,7 @@ constantsynth_pass1 (rtx_insn *insn, constantsynth_info &info)
constant. */
if (GET_CODE (pat = PATTERN (insn)) != SET
|| ! REG_P (dest = SET_DEST (pat)) || ! GP_REG_P (REGNO (dest))
|| GET_MODE (dest) != SImode
|| GET_MODE (dest) != SImode || rtx_equal_p (dest, stack_pointer_rtx)
|| ! CONST_INT_P (src = avoid_constant_pool_reference (SET_SRC (pat))))
return false;