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