aboutsummaryrefslogtreecommitdiff
path: root/gcc/tree-optimize.c
blob: e0a49fc4319d9fa1db97e373422f7a01660fa592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* Control and data flow functions for trees.
   Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
   Contributed by Diego Novillo <dnovillo@redhat.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "rtl.h"
#include "tm_p.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "output.h"
#include "expr.h"
#include "diagnostic.h"
#include "basic-block.h"
#include "flags.h"
#include "tree-flow.h"
#include "tree-dump.h"
#include "timevar.h"
#include "function.h"
#include "langhooks.h"
#include "toplev.h"
#include "flags.h"
#include "cgraph.h"
#include "tree-inline.h"
#include "tree-mudflap.h"
#include "ggc.h"

/* Rewrite a function tree to the SSA form and perform the SSA-based
   optimizations on it.  */

/* Main entry point to the tree SSA transformation routines.  FNDECL is the
   FUNCTION_DECL node for the function to optimize.  */
void
optimize_function_tree (tree fndecl)
{
  tree fnbody;
  struct tree_container head;
  tree_cell first, last;

  /* Don't bother doing anything if the program has errors.  */
  if (errorcount || sorrycount)
    return;

  lower_function_body (&DECL_SAVED_TREE (fndecl));
  fnbody = DECL_SAVED_TREE (fndecl);

  /* Avoid producing notes for blocks.  */
  cfun->dont_emit_block_notes = 1;
  reset_block_changes ();

  /* Flatten the trees.  */
  head.prev = head.next = NULL;
  last = &head;
  tree_flatten_statement (fnbody, &last, NULL_TREE);
  first = head.next;

  if (!first)
    {
      /* The function is empty, we are done.  */
      DECL_SAVED_TREE (fndecl) = build_empty_stmt ();
      return;
    }

  first->prev = NULL;

  /* Build the flowgraph.  */
  init_flow ();
  build_tree_cfg (first);

  /* Begin analysis and optimization passes.  After the function is
     initially renamed into SSA form, passes are responsible from keeping
     it in SSA form.  If a pass exposes new symbols or invalidates the SSA
     numbering for existing variables, it should add them to the
     VARS_TO_RENAME bitmap and call rewrite_into_ssa() afterwards.  */
  if (n_basic_blocks > 0 && ! (errorcount || sorrycount))
    {
      sbitmap vars_to_rename;

      /* Initialize common SSA structures.  */
      init_tree_ssa ();

      /* Find all the variables referenced in the function.  */
      find_referenced_vars (fndecl);
      assign_vars_to_scopes ();

      /* Compute aliasing information for all the variables referenced in
	 the function.  */
      compute_may_aliases (fndecl);


      /*			BEGIN SSA PASSES

	 IMPORTANT: If you change the order in which these passes are
		    executed, you also need to change the enum
		    TREE_DUMP_INDEX in tree.h and DUMP_FILES in
                    tree-dump.c.  */


      /* Rewrite the function into SSA form.  Initially, request all
	 variables to be renamed.  */
      rewrite_into_ssa (fndecl, NULL, TDI_ssa_1);

      /* Set up VARS_TO_RENAME to allow passes to inform which variables
	 need to be renamed.  */
      vars_to_rename = sbitmap_alloc (num_referenced_vars);

      /* Perform dominator optimizations.  */
      if (flag_tree_dom)
	{
	  sbitmap_zero (vars_to_rename);
	  tree_ssa_dominator_optimize (fndecl, vars_to_rename, TDI_dom_1);

	  /* If the dominator optimizations exposed new variables, we need
	      to repeat the SSA renaming process for those symbols.  */
	  if (sbitmap_first_set_bit (vars_to_rename) >= 0)
	    rewrite_into_ssa (fndecl, vars_to_rename, TDI_ssa_2);
	}

      /* Do a first DCE pass prior to must-alias.  This pass will remove
	 dead pointer assignments taking the address of local variables.  */
      if (flag_tree_dce)
	tree_ssa_dce (fndecl, TDI_dce_1);

      /* The must-alias pass removes the aliasing and addressability bits
	 from variables that used to have their address taken.  */
      if (flag_tree_must_alias)
	{
	  sbitmap_zero (vars_to_rename);
	  tree_compute_must_alias (fndecl, vars_to_rename, TDI_mustalias);

	  /* Run the SSA pass again if we need to rename new variables.  */
	  if (sbitmap_first_set_bit (vars_to_rename) >= 0)
	    rewrite_into_ssa (fndecl, vars_to_rename, TDI_ssa_3);
	}

      /* Run SCCP (Sparse Conditional Constant Propagation).  */
      if (flag_tree_ccp)
	{
	  sbitmap_zero (vars_to_rename);
	  tree_ssa_ccp (fndecl, vars_to_rename, TDI_ccp);

	  /* Run the SSA pass again if we need to rename new variables.  */
	  if (sbitmap_first_set_bit (vars_to_rename) >= 0)
	    rewrite_into_ssa (fndecl, vars_to_rename, TDI_ssa_4);
	}

      /* Run SSA-PRE (Partial Redundancy Elimination).  */
      if (flag_tree_pre)
	tree_perform_ssapre (fndecl, TDI_pre);

      /* Perform a second pass of dominator optimizations.  */
      if (flag_tree_dom)
	{
	  sbitmap_zero (vars_to_rename);
	  tree_ssa_dominator_optimize (fndecl, vars_to_rename, TDI_dom_2);

	  /* There should not be any new symbols exposed.  */
	  if (sbitmap_first_set_bit (vars_to_rename) >= 0)
	    abort ();
	}

      /* Run copy propagation.  */
      if (flag_tree_copyprop)
	tree_ssa_copyprop (fndecl, TDI_copyprop);

      /* Do a second DCE pass.  */
      if (flag_tree_dce)
	tree_ssa_dce (fndecl, TDI_dce_2);

#if defined ENABLE_CHECKING
      verify_flow_info ();
#endif

      /* Rewrite the function out of SSA form.  */
      rewrite_out_of_ssa (fndecl, TDI_optimized);

      sbitmap_free (vars_to_rename);
    }

  fnbody = tree_unflatten_statements ();
  DECL_SAVED_TREE (fndecl) = fnbody;

  /* Debugging dump after optimization.  */
  dump_function (TDI_optimized, fndecl);
}


