more tricky test
[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   irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
292
293   set_opt_strength_red(firm_opt.strength_red);
294   set_opt_scalar_replacement(firm_opt.scalar_replace);
295   set_opt_auto_create_sync(firm_opt.auto_sync);
296   set_opt_alias_analysis(firm_opt.alias_analysis);
297
298   aa_opt = aa_opt_no_opt;
299   if (firm_opt.strict_alias)
300     aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
301   if (firm_opt.no_alias)
302     aa_opt = aa_opt_no_alias;
303
304   set_irp_memory_disambiguator_options(aa_opt);
305
306   timer_start(TV_ALL_OPT);
307
308   if (firm_opt.remove_unused) {
309         ir_entity **keep_methods;
310         int         arr_len;
311
312     /* Analysis that finds the free methods,
313        i.e. methods that are dereferenced.
314        Optimizes polymorphic calls :-). */
315     cgana(&arr_len, &keep_methods);
316
317     /* Remove methods that are never called. */
318     gc_irgs(arr_len, keep_methods);
319
320     free(keep_methods);
321   }
322
323   if (! firm_opt.freestanding) {
324     rts_map();
325     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "rts");
326     CHECK_ALL(firm_opt.check_all);
327   }
328
329   if (firm_opt.tail_rec) {
330     timer_push(TV_TAIL_REC);
331       opt_tail_recursion();
332     timer_pop();
333
334     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "tail_rec");
335     CHECK_ALL(firm_opt.check_all);
336   }
337
338   if (firm_opt.func_calls) {
339     timer_push(TV_REAL_FUNC_CALL);
340       optimize_funccalls(firm_const_exists);
341     timer_pop();
342     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "func_call");
343     CHECK_ALL(firm_opt.check_all);
344   }
345
346   if (firm_opt.do_inline) {
347     timer_push(TV_INLINE);
348       inline_leave_functions(500, 80, 30, FALSE);
349     timer_pop();
350     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "inl");
351     CHECK_ALL(firm_opt.check_all);
352   }
353
354   for (i = 0; i < get_irp_n_irgs(); i++) {
355     irg = current_ir_graph = get_irp_irg(i);
356
357
358 #ifdef FIRM_EXT_GRS
359   /* If SIMD optimization is on, make sure we have only 1 return */
360   if (firm_ext_grs.create_pattern || firm_ext_grs.simd_opt)
361     normalize_one_return(irg);
362 #endif
363
364
365 #if 0
366     if (firm_opt.modes) {
367       /* convert all modes into integer if possible */
368       arch_mode_conversion(irg, predefs.mode_uint);
369     }
370 #endif
371     timer_push(TV_SCALAR_REPLACE);
372       scalar_replacement_opt(irg);
373     timer_pop();
374     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "scalar");
375     CHECK_ONE(firm_opt.check_all, irg);
376
377     timer_push(TV_LOCAL_OPT);
378       optimize_graph_df(irg);
379     timer_pop();
380     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
381     CHECK_ONE(firm_opt.check_all, irg);
382
383     timer_push(TV_REASSOCIATION);
384       optimize_reassociation(irg);
385     timer_pop();
386     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "reassoc");
387     CHECK_ONE(firm_opt.check_all, irg);
388
389     if (firm_opt.confirm) {
390       /* Confirm construction currently can only handle blocks with only one control
391          flow predecessor. Calling optimize_cf here removes Bad predecessors and help
392          the optimization of switch constructs. */
393       timer_push(TV_CF_OPT);
394         optimize_cf(irg);
395       timer_pop();
396       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
397       CHECK_ONE(firm_opt.check_all, irg);
398       timer_push(TV_CONFIRM_CREATE);
399         construct_confirms(irg);
400       timer_pop();
401       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "confirms");
402       CHECK_ONE(firm_opt.check_all, irg);
403     }
404
405     timer_push(TV_LOCAL_OPT);
406       optimize_graph_df(irg);
407     timer_pop();
408     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
409     CHECK_ONE(firm_opt.check_all, irg);
410
411     compute_doms(irg);
412     CHECK_ONE(firm_opt.check_all, irg);
413
414     if (firm_opt.code_place) {
415       timer_push(TV_CODE_PLACE);
416         set_opt_global_cse(1);
417         optimize_graph_df(irg);
418         place_code(irg);
419         set_opt_global_cse(0);
420       timer_pop();
421       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "place");
422       CHECK_ONE(firm_opt.check_all, irg);
423     }
424
425     if (firm_opt.luffig) {
426       opt_ldst2(irg);
427       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst2");
428     }
429
430     timer_push(TV_CF_OPT);
431       optimize_cf(irg);
432     timer_pop();
433     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
434     CHECK_ONE(firm_opt.check_all, irg);
435
436     /* should we really remove the Confirm here? */
437     if (firm_opt.confirm) {
438       timer_push(TV_CONFIRM_CREATE);
439         remove_confirms(irg);
440       timer_pop();
441     }
442
443     irg_verify(irg, VRFY_ENFORCE_SSA);
444     if (firm_opt.gvn_pre) {
445       do_gvn_pre(irg);
446       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "pre");
447       CHECK_ONE(firm_opt.check_all, irg);
448       irg_verify(irg, VRFY_ENFORCE_SSA);
449     }
450
451     if (firm_opt.loop_unrolling) {
452       timer_push(TV_LOOP_UNROLL);
453         optimize_loop_unrolling(irg);
454       timer_pop();
455       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "loop");
456       CHECK_ONE(firm_opt.check_all, irg);
457         }
458
459     if (firm_opt.load_store) {
460       timer_push(TV_LOAD_STORE);
461         optimize_load_store(irg);
462       timer_pop();
463       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst");
464       CHECK_ONE(firm_opt.check_all, irg);
465         }
466
467     lower_highlevel_graph(irg, firm_opt.lower_bitfields);
468
469     if (firm_opt.deconv) {
470       timer_push(TV_DECONV);
471         conv_opt(irg);
472       timer_pop();
473       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "deconv");
474       CHECK_ONE(firm_opt.check_all, irg);
475     }
476
477     if (firm_opt.cond_eval) {
478       timer_push(TV_COND_EVAL);
479         opt_cond_eval(irg);
480       timer_pop();
481       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cond_eval");
482       CHECK_ONE(firm_opt.check_all, irg);
483     }
484
485     compute_doms(irg);
486     compute_postdoms(irg);
487     DUMP_ONE_CFG_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dom");
488     CHECK_ONE(firm_opt.check_all, irg);
489
490     construct_backedges(irg);
491
492     timer_push(TV_CF_OPT);
493       optimize_cf(irg);
494     timer_pop();
495     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
496     CHECK_ONE(firm_opt.check_all, irg);
497
498     if (firm_opt.if_conversion) {
499       timer_push(TV_IF_CONV);
500         opt_if_conv(current_ir_graph, if_conv_info);
501       timer_pop();
502       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "if");
503       CHECK_ONE(firm_opt.check_all, current_ir_graph);
504
505       timer_push(TV_LOCAL_OPT);
506         optimize_graph_df(current_ir_graph);
507       timer_pop();
508       timer_push(TV_CF_OPT);
509         optimize_cf(current_ir_graph);
510       timer_pop();
511       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "after_if");
512       CHECK_ONE(firm_opt.check_all, current_ir_graph);
513     }
514
515     if (firm_opt.bool_opt) {
516       opt_bool(irg);
517       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "bool");
518       CHECK_ONE(firm_opt.check_all, irg);
519     }
520
521     timer_push(TV_OSR);
522       opt_osr(current_ir_graph, osr_flag_default /*| osr_flag_ignore_x86_shift*/);
523     timer_pop();
524     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "stred");
525     CHECK_ONE(firm_opt.check_all, irg);
526
527     timer_push(TV_LOCAL_OPT);
528       optimize_graph_df(irg);
529     timer_pop();
530     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
531     CHECK_ONE(firm_opt.check_all, irg);
532
533     edges_deactivate(irg);
534     timer_push(TV_DEAD_NODE);
535       dead_node_elimination(irg);
536     timer_pop();
537     DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dead");
538     CHECK_ONE(firm_opt.check_all, irg);
539   }
540
541   if (firm_opt.cloning) {
542     proc_cloning((float)firm_opt.clone_threshold);
543     DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "clone");
544   }
545
546   if (firm_dump.ir_graph) {
547     /* recompute backedges for nicer dumps */
548     for (i = 0; i < get_irp_n_irgs(); i++)
549       construct_cf_backedges(get_irp_irg(i));
550   }
551
552   if (firm_opt.remove_unused) {
553     ir_entity **keep_methods;
554     int arr_len;
555
556     /* Analysis that finds the free methods,
557        i.e. methods that are dereferenced.
558        Optimizes polymorphic calls :-). */
559     cgana(&arr_len, &keep_methods);
560
561     /* Remove methods that are never called. */
562     gc_irgs(arr_len, keep_methods);
563
564     free(keep_methods);
565   }
566
567
568   DUMP_ALL(firm_dump.ir_graph, "-opt");
569
570   /* verify optimized graphs */
571   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
572     irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
573
574   if (firm_dump.statistic & STAT_AFTER_OPT)
575     stat_dump_snapshot(input_filename, "opt");
576
577   timer_stop(TV_ALL_OPT);
578 }  /* do_firm_optimizations */
579
580 /**
581  * compute the size of a type (do implicit lowering)
582  *
583  * @param ty   a Firm type
584  */
585 static int compute_type_size(ir_type *ty)
586 {
587   optimization_state_t state;
588   unsigned             align_all = 1;
589   int                  n, size = 0, set = 0;
590   int                  i, dims, s;
591
592   if (get_type_state(ty) == layout_fixed) {
593     /* do not layout already layouted types again */
594     return 1;
595   }
596
597   if (is_Method_type(ty) || ty == get_glob_type()) {
598     /* no need for size calculation for method types or the global type */
599     return 1;
600   }
601
602   DBG(("compute type size visiting: %s\n", get_type_name(ty)));
603
604   switch (get_type_tpop_code(ty)) {
605   case tpo_class:
606   case tpo_struct:
607     for (i = 0, n = get_compound_n_members(ty); i < n; ++i) {
608       ir_entity *ent  = get_compound_member(ty, i);
609       ir_type *ent_ty = get_entity_type(ent);
610       unsigned align, misalign;
611
612       /* compute member types */
613       if (! compute_type_size(ent_ty))
614         return 0;
615
616       align     = get_type_alignment_bytes(ent_ty);
617       align_all = align > align_all ? align : align_all;
618       misalign  = (align ? size % align : 0);
619       size     += (misalign ? align - misalign : 0);
620
621       set_entity_offset(ent, size);
622       size += get_type_size_bytes(ent_ty);
623
624       DBG(("  member %s %s -> (size: %u, align: %u)\n",
625             get_type_name(ent_ty), get_entity_name(ent),
626             get_type_size_bytes(ent_ty), get_type_alignment_bytes(ent_ty)));
627     }
628     if (align_all > 0 && size % align_all) {
629        DBG(("align of the struct member: %u, type size: %d\n", align_all, size));
630        size += align_all - (size % align_all);
631        DBG(("correcting type-size to %d\n", size));
632     }
633     set_type_alignment_bytes(ty, align_all);
634     set = 1;
635     break;
636
637   case tpo_union:
638     for (i = 0, n = get_union_n_members(ty); i < n; ++i) {
639       ir_entity *ent = get_union_member(ty, i);
640
641       if (! compute_type_size(get_entity_type(ent)))
642         return 0;
643       s = get_type_size_bytes(get_entity_type(ent));
644
645       set_entity_offset(ent, 0);
646       size = (s > size ? s : size);
647     }
648     set = 1;
649     break;
650
651   case tpo_array:
652     dims = get_array_n_dimensions(ty);
653
654     if (! compute_type_size(get_array_element_type(ty)))
655       return 0;
656
657     size = 1;
658
659     save_optimization_state(&state);
660     set_optimize(1);
661     set_opt_constant_folding(1);
662
663     for (i = 0; i < dims; ++i) {
664       ir_node *lower   = get_array_lower_bound(ty, i);
665       ir_node *upper   = get_array_upper_bound(ty, i);
666       ir_graph *rem    = current_ir_graph;
667       tarval  *tv_lower, *tv_upper;
668       long     val_lower, val_upper;
669
670       current_ir_graph = get_const_code_irg();
671       local_optimize_node(lower);
672       local_optimize_node(upper);
673       current_ir_graph = rem;
674
675       tv_lower = computed_value(lower);
676       tv_upper = computed_value(upper);
677
678       if (tv_lower == tarval_bad || tv_upper == tarval_bad) {
679         /*
680          * we cannot calculate the size of this array yet, it
681          * even might be unknown until the end, like argv[]
682          */
683         restore_optimization_state(&state);
684         return 0;
685       }
686
687       val_upper = get_tarval_long(tv_upper);
688       val_lower = get_tarval_long(tv_lower);
689       size     *= val_upper - val_lower;
690     }
691     restore_optimization_state(&state);
692
693     DBG(("array %s -> (elements: %d, element type size: %d)\n",
694           get_type_name(ty),
695           size, get_type_size_bytes(get_array_element_type(ty))));
696     size *= get_type_size_bytes(get_array_element_type(ty));
697     set = 1;
698     break;
699
700   default:
701     break;
702   }
703
704   if (set) {
705     set_type_size_bytes(ty, size);
706     set_type_state(ty, layout_fixed);
707   }
708
709   DBG(("size: %d\n", get_type_size_bytes(ty)));
710
711   return set;
712 }  /* compute_type_size */
713
714 /**
715  * layout all types of the Firm graph
716  */
717 static void compute_type_sizes(void)
718 {
719   int i;
720   ir_type *tp;
721   ir_graph *irg;
722
723   /* all frame types */
724   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
725     irg = get_irp_irg(i);
726     /* do not optimize away variables in debug mode */
727     if (firm_opt.debug_mode == DBG_MODE_NONE)
728       opt_frame_irg(irg);
729     compute_type_size(get_irg_frame_type(irg));
730   }
731
732   /* all other types */
733   for (i = get_irp_n_types() - 1; i >= 0; --i) {
734     tp = get_irp_type(i);
735     compute_type_size(tp);
736
737     if (is_Method_type(tp)) {
738       tp = get_method_value_res_type(tp);
739
740       if (tp) {
741         /* we have a value result type for this method, lower */
742         compute_type_size(tp);
743       }
744     }
745   }
746 }  /* compute_type_sizes */
747
748 /**
749  * do Firm lowering
750  *
751  * @param input_filename  the name of the (main) source file
752  */
753 static void do_firm_lowering(const char *input_filename)
754 {
755   int i;
756
757   /* do class lowering and vtbl creation */
758 //  lower_classes_to_struct("vtbl", "m");
759
760 #if 0
761   timer_push(TV_LOWER);
762   lower_highlevel();
763   timer_pop();
764 #endif
765
766   if (firm_opt.lower_ll) {
767     lwrdw_param_t init = {
768       1,
769       1,
770       mode_Ls, mode_Lu,
771       mode_Is, mode_Iu,
772       def_create_intrinsic_fkt,
773       NULL
774     };
775
776
777     if (arch_create_intrinsic) {
778       init.create_intrinsic = arch_create_intrinsic;
779       init.ctx              = create_intrinsic_ctx;
780     }
781     timer_push(TV_DW_LOWER);
782       lower_dw_ops(&init);
783       DUMP_ALL(firm_dump.ir_graph, "-dw");
784     timer_pop();
785   }
786
787   if (firm_dump.statistic & STAT_AFTER_LOWER)
788     stat_dump_snapshot(input_filename, "low");
789
790   /* verify lowered graphs */
791   timer_push(TV_VERIFY);
792   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
793     irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
794   timer_pop();
795
796   DUMP_ALL(firm_dump.ir_graph, "-low");
797
798   if (firm_opt.enabled) {
799     timer_start(TV_ALL_OPT);
800
801     /* run reassociation first on all graphs BEFORE the architecture dependent optimizations
802        are enabled */
803     for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
804       current_ir_graph = get_irp_irg(i);
805
806       timer_push(TV_REASSOCIATION);
807         optimize_reassociation(current_ir_graph);
808       timer_pop();
809       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "reassoc");
810       CHECK_ONE(firm_opt.check_all, current_ir_graph);
811     }
812
813     for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
814       current_ir_graph = get_irp_irg(i);
815
816       if (firm_opt.code_place)
817         set_opt_global_cse(1);
818
819       timer_push(TV_LOCAL_OPT);
820         optimize_graph_df(current_ir_graph);
821       timer_pop();
822       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
823       if (! firm_opt.code_place)
824         CHECK_ONE(firm_opt.check_all, current_ir_graph);
825
826       if (firm_opt.code_place) {
827         timer_push(TV_CODE_PLACE);
828           place_code(current_ir_graph);
829           set_opt_global_cse(0);
830         timer_pop();
831         DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "place");
832         CHECK_ONE(firm_opt.check_all, current_ir_graph);
833       }
834
835 //      set_opt_global_cse(0);
836       timer_push(TV_LOAD_STORE);
837         optimize_load_store(current_ir_graph);
838       timer_pop();
839       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "ldst");
840       CHECK_ONE(firm_opt.check_all, current_ir_graph);
841
842
843       timer_push(TV_LOCAL_OPT);
844         optimize_graph_df(current_ir_graph);
845       timer_pop();
846       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
847
848       timer_push(TV_CF_OPT);
849         optimize_cf(current_ir_graph);
850       timer_pop();
851       DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "cf");
852       CHECK_ONE(firm_opt.check_all, current_ir_graph);
853
854       if (firm_opt.if_conversion) {
855         timer_push(TV_IF_CONV);
856           opt_if_conv(current_ir_graph, if_conv_info);
857         timer_pop();
858         DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "if");
859         CHECK_ONE(firm_opt.check_all, current_ir_graph);
860
861         timer_push(TV_LOCAL_OPT);
862           optimize_graph_df(current_ir_graph);
863         timer_pop();
864         timer_push(TV_CF_OPT);
865           optimize_cf(current_ir_graph);
866         timer_pop();
867         DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "after_if");
868         CHECK_ONE(firm_opt.check_all, current_ir_graph);
869       }
870     }
871     timer_stop(TV_ALL_OPT);
872
873     DUMP_ALL(firm_dump.ir_graph, "-low-opt");
874   }
875
876   if (firm_opt.cc_opt)
877     mark_private_methods();
878
879   /* set the phase to low */
880   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
881     set_irg_phase_low(get_irp_irg(i));
882
883   /* all graphs are lowered, set the irp phase to low */
884   set_irp_phase_state(phase_low);
885
886   if (firm_dump.statistic & STAT_FINAL) {
887     stat_dump_snapshot(input_filename, "final");
888   }
889 }  /* do_firm_lowering */
890
891 /**
892  * Initialize for the Firm-generating back end.
893  */
894 void gen_firm_init(void)
895 {
896   firm_parameter_t params;
897   char             *dump_filter;
898   unsigned         pattern = 0;
899
900   /* the automatic state is only set if inlining is enabled */
901   firm_opt.auto_inline = firm_opt.do_inline;
902
903   if (firm_dump.stat_pattern)
904     pattern |= FIRMSTAT_PATTERN_ENABLED;
905
906   if (firm_dump.stat_dag)
907     pattern |= FIRMSTAT_COUNT_DAG;
908
909   memset(&params, 0, sizeof(params));
910   params.size                  = sizeof(params);
911   params.enable_statistics     = firm_dump.statistic == STAT_NONE ? 0 :
912     FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP | FIRMSTAT_COUNT_CONSTS | pattern;
913   params.initialize_local_func = uninitialized_local_var;
914   params.cc_mask               = 0; /* no regparam, cdecl */
915   params.builtin_dbg           = NULL;
916
917 #ifdef FIRM_BACKEND
918   if (firm_be_opt.selection == BE_FIRM_BE) {
919     const backend_params *be_params = be_init();
920
921     firm_opt.lower_ll       = (a_byte) be_params->do_dw_lowering;
922     params.arch_op_settings = be_params->arch_op_settings;
923
924     arch_create_intrinsic   = be_params->arch_create_intrinsic_fkt;
925     create_intrinsic_ctx    = be_params->create_intrinsic_ctx;
926
927     ad_param                = be_params->dep_param;
928     if_conv_info            = be_params->if_conv_info;
929   }
930 #endif /* FIRM_BACKEND */
931
932 #ifdef FIRM_EXT_GRS
933   /* Activate Graph rewriting if SIMD optimization is turned on */
934   /* This has to be done before init_firm() is called! */
935   if (firm_ext_grs.simd_opt)
936           ext_grs_activate();
937 #endif
938
939   init_firm(&params);
940   dbg_init(NULL, NULL, dbg_snprint);
941   edges_init_dbg(firm_opt.vrfy_edges);
942   //cbackend_set_debug_retrieve(dbg_retrieve);
943
944   set_opt_precise_exc_context(firm_opt.precise_exc);
945   set_opt_fragile_ops(firm_opt.fragile_ops);
946
947   /* dynamic dispatch works currently only if whole world scenarios */
948   set_opt_dyn_meth_dispatch(0);
949
950   arch_dep_init(arch_factory);
951
952   /* do not run architecture dependent optimizations in building phase */
953   arch_dep_set_opts(arch_dep_none);
954
955   do_node_verification((firm_verification_t) firm_opt.vrfy);
956   if (firm_dump.filter)
957     only_dump_method_with_name(new_id_from_str(firm_dump.filter));
958
959   if (firm_opt.enabled) {
960     set_optimize(1);
961     set_opt_constant_folding(firm_opt.const_folding);
962     set_opt_cse(firm_opt.cse);
963     set_opt_global_cse (0);
964     set_opt_unreachable_code(1);
965     set_opt_control_flow(firm_opt.control_flow);
966     set_opt_control_flow_weak_simplification(1);
967     set_opt_control_flow_strong_simplification(1);
968   }
969   else
970     set_optimize(0);
971
972   dump_filter = getenv("FIRM_DUMP_FILTER");
973   if (dump_filter)
974     only_dump_method_with_name(new_id_from_str(dump_filter));
975
976   /* do not dump entity ld names */
977   dump_ld_names(0);
978
979 #if 0
980   /* init the ycomp debugger extension */
981   if (firm_opt.ycomp_dbg)
982     firm_init_ycomp_debugger(firm_opt.ycomp_host, firm_opt.ycomp_port);
983 #endif
984 }  /* gen_firm_init */
985
986 /**
987  * Called, after the Firm generation is completed,
988  * do all optimizations and backend call here.
989  *
990  * @param out                a file handle for the output, may be NULL
991  * @param input_filename     the name of the (main) source file
992  * @param c_mode             non-zero if "C" was compiled
993  * @param firm_const_exists  non-zero, if the const attribute was used on functions
994  */
995 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, int firm_const_exists)
996 {
997   int i;
998
999   /* the general for dumping option must be set, or the others will not work */
1000   firm_dump.ir_graph
1001       = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
1002
1003   dump_keepalive_edges(1);
1004   dump_consts_local(1);
1005   dump_dominator_information(1);
1006   dump_loop_information(0);
1007
1008   if (!firm_dump.edge_labels)
1009     turn_off_edge_labels();
1010
1011   if (firm_dump.all_types) {
1012     dump_all_types("");
1013     if (! c_mode) {
1014       dump_class_hierarchy(0, "");
1015       dump_class_hierarchy(1, "-with-entities");
1016     }
1017   }
1018
1019   /* finalize all graphs */
1020   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1021     ir_graph *irg = get_irp_irg(i);
1022
1023     irg_finalize_cons(irg);
1024     DUMP_ONE(firm_dump.ir_graph, irg, "");
1025
1026     /* verify the graph */
1027     timer_push(TV_VERIFY);
1028       irg_verify(irg, VRFY_ENFORCE_SSA);
1029     timer_pop();
1030   }
1031
1032   timer_push(TV_VERIFY);
1033     tr_vrfy();
1034   timer_pop();
1035
1036   /* all graphs are finalized, set the irp phase to high */
1037   set_irp_phase_state(phase_high);
1038
1039   /* lower all compound call return values */
1040   lower_compound_params();
1041
1042   /* computes the sizes of all types that are still not computed */
1043   compute_type_sizes();
1044
1045   if (firm_dump.statistic & STAT_BEFORE_OPT) {
1046     stat_dump_snapshot(input_filename, "noopt");
1047   }
1048
1049   if (firm_opt.enabled)
1050     do_firm_optimizations(input_filename, firm_const_exists);
1051
1052   if (firm_dump.gen_firm_asm) {
1053     timer_push(TV_FIRM_ASM);
1054       gen_Firm_assembler(input_filename);
1055     timer_pop();
1056     return;
1057   }
1058
1059   if (firm_opt.lower)
1060     do_firm_lowering(input_filename);
1061
1062   /* set the phase to low */
1063   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
1064     set_irg_phase_low(get_irp_irg(i));
1065
1066
1067 #ifdef FIRM_EXT_GRS
1068   /** SIMD Optimization  Extensions **/
1069
1070   /* Pattern creation step. No code has to be generated, so
1071      exit after pattern creation */
1072   if (firm_ext_grs.create_pattern) {
1073     ext_grs_create_pattern();
1074     exit(0);
1075   }
1076
1077   /* SIMD optimization step. Uses graph patterns to find
1078      rich instructions and rewrite */
1079   if (firm_ext_grs.simd_opt)
1080     ext_grs_simd_opt();
1081 #endif
1082
1083   /* enable architecture dependent optimizations */
1084   arch_dep_set_opts((arch_dep_opts_t)
1085                     ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
1086                     (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
1087                     (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
1088
1089
1090   if (firm_dump.statistic & STAT_FINAL_IR)
1091     stat_dump_snapshot(input_filename, "final-ir");
1092
1093   /* run the code generator */
1094   if (firm_be_opt.selection != BE_NONE)
1095     do_codegen(out, input_filename);
1096
1097   if (firm_dump.statistic & STAT_FINAL)
1098     stat_dump_snapshot(input_filename, "final");
1099
1100 #if 0
1101   if (firm_opt.ycomp_dbg)
1102     firm_finish_ycomp_debugger();
1103 #endif
1104 }  /* gen_firm_finish */
1105
1106 /**
1107  * Do very early initializations
1108  */
1109 void firm_early_init(void) {
1110 #ifdef FIRM_BACKEND
1111   /* arg: need this here for command line options */
1112   be_opt_register();
1113 #endif
1114   firm_init_options(NULL, 0, NULL);
1115 }  /* firm_early_init */