]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-ccp.c
ggcplug.c: Shuffle includes to include gcc-plugin.h earlier.
[thirdparty/gcc.git] / gcc / tree-ssa-ccp.c
1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
4 Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
11 later version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Conditional constant propagation (CCP) is based on the SSA
23 propagation engine (tree-ssa-propagate.c). Constant assignments of
24 the form VAR = CST are propagated from the assignments into uses of
25 VAR, which in turn may generate new constants. The simulation uses
26 a four level lattice to keep track of constant values associated
27 with SSA names. Given an SSA name V_i, it may take one of the
28 following values:
29
30 UNINITIALIZED -> the initial state of the value. This value
31 is replaced with a correct initial value
32 the first time the value is used, so the
33 rest of the pass does not need to care about
34 it. Using this value simplifies initialization
35 of the pass, and prevents us from needlessly
36 scanning statements that are never reached.
37
38 UNDEFINED -> V_i is a local variable whose definition
39 has not been processed yet. Therefore we
40 don't yet know if its value is a constant
41 or not.
42
43 CONSTANT -> V_i has been found to hold a constant
44 value C.
45
46 VARYING -> V_i cannot take a constant value, or if it
47 does, it is not possible to determine it
48 at compile time.
49
50 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
51
52 1- In ccp_visit_stmt, we are interested in assignments whose RHS
53 evaluates into a constant and conditional jumps whose predicate
54 evaluates into a boolean true or false. When an assignment of
55 the form V_i = CONST is found, V_i's lattice value is set to
56 CONSTANT and CONST is associated with it. This causes the
57 propagation engine to add all the SSA edges coming out the
58 assignment into the worklists, so that statements that use V_i
59 can be visited.
60
61 If the statement is a conditional with a constant predicate, we
62 mark the outgoing edges as executable or not executable
63 depending on the predicate's value. This is then used when
64 visiting PHI nodes to know when a PHI argument can be ignored.
65
66
67 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
68 same constant C, then the LHS of the PHI is set to C. This
69 evaluation is known as the "meet operation". Since one of the
70 goals of this evaluation is to optimistically return constant
71 values as often as possible, it uses two main short cuts:
72
73 - If an argument is flowing in through a non-executable edge, it
74 is ignored. This is useful in cases like this:
75
76 if (PRED)
77 a_9 = 3;
78 else
79 a_10 = 100;
80 a_11 = PHI (a_9, a_10)
81
82 If PRED is known to always evaluate to false, then we can
83 assume that a_11 will always take its value from a_10, meaning
84 that instead of consider it VARYING (a_9 and a_10 have
85 different values), we can consider it CONSTANT 100.
86
87 - If an argument has an UNDEFINED value, then it does not affect
88 the outcome of the meet operation. If a variable V_i has an
89 UNDEFINED value, it means that either its defining statement
90 hasn't been visited yet or V_i has no defining statement, in
91 which case the original symbol 'V' is being used
92 uninitialized. Since 'V' is a local variable, the compiler
93 may assume any initial value for it.
94
95
96 After propagation, every variable V_i that ends up with a lattice
97 value of CONSTANT will have the associated constant value in the
98 array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
99 final substitution and folding.
100
101 This algorithm uses wide-ints at the max precision of the target.
102 This means that, with one uninteresting exception, variables with
103 UNSIGNED types never go to VARYING because the bits above the
104 precision of the type of the variable are always zero. The
105 uninteresting case is a variable of UNSIGNED type that has the
106 maximum precision of the target. Such variables can go to VARYING,
107 but this causes no loss of infomation since these variables will
108 never be extended.
109
110 References:
111
112 Constant propagation with conditional branches,
113 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
114
115 Building an Optimizing Compiler,
116 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
117
118 Advanced Compiler Design and Implementation,
119 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
120
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "tm.h"
125 #include "tree.h"
126 #include "stor-layout.h"
127 #include "flags.h"
128 #include "tm_p.h"
129 #include "predict.h"
130 #include "vec.h"
131 #include "hashtab.h"
132 #include "hash-set.h"
133 #include "machmode.h"
134 #include "hard-reg-set.h"
135 #include "input.h"
136 #include "function.h"
137 #include "dominance.h"
138 #include "cfg.h"
139 #include "basic-block.h"
140 #include "gimple-pretty-print.h"
141 #include "hash-table.h"
142 #include "tree-ssa-alias.h"
143 #include "internal-fn.h"
144 #include "gimple-fold.h"
145 #include "tree-eh.h"
146 #include "gimple-expr.h"
147 #include "is-a.h"
148 #include "gimple.h"
149 #include "gimplify.h"
150 #include "gimple-iterator.h"
151 #include "gimple-ssa.h"
152 #include "tree-cfg.h"
153 #include "tree-phinodes.h"
154 #include "ssa-iterators.h"
155 #include "stringpool.h"
156 #include "tree-ssanames.h"
157 #include "tree-pass.h"
158 #include "tree-ssa-propagate.h"
159 #include "value-prof.h"
160 #include "langhooks.h"
161 #include "target.h"
162 #include "diagnostic-core.h"
163 #include "dbgcnt.h"
164 #include "params.h"
165 #include "wide-int-print.h"
166 #include "builtins.h"
167
168
169 /* Possible lattice values. */
170 typedef enum
171 {
172 UNINITIALIZED,
173 UNDEFINED,
174 CONSTANT,
175 VARYING
176 } ccp_lattice_t;
177
178 struct ccp_prop_value_t {
179 /* Lattice value. */
180 ccp_lattice_t lattice_val;
181
182 /* Propagated value. */
183 tree value;
184
185 /* Mask that applies to the propagated value during CCP. For X
186 with a CONSTANT lattice value X & ~mask == value & ~mask. The
187 zero bits in the mask cover constant values. The ones mean no
188 information. */
189 widest_int mask;
190 };
191
192 /* Array of propagated constant values. After propagation,
193 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
194 the constant is held in an SSA name representing a memory store
195 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
196 memory reference used to store (i.e., the LHS of the assignment
197 doing the store). */
198 static ccp_prop_value_t *const_val;
199 static unsigned n_const_val;
200
201 static void canonicalize_value (ccp_prop_value_t *);
202 static bool ccp_fold_stmt (gimple_stmt_iterator *);
203
204 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
205
206 static void
207 dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
208 {
209 switch (val.lattice_val)
210 {
211 case UNINITIALIZED:
212 fprintf (outf, "%sUNINITIALIZED", prefix);
213 break;
214 case UNDEFINED:
215 fprintf (outf, "%sUNDEFINED", prefix);
216 break;
217 case VARYING:
218 fprintf (outf, "%sVARYING", prefix);
219 break;
220 case CONSTANT:
221 if (TREE_CODE (val.value) != INTEGER_CST
222 || val.mask == 0)
223 {
224 fprintf (outf, "%sCONSTANT ", prefix);
225 print_generic_expr (outf, val.value, dump_flags);
226 }
227 else
228 {
229 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
230 val.mask);
231 fprintf (outf, "%sCONSTANT ", prefix);
232 print_hex (cval, outf);
233 fprintf (outf, " (");
234 print_hex (val.mask, outf);
235 fprintf (outf, ")");
236 }
237 break;
238 default:
239 gcc_unreachable ();
240 }
241 }
242
243
244 /* Print lattice value VAL to stderr. */
245
246 void debug_lattice_value (ccp_prop_value_t val);
247
248 DEBUG_FUNCTION void
249 debug_lattice_value (ccp_prop_value_t val)
250 {
251 dump_lattice_value (stderr, "", val);
252 fprintf (stderr, "\n");
253 }
254
255 /* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
256
257 static widest_int
258 extend_mask (const wide_int &nonzero_bits)
259 {
260 return (wi::mask <widest_int> (wi::get_precision (nonzero_bits), true)
261 | widest_int::from (nonzero_bits, UNSIGNED));
262 }
263
264 /* Compute a default value for variable VAR and store it in the
265 CONST_VAL array. The following rules are used to get default
266 values:
267
268 1- Global and static variables that are declared constant are
269 considered CONSTANT.
270
271 2- Any other value is considered UNDEFINED. This is useful when
272 considering PHI nodes. PHI arguments that are undefined do not
273 change the constant value of the PHI node, which allows for more
274 constants to be propagated.
275
276 3- Variables defined by statements other than assignments and PHI
277 nodes are considered VARYING.
278
279 4- Initial values of variables that are not GIMPLE registers are
280 considered VARYING. */
281
282 static ccp_prop_value_t
283 get_default_value (tree var)
284 {
285 ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
286 gimple stmt;
287
288 stmt = SSA_NAME_DEF_STMT (var);
289
290 if (gimple_nop_p (stmt))
291 {
292 /* Variables defined by an empty statement are those used
293 before being initialized. If VAR is a local variable, we
294 can assume initially that it is UNDEFINED, otherwise we must
295 consider it VARYING. */
296 if (!virtual_operand_p (var)
297 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
298 val.lattice_val = UNDEFINED;
299 else
300 {
301 val.lattice_val = VARYING;
302 val.mask = -1;
303 if (flag_tree_bit_ccp)
304 {
305 wide_int nonzero_bits = get_nonzero_bits (var);
306 if (nonzero_bits != -1)
307 {
308 val.lattice_val = CONSTANT;
309 val.value = build_zero_cst (TREE_TYPE (var));
310 val.mask = extend_mask (nonzero_bits);
311 }
312 }
313 }
314 }
315 else if (is_gimple_assign (stmt))
316 {
317 tree cst;
318 if (gimple_assign_single_p (stmt)
319 && DECL_P (gimple_assign_rhs1 (stmt))
320 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
321 {
322 val.lattice_val = CONSTANT;
323 val.value = cst;
324 }
325 else
326 {
327 /* Any other variable defined by an assignment is considered
328 UNDEFINED. */
329 val.lattice_val = UNDEFINED;
330 }
331 }
332 else if ((is_gimple_call (stmt)
333 && gimple_call_lhs (stmt) != NULL_TREE)
334 || gimple_code (stmt) == GIMPLE_PHI)
335 {
336 /* A variable defined by a call or a PHI node is considered
337 UNDEFINED. */
338 val.lattice_val = UNDEFINED;
339 }
340 else
341 {
342 /* Otherwise, VAR will never take on a constant value. */
343 val.lattice_val = VARYING;
344 val.mask = -1;
345 }
346
347 return val;
348 }
349
350
351 /* Get the constant value associated with variable VAR. */
352
353 static inline ccp_prop_value_t *
354 get_value (tree var)
355 {
356 ccp_prop_value_t *val;
357
358 if (const_val == NULL
359 || SSA_NAME_VERSION (var) >= n_const_val)
360 return NULL;
361
362 val = &const_val[SSA_NAME_VERSION (var)];
363 if (val->lattice_val == UNINITIALIZED)
364 *val = get_default_value (var);
365
366 canonicalize_value (val);
367
368 return val;
369 }
370
371 /* Return the constant tree value associated with VAR. */
372
373 static inline tree
374 get_constant_value (tree var)
375 {
376 ccp_prop_value_t *val;
377 if (TREE_CODE (var) != SSA_NAME)
378 {
379 if (is_gimple_min_invariant (var))
380 return var;
381 return NULL_TREE;
382 }
383 val = get_value (var);
384 if (val
385 && val->lattice_val == CONSTANT
386 && (TREE_CODE (val->value) != INTEGER_CST
387 || val->mask == 0))
388 return val->value;
389 return NULL_TREE;
390 }
391
392 /* Sets the value associated with VAR to VARYING. */
393
394 static inline void
395 set_value_varying (tree var)
396 {
397 ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
398
399 val->lattice_val = VARYING;
400 val->value = NULL_TREE;
401 val->mask = -1;
402 }
403
404 /* For float types, modify the value of VAL to make ccp work correctly
405 for non-standard values (-0, NaN):
406
407 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
408 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
409 This is to fix the following problem (see PR 29921): Suppose we have
410
411 x = 0.0 * y
412
413 and we set value of y to NaN. This causes value of x to be set to NaN.
414 When we later determine that y is in fact VARYING, fold uses the fact
415 that HONOR_NANS is false, and we try to change the value of x to 0,
416 causing an ICE. With HONOR_NANS being false, the real appearance of
417 NaN would cause undefined behavior, though, so claiming that y (and x)
418 are UNDEFINED initially is correct.
419
420 For other constants, make sure to drop TREE_OVERFLOW. */
421
422 static void
423 canonicalize_value (ccp_prop_value_t *val)
424 {
425 enum machine_mode mode;
426 tree type;
427 REAL_VALUE_TYPE d;
428
429 if (val->lattice_val != CONSTANT)
430 return;
431
432 if (TREE_OVERFLOW_P (val->value))
433 val->value = drop_tree_overflow (val->value);
434
435 if (TREE_CODE (val->value) != REAL_CST)
436 return;
437
438 d = TREE_REAL_CST (val->value);
439 type = TREE_TYPE (val->value);
440 mode = TYPE_MODE (type);
441
442 if (!HONOR_SIGNED_ZEROS (mode)
443 && REAL_VALUE_MINUS_ZERO (d))
444 {
445 val->value = build_real (type, dconst0);
446 return;
447 }
448
449 if (!HONOR_NANS (mode)
450 && REAL_VALUE_ISNAN (d))
451 {
452 val->lattice_val = UNDEFINED;
453 val->value = NULL;
454 return;
455 }
456 }
457
458 /* Return whether the lattice transition is valid. */
459
460 static bool
461 valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
462 {
463 /* Lattice transitions must always be monotonically increasing in
464 value. */
465 if (old_val.lattice_val < new_val.lattice_val)
466 return true;
467
468 if (old_val.lattice_val != new_val.lattice_val)
469 return false;
470
471 if (!old_val.value && !new_val.value)
472 return true;
473
474 /* Now both lattice values are CONSTANT. */
475
476 /* Allow transitioning from PHI <&x, not executable> == &x
477 to PHI <&x, &y> == common alignment. */
478 if (TREE_CODE (old_val.value) != INTEGER_CST
479 && TREE_CODE (new_val.value) == INTEGER_CST)
480 return true;
481
482 /* Bit-lattices have to agree in the still valid bits. */
483 if (TREE_CODE (old_val.value) == INTEGER_CST
484 && TREE_CODE (new_val.value) == INTEGER_CST)
485 return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
486 == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
487
488 /* Otherwise constant values have to agree. */
489 return operand_equal_p (old_val.value, new_val.value, 0);
490 }
491
492 /* Set the value for variable VAR to NEW_VAL. Return true if the new
493 value is different from VAR's previous value. */
494
495 static bool
496 set_lattice_value (tree var, ccp_prop_value_t new_val)
497 {
498 /* We can deal with old UNINITIALIZED values just fine here. */
499 ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
500
501 canonicalize_value (&new_val);
502
503 /* We have to be careful to not go up the bitwise lattice
504 represented by the mask.
505 ??? This doesn't seem to be the best place to enforce this. */
506 if (new_val.lattice_val == CONSTANT
507 && old_val->lattice_val == CONSTANT
508 && TREE_CODE (new_val.value) == INTEGER_CST
509 && TREE_CODE (old_val->value) == INTEGER_CST)
510 {
511 widest_int diff = (wi::to_widest (new_val.value)
512 ^ wi::to_widest (old_val->value));
513 new_val.mask = new_val.mask | old_val->mask | diff;
514 }
515
516 gcc_assert (valid_lattice_transition (*old_val, new_val));
517
518 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
519 caller that this was a non-transition. */
520 if (old_val->lattice_val != new_val.lattice_val
521 || (new_val.lattice_val == CONSTANT
522 && TREE_CODE (new_val.value) == INTEGER_CST
523 && (TREE_CODE (old_val->value) != INTEGER_CST
524 || new_val.mask != old_val->mask)))
525 {
526 /* ??? We would like to delay creation of INTEGER_CSTs from
527 partially constants here. */
528
529 if (dump_file && (dump_flags & TDF_DETAILS))
530 {
531 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
532 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
533 }
534
535 *old_val = new_val;
536
537 gcc_assert (new_val.lattice_val != UNINITIALIZED);
538 return true;
539 }
540
541 return false;
542 }
543
544 static ccp_prop_value_t get_value_for_expr (tree, bool);
545 static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
546 static void bit_value_binop_1 (enum tree_code, tree, widest_int *, widest_int *,
547 tree, const widest_int &, const widest_int &,
548 tree, const widest_int &, const widest_int &);
549
550 /* Return a widest_int that can be used for bitwise simplifications
551 from VAL. */
552
553 static widest_int
554 value_to_wide_int (ccp_prop_value_t val)
555 {
556 if (val.value
557 && TREE_CODE (val.value) == INTEGER_CST)
558 return wi::to_widest (val.value);
559
560 return 0;
561 }
562
563 /* Return the value for the address expression EXPR based on alignment
564 information. */
565
566 static ccp_prop_value_t
567 get_value_from_alignment (tree expr)
568 {
569 tree type = TREE_TYPE (expr);
570 ccp_prop_value_t val;
571 unsigned HOST_WIDE_INT bitpos;
572 unsigned int align;
573
574 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
575
576 get_pointer_alignment_1 (expr, &align, &bitpos);
577 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
578 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
579 : -1).and_not (align / BITS_PER_UNIT - 1);
580 val.lattice_val = val.mask == -1 ? VARYING : CONSTANT;
581 if (val.lattice_val == CONSTANT)
582 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
583 else
584 val.value = NULL_TREE;
585
586 return val;
587 }
588
589 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
590 return constant bits extracted from alignment information for
591 invariant addresses. */
592
593 static ccp_prop_value_t
594 get_value_for_expr (tree expr, bool for_bits_p)
595 {
596 ccp_prop_value_t val;
597
598 if (TREE_CODE (expr) == SSA_NAME)
599 {
600 val = *get_value (expr);
601 if (for_bits_p
602 && val.lattice_val == CONSTANT
603 && TREE_CODE (val.value) == ADDR_EXPR)
604 val = get_value_from_alignment (val.value);
605 }
606 else if (is_gimple_min_invariant (expr)
607 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
608 {
609 val.lattice_val = CONSTANT;
610 val.value = expr;
611 val.mask = 0;
612 canonicalize_value (&val);
613 }
614 else if (TREE_CODE (expr) == ADDR_EXPR)
615 val = get_value_from_alignment (expr);
616 else
617 {
618 val.lattice_val = VARYING;
619 val.mask = -1;
620 val.value = NULL_TREE;
621 }
622 return val;
623 }
624
625 /* Return the likely CCP lattice value for STMT.
626
627 If STMT has no operands, then return CONSTANT.
628
629 Else if undefinedness of operands of STMT cause its value to be
630 undefined, then return UNDEFINED.
631
632 Else if any operands of STMT are constants, then return CONSTANT.
633
634 Else return VARYING. */
635
636 static ccp_lattice_t
637 likely_value (gimple stmt)
638 {
639 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
640 tree use;
641 ssa_op_iter iter;
642 unsigned i;
643
644 enum gimple_code code = gimple_code (stmt);
645
646 /* This function appears to be called only for assignments, calls,
647 conditionals, and switches, due to the logic in visit_stmt. */
648 gcc_assert (code == GIMPLE_ASSIGN
649 || code == GIMPLE_CALL
650 || code == GIMPLE_COND
651 || code == GIMPLE_SWITCH);
652
653 /* If the statement has volatile operands, it won't fold to a
654 constant value. */
655 if (gimple_has_volatile_ops (stmt))
656 return VARYING;
657
658 /* Arrive here for more complex cases. */
659 has_constant_operand = false;
660 has_undefined_operand = false;
661 all_undefined_operands = true;
662 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
663 {
664 ccp_prop_value_t *val = get_value (use);
665
666 if (val->lattice_val == UNDEFINED)
667 has_undefined_operand = true;
668 else
669 all_undefined_operands = false;
670
671 if (val->lattice_val == CONSTANT)
672 has_constant_operand = true;
673 }
674
675 /* There may be constants in regular rhs operands. For calls we
676 have to ignore lhs, fndecl and static chain, otherwise only
677 the lhs. */
678 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
679 i < gimple_num_ops (stmt); ++i)
680 {
681 tree op = gimple_op (stmt, i);
682 if (!op || TREE_CODE (op) == SSA_NAME)
683 continue;
684 if (is_gimple_min_invariant (op))
685 has_constant_operand = true;
686 }
687
688 if (has_constant_operand)
689 all_undefined_operands = false;
690
691 if (has_undefined_operand
692 && code == GIMPLE_CALL
693 && gimple_call_internal_p (stmt))
694 switch (gimple_call_internal_fn (stmt))
695 {
696 /* These 3 builtins use the first argument just as a magic
697 way how to find out a decl uid. */
698 case IFN_GOMP_SIMD_LANE:
699 case IFN_GOMP_SIMD_VF:
700 case IFN_GOMP_SIMD_LAST_LANE:
701 has_undefined_operand = false;
702 break;
703 default:
704 break;
705 }
706
707 /* If the operation combines operands like COMPLEX_EXPR make sure to
708 not mark the result UNDEFINED if only one part of the result is
709 undefined. */
710 if (has_undefined_operand && all_undefined_operands)
711 return UNDEFINED;
712 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
713 {
714 switch (gimple_assign_rhs_code (stmt))
715 {
716 /* Unary operators are handled with all_undefined_operands. */
717 case PLUS_EXPR:
718 case MINUS_EXPR:
719 case POINTER_PLUS_EXPR:
720 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
721 Not bitwise operators, one VARYING operand may specify the
722 result completely. Not logical operators for the same reason.
723 Not COMPLEX_EXPR as one VARYING operand makes the result partly
724 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
725 the undefined operand may be promoted. */
726 return UNDEFINED;
727
728 case ADDR_EXPR:
729 /* If any part of an address is UNDEFINED, like the index
730 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
731 return UNDEFINED;
732
733 default:
734 ;
735 }
736 }
737 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
738 fall back to CONSTANT. During iteration UNDEFINED may still drop
739 to CONSTANT. */
740 if (has_undefined_operand)
741 return CONSTANT;
742
743 /* We do not consider virtual operands here -- load from read-only
744 memory may have only VARYING virtual operands, but still be
745 constant. */
746 if (has_constant_operand
747 || gimple_references_memory_p (stmt))
748 return CONSTANT;
749
750 return VARYING;
751 }
752
753 /* Returns true if STMT cannot be constant. */
754
755 static bool
756 surely_varying_stmt_p (gimple stmt)
757 {
758 /* If the statement has operands that we cannot handle, it cannot be
759 constant. */
760 if (gimple_has_volatile_ops (stmt))
761 return true;
762
763 /* If it is a call and does not return a value or is not a
764 builtin and not an indirect call or a call to function with
765 assume_aligned/alloc_align attribute, it is varying. */
766 if (is_gimple_call (stmt))
767 {
768 tree fndecl, fntype = gimple_call_fntype (stmt);
769 if (!gimple_call_lhs (stmt)
770 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
771 && !DECL_BUILT_IN (fndecl)
772 && !lookup_attribute ("assume_aligned",
773 TYPE_ATTRIBUTES (fntype))
774 && !lookup_attribute ("alloc_align",
775 TYPE_ATTRIBUTES (fntype))))
776 return true;
777 }
778
779 /* Any other store operation is not interesting. */
780 else if (gimple_vdef (stmt))
781 return true;
782
783 /* Anything other than assignments and conditional jumps are not
784 interesting for CCP. */
785 if (gimple_code (stmt) != GIMPLE_ASSIGN
786 && gimple_code (stmt) != GIMPLE_COND
787 && gimple_code (stmt) != GIMPLE_SWITCH
788 && gimple_code (stmt) != GIMPLE_CALL)
789 return true;
790
791 return false;
792 }
793
794 /* Initialize local data structures for CCP. */
795
796 static void
797 ccp_initialize (void)
798 {
799 basic_block bb;
800
801 n_const_val = num_ssa_names;
802 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
803
804 /* Initialize simulation flags for PHI nodes and statements. */
805 FOR_EACH_BB_FN (bb, cfun)
806 {
807 gimple_stmt_iterator i;
808
809 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
810 {
811 gimple stmt = gsi_stmt (i);
812 bool is_varying;
813
814 /* If the statement is a control insn, then we do not
815 want to avoid simulating the statement once. Failure
816 to do so means that those edges will never get added. */
817 if (stmt_ends_bb_p (stmt))
818 is_varying = false;
819 else
820 is_varying = surely_varying_stmt_p (stmt);
821
822 if (is_varying)
823 {
824 tree def;
825 ssa_op_iter iter;
826
827 /* If the statement will not produce a constant, mark
828 all its outputs VARYING. */
829 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
830 set_value_varying (def);
831 }
832 prop_set_simulate_again (stmt, !is_varying);
833 }
834 }
835
836 /* Now process PHI nodes. We never clear the simulate_again flag on
837 phi nodes, since we do not know which edges are executable yet,
838 except for phi nodes for virtual operands when we do not do store ccp. */
839 FOR_EACH_BB_FN (bb, cfun)
840 {
841 gimple_stmt_iterator i;
842
843 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
844 {
845 gimple phi = gsi_stmt (i);
846
847 if (virtual_operand_p (gimple_phi_result (phi)))
848 prop_set_simulate_again (phi, false);
849 else
850 prop_set_simulate_again (phi, true);
851 }
852 }
853 }
854
855 /* Debug count support. Reset the values of ssa names
856 VARYING when the total number ssa names analyzed is
857 beyond the debug count specified. */
858
859 static void
860 do_dbg_cnt (void)
861 {
862 unsigned i;
863 for (i = 0; i < num_ssa_names; i++)
864 {
865 if (!dbg_cnt (ccp))
866 {
867 const_val[i].lattice_val = VARYING;
868 const_val[i].mask = -1;
869 const_val[i].value = NULL_TREE;
870 }
871 }
872 }
873
874
875 /* Do final substitution of propagated values, cleanup the flowgraph and
876 free allocated storage.
877
878 Return TRUE when something was optimized. */
879
880 static bool
881 ccp_finalize (void)
882 {
883 bool something_changed;
884 unsigned i;
885
886 do_dbg_cnt ();
887
888 /* Derive alignment and misalignment information from partially
889 constant pointers in the lattice or nonzero bits from partially
890 constant integers. */
891 for (i = 1; i < num_ssa_names; ++i)
892 {
893 tree name = ssa_name (i);
894 ccp_prop_value_t *val;
895 unsigned int tem, align;
896
897 if (!name
898 || (!POINTER_TYPE_P (TREE_TYPE (name))
899 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
900 /* Don't record nonzero bits before IPA to avoid
901 using too much memory. */
902 || first_pass_instance)))
903 continue;
904
905 val = get_value (name);
906 if (val->lattice_val != CONSTANT
907 || TREE_CODE (val->value) != INTEGER_CST)
908 continue;
909
910 if (POINTER_TYPE_P (TREE_TYPE (name)))
911 {
912 /* Trailing mask bits specify the alignment, trailing value
913 bits the misalignment. */
914 tem = val->mask.to_uhwi ();
915 align = (tem & -tem);
916 if (align > 1)
917 set_ptr_info_alignment (get_ptr_info (name), align,
918 (TREE_INT_CST_LOW (val->value)
919 & (align - 1)));
920 }
921 else
922 {
923 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
924 wide_int nonzero_bits = wide_int::from (val->mask, precision,
925 UNSIGNED) | val->value;
926 nonzero_bits &= get_nonzero_bits (name);
927 set_nonzero_bits (name, nonzero_bits);
928 }
929 }
930
931 /* Perform substitutions based on the known constant values. */
932 something_changed = substitute_and_fold (get_constant_value,
933 ccp_fold_stmt, true);
934
935 free (const_val);
936 const_val = NULL;
937 return something_changed;;
938 }
939
940
941 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
942 in VAL1.
943
944 any M UNDEFINED = any
945 any M VARYING = VARYING
946 Ci M Cj = Ci if (i == j)
947 Ci M Cj = VARYING if (i != j)
948 */
949
950 static void
951 ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
952 {
953 if (val1->lattice_val == UNDEFINED)
954 {
955 /* UNDEFINED M any = any */
956 *val1 = *val2;
957 }
958 else if (val2->lattice_val == UNDEFINED)
959 {
960 /* any M UNDEFINED = any
961 Nothing to do. VAL1 already contains the value we want. */
962 ;
963 }
964 else if (val1->lattice_val == VARYING
965 || val2->lattice_val == VARYING)
966 {
967 /* any M VARYING = VARYING. */
968 val1->lattice_val = VARYING;
969 val1->mask = -1;
970 val1->value = NULL_TREE;
971 }
972 else if (val1->lattice_val == CONSTANT
973 && val2->lattice_val == CONSTANT
974 && TREE_CODE (val1->value) == INTEGER_CST
975 && TREE_CODE (val2->value) == INTEGER_CST)
976 {
977 /* Ci M Cj = Ci if (i == j)
978 Ci M Cj = VARYING if (i != j)
979
980 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
981 drop to varying. */
982 val1->mask = (val1->mask | val2->mask
983 | (wi::to_widest (val1->value)
984 ^ wi::to_widest (val2->value)));
985 if (val1->mask == -1)
986 {
987 val1->lattice_val = VARYING;
988 val1->value = NULL_TREE;
989 }
990 }
991 else if (val1->lattice_val == CONSTANT
992 && val2->lattice_val == CONSTANT
993 && simple_cst_equal (val1->value, val2->value) == 1)
994 {
995 /* Ci M Cj = Ci if (i == j)
996 Ci M Cj = VARYING if (i != j)
997
998 VAL1 already contains the value we want for equivalent values. */
999 }
1000 else if (val1->lattice_val == CONSTANT
1001 && val2->lattice_val == CONSTANT
1002 && (TREE_CODE (val1->value) == ADDR_EXPR
1003 || TREE_CODE (val2->value) == ADDR_EXPR))
1004 {
1005 /* When not equal addresses are involved try meeting for
1006 alignment. */
1007 ccp_prop_value_t tem = *val2;
1008 if (TREE_CODE (val1->value) == ADDR_EXPR)
1009 *val1 = get_value_for_expr (val1->value, true);
1010 if (TREE_CODE (val2->value) == ADDR_EXPR)
1011 tem = get_value_for_expr (val2->value, true);
1012 ccp_lattice_meet (val1, &tem);
1013 }
1014 else
1015 {
1016 /* Any other combination is VARYING. */
1017 val1->lattice_val = VARYING;
1018 val1->mask = -1;
1019 val1->value = NULL_TREE;
1020 }
1021 }
1022
1023
1024 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1025 lattice values to determine PHI_NODE's lattice value. The value of a
1026 PHI node is determined calling ccp_lattice_meet with all the arguments
1027 of the PHI node that are incoming via executable edges. */
1028
1029 static enum ssa_prop_result
1030 ccp_visit_phi_node (gimple phi)
1031 {
1032 unsigned i;
1033 ccp_prop_value_t *old_val, new_val;
1034
1035 if (dump_file && (dump_flags & TDF_DETAILS))
1036 {
1037 fprintf (dump_file, "\nVisiting PHI node: ");
1038 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1039 }
1040
1041 old_val = get_value (gimple_phi_result (phi));
1042 switch (old_val->lattice_val)
1043 {
1044 case VARYING:
1045 return SSA_PROP_VARYING;
1046
1047 case CONSTANT:
1048 new_val = *old_val;
1049 break;
1050
1051 case UNDEFINED:
1052 new_val.lattice_val = UNDEFINED;
1053 new_val.value = NULL_TREE;
1054 break;
1055
1056 default:
1057 gcc_unreachable ();
1058 }
1059
1060 for (i = 0; i < gimple_phi_num_args (phi); i++)
1061 {
1062 /* Compute the meet operator over all the PHI arguments flowing
1063 through executable edges. */
1064 edge e = gimple_phi_arg_edge (phi, i);
1065
1066 if (dump_file && (dump_flags & TDF_DETAILS))
1067 {
1068 fprintf (dump_file,
1069 "\n Argument #%d (%d -> %d %sexecutable)\n",
1070 i, e->src->index, e->dest->index,
1071 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1072 }
1073
1074 /* If the incoming edge is executable, Compute the meet operator for
1075 the existing value of the PHI node and the current PHI argument. */
1076 if (e->flags & EDGE_EXECUTABLE)
1077 {
1078 tree arg = gimple_phi_arg (phi, i)->def;
1079 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1080
1081 ccp_lattice_meet (&new_val, &arg_val);
1082
1083 if (dump_file && (dump_flags & TDF_DETAILS))
1084 {
1085 fprintf (dump_file, "\t");
1086 print_generic_expr (dump_file, arg, dump_flags);
1087 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1088 fprintf (dump_file, "\n");
1089 }
1090
1091 if (new_val.lattice_val == VARYING)
1092 break;
1093 }
1094 }
1095
1096 if (dump_file && (dump_flags & TDF_DETAILS))
1097 {
1098 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1099 fprintf (dump_file, "\n\n");
1100 }
1101
1102 /* Make the transition to the new value. */
1103 if (set_lattice_value (gimple_phi_result (phi), new_val))
1104 {
1105 if (new_val.lattice_val == VARYING)
1106 return SSA_PROP_VARYING;
1107 else
1108 return SSA_PROP_INTERESTING;
1109 }
1110 else
1111 return SSA_PROP_NOT_INTERESTING;
1112 }
1113
1114 /* Return the constant value for OP or OP otherwise. */
1115
1116 static tree
1117 valueize_op (tree op)
1118 {
1119 if (TREE_CODE (op) == SSA_NAME)
1120 {
1121 tree tem = get_constant_value (op);
1122 if (tem)
1123 return tem;
1124 }
1125 return op;
1126 }
1127
1128 /* CCP specific front-end to the non-destructive constant folding
1129 routines.
1130
1131 Attempt to simplify the RHS of STMT knowing that one or more
1132 operands are constants.
1133
1134 If simplification is possible, return the simplified RHS,
1135 otherwise return the original RHS or NULL_TREE. */
1136
1137 static tree
1138 ccp_fold (gimple stmt)
1139 {
1140 location_t loc = gimple_location (stmt);
1141 switch (gimple_code (stmt))
1142 {
1143 case GIMPLE_COND:
1144 {
1145 /* Handle comparison operators that can appear in GIMPLE form. */
1146 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1147 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1148 enum tree_code code = gimple_cond_code (stmt);
1149 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1150 }
1151
1152 case GIMPLE_SWITCH:
1153 {
1154 /* Return the constant switch index. */
1155 return valueize_op (gimple_switch_index (stmt));
1156 }
1157
1158 case GIMPLE_ASSIGN:
1159 case GIMPLE_CALL:
1160 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
1161
1162 default:
1163 gcc_unreachable ();
1164 }
1165 }
1166
1167 /* Apply the operation CODE in type TYPE to the value, mask pair
1168 RVAL and RMASK representing a value of type RTYPE and set
1169 the value, mask pair *VAL and *MASK to the result. */
1170
1171 static void
1172 bit_value_unop_1 (enum tree_code code, tree type,
1173 widest_int *val, widest_int *mask,
1174 tree rtype, const widest_int &rval, const widest_int &rmask)
1175 {
1176 switch (code)
1177 {
1178 case BIT_NOT_EXPR:
1179 *mask = rmask;
1180 *val = ~rval;
1181 break;
1182
1183 case NEGATE_EXPR:
1184 {
1185 widest_int temv, temm;
1186 /* Return ~rval + 1. */
1187 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1188 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1189 type, temv, temm, type, 1, 0);
1190 break;
1191 }
1192
1193 CASE_CONVERT:
1194 {
1195 signop sgn;
1196
1197 /* First extend mask and value according to the original type. */
1198 sgn = TYPE_SIGN (rtype);
1199 *mask = wi::ext (rmask, TYPE_PRECISION (rtype), sgn);
1200 *val = wi::ext (rval, TYPE_PRECISION (rtype), sgn);
1201
1202 /* Then extend mask and value according to the target type. */
1203 sgn = TYPE_SIGN (type);
1204 *mask = wi::ext (*mask, TYPE_PRECISION (type), sgn);
1205 *val = wi::ext (*val, TYPE_PRECISION (type), sgn);
1206 break;
1207 }
1208
1209 default:
1210 *mask = -1;
1211 break;
1212 }
1213 }
1214
1215 /* Apply the operation CODE in type TYPE to the value, mask pairs
1216 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1217 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1218
1219 static void
1220 bit_value_binop_1 (enum tree_code code, tree type,
1221 widest_int *val, widest_int *mask,
1222 tree r1type, const widest_int &r1val,
1223 const widest_int &r1mask, tree r2type,
1224 const widest_int &r2val, const widest_int &r2mask)
1225 {
1226 signop sgn = TYPE_SIGN (type);
1227 int width = TYPE_PRECISION (type);
1228 bool swap_p = false;
1229
1230 /* Assume we'll get a constant result. Use an initial non varying
1231 value, we fall back to varying in the end if necessary. */
1232 *mask = -1;
1233
1234 switch (code)
1235 {
1236 case BIT_AND_EXPR:
1237 /* The mask is constant where there is a known not
1238 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1239 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1240 *val = r1val & r2val;
1241 break;
1242
1243 case BIT_IOR_EXPR:
1244 /* The mask is constant where there is a known
1245 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1246 *mask = (r1mask | r2mask)
1247 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1248 *val = r1val | r2val;
1249 break;
1250
1251 case BIT_XOR_EXPR:
1252 /* m1 | m2 */
1253 *mask = r1mask | r2mask;
1254 *val = r1val ^ r2val;
1255 break;
1256
1257 case LROTATE_EXPR:
1258 case RROTATE_EXPR:
1259 if (r2mask == 0)
1260 {
1261 widest_int shift = r2val;
1262 if (shift == 0)
1263 {
1264 *mask = r1mask;
1265 *val = r1val;
1266 }
1267 else
1268 {
1269 if (wi::neg_p (shift))
1270 {
1271 shift = -shift;
1272 if (code == RROTATE_EXPR)
1273 code = LROTATE_EXPR;
1274 else
1275 code = RROTATE_EXPR;
1276 }
1277 if (code == RROTATE_EXPR)
1278 {
1279 *mask = wi::rrotate (r1mask, shift, width);
1280 *val = wi::rrotate (r1val, shift, width);
1281 }
1282 else
1283 {
1284 *mask = wi::lrotate (r1mask, shift, width);
1285 *val = wi::lrotate (r1val, shift, width);
1286 }
1287 }
1288 }
1289 break;
1290
1291 case LSHIFT_EXPR:
1292 case RSHIFT_EXPR:
1293 /* ??? We can handle partially known shift counts if we know
1294 its sign. That way we can tell that (x << (y | 8)) & 255
1295 is zero. */
1296 if (r2mask == 0)
1297 {
1298 widest_int shift = r2val;
1299 if (shift == 0)
1300 {
1301 *mask = r1mask;
1302 *val = r1val;
1303 }
1304 else
1305 {
1306 if (wi::neg_p (shift))
1307 {
1308 shift = -shift;
1309 if (code == RSHIFT_EXPR)
1310 code = LSHIFT_EXPR;
1311 else
1312 code = RSHIFT_EXPR;
1313 }
1314 if (code == RSHIFT_EXPR)
1315 {
1316 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1317 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1318 }
1319 else
1320 {
1321 *mask = wi::ext (wi::lshift (r1mask, shift), width, sgn);
1322 *val = wi::ext (wi::lshift (r1val, shift), width, sgn);
1323 }
1324 }
1325 }
1326 break;
1327
1328 case PLUS_EXPR:
1329 case POINTER_PLUS_EXPR:
1330 {
1331 /* Do the addition with unknown bits set to zero, to give carry-ins of
1332 zero wherever possible. */
1333 widest_int lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1334 lo = wi::ext (lo, width, sgn);
1335 /* Do the addition with unknown bits set to one, to give carry-ins of
1336 one wherever possible. */
1337 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1338 hi = wi::ext (hi, width, sgn);
1339 /* Each bit in the result is known if (a) the corresponding bits in
1340 both inputs are known, and (b) the carry-in to that bit position
1341 is known. We can check condition (b) by seeing if we got the same
1342 result with minimised carries as with maximised carries. */
1343 *mask = r1mask | r2mask | (lo ^ hi);
1344 *mask = wi::ext (*mask, width, sgn);
1345 /* It shouldn't matter whether we choose lo or hi here. */
1346 *val = lo;
1347 break;
1348 }
1349
1350 case MINUS_EXPR:
1351 {
1352 widest_int temv, temm;
1353 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1354 r2type, r2val, r2mask);
1355 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1356 r1type, r1val, r1mask,
1357 r2type, temv, temm);
1358 break;
1359 }
1360
1361 case MULT_EXPR:
1362 {
1363 /* Just track trailing zeros in both operands and transfer
1364 them to the other. */
1365 int r1tz = wi::ctz (r1val | r1mask);
1366 int r2tz = wi::ctz (r2val | r2mask);
1367 if (r1tz + r2tz >= width)
1368 {
1369 *mask = 0;
1370 *val = 0;
1371 }
1372 else if (r1tz + r2tz > 0)
1373 {
1374 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1375 width, sgn);
1376 *val = 0;
1377 }
1378 break;
1379 }
1380
1381 case EQ_EXPR:
1382 case NE_EXPR:
1383 {
1384 widest_int m = r1mask | r2mask;
1385 if (r1val.and_not (m) != r2val.and_not (m))
1386 {
1387 *mask = 0;
1388 *val = ((code == EQ_EXPR) ? 0 : 1);
1389 }
1390 else
1391 {
1392 /* We know the result of a comparison is always one or zero. */
1393 *mask = 1;
1394 *val = 0;
1395 }
1396 break;
1397 }
1398
1399 case GE_EXPR:
1400 case GT_EXPR:
1401 swap_p = true;
1402 code = swap_tree_comparison (code);
1403 /* Fall through. */
1404 case LT_EXPR:
1405 case LE_EXPR:
1406 {
1407 int minmax, maxmin;
1408
1409 const widest_int &o1val = swap_p ? r2val : r1val;
1410 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1411 const widest_int &o2val = swap_p ? r1val : r2val;
1412 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1413
1414 /* If the most significant bits are not known we know nothing. */
1415 if (wi::neg_p (o1mask) || wi::neg_p (o2mask))
1416 break;
1417
1418 /* For comparisons the signedness is in the comparison operands. */
1419 sgn = TYPE_SIGN (r1type);
1420
1421 /* If we know the most significant bits we know the values
1422 value ranges by means of treating varying bits as zero
1423 or one. Do a cross comparison of the max/min pairs. */
1424 maxmin = wi::cmp (o1val | o1mask, o2val.and_not (o2mask), sgn);
1425 minmax = wi::cmp (o1val.and_not (o1mask), o2val | o2mask, sgn);
1426 if (maxmin < 0) /* o1 is less than o2. */
1427 {
1428 *mask = 0;
1429 *val = 1;
1430 }
1431 else if (minmax > 0) /* o1 is not less or equal to o2. */
1432 {
1433 *mask = 0;
1434 *val = 0;
1435 }
1436 else if (maxmin == minmax) /* o1 and o2 are equal. */
1437 {
1438 /* This probably should never happen as we'd have
1439 folded the thing during fully constant value folding. */
1440 *mask = 0;
1441 *val = (code == LE_EXPR ? 1 : 0);
1442 }
1443 else
1444 {
1445 /* We know the result of a comparison is always one or zero. */
1446 *mask = 1;
1447 *val = 0;
1448 }
1449 break;
1450 }
1451
1452 default:;
1453 }
1454 }
1455
1456 /* Return the propagation value when applying the operation CODE to
1457 the value RHS yielding type TYPE. */
1458
1459 static ccp_prop_value_t
1460 bit_value_unop (enum tree_code code, tree type, tree rhs)
1461 {
1462 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
1463 widest_int value, mask;
1464 ccp_prop_value_t val;
1465
1466 if (rval.lattice_val == UNDEFINED)
1467 return rval;
1468
1469 gcc_assert ((rval.lattice_val == CONSTANT
1470 && TREE_CODE (rval.value) == INTEGER_CST)
1471 || rval.mask == -1);
1472 bit_value_unop_1 (code, type, &value, &mask,
1473 TREE_TYPE (rhs), value_to_wide_int (rval), rval.mask);
1474 if (mask != -1)
1475 {
1476 val.lattice_val = CONSTANT;
1477 val.mask = mask;
1478 /* ??? Delay building trees here. */
1479 val.value = wide_int_to_tree (type, value);
1480 }
1481 else
1482 {
1483 val.lattice_val = VARYING;
1484 val.value = NULL_TREE;
1485 val.mask = -1;
1486 }
1487 return val;
1488 }
1489
1490 /* Return the propagation value when applying the operation CODE to
1491 the values RHS1 and RHS2 yielding type TYPE. */
1492
1493 static ccp_prop_value_t
1494 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1495 {
1496 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
1497 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
1498 widest_int value, mask;
1499 ccp_prop_value_t val;
1500
1501 if (r1val.lattice_val == UNDEFINED
1502 || r2val.lattice_val == UNDEFINED)
1503 {
1504 val.lattice_val = VARYING;
1505 val.value = NULL_TREE;
1506 val.mask = -1;
1507 return val;
1508 }
1509
1510 gcc_assert ((r1val.lattice_val == CONSTANT
1511 && TREE_CODE (r1val.value) == INTEGER_CST)
1512 || r1val.mask == -1);
1513 gcc_assert ((r2val.lattice_val == CONSTANT
1514 && TREE_CODE (r2val.value) == INTEGER_CST)
1515 || r2val.mask == -1);
1516 bit_value_binop_1 (code, type, &value, &mask,
1517 TREE_TYPE (rhs1), value_to_wide_int (r1val), r1val.mask,
1518 TREE_TYPE (rhs2), value_to_wide_int (r2val), r2val.mask);
1519 if (mask != -1)
1520 {
1521 val.lattice_val = CONSTANT;
1522 val.mask = mask;
1523 /* ??? Delay building trees here. */
1524 val.value = wide_int_to_tree (type, value);
1525 }
1526 else
1527 {
1528 val.lattice_val = VARYING;
1529 val.value = NULL_TREE;
1530 val.mask = -1;
1531 }
1532 return val;
1533 }
1534
1535 /* Return the propagation value for __builtin_assume_aligned
1536 and functions with assume_aligned or alloc_aligned attribute.
1537 For __builtin_assume_aligned, ATTR is NULL_TREE,
1538 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
1539 is false, for alloc_aligned attribute ATTR is non-NULL and
1540 ALLOC_ALIGNED is true. */
1541
1542 static ccp_prop_value_t
1543 bit_value_assume_aligned (gimple stmt, tree attr, ccp_prop_value_t ptrval,
1544 bool alloc_aligned)
1545 {
1546 tree align, misalign = NULL_TREE, type;
1547 unsigned HOST_WIDE_INT aligni, misaligni = 0;
1548 ccp_prop_value_t alignval;
1549 widest_int value, mask;
1550 ccp_prop_value_t val;
1551
1552 if (attr == NULL_TREE)
1553 {
1554 tree ptr = gimple_call_arg (stmt, 0);
1555 type = TREE_TYPE (ptr);
1556 ptrval = get_value_for_expr (ptr, true);
1557 }
1558 else
1559 {
1560 tree lhs = gimple_call_lhs (stmt);
1561 type = TREE_TYPE (lhs);
1562 }
1563
1564 if (ptrval.lattice_val == UNDEFINED)
1565 return ptrval;
1566 gcc_assert ((ptrval.lattice_val == CONSTANT
1567 && TREE_CODE (ptrval.value) == INTEGER_CST)
1568 || ptrval.mask == -1);
1569 if (attr == NULL_TREE)
1570 {
1571 /* Get aligni and misaligni from __builtin_assume_aligned. */
1572 align = gimple_call_arg (stmt, 1);
1573 if (!tree_fits_uhwi_p (align))
1574 return ptrval;
1575 aligni = tree_to_uhwi (align);
1576 if (gimple_call_num_args (stmt) > 2)
1577 {
1578 misalign = gimple_call_arg (stmt, 2);
1579 if (!tree_fits_uhwi_p (misalign))
1580 return ptrval;
1581 misaligni = tree_to_uhwi (misalign);
1582 }
1583 }
1584 else
1585 {
1586 /* Get aligni and misaligni from assume_aligned or
1587 alloc_align attributes. */
1588 if (TREE_VALUE (attr) == NULL_TREE)
1589 return ptrval;
1590 attr = TREE_VALUE (attr);
1591 align = TREE_VALUE (attr);
1592 if (!tree_fits_uhwi_p (align))
1593 return ptrval;
1594 aligni = tree_to_uhwi (align);
1595 if (alloc_aligned)
1596 {
1597 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
1598 return ptrval;
1599 align = gimple_call_arg (stmt, aligni - 1);
1600 if (!tree_fits_uhwi_p (align))
1601 return ptrval;
1602 aligni = tree_to_uhwi (align);
1603 }
1604 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
1605 {
1606 misalign = TREE_VALUE (TREE_CHAIN (attr));
1607 if (!tree_fits_uhwi_p (misalign))
1608 return ptrval;
1609 misaligni = tree_to_uhwi (misalign);
1610 }
1611 }
1612 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
1613 return ptrval;
1614
1615 align = build_int_cst_type (type, -aligni);
1616 alignval = get_value_for_expr (align, true);
1617 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
1618 type, value_to_wide_int (ptrval), ptrval.mask,
1619 type, value_to_wide_int (alignval), alignval.mask);
1620 if (mask != -1)
1621 {
1622 val.lattice_val = CONSTANT;
1623 val.mask = mask;
1624 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
1625 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
1626 value |= misaligni;
1627 /* ??? Delay building trees here. */
1628 val.value = wide_int_to_tree (type, value);
1629 }
1630 else
1631 {
1632 val.lattice_val = VARYING;
1633 val.value = NULL_TREE;
1634 val.mask = -1;
1635 }
1636 return val;
1637 }
1638
1639 /* Evaluate statement STMT.
1640 Valid only for assignments, calls, conditionals, and switches. */
1641
1642 static ccp_prop_value_t
1643 evaluate_stmt (gimple stmt)
1644 {
1645 ccp_prop_value_t val;
1646 tree simplified = NULL_TREE;
1647 ccp_lattice_t likelyvalue = likely_value (stmt);
1648 bool is_constant = false;
1649 unsigned int align;
1650
1651 if (dump_file && (dump_flags & TDF_DETAILS))
1652 {
1653 fprintf (dump_file, "which is likely ");
1654 switch (likelyvalue)
1655 {
1656 case CONSTANT:
1657 fprintf (dump_file, "CONSTANT");
1658 break;
1659 case UNDEFINED:
1660 fprintf (dump_file, "UNDEFINED");
1661 break;
1662 case VARYING:
1663 fprintf (dump_file, "VARYING");
1664 break;
1665 default:;
1666 }
1667 fprintf (dump_file, "\n");
1668 }
1669
1670 /* If the statement is likely to have a CONSTANT result, then try
1671 to fold the statement to determine the constant value. */
1672 /* FIXME. This is the only place that we call ccp_fold.
1673 Since likely_value never returns CONSTANT for calls, we will
1674 not attempt to fold them, including builtins that may profit. */
1675 if (likelyvalue == CONSTANT)
1676 {
1677 fold_defer_overflow_warnings ();
1678 simplified = ccp_fold (stmt);
1679 is_constant = simplified && is_gimple_min_invariant (simplified);
1680 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1681 if (is_constant)
1682 {
1683 /* The statement produced a constant value. */
1684 val.lattice_val = CONSTANT;
1685 val.value = simplified;
1686 val.mask = 0;
1687 }
1688 }
1689 /* If the statement is likely to have a VARYING result, then do not
1690 bother folding the statement. */
1691 else if (likelyvalue == VARYING)
1692 {
1693 enum gimple_code code = gimple_code (stmt);
1694 if (code == GIMPLE_ASSIGN)
1695 {
1696 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1697
1698 /* Other cases cannot satisfy is_gimple_min_invariant
1699 without folding. */
1700 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1701 simplified = gimple_assign_rhs1 (stmt);
1702 }
1703 else if (code == GIMPLE_SWITCH)
1704 simplified = gimple_switch_index (stmt);
1705 else
1706 /* These cannot satisfy is_gimple_min_invariant without folding. */
1707 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
1708 is_constant = simplified && is_gimple_min_invariant (simplified);
1709 if (is_constant)
1710 {
1711 /* The statement produced a constant value. */
1712 val.lattice_val = CONSTANT;
1713 val.value = simplified;
1714 val.mask = 0;
1715 }
1716 }
1717
1718 /* Resort to simplification for bitwise tracking. */
1719 if (flag_tree_bit_ccp
1720 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
1721 && !is_constant)
1722 {
1723 enum gimple_code code = gimple_code (stmt);
1724 val.lattice_val = VARYING;
1725 val.value = NULL_TREE;
1726 val.mask = -1;
1727 if (code == GIMPLE_ASSIGN)
1728 {
1729 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1730 tree rhs1 = gimple_assign_rhs1 (stmt);
1731 switch (get_gimple_rhs_class (subcode))
1732 {
1733 case GIMPLE_SINGLE_RHS:
1734 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1735 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1736 val = get_value_for_expr (rhs1, true);
1737 break;
1738
1739 case GIMPLE_UNARY_RHS:
1740 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1741 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1742 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1743 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1744 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1745 break;
1746
1747 case GIMPLE_BINARY_RHS:
1748 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1749 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1750 {
1751 tree lhs = gimple_assign_lhs (stmt);
1752 tree rhs2 = gimple_assign_rhs2 (stmt);
1753 val = bit_value_binop (subcode,
1754 TREE_TYPE (lhs), rhs1, rhs2);
1755 }
1756 break;
1757
1758 default:;
1759 }
1760 }
1761 else if (code == GIMPLE_COND)
1762 {
1763 enum tree_code code = gimple_cond_code (stmt);
1764 tree rhs1 = gimple_cond_lhs (stmt);
1765 tree rhs2 = gimple_cond_rhs (stmt);
1766 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1767 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1768 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1769 }
1770 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1771 {
1772 tree fndecl = gimple_call_fndecl (stmt);
1773 switch (DECL_FUNCTION_CODE (fndecl))
1774 {
1775 case BUILT_IN_MALLOC:
1776 case BUILT_IN_REALLOC:
1777 case BUILT_IN_CALLOC:
1778 case BUILT_IN_STRDUP:
1779 case BUILT_IN_STRNDUP:
1780 val.lattice_val = CONSTANT;
1781 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1782 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
1783 / BITS_PER_UNIT - 1);
1784 break;
1785
1786 case BUILT_IN_ALLOCA:
1787 case BUILT_IN_ALLOCA_WITH_ALIGN:
1788 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1789 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1790 : BIGGEST_ALIGNMENT);
1791 val.lattice_val = CONSTANT;
1792 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1793 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
1794 break;
1795
1796 /* These builtins return their first argument, unmodified. */
1797 case BUILT_IN_MEMCPY:
1798 case BUILT_IN_MEMMOVE:
1799 case BUILT_IN_MEMSET:
1800 case BUILT_IN_STRCPY:
1801 case BUILT_IN_STRNCPY:
1802 case BUILT_IN_MEMCPY_CHK:
1803 case BUILT_IN_MEMMOVE_CHK:
1804 case BUILT_IN_MEMSET_CHK:
1805 case BUILT_IN_STRCPY_CHK:
1806 case BUILT_IN_STRNCPY_CHK:
1807 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1808 break;
1809
1810 case BUILT_IN_ASSUME_ALIGNED:
1811 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
1812 break;
1813
1814 case BUILT_IN_ALIGNED_ALLOC:
1815 {
1816 tree align = get_constant_value (gimple_call_arg (stmt, 0));
1817 if (align
1818 && tree_fits_uhwi_p (align))
1819 {
1820 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
1821 if (aligni > 1
1822 /* align must be power-of-two */
1823 && (aligni & (aligni - 1)) == 0)
1824 {
1825 val.lattice_val = CONSTANT;
1826 val.value = build_int_cst (ptr_type_node, 0);
1827 val.mask = -aligni;
1828 }
1829 }
1830 break;
1831 }
1832
1833 default:;
1834 }
1835 }
1836 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
1837 {
1838 tree fntype = gimple_call_fntype (stmt);
1839 if (fntype)
1840 {
1841 tree attrs = lookup_attribute ("assume_aligned",
1842 TYPE_ATTRIBUTES (fntype));
1843 if (attrs)
1844 val = bit_value_assume_aligned (stmt, attrs, val, false);
1845 attrs = lookup_attribute ("alloc_align",
1846 TYPE_ATTRIBUTES (fntype));
1847 if (attrs)
1848 val = bit_value_assume_aligned (stmt, attrs, val, true);
1849 }
1850 }
1851 is_constant = (val.lattice_val == CONSTANT);
1852 }
1853
1854 if (flag_tree_bit_ccp
1855 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
1856 || (!is_constant && likelyvalue != UNDEFINED))
1857 && gimple_get_lhs (stmt)
1858 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
1859 {
1860 tree lhs = gimple_get_lhs (stmt);
1861 wide_int nonzero_bits = get_nonzero_bits (lhs);
1862 if (nonzero_bits != -1)
1863 {
1864 if (!is_constant)
1865 {
1866 val.lattice_val = CONSTANT;
1867 val.value = build_zero_cst (TREE_TYPE (lhs));
1868 val.mask = extend_mask (nonzero_bits);
1869 is_constant = true;
1870 }
1871 else
1872 {
1873 if (wi::bit_and_not (val.value, nonzero_bits) != 0)
1874 val.value = wide_int_to_tree (TREE_TYPE (lhs),
1875 nonzero_bits & val.value);
1876 if (nonzero_bits == 0)
1877 val.mask = 0;
1878 else
1879 val.mask = val.mask & extend_mask (nonzero_bits);
1880 }
1881 }
1882 }
1883
1884 if (!is_constant)
1885 {
1886 /* The statement produced a nonconstant value. If the statement
1887 had UNDEFINED operands, then the result of the statement
1888 should be UNDEFINED. Otherwise, the statement is VARYING. */
1889 if (likelyvalue == UNDEFINED)
1890 {
1891 val.lattice_val = likelyvalue;
1892 val.mask = 0;
1893 }
1894 else
1895 {
1896 val.lattice_val = VARYING;
1897 val.mask = -1;
1898 }
1899
1900 val.value = NULL_TREE;
1901 }
1902
1903 return val;
1904 }
1905
1906 typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
1907
1908 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1909 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1910
1911 static void
1912 insert_clobber_before_stack_restore (tree saved_val, tree var,
1913 gimple_htab **visited)
1914 {
1915 gimple stmt, clobber_stmt;
1916 tree clobber;
1917 imm_use_iterator iter;
1918 gimple_stmt_iterator i;
1919 gimple *slot;
1920
1921 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1922 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1923 {
1924 clobber = build_constructor (TREE_TYPE (var),
1925 NULL);
1926 TREE_THIS_VOLATILE (clobber) = 1;
1927 clobber_stmt = gimple_build_assign (var, clobber);
1928
1929 i = gsi_for_stmt (stmt);
1930 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1931 }
1932 else if (gimple_code (stmt) == GIMPLE_PHI)
1933 {
1934 if (!*visited)
1935 *visited = new gimple_htab (10);
1936
1937 slot = (*visited)->find_slot (stmt, INSERT);
1938 if (*slot != NULL)
1939 continue;
1940
1941 *slot = stmt;
1942 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1943 visited);
1944 }
1945 else if (gimple_assign_ssa_name_copy_p (stmt))
1946 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1947 visited);
1948 else
1949 gcc_assert (is_gimple_debug (stmt));
1950 }
1951
1952 /* Advance the iterator to the previous non-debug gimple statement in the same
1953 or dominating basic block. */
1954
1955 static inline void
1956 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1957 {
1958 basic_block dom;
1959
1960 gsi_prev_nondebug (i);
1961 while (gsi_end_p (*i))
1962 {
1963 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1964 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1965 return;
1966
1967 *i = gsi_last_bb (dom);
1968 }
1969 }
1970
1971 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1972 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1973
1974 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1975 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1976 that case the function gives up without inserting the clobbers. */
1977
1978 static void
1979 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1980 {
1981 gimple stmt;
1982 tree saved_val;
1983 gimple_htab *visited = NULL;
1984
1985 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1986 {
1987 stmt = gsi_stmt (i);
1988
1989 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1990 continue;
1991
1992 saved_val = gimple_call_lhs (stmt);
1993 if (saved_val == NULL_TREE)
1994 continue;
1995
1996 insert_clobber_before_stack_restore (saved_val, var, &visited);
1997 break;
1998 }
1999
2000 delete visited;
2001 }
2002
2003 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2004 fixed-size array and returns the address, if found, otherwise returns
2005 NULL_TREE. */
2006
2007 static tree
2008 fold_builtin_alloca_with_align (gimple stmt)
2009 {
2010 unsigned HOST_WIDE_INT size, threshold, n_elem;
2011 tree lhs, arg, block, var, elem_type, array_type;
2012
2013 /* Get lhs. */
2014 lhs = gimple_call_lhs (stmt);
2015 if (lhs == NULL_TREE)
2016 return NULL_TREE;
2017
2018 /* Detect constant argument. */
2019 arg = get_constant_value (gimple_call_arg (stmt, 0));
2020 if (arg == NULL_TREE
2021 || TREE_CODE (arg) != INTEGER_CST
2022 || !tree_fits_uhwi_p (arg))
2023 return NULL_TREE;
2024
2025 size = tree_to_uhwi (arg);
2026
2027 /* Heuristic: don't fold large allocas. */
2028 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
2029 /* In case the alloca is located at function entry, it has the same lifetime
2030 as a declared array, so we allow a larger size. */
2031 block = gimple_block (stmt);
2032 if (!(cfun->after_inlining
2033 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2034 threshold /= 10;
2035 if (size > threshold)
2036 return NULL_TREE;
2037
2038 /* Declare array. */
2039 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2040 n_elem = size * 8 / BITS_PER_UNIT;
2041 array_type = build_array_type_nelts (elem_type, n_elem);
2042 var = create_tmp_var (array_type, NULL);
2043 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
2044 {
2045 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2046 if (pi != NULL && !pi->pt.anything)
2047 {
2048 bool singleton_p;
2049 unsigned uid;
2050 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
2051 gcc_assert (singleton_p);
2052 SET_DECL_PT_UID (var, uid);
2053 }
2054 }
2055
2056 /* Fold alloca to the address of the array. */
2057 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2058 }
2059
2060 /* Fold the stmt at *GSI with CCP specific information that propagating
2061 and regular folding does not catch. */
2062
2063 static bool
2064 ccp_fold_stmt (gimple_stmt_iterator *gsi)
2065 {
2066 gimple stmt = gsi_stmt (*gsi);
2067
2068 switch (gimple_code (stmt))
2069 {
2070 case GIMPLE_COND:
2071 {
2072 ccp_prop_value_t val;
2073 /* Statement evaluation will handle type mismatches in constants
2074 more gracefully than the final propagation. This allows us to
2075 fold more conditionals here. */
2076 val = evaluate_stmt (stmt);
2077 if (val.lattice_val != CONSTANT
2078 || val.mask != 0)
2079 return false;
2080
2081 if (dump_file)
2082 {
2083 fprintf (dump_file, "Folding predicate ");
2084 print_gimple_expr (dump_file, stmt, 0, 0);
2085 fprintf (dump_file, " to ");
2086 print_generic_expr (dump_file, val.value, 0);
2087 fprintf (dump_file, "\n");
2088 }
2089
2090 if (integer_zerop (val.value))
2091 gimple_cond_make_false (stmt);
2092 else
2093 gimple_cond_make_true (stmt);
2094
2095 return true;
2096 }
2097
2098 case GIMPLE_CALL:
2099 {
2100 tree lhs = gimple_call_lhs (stmt);
2101 int flags = gimple_call_flags (stmt);
2102 tree val;
2103 tree argt;
2104 bool changed = false;
2105 unsigned i;
2106
2107 /* If the call was folded into a constant make sure it goes
2108 away even if we cannot propagate into all uses because of
2109 type issues. */
2110 if (lhs
2111 && TREE_CODE (lhs) == SSA_NAME
2112 && (val = get_constant_value (lhs))
2113 /* Don't optimize away calls that have side-effects. */
2114 && (flags & (ECF_CONST|ECF_PURE)) != 0
2115 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2116 {
2117 tree new_rhs = unshare_expr (val);
2118 bool res;
2119 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2120 TREE_TYPE (new_rhs)))
2121 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2122 res = update_call_from_tree (gsi, new_rhs);
2123 gcc_assert (res);
2124 return true;
2125 }
2126
2127 /* Internal calls provide no argument types, so the extra laxity
2128 for normal calls does not apply. */
2129 if (gimple_call_internal_p (stmt))
2130 return false;
2131
2132 /* The heuristic of fold_builtin_alloca_with_align differs before and
2133 after inlining, so we don't require the arg to be changed into a
2134 constant for folding, but just to be constant. */
2135 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
2136 {
2137 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2138 if (new_rhs)
2139 {
2140 bool res = update_call_from_tree (gsi, new_rhs);
2141 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2142 gcc_assert (res);
2143 insert_clobbers_for_var (*gsi, var);
2144 return true;
2145 }
2146 }
2147
2148 /* Propagate into the call arguments. Compared to replace_uses_in
2149 this can use the argument slot types for type verification
2150 instead of the current argument type. We also can safely
2151 drop qualifiers here as we are dealing with constants anyway. */
2152 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2153 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2154 ++i, argt = TREE_CHAIN (argt))
2155 {
2156 tree arg = gimple_call_arg (stmt, i);
2157 if (TREE_CODE (arg) == SSA_NAME
2158 && (val = get_constant_value (arg))
2159 && useless_type_conversion_p
2160 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2161 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2162 {
2163 gimple_call_set_arg (stmt, i, unshare_expr (val));
2164 changed = true;
2165 }
2166 }
2167
2168 return changed;
2169 }
2170
2171 case GIMPLE_ASSIGN:
2172 {
2173 tree lhs = gimple_assign_lhs (stmt);
2174 tree val;
2175
2176 /* If we have a load that turned out to be constant replace it
2177 as we cannot propagate into all uses in all cases. */
2178 if (gimple_assign_single_p (stmt)
2179 && TREE_CODE (lhs) == SSA_NAME
2180 && (val = get_constant_value (lhs)))
2181 {
2182 tree rhs = unshare_expr (val);
2183 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2184 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2185 gimple_assign_set_rhs_from_tree (gsi, rhs);
2186 return true;
2187 }
2188
2189 return false;
2190 }
2191
2192 default:
2193 return false;
2194 }
2195 }
2196
2197 /* Visit the assignment statement STMT. Set the value of its LHS to the
2198 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2199 creates virtual definitions, set the value of each new name to that
2200 of the RHS (if we can derive a constant out of the RHS).
2201 Value-returning call statements also perform an assignment, and
2202 are handled here. */
2203
2204 static enum ssa_prop_result
2205 visit_assignment (gimple stmt, tree *output_p)
2206 {
2207 ccp_prop_value_t val;
2208 enum ssa_prop_result retval;
2209
2210 tree lhs = gimple_get_lhs (stmt);
2211
2212 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2213 || gimple_call_lhs (stmt) != NULL_TREE);
2214
2215 if (gimple_assign_single_p (stmt)
2216 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2217 /* For a simple copy operation, we copy the lattice values. */
2218 val = *get_value (gimple_assign_rhs1 (stmt));
2219 else
2220 /* Evaluate the statement, which could be
2221 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2222 val = evaluate_stmt (stmt);
2223
2224 retval = SSA_PROP_NOT_INTERESTING;
2225
2226 /* Set the lattice value of the statement's output. */
2227 if (TREE_CODE (lhs) == SSA_NAME)
2228 {
2229 /* If STMT is an assignment to an SSA_NAME, we only have one
2230 value to set. */
2231 if (set_lattice_value (lhs, val))
2232 {
2233 *output_p = lhs;
2234 if (val.lattice_val == VARYING)
2235 retval = SSA_PROP_VARYING;
2236 else
2237 retval = SSA_PROP_INTERESTING;
2238 }
2239 }
2240
2241 return retval;
2242 }
2243
2244
2245 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2246 if it can determine which edge will be taken. Otherwise, return
2247 SSA_PROP_VARYING. */
2248
2249 static enum ssa_prop_result
2250 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2251 {
2252 ccp_prop_value_t val;
2253 basic_block block;
2254
2255 block = gimple_bb (stmt);
2256 val = evaluate_stmt (stmt);
2257 if (val.lattice_val != CONSTANT
2258 || val.mask != 0)
2259 return SSA_PROP_VARYING;
2260
2261 /* Find which edge out of the conditional block will be taken and add it
2262 to the worklist. If no single edge can be determined statically,
2263 return SSA_PROP_VARYING to feed all the outgoing edges to the
2264 propagation engine. */
2265 *taken_edge_p = find_taken_edge (block, val.value);
2266 if (*taken_edge_p)
2267 return SSA_PROP_INTERESTING;
2268 else
2269 return SSA_PROP_VARYING;
2270 }
2271
2272
2273 /* Evaluate statement STMT. If the statement produces an output value and
2274 its evaluation changes the lattice value of its output, return
2275 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2276 output value.
2277
2278 If STMT is a conditional branch and we can determine its truth
2279 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2280 value, return SSA_PROP_VARYING. */
2281
2282 static enum ssa_prop_result
2283 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2284 {
2285 tree def;
2286 ssa_op_iter iter;
2287
2288 if (dump_file && (dump_flags & TDF_DETAILS))
2289 {
2290 fprintf (dump_file, "\nVisiting statement:\n");
2291 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2292 }
2293
2294 switch (gimple_code (stmt))
2295 {
2296 case GIMPLE_ASSIGN:
2297 /* If the statement is an assignment that produces a single
2298 output value, evaluate its RHS to see if the lattice value of
2299 its output has changed. */
2300 return visit_assignment (stmt, output_p);
2301
2302 case GIMPLE_CALL:
2303 /* A value-returning call also performs an assignment. */
2304 if (gimple_call_lhs (stmt) != NULL_TREE)
2305 return visit_assignment (stmt, output_p);
2306 break;
2307
2308 case GIMPLE_COND:
2309 case GIMPLE_SWITCH:
2310 /* If STMT is a conditional branch, see if we can determine
2311 which branch will be taken. */
2312 /* FIXME. It appears that we should be able to optimize
2313 computed GOTOs here as well. */
2314 return visit_cond_stmt (stmt, taken_edge_p);
2315
2316 default:
2317 break;
2318 }
2319
2320 /* Any other kind of statement is not interesting for constant
2321 propagation and, therefore, not worth simulating. */
2322 if (dump_file && (dump_flags & TDF_DETAILS))
2323 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2324
2325 /* Definitions made by statements other than assignments to
2326 SSA_NAMEs represent unknown modifications to their outputs.
2327 Mark them VARYING. */
2328 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2329 {
2330 ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
2331 set_lattice_value (def, v);
2332 }
2333
2334 return SSA_PROP_VARYING;
2335 }
2336
2337
2338 /* Main entry point for SSA Conditional Constant Propagation. */
2339
2340 static unsigned int
2341 do_ssa_ccp (void)
2342 {
2343 unsigned int todo = 0;
2344 calculate_dominance_info (CDI_DOMINATORS);
2345 ccp_initialize ();
2346 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2347 if (ccp_finalize ())
2348 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2349 free_dominance_info (CDI_DOMINATORS);
2350 return todo;
2351 }
2352
2353
2354 namespace {
2355
2356 const pass_data pass_data_ccp =
2357 {
2358 GIMPLE_PASS, /* type */
2359 "ccp", /* name */
2360 OPTGROUP_NONE, /* optinfo_flags */
2361 TV_TREE_CCP, /* tv_id */
2362 ( PROP_cfg | PROP_ssa ), /* properties_required */
2363 0, /* properties_provided */
2364 0, /* properties_destroyed */
2365 0, /* todo_flags_start */
2366 TODO_update_address_taken, /* todo_flags_finish */
2367 };
2368
2369 class pass_ccp : public gimple_opt_pass
2370 {
2371 public:
2372 pass_ccp (gcc::context *ctxt)
2373 : gimple_opt_pass (pass_data_ccp, ctxt)
2374 {}
2375
2376 /* opt_pass methods: */
2377 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2378 virtual bool gate (function *) { return flag_tree_ccp != 0; }
2379 virtual unsigned int execute (function *) { return do_ssa_ccp (); }
2380
2381 }; // class pass_ccp
2382
2383 } // anon namespace
2384
2385 gimple_opt_pass *
2386 make_pass_ccp (gcc::context *ctxt)
2387 {
2388 return new pass_ccp (ctxt);
2389 }
2390
2391
2392
2393 /* Try to optimize out __builtin_stack_restore. Optimize it out
2394 if there is another __builtin_stack_restore in the same basic
2395 block and no calls or ASM_EXPRs are in between, or if this block's
2396 only outgoing edge is to EXIT_BLOCK and there are no calls or
2397 ASM_EXPRs after this __builtin_stack_restore. */
2398
2399 static tree
2400 optimize_stack_restore (gimple_stmt_iterator i)
2401 {
2402 tree callee;
2403 gimple stmt;
2404
2405 basic_block bb = gsi_bb (i);
2406 gimple call = gsi_stmt (i);
2407
2408 if (gimple_code (call) != GIMPLE_CALL
2409 || gimple_call_num_args (call) != 1
2410 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2411 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2412 return NULL_TREE;
2413
2414 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2415 {
2416 stmt = gsi_stmt (i);
2417 if (gimple_code (stmt) == GIMPLE_ASM)
2418 return NULL_TREE;
2419 if (gimple_code (stmt) != GIMPLE_CALL)
2420 continue;
2421
2422 callee = gimple_call_fndecl (stmt);
2423 if (!callee
2424 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2425 /* All regular builtins are ok, just obviously not alloca. */
2426 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2427 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2428 return NULL_TREE;
2429
2430 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2431 goto second_stack_restore;
2432 }
2433
2434 if (!gsi_end_p (i))
2435 return NULL_TREE;
2436
2437 /* Allow one successor of the exit block, or zero successors. */
2438 switch (EDGE_COUNT (bb->succs))
2439 {
2440 case 0:
2441 break;
2442 case 1:
2443 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2444 return NULL_TREE;
2445 break;
2446 default:
2447 return NULL_TREE;
2448 }
2449 second_stack_restore:
2450
2451 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2452 If there are multiple uses, then the last one should remove the call.
2453 In any case, whether the call to __builtin_stack_save can be removed
2454 or not is irrelevant to removing the call to __builtin_stack_restore. */
2455 if (has_single_use (gimple_call_arg (call, 0)))
2456 {
2457 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2458 if (is_gimple_call (stack_save))
2459 {
2460 callee = gimple_call_fndecl (stack_save);
2461 if (callee
2462 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2463 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2464 {
2465 gimple_stmt_iterator stack_save_gsi;
2466 tree rhs;
2467
2468 stack_save_gsi = gsi_for_stmt (stack_save);
2469 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2470 update_call_from_tree (&stack_save_gsi, rhs);
2471 }
2472 }
2473 }
2474
2475 /* No effect, so the statement will be deleted. */
2476 return integer_zero_node;
2477 }
2478
2479 /* If va_list type is a simple pointer and nothing special is needed,
2480 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2481 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2482 pointer assignment. */
2483
2484 static tree
2485 optimize_stdarg_builtin (gimple call)
2486 {
2487 tree callee, lhs, rhs, cfun_va_list;
2488 bool va_list_simple_ptr;
2489 location_t loc = gimple_location (call);
2490
2491 if (gimple_code (call) != GIMPLE_CALL)
2492 return NULL_TREE;
2493
2494 callee = gimple_call_fndecl (call);
2495
2496 cfun_va_list = targetm.fn_abi_va_list (callee);
2497 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2498 && (TREE_TYPE (cfun_va_list) == void_type_node
2499 || TREE_TYPE (cfun_va_list) == char_type_node);
2500
2501 switch (DECL_FUNCTION_CODE (callee))
2502 {
2503 case BUILT_IN_VA_START:
2504 if (!va_list_simple_ptr
2505 || targetm.expand_builtin_va_start != NULL
2506 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2507 return NULL_TREE;
2508
2509 if (gimple_call_num_args (call) != 2)
2510 return NULL_TREE;
2511
2512 lhs = gimple_call_arg (call, 0);
2513 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2514 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2515 != TYPE_MAIN_VARIANT (cfun_va_list))
2516 return NULL_TREE;
2517
2518 lhs = build_fold_indirect_ref_loc (loc, lhs);
2519 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2520 1, integer_zero_node);
2521 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2522 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2523
2524 case BUILT_IN_VA_COPY:
2525 if (!va_list_simple_ptr)
2526 return NULL_TREE;
2527
2528 if (gimple_call_num_args (call) != 2)
2529 return NULL_TREE;
2530
2531 lhs = gimple_call_arg (call, 0);
2532 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2533 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2534 != TYPE_MAIN_VARIANT (cfun_va_list))
2535 return NULL_TREE;
2536
2537 lhs = build_fold_indirect_ref_loc (loc, lhs);
2538 rhs = gimple_call_arg (call, 1);
2539 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2540 != TYPE_MAIN_VARIANT (cfun_va_list))
2541 return NULL_TREE;
2542
2543 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2544 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2545
2546 case BUILT_IN_VA_END:
2547 /* No effect, so the statement will be deleted. */
2548 return integer_zero_node;
2549
2550 default:
2551 gcc_unreachable ();
2552 }
2553 }
2554
2555 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2556 the incoming jumps. Return true if at least one jump was changed. */
2557
2558 static bool
2559 optimize_unreachable (gimple_stmt_iterator i)
2560 {
2561 basic_block bb = gsi_bb (i);
2562 gimple_stmt_iterator gsi;
2563 gimple stmt;
2564 edge_iterator ei;
2565 edge e;
2566 bool ret;
2567
2568 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2569 {
2570 stmt = gsi_stmt (gsi);
2571
2572 if (is_gimple_debug (stmt))
2573 continue;
2574
2575 if (gimple_code (stmt) == GIMPLE_LABEL)
2576 {
2577 /* Verify we do not need to preserve the label. */
2578 if (FORCED_LABEL (gimple_label_label (stmt)))
2579 return false;
2580
2581 continue;
2582 }
2583
2584 /* Only handle the case that __builtin_unreachable is the first statement
2585 in the block. We rely on DCE to remove stmts without side-effects
2586 before __builtin_unreachable. */
2587 if (gsi_stmt (gsi) != gsi_stmt (i))
2588 return false;
2589 }
2590
2591 ret = false;
2592 FOR_EACH_EDGE (e, ei, bb->preds)
2593 {
2594 gsi = gsi_last_bb (e->src);
2595 if (gsi_end_p (gsi))
2596 continue;
2597
2598 stmt = gsi_stmt (gsi);
2599 if (gimple_code (stmt) == GIMPLE_COND)
2600 {
2601 if (e->flags & EDGE_TRUE_VALUE)
2602 gimple_cond_make_false (stmt);
2603 else if (e->flags & EDGE_FALSE_VALUE)
2604 gimple_cond_make_true (stmt);
2605 else
2606 gcc_unreachable ();
2607 update_stmt (stmt);
2608 }
2609 else
2610 {
2611 /* Todo: handle other cases, f.i. switch statement. */
2612 continue;
2613 }
2614
2615 ret = true;
2616 }
2617
2618 return ret;
2619 }
2620
2621 /* A simple pass that attempts to fold all builtin functions. This pass
2622 is run after we've propagated as many constants as we can. */
2623
2624 namespace {
2625
2626 const pass_data pass_data_fold_builtins =
2627 {
2628 GIMPLE_PASS, /* type */
2629 "fab", /* name */
2630 OPTGROUP_NONE, /* optinfo_flags */
2631 TV_NONE, /* tv_id */
2632 ( PROP_cfg | PROP_ssa ), /* properties_required */
2633 0, /* properties_provided */
2634 0, /* properties_destroyed */
2635 0, /* todo_flags_start */
2636 TODO_update_ssa, /* todo_flags_finish */
2637 };
2638
2639 class pass_fold_builtins : public gimple_opt_pass
2640 {
2641 public:
2642 pass_fold_builtins (gcc::context *ctxt)
2643 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2644 {}
2645
2646 /* opt_pass methods: */
2647 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2648 virtual unsigned int execute (function *);
2649
2650 }; // class pass_fold_builtins
2651
2652 unsigned int
2653 pass_fold_builtins::execute (function *fun)
2654 {
2655 bool cfg_changed = false;
2656 basic_block bb;
2657 unsigned int todoflags = 0;
2658
2659 FOR_EACH_BB_FN (bb, fun)
2660 {
2661 gimple_stmt_iterator i;
2662 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2663 {
2664 gimple stmt, old_stmt;
2665 tree callee;
2666 enum built_in_function fcode;
2667
2668 stmt = gsi_stmt (i);
2669
2670 if (gimple_code (stmt) != GIMPLE_CALL)
2671 {
2672 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2673 after the last GIMPLE DSE they aren't needed and might
2674 unnecessarily keep the SSA_NAMEs live. */
2675 if (gimple_clobber_p (stmt))
2676 {
2677 tree lhs = gimple_assign_lhs (stmt);
2678 if (TREE_CODE (lhs) == MEM_REF
2679 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2680 {
2681 unlink_stmt_vdef (stmt);
2682 gsi_remove (&i, true);
2683 release_defs (stmt);
2684 continue;
2685 }
2686 }
2687 gsi_next (&i);
2688 continue;
2689 }
2690
2691 callee = gimple_call_fndecl (stmt);
2692 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2693 {
2694 gsi_next (&i);
2695 continue;
2696 }
2697
2698 fcode = DECL_FUNCTION_CODE (callee);
2699 if (fold_stmt (&i))
2700 ;
2701 else
2702 {
2703 tree result = NULL_TREE;
2704 switch (DECL_FUNCTION_CODE (callee))
2705 {
2706 case BUILT_IN_CONSTANT_P:
2707 /* Resolve __builtin_constant_p. If it hasn't been
2708 folded to integer_one_node by now, it's fairly
2709 certain that the value simply isn't constant. */
2710 result = integer_zero_node;
2711 break;
2712
2713 case BUILT_IN_ASSUME_ALIGNED:
2714 /* Remove __builtin_assume_aligned. */
2715 result = gimple_call_arg (stmt, 0);
2716 break;
2717
2718 case BUILT_IN_STACK_RESTORE:
2719 result = optimize_stack_restore (i);
2720 if (result)
2721 break;
2722 gsi_next (&i);
2723 continue;
2724
2725 case BUILT_IN_UNREACHABLE:
2726 if (optimize_unreachable (i))
2727 cfg_changed = true;
2728 break;
2729
2730 case BUILT_IN_VA_START:
2731 case BUILT_IN_VA_END:
2732 case BUILT_IN_VA_COPY:
2733 /* These shouldn't be folded before pass_stdarg. */
2734 result = optimize_stdarg_builtin (stmt);
2735 if (result)
2736 break;
2737 /* FALLTHRU */
2738
2739 default:;
2740 }
2741
2742 if (!result)
2743 {
2744 gsi_next (&i);
2745 continue;
2746 }
2747
2748 if (!update_call_from_tree (&i, result))
2749 gimplify_and_update_call_from_tree (&i, result);
2750 }
2751
2752 todoflags |= TODO_update_address_taken;
2753
2754 if (dump_file && (dump_flags & TDF_DETAILS))
2755 {
2756 fprintf (dump_file, "Simplified\n ");
2757 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2758 }
2759
2760 old_stmt = stmt;
2761 stmt = gsi_stmt (i);
2762 update_stmt (stmt);
2763
2764 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2765 && gimple_purge_dead_eh_edges (bb))
2766 cfg_changed = true;
2767
2768 if (dump_file && (dump_flags & TDF_DETAILS))
2769 {
2770 fprintf (dump_file, "to\n ");
2771 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2772 fprintf (dump_file, "\n");
2773 }
2774
2775 /* Retry the same statement if it changed into another
2776 builtin, there might be new opportunities now. */
2777 if (gimple_code (stmt) != GIMPLE_CALL)
2778 {
2779 gsi_next (&i);
2780 continue;
2781 }
2782 callee = gimple_call_fndecl (stmt);
2783 if (!callee
2784 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2785 || DECL_FUNCTION_CODE (callee) == fcode)
2786 gsi_next (&i);
2787 }
2788 }
2789
2790 /* Delete unreachable blocks. */
2791 if (cfg_changed)
2792 todoflags |= TODO_cleanup_cfg;
2793
2794 return todoflags;
2795 }
2796
2797 } // anon namespace
2798
2799 gimple_opt_pass *
2800 make_pass_fold_builtins (gcc::context *ctxt)
2801 {
2802 return new pass_fold_builtins (ctxt);
2803 }