GCC Middle and Back End API Reference
pointer-query.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "stringpool.h"
#include "tree-vrp.h"
#include "diagnostic-core.h"
#include "fold-const.h"
#include "tree-object-size.h"
#include "tree-ssa-strlen.h"
#include "langhooks.h"
#include "attribs.h"
#include "gimple-iterator.h"
#include "gimple-fold.h"
#include "gimple-ssa.h"
#include "intl.h"
#include "attr-fnspec.h"
#include "gimple-range.h"
#include "pointer-query.h"
#include "tree-pretty-print.h"
#include "tree-ssanames.h"
#include "target.h"
Include dependency graph for pointer-query.cc:

Functions

static bool compute_objsize_r (tree, gimple *, bool, int, access_ref *, ssa_name_limit_t &, pointer_query *)
 
static bool get_offset_range (tree x, gimple *stmt, offset_int r[2], range_query *rvals)
 
static tree gimple_call_return_array (gimple *stmt, offset_int offrng[2], bool *past_end, ssa_name_limit_t &snlim, pointer_query *qry)
 
bool get_size_range (range_query *query, tree exp, gimple *stmt, tree range[2], int flags)
 
bool get_size_range (tree exp, tree range[2], int flags)
 
tree gimple_call_alloc_size (gimple *stmt, wide_int rng1[2], range_query *qry)
 
static tree gimple_parm_array_size (tree ptr, wide_int rng[2], bool *static_array)
 
static bool handle_min_max_size (tree ptr, int ostype, access_ref *pref, ssa_name_limit_t &snlim, pointer_query *qry)
 
static bool handle_decl (tree decl, bool addr, access_ref *pref)
 
static bool handle_array_ref (tree aref, gimple *stmt, bool addr, int ostype, access_ref *pref, ssa_name_limit_t &snlim, pointer_query *qry)
 
static void set_component_ref_size (tree cref, access_ref *pref)
 
static bool handle_component_ref (tree cref, gimple *stmt, bool addr, int ostype, access_ref *pref, ssa_name_limit_t &snlim, pointer_query *qry)
 
static bool handle_mem_ref (tree mref, gimple *stmt, int ostype, access_ref *pref, ssa_name_limit_t &snlim, pointer_query *qry)
 
static bool handle_ssa_name (tree ptr, bool addr, int ostype, access_ref *pref, ssa_name_limit_t &snlim, pointer_query *qry)
 
tree compute_objsize (tree ptr, gimple *stmt, int ostype, access_ref *pref, pointer_query *ptr_qry)
 
tree compute_objsize (tree ptr, gimple *stmt, int ostype, access_ref *pref, range_query *rvals)
 
tree compute_objsize (tree ptr, gimple *stmt, int ostype, tree *pdecl, tree *poff, range_query *rvals)
 
tree field_at_offset (tree type, tree start_after, HOST_WIDE_INT off, HOST_WIDE_INT *fldoff, HOST_WIDE_INT *nextoff)
 
tree array_elt_at_offset (tree artype, HOST_WIDE_INT off, HOST_WIDE_INT *eltoff, HOST_WIDE_INT *subar_size)
 
tree build_printable_array_type (tree eltype, unsigned HOST_WIDE_INT nelts)
 

Function Documentation

◆ array_elt_at_offset()

tree array_elt_at_offset ( tree artype,
HOST_WIDE_INT off,
HOST_WIDE_INT * eltoff,
HOST_WIDE_INT * subar_size )
Determine the offset *ELTOFF of the first byte of the array element
of array ARTYPE into which the byte offset OFF points.  On success
set *ELTOFF to the offset of the first byte and return type.
Otherwise, if no such element can be found, return null.   

References char_type_node, gcc_assert, int_size_in_bytes(), NULL_TREE, RECORD_OR_UNION_TYPE_P, TREE_CODE, TREE_TYPE, and TYPE_MODE.

Referenced by field_at_offset().

◆ build_printable_array_type()

tree build_printable_array_type ( tree eltype,
unsigned HOST_WIDE_INT nelts )

◆ compute_objsize() [1/3]

tree compute_objsize ( tree ptr,
gimple * stmt,
int ostype,
access_ref * pref,
pointer_query * ptr_qry )

◆ compute_objsize() [2/3]

tree compute_objsize ( tree ptr,
gimple * stmt,
int ostype,
access_ref * pref,
range_query * rvals )
Transitional wrapper.  The function should be removed once callers
transition to the pointer_query API.   

References compute_objsize(), pref, and pointer_query::rvals.

