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