/* Called to move the SAVE_EXPRs for parameter declarations in a
   nested function into the nested function.  DATA is really the
   nested FUNCTION_DECL.  */

static tree
set_save_expr_context (tree *tp,
		       int *walk_subtrees,
		       void *data)
{
  if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
    SAVE_EXPR_CONTEXT (*tp) = (tree) data;
  /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
     circularity.  */
  else if (DECL_P (*tp))
    *walk_subtrees = 0;

  return NULL;
}

/* Clear out the DECL_RTL for the non-static local variables in BLOCK and
   its sub-blocks.  DATA is the decl of the function being processed.  */

static tree
clear_decl_rtl (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
{
  bool nonstatic_p, local_p;
  tree t = *tp;

  switch (TREE_CODE (t))
    {
    case VAR_DECL:
      nonstatic_p = !TREE_STATIC (t) && !DECL_EXTERNAL (t);
      local_p = DECL_CONTEXT (t) == data;
      break;

    case PARM_DECL:
    case LABEL_DECL:
      nonstatic_p = true;
      local_p = DECL_CONTEXT (t) == data;
      break;

    case RESULT_DECL:
      nonstatic_p = local_p = true;
      break;

    default:
      nonstatic_p = local_p = false;
      break;
    }

  if (nonstatic_p && local_p)
    SET_DECL_RTL (t, NULL);

  return NULL;
}

/* For functions-as-trees languages, this performs all optimization and
   compilation for FNDECL.  */

void
tree_rest_of_compilation (tree fndecl, bool nested_p)
{
  location_t saved_loc;
  tree saved_tree = NULL;

  /* Don't bother doing anything if there are errors.  */
  if (errorcount || sorrycount)
    {
      TREE_ASM_WRITTEN (fndecl) = 1;
      return;
    }

  timevar_push (TV_EXPAND);

  if (flag_unit_at_a_time && !cgraph_global_info_ready)
    abort ();

  /* Initialize the RTL code for the function.  */
  current_function_decl = fndecl;
  saved_loc = input_location;
  input_location = DECL_SOURCE_LOCATION (fndecl);
  init_function_start (fndecl);

  /* This function is being processed in whole-function mode.  */
  cfun->x_whole_function_mode_p = 1;

  /* Even though we're inside a function body, we still don't want to
     call expand_expr to calculate the size of a variable-sized array.
     We haven't necessarily assigned RTL to all variables yet, so it's
     not safe to try to expand expressions involving them.  */
  immediate_size_expand = 0;
  cfun->x_dont_save_pending_sizes_p = 1;

  /* We might need the body of this function so that we can expand
     it inline somewhere else.  This means not lowering some constructs
     such as exception handling.  */
  if (DECL_INLINE (fndecl) && flag_inline_trees)
    {
      saved_tree = lhd_unsave_expr_now (DECL_SAVED_TREE (fndecl));
      /* ??? We're saving this value here on the stack.  Don't gc it.  */
      nested_p = true;
    }

  /* Gimplify the function.  Don't try to optimize the function if
     gimplification failed.  */
  if (!flag_disable_gimple
      && (keep_function_tree_in_gimple_form (fndecl)
          || gimplify_function_tree (fndecl)))
    {
      /* Debugging dump after gimplification.  */
      dump_function (TDI_gimple, fndecl);

      {
	int flags;
	FILE *file = dump_begin (TDI_useless, &flags);
	if (file)
	  {
	    dump_function_to_file (fndecl, file, flags);
	    dump_end (TDI_useless, file);
	  }
      }

      /* Run a pass to lower magic exception handling constructs into,
	 well, less magic though not completely mundane constructs.  */
      lower_eh_constructs (&DECL_SAVED_TREE (fndecl));

      /* Invoke the SSA tree optimizer.  */
      if (optimize >= 1 && !flag_disable_tree_ssa)
        optimize_function_tree (fndecl);
    }

  /* If the function has a variably modified type, there may be
     SAVE_EXPRs in the parameter types.  Their context must be set to
     refer to this function; they cannot be expanded in the containing
     function.  */
  if (decl_function_context (fndecl)
      && variably_modified_type_p (TREE_TYPE (fndecl)))
    walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
	       NULL);

  /* Set up parameters and prepare for return, for the function.  */
  expand_function_start (fndecl, 0);

  /* Allow language dialects to perform special processing.  */
  (*lang_hooks.rtl_expand.start) ();

  /* If this function is `main', emit a call to `__main'
     to run global initializers, etc.  */
  if (DECL_NAME (fndecl)
      && MAIN_NAME_P (DECL_NAME (fndecl))
      && DECL_FILE_SCOPE_P (fndecl))
    expand_main_function ();

  /* Add mudflap instrumentation to a copy of the function body.  */
  if (flag_mudflap)
    DECL_SAVED_TREE (fndecl) = mudflap_c_function (fndecl);

  /* Generate the RTL for this function.  */
  (*lang_hooks.rtl_expand.stmt) (DECL_SAVED_TREE (fndecl));

  /* We hard-wired immediate_size_expand to zero above.
     expand_function_end will decrement this variable.  So, we set the
     variable to one here, so that after the decrement it will remain
     zero.  */
  immediate_size_expand = 1;

  /* Make sure the locus is set to the end of the function, so that 
     epilogue line numbers and warnings are set properly.  */
  if (cfun->function_end_locus.file)
    input_location = cfun->function_end_locus;

  /* Allow language dialects to perform special processing.  */
  (*lang_hooks.rtl_expand.end) ();

  /* Generate rtl for function exit.  */
  expand_function_end ();

  /* If this is a nested function, protect the local variables in the stack
     above us from being collected while we're compiling this function.  */
  if (nested_p)
    ggc_push_context ();

  /* There's no need to defer outputting this function any more; we
     know we want to output it.  */
  DECL_DEFER_OUTPUT (fndecl) = 0;

  /* Run the optimizers and output the assembler code for this function.  */
  rest_of_compilation (fndecl);

  /* Undo the GC context switch.  */
  if (nested_p)
    ggc_pop_context ();

  /* If requested, warn about function definitions where the function will
     return a value (usually of some struct or union type) which itself will
     take up a lot of stack space.  */
  if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
    {
      tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));

      if (ret_type && TYPE_SIZE_UNIT (ret_type)
	  && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
	  && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
				   larger_than_size))
	{
	  unsigned int size_as_int
	    = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));

	  if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
	    warning ("%Jsize of return value of '%D' is %u bytes",
                     fndecl, fndecl, size_as_int);
	  else
	    warning ("%Jsize of return value of '%D' is larger than %wd bytes",
                     fndecl, fndecl, larger_than_size);
	}
    }

  /* ??? Looks like some of this could be combined.  */

  if (DECL_INLINE (fndecl) && flag_inline_trees)
    {
      /* We might need the body of this function so that we can expand
         it inline somewhere else.  */
      DECL_SAVED_TREE (fndecl) = saved_tree;
    }

  /* If possible, obliterate the body of the function so that it can
     be garbage collected.  */
  else if (dump_enabled_p (TDI_all))
    /* Keep the body; we're going to dump it.  */
    ;
  else
    /* We don't need the body; blow it away.  */
    DECL_SAVED_TREE (fndecl) = NULL;

  /* Since we don't need the RTL for this function anymore, stop pointing to
     it.  That's especially important for LABEL_DECLs, since you can reach all
     the instructions in the function from the CODE_LABEL stored in the
     DECL_RTL for the LABEL_DECL.  Walk the BLOCK-tree, clearing DECL_RTL for
     LABEL_DECLs and non-static local variables.  Note that we must check the
     context of the variables, otherwise processing a nested function can kill
     the rtl of a variable from an outer function.  */
  walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
				clear_decl_rtl,
				fndecl);

  if (DECL_SAVED_INSNS (fndecl) == 0 && !nested_p && !flag_inline_trees)
    {
      /* Stop pointing to the local nodes about to be freed.
	 But DECL_INITIAL must remain nonzero so we know this
	 was an actual function definition.
	 For a nested function, this is done in c_pop_function_context.
	 If rest_of_compilation set this to 0, leave it 0.  */
      if (DECL_INITIAL (fndecl) != 0)
	DECL_INITIAL (fndecl) = error_mark_node;

      DECL_ARGUMENTS (fndecl) = 0;
    }

  input_location = saved_loc;

  timevar_pop (TV_EXPAND);
}