Referenced by compute_objsize(), compute_objsize(), pointer_query::get_ref(), and access_ref::inform_access().

◆ compute_objsize() [3/3]

tree compute_objsize ( tree ptr,
gimple * stmt,
int ostype,
tree * pdecl,
tree * poff,
range_query * rvals )
Legacy wrapper around the above.  The function should be removed
once callers transition to one of the two above.   

References access_ref::base0, compute_objsize(), NULL_TREE, access_ref::offrng, ptrdiff_type_node, access_ref::ref, and wide_int_to_tree().

◆ compute_objsize_r()

static bool compute_objsize_r ( tree ptr,
gimple * stmt,
bool addr,
int ostype,
access_ref * pref,
ssa_name_limit_t & snlim,
pointer_query * qry )
static
Definitions of the pointer_query and related classes.

Copyright (C) 2020-2024 Free Software Foundation, Inc.

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/>.   
Helper to compute the size of the object referenced by the PTR
expression which must have pointer type, using Object Size type
OSTYPE (only the least significant 2 bits are used).
On success, sets PREF->REF to the DECL of the referenced object
if it's unique, otherwise to null, PREF->OFFRNG to the range of
offsets into it, and PREF->SIZRNG to the range of sizes of
the object(s).
ADDR is true for an enclosing ADDR_EXPR.
SNLIM is used to avoid visiting the same PHI operand multiple
times, and, when nonnull, RVALS to determine range information.
Returns true on success, false when a meaningful size (or range)
cannot be determined.

The function is intended for diagnostics and should not be used
to influence code generation or optimization.   

References compute_objsize_r(), DECL_P, get_offset_range(), handle_array_ref(), handle_component_ref(), handle_decl(), handle_mem_ref(), handle_ssa_name(), integer_zerop(), POINTER_TYPE_P, pref, pointer_query::put_ref(), pointer_query::rvals, STRIP_NOPS, targetm, wi::to_offset(), wi::to_widest(), TREE_CODE, TREE_OPERAND, TREE_STRING_LENGTH, TREE_TYPE, and TYPE_ADDR_SPACE.

Referenced by compute_objsize(), compute_objsize_r(), gimple_call_return_array(), handle_array_ref(), handle_component_ref(), handle_mem_ref(), handle_min_max_size(), handle_ssa_name(), and access_ref::merge_ref().

◆ field_at_offset()

tree field_at_offset ( tree type,
tree start_after,
HOST_WIDE_INT off,
HOST_WIDE_INT * fldoff,
HOST_WIDE_INT * nextoff )
Determine the offset *FLDOFF of the first byte of a struct member
of TYPE (possibly recursively) into which the byte offset OFF points,
starting after the field START_AFTER if it's non-null.  On success,
if nonnull, set *FLDOFF to the offset of the first byte, and return
the field decl.  If nonnull, set *NEXTOFF to the offset of the next
field (which reflects any padding between the returned field and
the next).  Otherwise, if no such member can be found, return null.   

References array_elt_at_offset(), byte_position(), DECL_ARTIFICIAL, field_at_offset(), HOST_WIDE_INT_MAX, int_byte_position(), NULL_TREE, TREE_CHAIN, TREE_CODE, tree_fits_shwi_p(), tree_fits_uhwi_p(), tree_to_shwi(), tree_to_uhwi(), TREE_TYPE, TYPE_FIELDS, and TYPE_SIZE_UNIT.

Referenced by field_at_offset(), and get_maxbound().

◆ get_offset_range()

static bool get_offset_range ( tree x,
gimple * stmt,
offset_int r[2],
range_query * rvals )
static
Wrapper around the wide_int overload of get_range that accepts
offset_int instead.  For middle end expressions returns the same
result.  For a subset of nonconstamt expressions emitted by the front
end determines a more precise range than would be possible otherwise.   

References fold_convert, get_range(), INTEGRAL_TYPE_P, POINTER_TYPE_P, r, SIGNED, signed_type_for(), sizetype, wi::to_offset(), TREE_CODE, TREE_OPERAND, TREE_TYPE, TYPE_MAX_VALUE, TYPE_MIN_VALUE, TYPE_PRECISION, TYPE_UNSIGNED, and UNSIGNED.

Referenced by compute_objsize_r(), gimple_call_return_array(), handle_array_ref(), handle_mem_ref(), and handle_ssa_name().

◆ get_size_range() [1/2]

