OSDN Git Service

* config/bfin/bfin.md (call_value_symbol, sibcall_value_symbol,
[pf3gnuchains/gcc-fork.git] / gcc / gimple-low.c
1 /* Tree lowering pass.  Lowers GIMPLE into unstructured form.
2
3    Copyright (C) 2003, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "varray.h"
29 #include "tree-gimple.h"
30 #include "tree-inline.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
33 #include "langhooks-def.h"
34 #include "tree-flow.h"
35 #include "timevar.h"
36 #include "except.h"
37 #include "hashtab.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "tree-pass.h"
43
44 struct lower_data
45 {
46   /* Block the current statement belongs to.  */
47   tree block;
48
49   /* A TREE_LIST of label and return statements to be moved to the end
50      of the function.  */
51   tree return_statements;
52 };
53
54 static void lower_stmt (tree_stmt_iterator *, struct lower_data *);
55 static void lower_bind_expr (tree_stmt_iterator *, struct lower_data *);
56 static void lower_cond_expr (tree_stmt_iterator *, struct lower_data *);
57 static void lower_return_expr (tree_stmt_iterator *, struct lower_data *);
58
59 /* Lowers the body of current_function_decl.  */
60
61 static unsigned int
62 lower_function_body (void)
63 {
64   struct lower_data data;
65   tree *body_p = &DECL_SAVED_TREE (current_function_decl);
66   tree bind = *body_p;
67   tree_stmt_iterator i;
68   tree t, x;
69
70   gcc_assert (TREE_CODE (bind) == BIND_EXPR);
71
72   memset (&data, 0, sizeof (data));
73   data.block = DECL_INITIAL (current_function_decl);
74   BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
75   BLOCK_CHAIN (data.block) = NULL_TREE;
76   TREE_ASM_WRITTEN (data.block) = 1;
77
78   *body_p = alloc_stmt_list ();
79   i = tsi_start (*body_p);
80   tsi_link_after (&i, bind, TSI_NEW_STMT);
81   lower_bind_expr (&i, &data);
82
83   i = tsi_last (*body_p);
84
85   /* If the function falls off the end, we need a null return statement.
86      If we've already got one in the return_statements list, we don't
87      need to do anything special.  Otherwise build one by hand.  */
88   if (block_may_fallthru (*body_p)
89       && (data.return_statements == NULL
90           || TREE_OPERAND (TREE_VALUE (data.return_statements), 0) != NULL))
91     {
92       x = build1 (RETURN_EXPR, void_type_node, NULL);
93       SET_EXPR_LOCATION (x, cfun->function_end_locus);
94       tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
95     }
96
97   /* If we lowered any return statements, emit the representative
98      at the end of the function.  */
99   for (t = data.return_statements ; t ; t = TREE_CHAIN (t))
100     {
101       x = build1 (LABEL_EXPR, void_type_node, TREE_PURPOSE (t));
102       tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
103
104       /* Remove the line number from the representative return statement.
105          It now fills in for many such returns.  Failure to remove this
106          will result in incorrect results for coverage analysis.  */
107       x = TREE_VALUE (t);
108 #ifdef USE_MAPPED_LOCATION
109       SET_EXPR_LOCATION (x, UNKNOWN_LOCATION);
110 #else
111       SET_EXPR_LOCUS (x, NULL);
112 #endif
113       tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
114     }
115
116   gcc_assert (data.block == DECL_INITIAL (current_function_decl));
117   BLOCK_SUBBLOCKS (data.block)
118     = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
119
120   clear_block_marks (data.block);
121   return 0;
122 }
123
124 struct tree_opt_pass pass_lower_cf = 
125 {
126   "lower",                              /* name */
127   NULL,                                 /* gate */
128   lower_function_body,                  /* execute */
129   NULL,                                 /* sub */
130   NULL,                                 /* next */
131   0,                                    /* static_pass_number */
132   0,                                    /* tv_id */
133   PROP_gimple_any,                      /* properties_required */
134   PROP_gimple_lcf,                      /* properties_provided */
135   0,                                    /* properties_destroyed */
136   0,                                    /* todo_flags_start */
137   TODO_dump_func,                       /* todo_flags_finish */
138   0                                     /* letter */
139 };
140
141
142 /* Lowers the EXPR.  Unlike gimplification the statements are not relowered
143    when they are changed -- if this has to be done, the lowering routine must
144    do it explicitly.  DATA is passed through the recursion.  */
145
146 static void
147 lower_stmt_body (tree expr, struct lower_data *data)
148 {
149   tree_stmt_iterator tsi;
150
151   for (tsi = tsi_start (expr); !tsi_end_p (tsi); )
152     lower_stmt (&tsi, data);
153 }
154
155
156 /* Lower the OpenMP directive statement pointed by TSI.  DATA is
157    passed through the recursion.  */
158
159 static void
160 lower_omp_directive (tree_stmt_iterator *tsi, struct lower_data *data)
161 {
162   tree clause, stmt;
163   
164   stmt = tsi_stmt (*tsi);
165
166   clause = (TREE_CODE (stmt) >= OMP_PARALLEL && TREE_CODE (stmt) <= OMP_SINGLE)
167            ? OMP_CLAUSES (stmt)
168            : NULL_TREE;
169
170   lower_stmt_body (OMP_BODY (stmt), data);
171   tsi_link_before (tsi, stmt, TSI_SAME_STMT);
172   tsi_link_before (tsi, OMP_BODY (stmt), TSI_SAME_STMT);
173   OMP_BODY (stmt) = NULL_TREE;
174   tsi_delink (tsi);
175 }
176
177
178 /* Lowers statement TSI.  DATA is passed through the recursion.  */
179
180 static void
181 lower_stmt (tree_stmt_iterator *tsi, struct lower_data *data)
182 {
183   tree stmt = tsi_stmt (*tsi);
184
185   if (EXPR_HAS_LOCATION (stmt) && data)
186     TREE_BLOCK (stmt) = data->block;
187
188   switch (TREE_CODE (stmt))
189     {
190     case BIND_EXPR:
191       lower_bind_expr (tsi, data);
192       return;
193     case COND_EXPR:
194       lower_cond_expr (tsi, data);
195       return;
196     case RETURN_EXPR:
197       lower_return_expr (tsi, data);
198       return;
199
200     case TRY_FINALLY_EXPR:
201     case TRY_CATCH_EXPR:
202       lower_stmt_body (TREE_OPERAND (stmt, 0), data);
203       lower_stmt_body (TREE_OPERAND (stmt, 1), data);
204       break;
205     case CATCH_EXPR:
206       lower_stmt_body (CATCH_BODY (stmt), data);
207       break;
208     case EH_FILTER_EXPR:
209       lower_stmt_body (EH_FILTER_FAILURE (stmt), data);
210       break;
211       
212     case NOP_EXPR:
213     case ASM_EXPR:
214     case MODIFY_EXPR:
215     case CALL_EXPR:
216     case GOTO_EXPR:
217     case LABEL_EXPR:
218     case SWITCH_EXPR:
219     case OMP_RETURN_EXPR:
220       break;
221
222     case OMP_PARALLEL:
223     case OMP_FOR:
224     case OMP_SECTIONS:
225     case OMP_SECTION:
226     case OMP_SINGLE:
227     case OMP_MASTER:
228     case OMP_ORDERED:
229     case OMP_CRITICAL:
230       lower_omp_directive (tsi, data);
231       return;
232
233     default:
234       gcc_unreachable ();
235     }
236
237   tsi_next (tsi);
238 }
239
240 /* Lowers a bind_expr TSI.  DATA is passed through the recursion.  */
241
242 static void
243 lower_bind_expr (tree_stmt_iterator *tsi, struct lower_data *data)
244 {
245   tree old_block = data->block;
246   tree stmt = tsi_stmt (*tsi);
247   tree new_block = BIND_EXPR_BLOCK (stmt);
248
249   if (new_block)
250     {
251       if (new_block == old_block)
252         {
253           /* The outermost block of the original function may not be the
254              outermost statement chain of the gimplified function.  So we
255              may see the outermost block just inside the function.  */
256           gcc_assert (new_block == DECL_INITIAL (current_function_decl));
257           new_block = NULL;
258         }
259       else
260         {
261           /* We do not expect to handle duplicate blocks.  */
262           gcc_assert (!TREE_ASM_WRITTEN (new_block));
263           TREE_ASM_WRITTEN (new_block) = 1;
264
265           /* Block tree may get clobbered by inlining.  Normally this would
266              be fixed in rest_of_decl_compilation using block notes, but
267              since we are not going to emit them, it is up to us.  */
268           BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
269           BLOCK_SUBBLOCKS (old_block) = new_block;
270           BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
271           BLOCK_SUPERCONTEXT (new_block) = old_block;
272
273           data->block = new_block;
274         }
275     }
276
277   record_vars (BIND_EXPR_VARS (stmt));
278   lower_stmt_body (BIND_EXPR_BODY (stmt), data);
279
280   if (new_block)
281     {
282       gcc_assert (data->block == new_block);
283
284       BLOCK_SUBBLOCKS (new_block)
285         = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
286       data->block = old_block;
287     }
288
289   /* The BIND_EXPR no longer carries any useful information -- kill it.  */
290   tsi_link_before (tsi, BIND_EXPR_BODY (stmt), TSI_SAME_STMT);
291   tsi_delink (tsi);
292 }
293
294 /* Try to determine whether a TRY_CATCH expression can fall through.
295    This is a subroutine of block_may_fallthru.  */
296
297 static bool
298 try_catch_may_fallthru (tree stmt)
299 {
300   tree_stmt_iterator i;
301
302   /* If the TRY block can fall through, the whole TRY_CATCH can
303      fall through.  */
304   if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
305     return true;
306
307   i = tsi_start (TREE_OPERAND (stmt, 1));
308   switch (TREE_CODE (tsi_stmt (i)))
309     {
310     case CATCH_EXPR:
311       /* We expect to see a sequence of CATCH_EXPR trees, each with a
312          catch expression and a body.  The whole TRY_CATCH may fall
313          through iff any of the catch bodies falls through.  */
314       for (; !tsi_end_p (i); tsi_next (&i))
315         {
316           if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
317             return true;
318         }
319       return false;
320
321     case EH_FILTER_EXPR:
322       /* The exception filter expression only matters if there is an
323          exception.  If the exception does not match EH_FILTER_TYPES,
324          we will execute EH_FILTER_FAILURE, and we will fall through
325          if that falls through.  If the exception does match
326          EH_FILTER_TYPES, the stack unwinder will continue up the
327          stack, so we will not fall through.  We don't know whether we
328          will throw an exception which matches EH_FILTER_TYPES or not,
329          so we just ignore EH_FILTER_TYPES and assume that we might
330          throw an exception which doesn't match.  */
331       return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
332
333     default:
334       /* This case represents statements to be executed when an
335          exception occurs.  Those statements are implicitly followed
336          by a RESX_EXPR to resume execution after the exception.  So
337          in this case the TRY_CATCH never falls through.  */
338       return false;
339     }
340 }
341
342 /* Try to determine if we can fall out of the bottom of BLOCK.  This guess
343    need not be 100% accurate; simply be conservative and return true if we
344    don't know.  This is used only to avoid stupidly generating extra code.
345    If we're wrong, we'll just delete the extra code later.  */
346
347 bool
348 block_may_fallthru (tree block)
349 {
350   tree stmt = expr_last (block);
351
352   switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
353     {
354     case GOTO_EXPR:
355     case RETURN_EXPR:
356     case RESX_EXPR:
357       /* Easy cases.  If the last statement of the block implies 
358          control transfer, then we can't fall through.  */
359       return false;
360
361     case SWITCH_EXPR:
362       /* If SWITCH_LABELS is set, this is lowered, and represents a
363          branch to a selected label and hence can not fall through.
364          Otherwise SWITCH_BODY is set, and the switch can fall
365          through.  */
366       return SWITCH_LABELS (stmt) == NULL_TREE;
367
368     case COND_EXPR:
369       if (block_may_fallthru (COND_EXPR_THEN (stmt)))
370         return true;
371       return block_may_fallthru (COND_EXPR_ELSE (stmt));
372
373     case BIND_EXPR:
374       return block_may_fallthru (BIND_EXPR_BODY (stmt));
375
376     case TRY_CATCH_EXPR:
377       return try_catch_may_fallthru (stmt);
378
379     case TRY_FINALLY_EXPR:
380       /* The finally clause is always executed after the try clause,
381          so if it does not fall through, then the try-finally will not
382          fall through.  Otherwise, if the try clause does not fall
383          through, then when the finally clause falls through it will
384          resume execution wherever the try clause was going.  So the
385          whole try-finally will only fall through if both the try
386          clause and the finally clause fall through.  */
387       return (block_may_fallthru (TREE_OPERAND (stmt, 0))
388               && block_may_fallthru (TREE_OPERAND (stmt, 1)));
389
390     case MODIFY_EXPR:
391       if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
392         stmt = TREE_OPERAND (stmt, 1);
393       else
394         return true;
395       /* FALLTHRU */
396
397     case CALL_EXPR:
398       /* Functions that do not return do not fall through.  */
399       return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
400     
401     case CLEANUP_POINT_EXPR:
402       return block_may_fallthru (TREE_OPERAND (stmt, 0));
403
404     default:
405       return true;
406     }
407 }
408
409 /* Lowers a cond_expr TSI.  DATA is passed through the recursion.  */
410
411 static void
412 lower_cond_expr (tree_stmt_iterator *tsi, struct lower_data *data)
413 {
414   tree stmt = tsi_stmt (*tsi);
415   bool then_is_goto, else_is_goto;
416   tree then_branch, else_branch;
417   tree then_goto, else_goto;
418   
419   then_branch = COND_EXPR_THEN (stmt);
420   else_branch = COND_EXPR_ELSE (stmt);
421
422   lower_stmt_body (then_branch, data);
423   lower_stmt_body (else_branch, data);
424
425   then_goto = expr_only (then_branch);
426   then_is_goto = then_goto && simple_goto_p (then_goto);
427
428   else_goto = expr_only (else_branch);
429   else_is_goto = else_goto && simple_goto_p (else_goto);
430
431   if (!then_is_goto || !else_is_goto)
432     {
433       tree then_label, else_label, end_label, t;
434
435       then_label = NULL_TREE;
436       else_label = NULL_TREE;
437       end_label = NULL_TREE;
438  
439       /* Replace the cond_expr with explicit gotos.  */
440       if (!then_is_goto)
441         {
442           t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
443           if (TREE_SIDE_EFFECTS (then_branch))
444             then_label = t;
445           else
446             end_label = t;
447           then_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
448         }
449
450       if (!else_is_goto)
451         {
452           t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
453           if (TREE_SIDE_EFFECTS (else_branch))
454             else_label = t;
455           else
456             {
457               /* Both THEN and ELSE can be no-ops if one or both contained an
458                  empty BIND_EXPR that was associated with the toplevel block
459                  of an inlined function.  In that case remove_useless_stmts
460                  can't have cleaned things up for us; kill the whole 
461                  conditional now.  */
462               if (end_label)
463                 {
464                   tsi_delink (tsi);
465                   return;
466                 }
467               else
468                 end_label = t;
469             }
470           else_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
471         }
472
473       if (then_label)
474         {
475           bool may_fallthru = block_may_fallthru (then_branch);
476
477           tsi_link_after (tsi, then_label, TSI_CONTINUE_LINKING);
478           tsi_link_after (tsi, then_branch, TSI_CONTINUE_LINKING);
479   
480           if (else_label && may_fallthru)
481             {
482               end_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
483               t = build_and_jump (&LABEL_EXPR_LABEL (end_label));
484               tsi_link_after (tsi, t, TSI_CONTINUE_LINKING);
485             }
486         }
487   
488       if (else_label)
489         {
490           tsi_link_after (tsi, else_label, TSI_CONTINUE_LINKING);
491           tsi_link_after (tsi, else_branch, TSI_CONTINUE_LINKING);
492         }
493
494       if (end_label)
495         tsi_link_after (tsi, end_label, TSI_CONTINUE_LINKING);
496     }
497
498   COND_EXPR_THEN (stmt) = then_goto;
499   COND_EXPR_ELSE (stmt) = else_goto;
500
501   tsi_next (tsi);
502 }
503
504 static void
505 lower_return_expr (tree_stmt_iterator *tsi, struct lower_data *data)
506 {
507   tree stmt = tsi_stmt (*tsi);
508   tree value, t, label;
509
510   /* Extract the value being returned.  */
511   value = TREE_OPERAND (stmt, 0);
512   if (value && TREE_CODE (value) == MODIFY_EXPR)
513     value = TREE_OPERAND (value, 1);
514
515   /* Match this up with an existing return statement that's been created.  */
516   for (t = data->return_statements; t ; t = TREE_CHAIN (t))
517     {
518       tree tvalue = TREE_OPERAND (TREE_VALUE (t), 0);
519       if (tvalue && TREE_CODE (tvalue) == MODIFY_EXPR)
520         tvalue = TREE_OPERAND (tvalue, 1);
521
522       if (value == tvalue)
523         {
524           label = TREE_PURPOSE (t);
525           goto found;
526         }
527     }
528
529   /* Not found.  Create a new label and record the return statement.  */
530   label = create_artificial_label ();
531   data->return_statements = tree_cons (label, stmt, data->return_statements);
532
533   /* Generate a goto statement and remove the return statement.  */
534  found:
535   t = build1 (GOTO_EXPR, void_type_node, label);
536   SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
537   tsi_link_before (tsi, t, TSI_SAME_STMT);
538   tsi_delink (tsi);
539 }
540 \f
541
542 /* Record the variables in VARS into function FN.  */
543
544 void
545 record_vars_into (tree vars, tree fn)
546 {
547   struct function *saved_cfun = cfun;
548
549   if (fn != current_function_decl)
550     cfun = DECL_STRUCT_FUNCTION (fn);
551
552   for (; vars; vars = TREE_CHAIN (vars))
553     {
554       tree var = vars;
555
556       /* BIND_EXPRs contains also function/type/constant declarations
557          we don't need to care about.  */
558       if (TREE_CODE (var) != VAR_DECL)
559         continue;
560
561       /* Nothing to do in this case.  */
562       if (DECL_EXTERNAL (var))
563         continue;
564
565       /* Record the variable.  */
566       cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
567                                              cfun->unexpanded_var_list);
568     }
569
570   if (fn != current_function_decl)
571     cfun = saved_cfun;
572 }
573
574
575 /* Record the variables in VARS into current_function_decl.  */
576
577 void
578 record_vars (tree vars)
579 {
580   record_vars_into (vars, current_function_decl);
581 }
582
583
584 /* Mark BLOCK used if it has a used variable in it, then recurse over its
585    subblocks.  */
586
587 static void
588 mark_blocks_with_used_vars (tree block)
589 {
590   tree var;
591   tree subblock;
592
593   if (!TREE_USED (block))
594     {
595       for (var = BLOCK_VARS (block);
596            var;
597            var = TREE_CHAIN (var))
598         {
599           if (TREE_USED (var))
600             {
601               TREE_USED (block) = true;
602               break;
603             }
604         }
605     }
606   for (subblock = BLOCK_SUBBLOCKS (block);
607        subblock;
608        subblock = BLOCK_CHAIN (subblock))
609     mark_blocks_with_used_vars (subblock);
610 }
611
612 /* Mark the used attribute on blocks correctly.  */
613   
614 static unsigned int
615 mark_used_blocks (void)
616 {  
617   mark_blocks_with_used_vars (DECL_INITIAL (current_function_decl));
618   return 0;
619 }
620
621
622 struct tree_opt_pass pass_mark_used_blocks = 
623 {
624   "blocks",                             /* name */
625   NULL,                                 /* gate */
626   mark_used_blocks,                     /* execute */
627   NULL,                                 /* sub */
628   NULL,                                 /* next */
629   0,                                    /* static_pass_number */
630   0,                                    /* tv_id */
631   0,                                    /* properties_required */
632   0,                                    /* properties_provided */
633   0,                                    /* properties_destroyed */
634   0,                                    /* todo_flags_start */
635   TODO_dump_func,                       /* todo_flags_finish */
636   0                                     /* letter */
637 };