* include/bits/regex_automaton.h (__detail::_State): Split
[official-gcc.git] / gcc / gimple-fold.c
bloba4be3aae4d554c183ee9ef6190b3fc6d386e91d1
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 "bitmap.h"
30 #include "gimple.h"
31 #include "gimple-ssa.h"
32 #include "tree-ssanames.h"
33 #include "tree-into-ssa.h"
34 #include "tree-dfa.h"
35 #include "tree-ssa.h"
36 #include "tree-ssa-propagate.h"
37 #include "target.h"
38 #include "ipa-utils.h"
39 #include "gimple-pretty-print.h"
40 #include "tree-ssa-address.h"
42 /* Return true when DECL can be referenced from current unit.
43 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
44 We can get declarations that are not possible to reference for various
45 reasons:
47 1) When analyzing C++ virtual tables.
48 C++ virtual tables do have known constructors even
49 when they are keyed to other compilation unit.
50 Those tables can contain pointers to methods and vars
51 in other units. Those methods have both STATIC and EXTERNAL
52 set.
53 2) In WHOPR mode devirtualization might lead to reference
54 to method that was partitioned elsehwere.
55 In this case we have static VAR_DECL or FUNCTION_DECL
56 that has no corresponding callgraph/varpool node
57 declaring the body.
58 3) COMDAT functions referred by external vtables that
59 we devirtualize only during final compilation stage.
60 At this time we already decided that we will not output
61 the function body and thus we can't reference the symbol
62 directly. */
64 static bool
65 can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
67 struct varpool_node *vnode;
68 struct cgraph_node *node;
69 symtab_node *snode;
71 if (DECL_ABSTRACT (decl))
72 return false;
74 /* We are concerned only about static/external vars and functions. */
75 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
76 || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL))
77 return true;
79 /* Static objects can be referred only if they was not optimized out yet. */
80 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
82 snode = symtab_get_node (decl);
83 if (!snode)
84 return false;
85 node = dyn_cast <cgraph_node> (snode);
86 return !node || !node->global.inlined_to;
89 /* We will later output the initializer, so we can refer to it.
90 So we are concerned only when DECL comes from initializer of
91 external var. */
92 if (!from_decl
93 || TREE_CODE (from_decl) != VAR_DECL
94 || !DECL_EXTERNAL (from_decl)
95 || (flag_ltrans
96 && symtab_get_node (from_decl)->in_other_partition))
97 return true;
98 /* We are folding reference from external vtable. The vtable may reffer
99 to a symbol keyed to other compilation unit. The other compilation
100 unit may be in separate DSO and the symbol may be hidden. */
101 if (DECL_VISIBILITY_SPECIFIED (decl)
102 && DECL_EXTERNAL (decl)
103 && (!(snode = symtab_get_node (decl)) || !snode->in_other_partition))
104 return false;
105 /* When function is public, we always can introduce new reference.
106 Exception are the COMDAT functions where introducing a direct
107 reference imply need to include function body in the curren tunit. */
108 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
109 return true;
110 /* We are not at ltrans stage; so don't worry about WHOPR.
111 Also when still gimplifying all referred comdat functions will be
112 produced.
114 As observed in PR20991 for already optimized out comdat virtual functions
115 it may be tempting to not necessarily give up because the copy will be
116 output elsewhere when corresponding vtable is output.
117 This is however not possible - ABI specify that COMDATs are output in
118 units where they are used and when the other unit was compiled with LTO
119 it is possible that vtable was kept public while the function itself
120 was privatized. */
121 if (!flag_ltrans && (!DECL_COMDAT (decl) || !cgraph_function_flags_ready))
122 return true;
124 /* OK we are seeing either COMDAT or static variable. In this case we must
125 check that the definition is still around so we can refer it. */
126 if (TREE_CODE (decl) == FUNCTION_DECL)
128 node = cgraph_get_node (decl);
129 /* Check that we still have function body and that we didn't took
130 the decision to eliminate offline copy of the function yet.
131 The second is important when devirtualization happens during final
132 compilation stage when making a new reference no longer makes callee
133 to be compiled. */
134 if (!node || !node->definition || node->global.inlined_to)
136 gcc_checking_assert (!TREE_ASM_WRITTEN (decl));
137 return false;
140 else if (TREE_CODE (decl) == VAR_DECL)
142 vnode = varpool_get_node (decl);
143 if (!vnode || !vnode->definition)
145 gcc_checking_assert (!TREE_ASM_WRITTEN (decl));
146 return false;
149 return true;
152 /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
153 acceptable form for is_gimple_min_invariant.
154 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
156 tree
157 canonicalize_constructor_val (tree cval, tree from_decl)
159 tree orig_cval = cval;
160 STRIP_NOPS (cval);
161 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
162 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
164 tree ptr = TREE_OPERAND (cval, 0);
165 if (is_gimple_min_invariant (ptr))
166 cval = build1_loc (EXPR_LOCATION (cval),
167 ADDR_EXPR, TREE_TYPE (ptr),
168 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
169 ptr,
170 fold_convert (ptr_type_node,
171 TREE_OPERAND (cval, 1))));
173 if (TREE_CODE (cval) == ADDR_EXPR)
175 tree base = NULL_TREE;
176 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
178 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
179 if (base)
180 TREE_OPERAND (cval, 0) = base;
182 else
183 base = get_base_address (TREE_OPERAND (cval, 0));
184 if (!base)
185 return NULL_TREE;
187 if ((TREE_CODE (base) == VAR_DECL
188 || TREE_CODE (base) == FUNCTION_DECL)
189 && !can_refer_decl_in_current_unit_p (base, from_decl))
190 return NULL_TREE;
191 if (TREE_CODE (base) == VAR_DECL)
192 TREE_ADDRESSABLE (base) = 1;
193 else if (TREE_CODE (base) == FUNCTION_DECL)
195 /* Make sure we create a cgraph node for functions we'll reference.
196 They can be non-existent if the reference comes from an entry
197 of an external vtable for example. */
198 cgraph_get_create_real_symbol_node (base);
200 /* Fixup types in global initializers. */
201 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
202 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
204 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
205 cval = fold_convert (TREE_TYPE (orig_cval), cval);
206 return cval;
208 if (TREE_OVERFLOW_P (cval))
209 return drop_tree_overflow (cval);
210 return orig_cval;
213 /* If SYM is a constant variable with known value, return the value.
214 NULL_TREE is returned otherwise. */
216 tree
217 get_symbol_constant_value (tree sym)
219 tree val = ctor_for_folding (sym);
220 if (val != error_mark_node)
222 if (val)
224 val = canonicalize_constructor_val (unshare_expr (val), sym);
225 if (val && is_gimple_min_invariant (val))
226 return val;
227 else
228 return NULL_TREE;
230 /* Variables declared 'const' without an initializer
231 have zero as the initializer if they may not be
232 overridden at link or run time. */
233 if (!val
234 && (INTEGRAL_TYPE_P (TREE_TYPE (sym))
235 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym))))
236 return build_zero_cst (TREE_TYPE (sym));
239 return NULL_TREE;
244 /* Subroutine of fold_stmt. We perform several simplifications of the
245 memory reference tree EXPR and make sure to re-gimplify them properly
246 after propagation of constant addresses. IS_LHS is true if the
247 reference is supposed to be an lvalue. */
249 static tree
250 maybe_fold_reference (tree expr, bool is_lhs)
252 tree *t = &expr;
253 tree result;
255 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
256 || TREE_CODE (expr) == REALPART_EXPR
257 || TREE_CODE (expr) == IMAGPART_EXPR)
258 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
259 return fold_unary_loc (EXPR_LOCATION (expr),
260 TREE_CODE (expr),
261 TREE_TYPE (expr),
262 TREE_OPERAND (expr, 0));
263 else if (TREE_CODE (expr) == BIT_FIELD_REF
264 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
265 return fold_ternary_loc (EXPR_LOCATION (expr),
266 TREE_CODE (expr),
267 TREE_TYPE (expr),
268 TREE_OPERAND (expr, 0),
269 TREE_OPERAND (expr, 1),
270 TREE_OPERAND (expr, 2));
272 while (handled_component_p (*t))
273 t = &TREE_OPERAND (*t, 0);
275 /* Canonicalize MEM_REFs invariant address operand. Do this first
276 to avoid feeding non-canonical MEM_REFs elsewhere. */
277 if (TREE_CODE (*t) == MEM_REF
278 && !is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)))
280 bool volatile_p = TREE_THIS_VOLATILE (*t);
281 tree tem = fold_binary (MEM_REF, TREE_TYPE (*t),
282 TREE_OPERAND (*t, 0),
283 TREE_OPERAND (*t, 1));
284 if (tem)
286 TREE_THIS_VOLATILE (tem) = volatile_p;
287 *t = tem;
288 tem = maybe_fold_reference (expr, is_lhs);
289 if (tem)
290 return tem;
291 return expr;
295 if (!is_lhs
296 && (result = fold_const_aggregate_ref (expr))
297 && is_gimple_min_invariant (result))
298 return result;
300 /* Fold back MEM_REFs to reference trees. */
301 if (TREE_CODE (*t) == MEM_REF
302 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
303 && integer_zerop (TREE_OPERAND (*t, 1))
304 && (TREE_THIS_VOLATILE (*t)
305 == TREE_THIS_VOLATILE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0)))
306 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*t, 1)))
307 && (TYPE_MAIN_VARIANT (TREE_TYPE (*t))
308 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (*t, 1)))))
309 /* We have to look out here to not drop a required conversion
310 from the rhs to the lhs if is_lhs, but we don't have the
311 rhs here to verify that. Thus require strict type
312 compatibility. */
313 && types_compatible_p (TREE_TYPE (*t),
314 TREE_TYPE (TREE_OPERAND
315 (TREE_OPERAND (*t, 0), 0))))
317 tree tem;
318 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
319 tem = maybe_fold_reference (expr, is_lhs);
320 if (tem)
321 return tem;
322 return expr;
324 else if (TREE_CODE (*t) == TARGET_MEM_REF)
326 tree tem = maybe_fold_tmr (*t);
327 if (tem)
329 *t = tem;
330 tem = maybe_fold_reference (expr, is_lhs);
331 if (tem)
332 return tem;
333 return expr;
337 return NULL_TREE;
341 /* Attempt to fold an assignment statement pointed-to by SI. Returns a
342 replacement rhs for the statement or NULL_TREE if no simplification
343 could be made. It is assumed that the operands have been previously
344 folded. */
346 static tree
347 fold_gimple_assign (gimple_stmt_iterator *si)
349 gimple stmt = gsi_stmt (*si);
350 enum tree_code subcode = gimple_assign_rhs_code (stmt);
351 location_t loc = gimple_location (stmt);
353 tree result = NULL_TREE;
355 switch (get_gimple_rhs_class (subcode))
357 case GIMPLE_SINGLE_RHS:
359 tree rhs = gimple_assign_rhs1 (stmt);
361 if (REFERENCE_CLASS_P (rhs))
362 return maybe_fold_reference (rhs, false);
364 else if (TREE_CODE (rhs) == ADDR_EXPR)
366 tree ref = TREE_OPERAND (rhs, 0);
367 tree tem = maybe_fold_reference (ref, true);
368 if (tem
369 && TREE_CODE (tem) == MEM_REF
370 && integer_zerop (TREE_OPERAND (tem, 1)))
371 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
372 else if (tem)
373 result = fold_convert (TREE_TYPE (rhs),
374 build_fold_addr_expr_loc (loc, tem));
375 else if (TREE_CODE (ref) == MEM_REF
376 && integer_zerop (TREE_OPERAND (ref, 1)))
377 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
380 else if (TREE_CODE (rhs) == CONSTRUCTOR
381 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
382 && (CONSTRUCTOR_NELTS (rhs)
383 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
385 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
386 unsigned i;
387 tree val;
389 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
390 if (TREE_CODE (val) != INTEGER_CST
391 && TREE_CODE (val) != REAL_CST
392 && TREE_CODE (val) != FIXED_CST)
393 return NULL_TREE;
395 return build_vector_from_ctor (TREE_TYPE (rhs),
396 CONSTRUCTOR_ELTS (rhs));
399 else if (DECL_P (rhs))
400 return get_symbol_constant_value (rhs);
402 /* If we couldn't fold the RHS, hand over to the generic
403 fold routines. */
404 if (result == NULL_TREE)
405 result = fold (rhs);
407 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR
408 that may have been added by fold, and "useless" type
409 conversions that might now be apparent due to propagation. */
410 STRIP_USELESS_TYPE_CONVERSION (result);
412 if (result != rhs && valid_gimple_rhs_p (result))
413 return result;
415 return NULL_TREE;
417 break;
419 case GIMPLE_UNARY_RHS:
421 tree rhs = gimple_assign_rhs1 (stmt);
423 result = fold_unary_loc (loc, subcode, gimple_expr_type (stmt), rhs);
424 if (result)
426 /* If the operation was a conversion do _not_ mark a
427 resulting constant with TREE_OVERFLOW if the original
428 constant was not. These conversions have implementation
429 defined behavior and retaining the TREE_OVERFLOW flag
430 here would confuse later passes such as VRP. */
431 if (CONVERT_EXPR_CODE_P (subcode)
432 && TREE_CODE (result) == INTEGER_CST
433 && TREE_CODE (rhs) == INTEGER_CST)
434 TREE_OVERFLOW (result) = TREE_OVERFLOW (rhs);
436 STRIP_USELESS_TYPE_CONVERSION (result);
437 if (valid_gimple_rhs_p (result))
438 return result;
441 break;
443 case GIMPLE_BINARY_RHS:
444 /* Try to canonicalize for boolean-typed X the comparisons
445 X == 0, X == 1, X != 0, and X != 1. */
446 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
447 || gimple_assign_rhs_code (stmt) == NE_EXPR)
449 tree lhs = gimple_assign_lhs (stmt);
450 tree op1 = gimple_assign_rhs1 (stmt);
451 tree op2 = gimple_assign_rhs2 (stmt);
452 tree type = TREE_TYPE (op1);
454 /* Check whether the comparison operands are of the same boolean
455 type as the result type is.
456 Check that second operand is an integer-constant with value
457 one or zero. */
458 if (TREE_CODE (op2) == INTEGER_CST
459 && (integer_zerop (op2) || integer_onep (op2))
460 && useless_type_conversion_p (TREE_TYPE (lhs), type))
462 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
463 bool is_logical_not = false;
465 /* X == 0 and X != 1 is a logical-not.of X
466 X == 1 and X != 0 is X */
467 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
468 || (cmp_code == NE_EXPR && integer_onep (op2)))
469 is_logical_not = true;
471 if (is_logical_not == false)
472 result = op1;
473 /* Only for one-bit precision typed X the transformation
474 !X -> ~X is valied. */
475 else if (TYPE_PRECISION (type) == 1)
476 result = build1_loc (gimple_location (stmt), BIT_NOT_EXPR,
477 type, op1);
478 /* Otherwise we use !X -> X ^ 1. */
479 else
480 result = build2_loc (gimple_location (stmt), BIT_XOR_EXPR,
481 type, op1, build_int_cst (type, 1));
486 if (!result)
487 result = fold_binary_loc (loc, subcode,
488 TREE_TYPE (gimple_assign_lhs (stmt)),
489 gimple_assign_rhs1 (stmt),
490 gimple_assign_rhs2 (stmt));
492 if (result)
494 STRIP_USELESS_TYPE_CONVERSION (result);
495 if (valid_gimple_rhs_p (result))
496 return result;
498 break;
500 case GIMPLE_TERNARY_RHS:
501 /* Try to fold a conditional expression. */
502 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
504 tree op0 = gimple_assign_rhs1 (stmt);
505 tree tem;
506 bool set = false;
507 location_t cond_loc = gimple_location (stmt);
509 if (COMPARISON_CLASS_P (op0))
511 fold_defer_overflow_warnings ();
512 tem = fold_binary_loc (cond_loc,
513 TREE_CODE (op0), TREE_TYPE (op0),
514 TREE_OPERAND (op0, 0),
515 TREE_OPERAND (op0, 1));
516 /* This is actually a conditional expression, not a GIMPLE
517 conditional statement, however, the valid_gimple_rhs_p
518 test still applies. */
519 set = (tem && is_gimple_condexpr (tem)
520 && valid_gimple_rhs_p (tem));
521 fold_undefer_overflow_warnings (set, stmt, 0);
523 else if (is_gimple_min_invariant (op0))
525 tem = op0;
526 set = true;
528 else
529 return NULL_TREE;
531 if (set)
532 result = fold_build3_loc (cond_loc, COND_EXPR,
533 TREE_TYPE (gimple_assign_lhs (stmt)), tem,
534 gimple_assign_rhs2 (stmt),
535 gimple_assign_rhs3 (stmt));
538 if (!result)
539 result = fold_ternary_loc (loc, subcode,
540 TREE_TYPE (gimple_assign_lhs (stmt)),
541 gimple_assign_rhs1 (stmt),
542 gimple_assign_rhs2 (stmt),
543 gimple_assign_rhs3 (stmt));
545 if (result)
547 STRIP_USELESS_TYPE_CONVERSION (result);
548 if (valid_gimple_rhs_p (result))
549 return result;
551 break;
553 case GIMPLE_INVALID_RHS:
554 gcc_unreachable ();
557 return NULL_TREE;
560 /* Attempt to fold a conditional statement. Return true if any changes were
561 made. We only attempt to fold the condition expression, and do not perform
562 any transformation that would require alteration of the cfg. It is
563 assumed that the operands have been previously folded. */
565 static bool
566 fold_gimple_cond (gimple stmt)
568 tree result = fold_binary_loc (gimple_location (stmt),
569 gimple_cond_code (stmt),
570 boolean_type_node,
571 gimple_cond_lhs (stmt),
572 gimple_cond_rhs (stmt));
574 if (result)
576 STRIP_USELESS_TYPE_CONVERSION (result);
577 if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result))
579 gimple_cond_set_condition_from_tree (stmt, result);
580 return true;
584 return false;
587 /* Convert EXPR into a GIMPLE value suitable for substitution on the
588 RHS of an assignment. Insert the necessary statements before
589 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
590 is replaced. If the call is expected to produces a result, then it
591 is replaced by an assignment of the new RHS to the result variable.
592 If the result is to be ignored, then the call is replaced by a
593 GIMPLE_NOP. A proper VDEF chain is retained by making the first
594 VUSE and the last VDEF of the whole sequence be the same as the replaced
595 statement and using new SSA names for stores in between. */
597 void
598 gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
600 tree lhs;
601 gimple stmt, new_stmt;
602 gimple_stmt_iterator i;
603 gimple_seq stmts = NULL;
604 struct gimplify_ctx gctx;
605 gimple laststore;
606 tree reaching_vuse;
608 stmt = gsi_stmt (*si_p);
610 gcc_assert (is_gimple_call (stmt));
612 push_gimplify_context (&gctx);
613 gctx.into_ssa = gimple_in_ssa_p (cfun);
615 lhs = gimple_call_lhs (stmt);
616 if (lhs == NULL_TREE)
618 gimplify_and_add (expr, &stmts);
619 /* We can end up with folding a memcpy of an empty class assignment
620 which gets optimized away by C++ gimplification. */
621 if (gimple_seq_empty_p (stmts))
623 pop_gimplify_context (NULL);
624 if (gimple_in_ssa_p (cfun))
626 unlink_stmt_vdef (stmt);
627 release_defs (stmt);
629 gsi_replace (si_p, gimple_build_nop (), true);
630 return;
633 else
635 tree tmp = get_initialized_tmp_var (expr, &stmts, NULL);
636 new_stmt = gimple_build_assign (lhs, tmp);
637 i = gsi_last (stmts);
638 gsi_insert_after_without_update (&i, new_stmt,
639 GSI_CONTINUE_LINKING);
642 pop_gimplify_context (NULL);
644 if (gimple_has_location (stmt))
645 annotate_all_with_location (stmts, gimple_location (stmt));
647 /* First iterate over the replacement statements backward, assigning
648 virtual operands to their defining statements. */
649 laststore = NULL;
650 for (i = gsi_last (stmts); !gsi_end_p (i); gsi_prev (&i))
652 new_stmt = gsi_stmt (i);
653 if ((gimple_assign_single_p (new_stmt)
654 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
655 || (is_gimple_call (new_stmt)
656 && (gimple_call_flags (new_stmt)
657 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
659 tree vdef;
660 if (!laststore)
661 vdef = gimple_vdef (stmt);
662 else
663 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
664 gimple_set_vdef (new_stmt, vdef);
665 if (vdef && TREE_CODE (vdef) == SSA_NAME)
666 SSA_NAME_DEF_STMT (vdef) = new_stmt;
667 laststore = new_stmt;
671 /* Second iterate over the statements forward, assigning virtual
672 operands to their uses. */
673 reaching_vuse = gimple_vuse (stmt);
674 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
676 new_stmt = gsi_stmt (i);
677 /* If the new statement possibly has a VUSE, update it with exact SSA
678 name we know will reach this one. */
679 if (gimple_has_mem_ops (new_stmt))
680 gimple_set_vuse (new_stmt, reaching_vuse);
681 gimple_set_modified (new_stmt, true);
682 if (gimple_vdef (new_stmt))
683 reaching_vuse = gimple_vdef (new_stmt);
686 /* If the new sequence does not do a store release the virtual
687 definition of the original statement. */
688 if (reaching_vuse
689 && reaching_vuse == gimple_vuse (stmt))
691 tree vdef = gimple_vdef (stmt);
692 if (vdef
693 && TREE_CODE (vdef) == SSA_NAME)
695 unlink_stmt_vdef (stmt);
696 release_ssa_name (vdef);
700 /* Finally replace the original statement with the sequence. */
701 gsi_replace_with_seq (si_p, stmts, false);
704 /* Return the string length, maximum string length or maximum value of
705 ARG in LENGTH.
706 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
707 is not NULL and, for TYPE == 0, its value is not equal to the length
708 we determine or if we are unable to determine the length or value,
709 return false. VISITED is a bitmap of visited variables.
710 TYPE is 0 if string length should be returned, 1 for maximum string
711 length and 2 for maximum value ARG can have. */
713 static bool
714 get_maxval_strlen (tree arg, tree *length, bitmap visited, int type)
716 tree var, val;
717 gimple def_stmt;
719 if (TREE_CODE (arg) != SSA_NAME)
721 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
722 if (TREE_CODE (arg) == ADDR_EXPR
723 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF
724 && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1)))
726 tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
727 if (TREE_CODE (aop0) == INDIRECT_REF
728 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
729 return get_maxval_strlen (TREE_OPERAND (aop0, 0),
730 length, visited, type);
733 if (type == 2)
735 val = arg;
736 if (TREE_CODE (val) != INTEGER_CST
737 || tree_int_cst_sgn (val) < 0)
738 return false;
740 else
741 val = c_strlen (arg, 1);
742 if (!val)
743 return false;
745 if (*length)
747 if (type > 0)
749 if (TREE_CODE (*length) != INTEGER_CST
750 || TREE_CODE (val) != INTEGER_CST)
751 return false;
753 if (tree_int_cst_lt (*length, val))
754 *length = val;
755 return true;
757 else if (simple_cst_equal (val, *length) != 1)
758 return false;
761 *length = val;
762 return true;
765 /* If ARG is registered for SSA update we cannot look at its defining
766 statement. */
767 if (name_registered_for_update_p (arg))
768 return false;
770 /* If we were already here, break the infinite cycle. */
771 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
772 return true;
774 var = arg;
775 def_stmt = SSA_NAME_DEF_STMT (var);
777 switch (gimple_code (def_stmt))
779 case GIMPLE_ASSIGN:
780 /* The RHS of the statement defining VAR must either have a
781 constant length or come from another SSA_NAME with a constant
782 length. */
783 if (gimple_assign_single_p (def_stmt)
784 || gimple_assign_unary_nop_p (def_stmt))
786 tree rhs = gimple_assign_rhs1 (def_stmt);
787 return get_maxval_strlen (rhs, length, visited, type);
789 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
791 tree op2 = gimple_assign_rhs2 (def_stmt);
792 tree op3 = gimple_assign_rhs3 (def_stmt);
793 return get_maxval_strlen (op2, length, visited, type)
794 && get_maxval_strlen (op3, length, visited, type);
796 return false;
798 case GIMPLE_PHI:
800 /* All the arguments of the PHI node must have the same constant
801 length. */
802 unsigned i;
804 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
806 tree arg = gimple_phi_arg (def_stmt, i)->def;
808 /* If this PHI has itself as an argument, we cannot
809 determine the string length of this argument. However,
810 if we can find a constant string length for the other
811 PHI args then we can still be sure that this is a
812 constant string length. So be optimistic and just
813 continue with the next argument. */
814 if (arg == gimple_phi_result (def_stmt))
815 continue;
817 if (!get_maxval_strlen (arg, length, visited, type))
818 return false;
821 return true;
823 default:
824 return false;
829 /* Fold builtin call in statement STMT. Returns a simplified tree.
830 We may return a non-constant expression, including another call
831 to a different function and with different arguments, e.g.,
832 substituting memcpy for strcpy when the string length is known.
833 Note that some builtins expand into inline code that may not
834 be valid in GIMPLE. Callers must take care. */
836 tree
837 gimple_fold_builtin (gimple stmt)
839 tree result, val[3];
840 tree callee, a;
841 int arg_idx, type;
842 bitmap visited;
843 bool ignore;
844 int nargs;
845 location_t loc = gimple_location (stmt);
847 gcc_assert (is_gimple_call (stmt));
849 ignore = (gimple_call_lhs (stmt) == NULL);
851 /* First try the generic builtin folder. If that succeeds, return the
852 result directly. */
853 result = fold_call_stmt (stmt, ignore);
854 if (result)
856 if (ignore)
857 STRIP_NOPS (result);
858 return result;
861 /* Ignore MD builtins. */
862 callee = gimple_call_fndecl (stmt);
863 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
864 return NULL_TREE;
866 /* Give up for always_inline inline builtins until they are
867 inlined. */
868 if (avoid_folding_inline_builtin (callee))
869 return NULL_TREE;
871 /* If the builtin could not be folded, and it has no argument list,
872 we're done. */
873 nargs = gimple_call_num_args (stmt);
874 if (nargs == 0)
875 return NULL_TREE;
877 /* Limit the work only for builtins we know how to simplify. */
878 switch (DECL_FUNCTION_CODE (callee))
880 case BUILT_IN_STRLEN:
881 case BUILT_IN_FPUTS:
882 case BUILT_IN_FPUTS_UNLOCKED:
883 arg_idx = 0;
884 type = 0;
885 break;
886 case BUILT_IN_STRCPY:
887 case BUILT_IN_STRNCPY:
888 arg_idx = 1;
889 type = 0;
890 break;
891 case BUILT_IN_MEMCPY_CHK:
892 case BUILT_IN_MEMPCPY_CHK:
893 case BUILT_IN_MEMMOVE_CHK:
894 case BUILT_IN_MEMSET_CHK:
895 case BUILT_IN_STRNCPY_CHK:
896 case BUILT_IN_STPNCPY_CHK:
897 arg_idx = 2;
898 type = 2;
899 break;
900 case BUILT_IN_STRCPY_CHK:
901 case BUILT_IN_STPCPY_CHK:
902 arg_idx = 1;
903 type = 1;
904 break;
905 case BUILT_IN_SNPRINTF_CHK:
906 case BUILT_IN_VSNPRINTF_CHK:
907 arg_idx = 1;
908 type = 2;
909 break;
910 default:
911 return NULL_TREE;
914 if (arg_idx >= nargs)
915 return NULL_TREE;
917 /* Try to use the dataflow information gathered by the CCP process. */
918 visited = BITMAP_ALLOC (NULL);
919 bitmap_clear (visited);
921 memset (val, 0, sizeof (val));
922 a = gimple_call_arg (stmt, arg_idx);
923 if (!get_maxval_strlen (a, &val[arg_idx], visited, type))
924 val[arg_idx] = NULL_TREE;
926 BITMAP_FREE (visited);
928 result = NULL_TREE;
929 switch (DECL_FUNCTION_CODE (callee))
931 case BUILT_IN_STRLEN:
932 if (val[0] && nargs == 1)
934 tree new_val =
935 fold_convert (TREE_TYPE (gimple_call_lhs (stmt)), val[0]);
937 /* If the result is not a valid gimple value, or not a cast
938 of a valid gimple value, then we cannot use the result. */
939 if (is_gimple_val (new_val)
940 || (CONVERT_EXPR_P (new_val)
941 && is_gimple_val (TREE_OPERAND (new_val, 0))))
942 return new_val;
944 break;
946 case BUILT_IN_STRCPY:
947 if (val[1] && is_gimple_val (val[1]) && nargs == 2)
948 result = fold_builtin_strcpy (loc, callee,
949 gimple_call_arg (stmt, 0),
950 gimple_call_arg (stmt, 1),
951 val[1]);
952 break;
954 case BUILT_IN_STRNCPY:
955 if (val[1] && is_gimple_val (val[1]) && nargs == 3)
956 result = fold_builtin_strncpy (loc, callee,
957 gimple_call_arg (stmt, 0),
958 gimple_call_arg (stmt, 1),
959 gimple_call_arg (stmt, 2),
960 val[1]);
961 break;
963 case BUILT_IN_FPUTS:
964 if (nargs == 2)
965 result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0),
966 gimple_call_arg (stmt, 1),
967 ignore, false, val[0]);
968 break;
970 case BUILT_IN_FPUTS_UNLOCKED:
971 if (nargs == 2)
972 result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0),
973 gimple_call_arg (stmt, 1),
974 ignore, true, val[0]);
975 break;
977 case BUILT_IN_MEMCPY_CHK:
978 case BUILT_IN_MEMPCPY_CHK:
979 case BUILT_IN_MEMMOVE_CHK:
980 case BUILT_IN_MEMSET_CHK:
981 if (val[2] && is_gimple_val (val[2]) && nargs == 4)
982 result = fold_builtin_memory_chk (loc, callee,
983 gimple_call_arg (stmt, 0),
984 gimple_call_arg (stmt, 1),
985 gimple_call_arg (stmt, 2),
986 gimple_call_arg (stmt, 3),
987 val[2], ignore,
988 DECL_FUNCTION_CODE (callee));
989 break;
991 case BUILT_IN_STRCPY_CHK:
992 case BUILT_IN_STPCPY_CHK:
993 if (val[1] && is_gimple_val (val[1]) && nargs == 3)
994 result = fold_builtin_stxcpy_chk (loc, callee,
995 gimple_call_arg (stmt, 0),
996 gimple_call_arg (stmt, 1),
997 gimple_call_arg (stmt, 2),
998 val[1], ignore,
999 DECL_FUNCTION_CODE (callee));
1000 break;
1002 case BUILT_IN_STRNCPY_CHK:
1003 case BUILT_IN_STPNCPY_CHK:
1004 if (val[2] && is_gimple_val (val[2]) && nargs == 4)
1005 result = fold_builtin_stxncpy_chk (loc, gimple_call_arg (stmt, 0),
1006 gimple_call_arg (stmt, 1),
1007 gimple_call_arg (stmt, 2),
1008 gimple_call_arg (stmt, 3),
1009 val[2], ignore,
1010 DECL_FUNCTION_CODE (callee));
1011 break;
1013 case BUILT_IN_SNPRINTF_CHK:
1014 case BUILT_IN_VSNPRINTF_CHK:
1015 if (val[1] && is_gimple_val (val[1]))
1016 result = gimple_fold_builtin_snprintf_chk (stmt, val[1],
1017 DECL_FUNCTION_CODE (callee));
1018 break;
1020 default:
1021 gcc_unreachable ();
1024 if (result && ignore)
1025 result = fold_ignored_result (result);
1026 return result;
1030 /* Return a binfo to be used for devirtualization of calls based on an object
1031 represented by a declaration (i.e. a global or automatically allocated one)
1032 or NULL if it cannot be found or is not safe. CST is expected to be an
1033 ADDR_EXPR of such object or the function will return NULL. Currently it is
1034 safe to use such binfo only if it has no base binfo (i.e. no ancestors)
1035 EXPECTED_TYPE is type of the class virtual belongs to. */
1037 tree
1038 gimple_extract_devirt_binfo_from_cst (tree cst, tree expected_type)
1040 HOST_WIDE_INT offset, size, max_size;
1041 tree base, type, binfo;
1042 bool last_artificial = false;
1044 if (!flag_devirtualize
1045 || TREE_CODE (cst) != ADDR_EXPR
1046 || TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) != RECORD_TYPE)
1047 return NULL_TREE;
1049 cst = TREE_OPERAND (cst, 0);
1050 base = get_ref_base_and_extent (cst, &offset, &size, &max_size);
1051 type = TREE_TYPE (base);
1052 if (!DECL_P (base)
1053 || max_size == -1
1054 || max_size != size
1055 || TREE_CODE (type) != RECORD_TYPE)
1056 return NULL_TREE;
1058 /* Find the sub-object the constant actually refers to and mark whether it is
1059 an artificial one (as opposed to a user-defined one). */
1060 while (true)
1062 HOST_WIDE_INT pos, size;
1063 tree fld;
1065 if (types_same_for_odr (type, expected_type))
1066 break;
1067 if (offset < 0)
1068 return NULL_TREE;
1070 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
1072 if (TREE_CODE (fld) != FIELD_DECL)
1073 continue;
1075 pos = int_bit_position (fld);
1076 size = tree_low_cst (DECL_SIZE (fld), 1);
1077 if (pos <= offset && (pos + size) > offset)
1078 break;
1080 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
1081 return NULL_TREE;
1083 last_artificial = DECL_ARTIFICIAL (fld);
1084 type = TREE_TYPE (fld);
1085 offset -= pos;
1087 /* Artificial sub-objects are ancestors, we do not want to use them for
1088 devirtualization, at least not here. */
1089 if (last_artificial)
1090 return NULL_TREE;
1091 binfo = TYPE_BINFO (type);
1092 if (!binfo || BINFO_N_BASE_BINFOS (binfo) > 0)
1093 return NULL_TREE;
1094 else
1095 return binfo;
1098 /* Attempt to fold a call statement referenced by the statement iterator GSI.
1099 The statement may be replaced by another statement, e.g., if the call
1100 simplifies to a constant value. Return true if any changes were made.
1101 It is assumed that the operands have been previously folded. */
1103 static bool
1104 gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
1106 gimple stmt = gsi_stmt (*gsi);
1107 tree callee;
1108 bool changed = false;
1109 unsigned i;
1111 /* Fold *& in call arguments. */
1112 for (i = 0; i < gimple_call_num_args (stmt); ++i)
1113 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
1115 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
1116 if (tmp)
1118 gimple_call_set_arg (stmt, i, tmp);
1119 changed = true;
1123 /* Check for virtual calls that became direct calls. */
1124 callee = gimple_call_fn (stmt);
1125 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
1127 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
1129 if (dump_file && virtual_method_call_p (callee)
1130 && !possible_polymorphic_call_target_p
1131 (callee, cgraph_get_node (gimple_call_addr_fndecl
1132 (OBJ_TYPE_REF_EXPR (callee)))))
1134 fprintf (dump_file,
1135 "Type inheritnace inconsistent devirtualization of ");
1136 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1137 fprintf (dump_file, " to ");
1138 print_generic_expr (dump_file, callee, TDF_SLIM);
1139 fprintf (dump_file, "\n");
1142 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
1143 changed = true;
1145 else if (virtual_method_call_p (callee))
1147 tree obj = OBJ_TYPE_REF_OBJECT (callee);
1148 tree binfo = gimple_extract_devirt_binfo_from_cst
1149 (obj, obj_type_ref_class (callee));
1150 if (binfo)
1152 HOST_WIDE_INT token
1153 = TREE_INT_CST_LOW (OBJ_TYPE_REF_TOKEN (callee));
1154 tree fndecl = gimple_get_virt_method_for_binfo (token, binfo);
1155 if (fndecl)
1157 #ifdef ENABLE_CHECKING
1158 gcc_assert (possible_polymorphic_call_target_p
1159 (callee, cgraph_get_node (fndecl)));
1161 #endif
1162 gimple_call_set_fndecl (stmt, fndecl);
1163 changed = true;
1169 if (inplace)
1170 return changed;
1172 /* Check for builtins that CCP can handle using information not
1173 available in the generic fold routines. */
1174 callee = gimple_call_fndecl (stmt);
1175 if (callee && DECL_BUILT_IN (callee))
1177 tree result = gimple_fold_builtin (stmt);
1178 if (result)
1180 if (!update_call_from_tree (gsi, result))
1181 gimplify_and_update_call_from_tree (gsi, result);
1182 changed = true;
1184 else if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
1185 changed |= targetm.gimple_fold_builtin (gsi);
1188 return changed;
1191 /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
1192 distinguishes both cases. */
1194 static bool
1195 fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace)
1197 bool changed = false;
1198 gimple stmt = gsi_stmt (*gsi);
1199 unsigned i;
1201 /* Fold the main computation performed by the statement. */
1202 switch (gimple_code (stmt))
1204 case GIMPLE_ASSIGN:
1206 unsigned old_num_ops = gimple_num_ops (stmt);
1207 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1208 tree lhs = gimple_assign_lhs (stmt);
1209 tree new_rhs;
1210 /* First canonicalize operand order. This avoids building new
1211 trees if this is the only thing fold would later do. */
1212 if ((commutative_tree_code (subcode)
1213 || commutative_ternary_tree_code (subcode))
1214 && tree_swap_operands_p (gimple_assign_rhs1 (stmt),
1215 gimple_assign_rhs2 (stmt), false))
1217 tree tem = gimple_assign_rhs1 (stmt);
1218 gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt));
1219 gimple_assign_set_rhs2 (stmt, tem);
1220 changed = true;
1222 new_rhs = fold_gimple_assign (gsi);
1223 if (new_rhs
1224 && !useless_type_conversion_p (TREE_TYPE (lhs),
1225 TREE_TYPE (new_rhs)))
1226 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
1227 if (new_rhs
1228 && (!inplace
1229 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
1231 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
1232 changed = true;
1234 break;
1237 case GIMPLE_COND:
1238 changed |= fold_gimple_cond (stmt);
1239 break;
1241 case GIMPLE_CALL:
1242 changed |= gimple_fold_call (gsi, inplace);
1243 break;
1245 case GIMPLE_ASM:
1246 /* Fold *& in asm operands. */
1248 size_t noutputs;
1249 const char **oconstraints;
1250 const char *constraint;
1251 bool allows_mem, allows_reg;
1253 noutputs = gimple_asm_noutputs (stmt);
1254 oconstraints = XALLOCAVEC (const char *, noutputs);
1256 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1258 tree link = gimple_asm_output_op (stmt, i);
1259 tree op = TREE_VALUE (link);
1260 oconstraints[i]
1261 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1262 if (REFERENCE_CLASS_P (op)
1263 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
1265 TREE_VALUE (link) = op;
1266 changed = true;
1269 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
1271 tree link = gimple_asm_input_op (stmt, i);
1272 tree op = TREE_VALUE (link);
1273 constraint
1274 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1275 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1276 oconstraints, &allows_mem, &allows_reg);
1277 if (REFERENCE_CLASS_P (op)
1278 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
1279 != NULL_TREE)
1281 TREE_VALUE (link) = op;
1282 changed = true;
1286 break;
1288 case GIMPLE_DEBUG:
1289 if (gimple_debug_bind_p (stmt))
1291 tree val = gimple_debug_bind_get_value (stmt);
1292 if (val
1293 && REFERENCE_CLASS_P (val))
1295 tree tem = maybe_fold_reference (val, false);
1296 if (tem)
1298 gimple_debug_bind_set_value (stmt, tem);
1299 changed = true;
1302 else if (val
1303 && TREE_CODE (val) == ADDR_EXPR)
1305 tree ref = TREE_OPERAND (val, 0);
1306 tree tem = maybe_fold_reference (ref, false);
1307 if (tem)
1309 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
1310 gimple_debug_bind_set_value (stmt, tem);
1311 changed = true;
1315 break;
1317 default:;
1320 stmt = gsi_stmt (*gsi);
1322 /* Fold *& on the lhs. */
1323 if (gimple_has_lhs (stmt))
1325 tree lhs = gimple_get_lhs (stmt);
1326 if (lhs && REFERENCE_CLASS_P (lhs))
1328 tree new_lhs = maybe_fold_reference (lhs, true);
1329 if (new_lhs)
1331 gimple_set_lhs (stmt, new_lhs);
1332 changed = true;
1337 return changed;
1340 /* Fold the statement pointed to by GSI. In some cases, this function may
1341 replace the whole statement with a new one. Returns true iff folding
1342 makes any changes.
1343 The statement pointed to by GSI should be in valid gimple form but may
1344 be in unfolded state as resulting from for example constant propagation
1345 which can produce *&x = 0. */
1347 bool
1348 fold_stmt (gimple_stmt_iterator *gsi)
1350 return fold_stmt_1 (gsi, false);
1353 /* Perform the minimal folding on statement *GSI. Only operations like
1354 *&x created by constant propagation are handled. The statement cannot
1355 be replaced with a new one. Return true if the statement was
1356 changed, false otherwise.
1357 The statement *GSI should be in valid gimple form but may
1358 be in unfolded state as resulting from for example constant propagation
1359 which can produce *&x = 0. */
1361 bool
1362 fold_stmt_inplace (gimple_stmt_iterator *gsi)
1364 gimple stmt = gsi_stmt (*gsi);
1365 bool changed = fold_stmt_1 (gsi, true);
1366 gcc_assert (gsi_stmt (*gsi) == stmt);
1367 return changed;
1370 /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
1371 if EXPR is null or we don't know how.
1372 If non-null, the result always has boolean type. */
1374 static tree
1375 canonicalize_bool (tree expr, bool invert)
1377 if (!expr)
1378 return NULL_TREE;
1379 else if (invert)
1381 if (integer_nonzerop (expr))
1382 return boolean_false_node;
1383 else if (integer_zerop (expr))
1384 return boolean_true_node;
1385 else if (TREE_CODE (expr) == SSA_NAME)
1386 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
1387 build_int_cst (TREE_TYPE (expr), 0));
1388 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison)
1389 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
1390 boolean_type_node,
1391 TREE_OPERAND (expr, 0),
1392 TREE_OPERAND (expr, 1));
1393 else
1394 return NULL_TREE;
1396 else
1398 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
1399 return expr;
1400 if (integer_nonzerop (expr))
1401 return boolean_true_node;
1402 else if (integer_zerop (expr))
1403 return boolean_false_node;
1404 else if (TREE_CODE (expr) == SSA_NAME)
1405 return fold_build2 (NE_EXPR, boolean_type_node, expr,
1406 build_int_cst (TREE_TYPE (expr), 0));
1407 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison)
1408 return fold_build2 (TREE_CODE (expr),
1409 boolean_type_node,
1410 TREE_OPERAND (expr, 0),
1411 TREE_OPERAND (expr, 1));
1412 else
1413 return NULL_TREE;
1417 /* Check to see if a boolean expression EXPR is logically equivalent to the
1418 comparison (OP1 CODE OP2). Check for various identities involving
1419 SSA_NAMEs. */
1421 static bool
1422 same_bool_comparison_p (const_tree expr, enum tree_code code,
1423 const_tree op1, const_tree op2)
1425 gimple s;
1427 /* The obvious case. */
1428 if (TREE_CODE (expr) == code
1429 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
1430 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
1431 return true;
1433 /* Check for comparing (name, name != 0) and the case where expr
1434 is an SSA_NAME with a definition matching the comparison. */
1435 if (TREE_CODE (expr) == SSA_NAME
1436 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
1438 if (operand_equal_p (expr, op1, 0))
1439 return ((code == NE_EXPR && integer_zerop (op2))
1440 || (code == EQ_EXPR && integer_nonzerop (op2)));
1441 s = SSA_NAME_DEF_STMT (expr);
1442 if (is_gimple_assign (s)
1443 && gimple_assign_rhs_code (s) == code
1444 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
1445 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
1446 return true;
1449 /* If op1 is of the form (name != 0) or (name == 0), and the definition
1450 of name is a comparison, recurse. */
1451 if (TREE_CODE (op1) == SSA_NAME
1452 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
1454 s = SSA_NAME_DEF_STMT (op1);
1455 if (is_gimple_assign (s)
1456 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
1458 enum tree_code c = gimple_assign_rhs_code (s);
1459 if ((c == NE_EXPR && integer_zerop (op2))
1460 || (c == EQ_EXPR && integer_nonzerop (op2)))
1461 return same_bool_comparison_p (expr, c,
1462 gimple_assign_rhs1 (s),
1463 gimple_assign_rhs2 (s));
1464 if ((c == EQ_EXPR && integer_zerop (op2))
1465 || (c == NE_EXPR && integer_nonzerop (op2)))
1466 return same_bool_comparison_p (expr,
1467 invert_tree_comparison (c, false),
1468 gimple_assign_rhs1 (s),
1469 gimple_assign_rhs2 (s));
1472 return false;
1475 /* Check to see if two boolean expressions OP1 and OP2 are logically
1476 equivalent. */
1478 static bool
1479 same_bool_result_p (const_tree op1, const_tree op2)
1481 /* Simple cases first. */
1482 if (operand_equal_p (op1, op2, 0))
1483 return true;
1485 /* Check the cases where at least one of the operands is a comparison.
1486 These are a bit smarter than operand_equal_p in that they apply some
1487 identifies on SSA_NAMEs. */
1488 if (TREE_CODE_CLASS (TREE_CODE (op2)) == tcc_comparison
1489 && same_bool_comparison_p (op1, TREE_CODE (op2),
1490 TREE_OPERAND (op2, 0),
1491 TREE_OPERAND (op2, 1)))
1492 return true;
1493 if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_comparison
1494 && same_bool_comparison_p (op2, TREE_CODE (op1),
1495 TREE_OPERAND (op1, 0),
1496 TREE_OPERAND (op1, 1)))
1497 return true;
1499 /* Default case. */
1500 return false;
1503 /* Forward declarations for some mutually recursive functions. */
1505 static tree
1506 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
1507 enum tree_code code2, tree op2a, tree op2b);
1508 static tree
1509 and_var_with_comparison (tree var, bool invert,
1510 enum tree_code code2, tree op2a, tree op2b);
1511 static tree
1512 and_var_with_comparison_1 (gimple stmt,
1513 enum tree_code code2, tree op2a, tree op2b);
1514 static tree
1515 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
1516 enum tree_code code2, tree op2a, tree op2b);
1517 static tree
1518 or_var_with_comparison (tree var, bool invert,
1519 enum tree_code code2, tree op2a, tree op2b);
1520 static tree
1521 or_var_with_comparison_1 (gimple stmt,
1522 enum tree_code code2, tree op2a, tree op2b);
1524 /* Helper function for and_comparisons_1: try to simplify the AND of the
1525 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1526 If INVERT is true, invert the value of the VAR before doing the AND.
1527 Return NULL_EXPR if we can't simplify this to a single expression. */
1529 static tree
1530 and_var_with_comparison (tree var, bool invert,
1531 enum tree_code code2, tree op2a, tree op2b)
1533 tree t;
1534 gimple stmt = SSA_NAME_DEF_STMT (var);
1536 /* We can only deal with variables whose definitions are assignments. */
1537 if (!is_gimple_assign (stmt))
1538 return NULL_TREE;
1540 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
1541 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
1542 Then we only have to consider the simpler non-inverted cases. */
1543 if (invert)
1544 t = or_var_with_comparison_1 (stmt,
1545 invert_tree_comparison (code2, false),
1546 op2a, op2b);
1547 else
1548 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
1549 return canonicalize_bool (t, invert);
1552 /* Try to simplify the AND of the ssa variable defined by the assignment
1553 STMT with the comparison specified by (OP2A CODE2 OP2B).
1554 Return NULL_EXPR if we can't simplify this to a single expression. */
1556 static tree
1557 and_var_with_comparison_1 (gimple stmt,
1558 enum tree_code code2, tree op2a, tree op2b)
1560 tree var = gimple_assign_lhs (stmt);
1561 tree true_test_var = NULL_TREE;
1562 tree false_test_var = NULL_TREE;
1563 enum tree_code innercode = gimple_assign_rhs_code (stmt);
1565 /* Check for identities like (var AND (var == 0)) => false. */
1566 if (TREE_CODE (op2a) == SSA_NAME
1567 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
1569 if ((code2 == NE_EXPR && integer_zerop (op2b))
1570 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
1572 true_test_var = op2a;
1573 if (var == true_test_var)
1574 return var;
1576 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
1577 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
1579 false_test_var = op2a;
1580 if (var == false_test_var)
1581 return boolean_false_node;
1585 /* If the definition is a comparison, recurse on it. */
1586 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
1588 tree t = and_comparisons_1 (innercode,
1589 gimple_assign_rhs1 (stmt),
1590 gimple_assign_rhs2 (stmt),
1591 code2,
1592 op2a,
1593 op2b);
1594 if (t)
1595 return t;
1598 /* If the definition is an AND or OR expression, we may be able to
1599 simplify by reassociating. */
1600 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
1601 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
1603 tree inner1 = gimple_assign_rhs1 (stmt);
1604 tree inner2 = gimple_assign_rhs2 (stmt);
1605 gimple s;
1606 tree t;
1607 tree partial = NULL_TREE;
1608 bool is_and = (innercode == BIT_AND_EXPR);
1610 /* Check for boolean identities that don't require recursive examination
1611 of inner1/inner2:
1612 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
1613 inner1 AND (inner1 OR inner2) => inner1
1614 !inner1 AND (inner1 AND inner2) => false
1615 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
1616 Likewise for similar cases involving inner2. */
1617 if (inner1 == true_test_var)
1618 return (is_and ? var : inner1);
1619 else if (inner2 == true_test_var)
1620 return (is_and ? var : inner2);
1621 else if (inner1 == false_test_var)
1622 return (is_and
1623 ? boolean_false_node
1624 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
1625 else if (inner2 == false_test_var)
1626 return (is_and
1627 ? boolean_false_node
1628 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
1630 /* Next, redistribute/reassociate the AND across the inner tests.
1631 Compute the first partial result, (inner1 AND (op2a code op2b)) */
1632 if (TREE_CODE (inner1) == SSA_NAME
1633 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
1634 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
1635 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
1636 gimple_assign_rhs1 (s),
1637 gimple_assign_rhs2 (s),
1638 code2, op2a, op2b)))
1640 /* Handle the AND case, where we are reassociating:
1641 (inner1 AND inner2) AND (op2a code2 op2b)
1642 => (t AND inner2)
1643 If the partial result t is a constant, we win. Otherwise
1644 continue on to try reassociating with the other inner test. */
1645 if (is_and)
1647 if (integer_onep (t))
1648 return inner2;
1649 else if (integer_zerop (t))
1650 return boolean_false_node;
1653 /* Handle the OR case, where we are redistributing:
1654 (inner1 OR inner2) AND (op2a code2 op2b)
1655 => (t OR (inner2 AND (op2a code2 op2b))) */
1656 else if (integer_onep (t))
1657 return boolean_true_node;
1659 /* Save partial result for later. */
1660 partial = t;
1663 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
1664 if (TREE_CODE (inner2) == SSA_NAME
1665 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
1666 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
1667 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
1668 gimple_assign_rhs1 (s),
1669 gimple_assign_rhs2 (s),
1670 code2, op2a, op2b)))
1672 /* Handle the AND case, where we are reassociating:
1673 (inner1 AND inner2) AND (op2a code2 op2b)
1674 => (inner1 AND t) */
1675 if (is_and)
1677 if (integer_onep (t))
1678 return inner1;
1679 else if (integer_zerop (t))
1680 return boolean_false_node;
1681 /* If both are the same, we can apply the identity
1682 (x AND x) == x. */
1683 else if (partial && same_bool_result_p (t, partial))
1684 return t;
1687 /* Handle the OR case. where we are redistributing:
1688 (inner1 OR inner2) AND (op2a code2 op2b)
1689 => (t OR (inner1 AND (op2a code2 op2b)))
1690 => (t OR partial) */
1691 else
1693 if (integer_onep (t))
1694 return boolean_true_node;
1695 else if (partial)
1697 /* We already got a simplification for the other
1698 operand to the redistributed OR expression. The
1699 interesting case is when at least one is false.
1700 Or, if both are the same, we can apply the identity
1701 (x OR x) == x. */
1702 if (integer_zerop (partial))
1703 return t;
1704 else if (integer_zerop (t))
1705 return partial;
1706 else if (same_bool_result_p (t, partial))
1707 return t;
1712 return NULL_TREE;
1715 /* Try to simplify the AND of two comparisons defined by
1716 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
1717 If this can be done without constructing an intermediate value,
1718 return the resulting tree; otherwise NULL_TREE is returned.
1719 This function is deliberately asymmetric as it recurses on SSA_DEFs
1720 in the first comparison but not the second. */
1722 static tree
1723 and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
1724 enum tree_code code2, tree op2a, tree op2b)
1726 tree truth_type = truth_type_for (TREE_TYPE (op1a));
1728 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
1729 if (operand_equal_p (op1a, op2a, 0)
1730 && operand_equal_p (op1b, op2b, 0))
1732 /* Result will be either NULL_TREE, or a combined comparison. */
1733 tree t = combine_comparisons (UNKNOWN_LOCATION,
1734 TRUTH_ANDIF_EXPR, code1, code2,
1735 truth_type, op1a, op1b);
1736 if (t)
1737 return t;
1740 /* Likewise the swapped case of the above. */
1741 if (operand_equal_p (op1a, op2b, 0)
1742 && operand_equal_p (op1b, op2a, 0))
1744 /* Result will be either NULL_TREE, or a combined comparison. */
1745 tree t = combine_comparisons (UNKNOWN_LOCATION,
1746 TRUTH_ANDIF_EXPR, code1,
1747 swap_tree_comparison (code2),
1748 truth_type, op1a, op1b);
1749 if (t)
1750 return t;
1753 /* If both comparisons are of the same value against constants, we might
1754 be able to merge them. */
1755 if (operand_equal_p (op1a, op2a, 0)
1756 && TREE_CODE (op1b) == INTEGER_CST
1757 && TREE_CODE (op2b) == INTEGER_CST)
1759 int cmp = tree_int_cst_compare (op1b, op2b);
1761 /* If we have (op1a == op1b), we should either be able to
1762 return that or FALSE, depending on whether the constant op1b
1763 also satisfies the other comparison against op2b. */
1764 if (code1 == EQ_EXPR)
1766 bool done = true;
1767 bool val;
1768 switch (code2)
1770 case EQ_EXPR: val = (cmp == 0); break;
1771 case NE_EXPR: val = (cmp != 0); break;
1772 case LT_EXPR: val = (cmp < 0); break;
1773 case GT_EXPR: val = (cmp > 0); break;
1774 case LE_EXPR: val = (cmp <= 0); break;
1775 case GE_EXPR: val = (cmp >= 0); break;
1776 default: done = false;
1778 if (done)
1780 if (val)
1781 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1782 else
1783 return boolean_false_node;
1786 /* Likewise if the second comparison is an == comparison. */
1787 else if (code2 == EQ_EXPR)
1789 bool done = true;
1790 bool val;
1791 switch (code1)
1793 case EQ_EXPR: val = (cmp == 0); break;
1794 case NE_EXPR: val = (cmp != 0); break;
1795 case LT_EXPR: val = (cmp > 0); break;
1796 case GT_EXPR: val = (cmp < 0); break;
1797 case LE_EXPR: val = (cmp >= 0); break;
1798 case GE_EXPR: val = (cmp <= 0); break;
1799 default: done = false;
1801 if (done)
1803 if (val)
1804 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1805 else
1806 return boolean_false_node;
1810 /* Same business with inequality tests. */
1811 else if (code1 == NE_EXPR)
1813 bool val;
1814 switch (code2)
1816 case EQ_EXPR: val = (cmp != 0); break;
1817 case NE_EXPR: val = (cmp == 0); break;
1818 case LT_EXPR: val = (cmp >= 0); break;
1819 case GT_EXPR: val = (cmp <= 0); break;
1820 case LE_EXPR: val = (cmp > 0); break;
1821 case GE_EXPR: val = (cmp < 0); break;
1822 default:
1823 val = false;
1825 if (val)
1826 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1828 else if (code2 == NE_EXPR)
1830 bool val;
1831 switch (code1)
1833 case EQ_EXPR: val = (cmp == 0); break;
1834 case NE_EXPR: val = (cmp != 0); break;
1835 case LT_EXPR: val = (cmp <= 0); break;
1836 case GT_EXPR: val = (cmp >= 0); break;
1837 case LE_EXPR: val = (cmp < 0); break;
1838 case GE_EXPR: val = (cmp > 0); break;
1839 default:
1840 val = false;
1842 if (val)
1843 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1846 /* Chose the more restrictive of two < or <= comparisons. */
1847 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
1848 && (code2 == LT_EXPR || code2 == LE_EXPR))
1850 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
1851 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1852 else
1853 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1856 /* Likewise chose the more restrictive of two > or >= comparisons. */
1857 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
1858 && (code2 == GT_EXPR || code2 == GE_EXPR))
1860 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
1861 return fold_build2 (code1, boolean_type_node, op1a, op1b);
1862 else
1863 return fold_build2 (code2, boolean_type_node, op2a, op2b);
1866 /* Check for singleton ranges. */
1867 else if (cmp == 0
1868 && ((code1 == LE_EXPR && code2 == GE_EXPR)
1869 || (code1 == GE_EXPR && code2 == LE_EXPR)))
1870 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
1872 /* Check for disjoint ranges. */
1873 else if (cmp <= 0
1874 && (code1 == LT_EXPR || code1 == LE_EXPR)
1875 && (code2 == GT_EXPR || code2 == GE_EXPR))
1876 return boolean_false_node;
1877 else if (cmp >= 0
1878 && (code1 == GT_EXPR || code1 == GE_EXPR)
1879 && (code2 == LT_EXPR || code2 == LE_EXPR))
1880 return boolean_false_node;
1883 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
1884 NAME's definition is a truth value. See if there are any simplifications
1885 that can be done against the NAME's definition. */
1886 if (TREE_CODE (op1a) == SSA_NAME
1887 && (code1 == NE_EXPR || code1 == EQ_EXPR)
1888 && (integer_zerop (op1b) || integer_onep (op1b)))
1890 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
1891 || (code1 == NE_EXPR && integer_onep (op1b)));
1892 gimple stmt = SSA_NAME_DEF_STMT (op1a);
1893 switch (gimple_code (stmt))
1895 case GIMPLE_ASSIGN:
1896 /* Try to simplify by copy-propagating the definition. */
1897 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
1899 case GIMPLE_PHI:
1900 /* If every argument to the PHI produces the same result when
1901 ANDed with the second comparison, we win.
1902 Do not do this unless the type is bool since we need a bool
1903 result here anyway. */
1904 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
1906 tree result = NULL_TREE;
1907 unsigned i;
1908 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1910 tree arg = gimple_phi_arg_def (stmt, i);
1912 /* If this PHI has itself as an argument, ignore it.
1913 If all the other args produce the same result,
1914 we're still OK. */
1915 if (arg == gimple_phi_result (stmt))
1916 continue;
1917 else if (TREE_CODE (arg) == INTEGER_CST)
1919 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
1921 if (!result)
1922 result = boolean_false_node;
1923 else if (!integer_zerop (result))
1924 return NULL_TREE;
1926 else if (!result)
1927 result = fold_build2 (code2, boolean_type_node,
1928 op2a, op2b);
1929 else if (!same_bool_comparison_p (result,
1930 code2, op2a, op2b))
1931 return NULL_TREE;
1933 else if (TREE_CODE (arg) == SSA_NAME
1934 && !SSA_NAME_IS_DEFAULT_DEF (arg))
1936 tree temp;
1937 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
1938 /* In simple cases we can look through PHI nodes,
1939 but we have to be careful with loops.
1940 See PR49073. */
1941 if (! dom_info_available_p (CDI_DOMINATORS)
1942 || gimple_bb (def_stmt) == gimple_bb (stmt)
1943 || dominated_by_p (CDI_DOMINATORS,
1944 gimple_bb (def_stmt),
1945 gimple_bb (stmt)))
1946 return NULL_TREE;
1947 temp = and_var_with_comparison (arg, invert, code2,
1948 op2a, op2b);
1949 if (!temp)
1950 return NULL_TREE;
1951 else if (!result)
1952 result = temp;
1953 else if (!same_bool_result_p (result, temp))
1954 return NULL_TREE;
1956 else
1957 return NULL_TREE;
1959 return result;
1962 default:
1963 break;
1966 return NULL_TREE;
1969 /* Try to simplify the AND of two comparisons, specified by
1970 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
1971 If this can be simplified to a single expression (without requiring
1972 introducing more SSA variables to hold intermediate values),
1973 return the resulting tree. Otherwise return NULL_TREE.
1974 If the result expression is non-null, it has boolean type. */
1976 tree
1977 maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
1978 enum tree_code code2, tree op2a, tree op2b)
1980 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
1981 if (t)
1982 return t;
1983 else
1984 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
1987 /* Helper function for or_comparisons_1: try to simplify the OR of the
1988 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
1989 If INVERT is true, invert the value of VAR before doing the OR.
1990 Return NULL_EXPR if we can't simplify this to a single expression. */
1992 static tree
1993 or_var_with_comparison (tree var, bool invert,
1994 enum tree_code code2, tree op2a, tree op2b)
1996 tree t;
1997 gimple stmt = SSA_NAME_DEF_STMT (var);
1999 /* We can only deal with variables whose definitions are assignments. */
2000 if (!is_gimple_assign (stmt))
2001 return NULL_TREE;
2003 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
2004 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
2005 Then we only have to consider the simpler non-inverted cases. */
2006 if (invert)
2007 t = and_var_with_comparison_1 (stmt,
2008 invert_tree_comparison (code2, false),
2009 op2a, op2b);
2010 else
2011 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
2012 return canonicalize_bool (t, invert);
2015 /* Try to simplify the OR of the ssa variable defined by the assignment
2016 STMT with the comparison specified by (OP2A CODE2 OP2B).
2017 Return NULL_EXPR if we can't simplify this to a single expression. */
2019 static tree
2020 or_var_with_comparison_1 (gimple stmt,
2021 enum tree_code code2, tree op2a, tree op2b)
2023 tree var = gimple_assign_lhs (stmt);
2024 tree true_test_var = NULL_TREE;
2025 tree false_test_var = NULL_TREE;
2026 enum tree_code innercode = gimple_assign_rhs_code (stmt);
2028 /* Check for identities like (var OR (var != 0)) => true . */
2029 if (TREE_CODE (op2a) == SSA_NAME
2030 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
2032 if ((code2 == NE_EXPR && integer_zerop (op2b))
2033 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
2035 true_test_var = op2a;
2036 if (var == true_test_var)
2037 return var;
2039 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
2040 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
2042 false_test_var = op2a;
2043 if (var == false_test_var)
2044 return boolean_true_node;
2048 /* If the definition is a comparison, recurse on it. */
2049 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
2051 tree t = or_comparisons_1 (innercode,
2052 gimple_assign_rhs1 (stmt),
2053 gimple_assign_rhs2 (stmt),
2054 code2,
2055 op2a,
2056 op2b);
2057 if (t)
2058 return t;
2061 /* If the definition is an AND or OR expression, we may be able to
2062 simplify by reassociating. */
2063 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
2064 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
2066 tree inner1 = gimple_assign_rhs1 (stmt);
2067 tree inner2 = gimple_assign_rhs2 (stmt);
2068 gimple s;
2069 tree t;
2070 tree partial = NULL_TREE;
2071 bool is_or = (innercode == BIT_IOR_EXPR);
2073 /* Check for boolean identities that don't require recursive examination
2074 of inner1/inner2:
2075 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
2076 inner1 OR (inner1 AND inner2) => inner1
2077 !inner1 OR (inner1 OR inner2) => true
2078 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
2080 if (inner1 == true_test_var)
2081 return (is_or ? var : inner1);
2082 else if (inner2 == true_test_var)
2083 return (is_or ? var : inner2);
2084 else if (inner1 == false_test_var)
2085 return (is_or
2086 ? boolean_true_node
2087 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
2088 else if (inner2 == false_test_var)
2089 return (is_or
2090 ? boolean_true_node
2091 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
2093 /* Next, redistribute/reassociate the OR across the inner tests.
2094 Compute the first partial result, (inner1 OR (op2a code op2b)) */
2095 if (TREE_CODE (inner1) == SSA_NAME
2096 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
2097 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
2098 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
2099 gimple_assign_rhs1 (s),
2100 gimple_assign_rhs2 (s),
2101 code2, op2a, op2b)))
2103 /* Handle the OR case, where we are reassociating:
2104 (inner1 OR inner2) OR (op2a code2 op2b)
2105 => (t OR inner2)
2106 If the partial result t is a constant, we win. Otherwise
2107 continue on to try reassociating with the other inner test. */
2108 if (is_or)
2110 if (integer_onep (t))
2111 return boolean_true_node;
2112 else if (integer_zerop (t))
2113 return inner2;
2116 /* Handle the AND case, where we are redistributing:
2117 (inner1 AND inner2) OR (op2a code2 op2b)
2118 => (t AND (inner2 OR (op2a code op2b))) */
2119 else if (integer_zerop (t))
2120 return boolean_false_node;
2122 /* Save partial result for later. */
2123 partial = t;
2126 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
2127 if (TREE_CODE (inner2) == SSA_NAME
2128 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
2129 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
2130 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
2131 gimple_assign_rhs1 (s),
2132 gimple_assign_rhs2 (s),
2133 code2, op2a, op2b)))
2135 /* Handle the OR case, where we are reassociating:
2136 (inner1 OR inner2) OR (op2a code2 op2b)
2137 => (inner1 OR t)
2138 => (t OR partial) */
2139 if (is_or)
2141 if (integer_zerop (t))
2142 return inner1;
2143 else if (integer_onep (t))
2144 return boolean_true_node;
2145 /* If both are the same, we can apply the identity
2146 (x OR x) == x. */
2147 else if (partial && same_bool_result_p (t, partial))
2148 return t;
2151 /* Handle the AND case, where we are redistributing:
2152 (inner1 AND inner2) OR (op2a code2 op2b)
2153 => (t AND (inner1 OR (op2a code2 op2b)))
2154 => (t AND partial) */
2155 else
2157 if (integer_zerop (t))
2158 return boolean_false_node;
2159 else if (partial)
2161 /* We already got a simplification for the other
2162 operand to the redistributed AND expression. The
2163 interesting case is when at least one is true.
2164 Or, if both are the same, we can apply the identity
2165 (x AND x) == x. */
2166 if (integer_onep (partial))
2167 return t;
2168 else if (integer_onep (t))
2169 return partial;
2170 else if (same_bool_result_p (t, partial))
2171 return t;
2176 return NULL_TREE;
2179 /* Try to simplify the OR of two comparisons defined by
2180 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
2181 If this can be done without constructing an intermediate value,
2182 return the resulting tree; otherwise NULL_TREE is returned.
2183 This function is deliberately asymmetric as it recurses on SSA_DEFs
2184 in the first comparison but not the second. */
2186 static tree
2187 or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
2188 enum tree_code code2, tree op2a, tree op2b)
2190 tree truth_type = truth_type_for (TREE_TYPE (op1a));
2192 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
2193 if (operand_equal_p (op1a, op2a, 0)
2194 && operand_equal_p (op1b, op2b, 0))
2196 /* Result will be either NULL_TREE, or a combined comparison. */
2197 tree t = combine_comparisons (UNKNOWN_LOCATION,
2198 TRUTH_ORIF_EXPR, code1, code2,
2199 truth_type, op1a, op1b);
2200 if (t)
2201 return t;
2204 /* Likewise the swapped case of the above. */
2205 if (operand_equal_p (op1a, op2b, 0)
2206 && operand_equal_p (op1b, op2a, 0))
2208 /* Result will be either NULL_TREE, or a combined comparison. */
2209 tree t = combine_comparisons (UNKNOWN_LOCATION,
2210 TRUTH_ORIF_EXPR, code1,
2211 swap_tree_comparison (code2),
2212 truth_type, op1a, op1b);
2213 if (t)
2214 return t;
2217 /* If both comparisons are of the same value against constants, we might
2218 be able to merge them. */
2219 if (operand_equal_p (op1a, op2a, 0)
2220 && TREE_CODE (op1b) == INTEGER_CST
2221 && TREE_CODE (op2b) == INTEGER_CST)
2223 int cmp = tree_int_cst_compare (op1b, op2b);
2225 /* If we have (op1a != op1b), we should either be able to
2226 return that or TRUE, depending on whether the constant op1b
2227 also satisfies the other comparison against op2b. */
2228 if (code1 == NE_EXPR)
2230 bool done = true;
2231 bool val;
2232 switch (code2)
2234 case EQ_EXPR: val = (cmp == 0); break;
2235 case NE_EXPR: val = (cmp != 0); break;
2236 case LT_EXPR: val = (cmp < 0); break;
2237 case GT_EXPR: val = (cmp > 0); break;
2238 case LE_EXPR: val = (cmp <= 0); break;
2239 case GE_EXPR: val = (cmp >= 0); break;
2240 default: done = false;
2242 if (done)
2244 if (val)
2245 return boolean_true_node;
2246 else
2247 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2250 /* Likewise if the second comparison is a != comparison. */
2251 else if (code2 == NE_EXPR)
2253 bool done = true;
2254 bool val;
2255 switch (code1)
2257 case EQ_EXPR: val = (cmp == 0); break;
2258 case NE_EXPR: val = (cmp != 0); break;
2259 case LT_EXPR: val = (cmp > 0); break;
2260 case GT_EXPR: val = (cmp < 0); break;
2261 case LE_EXPR: val = (cmp >= 0); break;
2262 case GE_EXPR: val = (cmp <= 0); break;
2263 default: done = false;
2265 if (done)
2267 if (val)
2268 return boolean_true_node;
2269 else
2270 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2274 /* See if an equality test is redundant with the other comparison. */
2275 else if (code1 == EQ_EXPR)
2277 bool val;
2278 switch (code2)
2280 case EQ_EXPR: val = (cmp == 0); break;
2281 case NE_EXPR: val = (cmp != 0); break;
2282 case LT_EXPR: val = (cmp < 0); break;
2283 case GT_EXPR: val = (cmp > 0); break;
2284 case LE_EXPR: val = (cmp <= 0); break;
2285 case GE_EXPR: val = (cmp >= 0); break;
2286 default:
2287 val = false;
2289 if (val)
2290 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2292 else if (code2 == EQ_EXPR)
2294 bool val;
2295 switch (code1)
2297 case EQ_EXPR: val = (cmp == 0); break;
2298 case NE_EXPR: val = (cmp != 0); break;
2299 case LT_EXPR: val = (cmp > 0); break;
2300 case GT_EXPR: val = (cmp < 0); break;
2301 case LE_EXPR: val = (cmp >= 0); break;
2302 case GE_EXPR: val = (cmp <= 0); break;
2303 default:
2304 val = false;
2306 if (val)
2307 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2310 /* Chose the less restrictive of two < or <= comparisons. */
2311 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
2312 && (code2 == LT_EXPR || code2 == LE_EXPR))
2314 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2315 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2316 else
2317 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2320 /* Likewise chose the less restrictive of two > or >= comparisons. */
2321 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
2322 && (code2 == GT_EXPR || code2 == GE_EXPR))
2324 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2325 return fold_build2 (code2, boolean_type_node, op2a, op2b);
2326 else
2327 return fold_build2 (code1, boolean_type_node, op1a, op1b);
2330 /* Check for singleton ranges. */
2331 else if (cmp == 0
2332 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2333 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2334 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
2336 /* Check for less/greater pairs that don't restrict the range at all. */
2337 else if (cmp >= 0
2338 && (code1 == LT_EXPR || code1 == LE_EXPR)
2339 && (code2 == GT_EXPR || code2 == GE_EXPR))
2340 return boolean_true_node;
2341 else if (cmp <= 0
2342 && (code1 == GT_EXPR || code1 == GE_EXPR)
2343 && (code2 == LT_EXPR || code2 == LE_EXPR))
2344 return boolean_true_node;
2347 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
2348 NAME's definition is a truth value. See if there are any simplifications
2349 that can be done against the NAME's definition. */
2350 if (TREE_CODE (op1a) == SSA_NAME
2351 && (code1 == NE_EXPR || code1 == EQ_EXPR)
2352 && (integer_zerop (op1b) || integer_onep (op1b)))
2354 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
2355 || (code1 == NE_EXPR && integer_onep (op1b)));
2356 gimple stmt = SSA_NAME_DEF_STMT (op1a);
2357 switch (gimple_code (stmt))
2359 case GIMPLE_ASSIGN:
2360 /* Try to simplify by copy-propagating the definition. */
2361 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
2363 case GIMPLE_PHI:
2364 /* If every argument to the PHI produces the same result when
2365 ORed with the second comparison, we win.
2366 Do not do this unless the type is bool since we need a bool
2367 result here anyway. */
2368 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
2370 tree result = NULL_TREE;
2371 unsigned i;
2372 for (i = 0; i < gimple_phi_num_args (stmt); i++)
2374 tree arg = gimple_phi_arg_def (stmt, i);
2376 /* If this PHI has itself as an argument, ignore it.
2377 If all the other args produce the same result,
2378 we're still OK. */
2379 if (arg == gimple_phi_result (stmt))
2380 continue;
2381 else if (TREE_CODE (arg) == INTEGER_CST)
2383 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
2385 if (!result)
2386 result = boolean_true_node;
2387 else if (!integer_onep (result))
2388 return NULL_TREE;
2390 else if (!result)
2391 result = fold_build2 (code2, boolean_type_node,
2392 op2a, op2b);
2393 else if (!same_bool_comparison_p (result,
2394 code2, op2a, op2b))
2395 return NULL_TREE;
2397 else if (TREE_CODE (arg) == SSA_NAME
2398 && !SSA_NAME_IS_DEFAULT_DEF (arg))
2400 tree temp;
2401 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
2402 /* In simple cases we can look through PHI nodes,
2403 but we have to be careful with loops.
2404 See PR49073. */
2405 if (! dom_info_available_p (CDI_DOMINATORS)
2406 || gimple_bb (def_stmt) == gimple_bb (stmt)
2407 || dominated_by_p (CDI_DOMINATORS,
2408 gimple_bb (def_stmt),
2409 gimple_bb (stmt)))
2410 return NULL_TREE;
2411 temp = or_var_with_comparison (arg, invert, code2,
2412 op2a, op2b);
2413 if (!temp)
2414 return NULL_TREE;
2415 else if (!result)
2416 result = temp;
2417 else if (!same_bool_result_p (result, temp))
2418 return NULL_TREE;
2420 else
2421 return NULL_TREE;
2423 return result;
2426 default:
2427 break;
2430 return NULL_TREE;
2433 /* Try to simplify the OR of two comparisons, specified by
2434 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
2435 If this can be simplified to a single expression (without requiring
2436 introducing more SSA variables to hold intermediate values),
2437 return the resulting tree. Otherwise return NULL_TREE.
2438 If the result expression is non-null, it has boolean type. */
2440 tree
2441 maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
2442 enum tree_code code2, tree op2a, tree op2b)
2444 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
2445 if (t)
2446 return t;
2447 else
2448 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
2452 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2454 Either NULL_TREE, a simplified but non-constant or a constant
2455 is returned.
2457 ??? This should go into a gimple-fold-inline.h file to be eventually
2458 privatized with the single valueize function used in the various TUs
2459 to avoid the indirect function call overhead. */
2461 tree
2462 gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree))
2464 location_t loc = gimple_location (stmt);
2465 switch (gimple_code (stmt))
2467 case GIMPLE_ASSIGN:
2469 enum tree_code subcode = gimple_assign_rhs_code (stmt);
2471 switch (get_gimple_rhs_class (subcode))
2473 case GIMPLE_SINGLE_RHS:
2475 tree rhs = gimple_assign_rhs1 (stmt);
2476 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
2478 if (TREE_CODE (rhs) == SSA_NAME)
2480 /* If the RHS is an SSA_NAME, return its known constant value,
2481 if any. */
2482 return (*valueize) (rhs);
2484 /* Handle propagating invariant addresses into address
2485 operations. */
2486 else if (TREE_CODE (rhs) == ADDR_EXPR
2487 && !is_gimple_min_invariant (rhs))
2489 HOST_WIDE_INT offset = 0;
2490 tree base;
2491 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
2492 &offset,
2493 valueize);
2494 if (base
2495 && (CONSTANT_CLASS_P (base)
2496 || decl_address_invariant_p (base)))
2497 return build_invariant_address (TREE_TYPE (rhs),
2498 base, offset);
2500 else if (TREE_CODE (rhs) == CONSTRUCTOR
2501 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
2502 && (CONSTRUCTOR_NELTS (rhs)
2503 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
2505 unsigned i;
2506 tree val, *vec;
2508 vec = XALLOCAVEC (tree,
2509 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)));
2510 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
2512 val = (*valueize) (val);
2513 if (TREE_CODE (val) == INTEGER_CST
2514 || TREE_CODE (val) == REAL_CST
2515 || TREE_CODE (val) == FIXED_CST)
2516 vec[i] = val;
2517 else
2518 return NULL_TREE;
2521 return build_vector (TREE_TYPE (rhs), vec);
2524 if (kind == tcc_reference)
2526 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
2527 || TREE_CODE (rhs) == REALPART_EXPR
2528 || TREE_CODE (rhs) == IMAGPART_EXPR)
2529 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
2531 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
2532 return fold_unary_loc (EXPR_LOCATION (rhs),
2533 TREE_CODE (rhs),
2534 TREE_TYPE (rhs), val);
2536 else if (TREE_CODE (rhs) == BIT_FIELD_REF
2537 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
2539 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
2540 return fold_ternary_loc (EXPR_LOCATION (rhs),
2541 TREE_CODE (rhs),
2542 TREE_TYPE (rhs), val,
2543 TREE_OPERAND (rhs, 1),
2544 TREE_OPERAND (rhs, 2));
2546 else if (TREE_CODE (rhs) == MEM_REF
2547 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
2549 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
2550 if (TREE_CODE (val) == ADDR_EXPR
2551 && is_gimple_min_invariant (val))
2553 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
2554 unshare_expr (val),
2555 TREE_OPERAND (rhs, 1));
2556 if (tem)
2557 rhs = tem;
2560 return fold_const_aggregate_ref_1 (rhs, valueize);
2562 else if (kind == tcc_declaration)
2563 return get_symbol_constant_value (rhs);
2564 return rhs;
2567 case GIMPLE_UNARY_RHS:
2569 /* Handle unary operators that can appear in GIMPLE form.
2570 Note that we know the single operand must be a constant,
2571 so this should almost always return a simplified RHS. */
2572 tree lhs = gimple_assign_lhs (stmt);
2573 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
2575 /* Conversions are useless for CCP purposes if they are
2576 value-preserving. Thus the restrictions that
2577 useless_type_conversion_p places for restrict qualification
2578 of pointer types should not apply here.
2579 Substitution later will only substitute to allowed places. */
2580 if (CONVERT_EXPR_CODE_P (subcode)
2581 && POINTER_TYPE_P (TREE_TYPE (lhs))
2582 && POINTER_TYPE_P (TREE_TYPE (op0))
2583 && TYPE_ADDR_SPACE (TREE_TYPE (lhs))
2584 == TYPE_ADDR_SPACE (TREE_TYPE (op0))
2585 && TYPE_MODE (TREE_TYPE (lhs))
2586 == TYPE_MODE (TREE_TYPE (op0)))
2587 return op0;
2589 return
2590 fold_unary_ignore_overflow_loc (loc, subcode,
2591 gimple_expr_type (stmt), op0);
2594 case GIMPLE_BINARY_RHS:
2596 /* Handle binary operators that can appear in GIMPLE form. */
2597 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
2598 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
2600 /* Translate &x + CST into an invariant form suitable for
2601 further propagation. */
2602 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
2603 && TREE_CODE (op0) == ADDR_EXPR
2604 && TREE_CODE (op1) == INTEGER_CST)
2606 tree off = fold_convert (ptr_type_node, op1);
2607 return build_fold_addr_expr_loc
2608 (loc,
2609 fold_build2 (MEM_REF,
2610 TREE_TYPE (TREE_TYPE (op0)),
2611 unshare_expr (op0), off));
2614 return fold_binary_loc (loc, subcode,
2615 gimple_expr_type (stmt), op0, op1);
2618 case GIMPLE_TERNARY_RHS:
2620 /* Handle ternary operators that can appear in GIMPLE form. */
2621 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
2622 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
2623 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
2625 /* Fold embedded expressions in ternary codes. */
2626 if ((subcode == COND_EXPR
2627 || subcode == VEC_COND_EXPR)
2628 && COMPARISON_CLASS_P (op0))
2630 tree op00 = (*valueize) (TREE_OPERAND (op0, 0));
2631 tree op01 = (*valueize) (TREE_OPERAND (op0, 1));
2632 tree tem = fold_binary_loc (loc, TREE_CODE (op0),
2633 TREE_TYPE (op0), op00, op01);
2634 if (tem)
2635 op0 = tem;
2638 return fold_ternary_loc (loc, subcode,
2639 gimple_expr_type (stmt), op0, op1, op2);
2642 default:
2643 gcc_unreachable ();
2647 case GIMPLE_CALL:
2649 tree fn;
2651 if (gimple_call_internal_p (stmt))
2652 /* No folding yet for these functions. */
2653 return NULL_TREE;
2655 fn = (*valueize) (gimple_call_fn (stmt));
2656 if (TREE_CODE (fn) == ADDR_EXPR
2657 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
2658 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
2660 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
2661 tree call, retval;
2662 unsigned i;
2663 for (i = 0; i < gimple_call_num_args (stmt); ++i)
2664 args[i] = (*valueize) (gimple_call_arg (stmt, i));
2665 call = build_call_array_loc (loc,
2666 gimple_call_return_type (stmt),
2667 fn, gimple_call_num_args (stmt), args);
2668 retval = fold_call_expr (EXPR_LOCATION (call), call, false);
2669 if (retval)
2670 /* fold_call_expr wraps the result inside a NOP_EXPR. */
2671 STRIP_NOPS (retval);
2672 return retval;
2674 return NULL_TREE;
2677 default:
2678 return NULL_TREE;
2682 /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
2683 Returns NULL_TREE if folding to a constant is not possible, otherwise
2684 returns a constant according to is_gimple_min_invariant. */
2686 tree
2687 gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree))
2689 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
2690 if (res && is_gimple_min_invariant (res))
2691 return res;
2692 return NULL_TREE;
2696 /* The following set of functions are supposed to fold references using
2697 their constant initializers. */
2699 static tree fold_ctor_reference (tree type, tree ctor,
2700 unsigned HOST_WIDE_INT offset,
2701 unsigned HOST_WIDE_INT size, tree);
2703 /* See if we can find constructor defining value of BASE.
2704 When we know the consructor with constant offset (such as
2705 base is array[40] and we do know constructor of array), then
2706 BIT_OFFSET is adjusted accordingly.
2708 As a special case, return error_mark_node when constructor
2709 is not explicitly available, but it is known to be zero
2710 such as 'static const int a;'. */
2711 static tree
2712 get_base_constructor (tree base, HOST_WIDE_INT *bit_offset,
2713 tree (*valueize)(tree))
2715 HOST_WIDE_INT bit_offset2, size, max_size;
2716 if (TREE_CODE (base) == MEM_REF)
2718 if (!integer_zerop (TREE_OPERAND (base, 1)))
2720 if (!host_integerp (TREE_OPERAND (base, 1), 0))
2721 return NULL_TREE;
2722 *bit_offset += (mem_ref_offset (base).low
2723 * BITS_PER_UNIT);
2726 if (valueize
2727 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2728 base = valueize (TREE_OPERAND (base, 0));
2729 if (!base || TREE_CODE (base) != ADDR_EXPR)
2730 return NULL_TREE;
2731 base = TREE_OPERAND (base, 0);
2734 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
2735 DECL_INITIAL. If BASE is a nested reference into another
2736 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
2737 the inner reference. */
2738 switch (TREE_CODE (base))
2740 case VAR_DECL:
2741 case CONST_DECL:
2743 tree init = ctor_for_folding (base);
2745 /* Our semantic is exact opposite of ctor_for_folding;
2746 NULL means unknown, while error_mark_node is 0. */
2747 if (init == error_mark_node)
2748 return NULL_TREE;
2749 if (!init)
2750 return error_mark_node;
2751 return init;
2754 case ARRAY_REF:
2755 case COMPONENT_REF:
2756 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size);
2757 if (max_size == -1 || size != max_size)
2758 return NULL_TREE;
2759 *bit_offset += bit_offset2;
2760 return get_base_constructor (base, bit_offset, valueize);
2762 case STRING_CST:
2763 case CONSTRUCTOR:
2764 return base;
2766 default:
2767 return NULL_TREE;
2771 /* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE
2772 to the memory at bit OFFSET.
2774 We do only simple job of folding byte accesses. */
2776 static tree
2777 fold_string_cst_ctor_reference (tree type, tree ctor,
2778 unsigned HOST_WIDE_INT offset,
2779 unsigned HOST_WIDE_INT size)
2781 if (INTEGRAL_TYPE_P (type)
2782 && (TYPE_MODE (type)
2783 == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
2784 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
2785 == MODE_INT)
2786 && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1
2787 && size == BITS_PER_UNIT
2788 && !(offset % BITS_PER_UNIT))
2790 offset /= BITS_PER_UNIT;
2791 if (offset < (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (ctor))
2792 return build_int_cst_type (type, (TREE_STRING_POINTER (ctor)
2793 [offset]));
2794 /* Folding
2795 const char a[20]="hello";
2796 return a[10];
2798 might lead to offset greater than string length. In this case we
2799 know value is either initialized to 0 or out of bounds. Return 0
2800 in both cases. */
2801 return build_zero_cst (type);
2803 return NULL_TREE;
2806 /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
2807 SIZE to the memory at bit OFFSET. */
2809 static tree
2810 fold_array_ctor_reference (tree type, tree ctor,
2811 unsigned HOST_WIDE_INT offset,
2812 unsigned HOST_WIDE_INT size,
2813 tree from_decl)
2815 unsigned HOST_WIDE_INT cnt;
2816 tree cfield, cval;
2817 double_int low_bound, elt_size;
2818 double_int index, max_index;
2819 double_int access_index;
2820 tree domain_type = NULL_TREE, index_type = NULL_TREE;
2821 HOST_WIDE_INT inner_offset;
2823 /* Compute low bound and elt size. */
2824 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
2825 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
2826 if (domain_type && TYPE_MIN_VALUE (domain_type))
2828 /* Static constructors for variably sized objects makes no sense. */
2829 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
2830 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
2831 low_bound = tree_to_double_int (TYPE_MIN_VALUE (domain_type));
2833 else
2834 low_bound = double_int_zero;
2835 /* Static constructors for variably sized objects makes no sense. */
2836 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))))
2837 == INTEGER_CST);
2838 elt_size =
2839 tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
2842 /* We can handle only constantly sized accesses that are known to not
2843 be larger than size of array element. */
2844 if (!TYPE_SIZE_UNIT (type)
2845 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
2846 || elt_size.slt (tree_to_double_int (TYPE_SIZE_UNIT (type))))
2847 return NULL_TREE;
2849 /* Compute the array index we look for. */
2850 access_index = double_int::from_uhwi (offset / BITS_PER_UNIT)
2851 .udiv (elt_size, TRUNC_DIV_EXPR);
2852 access_index += low_bound;
2853 if (index_type)
2854 access_index = access_index.ext (TYPE_PRECISION (index_type),
2855 TYPE_UNSIGNED (index_type));
2857 /* And offset within the access. */
2858 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
2860 /* See if the array field is large enough to span whole access. We do not
2861 care to fold accesses spanning multiple array indexes. */
2862 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
2863 return NULL_TREE;
2865 index = low_bound - double_int_one;
2866 if (index_type)
2867 index = index.ext (TYPE_PRECISION (index_type), TYPE_UNSIGNED (index_type));
2869 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
2871 /* Array constructor might explicitely set index, or specify range
2872 or leave index NULL meaning that it is next index after previous
2873 one. */
2874 if (cfield)
2876 if (TREE_CODE (cfield) == INTEGER_CST)
2877 max_index = index = tree_to_double_int (cfield);
2878 else
2880 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
2881 index = tree_to_double_int (TREE_OPERAND (cfield, 0));
2882 max_index = tree_to_double_int (TREE_OPERAND (cfield, 1));
2885 else
2887 index += double_int_one;
2888 if (index_type)
2889 index = index.ext (TYPE_PRECISION (index_type),
2890 TYPE_UNSIGNED (index_type));
2891 max_index = index;
2894 /* Do we have match? */
2895 if (access_index.cmp (index, 1) >= 0
2896 && access_index.cmp (max_index, 1) <= 0)
2897 return fold_ctor_reference (type, cval, inner_offset, size,
2898 from_decl);
2900 /* When memory is not explicitely mentioned in constructor,
2901 it is 0 (or out of range). */
2902 return build_zero_cst (type);
2905 /* CTOR is CONSTRUCTOR of an aggregate or vector.
2906 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
2908 static tree
2909 fold_nonarray_ctor_reference (tree type, tree ctor,
2910 unsigned HOST_WIDE_INT offset,
2911 unsigned HOST_WIDE_INT size,
2912 tree from_decl)
2914 unsigned HOST_WIDE_INT cnt;
2915 tree cfield, cval;
2917 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
2918 cval)
2920 tree byte_offset = DECL_FIELD_OFFSET (cfield);
2921 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
2922 tree field_size = DECL_SIZE (cfield);
2923 double_int bitoffset;
2924 double_int byte_offset_cst = tree_to_double_int (byte_offset);
2925 double_int bits_per_unit_cst = double_int::from_uhwi (BITS_PER_UNIT);
2926 double_int bitoffset_end, access_end;
2928 /* Variable sized objects in static constructors makes no sense,
2929 but field_size can be NULL for flexible array members. */
2930 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
2931 && TREE_CODE (byte_offset) == INTEGER_CST
2932 && (field_size != NULL_TREE
2933 ? TREE_CODE (field_size) == INTEGER_CST
2934 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
2936 /* Compute bit offset of the field. */
2937 bitoffset = tree_to_double_int (field_offset)
2938 + byte_offset_cst * bits_per_unit_cst;
2939 /* Compute bit offset where the field ends. */
2940 if (field_size != NULL_TREE)
2941 bitoffset_end = bitoffset + tree_to_double_int (field_size);
2942 else
2943 bitoffset_end = double_int_zero;
2945 access_end = double_int::from_uhwi (offset)
2946 + double_int::from_uhwi (size);
2948 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
2949 [BITOFFSET, BITOFFSET_END)? */
2950 if (access_end.cmp (bitoffset, 0) > 0
2951 && (field_size == NULL_TREE
2952 || double_int::from_uhwi (offset).slt (bitoffset_end)))
2954 double_int inner_offset = double_int::from_uhwi (offset) - bitoffset;
2955 /* We do have overlap. Now see if field is large enough to
2956 cover the access. Give up for accesses spanning multiple
2957 fields. */
2958 if (access_end.cmp (bitoffset_end, 0) > 0)
2959 return NULL_TREE;
2960 if (double_int::from_uhwi (offset).slt (bitoffset))
2961 return NULL_TREE;
2962 return fold_ctor_reference (type, cval,
2963 inner_offset.to_uhwi (), size,
2964 from_decl);
2967 /* When memory is not explicitely mentioned in constructor, it is 0. */
2968 return build_zero_cst (type);
2971 /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE
2972 to the memory at bit OFFSET. */
2974 static tree
2975 fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset,
2976 unsigned HOST_WIDE_INT size, tree from_decl)
2978 tree ret;
2980 /* We found the field with exact match. */
2981 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
2982 && !offset)
2983 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
2985 /* We are at the end of walk, see if we can view convert the
2986 result. */
2987 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
2988 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
2989 && operand_equal_p (TYPE_SIZE (type),
2990 TYPE_SIZE (TREE_TYPE (ctor)), 0))
2992 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
2993 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
2994 if (ret)
2995 STRIP_NOPS (ret);
2996 return ret;
2998 if (TREE_CODE (ctor) == STRING_CST)
2999 return fold_string_cst_ctor_reference (type, ctor, offset, size);
3000 if (TREE_CODE (ctor) == CONSTRUCTOR)
3003 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
3004 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
3005 return fold_array_ctor_reference (type, ctor, offset, size,
3006 from_decl);
3007 else
3008 return fold_nonarray_ctor_reference (type, ctor, offset, size,
3009 from_decl);
3012 return NULL_TREE;
3015 /* Return the tree representing the element referenced by T if T is an
3016 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
3017 names using VALUEIZE. Return NULL_TREE otherwise. */
3019 tree
3020 fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
3022 tree ctor, idx, base;
3023 HOST_WIDE_INT offset, size, max_size;
3024 tree tem;
3026 if (TREE_THIS_VOLATILE (t))
3027 return NULL_TREE;
3029 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
3030 return get_symbol_constant_value (t);
3032 tem = fold_read_from_constant_string (t);
3033 if (tem)
3034 return tem;
3036 switch (TREE_CODE (t))
3038 case ARRAY_REF:
3039 case ARRAY_RANGE_REF:
3040 /* Constant indexes are handled well by get_base_constructor.
3041 Only special case variable offsets.
3042 FIXME: This code can't handle nested references with variable indexes
3043 (they will be handled only by iteration of ccp). Perhaps we can bring
3044 get_ref_base_and_extent here and make it use a valueize callback. */
3045 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
3046 && valueize
3047 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
3048 && TREE_CODE (idx) == INTEGER_CST)
3050 tree low_bound, unit_size;
3051 double_int doffset;
3053 /* If the resulting bit-offset is constant, track it. */
3054 if ((low_bound = array_ref_low_bound (t),
3055 TREE_CODE (low_bound) == INTEGER_CST)
3056 && (unit_size = array_ref_element_size (t),
3057 host_integerp (unit_size, 1))
3058 && (doffset = (TREE_INT_CST (idx) - TREE_INT_CST (low_bound))
3059 .sext (TYPE_PRECISION (TREE_TYPE (idx))),
3060 doffset.fits_shwi ()))
3062 offset = doffset.to_shwi ();
3063 offset *= TREE_INT_CST_LOW (unit_size);
3064 offset *= BITS_PER_UNIT;
3066 base = TREE_OPERAND (t, 0);
3067 ctor = get_base_constructor (base, &offset, valueize);
3068 /* Empty constructor. Always fold to 0. */
3069 if (ctor == error_mark_node)
3070 return build_zero_cst (TREE_TYPE (t));
3071 /* Out of bound array access. Value is undefined,
3072 but don't fold. */
3073 if (offset < 0)
3074 return NULL_TREE;
3075 /* We can not determine ctor. */
3076 if (!ctor)
3077 return NULL_TREE;
3078 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
3079 TREE_INT_CST_LOW (unit_size)
3080 * BITS_PER_UNIT,
3081 base);
3084 /* Fallthru. */
3086 case COMPONENT_REF:
3087 case BIT_FIELD_REF:
3088 case TARGET_MEM_REF:
3089 case MEM_REF:
3090 base = get_ref_base_and_extent (t, &offset, &size, &max_size);
3091 ctor = get_base_constructor (base, &offset, valueize);
3093 /* Empty constructor. Always fold to 0. */
3094 if (ctor == error_mark_node)
3095 return build_zero_cst (TREE_TYPE (t));
3096 /* We do not know precise address. */
3097 if (max_size == -1 || max_size != size)
3098 return NULL_TREE;
3099 /* We can not determine ctor. */
3100 if (!ctor)
3101 return NULL_TREE;
3103 /* Out of bound array access. Value is undefined, but don't fold. */
3104 if (offset < 0)
3105 return NULL_TREE;
3107 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
3108 base);
3110 case REALPART_EXPR:
3111 case IMAGPART_EXPR:
3113 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
3114 if (c && TREE_CODE (c) == COMPLEX_CST)
3115 return fold_build1_loc (EXPR_LOCATION (t),
3116 TREE_CODE (t), TREE_TYPE (t), c);
3117 break;
3120 default:
3121 break;
3124 return NULL_TREE;
3127 tree
3128 fold_const_aggregate_ref (tree t)
3130 return fold_const_aggregate_ref_1 (t, NULL);
3133 /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
3134 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
3135 KNOWN_BINFO carries the binfo describing the true type of
3136 OBJ_TYPE_REF_OBJECT(REF). */
3138 tree
3139 gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo)
3141 unsigned HOST_WIDE_INT offset, size;
3142 tree v, fn, vtable, init;
3144 vtable = v = BINFO_VTABLE (known_binfo);
3145 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
3146 if (!v)
3147 return NULL_TREE;
3149 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
3151 offset = tree_low_cst (TREE_OPERAND (v, 1), 1) * BITS_PER_UNIT;
3152 v = TREE_OPERAND (v, 0);
3154 else
3155 offset = 0;
3157 if (TREE_CODE (v) != ADDR_EXPR)
3158 return NULL_TREE;
3159 v = TREE_OPERAND (v, 0);
3161 if (TREE_CODE (v) != VAR_DECL
3162 || !DECL_VIRTUAL_P (v))
3163 return NULL_TREE;
3164 init = ctor_for_folding (v);
3166 /* The virtual tables should always be born with constructors.
3167 and we always should assume that they are avaialble for
3168 folding. At the moment we do not stream them in all cases,
3169 but it should never happen that ctor seem unreachable. */
3170 gcc_assert (init);
3171 if (init == error_mark_node)
3173 gcc_assert (in_lto_p);
3174 return NULL_TREE;
3176 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
3177 size = tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))), 1);
3178 offset += token * size;
3179 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
3180 offset, size, v);
3181 if (!fn || integer_zerop (fn))
3182 return NULL_TREE;
3183 gcc_assert (TREE_CODE (fn) == ADDR_EXPR
3184 || TREE_CODE (fn) == FDESC_EXPR);
3185 fn = TREE_OPERAND (fn, 0);
3186 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
3188 /* When cgraph node is missing and function is not public, we cannot
3189 devirtualize. This can happen in WHOPR when the actual method
3190 ends up in other partition, because we found devirtualization
3191 possibility too late. */
3192 if (!can_refer_decl_in_current_unit_p (fn, vtable))
3193 return NULL_TREE;
3195 /* Make sure we create a cgraph node for functions we'll reference.
3196 They can be non-existent if the reference comes from an entry
3197 of an external vtable for example. */
3198 cgraph_get_create_node (fn);
3200 return fn;
3203 /* Return true iff VAL is a gimple expression that is known to be
3204 non-negative. Restricted to floating-point inputs. */
3206 bool
3207 gimple_val_nonnegative_real_p (tree val)
3209 gimple def_stmt;
3211 gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)));
3213 /* Use existing logic for non-gimple trees. */
3214 if (tree_expr_nonnegative_p (val))
3215 return true;
3217 if (TREE_CODE (val) != SSA_NAME)
3218 return false;
3220 /* Currently we look only at the immediately defining statement
3221 to make this determination, since recursion on defining
3222 statements of operands can lead to quadratic behavior in the
3223 worst case. This is expected to catch almost all occurrences
3224 in practice. It would be possible to implement limited-depth
3225 recursion if important cases are lost. Alternatively, passes
3226 that need this information (such as the pow/powi lowering code
3227 in the cse_sincos pass) could be revised to provide it through
3228 dataflow propagation. */
3230 def_stmt = SSA_NAME_DEF_STMT (val);
3232 if (is_gimple_assign (def_stmt))
3234 tree op0, op1;
3236 /* See fold-const.c:tree_expr_nonnegative_p for additional
3237 cases that could be handled with recursion. */
3239 switch (gimple_assign_rhs_code (def_stmt))
3241 case ABS_EXPR:
3242 /* Always true for floating-point operands. */
3243 return true;
3245 case MULT_EXPR:
3246 /* True if the two operands are identical (since we are
3247 restricted to floating-point inputs). */
3248 op0 = gimple_assign_rhs1 (def_stmt);
3249 op1 = gimple_assign_rhs2 (def_stmt);
3251 if (op0 == op1
3252 || operand_equal_p (op0, op1, 0))
3253 return true;
3255 default:
3256 return false;
3259 else if (is_gimple_call (def_stmt))
3261 tree fndecl = gimple_call_fndecl (def_stmt);
3262 if (fndecl
3263 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3265 tree arg1;
3267 switch (DECL_FUNCTION_CODE (fndecl))
3269 CASE_FLT_FN (BUILT_IN_ACOS):
3270 CASE_FLT_FN (BUILT_IN_ACOSH):
3271 CASE_FLT_FN (BUILT_IN_CABS):
3272 CASE_FLT_FN (BUILT_IN_COSH):
3273 CASE_FLT_FN (BUILT_IN_ERFC):
3274 CASE_FLT_FN (BUILT_IN_EXP):
3275 CASE_FLT_FN (BUILT_IN_EXP10):
3276 CASE_FLT_FN (BUILT_IN_EXP2):
3277 CASE_FLT_FN (BUILT_IN_FABS):
3278 CASE_FLT_FN (BUILT_IN_FDIM):
3279 CASE_FLT_FN (BUILT_IN_HYPOT):
3280 CASE_FLT_FN (BUILT_IN_POW10):
3281 return true;
3283 CASE_FLT_FN (BUILT_IN_SQRT):
3284 /* sqrt(-0.0) is -0.0, and sqrt is not defined over other
3285 nonnegative inputs. */
3286 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val))))
3287 return true;
3289 break;
3291 CASE_FLT_FN (BUILT_IN_POWI):
3292 /* True if the second argument is an even integer. */
3293 arg1 = gimple_call_arg (def_stmt, 1);
3295 if (TREE_CODE (arg1) == INTEGER_CST
3296 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
3297 return true;
3299 break;
3301 CASE_FLT_FN (BUILT_IN_POW):
3302 /* True if the second argument is an even integer-valued
3303 real. */
3304 arg1 = gimple_call_arg (def_stmt, 1);
3306 if (TREE_CODE (arg1) == REAL_CST)
3308 REAL_VALUE_TYPE c;
3309 HOST_WIDE_INT n;
3311 c = TREE_REAL_CST (arg1);
3312 n = real_to_integer (&c);
3314 if ((n & 1) == 0)
3316 REAL_VALUE_TYPE cint;
3317 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3318 if (real_identical (&c, &cint))
3319 return true;
3323 break;
3325 default:
3326 return false;
3331 return false;
3334 /* Given a pointer value OP0, return a simplified version of an
3335 indirection through OP0, or NULL_TREE if no simplification is
3336 possible. Note that the resulting type may be different from
3337 the type pointed to in the sense that it is still compatible
3338 from the langhooks point of view. */
3340 tree
3341 gimple_fold_indirect_ref (tree t)
3343 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
3344 tree sub = t;
3345 tree subtype;
3347 STRIP_NOPS (sub);
3348 subtype = TREE_TYPE (sub);
3349 if (!POINTER_TYPE_P (subtype))
3350 return NULL_TREE;
3352 if (TREE_CODE (sub) == ADDR_EXPR)
3354 tree op = TREE_OPERAND (sub, 0);
3355 tree optype = TREE_TYPE (op);
3356 /* *&p => p */
3357 if (useless_type_conversion_p (type, optype))
3358 return op;
3360 /* *(foo *)&fooarray => fooarray[0] */
3361 if (TREE_CODE (optype) == ARRAY_TYPE
3362 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
3363 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3365 tree type_domain = TYPE_DOMAIN (optype);
3366 tree min_val = size_zero_node;
3367 if (type_domain && TYPE_MIN_VALUE (type_domain))
3368 min_val = TYPE_MIN_VALUE (type_domain);
3369 if (TREE_CODE (min_val) == INTEGER_CST)
3370 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3372 /* *(foo *)&complexfoo => __real__ complexfoo */
3373 else if (TREE_CODE (optype) == COMPLEX_TYPE
3374 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3375 return fold_build1 (REALPART_EXPR, type, op);
3376 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3377 else if (TREE_CODE (optype) == VECTOR_TYPE
3378 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3380 tree part_width = TYPE_SIZE (type);
3381 tree index = bitsize_int (0);
3382 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
3386 /* *(p + CST) -> ... */
3387 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
3388 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
3390 tree addr = TREE_OPERAND (sub, 0);
3391 tree off = TREE_OPERAND (sub, 1);
3392 tree addrtype;
3394 STRIP_NOPS (addr);
3395 addrtype = TREE_TYPE (addr);
3397 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
3398 if (TREE_CODE (addr) == ADDR_EXPR
3399 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
3400 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
3401 && host_integerp (off, 1))
3403 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
3404 tree part_width = TYPE_SIZE (type);
3405 unsigned HOST_WIDE_INT part_widthi
3406 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
3407 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
3408 tree index = bitsize_int (indexi);
3409 if (offset / part_widthi
3410 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
3411 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
3412 part_width, index);
3415 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
3416 if (TREE_CODE (addr) == ADDR_EXPR
3417 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
3418 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
3420 tree size = TYPE_SIZE_UNIT (type);
3421 if (tree_int_cst_equal (size, off))
3422 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
3425 /* *(p + CST) -> MEM_REF <p, CST>. */
3426 if (TREE_CODE (addr) != ADDR_EXPR
3427 || DECL_P (TREE_OPERAND (addr, 0)))
3428 return fold_build2 (MEM_REF, type,
3429 addr,
3430 build_int_cst_wide (ptype,
3431 TREE_INT_CST_LOW (off),
3432 TREE_INT_CST_HIGH (off)));
3435 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3436 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3437 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
3438 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3440 tree type_domain;
3441 tree min_val = size_zero_node;
3442 tree osub = sub;
3443 sub = gimple_fold_indirect_ref (sub);
3444 if (! sub)
3445 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3446 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3447 if (type_domain && TYPE_MIN_VALUE (type_domain))
3448 min_val = TYPE_MIN_VALUE (type_domain);
3449 if (TREE_CODE (min_val) == INTEGER_CST)
3450 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3453 return NULL_TREE;