bool get_size_range ( range_query * query,
tree exp,
gimple * stmt,
tree range[2],
int flags )
Return true when EXP's range can be determined and set RANGE[] to it
after adjusting it if necessary to make EXP a represents a valid size
of object, or a valid size argument to an allocation function declared
with attribute alloc_size (whose argument may be signed), or to a string
manipulation function like memset.
When ALLOW_ZERO is set in FLAGS, allow returning a range of [0, 0] for
a size in an anti-range [1, N] where N > PTRDIFF_MAX.  A zero range is
a (nearly) invalid argument to allocation functions like malloc but it
is a valid argument to functions like memset.
When USE_LARGEST is set in FLAGS set RANGE to the largest valid subrange
in a multi-range, otherwise to the smallest valid subrange.   

References cfun, wi::eq_p(), exp(), wide_int_storage::from(), get_legacy_range(), wide_int_storage::get_precision(), get_range_query(), INTEGRAL_TYPE_P, wi::les_p(), wi::leu_p(), wi::ltu_p(), max_object_size(), NULL_TREE, range_query::range_of_expr(), irange::set_varying(), SR_ALLOW_ZERO, SR_USE_LARGEST, wi::to_wide(), tree_fits_uhwi_p(), TREE_TYPE, TYPE_MAX_VALUE, TYPE_MIN_VALUE, TYPE_PRECISION, TYPE_UNSIGNED, vrange::undefined_p(), UNSIGNED, VR_ANTI_RANGE, VR_VARYING, wide_int_to_tree(), and wi::zero().

Referenced by get_size_range(), and gimple_call_alloc_size().

◆ get_size_range() [2/2]

bool get_size_range ( tree exp,
tree range[2],
int flags )

References exp(), get_size_range(), and NULL.

Referenced by access_data::set_bound().

◆ gimple_call_alloc_size()

tree gimple_call_alloc_size ( gimple * stmt,
wide_int rng1[2],
range_query * qry )
If STMT is a call to an allocation function, returns the constant
maximum size of the object allocated by the call represented as
sizetype.  If nonnull, sets RNG1[] to the range of the size.
When nonnull, uses RVALS for range information, otherwise gets global
range info.
Returns null when STMT is not a call to a valid allocation function.   

References ADDR_MAX_PRECISION, fold_convert, get_size_range(), gimple_call_arg(), gimple_call_builtin_p(), gimple_call_fndecl(), gimple_call_fntype(), gimple_call_num_args(), wi::gtu_p(), integer_one_node, is_gimple_call(), lookup_attribute(), NULL_TREE, r, sizetype, SR_ALLOW_ZERO, SR_USE_LARGEST, wi::to_wide(), TREE_CHAIN, TREE_CODE, TREE_INT_CST_LOW, TREE_TYPE, TREE_VALUE, TYPE_ATTRIBUTES, TYPE_MAX_VALUE, UINT_MAX, and wide_int_to_tree().

Referenced by strlen_pass::handle_builtin_stxncpy_strncat(), and handle_ssa_name().

◆ gimple_call_return_array()

static tree gimple_call_return_array ( gimple * stmt,
offset_int offrng[2],
bool * past_end,
ssa_name_limit_t & snlim,
pointer_query * qry )
static
Return the argument that the call STMT to a built-in function returns
or null if it doesn't.  On success, set OFFRNG[] to the range of offsets
from the argument reflected in the value returned by the built-in if it
can be determined, otherwise to 0 and HWI_M1U respectively.  Set
*PAST_END for functions like mempcpy that might return a past the end
pointer (most functions return a dereferenceable pointer to an existing
element of an array).   

References as_a(), BUILT_IN_NORMAL, compute_objsize_r(), DECL_ASSEMBLER_NAME, DECL_FUNCTION_CODE(), DECL_IS_OPERATOR_NEW_P, DECL_IS_REPLACEABLE_OPERATOR_NEW_P, get_offset_range(), gimple_call_arg(), gimple_call_builtin_p(), gimple_call_fndecl(), gimple_call_fnspec(), gimple_call_num_args(), HOST_WIDE_INT_M1U, id_equal(), NULL_TREE, attr_fnspec::returns_arg(), pointer_query::rvals, and access_ref::sizrng.

Referenced by handle_ssa_name().

◆ gimple_parm_array_size()

static tree gimple_parm_array_size ( tree ptr,
wide_int rng[2],
bool * static_array )
static
For an access to an object referenced to by the function parameter PTR
of pointer type, and set RNG[] to the range of sizes of the object
obtainedfrom the attribute access specification for the current function.
Set STATIC_ARRAY if the array parameter has been declared [static].
Return the function parameter on success and null otherwise.   

