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