opt_funccall() now takes an additional parameter
[cparser] / driver / firm_opt.c
1 /**
2  *
3  * @file firm_opt.c -- Firm-generating back end optimizations.
4  *
5  * (C) 2005-2007  Michael Beck   beck@ipd.info.uni-karlsruhe.de
6  *
7  * $Id$
8  */
9 #include <stdlib.h>
10 #include <string.h>
11 #include <libfirm/firm.h>
12
13 #ifdef FIRM_BACKEND
14 #include <libfirm/be.h>
15 #endif /* FIRM_BACKEND */
16
17
18 #include "firm_opt.h"
19 #include "firm_codegen.h"
20 #include "firm_cmdline.h"
21 #include "firm_timing.h"
22
23 #if defined(_DEBUG) || defined(FIRM_DEBUG)
24 #define DBG(x)  dbg_printf x
25 #else
26 #define DBG(x)
27 #endif /* _DEBUG || FIRM_DEBUG */
28
29
30 /** dump all the graphs depending on cond */
31 #define DUMP_ALL(cond, suffix)                             \
32   do {                                                     \
33     if (cond) {                                            \
34       timer_push(TV_VCG_DUMP);                             \
35       if (firm_dump.no_blocks)                             \
36         dump_all_ir_graphs(dump_ir_graph, suffix);         \
37       else if (firm_dump.extbb)                            \
38         dump_all_ir_graphs(dump_ir_extblock_graph, suffix);\
39       else                                                 \
40         dump_all_ir_graphs(dump_ir_block_graph, suffix);   \
41       timer_pop();                                         \
42     }                                                      \
43   } while (0)
44
45 /** dump all control flow graphs depending on cond */
46 #define DUMP_ALL_CFG(cond, suffix)                      \
47   do {                                                  \
48     if (cond) {                                         \
49       timer_push(TV_VCG_DUMP);                          \
50         dump_all_ir_graphs(dump_cfg, suffix);           \
51       timer_pop();                                      \
52     }                                                   \
53   } while (0)
54
55 /** check all graphs depending on cond */
56 #define CHECK_ALL(cond)                                 \
57   do {                                                  \
58     if (cond) {                                         \
59       int ii;                                           \
60       timer_push(TV_VERIFY);                            \
61       for (ii = get_irp_n_irgs() - 1; ii >= 0; --ii)    \
62         irg_verify(get_irp_irg(ii), VRFY_ENFORCE_SSA);  \
63       timer_pop();                                      \
64     }                                                   \
65   } while (0)
66
67
68
69 /** dump graphs irg depending on cond */
70 #define DUMP_ONE(cond, irg, suffix)                     \
71   do {                                                  \
72     if (cond) {                                         \
73       timer_push(TV_VCG_DUMP);                          \
74       if (firm_dump.no_blocks)                          \
75         dump_ir_graph(irg, suffix);                     \
76       else if (firm_dump.extbb)                         \
77         dump_ir_extblock_graph(irg, suffix);            \
78       else                                              \
79         dump_ir_block_graph(irg, suffix);               \
80       timer_pop();                                      \
81     }                                                   \
82   } while (0)
83
84 /** dump control flow graph irg depending on cond */
85 #define DUMP_ONE_CFG(cond, irg, suffix)                 \
86   do {                                                  \
87     if (cond) {                                         \
88       timer_push(TV_VCG_DUMP);                          \
89       dump_cfg(irg, suffix);                            \
90       timer_pop();                                      \
91     }                                                   \
92   } while (0)
93
94 /** check a graph irg depending on cond */
95 #define CHECK_ONE(cond, irg)                            \
96   do {                                                  \
97     if (cond) {                                         \
98       timer_push(TV_VERIFY);                            \
99         irg_verify(irg, VRFY_ENFORCE_SSA);              \
100       timer_pop();                                      \
101     }                                                   \
102   } while (0)
103
104
105 /* set by the backend parameters */
106 static const ir_settings_arch_dep_t *ad_param = NULL;
107 static create_intrinsic_fkt *arch_create_intrinsic = NULL;
108 static void *create_intrinsic_ctx = NULL;
109 static const ir_settings_if_conv_t *if_conv_info = NULL;
110
111 /* entities of runtime functions */
112 ir_entity_ptr rts_entities[rts_max];
113
114 /**
115  * factory for setting architecture dependent parameters
116  */
117 static const ir_settings_arch_dep_t *arch_factory(void)
118 {
119   static const ir_settings_arch_dep_t param = {
120       1,   /* also use subs */
121       4,   /* maximum shifts */
122      31,   /* maximum shift amount */
123      NULL, /* use default evaluator */
124
125       1, /* allow Mulhs */
126       1, /* allow Mulus */
127      32  /* Mulh allowed up to 32 bit */
128   };
129
130   return ad_param ? ad_param : &param;
131 }
132
133 /**
134  * Map runtime functions.
135  */
136 static void rts_map(void) {
137   static const struct {
138     ir_entity_ptr *ent; /**< address of the rts entity */
139     i_mapper_func func; /**< mapper function. */
140   } mapper[] = {
141     /* integer */
142     { &rts_entities[rts_abs],     i_mapper_abs },
143     { &rts_entities[rts_labs],    i_mapper_abs },
144     { &rts_entities[rts_llabs],   i_mapper_abs },
145     { &rts_entities[rts_imaxabs], i_mapper_abs },
146
147     /* double -> double */
148     { &rts_entities[rts_fabs],    i_mapper_abs },
149     { &rts_entities[rts_sqrt],    i_mapper_sqrt },
150     { &rts_entities[rts_cbrt],    i_mapper_cbrt },
151     { &rts_entities[rts_pow],     i_mapper_pow },
152     { &rts_entities[rts_exp],     i_mapper_exp },
153     { &rts_entities[rts_exp2],    i_mapper_exp },
154     { &rts_entities[rts_exp10],   i_mapper_exp },
155     { &rts_entities[rts_log],     i_mapper_log },
156     { &rts_entities[rts_log2],    i_mapper_log2 },
157     { &rts_entities[rts_log10],   i_mapper_log10 },
158     { &rts_entities[rts_sin],     i_mapper_sin },
159     { &rts_entities[rts_cos],     i_mapper_cos },
160     { &rts_entities[rts_tan],     i_mapper_tan },
161     { &rts_entities[rts_asin],    i_mapper_asin },
162     { &rts_entities[rts_acos],    i_mapper_acos },
163     { &rts_entities[rts_atan],    i_mapper_atan },
164     { &rts_entities[rts_sinh],    i_mapper_sinh },
165     { &rts_entities[rts_cosh],    i_mapper_cosh },
166     { &rts_entities[rts_tanh],    i_mapper_tanh },
167
168     /* float -> float */
169     { &rts_entities[rts_fabsf],   i_mapper_abs },
170     { &rts_entities[rts_sqrtf],   i_mapper_sqrt },
171     { &rts_entities[rts_cbrtf],   i_mapper_cbrt },
172     { &rts_entities[rts_powf],    i_mapper_pow },
173     { &rts_entities[rts_expf],    i_mapper_exp },
174     { &rts_entities[rts_exp2f],   i_mapper_exp },
175     { &rts_entities[rts_exp10f],  i_mapper_exp },
176     { &rts_entities[rts_logf],    i_mapper_log },
177     { &rts_entities[rts_log2f],   i_mapper_log2 },
178     { &rts_entities[rts_log10f],  i_mapper_log10 },
179     { &rts_entities[rts_sinf],    i_mapper_sin },
180     { &rts_entities[rts_cosf],    i_mapper_cos },
181     { &rts_entities[rts_tanf],    i_mapper_tan },
182     { &rts_entities[rts_asinf],   i_mapper_asin },
183     { &rts_entities[rts_acosf],   i_mapper_acos },
184     { &rts_entities[rts_atanf],   i_mapper_atan },
185     { &rts_entities[rts_sinhf],   i_mapper_sinh },
186     { &rts_entities[rts_coshf],   i_mapper_cosh },
187     { &rts_entities[rts_tanhf],   i_mapper_tanh },
188
189     /* long double -> long double */
190     { &rts_entities[rts_fabsl],   i_mapper_abs },
191     { &rts_entities[rts_sqrtl],   i_mapper_sqrt },
192     { &rts_entities[rts_cbrtl],   i_mapper_cbrt },
193     { &rts_entities[rts_powl],    i_mapper_pow },
194     { &rts_entities[rts_expl],    i_mapper_exp },
195     { &rts_entities[rts_exp2l],   i_mapper_exp },
196     { &rts_entities[rts_exp10l],  i_mapper_exp },
197     { &rts_entities[rts_logl],    i_mapper_log },
198     { &rts_entities[rts_log2l],   i_mapper_log2 },
199     { &rts_entities[rts_log10l],  i_mapper_log10 },
200     { &rts_entities[rts_sinl],    i_mapper_sin },
201     { &rts_entities[rts_cosl],    i_mapper_cos },
202     { &rts_entities[rts_tanl],    i_mapper_tan },
203     { &rts_entities[rts_asinl],   i_mapper_asin },
204     { &rts_entities[rts_acosl],   i_mapper_acos },
205     { &rts_entities[rts_atanl],   i_mapper_atan },
206     { &rts_entities[rts_sinhl],   i_mapper_sinh },
207     { &rts_entities[rts_coshl],   i_mapper_cosh },
208     { &rts_entities[rts_tanhl],   i_mapper_tanh },
209
210     /* string */
211     { &rts_entities[rts_memcpy],  i_mapper_memcpy },
212     { &rts_entities[rts_memset],  i_mapper_memset },
213     { &rts_entities[rts_strcmp],  i_mapper_strcmp },
214     { &rts_entities[rts_strncmp], i_mapper_strncmp },
215     { &rts_entities[rts_strlen],  i_mapper_strlen }
216   };
217   i_record rec[sizeof(mapper)/sizeof(mapper[0])];
218   unsigned i, n_map;
219
220   for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i)
221     if (*mapper[i].ent != NULL) {
222       rec[n_map].i_call.kind     = INTRINSIC_CALL;
223       rec[n_map].i_call.i_ent    = *mapper[i].ent;
224       rec[n_map].i_call.i_mapper = mapper[i].func;
225       rec[n_map].i_call.ctx      = NULL;
226       rec[n_map].i_call.link     = NULL;
227       ++n_map;
228   }  /* if */
229   if (n_map > 0)
230     lower_intrinsics(rec, n_map, /* part_block_used=*/0);
231 }  /* rts_map */
232
233 static int *irg_dump_no;
234
235 static void dump_graph_count(ir_graph *const irg, const char *const suffix)
236 {
237   char name[64];
238   snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++, suffix);
239   DUMP_ONE(1, irg, name);
240 }
241
242 static void dump_graph_cfg_count(ir_graph *const irg, const char *const suffix)
243 {
244   char name[64];
245   snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++, suffix);
246   DUMP_ONE_CFG(1, irg, name);
247 }
248
249 static void dump_all_count(const char *const suffix)
250 {
251   const int n_irgs = get_irp_n_irgs();
252   int i;
253
254   for (i = 0; i < n_irgs; ++i)
255     dump_graph_count(get_irp_irg(i), suffix);
256 }
257
258 #define DUMP_ONE_C(cond, irg, suffix)    \
259   do {                                   \
260     if (cond) {                          \
261       dump_graph_count((irg), (suffix)); \
262     }                                    \
263   } while (0)
264
265 #define DUMP_ONE_CFG_C(cond, irg, suffix)    \
266   do {                                       \
267     if (cond) {                              \
268       dump_graph_cfg_count((irg), (suffix)); \
269     }                                        \
270   } while (0)
271
272 #define DUMP_ALL_C(cond, suffix) \
273   do {                           \
274     if (cond) {                  \
275       dump_all_count((suffix));  \
276     }                            \
277   } while (0)
278
279 /**
280  * run all the Firm optimizations
281  *
282  * @param input_filename     the name of the (main) source file
283  * @param firm_const_exists  non-zero, if the const attribute was used on functions
284  */
285 static void do_firm_optimizations(const char *input_filename, int firm_const_exists)
286 {
287   int      i;
288   ir_graph *irg;
289   unsigned aa_opt;
290
291   /* FIXME: cloning might ADD new graphs. */
292   irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
293
294   set_opt_strength_red(firm_opt.strength_red);
295   set_opt_scalar_replacement(firm_opt.scalar_replace);
296   set_opt_auto_create_sync(firm_opt.auto_sync);
297   set_opt_alias_analysis(firm_opt.alias_analysis);
298
299   aa_opt = aa_opt_no_opt;
300   if (firm_opt.strict_alias)
301     aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
302   if (firm_opt.no_alias)
303     aa_opt = aa_opt_no_alias;
304
305   set_irp_memory_disambiguator_options(aa_opt);
306
307   timer_start(TV_ALL_OPT);
308
309   if (firm_opt.remove_unused) {
310         ir_entity **keep_methods;
311         int         arr_len;
312
313     /* Analysis that finds the free methods,
314        i.e. methods that are dereferenced.
315        Optimizes polymorphic calls :-). */
316     cgana(&arr_len, &keep_methods);
317
318     /* Remove methods that are never called. */
319     gc_irgs(arr_len, keep_methods);
320
321     free(keep_methods);
322   }
323
324   if (! firm_opt.freestanding) {
325     rts_map();
326     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "rts");
327     CHECK_ALL(firm_opt.check_all);
328   }
329
330   if (firm_opt.tail_rec) {
331     timer_push(TV_TAIL_REC);
332       opt_tail_recursion();
333     timer_pop();
334
335     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "tail_rec");
336     CHECK_ALL(firm_opt.check_all);
337   }
338
339   if (firm_opt.func_calls) {
340     timer_push(TV_REAL_FUNC_CALL);
341       optimize_funccalls(firm_const_exists, NULL);
342     timer_pop();
343     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "func_call");
344     CHECK_ALL(firm_opt.check_all);
345   }
346
347   if (firm_opt.do_inline) {
348     timer_push(TV_INLINE);
349       inline_leave_functions(500, 80, 30, FALSE);
350     timer_pop();
351     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "inl");
352     CHECK_ALL(firm_opt.check_all);
353   }
354
355   for (i = 0; i < get_irp_n_irgs(); i++) {
356     irg = current_ir_graph = get_irp_irg(i);
357
358
359 #ifdef FIRM_EXT_GRS
360   /* If SIMD optimization is on, make sure we have only 1 return */
361   if (firm_ext_grs.create_pattern || firm_ext_grs.simd_opt)
362     normalize_one_return(irg);
363 #endif
364
365
366 #if 0
367     if (firm_opt.modes) {
368       /* convert all modes into integer if possible */
369       arch_mode_conversion(irg, predefs.mode_uint);
370     }
371 #endif
372     timer_push(TV_SCALAR_REPLACE);
373       scalar_replacement_opt(irg);
374     timer_pop();
375     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "scalar");
376     CHECK_ONE(firm_opt.check_all, irg);
377
378     timer_push(TV_LOCAL_OPT);
379       optimize_graph_df(irg);
380     timer_pop();
381     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
382     CHECK_ONE(firm_opt.check_all, irg);
383
384     timer_push(TV_REASSOCIATION);
385       optimize_reassociation(irg);
386     timer_pop();
387     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "reassoc");
388     CHECK_ONE(firm_opt.check_all, irg);
389
390     if (firm_opt.confirm) {
391       /* Confirm construction currently can only handle blocks with only one control
392          flow predecessor. Calling optimize_cf here removes Bad predecessors and help
393          the optimization of switch constructs. */
394       timer_push(TV_CF_OPT);
395         optimize_cf(irg);
396       timer_pop();
397       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
398       CHECK_ONE(firm_opt.check_all, irg);
399       timer_push(TV_CONFIRM_CREATE);
400         construct_confirms(irg);
401       timer_pop();
402       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "confirms");
403       CHECK_ONE(firm_opt.check_all, irg);
404     }
405
406     timer_push(TV_LOCAL_OPT);
407       optimize_graph_df(irg);
408     timer_pop();
409     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
410     CHECK_ONE(firm_opt.check_all, irg);
411
412     compute_doms(irg);
413     CHECK_ONE(firm_opt.check_all, irg);
414
415     if (firm_opt.code_place) {
416       timer_push(TV_CODE_PLACE);
417         set_opt_global_cse(1);
418         optimize_graph_df(irg);
419         place_code(irg);
420         set_opt_global_cse(0);
421       timer_pop();
422       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "place");
423       CHECK_ONE(firm_opt.check_all, irg);
424     }
425
426     if (firm_opt.luffig) {
427       opt_ldst2(irg);
428       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst2");
429       CHECK_ONE(firm_opt.check_all, irg);
430     }
431
432     timer_push(TV_CF_OPT);
433       optimize_cf(irg);
434     timer_pop();
435     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
436     CHECK_ONE(firm_opt.check_all, irg);
437
438     /* should we really remove the Confirm here? */
439     if (firm_opt.confirm) {
440       timer_push(TV_CONFIRM_CREATE);
441         remove_confirms(irg);
442       timer_pop();
443     }
444
445     irg_verify(irg, VRFY_ENFORCE_SSA);
446     if (firm_opt.gvn_pre) {
447       do_gvn_pre(irg);
448       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "pre");
449       CHECK_ONE(firm_opt.check_all, irg);
450       irg_verify(irg, VRFY_ENFORCE_SSA);
451     }
452
453     if (firm_opt.loop_unrolling) {
454       timer_push(TV_LOOP_UNROLL);
455         optimize_loop_unrolling(irg);
456       timer_pop();
457       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "loop");
458       CHECK_ONE(firm_opt.check_all, irg);
459         }
460
461     if (firm_opt.load_store) {
462       timer_push(TV_LOAD_STORE);
463         optimize_load_store(irg);
464       timer_pop();
465       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst");
466       CHECK_ONE(firm_opt.check_all, irg);
467         }
468
469     lower_highlevel_graph(irg, firm_opt.lower_bitfields);
470
471     if (firm_opt.deconv) {
472       timer_push(TV_DECONV);
473         conv_opt(irg);
474       timer_pop();
475       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "deconv");
476       CHECK_ONE(firm_opt.check_all, irg);
477     }
478
479     if (firm_opt.cond_eval) {
480       timer_push(TV_COND_EVAL);
481         opt_cond_eval(irg);
482       timer_pop();
483       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cond_eval");
484       CHECK_ONE(firm_opt.check_all, irg);
485     }
486
487     compute_doms(irg);
488     compute_postdoms(irg);
489     DUMP_ONE_CFG_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dom");
490     CHECK_ONE(firm_opt.check_all, irg);
491
492     construct_backedges(irg);
493
494     timer_push(TV_CF_OPT);
495       optimize_cf(irg);
496     timer_pop();
497     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
498     CHECK_ONE(firm_opt.check_all, irg);
499
500     if (firm_opt.if_conversion) {
501       timer_push(TV_IF_CONV);
502         opt_if_conv(current_ir_graph, if_conv_info);
503       timer_pop();
504       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "if");
505       CHECK_ONE(firm_opt.check_all, current_ir_graph);
506
507       timer_push(TV_LOCAL_OPT);
508         optimize_graph_df(current_ir_graph);
509       timer_pop();
510       timer_push(TV_CF_OPT);
511         optimize_cf(current_ir_graph);
512       timer_pop();
513       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "after_if");
514       CHECK_ONE(firm_opt.check_all, current_ir_graph);
515     }
516
517     if (firm_opt.bool_opt) {
518       opt_bool(irg);
519       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "bool");
520       CHECK_ONE(firm_opt.check_all, irg);
521     }
522
523     timer_push(TV_OSR);
524       opt_osr(current_ir_graph, osr_flag_default /*| osr_flag_ignore_x86_shift*/);
525     timer_pop();
526     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "stred");
527     CHECK_ONE(firm_opt.check_all, irg);
528
529     timer_push(TV_LOCAL_OPT);
530       optimize_graph_df(irg);
531     timer_pop();
532     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
533     CHECK_ONE(firm_opt.check_all, irg);
534
535     edges_deactivate(irg);
536     timer_push(TV_DEAD_NODE);
537       dead_node_elimination(irg);
538     timer_pop();
539     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dead");
540     CHECK_ONE(firm_opt.check_all, irg);
541   }
542
543   if (firm_opt.cloning) {
544     proc_cloning((float)firm_opt.clone_threshold);
545     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "clone");
546     CHECK_ALL(firm_opt.check_all);
547   }
548
549   if (firm_dump.ir_graph) {
550     /* recompute backedges for nicer dumps */
551     for (i = 0; i < get_irp_n_irgs(); i++)
552       construct_cf_backedges(get_irp_irg(i));
553   }
554
555   if (firm_opt.remove_unused) {
556     ir_entity **keep_methods;
557     int arr_len;
558
559     /* Analysis that finds the free methods,
560        i.e. methods that are dereferenced.
561        Optimizes polymorphic calls :-). */
562     cgana(&arr_len, &keep_methods);
563
564     /* Remove methods that are never called. */
565     gc_irgs(arr_len, keep_methods);
566
567     free(keep_methods);
568   }
569
570
571   DUMP_ALL(firm_dump.ir_graph, "-opt");
572
573   /* verify optimized graphs */
574   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
575     irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
576
577   if (firm_dump.statistic & STAT_AFTER_OPT)
578     stat_dump_snapshot(input_filename, "opt");
579
580   timer_stop(TV_ALL_OPT);
581 }  /* do_firm_optimizations */
582
583 /**
584  * compute the size of a type (do implicit lowering)
585  *
586  * @param ty   a Firm type
587  */
588 static int compute_type_size(ir_type *ty)
589 {
590   optimization_state_t state;
591   unsigned             align_all = 1;
592   int                  n, size = 0, set = 0;
593   int                  i, dims, s;
594
595   if (get_type_state(ty) == layout_fixed) {
596     /* do not layout already layouted types again */
597     return 1;
598   }
599
600   if (is_Method_type(ty) || ty == get_glob_type()) {
601     /* no need for size calculation for method types or the global type */
602     return 1;
603   }
604
605   DBG(("compute type size visiting: %s\n", get_type_name(ty)));
606
607   switch (get_type_tpop_code(ty)) {
608   case tpo_class:
609   case tpo_struct:
610     for (i = 0, n = get_compound_n_members(ty); i < n; ++i) {
611       ir_entity *ent  = get_compound_member(ty, i);
612       ir_type *ent_ty = get_entity_type(ent);
613       unsigned align, misalign;
614
615       /* compute member types */
616       if (! compute_type_size(ent_ty))
617         return 0;
618
619       align     = get_type_alignment_bytes(ent_ty);
620       align_all = align > align_all ? align : align_all;
621       misalign  = (align ? size % align : 0);
622       size     += (misalign ? align - misalign : 0);
623
624       set_entity_offset(ent, size);
625       size += get_type_size_bytes(ent_ty);
626
627       DBG(("  member %s %s -> (size: %u, align: %u)\n",
628             get_type_name(ent_ty), get_entity_name(ent),
629             get_type_size_bytes(ent_ty), get_type_alignment_bytes(ent_ty)));
630     }
631     if (align_all > 0 && size % align_all) {
632        DBG(("align of the struct member: %u, type size: %d\n", align_all, size));
633        size += align_all - (size % align_all);
634        DBG(("correcting type-size to %d\n", size));
635     }
636     set_type_alignment_bytes(ty, align_all);
637     set = 1;
638     break;
639
640   case tpo_union:
641     for (i = 0, n = get_union_n_members(ty); i < n; ++i) {
642       ir_entity *ent = get_union_member(ty, i);
643
644       if (! compute_type_size(get_entity_type(ent)))
645         return 0;
646       s = get_type_size_bytes(get_entity_type(ent));
647
648       set_entity_offset(ent, 0);
649       size = (s > size ? s : size);
650     }
651     set = 1;
652     break;
653
654   case tpo_array:
655     dims = get_array_n_dimensions(ty);
656
657     if (! compute_type_size(get_array_element_type(ty)))
658       return 0;
659
660     size = 1;
661
662     save_optimization_state(&state);
663     set_optimize(1);
664     set_opt_constant_folding(1);
665
666     for (i = 0; i < dims; ++i) {
667       ir_node *lower   = get_array_lower_bound(ty, i);
668       ir_node *upper   = get_array_upper_bound(ty, i);
669       ir_graph *rem    = current_ir_graph;
670       tarval  *tv_lower, *tv_upper;
671       long     val_lower, val_upper;
672
673       current_ir_graph = get_const_code_irg();
674       local_optimize_node(lower);
675       local_optimize_node(upper);
676       current_ir_graph = rem;
677
678       tv_lower = computed_value(lower);
679       tv_upper = computed_value(upper);
680
681       if (tv_lower == tarval_bad || tv_upper == tarval_bad) {
682         /*
683          * we cannot calculate the size of this array yet, it
684          * even might be unknown until the end, like argv[]
685          */
686         restore_optimization_state(&state);
687         return 0;
688       }
689
690       val_upper = get_tarval_long(tv_upper);
691       val_lower = get_tarval_long(tv_lower);
692       size     *= val_upper - val_lower;
693     }
694     restore_optimization_state(&state);
695
696     DBG(("array %s -> (elements: %d, element type size: %d)\n",
697           get_type_name(ty),
698           size, get_type_size_bytes(get_array_element_type(ty))));
699     size *= get_type_size_bytes(get_array_element_type(ty));
700     set = 1;
701     break;
702
703   default:
704     break;
705   }
706
707   if (set) {
708     set_type_size_bytes(ty, size);
709     set_type_state(ty, layout_fixed);
710   }
711
712   DBG(("size: %d\n", get_type_size_bytes(ty)));
713
714   return set;
715 }  /* compute_type_size */
716
717 /**
718  * layout all types of the Firm graph
719  */
720 static void compute_type_sizes(void)
721 {
722   int i;
723   ir_type *tp;
724   ir_graph *irg;
725
726   /* all frame types */
727   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
728     irg = get_irp_irg(i);
729     /* do not optimize away variables in debug mode */
730     if (firm_opt.debug_mode == DBG_MODE_NONE)
731       opt_frame_irg(irg);
732     compute_type_size(get_irg_frame_type(irg));
733   }
734
735   /* all other types */
736   for (i = get_irp_n_types() - 1; i >= 0; --i) {
737     tp = get_irp_type(i);
738     compute_type_size(tp);
739
740     if (is_Method_type(tp)) {
741       tp = get_method_value_res_type(tp);
742
743       if (tp) {
744         /* we have a value result type for this method, lower */
745         compute_type_size(tp);
746       }
747     }
748   }
749 }  /* compute_type_sizes */
750
751 /**
752  * do Firm lowering
753  *
754  * @param input_filename  the name of the (main) source file
755  */
756 static void do_firm_lowering(const char *input_filename)
757 {
758   int i;
759
760   /* do class lowering and vtbl creation */
761 //  lower_classes_to_struct("vtbl", "m");
762
763 #if 0
764   timer_push(TV_LOWER);
765   lower_highlevel();
766   timer_pop();
767 #endif
768
769   if (firm_opt.lower_ll) {
770     lwrdw_param_t init = {
771       1,
772       1,
773       mode_Ls, mode_Lu,
774       mode_Is, mode_Iu,
775       def_create_intrinsic_fkt,
776       NULL
777     };
778
779
780     if (arch_create_intrinsic) {
781       init.create_intrinsic = arch_create_intrinsic;
782       init.ctx              = create_intrinsic_ctx;
783     }
784     timer_push(TV_DW_LOWER);
785       lower_dw_ops(&init);
786       DUMP_ALL(firm_dump.ir_graph, "-dw");
787     timer_pop();
788   }
789
790   if (firm_dump.statistic & STAT_AFTER_LOWER)
791     stat_dump_snapshot(input_filename, "low");
792
793   /* verify lowered graphs */
794   timer_push(TV_VERIFY);
795   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
796     irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
797   timer_pop();
798
799   DUMP_ALL(firm_dump.ir_graph, "-low");
800
801   if (firm_opt.enabled) {
802     timer_start(TV_ALL_OPT);
803
804     /* run reassociation first on all graphs BEFORE the architecture dependent optimizations
805        are enabled */
806     for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
807       current_ir_graph = get_irp_irg(i);
808
809       timer_push(TV_REASSOCIATION);
810         optimize_reassociation(current_ir_graph);
811       timer_pop();
812       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "reassoc");
813       CHECK_ONE(firm_opt.check_all, current_ir_graph);
814     }
815
816     for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
817       current_ir_graph = get_irp_irg(i);
818
819       if (firm_opt.code_place)
820         set_opt_global_cse(1);
821
822       timer_push(TV_LOCAL_OPT);
823         optimize_graph_df(current_ir_graph);
824       timer_pop();
825       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
826       if (! firm_opt.code_place)
827         CHECK_ONE(firm_opt.check_all, current_ir_graph);
828
829       if (firm_opt.code_place) {
830         timer_push(TV_CODE_PLACE);
831           place_code(current_ir_graph);
832           set_opt_global_cse(0);
833         timer_pop();
834         DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "place");
835         CHECK_ONE(firm_opt.check_all, current_ir_graph);
836       }
837
838 //      set_opt_global_cse(0);
839       timer_push(TV_LOAD_STORE);
840         optimize_load_store(current_ir_graph);
841       timer_pop();
842       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "ldst");
843       CHECK_ONE(firm_opt.check_all, current_ir_graph);
844
845
846       timer_push(TV_LOCAL_OPT);
847         optimize_graph_df(current_ir_graph);
848       timer_pop();
849       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
850
851       timer_push(TV_CF_OPT);
852         optimize_cf(current_ir_graph);
853       timer_pop();
854       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "cf");
855       CHECK_ONE(firm_opt.check_all, current_ir_graph);
856
857       if (firm_opt.if_conversion) {
858         timer_push(TV_IF_CONV);
859           opt_if_conv(current_ir_graph, if_conv_info);
860         timer_pop();
861         DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "if");
862         CHECK_ONE(firm_opt.check_all, current_ir_graph);
863
864         timer_push(TV_LOCAL_OPT);
865           optimize_graph_df(current_ir_graph);
866         timer_pop();
867         timer_push(TV_CF_OPT);
868           optimize_cf(current_ir_graph);
869         timer_pop();
870         DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "after_if");
871         CHECK_ONE(firm_opt.check_all, current_ir_graph);
872       }
873     }
874     timer_stop(TV_ALL_OPT);
875
876     DUMP_ALL(firm_dump.ir_graph, "-low-opt");
877   }
878
879   if (firm_opt.cc_opt)
880     mark_private_methods();
881
882   /* set the phase to low */
883   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
884     set_irg_phase_low(get_irp_irg(i));
885
886   /* all graphs are lowered, set the irp phase to low */
887   set_irp_phase_state(phase_low);
888
889   if (firm_dump.statistic & STAT_FINAL) {
890     stat_dump_snapshot(input_filename, "final");
891   }
892 }  /* do_firm_lowering */
893
894 /**
895  * Initialize for the Firm-generating back end.
896  */
897 void gen_firm_init(void)
898 {
899   firm_parameter_t params;
900   char             *dump_filter;
901   unsigned         pattern = 0;
902
903   /* the automatic state is only set if inlining is enabled */
904   firm_opt.auto_inline = firm_opt.do_inline;
905
906   if (firm_dump.stat_pattern)
907     pattern |= FIRMSTAT_PATTERN_ENABLED;
908
909   if (firm_dump.stat_dag)
910     pattern |= FIRMSTAT_COUNT_DAG;
911
912   memset(&params, 0, sizeof(params));
913   params.size                  = sizeof(params);
914   params.enable_statistics     = firm_dump.statistic == STAT_NONE ? 0 :
915     FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP | FIRMSTAT_COUNT_CONSTS | pattern;
916   params.initialize_local_func = uninitialized_local_var;
917   params.cc_mask               = 0; /* no regparam, cdecl */
918   params.builtin_dbg           = NULL;
919
920 #ifdef FIRM_BACKEND
921   if (firm_be_opt.selection == BE_FIRM_BE) {
922     const backend_params *be_params = be_init();
923
924     firm_opt.lower_ll       = (a_byte) be_params->do_dw_lowering;
925     params.arch_op_settings = be_params->arch_op_settings;
926
927     arch_create_intrinsic   = be_params->arch_create_intrinsic_fkt;
928     create_intrinsic_ctx    = be_params->create_intrinsic_ctx;
929
930     ad_param                = be_params->dep_param;
931     if_conv_info            = be_params->if_conv_info;
932   }
933 #endif /* FIRM_BACKEND */
934
935 #ifdef FIRM_EXT_GRS
936   /* Activate Graph rewriting if SIMD optimization is turned on */
937   /* This has to be done before init_firm() is called! */
938   if (firm_ext_grs.simd_opt)
939           ext_grs_activate();
940 #endif
941
942   init_firm(&params);
943   dbg_init(NULL, NULL, dbg_snprint);
944   edges_init_dbg(firm_opt.vrfy_edges);
945   //cbackend_set_debug_retrieve(dbg_retrieve);
946
947   set_opt_precise_exc_context(firm_opt.precise_exc);
948   set_opt_fragile_ops(firm_opt.fragile_ops);
949
950   /* dynamic dispatch works currently only if whole world scenarios */
951   set_opt_dyn_meth_dispatch(0);
952
953   arch_dep_init(arch_factory);
954
955   /* do not run architecture dependent optimizations in building phase */
956   arch_dep_set_opts(arch_dep_none);
957
958   do_node_verification((firm_verification_t) firm_opt.vrfy);
959   if (firm_dump.filter)
960     only_dump_method_with_name(new_id_from_str(firm_dump.filter));
961
962   if (firm_opt.enabled) {
963     set_optimize(1);
964     set_opt_constant_folding(firm_opt.const_folding);
965     set_opt_cse(firm_opt.cse);
966     set_opt_global_cse (0);
967     set_opt_unreachable_code(1);
968     set_opt_control_flow(firm_opt.control_flow);
969     set_opt_control_flow_weak_simplification(1);
970     set_opt_control_flow_strong_simplification(1);
971   }
972   else
973     set_optimize(0);
974
975   dump_filter = getenv("FIRM_DUMP_FILTER");
976   if (dump_filter)
977     only_dump_method_with_name(new_id_from_str(dump_filter));
978
979   /* do not dump entity ld names */
980   dump_ld_names(0);
981
982 #if 0
983   /* init the ycomp debugger extension */
984   if (firm_opt.ycomp_dbg)
985     firm_init_ycomp_debugger(firm_opt.ycomp_host, firm_opt.ycomp_port);
986 #endif
987 }  /* gen_firm_init */
988
989 /**
990  * Called, after the Firm generation is completed,
991  * do all optimizations and backend call here.
992  *
993  * @param out                a file handle for the output, may be NULL
994  * @param input_filename     the name of the (main) source file
995  * @param c_mode             non-zero if "C" was compiled
996  * @param firm_const_exists  non-zero, if the const attribute was used on functions
997  */
998 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, int firm_const_exists)
999 {
1000   int i;
1001
1002   /* the general for dumping option must be set, or the others will not work */
1003   firm_dump.ir_graph
1004       = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
1005
1006   dump_keepalive_edges(1);
1007   dump_consts_local(1);
1008   dump_dominator_information(1);
1009   dump_loop_information(0);
1010
1011   if (!firm_dump.edge_labels)
1012     turn_off_edge_labels();
1013
1014   if (firm_dump.all_types) {
1015     dump_all_types("");
1016     if (! c_mode) {
1017       dump_class_hierarchy(0, "");
1018       dump_class_hierarchy(1, "-with-entities");
1019     }
1020   }
1021
1022   /* finalize all graphs */
1023   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1024     ir_graph *irg = get_irp_irg(i);
1025
1026     irg_finalize_cons(irg);
1027     DUMP_ONE(firm_dump.ir_graph, irg, "");
1028
1029     /* verify the graph */
1030     timer_push(TV_VERIFY);
1031       irg_verify(irg, VRFY_ENFORCE_SSA);
1032     timer_pop();
1033   }
1034
1035   timer_push(TV_VERIFY);
1036     tr_vrfy();
1037   timer_pop();
1038
1039   /* all graphs are finalized, set the irp phase to high */
1040   set_irp_phase_state(phase_high);
1041
1042   /* lower all compound call return values */
1043   lower_compound_params();
1044
1045   /* computes the sizes of all types that are still not computed */
1046   compute_type_sizes();
1047
1048   if (firm_dump.statistic & STAT_BEFORE_OPT) {
1049     stat_dump_snapshot(input_filename, "noopt");
1050   }
1051
1052   if (firm_opt.enabled)
1053     do_firm_optimizations(input_filename, firm_const_exists);
1054
1055   if (firm_dump.gen_firm_asm) {
1056     timer_push(TV_FIRM_ASM);
1057       gen_Firm_assembler(input_filename);
1058     timer_pop();
1059     return;
1060   }
1061
1062   if (firm_opt.lower)
1063     do_firm_lowering(input_filename);
1064
1065   /* set the phase to low */
1066   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
1067     set_irg_phase_low(get_irp_irg(i));
1068
1069
1070 #ifdef FIRM_EXT_GRS
1071   /** SIMD Optimization  Extensions **/
1072
1073   /* Pattern creation step. No code has to be generated, so
1074      exit after pattern creation */
1075   if (firm_ext_grs.create_pattern) {
1076     ext_grs_create_pattern();
1077     exit(0);
1078   }
1079
1080   /* SIMD optimization step. Uses graph patterns to find
1081      rich instructions and rewrite */
1082   if (firm_ext_grs.simd_opt)
1083     ext_grs_simd_opt();
1084 #endif
1085
1086   /* enable architecture dependent optimizations */
1087   arch_dep_set_opts((arch_dep_opts_t)
1088                     ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
1089                     (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
1090                     (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
1091
1092
1093   if (firm_dump.statistic & STAT_FINAL_IR)
1094     stat_dump_snapshot(input_filename, "final-ir");
1095
1096   /* run the code generator */
1097   if (firm_be_opt.selection != BE_NONE)
1098     do_codegen(out, input_filename);
1099
1100   if (firm_dump.statistic & STAT_FINAL)
1101     stat_dump_snapshot(input_filename, "final");
1102
1103 #if 0
1104   if (firm_opt.ycomp_dbg)
1105     firm_finish_ycomp_debugger();
1106 #endif
1107 }  /* gen_firm_finish */
1108
1109 /**
1110  * Do very early initializations
1111  */
1112 void firm_early_init(void) {
1113 #ifdef FIRM_BACKEND
1114   /* arg: need this here for command line options */
1115   be_opt_register();
1116 #endif
1117   firm_init_options(NULL, 0, NULL);
1118 }  /* firm_early_init */