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