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