]> gcc.gnu.org Git - gcc.git/blob - gcc/gimplify.c
640e3b04bf125a352bbb87a6dec863022fc287ec
[gcc.git] / gcc / gimplify.c
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "rtl.h"
30 #include "errors.h"
31 #include "varray.h"
32 #include "tree-gimple.h"
33 #include "tree-inline.h"
34 #include "diagnostic.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "tree-flow.h"
38 #include "cgraph.h"
39 #include "timevar.h"
40 #include "except.h"
41 #include "hashtab.h"
42 #include "flags.h"
43 #include "real.h"
44 #include "function.h"
45 #include "output.h"
46 #include "expr.h"
47 #include "ggc.h"
48 #include "target.h"
49
50 static struct gimplify_ctx
51 {
52 tree current_bind_expr;
53 tree temps;
54 tree conditional_cleanups;
55 tree exit_label;
56 tree return_temp;
57 varray_type case_labels;
58 /* The formal temporary table. Should this be persistent? */
59 htab_t temp_htab;
60 int conditions;
61 bool save_stack;
62 bool into_ssa;
63 } *gimplify_ctxp;
64
65
66 /* Formal (expression) temporary table handling: Multiple occurrences of
67 the same scalar expression are evaluated into the same temporary. */
68
69 typedef struct gimple_temp_hash_elt
70 {
71 tree val; /* Key */
72 tree temp; /* Value */
73 } elt_t;
74
75 /* Forward declarations. */
76 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
77
78
79 /* Return a hash value for a formal temporary table entry. */
80
81 static hashval_t
82 gimple_tree_hash (const void *p)
83 {
84 tree t = ((const elt_t *) p)->val;
85 return iterative_hash_expr (t, 0);
86 }
87
88 /* Compare two formal temporary table entries. */
89
90 static int
91 gimple_tree_eq (const void *p1, const void *p2)
92 {
93 tree t1 = ((const elt_t *) p1)->val;
94 tree t2 = ((const elt_t *) p2)->val;
95 enum tree_code code = TREE_CODE (t1);
96
97 if (TREE_CODE (t2) != code
98 || TREE_TYPE (t1) != TREE_TYPE (t2))
99 return 0;
100
101 if (!operand_equal_p (t1, t2, 0))
102 return 0;
103
104 /* Only allow them to compare equal if they also hash equal; otherwise
105 results are nondeterminate, and we fail bootstrap comparison. */
106 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
107
108 return 1;
109 }
110
111 /* Set up a context for the gimplifier. */
112
113 void
114 push_gimplify_context (void)
115 {
116 gcc_assert (!gimplify_ctxp);
117 gimplify_ctxp
118 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
119 if (optimize)
120 gimplify_ctxp->temp_htab
121 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
122 else
123 gimplify_ctxp->temp_htab = NULL;
124 }
125
126 /* Tear down a context for the gimplifier. If BODY is non-null, then
127 put the temporaries into the outer BIND_EXPR. Otherwise, put them
128 in the unexpanded_var_list. */
129
130 void
131 pop_gimplify_context (tree body)
132 {
133 tree t;
134
135 gcc_assert (gimplify_ctxp && !gimplify_ctxp->current_bind_expr);
136
137 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
138 DECL_GIMPLE_FORMAL_TEMP_P (t) = 0;
139
140 if (body)
141 declare_tmp_vars (gimplify_ctxp->temps, body);
142 else
143 record_vars (gimplify_ctxp->temps);
144
145 #if 0
146 if (!quiet_flag && optimize)
147 fprintf (stderr, " collisions: %f ",
148 htab_collisions (gimplify_ctxp->temp_htab));
149 #endif
150
151 if (optimize)
152 htab_delete (gimplify_ctxp->temp_htab);
153 free (gimplify_ctxp);
154 gimplify_ctxp = NULL;
155 }
156
157 void
158 gimple_push_bind_expr (tree bind)
159 {
160 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
161 gimplify_ctxp->current_bind_expr = bind;
162 }
163
164 void
165 gimple_pop_bind_expr (void)
166 {
167 gimplify_ctxp->current_bind_expr
168 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
169 }
170
171 tree
172 gimple_current_bind_expr (void)
173 {
174 return gimplify_ctxp->current_bind_expr;
175 }
176
177 /* Returns true iff there is a COND_EXPR between us and the innermost
178 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
179
180 static bool
181 gimple_conditional_context (void)
182 {
183 return gimplify_ctxp->conditions > 0;
184 }
185
186 /* Note that we've entered a COND_EXPR. */
187
188 static void
189 gimple_push_condition (void)
190 {
191 ++(gimplify_ctxp->conditions);
192 }
193
194 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
195 now, add any conditional cleanups we've seen to the prequeue. */
196
197 static void
198 gimple_pop_condition (tree *pre_p)
199 {
200 int conds = --(gimplify_ctxp->conditions);
201
202 gcc_assert (conds >= 0);
203 if (conds == 0)
204 {
205 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
206 gimplify_ctxp->conditional_cleanups = NULL_TREE;
207 }
208 }
209
210 /* A subroutine of append_to_statement_list{,_force}. */
211
212 static void
213 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
214 {
215 tree list = *list_p;
216 tree_stmt_iterator i;
217
218 if (!side_effects)
219 return;
220
221 if (!list)
222 {
223 if (t && TREE_CODE (t) == STATEMENT_LIST)
224 {
225 *list_p = t;
226 return;
227 }
228 *list_p = list = alloc_stmt_list ();
229 }
230
231 i = tsi_last (list);
232 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
233 }
234
235 /* Add T to the end of the list container pointed by LIST_P.
236 If T is an expression with no effects, it is ignored. */
237
238 void
239 append_to_statement_list (tree t, tree *list_p)
240 {
241 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
242 }
243
244 /* Similar, but the statement is always added, regardless of side effects. */
245
246 void
247 append_to_statement_list_force (tree t, tree *list_p)
248 {
249 append_to_statement_list_1 (t, list_p, t != NULL);
250 }
251
252 /* Both gimplify the statement T and append it to LIST_P. */
253
254 void
255 gimplify_and_add (tree t, tree *list_p)
256 {
257 gimplify_stmt (&t);
258 append_to_statement_list (t, list_p);
259 }
260
261 /* Strip off a legitimate source ending from the input string NAME of
262 length LEN. Rather than having to know the names used by all of
263 our front ends, we strip off an ending of a period followed by
264 up to five characters. (Java uses ".class".) */
265
266 static inline void
267 remove_suffix (char *name, int len)
268 {
269 int i;
270
271 for (i = 2; i < 8 && len > i; i++)
272 {
273 if (name[len - i] == '.')
274 {
275 name[len - i] = '\0';
276 break;
277 }
278 }
279 }
280
281 /* Create a nameless artificial label and put it in the current function
282 context. Returns the newly created label. */
283
284 tree
285 create_artificial_label (void)
286 {
287 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
288
289 DECL_ARTIFICIAL (lab) = 1;
290 DECL_CONTEXT (lab) = current_function_decl;
291 return lab;
292 }
293
294 /* Create a new temporary name with PREFIX. Returns an identifier. */
295
296 static GTY(()) unsigned int tmp_var_id_num;
297
298 tree
299 create_tmp_var_name (const char *prefix)
300 {
301 char *tmp_name;
302
303 if (prefix)
304 {
305 char *preftmp = ASTRDUP (prefix);
306
307 remove_suffix (preftmp, strlen (preftmp));
308 prefix = preftmp;
309 }
310
311 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
312 return get_identifier (tmp_name);
313 }
314
315
316 /* Create a new temporary variable declaration of type TYPE.
317 Does NOT push it into the current binding. */
318
319 tree
320 create_tmp_var_raw (tree type, const char *prefix)
321 {
322 tree tmp_var;
323 tree new_type;
324
325 /* Make the type of the variable writable. */
326 new_type = build_type_variant (type, 0, 0);
327 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
328
329 tmp_var = build_decl (VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
330 type);
331
332 /* The variable was declared by the compiler. */
333 DECL_ARTIFICIAL (tmp_var) = 1;
334 /* And we don't want debug info for it. */
335 DECL_IGNORED_P (tmp_var) = 1;
336
337 /* Make the variable writable. */
338 TREE_READONLY (tmp_var) = 0;
339
340 DECL_EXTERNAL (tmp_var) = 0;
341 TREE_STATIC (tmp_var) = 0;
342 TREE_USED (tmp_var) = 1;
343
344 return tmp_var;
345 }
346
347 /* Create a new temporary variable declaration of type TYPE. DOES push the
348 variable into the current binding. Further, assume that this is called
349 only from gimplification or optimization, at which point the creation of
350 certain types are bugs. */
351
352 tree
353 create_tmp_var (tree type, const char *prefix)
354 {
355 tree tmp_var;
356
357 /* We don't allow types that are addressable (meaning we can't make copies),
358 incomplete, or of variable size. */
359 gcc_assert (!TREE_ADDRESSABLE (type)
360 && COMPLETE_TYPE_P (type)
361 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
362
363 tmp_var = create_tmp_var_raw (type, prefix);
364 gimple_add_tmp_var (tmp_var);
365 return tmp_var;
366 }
367
368 /* Given a tree, try to return a useful variable name that we can use
369 to prefix a temporary that is being assigned the value of the tree.
370 I.E. given <temp> = &A, return A. */
371
372 const char *
373 get_name (tree t)
374 {
375 tree stripped_decl;
376
377 stripped_decl = t;
378 STRIP_NOPS (stripped_decl);
379 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
380 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
381 else
382 {
383 switch (TREE_CODE (stripped_decl))
384 {
385 case ADDR_EXPR:
386 return get_name (TREE_OPERAND (stripped_decl, 0));
387 break;
388 default:
389 return NULL;
390 }
391 }
392 }
393
394 /* Create a temporary with a name derived from VAL. Subroutine of
395 lookup_tmp_var; nobody else should call this function. */
396
397 static inline tree
398 create_tmp_from_val (tree val)
399 {
400 return create_tmp_var (TREE_TYPE (val), get_name (val));
401 }
402
403 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
404 an existing expression temporary. */
405
406 static tree
407 lookup_tmp_var (tree val, bool is_formal)
408 {
409 tree ret;
410
411 /* If not optimizing, never really reuse a temporary. local-alloc
412 won't allocate any variable that is used in more than one basic
413 block, which means it will go into memory, causing much extra
414 work in reload and final and poorer code generation, outweighing
415 the extra memory allocation here. */
416 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
417 ret = create_tmp_from_val (val);
418 else
419 {
420 elt_t elt, *elt_p;
421 void **slot;
422
423 elt.val = val;
424 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
425 if (*slot == NULL)
426 {
427 elt_p = xmalloc (sizeof (*elt_p));
428 elt_p->val = val;
429 elt_p->temp = ret = create_tmp_from_val (val);
430 *slot = (void *) elt_p;
431 }
432 else
433 {
434 elt_p = (elt_t *) *slot;
435 ret = elt_p->temp;
436 }
437 }
438
439 if (is_formal)
440 DECL_GIMPLE_FORMAL_TEMP_P (ret) = 1;
441
442 return ret;
443 }
444
445 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
446 in gimplify_expr. Only use this function if:
447
448 1) The value of the unfactored expression represented by VAL will not
449 change between the initialization and use of the temporary, and
450 2) The temporary will not be otherwise modified.
451
452 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
453 and #2 means it is inappropriate for && temps.
454
455 For other cases, use get_initialized_tmp_var instead. */
456
457 static tree
458 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
459 {
460 tree t, mod;
461
462 gimplify_expr (&val, pre_p, post_p, is_gimple_formal_tmp_rhs, fb_rvalue);
463
464 t = lookup_tmp_var (val, is_formal);
465
466 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
467
468 if (EXPR_HAS_LOCATION (val))
469 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
470 else
471 SET_EXPR_LOCATION (mod, input_location);
472
473 /* gimplify_modify_expr might want to reduce this further. */
474 gimplify_and_add (mod, pre_p);
475
476 /* If we're gimplifying into ssa, gimplify_modify_expr will have
477 given our temporary an ssa name. Find and return it. */
478 if (gimplify_ctxp->into_ssa)
479 t = TREE_OPERAND (mod, 0);
480
481 return t;
482 }
483
484 tree
485 get_formal_tmp_var (tree val, tree *pre_p)
486 {
487 return internal_get_tmp_var (val, pre_p, NULL, true);
488 }
489
490 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
491 are as in gimplify_expr. */
492
493 tree
494 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
495 {
496 return internal_get_tmp_var (val, pre_p, post_p, false);
497 }
498
499 /* Declares all the variables in VARS in SCOPE. */
500
501 void
502 declare_tmp_vars (tree vars, tree scope)
503 {
504 tree last = vars;
505 if (last)
506 {
507 tree temps;
508
509 /* C99 mode puts the default 'return 0;' for main outside the outer
510 braces. So drill down until we find an actual scope. */
511 while (TREE_CODE (scope) == COMPOUND_EXPR)
512 scope = TREE_OPERAND (scope, 0);
513
514 gcc_assert (TREE_CODE (scope) == BIND_EXPR);
515
516 temps = nreverse (last);
517 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
518 BIND_EXPR_VARS (scope) = temps;
519 }
520 }
521
522 void
523 gimple_add_tmp_var (tree tmp)
524 {
525 gcc_assert (!TREE_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
526
527 DECL_CONTEXT (tmp) = current_function_decl;
528 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
529
530 if (gimplify_ctxp)
531 {
532 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
533 gimplify_ctxp->temps = tmp;
534 }
535 else if (cfun)
536 record_vars (tmp);
537 else
538 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
539 }
540
541 /* Determines whether to assign a locus to the statement STMT. */
542
543 static bool
544 should_carry_locus_p (tree stmt)
545 {
546 /* Don't emit a line note for a label. We particularly don't want to
547 emit one for the break label, since it doesn't actually correspond
548 to the beginning of the loop/switch. */
549 if (TREE_CODE (stmt) == LABEL_EXPR)
550 return false;
551
552 /* Do not annotate empty statements, since it confuses gcov. */
553 if (!TREE_SIDE_EFFECTS (stmt))
554 return false;
555
556 return true;
557 }
558
559 static void
560 annotate_one_with_locus (tree t, location_t locus)
561 {
562 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
563 && ! EXPR_HAS_LOCATION (t)
564 && should_carry_locus_p (t))
565 SET_EXPR_LOCATION (t, locus);
566 }
567
568 void
569 annotate_all_with_locus (tree *stmt_p, location_t locus)
570 {
571 tree_stmt_iterator i;
572
573 if (!*stmt_p)
574 return;
575
576 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
577 {
578 tree t = tsi_stmt (i);
579
580 /* Assuming we've already been gimplified, we shouldn't
581 see nested chaining constructs anymore. */
582 gcc_assert (TREE_CODE (t) != STATEMENT_LIST
583 && TREE_CODE (t) != COMPOUND_EXPR);
584
585 annotate_one_with_locus (t, locus);
586 }
587 }
588
589 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
590 These nodes model computations that should only be done once. If we
591 were to unshare something like SAVE_EXPR(i++), the gimplification
592 process would create wrong code. */
593
594 static tree
595 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
596 {
597 enum tree_code code = TREE_CODE (*tp);
598 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
599 if (TREE_CODE_CLASS (code) == 't'
600 || TREE_CODE_CLASS (code) == 'd'
601 || TREE_CODE_CLASS (code) == 'c'
602 || code == SAVE_EXPR || code == TARGET_EXPR
603 /* We can't do anything sensible with a BLOCK used as an expression,
604 but we also can't abort when we see it because of non-expression
605 uses. So just avert our eyes and cross our fingers. Silly Java. */
606 || code == BLOCK)
607 *walk_subtrees = 0;
608 else
609 {
610 gcc_assert (code != BIND_EXPR);
611 copy_tree_r (tp, walk_subtrees, data);
612 }
613
614 return NULL_TREE;
615 }
616
617 /* Callback for walk_tree to unshare most of the shared trees rooted at
618 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
619 then *TP is deep copied by calling copy_tree_r.
620
621 This unshares the same trees as copy_tree_r with the exception of
622 SAVE_EXPR nodes. These nodes model computations that should only be
623 done once. If we were to unshare something like SAVE_EXPR(i++), the
624 gimplification process would create wrong code. */
625
626 static tree
627 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
628 void *data ATTRIBUTE_UNUSED)
629 {
630 tree t = *tp;
631 enum tree_code code = TREE_CODE (t);
632
633 /* Skip types, decls, and constants. But we do want to look at their
634 types and the bounds of types. Mark them as visited so we properly
635 unmark their subtrees on the unmark pass. If we've already seen them,
636 don't look down further. */
637 if (TREE_CODE_CLASS (code) == 't'
638 || TREE_CODE_CLASS (code) == 'd'
639 || TREE_CODE_CLASS (code) == 'c')
640 {
641 if (TREE_VISITED (t))
642 *walk_subtrees = 0;
643 else
644 TREE_VISITED (t) = 1;
645 }
646
647 /* If this node has been visited already, unshare it and don't look
648 any deeper. */
649 else if (TREE_VISITED (t))
650 {
651 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
652 *walk_subtrees = 0;
653 }
654
655 /* Otherwise, mark the tree as visited and keep looking. */
656 else
657 TREE_VISITED (t) = 1;
658
659 return NULL_TREE;
660 }
661
662 static tree
663 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
664 void *data ATTRIBUTE_UNUSED)
665 {
666 if (TREE_VISITED (*tp))
667 TREE_VISITED (*tp) = 0;
668 else
669 *walk_subtrees = 0;
670
671 return NULL_TREE;
672 }
673
674 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
675 bodies of any nested functions if we are unsharing the entire body of
676 FNDECL. */
677
678 static void
679 unshare_body (tree *body_p, tree fndecl)
680 {
681 struct cgraph_node *cgn = cgraph_node (fndecl);
682
683 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
684 if (body_p == &DECL_SAVED_TREE (fndecl))
685 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
686 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
687 }
688
689 /* Likewise, but mark all trees as not visited. */
690
691 static void
692 unvisit_body (tree *body_p, tree fndecl)
693 {
694 struct cgraph_node *cgn = cgraph_node (fndecl);
695
696 walk_tree (body_p, unmark_visited_r, NULL, NULL);
697 if (body_p == &DECL_SAVED_TREE (fndecl))
698 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
699 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
700 }
701
702 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
703
704 void
705 unshare_all_trees (tree t)
706 {
707 walk_tree (&t, copy_if_shared_r, NULL, NULL);
708 walk_tree (&t, unmark_visited_r, NULL, NULL);
709 }
710
711 /* Unconditionally make an unshared copy of EXPR. This is used when using
712 stored expressions which span multiple functions, such as BINFO_VTABLE,
713 as the normal unsharing process can't tell that they're shared. */
714
715 tree
716 unshare_expr (tree expr)
717 {
718 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
719 return expr;
720 }
721
722 /* A terser interface for building a representation of a exception
723 specification. */
724
725 tree
726 gimple_build_eh_filter (tree body, tree allowed, tree failure)
727 {
728 tree t;
729
730 /* FIXME should the allowed types go in TREE_TYPE? */
731 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
732 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
733
734 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
735 append_to_statement_list (body, &TREE_OPERAND (t, 0));
736
737 return t;
738 }
739
740 \f
741 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
742 contain statements and have a value. Assign its value to a temporary
743 and give it void_type_node. Returns the temporary, or NULL_TREE if
744 WRAPPER was already void. */
745
746 tree
747 voidify_wrapper_expr (tree wrapper, tree temp)
748 {
749 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
750 {
751 tree *p, sub = wrapper;
752
753 restart:
754 /* Set p to point to the body of the wrapper. */
755 switch (TREE_CODE (sub))
756 {
757 case BIND_EXPR:
758 /* For a BIND_EXPR, the body is operand 1. */
759 p = &BIND_EXPR_BODY (sub);
760 break;
761
762 default:
763 p = &TREE_OPERAND (sub, 0);
764 break;
765 }
766
767 /* Advance to the last statement. Set all container types to void. */
768 if (TREE_CODE (*p) == STATEMENT_LIST)
769 {
770 tree_stmt_iterator i = tsi_last (*p);
771 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
772 }
773 else
774 {
775 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
776 {
777 TREE_SIDE_EFFECTS (*p) = 1;
778 TREE_TYPE (*p) = void_type_node;
779 }
780 }
781
782 if (p == NULL || IS_EMPTY_STMT (*p))
783 ;
784 /* Look through exception handling. */
785 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
786 || TREE_CODE (*p) == TRY_CATCH_EXPR)
787 {
788 sub = *p;
789 goto restart;
790 }
791 /* The C++ frontend already did this for us. */
792 else if (TREE_CODE (*p) == INIT_EXPR
793 || TREE_CODE (*p) == TARGET_EXPR)
794 temp = TREE_OPERAND (*p, 0);
795 /* If we're returning a dereference, move the dereference
796 outside the wrapper. */
797 else if (TREE_CODE (*p) == INDIRECT_REF)
798 {
799 tree ptr = TREE_OPERAND (*p, 0);
800 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
801 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
802 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
803 /* If this is a BIND_EXPR for a const inline function, it might not
804 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
805 TREE_SIDE_EFFECTS (wrapper) = 1;
806 }
807 else
808 {
809 if (!temp)
810 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
811 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
812 TREE_SIDE_EFFECTS (wrapper) = 1;
813 }
814
815 TREE_TYPE (wrapper) = void_type_node;
816 return temp;
817 }
818
819 return NULL_TREE;
820 }
821
822 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
823 a temporary through which they communicate. */
824
825 static void
826 build_stack_save_restore (tree *save, tree *restore)
827 {
828 tree save_call, tmp_var;
829
830 save_call =
831 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
832 NULL_TREE);
833 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
834
835 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
836 *restore =
837 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
838 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
839 }
840
841 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
842
843 static enum gimplify_status
844 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
845 {
846 tree bind_expr = *expr_p;
847 bool old_save_stack = gimplify_ctxp->save_stack;
848 tree t;
849
850 temp = voidify_wrapper_expr (bind_expr, temp);
851
852 /* Mark variables seen in this bind expr. */
853 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
854 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
855
856 gimple_push_bind_expr (bind_expr);
857 gimplify_ctxp->save_stack = false;
858
859 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
860
861 if (gimplify_ctxp->save_stack)
862 {
863 tree stack_save, stack_restore;
864
865 /* Save stack on entry and restore it on exit. Add a try_finally
866 block to achieve this. Note that mudflap depends on the
867 format of the emitted code: see mx_register_decls(). */
868 build_stack_save_restore (&stack_save, &stack_restore);
869
870 t = build (TRY_FINALLY_EXPR, void_type_node,
871 BIND_EXPR_BODY (bind_expr), NULL_TREE);
872 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
873
874 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
875 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
876 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
877 }
878
879 gimplify_ctxp->save_stack = old_save_stack;
880 gimple_pop_bind_expr ();
881
882 if (temp)
883 {
884 *expr_p = temp;
885 append_to_statement_list (bind_expr, pre_p);
886 return GS_OK;
887 }
888 else
889 return GS_ALL_DONE;
890 }
891
892 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
893 GIMPLE value, it is assigned to a new temporary and the statement is
894 re-written to return the temporary.
895
896 PRE_P points to the list where side effects that must happen before
897 STMT should be stored. */
898
899 static enum gimplify_status
900 gimplify_return_expr (tree stmt, tree *pre_p)
901 {
902 tree ret_expr = TREE_OPERAND (stmt, 0);
903 tree result_decl, result;
904
905 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
906 || ret_expr == error_mark_node)
907 return GS_ALL_DONE;
908
909 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
910 result_decl = NULL_TREE;
911 else
912 {
913 result_decl = TREE_OPERAND (ret_expr, 0);
914 if (TREE_CODE (result_decl) == INDIRECT_REF)
915 /* See through a return by reference. */
916 result_decl = TREE_OPERAND (result_decl, 0);
917
918 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
919 || TREE_CODE (ret_expr) == INIT_EXPR)
920 && TREE_CODE (result_decl) == RESULT_DECL);
921 }
922
923 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
924 Recall that aggregate_value_p is FALSE for any aggregate type that is
925 returned in registers. If we're returning values in registers, then
926 we don't want to extend the lifetime of the RESULT_DECL, particularly
927 across another call. In addition, for those aggregates for which
928 hard_function_value generates a PARALLEL, we'll abort during normal
929 expansion of structure assignments; there's special code in expand_return
930 to handle this case that does not exist in expand_expr. */
931 if (!result_decl
932 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
933 result = result_decl;
934 else if (gimplify_ctxp->return_temp)
935 result = gimplify_ctxp->return_temp;
936 else
937 {
938 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
939
940 /* ??? With complex control flow (usually involving abnormal edges),
941 we can wind up warning about an uninitialized value for this. Due
942 to how this variable is constructed and initialized, this is never
943 true. Give up and never warn. */
944 TREE_NO_WARNING (result) = 1;
945
946 gimplify_ctxp->return_temp = result;
947 }
948
949 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
950 Then gimplify the whole thing. */
951 if (result != result_decl)
952 TREE_OPERAND (ret_expr, 0) = result;
953
954 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
955
956 /* If we didn't use a temporary, then the result is just the result_decl.
957 Otherwise we need a simple copy. This should already be gimple. */
958 if (result == result_decl)
959 ret_expr = result;
960 else
961 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
962 TREE_OPERAND (stmt, 0) = ret_expr;
963
964 return GS_ALL_DONE;
965 }
966
967 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
968 and initialization explicit. */
969
970 static enum gimplify_status
971 gimplify_decl_expr (tree *stmt_p)
972 {
973 tree stmt = *stmt_p;
974 tree decl = DECL_EXPR_DECL (stmt);
975
976 *stmt_p = NULL_TREE;
977
978 if (TREE_TYPE (decl) == error_mark_node)
979 return GS_ERROR;
980
981 else if (TREE_CODE (decl) == TYPE_DECL)
982 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
983
984 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
985 {
986 tree init = DECL_INITIAL (decl);
987
988 if (!TREE_CONSTANT (DECL_SIZE (decl)))
989 {
990 /* This is a variable-sized decl. Simplify its size and mark it
991 for deferred expansion. Note that mudflap depends on the format
992 of the emitted code: see mx_register_decls(). */
993 tree t, args, addr, ptr_type;
994
995 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
996 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
997 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
998
999 /* All occurrences of this decl in final gimplified code will be
1000 replaced by indirection. Setting DECL_VALUE_EXPR does two
1001 things: First, it lets the rest of the gimplifier know what
1002 replacement to use. Second, it lets the debug info know
1003 where to find the value. */
1004 ptr_type = build_pointer_type (TREE_TYPE (decl));
1005 addr = create_tmp_var (ptr_type, get_name (decl));
1006 DECL_IGNORED_P (addr) = 0;
1007 t = build_fold_indirect_ref (addr);
1008 DECL_VALUE_EXPR (decl) = t;
1009
1010 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
1011 t = built_in_decls[BUILT_IN_ALLOCA];
1012 t = build_function_call_expr (t, args);
1013 t = fold_convert (ptr_type, t);
1014 t = build2 (MODIFY_EXPR, void_type_node, addr, t);
1015
1016 gimplify_and_add (t, stmt_p);
1017
1018 /* Indicate that we need to restore the stack level when the
1019 enclosing BIND_EXPR is exited. */
1020 gimplify_ctxp->save_stack = true;
1021 }
1022
1023 if (init && init != error_mark_node)
1024 {
1025 if (!TREE_STATIC (decl))
1026 {
1027 DECL_INITIAL (decl) = NULL_TREE;
1028 init = build (MODIFY_EXPR, void_type_node, decl, init);
1029 gimplify_and_add (init, stmt_p);
1030 }
1031 else
1032 /* We must still examine initializers for static variables
1033 as they may contain a label address. */
1034 walk_tree (&init, force_labels_r, NULL, NULL);
1035 }
1036
1037 /* This decl isn't mentioned in the enclosing block, so add it to the
1038 list of temps. FIXME it seems a bit of a kludge to say that
1039 anonymous artificial vars aren't pushed, but everything else is. */
1040 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1041 gimple_add_tmp_var (decl);
1042 }
1043
1044 return GS_ALL_DONE;
1045 }
1046
1047 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1048 and replacing the LOOP_EXPR with goto, but if the loop contains an
1049 EXIT_EXPR, we need to append a label for it to jump to. */
1050
1051 static enum gimplify_status
1052 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1053 {
1054 tree saved_label = gimplify_ctxp->exit_label;
1055 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1056 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1057
1058 append_to_statement_list (start_label, pre_p);
1059
1060 gimplify_ctxp->exit_label = NULL_TREE;
1061
1062 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1063
1064 if (gimplify_ctxp->exit_label)
1065 {
1066 append_to_statement_list (jump_stmt, pre_p);
1067 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1068 }
1069 else
1070 *expr_p = jump_stmt;
1071
1072 gimplify_ctxp->exit_label = saved_label;
1073
1074 return GS_ALL_DONE;
1075 }
1076
1077 /* Compare two case labels. Because the front end should already have
1078 made sure that case ranges do not overlap, it is enough to only compare
1079 the CASE_LOW values of each case label. */
1080
1081 static int
1082 compare_case_labels (const void *p1, const void *p2)
1083 {
1084 tree case1 = *(tree *)p1;
1085 tree case2 = *(tree *)p2;
1086
1087 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1088 }
1089
1090 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1091
1092 void
1093 sort_case_labels (tree label_vec)
1094 {
1095 size_t len = TREE_VEC_LENGTH (label_vec);
1096 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1097
1098 if (CASE_LOW (default_case))
1099 {
1100 size_t i;
1101
1102 /* The last label in the vector should be the default case
1103 but it is not. */
1104 for (i = 0; i < len; ++i)
1105 {
1106 tree t = TREE_VEC_ELT (label_vec, i);
1107 if (!CASE_LOW (t))
1108 {
1109 default_case = t;
1110 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1111 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1112 break;
1113 }
1114 }
1115 }
1116
1117 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1118 compare_case_labels);
1119 }
1120
1121 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1122 branch to. */
1123
1124 static enum gimplify_status
1125 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1126 {
1127 tree switch_expr = *expr_p;
1128 enum gimplify_status ret;
1129
1130 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1131 is_gimple_val, fb_rvalue);
1132
1133 if (SWITCH_BODY (switch_expr))
1134 {
1135 varray_type labels, saved_labels;
1136 tree label_vec, default_case = NULL_TREE;
1137 size_t i, len;
1138
1139 /* If someone can be bothered to fill in the labels, they can
1140 be bothered to null out the body too. */
1141 gcc_assert (!SWITCH_LABELS (switch_expr));
1142
1143 saved_labels = gimplify_ctxp->case_labels;
1144 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1145
1146 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1147
1148 labels = gimplify_ctxp->case_labels;
1149 gimplify_ctxp->case_labels = saved_labels;
1150
1151 len = VARRAY_ACTIVE_SIZE (labels);
1152
1153 for (i = 0; i < len; ++i)
1154 {
1155 tree t = VARRAY_TREE (labels, i);
1156 if (!CASE_LOW (t))
1157 {
1158 /* The default case must be the last label in the list. */
1159 default_case = t;
1160 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1161 len--;
1162 break;
1163 }
1164 }
1165
1166 label_vec = make_tree_vec (len + 1);
1167 SWITCH_LABELS (*expr_p) = label_vec;
1168 append_to_statement_list (switch_expr, pre_p);
1169
1170 if (! default_case)
1171 {
1172 /* If the switch has no default label, add one, so that we jump
1173 around the switch body. */
1174 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1175 NULL_TREE, create_artificial_label ());
1176 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1177 *expr_p = build (LABEL_EXPR, void_type_node,
1178 CASE_LABEL (default_case));
1179 }
1180 else
1181 *expr_p = SWITCH_BODY (switch_expr);
1182
1183 for (i = 0; i < len; ++i)
1184 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1185 TREE_VEC_ELT (label_vec, len) = default_case;
1186
1187 sort_case_labels (label_vec);
1188
1189 SWITCH_BODY (switch_expr) = NULL;
1190 }
1191 else
1192 gcc_assert (SWITCH_LABELS (switch_expr));
1193
1194 return ret;
1195 }
1196
1197 static enum gimplify_status
1198 gimplify_case_label_expr (tree *expr_p)
1199 {
1200 tree expr = *expr_p;
1201
1202 gcc_assert (gimplify_ctxp->case_labels);
1203 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1204 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1205 return GS_ALL_DONE;
1206 }
1207
1208 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1209 a (possibly empty) body. */
1210
1211 static enum gimplify_status
1212 gimplify_labeled_block_expr (tree *expr_p)
1213 {
1214 tree body = LABELED_BLOCK_BODY (*expr_p);
1215 tree label = LABELED_BLOCK_LABEL (*expr_p);
1216 tree t;
1217
1218 DECL_CONTEXT (label) = current_function_decl;
1219 t = build (LABEL_EXPR, void_type_node, label);
1220 if (body != NULL_TREE)
1221 t = build (COMPOUND_EXPR, void_type_node, body, t);
1222 *expr_p = t;
1223
1224 return GS_OK;
1225 }
1226
1227 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1228
1229 static enum gimplify_status
1230 gimplify_exit_block_expr (tree *expr_p)
1231 {
1232 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1233 tree label;
1234
1235 /* First operand must be a LABELED_BLOCK_EXPR, which should
1236 already be lowered (or partially lowered) when we get here. */
1237 gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
1238
1239 label = LABELED_BLOCK_LABEL (labeled_block);
1240 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1241
1242 return GS_OK;
1243 }
1244
1245 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1246 if necessary. */
1247
1248 tree
1249 build_and_jump (tree *label_p)
1250 {
1251 if (label_p == NULL)
1252 /* If there's nowhere to jump, just fall through. */
1253 return NULL_TREE;
1254
1255 if (*label_p == NULL_TREE)
1256 {
1257 tree label = create_artificial_label ();
1258 *label_p = label;
1259 }
1260
1261 return build1 (GOTO_EXPR, void_type_node, *label_p);
1262 }
1263
1264 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1265 This also involves building a label to jump to and communicating it to
1266 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1267
1268 static enum gimplify_status
1269 gimplify_exit_expr (tree *expr_p)
1270 {
1271 tree cond = TREE_OPERAND (*expr_p, 0);
1272 tree expr;
1273
1274 expr = build_and_jump (&gimplify_ctxp->exit_label);
1275 expr = build (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1276 *expr_p = expr;
1277
1278 return GS_OK;
1279 }
1280
1281 /* A helper function to be called via walk_tree. Mark all labels under *TP
1282 as being forced. To be called for DECL_INITIAL of static variables. */
1283
1284 tree
1285 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1286 {
1287 if (TYPE_P (*tp))
1288 *walk_subtrees = 0;
1289 if (TREE_CODE (*tp) == LABEL_DECL)
1290 FORCED_LABEL (*tp) = 1;
1291
1292 return NULL_TREE;
1293 }
1294
1295 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1296 different from its canonical type, wrap the whole thing inside a
1297 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1298 type.
1299
1300 The canonical type of a COMPONENT_REF is the type of the field being
1301 referenced--unless the field is a bit-field which can be read directly
1302 in a smaller mode, in which case the canonical type is the
1303 sign-appropriate type corresponding to that mode. */
1304
1305 static void
1306 canonicalize_component_ref (tree *expr_p)
1307 {
1308 tree expr = *expr_p;
1309 tree type;
1310
1311 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1312
1313 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1314 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1315 else
1316 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1317
1318 if (TREE_TYPE (expr) != type)
1319 {
1320 tree old_type = TREE_TYPE (expr);
1321
1322 /* Set the type of the COMPONENT_REF to the underlying type. */
1323 TREE_TYPE (expr) = type;
1324
1325 /* And wrap the whole thing inside a NOP_EXPR. */
1326 expr = build1 (NOP_EXPR, old_type, expr);
1327
1328 *expr_p = expr;
1329 }
1330 }
1331
1332 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1333 to foo, embed that change in the ADDR_EXPR by converting
1334 T array[U];
1335 (T *)&array
1336 ==>
1337 &array[L]
1338 where L is the lower bound. For simplicity, only do this for constant
1339 lower bound. */
1340
1341 static void
1342 canonicalize_addr_expr (tree *expr_p)
1343 {
1344 tree expr = *expr_p;
1345 tree ctype = TREE_TYPE (expr);
1346 tree addr_expr = TREE_OPERAND (expr, 0);
1347 tree atype = TREE_TYPE (addr_expr);
1348 tree dctype, datype, ddatype, otype, obj_expr;
1349
1350 /* Both cast and addr_expr types should be pointers. */
1351 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1352 return;
1353
1354 /* The addr_expr type should be a pointer to an array. */
1355 datype = TREE_TYPE (atype);
1356 if (TREE_CODE (datype) != ARRAY_TYPE)
1357 return;
1358
1359 /* Both cast and addr_expr types should address the same object type. */
1360 dctype = TREE_TYPE (ctype);
1361 ddatype = TREE_TYPE (datype);
1362 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1363 return;
1364
1365 /* The addr_expr and the object type should match. */
1366 obj_expr = TREE_OPERAND (addr_expr, 0);
1367 otype = TREE_TYPE (obj_expr);
1368 if (!lang_hooks.types_compatible_p (otype, datype))
1369 return;
1370
1371 /* The lower bound and element sizes must be constant. */
1372 if (TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1373 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1374 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1375 return;
1376
1377 /* All checks succeeded. Build a new node to merge the cast. */
1378 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1379 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1380 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1381 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1382 size_int (TYPE_ALIGN_UNIT (dctype))));
1383 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1384 }
1385
1386 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1387 underneath as appropriate. */
1388
1389 static enum gimplify_status
1390 gimplify_conversion (tree *expr_p)
1391 {
1392 /* If we still have a conversion at the toplevel, then strip
1393 away all but the outermost conversion. */
1394 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1395 {
1396 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1397
1398 /* And remove the outermost conversion if it's useless. */
1399 if (tree_ssa_useless_type_conversion (*expr_p))
1400 *expr_p = TREE_OPERAND (*expr_p, 0);
1401 }
1402
1403 /* If we still have a conversion at the toplevel,
1404 then canonicalize some constructs. */
1405 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1406 {
1407 tree sub = TREE_OPERAND (*expr_p, 0);
1408
1409 /* If a NOP conversion is changing the type of a COMPONENT_REF
1410 expression, then canonicalize its type now in order to expose more
1411 redundant conversions. */
1412 if (TREE_CODE (sub) == COMPONENT_REF)
1413 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1414
1415 /* If a NOP conversion is changing a pointer to array of foo
1416 to a pointer to foo, embed that change in the ADDR_EXPR. */
1417 else if (TREE_CODE (sub) == ADDR_EXPR)
1418 canonicalize_addr_expr (expr_p);
1419 }
1420
1421 return GS_OK;
1422 }
1423
1424 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1425 node pointed by EXPR_P.
1426
1427 compound_lval
1428 : min_lval '[' val ']'
1429 | min_lval '.' ID
1430 | compound_lval '[' val ']'
1431 | compound_lval '.' ID
1432
1433 This is not part of the original SIMPLE definition, which separates
1434 array and member references, but it seems reasonable to handle them
1435 together. Also, this way we don't run into problems with union
1436 aliasing; gcc requires that for accesses through a union to alias, the
1437 union reference must be explicit, which was not always the case when we
1438 were splitting up array and member refs.
1439
1440 PRE_P points to the list where side effects that must happen before
1441 *EXPR_P should be stored.
1442
1443 POST_P points to the list where side effects that must happen after
1444 *EXPR_P should be stored. */
1445
1446 static enum gimplify_status
1447 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1448 tree *post_p, fallback_t fallback)
1449 {
1450 tree *p;
1451 varray_type stack;
1452 enum gimplify_status ret = GS_OK, tret;
1453 int i;
1454
1455 /* Create a stack of the subexpressions so later we can walk them in
1456 order from inner to outer.
1457
1458 This array is very memory consuming. Don't even think of making
1459 it VARRAY_TREE. */
1460 VARRAY_GENERIC_PTR_NOGC_INIT (stack, 10, "stack");
1461
1462 /* We can either handle REALPART_EXPR, IMAGEPART_EXPR anything that
1463 handled_components can deal with. */
1464 for (p = expr_p;
1465 (handled_component_p (*p)
1466 || TREE_CODE (*p) == REALPART_EXPR || TREE_CODE (*p) == IMAGPART_EXPR);
1467 p = &TREE_OPERAND (*p, 0))
1468 VARRAY_PUSH_GENERIC_PTR_NOGC (stack, *p);
1469
1470 gcc_assert (VARRAY_ACTIVE_SIZE (stack));
1471
1472 /* Now STACK is a stack of pointers to all the refs we've walked through
1473 and P points to the innermost expression.
1474
1475 Java requires that we elaborated nodes in source order. That
1476 means we must gimplify the inner expression followed by each of
1477 the indices, in order. But we can't gimplify the inner
1478 expression until we deal with any variable bounds, sizes, or
1479 positions in order to deal with PLACEHOLDER_EXPRs.
1480
1481 So we do this in three steps. First we deal with the annotations
1482 for any variables in the components, then we gimplify the base,
1483 then we gimplify any indices, from left to right. */
1484 for (i = VARRAY_ACTIVE_SIZE (stack) - 1; i >= 0; i--)
1485 {
1486 tree t = VARRAY_GENERIC_PTR_NOGC (stack, i);
1487
1488 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1489 {
1490 /* Gimplify the low bound and element type size and put them into
1491 the ARRAY_REF. If these values are set, they have already been
1492 gimplified. */
1493 if (!TREE_OPERAND (t, 2))
1494 {
1495 tree low = unshare_expr (array_ref_low_bound (t));
1496 if (!is_gimple_min_invariant (low))
1497 {
1498 TREE_OPERAND (t, 2) = low;
1499 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1500 is_gimple_formal_tmp_reg, fb_rvalue);
1501 ret = MIN (ret, tret);
1502 }
1503 }
1504
1505 if (!TREE_OPERAND (t, 3))
1506 {
1507 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1508 tree elmt_size = unshare_expr (array_ref_element_size (t));
1509 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1510
1511 /* Divide the element size by the alignment of the element
1512 type (above). */
1513 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1514
1515 if (!is_gimple_min_invariant (elmt_size))
1516 {
1517 TREE_OPERAND (t, 3) = elmt_size;
1518 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1519 is_gimple_formal_tmp_reg, fb_rvalue);
1520 ret = MIN (ret, tret);
1521 }
1522 }
1523 }
1524 else if (TREE_CODE (t) == COMPONENT_REF)
1525 {
1526 /* Set the field offset into T and gimplify it. */
1527 if (!TREE_OPERAND (t, 2))
1528 {
1529 tree offset = unshare_expr (component_ref_field_offset (t));
1530 tree field = TREE_OPERAND (t, 1);
1531 tree factor
1532 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1533
1534 /* Divide the offset by its alignment. */
1535 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1536
1537 if (!is_gimple_min_invariant (offset))
1538 {
1539 TREE_OPERAND (t, 2) = offset;
1540 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1541 is_gimple_formal_tmp_reg, fb_rvalue);
1542 ret = MIN (ret, tret);
1543 }
1544 }
1545 }
1546 }
1547
1548 /* Step 2 is to gimplify the base expression. */
1549 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1550 ret = MIN (ret, tret);
1551
1552 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1553 loop we also remove any useless conversions. */
1554 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1555 {
1556 tree t = VARRAY_TOP_TREE (stack);
1557
1558 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1559 {
1560 /* Gimplify the dimension.
1561 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1562 Gimplify non-constant array indices into a temporary
1563 variable.
1564 FIXME - The real fix is to gimplify post-modify
1565 expressions into a minimal gimple lvalue. However, that
1566 exposes bugs in alias analysis. The alias analyzer does
1567 not handle &PTR->FIELD very well. Will fix after the
1568 branch is merged into mainline (dnovillo 2004-05-03). */
1569 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1570 {
1571 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1572 is_gimple_formal_tmp_reg, fb_rvalue);
1573 ret = MIN (ret, tret);
1574 }
1575 }
1576 else if (TREE_CODE (t) == BIT_FIELD_REF)
1577 {
1578 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1579 is_gimple_val, fb_rvalue);
1580 ret = MIN (ret, tret);
1581 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1582 is_gimple_val, fb_rvalue);
1583 ret = MIN (ret, tret);
1584 }
1585
1586 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1587
1588 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1589 set which would have caused all the outer expressions in EXPR_P
1590 leading to P to also have had TREE_SIDE_EFFECTS set. */
1591 recalculate_side_effects (t);
1592 VARRAY_POP (stack);
1593 }
1594
1595 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1596 ret = MIN (ret, tret);
1597
1598 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1599 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1600 {
1601 canonicalize_component_ref (expr_p);
1602 ret = MIN (ret, GS_OK);
1603 }
1604
1605 VARRAY_FREE (stack);
1606
1607 return ret;
1608 }
1609
1610 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1611
1612 PRE_P points to the list where side effects that must happen before
1613 *EXPR_P should be stored.
1614
1615 POST_P points to the list where side effects that must happen after
1616 *EXPR_P should be stored.
1617
1618 WANT_VALUE is nonzero iff we want to use the value of this expression
1619 in another expression. */
1620
1621 static enum gimplify_status
1622 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1623 bool want_value)
1624 {
1625 enum tree_code code;
1626 tree lhs, lvalue, rhs, t1;
1627 bool postfix;
1628 enum tree_code arith_code;
1629 enum gimplify_status ret;
1630
1631 code = TREE_CODE (*expr_p);
1632
1633 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
1634 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
1635
1636 /* Prefix or postfix? */
1637 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1638 /* Faster to treat as prefix if result is not used. */
1639 postfix = want_value;
1640 else
1641 postfix = false;
1642
1643 /* Add or subtract? */
1644 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1645 arith_code = PLUS_EXPR;
1646 else
1647 arith_code = MINUS_EXPR;
1648
1649 /* Gimplify the LHS into a GIMPLE lvalue. */
1650 lvalue = TREE_OPERAND (*expr_p, 0);
1651 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1652 if (ret == GS_ERROR)
1653 return ret;
1654
1655 /* Extract the operands to the arithmetic operation. */
1656 lhs = lvalue;
1657 rhs = TREE_OPERAND (*expr_p, 1);
1658
1659 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1660 that as the result value and in the postqueue operation. */
1661 if (postfix)
1662 {
1663 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1664 if (ret == GS_ERROR)
1665 return ret;
1666 }
1667
1668 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1669 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1670
1671 if (postfix)
1672 {
1673 gimplify_and_add (t1, post_p);
1674 *expr_p = lhs;
1675 return GS_ALL_DONE;
1676 }
1677 else
1678 {
1679 *expr_p = t1;
1680 return GS_OK;
1681 }
1682 }
1683
1684 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
1685
1686 static void
1687 maybe_with_size_expr (tree *expr_p)
1688 {
1689 tree expr = *expr_p;
1690 tree type = TREE_TYPE (expr);
1691 tree size;
1692
1693 /* If we've already wrapped this or the type is error_mark_node, we can't do
1694 anything. */
1695 if (TREE_CODE (expr) == WITH_SIZE_EXPR
1696 || type == error_mark_node)
1697 return;
1698
1699 /* If the size isn't known or is a constant, we have nothing to do. */
1700 size = TYPE_SIZE_UNIT (type);
1701 if (!size || TREE_CODE (size) == INTEGER_CST)
1702 return;
1703
1704 /* Otherwise, make a WITH_SIZE_EXPR. */
1705 size = unshare_expr (size);
1706 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
1707 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
1708 }
1709
1710 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
1711
1712 static enum gimplify_status
1713 gimplify_arg (tree *expr_p, tree *pre_p)
1714 {
1715 bool (*test) (tree);
1716 fallback_t fb;
1717
1718 /* In general, we allow lvalues for function arguments to avoid
1719 extra overhead of copying large aggregates out of even larger
1720 aggregates into temporaries only to copy the temporaries to
1721 the argument list. Make optimizers happy by pulling out to
1722 temporaries those types that fit in registers. */
1723 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
1724 test = is_gimple_val, fb = fb_rvalue;
1725 else
1726 test = is_gimple_lvalue, fb = fb_either;
1727
1728 /* If this is a variable sized type, we must remember the size. */
1729 maybe_with_size_expr (expr_p);
1730
1731 /* There is a sequence point before a function call. Side effects in
1732 the argument list must occur before the actual call. So, when
1733 gimplifying arguments, force gimplify_expr to use an internal
1734 post queue which is then appended to the end of PRE_P. */
1735 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
1736 }
1737
1738 /* Gimplify the CALL_EXPR node pointed by EXPR_P. PRE_P points to the
1739 list where side effects that must happen before *EXPR_P should be stored.
1740 WANT_VALUE is true if the result of the call is desired. */
1741
1742 static enum gimplify_status
1743 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
1744 {
1745 tree decl;
1746 tree arglist;
1747 enum gimplify_status ret;
1748
1749 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
1750
1751 /* For reliable diagnostics during inlining, it is necessary that
1752 every call_expr be annotated with file and line. */
1753 if (! EXPR_HAS_LOCATION (*expr_p))
1754 SET_EXPR_LOCATION (*expr_p, input_location);
1755
1756 /* This may be a call to a builtin function.
1757
1758 Builtin function calls may be transformed into different
1759 (and more efficient) builtin function calls under certain
1760 circumstances. Unfortunately, gimplification can muck things
1761 up enough that the builtin expanders are not aware that certain
1762 transformations are still valid.
1763
1764 So we attempt transformation/gimplification of the call before
1765 we gimplify the CALL_EXPR. At this time we do not manage to
1766 transform all calls in the same manner as the expanders do, but
1767 we do transform most of them. */
1768 decl = get_callee_fndecl (*expr_p);
1769 if (decl && DECL_BUILT_IN (decl))
1770 {
1771 tree new = simplify_builtin (*expr_p, !want_value);
1772
1773 if (new && new != *expr_p)
1774 {
1775 /* There was a transformation of this call which computes the
1776 same value, but in a more efficient way. Return and try
1777 again. */
1778 *expr_p = new;
1779 return GS_OK;
1780 }
1781
1782 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
1783 /* Avoid gimplifying the second argument to va_start, which needs
1784 to be the plain PARM_DECL. */
1785 return gimplify_arg (&TREE_VALUE (TREE_OPERAND (*expr_p, 1)), pre_p);
1786 }
1787
1788 /* There is a sequence point before the call, so any side effects in
1789 the calling expression must occur before the actual call. Force
1790 gimplify_expr to use an internal post queue. */
1791 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1792 is_gimple_call_addr, fb_rvalue);
1793
1794 if (PUSH_ARGS_REVERSED)
1795 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1796 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1797 arglist = TREE_CHAIN (arglist))
1798 {
1799 enum gimplify_status t;
1800
1801 t = gimplify_arg (&TREE_VALUE (arglist), pre_p);
1802
1803 if (t == GS_ERROR)
1804 ret = GS_ERROR;
1805 }
1806 if (PUSH_ARGS_REVERSED)
1807 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1808
1809 /* Try this again in case gimplification exposed something. */
1810 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
1811 {
1812 tree new = simplify_builtin (*expr_p, !want_value);
1813
1814 if (new && new != *expr_p)
1815 {
1816 /* There was a transformation of this call which computes the
1817 same value, but in a more efficient way. Return and try
1818 again. */
1819 *expr_p = new;
1820 return GS_OK;
1821 }
1822 }
1823
1824 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1825 decl. This allows us to eliminate redundant or useless
1826 calls to "const" functions. */
1827 if (TREE_CODE (*expr_p) == CALL_EXPR
1828 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
1829 TREE_SIDE_EFFECTS (*expr_p) = 0;
1830
1831 return ret;
1832 }
1833
1834 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
1835 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
1836
1837 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
1838 condition is true or false, respectively. If null, we should generate
1839 our own to skip over the evaluation of this specific expression.
1840
1841 This function is the tree equivalent of do_jump.
1842
1843 shortcut_cond_r should only be called by shortcut_cond_expr. */
1844
1845 static tree
1846 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
1847 {
1848 tree local_label = NULL_TREE;
1849 tree t, expr = NULL;
1850
1851 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
1852 retain the shortcut semantics. Just insert the gotos here;
1853 shortcut_cond_expr will append the real blocks later. */
1854 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
1855 {
1856 /* Turn if (a && b) into
1857
1858 if (a); else goto no;
1859 if (b) goto yes; else goto no;
1860 (no:) */
1861
1862 if (false_label_p == NULL)
1863 false_label_p = &local_label;
1864
1865 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
1866 append_to_statement_list (t, &expr);
1867
1868 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1869 false_label_p);
1870 append_to_statement_list (t, &expr);
1871 }
1872 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
1873 {
1874 /* Turn if (a || b) into
1875
1876 if (a) goto yes;
1877 if (b) goto yes; else goto no;
1878 (yes:) */
1879
1880 if (true_label_p == NULL)
1881 true_label_p = &local_label;
1882
1883 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
1884 append_to_statement_list (t, &expr);
1885
1886 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1887 false_label_p);
1888 append_to_statement_list (t, &expr);
1889 }
1890 else if (TREE_CODE (pred) == COND_EXPR)
1891 {
1892 /* As long as we're messing with gotos, turn if (a ? b : c) into
1893 if (a)
1894 if (b) goto yes; else goto no;
1895 else
1896 if (c) goto yes; else goto no; */
1897 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
1898 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1899 false_label_p),
1900 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
1901 false_label_p));
1902 }
1903 else
1904 {
1905 expr = build (COND_EXPR, void_type_node, pred,
1906 build_and_jump (true_label_p),
1907 build_and_jump (false_label_p));
1908 }
1909
1910 if (local_label)
1911 {
1912 t = build1 (LABEL_EXPR, void_type_node, local_label);
1913 append_to_statement_list (t, &expr);
1914 }
1915
1916 return expr;
1917 }
1918
1919 static tree
1920 shortcut_cond_expr (tree expr)
1921 {
1922 tree pred = TREE_OPERAND (expr, 0);
1923 tree then_ = TREE_OPERAND (expr, 1);
1924 tree else_ = TREE_OPERAND (expr, 2);
1925 tree true_label, false_label, end_label, t;
1926 tree *true_label_p;
1927 tree *false_label_p;
1928 bool emit_end, emit_false;
1929 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
1930 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
1931
1932 /* First do simple transformations. */
1933 if (!else_se)
1934 {
1935 /* If there is no 'else', turn (a && b) into if (a) if (b). */
1936 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
1937 {
1938 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
1939 then_ = shortcut_cond_expr (expr);
1940 pred = TREE_OPERAND (pred, 0);
1941 expr = build (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
1942 }
1943 }
1944 if (!then_se)
1945 {
1946 /* If there is no 'then', turn
1947 if (a || b); else d
1948 into
1949 if (a); else if (b); else d. */
1950 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
1951 {
1952 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
1953 else_ = shortcut_cond_expr (expr);
1954 pred = TREE_OPERAND (pred, 0);
1955 expr = build (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
1956 }
1957 }
1958
1959 /* If we're done, great. */
1960 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
1961 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
1962 return expr;
1963
1964 /* Otherwise we need to mess with gotos. Change
1965 if (a) c; else d;
1966 to
1967 if (a); else goto no;
1968 c; goto end;
1969 no: d; end:
1970 and recursively gimplify the condition. */
1971
1972 true_label = false_label = end_label = NULL_TREE;
1973
1974 /* If our arms just jump somewhere, hijack those labels so we don't
1975 generate jumps to jumps. */
1976
1977 if (then_
1978 && TREE_CODE (then_) == GOTO_EXPR
1979 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
1980 {
1981 true_label = GOTO_DESTINATION (then_);
1982 then_ = NULL;
1983 then_se = false;
1984 }
1985
1986 if (else_
1987 && TREE_CODE (else_) == GOTO_EXPR
1988 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
1989 {
1990 false_label = GOTO_DESTINATION (else_);
1991 else_ = NULL;
1992 else_se = false;
1993 }
1994
1995 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
1996 if (true_label)
1997 true_label_p = &true_label;
1998 else
1999 true_label_p = NULL;
2000
2001 /* The 'else' branch also needs a label if it contains interesting code. */
2002 if (false_label || else_se)
2003 false_label_p = &false_label;
2004 else
2005 false_label_p = NULL;
2006
2007 /* If there was nothing else in our arms, just forward the label(s). */
2008 if (!then_se && !else_se)
2009 return shortcut_cond_r (pred, true_label_p, false_label_p);
2010
2011 /* If our last subexpression already has a terminal label, reuse it. */
2012 if (else_se)
2013 expr = expr_last (else_);
2014 else if (then_se)
2015 expr = expr_last (then_);
2016 else
2017 expr = NULL;
2018 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2019 end_label = LABEL_EXPR_LABEL (expr);
2020
2021 /* If we don't care about jumping to the 'else' branch, jump to the end
2022 if the condition is false. */
2023 if (!false_label_p)
2024 false_label_p = &end_label;
2025
2026 /* We only want to emit these labels if we aren't hijacking them. */
2027 emit_end = (end_label == NULL_TREE);
2028 emit_false = (false_label == NULL_TREE);
2029
2030 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2031
2032 expr = NULL;
2033 append_to_statement_list (pred, &expr);
2034
2035 append_to_statement_list (then_, &expr);
2036 if (else_se)
2037 {
2038 t = build_and_jump (&end_label);
2039 append_to_statement_list (t, &expr);
2040 if (emit_false)
2041 {
2042 t = build1 (LABEL_EXPR, void_type_node, false_label);
2043 append_to_statement_list (t, &expr);
2044 }
2045 append_to_statement_list (else_, &expr);
2046 }
2047 if (emit_end && end_label)
2048 {
2049 t = build1 (LABEL_EXPR, void_type_node, end_label);
2050 append_to_statement_list (t, &expr);
2051 }
2052
2053 return expr;
2054 }
2055
2056 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2057
2058 static tree
2059 gimple_boolify (tree expr)
2060 {
2061 tree type = TREE_TYPE (expr);
2062
2063 if (TREE_CODE (type) == BOOLEAN_TYPE)
2064 return expr;
2065
2066 /* If this is the predicate of a COND_EXPR, it might not even be a
2067 truthvalue yet. */
2068 expr = lang_hooks.truthvalue_conversion (expr);
2069
2070 switch (TREE_CODE (expr))
2071 {
2072 case TRUTH_AND_EXPR:
2073 case TRUTH_OR_EXPR:
2074 case TRUTH_XOR_EXPR:
2075 case TRUTH_ANDIF_EXPR:
2076 case TRUTH_ORIF_EXPR:
2077 /* Also boolify the arguments of truth exprs. */
2078 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2079 /* FALLTHRU */
2080
2081 case TRUTH_NOT_EXPR:
2082 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2083 /* FALLTHRU */
2084
2085 case EQ_EXPR: case NE_EXPR:
2086 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2087 /* These expressions always produce boolean results. */
2088 TREE_TYPE (expr) = boolean_type_node;
2089 return expr;
2090
2091 default:
2092 /* Other expressions that get here must have boolean values, but
2093 might need to be converted to the appropriate mode. */
2094 return convert (boolean_type_node, expr);
2095 }
2096 }
2097
2098 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2099 into
2100
2101 if (p) if (p)
2102 t1 = a; a;
2103 else or else
2104 t1 = b; b;
2105 t1;
2106
2107 The second form is used when *EXPR_P is of type void.
2108
2109 TARGET is the tree for T1 above.
2110
2111 PRE_P points to the list where side effects that must happen before
2112 *EXPR_P should be stored. */
2113
2114 static enum gimplify_status
2115 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2116 {
2117 tree expr = *expr_p;
2118 tree tmp, tmp2, type;
2119 enum gimplify_status ret;
2120
2121 type = TREE_TYPE (expr);
2122 if (!type)
2123 TREE_TYPE (expr) = void_type_node;
2124
2125 /* If this COND_EXPR has a value, copy the values into a temporary within
2126 the arms. */
2127 else if (! VOID_TYPE_P (type))
2128 {
2129 if (target)
2130 {
2131 ret = gimplify_expr (&target, pre_p, NULL,
2132 is_gimple_min_lval, fb_lvalue);
2133 if (ret != GS_ERROR)
2134 ret = GS_OK;
2135 tmp = target;
2136 tmp2 = unshare_expr (target);
2137 }
2138 else
2139 {
2140 tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2141 ret = GS_ALL_DONE;
2142 }
2143
2144 /* Build the then clause, 't1 = a;'. But don't build an assignment
2145 if this branch is void; in C++ it can be, if it's a throw. */
2146 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2147 TREE_OPERAND (expr, 1)
2148 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2149
2150 /* Build the else clause, 't1 = b;'. */
2151 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2152 TREE_OPERAND (expr, 2)
2153 = build (MODIFY_EXPR, void_type_node, tmp2, TREE_OPERAND (expr, 2));
2154
2155 TREE_TYPE (expr) = void_type_node;
2156 recalculate_side_effects (expr);
2157
2158 /* Move the COND_EXPR to the prequeue. */
2159 gimplify_and_add (expr, pre_p);
2160
2161 *expr_p = tmp;
2162 return ret;
2163 }
2164
2165 /* Make sure the condition has BOOLEAN_TYPE. */
2166 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2167
2168 /* Break apart && and || conditions. */
2169 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2170 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2171 {
2172 expr = shortcut_cond_expr (expr);
2173
2174 if (expr != *expr_p)
2175 {
2176 *expr_p = expr;
2177
2178 /* We can't rely on gimplify_expr to re-gimplify the expanded
2179 form properly, as cleanups might cause the target labels to be
2180 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2181 set up a conditional context. */
2182 gimple_push_condition ();
2183 gimplify_stmt (expr_p);
2184 gimple_pop_condition (pre_p);
2185
2186 return GS_ALL_DONE;
2187 }
2188 }
2189
2190 /* Now do the normal gimplification. */
2191 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2192 is_gimple_condexpr, fb_rvalue);
2193
2194 gimple_push_condition ();
2195
2196 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2197 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2198 recalculate_side_effects (expr);
2199
2200 gimple_pop_condition (pre_p);
2201
2202 if (ret == GS_ERROR)
2203 ;
2204 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2205 ret = GS_ALL_DONE;
2206 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2207 /* Rewrite "if (a); else b" to "if (!a) b" */
2208 {
2209 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2210 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2211 is_gimple_condexpr, fb_rvalue);
2212
2213 tmp = TREE_OPERAND (expr, 1);
2214 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2215 TREE_OPERAND (expr, 2) = tmp;
2216 }
2217 else
2218 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2219 expr = TREE_OPERAND (expr, 0);
2220
2221 *expr_p = expr;
2222 return ret;
2223 }
2224
2225 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2226 a call to __builtin_memcpy. */
2227
2228 static enum gimplify_status
2229 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2230 {
2231 tree args, t, to, to_ptr, from;
2232
2233 to = TREE_OPERAND (*expr_p, 0);
2234 from = TREE_OPERAND (*expr_p, 1);
2235
2236 args = tree_cons (NULL, size, NULL);
2237
2238 t = build_fold_addr_expr (from);
2239 args = tree_cons (NULL, t, args);
2240
2241 to_ptr = build_fold_addr_expr (to);
2242 args = tree_cons (NULL, to_ptr, args);
2243 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2244 t = build_function_call_expr (t, args);
2245
2246 if (want_value)
2247 {
2248 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2249 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2250 }
2251
2252 *expr_p = t;
2253 return GS_OK;
2254 }
2255
2256 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2257 a call to __builtin_memset. In this case we know that the RHS is
2258 a CONSTRUCTOR with an empty element list. */
2259
2260 static enum gimplify_status
2261 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2262 {
2263 tree args, t, to, to_ptr;
2264
2265 to = TREE_OPERAND (*expr_p, 0);
2266
2267 args = tree_cons (NULL, size, NULL);
2268
2269 args = tree_cons (NULL, integer_zero_node, args);
2270
2271 to_ptr = build_fold_addr_expr (to);
2272 args = tree_cons (NULL, to_ptr, args);
2273 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2274 t = build_function_call_expr (t, args);
2275
2276 if (want_value)
2277 {
2278 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2279 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2280 }
2281
2282 *expr_p = t;
2283 return GS_OK;
2284 }
2285
2286 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
2287 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2288 assignment. Returns non-null if we detect a potential overlap. */
2289
2290 struct gimplify_init_ctor_preeval_data
2291 {
2292 /* The base decl of the lhs object. May be NULL, in which case we
2293 have to assume the lhs is indirect. */
2294 tree lhs_base_decl;
2295
2296 /* The alias set of the lhs object. */
2297 int lhs_alias_set;
2298 };
2299
2300 static tree
2301 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2302 {
2303 struct gimplify_init_ctor_preeval_data *data
2304 = (struct gimplify_init_ctor_preeval_data *) xdata;
2305 tree t = *tp;
2306
2307 /* If we find the base object, obviously we have overlap. */
2308 if (data->lhs_base_decl == t)
2309 return t;
2310
2311 /* If the constructor component is indirect, determine if we have a
2312 potential overlap with the lhs. The only bits of information we
2313 have to go on at this point are addressability and alias sets. */
2314 if (TREE_CODE (t) == INDIRECT_REF
2315 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2316 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2317 return t;
2318
2319 if (DECL_P (t) || TYPE_P (t))
2320 *walk_subtrees = 0;
2321 return NULL;
2322 }
2323
2324 /* A subroutine of gimplify_init_constructor. Pre-evaluate *EXPR_P,
2325 force values that overlap with the lhs (as described by *DATA)
2326 into temporaries. */
2327
2328 static void
2329 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2330 struct gimplify_init_ctor_preeval_data *data)
2331 {
2332 enum gimplify_status one;
2333
2334 /* If the value is invariant, then there's nothing to pre-evaluate.
2335 But ensure it doesn't have any side-effects since a SAVE_EXPR is
2336 invariant but has side effects and might contain a reference to
2337 the object we're initializing. */
2338 if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2339 return;
2340
2341 /* If the type has non-trivial constructors, we can't pre-evaluate. */
2342 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2343 return;
2344
2345 /* Recurse for nested constructors. */
2346 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2347 {
2348 tree list;
2349 for (list = CONSTRUCTOR_ELTS (*expr_p); list ; list = TREE_CHAIN (list))
2350 gimplify_init_ctor_preeval (&TREE_VALUE (list), pre_p, post_p, data);
2351 return;
2352 }
2353
2354 /* We can't preevaluate if the type contains a placeholder. */
2355 if (type_contains_placeholder_p (TREE_TYPE (*expr_p)))
2356 return;
2357
2358 /* Gimplify the constructor element to something appropriate for the rhs
2359 of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
2360 the gimplifier will consider this a store to memory. Doing this
2361 gimplification now means that we won't have to deal with complicated
2362 language-specific trees, nor trees like SAVE_EXPR that can induce
2363 exponential search behaviour. */
2364 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2365 if (one == GS_ERROR)
2366 {
2367 *expr_p = NULL;
2368 return;
2369 }
2370
2371 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2372 with the lhs, since "a = { .x=a }" doesn't make sense. This will
2373 always be true for all scalars, since is_gimple_mem_rhs insists on a
2374 temporary variable for them. */
2375 if (DECL_P (*expr_p))
2376 return;
2377
2378 /* If this is of variable size, we have no choice but to assume it doesn't
2379 overlap since we can't make a temporary for it. */
2380 if (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (*expr_p))))
2381 return;
2382
2383 /* Otherwise, we must search for overlap ... */
2384 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2385 return;
2386
2387 /* ... and if found, force the value into a temporary. */
2388 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2389 }
2390
2391 /* A subroutine of gimplify_init_constructor. Generate individual
2392 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
2393 assignments should happen. LIST is the CONSTRUCTOR_ELTS of the
2394 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
2395 zeroed first. */
2396
2397 static void
2398 gimplify_init_ctor_eval (tree object, tree list, tree *pre_p, bool cleared)
2399 {
2400 tree array_elt_type = NULL;
2401
2402 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2403 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2404
2405 for (; list; list = TREE_CHAIN (list))
2406 {
2407 tree purpose, value, cref, init;
2408
2409 purpose = TREE_PURPOSE (list);
2410 value = TREE_VALUE (list);
2411
2412 /* NULL values are created above for gimplification errors. */
2413 if (value == NULL)
2414 continue;
2415
2416 if (cleared && initializer_zerop (value))
2417 continue;
2418
2419 if (array_elt_type)
2420 {
2421 /* ??? Here's to hoping the front end fills in all of the indicies,
2422 so we don't have to figure out what's missing ourselves. */
2423 gcc_assert (purpose);
2424 /* ??? Need to handle this. */
2425 gcc_assert (TREE_CODE (purpose) != RANGE_EXPR);
2426
2427 cref = build (ARRAY_REF, array_elt_type, unshare_expr (object),
2428 purpose, NULL_TREE, NULL_TREE);
2429 }
2430 else
2431 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
2432 unshare_expr (object), purpose, NULL_TREE);
2433
2434 if (TREE_CODE (value) == CONSTRUCTOR)
2435 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2436 pre_p, cleared);
2437 else
2438 {
2439 init = build (MODIFY_EXPR, TREE_TYPE (cref), cref, value);
2440 gimplify_and_add (init, pre_p);
2441 }
2442 }
2443 }
2444
2445 /* A subroutine of gimplify_modify_expr. Break out elements of a
2446 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2447
2448 Note that we still need to clear any elements that don't have explicit
2449 initializers, so if not all elements are initialized we keep the
2450 original MODIFY_EXPR, we just remove all of the constructor elements. */
2451
2452 static enum gimplify_status
2453 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2454 tree *post_p, bool want_value)
2455 {
2456 tree object;
2457 tree ctor = TREE_OPERAND (*expr_p, 1);
2458 tree type = TREE_TYPE (ctor);
2459 enum gimplify_status ret;
2460 tree elt_list;
2461
2462 if (TREE_CODE (ctor) != CONSTRUCTOR)
2463 return GS_UNHANDLED;
2464
2465 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
2466 is_gimple_lvalue, fb_lvalue);
2467 if (ret == GS_ERROR)
2468 return ret;
2469 object = TREE_OPERAND (*expr_p, 0);
2470
2471 elt_list = CONSTRUCTOR_ELTS (ctor);
2472
2473 ret = GS_ALL_DONE;
2474 switch (TREE_CODE (type))
2475 {
2476 case RECORD_TYPE:
2477 case UNION_TYPE:
2478 case QUAL_UNION_TYPE:
2479 case ARRAY_TYPE:
2480 {
2481 struct gimplify_init_ctor_preeval_data preeval_data;
2482 HOST_WIDE_INT num_elements, num_nonzero_elements;
2483 HOST_WIDE_INT num_nonconstant_elements;
2484 bool cleared;
2485
2486 /* Aggregate types must lower constructors to initialization of
2487 individual elements. The exception is that a CONSTRUCTOR node
2488 with no elements indicates zero-initialization of the whole. */
2489 if (elt_list == NULL)
2490 break;
2491
2492 categorize_ctor_elements (ctor, &num_nonzero_elements,
2493 &num_nonconstant_elements);
2494
2495 /* If a const aggregate variable is being initialized, then it
2496 should never be a lose to promote the variable to be static. */
2497 if (num_nonconstant_elements == 0
2498 && TREE_READONLY (object)
2499 && TREE_CODE (object) == VAR_DECL)
2500 {
2501 DECL_INITIAL (object) = ctor;
2502 TREE_STATIC (object) = 1;
2503 if (!DECL_NAME (object))
2504 DECL_NAME (object) = create_tmp_var_name ("C");
2505 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2506
2507 /* ??? C++ doesn't automatically append a .<number> to the
2508 assembler name, and even when it does, it looks a FE private
2509 data structures to figure out what that number should be,
2510 which are not set for this variable. I suppose this is
2511 important for local statics for inline functions, which aren't
2512 "local" in the object file sense. So in order to get a unique
2513 TU-local symbol, we must invoke the lhd version now. */
2514 lhd_set_decl_assembler_name (object);
2515
2516 *expr_p = NULL_TREE;
2517 break;
2518 }
2519
2520 /* If there are "lots" of initialized elements, and all of them
2521 are valid address constants, then the entire initializer can
2522 be dropped to memory, and then memcpy'd out. */
2523 if (num_nonconstant_elements == 0)
2524 {
2525 HOST_WIDE_INT size = int_size_in_bytes (type);
2526 unsigned int align;
2527
2528 /* ??? We can still get unbounded array types, at least
2529 from the C++ front end. This seems wrong, but attempt
2530 to work around it for now. */
2531 if (size < 0)
2532 {
2533 size = int_size_in_bytes (TREE_TYPE (object));
2534 if (size >= 0)
2535 TREE_TYPE (ctor) = type = TREE_TYPE (object);
2536 }
2537
2538 /* Find the maximum alignment we can assume for the object. */
2539 /* ??? Make use of DECL_OFFSET_ALIGN. */
2540 if (DECL_P (object))
2541 align = DECL_ALIGN (object);
2542 else
2543 align = TYPE_ALIGN (type);
2544
2545 if (size > 0 && !can_move_by_pieces (size, align))
2546 {
2547 tree new = create_tmp_var_raw (type, "C");
2548
2549 gimple_add_tmp_var (new);
2550 TREE_STATIC (new) = 1;
2551 TREE_READONLY (new) = 1;
2552 DECL_INITIAL (new) = ctor;
2553 if (align > DECL_ALIGN (new))
2554 {
2555 DECL_ALIGN (new) = align;
2556 DECL_USER_ALIGN (new) = 1;
2557 }
2558 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
2559
2560 TREE_OPERAND (*expr_p, 1) = new;
2561
2562 /* This is no longer an assignment of a CONSTRUCTOR, but
2563 we still may have processing to do on the LHS. So
2564 pretend we didn't do anything here to let that happen. */
2565 return GS_UNHANDLED;
2566 }
2567 }
2568
2569 /* If there are "lots" of initialized elements, even discounting
2570 those that are not address constants (and thus *must* be
2571 computed at runtime), then partition the constructor into
2572 constant and non-constant parts. Block copy the constant
2573 parts in, then generate code for the non-constant parts. */
2574 /* TODO. There's code in cp/typeck.c to do this. */
2575
2576 num_elements = count_type_elements (TREE_TYPE (ctor));
2577
2578 /* If there are "lots" of zeros, then block clear the object first. */
2579 cleared = false;
2580 if (num_elements - num_nonzero_elements > CLEAR_RATIO
2581 && num_nonzero_elements < num_elements/4)
2582 cleared = true;
2583
2584 /* ??? This bit ought not be needed. For any element not present
2585 in the initializer, we should simply set them to zero. Except
2586 we'd need to *find* the elements that are not present, and that
2587 requires trickery to avoid quadratic compile-time behavior in
2588 large cases or excessive memory use in small cases. */
2589 else
2590 {
2591 HOST_WIDE_INT len = list_length (elt_list);
2592 if (TREE_CODE (type) == ARRAY_TYPE)
2593 {
2594 tree nelts = array_type_nelts (type);
2595 if (!host_integerp (nelts, 1)
2596 || tree_low_cst (nelts, 1) + 1 != len)
2597 cleared = true;
2598 }
2599 else if (len != fields_length (type))
2600 cleared = true;
2601 }
2602
2603 if (cleared)
2604 {
2605 /* Zap the CONSTRUCTOR element list, which simplifies this case.
2606 Note that we still have to gimplify, in order to handle the
2607 case of variable sized types. Avoid shared tree structures. */
2608 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
2609 object = unshare_expr (object);
2610 gimplify_stmt (expr_p);
2611 append_to_statement_list (*expr_p, pre_p);
2612 }
2613
2614 preeval_data.lhs_base_decl = get_base_address (object);
2615 if (!DECL_P (preeval_data.lhs_base_decl))
2616 preeval_data.lhs_base_decl = NULL;
2617 preeval_data.lhs_alias_set = get_alias_set (object);
2618
2619 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
2620 pre_p, post_p, &preeval_data);
2621 gimplify_init_ctor_eval (object, elt_list, pre_p, cleared);
2622
2623 *expr_p = NULL_TREE;
2624 }
2625 break;
2626
2627 case COMPLEX_TYPE:
2628 {
2629 tree r, i;
2630
2631 /* Extract the real and imaginary parts out of the ctor. */
2632 r = i = NULL_TREE;
2633 if (elt_list)
2634 {
2635 r = TREE_VALUE (elt_list);
2636 elt_list = TREE_CHAIN (elt_list);
2637 if (elt_list)
2638 {
2639 i = TREE_VALUE (elt_list);
2640 gcc_assert (!TREE_CHAIN (elt_list));
2641 }
2642 }
2643 if (r == NULL || i == NULL)
2644 {
2645 tree zero = convert (TREE_TYPE (type), integer_zero_node);
2646 if (r == NULL)
2647 r = zero;
2648 if (i == NULL)
2649 i = zero;
2650 }
2651
2652 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
2653 represent creation of a complex value. */
2654 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
2655 {
2656 ctor = build_complex (type, r, i);
2657 TREE_OPERAND (*expr_p, 1) = ctor;
2658 }
2659 else
2660 {
2661 ctor = build (COMPLEX_EXPR, type, r, i);
2662 TREE_OPERAND (*expr_p, 1) = ctor;
2663 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
2664 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
2665 fb_rvalue);
2666 }
2667 }
2668 break;
2669
2670 case VECTOR_TYPE:
2671 /* Go ahead and simplify constant constructors to VECTOR_CST. */
2672 if (TREE_CONSTANT (ctor))
2673 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
2674 else
2675 {
2676 /* Vector types use CONSTRUCTOR all the way through gimple
2677 compilation as a general initializer. */
2678 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
2679 {
2680 enum gimplify_status tret;
2681 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
2682 is_gimple_val, fb_rvalue);
2683 if (tret == GS_ERROR)
2684 ret = GS_ERROR;
2685 }
2686 }
2687 break;
2688
2689 default:
2690 /* So how did we get a CONSTRUCTOR for a scalar type? */
2691 gcc_unreachable ();
2692 }
2693
2694 if (ret == GS_ERROR)
2695 return GS_ERROR;
2696 else if (want_value)
2697 {
2698 append_to_statement_list (*expr_p, pre_p);
2699 *expr_p = object;
2700 return GS_OK;
2701 }
2702 else
2703 return GS_ALL_DONE;
2704 }
2705
2706 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
2707 based on the code of the RHS. We loop for as long as something changes. */
2708
2709 static enum gimplify_status
2710 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
2711 tree *post_p, bool want_value)
2712 {
2713 enum gimplify_status ret = GS_OK;
2714
2715 while (ret != GS_UNHANDLED)
2716 switch (TREE_CODE (*from_p))
2717 {
2718 case TARGET_EXPR:
2719 {
2720 /* If we are initializing something from a TARGET_EXPR, strip the
2721 TARGET_EXPR and initialize it directly, if possible. This can't
2722 be done if the initializer is void, since that implies that the
2723 temporary is set in some non-trivial way.
2724
2725 ??? What about code that pulls out the temp and uses it
2726 elsewhere? I think that such code never uses the TARGET_EXPR as
2727 an initializer. If I'm wrong, we'll abort because the temp won't
2728 have any RTL. In that case, I guess we'll need to replace
2729 references somehow. */
2730 tree init = TARGET_EXPR_INITIAL (*from_p);
2731
2732 if (!VOID_TYPE_P (TREE_TYPE (init)))
2733 {
2734 *from_p = init;
2735 ret = GS_OK;
2736 }
2737 else
2738 ret = GS_UNHANDLED;
2739 }
2740 break;
2741
2742 case COMPOUND_EXPR:
2743 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
2744 caught. */
2745 gimplify_compound_expr (from_p, pre_p, true);
2746 ret = GS_OK;
2747 break;
2748
2749 case CONSTRUCTOR:
2750 /* If we're initializing from a CONSTRUCTOR, break this into
2751 individual MODIFY_EXPRs. */
2752 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2753
2754 case COND_EXPR:
2755 /* If we're assigning to a non-register type, push the assignment
2756 down into the branches. This is mandatory for ADDRESSABLE types,
2757 since we cannot generate temporaries for such, but it saves a
2758 copy in other cases as well. */
2759 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
2760 {
2761 *expr_p = *from_p;
2762 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2763 }
2764 else
2765 ret = GS_UNHANDLED;
2766 break;
2767
2768 default:
2769 ret = GS_UNHANDLED;
2770 break;
2771 }
2772
2773 return ret;
2774 }
2775
2776 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2777
2778 modify_expr
2779 : varname '=' rhs
2780 | '*' ID '=' rhs
2781
2782 PRE_P points to the list where side effects that must happen before
2783 *EXPR_P should be stored.
2784
2785 POST_P points to the list where side effects that must happen after
2786 *EXPR_P should be stored.
2787
2788 WANT_VALUE is nonzero iff we want to use the value of this expression
2789 in another expression. */
2790
2791 static enum gimplify_status
2792 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2793 {
2794 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2795 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2796 enum gimplify_status ret = GS_UNHANDLED;
2797
2798 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
2799 || TREE_CODE (*expr_p) == INIT_EXPR);
2800
2801 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2802 if (TREE_CODE (*expr_p) == INIT_EXPR)
2803 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2804
2805 /* See if any simplifications can be done based on what the RHS is. */
2806 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2807 want_value);
2808 if (ret != GS_UNHANDLED)
2809 return ret;
2810
2811 /* If the value being copied is of variable width, compute the length
2812 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
2813 before gimplifying any of the operands so that we can resolve any
2814 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
2815 the size of the expression to be copied, not of the destination, so
2816 that is what we must here. */
2817 maybe_with_size_expr (from_p);
2818
2819 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2820 if (ret == GS_ERROR)
2821 return ret;
2822
2823 ret = gimplify_expr (from_p, pre_p, post_p,
2824 rhs_predicate_for (*to_p), fb_rvalue);
2825 if (ret == GS_ERROR)
2826 return ret;
2827
2828 /* Now see if the above changed *from_p to something we handle specially. */
2829 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2830 want_value);
2831 if (ret != GS_UNHANDLED)
2832 return ret;
2833
2834 /* If we've got a variable sized assignment between two lvalues (i.e. does
2835 not involve a call), then we can make things a bit more straightforward
2836 by converting the assignment to memcpy or memset. */
2837 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
2838 {
2839 tree from = TREE_OPERAND (*from_p, 0);
2840 tree size = TREE_OPERAND (*from_p, 1);
2841
2842 if (TREE_CODE (from) == CONSTRUCTOR)
2843 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
2844 if (is_gimple_addressable (from))
2845 {
2846 *from_p = from;
2847 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
2848 }
2849 }
2850
2851 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
2852 {
2853 /* If we've somehow already got an SSA_NAME on the LHS, then
2854 we're probably modifying it twice. Not good. */
2855 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
2856 *to_p = make_ssa_name (*to_p, *expr_p);
2857 }
2858
2859 if (want_value)
2860 {
2861 append_to_statement_list (*expr_p, pre_p);
2862 *expr_p = *to_p;
2863 return GS_OK;
2864 }
2865
2866 return GS_ALL_DONE;
2867 }
2868
2869 /* Gimplify a comparison between two variable-sized objects. Do this
2870 with a call to BUILT_IN_MEMCMP. */
2871
2872 static enum gimplify_status
2873 gimplify_variable_sized_compare (tree *expr_p)
2874 {
2875 tree op0 = TREE_OPERAND (*expr_p, 0);
2876 tree op1 = TREE_OPERAND (*expr_p, 1);
2877 tree args, t, dest;
2878
2879 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
2880 t = unshare_expr (t);
2881 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
2882 args = tree_cons (NULL, t, NULL);
2883 t = build_fold_addr_expr (op1);
2884 args = tree_cons (NULL, t, args);
2885 dest = build_fold_addr_expr (op0);
2886 args = tree_cons (NULL, dest, args);
2887 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
2888 t = build_function_call_expr (t, args);
2889 *expr_p
2890 = build (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
2891
2892 return GS_OK;
2893 }
2894
2895 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2896 points to the expression to gimplify.
2897
2898 Expressions of the form 'a && b' are gimplified to:
2899
2900 a && b ? true : false
2901
2902 gimplify_cond_expr will do the rest.
2903
2904 PRE_P points to the list where side effects that must happen before
2905 *EXPR_P should be stored. */
2906
2907 static enum gimplify_status
2908 gimplify_boolean_expr (tree *expr_p)
2909 {
2910 /* Preserve the original type of the expression. */
2911 tree type = TREE_TYPE (*expr_p);
2912
2913 *expr_p = build (COND_EXPR, type, *expr_p,
2914 convert (type, boolean_true_node),
2915 convert (type, boolean_false_node));
2916
2917 return GS_OK;
2918 }
2919
2920 /* Gimplifies an expression sequence. This function gimplifies each
2921 expression and re-writes the original expression with the last
2922 expression of the sequence in GIMPLE form.
2923
2924 PRE_P points to the list where the side effects for all the
2925 expressions in the sequence will be emitted.
2926
2927 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2928 /* ??? Should rearrange to share the pre-queue with all the indirect
2929 invocations of gimplify_expr. Would probably save on creations
2930 of statement_list nodes. */
2931
2932 static enum gimplify_status
2933 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2934 {
2935 tree t = *expr_p;
2936
2937 do
2938 {
2939 tree *sub_p = &TREE_OPERAND (t, 0);
2940
2941 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2942 gimplify_compound_expr (sub_p, pre_p, false);
2943 else
2944 gimplify_stmt (sub_p);
2945 append_to_statement_list (*sub_p, pre_p);
2946
2947 t = TREE_OPERAND (t, 1);
2948 }
2949 while (TREE_CODE (t) == COMPOUND_EXPR);
2950
2951 *expr_p = t;
2952 if (want_value)
2953 return GS_OK;
2954 else
2955 {
2956 gimplify_stmt (expr_p);
2957 return GS_ALL_DONE;
2958 }
2959 }
2960
2961 /* Gimplifies a statement list. These may be created either by an
2962 enlightened front-end, or by shortcut_cond_expr. */
2963
2964 static enum gimplify_status
2965 gimplify_statement_list (tree *expr_p)
2966 {
2967 tree_stmt_iterator i = tsi_start (*expr_p);
2968
2969 while (!tsi_end_p (i))
2970 {
2971 tree t;
2972
2973 gimplify_stmt (tsi_stmt_ptr (i));
2974
2975 t = tsi_stmt (i);
2976 if (t == NULL)
2977 tsi_delink (&i);
2978 else if (TREE_CODE (t) == STATEMENT_LIST)
2979 {
2980 tsi_link_before (&i, t, TSI_SAME_STMT);
2981 tsi_delink (&i);
2982 }
2983 else
2984 tsi_next (&i);
2985 }
2986
2987 return GS_ALL_DONE;
2988 }
2989
2990 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2991 gimplify. After gimplification, EXPR_P will point to a new temporary
2992 that holds the original value of the SAVE_EXPR node.
2993
2994 PRE_P points to the list where side effects that must happen before
2995 *EXPR_P should be stored. */
2996
2997 static enum gimplify_status
2998 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2999 {
3000 enum gimplify_status ret = GS_ALL_DONE;
3001 tree val;
3002
3003 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
3004 val = TREE_OPERAND (*expr_p, 0);
3005
3006 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
3007 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
3008 {
3009 /* The operand may be a void-valued expression such as SAVE_EXPRs
3010 generated by the Java frontend for class initialization. It is
3011 being executed only for its side-effects. */
3012 if (TREE_TYPE (val) == void_type_node)
3013 {
3014 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3015 is_gimple_stmt, fb_none);
3016 append_to_statement_list (TREE_OPERAND (*expr_p, 0), pre_p);
3017 val = NULL;
3018 }
3019 else
3020 val = get_initialized_tmp_var (val, pre_p, post_p);
3021
3022 TREE_OPERAND (*expr_p, 0) = val;
3023 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
3024 }
3025
3026 *expr_p = val;
3027
3028 return ret;
3029 }
3030
3031 /* Re-write the ADDR_EXPR node pointed by EXPR_P
3032
3033 unary_expr
3034 : ...
3035 | '&' varname
3036 ...
3037
3038 PRE_P points to the list where side effects that must happen before
3039 *EXPR_P should be stored.
3040
3041 POST_P points to the list where side effects that must happen after
3042 *EXPR_P should be stored. */
3043
3044 static enum gimplify_status
3045 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3046 {
3047 tree expr = *expr_p;
3048 tree op0 = TREE_OPERAND (expr, 0);
3049 enum gimplify_status ret;
3050
3051 switch (TREE_CODE (op0))
3052 {
3053 case INDIRECT_REF:
3054 /* Check if we are dealing with an expression of the form '&*ptr'.
3055 While the front end folds away '&*ptr' into 'ptr', these
3056 expressions may be generated internally by the compiler (e.g.,
3057 builtins like __builtin_va_end). */
3058 *expr_p = TREE_OPERAND (op0, 0);
3059 ret = GS_OK;
3060 break;
3061
3062 case VIEW_CONVERT_EXPR:
3063 /* Take the address of our operand and then convert it to the type of
3064 this ADDR_EXPR.
3065
3066 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3067 all clear. The impact of this transformation is even less clear. */
3068 *expr_p = fold_convert (TREE_TYPE (expr),
3069 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3070 ret = GS_OK;
3071 break;
3072
3073 default:
3074 /* We use fb_either here because the C frontend sometimes takes
3075 the address of a call that returns a struct; see
3076 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
3077 the implied temporary explicit. */
3078 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3079 is_gimple_addressable, fb_either);
3080 if (ret != GS_ERROR)
3081 {
3082 /* The above may have made an INDIRECT_REF (e.g, Ada's NULL_EXPR),
3083 so check for it here. It's not worth checking for the other
3084 cases above. */
3085 if (TREE_CODE (TREE_OPERAND (expr, 0)) == INDIRECT_REF)
3086 {
3087 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3088 break;
3089 }
3090
3091 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
3092 is set properly. */
3093 recompute_tree_invarant_for_addr_expr (expr);
3094
3095 /* Mark the RHS addressable. */
3096 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3097 }
3098 break;
3099 }
3100
3101 return ret;
3102 }
3103
3104 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3105 value; output operands should be a gimple lvalue. */
3106
3107 static enum gimplify_status
3108 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3109 {
3110 tree expr = *expr_p;
3111 int noutputs = list_length (ASM_OUTPUTS (expr));
3112 const char **oconstraints
3113 = (const char **) alloca ((noutputs) * sizeof (const char *));
3114 int i;
3115 tree link;
3116 const char *constraint;
3117 bool allows_mem, allows_reg, is_inout;
3118 enum gimplify_status ret, tret;
3119
3120 ASM_STRING (expr)
3121 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
3122 ASM_INPUTS (expr));
3123
3124 ret = GS_ALL_DONE;
3125 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3126 {
3127 oconstraints[i] = constraint
3128 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3129
3130 parse_output_constraint (&constraint, i, 0, 0,
3131 &allows_mem, &allows_reg, &is_inout);
3132
3133 if (!allows_reg && allows_mem)
3134 lang_hooks.mark_addressable (TREE_VALUE (link));
3135
3136 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3137 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3138 fb_lvalue | fb_mayfail);
3139 if (tret == GS_ERROR)
3140 {
3141 error ("invalid lvalue in asm output %d", i);
3142 ret = tret;
3143 }
3144
3145 if (is_inout)
3146 {
3147 /* An input/output operand. To give the optimizers more
3148 flexibility, split it into separate input and output
3149 operands. */
3150 tree input;
3151 char buf[10];
3152 size_t constraint_len = strlen (constraint);
3153
3154 /* Turn the in/out constraint into an output constraint. */
3155 char *p = xstrdup (constraint);
3156 p[0] = '=';
3157 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3158 free (p);
3159
3160 /* And add a matching input constraint. */
3161 if (allows_reg)
3162 {
3163 sprintf (buf, "%d", i);
3164 input = build_string (strlen (buf), buf);
3165 }
3166 else
3167 input = build_string (constraint_len - 1, constraint + 1);
3168 input = build_tree_list (build_tree_list (NULL_TREE, input),
3169 unshare_expr (TREE_VALUE (link)));
3170 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3171 }
3172 }
3173
3174 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3175 {
3176 constraint
3177 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3178 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3179 oconstraints, &allows_mem, &allows_reg);
3180
3181 /* If the operand is a memory input, it should be an lvalue. */
3182 if (!allows_reg && allows_mem)
3183 {
3184 lang_hooks.mark_addressable (TREE_VALUE (link));
3185 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3186 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3187 if (tret == GS_ERROR)
3188 {
3189 error ("memory input %d is not directly addressable", i);
3190 ret = tret;
3191 }
3192 }
3193 else
3194 {
3195 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3196 is_gimple_val, fb_rvalue);
3197 if (tret == GS_ERROR)
3198 ret = tret;
3199 }
3200 }
3201
3202 return ret;
3203 }
3204
3205 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3206 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3207 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3208 return to this function.
3209
3210 FIXME should we complexify the prequeue handling instead? Or use flags
3211 for all the cleanups and let the optimizer tighten them up? The current
3212 code seems pretty fragile; it will break on a cleanup within any
3213 non-conditional nesting. But any such nesting would be broken, anyway;
3214 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3215 and continues out of it. We can do that at the RTL level, though, so
3216 having an optimizer to tighten up try/finally regions would be a Good
3217 Thing. */
3218
3219 static enum gimplify_status
3220 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3221 {
3222 tree_stmt_iterator iter;
3223 tree body;
3224
3225 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3226
3227 /* We only care about the number of conditions between the innermost
3228 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
3229 int old_conds = gimplify_ctxp->conditions;
3230 gimplify_ctxp->conditions = 0;
3231
3232 body = TREE_OPERAND (*expr_p, 0);
3233 gimplify_to_stmt_list (&body);
3234
3235 gimplify_ctxp->conditions = old_conds;
3236
3237 for (iter = tsi_start (body); !tsi_end_p (iter); )
3238 {
3239 tree *wce_p = tsi_stmt_ptr (iter);
3240 tree wce = *wce_p;
3241
3242 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3243 {
3244 if (tsi_one_before_end_p (iter))
3245 {
3246 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
3247 tsi_delink (&iter);
3248 break;
3249 }
3250 else
3251 {
3252 tree sl, tfe;
3253 enum tree_code code;
3254
3255 if (CLEANUP_EH_ONLY (wce))
3256 code = TRY_CATCH_EXPR;
3257 else
3258 code = TRY_FINALLY_EXPR;
3259
3260 sl = tsi_split_statement_list_after (&iter);
3261 tfe = build (code, void_type_node, sl, NULL_TREE);
3262 append_to_statement_list (TREE_OPERAND (wce, 0),
3263 &TREE_OPERAND (tfe, 1));
3264 *wce_p = tfe;
3265 iter = tsi_start (sl);
3266 }
3267 }
3268 else
3269 tsi_next (&iter);
3270 }
3271
3272 if (temp)
3273 {
3274 *expr_p = temp;
3275 append_to_statement_list (body, pre_p);
3276 return GS_OK;
3277 }
3278 else
3279 {
3280 *expr_p = body;
3281 return GS_ALL_DONE;
3282 }
3283 }
3284
3285 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
3286 is the cleanup action required. */
3287
3288 static void
3289 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, tree *pre_p)
3290 {
3291 tree wce;
3292
3293 /* Errors can result in improperly nested cleanups. Which results in
3294 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
3295 if (errorcount || sorrycount)
3296 return;
3297
3298 if (gimple_conditional_context ())
3299 {
3300 /* If we're in a conditional context, this is more complex. We only
3301 want to run the cleanup if we actually ran the initialization that
3302 necessitates it, but we want to run it after the end of the
3303 conditional context. So we wrap the try/finally around the
3304 condition and use a flag to determine whether or not to actually
3305 run the destructor. Thus
3306
3307 test ? f(A()) : 0
3308
3309 becomes (approximately)
3310
3311 flag = 0;
3312 try {
3313 if (test) { A::A(temp); flag = 1; val = f(temp); }
3314 else { val = 0; }
3315 } finally {
3316 if (flag) A::~A(temp);
3317 }
3318 val
3319 */
3320
3321 tree flag = create_tmp_var (boolean_type_node, "cleanup");
3322 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
3323 boolean_false_node);
3324 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
3325 boolean_true_node);
3326 cleanup = build (COND_EXPR, void_type_node, flag, cleanup, NULL);
3327 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3328 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
3329 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
3330 append_to_statement_list (ftrue, pre_p);
3331
3332 /* Because of this manipulation, and the EH edges that jump
3333 threading cannot redirect, the temporary (VAR) will appear
3334 to be used uninitialized. Don't warn. */
3335 TREE_NO_WARNING (var) = 1;
3336 }
3337 else
3338 {
3339 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3340 CLEANUP_EH_ONLY (wce) = eh_only;
3341 append_to_statement_list (wce, pre_p);
3342 }
3343
3344 gimplify_stmt (&TREE_OPERAND (wce, 0));
3345 }
3346
3347 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3348
3349 static enum gimplify_status
3350 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3351 {
3352 tree targ = *expr_p;
3353 tree temp = TARGET_EXPR_SLOT (targ);
3354 tree init = TARGET_EXPR_INITIAL (targ);
3355 enum gimplify_status ret;
3356
3357 if (init)
3358 {
3359 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
3360 to the temps list. */
3361 gimple_add_tmp_var (temp);
3362
3363 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
3364 expression is supposed to initialize the slot. */
3365 if (VOID_TYPE_P (TREE_TYPE (init)))
3366 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3367 else
3368 {
3369 /* Special handling for BIND_EXPR can result in fewer temps. */
3370 ret = GS_OK;
3371 if (TREE_CODE (init) == BIND_EXPR)
3372 gimplify_bind_expr (&init, temp, pre_p);
3373 if (init != temp)
3374 {
3375 init = build (MODIFY_EXPR, void_type_node, temp, init);
3376 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
3377 fb_none);
3378 }
3379 }
3380 if (ret == GS_ERROR)
3381 return GS_ERROR;
3382 append_to_statement_list (init, pre_p);
3383
3384 /* If needed, push the cleanup for the temp. */
3385 if (TARGET_EXPR_CLEANUP (targ))
3386 {
3387 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3388 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
3389 CLEANUP_EH_ONLY (targ), pre_p);
3390 }
3391
3392 /* Only expand this once. */
3393 TREE_OPERAND (targ, 3) = init;
3394 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3395 }
3396 else
3397 /* We should have expanded this before. */
3398 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
3399
3400 *expr_p = temp;
3401 return GS_OK;
3402 }
3403
3404 /* Gimplification of expression trees. */
3405
3406 /* Gimplify an expression which appears at statement context; usually, this
3407 means replacing it with a suitably gimple STATEMENT_LIST. */
3408
3409 void
3410 gimplify_stmt (tree *stmt_p)
3411 {
3412 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3413 }
3414
3415 /* Similarly, but force the result to be a STATEMENT_LIST. */
3416
3417 void
3418 gimplify_to_stmt_list (tree *stmt_p)
3419 {
3420 gimplify_stmt (stmt_p);
3421 if (!*stmt_p)
3422 *stmt_p = alloc_stmt_list ();
3423 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3424 {
3425 tree t = *stmt_p;
3426 *stmt_p = alloc_stmt_list ();
3427 append_to_statement_list (t, stmt_p);
3428 }
3429 }
3430
3431
3432 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3433 gimplification failed.
3434
3435 PRE_P points to the list where side effects that must happen before
3436 EXPR should be stored.
3437
3438 POST_P points to the list where side effects that must happen after
3439 EXPR should be stored, or NULL if there is no suitable list. In
3440 that case, we copy the result to a temporary, emit the
3441 post-effects, and then return the temporary.
3442
3443 GIMPLE_TEST_F points to a function that takes a tree T and
3444 returns nonzero if T is in the GIMPLE form requested by the
3445 caller. The GIMPLE predicates are in tree-gimple.c.
3446
3447 This test is used twice. Before gimplification, the test is
3448 invoked to determine whether *EXPR_P is already gimple enough. If
3449 that fails, *EXPR_P is gimplified according to its code and
3450 GIMPLE_TEST_F is called again. If the test still fails, then a new
3451 temporary variable is created and assigned the value of the
3452 gimplified expression.
3453
3454 FALLBACK tells the function what sort of a temporary we want. If the 1
3455 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3456 If both are set, either is OK, but an lvalue is preferable.
3457
3458 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3459 iterates until solution. */
3460
3461 enum gimplify_status
3462 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3463 bool (* gimple_test_f) (tree), fallback_t fallback)
3464 {
3465 tree tmp;
3466 tree internal_pre = NULL_TREE;
3467 tree internal_post = NULL_TREE;
3468 tree save_expr;
3469 int is_statement = (pre_p == NULL);
3470 location_t saved_location;
3471 enum gimplify_status ret;
3472
3473 save_expr = *expr_p;
3474 if (save_expr == NULL_TREE)
3475 return GS_ALL_DONE;
3476
3477 /* We used to check the predicate here and return immediately if it
3478 succeeds. This is wrong; the design is for gimplification to be
3479 idempotent, and for the predicates to only test for valid forms, not
3480 whether they are fully simplified. */
3481
3482 /* Set up our internal queues if needed. */
3483 if (pre_p == NULL)
3484 pre_p = &internal_pre;
3485 if (post_p == NULL)
3486 post_p = &internal_post;
3487
3488 saved_location = input_location;
3489 if (save_expr != error_mark_node
3490 && EXPR_HAS_LOCATION (*expr_p))
3491 input_location = EXPR_LOCATION (*expr_p);
3492
3493 /* Loop over the specific gimplifiers until the toplevel node
3494 remains the same. */
3495 do
3496 {
3497 /* Strip away as many useless type conversions as possible
3498 at the toplevel. */
3499 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
3500
3501 /* Remember the expr. */
3502 save_expr = *expr_p;
3503
3504 /* Die, die, die, my darling. */
3505 if (save_expr == error_mark_node
3506 || (TREE_TYPE (save_expr)
3507 && TREE_TYPE (save_expr) == error_mark_node))
3508 {
3509 ret = GS_ERROR;
3510 break;
3511 }
3512
3513 /* Do any language-specific gimplification. */
3514 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3515 if (ret == GS_OK)
3516 {
3517 if (*expr_p == NULL_TREE)
3518 break;
3519 if (*expr_p != save_expr)
3520 continue;
3521 }
3522 else if (ret != GS_UNHANDLED)
3523 break;
3524
3525 ret = GS_OK;
3526 switch (TREE_CODE (*expr_p))
3527 {
3528 /* First deal with the special cases. */
3529
3530 case POSTINCREMENT_EXPR:
3531 case POSTDECREMENT_EXPR:
3532 case PREINCREMENT_EXPR:
3533 case PREDECREMENT_EXPR:
3534 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3535 fallback != fb_none);
3536 break;
3537
3538 case ARRAY_REF:
3539 case ARRAY_RANGE_REF:
3540 case REALPART_EXPR:
3541 case IMAGPART_EXPR:
3542 case COMPONENT_REF:
3543 case VIEW_CONVERT_EXPR:
3544 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3545 fallback ? fallback : fb_rvalue);
3546 break;
3547
3548 case COND_EXPR:
3549 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3550 break;
3551
3552 case CALL_EXPR:
3553 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
3554 break;
3555
3556 case TREE_LIST:
3557 gcc_unreachable ();
3558
3559 case COMPOUND_EXPR:
3560 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3561 break;
3562
3563 case MODIFY_EXPR:
3564 case INIT_EXPR:
3565 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3566 fallback != fb_none);
3567 break;
3568
3569 case TRUTH_ANDIF_EXPR:
3570 case TRUTH_ORIF_EXPR:
3571 ret = gimplify_boolean_expr (expr_p);
3572 break;
3573
3574 case TRUTH_NOT_EXPR:
3575 TREE_OPERAND (*expr_p, 0)
3576 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3577 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3578 is_gimple_val, fb_rvalue);
3579 recalculate_side_effects (*expr_p);
3580 break;
3581
3582 case ADDR_EXPR:
3583 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3584 break;
3585
3586 case VA_ARG_EXPR:
3587 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3588 break;
3589
3590 case CONVERT_EXPR:
3591 case NOP_EXPR:
3592 if (IS_EMPTY_STMT (*expr_p))
3593 {
3594 ret = GS_ALL_DONE;
3595 break;
3596 }
3597
3598 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3599 || fallback == fb_none)
3600 {
3601 /* Just strip a conversion to void (or in void context) and
3602 try again. */
3603 *expr_p = TREE_OPERAND (*expr_p, 0);
3604 break;
3605 }
3606
3607 ret = gimplify_conversion (expr_p);
3608 if (ret == GS_ERROR)
3609 break;
3610 if (*expr_p != save_expr)
3611 break;
3612 /* FALLTHRU */
3613
3614 case FIX_TRUNC_EXPR:
3615 case FIX_CEIL_EXPR:
3616 case FIX_FLOOR_EXPR:
3617 case FIX_ROUND_EXPR:
3618 /* unary_expr: ... | '(' cast ')' val | ... */
3619 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3620 is_gimple_val, fb_rvalue);
3621 recalculate_side_effects (*expr_p);
3622 break;
3623
3624 case INDIRECT_REF:
3625 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3626 is_gimple_reg, fb_rvalue);
3627 recalculate_side_effects (*expr_p);
3628 break;
3629
3630 /* Constants need not be gimplified. */
3631 case INTEGER_CST:
3632 case REAL_CST:
3633 case STRING_CST:
3634 case COMPLEX_CST:
3635 case VECTOR_CST:
3636 ret = GS_ALL_DONE;
3637 break;
3638
3639 case CONST_DECL:
3640 /* If we require an lvalue, such as for ADDR_EXPR, retain the
3641 CONST_DECL node. Otherwise the decl is replaceable by its
3642 value. */
3643 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
3644 if (fallback & fb_lvalue)
3645 ret = GS_ALL_DONE;
3646 else
3647 *expr_p = DECL_INITIAL (*expr_p);
3648 break;
3649
3650 case DECL_EXPR:
3651 ret = gimplify_decl_expr (expr_p);
3652 break;
3653
3654 case EXC_PTR_EXPR:
3655 /* FIXME make this a decl. */
3656 ret = GS_ALL_DONE;
3657 break;
3658
3659 case BIND_EXPR:
3660 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3661 break;
3662
3663 case LOOP_EXPR:
3664 ret = gimplify_loop_expr (expr_p, pre_p);
3665 break;
3666
3667 case SWITCH_EXPR:
3668 ret = gimplify_switch_expr (expr_p, pre_p);
3669 break;
3670
3671 case LABELED_BLOCK_EXPR:
3672 ret = gimplify_labeled_block_expr (expr_p);
3673 break;
3674
3675 case EXIT_BLOCK_EXPR:
3676 ret = gimplify_exit_block_expr (expr_p);
3677 break;
3678
3679 case EXIT_EXPR:
3680 ret = gimplify_exit_expr (expr_p);
3681 break;
3682
3683 case GOTO_EXPR:
3684 /* If the target is not LABEL, then it is a computed jump
3685 and the target needs to be gimplified. */
3686 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3687 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3688 NULL, is_gimple_val, fb_rvalue);
3689 break;
3690
3691 case LABEL_EXPR:
3692 ret = GS_ALL_DONE;
3693 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
3694 == current_function_decl);
3695 break;
3696
3697 case CASE_LABEL_EXPR:
3698 ret = gimplify_case_label_expr (expr_p);
3699 break;
3700
3701 case RETURN_EXPR:
3702 ret = gimplify_return_expr (*expr_p, pre_p);
3703 break;
3704
3705 case CONSTRUCTOR:
3706 /* Don't reduce this in place; let gimplify_init_constructor work its
3707 magic. Buf if we're just elaborating this for side effects, just
3708 gimplify any element that has side-effects. */
3709 if (fallback == fb_none)
3710 {
3711 for (tmp = CONSTRUCTOR_ELTS (*expr_p); tmp;
3712 tmp = TREE_CHAIN (tmp))
3713 if (TREE_SIDE_EFFECTS (TREE_VALUE (tmp)))
3714 gimplify_expr (&TREE_VALUE (tmp), pre_p, post_p,
3715 gimple_test_f, fallback);
3716
3717 *expr_p = NULL_TREE;
3718 }
3719
3720 ret = GS_ALL_DONE;
3721 break;
3722
3723 /* The following are special cases that are not handled by the
3724 original GIMPLE grammar. */
3725
3726 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3727 eliminated. */
3728 case SAVE_EXPR:
3729 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3730 break;
3731
3732 case BIT_FIELD_REF:
3733 {
3734 enum gimplify_status r0, r1, r2;
3735
3736 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3737 is_gimple_lvalue, fb_either);
3738 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3739 is_gimple_val, fb_rvalue);
3740 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3741 is_gimple_val, fb_rvalue);
3742 recalculate_side_effects (*expr_p);
3743
3744 ret = MIN (r0, MIN (r1, r2));
3745 }
3746 break;
3747
3748 case NON_LVALUE_EXPR:
3749 /* This should have been stripped above. */
3750 gcc_unreachable ();
3751
3752 case ASM_EXPR:
3753 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3754 break;
3755
3756 case TRY_FINALLY_EXPR:
3757 case TRY_CATCH_EXPR:
3758 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3759 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3760 ret = GS_ALL_DONE;
3761 break;
3762
3763 case CLEANUP_POINT_EXPR:
3764 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3765 break;
3766
3767 case TARGET_EXPR:
3768 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3769 break;
3770
3771 case CATCH_EXPR:
3772 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3773 ret = GS_ALL_DONE;
3774 break;
3775
3776 case EH_FILTER_EXPR:
3777 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3778 ret = GS_ALL_DONE;
3779 break;
3780
3781 case OBJ_TYPE_REF:
3782 {
3783 enum gimplify_status r0, r1;
3784 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
3785 is_gimple_val, fb_rvalue);
3786 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
3787 is_gimple_val, fb_rvalue);
3788 ret = MIN (r0, r1);
3789 }
3790 break;
3791
3792 case LABEL_DECL:
3793 /* We get here when taking the address of a label. We mark
3794 the label as "forced"; meaning it can never be removed and
3795 it is a potential target for any computed goto. */
3796 FORCED_LABEL (*expr_p) = 1;
3797 ret = GS_ALL_DONE;
3798 break;
3799
3800 case STATEMENT_LIST:
3801 ret = gimplify_statement_list (expr_p);
3802 break;
3803
3804 case WITH_SIZE_EXPR:
3805 {
3806 enum gimplify_status r0, r1;
3807 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3808 post_p == &internal_post ? NULL : post_p,
3809 gimple_test_f, fallback);
3810 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3811 is_gimple_val, fb_rvalue);
3812 }
3813 break;
3814
3815 case VAR_DECL:
3816 /* ??? If this is a local variable, and it has not been seen in any
3817 outer BIND_EXPR, then it's probably the result of a duplicate
3818 declaration, for which we've already issued an error. It would
3819 be really nice if the front end wouldn't leak these at all.
3820 Currently the only known culprit is C++ destructors, as seen
3821 in g++.old-deja/g++.jason/binding.C. */
3822 tmp = *expr_p;
3823 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3824 && decl_function_context (tmp) == current_function_decl
3825 && !DECL_SEEN_IN_BIND_EXPR_P (tmp))
3826 {
3827 gcc_assert (errorcount || sorrycount);
3828 ret = GS_ERROR;
3829 break;
3830 }
3831
3832 /* If this is a local variable sized decl, it must be accessed
3833 indirectly. Perform that substitution. */
3834 if (DECL_VALUE_EXPR (tmp))
3835 {
3836 *expr_p = unshare_expr (DECL_VALUE_EXPR (tmp));
3837 ret = GS_OK;
3838 break;
3839 }
3840
3841 ret = GS_ALL_DONE;
3842 break;
3843
3844 case SSA_NAME:
3845 /* Allow callbacks into the gimplifier during optimization. */
3846 ret = GS_ALL_DONE;
3847 break;
3848
3849 default:
3850 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
3851 {
3852 case '<':
3853 /* If this is a comparison of objects of aggregate type,
3854 handle it specially (by converting to a call to
3855 memcmp). It would be nice to only have to do this
3856 for variable-sized objects, but then we'd have to
3857 allow the same nest of reference nodes we allow for
3858 MODIFY_EXPR and that's too complex. */
3859 if (!AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 1))))
3860 goto expr_2;
3861 ret = gimplify_variable_sized_compare (expr_p);
3862 break;
3863
3864 /* If *EXPR_P does not need to be special-cased, handle it
3865 according to its class. */
3866 case '1':
3867 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3868 post_p, is_gimple_val, fb_rvalue);
3869 break;
3870
3871 case '2':
3872 expr_2:
3873 {
3874 enum gimplify_status r0, r1;
3875
3876 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3877 post_p, is_gimple_val, fb_rvalue);
3878 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3879 post_p, is_gimple_val, fb_rvalue);
3880
3881 ret = MIN (r0, r1);
3882 break;
3883 }
3884
3885 case 'd':
3886 case 'c':
3887 ret = GS_ALL_DONE;
3888 goto dont_recalculate;
3889
3890 default:
3891 gcc_assert (TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3892 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3893 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR);
3894 goto expr_2;
3895 }
3896
3897 recalculate_side_effects (*expr_p);
3898 dont_recalculate:
3899 break;
3900 }
3901
3902 /* If we replaced *expr_p, gimplify again. */
3903 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3904 ret = GS_ALL_DONE;
3905 }
3906 while (ret == GS_OK);
3907
3908 /* If we encountered an error_mark somewhere nested inside, either
3909 stub out the statement or propagate the error back out. */
3910 if (ret == GS_ERROR)
3911 {
3912 if (is_statement)
3913 *expr_p = NULL;
3914 goto out;
3915 }
3916
3917 /* This was only valid as a return value from the langhook, which
3918 we handled. Make sure it doesn't escape from any other context. */
3919 gcc_assert (ret != GS_UNHANDLED);
3920
3921 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
3922 {
3923 /* We aren't looking for a value, and we don't have a valid
3924 statement. If it doesn't have side-effects, throw it away. */
3925 if (!TREE_SIDE_EFFECTS (*expr_p))
3926 *expr_p = NULL;
3927 else if (!TREE_THIS_VOLATILE (*expr_p))
3928 {
3929 /* This is probably a _REF that contains something nested that
3930 has side effects. Recurse through the operands to find it. */
3931 enum tree_code code = TREE_CODE (*expr_p);
3932
3933 switch (code)
3934 {
3935 case COMPONENT_REF:
3936 case REALPART_EXPR: case IMAGPART_EXPR:
3937 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3938 gimple_test_f, fallback);
3939 break;
3940
3941 case ARRAY_REF: case ARRAY_RANGE_REF:
3942 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3943 gimple_test_f, fallback);
3944 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3945 gimple_test_f, fallback);
3946 break;
3947
3948 default:
3949 /* Anything else with side-effects must be converted to
3950 a valid statement before we get here. */
3951 gcc_unreachable ();
3952 }
3953
3954 *expr_p = NULL;
3955 }
3956 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3957 {
3958 /* Historically, the compiler has treated a bare
3959 reference to a volatile lvalue as forcing a load. */
3960 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3961 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3962 }
3963 else
3964 /* We can't do anything useful with a volatile reference to
3965 incomplete type, so just throw it away. */
3966 *expr_p = NULL;
3967 }
3968
3969 /* If we are gimplifying at the statement level, we're done. Tack
3970 everything together and replace the original statement with the
3971 gimplified form. */
3972 if (fallback == fb_none || is_statement)
3973 {
3974 if (internal_pre || internal_post)
3975 {
3976 append_to_statement_list (*expr_p, &internal_pre);
3977 append_to_statement_list (internal_post, &internal_pre);
3978 annotate_all_with_locus (&internal_pre, input_location);
3979 *expr_p = internal_pre;
3980 }
3981 else if (!*expr_p)
3982 ;
3983 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
3984 annotate_all_with_locus (expr_p, input_location);
3985 else
3986 annotate_one_with_locus (*expr_p, input_location);
3987 goto out;
3988 }
3989
3990 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3991 interesting. */
3992
3993 /* If it's sufficiently simple already, we're done. Unless we are
3994 handling some post-effects internally; if that's the case, we need to
3995 copy into a temp before adding the post-effects to the tree. */
3996 if (!internal_post && (*gimple_test_f) (*expr_p))
3997 goto out;
3998
3999 /* Otherwise, we need to create a new temporary for the gimplified
4000 expression. */
4001
4002 /* We can't return an lvalue if we have an internal postqueue. The
4003 object the lvalue refers to would (probably) be modified by the
4004 postqueue; we need to copy the value out first, which means an
4005 rvalue. */
4006 if ((fallback & fb_lvalue) && !internal_post
4007 && is_gimple_addressable (*expr_p))
4008 {
4009 /* An lvalue will do. Take the address of the expression, store it
4010 in a temporary, and replace the expression with an INDIRECT_REF of
4011 that temporary. */
4012 tmp = build_fold_addr_expr (*expr_p);
4013 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
4014 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
4015 }
4016 else if ((fallback & fb_rvalue) && is_gimple_formal_tmp_rhs (*expr_p))
4017 {
4018 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
4019
4020 /* An rvalue will do. Assign the gimplified expression into a new
4021 temporary TMP and replace the original expression with TMP. */
4022
4023 if (internal_post || (fallback & fb_lvalue))
4024 /* The postqueue might change the value of the expression between
4025 the initialization and use of the temporary, so we can't use a
4026 formal temp. FIXME do we care? */
4027 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
4028 else
4029 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4030
4031 if (TREE_CODE (*expr_p) != SSA_NAME)
4032 DECL_GIMPLE_FORMAL_TEMP_P (*expr_p) = 1;
4033 }
4034 else
4035 {
4036 #ifdef ENABLE_CHECKING
4037 if (!(fallback & fb_mayfail))
4038 {
4039 fprintf (stderr, "gimplification failed:\n");
4040 print_generic_expr (stderr, *expr_p, 0);
4041 debug_tree (*expr_p);
4042 internal_error ("gimplification failed");
4043 }
4044 #endif
4045 gcc_assert (fallback & fb_mayfail);
4046 /* If this is an asm statement, and the user asked for the
4047 impossible, don't abort. Fail and let gimplify_asm_expr
4048 issue an error. */
4049 ret = GS_ERROR;
4050 goto out;
4051 }
4052
4053 /* Make sure the temporary matches our predicate. */
4054 gcc_assert ((*gimple_test_f) (*expr_p));
4055
4056 if (internal_post)
4057 {
4058 annotate_all_with_locus (&internal_post, input_location);
4059 append_to_statement_list (internal_post, pre_p);
4060 }
4061
4062 out:
4063 input_location = saved_location;
4064 return ret;
4065 }
4066
4067 /* Look through TYPE for variable-sized objects and gimplify each such
4068 size that we find. Add to LIST_P any statements generated. */
4069
4070 void
4071 gimplify_type_sizes (tree type, tree *list_p)
4072 {
4073 tree field;
4074
4075 switch (TREE_CODE (type))
4076 {
4077 case ERROR_MARK:
4078 return;
4079
4080 case INTEGER_TYPE:
4081 case ENUMERAL_TYPE:
4082 case BOOLEAN_TYPE:
4083 case CHAR_TYPE:
4084 case REAL_TYPE:
4085 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
4086 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
4087 break;
4088
4089 case ARRAY_TYPE:
4090 /* These anonymous types don't have declarations, so handle them here. */
4091 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
4092 break;
4093
4094 case RECORD_TYPE:
4095 case UNION_TYPE:
4096 case QUAL_UNION_TYPE:
4097 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4098 if (TREE_CODE (field) == FIELD_DECL)
4099 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
4100 break;
4101
4102 default:
4103 break;
4104 }
4105
4106 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
4107 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
4108 }
4109
4110 /* Subroutine of the above to gimplify one size or position, *EXPR_P.
4111 We add any required statements to STMT_P. */
4112
4113 void
4114 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
4115 {
4116 /* We don't do anything if the value isn't there, is constant, or contains
4117 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
4118 a VAR_DECL. If it's a VAR_DECL from another function, the gimplfier
4119 will want to replace it with a new variable, but that will cause problems
4120 if this type is from outside the function. It's OK to have that here. */
4121 if (*expr_p == NULL_TREE || TREE_CONSTANT (*expr_p)
4122 || TREE_CODE (*expr_p) == VAR_DECL
4123 || CONTAINS_PLACEHOLDER_P (*expr_p))
4124 return;
4125
4126 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
4127 }
4128 \f
4129 #ifdef ENABLE_CHECKING
4130 /* Compare types A and B for a "close enough" match. */
4131
4132 static bool
4133 cpt_same_type (tree a, tree b)
4134 {
4135 if (lang_hooks.types_compatible_p (a, b))
4136 return true;
4137
4138 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
4139 link them together. This routine is intended to catch type errors
4140 that will affect the optimizers, and the optimizers don't add new
4141 dereferences of function pointers, so ignore it. */
4142 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
4143 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
4144 return true;
4145
4146 /* ??? The C FE pushes type qualifiers after the fact into the type of
4147 the element from the type of the array. See build_unary_op's handling
4148 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
4149 should have done it when creating the variable in the first place.
4150 Alternately, why aren't the two array types made variants? */
4151 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
4152 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4153
4154 /* And because of those, we have to recurse down through pointers. */
4155 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
4156 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4157
4158 return false;
4159 }
4160
4161 /* Check for some cases of the front end missing cast expressions.
4162 The type of a dereference should correspond to the pointer type;
4163 similarly the type of an address should match its object. */
4164
4165 static tree
4166 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4167 void *data ATTRIBUTE_UNUSED)
4168 {
4169 tree t = *tp;
4170 tree ptype, otype, dtype;
4171
4172 switch (TREE_CODE (t))
4173 {
4174 case INDIRECT_REF:
4175 case ARRAY_REF:
4176 otype = TREE_TYPE (t);
4177 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
4178 dtype = TREE_TYPE (ptype);
4179 gcc_assert (cpt_same_type (otype, dtype));
4180 break;
4181
4182 case ADDR_EXPR:
4183 ptype = TREE_TYPE (t);
4184 otype = TREE_TYPE (TREE_OPERAND (t, 0));
4185 dtype = TREE_TYPE (ptype);
4186 if (!cpt_same_type (otype, dtype))
4187 {
4188 /* &array is allowed to produce a pointer to the element, rather than
4189 a pointer to the array type. We must allow this in order to
4190 properly represent assigning the address of an array in C into
4191 pointer to the element type. */
4192 gcc_assert (TREE_CODE (otype) == ARRAY_TYPE
4193 && POINTER_TYPE_P (ptype)
4194 && cpt_same_type (TREE_TYPE (otype), dtype));
4195 break;
4196 }
4197 break;
4198
4199 default:
4200 return NULL_TREE;
4201 }
4202
4203
4204 return NULL_TREE;
4205 }
4206 #endif
4207
4208 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
4209 function decl containing BODY. */
4210
4211 void
4212 gimplify_body (tree *body_p, tree fndecl)
4213 {
4214 location_t saved_location = input_location;
4215 tree body;
4216
4217 timevar_push (TV_TREE_GIMPLIFY);
4218 push_gimplify_context ();
4219
4220 /* Unshare most shared trees in the body and in that of any nested functions.
4221 It would seem we don't have to do this for nested functions because
4222 they are supposed to be output and then the outer function gimplified
4223 first, but the g++ front end doesn't always do it that way. */
4224 unshare_body (body_p, fndecl);
4225 unvisit_body (body_p, fndecl);
4226
4227 /* Make sure input_location isn't set to something wierd. */
4228 input_location = DECL_SOURCE_LOCATION (fndecl);
4229
4230 /* Gimplify the function's body. */
4231 gimplify_stmt (body_p);
4232 body = *body_p;
4233
4234 /* Unshare again, in case gimplification was sloppy. */
4235 unshare_all_trees (body);
4236
4237 if (!body)
4238 body = alloc_stmt_list ();
4239 else if (TREE_CODE (body) == STATEMENT_LIST)
4240 {
4241 tree t = expr_only (*body_p);
4242 if (t)
4243 body = t;
4244 }
4245
4246 /* If there isn't an outer BIND_EXPR, add one. */
4247 if (TREE_CODE (body) != BIND_EXPR)
4248 {
4249 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
4250 NULL_TREE, NULL_TREE);
4251 TREE_SIDE_EFFECTS (b) = 1;
4252 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
4253 body = b;
4254 }
4255 *body_p = body;
4256
4257 pop_gimplify_context (body);
4258
4259 #ifdef ENABLE_CHECKING
4260 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
4261 #endif
4262
4263 timevar_pop (TV_TREE_GIMPLIFY);
4264 input_location = saved_location;
4265 }
4266
4267 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
4268 node for the function we want to gimplify. */
4269
4270 void
4271 gimplify_function_tree (tree fndecl)
4272 {
4273 tree oldfn;
4274
4275 oldfn = current_function_decl;
4276 current_function_decl = fndecl;
4277
4278 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
4279
4280 /* If we're instrumenting function entry/exit, then prepend the call to
4281 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
4282 catch the exit hook. */
4283 /* ??? Add some way to ignore exceptions for this TFE. */
4284 if (flag_instrument_function_entry_exit
4285 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
4286 {
4287 tree tf, x, bind;
4288
4289 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4290 TREE_SIDE_EFFECTS (tf) = 1;
4291 x = DECL_SAVED_TREE (fndecl);
4292 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
4293 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
4294 x = build_function_call_expr (x, NULL);
4295 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
4296
4297 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4298 TREE_SIDE_EFFECTS (bind) = 1;
4299 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
4300 x = build_function_call_expr (x, NULL);
4301 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
4302 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
4303
4304 DECL_SAVED_TREE (fndecl) = bind;
4305 }
4306
4307 current_function_decl = oldfn;
4308 }
4309
4310 \f
4311 /* Expands EXPR to list of gimple statements STMTS. If SIMPLE is true,
4312 force the result to be either ssa_name or an invariant, otherwise
4313 just force it to be a rhs expression. If VAR is not NULL, make the
4314 base variable of the final destination be VAR if suitable. */
4315
4316 tree
4317 force_gimple_operand (tree expr, tree *stmts, bool simple, tree var)
4318 {
4319 tree t;
4320 enum gimplify_status ret;
4321 gimple_predicate gimple_test_f;
4322
4323 *stmts = NULL_TREE;
4324
4325 if (is_gimple_val (expr))
4326 return expr;
4327
4328 gimple_test_f = simple ? is_gimple_val : is_gimple_reg_rhs;
4329
4330 push_gimplify_context ();
4331 gimplify_ctxp->into_ssa = true;
4332
4333 if (var)
4334 expr = build (MODIFY_EXPR, TREE_TYPE (var), var, expr);
4335
4336 ret = gimplify_expr (&expr, stmts, NULL,
4337 gimple_test_f, fb_rvalue);
4338 gcc_assert (ret != GS_ERROR);
4339
4340 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
4341 add_referenced_tmp_var (t);
4342
4343 pop_gimplify_context (NULL);
4344
4345 return expr;
4346 }
4347
4348 #include "gt-gimplify.h"
This page took 0.23255 seconds and 5 git commands to generate.