1 /* Header file for gimple range inference.
2 Copyright (C) 2022-2025 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_GIMPLE_RANGE_SIDE_H
22 #define GCC_GIMPLE_RANGE_SIDE_H
24 // Inferred ranges are ranges which are applied to use operands as a by product
25 // of executing an operation.
27 // This class manages an on-demand summary of inferred ranges for a statement.
28 // It can be instantiated as required and provides a list of inferred ranges.
29 // New inferred ranges should be added in the constructor of this class.
31 // There are 2 main instantiations.
32 // gimple_range_infer (gimple *s, range_query *q, bool use_range_ops)
33 // S is the statement being queried.
34 // Q is a range-query object which is used to resolve any ranges that
35 // might be required. This defaults to NULL which maps to the
36 // global_range_query, which is what most passes will want.
37 // Ranger internally will pass the cache's range-query which is a
38 // read-only query and prevents any additional lookup.
39 // USE_RANGEOPS is a boolean flag which defaults to false. if TRUE,
40 // range-ops is invoked to see if any additional side effects are seen
41 // based on the stmt. ie .x = y * 2 will reigster a side effect for Y
42 // which is [-INF/2 , +INF/2]. It is not on by default because it
43 // is a relatively expensive operation to do on every statement, and
44 // ranger will already incorporate that range for Y via GORI most of the
45 // time that it matters. Individual passes may have use for it however.
46 // PR 113879 is an example where this can be of use.
48 // gimple_range_infer (tree name, vrange &r)
49 // This instantiation simply create an inferred range record directly.
50 // NAME is the SSA_NAME to create the record for
51 // R is the range for NAME.
53 // Once a gimple_infer_range record has been created, the API is simple:
54 // num () - The number of inferred ranges in this record.
55 // name (i) - The i'th SSA_NAME in this record.
56 // range (i) - The range of the i'th SSA_NAME.
58 class gimple_infer_range
61 gimple_infer_range (gimple
*s
, range_query
*q
= NULL
,
62 bool use_rangeops
= false);
63 gimple_infer_range (tree name
, vrange
&r
);
64 inline unsigned num () const { return num_args
; }
65 inline tree
name (unsigned index
) const
66 { gcc_checking_assert (index
< num_args
); return m_names
[index
]; }
67 inline const vrange
& range (unsigned index
) const
68 { gcc_checking_assert (index
< num_args
); return m_ranges
[index
]; }
70 void add_range (tree name
, vrange
&range
);
71 void add_nonzero (tree name
);
72 void check_assume_func (gcall
*call
);
74 static const int size_limit
= 10;
75 tree m_names
[size_limit
];
76 value_range m_ranges
[size_limit
];
77 inline void bump_index () { if (num_args
< size_limit
- 1) num_args
++; }
78 friend class non_null_wrapper
;
81 // This is the basic infer oracle API. Default functionaility does nothing.
83 class infer_range_oracle
86 infer_range_oracle () { }
87 virtual ~infer_range_oracle () { }
88 virtual void add_ranges (gimple
*, gimple_infer_range
&) { }
89 virtual bool has_range_p (basic_block
, tree
= NULL_TREE
) { return false; }
90 virtual bool maybe_adjust_range (vrange
&, tree
, basic_block
)
94 // This class manages a list of inferred ranges for each basic block.
95 // As inferences are made, they can be registered to a block and later
96 // queried via a DOM search.
97 // When DO_SEARCH is TRUE, immediate uses chains are followed the first time
98 // a name is referenced and block populated if there are any inferred ranges.
99 // range_query Q is the range_query to use for any range lookups. It defaults
100 // to NULL which maps to the global_range_query. This is what most passes
101 // will want to use. Ranger invokes it with the cache's internal query which
102 // can provide better ranges during a DOM walk.
104 // add_ranges is used to add inferred range IR assocaited with stmt S.
105 // has_range_p is used to check if NAME has an inferred range in block BB.
106 // maybe_adjust_range will adjust the range R to incorporate any inferred
107 // range NAME may have in block BB. If there are on inferred ranges in
108 // block BB, then R will be unchanged, otherwise the ranges are intersected.
110 class infer_range_manager
: public infer_range_oracle
113 infer_range_manager (bool do_search
, range_query
*q
= NULL
);
114 virtual ~infer_range_manager ();
115 virtual void add_ranges (gimple
*s
, gimple_infer_range
&ir
);
116 virtual bool has_range_p (basic_block bb
, tree name
= NULL_TREE
);
117 virtual bool maybe_adjust_range (vrange
&r
, tree name
, basic_block bb
);
119 void add_range (tree name
, gimple
*s
, const vrange
&r
);
120 void add_nonzero (tree name
, gimple
*s
);
121 class exit_range_head
124 bitmap m_names
; // list of names with an outgoing range.
125 class exit_range
*head
;
127 exit_range
*find_ptr (tree name
);
129 void register_all_uses (tree name
);
130 vec
<exit_range_head
> m_on_exit
;
131 const vrange
&get_nonzero (tree name
);
132 vec
<vrange
*> m_nonzero
;
134 bitmap_obstack m_bitmaps
;
135 struct obstack m_list_obstack
;
136 class vrange_allocator
*m_range_allocator
;
137 range_query
*m_query
;
139 #endif // GCC_GIMPLE_RANGE_SIDE_H