On Mon, Jun 2, 2025 at 4:45 PM Richard Sandiford <richard.sandif...@arm.com> wrote: > > "H.J. Lu" <hjl.to...@gmail.com> writes: > > On Mon, Jun 2, 2025 at 4:07 PM Richard Sandiford > > <richard.sandif...@arm.com> wrote: > >> > >> "H.J. Lu" <hjl.to...@gmail.com> writes: > >> > Since not all CALL instructions in RTL passes have a REG_CALL_DECL note, > >> > update get_call_fndecl to also check function symbol for function > >> > declaration so that it can be used on CALL instructions like > >> > > >> > (call_insn 39 38 61 7 (set (reg:SI 0 ax) > >> > (call (mem:QI (symbol_ref:DI ("foo") [flags 0x3] > >> > <function_decl 0x7fffe96da900 foo>) [0 foo S1 A8]) > >> > (const_int 0 [0]))) "pr92080-15.c":24:9 1480 {*call_value} > >> > (expr_list:REG_DEAD (reg:SI 5 di) > >> > (expr_list:REG_DEAD (reg:SI 4 si) > >> > (expr_list:REG_EH_REGION (const_int 0 [0]) > >> > (nil)))) > >> > (expr_list:SI (use (reg:SI 5 di)) > >> > (expr_list:SI (use (reg:SI 4 si)) > >> > (nil)))) > >> > > >> > PR other/120494 > >> > * rtlanal.cc (get_call_fndecl): Also check function symbol to > >> > get function declaration. > >> > >> What's your use case for this? I think we should instead move to making > >> the notes more generally available, since looking at the call rtx won't > >> work for targets that need indirect calls (e.g. for -mlong-calls). > > > > I am working on a pass which needs to check if CALL insn is a recursive > > call. > > I assume false negatives are allowed? That is, "false" means "might be > recursive, might not"? > > > Currently I have > > > > * Get the declaration of the function called by INSN. If INDIRECT_P > > is true, also get indirect declaration. */ > > > > static tree > > get_call_fndecl_from_rtx (const rtx_insn *insn, bool indirect_p = false) > > { > > rtx note = find_reg_note (insn, REG_CALL_DECL, NULL_RTX); > > if (note) > > { > > rtx datum = XEXP (note, 0); > > if (datum) > > return SYMBOL_REF_DECL (datum); > > } > > rtx call = get_call_rtx_from (insn); > > rtx fnaddr = XEXP (call, 0); > > rtx addr = XEXP (fnaddr, 0); > > if (GET_CODE (addr) == SYMBOL_REF) > > { > > tree fndecl = SYMBOL_REF_DECL (addr); > > if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL) > > return fndecl; > > } > > return indirect_p ? MEM_EXPR (fnaddr) : nullptr; > > } > > > > if (CALL_P (insn)) > > { > > tree fndecl = get_call_fndecl_from_rtx (insn); > > if (fndecl == current_function_decl > > && decl_binds_to_current_def_p (fndecl)) > > { > > recursive_call_p = true; > > break; > > } > > } > > > > If get_call_fndecl works without a REG_CALL_DECL note, I can use it > > instead of writing my own. > > I think we should consider removing the flag_ipa_ra guards around > the code that adds REG_CALL_DECLs. >
Like this? Always add REG_CALL_DECL note for CALL so that get_call_fndecl works without -fipa-ra. PR other/120494 * calls.cc (expand_call): Always add REG_CALL_DECL note. (emit_library_call_value_1): Likewise. Thanks. -- H.J.
From 0893b859c32e07cc96f592f32b8f4d74998ddea7 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Tue, 3 Jun 2025 05:56:37 +0800 Subject: [PATCH] Always add REG_CALL_DECL note for CALL Always add REG_CALL_DECL note for CALL so that get_call_fndecl works without -fipa-ra. PR other/120494 * calls.cc (expand_call): Always add REG_CALL_DECL note. (emit_library_call_value_1): Likewise. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> --- gcc/calls.cc | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/gcc/calls.cc b/gcc/calls.cc index 164f3c515d9..e16190c12a6 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -3736,19 +3736,16 @@ expand_call (tree exp, rtx target, int ignore) next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage, flags, args_so_far); - if (flag_ipa_ra) + rtx_call_insn *last; + rtx datum = NULL_RTX; + if (fndecl != NULL_TREE) { - rtx_call_insn *last; - rtx datum = NULL_RTX; - if (fndecl != NULL_TREE) - { - datum = XEXP (DECL_RTL (fndecl), 0); - gcc_assert (datum != NULL_RTX - && GET_CODE (datum) == SYMBOL_REF); - } - last = last_call_insn (); - add_reg_note (last, REG_CALL_DECL, datum); + datum = XEXP (DECL_RTL (fndecl), 0); + gcc_assert (datum != NULL_RTX + && GET_CODE (datum) == SYMBOL_REF); } + last = last_call_insn (); + add_reg_note (last, REG_CALL_DECL, datum); /* If the call setup or the call itself overlaps with anything of the argument setup we probably clobbered our call address. @@ -4804,13 +4801,10 @@ emit_library_call_value_1 (int retval, rtx orgfun, rtx value, struct_value_size, call_cookie, valreg, old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far); - if (flag_ipa_ra) - { - rtx datum = orgfun; - gcc_assert (GET_CODE (datum) == SYMBOL_REF); - rtx_call_insn *last = last_call_insn (); - add_reg_note (last, REG_CALL_DECL, datum); - } + rtx datum = orgfun; + gcc_assert (GET_CODE (datum) == SYMBOL_REF); + rtx_call_insn *last = last_call_insn (); + add_reg_note (last, REG_CALL_DECL, datum); /* Right-shift returned value if necessary. */ if (!pcc_struct_value -- 2.49.0