OSDN Git Service

2004-07-16 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / gimple-low.c
1 /* Tree lowering pass.  Lowers GIMPLE into unstructured form.
2
3    Copyright (C) 2003 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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 "errors.h"
29 #include "varray.h"
30 #include "tree-gimple.h"
31 #include "tree-inline.h"
32 #include "diagnostic.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "tree-flow.h"
36 #include "timevar.h"
37 #include "except.h"
38 #include "hashtab.h"
39 #include "flags.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "toplev.h"
43 #include "tree-pass.h"
44
45 struct lower_data
46 {
47   /* Block the current statement belongs to.  */
48   tree block;
49
50   /* A TREE_LIST of label and return statements to be moved to the end
51      of the function.  */
52   tree return_statements;
53 };
54
55 static void lower_stmt (tree_stmt_iterator *, struct lower_data *);
56 static void lower_bind_expr (tree_stmt_iterator *, struct lower_data *);
57 static void lower_cond_expr (tree_stmt_iterator *, struct lower_data *);
58 static void lower_return_expr (tree_stmt_iterator *, struct lower_data *);
59 static bool expand_var_p (tree);
60
61 /* Lowers the body of current_function_decl.  */
62
63 static void
64 lower_function_body (void)
65 {
66   struct lower_data data;
67   tree *body_p = &DECL_SAVED_TREE (current_function_decl);
68   tree bind = *body_p;
69   tree_stmt_iterator i;
70   tree t, x;
71
72   if (TREE_CODE (bind) != BIND_EXPR)
73     abort ();
74
75   data.block = DECL_INITIAL (current_function_decl);
76   BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
77   BLOCK_CHAIN (data.block) = NULL_TREE;
78   TREE_ASM_WRITTEN (data.block) = 1;
79
80   data.return_statements = NULL_TREE;
81
82   *body_p = alloc_stmt_list ();
83   i = tsi_start (*body_p);
84   tsi_link_after (&i, bind, TSI_NEW_STMT);
85   lower_bind_expr (&i, &data);
86
87   i = tsi_last (*body_p);
88
89   /* If the function falls off the end, we need a null return statement.
90      If we've already got one in the return_statements list, we don't
91      need to do anything special.  Otherwise build one by hand.  */
92   if (block_may_fallthru (*body_p)
93       && (data.return_statements == NULL
94           || TREE_OPERAND (TREE_VALUE (data.return_statements), 0) != NULL))
95     {
96       x = build (RETURN_EXPR, void_type_node, NULL);
97       SET_EXPR_LOCATION (x, cfun->function_end_locus);
98       tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
99     }
100
101   /* If we lowered any return statements, emit the representative
102      at the end of the function.  */
103   for (t = data.return_statements ; t ; t = TREE_CHAIN (t))
104     {
105       x = build (LABEL_EXPR, void_type_node, TREE_PURPOSE (t));
106       tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
107
108       /* Remove the line number from the representative return statement.
109          It now fills in for many such returns.  Failure to remove this
110          will result in incorrect results for coverage analysis.  */
111       x = TREE_VALUE (t);
112 #ifdef USE_MAPPED_LOCATION
113       SET_EXPR_LOCATION (x, UNKNOWN_LOCATION);
114 #else
115       SET_EXPR_LOCUS (x, NULL);
116 #endif
117       tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
118     }
119
120   if (data.block != DECL_INITIAL (current_function_decl))
121     abort ();
122   BLOCK_SUBBLOCKS (data.block)
123     = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
124
125   clear_block_marks (data.block);
126 }
127
128 struct tree_opt_pass pass_lower_cf = 
129 {
130   "lower",                              /* name */
131   NULL,                                 /* gate */
132   lower_function_body,                  /* execute */
133   NULL,                                 /* sub */
134   NULL,                                 /* next */
135   0,                                    /* static_pass_number */
136   0,                                    /* tv_id */
137   PROP_gimple_any,                      /* properties_required */
138   PROP_gimple_lcf,                      /* properties_provided */
139   PROP_gimple_any,                      /* properties_destroyed */
140   0,                                    /* todo_flags_start */
141   TODO_dump_func                        /* todo_flags_finish */
142 };
143
144
145 /* Lowers the EXPR.  Unlike gimplification the statements are not relowered
146    when they are changed -- if this has to be done, the lowering routine must
147    do it explicitly.  DATA is passed through the recursion.  */
148
149 void
150 lower_stmt_body (tree expr, struct lower_data *data)
151 {
152   tree_stmt_iterator tsi;
153
154   for (tsi = tsi_start (expr); !tsi_end_p (tsi); )
155     lower_stmt (&tsi, data);
156 }
157
158 /* Lowers statement TSI.  DATA is passed through the recursion.  */
159
160 static void
161 lower_stmt (tree_stmt_iterator *tsi, struct lower_data *data)
162 {
163   tree stmt = tsi_stmt (*tsi);
164
165   if (EXPR_HAS_LOCATION (stmt) && data)
166     TREE_BLOCK (stmt) = data->block;
167
168   switch (TREE_CODE (stmt))
169     {
170     case BIND_EXPR:
171       lower_bind_expr (tsi, data);
172       return;
173     case COND_EXPR:
174       lower_cond_expr (tsi, data);
175       return;
176     case RETURN_EXPR:
177       lower_return_expr (tsi, data);
178       return;
179
180     case TRY_FINALLY_EXPR:
181     case TRY_CATCH_EXPR:
182       lower_stmt_body (TREE_OPERAND (stmt, 0), data);
183       lower_stmt_body (TREE_OPERAND (stmt, 1), data);
184       break;
185     case CATCH_EXPR:
186       lower_stmt_body (CATCH_BODY (stmt), data);
187       break;
188     case EH_FILTER_EXPR:
189       lower_stmt_body (EH_FILTER_FAILURE (stmt), data);
190       break;
191       
192     case NOP_EXPR:
193     case ASM_EXPR:
194     case MODIFY_EXPR:
195     case CALL_EXPR:
196     case GOTO_EXPR:
197     case LABEL_EXPR:
198     case VA_ARG_EXPR:
199     case SWITCH_EXPR:
200       break;
201
202     default:
203       print_node_brief (stderr, "", stmt, 0);
204     case COMPOUND_EXPR:
205       abort ();
206     }
207
208   tsi_next (tsi);
209 }
210
211 /* Lowers a bind_expr TSI.  DATA is passed through the recursion.  */
212
213 static void
214 lower_bind_expr (tree_stmt_iterator *tsi, struct lower_data *data)
215 {
216   tree old_block = data->block;
217   tree stmt = tsi_stmt (*tsi);
218   tree new_block = BIND_EXPR_BLOCK (stmt);
219
220   if (new_block)
221     {
222       if (new_block == old_block)
223         {
224           /* The outermost block of the original function may not be the
225              outermost statement chain of the gimplified function.  So we
226              may see the outermost block just inside the function.  */
227           if (new_block != DECL_INITIAL (current_function_decl))
228             abort ();
229           new_block = NULL;
230         }
231       else
232         {
233           /* We do not expect to handle duplicate blocks.  */
234           if (TREE_ASM_WRITTEN (new_block))
235             abort ();
236           TREE_ASM_WRITTEN (new_block) = 1;
237
238           /* Block tree may get clobbered by inlining.  Normally this would
239              be fixed in rest_of_decl_compilation using block notes, but
240              since we are not going to emit them, it is up to us.  */
241           BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
242           BLOCK_SUBBLOCKS (old_block) = new_block;
243           BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
244           BLOCK_SUPERCONTEXT (new_block) = old_block;
245
246           data->block = new_block;
247         }
248     }
249
250   record_vars (BIND_EXPR_VARS (stmt));
251   lower_stmt_body (BIND_EXPR_BODY (stmt), data);
252
253   if (new_block)
254     {
255       if (data->block != new_block)
256         abort ();
257
258       BLOCK_SUBBLOCKS (new_block)
259         = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
260       data->block = old_block;
261     }
262
263   /* The BIND_EXPR no longer carries any useful information -- kill it.  */
264   tsi_link_before (tsi, BIND_EXPR_BODY (stmt), TSI_SAME_STMT);
265   tsi_delink (tsi);
266 }
267
268 /* Try to determine if we can fall out of the bottom of BLOCK.  This guess
269    need not be 100% accurate; simply be conservative and return true if we
270    don't know.  This is used only to avoid stupidly generating extra code.
271    If we're wrong, we'll just delete the extra code later.  */
272
273 bool
274 block_may_fallthru (tree block)
275 {
276   tree stmt = expr_last (block);
277
278   switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
279     {
280     case GOTO_EXPR:
281     case RETURN_EXPR:
282     case RESX_EXPR:
283     case SWITCH_EXPR:
284       /* Easy cases.  If the last statement of the block implies 
285          control transfer, then we can't fall through.  */
286       return false;
287
288     case COND_EXPR:
289       if (block_may_fallthru (COND_EXPR_THEN (stmt)))
290         return true;
291       return block_may_fallthru (COND_EXPR_ELSE (stmt));
292
293     case BIND_EXPR:
294       return block_may_fallthru (BIND_EXPR_BODY (stmt));
295
296     case TRY_FINALLY_EXPR:
297       return block_may_fallthru (TREE_OPERAND (stmt, 1));
298
299     case MODIFY_EXPR:
300       if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
301         stmt = TREE_OPERAND (stmt, 1);
302       else
303         return true;
304       /* FALLTHRU */
305
306     case CALL_EXPR:
307       /* Functions that do not return do not fall through.  */
308       return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
309
310     default:
311       return true;
312     }
313 }
314
315 /* Lowers a cond_expr TSI.  DATA is passed through the recursion.  */
316
317 static void
318 lower_cond_expr (tree_stmt_iterator *tsi, struct lower_data *data)
319 {
320   tree stmt = tsi_stmt (*tsi);
321   bool then_is_goto, else_is_goto;
322   tree then_branch, else_branch;
323   tree then_goto, else_goto;
324   
325   then_branch = COND_EXPR_THEN (stmt);
326   else_branch = COND_EXPR_ELSE (stmt);
327
328   lower_stmt_body (then_branch, data);
329   lower_stmt_body (else_branch, data);
330
331   then_goto = expr_only (then_branch);
332   then_is_goto = then_goto && simple_goto_p (then_goto);
333
334   else_goto = expr_only (else_branch);
335   else_is_goto = else_goto && simple_goto_p (else_goto);
336
337   if (!then_is_goto || !else_is_goto)
338     {
339       tree then_label, else_label, end_label, t;
340
341       then_label = NULL_TREE;
342       else_label = NULL_TREE;
343       end_label = NULL_TREE;
344  
345       /* Replace the cond_expr with explicit gotos.  */
346       if (!then_is_goto)
347         {
348           t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
349           if (TREE_SIDE_EFFECTS (then_branch))
350             then_label = t;
351           else
352             end_label = t;
353           then_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
354         }
355
356       if (!else_is_goto)
357         {
358           t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
359           if (TREE_SIDE_EFFECTS (else_branch))
360             else_label = t;
361           else
362             {
363               /* Both THEN and ELSE can be no-ops if one or both contained an
364                  empty BIND_EXPR that was associated with the toplevel block
365                  of an inlined function.  In that case remove_useless_stmts
366                  can't have cleaned things up for us; kill the whole 
367                  conditional now.  */
368               if (end_label)
369                 {
370                   tsi_delink (tsi);
371                   return;
372                 }
373               else
374                 end_label = t;
375             }
376           else_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
377         }
378
379       if (then_label)
380         {
381           bool may_fallthru = block_may_fallthru (then_branch);
382
383           tsi_link_after (tsi, then_label, TSI_CONTINUE_LINKING);
384           tsi_link_after (tsi, then_branch, TSI_CONTINUE_LINKING);
385   
386           if (else_label && may_fallthru)
387             {
388               end_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
389               t = build_and_jump (&LABEL_EXPR_LABEL (end_label));
390               tsi_link_after (tsi, t, TSI_CONTINUE_LINKING);
391             }
392         }
393   
394       if (else_label)
395         {
396           tsi_link_after (tsi, else_label, TSI_CONTINUE_LINKING);
397           tsi_link_after (tsi, else_branch, TSI_CONTINUE_LINKING);
398         }
399
400       if (end_label)
401         tsi_link_after (tsi, end_label, TSI_CONTINUE_LINKING);
402     }
403
404   COND_EXPR_THEN (stmt) = then_goto;
405   COND_EXPR_ELSE (stmt) = else_goto;
406
407   tsi_next (tsi);
408 }
409
410 static void
411 lower_return_expr (tree_stmt_iterator *tsi, struct lower_data *data)
412 {
413   tree stmt = tsi_stmt (*tsi);
414   tree value, t, label;
415
416   /* Extract the value being returned.  */
417   value = TREE_OPERAND (stmt, 0);
418   if (value && TREE_CODE (value) == MODIFY_EXPR)
419     value = TREE_OPERAND (value, 1);
420
421   /* Match this up with an existing return statement that's been created.  */
422   for (t = data->return_statements; t ; t = TREE_CHAIN (t))
423     {
424       tree tvalue = TREE_OPERAND (TREE_VALUE (t), 0);
425       if (tvalue && TREE_CODE (tvalue) == MODIFY_EXPR)
426         tvalue = TREE_OPERAND (tvalue, 1);
427
428       if (value == tvalue)
429         {
430           label = TREE_PURPOSE (t);
431           goto found;
432         }
433     }
434
435   /* Not found.  Create a new label and record the return statement.  */
436   label = create_artificial_label ();
437   data->return_statements = tree_cons (label, stmt, data->return_statements);
438
439   /* Generate a goto statement and remove the return statement.  */
440  found:
441   t = build (GOTO_EXPR, void_type_node, label);
442   SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
443   tsi_link_before (tsi, t, TSI_SAME_STMT);
444   tsi_delink (tsi);
445 }
446 \f
447
448 /* Record the variables in VARS.  */
449
450 void
451 record_vars (tree vars)
452 {
453   for (; vars; vars = TREE_CHAIN (vars))
454     {
455       tree var = vars;
456
457       /* Nothing to do in this case.  */
458       if (DECL_EXTERNAL (var))
459         continue;
460       if (TREE_CODE (var) == FUNCTION_DECL)
461         continue;
462
463       /* Record the variable.  */
464       cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
465                                              cfun->unexpanded_var_list);
466     }
467 }
468
469 /* Check whether to expand a variable VAR.  */
470
471 static bool
472 expand_var_p (tree var)
473 {
474   struct var_ann_d *ann;
475
476   if (TREE_CODE (var) != VAR_DECL)
477     return true;
478
479   /* Remove all unused, unaliased temporaries.  Also remove unused, unaliased
480      local variables during highly optimizing compilations.  */
481   ann = var_ann (var);
482   if (ann
483       && ! ann->may_aliases
484       && ! ann->used
485       && ! TREE_ADDRESSABLE (var)
486       && ! TREE_THIS_VOLATILE (var)
487       && (DECL_ARTIFICIAL (var) || optimize >= 2))
488     return false;
489
490   return true;
491 }
492
493 /* Throw away variables that are unused.  */
494
495 static void
496 remove_useless_vars (void)
497 {
498   tree var, *cell;
499
500   for (cell = &cfun->unexpanded_var_list; *cell; )
501     {
502       var = TREE_VALUE (*cell);
503
504       if (!expand_var_p (var))
505         {
506           *cell = TREE_CHAIN (*cell);
507           continue;
508         }
509
510       cell = &TREE_CHAIN (*cell);
511     }
512 }
513
514 /* Expand variables in the unexpanded_var_list.  */
515
516 void
517 expand_used_vars (void)
518 {
519   tree cell;
520
521   cfun->unexpanded_var_list = nreverse (cfun->unexpanded_var_list);
522
523   for (cell = cfun->unexpanded_var_list; cell; cell = TREE_CHAIN (cell))
524     expand_var (TREE_VALUE (cell));
525
526   cfun->unexpanded_var_list = NULL_TREE;
527 }
528
529 struct tree_opt_pass pass_remove_useless_vars = 
530 {
531   "vars",                               /* name */
532   NULL,                                 /* gate */
533   remove_useless_vars,                  /* execute */
534   NULL,                                 /* sub */
535   NULL,                                 /* next */
536   0,                                    /* static_pass_number */
537   0,                                    /* tv_id */
538   0,                                    /* properties_required */
539   0,                                    /* properties_provided */
540   0,                                    /* properties_destroyed */
541   0,                                    /* todo_flags_start */
542   TODO_dump_func                        /* todo_flags_finish */
543 };