GCC Middle and Back End API Reference
|
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "tree-streamer.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "calls.h"
#include "cfganal.h"
#include "tree-eh.h"
#include "gimple-iterator.h"
#include "gimple-walk.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-niter.h"
#include "langhooks.h"
#include "ipa-utils.h"
#include "gimple-pretty-print.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "intl.h"
#include "opts.h"
#include "ssa.h"
#include "alloc-pool.h"
#include "symbol-summary.h"
#include "sreal.h"
#include "ipa-cp.h"
#include "ipa-prop.h"
#include "ipa-fnsummary.h"
#include "symtab-thunks.h"
#include "dbgcnt.h"
#include "gcc-urlifier.h"
Data Structures | |
class | funct_state_d |
class | funct_state_summary_t |
Macros | |
#define | DUMP_AND_RETURN(reason) |
Typedefs | |
typedef class funct_state_d * | funct_state |
Enumerations | |
enum | pure_const_state_e { IPA_CONST , IPA_PURE , IPA_NEITHER } |
enum | malloc_state_e { STATE_MALLOC_TOP , STATE_MALLOC , STATE_MALLOC_BOTTOM } |
Variables | |
static const char * | pure_const_names [3] = {"const", "pure", "neither"} |
static const char * | malloc_state_names [] = {"malloc_top", "malloc", "malloc_bottom"} |
static funct_state_summary_t * | funct_state_summaries = NULL |
#define DUMP_AND_RETURN | ( | reason | ) |
malloc_candidate_p() checks if FUN can possibly be annotated with malloc attribute. Currently this function does a very conservative analysis. FUN is considered to be a candidate if 1) It returns a value of pointer type. 2) SSA_NAME_DEF_STMT (return_value) is either a function call or a phi, and element of phi is either NULL or SSA_NAME_DEF_STMT(element) is function call. 3) The return-value has immediate uses only within comparisons (gcond or gassign) and return_stmt (and likewise a phi arg has immediate use only within comparison or the phi stmt).
Referenced by malloc_candidate_p(), and malloc_candidate_p_1().
typedef class funct_state_d* funct_state |
enum malloc_state_e |
enum pure_const_state_e |
Callgraph based analysis of static variables. Copyright (C) 2004-2024 Free Software Foundation, Inc. Contributed by Kenneth Zadeck <zadeck@naturalbridge.com> This file is part of GCC. GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see <http://www.gnu.org/licenses/>.
This file marks functions as being either const (TREE_READONLY) or pure (DECL_PURE_P). It can also set a variant of these that are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P). This must be run after inlining decisions have been made since otherwise, the local sets will not contain information that is consistent with post inlined state. The global sets are not prone to this problem since they are by definition transitive.
The code in this module is called by the ipa pass manager. It should be one of the later passes since it's information is used by the rest of the compilation.
Lattice values for const and pure functions. Everything starts out being const, then may drop to pure and then neither depending on what is found.
Enumerator | |
---|---|
IPA_CONST | |
IPA_PURE | |
IPA_NEITHER |
|
static |
This is the main routine for finding the reference patterns for global variables within a function FN.
References symtab_node::alias, better_state(), cgraph_node::cannot_return_p(), cfun, check_stmt(), symtab_node::decl, DECL_IS_MALLOC, DECL_STRUCT_FUNCTION, dump_file, dump_flags, symtab_node::dump_name(), end(), finite_function_p(), flags_from_decl_or_type(), FOR_EACH_BB_FN, gcc_assert, thunk_info::get(), walk_stmt_info::gsi, gsi_end_p(), gsi_next(), gsi_start_bb(), gsi_stmt(), infer_nonnull_range_by_dereference(), IPA_CONST, IPA_NEITHER, IPA_PURE, malloc_candidate_p(), null_pointer_node, pop_cfun(), push_cfun(), state_from_flags(), STATE_MALLOC, STATE_MALLOC_BOTTOM, STATE_MALLOC_TOP, stmt_can_throw_external(), TDF_DETAILS, cgraph_node::thunk, TREE_NOTHROW, and thunk_info::virtual_offset_p.
Referenced by funct_state_summary_t::insert(), modref_summaries::insert(), modref_summaries_lto::insert(), and pure_const_generate_summary().
|
inlinestatic |
Merge STATE and STATE2 and LOOPING and LOOPING2 and store into STATE and LOOPING better of the two variants. Be sure to merge looping correctly. IPA_NEITHER functions have looping 0 even if they don't have to return.
References IPA_NEITHER, and MIN.
Referenced by analyze_function(), and propagate_pure_const().
Recognize special cases of builtins that are by themselves not const but function using them is.
References BUILT_IN_NORMAL, CASE_BUILT_IN_ALLOCA, DECL_BUILT_IN_CLASS, and DECL_FUNCTION_CODE().
Referenced by check_call(), and propagate_pure_const().
|
static |
Return true if N is cdtor that is not const or pure. In this case we may need to remove unreachable function if it is marked const/pure.
References symtab_node::decl, DECL_LOOPING_CONST_OR_PURE_P, DECL_PURE_P, DECL_STATIC_CONSTRUCTOR, DECL_STATIC_DESTRUCTOR, and TREE_READONLY.
Referenced by ipa_make_function_const(), and ipa_make_function_pure().
|
static |
Check the parameters of a function call to CALL_EXPR to see if there are any references in the parameters that are not allowed for pure or const functions. Also check to see if this is either an indirect call, a call outside the compilation unit, or has special attributes that may also effect the purity. The CALL_EXPR node for the entire call expression.
References BUILT_IN_NORMAL, builtin_safe_for_const_function_p(), cfun, current_function_decl, DECL_ASSEMBLER_NAME, DECL_BUILT_IN_CLASS, DECL_FUNCTION_CODE(), dump_file, dump_flags, ECF_NORETURN, ECF_NOTHROW, gimple_call_builtin_p(), gimple_call_flags(), gimple_call_fndecl(), gimple_call_internal_p(), gimple_num_ops(), gimple_op(), i, IDENTIFIER_POINTER, IPA_CONST, IPA_NEITHER, lookup_stmt_eh_lp(), nonfreeing_call_p(), NULL, recursive_call_p(), setjmp_call_p(), state_from_flags(), stmt_can_throw_external(), stmt_could_throw_p(), TDF_DETAILS, tree_could_throw_p(), and worse_state().
Referenced by check_stmt().
|
inlinestatic |
Check to see if the use (or definition when CHECKING_WRITE is true) variable T is legal in a function that is either pure or const.
References DECL_EXTERNAL, DECL_PRESERVE_P, dump_file, IPA_CONST, IPA_NEITHER, IPA_PURE, TREE_PUBLIC, TREE_READONLY, TREE_STATIC, and TREE_THIS_VOLATILE.
Referenced by check_ipa_load(), check_ipa_store(), check_load(), and check_store().
Wrapper around check_decl for loads in ipa mode.
References check_decl(), check_op(), and DECL_P.
Referenced by check_stmt().
Wrapper around check_decl for stores in ipa mode.
References check_decl(), check_op(), and DECL_P.
Referenced by check_stmt().
Wrapper around check_decl for loads in local more.
References check_decl(), check_op(), and DECL_P.
Referenced by check_stmt().
|
inlinestatic |
Check to see if the use (or definition when CHECKING_WRITE is true) variable T is legal in a function that is either pure or const.
References dump_file, get_base_address(), IPA_CONST, IPA_NEITHER, IPA_PURE, refs_local_or_readonly_memory_p(), and TREE_THIS_VOLATILE.
Referenced by check_ipa_load(), check_ipa_store(), check_load(), and check_store().
Check that RETVAL is used only in STMT and in comparisons against 0. RETVAL is return value of the function and STMT is return stmt.
References dyn_cast(), FOR_EACH_IMM_USE_STMT, gimple_assign_rhs2(), gimple_assign_rhs_code(), gimple_cond_rhs(), integer_zerop(), is_gimple_debug(), tcc_comparison, and TREE_CODE_CLASS.
Referenced by malloc_candidate_p_1().
|
static |
Look into pointer pointed to by GSIP and figure out what interesting side effects it has.
References as_a(), cfun, check_call(), check_ipa_load(), check_ipa_store(), check_load(), check_store(), DECL_NONLOCAL, dump_file, gimple_asm_clobbers_memory_p(), gimple_asm_volatile_p(), gimple_clobber_p(), gimple_has_volatile_ops(), gimple_label_label(), gsi_stmt(), IPA_NEITHER, is_gimple_debug(), print_gimple_stmt(), stmt_can_throw_external(), stmt_could_throw_p(), and walk_stmt_load_store_ops().
Referenced by analyze_function().
Wrapper around check_decl for stores in local more.
References check_decl(), check_op(), and DECL_P.
Referenced by check_stmt().
|
static |
Debugging function to dump state of malloc lattice.
References dump_file, symtab_node::dump_name(), FOR_EACH_FUNCTION, funct_state_summaries, and malloc_state_names.
Referenced by propagate_malloc().
bool finite_function_p | ( | ) |
Return true if function is known to be finite.
References cfun, dump_file, dump_flags, finite_loop_p(), flow_loops_dump(), loop_optimizer_finalize(), loop_optimizer_init(), LOOPS_HAVE_PREHEADERS, LOOPS_HAVE_RECORDED_EXITS, LOOPS_HAVE_SIMPLE_LATCHES, mark_dfs_back_edges(), mark_irreducible_loops(), NULL, loop::num, scev_finalize(), scev_initialize(), and TDF_DETAILS.
Referenced by analyze_function().
Try to guess if function body will always be visible to compiler when compiling the call and whether compiler will be able to propagate the information by itself.
References DECL_COMDAT, DECL_DECLARED_INLINE_P, and TREE_PUBLIC.
Referenced by suggest_attribute().
|
static |
|
static |
We only propagate across edges that can throw externally and their callee is not interposable.
References AVAIL_INTERPOSABLE, symtab_node::binds_to_current_def_p(), cgraph_edge::callee, cgraph_edge::caller, cgraph_edge::can_throw_external, symtab_node::decl, cgraph_node::function_or_virtual_thunk_symbol(), opt_for_fn, and TREE_NOTHROW.
Referenced by propagate_nothrow().
|
static |
Skip edges from and to nodes without ipa_pure_const enabled. Ignore not available symbols.
References AVAIL_INTERPOSABLE, cgraph_edge::callee, cgraph_edge::caller, symtab_node::decl, cgraph_node::function_or_virtual_thunk_symbol(), and opt_for_fn.
Referenced by propagate_pure_const().
bool ipa_make_function_const | ( | struct cgraph_node * | node, |
bool | looping, | ||
bool | local ) |
Make function const and output warning. If LOCAL is true, return true if anything changed. Otherwise return true if we may have introduced removale ctors.
References cgraph_node::call_for_symbol_and_aliases(), cdtor_p(), dbg_cnt(), symtab_node::decl, DECL_LOOPING_CONST_OR_PURE_P, dump_file, symtab_node::dump_name(), NULL, cgraph_node::set_const_flag(), skip_function_for_local_pure_const(), TREE_READONLY, and warn_function_const().
Referenced by propagate_pure_const().
bool ipa_make_function_pure | ( | struct cgraph_node * | node, |
bool | looping, | ||
bool | local ) |
Make function const and output warning. If LOCAL is true, return true if anything changed. Otherwise return true if we may have introduced removale ctors.
References cgraph_node::call_for_symbol_and_aliases(), cdtor_p(), dbg_cnt(), symtab_node::decl, DECL_LOOPING_CONST_OR_PURE_P, DECL_PURE_P, dump_file, symtab_node::dump_name(), NULL, cgraph_node::set_pure_flag(), skip_function_for_local_pure_const(), TREE_READONLY, and warn_function_pure().
Referenced by propagate_pure_const().
ipa_opt_pass_d * make_pass_ipa_pure_const | ( | gcc::context * | ctxt | ) |
gimple_opt_pass * make_pass_local_pure_const | ( | gcc::context * | ctxt | ) |
gimple_opt_pass * make_pass_nothrow | ( | gcc::context * | ctxt | ) |
gimple_opt_pass * make_pass_warn_function_noreturn | ( | gcc::context * | ctxt | ) |
References function::decl, DECL_NAME, DUMP_AND_RETURN, dump_file, dump_flags, dyn_cast(), EDGE_COUNT, EXIT_BLOCK_PTR_FOR_FN, FOR_EACH_EDGE, cgraph_node::get_create(), gimple_return_retval(), gsi_last_bb(), gsi_stmt(), IDENTIFIER_POINTER, malloc_candidate_p_1(), basic_block_def::preds, TDF_DETAILS, TREE_CODE, TREE_TYPE, and visited.
Referenced by analyze_function().
|
static |
References bitmap_set_bit, check_retval_uses(), function::decl, DECL_IS_MALLOC, DUMP_AND_RETURN, dyn_cast(), cgraph_node::get_create(), cgraph_node::get_edge(), gimple_call_fndecl(), gimple_phi_arg_def(), gimple_phi_num_args(), i, integer_zerop(), ipa_call_summaries, is_a(), ipa_call_summary::is_return_callee_uncaptured, malloc_candidate_p_1(), SSA_NAME_DEF_STMT, SSA_NAME_VERSION, TREE_CODE, and visited.
Referenced by malloc_candidate_p(), and malloc_candidate_p_1().
|
static |
Propagate malloc attribute across the callgraph.
References symtab_node::alias, AVAIL_INTERPOSABLE, cgraph_node::callees, symbol_table::cgraph_count, changed, symtab_node::decl, DECL_IS_MALLOC, symtab_node::definition, dump_file, dump_flags, dump_malloc_lattice(), symtab_node::dump_name(), FOR_EACH_DEFINED_FUNCTION, FOR_EACH_FUNCTION, free(), funct_state_summaries, cgraph_node::get_availability(), i, cgraph_node::indirect_calls, cgraph_node::inlined_to, ipa_call_summaries, ipa_free_postorder_info(), ipa_reverse_postorder(), ipa_call_summary::is_return_callee_uncaptured, cgraph_edge::next_callee, cgraph_node::set_malloc_flag(), STATE_MALLOC, STATE_MALLOC_BOTTOM, symtab, TDF_DETAILS, TREE_TYPE, VOID_TYPE_P, and warn_function_malloc().
|
static |
Produce transitive closure over the callgraph and compute nothrow attributes.
References symtab_node::alias, symtab_node::aux, AVAIL_INTERPOSABLE, symtab_node::binds_to_current_def_p(), cgraph_edge::callee, cgraph_node::callees, cgraph_edge::caller, cgraph_edge::can_throw_external, symbol_table::cgraph_count, symtab_node::decl, cgraph_node::dump_cgraph(), dump_file, symtab_node::dump_name(), cgraph_indirect_call_info::ecf_flags, ECF_NOTHROW, free(), funct_state_summaries, cgraph_node::get_availability(), i, ignore_edge_for_nothrow(), cgraph_node::indirect_calls, cgraph_edge::indirect_info, cgraph_node::inlined_to, ipa_free_postorder_info(), ipa_print_order(), ipa_reduced_postorder(), cgraph_edge::next_callee, ipa_dfs_info::next_cycle, opt_for_fn, cgraph_node::set_nothrow_flag(), symtab, TREE_NOTHROW, and y.
|
static |
Produce transitive closure over the callgraph and compute pure/const attributes.
References symtab_node::alias, symtab_node::aux, AVAIL_INTERPOSABLE, better_state(), builtin_safe_for_const_function_p(), cgraph_edge::callee, cgraph_node::callees, cgraph_edge::caller, ipa_ref::cannot_lead_to_return(), cgraph_edge::cannot_lead_to_return_p(), symbol_table::cgraph_count, count, symtab_node::decl, cgraph_node::dump_cgraph(), dump_file, dump_flags, symtab_node::dump_name(), cgraph_indirect_call_info::ecf_flags, flags_from_decl_or_type(), free(), funct_state_summaries, gcc_unreachable, cgraph_node::get_availability(), i, ignore_edge_for_pure_const(), cgraph_node::indirect_calls, cgraph_edge::indirect_info, cgraph_node::inlined_to, IPA_CONST, ipa_free_postorder_info(), ipa_make_function_const(), ipa_make_function_pure(), IPA_NEITHER, ipa_print_order(), IPA_PURE, ipa_reduced_postorder(), IPA_REF_ADDR, IPA_REF_LOAD, IPA_REF_STORE, symtab_node::iterate_reference(), cgraph_edge::next_callee, ipa_dfs_info::next_cycle, cgraph_node::nonfreeing_fn, NULL, pure_const_names, cgraph_edge::recursive_p(), ipa_ref::referred, self_recursive_p(), state_from_flags(), symtab, TDF_DETAILS, TREE_READONLY, ipa_ref::use, worse_state(), and y.
|
static |
Analyze each function in the cgraph to see if it is locally PURE or CONST.
References a, analyze_function(), current_pass, symtab_node::decl, FOR_EACH_DEFINED_FUNCTION, free(), funct_state_summaries, and opt_for_fn.
|
static |
Deserialize the ipa info for lto.
References bp_unpack_value(), count, current_pass, symtab_node::decl, dump_file, symtab_node::dump_name(), dyn_cast(), ECF_CONST, ECF_NOTHROW, ECF_PURE, flags_from_decl_or_type(), funct_state_summaries, i, lto_create_simple_input_block(), lto_destroy_simple_input_block(), lto_get_file_decl_data(), LTO_section_ipa_pure_const, lto_symtab_encoder_deref(), malloc_state_names, pure_const_names, streamer_read_bitpack(), streamer_read_uhwi(), and lto_file_decl_data::symtab_node_encoder.
|
static |
Serialize the ipa info for lto.
References bitpack_create(), bp_pack_value(), funct_state_d::can_free, funct_state_d::can_throw, count, lto_simple_output_block::decl_state, symtab_node::definition, funct_state_summaries, funct_state_d::looping, funct_state_d::looping_previously_known, lsei_cgraph_node(), lsei_end_p(), lsei_next_function_in_partition(), lsei_start_function_in_partition(), lto_create_simple_output_block(), lto_destroy_simple_output_block(), lto_get_out_decl_state(), LTO_section_ipa_pure_const, lto_symtab_encoder_encode(), lto_simple_output_block::main_stream, funct_state_d::malloc_state, NULL, funct_state_d::pure_const_state, funct_state_d::state_previously_known, streamer_write_bitpack(), streamer_write_uhwi_stream(), and lto_out_decl_state::symtab_node_encoder.
|
static |
Return true if NODE is self recursive function. Indirectly recursive functions appears as non-trivial strongly connected components, so we need to care about self recursion only.
References cgraph_edge::callee, cgraph_node::callees, cgraph_node::function_symbol(), and cgraph_edge::next_callee.
Referenced by propagate_pure_const().
|
static |
Return true if function should be skipped for local pure const analysis.
References AVAIL_INTERPOSABLE, dump_file, function_called_by_processed_nodes_p(), cgraph_node::get_availability(), and symtab_node::has_aliases_p().
Referenced by ipa_make_function_const(), and ipa_make_function_pure().
|
static |
compute state based on ECF FLAGS and store to STATE and LOOPING.
References dump_file, dump_flags, ECF_CONST, ECF_LOOPING_CONST_OR_PURE, ECF_PURE, IPA_CONST, IPA_NEITHER, IPA_PURE, and TDF_DETAILS.
Referenced by analyze_function(), check_call(), and propagate_pure_const().
|
static |
Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE is true if the function is known to be finite. The diagnostic is controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for OPTION, this function may initialize it and it is always returned by the function.
References hash_set< KeyId, Lazy, Traits >::add(), hash_set< KeyId, Lazy, Traits >::contains(), DECL_SOURCE_LOCATION, function_always_visible_to_compiler_p(), G_, global_options, option(), option_enabled(), lang_hooks::option_lang_mask, TREE_THIS_VOLATILE, and warning_at().
Referenced by warn_function_cold(), warn_function_const(), warn_function_malloc(), warn_function_noreturn(), warn_function_pure(), and warn_function_returns_nonnull().
void warn_function_cold | ( | tree | decl | ) |
Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE is true if the function is known to be finite.
References suggest_attribute(), TREE_TYPE, and VOID_TYPE_P.
Referenced by ipa_make_function_const().
|
static |
Emit suggestion about __attribute__((malloc)) for DECL.
References suggest_attribute().
Referenced by propagate_malloc().
|
static |
Emit suggestion about __attribute__((noreturn)) for DECL.
References lang_hooks::missing_noreturn_ok_p, suggest_attribute(), and targetm.
Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE is true if the function is known to be finite.
References suggest_attribute(), TREE_TYPE, and VOID_TYPE_P.
Referenced by ipa_make_function_pure().
void warn_function_returns_nonnull | ( | tree | decl | ) |
References suggest_attribute().
Referenced by execute_ranger_vrp().
|
inlinestatic |
Merge STATE and STATE2 and LOOPING and LOOPING2 and store into STATE and LOOPING worse of the two variants. N is the actual node called.
References symtab_node::binds_to_current_def_p(), symtab_node::decl, dump_file, dump_flags, symtab_node::dump_name(), IPA_CONST, IPA_PURE, MAX, TDF_DETAILS, and TREE_READONLY.
Referenced by check_call(), and propagate_pure_const().
|
static |
|
static |
Referenced by dump_malloc_lattice(), and pure_const_read_summary().
|
static |
Referenced by propagate_pure_const(), and pure_const_read_summary().