]>
Commit | Line | Data |
---|---|---|
cbdd87d4 | 1 | /* Statement simplification on GIMPLE. |
d1e082c2 | 2 | Copyright (C) 2010-2013 Free Software Foundation, Inc. |
cbdd87d4 RG |
3 | Split out from tree-ssa-ccp.c. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
9 | Free Software Foundation; either version 3, or (at your option) any | |
10 | later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
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/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
26 | #include "flags.h" | |
cbdd87d4 | 27 | #include "function.h" |
7ee2468b | 28 | #include "dumpfile.h" |
7a300452 | 29 | #include "tree-ssa.h" |
cbdd87d4 | 30 | #include "tree-ssa-propagate.h" |
cbdd87d4 | 31 | #include "target.h" |
450ad0cd JH |
32 | #include "ipa-utils.h" |
33 | #include "gimple-pretty-print.h" | |
cbdd87d4 | 34 | |
b3b9f3d0 | 35 | /* Return true when DECL can be referenced from current unit. |
c44c2088 JH |
36 | FROM_DECL (if non-null) specify constructor of variable DECL was taken from. |
37 | We can get declarations that are not possible to reference for various | |
38 | reasons: | |
1389294c | 39 | |
1389294c JH |
40 | 1) When analyzing C++ virtual tables. |
41 | C++ virtual tables do have known constructors even | |
42 | when they are keyed to other compilation unit. | |
43 | Those tables can contain pointers to methods and vars | |
44 | in other units. Those methods have both STATIC and EXTERNAL | |
45 | set. | |
46 | 2) In WHOPR mode devirtualization might lead to reference | |
47 | to method that was partitioned elsehwere. | |
48 | In this case we have static VAR_DECL or FUNCTION_DECL | |
49 | that has no corresponding callgraph/varpool node | |
b3b9f3d0 JH |
50 | declaring the body. |
51 | 3) COMDAT functions referred by external vtables that | |
52 | we devirtualize only during final copmilation stage. | |
53 | At this time we already decided that we will not output | |
54 | the function body and thus we can't reference the symbol | |
55 | directly. */ | |
56 | ||
1389294c | 57 | static bool |
c44c2088 | 58 | can_refer_decl_in_current_unit_p (tree decl, tree from_decl) |
1389294c JH |
59 | { |
60 | struct varpool_node *vnode; | |
61 | struct cgraph_node *node; | |
c44c2088 JH |
62 | symtab_node snode; |
63 | ||
1632a686 JH |
64 | if (DECL_ABSTRACT (decl)) |
65 | return false; | |
66 | ||
67 | /* We are concerned only about static/external vars and functions. */ | |
68 | if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) | |
69 | || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)) | |
70 | return true; | |
71 | ||
72 | /* Static objects can be referred only if they was not optimized out yet. */ | |
73 | if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) | |
74 | { | |
75 | snode = symtab_get_node (decl); | |
76 | if (!snode) | |
77 | return false; | |
78 | node = dyn_cast <cgraph_node> (snode); | |
79 | return !node || !node->global.inlined_to; | |
80 | } | |
81 | ||
6da8be89 | 82 | /* We will later output the initializer, so we can refer to it. |
c44c2088 JH |
83 | So we are concerned only when DECL comes from initializer of |
84 | external var. */ | |
85 | if (!from_decl | |
86 | || TREE_CODE (from_decl) != VAR_DECL | |
87 | || !DECL_EXTERNAL (from_decl) | |
6da8be89 MM |
88 | || (flag_ltrans |
89 | && symtab_get_node (from_decl)->symbol.in_other_partition)) | |
c44c2088 | 90 | return true; |
c44c2088 JH |
91 | /* We are folding reference from external vtable. The vtable may reffer |
92 | to a symbol keyed to other compilation unit. The other compilation | |
93 | unit may be in separate DSO and the symbol may be hidden. */ | |
94 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
95 | && DECL_EXTERNAL (decl) | |
96 | && (!(snode = symtab_get_node (decl)) || !snode->symbol.in_other_partition)) | |
97 | return false; | |
b3b9f3d0 JH |
98 | /* When function is public, we always can introduce new reference. |
99 | Exception are the COMDAT functions where introducing a direct | |
100 | reference imply need to include function body in the curren tunit. */ | |
101 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
102 | return true; | |
103 | /* We are not at ltrans stage; so don't worry about WHOPR. | |
104 | Also when still gimplifying all referred comdat functions will be | |
2e9bb6ba | 105 | produced. |
c44c2088 JH |
106 | |
107 | As observed in PR20991 for already optimized out comdat virtual functions | |
073a8998 | 108 | it may be tempting to not necessarily give up because the copy will be |
c44c2088 JH |
109 | output elsewhere when corresponding vtable is output. |
110 | This is however not possible - ABI specify that COMDATs are output in | |
111 | units where they are used and when the other unit was compiled with LTO | |
112 | it is possible that vtable was kept public while the function itself | |
113 | was privatized. */ | |
b3b9f3d0 JH |
114 | if (!flag_ltrans && (!DECL_COMDAT (decl) || !cgraph_function_flags_ready)) |
115 | return true; | |
c44c2088 JH |
116 | |
117 | /* OK we are seeing either COMDAT or static variable. In this case we must | |
118 | check that the definition is still around so we can refer it. */ | |
1389294c JH |
119 | if (TREE_CODE (decl) == FUNCTION_DECL) |
120 | { | |
121 | node = cgraph_get_node (decl); | |
b3b9f3d0 JH |
122 | /* Check that we still have function body and that we didn't took |
123 | the decision to eliminate offline copy of the function yet. | |
124 | The second is important when devirtualization happens during final | |
125 | compilation stage when making a new reference no longer makes callee | |
126 | to be compiled. */ | |
e70670cf | 127 | if (!node || !node->symbol.definition || node->global.inlined_to) |
c44c2088 JH |
128 | { |
129 | gcc_checking_assert (!TREE_ASM_WRITTEN (decl)); | |
130 | return false; | |
131 | } | |
1389294c JH |
132 | } |
133 | else if (TREE_CODE (decl) == VAR_DECL) | |
134 | { | |
135 | vnode = varpool_get_node (decl); | |
e70670cf | 136 | if (!vnode || !vnode->symbol.definition) |
c44c2088 JH |
137 | { |
138 | gcc_checking_assert (!TREE_ASM_WRITTEN (decl)); | |
139 | return false; | |
140 | } | |
1389294c | 141 | } |
b3b9f3d0 | 142 | return true; |
1389294c JH |
143 | } |
144 | ||
0038d4e0 | 145 | /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into |
c44c2088 JH |
146 | acceptable form for is_gimple_min_invariant. |
147 | FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */ | |
17f39a39 JH |
148 | |
149 | tree | |
c44c2088 | 150 | canonicalize_constructor_val (tree cval, tree from_decl) |
17f39a39 | 151 | { |
50619002 EB |
152 | tree orig_cval = cval; |
153 | STRIP_NOPS (cval); | |
315f5f1b RG |
154 | if (TREE_CODE (cval) == POINTER_PLUS_EXPR |
155 | && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST) | |
17f39a39 | 156 | { |
315f5f1b RG |
157 | tree ptr = TREE_OPERAND (cval, 0); |
158 | if (is_gimple_min_invariant (ptr)) | |
159 | cval = build1_loc (EXPR_LOCATION (cval), | |
160 | ADDR_EXPR, TREE_TYPE (ptr), | |
161 | fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)), | |
162 | ptr, | |
163 | fold_convert (ptr_type_node, | |
164 | TREE_OPERAND (cval, 1)))); | |
17f39a39 JH |
165 | } |
166 | if (TREE_CODE (cval) == ADDR_EXPR) | |
167 | { | |
5a27a197 RG |
168 | tree base = NULL_TREE; |
169 | if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR) | |
ca5f4331 MM |
170 | { |
171 | base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0)); | |
172 | if (base) | |
173 | TREE_OPERAND (cval, 0) = base; | |
174 | } | |
5a27a197 RG |
175 | else |
176 | base = get_base_address (TREE_OPERAND (cval, 0)); | |
7501ca28 RG |
177 | if (!base) |
178 | return NULL_TREE; | |
b3b9f3d0 | 179 | |
7501ca28 RG |
180 | if ((TREE_CODE (base) == VAR_DECL |
181 | || TREE_CODE (base) == FUNCTION_DECL) | |
c44c2088 | 182 | && !can_refer_decl_in_current_unit_p (base, from_decl)) |
1389294c | 183 | return NULL_TREE; |
7501ca28 | 184 | if (TREE_CODE (base) == VAR_DECL) |
46eb666a | 185 | TREE_ADDRESSABLE (base) = 1; |
7501ca28 RG |
186 | else if (TREE_CODE (base) == FUNCTION_DECL) |
187 | { | |
188 | /* Make sure we create a cgraph node for functions we'll reference. | |
189 | They can be non-existent if the reference comes from an entry | |
190 | of an external vtable for example. */ | |
66e6b990 | 191 | cgraph_get_create_real_symbol_node (base); |
7501ca28 | 192 | } |
0038d4e0 | 193 | /* Fixup types in global initializers. */ |
73aef89e RG |
194 | if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0))) |
195 | cval = build_fold_addr_expr (TREE_OPERAND (cval, 0)); | |
50619002 EB |
196 | |
197 | if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval))) | |
198 | cval = fold_convert (TREE_TYPE (orig_cval), cval); | |
199 | return cval; | |
17f39a39 | 200 | } |
50619002 | 201 | return orig_cval; |
17f39a39 | 202 | } |
cbdd87d4 RG |
203 | |
204 | /* If SYM is a constant variable with known value, return the value. | |
205 | NULL_TREE is returned otherwise. */ | |
206 | ||
207 | tree | |
208 | get_symbol_constant_value (tree sym) | |
209 | { | |
6a6dac52 JH |
210 | tree val = ctor_for_folding (sym); |
211 | if (val != error_mark_node) | |
cbdd87d4 | 212 | { |
cbdd87d4 RG |
213 | if (val) |
214 | { | |
9d60be38 | 215 | val = canonicalize_constructor_val (unshare_expr (val), sym); |
1389294c | 216 | if (val && is_gimple_min_invariant (val)) |
17f39a39 | 217 | return val; |
1389294c JH |
218 | else |
219 | return NULL_TREE; | |
cbdd87d4 RG |
220 | } |
221 | /* Variables declared 'const' without an initializer | |
222 | have zero as the initializer if they may not be | |
223 | overridden at link or run time. */ | |
224 | if (!val | |
cbdd87d4 RG |
225 | && (INTEGRAL_TYPE_P (TREE_TYPE (sym)) |
226 | || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym)))) | |
e8160c9a | 227 | return build_zero_cst (TREE_TYPE (sym)); |
cbdd87d4 RG |
228 | } |
229 | ||
230 | return NULL_TREE; | |
231 | } | |
232 | ||
233 | ||
cbdd87d4 RG |
234 | |
235 | /* Subroutine of fold_stmt. We perform several simplifications of the | |
236 | memory reference tree EXPR and make sure to re-gimplify them properly | |
237 | after propagation of constant addresses. IS_LHS is true if the | |
238 | reference is supposed to be an lvalue. */ | |
239 | ||
240 | static tree | |
241 | maybe_fold_reference (tree expr, bool is_lhs) | |
242 | { | |
243 | tree *t = &expr; | |
17f39a39 | 244 | tree result; |
cbdd87d4 | 245 | |
f0eddb90 RG |
246 | if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR |
247 | || TREE_CODE (expr) == REALPART_EXPR | |
248 | || TREE_CODE (expr) == IMAGPART_EXPR) | |
249 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
250 | return fold_unary_loc (EXPR_LOCATION (expr), | |
251 | TREE_CODE (expr), | |
252 | TREE_TYPE (expr), | |
253 | TREE_OPERAND (expr, 0)); | |
254 | else if (TREE_CODE (expr) == BIT_FIELD_REF | |
255 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
256 | return fold_ternary_loc (EXPR_LOCATION (expr), | |
257 | TREE_CODE (expr), | |
258 | TREE_TYPE (expr), | |
259 | TREE_OPERAND (expr, 0), | |
260 | TREE_OPERAND (expr, 1), | |
261 | TREE_OPERAND (expr, 2)); | |
262 | ||
263 | while (handled_component_p (*t)) | |
264 | t = &TREE_OPERAND (*t, 0); | |
cbdd87d4 | 265 | |
f0eddb90 RG |
266 | /* Canonicalize MEM_REFs invariant address operand. Do this first |
267 | to avoid feeding non-canonical MEM_REFs elsewhere. */ | |
268 | if (TREE_CODE (*t) == MEM_REF | |
269 | && !is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0))) | |
cbdd87d4 | 270 | { |
f0eddb90 RG |
271 | bool volatile_p = TREE_THIS_VOLATILE (*t); |
272 | tree tem = fold_binary (MEM_REF, TREE_TYPE (*t), | |
273 | TREE_OPERAND (*t, 0), | |
274 | TREE_OPERAND (*t, 1)); | |
275 | if (tem) | |
276 | { | |
277 | TREE_THIS_VOLATILE (tem) = volatile_p; | |
278 | *t = tem; | |
279 | tem = maybe_fold_reference (expr, is_lhs); | |
280 | if (tem) | |
281 | return tem; | |
282 | return expr; | |
283 | } | |
cbdd87d4 RG |
284 | } |
285 | ||
f0eddb90 RG |
286 | if (!is_lhs |
287 | && (result = fold_const_aggregate_ref (expr)) | |
288 | && is_gimple_min_invariant (result)) | |
289 | return result; | |
cbdd87d4 | 290 | |
70f34814 RG |
291 | /* Fold back MEM_REFs to reference trees. */ |
292 | if (TREE_CODE (*t) == MEM_REF | |
293 | && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR | |
294 | && integer_zerop (TREE_OPERAND (*t, 1)) | |
295 | && (TREE_THIS_VOLATILE (*t) | |
296 | == TREE_THIS_VOLATILE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))) | |
297 | && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*t, 1))) | |
298 | && (TYPE_MAIN_VARIANT (TREE_TYPE (*t)) | |
299 | == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (*t, 1))))) | |
300 | /* We have to look out here to not drop a required conversion | |
301 | from the rhs to the lhs if is_lhs, but we don't have the | |
302 | rhs here to verify that. Thus require strict type | |
303 | compatibility. */ | |
304 | && types_compatible_p (TREE_TYPE (*t), | |
305 | TREE_TYPE (TREE_OPERAND | |
f0eddb90 | 306 | (TREE_OPERAND (*t, 0), 0)))) |
cbdd87d4 | 307 | { |
70f34814 RG |
308 | tree tem; |
309 | *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
310 | tem = maybe_fold_reference (expr, is_lhs); | |
311 | if (tem) | |
312 | return tem; | |
313 | return expr; | |
314 | } | |
4d948885 RG |
315 | else if (TREE_CODE (*t) == TARGET_MEM_REF) |
316 | { | |
317 | tree tem = maybe_fold_tmr (*t); | |
318 | if (tem) | |
319 | { | |
320 | *t = tem; | |
321 | tem = maybe_fold_reference (expr, is_lhs); | |
322 | if (tem) | |
323 | return tem; | |
324 | return expr; | |
325 | } | |
326 | } | |
cbdd87d4 RG |
327 | |
328 | return NULL_TREE; | |
329 | } | |
330 | ||
331 | ||
332 | /* Attempt to fold an assignment statement pointed-to by SI. Returns a | |
333 | replacement rhs for the statement or NULL_TREE if no simplification | |
334 | could be made. It is assumed that the operands have been previously | |
335 | folded. */ | |
336 | ||
337 | static tree | |
338 | fold_gimple_assign (gimple_stmt_iterator *si) | |
339 | { | |
340 | gimple stmt = gsi_stmt (*si); | |
341 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
342 | location_t loc = gimple_location (stmt); | |
343 | ||
344 | tree result = NULL_TREE; | |
345 | ||
346 | switch (get_gimple_rhs_class (subcode)) | |
347 | { | |
348 | case GIMPLE_SINGLE_RHS: | |
349 | { | |
350 | tree rhs = gimple_assign_rhs1 (stmt); | |
351 | ||
4e71066d | 352 | if (REFERENCE_CLASS_P (rhs)) |
cbdd87d4 RG |
353 | return maybe_fold_reference (rhs, false); |
354 | ||
355 | else if (TREE_CODE (rhs) == ADDR_EXPR) | |
356 | { | |
70f34814 RG |
357 | tree ref = TREE_OPERAND (rhs, 0); |
358 | tree tem = maybe_fold_reference (ref, true); | |
359 | if (tem | |
360 | && TREE_CODE (tem) == MEM_REF | |
361 | && integer_zerop (TREE_OPERAND (tem, 1))) | |
362 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0)); | |
363 | else if (tem) | |
cbdd87d4 RG |
364 | result = fold_convert (TREE_TYPE (rhs), |
365 | build_fold_addr_expr_loc (loc, tem)); | |
70f34814 RG |
366 | else if (TREE_CODE (ref) == MEM_REF |
367 | && integer_zerop (TREE_OPERAND (ref, 1))) | |
368 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0)); | |
cbdd87d4 RG |
369 | } |
370 | ||
371 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
372 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
373 | && (CONSTRUCTOR_NELTS (rhs) | |
374 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
375 | { | |
376 | /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */ | |
377 | unsigned i; | |
378 | tree val; | |
379 | ||
380 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) | |
381 | if (TREE_CODE (val) != INTEGER_CST | |
382 | && TREE_CODE (val) != REAL_CST | |
383 | && TREE_CODE (val) != FIXED_CST) | |
384 | return NULL_TREE; | |
385 | ||
386 | return build_vector_from_ctor (TREE_TYPE (rhs), | |
387 | CONSTRUCTOR_ELTS (rhs)); | |
388 | } | |
389 | ||
390 | else if (DECL_P (rhs)) | |
9d60be38 | 391 | return get_symbol_constant_value (rhs); |
cbdd87d4 RG |
392 | |
393 | /* If we couldn't fold the RHS, hand over to the generic | |
394 | fold routines. */ | |
395 | if (result == NULL_TREE) | |
396 | result = fold (rhs); | |
397 | ||
398 | /* Strip away useless type conversions. Both the NON_LVALUE_EXPR | |
399 | that may have been added by fold, and "useless" type | |
400 | conversions that might now be apparent due to propagation. */ | |
401 | STRIP_USELESS_TYPE_CONVERSION (result); | |
402 | ||
403 | if (result != rhs && valid_gimple_rhs_p (result)) | |
404 | return result; | |
405 | ||
406 | return NULL_TREE; | |
407 | } | |
408 | break; | |
409 | ||
410 | case GIMPLE_UNARY_RHS: | |
411 | { | |
412 | tree rhs = gimple_assign_rhs1 (stmt); | |
413 | ||
414 | result = fold_unary_loc (loc, subcode, gimple_expr_type (stmt), rhs); | |
415 | if (result) | |
416 | { | |
417 | /* If the operation was a conversion do _not_ mark a | |
418 | resulting constant with TREE_OVERFLOW if the original | |
419 | constant was not. These conversions have implementation | |
420 | defined behavior and retaining the TREE_OVERFLOW flag | |
421 | here would confuse later passes such as VRP. */ | |
422 | if (CONVERT_EXPR_CODE_P (subcode) | |
423 | && TREE_CODE (result) == INTEGER_CST | |
424 | && TREE_CODE (rhs) == INTEGER_CST) | |
425 | TREE_OVERFLOW (result) = TREE_OVERFLOW (rhs); | |
426 | ||
427 | STRIP_USELESS_TYPE_CONVERSION (result); | |
428 | if (valid_gimple_rhs_p (result)) | |
429 | return result; | |
430 | } | |
cbdd87d4 RG |
431 | } |
432 | break; | |
433 | ||
434 | case GIMPLE_BINARY_RHS: | |
9b80d091 KT |
435 | /* Try to canonicalize for boolean-typed X the comparisons |
436 | X == 0, X == 1, X != 0, and X != 1. */ | |
315f5f1b RG |
437 | if (gimple_assign_rhs_code (stmt) == EQ_EXPR |
438 | || gimple_assign_rhs_code (stmt) == NE_EXPR) | |
9b80d091 KT |
439 | { |
440 | tree lhs = gimple_assign_lhs (stmt); | |
441 | tree op1 = gimple_assign_rhs1 (stmt); | |
442 | tree op2 = gimple_assign_rhs2 (stmt); | |
443 | tree type = TREE_TYPE (op1); | |
444 | ||
445 | /* Check whether the comparison operands are of the same boolean | |
446 | type as the result type is. | |
447 | Check that second operand is an integer-constant with value | |
448 | one or zero. */ | |
449 | if (TREE_CODE (op2) == INTEGER_CST | |
450 | && (integer_zerop (op2) || integer_onep (op2)) | |
451 | && useless_type_conversion_p (TREE_TYPE (lhs), type)) | |
452 | { | |
453 | enum tree_code cmp_code = gimple_assign_rhs_code (stmt); | |
454 | bool is_logical_not = false; | |
455 | ||
456 | /* X == 0 and X != 1 is a logical-not.of X | |
457 | X == 1 and X != 0 is X */ | |
458 | if ((cmp_code == EQ_EXPR && integer_zerop (op2)) | |
459 | || (cmp_code == NE_EXPR && integer_onep (op2))) | |
460 | is_logical_not = true; | |
461 | ||
462 | if (is_logical_not == false) | |
463 | result = op1; | |
464 | /* Only for one-bit precision typed X the transformation | |
465 | !X -> ~X is valied. */ | |
466 | else if (TYPE_PRECISION (type) == 1) | |
467 | result = build1_loc (gimple_location (stmt), BIT_NOT_EXPR, | |
468 | type, op1); | |
469 | /* Otherwise we use !X -> X ^ 1. */ | |
470 | else | |
471 | result = build2_loc (gimple_location (stmt), BIT_XOR_EXPR, | |
472 | type, op1, build_int_cst (type, 1)); | |
473 | ||
474 | } | |
475 | } | |
cbdd87d4 RG |
476 | |
477 | if (!result) | |
478 | result = fold_binary_loc (loc, subcode, | |
5fbcc0ed RG |
479 | TREE_TYPE (gimple_assign_lhs (stmt)), |
480 | gimple_assign_rhs1 (stmt), | |
481 | gimple_assign_rhs2 (stmt)); | |
cbdd87d4 RG |
482 | |
483 | if (result) | |
484 | { | |
485 | STRIP_USELESS_TYPE_CONVERSION (result); | |
486 | if (valid_gimple_rhs_p (result)) | |
487 | return result; | |
cbdd87d4 RG |
488 | } |
489 | break; | |
490 | ||
0354c0c7 | 491 | case GIMPLE_TERNARY_RHS: |
4e71066d RG |
492 | /* Try to fold a conditional expression. */ |
493 | if (gimple_assign_rhs_code (stmt) == COND_EXPR) | |
494 | { | |
495 | tree op0 = gimple_assign_rhs1 (stmt); | |
496 | tree tem; | |
497 | bool set = false; | |
498 | location_t cond_loc = gimple_location (stmt); | |
499 | ||
500 | if (COMPARISON_CLASS_P (op0)) | |
501 | { | |
502 | fold_defer_overflow_warnings (); | |
503 | tem = fold_binary_loc (cond_loc, | |
504 | TREE_CODE (op0), TREE_TYPE (op0), | |
505 | TREE_OPERAND (op0, 0), | |
506 | TREE_OPERAND (op0, 1)); | |
507 | /* This is actually a conditional expression, not a GIMPLE | |
508 | conditional statement, however, the valid_gimple_rhs_p | |
509 | test still applies. */ | |
510 | set = (tem && is_gimple_condexpr (tem) | |
511 | && valid_gimple_rhs_p (tem)); | |
512 | fold_undefer_overflow_warnings (set, stmt, 0); | |
513 | } | |
514 | else if (is_gimple_min_invariant (op0)) | |
515 | { | |
516 | tem = op0; | |
517 | set = true; | |
518 | } | |
519 | else | |
520 | return NULL_TREE; | |
521 | ||
522 | if (set) | |
523 | result = fold_build3_loc (cond_loc, COND_EXPR, | |
524 | TREE_TYPE (gimple_assign_lhs (stmt)), tem, | |
525 | gimple_assign_rhs2 (stmt), | |
526 | gimple_assign_rhs3 (stmt)); | |
527 | } | |
528 | ||
529 | if (!result) | |
530 | result = fold_ternary_loc (loc, subcode, | |
531 | TREE_TYPE (gimple_assign_lhs (stmt)), | |
532 | gimple_assign_rhs1 (stmt), | |
533 | gimple_assign_rhs2 (stmt), | |
534 | gimple_assign_rhs3 (stmt)); | |
0354c0c7 BS |
535 | |
536 | if (result) | |
537 | { | |
538 | STRIP_USELESS_TYPE_CONVERSION (result); | |
539 | if (valid_gimple_rhs_p (result)) | |
540 | return result; | |
0354c0c7 BS |
541 | } |
542 | break; | |
543 | ||
cbdd87d4 RG |
544 | case GIMPLE_INVALID_RHS: |
545 | gcc_unreachable (); | |
546 | } | |
547 | ||
548 | return NULL_TREE; | |
549 | } | |
550 | ||
551 | /* Attempt to fold a conditional statement. Return true if any changes were | |
552 | made. We only attempt to fold the condition expression, and do not perform | |
553 | any transformation that would require alteration of the cfg. It is | |
554 | assumed that the operands have been previously folded. */ | |
555 | ||
556 | static bool | |
557 | fold_gimple_cond (gimple stmt) | |
558 | { | |
559 | tree result = fold_binary_loc (gimple_location (stmt), | |
560 | gimple_cond_code (stmt), | |
561 | boolean_type_node, | |
562 | gimple_cond_lhs (stmt), | |
563 | gimple_cond_rhs (stmt)); | |
564 | ||
565 | if (result) | |
566 | { | |
567 | STRIP_USELESS_TYPE_CONVERSION (result); | |
568 | if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result)) | |
569 | { | |
570 | gimple_cond_set_condition_from_tree (stmt, result); | |
571 | return true; | |
572 | } | |
573 | } | |
574 | ||
575 | return false; | |
576 | } | |
577 | ||
578 | /* Convert EXPR into a GIMPLE value suitable for substitution on the | |
579 | RHS of an assignment. Insert the necessary statements before | |
580 | iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL | |
581 | is replaced. If the call is expected to produces a result, then it | |
582 | is replaced by an assignment of the new RHS to the result variable. | |
583 | If the result is to be ignored, then the call is replaced by a | |
fe2ef088 MM |
584 | GIMPLE_NOP. A proper VDEF chain is retained by making the first |
585 | VUSE and the last VDEF of the whole sequence be the same as the replaced | |
586 | statement and using new SSA names for stores in between. */ | |
cbdd87d4 RG |
587 | |
588 | void | |
589 | gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr) | |
590 | { | |
591 | tree lhs; | |
cbdd87d4 RG |
592 | gimple stmt, new_stmt; |
593 | gimple_stmt_iterator i; | |
355a7673 | 594 | gimple_seq stmts = NULL; |
cbdd87d4 | 595 | struct gimplify_ctx gctx; |
e256dfce | 596 | gimple laststore; |
fe2ef088 | 597 | tree reaching_vuse; |
cbdd87d4 RG |
598 | |
599 | stmt = gsi_stmt (*si_p); | |
600 | ||
601 | gcc_assert (is_gimple_call (stmt)); | |
602 | ||
cbdd87d4 | 603 | push_gimplify_context (&gctx); |
21860814 | 604 | gctx.into_ssa = gimple_in_ssa_p (cfun); |
cbdd87d4 | 605 | |
e256dfce | 606 | lhs = gimple_call_lhs (stmt); |
cbdd87d4 | 607 | if (lhs == NULL_TREE) |
6e572326 RG |
608 | { |
609 | gimplify_and_add (expr, &stmts); | |
610 | /* We can end up with folding a memcpy of an empty class assignment | |
611 | which gets optimized away by C++ gimplification. */ | |
612 | if (gimple_seq_empty_p (stmts)) | |
613 | { | |
9fdc58de | 614 | pop_gimplify_context (NULL); |
6e572326 RG |
615 | if (gimple_in_ssa_p (cfun)) |
616 | { | |
617 | unlink_stmt_vdef (stmt); | |
618 | release_defs (stmt); | |
619 | } | |
77d19c72 | 620 | gsi_replace (si_p, gimple_build_nop (), true); |
6e572326 RG |
621 | return; |
622 | } | |
623 | } | |
cbdd87d4 | 624 | else |
e256dfce RG |
625 | { |
626 | tree tmp = get_initialized_tmp_var (expr, &stmts, NULL); | |
627 | new_stmt = gimple_build_assign (lhs, tmp); | |
628 | i = gsi_last (stmts); | |
629 | gsi_insert_after_without_update (&i, new_stmt, | |
630 | GSI_CONTINUE_LINKING); | |
631 | } | |
cbdd87d4 RG |
632 | |
633 | pop_gimplify_context (NULL); | |
634 | ||
635 | if (gimple_has_location (stmt)) | |
636 | annotate_all_with_location (stmts, gimple_location (stmt)); | |
637 | ||
e256dfce RG |
638 | /* First iterate over the replacement statements backward, assigning |
639 | virtual operands to their defining statements. */ | |
640 | laststore = NULL; | |
641 | for (i = gsi_last (stmts); !gsi_end_p (i); gsi_prev (&i)) | |
642 | { | |
643 | new_stmt = gsi_stmt (i); | |
949e47e5 JJ |
644 | if ((gimple_assign_single_p (new_stmt) |
645 | && !is_gimple_reg (gimple_assign_lhs (new_stmt))) | |
646 | || (is_gimple_call (new_stmt) | |
647 | && (gimple_call_flags (new_stmt) | |
648 | & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0)) | |
e256dfce RG |
649 | { |
650 | tree vdef; | |
651 | if (!laststore) | |
652 | vdef = gimple_vdef (stmt); | |
653 | else | |
654 | vdef = make_ssa_name (gimple_vop (cfun), new_stmt); | |
655 | gimple_set_vdef (new_stmt, vdef); | |
52f26be4 | 656 | if (vdef && TREE_CODE (vdef) == SSA_NAME) |
e256dfce RG |
657 | SSA_NAME_DEF_STMT (vdef) = new_stmt; |
658 | laststore = new_stmt; | |
659 | } | |
660 | } | |
661 | ||
662 | /* Second iterate over the statements forward, assigning virtual | |
663 | operands to their uses. */ | |
e256dfce | 664 | reaching_vuse = gimple_vuse (stmt); |
cbdd87d4 RG |
665 | for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i)) |
666 | { | |
cbdd87d4 | 667 | new_stmt = gsi_stmt (i); |
a24ac460 RG |
668 | /* If the new statement possibly has a VUSE, update it with exact SSA |
669 | name we know will reach this one. */ | |
670 | if (gimple_has_mem_ops (new_stmt)) | |
e256dfce RG |
671 | gimple_set_vuse (new_stmt, reaching_vuse); |
672 | gimple_set_modified (new_stmt, true); | |
673 | if (gimple_vdef (new_stmt)) | |
674 | reaching_vuse = gimple_vdef (new_stmt); | |
cbdd87d4 RG |
675 | } |
676 | ||
e256dfce RG |
677 | /* If the new sequence does not do a store release the virtual |
678 | definition of the original statement. */ | |
679 | if (reaching_vuse | |
680 | && reaching_vuse == gimple_vuse (stmt)) | |
cbdd87d4 | 681 | { |
e256dfce RG |
682 | tree vdef = gimple_vdef (stmt); |
683 | if (vdef | |
684 | && TREE_CODE (vdef) == SSA_NAME) | |
fe2ef088 MM |
685 | { |
686 | unlink_stmt_vdef (stmt); | |
e256dfce | 687 | release_ssa_name (vdef); |
8a1561bc | 688 | } |
cbdd87d4 RG |
689 | } |
690 | ||
355a7673 MM |
691 | /* Finally replace the original statement with the sequence. */ |
692 | gsi_replace_with_seq (si_p, stmts, false); | |
cbdd87d4 RG |
693 | } |
694 | ||
695 | /* Return the string length, maximum string length or maximum value of | |
696 | ARG in LENGTH. | |
697 | If ARG is an SSA name variable, follow its use-def chains. If LENGTH | |
698 | is not NULL and, for TYPE == 0, its value is not equal to the length | |
699 | we determine or if we are unable to determine the length or value, | |
700 | return false. VISITED is a bitmap of visited variables. | |
701 | TYPE is 0 if string length should be returned, 1 for maximum string | |
702 | length and 2 for maximum value ARG can have. */ | |
703 | ||
704 | static bool | |
705 | get_maxval_strlen (tree arg, tree *length, bitmap visited, int type) | |
706 | { | |
707 | tree var, val; | |
708 | gimple def_stmt; | |
709 | ||
710 | if (TREE_CODE (arg) != SSA_NAME) | |
711 | { | |
cbdd87d4 | 712 | /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */ |
763dc99d AP |
713 | if (TREE_CODE (arg) == ADDR_EXPR |
714 | && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF | |
715 | && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1))) | |
cbdd87d4 RG |
716 | { |
717 | tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
718 | if (TREE_CODE (aop0) == INDIRECT_REF | |
719 | && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME) | |
720 | return get_maxval_strlen (TREE_OPERAND (aop0, 0), | |
721 | length, visited, type); | |
722 | } | |
723 | ||
724 | if (type == 2) | |
725 | { | |
726 | val = arg; | |
727 | if (TREE_CODE (val) != INTEGER_CST | |
728 | || tree_int_cst_sgn (val) < 0) | |
729 | return false; | |
730 | } | |
731 | else | |
732 | val = c_strlen (arg, 1); | |
733 | if (!val) | |
734 | return false; | |
735 | ||
736 | if (*length) | |
737 | { | |
738 | if (type > 0) | |
739 | { | |
740 | if (TREE_CODE (*length) != INTEGER_CST | |
741 | || TREE_CODE (val) != INTEGER_CST) | |
742 | return false; | |
743 | ||
744 | if (tree_int_cst_lt (*length, val)) | |
745 | *length = val; | |
746 | return true; | |
747 | } | |
748 | else if (simple_cst_equal (val, *length) != 1) | |
749 | return false; | |
750 | } | |
751 | ||
752 | *length = val; | |
753 | return true; | |
754 | } | |
755 | ||
491e0b9b RG |
756 | /* If ARG is registered for SSA update we cannot look at its defining |
757 | statement. */ | |
758 | if (name_registered_for_update_p (arg)) | |
759 | return false; | |
760 | ||
cbdd87d4 | 761 | /* If we were already here, break the infinite cycle. */ |
fcaa4ca4 | 762 | if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg))) |
cbdd87d4 | 763 | return true; |
cbdd87d4 RG |
764 | |
765 | var = arg; | |
766 | def_stmt = SSA_NAME_DEF_STMT (var); | |
767 | ||
768 | switch (gimple_code (def_stmt)) | |
769 | { | |
770 | case GIMPLE_ASSIGN: | |
771 | /* The RHS of the statement defining VAR must either have a | |
772 | constant length or come from another SSA_NAME with a constant | |
773 | length. */ | |
774 | if (gimple_assign_single_p (def_stmt) | |
775 | || gimple_assign_unary_nop_p (def_stmt)) | |
776 | { | |
777 | tree rhs = gimple_assign_rhs1 (def_stmt); | |
778 | return get_maxval_strlen (rhs, length, visited, type); | |
779 | } | |
763dc99d AP |
780 | else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR) |
781 | { | |
782 | tree op2 = gimple_assign_rhs2 (def_stmt); | |
783 | tree op3 = gimple_assign_rhs3 (def_stmt); | |
784 | return get_maxval_strlen (op2, length, visited, type) | |
785 | && get_maxval_strlen (op3, length, visited, type); | |
786 | } | |
cbdd87d4 RG |
787 | return false; |
788 | ||
789 | case GIMPLE_PHI: | |
790 | { | |
791 | /* All the arguments of the PHI node must have the same constant | |
792 | length. */ | |
793 | unsigned i; | |
794 | ||
795 | for (i = 0; i < gimple_phi_num_args (def_stmt); i++) | |
796 | { | |
797 | tree arg = gimple_phi_arg (def_stmt, i)->def; | |
798 | ||
799 | /* If this PHI has itself as an argument, we cannot | |
800 | determine the string length of this argument. However, | |
801 | if we can find a constant string length for the other | |
802 | PHI args then we can still be sure that this is a | |
803 | constant string length. So be optimistic and just | |
804 | continue with the next argument. */ | |
805 | if (arg == gimple_phi_result (def_stmt)) | |
806 | continue; | |
807 | ||
808 | if (!get_maxval_strlen (arg, length, visited, type)) | |
809 | return false; | |
810 | } | |
811 | } | |
812 | return true; | |
813 | ||
814 | default: | |
815 | return false; | |
816 | } | |
817 | } | |
818 | ||
819 | ||
820 | /* Fold builtin call in statement STMT. Returns a simplified tree. | |
821 | We may return a non-constant expression, including another call | |
822 | to a different function and with different arguments, e.g., | |
823 | substituting memcpy for strcpy when the string length is known. | |
824 | Note that some builtins expand into inline code that may not | |
825 | be valid in GIMPLE. Callers must take care. */ | |
826 | ||
827 | tree | |
828 | gimple_fold_builtin (gimple stmt) | |
829 | { | |
830 | tree result, val[3]; | |
831 | tree callee, a; | |
832 | int arg_idx, type; | |
833 | bitmap visited; | |
834 | bool ignore; | |
835 | int nargs; | |
836 | location_t loc = gimple_location (stmt); | |
837 | ||
838 | gcc_assert (is_gimple_call (stmt)); | |
839 | ||
840 | ignore = (gimple_call_lhs (stmt) == NULL); | |
841 | ||
842 | /* First try the generic builtin folder. If that succeeds, return the | |
843 | result directly. */ | |
844 | result = fold_call_stmt (stmt, ignore); | |
845 | if (result) | |
846 | { | |
847 | if (ignore) | |
848 | STRIP_NOPS (result); | |
849 | return result; | |
850 | } | |
851 | ||
852 | /* Ignore MD builtins. */ | |
853 | callee = gimple_call_fndecl (stmt); | |
854 | if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD) | |
855 | return NULL_TREE; | |
856 | ||
e7f9dae0 JJ |
857 | /* Give up for always_inline inline builtins until they are |
858 | inlined. */ | |
859 | if (avoid_folding_inline_builtin (callee)) | |
860 | return NULL_TREE; | |
861 | ||
cbdd87d4 RG |
862 | /* If the builtin could not be folded, and it has no argument list, |
863 | we're done. */ | |
864 | nargs = gimple_call_num_args (stmt); | |
865 | if (nargs == 0) | |
866 | return NULL_TREE; | |
867 | ||
868 | /* Limit the work only for builtins we know how to simplify. */ | |
869 | switch (DECL_FUNCTION_CODE (callee)) | |
870 | { | |
871 | case BUILT_IN_STRLEN: | |
872 | case BUILT_IN_FPUTS: | |
873 | case BUILT_IN_FPUTS_UNLOCKED: | |
874 | arg_idx = 0; | |
875 | type = 0; | |
876 | break; | |
877 | case BUILT_IN_STRCPY: | |
878 | case BUILT_IN_STRNCPY: | |
879 | arg_idx = 1; | |
880 | type = 0; | |
881 | break; | |
882 | case BUILT_IN_MEMCPY_CHK: | |
883 | case BUILT_IN_MEMPCPY_CHK: | |
884 | case BUILT_IN_MEMMOVE_CHK: | |
885 | case BUILT_IN_MEMSET_CHK: | |
886 | case BUILT_IN_STRNCPY_CHK: | |
f3fc9b80 | 887 | case BUILT_IN_STPNCPY_CHK: |
cbdd87d4 RG |
888 | arg_idx = 2; |
889 | type = 2; | |
890 | break; | |
891 | case BUILT_IN_STRCPY_CHK: | |
892 | case BUILT_IN_STPCPY_CHK: | |
893 | arg_idx = 1; | |
894 | type = 1; | |
895 | break; | |
896 | case BUILT_IN_SNPRINTF_CHK: | |
897 | case BUILT_IN_VSNPRINTF_CHK: | |
898 | arg_idx = 1; | |
899 | type = 2; | |
900 | break; | |
901 | default: | |
902 | return NULL_TREE; | |
903 | } | |
904 | ||
905 | if (arg_idx >= nargs) | |
906 | return NULL_TREE; | |
907 | ||
908 | /* Try to use the dataflow information gathered by the CCP process. */ | |
909 | visited = BITMAP_ALLOC (NULL); | |
910 | bitmap_clear (visited); | |
911 | ||
912 | memset (val, 0, sizeof (val)); | |
913 | a = gimple_call_arg (stmt, arg_idx); | |
914 | if (!get_maxval_strlen (a, &val[arg_idx], visited, type)) | |
915 | val[arg_idx] = NULL_TREE; | |
916 | ||
917 | BITMAP_FREE (visited); | |
918 | ||
919 | result = NULL_TREE; | |
920 | switch (DECL_FUNCTION_CODE (callee)) | |
921 | { | |
922 | case BUILT_IN_STRLEN: | |
923 | if (val[0] && nargs == 1) | |
924 | { | |
925 | tree new_val = | |
926 | fold_convert (TREE_TYPE (gimple_call_lhs (stmt)), val[0]); | |
927 | ||
928 | /* If the result is not a valid gimple value, or not a cast | |
6e4da084 | 929 | of a valid gimple value, then we cannot use the result. */ |
cbdd87d4 | 930 | if (is_gimple_val (new_val) |
3dbe9454 | 931 | || (CONVERT_EXPR_P (new_val) |
cbdd87d4 RG |
932 | && is_gimple_val (TREE_OPERAND (new_val, 0)))) |
933 | return new_val; | |
934 | } | |
935 | break; | |
936 | ||
937 | case BUILT_IN_STRCPY: | |
938 | if (val[1] && is_gimple_val (val[1]) && nargs == 2) | |
939 | result = fold_builtin_strcpy (loc, callee, | |
940 | gimple_call_arg (stmt, 0), | |
941 | gimple_call_arg (stmt, 1), | |
942 | val[1]); | |
943 | break; | |
944 | ||
945 | case BUILT_IN_STRNCPY: | |
946 | if (val[1] && is_gimple_val (val[1]) && nargs == 3) | |
947 | result = fold_builtin_strncpy (loc, callee, | |
948 | gimple_call_arg (stmt, 0), | |
949 | gimple_call_arg (stmt, 1), | |
950 | gimple_call_arg (stmt, 2), | |
951 | val[1]); | |
952 | break; | |
953 | ||
954 | case BUILT_IN_FPUTS: | |
955 | if (nargs == 2) | |
956 | result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0), | |
957 | gimple_call_arg (stmt, 1), | |
958 | ignore, false, val[0]); | |
959 | break; | |
960 | ||
961 | case BUILT_IN_FPUTS_UNLOCKED: | |
962 | if (nargs == 2) | |
963 | result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0), | |
964 | gimple_call_arg (stmt, 1), | |
965 | ignore, true, val[0]); | |
966 | break; | |
967 | ||
968 | case BUILT_IN_MEMCPY_CHK: | |
969 | case BUILT_IN_MEMPCPY_CHK: | |
970 | case BUILT_IN_MEMMOVE_CHK: | |
971 | case BUILT_IN_MEMSET_CHK: | |
972 | if (val[2] && is_gimple_val (val[2]) && nargs == 4) | |
973 | result = fold_builtin_memory_chk (loc, callee, | |
974 | gimple_call_arg (stmt, 0), | |
975 | gimple_call_arg (stmt, 1), | |
976 | gimple_call_arg (stmt, 2), | |
977 | gimple_call_arg (stmt, 3), | |
978 | val[2], ignore, | |
979 | DECL_FUNCTION_CODE (callee)); | |
980 | break; | |
981 | ||
982 | case BUILT_IN_STRCPY_CHK: | |
983 | case BUILT_IN_STPCPY_CHK: | |
984 | if (val[1] && is_gimple_val (val[1]) && nargs == 3) | |
985 | result = fold_builtin_stxcpy_chk (loc, callee, | |
986 | gimple_call_arg (stmt, 0), | |
987 | gimple_call_arg (stmt, 1), | |
988 | gimple_call_arg (stmt, 2), | |
989 | val[1], ignore, | |
990 | DECL_FUNCTION_CODE (callee)); | |
991 | break; | |
992 | ||
993 | case BUILT_IN_STRNCPY_CHK: | |
f3fc9b80 | 994 | case BUILT_IN_STPNCPY_CHK: |
cbdd87d4 | 995 | if (val[2] && is_gimple_val (val[2]) && nargs == 4) |
f3fc9b80 | 996 | result = fold_builtin_stxncpy_chk (loc, gimple_call_arg (stmt, 0), |
cbdd87d4 RG |
997 | gimple_call_arg (stmt, 1), |
998 | gimple_call_arg (stmt, 2), | |
999 | gimple_call_arg (stmt, 3), | |
f3fc9b80 RG |
1000 | val[2], ignore, |
1001 | DECL_FUNCTION_CODE (callee)); | |
cbdd87d4 RG |
1002 | break; |
1003 | ||
1004 | case BUILT_IN_SNPRINTF_CHK: | |
1005 | case BUILT_IN_VSNPRINTF_CHK: | |
1006 | if (val[1] && is_gimple_val (val[1])) | |
1007 | result = gimple_fold_builtin_snprintf_chk (stmt, val[1], | |
1008 | DECL_FUNCTION_CODE (callee)); | |
1009 | break; | |
1010 | ||
1011 | default: | |
1012 | gcc_unreachable (); | |
1013 | } | |
1014 | ||
1015 | if (result && ignore) | |
1016 | result = fold_ignored_result (result); | |
1017 | return result; | |
1018 | } | |
1019 | ||
1ae6fe9b | 1020 | |
49c471e3 MJ |
1021 | /* Return a binfo to be used for devirtualization of calls based on an object |
1022 | represented by a declaration (i.e. a global or automatically allocated one) | |
1023 | or NULL if it cannot be found or is not safe. CST is expected to be an | |
1024 | ADDR_EXPR of such object or the function will return NULL. Currently it is | |
c49bdb2e JH |
1025 | safe to use such binfo only if it has no base binfo (i.e. no ancestors) |
1026 | EXPECTED_TYPE is type of the class virtual belongs to. */ | |
49c471e3 MJ |
1027 | |
1028 | tree | |
c49bdb2e | 1029 | gimple_extract_devirt_binfo_from_cst (tree cst, tree expected_type) |
49c471e3 MJ |
1030 | { |
1031 | HOST_WIDE_INT offset, size, max_size; | |
c49bdb2e | 1032 | tree base, type, binfo; |
49c471e3 MJ |
1033 | bool last_artificial = false; |
1034 | ||
1035 | if (!flag_devirtualize | |
1036 | || TREE_CODE (cst) != ADDR_EXPR | |
1037 | || TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) != RECORD_TYPE) | |
1038 | return NULL_TREE; | |
1039 | ||
1040 | cst = TREE_OPERAND (cst, 0); | |
49c471e3 MJ |
1041 | base = get_ref_base_and_extent (cst, &offset, &size, &max_size); |
1042 | type = TREE_TYPE (base); | |
1043 | if (!DECL_P (base) | |
1044 | || max_size == -1 | |
1045 | || max_size != size | |
1046 | || TREE_CODE (type) != RECORD_TYPE) | |
1047 | return NULL_TREE; | |
1048 | ||
1049 | /* Find the sub-object the constant actually refers to and mark whether it is | |
1050 | an artificial one (as opposed to a user-defined one). */ | |
1051 | while (true) | |
1052 | { | |
1053 | HOST_WIDE_INT pos, size; | |
1054 | tree fld; | |
1055 | ||
d84db1ed | 1056 | if (types_same_for_odr (type, expected_type)) |
49c471e3 MJ |
1057 | break; |
1058 | if (offset < 0) | |
1059 | return NULL_TREE; | |
1060 | ||
1061 | for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld)) | |
1062 | { | |
1063 | if (TREE_CODE (fld) != FIELD_DECL) | |
1064 | continue; | |
1065 | ||
1066 | pos = int_bit_position (fld); | |
1067 | size = tree_low_cst (DECL_SIZE (fld), 1); | |
1068 | if (pos <= offset && (pos + size) > offset) | |
1069 | break; | |
1070 | } | |
1071 | if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE) | |
1072 | return NULL_TREE; | |
1073 | ||
1074 | last_artificial = DECL_ARTIFICIAL (fld); | |
1075 | type = TREE_TYPE (fld); | |
1076 | offset -= pos; | |
1077 | } | |
073a8998 | 1078 | /* Artificial sub-objects are ancestors, we do not want to use them for |
49c471e3 MJ |
1079 | devirtualization, at least not here. */ |
1080 | if (last_artificial) | |
1081 | return NULL_TREE; | |
1082 | binfo = TYPE_BINFO (type); | |
1083 | if (!binfo || BINFO_N_BASE_BINFOS (binfo) > 0) | |
1084 | return NULL_TREE; | |
1085 | else | |
1086 | return binfo; | |
1087 | } | |
1088 | ||
cbdd87d4 RG |
1089 | /* Attempt to fold a call statement referenced by the statement iterator GSI. |
1090 | The statement may be replaced by another statement, e.g., if the call | |
1091 | simplifies to a constant value. Return true if any changes were made. | |
1092 | It is assumed that the operands have been previously folded. */ | |
1093 | ||
e021c122 | 1094 | static bool |
ceeffab0 | 1095 | gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace) |
cbdd87d4 RG |
1096 | { |
1097 | gimple stmt = gsi_stmt (*gsi); | |
3b45a007 | 1098 | tree callee; |
e021c122 RG |
1099 | bool changed = false; |
1100 | unsigned i; | |
cbdd87d4 | 1101 | |
e021c122 RG |
1102 | /* Fold *& in call arguments. */ |
1103 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
1104 | if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i))) | |
1105 | { | |
1106 | tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false); | |
1107 | if (tmp) | |
1108 | { | |
1109 | gimple_call_set_arg (stmt, i, tmp); | |
1110 | changed = true; | |
1111 | } | |
1112 | } | |
3b45a007 RG |
1113 | |
1114 | /* Check for virtual calls that became direct calls. */ | |
1115 | callee = gimple_call_fn (stmt); | |
25583c4f | 1116 | if (callee && TREE_CODE (callee) == OBJ_TYPE_REF) |
3b45a007 | 1117 | { |
49c471e3 MJ |
1118 | if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE) |
1119 | { | |
450ad0cd JH |
1120 | if (dump_file && virtual_method_call_p (callee) |
1121 | && !possible_polymorphic_call_target_p | |
1122 | (callee, cgraph_get_node (gimple_call_addr_fndecl | |
1123 | (OBJ_TYPE_REF_EXPR (callee))))) | |
1124 | { | |
1125 | fprintf (dump_file, | |
1126 | "Type inheritnace inconsistent devirtualization of "); | |
1127 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
1128 | fprintf (dump_file, " to "); | |
1129 | print_generic_expr (dump_file, callee, TDF_SLIM); | |
1130 | fprintf (dump_file, "\n"); | |
1131 | } | |
1132 | ||
49c471e3 | 1133 | gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee)); |
e021c122 RG |
1134 | changed = true; |
1135 | } | |
1d5755ef | 1136 | else if (virtual_method_call_p (callee)) |
e021c122 RG |
1137 | { |
1138 | tree obj = OBJ_TYPE_REF_OBJECT (callee); | |
c49bdb2e JH |
1139 | tree binfo = gimple_extract_devirt_binfo_from_cst |
1140 | (obj, obj_type_ref_class (callee)); | |
e021c122 RG |
1141 | if (binfo) |
1142 | { | |
1143 | HOST_WIDE_INT token | |
1144 | = TREE_INT_CST_LOW (OBJ_TYPE_REF_TOKEN (callee)); | |
1145 | tree fndecl = gimple_get_virt_method_for_binfo (token, binfo); | |
1146 | if (fndecl) | |
1147 | { | |
450ad0cd JH |
1148 | #ifdef ENABLE_CHECKING |
1149 | gcc_assert (possible_polymorphic_call_target_p | |
1150 | (callee, cgraph_get_node (fndecl))); | |
1151 | ||
1152 | #endif | |
e021c122 RG |
1153 | gimple_call_set_fndecl (stmt, fndecl); |
1154 | changed = true; | |
1155 | } | |
1156 | } | |
49c471e3 | 1157 | } |
e021c122 | 1158 | } |
49c471e3 | 1159 | |
e021c122 RG |
1160 | if (inplace) |
1161 | return changed; | |
1162 | ||
1163 | /* Check for builtins that CCP can handle using information not | |
1164 | available in the generic fold routines. */ | |
89faf322 | 1165 | callee = gimple_call_fndecl (stmt); |
e021c122 RG |
1166 | if (callee && DECL_BUILT_IN (callee)) |
1167 | { | |
1168 | tree result = gimple_fold_builtin (stmt); | |
f6dbed32 | 1169 | if (result) |
e021c122 RG |
1170 | { |
1171 | if (!update_call_from_tree (gsi, result)) | |
1172 | gimplify_and_update_call_from_tree (gsi, result); | |
1173 | changed = true; | |
1174 | } | |
ea679d55 JG |
1175 | else if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD) |
1176 | changed |= targetm.gimple_fold_builtin (gsi); | |
3b45a007 RG |
1177 | } |
1178 | ||
e021c122 | 1179 | return changed; |
cbdd87d4 RG |
1180 | } |
1181 | ||
1182 | /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument | |
1183 | distinguishes both cases. */ | |
1184 | ||
1185 | static bool | |
1186 | fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace) | |
1187 | { | |
1188 | bool changed = false; | |
1189 | gimple stmt = gsi_stmt (*gsi); | |
1190 | unsigned i; | |
1191 | ||
1192 | /* Fold the main computation performed by the statement. */ | |
1193 | switch (gimple_code (stmt)) | |
1194 | { | |
1195 | case GIMPLE_ASSIGN: | |
1196 | { | |
1197 | unsigned old_num_ops = gimple_num_ops (stmt); | |
5fbcc0ed | 1198 | enum tree_code subcode = gimple_assign_rhs_code (stmt); |
cbdd87d4 | 1199 | tree lhs = gimple_assign_lhs (stmt); |
5fbcc0ed RG |
1200 | tree new_rhs; |
1201 | /* First canonicalize operand order. This avoids building new | |
1202 | trees if this is the only thing fold would later do. */ | |
1203 | if ((commutative_tree_code (subcode) | |
1204 | || commutative_ternary_tree_code (subcode)) | |
1205 | && tree_swap_operands_p (gimple_assign_rhs1 (stmt), | |
1206 | gimple_assign_rhs2 (stmt), false)) | |
1207 | { | |
1208 | tree tem = gimple_assign_rhs1 (stmt); | |
1209 | gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt)); | |
1210 | gimple_assign_set_rhs2 (stmt, tem); | |
1211 | changed = true; | |
1212 | } | |
1213 | new_rhs = fold_gimple_assign (gsi); | |
cbdd87d4 RG |
1214 | if (new_rhs |
1215 | && !useless_type_conversion_p (TREE_TYPE (lhs), | |
1216 | TREE_TYPE (new_rhs))) | |
1217 | new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); | |
1218 | if (new_rhs | |
1219 | && (!inplace | |
1220 | || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) | |
1221 | { | |
1222 | gimple_assign_set_rhs_from_tree (gsi, new_rhs); | |
1223 | changed = true; | |
1224 | } | |
1225 | break; | |
1226 | } | |
1227 | ||
1228 | case GIMPLE_COND: | |
1229 | changed |= fold_gimple_cond (stmt); | |
1230 | break; | |
1231 | ||
1232 | case GIMPLE_CALL: | |
ceeffab0 | 1233 | changed |= gimple_fold_call (gsi, inplace); |
cbdd87d4 RG |
1234 | break; |
1235 | ||
1236 | case GIMPLE_ASM: | |
1237 | /* Fold *& in asm operands. */ | |
38384150 JJ |
1238 | { |
1239 | size_t noutputs; | |
1240 | const char **oconstraints; | |
1241 | const char *constraint; | |
1242 | bool allows_mem, allows_reg; | |
1243 | ||
1244 | noutputs = gimple_asm_noutputs (stmt); | |
1245 | oconstraints = XALLOCAVEC (const char *, noutputs); | |
1246 | ||
1247 | for (i = 0; i < gimple_asm_noutputs (stmt); ++i) | |
1248 | { | |
1249 | tree link = gimple_asm_output_op (stmt, i); | |
1250 | tree op = TREE_VALUE (link); | |
1251 | oconstraints[i] | |
1252 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
1253 | if (REFERENCE_CLASS_P (op) | |
1254 | && (op = maybe_fold_reference (op, true)) != NULL_TREE) | |
1255 | { | |
1256 | TREE_VALUE (link) = op; | |
1257 | changed = true; | |
1258 | } | |
1259 | } | |
1260 | for (i = 0; i < gimple_asm_ninputs (stmt); ++i) | |
1261 | { | |
1262 | tree link = gimple_asm_input_op (stmt, i); | |
1263 | tree op = TREE_VALUE (link); | |
1264 | constraint | |
1265 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
1266 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, | |
1267 | oconstraints, &allows_mem, &allows_reg); | |
1268 | if (REFERENCE_CLASS_P (op) | |
1269 | && (op = maybe_fold_reference (op, !allows_reg && allows_mem)) | |
1270 | != NULL_TREE) | |
1271 | { | |
1272 | TREE_VALUE (link) = op; | |
1273 | changed = true; | |
1274 | } | |
1275 | } | |
1276 | } | |
cbdd87d4 RG |
1277 | break; |
1278 | ||
bd422c4a RG |
1279 | case GIMPLE_DEBUG: |
1280 | if (gimple_debug_bind_p (stmt)) | |
1281 | { | |
1282 | tree val = gimple_debug_bind_get_value (stmt); | |
1283 | if (val | |
1284 | && REFERENCE_CLASS_P (val)) | |
1285 | { | |
1286 | tree tem = maybe_fold_reference (val, false); | |
1287 | if (tem) | |
1288 | { | |
1289 | gimple_debug_bind_set_value (stmt, tem); | |
1290 | changed = true; | |
1291 | } | |
1292 | } | |
3e888a5e RG |
1293 | else if (val |
1294 | && TREE_CODE (val) == ADDR_EXPR) | |
1295 | { | |
1296 | tree ref = TREE_OPERAND (val, 0); | |
1297 | tree tem = maybe_fold_reference (ref, false); | |
1298 | if (tem) | |
1299 | { | |
1300 | tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val)); | |
1301 | gimple_debug_bind_set_value (stmt, tem); | |
1302 | changed = true; | |
1303 | } | |
1304 | } | |
bd422c4a RG |
1305 | } |
1306 | break; | |
1307 | ||
cbdd87d4 RG |
1308 | default:; |
1309 | } | |
1310 | ||
1311 | stmt = gsi_stmt (*gsi); | |
1312 | ||
37376165 RB |
1313 | /* Fold *& on the lhs. */ |
1314 | if (gimple_has_lhs (stmt)) | |
cbdd87d4 RG |
1315 | { |
1316 | tree lhs = gimple_get_lhs (stmt); | |
1317 | if (lhs && REFERENCE_CLASS_P (lhs)) | |
1318 | { | |
1319 | tree new_lhs = maybe_fold_reference (lhs, true); | |
1320 | if (new_lhs) | |
1321 | { | |
1322 | gimple_set_lhs (stmt, new_lhs); | |
1323 | changed = true; | |
1324 | } | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | return changed; | |
1329 | } | |
1330 | ||
1331 | /* Fold the statement pointed to by GSI. In some cases, this function may | |
1332 | replace the whole statement with a new one. Returns true iff folding | |
1333 | makes any changes. | |
1334 | The statement pointed to by GSI should be in valid gimple form but may | |
1335 | be in unfolded state as resulting from for example constant propagation | |
1336 | which can produce *&x = 0. */ | |
1337 | ||
1338 | bool | |
1339 | fold_stmt (gimple_stmt_iterator *gsi) | |
1340 | { | |
1341 | return fold_stmt_1 (gsi, false); | |
1342 | } | |
1343 | ||
59401b92 | 1344 | /* Perform the minimal folding on statement *GSI. Only operations like |
cbdd87d4 RG |
1345 | *&x created by constant propagation are handled. The statement cannot |
1346 | be replaced with a new one. Return true if the statement was | |
1347 | changed, false otherwise. | |
59401b92 | 1348 | The statement *GSI should be in valid gimple form but may |
cbdd87d4 RG |
1349 | be in unfolded state as resulting from for example constant propagation |
1350 | which can produce *&x = 0. */ | |
1351 | ||
1352 | bool | |
59401b92 | 1353 | fold_stmt_inplace (gimple_stmt_iterator *gsi) |
cbdd87d4 | 1354 | { |
59401b92 RG |
1355 | gimple stmt = gsi_stmt (*gsi); |
1356 | bool changed = fold_stmt_1 (gsi, true); | |
1357 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
cbdd87d4 RG |
1358 | return changed; |
1359 | } | |
1360 | ||
e89065a1 SL |
1361 | /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE |
1362 | if EXPR is null or we don't know how. | |
1363 | If non-null, the result always has boolean type. */ | |
1364 | ||
1365 | static tree | |
1366 | canonicalize_bool (tree expr, bool invert) | |
1367 | { | |
1368 | if (!expr) | |
1369 | return NULL_TREE; | |
1370 | else if (invert) | |
1371 | { | |
1372 | if (integer_nonzerop (expr)) | |
1373 | return boolean_false_node; | |
1374 | else if (integer_zerop (expr)) | |
1375 | return boolean_true_node; | |
1376 | else if (TREE_CODE (expr) == SSA_NAME) | |
1377 | return fold_build2 (EQ_EXPR, boolean_type_node, expr, | |
1378 | build_int_cst (TREE_TYPE (expr), 0)); | |
1379 | else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison) | |
1380 | return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false), | |
1381 | boolean_type_node, | |
1382 | TREE_OPERAND (expr, 0), | |
1383 | TREE_OPERAND (expr, 1)); | |
1384 | else | |
1385 | return NULL_TREE; | |
1386 | } | |
1387 | else | |
1388 | { | |
1389 | if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
1390 | return expr; | |
1391 | if (integer_nonzerop (expr)) | |
1392 | return boolean_true_node; | |
1393 | else if (integer_zerop (expr)) | |
1394 | return boolean_false_node; | |
1395 | else if (TREE_CODE (expr) == SSA_NAME) | |
1396 | return fold_build2 (NE_EXPR, boolean_type_node, expr, | |
1397 | build_int_cst (TREE_TYPE (expr), 0)); | |
1398 | else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison) | |
1399 | return fold_build2 (TREE_CODE (expr), | |
1400 | boolean_type_node, | |
1401 | TREE_OPERAND (expr, 0), | |
1402 | TREE_OPERAND (expr, 1)); | |
1403 | else | |
1404 | return NULL_TREE; | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | /* Check to see if a boolean expression EXPR is logically equivalent to the | |
1409 | comparison (OP1 CODE OP2). Check for various identities involving | |
1410 | SSA_NAMEs. */ | |
1411 | ||
1412 | static bool | |
1413 | same_bool_comparison_p (const_tree expr, enum tree_code code, | |
1414 | const_tree op1, const_tree op2) | |
1415 | { | |
1416 | gimple s; | |
1417 | ||
1418 | /* The obvious case. */ | |
1419 | if (TREE_CODE (expr) == code | |
1420 | && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0) | |
1421 | && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0)) | |
1422 | return true; | |
1423 | ||
1424 | /* Check for comparing (name, name != 0) and the case where expr | |
1425 | is an SSA_NAME with a definition matching the comparison. */ | |
1426 | if (TREE_CODE (expr) == SSA_NAME | |
1427 | && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
1428 | { | |
1429 | if (operand_equal_p (expr, op1, 0)) | |
1430 | return ((code == NE_EXPR && integer_zerop (op2)) | |
1431 | || (code == EQ_EXPR && integer_nonzerop (op2))); | |
1432 | s = SSA_NAME_DEF_STMT (expr); | |
1433 | if (is_gimple_assign (s) | |
1434 | && gimple_assign_rhs_code (s) == code | |
1435 | && operand_equal_p (gimple_assign_rhs1 (s), op1, 0) | |
1436 | && operand_equal_p (gimple_assign_rhs2 (s), op2, 0)) | |
1437 | return true; | |
1438 | } | |
1439 | ||
1440 | /* If op1 is of the form (name != 0) or (name == 0), and the definition | |
1441 | of name is a comparison, recurse. */ | |
1442 | if (TREE_CODE (op1) == SSA_NAME | |
1443 | && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE) | |
1444 | { | |
1445 | s = SSA_NAME_DEF_STMT (op1); | |
1446 | if (is_gimple_assign (s) | |
1447 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison) | |
1448 | { | |
1449 | enum tree_code c = gimple_assign_rhs_code (s); | |
1450 | if ((c == NE_EXPR && integer_zerop (op2)) | |
1451 | || (c == EQ_EXPR && integer_nonzerop (op2))) | |
1452 | return same_bool_comparison_p (expr, c, | |
1453 | gimple_assign_rhs1 (s), | |
1454 | gimple_assign_rhs2 (s)); | |
1455 | if ((c == EQ_EXPR && integer_zerop (op2)) | |
1456 | || (c == NE_EXPR && integer_nonzerop (op2))) | |
1457 | return same_bool_comparison_p (expr, | |
1458 | invert_tree_comparison (c, false), | |
1459 | gimple_assign_rhs1 (s), | |
1460 | gimple_assign_rhs2 (s)); | |
1461 | } | |
1462 | } | |
1463 | return false; | |
1464 | } | |
1465 | ||
1466 | /* Check to see if two boolean expressions OP1 and OP2 are logically | |
1467 | equivalent. */ | |
1468 | ||
1469 | static bool | |
1470 | same_bool_result_p (const_tree op1, const_tree op2) | |
1471 | { | |
1472 | /* Simple cases first. */ | |
1473 | if (operand_equal_p (op1, op2, 0)) | |
1474 | return true; | |
1475 | ||
1476 | /* Check the cases where at least one of the operands is a comparison. | |
1477 | These are a bit smarter than operand_equal_p in that they apply some | |
1478 | identifies on SSA_NAMEs. */ | |
1479 | if (TREE_CODE_CLASS (TREE_CODE (op2)) == tcc_comparison | |
1480 | && same_bool_comparison_p (op1, TREE_CODE (op2), | |
1481 | TREE_OPERAND (op2, 0), | |
1482 | TREE_OPERAND (op2, 1))) | |
1483 | return true; | |
1484 | if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_comparison | |
1485 | && same_bool_comparison_p (op2, TREE_CODE (op1), | |
1486 | TREE_OPERAND (op1, 0), | |
1487 | TREE_OPERAND (op1, 1))) | |
1488 | return true; | |
1489 | ||
1490 | /* Default case. */ | |
1491 | return false; | |
1492 | } | |
1493 | ||
1494 | /* Forward declarations for some mutually recursive functions. */ | |
1495 | ||
1496 | static tree | |
1497 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
1498 | enum tree_code code2, tree op2a, tree op2b); | |
1499 | static tree | |
1500 | and_var_with_comparison (tree var, bool invert, | |
1501 | enum tree_code code2, tree op2a, tree op2b); | |
1502 | static tree | |
1503 | and_var_with_comparison_1 (gimple stmt, | |
1504 | enum tree_code code2, tree op2a, tree op2b); | |
1505 | static tree | |
1506 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
1507 | enum tree_code code2, tree op2a, tree op2b); | |
1508 | static tree | |
1509 | or_var_with_comparison (tree var, bool invert, | |
1510 | enum tree_code code2, tree op2a, tree op2b); | |
1511 | static tree | |
1512 | or_var_with_comparison_1 (gimple stmt, | |
1513 | enum tree_code code2, tree op2a, tree op2b); | |
1514 | ||
1515 | /* Helper function for and_comparisons_1: try to simplify the AND of the | |
1516 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
1517 | If INVERT is true, invert the value of the VAR before doing the AND. | |
1518 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
1519 | ||
1520 | static tree | |
1521 | and_var_with_comparison (tree var, bool invert, | |
1522 | enum tree_code code2, tree op2a, tree op2b) | |
1523 | { | |
1524 | tree t; | |
1525 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
1526 | ||
1527 | /* We can only deal with variables whose definitions are assignments. */ | |
1528 | if (!is_gimple_assign (stmt)) | |
1529 | return NULL_TREE; | |
1530 | ||
1531 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
1532 | !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b)) | |
1533 | Then we only have to consider the simpler non-inverted cases. */ | |
1534 | if (invert) | |
1535 | t = or_var_with_comparison_1 (stmt, | |
1536 | invert_tree_comparison (code2, false), | |
1537 | op2a, op2b); | |
1538 | else | |
1539 | t = and_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
1540 | return canonicalize_bool (t, invert); | |
1541 | } | |
1542 | ||
1543 | /* Try to simplify the AND of the ssa variable defined by the assignment | |
1544 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
1545 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
1546 | ||
1547 | static tree | |
1548 | and_var_with_comparison_1 (gimple stmt, | |
1549 | enum tree_code code2, tree op2a, tree op2b) | |
1550 | { | |
1551 | tree var = gimple_assign_lhs (stmt); | |
1552 | tree true_test_var = NULL_TREE; | |
1553 | tree false_test_var = NULL_TREE; | |
1554 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
1555 | ||
1556 | /* Check for identities like (var AND (var == 0)) => false. */ | |
1557 | if (TREE_CODE (op2a) == SSA_NAME | |
1558 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
1559 | { | |
1560 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
1561 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
1562 | { | |
1563 | true_test_var = op2a; | |
1564 | if (var == true_test_var) | |
1565 | return var; | |
1566 | } | |
1567 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
1568 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
1569 | { | |
1570 | false_test_var = op2a; | |
1571 | if (var == false_test_var) | |
1572 | return boolean_false_node; | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | /* If the definition is a comparison, recurse on it. */ | |
1577 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
1578 | { | |
1579 | tree t = and_comparisons_1 (innercode, | |
1580 | gimple_assign_rhs1 (stmt), | |
1581 | gimple_assign_rhs2 (stmt), | |
1582 | code2, | |
1583 | op2a, | |
1584 | op2b); | |
1585 | if (t) | |
1586 | return t; | |
1587 | } | |
1588 | ||
1589 | /* If the definition is an AND or OR expression, we may be able to | |
1590 | simplify by reassociating. */ | |
eb9820c0 KT |
1591 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
1592 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
e89065a1 SL |
1593 | { |
1594 | tree inner1 = gimple_assign_rhs1 (stmt); | |
1595 | tree inner2 = gimple_assign_rhs2 (stmt); | |
1596 | gimple s; | |
1597 | tree t; | |
1598 | tree partial = NULL_TREE; | |
eb9820c0 | 1599 | bool is_and = (innercode == BIT_AND_EXPR); |
e89065a1 SL |
1600 | |
1601 | /* Check for boolean identities that don't require recursive examination | |
1602 | of inner1/inner2: | |
1603 | inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var | |
1604 | inner1 AND (inner1 OR inner2) => inner1 | |
1605 | !inner1 AND (inner1 AND inner2) => false | |
1606 | !inner1 AND (inner1 OR inner2) => !inner1 AND inner2 | |
1607 | Likewise for similar cases involving inner2. */ | |
1608 | if (inner1 == true_test_var) | |
1609 | return (is_and ? var : inner1); | |
1610 | else if (inner2 == true_test_var) | |
1611 | return (is_and ? var : inner2); | |
1612 | else if (inner1 == false_test_var) | |
1613 | return (is_and | |
1614 | ? boolean_false_node | |
1615 | : and_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
1616 | else if (inner2 == false_test_var) | |
1617 | return (is_and | |
1618 | ? boolean_false_node | |
1619 | : and_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
1620 | ||
1621 | /* Next, redistribute/reassociate the AND across the inner tests. | |
1622 | Compute the first partial result, (inner1 AND (op2a code op2b)) */ | |
1623 | if (TREE_CODE (inner1) == SSA_NAME | |
1624 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
1625 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
1626 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
1627 | gimple_assign_rhs1 (s), | |
1628 | gimple_assign_rhs2 (s), | |
1629 | code2, op2a, op2b))) | |
1630 | { | |
1631 | /* Handle the AND case, where we are reassociating: | |
1632 | (inner1 AND inner2) AND (op2a code2 op2b) | |
1633 | => (t AND inner2) | |
1634 | If the partial result t is a constant, we win. Otherwise | |
1635 | continue on to try reassociating with the other inner test. */ | |
1636 | if (is_and) | |
1637 | { | |
1638 | if (integer_onep (t)) | |
1639 | return inner2; | |
1640 | else if (integer_zerop (t)) | |
1641 | return boolean_false_node; | |
1642 | } | |
1643 | ||
1644 | /* Handle the OR case, where we are redistributing: | |
1645 | (inner1 OR inner2) AND (op2a code2 op2b) | |
1646 | => (t OR (inner2 AND (op2a code2 op2b))) */ | |
8236c8eb JJ |
1647 | else if (integer_onep (t)) |
1648 | return boolean_true_node; | |
1649 | ||
1650 | /* Save partial result for later. */ | |
1651 | partial = t; | |
e89065a1 SL |
1652 | } |
1653 | ||
1654 | /* Compute the second partial result, (inner2 AND (op2a code op2b)) */ | |
1655 | if (TREE_CODE (inner2) == SSA_NAME | |
1656 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
1657 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
1658 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
1659 | gimple_assign_rhs1 (s), | |
1660 | gimple_assign_rhs2 (s), | |
1661 | code2, op2a, op2b))) | |
1662 | { | |
1663 | /* Handle the AND case, where we are reassociating: | |
1664 | (inner1 AND inner2) AND (op2a code2 op2b) | |
1665 | => (inner1 AND t) */ | |
1666 | if (is_and) | |
1667 | { | |
1668 | if (integer_onep (t)) | |
1669 | return inner1; | |
1670 | else if (integer_zerop (t)) | |
1671 | return boolean_false_node; | |
8236c8eb JJ |
1672 | /* If both are the same, we can apply the identity |
1673 | (x AND x) == x. */ | |
1674 | else if (partial && same_bool_result_p (t, partial)) | |
1675 | return t; | |
e89065a1 SL |
1676 | } |
1677 | ||
1678 | /* Handle the OR case. where we are redistributing: | |
1679 | (inner1 OR inner2) AND (op2a code2 op2b) | |
1680 | => (t OR (inner1 AND (op2a code2 op2b))) | |
1681 | => (t OR partial) */ | |
1682 | else | |
1683 | { | |
1684 | if (integer_onep (t)) | |
1685 | return boolean_true_node; | |
1686 | else if (partial) | |
1687 | { | |
1688 | /* We already got a simplification for the other | |
1689 | operand to the redistributed OR expression. The | |
1690 | interesting case is when at least one is false. | |
1691 | Or, if both are the same, we can apply the identity | |
1692 | (x OR x) == x. */ | |
1693 | if (integer_zerop (partial)) | |
1694 | return t; | |
1695 | else if (integer_zerop (t)) | |
1696 | return partial; | |
1697 | else if (same_bool_result_p (t, partial)) | |
1698 | return t; | |
1699 | } | |
1700 | } | |
1701 | } | |
1702 | } | |
1703 | return NULL_TREE; | |
1704 | } | |
1705 | ||
1706 | /* Try to simplify the AND of two comparisons defined by | |
1707 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
1708 | If this can be done without constructing an intermediate value, | |
1709 | return the resulting tree; otherwise NULL_TREE is returned. | |
1710 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
1711 | in the first comparison but not the second. */ | |
1712 | ||
1713 | static tree | |
1714 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
1715 | enum tree_code code2, tree op2a, tree op2b) | |
1716 | { | |
ae22ac3c | 1717 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
31ed6226 | 1718 | |
e89065a1 SL |
1719 | /* First check for ((x CODE1 y) AND (x CODE2 y)). */ |
1720 | if (operand_equal_p (op1a, op2a, 0) | |
1721 | && operand_equal_p (op1b, op2b, 0)) | |
1722 | { | |
eb9820c0 | 1723 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
1724 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
1725 | TRUTH_ANDIF_EXPR, code1, code2, | |
31ed6226 | 1726 | truth_type, op1a, op1b); |
e89065a1 SL |
1727 | if (t) |
1728 | return t; | |
1729 | } | |
1730 | ||
1731 | /* Likewise the swapped case of the above. */ | |
1732 | if (operand_equal_p (op1a, op2b, 0) | |
1733 | && operand_equal_p (op1b, op2a, 0)) | |
1734 | { | |
eb9820c0 | 1735 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
1736 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
1737 | TRUTH_ANDIF_EXPR, code1, | |
1738 | swap_tree_comparison (code2), | |
31ed6226 | 1739 | truth_type, op1a, op1b); |
e89065a1 SL |
1740 | if (t) |
1741 | return t; | |
1742 | } | |
1743 | ||
1744 | /* If both comparisons are of the same value against constants, we might | |
1745 | be able to merge them. */ | |
1746 | if (operand_equal_p (op1a, op2a, 0) | |
1747 | && TREE_CODE (op1b) == INTEGER_CST | |
1748 | && TREE_CODE (op2b) == INTEGER_CST) | |
1749 | { | |
1750 | int cmp = tree_int_cst_compare (op1b, op2b); | |
1751 | ||
1752 | /* If we have (op1a == op1b), we should either be able to | |
1753 | return that or FALSE, depending on whether the constant op1b | |
1754 | also satisfies the other comparison against op2b. */ | |
1755 | if (code1 == EQ_EXPR) | |
1756 | { | |
1757 | bool done = true; | |
1758 | bool val; | |
1759 | switch (code2) | |
1760 | { | |
1761 | case EQ_EXPR: val = (cmp == 0); break; | |
1762 | case NE_EXPR: val = (cmp != 0); break; | |
1763 | case LT_EXPR: val = (cmp < 0); break; | |
1764 | case GT_EXPR: val = (cmp > 0); break; | |
1765 | case LE_EXPR: val = (cmp <= 0); break; | |
1766 | case GE_EXPR: val = (cmp >= 0); break; | |
1767 | default: done = false; | |
1768 | } | |
1769 | if (done) | |
1770 | { | |
1771 | if (val) | |
1772 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1773 | else | |
1774 | return boolean_false_node; | |
1775 | } | |
1776 | } | |
1777 | /* Likewise if the second comparison is an == comparison. */ | |
1778 | else if (code2 == EQ_EXPR) | |
1779 | { | |
1780 | bool done = true; | |
1781 | bool val; | |
1782 | switch (code1) | |
1783 | { | |
1784 | case EQ_EXPR: val = (cmp == 0); break; | |
1785 | case NE_EXPR: val = (cmp != 0); break; | |
1786 | case LT_EXPR: val = (cmp > 0); break; | |
1787 | case GT_EXPR: val = (cmp < 0); break; | |
1788 | case LE_EXPR: val = (cmp >= 0); break; | |
1789 | case GE_EXPR: val = (cmp <= 0); break; | |
1790 | default: done = false; | |
1791 | } | |
1792 | if (done) | |
1793 | { | |
1794 | if (val) | |
1795 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1796 | else | |
1797 | return boolean_false_node; | |
1798 | } | |
1799 | } | |
1800 | ||
1801 | /* Same business with inequality tests. */ | |
1802 | else if (code1 == NE_EXPR) | |
1803 | { | |
1804 | bool val; | |
1805 | switch (code2) | |
1806 | { | |
1807 | case EQ_EXPR: val = (cmp != 0); break; | |
1808 | case NE_EXPR: val = (cmp == 0); break; | |
1809 | case LT_EXPR: val = (cmp >= 0); break; | |
1810 | case GT_EXPR: val = (cmp <= 0); break; | |
1811 | case LE_EXPR: val = (cmp > 0); break; | |
1812 | case GE_EXPR: val = (cmp < 0); break; | |
1813 | default: | |
1814 | val = false; | |
1815 | } | |
1816 | if (val) | |
1817 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1818 | } | |
1819 | else if (code2 == NE_EXPR) | |
1820 | { | |
1821 | bool val; | |
1822 | switch (code1) | |
1823 | { | |
1824 | case EQ_EXPR: val = (cmp == 0); break; | |
1825 | case NE_EXPR: val = (cmp != 0); break; | |
1826 | case LT_EXPR: val = (cmp <= 0); break; | |
1827 | case GT_EXPR: val = (cmp >= 0); break; | |
1828 | case LE_EXPR: val = (cmp < 0); break; | |
1829 | case GE_EXPR: val = (cmp > 0); break; | |
1830 | default: | |
1831 | val = false; | |
1832 | } | |
1833 | if (val) | |
1834 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1835 | } | |
1836 | ||
1837 | /* Chose the more restrictive of two < or <= comparisons. */ | |
1838 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
1839 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
1840 | { | |
1841 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
1842 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1843 | else | |
1844 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1845 | } | |
1846 | ||
1847 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
1848 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
1849 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
1850 | { | |
1851 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
1852 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1853 | else | |
1854 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1855 | } | |
1856 | ||
1857 | /* Check for singleton ranges. */ | |
1858 | else if (cmp == 0 | |
1859 | && ((code1 == LE_EXPR && code2 == GE_EXPR) | |
1860 | || (code1 == GE_EXPR && code2 == LE_EXPR))) | |
1861 | return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b); | |
1862 | ||
1863 | /* Check for disjoint ranges. */ | |
1864 | else if (cmp <= 0 | |
1865 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
1866 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
1867 | return boolean_false_node; | |
1868 | else if (cmp >= 0 | |
1869 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
1870 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
1871 | return boolean_false_node; | |
1872 | } | |
1873 | ||
1874 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
1875 | NAME's definition is a truth value. See if there are any simplifications | |
1876 | that can be done against the NAME's definition. */ | |
1877 | if (TREE_CODE (op1a) == SSA_NAME | |
1878 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
1879 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
1880 | { | |
1881 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
1882 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
1883 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
1884 | switch (gimple_code (stmt)) | |
1885 | { | |
1886 | case GIMPLE_ASSIGN: | |
1887 | /* Try to simplify by copy-propagating the definition. */ | |
1888 | return and_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
1889 | ||
1890 | case GIMPLE_PHI: | |
1891 | /* If every argument to the PHI produces the same result when | |
1892 | ANDed with the second comparison, we win. | |
1893 | Do not do this unless the type is bool since we need a bool | |
1894 | result here anyway. */ | |
1895 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
1896 | { | |
1897 | tree result = NULL_TREE; | |
1898 | unsigned i; | |
1899 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
1900 | { | |
1901 | tree arg = gimple_phi_arg_def (stmt, i); | |
1902 | ||
1903 | /* If this PHI has itself as an argument, ignore it. | |
1904 | If all the other args produce the same result, | |
1905 | we're still OK. */ | |
1906 | if (arg == gimple_phi_result (stmt)) | |
1907 | continue; | |
1908 | else if (TREE_CODE (arg) == INTEGER_CST) | |
1909 | { | |
1910 | if (invert ? integer_nonzerop (arg) : integer_zerop (arg)) | |
1911 | { | |
1912 | if (!result) | |
1913 | result = boolean_false_node; | |
1914 | else if (!integer_zerop (result)) | |
1915 | return NULL_TREE; | |
1916 | } | |
1917 | else if (!result) | |
1918 | result = fold_build2 (code2, boolean_type_node, | |
1919 | op2a, op2b); | |
1920 | else if (!same_bool_comparison_p (result, | |
1921 | code2, op2a, op2b)) | |
1922 | return NULL_TREE; | |
1923 | } | |
0e8b84ec JJ |
1924 | else if (TREE_CODE (arg) == SSA_NAME |
1925 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
e89065a1 | 1926 | { |
6c66f733 JJ |
1927 | tree temp; |
1928 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
1929 | /* In simple cases we can look through PHI nodes, | |
1930 | but we have to be careful with loops. | |
1931 | See PR49073. */ | |
1932 | if (! dom_info_available_p (CDI_DOMINATORS) | |
1933 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
1934 | || dominated_by_p (CDI_DOMINATORS, | |
1935 | gimple_bb (def_stmt), | |
1936 | gimple_bb (stmt))) | |
1937 | return NULL_TREE; | |
1938 | temp = and_var_with_comparison (arg, invert, code2, | |
1939 | op2a, op2b); | |
e89065a1 SL |
1940 | if (!temp) |
1941 | return NULL_TREE; | |
1942 | else if (!result) | |
1943 | result = temp; | |
1944 | else if (!same_bool_result_p (result, temp)) | |
1945 | return NULL_TREE; | |
1946 | } | |
1947 | else | |
1948 | return NULL_TREE; | |
1949 | } | |
1950 | return result; | |
1951 | } | |
1952 | ||
1953 | default: | |
1954 | break; | |
1955 | } | |
1956 | } | |
1957 | return NULL_TREE; | |
1958 | } | |
1959 | ||
1960 | /* Try to simplify the AND of two comparisons, specified by | |
1961 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
1962 | If this can be simplified to a single expression (without requiring | |
1963 | introducing more SSA variables to hold intermediate values), | |
1964 | return the resulting tree. Otherwise return NULL_TREE. | |
1965 | If the result expression is non-null, it has boolean type. */ | |
1966 | ||
1967 | tree | |
1968 | maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
1969 | enum tree_code code2, tree op2a, tree op2b) | |
1970 | { | |
1971 | tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
1972 | if (t) | |
1973 | return t; | |
1974 | else | |
1975 | return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
1976 | } | |
1977 | ||
1978 | /* Helper function for or_comparisons_1: try to simplify the OR of the | |
1979 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
1980 | If INVERT is true, invert the value of VAR before doing the OR. | |
1981 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
1982 | ||
1983 | static tree | |
1984 | or_var_with_comparison (tree var, bool invert, | |
1985 | enum tree_code code2, tree op2a, tree op2b) | |
1986 | { | |
1987 | tree t; | |
1988 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
1989 | ||
1990 | /* We can only deal with variables whose definitions are assignments. */ | |
1991 | if (!is_gimple_assign (stmt)) | |
1992 | return NULL_TREE; | |
1993 | ||
1994 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
1995 | !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b)) | |
1996 | Then we only have to consider the simpler non-inverted cases. */ | |
1997 | if (invert) | |
1998 | t = and_var_with_comparison_1 (stmt, | |
1999 | invert_tree_comparison (code2, false), | |
2000 | op2a, op2b); | |
2001 | else | |
2002 | t = or_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
2003 | return canonicalize_bool (t, invert); | |
2004 | } | |
2005 | ||
2006 | /* Try to simplify the OR of the ssa variable defined by the assignment | |
2007 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
2008 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
2009 | ||
2010 | static tree | |
2011 | or_var_with_comparison_1 (gimple stmt, | |
2012 | enum tree_code code2, tree op2a, tree op2b) | |
2013 | { | |
2014 | tree var = gimple_assign_lhs (stmt); | |
2015 | tree true_test_var = NULL_TREE; | |
2016 | tree false_test_var = NULL_TREE; | |
2017 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
2018 | ||
2019 | /* Check for identities like (var OR (var != 0)) => true . */ | |
2020 | if (TREE_CODE (op2a) == SSA_NAME | |
2021 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
2022 | { | |
2023 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
2024 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
2025 | { | |
2026 | true_test_var = op2a; | |
2027 | if (var == true_test_var) | |
2028 | return var; | |
2029 | } | |
2030 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
2031 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
2032 | { | |
2033 | false_test_var = op2a; | |
2034 | if (var == false_test_var) | |
2035 | return boolean_true_node; | |
2036 | } | |
2037 | } | |
2038 | ||
2039 | /* If the definition is a comparison, recurse on it. */ | |
2040 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
2041 | { | |
2042 | tree t = or_comparisons_1 (innercode, | |
2043 | gimple_assign_rhs1 (stmt), | |
2044 | gimple_assign_rhs2 (stmt), | |
2045 | code2, | |
2046 | op2a, | |
2047 | op2b); | |
2048 | if (t) | |
2049 | return t; | |
2050 | } | |
2051 | ||
2052 | /* If the definition is an AND or OR expression, we may be able to | |
2053 | simplify by reassociating. */ | |
eb9820c0 KT |
2054 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
2055 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
e89065a1 SL |
2056 | { |
2057 | tree inner1 = gimple_assign_rhs1 (stmt); | |
2058 | tree inner2 = gimple_assign_rhs2 (stmt); | |
2059 | gimple s; | |
2060 | tree t; | |
2061 | tree partial = NULL_TREE; | |
eb9820c0 | 2062 | bool is_or = (innercode == BIT_IOR_EXPR); |
e89065a1 SL |
2063 | |
2064 | /* Check for boolean identities that don't require recursive examination | |
2065 | of inner1/inner2: | |
2066 | inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var | |
2067 | inner1 OR (inner1 AND inner2) => inner1 | |
2068 | !inner1 OR (inner1 OR inner2) => true | |
2069 | !inner1 OR (inner1 AND inner2) => !inner1 OR inner2 | |
2070 | */ | |
2071 | if (inner1 == true_test_var) | |
2072 | return (is_or ? var : inner1); | |
2073 | else if (inner2 == true_test_var) | |
2074 | return (is_or ? var : inner2); | |
2075 | else if (inner1 == false_test_var) | |
2076 | return (is_or | |
2077 | ? boolean_true_node | |
2078 | : or_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
2079 | else if (inner2 == false_test_var) | |
2080 | return (is_or | |
2081 | ? boolean_true_node | |
2082 | : or_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
2083 | ||
2084 | /* Next, redistribute/reassociate the OR across the inner tests. | |
2085 | Compute the first partial result, (inner1 OR (op2a code op2b)) */ | |
2086 | if (TREE_CODE (inner1) == SSA_NAME | |
2087 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
2088 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
2089 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
2090 | gimple_assign_rhs1 (s), | |
2091 | gimple_assign_rhs2 (s), | |
2092 | code2, op2a, op2b))) | |
2093 | { | |
2094 | /* Handle the OR case, where we are reassociating: | |
2095 | (inner1 OR inner2) OR (op2a code2 op2b) | |
2096 | => (t OR inner2) | |
2097 | If the partial result t is a constant, we win. Otherwise | |
2098 | continue on to try reassociating with the other inner test. */ | |
8236c8eb | 2099 | if (is_or) |
e89065a1 SL |
2100 | { |
2101 | if (integer_onep (t)) | |
2102 | return boolean_true_node; | |
2103 | else if (integer_zerop (t)) | |
2104 | return inner2; | |
2105 | } | |
2106 | ||
2107 | /* Handle the AND case, where we are redistributing: | |
2108 | (inner1 AND inner2) OR (op2a code2 op2b) | |
2109 | => (t AND (inner2 OR (op2a code op2b))) */ | |
8236c8eb JJ |
2110 | else if (integer_zerop (t)) |
2111 | return boolean_false_node; | |
2112 | ||
2113 | /* Save partial result for later. */ | |
2114 | partial = t; | |
e89065a1 SL |
2115 | } |
2116 | ||
2117 | /* Compute the second partial result, (inner2 OR (op2a code op2b)) */ | |
2118 | if (TREE_CODE (inner2) == SSA_NAME | |
2119 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
2120 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
2121 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
2122 | gimple_assign_rhs1 (s), | |
2123 | gimple_assign_rhs2 (s), | |
2124 | code2, op2a, op2b))) | |
2125 | { | |
2126 | /* Handle the OR case, where we are reassociating: | |
2127 | (inner1 OR inner2) OR (op2a code2 op2b) | |
8236c8eb JJ |
2128 | => (inner1 OR t) |
2129 | => (t OR partial) */ | |
2130 | if (is_or) | |
e89065a1 SL |
2131 | { |
2132 | if (integer_zerop (t)) | |
2133 | return inner1; | |
2134 | else if (integer_onep (t)) | |
2135 | return boolean_true_node; | |
8236c8eb JJ |
2136 | /* If both are the same, we can apply the identity |
2137 | (x OR x) == x. */ | |
2138 | else if (partial && same_bool_result_p (t, partial)) | |
2139 | return t; | |
e89065a1 SL |
2140 | } |
2141 | ||
2142 | /* Handle the AND case, where we are redistributing: | |
2143 | (inner1 AND inner2) OR (op2a code2 op2b) | |
2144 | => (t AND (inner1 OR (op2a code2 op2b))) | |
2145 | => (t AND partial) */ | |
2146 | else | |
2147 | { | |
2148 | if (integer_zerop (t)) | |
2149 | return boolean_false_node; | |
2150 | else if (partial) | |
2151 | { | |
2152 | /* We already got a simplification for the other | |
2153 | operand to the redistributed AND expression. The | |
2154 | interesting case is when at least one is true. | |
2155 | Or, if both are the same, we can apply the identity | |
8236c8eb | 2156 | (x AND x) == x. */ |
e89065a1 SL |
2157 | if (integer_onep (partial)) |
2158 | return t; | |
2159 | else if (integer_onep (t)) | |
2160 | return partial; | |
2161 | else if (same_bool_result_p (t, partial)) | |
8236c8eb | 2162 | return t; |
e89065a1 SL |
2163 | } |
2164 | } | |
2165 | } | |
2166 | } | |
2167 | return NULL_TREE; | |
2168 | } | |
2169 | ||
2170 | /* Try to simplify the OR of two comparisons defined by | |
2171 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
2172 | If this can be done without constructing an intermediate value, | |
2173 | return the resulting tree; otherwise NULL_TREE is returned. | |
2174 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
2175 | in the first comparison but not the second. */ | |
2176 | ||
2177 | static tree | |
2178 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
2179 | enum tree_code code2, tree op2a, tree op2b) | |
2180 | { | |
ae22ac3c | 2181 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
31ed6226 | 2182 | |
e89065a1 SL |
2183 | /* First check for ((x CODE1 y) OR (x CODE2 y)). */ |
2184 | if (operand_equal_p (op1a, op2a, 0) | |
2185 | && operand_equal_p (op1b, op2b, 0)) | |
2186 | { | |
eb9820c0 | 2187 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
2188 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
2189 | TRUTH_ORIF_EXPR, code1, code2, | |
31ed6226 | 2190 | truth_type, op1a, op1b); |
e89065a1 SL |
2191 | if (t) |
2192 | return t; | |
2193 | } | |
2194 | ||
2195 | /* Likewise the swapped case of the above. */ | |
2196 | if (operand_equal_p (op1a, op2b, 0) | |
2197 | && operand_equal_p (op1b, op2a, 0)) | |
2198 | { | |
eb9820c0 | 2199 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
2200 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
2201 | TRUTH_ORIF_EXPR, code1, | |
2202 | swap_tree_comparison (code2), | |
31ed6226 | 2203 | truth_type, op1a, op1b); |
e89065a1 SL |
2204 | if (t) |
2205 | return t; | |
2206 | } | |
2207 | ||
2208 | /* If both comparisons are of the same value against constants, we might | |
2209 | be able to merge them. */ | |
2210 | if (operand_equal_p (op1a, op2a, 0) | |
2211 | && TREE_CODE (op1b) == INTEGER_CST | |
2212 | && TREE_CODE (op2b) == INTEGER_CST) | |
2213 | { | |
2214 | int cmp = tree_int_cst_compare (op1b, op2b); | |
2215 | ||
2216 | /* If we have (op1a != op1b), we should either be able to | |
2217 | return that or TRUE, depending on whether the constant op1b | |
2218 | also satisfies the other comparison against op2b. */ | |
2219 | if (code1 == NE_EXPR) | |
2220 | { | |
2221 | bool done = true; | |
2222 | bool val; | |
2223 | switch (code2) | |
2224 | { | |
2225 | case EQ_EXPR: val = (cmp == 0); break; | |
2226 | case NE_EXPR: val = (cmp != 0); break; | |
2227 | case LT_EXPR: val = (cmp < 0); break; | |
2228 | case GT_EXPR: val = (cmp > 0); break; | |
2229 | case LE_EXPR: val = (cmp <= 0); break; | |
2230 | case GE_EXPR: val = (cmp >= 0); break; | |
2231 | default: done = false; | |
2232 | } | |
2233 | if (done) | |
2234 | { | |
2235 | if (val) | |
2236 | return boolean_true_node; | |
2237 | else | |
2238 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2239 | } | |
2240 | } | |
2241 | /* Likewise if the second comparison is a != comparison. */ | |
2242 | else if (code2 == NE_EXPR) | |
2243 | { | |
2244 | bool done = true; | |
2245 | bool val; | |
2246 | switch (code1) | |
2247 | { | |
2248 | case EQ_EXPR: val = (cmp == 0); break; | |
2249 | case NE_EXPR: val = (cmp != 0); break; | |
2250 | case LT_EXPR: val = (cmp > 0); break; | |
2251 | case GT_EXPR: val = (cmp < 0); break; | |
2252 | case LE_EXPR: val = (cmp >= 0); break; | |
2253 | case GE_EXPR: val = (cmp <= 0); break; | |
2254 | default: done = false; | |
2255 | } | |
2256 | if (done) | |
2257 | { | |
2258 | if (val) | |
2259 | return boolean_true_node; | |
2260 | else | |
2261 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2262 | } | |
2263 | } | |
2264 | ||
2265 | /* See if an equality test is redundant with the other comparison. */ | |
2266 | else if (code1 == EQ_EXPR) | |
2267 | { | |
2268 | bool val; | |
2269 | switch (code2) | |
2270 | { | |
2271 | case EQ_EXPR: val = (cmp == 0); break; | |
2272 | case NE_EXPR: val = (cmp != 0); break; | |
2273 | case LT_EXPR: val = (cmp < 0); break; | |
2274 | case GT_EXPR: val = (cmp > 0); break; | |
2275 | case LE_EXPR: val = (cmp <= 0); break; | |
2276 | case GE_EXPR: val = (cmp >= 0); break; | |
2277 | default: | |
2278 | val = false; | |
2279 | } | |
2280 | if (val) | |
2281 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2282 | } | |
2283 | else if (code2 == EQ_EXPR) | |
2284 | { | |
2285 | bool val; | |
2286 | switch (code1) | |
2287 | { | |
2288 | case EQ_EXPR: val = (cmp == 0); break; | |
2289 | case NE_EXPR: val = (cmp != 0); break; | |
2290 | case LT_EXPR: val = (cmp > 0); break; | |
2291 | case GT_EXPR: val = (cmp < 0); break; | |
2292 | case LE_EXPR: val = (cmp >= 0); break; | |
2293 | case GE_EXPR: val = (cmp <= 0); break; | |
2294 | default: | |
2295 | val = false; | |
2296 | } | |
2297 | if (val) | |
2298 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2299 | } | |
2300 | ||
2301 | /* Chose the less restrictive of two < or <= comparisons. */ | |
2302 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
2303 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2304 | { | |
2305 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
2306 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2307 | else | |
2308 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2309 | } | |
2310 | ||
2311 | /* Likewise chose the less restrictive of two > or >= comparisons. */ | |
2312 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
2313 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2314 | { | |
2315 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
2316 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2317 | else | |
2318 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2319 | } | |
2320 | ||
2321 | /* Check for singleton ranges. */ | |
2322 | else if (cmp == 0 | |
2323 | && ((code1 == LT_EXPR && code2 == GT_EXPR) | |
2324 | || (code1 == GT_EXPR && code2 == LT_EXPR))) | |
2325 | return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b); | |
2326 | ||
2327 | /* Check for less/greater pairs that don't restrict the range at all. */ | |
2328 | else if (cmp >= 0 | |
2329 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
2330 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2331 | return boolean_true_node; | |
2332 | else if (cmp <= 0 | |
2333 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
2334 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2335 | return boolean_true_node; | |
2336 | } | |
2337 | ||
2338 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
2339 | NAME's definition is a truth value. See if there are any simplifications | |
2340 | that can be done against the NAME's definition. */ | |
2341 | if (TREE_CODE (op1a) == SSA_NAME | |
2342 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
2343 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
2344 | { | |
2345 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
2346 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
2347 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
2348 | switch (gimple_code (stmt)) | |
2349 | { | |
2350 | case GIMPLE_ASSIGN: | |
2351 | /* Try to simplify by copy-propagating the definition. */ | |
2352 | return or_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
2353 | ||
2354 | case GIMPLE_PHI: | |
2355 | /* If every argument to the PHI produces the same result when | |
2356 | ORed with the second comparison, we win. | |
2357 | Do not do this unless the type is bool since we need a bool | |
2358 | result here anyway. */ | |
2359 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
2360 | { | |
2361 | tree result = NULL_TREE; | |
2362 | unsigned i; | |
2363 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
2364 | { | |
2365 | tree arg = gimple_phi_arg_def (stmt, i); | |
2366 | ||
2367 | /* If this PHI has itself as an argument, ignore it. | |
2368 | If all the other args produce the same result, | |
2369 | we're still OK. */ | |
2370 | if (arg == gimple_phi_result (stmt)) | |
2371 | continue; | |
2372 | else if (TREE_CODE (arg) == INTEGER_CST) | |
2373 | { | |
2374 | if (invert ? integer_zerop (arg) : integer_nonzerop (arg)) | |
2375 | { | |
2376 | if (!result) | |
2377 | result = boolean_true_node; | |
2378 | else if (!integer_onep (result)) | |
2379 | return NULL_TREE; | |
2380 | } | |
2381 | else if (!result) | |
2382 | result = fold_build2 (code2, boolean_type_node, | |
2383 | op2a, op2b); | |
2384 | else if (!same_bool_comparison_p (result, | |
2385 | code2, op2a, op2b)) | |
2386 | return NULL_TREE; | |
2387 | } | |
0e8b84ec JJ |
2388 | else if (TREE_CODE (arg) == SSA_NAME |
2389 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
e89065a1 | 2390 | { |
6c66f733 JJ |
2391 | tree temp; |
2392 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
2393 | /* In simple cases we can look through PHI nodes, | |
2394 | but we have to be careful with loops. | |
2395 | See PR49073. */ | |
2396 | if (! dom_info_available_p (CDI_DOMINATORS) | |
2397 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
2398 | || dominated_by_p (CDI_DOMINATORS, | |
2399 | gimple_bb (def_stmt), | |
2400 | gimple_bb (stmt))) | |
2401 | return NULL_TREE; | |
2402 | temp = or_var_with_comparison (arg, invert, code2, | |
2403 | op2a, op2b); | |
e89065a1 SL |
2404 | if (!temp) |
2405 | return NULL_TREE; | |
2406 | else if (!result) | |
2407 | result = temp; | |
2408 | else if (!same_bool_result_p (result, temp)) | |
2409 | return NULL_TREE; | |
2410 | } | |
2411 | else | |
2412 | return NULL_TREE; | |
2413 | } | |
2414 | return result; | |
2415 | } | |
2416 | ||
2417 | default: | |
2418 | break; | |
2419 | } | |
2420 | } | |
2421 | return NULL_TREE; | |
2422 | } | |
2423 | ||
2424 | /* Try to simplify the OR of two comparisons, specified by | |
2425 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
2426 | If this can be simplified to a single expression (without requiring | |
2427 | introducing more SSA variables to hold intermediate values), | |
2428 | return the resulting tree. Otherwise return NULL_TREE. | |
2429 | If the result expression is non-null, it has boolean type. */ | |
2430 | ||
2431 | tree | |
2432 | maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
2433 | enum tree_code code2, tree op2a, tree op2b) | |
2434 | { | |
2435 | tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
2436 | if (t) | |
2437 | return t; | |
2438 | else | |
2439 | return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
2440 | } | |
cfef45c8 RG |
2441 | |
2442 | ||
2443 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
2444 | ||
2445 | Either NULL_TREE, a simplified but non-constant or a constant | |
2446 | is returned. | |
2447 | ||
2448 | ??? This should go into a gimple-fold-inline.h file to be eventually | |
2449 | privatized with the single valueize function used in the various TUs | |
2450 | to avoid the indirect function call overhead. */ | |
2451 | ||
2452 | tree | |
2453 | gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree)) | |
2454 | { | |
2455 | location_t loc = gimple_location (stmt); | |
2456 | switch (gimple_code (stmt)) | |
2457 | { | |
2458 | case GIMPLE_ASSIGN: | |
2459 | { | |
2460 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
2461 | ||
2462 | switch (get_gimple_rhs_class (subcode)) | |
2463 | { | |
2464 | case GIMPLE_SINGLE_RHS: | |
2465 | { | |
2466 | tree rhs = gimple_assign_rhs1 (stmt); | |
2467 | enum tree_code_class kind = TREE_CODE_CLASS (subcode); | |
2468 | ||
2469 | if (TREE_CODE (rhs) == SSA_NAME) | |
2470 | { | |
2471 | /* If the RHS is an SSA_NAME, return its known constant value, | |
2472 | if any. */ | |
2473 | return (*valueize) (rhs); | |
2474 | } | |
2475 | /* Handle propagating invariant addresses into address | |
2476 | operations. */ | |
2477 | else if (TREE_CODE (rhs) == ADDR_EXPR | |
2478 | && !is_gimple_min_invariant (rhs)) | |
2479 | { | |
d25c4172 | 2480 | HOST_WIDE_INT offset = 0; |
cfef45c8 RG |
2481 | tree base; |
2482 | base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0), | |
2483 | &offset, | |
2484 | valueize); | |
2485 | if (base | |
2486 | && (CONSTANT_CLASS_P (base) | |
2487 | || decl_address_invariant_p (base))) | |
2488 | return build_invariant_address (TREE_TYPE (rhs), | |
2489 | base, offset); | |
2490 | } | |
2491 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
2492 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
2493 | && (CONSTRUCTOR_NELTS (rhs) | |
2494 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
2495 | { | |
2496 | unsigned i; | |
d2a12ae7 | 2497 | tree val, *vec; |
cfef45c8 | 2498 | |
d2a12ae7 RG |
2499 | vec = XALLOCAVEC (tree, |
2500 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))); | |
cfef45c8 RG |
2501 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) |
2502 | { | |
2503 | val = (*valueize) (val); | |
2504 | if (TREE_CODE (val) == INTEGER_CST | |
2505 | || TREE_CODE (val) == REAL_CST | |
2506 | || TREE_CODE (val) == FIXED_CST) | |
d2a12ae7 | 2507 | vec[i] = val; |
cfef45c8 RG |
2508 | else |
2509 | return NULL_TREE; | |
2510 | } | |
2511 | ||
d2a12ae7 | 2512 | return build_vector (TREE_TYPE (rhs), vec); |
cfef45c8 RG |
2513 | } |
2514 | ||
2515 | if (kind == tcc_reference) | |
2516 | { | |
2517 | if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR | |
2518 | || TREE_CODE (rhs) == REALPART_EXPR | |
2519 | || TREE_CODE (rhs) == IMAGPART_EXPR) | |
2520 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
2521 | { | |
2522 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
2523 | return fold_unary_loc (EXPR_LOCATION (rhs), | |
2524 | TREE_CODE (rhs), | |
2525 | TREE_TYPE (rhs), val); | |
2526 | } | |
2527 | else if (TREE_CODE (rhs) == BIT_FIELD_REF | |
2528 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
2529 | { | |
2530 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
2531 | return fold_ternary_loc (EXPR_LOCATION (rhs), | |
2532 | TREE_CODE (rhs), | |
2533 | TREE_TYPE (rhs), val, | |
2534 | TREE_OPERAND (rhs, 1), | |
2535 | TREE_OPERAND (rhs, 2)); | |
2536 | } | |
2537 | else if (TREE_CODE (rhs) == MEM_REF | |
2538 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
2539 | { | |
2540 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
2541 | if (TREE_CODE (val) == ADDR_EXPR | |
2542 | && is_gimple_min_invariant (val)) | |
2543 | { | |
2544 | tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs), | |
2545 | unshare_expr (val), | |
2546 | TREE_OPERAND (rhs, 1)); | |
2547 | if (tem) | |
2548 | rhs = tem; | |
2549 | } | |
2550 | } | |
2551 | return fold_const_aggregate_ref_1 (rhs, valueize); | |
2552 | } | |
2553 | else if (kind == tcc_declaration) | |
2554 | return get_symbol_constant_value (rhs); | |
2555 | return rhs; | |
2556 | } | |
2557 | ||
2558 | case GIMPLE_UNARY_RHS: | |
2559 | { | |
2560 | /* Handle unary operators that can appear in GIMPLE form. | |
2561 | Note that we know the single operand must be a constant, | |
2562 | so this should almost always return a simplified RHS. */ | |
315f5f1b | 2563 | tree lhs = gimple_assign_lhs (stmt); |
cfef45c8 RG |
2564 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); |
2565 | ||
2566 | /* Conversions are useless for CCP purposes if they are | |
2567 | value-preserving. Thus the restrictions that | |
315f5f1b RG |
2568 | useless_type_conversion_p places for restrict qualification |
2569 | of pointer types should not apply here. | |
2570 | Substitution later will only substitute to allowed places. */ | |
cfef45c8 RG |
2571 | if (CONVERT_EXPR_CODE_P (subcode) |
2572 | && POINTER_TYPE_P (TREE_TYPE (lhs)) | |
315f5f1b | 2573 | && POINTER_TYPE_P (TREE_TYPE (op0)) |
8f420307 EB |
2574 | && TYPE_ADDR_SPACE (TREE_TYPE (lhs)) |
2575 | == TYPE_ADDR_SPACE (TREE_TYPE (op0)) | |
2576 | && TYPE_MODE (TREE_TYPE (lhs)) | |
2577 | == TYPE_MODE (TREE_TYPE (op0))) | |
315f5f1b | 2578 | return op0; |
cfef45c8 RG |
2579 | |
2580 | return | |
2581 | fold_unary_ignore_overflow_loc (loc, subcode, | |
2582 | gimple_expr_type (stmt), op0); | |
2583 | } | |
2584 | ||
2585 | case GIMPLE_BINARY_RHS: | |
2586 | { | |
2587 | /* Handle binary operators that can appear in GIMPLE form. */ | |
2588 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
2589 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
2590 | ||
2591 | /* Translate &x + CST into an invariant form suitable for | |
2592 | further propagation. */ | |
2593 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR | |
2594 | && TREE_CODE (op0) == ADDR_EXPR | |
2595 | && TREE_CODE (op1) == INTEGER_CST) | |
2596 | { | |
2597 | tree off = fold_convert (ptr_type_node, op1); | |
4d59a001 RG |
2598 | return build_fold_addr_expr_loc |
2599 | (loc, | |
2600 | fold_build2 (MEM_REF, | |
cfef45c8 RG |
2601 | TREE_TYPE (TREE_TYPE (op0)), |
2602 | unshare_expr (op0), off)); | |
2603 | } | |
2604 | ||
2605 | return fold_binary_loc (loc, subcode, | |
2606 | gimple_expr_type (stmt), op0, op1); | |
2607 | } | |
2608 | ||
2609 | case GIMPLE_TERNARY_RHS: | |
2610 | { | |
2611 | /* Handle ternary operators that can appear in GIMPLE form. */ | |
2612 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
2613 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
2614 | tree op2 = (*valueize) (gimple_assign_rhs3 (stmt)); | |
2615 | ||
d3878abf RG |
2616 | /* Fold embedded expressions in ternary codes. */ |
2617 | if ((subcode == COND_EXPR | |
2618 | || subcode == VEC_COND_EXPR) | |
2619 | && COMPARISON_CLASS_P (op0)) | |
2620 | { | |
2621 | tree op00 = (*valueize) (TREE_OPERAND (op0, 0)); | |
2622 | tree op01 = (*valueize) (TREE_OPERAND (op0, 1)); | |
2623 | tree tem = fold_binary_loc (loc, TREE_CODE (op0), | |
2624 | TREE_TYPE (op0), op00, op01); | |
2625 | if (tem) | |
2626 | op0 = tem; | |
2627 | } | |
2628 | ||
cfef45c8 RG |
2629 | return fold_ternary_loc (loc, subcode, |
2630 | gimple_expr_type (stmt), op0, op1, op2); | |
2631 | } | |
2632 | ||
2633 | default: | |
2634 | gcc_unreachable (); | |
2635 | } | |
2636 | } | |
2637 | ||
2638 | case GIMPLE_CALL: | |
2639 | { | |
25583c4f RS |
2640 | tree fn; |
2641 | ||
2642 | if (gimple_call_internal_p (stmt)) | |
2643 | /* No folding yet for these functions. */ | |
2644 | return NULL_TREE; | |
2645 | ||
2646 | fn = (*valueize) (gimple_call_fn (stmt)); | |
cfef45c8 RG |
2647 | if (TREE_CODE (fn) == ADDR_EXPR |
2648 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
2649 | && DECL_BUILT_IN (TREE_OPERAND (fn, 0))) | |
2650 | { | |
2651 | tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt)); | |
2652 | tree call, retval; | |
2653 | unsigned i; | |
2654 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
2655 | args[i] = (*valueize) (gimple_call_arg (stmt, i)); | |
2656 | call = build_call_array_loc (loc, | |
2657 | gimple_call_return_type (stmt), | |
2658 | fn, gimple_call_num_args (stmt), args); | |
2659 | retval = fold_call_expr (EXPR_LOCATION (call), call, false); | |
2660 | if (retval) | |
2661 | /* fold_call_expr wraps the result inside a NOP_EXPR. */ | |
2662 | STRIP_NOPS (retval); | |
2663 | return retval; | |
2664 | } | |
2665 | return NULL_TREE; | |
2666 | } | |
2667 | ||
2668 | default: | |
2669 | return NULL_TREE; | |
2670 | } | |
2671 | } | |
2672 | ||
2673 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
2674 | Returns NULL_TREE if folding to a constant is not possible, otherwise | |
2675 | returns a constant according to is_gimple_min_invariant. */ | |
2676 | ||
2677 | tree | |
2678 | gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree)) | |
2679 | { | |
2680 | tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize); | |
2681 | if (res && is_gimple_min_invariant (res)) | |
2682 | return res; | |
2683 | return NULL_TREE; | |
2684 | } | |
2685 | ||
2686 | ||
2687 | /* The following set of functions are supposed to fold references using | |
2688 | their constant initializers. */ | |
2689 | ||
2690 | static tree fold_ctor_reference (tree type, tree ctor, | |
2691 | unsigned HOST_WIDE_INT offset, | |
c44c2088 | 2692 | unsigned HOST_WIDE_INT size, tree); |
cfef45c8 RG |
2693 | |
2694 | /* See if we can find constructor defining value of BASE. | |
2695 | When we know the consructor with constant offset (such as | |
2696 | base is array[40] and we do know constructor of array), then | |
2697 | BIT_OFFSET is adjusted accordingly. | |
2698 | ||
2699 | As a special case, return error_mark_node when constructor | |
2700 | is not explicitly available, but it is known to be zero | |
2701 | such as 'static const int a;'. */ | |
2702 | static tree | |
2703 | get_base_constructor (tree base, HOST_WIDE_INT *bit_offset, | |
2704 | tree (*valueize)(tree)) | |
2705 | { | |
2706 | HOST_WIDE_INT bit_offset2, size, max_size; | |
2707 | if (TREE_CODE (base) == MEM_REF) | |
2708 | { | |
2709 | if (!integer_zerop (TREE_OPERAND (base, 1))) | |
2710 | { | |
2711 | if (!host_integerp (TREE_OPERAND (base, 1), 0)) | |
2712 | return NULL_TREE; | |
2713 | *bit_offset += (mem_ref_offset (base).low | |
2714 | * BITS_PER_UNIT); | |
2715 | } | |
2716 | ||
2717 | if (valueize | |
2718 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) | |
2719 | base = valueize (TREE_OPERAND (base, 0)); | |
2720 | if (!base || TREE_CODE (base) != ADDR_EXPR) | |
2721 | return NULL_TREE; | |
2722 | base = TREE_OPERAND (base, 0); | |
2723 | } | |
2724 | ||
2725 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
2726 | DECL_INITIAL. If BASE is a nested reference into another | |
2727 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
2728 | the inner reference. */ | |
2729 | switch (TREE_CODE (base)) | |
2730 | { | |
2731 | case VAR_DECL: | |
cfef45c8 | 2732 | case CONST_DECL: |
6a6dac52 JH |
2733 | { |
2734 | tree init = ctor_for_folding (base); | |
2735 | ||
688010ba | 2736 | /* Our semantic is exact opposite of ctor_for_folding; |
6a6dac52 JH |
2737 | NULL means unknown, while error_mark_node is 0. */ |
2738 | if (init == error_mark_node) | |
2739 | return NULL_TREE; | |
2740 | if (!init) | |
2741 | return error_mark_node; | |
2742 | return init; | |
2743 | } | |
cfef45c8 RG |
2744 | |
2745 | case ARRAY_REF: | |
2746 | case COMPONENT_REF: | |
2747 | base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size); | |
2748 | if (max_size == -1 || size != max_size) | |
2749 | return NULL_TREE; | |
2750 | *bit_offset += bit_offset2; | |
2751 | return get_base_constructor (base, bit_offset, valueize); | |
2752 | ||
2753 | case STRING_CST: | |
2754 | case CONSTRUCTOR: | |
2755 | return base; | |
2756 | ||
2757 | default: | |
2758 | return NULL_TREE; | |
2759 | } | |
2760 | } | |
2761 | ||
2762 | /* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE | |
2763 | to the memory at bit OFFSET. | |
2764 | ||
2765 | We do only simple job of folding byte accesses. */ | |
2766 | ||
2767 | static tree | |
2768 | fold_string_cst_ctor_reference (tree type, tree ctor, | |
2769 | unsigned HOST_WIDE_INT offset, | |
2770 | unsigned HOST_WIDE_INT size) | |
2771 | { | |
2772 | if (INTEGRAL_TYPE_P (type) | |
2773 | && (TYPE_MODE (type) | |
2774 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) | |
2775 | && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) | |
2776 | == MODE_INT) | |
2777 | && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1 | |
2778 | && size == BITS_PER_UNIT | |
2779 | && !(offset % BITS_PER_UNIT)) | |
2780 | { | |
2781 | offset /= BITS_PER_UNIT; | |
2782 | if (offset < (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (ctor)) | |
2783 | return build_int_cst_type (type, (TREE_STRING_POINTER (ctor) | |
2784 | [offset])); | |
2785 | /* Folding | |
2786 | const char a[20]="hello"; | |
2787 | return a[10]; | |
2788 | ||
2789 | might lead to offset greater than string length. In this case we | |
2790 | know value is either initialized to 0 or out of bounds. Return 0 | |
2791 | in both cases. */ | |
2792 | return build_zero_cst (type); | |
2793 | } | |
2794 | return NULL_TREE; | |
2795 | } | |
2796 | ||
2797 | /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size | |
2798 | SIZE to the memory at bit OFFSET. */ | |
2799 | ||
2800 | static tree | |
2801 | fold_array_ctor_reference (tree type, tree ctor, | |
2802 | unsigned HOST_WIDE_INT offset, | |
c44c2088 JH |
2803 | unsigned HOST_WIDE_INT size, |
2804 | tree from_decl) | |
cfef45c8 RG |
2805 | { |
2806 | unsigned HOST_WIDE_INT cnt; | |
2807 | tree cfield, cval; | |
2808 | double_int low_bound, elt_size; | |
2809 | double_int index, max_index; | |
2810 | double_int access_index; | |
b48e22b2 | 2811 | tree domain_type = NULL_TREE, index_type = NULL_TREE; |
cfef45c8 RG |
2812 | HOST_WIDE_INT inner_offset; |
2813 | ||
2814 | /* Compute low bound and elt size. */ | |
eb8f1123 RG |
2815 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE) |
2816 | domain_type = TYPE_DOMAIN (TREE_TYPE (ctor)); | |
cfef45c8 RG |
2817 | if (domain_type && TYPE_MIN_VALUE (domain_type)) |
2818 | { | |
2819 | /* Static constructors for variably sized objects makes no sense. */ | |
2820 | gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST); | |
b48e22b2 | 2821 | index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type)); |
cfef45c8 RG |
2822 | low_bound = tree_to_double_int (TYPE_MIN_VALUE (domain_type)); |
2823 | } | |
2824 | else | |
2825 | low_bound = double_int_zero; | |
2826 | /* Static constructors for variably sized objects makes no sense. */ | |
c3284718 | 2827 | gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) |
cfef45c8 RG |
2828 | == INTEGER_CST); |
2829 | elt_size = | |
2830 | tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))); | |
2831 | ||
2832 | ||
2833 | /* We can handle only constantly sized accesses that are known to not | |
2834 | be larger than size of array element. */ | |
2835 | if (!TYPE_SIZE_UNIT (type) | |
2836 | || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST | |
27bcd47c | 2837 | || elt_size.slt (tree_to_double_int (TYPE_SIZE_UNIT (type)))) |
cfef45c8 RG |
2838 | return NULL_TREE; |
2839 | ||
2840 | /* Compute the array index we look for. */ | |
27bcd47c LC |
2841 | access_index = double_int::from_uhwi (offset / BITS_PER_UNIT) |
2842 | .udiv (elt_size, TRUNC_DIV_EXPR); | |
2843 | access_index += low_bound; | |
b48e22b2 | 2844 | if (index_type) |
27bcd47c LC |
2845 | access_index = access_index.ext (TYPE_PRECISION (index_type), |
2846 | TYPE_UNSIGNED (index_type)); | |
cfef45c8 RG |
2847 | |
2848 | /* And offset within the access. */ | |
27bcd47c | 2849 | inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT); |
cfef45c8 RG |
2850 | |
2851 | /* See if the array field is large enough to span whole access. We do not | |
2852 | care to fold accesses spanning multiple array indexes. */ | |
27bcd47c | 2853 | if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT) |
cfef45c8 RG |
2854 | return NULL_TREE; |
2855 | ||
27bcd47c | 2856 | index = low_bound - double_int_one; |
b48e22b2 | 2857 | if (index_type) |
27bcd47c | 2858 | index = index.ext (TYPE_PRECISION (index_type), TYPE_UNSIGNED (index_type)); |
b48e22b2 | 2859 | |
cfef45c8 RG |
2860 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
2861 | { | |
2862 | /* Array constructor might explicitely set index, or specify range | |
2863 | or leave index NULL meaning that it is next index after previous | |
2864 | one. */ | |
2865 | if (cfield) | |
2866 | { | |
2867 | if (TREE_CODE (cfield) == INTEGER_CST) | |
2868 | max_index = index = tree_to_double_int (cfield); | |
2869 | else | |
2870 | { | |
2871 | gcc_assert (TREE_CODE (cfield) == RANGE_EXPR); | |
2872 | index = tree_to_double_int (TREE_OPERAND (cfield, 0)); | |
2873 | max_index = tree_to_double_int (TREE_OPERAND (cfield, 1)); | |
2874 | } | |
2875 | } | |
2876 | else | |
b48e22b2 | 2877 | { |
27bcd47c | 2878 | index += double_int_one; |
b48e22b2 | 2879 | if (index_type) |
27bcd47c LC |
2880 | index = index.ext (TYPE_PRECISION (index_type), |
2881 | TYPE_UNSIGNED (index_type)); | |
b48e22b2 EB |
2882 | max_index = index; |
2883 | } | |
cfef45c8 RG |
2884 | |
2885 | /* Do we have match? */ | |
27bcd47c LC |
2886 | if (access_index.cmp (index, 1) >= 0 |
2887 | && access_index.cmp (max_index, 1) <= 0) | |
c44c2088 JH |
2888 | return fold_ctor_reference (type, cval, inner_offset, size, |
2889 | from_decl); | |
cfef45c8 RG |
2890 | } |
2891 | /* When memory is not explicitely mentioned in constructor, | |
2892 | it is 0 (or out of range). */ | |
2893 | return build_zero_cst (type); | |
2894 | } | |
2895 | ||
2896 | /* CTOR is CONSTRUCTOR of an aggregate or vector. | |
2897 | Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */ | |
2898 | ||
2899 | static tree | |
2900 | fold_nonarray_ctor_reference (tree type, tree ctor, | |
2901 | unsigned HOST_WIDE_INT offset, | |
c44c2088 JH |
2902 | unsigned HOST_WIDE_INT size, |
2903 | tree from_decl) | |
cfef45c8 RG |
2904 | { |
2905 | unsigned HOST_WIDE_INT cnt; | |
2906 | tree cfield, cval; | |
2907 | ||
2908 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, | |
2909 | cval) | |
2910 | { | |
2911 | tree byte_offset = DECL_FIELD_OFFSET (cfield); | |
2912 | tree field_offset = DECL_FIELD_BIT_OFFSET (cfield); | |
2913 | tree field_size = DECL_SIZE (cfield); | |
2914 | double_int bitoffset; | |
2915 | double_int byte_offset_cst = tree_to_double_int (byte_offset); | |
27bcd47c | 2916 | double_int bits_per_unit_cst = double_int::from_uhwi (BITS_PER_UNIT); |
b8b2b009 | 2917 | double_int bitoffset_end, access_end; |
cfef45c8 RG |
2918 | |
2919 | /* Variable sized objects in static constructors makes no sense, | |
2920 | but field_size can be NULL for flexible array members. */ | |
2921 | gcc_assert (TREE_CODE (field_offset) == INTEGER_CST | |
2922 | && TREE_CODE (byte_offset) == INTEGER_CST | |
2923 | && (field_size != NULL_TREE | |
2924 | ? TREE_CODE (field_size) == INTEGER_CST | |
2925 | : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE)); | |
2926 | ||
2927 | /* Compute bit offset of the field. */ | |
27bcd47c LC |
2928 | bitoffset = tree_to_double_int (field_offset) |
2929 | + byte_offset_cst * bits_per_unit_cst; | |
cfef45c8 RG |
2930 | /* Compute bit offset where the field ends. */ |
2931 | if (field_size != NULL_TREE) | |
27bcd47c | 2932 | bitoffset_end = bitoffset + tree_to_double_int (field_size); |
cfef45c8 RG |
2933 | else |
2934 | bitoffset_end = double_int_zero; | |
2935 | ||
27bcd47c LC |
2936 | access_end = double_int::from_uhwi (offset) |
2937 | + double_int::from_uhwi (size); | |
b8b2b009 JJ |
2938 | |
2939 | /* Is there any overlap between [OFFSET, OFFSET+SIZE) and | |
2940 | [BITOFFSET, BITOFFSET_END)? */ | |
27bcd47c | 2941 | if (access_end.cmp (bitoffset, 0) > 0 |
cfef45c8 | 2942 | && (field_size == NULL_TREE |
27bcd47c | 2943 | || double_int::from_uhwi (offset).slt (bitoffset_end))) |
cfef45c8 | 2944 | { |
27bcd47c | 2945 | double_int inner_offset = double_int::from_uhwi (offset) - bitoffset; |
cfef45c8 RG |
2946 | /* We do have overlap. Now see if field is large enough to |
2947 | cover the access. Give up for accesses spanning multiple | |
2948 | fields. */ | |
27bcd47c | 2949 | if (access_end.cmp (bitoffset_end, 0) > 0) |
cfef45c8 | 2950 | return NULL_TREE; |
27bcd47c | 2951 | if (double_int::from_uhwi (offset).slt (bitoffset)) |
b8b2b009 | 2952 | return NULL_TREE; |
cfef45c8 | 2953 | return fold_ctor_reference (type, cval, |
27bcd47c | 2954 | inner_offset.to_uhwi (), size, |
c44c2088 | 2955 | from_decl); |
cfef45c8 RG |
2956 | } |
2957 | } | |
2958 | /* When memory is not explicitely mentioned in constructor, it is 0. */ | |
2959 | return build_zero_cst (type); | |
2960 | } | |
2961 | ||
2962 | /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE | |
2963 | to the memory at bit OFFSET. */ | |
2964 | ||
2965 | static tree | |
2966 | fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset, | |
c44c2088 | 2967 | unsigned HOST_WIDE_INT size, tree from_decl) |
cfef45c8 RG |
2968 | { |
2969 | tree ret; | |
2970 | ||
2971 | /* We found the field with exact match. */ | |
2972 | if (useless_type_conversion_p (type, TREE_TYPE (ctor)) | |
2973 | && !offset) | |
9d60be38 | 2974 | return canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
cfef45c8 RG |
2975 | |
2976 | /* We are at the end of walk, see if we can view convert the | |
2977 | result. */ | |
2978 | if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset | |
2979 | /* VIEW_CONVERT_EXPR is defined only for matching sizes. */ | |
2980 | && operand_equal_p (TYPE_SIZE (type), | |
2981 | TYPE_SIZE (TREE_TYPE (ctor)), 0)) | |
2982 | { | |
9d60be38 | 2983 | ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
cfef45c8 RG |
2984 | ret = fold_unary (VIEW_CONVERT_EXPR, type, ret); |
2985 | if (ret) | |
2986 | STRIP_NOPS (ret); | |
2987 | return ret; | |
2988 | } | |
2989 | if (TREE_CODE (ctor) == STRING_CST) | |
2990 | return fold_string_cst_ctor_reference (type, ctor, offset, size); | |
2991 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
2992 | { | |
2993 | ||
eb8f1123 RG |
2994 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE |
2995 | || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE) | |
c44c2088 JH |
2996 | return fold_array_ctor_reference (type, ctor, offset, size, |
2997 | from_decl); | |
cfef45c8 | 2998 | else |
c44c2088 JH |
2999 | return fold_nonarray_ctor_reference (type, ctor, offset, size, |
3000 | from_decl); | |
cfef45c8 RG |
3001 | } |
3002 | ||
3003 | return NULL_TREE; | |
3004 | } | |
3005 | ||
3006 | /* Return the tree representing the element referenced by T if T is an | |
3007 | ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA | |
3008 | names using VALUEIZE. Return NULL_TREE otherwise. */ | |
3009 | ||
3010 | tree | |
3011 | fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree)) | |
3012 | { | |
3013 | tree ctor, idx, base; | |
3014 | HOST_WIDE_INT offset, size, max_size; | |
3015 | tree tem; | |
3016 | ||
f8a7df45 RG |
3017 | if (TREE_THIS_VOLATILE (t)) |
3018 | return NULL_TREE; | |
3019 | ||
cfef45c8 RG |
3020 | if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration) |
3021 | return get_symbol_constant_value (t); | |
3022 | ||
3023 | tem = fold_read_from_constant_string (t); | |
3024 | if (tem) | |
3025 | return tem; | |
3026 | ||
3027 | switch (TREE_CODE (t)) | |
3028 | { | |
3029 | case ARRAY_REF: | |
3030 | case ARRAY_RANGE_REF: | |
3031 | /* Constant indexes are handled well by get_base_constructor. | |
3032 | Only special case variable offsets. | |
3033 | FIXME: This code can't handle nested references with variable indexes | |
3034 | (they will be handled only by iteration of ccp). Perhaps we can bring | |
3035 | get_ref_base_and_extent here and make it use a valueize callback. */ | |
3036 | if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME | |
3037 | && valueize | |
3038 | && (idx = (*valueize) (TREE_OPERAND (t, 1))) | |
b48e22b2 | 3039 | && TREE_CODE (idx) == INTEGER_CST) |
cfef45c8 RG |
3040 | { |
3041 | tree low_bound, unit_size; | |
b48e22b2 | 3042 | double_int doffset; |
cfef45c8 RG |
3043 | |
3044 | /* If the resulting bit-offset is constant, track it. */ | |
3045 | if ((low_bound = array_ref_low_bound (t), | |
b48e22b2 | 3046 | TREE_CODE (low_bound) == INTEGER_CST) |
cfef45c8 | 3047 | && (unit_size = array_ref_element_size (t), |
b48e22b2 | 3048 | host_integerp (unit_size, 1)) |
27bcd47c LC |
3049 | && (doffset = (TREE_INT_CST (idx) - TREE_INT_CST (low_bound)) |
3050 | .sext (TYPE_PRECISION (TREE_TYPE (idx))), | |
3051 | doffset.fits_shwi ())) | |
cfef45c8 | 3052 | { |
27bcd47c | 3053 | offset = doffset.to_shwi (); |
cfef45c8 RG |
3054 | offset *= TREE_INT_CST_LOW (unit_size); |
3055 | offset *= BITS_PER_UNIT; | |
3056 | ||
3057 | base = TREE_OPERAND (t, 0); | |
3058 | ctor = get_base_constructor (base, &offset, valueize); | |
3059 | /* Empty constructor. Always fold to 0. */ | |
3060 | if (ctor == error_mark_node) | |
3061 | return build_zero_cst (TREE_TYPE (t)); | |
3062 | /* Out of bound array access. Value is undefined, | |
3063 | but don't fold. */ | |
3064 | if (offset < 0) | |
3065 | return NULL_TREE; | |
3066 | /* We can not determine ctor. */ | |
3067 | if (!ctor) | |
3068 | return NULL_TREE; | |
3069 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, | |
3070 | TREE_INT_CST_LOW (unit_size) | |
c44c2088 JH |
3071 | * BITS_PER_UNIT, |
3072 | base); | |
cfef45c8 RG |
3073 | } |
3074 | } | |
3075 | /* Fallthru. */ | |
3076 | ||
3077 | case COMPONENT_REF: | |
3078 | case BIT_FIELD_REF: | |
3079 | case TARGET_MEM_REF: | |
3080 | case MEM_REF: | |
3081 | base = get_ref_base_and_extent (t, &offset, &size, &max_size); | |
3082 | ctor = get_base_constructor (base, &offset, valueize); | |
3083 | ||
3084 | /* Empty constructor. Always fold to 0. */ | |
3085 | if (ctor == error_mark_node) | |
3086 | return build_zero_cst (TREE_TYPE (t)); | |
3087 | /* We do not know precise address. */ | |
3088 | if (max_size == -1 || max_size != size) | |
3089 | return NULL_TREE; | |
3090 | /* We can not determine ctor. */ | |
3091 | if (!ctor) | |
3092 | return NULL_TREE; | |
3093 | ||
3094 | /* Out of bound array access. Value is undefined, but don't fold. */ | |
3095 | if (offset < 0) | |
3096 | return NULL_TREE; | |
3097 | ||
c44c2088 JH |
3098 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, |
3099 | base); | |
cfef45c8 RG |
3100 | |
3101 | case REALPART_EXPR: | |
3102 | case IMAGPART_EXPR: | |
3103 | { | |
3104 | tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize); | |
3105 | if (c && TREE_CODE (c) == COMPLEX_CST) | |
3106 | return fold_build1_loc (EXPR_LOCATION (t), | |
3107 | TREE_CODE (t), TREE_TYPE (t), c); | |
3108 | break; | |
3109 | } | |
3110 | ||
3111 | default: | |
3112 | break; | |
3113 | } | |
3114 | ||
3115 | return NULL_TREE; | |
3116 | } | |
3117 | ||
3118 | tree | |
3119 | fold_const_aggregate_ref (tree t) | |
3120 | { | |
3121 | return fold_const_aggregate_ref_1 (t, NULL); | |
3122 | } | |
06bc3ec7 | 3123 | |
81fa35bd MJ |
3124 | /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN |
3125 | is integer form of OBJ_TYPE_REF_TOKEN of the reference expression. | |
3126 | KNOWN_BINFO carries the binfo describing the true type of | |
3127 | OBJ_TYPE_REF_OBJECT(REF). */ | |
3128 | ||
3129 | tree | |
3130 | gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo) | |
3131 | { | |
3132 | unsigned HOST_WIDE_INT offset, size; | |
2aa3da06 | 3133 | tree v, fn, vtable, init; |
81fa35bd | 3134 | |
c44c2088 | 3135 | vtable = v = BINFO_VTABLE (known_binfo); |
81fa35bd MJ |
3136 | /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */ |
3137 | if (!v) | |
3138 | return NULL_TREE; | |
3139 | ||
3140 | if (TREE_CODE (v) == POINTER_PLUS_EXPR) | |
3141 | { | |
3142 | offset = tree_low_cst (TREE_OPERAND (v, 1), 1) * BITS_PER_UNIT; | |
3143 | v = TREE_OPERAND (v, 0); | |
3144 | } | |
3145 | else | |
3146 | offset = 0; | |
3147 | ||
3148 | if (TREE_CODE (v) != ADDR_EXPR) | |
3149 | return NULL_TREE; | |
3150 | v = TREE_OPERAND (v, 0); | |
3151 | ||
3152 | if (TREE_CODE (v) != VAR_DECL | |
2aa3da06 | 3153 | || !DECL_VIRTUAL_P (v)) |
81fa35bd | 3154 | return NULL_TREE; |
2aa3da06 JH |
3155 | init = ctor_for_folding (v); |
3156 | ||
3157 | /* The virtual tables should always be born with constructors. | |
3158 | and we always should assume that they are avaialble for | |
3159 | folding. At the moment we do not stream them in all cases, | |
3160 | but it should never happen that ctor seem unreachable. */ | |
3161 | gcc_assert (init); | |
3162 | if (init == error_mark_node) | |
3163 | { | |
3164 | gcc_assert (in_lto_p); | |
3165 | return NULL_TREE; | |
3166 | } | |
81fa35bd MJ |
3167 | gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE); |
3168 | size = tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))), 1); | |
3169 | offset += token * size; | |
2aa3da06 | 3170 | fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init, |
668b6894 | 3171 | offset, size, v); |
8e8483e6 | 3172 | if (!fn || integer_zerop (fn)) |
81fa35bd MJ |
3173 | return NULL_TREE; |
3174 | gcc_assert (TREE_CODE (fn) == ADDR_EXPR | |
3175 | || TREE_CODE (fn) == FDESC_EXPR); | |
3176 | fn = TREE_OPERAND (fn, 0); | |
3177 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); | |
3178 | ||
3179 | /* When cgraph node is missing and function is not public, we cannot | |
3180 | devirtualize. This can happen in WHOPR when the actual method | |
3181 | ends up in other partition, because we found devirtualization | |
3182 | possibility too late. */ | |
c44c2088 | 3183 | if (!can_refer_decl_in_current_unit_p (fn, vtable)) |
81fa35bd MJ |
3184 | return NULL_TREE; |
3185 | ||
7501ca28 RG |
3186 | /* Make sure we create a cgraph node for functions we'll reference. |
3187 | They can be non-existent if the reference comes from an entry | |
3188 | of an external vtable for example. */ | |
3189 | cgraph_get_create_node (fn); | |
3190 | ||
81fa35bd MJ |
3191 | return fn; |
3192 | } | |
3193 | ||
06bc3ec7 BS |
3194 | /* Return true iff VAL is a gimple expression that is known to be |
3195 | non-negative. Restricted to floating-point inputs. */ | |
3196 | ||
3197 | bool | |
3198 | gimple_val_nonnegative_real_p (tree val) | |
3199 | { | |
3200 | gimple def_stmt; | |
3201 | ||
3202 | gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))); | |
3203 | ||
3204 | /* Use existing logic for non-gimple trees. */ | |
3205 | if (tree_expr_nonnegative_p (val)) | |
3206 | return true; | |
3207 | ||
3208 | if (TREE_CODE (val) != SSA_NAME) | |
3209 | return false; | |
3210 | ||
3211 | /* Currently we look only at the immediately defining statement | |
3212 | to make this determination, since recursion on defining | |
3213 | statements of operands can lead to quadratic behavior in the | |
3214 | worst case. This is expected to catch almost all occurrences | |
3215 | in practice. It would be possible to implement limited-depth | |
3216 | recursion if important cases are lost. Alternatively, passes | |
3217 | that need this information (such as the pow/powi lowering code | |
3218 | in the cse_sincos pass) could be revised to provide it through | |
3219 | dataflow propagation. */ | |
3220 | ||
3221 | def_stmt = SSA_NAME_DEF_STMT (val); | |
3222 | ||
3223 | if (is_gimple_assign (def_stmt)) | |
3224 | { | |
3225 | tree op0, op1; | |
3226 | ||
3227 | /* See fold-const.c:tree_expr_nonnegative_p for additional | |
3228 | cases that could be handled with recursion. */ | |
3229 | ||
3230 | switch (gimple_assign_rhs_code (def_stmt)) | |
3231 | { | |
3232 | case ABS_EXPR: | |
3233 | /* Always true for floating-point operands. */ | |
3234 | return true; | |
3235 | ||
3236 | case MULT_EXPR: | |
3237 | /* True if the two operands are identical (since we are | |
3238 | restricted to floating-point inputs). */ | |
3239 | op0 = gimple_assign_rhs1 (def_stmt); | |
3240 | op1 = gimple_assign_rhs2 (def_stmt); | |
3241 | ||
3242 | if (op0 == op1 | |
3243 | || operand_equal_p (op0, op1, 0)) | |
3244 | return true; | |
3245 | ||
3246 | default: | |
3247 | return false; | |
3248 | } | |
3249 | } | |
3250 | else if (is_gimple_call (def_stmt)) | |
3251 | { | |
3252 | tree fndecl = gimple_call_fndecl (def_stmt); | |
3253 | if (fndecl | |
3254 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
3255 | { | |
3256 | tree arg1; | |
3257 | ||
3258 | switch (DECL_FUNCTION_CODE (fndecl)) | |
3259 | { | |
3260 | CASE_FLT_FN (BUILT_IN_ACOS): | |
3261 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
3262 | CASE_FLT_FN (BUILT_IN_CABS): | |
3263 | CASE_FLT_FN (BUILT_IN_COSH): | |
3264 | CASE_FLT_FN (BUILT_IN_ERFC): | |
3265 | CASE_FLT_FN (BUILT_IN_EXP): | |
3266 | CASE_FLT_FN (BUILT_IN_EXP10): | |
3267 | CASE_FLT_FN (BUILT_IN_EXP2): | |
3268 | CASE_FLT_FN (BUILT_IN_FABS): | |
3269 | CASE_FLT_FN (BUILT_IN_FDIM): | |
3270 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
3271 | CASE_FLT_FN (BUILT_IN_POW10): | |
3272 | return true; | |
3273 | ||
3274 | CASE_FLT_FN (BUILT_IN_SQRT): | |
3275 | /* sqrt(-0.0) is -0.0, and sqrt is not defined over other | |
3276 | nonnegative inputs. */ | |
3277 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val)))) | |
3278 | return true; | |
3279 | ||
3280 | break; | |
3281 | ||
3282 | CASE_FLT_FN (BUILT_IN_POWI): | |
3283 | /* True if the second argument is an even integer. */ | |
3284 | arg1 = gimple_call_arg (def_stmt, 1); | |
3285 | ||
3286 | if (TREE_CODE (arg1) == INTEGER_CST | |
3287 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) | |
3288 | return true; | |
3289 | ||
3290 | break; | |
3291 | ||
3292 | CASE_FLT_FN (BUILT_IN_POW): | |
3293 | /* True if the second argument is an even integer-valued | |
3294 | real. */ | |
3295 | arg1 = gimple_call_arg (def_stmt, 1); | |
3296 | ||
3297 | if (TREE_CODE (arg1) == REAL_CST) | |
3298 | { | |
3299 | REAL_VALUE_TYPE c; | |
3300 | HOST_WIDE_INT n; | |
3301 | ||
3302 | c = TREE_REAL_CST (arg1); | |
3303 | n = real_to_integer (&c); | |
3304 | ||
3305 | if ((n & 1) == 0) | |
3306 | { | |
3307 | REAL_VALUE_TYPE cint; | |
3308 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
3309 | if (real_identical (&c, &cint)) | |
3310 | return true; | |
3311 | } | |
3312 | } | |
3313 | ||
3314 | break; | |
3315 | ||
3316 | default: | |
3317 | return false; | |
3318 | } | |
3319 | } | |
3320 | } | |
3321 | ||
3322 | return false; | |
3323 | } |