References get_parm_access(), NULL_TREE, POINTER_TYPE_P, sizetype, SSA_NAME_VAR, static_p, wi::to_wide(), TREE_CODE, TREE_TYPE, TYPE_PRECISION, TYPE_SIZE_UNIT, wi::uhwi(), UINT_MAX, and wi::zero().

Referenced by handle_ssa_name().

◆ handle_array_ref()

static bool handle_array_ref ( tree aref,
gimple * stmt,
bool addr,
int ostype,
access_ref * pref,
ssa_name_limit_t & snlim,
pointer_query * qry )
static
A helper of compute_objsize_r() to determine the size from ARRAY_REF
AREF.  ADDR is true if PTR is the operand of ADDR_EXPR.  Return true
on success and false on failure.   

References array_ref_low_bound(), compute_objsize_r(), gcc_assert, get_offset_range(), wide_int_storage::get_precision(), integer_zerop(), NULL, pref, ptrdiff_type_node, pointer_query::rvals, SIGNED, sizetype, wi::to_offset(), wi::to_wide(), TREE_CODE, TREE_OPERAND, TREE_TYPE, TYPE_MAX_VALUE, TYPE_PRECISION, TYPE_SIZE_UNIT, TYPE_UNSIGNED, and UNSIGNED.

Referenced by compute_objsize_r().

◆ handle_component_ref()

static bool handle_component_ref ( tree cref,
gimple * stmt,
bool addr,
int ostype,
access_ref * pref,
ssa_name_limit_t & snlim,
pointer_query * qry )
static
A helper of compute_objsize_r() to determine the size from COMPONENT_REF
CREF.  Return true on success and false on failure.   

References access_ref::add_max_offset(), access_ref::add_offset(), byte_position(), compute_objsize_r(), gcc_assert, offset, POINTER_TYPE_P, pref, access_ref::ref, set_component_ref_size(), access_ref::size_remaining(), wi::to_offset(), TREE_CODE, TREE_OPERAND, and TREE_TYPE.

Referenced by compute_objsize_r().

◆ handle_decl()

static bool handle_decl ( tree decl,
bool addr,
access_ref * pref )
static
A helper of compute_objsize_r() to determine the size of a DECL.
Return true on success and (possibly in the future) false on failure.   

References decl_init_size(), POINTER_TYPE_P, pref, wi::to_offset(), TREE_CODE, and TREE_TYPE.

Referenced by compute_objsize_r().

◆ handle_mem_ref()

static bool handle_mem_ref ( tree mref,
gimple * stmt,
int ostype,
access_ref * pref,
ssa_name_limit_t & snlim,
pointer_query * qry )
static
A helper of compute_objsize_r() to determine the size from MEM_REF
MREF.  Return true on success and false on failure.   

References compute_objsize_r(), gcc_assert, get_offset_range(), NULL, pref, ptrdiff_type_node, pointer_query::rvals, wi::to_offset(), TREE_CODE, TREE_OPERAND, TREE_TYPE, TYPE_MAIN_VARIANT, TYPE_MAX_VALUE, and VECTOR_TYPE_P.

Referenced by compute_objsize_r().

◆ handle_min_max_size()

static bool handle_min_max_size ( tree ptr,
int ostype,
access_ref * pref,
ssa_name_limit_t & snlim,
pointer_query * qry )
static
A helper of compute_objsize_r() to determine the size from an assignment
statement STMT with the RHS of either MIN_EXPR or MAX_EXPR.  On success
set PREF->REF to the operand with more or less space remaining,
respectively, if both refer to the same (sub)object, or to PTR if they
might not, and return true.  Otherwise, if the identity of neither
operand can be determined, return false.   

References access_ref::add_max_offset(), access_ref::base0, compute_objsize_r(), gimple_assign_rhs1(), gimple_assign_rhs2(), gimple_assign_rhs_code(), i1, access_ref::offrng, pref, access_ref::ref, access_ref::set_max_size_range(), and SSA_NAME_DEF_STMT.

Referenced by handle_ssa_name().

◆ handle_ssa_name()

◆ set_component_ref_size()

static void set_component_ref_size ( tree cref,
access_ref * pref )
static
Given a COMPONENT_REF CREF, set *PREF size to the size of the referenced
member.   

References component_ref_size(), int_0, pref, ptrdiff_type_node, wi::to_offset(), trail_1, TREE_CODE, TREE_OPERAND, TREE_TYPE, TYPE_MAX_VALUE, and TYPE_SIZE_UNIT.

Referenced by handle_component_ref().