* config/rl78/rl78.c (rl78_alloc_address_registers_macax): Verify
[official-gcc.git] / gcc / gimple-fold.c
blob33e677f246eb7adc7e83ed3157041ef5b52b6ca1
1 /* Statement simplification on GIMPLE.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 Split out from tree-ssa-ccp.c.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "dumpfile.h"
29 #include "tree-ssa.h"
30 #include "tree-ssa-propagate.h"
31 #include "target.h"
32 #include "ipa-utils.h"
33 #include "gimple-pretty-print.h"
35 /* Return true when DECL can be referenced from current unit.
36 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
37 We can get declarations that are not possible to reference for various
38 reasons:
40 1) When analyzing C++ virtual tables.
41 C++ virtual tables do have known constructors even
42 when they are keyed to other compilation unit.
43 Those tables can contain pointers to methods and vars
44 in other units. Those methods have both STATIC and EXTERNAL
45 set.
46 2) In WHOPR mode devirtualization might lead to reference
47 to method that was partitioned elsehwere.
48 In this case we have static VAR_DECL or FUNCTION_DECL
49 that has no corresponding callgraph/varpool node
50 declaring the body.
51 3) COMDAT functions referred by external vtables that
52 we devirtualize only during final copmilation stage.
53 At this time we already decided that we will not output
54 the function body and thus we can't reference the symbol
55 directly. */
57 static bool
58 can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
60 struct varpool_node *vnode;
61 struct cgraph_node *node;
62 symtab_node snode;
64 if (DECL_ABSTRACT (decl))
65 return false;
67 /* We are concerned only about static/external vars and functions. */
68 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
69 || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL))
70 return true;
72 /* Static objects can be referred only if they was not optimized out yet. */
73 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
75 snode = symtab_get_node (decl);
76 if (!snode)
77 return false;
78 node = dyn_cast <cgraph_node> (snode);
79 return !node || !node->global.inlined_to;
82 /* We will later output the initializer, so we can refer to it.
83 So we are concerned only when DECL comes from initializer of
84 external var. */
85 if (!from_decl
86 || TREE_CODE (from_decl) != VAR_DECL
87 || !DECL_EXTERNAL (from_decl)
88 || (flag_ltrans
89 && symtab_get_node (from_decl)->symbol.in_other_partition))
90 return true;
91 /* We are folding reference from external vtable. The vtable may reffer
92 to a symbol keyed to other compilation unit. The other compilation
93 unit may be in separate DSO and the symbol may be hidden. */
94 if (DECL_VISIBILITY_SPECIFIED (decl)
95 && DECL_EXTERNAL (decl)
96 && (!(snode = symtab_get_node (decl)) || !snode->symbol.in_other_partition))
97 return false;
98 /* When function is public, we always can introduce new reference.
99 Exception are the COMDAT functions where introducing a direct
100 reference imply need to include function body in the curren tunit. */
101 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
102 return true;
103 /* We are not at ltrans stage; so don't worry about WHOPR.
104 Also when still gimplifying all referred comdat functions will be
105 produced.
107 As observed in PR20991 for already optimized out comdat virtual functions
108 it may be tempting to not necessarily give up because the copy will be
109 output elsewhere when corresponding vtable is output.
110 This is however not possible - ABI specify that COMDATs are output in
111 units where they are used and when the other unit was compiled with LTO
112 it is possible that vtable was kept public while the function itself
113 was privatized. */
114 if (!flag_ltrans && (!DECL_COMDAT (decl) || !cgraph_function_flags_ready))
115 return true;
117 /* OK we are seeing either COMDAT or static variable. In this case we must
118 check that the definition is still around so we can refer it. */
119 if (TREE_CODE (decl) == FUNCTION_DECL)
121 node = cgraph_get_node (decl);
122 /* Check that we still have function body and that we didn't took
123 the decision to eliminate offline copy of the function yet.
124 The second is important when devirtualization happens during final
125 compilation stage when making a new reference no longer makes callee
126 to be compiled. */
127 if (!node || !node->symbol.definition || node->global.inlined_to)
129 gcc_checking_assert (!TREE_ASM_WRITTEN (decl));
130 return false;
133 else if (TREE_CODE (decl) == VAR_DECL)
135 vnode = varpool_get_node (decl);
136 if (!vnode || !vnode->symbol.definition)
138 gcc_checking_assert (!TREE_ASM_WRITTEN (decl));
139 return false;
142 return true;
145 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
146 acceptable form for is_gimple_min_invariant.
147 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
149 tree
150 canonicalize_constructor_val (tree cval, tree from_decl)
152 tree orig_cval = cval;
153 STRIP_NOPS (cval);
154 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
155 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
157 tree ptr = TREE_OPERAND (cval, 0);
158 if (is_gimple_min_invariant (ptr))
159 cval = build1_loc (EXPR_LOCATION (cval),
160 ADDR_EXPR, TREE_TYPE (ptr),
161 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
162 ptr,
163 fold_convert (ptr_type_node,
164 TREE_OPERAND (cval, 1))));
166 if (TREE_CODE (cval) == ADDR_EXPR)
168 tree base = NULL_TREE;
169 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
171 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
172 if (base)
173 TREE_OPERAND (cval, 0) = base;
175 else
176 base = get_base_address (TREE_OPERAND (cval, 0));
177 if (!base)
178 return NULL_TREE;
180 if ((TREE_CODE (base) == VAR_DECL
181 || TREE_CODE (base) == FUNCTION_DECL)
182 && !can_refer_decl_in_current_unit_p (base, from_decl))
183 return NULL_TREE;
184 if (TREE_CODE (base) == VAR_DECL)
185 TREE_ADDRESSABLE (base) = 1;
186 else if (TREE_CODE (base) == FUNCTION_DECL)
188 /* Make sure we create a cgraph node for functions we'll reference.
189 They can be non-existent if the reference comes from an entry
190 of an external vtable for example. */
191 cgraph_get_create_real_symbol_node (base);
193 /* Fixup types in global initializers. */
194 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
195 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
197 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
198 cval = fold_convert (TREE_TYPE (orig_cval), cval);
199 return cval;
201 return orig_cval;
204 /* If SYM is a constant variable with known value, return the value.
205 NULL_TREE is returned otherwise. */
207 tree
208 get_symbol_constant_value (tree sym)
210 tree val = ctor_for_folding (sym);
211 if (val != error_mark_node)
213 if (val)
215 val = canonicalize_constructor_val (unshare_expr (val), sym);
216 if (val && is_gimple_min_invariant (val))
217 return val;
218 else
219 return NULL_TREE;
221 /* Variables declared 'const' without an initializer
222 have zero as the initializer if they may not be
223 overridden at link or run time. */
224 if (!val
225 && (INTEGRAL_TYPE_P (TREE_TYPE (sym))
226 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym))))
227 return build_zero_cst (TREE_TYPE (sym));
230 return NULL_TREE;
235 /* Subroutine of fold_stmt. We perform several simplifications of the
236 memory reference tree EXPR and make sure to re-gimplify them properly
237 after propagation of constant addresses. IS_LHS is true if the
238 reference is supposed to be an lvalue. */
240 static tree
241 maybe_fold_reference (tree expr, bool is_lhs)
243 tree *t = &expr;
244 tree result;
246 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
247 || TREE_CODE (expr) == REALPART_EXPR
248 || TREE_CODE (expr) == IMAGPART_EXPR)
249 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
250 return fold_unary_loc (EXPR_LOCATION (expr),
251 TREE_CODE (expr),
252 TREE_TYPE (expr),
253 TREE_OPERAND (expr, 0));
254 else if (TREE_CODE (expr) == BIT_FIELD_REF
255 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
256 return fold_ternary_loc (EXPR_LOCATION (expr),
257 TREE_CODE (expr),
258 TREE_TYPE (expr),
259 TREE_OPERAND (expr, 0),
260 TREE_OPERAND (expr, 1),
261 TREE_OPERAND (expr, 2));
263 while (handled_component_p (*t))
264 t = &TREE_OPERAND (*t, 0);
266 /* Canonicalize MEM_REFs invariant address operand. Do this first
267 to avoid feeding non-canonical MEM_REFs elsewhere. */
268 if (TREE_CODE (*t) == MEM_REF
269 && !is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)))
271 bool volatile_p = TREE_THIS_VOLATILE (*t);
272 tree tem = fold_binary (MEM_REF, TREE_TYPE (*t),
273 TREE_OPERAND (*t, 0),
274 TREE_OPERAND (*t, 1));
275 if (tem)
277 TREE_THIS_VOLATILE (tem) = volatile_p;
278 *t = tem;
279 tem = maybe_fold_reference (expr, is_lhs);
280 if (tem)
281 return tem;
282 return expr;
286 if (!is_lhs
287 && (result = fold_const_aggregate_ref (expr))
288 && is_gimple_min_invariant (result))
289 return result;
291 /* Fold back MEM_REFs to reference trees. */
292 if (TREE_CODE (*t) == MEM_REF
293 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
294 && integer_zerop (TREE_OPERAND (*t, 1))
295 && (TREE_THIS_VOLATILE (*t)
296 == TREE_THIS_VOLATILE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0)))
297 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*t, 1)))
298 && (TYPE_MAIN_VARIANT (TREE_TYPE (*t))
299 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (*t, 1)))))
300 /* We have to look out here to not drop a required conversion
301 from the rhs to the lhs if is_lhs, but we don't have the
302 rhs here to verify that. Thus require strict type
303 compatibility. */
304 && types_compatible_p (TREE_TYPE (*t),
305 TREE_TYPE (TREE_OPERAND
306 (TREE_OPERAND (*t, 0), 0))))
308 tree tem;
309 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
310 tem = maybe_fold_reference (expr, is_lhs);
311 if (tem)
312 return tem;
313 return expr;
315 else if (TREE_CODE (*t) == TARGET_MEM_REF)
317 tree tem = maybe_fold_tmr (*t);
318 if (tem)
320 *t = tem;
321 tem = maybe_fold_reference (expr, is_lhs);
322 if (tem)
323 return tem;
324 return expr;
328 return NULL_TREE;
332 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
333 replacement rhs for the statement or NULL_TREE if no simplification
334 could be made. It is assumed that the operands have been previously
335 folded. */
337 static tree
338 fold_gimple_assign (gimple_stmt_iterator *si)
340 gimple stmt = gsi_stmt (*si);
341 enum tree_code subcode = gimple_assign_rhs_code (stmt);
342 location_t loc = gimple_location (stmt);
344 tree result = NULL_TREE;
346 switch (get_gimple_rhs_class (subcode))
348 case GIMPLE_SINGLE_RHS:
350 tree rhs = gimple_assign_rhs1 (stmt);
352 if (REFERENCE_CLASS_P (rhs))
353 return maybe_fold_reference (rhs, false);
355 else if (TREE_CODE (rhs) == ADDR_EXPR)
357 tree ref = TREE_OPERAND (rhs, 0);
358 tree tem = maybe_fold_reference (ref, true);
359 if (tem
360 && TREE_CODE (tem) == MEM_REF
361 && integer_zerop (TREE_OPERAND (tem, 1)))
362 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
363 else if (tem)
364 result = fold_convert (TREE_TYPE (rhs),
365 build_fold_addr_expr_loc (loc, tem));
366 else if (TREE_CODE (ref) == MEM_REF
367 && integer_zerop (TREE_OPERAND (ref, 1)))
368 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
371 else if (TREE_CODE (rhs) == CONSTRUCTOR
372 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
373 && (CONSTRUCTOR_NELTS (rhs)
374 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
376 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
377 unsigned i;
378 tree val;
380 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
381 if (TREE_CODE (val) != INTEGER_CST
382 && TREE_CODE (val) != REAL_CST
383 && TREE_CODE (val) != FIXED_CST)
384 return NULL_TREE;
386 return build_vector_from_ctor (TREE_TYPE (rhs),
387 CONSTRUCTOR_ELTS (rhs));
390 else if (DECL_P (rhs))
391 return get_symbol_constant_value (rhs);
393 /* If we couldn't fold the RHS, hand over to the generic
394 fold routines. */
395 if (result == NULL_TREE)
396 result = fold (rhs);
398 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
399 that may have been added by fold, and "useless" type
400 conversions that might now be apparent due to propagation. */
401 STRIP_USELESS_TYPE_CONVERSION (result);
403 if (result != rhs && valid_gimple_rhs_p (result))
404 return result;
406 return NULL_TREE;
408 break;
410 case GIMPLE_UNARY_RHS:
412 tree rhs = gimple_assign_rhs1 (stmt);
414 result = fold_unary_loc (loc, subcode, gimple_expr_type (stmt), rhs);
415 if (result)
417 /* If the operation was a conversion do _not_ mark a
418 resulting constant with TREE_OVERFLOW if the original
419 constant was not. These conversions have implementation
420 defined behavior and retaining the TREE_OVERFLOW flag
421 here would confuse later passes such as VRP. */
422 if (CONVERT_EXPR_CODE_P (subcode)
423 && TREE_CODE (result) == INTEGER_CST
424 && TREE_CODE (rhs) == INTEGER_CST)
425 TREE_OVERFLOW (result) = TREE_OVERFLOW (rhs);
427 STRIP_USELESS_TYPE_CONVERSION (result);
428 if (valid_gimple_rhs_p (result))
429 return result;
432 break;
434 case GIMPLE_BINARY_RHS:
435 /* Try to canonicalize for boolean-typed X the comparisons
436 X == 0, X == 1, X != 0, and X != 1. */
437 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
438 || gimple_assign_rhs_code (stmt) == NE_EXPR)
440 tree lhs = gimple_assign_lhs (stmt);
441 tree op1 = gimple_assign_rhs1 (stmt);
442 tree op2 = gimple_assign_rhs2 (stmt);
443 tree type = TREE_TYPE (op1);
445 /* Check whether the comparison operands are of the same boolean
446 type as the result type is.
447 Check that second operand is an integer-constant with value
448 one or zero. */
449 if (TREE_CODE (op2) == INTEGER_CST
450 && (integer_zerop (op2) || integer_onep (op2))
451 && useless_type_conversion_p (TREE_TYPE (lhs), type))
453 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
454 bool is_logical_not = false;
456 /* X == 0 and X != 1 is a logical-not.of X
457 X == 1 and X != 0 is X */
458 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
459 || (cmp_code == NE_EXPR && integer_onep (op2)))
460 is_logical_not = true;
462 if (is_logical_not == false)
463 result = op1;
464 /* Only for one-bit precision typed X the transformation
465 !X -> ~X is valied. */
466 else if (TYPE_PRECISION (type) == 1)
467 result = build1_loc (gimple_location (stmt), BIT_NOT_EXPR,
468 type, op1);
469 /* Otherwise we use !X -> X ^ 1. */
470 else
471 result = build2_loc (gimple_location (stmt), BIT_XOR_EXPR,
472 type, op1, build_int_cst (type, 1));
477 if (!result)
478 result = fold_binary_loc (loc, subcode,
479 TREE_TYPE (gimple_assign_lhs (stmt)),
480 gimple_assign_rhs1 (stmt),
481 gimple_assign_rhs2 (stmt));
483 if (result)
485 STRIP_USELESS_TYPE_CONVERSION (result);
486 if (valid_gimple_rhs_p (result))
487 return result;
489 break;
491 case GIMPLE_TERNARY_RHS:
492 /* Try to fold a conditional expression. */
493 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
495 tree op0 = gimple_assign_rhs1 (stmt);
496 tree tem;
497 bool set = false;
498 location_t cond_loc = gimple_location (stmt);
500 if (COMPARISON_CLASS_P (op0))
502 fold_defer_overflow_warnings ();
503 tem = fold_binary_loc (cond_loc,
504 TREE_CODE (op0), TREE_TYPE (op0),
505 TREE_OPERAND (op0, 0),
506 TREE_OPERAND (op0, 1));
507 /* This is actually a conditional expression, not a GIMPLE
508 conditional statement, however, the valid_gimple_rhs_p
509 test still applies. */
510 set = (tem && is_gimple_condexpr (tem)
511 && valid_gimple_rhs_p (tem));
512 fold_undefer_overflow_warnings (set, stmt, 0);
514 else if (is_gimple_min_invariant (op0))
516 tem = op0;
517 set = true;
519 else
520 return NULL_TREE;
522 if (set)
523 result = fold_build3_loc (cond_loc, COND_EXPR,
524 TREE_TYPE (gimple_assign_lhs (stmt)), tem,
525 gimple_assign_rhs2 (stmt),
526 gimple_assign_rhs3 (stmt));
529 if (!result)
530 result = fold_ternary_loc (loc, subcode,
531 TREE_TYPE (gimple_assign_lhs (stmt)),
532 gimple_assign_rhs1 (stmt),
533 gimple_assign_rhs2 (stmt),
534 gimple_assign_rhs3 (stmt));
536 if (result)
538 STRIP_USELESS_TYPE_CONVERSION (result);
539 if (valid_gimple_rhs_p (result))
540 return result;
542 break;
544 case GIMPLE_INVALID_RHS:
545 gcc_unreachable ();
548 return NULL_TREE;
551 /* Attempt to fold a conditional statement. Return true if any changes were
552 made. We only attempt to fold the condition expression, and do not perform
553 any transformation that would require alteration of the cfg. It is
554 assumed that the operands have been previously folded. */
556 static bool
557 fold_gimple_cond (gimple stmt)
559 tree result = fold_binary_loc (gimple_location (stmt),
560 gimple_cond_code (stmt),
561 boolean_type_node,
562 gimple_cond_lhs (stmt),
563 gimple_cond_rhs (stmt));
565 if (result)
567 STRIP_USELESS_TYPE_CONVERSION (result);
568 if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result))
570 gimple_cond_set_condition_from_tree (stmt, result);
571 return true;
575 return false;
578 /* Convert EXPR into a GIMPLE value suitable for substitution on the
579 RHS of an assignment. Insert the necessary statements before
580 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
581 is replaced. If the call is expected to produces a result, then it
582 is replaced by an assignment of the new RHS to the result variable.
583 If the result is to be ignored, then the call is replaced by a
584 GIMPLE_NOP. A proper VDEF chain is retained by making the first
585 VUSE and the last VDEF of the whole sequence be the same as the replaced
586 statement and using new SSA names for stores in between. */
588 void
589 gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
591 tree lhs;
592 gimple stmt, new_stmt;
593 gimple_stmt_iterator i;
594 gimple_seq stmts = NULL;
595 struct gimplify_ctx gctx;
596 gimple laststore;
597 tree reaching_vuse;
599 stmt = gsi_stmt (*si_p);
601 gcc_assert (is_gimple_call (stmt));
603 push_gimplify_context (&gctx);
604 gctx.into_ssa = gimple_in_ssa_p (cfun);
606 lhs = gimple_call_lhs (stmt);
607 if (lhs == NULL_TREE)
609 gimplify_and_add (expr, &stmts);
610 /* We can end up with folding a memcpy of an empty class assignment
611 which gets optimized away by C++ gimplification. */
612 if (gimple_seq_empty_p (stmts))
614 pop_gimplify_context (NULL);
615 if (gimple_in_ssa_p (cfun))
617 unlink_stmt_vdef (stmt);
618 release_defs (stmt);
620 gsi_replace (si_p, gimple_build_nop (), true);
621 return;
624 else
626 tree tmp = get_initialized_tmp_var (expr, &stmts, NULL);
627 new_stmt = gimple_build_assign (lhs, tmp);
628 i = gsi_last (stmts);
629 gsi_insert_after_without_update (&i, new_stmt,
630 GSI_CONTINUE_LINKING);
633 pop_gimplify_context (NULL);
635 if (gimple_has_location (stmt))
636 annotate_all_with_location (stmts, gimple_location (stmt));
638 /* First iterate over the replacement statements backward, assigning
639 virtual operands to their defining statements. */
640 laststore = NULL;
641 for (i = gsi_last (stmts); !gsi_end_p (i); gsi_prev (&i))
643 new_stmt = gsi_stmt (i);
644 if ((gimple_assign_single_p (new_stmt)
645 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
646 || (is_gimple_call (new_stmt)
647 && (gimple_call_flags (new_stmt)
648 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
650 tree vdef;
651 if (!laststore)
652 vdef = gimple_vdef (stmt);
653 else
654 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
655 gimple_set_vdef (new_stmt, vdef);
656 if (vdef && TREE_CODE (vdef) == SSA_NAME)
657 SSA_NAME_DEF_STMT (vdef) = new_stmt;
658 laststore = new_stmt;
662 /* Second iterate over the statements forward, assigning virtual
663 operands to their uses. */
664 reaching_vuse = gimple_vuse (stmt);
665 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
667 new_stmt = gsi_stmt (i);
668 /* If the new statement possibly has a VUSE, update it with exact SSA
669 name we know will reach this one. */
670 if (gimple_has_mem_ops (new_stmt))
671 gimple_set_vuse (new_stmt, reaching_vuse);
672 gimple_set_modified (new_stmt, true);
673 if (gimple_vdef (new_stmt))
674 reaching_vuse = gimple_vdef (new_stmt);
677 /* If the new sequence does not do a store release the virtual
678 definition of the original statement. */
679 if (reaching_vuse
680 && reaching_vuse == gimple_vuse (stmt))
682 tree vdef = gimple_vdef (stmt);
683 if (vdef
684 && TREE_CODE (vdef) == SSA_NAME)
686 unlink_stmt_vdef (stmt);
687 release_ssa_name (vdef);
691 /* Finally replace the original statement with the sequence. */
692 gsi_replace_with_seq (si_p, stmts, false);
695 /* Return the string length, maximum string length or maximum value of
696 ARG in LENGTH.
697 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
698 is not NULL and, for TYPE == 0, its value is not equal to the length
699 we determine or if we are unable to determine the length or value,
700 return false. VISITED is a bitmap of visited variables.
701 TYPE is 0 if string length should be returned, 1 for maximum string
702 length and 2 for maximum value ARG can have. */
704 static bool
705 get_maxval_strlen (tree arg, tree *length, bitmap visited, int type)
707 tree var, val;
708 gimple def_stmt;
710 if (TREE_CODE (arg) != SSA_NAME)
712 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
713 if (TREE_CODE (arg) == ADDR_EXPR
714 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
715 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
717 tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
718 if (TREE_CODE (aop0) == INDIRECT_REF
719 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
720 return get_maxval_strlen (TREE_OPERAND (aop0, 0),
721 length, visited, type);
724 if (type == 2)
726 val = arg;
727 if (TREE_CODE (val) != INTEGER_CST
728 || tree_int_cst_sgn (val) < 0)
729 return false;
731 else
732 val = c_strlen (arg, 1);
733 if (!val)
734 return false;
736 if (*length)
738 if (type > 0)
740 if (TREE_CODE (*length) != INTEGER_CST
741 || TREE_CODE (val) != INTEGER_CST)
742 return false;
744 if (tree_int_cst_lt (*length, val))
745 *length = val;
746 return true;
748 else if (simple_cst_equal (val, *length) != 1)
749 return false;
752 *length = val;
753 return true;
756 /* If ARG is registered for SSA update we cannot look at its defining
757 statement. */
758 if (name_registered_for_update_p (arg))
759 return false;
761 /* If we were already here, break the infinite cycle. */
762 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
763 return true;
765 var = arg;
766 def_stmt = SSA_NAME_DEF_STMT (var);
768 switch (gimple_code (def_stmt))
770 case GIMPLE_ASSIGN:
771 /* The RHS of the statement defining VAR must either have a
772 constant length or come from another SSA_NAME with a constant
773 length. */
774 if (gimple_assign_single_p (def_stmt)
775 || gimple_assign_unary_nop_p (def_stmt))
777 tree rhs = gimple_assign_rhs1 (def_stmt);
778 return get_maxval_strlen (rhs, length, visited, type);
780 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
782 tree op2 = gimple_assign_rhs2 (def_stmt);
783 tree op3 = gimple_assign_rhs3 (def_stmt);
784 return get_maxval_strlen (op2, length, visited, type)
785 && get_maxval_strlen (op3, length, visited, type);
787 return false;
789 case GIMPLE_PHI:
791 /* All the arguments of the PHI node must have the same constant
792 length. */
793 unsigned i;
795 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
797 tree arg = gimple_phi_arg (def_stmt, i)->def;
799 /* If this PHI has itself as an argument, we cannot
800 determine the string length of this argument. However,
801 if we can find a constant string length for the other
802 PHI args then we can still be sure that this is a
803 constant string length. So be optimistic and just
804 continue with the next argument. */
805 if (arg == gimple_phi_result (def_stmt))
806 continue;
808 if (!get_maxval_strlen (arg, length, visited, type))
809 return false;
812 return true;
814 default:
815 return false;
820 /* Fold builtin call in statement STMT. Returns a simplified tree.
821 We may return a non-constant expression, including another call
822 to a different function and with different arguments, e.g.,
823 substituting memcpy for strcpy when the string length is known.
824 Note that some builtins expand into inline code that may not
825 be valid in GIMPLE. Callers must take care. */
827 tree
828 gimple_fold_builtin (gimple stmt)
830 tree result, val[3];
831 tree callee, a;
832 int arg_idx, type;
833 bitmap visited;
834 bool ignore;
835 int nargs;
836 location_t loc = gimple_location (stmt);
838 gcc_assert (is_gimple_call (stmt));
840 ignore = (gimple_call_lhs (stmt) == NULL);
842 /* First try the generic builtin folder. If that succeeds, return the
843 result directly. */
844 result = fold_call_stmt (stmt, ignore);
845 if (result)
847 if (ignore)
848 STRIP_NOPS (result);
849 return result;
852 /* Ignore MD builtins. */
853 callee = gimple_call_fndecl (stmt);
854 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
855 return NULL_TREE;
857 /* Give up for always_inline inline builtins until they are
858 inlined. */
859 if (avoid_folding_inline_builtin (callee))
860 return NULL_TREE;
862 /* If the builtin could not be folded, and it has no argument list,
863 we're done. */
864 nargs = gimple_call_num_args (stmt);
865 if (nargs == 0)
866 return NULL_TREE;
868 /* Limit the work only for builtins we know how to simplify. */
869 switch (DECL_FUNCTION_CODE (callee))
871 case BUILT_IN_STRLEN:
872 case BUILT_IN_FPUTS:
873 case BUILT_IN_FPUTS_UNLOCKED:
874 arg_idx = 0;
875 type = 0;
876 break;
877 case BUILT_IN_STRCPY:
878 case BUILT_IN_STRNCPY:
879 arg_idx = 1;
880 type = 0;
881 break;
882 case BUILT_IN_MEMCPY_CHK:
883 case BUILT_IN_MEMPCPY_CHK:
884 case BUILT_IN_MEMMOVE_CHK:
885 case BUILT_IN_MEMSET_CHK:
886 case BUILT_IN_STRNCPY_CHK:
887 case BUILT_IN_STPNCPY_CHK:
888 arg_idx = 2;
889 type = 2;
890 break;
891 case BUILT_IN_STRCPY_CHK:
892 case BUILT_IN_STPCPY_CHK:
893 arg_idx = 1;
894 type = 1;
895 break;
896 case BUILT_IN_SNPRINTF_CHK:
897 case BUILT_IN_VSNPRINTF_CHK:
898 arg_idx = 1;
899 type = 2;
900 break;
901 default:
902 return NULL_TREE;
905 if (arg_idx >= nargs)
906 return NULL_TREE;
908 /* Try to use the dataflow information gathered by the CCP process. */
909 visited = BITMAP_ALLOC (NULL);
910 bitmap_clear (visited);
912 memset (val, 0, sizeof (val));
913 a = gimple_call_arg (stmt, arg_idx);
914 if (!get_maxval_strlen (a, &val[arg_idx], visited, type))
915 val[arg_idx] = NULL_TREE;
917 BITMAP_FREE (visited);
919 result = NULL_TREE;
920 switch (DECL_FUNCTION_CODE (callee))
922 case BUILT_IN_STRLEN:
923 if (val[0] && nargs == 1)
925 tree new_val =
926 fold_convert (TREE_TYPE (gimple_call_lhs (stmt)), val[0]);
928 /* If the result is not a valid gimple value, or not a cast
929 of a valid gimple value, then we cannot use the result. */
930 if (is_gimple_val (new_val)
931 || (CONVERT_EXPR_P (new_val)
932 && is_gimple_val (TREE_OPERAND (new_val, 0))))
933 return new_val;
935 break;
937 case BUILT_IN_STRCPY:
938 if (val[1] && is_gimple_val (val[1]) && nargs == 2)
939 result = fold_builtin_strcpy (loc, callee,
940 gimple_call_arg (stmt, 0),
941 gimple_call_arg (stmt, 1),
942 val[1]);
943 break;
945 case BUILT_IN_STRNCPY:
946 if (val[1] && is_gimple_val (val[1]) && nargs == 3)
947 result = fold_builtin_strncpy (loc, callee,
948 gimple_call_arg (stmt, 0),
949 gimple_call_arg (stmt, 1),
950 gimple_call_arg (stmt, 2),
951 val[1]);
952 break;
954 case BUILT_IN_FPUTS:
955 if (nargs == 2)
956 result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0),
957 gimple_call_arg (stmt, 1),
958 ignore, false, val[0]);
959 break;
961 case BUILT_IN_FPUTS_UNLOCKED:
962 if (nargs == 2)
963 result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0),
964 gimple_call_arg (stmt, 1),
965 ignore, true, val[0]);
966 break;
968 case BUILT_IN_MEMCPY_CHK:
969 case BUILT_IN_MEMPCPY_CHK:
970 case BUILT_IN_MEMMOVE_CHK:
971 case BUILT_IN_MEMSET_CHK:
972 if (val[2] && is_gimple_val (val[2]) && nargs == 4)
973 result = fold_builtin_memory_chk (loc, callee,
974 gimple_call_arg (stmt, 0),
975 gimple_call_arg (stmt, 1),
976 gimple_call_arg (stmt, 2),
977 gimple_call_arg (stmt, 3),
978 val[2], ignore,
979 DECL_FUNCTION_CODE (callee));
980 break;
982 case BUILT_IN_STRCPY_CHK:
983 case BUILT_IN_STPCPY_CHK:
984 if (val[1] && is_gimple_val (val[1]) && nargs == 3)
985 result = fold_builtin_stxcpy_chk (loc, callee,
986 gimple_call_arg (stmt, 0),
987 gimple_call_arg (stmt, 1),
988 gimple_call_arg (stmt, 2),
989 val[1], ignore,
990 DECL_FUNCTION_CODE (callee));
991 break;
993 case BUILT_IN_STRNCPY_CHK:
994 case BUILT_IN_STPNCPY_CHK:
995 if (val[2] && is_gimple_val (val[2]) && nargs == 4)
996 result = fold_builtin_stxncpy_chk (loc, gimple_call_arg (stmt, 0),
997 gimple_call_arg (stmt, 1),
998 gimple_call_arg (stmt, 2),
999 gimple_call_arg (stmt, 3),
1000 val[2], ignore,
1001 DECL_FUNCTION_CODE (callee));
1002 break;
1004 case BUILT_IN_SNPRINTF_CHK:
1005 case BUILT_IN_VSNPRINTF_CHK:
1006 if (val[1] && is_gimple_val (val[1]))
1007 result = gimple_fold_builtin_snprintf_chk (stmt, val[1],
1008 DECL_FUNCTION_CODE (callee));
1009 break;
1011 default:
1012 gcc_unreachable ();
1015 if (result && ignore)
1016 result = fold_ignored_result (result);
1017 return result;
1021 /* Return a binfo to be used for devirtualization of calls based on an object
1022 represented by a declaration (i.e. a global or automatically allocated one)
1023 or NULL if it cannot be found or is not safe. CST is expected to be an
1024 ADDR_EXPR of such object or the function will return NULL. Currently it is
1025 safe to use such binfo only if it has no base binfo (i.e. no ancestors)
1026 EXPECTED_TYPE is type of the class virtual belongs to. */
1028 tree
1029 gimple_extract_devirt_binfo_from_cst (tree cst, tree expected_type)
1031 HOST_WIDE_INT offset, size, max_size;
1032 tree base, type, binfo;
1033 bool last_artificial = false;
1035 if (!flag_devirtualize
1036 || TREE_CODE (cst) != ADDR_EXPR
1037 || TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) != RECORD_TYPE)
1038 return NULL_TREE;
1040 cst = TREE_OPERAND (cst, 0);
1041 base = get_ref_base_and_extent (cst, &offset, &size, &max_size);
1042 type = TREE_TYPE (base);
1043 if (!DECL_P (base)
1044 || max_size == -1
1045 || max_size != size
1046 || TREE_CODE (type) != RECORD_TYPE)
1047 return NULL_TREE;
1049 /* Find the sub-object the constant actually refers to and mark whether it is
1050 an artificial one (as opposed to a user-defined one). */
1051 while (true)
1053 HOST_WIDE_INT pos, size;
1054 tree fld;
1056 if (types_same_for_odr (type, expected_type))
1057 break;
1058 if (offset < 0)
1059 return NULL_TREE;
1061 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
1063 if (TREE_CODE (fld) != FIELD_DECL)
1064 continue;
1066 pos = int_bit_position (fld);
1067 size = tree_low_cst (DECL_SIZE (fld), 1);
1068 if (pos <= offset && (pos + size) > offset)
1069 break;
1071 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
1072 return NULL_TREE;
1074 last_artificial = DECL_ARTIFICIAL (fld);
1075 type = TREE_TYPE (fld);
1076 offset -= pos;
1078 /* Artificial sub-objects are ancestors, we do not want to use them for
1079 devirtualization, at least not here. */
1080 if (last_artificial)
1081 return NULL_TREE;
1082 binfo = TYPE_BINFO (type);
1083 if (!binfo || BINFO_N_BASE_BINFOS (binfo) > 0)
1084 return NULL_TREE;
1085 else
1086 return binfo;
1089 /* Attempt to fold a call statement referenced by the statement iterator GSI.
1090 The statement may be replaced by another statement, e.g., if the call
1091 simplifies to a constant value. Return true if any changes were made.
1092 It is assumed that the operands have been previously folded. */
1094 static bool
1095 gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
1097 gimple stmt = gsi_stmt (*gsi);
1098 tree callee;
1099 bool changed = false;
1100 unsigned i;
1102 /* Fold *& in call arguments. */
1103 for (i = 0; i < gimple_call_num_args (stmt); ++i)
1104 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
1106 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
1107 if (tmp)
1109 gimple_call_set_arg (stmt, i, tmp);
1110 changed = true;
1114 /* Check for virtual calls that became direct calls. */
1115 callee = gimple_call_fn (stmt);
1116 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
1118 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
1120 if (dump_file && virtual_method_call_p (callee)
1121 && !possible_polymorphic_call_target_p
1122 (callee, cgraph_get_node (gimple_call_addr_fndecl
1123 (OBJ_TYPE_REF_EXPR (callee)))))
1125 fprintf (dump_file,
1126 "Type inheritnace inconsistent devirtualization of ");
1127 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1128 fprintf (dump_file, " to ");
1129 print_generic_expr (dump_file, callee, TDF_SLIM);
1130 fprintf (dump_file, "\n");
1133 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
1134 changed = true;
1136 else if (virtual_method_call_p (callee))
1138 tree obj = OBJ_TYPE_REF_OBJECT (callee);
1139 tree binfo = gimple_extract_devirt_binfo_from_cst
1140 (obj, obj_type_ref_class (callee));
1141 if (binfo)
1143 HOST_WIDE_INT token
1144 = TREE_INT_CST_LOW (OBJ_TYPE_REF_TOKEN (callee));
1145 tree fndecl = gimple_get_virt_method_for_binfo (token, binfo);
1146 if (fndecl)
1148 #ifdef ENABLE_CHECKING
1149 gcc_assert (possible_polymorphic_call_target_p
1150 (callee, cgraph_get_node (fndecl)));
1152 #endif
1153 gimple_call_set_fndecl (stmt, fndecl);
1154 changed = true;
1160 if (inplace)
1161 return changed;
1163 /* Check for builtins that CCP can handle using information not
1164 available in the generic fold routines. */
1165 callee = gimple_call_fndecl (stmt);
1166 if (callee && DECL_BUILT_IN (callee))
1168 tree result = gimple_fold_builtin (stmt);
1169 if (result)
1171 if (!update_call_from_tree (gsi, result))
1172 gimplify_and_update_call_from_tree (gsi, result);
1173 changed = true;
1175 else if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
1176 changed |= targetm.gimple_fold_builtin (gsi);
1179 return changed;
1182 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
1183 distinguishes both cases. */
1185 static bool
1186 fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace)
1188 bool changed = false;
1189 gimple stmt = gsi_stmt (*gsi);
1190 unsigned i;
1192 /* Fold the main computation performed by the statement. */
1193 switch (gimple_code (stmt))
1195 case GIMPLE_ASSIGN:
1197 unsigned old_num_ops = gimple_num_ops (stmt);
1198 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1199 tree lhs = gimple_assign_lhs (stmt);
1200 tree new_rhs;
1201 /* First canonicalize operand order. This avoids building new
1202 trees if this is the only thing fold would later do. */
1203 if ((commutative_tree_code (subcode)
1204 || commutative_ternary_tree_code (subcode))
1205 && tree_swap_operands_p (gimple_assign_rhs1 (stmt),
1206 gimple_assign_rhs2 (stmt), false))
1208 tree tem = gimple_assign_rhs1 (stmt);
1209 gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt));
1210 gimple_assign_set_rhs2 (stmt, tem);
1211 changed = true;
1213 new_rhs = fold_gimple_assign (gsi);
1214 if (new_rhs
1215 && !useless_type_conversion_p (TREE_TYPE (lhs),
1216 TREE_TYPE (new_rhs)))
1217 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
1218 if (new_rhs
1219 && (!inplace
1220 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
1222 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
1223 changed = true;
1225 break;
1228 case GIMPLE_COND:
1229 changed |= fold_gimple_cond (stmt);
1230 break;
1232 case GIMPLE_CALL:
1233 changed |= gimple_fold_call (gsi, inplace);
1234 break;
1236 case GIMPLE_ASM:
1237 /* Fold *& in asm operands. */
1239 size_t noutputs;
1240 const char **oconstraints;
1241 const char *constraint;
1242 bool allows_mem, allows_reg;
1244 noutputs = gimple_asm_noutputs (stmt);
1245 oconstraints = XALLOCAVEC (const char *, noutputs);
1247 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1249 tree link = gimple_asm_output_op (stmt, i);
1250 tree op = TREE_VALUE (link);
1251 oconstraints[i]
1252 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1253 if (REFERENCE_CLASS_P (op)
1254 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
1256 TREE_VALUE (link) = op;
1257 changed = true;
1260 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
1262 tree link = gimple_asm_input_op (stmt, i);
1263 tree op = TREE_VALUE (link);
1264 constraint
1265 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1266 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1267 oconstraints, &allows_mem, &allows_reg);
1268 if (REFERENCE_CLASS_P (op)
1269 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
1270 != NULL_TREE)
1272 TREE_VALUE (link) = op;
1273 changed = true;
1277 break;
1279 case GIMPLE_DEBUG:
1280 if (gimple_debug_bind_p (stmt))
1282 tree val = gimple_debug_bind_get_value (stmt);
1283 if (val
1284 && REFERENCE_CLASS_P (val))
1286 tree tem = maybe_fold_reference (val, false);
1287 if (tem)
1289 gimple_debug_bind_set_value (stmt, tem);
1290 changed = true;
1293 else if (val
1294 && TREE_CODE (val) == ADDR_EXPR)
1296 tree ref = TREE_OPERAND (val, 0);
1297 tree tem = maybe_fold_reference (ref, false);
1298 if (tem)
1300 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
1301 gimple_debug_bind_set_value (stmt, tem);
1302 changed = true;
1306 break;
1308 default:;
1311 stmt = gsi_stmt (*gsi);
1313 /* Fold *& on the lhs. */
1314 if (gimple_has_lhs (stmt))
1316 tree lhs = gimple_get_lhs (stmt);
1317 if (lhs && REFERENCE_CLASS_P (lhs))
1319 tree new_lhs = maybe_fold_reference (lhs, true);
1320 if (new_lhs)
1322 gimple_set_lhs (stmt, new_lhs);
1323 changed = true;
1328 return changed;
1331 /* Fold the statement pointed to by GSI. In some cases, this function may
1332 replace the whole statement with a new one. Returns true iff folding
1333 makes any changes.
1334 The statement pointed to by GSI should be in valid gimple form but may
1335 be in unfolded state as resulting from for example constant propagation
1336 which can produce *&x = 0. */
1338 bool
1339 fold_stmt (gimple_stmt_iterator *gsi)
1341 return fold_stmt_1 (gsi, false);
1344 /* Perform the minimal folding on statement *GSI. Only operations like
1345 *&x created by constant propagation are handled. The statement cannot
1346 be replaced with a new one. Return true if the statement was
1347 changed, false otherwise.
1348 The statement *GSI should be in valid gimple form but may
1349 be in unfolded state as resulting from for example constant propagation
1350 which can produce *&x = 0. */
1352 bool
1353 fold_stmt_inplace (gimple_stmt_iterator *gsi)
1355 gimple stmt = gsi_stmt (*gsi);
1356 bool changed = fold_stmt_1 (gsi, true);
1357 gcc_assert (gsi_stmt (*gsi) == stmt);
1358 return changed;
1361 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
1362 if EXPR is null or we don't know how.
1363 If non-null, the result always has boolean type. */
1365 static tree
1366 canonicalize_bool (tree expr, bool invert)
1368 if (!expr)
1369 return NULL_TREE;
1370 else if (invert)
1372 if (integer_nonzerop (expr))
1373 return boolean_false_node;
1374 else if (integer_zerop (expr))
1375 return boolean_true_node;
1376 else if (TREE_CODE (expr) == SSA_NAME)
1377 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
1378 build_int_cst (TREE_TYPE (expr), 0));
1379 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison)
1380 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
1381 boolean_type_node,
1382 TREE_OPERAND (expr, 0),
1383 TREE_OPERAND (expr, 1));
1384 else
1385 return NULL_TREE;
1387 else
1389 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
1390 return expr;
1391 if (integer_nonzerop (expr))
1392 return boolean_true_node;
1393 else if (integer_zerop (expr))
1394 return boolean_false_node;
1395 else if (TREE_CODE (expr) == SSA_NAME)
1396 return fold_build2 (NE_EXPR, boolean_type_node, expr,
1397 build_int_cst (TREE_TYPE (expr), 0));
1398 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison)
1399 return fold_build2 (TREE_CODE (expr),
1400 boolean_type_node,
1401 TREE_OPERAND (expr, 0),
1402 TREE_OPERAND (expr, 1));
1403 else
1404 return NULL_TREE;
1408 /* Check to see if a boolean expression EXPR is logically equivalent to the
1409 comparison (OP1 CODE OP2). Check for various identities involving
1410 SSA_NAMEs. */
1412 static bool
1413 same_bool_comparison_p (const_tree expr, enum tree_code code,
1414 const_tree op1, const_tree op2)
1416 gimple s;
1418 /* The obvious case. */
1419 if (TREE_CODE (expr) == code
1420 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
1421 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
1422 return true;
1424 /* Check for comparing (name, name != 0) and the case where expr
1425 is an SSA_NAME with a definition matching the comparison. */
1426 if (TREE_CODE (expr) == SSA_NAME
1427 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
1429 if (operand_equal_p (expr, op1, 0))
1430 return ((code == NE_EXPR && integer_zerop (op2))
1431 || (code == EQ_EXPR && integer_nonzerop (op2)));
1432 s = SSA_NAME_DEF_STMT (expr);
1433 if (is_gimple_assign (s)
1434 && gimple_assign_rhs_code (s) == code
1435 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
1436 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
1437 return true;
1440 /* If op1 is of the form (name != 0) or (name == 0), and the definition
1441 of name is a comparison, recurse. */
1442 if (TREE_CODE (op1) == SSA_NAME
1443 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
1445 s = SSA_NAME_DEF_STMT (op1);
1446 if (is_gimple_assign (s)
1447 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
1449 enum tree_code c = gimple_assign_rhs_code (s);
1450 if ((c == NE_EXPR && integer_zerop (op2))
1451 || (c == EQ_EXPR && integer_nonzerop (op2)))
1452 return same_bool_comparison_p (expr, c,
1453 gimple_assign_rhs1 (s),
1454 gimple_assign_rhs2 (s));
1455 if ((c == EQ_EXPR && integer_zerop (op2))
1456 || (c == NE_EXPR && integer_nonzerop (op2)))
1457 return same_bool_comparison_p (expr,
1458 invert_tree_comparison (c, false),
1459 gimple_assign_rhs1 (s),
1460 gimple_assign_rhs2 (s));
1463 return false;
1466 /* Check to see if two boolean expressions OP1 and OP2 are logically
1467 equivalent. */
1469 static bool
1470 same_bool_result_p (const_tree op1, const_tree op2)
1472 /* Simple cases first. */
1473 if (operand_equal_p (op1, op2, 0))
1474 return true;
1476 /* Check the cases where at least one of the operands is a comparison.
1477 These are a bit smarter than operand_equal_p in that they apply some
1478 identifies on SSA_NAMEs. */
1479 if (TREE_CODE_CLASS (TREE_CODE (op2)) == tcc_comparison
1480 && same_bool_comparison_p (op1, TREE_CODE (op2),
1481 TREE_OPERAND (op2, 0),
1482 TREE_OPERAND (op2, 1)))
1483 return true;
1484 if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_comparison
1485 && same_bool_comparison_p (op2, TREE_CODE (op1),
1486 TREE_OPERAND (op1, 0),
1487 TREE_OPERAND (op1, 1)))
1488 return true;
1490 /* Default case. */
1491 return false;
1494 /* Forward declarations for some mutually recursive functions. */
1496 static tree
1497 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
1498 enum tree_code code2, tree op2a, tree op2b);
1499 static tree
1500 and_var_with_comparison (tree var, bool invert,
1501 enum tree_code code2, tree op2a, tree op2b);
1502 static tree
1503 and_var_with_comparison_1 (gimple stmt,
1504 enum tree_code code2, tree op2a, tree op2b);
1505 static tree
1506 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
1507 enum tree_code code2, tree op2a, tree op2b);
1508 static tree
1509 or_var_with_comparison (tree var, bool invert,
1510 enum tree_code code2, tree op2a, tree op2b);
1511 static tree
1512 or_var_with_comparison_1 (gimple stmt,
1513 enum tree_code code2, tree op2a, tree op2b);
1515 /* Helper function for and_comparisons_1: try to simplify the AND of the
1516 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1517 If INVERT is true, invert the value of the VAR before doing the AND.
1518 Return NULL_EXPR if we can't simplify this to a single expression. */
1520 static tree
1521 and_var_with_comparison (tree var, bool invert,
1522 enum tree_code code2, tree op2a, tree op2b)
1524 tree t;
1525 gimple stmt = SSA_NAME_DEF_STMT (var);
1527 /* We can only deal with variables whose definitions are assignments. */
1528 if (!is_gimple_assign (stmt))
1529 return NULL_TREE;
1531 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
1532 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
1533 Then we only have to consider the simpler non-inverted cases. */
1534 if (invert)
1535 t = or_var_with_comparison_1 (stmt,
1536 invert_tree_comparison (code2, false),
1537 op2a, op2b);
1538 else
1539 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
1540 return canonicalize_bool (t, invert);
1543 /* Try to simplify the AND of the ssa variable defined by the assignment
1544 STMT with the comparison specified by (OP2A CODE2 OP2B).
1545 Return NULL_EXPR if we can't simplify this to a single expression. */
1547 static tree
1548 and_var_with_comparison_1 (gimple stmt,
1549 enum tree_code code2, tree op2a, tree op2b)
1551 tree var = gimple_assign_lhs (stmt);
1552 tree true_test_var = NULL_TREE;
1553 tree false_test_var = NULL_TREE;
1554 enum tree_code innercode = gimple_assign_rhs_code (stmt);
1556 /* Check for identities like (var AND (var == 0)) => false. */
1557 if (TREE_CODE (op2a) == SSA_NAME
1558 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
1560 if ((code2 == NE_EXPR && integer_zerop (op2b))
1561 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
1563 true_test_var = op2a;
1564 if (var == true_test_var)
1565 return var;
1567 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
1568 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
1570 false_test_var = op2a;
1571 if (var == false_test_var)
1572 return boolean_false_node;
1576 /* If the definition is a comparison, recurse on it. */
1577 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
1579 tree t = and_comparisons_1 (innercode,
1580 gimple_assign_rhs1 (stmt),
1581 gimple_assign_rhs2 (stmt),
1582 code2,
1583 op2a,
1584 op2b);
1585 if (t)
1586 return t;
1589 /* If the definition is an AND or OR expression, we may be able to
1590 simplify by reassociating. */
1591 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
1592 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
1594 tree inner1 = gimple_assign_rhs1 (stmt);
1595 tree inner2 = gimple_assign_rhs2 (stmt);
1596 gimple s;
1597 tree t;
1598 tree partial = NULL_TREE;
1599 bool is_and = (innercode == BIT_AND_EXPR);
1601 /* Check for boolean identities that don't require recursive examination
1602 of inner1/inner2:
1603 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
1604 inner1 AND (inner1 OR inner2) => inner1
1605 !inner1 AND (inner1 AND inner2) => false
1606 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
1607 Likewise for similar cases involving inner2. */
1608 if (inner1 == true_test_var)
1609 return (is_and ? var : inner1);
1610 else if (inner2 == true_test_var)
1611 return (is_and ? var : inner2);
1612 else if (inner1 == false_test_var)
1613 return (is_and
1614 ? boolean_false_node
1615 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
1616 else if (inner2 == false_test_var)
1617 return (is_and
1618 ? boolean_false_node
1619 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
1621 /* Next, redistribute/reassociate the AND across the inner tests.
1622 Compute the first partial result, (inner1 AND (op2a code op2b)) */
1623 if (TREE_CODE (inner1) == SSA_NAME
1624 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
1625 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
1626 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
1627 gimple_assign_rhs1 (s),
1628 gimple_assign_rhs2 (s),
1629 code2, op2a, op2b)))
1631 /* Handle the AND case, where we are reassociating:
1632 (inner1 AND inner2) AND (op2a code2 op2b)
1633 => (t AND inner2)
1634 If the partial result t is a constant, we win. Otherwise
1635 continue on to try reassociating with the other inner test. */
1636 if (is_and)
1638 if (integer_onep (t))
1639 return inner2;
1640 else if (integer_zerop (t))
1641 return boolean_false_node;
1644 /* Handle the OR case, where we are redistributing:
1645 (inner1 OR inner2) AND (op2a code2 op2b)
1646 => (t OR (inner2 AND (op2a code2 op2b))) */
1647 else if (integer_onep (t))
1648 return boolean_true_node;
1650 /* Save partial result for later. */
1651 partial = t;
1654 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
1655 if (TREE_CODE (inner2) == SSA_NAME
1656 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
1657 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
1658 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
1659 gimple_assign_rhs1 (s),
1660 gimple_assign_rhs2 (s),
1661 code2, op2a, op2b)))
1663 /* Handle the AND case, where we are reassociating:
1664 (inner1 AND inner2) AND (op2a code2 op2b)
1665 => (inner1 AND t) */
1666 if (is_and)
1668 if (integer_onep (t))
1669 return inner1;
1670 else if (integer_zerop (t))
1671 return boolean_false_node;
1672 /* If both are the same, we can apply the identity
1673 (x AND x) == x. */
1674 else if (partial && same_bool_result_p (t, partial))
1675 return t;
1678 /* Handle the OR case. where we are redistributing:
1679 (inner1 OR inner2) AND (op2a code2 op2b)
1680 => (t OR (inner1 AND (op2a code2 op2b)))
1681 => (t OR partial) */
1682 else
1684 if (integer_onep (t))
1685 return boolean_true_node;
1686 else if (partial)
1688 /* We already got a simplification for the other
1689 operand to the redistributed OR expression. The
1690 interesting case is when at least one is false.
1691 Or, if both are the same, we can apply the identity
1692 (x OR x) == x. */
1693 if (integer_zerop (partial))
1694 return t;
1695 else if (integer_zerop (t))
1696 return partial;
1697 else if (same_bool_result_p (t, partial))
1698 return t;
1703 return NULL_TREE;
1706 /* Try to simplify the AND of two comparisons defined by
1707 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
1708 If this can be done without constructing an intermediate value,
1709 return the resulting tree; otherwise NULL_TREE is returned.
1710 This function is deliberately asymmetric as it recurses on SSA_DEFs
1711 in the first comparison but not the second. */
1713 static tree
1714 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
1715 enum tree_code code2, tree op2a, tree op2b)
1717 tree truth_type = truth_type_for (TREE_TYPE (op1a));
1719 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
1720 if (operand_equal_p (op1a, op2a, 0)
1721 && operand_equal_p (op1b, op2b, 0))
1723 /* Result will be either NULL_TREE, or a combined comparison. */
1724 tree t = combine_comparisons (UNKNOWN_LOCATION,
1725 TRUTH_ANDIF_EXPR, code1, code2,
1726 truth_type, op1a, op1b);
1727 if (t)
1728 return t;
1731 /* Likewise the swapped case of the above. */
1732 if (operand_equal_p (op1a, op2b, 0)
1733 && operand_equal_p (op1b, op2a, 0))
1735 /* Result will be either NULL_TREE, or a combined comparison. */
1736 tree t = combine_comparisons (UNKNOWN_LOCATION,
1737 TRUTH_ANDIF_EXPR, code1,
1738 swap_tree_comparison (code2),
1739 truth_type, op1a, op1b);
1740 if (t)
1741 return t;
1744 /* If both comparisons are of the same value against constants, we might
1745 be able to merge them. */
1746 if (operand_equal_p (op1a, op2a, 0)
1747 && TREE_CODE (op1b) == INTEGER_CST
1748 && TREE_CODE (op2b) == INTEGER_CST)
1750 int cmp = tree_int_cst_compare (op1b, op2b);
1752 /* If we have (op1a == op1b), we should either be able to
1753 return that or FALSE, depending on whether the constant op1b
1754 also satisfies the other comparison against op2b. */
1755 if (code1 == EQ_EXPR)
1757 bool done = true;
1758 bool val;
1759 switch (code2)
1761 case EQ_EXPR: val = (cmp == 0); break;
1762 case NE_EXPR: val = (cmp != 0); break;
1763 case LT_EXPR: val = (cmp < 0); break;
1764 case GT_EXPR: val = (cmp > 0); break;
1765 case LE_EXPR: val = (cmp <= 0); break;
1766 case GE_EXPR: val = (cmp >= 0); break;
1767 default: done = false;
1769 if (done)
1771 if (val)
1772 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1773 else
1774 return boolean_false_node;
1777 /* Likewise if the second comparison is an == comparison. */
1778 else if (code2 == EQ_EXPR)
1780 bool done = true;
1781 bool val;
1782 switch (code1)
1784 case EQ_EXPR: val = (cmp == 0); break;
1785 case NE_EXPR: val = (cmp != 0); break;
1786 case LT_EXPR: val = (cmp > 0); break;
1787 case GT_EXPR: val = (cmp < 0); break;
1788 case LE_EXPR: val = (cmp >= 0); break;
1789 case GE_EXPR: val = (cmp <= 0); break;
1790 default: done = false;
1792 if (done)
1794 if (val)
1795 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1796 else
1797 return boolean_false_node;
1801 /* Same business with inequality tests. */
1802 else if (code1 == NE_EXPR)
1804 bool val;
1805 switch (code2)
1807 case EQ_EXPR: val = (cmp != 0); break;
1808 case NE_EXPR: val = (cmp == 0); break;
1809 case LT_EXPR: val = (cmp >= 0); break;
1810 case GT_EXPR: val = (cmp <= 0); break;
1811 case LE_EXPR: val = (cmp > 0); break;
1812 case GE_EXPR: val = (cmp < 0); break;
1813 default:
1814 val = false;
1816 if (val)
1817 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1819 else if (code2 == NE_EXPR)
1821 bool val;
1822 switch (code1)
1824 case EQ_EXPR: val = (cmp == 0); break;
1825 case NE_EXPR: val = (cmp != 0); break;
1826 case LT_EXPR: val = (cmp <= 0); break;
1827 case GT_EXPR: val = (cmp >= 0); break;
1828 case LE_EXPR: val = (cmp < 0); break;
1829 case GE_EXPR: val = (cmp > 0); break;
1830 default:
1831 val = false;
1833 if (val)
1834 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1837 /* Chose the more restrictive of two < or <= comparisons. */
1838 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
1839 && (code2 == LT_EXPR || code2 == LE_EXPR))
1841 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
1842 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1843 else
1844 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1847 /* Likewise chose the more restrictive of two > or >= comparisons. */
1848 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
1849 && (code2 == GT_EXPR || code2 == GE_EXPR))
1851 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
1852 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1853 else
1854 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1857 /* Check for singleton ranges. */
1858 else if (cmp == 0
1859 && ((code1 == LE_EXPR && code2 == GE_EXPR)
1860 || (code1 == GE_EXPR && code2 == LE_EXPR)))
1861 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
1863 /* Check for disjoint ranges. */
1864 else if (cmp <= 0
1865 && (code1 == LT_EXPR || code1 == LE_EXPR)
1866 && (code2 == GT_EXPR || code2 == GE_EXPR))
1867 return boolean_false_node;
1868 else if (cmp >= 0
1869 && (code1 == GT_EXPR || code1 == GE_EXPR)
1870 && (code2 == LT_EXPR || code2 == LE_EXPR))
1871 return boolean_false_node;
1874 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
1875 NAME's definition is a truth value. See if there are any simplifications
1876 that can be done against the NAME's definition. */
1877 if (TREE_CODE (op1a) == SSA_NAME
1878 && (code1 == NE_EXPR || code1 == EQ_EXPR)
1879 && (integer_zerop (op1b) || integer_onep (op1b)))
1881 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
1882 || (code1 == NE_EXPR && integer_onep (op1b)));
1883 gimple stmt = SSA_NAME_DEF_STMT (op1a);
1884 switch (gimple_code (stmt))
1886 case GIMPLE_ASSIGN:
1887 /* Try to simplify by copy-propagating the definition. */
1888 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
1890 case GIMPLE_PHI:
1891 /* If every argument to the PHI produces the same result when
1892 ANDed with the second comparison, we win.
1893 Do not do this unless the type is bool since we need a bool
1894 result here anyway. */
1895 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
1897 tree result = NULL_TREE;
1898 unsigned i;
1899 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1901 tree arg = gimple_phi_arg_def (stmt, i);
1903 /* If this PHI has itself as an argument, ignore it.
1904 If all the other args produce the same result,
1905 we're still OK. */
1906 if (arg == gimple_phi_result (stmt))
1907 continue;
1908 else if (TREE_CODE (arg) == INTEGER_CST)
1910 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
1912 if (!result)
1913 result = boolean_false_node;
1914 else if (!integer_zerop (result))
1915 return NULL_TREE;
1917 else if (!result)
1918 result = fold_build2 (code2, boolean_type_node,
1919 op2a, op2b);
1920 else if (!same_bool_comparison_p (result,
1921 code2, op2a, op2b))
1922 return NULL_TREE;
1924 else if (TREE_CODE (arg) == SSA_NAME
1925 && !SSA_NAME_IS_DEFAULT_DEF (arg))
1927 tree temp;
1928 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
1929 /* In simple cases we can look through PHI nodes,
1930 but we have to be careful with loops.
1931 See PR49073. */
1932 if (! dom_info_available_p (CDI_DOMINATORS)
1933 || gimple_bb (def_stmt) == gimple_bb (stmt)
1934 || dominated_by_p (CDI_DOMINATORS,
1935 gimple_bb (def_stmt),
1936 gimple_bb (stmt)))
1937 return NULL_TREE;
1938 temp = and_var_with_comparison (arg, invert, code2,
1939 op2a, op2b);
1940 if (!temp)
1941 return NULL_TREE;
1942 else if (!result)
1943 result = temp;
1944 else if (!same_bool_result_p (result, temp))
1945 return NULL_TREE;
1947 else
1948 return NULL_TREE;
1950 return result;
1953 default:
1954 break;
1957 return NULL_TREE;
1960 /* Try to simplify the AND of two comparisons, specified by
1961 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
1962 If this can be simplified to a single expression (without requiring
1963 introducing more SSA variables to hold intermediate values),
1964 return the resulting tree. Otherwise return NULL_TREE.
1965 If the result expression is non-null, it has boolean type. */
1967 tree
1968 maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
1969 enum tree_code code2, tree op2a, tree op2b)
1971 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
1972 if (t)
1973 return t;
1974 else
1975 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
1978 /* Helper function for or_comparisons_1: try to simplify the OR of the
1979 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1980 If INVERT is true, invert the value of VAR before doing the OR.
1981 Return NULL_EXPR if we can't simplify this to a single expression. */
1983 static tree
1984 or_var_with_comparison (tree var, bool invert,
1985 enum tree_code code2, tree op2a, tree op2b)
1987 tree t;
1988 gimple stmt = SSA_NAME_DEF_STMT (var);
1990 /* We can only deal with variables whose definitions are assignments. */
1991 if (!is_gimple_assign (stmt))
1992 return NULL_TREE;
1994 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
1995 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
1996 Then we only have to consider the simpler non-inverted cases. */
1997 if (invert)
1998 t = and_var_with_comparison_1 (stmt,
1999 invert_tree_comparison (code2, false),
2000 op2a, op2b);
2001 else
2002 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
2003 return canonicalize_bool (t, invert);
2006 /* Try to simplify the OR of the ssa variable defined by the assignment
2007 STMT with the comparison specified by (OP2A CODE2 OP2B).
2008 Return NULL_EXPR if we can't simplify this to a single expression. */
2010 static tree
2011 or_var_with_comparison_1 (gimple stmt,
2012 enum tree_code code2, tree op2a, tree op2b)
2014 tree var = gimple_assign_lhs (stmt);
2015 tree true_test_var = NULL_TREE;
2016 tree false_test_var = NULL_TREE;
2017 enum tree_code innercode = gimple_assign_rhs_code (stmt);
2019 /* Check for identities like (var OR (var != 0)) => true . */
2020 if (TREE_CODE (op2a) == SSA_NAME
2021 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
2023 if ((code2 == NE_EXPR && integer_zerop (op2b))
2024 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
2026 true_test_var = op2a;
2027 if (var == true_test_var)
2028 return var;
2030 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
2031 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
2033 false_test_var = op2a;
2034 if (var == false_test_var)
2035 return boolean_true_node;
2039 /* If the definition is a comparison, recurse on it. */
2040 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
2042 tree t = or_comparisons_1 (innercode,
2043 gimple_assign_rhs1 (stmt),
2044 gimple_assign_rhs2 (stmt),
2045 code2,
2046 op2a,
2047 op2b);
2048 if (t)
2049 return t;
2052 /* If the definition is an AND or OR expression, we may be able to
2053 simplify by reassociating. */
2054 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
2055 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
2057 tree inner1 = gimple_assign_rhs1 (stmt);
2058 tree inner2 = gimple_assign_rhs2 (stmt);
2059 gimple s;
2060 tree t;
2061 tree partial = NULL_TREE;
2062 bool is_or = (innercode == BIT_IOR_EXPR);
2064 /* Check for boolean identities that don't require recursive examination
2065 of inner1/inner2:
2066 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
2067 inner1 OR (inner1 AND inner2) => inner1
2068 !inner1 OR (inner1 OR inner2) => true
2069 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
2071 if (inner1 == true_test_var)
2072 return (is_or ? var : inner1);
2073 else if (inner2 == true_test_var)
2074 return (is_or ? var : inner2);
2075 else if (inner1 == false_test_var)
2076 return (is_or
2077 ? boolean_true_node
2078 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
2079 else if (inner2 == false_test_var)
2080 return (is_or
2081 ? boolean_true_node
2082 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
2084 /* Next, redistribute/reassociate the OR across the inner tests.
2085 Compute the first partial result, (inner1 OR (op2a code op2b)) */
2086 if (TREE_CODE (inner1) == SSA_NAME
2087 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
2088 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
2089 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
2090 gimple_assign_rhs1 (s),
2091 gimple_assign_rhs2 (s),
2092 code2, op2a, op2b)))
2094 /* Handle the OR case, where we are reassociating:
2095 (inner1 OR inner2) OR (op2a code2 op2b)
2096 => (t OR inner2)
2097 If the partial result t is a constant, we win. Otherwise
2098 continue on to try reassociating with the other inner test. */
2099 if (is_or)
2101 if (integer_onep (t))
2102 return boolean_true_node;
2103 else if (integer_zerop (t))
2104 return inner2;
2107 /* Handle the AND case, where we are redistributing:
2108 (inner1 AND inner2) OR (op2a code2 op2b)
2109 => (t AND (inner2 OR (op2a code op2b))) */
2110 else if (integer_zerop (t))
2111 return boolean_false_node;
2113 /* Save partial result for later. */
2114 partial = t;
2117 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
2118 if (TREE_CODE (inner2) == SSA_NAME
2119 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
2120 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
2121 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
2122 gimple_assign_rhs1 (s),
2123 gimple_assign_rhs2 (s),
2124 code2, op2a, op2b)))
2126 /* Handle the OR case, where we are reassociating:
2127 (inner1 OR inner2) OR (op2a code2 op2b)
2128 => (inner1 OR t)
2129 => (t OR partial) */
2130 if (is_or)
2132 if (integer_zerop (t))
2133 return inner1;
2134 else if (integer_onep (t))
2135 return boolean_true_node;
2136 /* If both are the same, we can apply the identity
2137 (x OR x) == x. */
2138 else if (partial && same_bool_result_p (t, partial))
2139 return t;
2142 /* Handle the AND case, where we are redistributing:
2143 (inner1 AND inner2) OR (op2a code2 op2b)
2144 => (t AND (inner1 OR (op2a code2 op2b)))
2145 => (t AND partial) */
2146 else
2148 if (integer_zerop (t))
2149 return boolean_false_node;
2150 else if (partial)
2152 /* We already got a simplification for the other
2153 operand to the redistributed AND expression. The
2154 interesting case is when at least one is true.
2155 Or, if both are the same, we can apply the identity
2156 (x AND x) == x. */
2157 if (integer_onep (partial))
2158 return t;
2159 else if (integer_onep (t))
2160 return partial;
2161 else if (same_bool_result_p (t, partial))
2162 return t;
2167 return NULL_TREE;
2170 /* Try to simplify the OR of two comparisons defined by
2171 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
2172 If this can be done without constructing an intermediate value,
2173 return the resulting tree; otherwise NULL_TREE is returned.
2174 This function is deliberately asymmetric as it recurses on SSA_DEFs
2175 in the first comparison but not the second. */
2177 static tree
2178 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
2179 enum tree_code code2, tree op2a, tree op2b)
2181 tree truth_type = truth_type_for (TREE_TYPE (op1a));
2183 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
2184 if (operand_equal_p (op1a, op2a, 0)
2185 && operand_equal_p (op1b, op2b, 0))
2187 /* Result will be either NULL_TREE, or a combined comparison. */
2188 tree t = combine_comparisons (UNKNOWN_LOCATION,
2189 TRUTH_ORIF_EXPR, code1, code2,
2190 truth_type, op1a, op1b);
2191 if (t)
2192 return t;
2195 /* Likewise the swapped case of the above. */
2196 if (operand_equal_p (op1a, op2b, 0)
2197 && operand_equal_p (op1b, op2a, 0))
2199 /* Result will be either NULL_TREE, or a combined comparison. */
2200 tree t = combine_comparisons (UNKNOWN_LOCATION,
2201 TRUTH_ORIF_EXPR, code1,
2202 swap_tree_comparison (code2),
2203 truth_type, op1a, op1b);
2204 if (t)
2205 return t;
2208 /* If both comparisons are of the same value against constants, we might
2209 be able to merge them. */
2210 if (operand_equal_p (op1a, op2a, 0)
2211 && TREE_CODE (op1b) == INTEGER_CST
2212 && TREE_CODE (op2b) == INTEGER_CST)
2214 int cmp = tree_int_cst_compare (op1b, op2b);
2216 /* If we have (op1a != op1b), we should either be able to
2217 return that or TRUE, depending on whether the constant op1b
2218 also satisfies the other comparison against op2b. */
2219 if (code1 == NE_EXPR)
2221 bool done = true;
2222 bool val;
2223 switch (code2)
2225 case EQ_EXPR: val = (cmp == 0); break;
2226 case NE_EXPR: val = (cmp != 0); break;
2227 case LT_EXPR: val = (cmp < 0); break;
2228 case GT_EXPR: val = (cmp > 0); break;
2229 case LE_EXPR: val = (cmp <= 0); break;
2230 case GE_EXPR: val = (cmp >= 0); break;
2231 default: done = false;
2233 if (done)
2235 if (val)
2236 return boolean_true_node;
2237 else
2238 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2241 /* Likewise if the second comparison is a != comparison. */
2242 else if (code2 == NE_EXPR)
2244 bool done = true;
2245 bool val;
2246 switch (code1)
2248 case EQ_EXPR: val = (cmp == 0); break;
2249 case NE_EXPR: val = (cmp != 0); break;
2250 case LT_EXPR: val = (cmp > 0); break;
2251 case GT_EXPR: val = (cmp < 0); break;
2252 case LE_EXPR: val = (cmp >= 0); break;
2253 case GE_EXPR: val = (cmp <= 0); break;
2254 default: done = false;
2256 if (done)
2258 if (val)
2259 return boolean_true_node;
2260 else
2261 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2265 /* See if an equality test is redundant with the other comparison. */
2266 else if (code1 == EQ_EXPR)
2268 bool val;
2269 switch (code2)
2271 case EQ_EXPR: val = (cmp == 0); break;
2272 case NE_EXPR: val = (cmp != 0); break;
2273 case LT_EXPR: val = (cmp < 0); break;
2274 case GT_EXPR: val = (cmp > 0); break;
2275 case LE_EXPR: val = (cmp <= 0); break;
2276 case GE_EXPR: val = (cmp >= 0); break;
2277 default:
2278 val = false;
2280 if (val)
2281 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2283 else if (code2 == EQ_EXPR)
2285 bool val;
2286 switch (code1)
2288 case EQ_EXPR: val = (cmp == 0); break;
2289 case NE_EXPR: val = (cmp != 0); break;
2290 case LT_EXPR: val = (cmp > 0); break;
2291 case GT_EXPR: val = (cmp < 0); break;
2292 case LE_EXPR: val = (cmp >= 0); break;
2293 case GE_EXPR: val = (cmp <= 0); break;
2294 default:
2295 val = false;
2297 if (val)
2298 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2301 /* Chose the less restrictive of two < or <= comparisons. */
2302 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
2303 && (code2 == LT_EXPR || code2 == LE_EXPR))
2305 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2306 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2307 else
2308 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2311 /* Likewise chose the less restrictive of two > or >= comparisons. */
2312 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
2313 && (code2 == GT_EXPR || code2 == GE_EXPR))
2315 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2316 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2317 else
2318 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2321 /* Check for singleton ranges. */
2322 else if (cmp == 0
2323 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2324 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2325 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
2327 /* Check for less/greater pairs that don't restrict the range at all. */
2328 else if (cmp >= 0
2329 && (code1 == LT_EXPR || code1 == LE_EXPR)
2330 && (code2 == GT_EXPR || code2 == GE_EXPR))
2331 return boolean_true_node;
2332 else if (cmp <= 0
2333 && (code1 == GT_EXPR || code1 == GE_EXPR)
2334 && (code2 == LT_EXPR || code2 == LE_EXPR))
2335 return boolean_true_node;
2338 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
2339 NAME's definition is a truth value. See if there are any simplifications
2340 that can be done against the NAME's definition. */
2341 if (TREE_CODE (op1a) == SSA_NAME
2342 && (code1 == NE_EXPR || code1 == EQ_EXPR)
2343 && (integer_zerop (op1b) || integer_onep (op1b)))
2345 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
2346 || (code1 == NE_EXPR && integer_onep (op1b)));
2347 gimple stmt = SSA_NAME_DEF_STMT (op1a);
2348 switch (gimple_code (stmt))
2350 case GIMPLE_ASSIGN:
2351 /* Try to simplify by copy-propagating the definition. */
2352 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
2354 case GIMPLE_PHI:
2355 /* If every argument to the PHI produces the same result when
2356 ORed with the second comparison, we win.
2357 Do not do this unless the type is bool since we need a bool
2358 result here anyway. */
2359 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
2361 tree result = NULL_TREE;
2362 unsigned i;
2363 for (i = 0; i < gimple_phi_num_args (stmt); i++)
2365 tree arg = gimple_phi_arg_def (stmt, i);
2367 /* If this PHI has itself as an argument, ignore it.
2368 If all the other args produce the same result,
2369 we're still OK. */
2370 if (arg == gimple_phi_result (stmt))
2371 continue;
2372 else if (TREE_CODE (arg) == INTEGER_CST)
2374 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
2376 if (!result)
2377 result = boolean_true_node;
2378 else if (!integer_onep (result))
2379 return NULL_TREE;
2381 else if (!result)
2382 result = fold_build2 (code2, boolean_type_node,
2383 op2a, op2b);
2384 else if (!same_bool_comparison_p (result,
2385 code2, op2a, op2b))
2386 return NULL_TREE;
2388 else if (TREE_CODE (arg) == SSA_NAME
2389 && !SSA_NAME_IS_DEFAULT_DEF (arg))
2391 tree temp;
2392 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
2393 /* In simple cases we can look through PHI nodes,
2394 but we have to be careful with loops.
2395 See PR49073. */
2396 if (! dom_info_available_p (CDI_DOMINATORS)
2397 || gimple_bb (def_stmt) == gimple_bb (stmt)
2398 || dominated_by_p (CDI_DOMINATORS,
2399 gimple_bb (def_stmt),
2400 gimple_bb (stmt)))
2401 return NULL_TREE;
2402 temp = or_var_with_comparison (arg, invert, code2,
2403 op2a, op2b);
2404 if (!temp)
2405 return NULL_TREE;
2406 else if (!result)
2407 result = temp;
2408 else if (!same_bool_result_p (result, temp))
2409 return NULL_TREE;
2411 else
2412 return NULL_TREE;
2414 return result;
2417 default:
2418 break;
2421 return NULL_TREE;
2424 /* Try to simplify the OR of two comparisons, specified by
2425 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
2426 If this can be simplified to a single expression (without requiring
2427 introducing more SSA variables to hold intermediate values),
2428 return the resulting tree. Otherwise return NULL_TREE.
2429 If the result expression is non-null, it has boolean type. */
2431 tree
2432 maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
2433 enum tree_code code2, tree op2a, tree op2b)
2435 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
2436 if (t)
2437 return t;
2438 else
2439 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
2443 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2445 Either NULL_TREE, a simplified but non-constant or a constant
2446 is returned.
2448 ??? This should go into a gimple-fold-inline.h file to be eventually
2449 privatized with the single valueize function used in the various TUs
2450 to avoid the indirect function call overhead. */
2452 tree
2453 gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree))
2455 location_t loc = gimple_location (stmt);
2456 switch (gimple_code (stmt))
2458 case GIMPLE_ASSIGN:
2460 enum tree_code subcode = gimple_assign_rhs_code (stmt);
2462 switch (get_gimple_rhs_class (subcode))
2464 case GIMPLE_SINGLE_RHS:
2466 tree rhs = gimple_assign_rhs1 (stmt);
2467 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
2469 if (TREE_CODE (rhs) == SSA_NAME)
2471 /* If the RHS is an SSA_NAME, return its known constant value,
2472 if any. */
2473 return (*valueize) (rhs);
2475 /* Handle propagating invariant addresses into address
2476 operations. */
2477 else if (TREE_CODE (rhs) == ADDR_EXPR
2478 && !is_gimple_min_invariant (rhs))
2480 HOST_WIDE_INT offset = 0;
2481 tree base;
2482 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
2483 &offset,
2484 valueize);
2485 if (base
2486 && (CONSTANT_CLASS_P (base)
2487 || decl_address_invariant_p (base)))
2488 return build_invariant_address (TREE_TYPE (rhs),
2489 base, offset);
2491 else if (TREE_CODE (rhs) == CONSTRUCTOR
2492 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
2493 && (CONSTRUCTOR_NELTS (rhs)
2494 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
2496 unsigned i;
2497 tree val, *vec;
2499 vec = XALLOCAVEC (tree,
2500 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)));
2501 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
2503 val = (*valueize) (val);
2504 if (TREE_CODE (val) == INTEGER_CST
2505 || TREE_CODE (val) == REAL_CST
2506 || TREE_CODE (val) == FIXED_CST)
2507 vec[i] = val;
2508 else
2509 return NULL_TREE;
2512 return build_vector (TREE_TYPE (rhs), vec);
2515 if (kind == tcc_reference)
2517 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
2518 || TREE_CODE (rhs) == REALPART_EXPR
2519 || TREE_CODE (rhs) == IMAGPART_EXPR)
2520 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
2522 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
2523 return fold_unary_loc (EXPR_LOCATION (rhs),
2524 TREE_CODE (rhs),
2525 TREE_TYPE (rhs), val);
2527 else if (TREE_CODE (rhs) == BIT_FIELD_REF
2528 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
2530 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
2531 return fold_ternary_loc (EXPR_LOCATION (rhs),
2532 TREE_CODE (rhs),
2533 TREE_TYPE (rhs), val,
2534 TREE_OPERAND (rhs, 1),
2535 TREE_OPERAND (rhs, 2));
2537 else if (TREE_CODE (rhs) == MEM_REF
2538 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
2540 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
2541 if (TREE_CODE (val) == ADDR_EXPR
2542 && is_gimple_min_invariant (val))
2544 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
2545 unshare_expr (val),
2546 TREE_OPERAND (rhs, 1));
2547 if (tem)
2548 rhs = tem;
2551 return fold_const_aggregate_ref_1 (rhs, valueize);
2553 else if (kind == tcc_declaration)
2554 return get_symbol_constant_value (rhs);
2555 return rhs;
2558 case GIMPLE_UNARY_RHS:
2560 /* Handle unary operators that can appear in GIMPLE form.
2561 Note that we know the single operand must be a constant,
2562 so this should almost always return a simplified RHS. */
2563 tree lhs = gimple_assign_lhs (stmt);
2564 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
2566 /* Conversions are useless for CCP purposes if they are
2567 value-preserving. Thus the restrictions that
2568 useless_type_conversion_p places for restrict qualification
2569 of pointer types should not apply here.
2570 Substitution later will only substitute to allowed places. */
2571 if (CONVERT_EXPR_CODE_P (subcode)
2572 && POINTER_TYPE_P (TREE_TYPE (lhs))
2573 && POINTER_TYPE_P (TREE_TYPE (op0))
2574 && TYPE_ADDR_SPACE (TREE_TYPE (lhs))
2575 == TYPE_ADDR_SPACE (TREE_TYPE (op0))
2576 && TYPE_MODE (TREE_TYPE (lhs))
2577 == TYPE_MODE (TREE_TYPE (op0)))
2578 return op0;
2580 return
2581 fold_unary_ignore_overflow_loc (loc, subcode,
2582 gimple_expr_type (stmt), op0);
2585 case GIMPLE_BINARY_RHS:
2587 /* Handle binary operators that can appear in GIMPLE form. */
2588 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
2589 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
2591 /* Translate &x + CST into an invariant form suitable for
2592 further propagation. */
2593 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
2594 && TREE_CODE (op0) == ADDR_EXPR
2595 && TREE_CODE (op1) == INTEGER_CST)
2597 tree off = fold_convert (ptr_type_node, op1);
2598 return build_fold_addr_expr_loc
2599 (loc,
2600 fold_build2 (MEM_REF,
2601 TREE_TYPE (TREE_TYPE (op0)),
2602 unshare_expr (op0), off));
2605 return fold_binary_loc (loc, subcode,
2606 gimple_expr_type (stmt), op0, op1);
2609 case GIMPLE_TERNARY_RHS:
2611 /* Handle ternary operators that can appear in GIMPLE form. */
2612 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
2613 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
2614 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
2616 /* Fold embedded expressions in ternary codes. */
2617 if ((subcode == COND_EXPR
2618 || subcode == VEC_COND_EXPR)
2619 && COMPARISON_CLASS_P (op0))
2621 tree op00 = (*valueize) (TREE_OPERAND (op0, 0));
2622 tree op01 = (*valueize) (TREE_OPERAND (op0, 1));
2623 tree tem = fold_binary_loc (loc, TREE_CODE (op0),
2624 TREE_TYPE (op0), op00, op01);
2625 if (tem)
2626 op0 = tem;
2629 return fold_ternary_loc (loc, subcode,
2630 gimple_expr_type (stmt), op0, op1, op2);
2633 default:
2634 gcc_unreachable ();
2638 case GIMPLE_CALL:
2640 tree fn;
2642 if (gimple_call_internal_p (stmt))
2643 /* No folding yet for these functions. */
2644 return NULL_TREE;
2646 fn = (*valueize) (gimple_call_fn (stmt));
2647 if (TREE_CODE (fn) == ADDR_EXPR
2648 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
2649 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
2651 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
2652 tree call, retval;
2653 unsigned i;
2654 for (i = 0; i < gimple_call_num_args (stmt); ++i)
2655 args[i] = (*valueize) (gimple_call_arg (stmt, i));
2656 call = build_call_array_loc (loc,
2657 gimple_call_return_type (stmt),
2658 fn, gimple_call_num_args (stmt), args);
2659 retval = fold_call_expr (EXPR_LOCATION (call), call, false);
2660 if (retval)
2661 /* fold_call_expr wraps the result inside a NOP_EXPR. */
2662 STRIP_NOPS (retval);
2663 return retval;
2665 return NULL_TREE;
2668 default:
2669 return NULL_TREE;
2673 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2674 Returns NULL_TREE if folding to a constant is not possible, otherwise
2675 returns a constant according to is_gimple_min_invariant. */
2677 tree
2678 gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree))
2680 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
2681 if (res && is_gimple_min_invariant (res))
2682 return res;
2683 return NULL_TREE;
2687 /* The following set of functions are supposed to fold references using
2688 their constant initializers. */
2690 static tree fold_ctor_reference (tree type, tree ctor,
2691 unsigned HOST_WIDE_INT offset,
2692 unsigned HOST_WIDE_INT size, tree);
2694 /* See if we can find constructor defining value of BASE.
2695 When we know the consructor with constant offset (such as
2696 base is array[40] and we do know constructor of array), then
2697 BIT_OFFSET is adjusted accordingly.
2699 As a special case, return error_mark_node when constructor
2700 is not explicitly available, but it is known to be zero
2701 such as 'static const int a;'. */
2702 static tree
2703 get_base_constructor (tree base, HOST_WIDE_INT *bit_offset,
2704 tree (*valueize)(tree))
2706 HOST_WIDE_INT bit_offset2, size, max_size;
2707 if (TREE_CODE (base) == MEM_REF)
2709 if (!integer_zerop (TREE_OPERAND (base, 1)))
2711 if (!host_integerp (TREE_OPERAND (base, 1), 0))
2712 return NULL_TREE;
2713 *bit_offset += (mem_ref_offset (base).low
2714 * BITS_PER_UNIT);
2717 if (valueize
2718 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2719 base = valueize (TREE_OPERAND (base, 0));
2720 if (!base || TREE_CODE (base) != ADDR_EXPR)
2721 return NULL_TREE;
2722 base = TREE_OPERAND (base, 0);
2725 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
2726 DECL_INITIAL. If BASE is a nested reference into another
2727 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
2728 the inner reference. */
2729 switch (TREE_CODE (base))
2731 case VAR_DECL:
2732 case CONST_DECL:
2734 tree init = ctor_for_folding (base);
2736 /* Our semantic is exact opposite of ctor_for_folding;
2737 NULL means unknown, while error_mark_node is 0. */
2738 if (init == error_mark_node)
2739 return NULL_TREE;
2740 if (!init)
2741 return error_mark_node;
2742 return init;
2745 case ARRAY_REF:
2746 case COMPONENT_REF:
2747 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size);
2748 if (max_size == -1 || size != max_size)
2749 return NULL_TREE;
2750 *bit_offset += bit_offset2;
2751 return get_base_constructor (base, bit_offset, valueize);
2753 case STRING_CST:
2754 case CONSTRUCTOR:
2755 return base;
2757 default:
2758 return NULL_TREE;
2762 /* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE
2763 to the memory at bit OFFSET.
2765 We do only simple job of folding byte accesses. */
2767 static tree
2768 fold_string_cst_ctor_reference (tree type, tree ctor,
2769 unsigned HOST_WIDE_INT offset,
2770 unsigned HOST_WIDE_INT size)
2772 if (INTEGRAL_TYPE_P (type)
2773 && (TYPE_MODE (type)
2774 == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
2775 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
2776 == MODE_INT)
2777 && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1
2778 && size == BITS_PER_UNIT
2779 && !(offset % BITS_PER_UNIT))
2781 offset /= BITS_PER_UNIT;
2782 if (offset < (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (ctor))
2783 return build_int_cst_type (type, (TREE_STRING_POINTER (ctor)
2784 [offset]));
2785 /* Folding
2786 const char a[20]="hello";
2787 return a[10];
2789 might lead to offset greater than string length. In this case we
2790 know value is either initialized to 0 or out of bounds. Return 0
2791 in both cases. */
2792 return build_zero_cst (type);
2794 return NULL_TREE;
2797 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
2798 SIZE to the memory at bit OFFSET. */
2800 static tree
2801 fold_array_ctor_reference (tree type, tree ctor,
2802 unsigned HOST_WIDE_INT offset,
2803 unsigned HOST_WIDE_INT size,
2804 tree from_decl)
2806 unsigned HOST_WIDE_INT cnt;
2807 tree cfield, cval;
2808 double_int low_bound, elt_size;
2809 double_int index, max_index;
2810 double_int access_index;
2811 tree domain_type = NULL_TREE, index_type = NULL_TREE;
2812 HOST_WIDE_INT inner_offset;
2814 /* Compute low bound and elt size. */
2815 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
2816 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
2817 if (domain_type && TYPE_MIN_VALUE (domain_type))
2819 /* Static constructors for variably sized objects makes no sense. */
2820 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
2821 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
2822 low_bound = tree_to_double_int (TYPE_MIN_VALUE (domain_type));
2824 else
2825 low_bound = double_int_zero;
2826 /* Static constructors for variably sized objects makes no sense. */
2827 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))))
2828 == INTEGER_CST);
2829 elt_size =
2830 tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
2833 /* We can handle only constantly sized accesses that are known to not
2834 be larger than size of array element. */
2835 if (!TYPE_SIZE_UNIT (type)
2836 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
2837 || elt_size.slt (tree_to_double_int (TYPE_SIZE_UNIT (type))))
2838 return NULL_TREE;
2840 /* Compute the array index we look for. */
2841 access_index = double_int::from_uhwi (offset / BITS_PER_UNIT)
2842 .udiv (elt_size, TRUNC_DIV_EXPR);
2843 access_index += low_bound;
2844 if (index_type)
2845 access_index = access_index.ext (TYPE_PRECISION (index_type),
2846 TYPE_UNSIGNED (index_type));
2848 /* And offset within the access. */
2849 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
2851 /* See if the array field is large enough to span whole access. We do not
2852 care to fold accesses spanning multiple array indexes. */
2853 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
2854 return NULL_TREE;
2856 index = low_bound - double_int_one;
2857 if (index_type)
2858 index = index.ext (TYPE_PRECISION (index_type), TYPE_UNSIGNED (index_type));
2860 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
2862 /* Array constructor might explicitely set index, or specify range
2863 or leave index NULL meaning that it is next index after previous
2864 one. */
2865 if (cfield)
2867 if (TREE_CODE (cfield) == INTEGER_CST)
2868 max_index = index = tree_to_double_int (cfield);
2869 else
2871 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
2872 index = tree_to_double_int (TREE_OPERAND (cfield, 0));
2873 max_index = tree_to_double_int (TREE_OPERAND (cfield, 1));
2876 else
2878 index += double_int_one;
2879 if (index_type)
2880 index = index.ext (TYPE_PRECISION (index_type),
2881 TYPE_UNSIGNED (index_type));
2882 max_index = index;
2885 /* Do we have match? */
2886 if (access_index.cmp (index, 1) >= 0
2887 && access_index.cmp (max_index, 1) <= 0)
2888 return fold_ctor_reference (type, cval, inner_offset, size,
2889 from_decl);
2891 /* When memory is not explicitely mentioned in constructor,
2892 it is 0 (or out of range). */
2893 return build_zero_cst (type);
2896 /* CTOR is CONSTRUCTOR of an aggregate or vector.
2897 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
2899 static tree
2900 fold_nonarray_ctor_reference (tree type, tree ctor,
2901 unsigned HOST_WIDE_INT offset,
2902 unsigned HOST_WIDE_INT size,
2903 tree from_decl)
2905 unsigned HOST_WIDE_INT cnt;
2906 tree cfield, cval;
2908 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
2909 cval)
2911 tree byte_offset = DECL_FIELD_OFFSET (cfield);
2912 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
2913 tree field_size = DECL_SIZE (cfield);
2914 double_int bitoffset;
2915 double_int byte_offset_cst = tree_to_double_int (byte_offset);
2916 double_int bits_per_unit_cst = double_int::from_uhwi (BITS_PER_UNIT);
2917 double_int bitoffset_end, access_end;
2919 /* Variable sized objects in static constructors makes no sense,
2920 but field_size can be NULL for flexible array members. */
2921 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
2922 && TREE_CODE (byte_offset) == INTEGER_CST
2923 && (field_size != NULL_TREE
2924 ? TREE_CODE (field_size) == INTEGER_CST
2925 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
2927 /* Compute bit offset of the field. */
2928 bitoffset = tree_to_double_int (field_offset)
2929 + byte_offset_cst * bits_per_unit_cst;
2930 /* Compute bit offset where the field ends. */
2931 if (field_size != NULL_TREE)
2932 bitoffset_end = bitoffset + tree_to_double_int (field_size);
2933 else
2934 bitoffset_end = double_int_zero;
2936 access_end = double_int::from_uhwi (offset)
2937 + double_int::from_uhwi (size);
2939 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
2940 [BITOFFSET, BITOFFSET_END)? */
2941 if (access_end.cmp (bitoffset, 0) > 0
2942 && (field_size == NULL_TREE
2943 || double_int::from_uhwi (offset).slt (bitoffset_end)))
2945 double_int inner_offset = double_int::from_uhwi (offset) - bitoffset;
2946 /* We do have overlap. Now see if field is large enough to
2947 cover the access. Give up for accesses spanning multiple
2948 fields. */
2949 if (access_end.cmp (bitoffset_end, 0) > 0)
2950 return NULL_TREE;
2951 if (double_int::from_uhwi (offset).slt (bitoffset))
2952 return NULL_TREE;
2953 return fold_ctor_reference (type, cval,
2954 inner_offset.to_uhwi (), size,
2955 from_decl);
2958 /* When memory is not explicitely mentioned in constructor, it is 0. */
2959 return build_zero_cst (type);
2962 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
2963 to the memory at bit OFFSET. */
2965 static tree
2966 fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset,
2967 unsigned HOST_WIDE_INT size, tree from_decl)
2969 tree ret;
2971 /* We found the field with exact match. */
2972 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
2973 && !offset)
2974 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
2976 /* We are at the end of walk, see if we can view convert the
2977 result. */
2978 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
2979 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
2980 && operand_equal_p (TYPE_SIZE (type),
2981 TYPE_SIZE (TREE_TYPE (ctor)), 0))
2983 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
2984 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
2985 if (ret)
2986 STRIP_NOPS (ret);
2987 return ret;
2989 if (TREE_CODE (ctor) == STRING_CST)
2990 return fold_string_cst_ctor_reference (type, ctor, offset, size);
2991 if (TREE_CODE (ctor) == CONSTRUCTOR)
2994 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
2995 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
2996 return fold_array_ctor_reference (type, ctor, offset, size,
2997 from_decl);
2998 else
2999 return fold_nonarray_ctor_reference (type, ctor, offset, size,
3000 from_decl);
3003 return NULL_TREE;
3006 /* Return the tree representing the element referenced by T if T is an
3007 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
3008 names using VALUEIZE. Return NULL_TREE otherwise. */
3010 tree
3011 fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
3013 tree ctor, idx, base;
3014 HOST_WIDE_INT offset, size, max_size;
3015 tree tem;
3017 if (TREE_THIS_VOLATILE (t))
3018 return NULL_TREE;
3020 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
3021 return get_symbol_constant_value (t);
3023 tem = fold_read_from_constant_string (t);
3024 if (tem)
3025 return tem;
3027 switch (TREE_CODE (t))
3029 case ARRAY_REF:
3030 case ARRAY_RANGE_REF:
3031 /* Constant indexes are handled well by get_base_constructor.
3032 Only special case variable offsets.
3033 FIXME: This code can't handle nested references with variable indexes
3034 (they will be handled only by iteration of ccp). Perhaps we can bring
3035 get_ref_base_and_extent here and make it use a valueize callback. */
3036 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
3037 && valueize
3038 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
3039 && TREE_CODE (idx) == INTEGER_CST)
3041 tree low_bound, unit_size;
3042 double_int doffset;
3044 /* If the resulting bit-offset is constant, track it. */
3045 if ((low_bound = array_ref_low_bound (t),
3046 TREE_CODE (low_bound) == INTEGER_CST)
3047 && (unit_size = array_ref_element_size (t),
3048 host_integerp (unit_size, 1))
3049 && (doffset = (TREE_INT_CST (idx) - TREE_INT_CST (low_bound))
3050 .sext (TYPE_PRECISION (TREE_TYPE (idx))),
3051 doffset.fits_shwi ()))
3053 offset = doffset.to_shwi ();
3054 offset *= TREE_INT_CST_LOW (unit_size);
3055 offset *= BITS_PER_UNIT;
3057 base = TREE_OPERAND (t, 0);
3058 ctor = get_base_constructor (base, &offset, valueize);
3059 /* Empty constructor. Always fold to 0. */
3060 if (ctor == error_mark_node)
3061 return build_zero_cst (TREE_TYPE (t));
3062 /* Out of bound array access. Value is undefined,
3063 but don't fold. */
3064 if (offset < 0)
3065 return NULL_TREE;
3066 /* We can not determine ctor. */
3067 if (!ctor)
3068 return NULL_TREE;
3069 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
3070 TREE_INT_CST_LOW (unit_size)
3071 * BITS_PER_UNIT,
3072 base);
3075 /* Fallthru. */
3077 case COMPONENT_REF:
3078 case BIT_FIELD_REF:
3079 case TARGET_MEM_REF:
3080 case MEM_REF:
3081 base = get_ref_base_and_extent (t, &offset, &size, &max_size);
3082 ctor = get_base_constructor (base, &offset, valueize);
3084 /* Empty constructor. Always fold to 0. */
3085 if (ctor == error_mark_node)
3086 return build_zero_cst (TREE_TYPE (t));
3087 /* We do not know precise address. */
3088 if (max_size == -1 || max_size != size)
3089 return NULL_TREE;
3090 /* We can not determine ctor. */
3091 if (!ctor)
3092 return NULL_TREE;
3094 /* Out of bound array access. Value is undefined, but don't fold. */
3095 if (offset < 0)
3096 return NULL_TREE;
3098 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
3099 base);
3101 case REALPART_EXPR:
3102 case IMAGPART_EXPR:
3104 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
3105 if (c && TREE_CODE (c) == COMPLEX_CST)
3106 return fold_build1_loc (EXPR_LOCATION (t),
3107 TREE_CODE (t), TREE_TYPE (t), c);
3108 break;
3111 default:
3112 break;
3115 return NULL_TREE;
3118 tree
3119 fold_const_aggregate_ref (tree t)
3121 return fold_const_aggregate_ref_1 (t, NULL);
3124 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
3125 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
3126 KNOWN_BINFO carries the binfo describing the true type of
3127 OBJ_TYPE_REF_OBJECT(REF). */
3129 tree
3130 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo)
3132 unsigned HOST_WIDE_INT offset, size;
3133 tree v, fn, vtable, init;
3135 vtable = v = BINFO_VTABLE (known_binfo);
3136 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
3137 if (!v)
3138 return NULL_TREE;
3140 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
3142 offset = tree_low_cst (TREE_OPERAND (v, 1), 1) * BITS_PER_UNIT;
3143 v = TREE_OPERAND (v, 0);
3145 else
3146 offset = 0;
3148 if (TREE_CODE (v) != ADDR_EXPR)
3149 return NULL_TREE;
3150 v = TREE_OPERAND (v, 0);
3152 if (TREE_CODE (v) != VAR_DECL
3153 || !DECL_VIRTUAL_P (v))
3154 return NULL_TREE;
3155 init = ctor_for_folding (v);
3157 /* The virtual tables should always be born with constructors.
3158 and we always should assume that they are avaialble for
3159 folding. At the moment we do not stream them in all cases,
3160 but it should never happen that ctor seem unreachable. */
3161 gcc_assert (init);
3162 if (init == error_mark_node)
3164 gcc_assert (in_lto_p);
3165 return NULL_TREE;
3167 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
3168 size = tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))), 1);
3169 offset += token * size;
3170 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
3171 offset, size, v);
3172 if (!fn || integer_zerop (fn))
3173 return NULL_TREE;
3174 gcc_assert (TREE_CODE (fn) == ADDR_EXPR
3175 || TREE_CODE (fn) == FDESC_EXPR);
3176 fn = TREE_OPERAND (fn, 0);
3177 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
3179 /* When cgraph node is missing and function is not public, we cannot
3180 devirtualize. This can happen in WHOPR when the actual method
3181 ends up in other partition, because we found devirtualization
3182 possibility too late. */
3183 if (!can_refer_decl_in_current_unit_p (fn, vtable))
3184 return NULL_TREE;
3186 /* Make sure we create a cgraph node for functions we'll reference.
3187 They can be non-existent if the reference comes from an entry
3188 of an external vtable for example. */
3189 cgraph_get_create_node (fn);
3191 return fn;
3194 /* Return true iff VAL is a gimple expression that is known to be
3195 non-negative. Restricted to floating-point inputs. */
3197 bool
3198 gimple_val_nonnegative_real_p (tree val)
3200 gimple def_stmt;
3202 gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)));
3204 /* Use existing logic for non-gimple trees. */
3205 if (tree_expr_nonnegative_p (val))
3206 return true;
3208 if (TREE_CODE (val) != SSA_NAME)
3209 return false;
3211 /* Currently we look only at the immediately defining statement
3212 to make this determination, since recursion on defining
3213 statements of operands can lead to quadratic behavior in the
3214 worst case. This is expected to catch almost all occurrences
3215 in practice. It would be possible to implement limited-depth
3216 recursion if important cases are lost. Alternatively, passes
3217 that need this information (such as the pow/powi lowering code
3218 in the cse_sincos pass) could be revised to provide it through
3219 dataflow propagation. */
3221 def_stmt = SSA_NAME_DEF_STMT (val);
3223 if (is_gimple_assign (def_stmt))
3225 tree op0, op1;
3227 /* See fold-const.c:tree_expr_nonnegative_p for additional
3228 cases that could be handled with recursion. */
3230 switch (gimple_assign_rhs_code (def_stmt))
3232 case ABS_EXPR:
3233 /* Always true for floating-point operands. */
3234 return true;
3236 case MULT_EXPR:
3237 /* True if the two operands are identical (since we are
3238 restricted to floating-point inputs). */
3239 op0 = gimple_assign_rhs1 (def_stmt);
3240 op1 = gimple_assign_rhs2 (def_stmt);
3242 if (op0 == op1
3243 || operand_equal_p (op0, op1, 0))
3244 return true;
3246 default:
3247 return false;
3250 else if (is_gimple_call (def_stmt))
3252 tree fndecl = gimple_call_fndecl (def_stmt);
3253 if (fndecl
3254 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3256 tree arg1;
3258 switch (DECL_FUNCTION_CODE (fndecl))
3260 CASE_FLT_FN (BUILT_IN_ACOS):
3261 CASE_FLT_FN (BUILT_IN_ACOSH):
3262 CASE_FLT_FN (BUILT_IN_CABS):
3263 CASE_FLT_FN (BUILT_IN_COSH):
3264 CASE_FLT_FN (BUILT_IN_ERFC):
3265 CASE_FLT_FN (BUILT_IN_EXP):
3266 CASE_FLT_FN (BUILT_IN_EXP10):
3267 CASE_FLT_FN (BUILT_IN_EXP2):
3268 CASE_FLT_FN (BUILT_IN_FABS):
3269 CASE_FLT_FN (BUILT_IN_FDIM):
3270 CASE_FLT_FN (BUILT_IN_HYPOT):
3271 CASE_FLT_FN (BUILT_IN_POW10):
3272 return true;
3274 CASE_FLT_FN (BUILT_IN_SQRT):
3275 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
3276 nonnegative inputs. */
3277 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val))))
3278 return true;
3280 break;
3282 CASE_FLT_FN (BUILT_IN_POWI):
3283 /* True if the second argument is an even integer. */
3284 arg1 = gimple_call_arg (def_stmt, 1);
3286 if (TREE_CODE (arg1) == INTEGER_CST
3287 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
3288 return true;
3290 break;
3292 CASE_FLT_FN (BUILT_IN_POW):
3293 /* True if the second argument is an even integer-valued
3294 real. */
3295 arg1 = gimple_call_arg (def_stmt, 1);
3297 if (TREE_CODE (arg1) == REAL_CST)
3299 REAL_VALUE_TYPE c;
3300 HOST_WIDE_INT n;
3302 c = TREE_REAL_CST (arg1);
3303 n = real_to_integer (&c);
3305 if ((n & 1) == 0)
3307 REAL_VALUE_TYPE cint;
3308 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3309 if (real_identical (&c, &cint))
3310 return true;
3314 break;
3316 default:
3317 return false;
3322 return false;
3325 /* Given a pointer value OP0, return a simplified version of an
3326 indirection through OP0, or NULL_TREE if no simplification is
3327 possible. Note that the resulting type may be different from
3328 the type pointed to in the sense that it is still compatible
3329 from the langhooks point of view. */
3331 tree
3332 gimple_fold_indirect_ref (tree t)
3334 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
3335 tree sub = t;
3336 tree subtype;
3338 STRIP_NOPS (sub);
3339 subtype = TREE_TYPE (sub);
3340 if (!POINTER_TYPE_P (subtype))
3341 return NULL_TREE;
3343 if (TREE_CODE (sub) == ADDR_EXPR)
3345 tree op = TREE_OPERAND (sub, 0);
3346 tree optype = TREE_TYPE (op);
3347 /* *&p => p */
3348 if (useless_type_conversion_p (type, optype))
3349 return op;
3351 /* *(foo *)&fooarray => fooarray[0] */
3352 if (TREE_CODE (optype) == ARRAY_TYPE
3353 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
3354 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3356 tree type_domain = TYPE_DOMAIN (optype);
3357 tree min_val = size_zero_node;
3358 if (type_domain && TYPE_MIN_VALUE (type_domain))
3359 min_val = TYPE_MIN_VALUE (type_domain);
3360 if (TREE_CODE (min_val) == INTEGER_CST)
3361 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3363 /* *(foo *)&complexfoo => __real__ complexfoo */
3364 else if (TREE_CODE (optype) == COMPLEX_TYPE
3365 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3366 return fold_build1 (REALPART_EXPR, type, op);
3367 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3368 else if (TREE_CODE (optype) == VECTOR_TYPE
3369 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3371 tree part_width = TYPE_SIZE (type);
3372 tree index = bitsize_int (0);
3373 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
3377 /* *(p + CST) -> ... */
3378 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
3379 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
3381 tree addr = TREE_OPERAND (sub, 0);
3382 tree off = TREE_OPERAND (sub, 1);
3383 tree addrtype;
3385 STRIP_NOPS (addr);
3386 addrtype = TREE_TYPE (addr);
3388 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
3389 if (TREE_CODE (addr) == ADDR_EXPR
3390 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
3391 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
3392 && host_integerp (off, 1))
3394 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
3395 tree part_width = TYPE_SIZE (type);
3396 unsigned HOST_WIDE_INT part_widthi
3397 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
3398 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
3399 tree index = bitsize_int (indexi);
3400 if (offset / part_widthi
3401 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
3402 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
3403 part_width, index);
3406 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
3407 if (TREE_CODE (addr) == ADDR_EXPR
3408 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
3409 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
3411 tree size = TYPE_SIZE_UNIT (type);
3412 if (tree_int_cst_equal (size, off))
3413 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
3416 /* *(p + CST) -> MEM_REF <p, CST>. */
3417 if (TREE_CODE (addr) != ADDR_EXPR
3418 || DECL_P (TREE_OPERAND (addr, 0)))
3419 return fold_build2 (MEM_REF, type,
3420 addr,
3421 build_int_cst_wide (ptype,
3422 TREE_INT_CST_LOW (off),
3423 TREE_INT_CST_HIGH (off)));
3426 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3427 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3428 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
3429 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3431 tree type_domain;
3432 tree min_val = size_zero_node;
3433 tree osub = sub;
3434 sub = gimple_fold_indirect_ref (sub);
3435 if (! sub)
3436 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3437 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3438 if (type_domain && TYPE_MIN_VALUE (type_domain))
3439 min_val = TYPE_MIN_VALUE (type_domain);
3440 if (TREE_CODE (min_val) == INTEGER_CST)
3441 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3444 return NULL_TREE;