annotate gcc/c/c-fold.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Support for fully folding sub-trees of an expression for C compiler.
kono
parents:
diff changeset
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "target.h"
kono
parents:
diff changeset
24 #include "function.h"
kono
parents:
diff changeset
25 #include "bitmap.h"
kono
parents:
diff changeset
26 #include "c-tree.h"
kono
parents:
diff changeset
27 #include "intl.h"
kono
parents:
diff changeset
28 #include "gimplify.h"
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 /* If DISABLE is true, stop issuing warnings. This is used when
kono
parents:
diff changeset
33 parsing code that we know will not be executed. This function may
kono
parents:
diff changeset
34 be called multiple times, and works as a stack. */
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 static void
kono
parents:
diff changeset
37 c_disable_warnings (bool disable)
kono
parents:
diff changeset
38 {
kono
parents:
diff changeset
39 if (disable)
kono
parents:
diff changeset
40 {
kono
parents:
diff changeset
41 ++c_inhibit_evaluation_warnings;
kono
parents:
diff changeset
42 fold_defer_overflow_warnings ();
kono
parents:
diff changeset
43 }
kono
parents:
diff changeset
44 }
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 /* If ENABLE is true, reenable issuing warnings. */
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 static void
kono
parents:
diff changeset
49 c_enable_warnings (bool enable)
kono
parents:
diff changeset
50 {
kono
parents:
diff changeset
51 if (enable)
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 --c_inhibit_evaluation_warnings;
kono
parents:
diff changeset
54 fold_undefer_and_ignore_overflow_warnings ();
kono
parents:
diff changeset
55 }
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 /* Fully fold EXPR, an expression that was not folded (beyond integer
kono
parents:
diff changeset
59 constant expressions and null pointer constants) when being built
kono
parents:
diff changeset
60 up. If IN_INIT, this is in a static initializer and certain
kono
parents:
diff changeset
61 changes are made to the folding done. Clear *MAYBE_CONST if
kono
parents:
diff changeset
62 MAYBE_CONST is not NULL and EXPR is definitely not a constant
kono
parents:
diff changeset
63 expression because it contains an evaluated operator (in C99) or an
kono
parents:
diff changeset
64 operator outside of sizeof returning an integer constant (in C90)
kono
parents:
diff changeset
65 not permitted in constant expressions, or because it contains an
kono
parents:
diff changeset
66 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
kono
parents:
diff changeset
67 set to true by callers before calling this function.) Return the
kono
parents:
diff changeset
68 folded expression. Function arguments have already been folded
kono
parents:
diff changeset
69 before calling this function, as have the contents of SAVE_EXPR,
kono
parents:
diff changeset
70 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
kono
parents:
diff changeset
71 C_MAYBE_CONST_EXPR. */
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 tree
kono
parents:
diff changeset
74 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 tree ret;
kono
parents:
diff changeset
77 tree eptype = NULL_TREE;
kono
parents:
diff changeset
78 bool dummy = true;
kono
parents:
diff changeset
79 bool maybe_const_itself = true;
kono
parents:
diff changeset
80 location_t loc = EXPR_LOCATION (expr);
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 if (!maybe_const)
kono
parents:
diff changeset
83 maybe_const = &dummy;
kono
parents:
diff changeset
84 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
kono
parents:
diff changeset
85 {
kono
parents:
diff changeset
86 eptype = TREE_TYPE (expr);
kono
parents:
diff changeset
87 expr = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
88 }
kono
parents:
diff changeset
89 ret = c_fully_fold_internal (expr, in_init, maybe_const,
kono
parents:
diff changeset
90 &maybe_const_itself, false);
kono
parents:
diff changeset
91 if (eptype)
kono
parents:
diff changeset
92 ret = fold_convert_loc (loc, eptype, ret);
kono
parents:
diff changeset
93 *maybe_const &= maybe_const_itself;
kono
parents:
diff changeset
94 return ret;
kono
parents:
diff changeset
95 }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
kono
parents:
diff changeset
98 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
kono
parents:
diff changeset
99 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
kono
parents:
diff changeset
100 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
kono
parents:
diff changeset
101 both evaluated and unevaluated subexpressions while
kono
parents:
diff changeset
102 *MAYBE_CONST_ITSELF is carried from only evaluated
kono
parents:
diff changeset
103 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
kono
parents:
diff changeset
104 with integer constant operands, and if any of the operands doesn't
kono
parents:
diff changeset
105 get folded to an integer constant, don't fold the expression itself. */
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 static tree
kono
parents:
diff changeset
108 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
kono
parents:
diff changeset
109 bool *maybe_const_itself, bool for_int_const)
kono
parents:
diff changeset
110 {
kono
parents:
diff changeset
111 tree ret = expr;
kono
parents:
diff changeset
112 enum tree_code code = TREE_CODE (expr);
kono
parents:
diff changeset
113 enum tree_code_class kind = TREE_CODE_CLASS (code);
kono
parents:
diff changeset
114 location_t loc = EXPR_LOCATION (expr);
kono
parents:
diff changeset
115 tree op0, op1, op2, op3;
kono
parents:
diff changeset
116 tree orig_op0, orig_op1, orig_op2;
kono
parents:
diff changeset
117 bool op0_const = true, op1_const = true, op2_const = true;
kono
parents:
diff changeset
118 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
kono
parents:
diff changeset
119 bool nowarning = TREE_NO_WARNING (expr);
kono
parents:
diff changeset
120 bool unused_p;
kono
parents:
diff changeset
121 source_range old_range;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 /* Constants, declarations, statements, errors, and anything else not
kono
parents:
diff changeset
124 counted as an expression cannot usefully be folded further at this
kono
parents:
diff changeset
125 point. */
kono
parents:
diff changeset
126 if (!IS_EXPR_CODE_CLASS (kind)
kono
parents:
diff changeset
127 || kind == tcc_statement)
kono
parents:
diff changeset
128 return expr;
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 if (IS_EXPR_CODE_CLASS (kind))
kono
parents:
diff changeset
131 old_range = EXPR_LOCATION_RANGE (expr);
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 /* Operands of variable-length expressions (function calls) have
kono
parents:
diff changeset
134 already been folded, as have __builtin_* function calls, and such
kono
parents:
diff changeset
135 expressions cannot occur in constant expressions. */
kono
parents:
diff changeset
136 if (kind == tcc_vl_exp)
kono
parents:
diff changeset
137 {
kono
parents:
diff changeset
138 *maybe_const_operands = false;
kono
parents:
diff changeset
139 ret = fold (expr);
kono
parents:
diff changeset
140 goto out;
kono
parents:
diff changeset
141 }
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 if (code == C_MAYBE_CONST_EXPR)
kono
parents:
diff changeset
144 {
kono
parents:
diff changeset
145 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
kono
parents:
diff changeset
146 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
kono
parents:
diff changeset
147 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
kono
parents:
diff changeset
148 *maybe_const_operands = false;
kono
parents:
diff changeset
149 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
kono
parents:
diff changeset
150 {
kono
parents:
diff changeset
151 *maybe_const_itself = false;
kono
parents:
diff changeset
152 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
kono
parents:
diff changeset
153 maybe_const_itself, true);
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155 if (pre && !in_init)
kono
parents:
diff changeset
156 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
kono
parents:
diff changeset
157 else
kono
parents:
diff changeset
158 ret = inner;
kono
parents:
diff changeset
159 goto out;
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 /* Assignment, increment, decrement, function call and comma
kono
parents:
diff changeset
163 operators, and statement expressions, cannot occur in constant
kono
parents:
diff changeset
164 expressions if evaluated / outside of sizeof. (Function calls
kono
parents:
diff changeset
165 were handled above, though VA_ARG_EXPR is treated like a function
kono
parents:
diff changeset
166 call here, and statement expressions are handled through
kono
parents:
diff changeset
167 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
kono
parents:
diff changeset
168 switch (code)
kono
parents:
diff changeset
169 {
kono
parents:
diff changeset
170 case MODIFY_EXPR:
kono
parents:
diff changeset
171 case PREDECREMENT_EXPR:
kono
parents:
diff changeset
172 case PREINCREMENT_EXPR:
kono
parents:
diff changeset
173 case POSTDECREMENT_EXPR:
kono
parents:
diff changeset
174 case POSTINCREMENT_EXPR:
kono
parents:
diff changeset
175 case COMPOUND_EXPR:
kono
parents:
diff changeset
176 *maybe_const_operands = false;
kono
parents:
diff changeset
177 break;
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 case VA_ARG_EXPR:
kono
parents:
diff changeset
180 case TARGET_EXPR:
kono
parents:
diff changeset
181 case BIND_EXPR:
kono
parents:
diff changeset
182 case OBJ_TYPE_REF:
kono
parents:
diff changeset
183 *maybe_const_operands = false;
kono
parents:
diff changeset
184 ret = fold (expr);
kono
parents:
diff changeset
185 goto out;
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 default:
kono
parents:
diff changeset
188 break;
kono
parents:
diff changeset
189 }
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 /* Fold individual tree codes as appropriate. */
kono
parents:
diff changeset
192 switch (code)
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 case COMPOUND_LITERAL_EXPR:
kono
parents:
diff changeset
195 /* Any non-constancy will have been marked in a containing
kono
parents:
diff changeset
196 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
kono
parents:
diff changeset
197 goto out;
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 case COMPONENT_REF:
kono
parents:
diff changeset
200 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
201 op1 = TREE_OPERAND (expr, 1);
kono
parents:
diff changeset
202 op2 = TREE_OPERAND (expr, 2);
kono
parents:
diff changeset
203 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
kono
parents:
diff changeset
204 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
205 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
206 if (op0 != orig_op0)
kono
parents:
diff changeset
207 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
kono
parents:
diff changeset
208 if (ret != expr)
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 TREE_READONLY (ret) = TREE_READONLY (expr);
kono
parents:
diff changeset
211 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
kono
parents:
diff changeset
212 }
kono
parents:
diff changeset
213 goto out;
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 case ARRAY_REF:
kono
parents:
diff changeset
216 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
217 orig_op1 = op1 = TREE_OPERAND (expr, 1);
kono
parents:
diff changeset
218 op2 = TREE_OPERAND (expr, 2);
kono
parents:
diff changeset
219 op3 = TREE_OPERAND (expr, 3);
kono
parents:
diff changeset
220 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
kono
parents:
diff changeset
221 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
222 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
223 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
kono
parents:
diff changeset
224 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
225 STRIP_TYPE_NOPS (op1);
kono
parents:
diff changeset
226 op1 = decl_constant_value_for_optimization (op1);
kono
parents:
diff changeset
227 if (op0 != orig_op0 || op1 != orig_op1)
kono
parents:
diff changeset
228 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
kono
parents:
diff changeset
229 if (ret != expr)
kono
parents:
diff changeset
230 {
kono
parents:
diff changeset
231 TREE_READONLY (ret) = TREE_READONLY (expr);
kono
parents:
diff changeset
232 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
kono
parents:
diff changeset
233 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
kono
parents:
diff changeset
234 }
kono
parents:
diff changeset
235 ret = fold (ret);
kono
parents:
diff changeset
236 goto out;
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 case COMPOUND_EXPR:
kono
parents:
diff changeset
239 case MODIFY_EXPR:
kono
parents:
diff changeset
240 case PREDECREMENT_EXPR:
kono
parents:
diff changeset
241 case PREINCREMENT_EXPR:
kono
parents:
diff changeset
242 case POSTDECREMENT_EXPR:
kono
parents:
diff changeset
243 case POSTINCREMENT_EXPR:
kono
parents:
diff changeset
244 case PLUS_EXPR:
kono
parents:
diff changeset
245 case MINUS_EXPR:
kono
parents:
diff changeset
246 case MULT_EXPR:
kono
parents:
diff changeset
247 case POINTER_PLUS_EXPR:
kono
parents:
diff changeset
248 case TRUNC_DIV_EXPR:
kono
parents:
diff changeset
249 case CEIL_DIV_EXPR:
kono
parents:
diff changeset
250 case FLOOR_DIV_EXPR:
kono
parents:
diff changeset
251 case TRUNC_MOD_EXPR:
kono
parents:
diff changeset
252 case RDIV_EXPR:
kono
parents:
diff changeset
253 case EXACT_DIV_EXPR:
kono
parents:
diff changeset
254 case LSHIFT_EXPR:
kono
parents:
diff changeset
255 case RSHIFT_EXPR:
kono
parents:
diff changeset
256 case BIT_IOR_EXPR:
kono
parents:
diff changeset
257 case BIT_XOR_EXPR:
kono
parents:
diff changeset
258 case BIT_AND_EXPR:
kono
parents:
diff changeset
259 case LT_EXPR:
kono
parents:
diff changeset
260 case LE_EXPR:
kono
parents:
diff changeset
261 case GT_EXPR:
kono
parents:
diff changeset
262 case GE_EXPR:
kono
parents:
diff changeset
263 case EQ_EXPR:
kono
parents:
diff changeset
264 case NE_EXPR:
kono
parents:
diff changeset
265 case COMPLEX_EXPR:
kono
parents:
diff changeset
266 case TRUTH_AND_EXPR:
kono
parents:
diff changeset
267 case TRUTH_OR_EXPR:
kono
parents:
diff changeset
268 case TRUTH_XOR_EXPR:
kono
parents:
diff changeset
269 case UNORDERED_EXPR:
kono
parents:
diff changeset
270 case ORDERED_EXPR:
kono
parents:
diff changeset
271 case UNLT_EXPR:
kono
parents:
diff changeset
272 case UNLE_EXPR:
kono
parents:
diff changeset
273 case UNGT_EXPR:
kono
parents:
diff changeset
274 case UNGE_EXPR:
kono
parents:
diff changeset
275 case UNEQ_EXPR:
kono
parents:
diff changeset
276 /* Binary operations evaluating both arguments (increment and
kono
parents:
diff changeset
277 decrement are binary internally in GCC). */
kono
parents:
diff changeset
278 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
279 orig_op1 = op1 = TREE_OPERAND (expr, 1);
kono
parents:
diff changeset
280 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
kono
parents:
diff changeset
281 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
282 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
283 if (code != MODIFY_EXPR
kono
parents:
diff changeset
284 && code != PREDECREMENT_EXPR
kono
parents:
diff changeset
285 && code != PREINCREMENT_EXPR
kono
parents:
diff changeset
286 && code != POSTDECREMENT_EXPR
kono
parents:
diff changeset
287 && code != POSTINCREMENT_EXPR)
kono
parents:
diff changeset
288 op0 = decl_constant_value_for_optimization (op0);
kono
parents:
diff changeset
289 /* The RHS of a MODIFY_EXPR was fully folded when building that
kono
parents:
diff changeset
290 expression for the sake of conversion warnings. */
kono
parents:
diff changeset
291 if (code != MODIFY_EXPR)
kono
parents:
diff changeset
292 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
kono
parents:
diff changeset
293 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
294 STRIP_TYPE_NOPS (op1);
kono
parents:
diff changeset
295 op1 = decl_constant_value_for_optimization (op1);
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
kono
parents:
diff changeset
298 || TREE_CODE (op1) != INTEGER_CST))
kono
parents:
diff changeset
299 goto out;
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
kono
parents:
diff changeset
302 ret = in_init
kono
parents:
diff changeset
303 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
kono
parents:
diff changeset
304 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
kono
parents:
diff changeset
305 else
kono
parents:
diff changeset
306 ret = fold (expr);
kono
parents:
diff changeset
307 if (TREE_OVERFLOW_P (ret)
kono
parents:
diff changeset
308 && !TREE_OVERFLOW_P (op0)
kono
parents:
diff changeset
309 && !TREE_OVERFLOW_P (op1))
kono
parents:
diff changeset
310 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret, expr);
kono
parents:
diff changeset
311 if (code == LSHIFT_EXPR
kono
parents:
diff changeset
312 && TREE_CODE (orig_op0) != INTEGER_CST
kono
parents:
diff changeset
313 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
kono
parents:
diff changeset
314 && TREE_CODE (op0) == INTEGER_CST
kono
parents:
diff changeset
315 && c_inhibit_evaluation_warnings == 0
kono
parents:
diff changeset
316 && tree_int_cst_sgn (op0) < 0)
kono
parents:
diff changeset
317 warning_at (loc, OPT_Wshift_negative_value,
kono
parents:
diff changeset
318 "left shift of negative value");
kono
parents:
diff changeset
319 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
kono
parents:
diff changeset
320 && TREE_CODE (orig_op1) != INTEGER_CST
kono
parents:
diff changeset
321 && TREE_CODE (op1) == INTEGER_CST
kono
parents:
diff changeset
322 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
kono
parents:
diff changeset
323 && c_inhibit_evaluation_warnings == 0)
kono
parents:
diff changeset
324 {
kono
parents:
diff changeset
325 if (tree_int_cst_sgn (op1) < 0)
kono
parents:
diff changeset
326 warning_at (loc, OPT_Wshift_count_negative,
kono
parents:
diff changeset
327 (code == LSHIFT_EXPR
kono
parents:
diff changeset
328 ? G_("left shift count is negative")
kono
parents:
diff changeset
329 : G_("right shift count is negative")));
kono
parents:
diff changeset
330 else if ((TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
kono
parents:
diff changeset
331 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
kono
parents:
diff changeset
332 && compare_tree_int (op1,
kono
parents:
diff changeset
333 TYPE_PRECISION (TREE_TYPE (orig_op0)))
kono
parents:
diff changeset
334 >= 0)
kono
parents:
diff changeset
335 warning_at (loc, OPT_Wshift_count_overflow,
kono
parents:
diff changeset
336 (code == LSHIFT_EXPR
kono
parents:
diff changeset
337 ? G_("left shift count >= width of type")
kono
parents:
diff changeset
338 : G_("right shift count >= width of type")));
kono
parents:
diff changeset
339 else if (TREE_CODE (TREE_TYPE (orig_op0)) == VECTOR_TYPE
kono
parents:
diff changeset
340 && compare_tree_int (op1,
kono
parents:
diff changeset
341 TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig_op0))))
kono
parents:
diff changeset
342 >= 0)
kono
parents:
diff changeset
343 warning_at (loc, OPT_Wshift_count_overflow,
kono
parents:
diff changeset
344 code == LSHIFT_EXPR
kono
parents:
diff changeset
345 ? G_("left shift count >= width of vector element")
kono
parents:
diff changeset
346 : G_("right shift count >= width of vector element"));
kono
parents:
diff changeset
347 }
kono
parents:
diff changeset
348 if (code == LSHIFT_EXPR
kono
parents:
diff changeset
349 /* If either OP0 has been folded to INTEGER_CST... */
kono
parents:
diff changeset
350 && ((TREE_CODE (orig_op0) != INTEGER_CST
kono
parents:
diff changeset
351 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
kono
parents:
diff changeset
352 && TREE_CODE (op0) == INTEGER_CST)
kono
parents:
diff changeset
353 /* ...or if OP1 has been folded to INTEGER_CST... */
kono
parents:
diff changeset
354 || (TREE_CODE (orig_op1) != INTEGER_CST
kono
parents:
diff changeset
355 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
kono
parents:
diff changeset
356 && TREE_CODE (op1) == INTEGER_CST))
kono
parents:
diff changeset
357 && c_inhibit_evaluation_warnings == 0)
kono
parents:
diff changeset
358 /* ...then maybe we can detect an overflow. */
kono
parents:
diff changeset
359 maybe_warn_shift_overflow (loc, op0, op1);
kono
parents:
diff changeset
360 if ((code == TRUNC_DIV_EXPR
kono
parents:
diff changeset
361 || code == CEIL_DIV_EXPR
kono
parents:
diff changeset
362 || code == FLOOR_DIV_EXPR
kono
parents:
diff changeset
363 || code == EXACT_DIV_EXPR
kono
parents:
diff changeset
364 || code == TRUNC_MOD_EXPR)
kono
parents:
diff changeset
365 && TREE_CODE (orig_op1) != INTEGER_CST
kono
parents:
diff changeset
366 && TREE_CODE (op1) == INTEGER_CST
kono
parents:
diff changeset
367 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
kono
parents:
diff changeset
368 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
kono
parents:
diff changeset
369 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
kono
parents:
diff changeset
370 warn_for_div_by_zero (loc, op1);
kono
parents:
diff changeset
371 goto out;
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 case INDIRECT_REF:
kono
parents:
diff changeset
374 case FIX_TRUNC_EXPR:
kono
parents:
diff changeset
375 case FLOAT_EXPR:
kono
parents:
diff changeset
376 CASE_CONVERT:
kono
parents:
diff changeset
377 case ADDR_SPACE_CONVERT_EXPR:
kono
parents:
diff changeset
378 case VIEW_CONVERT_EXPR:
kono
parents:
diff changeset
379 case NON_LVALUE_EXPR:
kono
parents:
diff changeset
380 case NEGATE_EXPR:
kono
parents:
diff changeset
381 case BIT_NOT_EXPR:
kono
parents:
diff changeset
382 case TRUTH_NOT_EXPR:
kono
parents:
diff changeset
383 case ADDR_EXPR:
kono
parents:
diff changeset
384 case CONJ_EXPR:
kono
parents:
diff changeset
385 case REALPART_EXPR:
kono
parents:
diff changeset
386 case IMAGPART_EXPR:
kono
parents:
diff changeset
387 /* Unary operations. */
kono
parents:
diff changeset
388 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
389 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
kono
parents:
diff changeset
390 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
391 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
392 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
kono
parents:
diff changeset
393 op0 = decl_constant_value_for_optimization (op0);
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
kono
parents:
diff changeset
396 goto out;
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
kono
parents:
diff changeset
399 not prepared to deal with them if they occur in initializers. */
kono
parents:
diff changeset
400 if (op0 != orig_op0
kono
parents:
diff changeset
401 && code == ADDR_EXPR
kono
parents:
diff changeset
402 && (op1 = get_base_address (op0)) != NULL_TREE
kono
parents:
diff changeset
403 && INDIRECT_REF_P (op1)
kono
parents:
diff changeset
404 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
kono
parents:
diff changeset
405 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
kono
parents:
diff changeset
406 else if (op0 != orig_op0 || in_init)
kono
parents:
diff changeset
407 ret = in_init
kono
parents:
diff changeset
408 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
kono
parents:
diff changeset
409 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
kono
parents:
diff changeset
410 else
kono
parents:
diff changeset
411 ret = fold (expr);
kono
parents:
diff changeset
412 if (code == INDIRECT_REF
kono
parents:
diff changeset
413 && ret != expr
kono
parents:
diff changeset
414 && INDIRECT_REF_P (ret))
kono
parents:
diff changeset
415 {
kono
parents:
diff changeset
416 TREE_READONLY (ret) = TREE_READONLY (expr);
kono
parents:
diff changeset
417 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
kono
parents:
diff changeset
418 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
kono
parents:
diff changeset
419 }
kono
parents:
diff changeset
420 switch (code)
kono
parents:
diff changeset
421 {
kono
parents:
diff changeset
422 case FIX_TRUNC_EXPR:
kono
parents:
diff changeset
423 case FLOAT_EXPR:
kono
parents:
diff changeset
424 CASE_CONVERT:
kono
parents:
diff changeset
425 /* Don't warn about explicit conversions. We will already
kono
parents:
diff changeset
426 have warned about suspect implicit conversions. */
kono
parents:
diff changeset
427 break;
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 default:
kono
parents:
diff changeset
430 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
kono
parents:
diff changeset
431 overflow_warning (EXPR_LOCATION (expr), ret, op0);
kono
parents:
diff changeset
432 break;
kono
parents:
diff changeset
433 }
kono
parents:
diff changeset
434 goto out;
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 case TRUTH_ANDIF_EXPR:
kono
parents:
diff changeset
437 case TRUTH_ORIF_EXPR:
kono
parents:
diff changeset
438 /* Binary operations not necessarily evaluating both
kono
parents:
diff changeset
439 arguments. */
kono
parents:
diff changeset
440 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
441 orig_op1 = op1 = TREE_OPERAND (expr, 1);
kono
parents:
diff changeset
442 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
kono
parents:
diff changeset
443 for_int_const);
kono
parents:
diff changeset
444 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
kono
parents:
diff changeset
447 ? truthvalue_false_node
kono
parents:
diff changeset
448 : truthvalue_true_node));
kono
parents:
diff changeset
449 c_disable_warnings (unused_p);
kono
parents:
diff changeset
450 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
kono
parents:
diff changeset
451 for_int_const);
kono
parents:
diff changeset
452 STRIP_TYPE_NOPS (op1);
kono
parents:
diff changeset
453 c_enable_warnings (unused_p);
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 if (for_int_const
kono
parents:
diff changeset
456 && (TREE_CODE (op0) != INTEGER_CST
kono
parents:
diff changeset
457 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
kono
parents:
diff changeset
458 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
kono
parents:
diff changeset
459 goto out;
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
kono
parents:
diff changeset
462 ret = in_init
kono
parents:
diff changeset
463 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
kono
parents:
diff changeset
464 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
kono
parents:
diff changeset
465 else
kono
parents:
diff changeset
466 ret = fold (expr);
kono
parents:
diff changeset
467 *maybe_const_operands &= op0_const;
kono
parents:
diff changeset
468 *maybe_const_itself &= op0_const_self;
kono
parents:
diff changeset
469 if (!(flag_isoc99
kono
parents:
diff changeset
470 && op0_const
kono
parents:
diff changeset
471 && op0_const_self
kono
parents:
diff changeset
472 && (code == TRUTH_ANDIF_EXPR
kono
parents:
diff changeset
473 ? op0 == truthvalue_false_node
kono
parents:
diff changeset
474 : op0 == truthvalue_true_node)))
kono
parents:
diff changeset
475 *maybe_const_operands &= op1_const;
kono
parents:
diff changeset
476 if (!(op0_const
kono
parents:
diff changeset
477 && op0_const_self
kono
parents:
diff changeset
478 && (code == TRUTH_ANDIF_EXPR
kono
parents:
diff changeset
479 ? op0 == truthvalue_false_node
kono
parents:
diff changeset
480 : op0 == truthvalue_true_node)))
kono
parents:
diff changeset
481 *maybe_const_itself &= op1_const_self;
kono
parents:
diff changeset
482 goto out;
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 case COND_EXPR:
kono
parents:
diff changeset
485 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
486 orig_op1 = op1 = TREE_OPERAND (expr, 1);
kono
parents:
diff changeset
487 orig_op2 = op2 = TREE_OPERAND (expr, 2);
kono
parents:
diff changeset
488 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
kono
parents:
diff changeset
489 for_int_const);
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
492 c_disable_warnings (op0 == truthvalue_false_node);
kono
parents:
diff changeset
493 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
kono
parents:
diff changeset
494 for_int_const);
kono
parents:
diff changeset
495 STRIP_TYPE_NOPS (op1);
kono
parents:
diff changeset
496 c_enable_warnings (op0 == truthvalue_false_node);
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 c_disable_warnings (op0 == truthvalue_true_node);
kono
parents:
diff changeset
499 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
kono
parents:
diff changeset
500 for_int_const);
kono
parents:
diff changeset
501 STRIP_TYPE_NOPS (op2);
kono
parents:
diff changeset
502 c_enable_warnings (op0 == truthvalue_true_node);
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504 if (for_int_const
kono
parents:
diff changeset
505 && (TREE_CODE (op0) != INTEGER_CST
kono
parents:
diff changeset
506 /* Only the evaluated operand must be an INTEGER_CST. */
kono
parents:
diff changeset
507 || (op0 == truthvalue_true_node
kono
parents:
diff changeset
508 ? TREE_CODE (op1) != INTEGER_CST
kono
parents:
diff changeset
509 : TREE_CODE (op2) != INTEGER_CST)))
kono
parents:
diff changeset
510 goto out;
kono
parents:
diff changeset
511
kono
parents:
diff changeset
512 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
kono
parents:
diff changeset
513 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
kono
parents:
diff changeset
514 else
kono
parents:
diff changeset
515 ret = fold (expr);
kono
parents:
diff changeset
516 *maybe_const_operands &= op0_const;
kono
parents:
diff changeset
517 *maybe_const_itself &= op0_const_self;
kono
parents:
diff changeset
518 if (!(flag_isoc99
kono
parents:
diff changeset
519 && op0_const
kono
parents:
diff changeset
520 && op0_const_self
kono
parents:
diff changeset
521 && op0 == truthvalue_false_node))
kono
parents:
diff changeset
522 *maybe_const_operands &= op1_const;
kono
parents:
diff changeset
523 if (!(op0_const
kono
parents:
diff changeset
524 && op0_const_self
kono
parents:
diff changeset
525 && op0 == truthvalue_false_node))
kono
parents:
diff changeset
526 *maybe_const_itself &= op1_const_self;
kono
parents:
diff changeset
527 if (!(flag_isoc99
kono
parents:
diff changeset
528 && op0_const
kono
parents:
diff changeset
529 && op0_const_self
kono
parents:
diff changeset
530 && op0 == truthvalue_true_node))
kono
parents:
diff changeset
531 *maybe_const_operands &= op2_const;
kono
parents:
diff changeset
532 if (!(op0_const
kono
parents:
diff changeset
533 && op0_const_self
kono
parents:
diff changeset
534 && op0 == truthvalue_true_node))
kono
parents:
diff changeset
535 *maybe_const_itself &= op2_const_self;
kono
parents:
diff changeset
536 goto out;
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 case VEC_COND_EXPR:
kono
parents:
diff changeset
539 orig_op0 = op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
540 orig_op1 = op1 = TREE_OPERAND (expr, 1);
kono
parents:
diff changeset
541 orig_op2 = op2 = TREE_OPERAND (expr, 2);
kono
parents:
diff changeset
542 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
kono
parents:
diff changeset
543 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
544 STRIP_TYPE_NOPS (op0);
kono
parents:
diff changeset
545 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
kono
parents:
diff changeset
546 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
547 STRIP_TYPE_NOPS (op1);
kono
parents:
diff changeset
548 op2 = c_fully_fold_internal (op2, in_init, maybe_const_operands,
kono
parents:
diff changeset
549 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
550 STRIP_TYPE_NOPS (op2);
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
kono
parents:
diff changeset
553 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
kono
parents:
diff changeset
554 else
kono
parents:
diff changeset
555 ret = fold (expr);
kono
parents:
diff changeset
556 goto out;
kono
parents:
diff changeset
557
kono
parents:
diff changeset
558 case EXCESS_PRECISION_EXPR:
kono
parents:
diff changeset
559 /* Each case where an operand with excess precision may be
kono
parents:
diff changeset
560 encountered must remove the EXCESS_PRECISION_EXPR around
kono
parents:
diff changeset
561 inner operands and possibly put one around the whole
kono
parents:
diff changeset
562 expression or possibly convert to the semantic type (which
kono
parents:
diff changeset
563 c_fully_fold does); we cannot tell at this stage which is
kono
parents:
diff changeset
564 appropriate in any particular case. */
kono
parents:
diff changeset
565 gcc_unreachable ();
kono
parents:
diff changeset
566
kono
parents:
diff changeset
567 case SAVE_EXPR:
kono
parents:
diff changeset
568 /* Make sure to fold the contents of a SAVE_EXPR exactly once. */
kono
parents:
diff changeset
569 op0 = TREE_OPERAND (expr, 0);
kono
parents:
diff changeset
570 if (!SAVE_EXPR_FOLDED_P (expr))
kono
parents:
diff changeset
571 {
kono
parents:
diff changeset
572 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
kono
parents:
diff changeset
573 maybe_const_itself, for_int_const);
kono
parents:
diff changeset
574 TREE_OPERAND (expr, 0) = op0;
kono
parents:
diff changeset
575 SAVE_EXPR_FOLDED_P (expr) = true;
kono
parents:
diff changeset
576 }
kono
parents:
diff changeset
577 /* Return the SAVE_EXPR operand if it is invariant. */
kono
parents:
diff changeset
578 if (tree_invariant_p (op0))
kono
parents:
diff changeset
579 ret = op0;
kono
parents:
diff changeset
580 goto out;
kono
parents:
diff changeset
581
kono
parents:
diff changeset
582 default:
kono
parents:
diff changeset
583 /* Various codes may appear through folding built-in functions
kono
parents:
diff changeset
584 and their arguments. */
kono
parents:
diff changeset
585 goto out;
kono
parents:
diff changeset
586 }
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 out:
kono
parents:
diff changeset
589 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
kono
parents:
diff changeset
590 have been done by this point, so remove them again. */
kono
parents:
diff changeset
591 nowarning |= TREE_NO_WARNING (ret);
kono
parents:
diff changeset
592 STRIP_TYPE_NOPS (ret);
kono
parents:
diff changeset
593 if (nowarning && !TREE_NO_WARNING (ret))
kono
parents:
diff changeset
594 {
kono
parents:
diff changeset
595 if (!CAN_HAVE_LOCATION_P (ret))
kono
parents:
diff changeset
596 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
kono
parents:
diff changeset
597 TREE_NO_WARNING (ret) = 1;
kono
parents:
diff changeset
598 }
kono
parents:
diff changeset
599 if (ret != expr)
kono
parents:
diff changeset
600 {
kono
parents:
diff changeset
601 protected_set_expr_location (ret, loc);
kono
parents:
diff changeset
602 if (IS_EXPR_CODE_CLASS (kind))
kono
parents:
diff changeset
603 set_source_range (ret, old_range.m_start, old_range.m_finish);
kono
parents:
diff changeset
604 }
kono
parents:
diff changeset
605 return ret;
kono
parents:
diff changeset
606 }
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
kono
parents:
diff changeset
609 return EXP. Otherwise, return either EXP or its known constant
kono
parents:
diff changeset
610 value (if it has one), but return EXP if EXP has mode BLKmode. ???
kono
parents:
diff changeset
611 Is the BLKmode test appropriate? */
kono
parents:
diff changeset
612
kono
parents:
diff changeset
613 tree
kono
parents:
diff changeset
614 decl_constant_value_for_optimization (tree exp)
kono
parents:
diff changeset
615 {
kono
parents:
diff changeset
616 tree ret;
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 if (!optimize
kono
parents:
diff changeset
619 || !VAR_P (exp)
kono
parents:
diff changeset
620 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
kono
parents:
diff changeset
621 || DECL_MODE (exp) == BLKmode)
kono
parents:
diff changeset
622 return exp;
kono
parents:
diff changeset
623
kono
parents:
diff changeset
624 ret = decl_constant_value (exp);
kono
parents:
diff changeset
625 /* Avoid unwanted tree sharing between the initializer and current
kono
parents:
diff changeset
626 function's body where the tree can be modified e.g. by the
kono
parents:
diff changeset
627 gimplifier. */
kono
parents:
diff changeset
628 if (ret != exp && TREE_STATIC (exp))
kono
parents:
diff changeset
629 ret = unshare_expr (ret);
kono
parents:
diff changeset
630 return ret;
kono
parents:
diff changeset
631 }