c++: 'this' adjustment for devirtualized call
[official-gcc.git] / gcc / gimple-range.h
blobecd332a3c541c4d7407543194403924e85670c8d
1 /* Header file for the GIMPLE range interface.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #ifndef GCC_GIMPLE_RANGE_STMT_H
23 #define GCC_GIMPLE_RANGE_STMT_H
26 #include "range.h"
27 #include "range-op.h"
28 #include "gimple-range-edge.h"
29 #include "gimple-range-gori.h"
30 #include "gimple-range-cache.h"
31 #include "value-query.h"
33 // This file is the main include point for gimple ranges.
34 // There are two fold_range routines of interest:
35 // bool fold_range (irange &r, gimple *s, range_query *q)
36 // bool fold_range (irange &r, gimple *s, edge on_edge, range_query *q)
37 // These routines will fold stmt S into the result irange R.
38 // Any ssa_names on the stmt will be calculated using the range_query
39 // parameter via a call to range_of_expr.
40 // If no range_query is provided, current global range info will be used.
41 // The second variation specifies an edge, and stmt S is recalculated as if
42 // it appeared on that edge.
45 // This is the basic range generator interface.
47 // This base class provides all the API entry points, but only provides
48 // functionality at the statement level. Ie, it can calculate ranges on
49 // statements, but does no additonal lookup.
51 // All the range_of_* methods will return a range if the types is
52 // supported by the range engine. It may be the full range for the
53 // type, AKA varying_p or it may be a refined range. If the range
54 // type is not supported, then false is returned. Non-statement
55 // related methods return whatever the current global value is.
58 class gimple_ranger : public range_query
60 public:
61 gimple_ranger () : m_cache (*this) { }
62 virtual bool range_of_stmt (irange &r, gimple *, tree name = NULL) OVERRIDE;
63 virtual bool range_of_expr (irange &r, tree name, gimple * = NULL) OVERRIDE;
64 virtual bool range_on_edge (irange &r, edge e, tree name) OVERRIDE;
65 virtual void range_on_entry (irange &r, basic_block bb, tree name);
66 virtual void range_on_exit (irange &r, basic_block bb, tree name);
67 void export_global_ranges ();
68 virtual void dump (FILE *f) OVERRIDE;
69 void dump_bb (FILE *f, basic_block bb);
70 protected:
71 bool fold_range_internal (irange &r, gimple *s, tree name);
72 ranger_cache m_cache;
75 // Source of an operand for fold_using_range.
76 // It can specify a stmt or and edge, or thru an internal API which uses
77 // the ranger cache.
78 // Its primary function is to retreive an operand from the source via a
79 // call thru the range_query object.
81 class fur_source
83 friend class fold_using_range;
84 public:
85 inline fur_source (range_query *q, edge e);
86 inline fur_source (range_query *q, gimple *s);
87 inline fur_source (range_query *q, gori_compute *g, edge e, gimple *s);
88 bool get_operand (irange &r, tree expr);
89 protected:
90 gori_compute *m_gori;
91 range_query *m_query;
92 edge m_edge;
93 gimple *m_stmt;
97 // This class uses ranges to fold a gimple statement producinf a range for
98 // the LHS. The source of all operands is supplied via the fur_source class
99 // which provides a range_query as well as a source location and any other
100 // required information.
102 class fold_using_range
104 public:
105 bool fold_stmt (irange &r, gimple *s, class fur_source &src,
106 tree name = NULL_TREE);
107 protected:
108 bool range_of_range_op (irange &r, gimple *s, fur_source &src);
109 bool range_of_call (irange &r, gcall *call, fur_source &src);
110 bool range_of_cond_expr (irange &r, gassign* cond, fur_source &src);
111 bool range_of_address (irange &r, gimple *s, fur_source &src);
112 bool range_of_builtin_call (irange &r, gcall *call, fur_source &src);
113 void range_of_builtin_ubsan_call (irange &r, gcall *call, tree_code code,
114 fur_source &src);
115 bool range_of_phi (irange &r, gphi *phi, fur_source &src);
116 void range_of_ssa_name_with_loop_info (irange &, tree, class loop *, gphi *,
117 fur_source &src);
121 // Create a source for a query on an edge.
123 inline
124 fur_source::fur_source (range_query *q, edge e)
126 m_query = q;
127 m_gori = NULL;
128 m_edge = e;
129 m_stmt = NULL;
132 // Create a source for a query at a statement.
134 inline
135 fur_source::fur_source (range_query *q, gimple *s)
137 m_query = q;
138 m_gori = NULL;
139 m_edge = NULL;
140 m_stmt = s;
143 // Create a source for Ranger. THis can recalculate from a different location
144 // and can also set the dependency information as appropriate when invoked.
146 inline
147 fur_source::fur_source (range_query *q, gori_compute *g, edge e, gimple *s)
149 m_query = q;
150 m_gori = g;
151 m_edge = e;
152 m_stmt = s;
155 // Fold stmt S into range R using range query Q.
157 inline bool
158 fold_range (irange &r, gimple *s, range_query *q = NULL)
160 fold_using_range f;
161 fur_source src (q, s);
162 return f.fold_stmt (r, s, src);
165 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
167 inline bool
168 fold_range (irange &r, gimple *s, edge on_edge, range_query *q = NULL)
170 fold_using_range f;
171 fur_source src (q, on_edge);
172 return f.fold_stmt (r, s, src);
175 // Calculate a basic range for a tree node expression.
176 extern bool get_tree_range (irange &r, tree expr);
178 // These routines provide a GIMPLE interface to the range-ops code.
179 extern tree gimple_range_operand1 (const gimple *s);
180 extern tree gimple_range_operand2 (const gimple *s);
181 extern bool gimple_range_calc_op1 (irange &r, const gimple *s,
182 const irange &lhs_range);
183 extern bool gimple_range_calc_op1 (irange &r, const gimple *s,
184 const irange &lhs_range,
185 const irange &op2_range);
186 extern bool gimple_range_calc_op2 (irange &r, const gimple *s,
187 const irange &lhs_range,
188 const irange &op1_range);
191 // Return the range_operator pointer for this statement. This routine
192 // can also be used to gate whether a routine is range-ops enabled.
194 static inline range_operator *
195 gimple_range_handler (const gimple *s)
197 if (const gassign *ass = dyn_cast<const gassign *> (s))
198 return range_op_handler (gimple_assign_rhs_code (ass),
199 TREE_TYPE (gimple_assign_lhs (ass)));
200 if (const gcond *cond = dyn_cast<const gcond *> (s))
201 return range_op_handler (gimple_cond_code (cond),
202 TREE_TYPE (gimple_cond_lhs (cond)));
203 return NULL;
206 // Return EXP if it is an SSA_NAME with a type supported by gimple ranges.
208 static inline tree
209 gimple_range_ssa_p (tree exp)
211 if (exp && TREE_CODE (exp) == SSA_NAME &&
212 !SSA_NAME_IS_VIRTUAL_OPERAND (exp) &&
213 irange::supports_type_p (TREE_TYPE (exp)))
214 return exp;
215 return NULL_TREE;
218 // Return true if TYPE1 and TYPE2 are compatible range types.
220 static inline bool
221 range_compatible_p (tree type1, tree type2)
223 // types_compatible_p requires conversion in both directions to be useless.
224 // GIMPLE only requires a cast one way in order to be compatible.
225 // Ranges really only need the sign and precision to be the same.
226 return (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
227 && TYPE_SIGN (type1) == TYPE_SIGN (type2));
230 // This class overloads the ranger routines to provide tracing facilties
231 // Entry and exit values to each of the APIs is placed in the dumpfile.
233 class trace_ranger : public gimple_ranger
235 public:
236 trace_ranger ();
237 virtual bool range_of_stmt (irange &r, gimple *s, tree name = NULL_TREE);
238 virtual bool range_of_expr (irange &r, tree name, gimple *s = NULL);
239 virtual bool range_on_edge (irange &r, edge e, tree name);
240 virtual void range_on_entry (irange &r, basic_block bb, tree name);
241 virtual void range_on_exit (irange &r, basic_block bb, tree name);
242 private:
243 static const unsigned bump = 2;
244 unsigned indent;
245 unsigned trace_count; // Current trace index count.
247 bool dumping (unsigned counter, bool trailing = false);
248 bool trailer (unsigned counter, const char *caller, bool result, tree name,
249 const irange &r);
252 // Flag to enable debugging the various internal Caches.
253 #define DEBUG_RANGE_CACHE (dump_file && (param_evrp_mode & EVRP_MODE_DEBUG))
255 extern gimple_ranger *enable_ranger (struct function *);
256 extern void disable_ranger (struct function *);
258 #endif // GCC_GIMPLE_RANGE_STMT_H