public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Check whether a function is a pure function
@ 2023-10-17  9:20 Hanke Zhang
  2023-10-17  9:26 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Hanke Zhang @ 2023-10-17  9:20 UTC (permalink / raw)
  To: gcc

Hi, I'm trying to write a pass to erase some useless functions or to
put it another way, detect whether a function is pure or not. (Of
course I know some passes can do the clean after inline)

Here is the problem I got, If a function satisfy the following points,
can it be considered safe to delete?

1. return value is ignored or has no return
2. params are pure 32bit Integer Const or has no params
3. No use of global variable
4. No function call, assembly code, goto statement
5. No cast

And in my pass, I try to do these things through these points:

1. check GIMPLE_RETURN statement and check all the function call points
2. check DECL_PARAM(fndecl) and check the input params at all the
function call points
3. check that fndecl is not in the global_var->referring list
4. check GIMPLE_CALL, GIMPLE_ASM, GIMPLE_GOTO are not present
5. check that there is no difference between TREE_TYPE(lhs) and
TREE_TYPE(rhs1) types in the GIMPLE_ASSIGN statement

I would like to ask that if there are any omissions or errors in the
prerequisites and corresponding implementation plans I listed above.

Thanks
Hanke Zhang

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Check whether a function is a pure function
  2023-10-17  9:20 Check whether a function is a pure function Hanke Zhang
@ 2023-10-17  9:26 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-10-17  9:26 UTC (permalink / raw)
  To: Hanke Zhang; +Cc: gcc

On Tue, Oct 17, 2023 at 11:21 AM Hanke Zhang via Gcc <gcc@gcc.gnu.org> wrote:
>
> Hi, I'm trying to write a pass to erase some useless functions or to
> put it another way, detect whether a function is pure or not. (Of
> course I know some passes can do the clean after inline)
>
> Here is the problem I got, If a function satisfy the following points,
> can it be considered safe to delete?
>
> 1. return value is ignored or has no return
> 2. params are pure 32bit Integer Const or has no params
> 3. No use of global variable
> 4. No function call, assembly code, goto statement
> 5. No cast
>
> And in my pass, I try to do these things through these points:
>
> 1. check GIMPLE_RETURN statement and check all the function call points
> 2. check DECL_PARAM(fndecl) and check the input params at all the
> function call points
> 3. check that fndecl is not in the global_var->referring list
> 4. check GIMPLE_CALL, GIMPLE_ASM, GIMPLE_GOTO are not present
> 5. check that there is no difference between TREE_TYPE(lhs) and
> TREE_TYPE(rhs1) types in the GIMPLE_ASSIGN statement
>
> I would like to ask that if there are any omissions or errors in the
> prerequisites and corresponding implementation plans I listed above.

You can look into ipa-pure-const.cc which implements pure/const (and more)
discovery.  The DCE pass should, with this info, already be able to
"erase some useless function [calls]"

> Thanks
> Hanke Zhang

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-17  9:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-17  9:20 Check whether a function is a pure function Hanke Zhang
2023-10-17  9:26 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).