1 /* Header file for the GIMPLE range interface.
2 Copyright (C) 2019-2020 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
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
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
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 is the basic range generator interface.
35 // This base class provides all the API entry points, but only provides
36 // functionality at the statement level. Ie, it can calculate ranges on
37 // statements, but does no additonal lookup.
39 // All the range_of_* methods will return a range if the types is
40 // supported by the range engine. It may be the full range for the
41 // type, AKA varying_p or it may be a refined range. If the range
42 // type is not supported, then false is returned. Non-statement
43 // related methods return whatever the current global value is.
46 class gimple_ranger
: public range_query
49 gimple_ranger () : m_cache (*this) { }
50 virtual bool range_of_stmt (irange
&r
, gimple
*, tree name
= NULL
) OVERRIDE
;
51 virtual bool range_of_expr (irange
&r
, tree name
, gimple
* = NULL
) OVERRIDE
;
52 virtual bool range_on_edge (irange
&r
, edge e
, tree name
) OVERRIDE
;
53 virtual void range_on_entry (irange
&r
, basic_block bb
, tree name
);
54 virtual void range_on_exit (irange
&r
, basic_block bb
, tree name
);
55 void export_global_ranges ();
58 bool calc_stmt (irange
&r
, gimple
*s
, tree name
= NULL_TREE
);
59 bool range_of_range_op (irange
&r
, gimple
*s
);
60 bool range_of_call (irange
&r
, gcall
*call
);
61 bool range_of_cond_expr (irange
&r
, gassign
* cond
);
64 bool range_of_phi (irange
&r
, gphi
*phi
);
65 bool range_of_non_trivial_assignment (irange
&r
, gimple
*s
);
66 bool range_of_builtin_call (irange
&r
, gcall
*call
);
67 bool range_with_loop_info (irange
&r
, tree name
);
68 void range_of_ssa_name_with_loop_info (irange
&, tree
, class loop
*,
72 // Calculate a basic range for a tree expression.
73 extern bool get_tree_range (irange
&r
, tree expr
);
75 // These routines provide a GIMPLE interface to the range-ops code.
76 extern tree
gimple_range_operand1 (const gimple
*s
);
77 extern tree
gimple_range_operand2 (const gimple
*s
);
78 extern tree
gimple_range_base_of_assignment (const gimple
*s
);
79 extern bool gimple_range_fold (irange
&res
, const gimple
*s
,
81 extern bool gimple_range_fold (irange
&res
, const gimple
*s
,
84 extern bool gimple_range_calc_op1 (irange
&r
, const gimple
*s
,
85 const irange
&lhs_range
);
86 extern bool gimple_range_calc_op1 (irange
&r
, const gimple
*s
,
87 const irange
&lhs_range
,
88 const irange
&op2_range
);
89 extern bool gimple_range_calc_op2 (irange
&r
, const gimple
*s
,
90 const irange
&lhs_range
,
91 const irange
&op1_range
);
94 // Return the range_operator pointer for this statement. This routine
95 // can also be used to gate whether a routine is range-ops enabled.
97 static inline range_operator
*
98 gimple_range_handler (const gimple
*s
)
100 if ((gimple_code (s
) == GIMPLE_ASSIGN
) || (gimple_code (s
) == GIMPLE_COND
))
101 return range_op_handler (gimple_expr_code (s
), gimple_expr_type (s
));
105 // Return EXP if it is an SSA_NAME with a type supported by gimple ranges.
108 gimple_range_ssa_p (tree exp
)
110 if (exp
&& TREE_CODE (exp
) == SSA_NAME
&&
111 !SSA_NAME_IS_VIRTUAL_OPERAND (exp
) &&
112 irange::supports_type_p (TREE_TYPE (exp
)))
117 // Return true if TYPE1 and TYPE2 are compatible range types.
120 range_compatible_p (tree type1
, tree type2
)
122 // types_compatible_p requires conversion in both directions to be useless.
123 // GIMPLE only requires a cast one way in order to be compatible.
124 // Ranges really only need the sign and precision to be the same.
125 return (TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
126 && TYPE_SIGN (type1
) == TYPE_SIGN (type2
));
129 // Return the legacy GCC global range for NAME if it has one, otherwise
132 static inline value_range
133 gimple_range_global (tree name
)
135 gcc_checking_assert (gimple_range_ssa_p (name
));
136 tree type
= TREE_TYPE (name
);
138 // Reenable picking up global ranges when we are OK failing tests that look
139 // for builtin_unreachable in the code, like
140 // RUNTESTFLAGS=dg.exp=pr61034.C check-g++
141 // pre-optimizations (inlining) set a global range which causes the ranger
142 // to remove the condition which leads to builtin_unreachable.
143 if (!POINTER_TYPE_P (type
) && SSA_NAME_RANGE_INFO (name
))
145 // Return a range from an SSA_NAME's available range.
147 enum value_range_kind kind
= get_range_info (name
, &min
, &max
);
148 return value_range (type
, min
, max
, kind
);
151 // Otherwise return range for the type.
152 return value_range (type
);
156 // This class overloads the ranger routines to provide tracing facilties
157 // Entry and exit values to each of the APIs is placed in the dumpfile.
159 class trace_ranger
: public gimple_ranger
163 virtual bool range_of_stmt (irange
&r
, gimple
*s
, tree name
= NULL_TREE
);
164 virtual bool range_of_expr (irange
&r
, tree name
, gimple
*s
= NULL
);
165 virtual bool range_on_edge (irange
&r
, edge e
, tree name
);
166 virtual void range_on_entry (irange
&r
, basic_block bb
, tree name
);
167 virtual void range_on_exit (irange
&r
, basic_block bb
, tree name
);
169 static const unsigned bump
= 2;
171 unsigned trace_count
; // Current trace index count.
173 bool dumping (unsigned counter
, bool trailing
= false);
174 bool trailer (unsigned counter
, const char *caller
, bool result
, tree name
,
178 // Flag to enable debugging the various internal Caches.
179 #define DEBUG_RANGE_CACHE (dump_file && (param_evrp_mode & EVRP_MODE_DEBUG))
181 // Temporary external interface to share with vr_values.
182 bool range_of_builtin_call (range_query
&query
, irange
&r
, gcall
*call
);
184 #endif // GCC_GIMPLE_RANGE_STMT_H